blob: b5a6d685c6cec7a6c8e5e4131f42c999a6578255 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010032static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033static char *e_stringreq = N_("E928: String required");
Bram Moolenaaraff74912019-03-30 18:11:49 +010034static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020035
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020066static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010067static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010069static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010070# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010071#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_browse(typval_T *argvars, typval_T *rettv);
73static void f_browsedir(typval_T *argvars, typval_T *rettv);
74static void f_bufexists(typval_T *argvars, typval_T *rettv);
75static void f_buflisted(typval_T *argvars, typval_T *rettv);
76static void f_bufloaded(typval_T *argvars, typval_T *rettv);
77static void f_bufname(typval_T *argvars, typval_T *rettv);
78static void f_bufnr(typval_T *argvars, typval_T *rettv);
79static void f_bufwinid(typval_T *argvars, typval_T *rettv);
80static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
81static void f_byte2line(typval_T *argvars, typval_T *rettv);
82static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
83static void f_byteidx(typval_T *argvars, typval_T *rettv);
84static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
85static void f_call(typval_T *argvars, typval_T *rettv);
86#ifdef FEAT_FLOAT
87static void f_ceil(typval_T *argvars, typval_T *rettv);
88#endif
89#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010090static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020092static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
94static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
95static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
96static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
97static void f_ch_info(typval_T *argvars, typval_T *rettv);
98static void f_ch_log(typval_T *argvars, typval_T *rettv);
99static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
100static void f_ch_open(typval_T *argvars, typval_T *rettv);
101static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100102static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200103static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
105static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
106static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
107static void f_ch_status(typval_T *argvars, typval_T *rettv);
108#endif
109static void f_changenr(typval_T *argvars, typval_T *rettv);
110static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200111static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200112static void f_cindent(typval_T *argvars, typval_T *rettv);
113static void f_clearmatches(typval_T *argvars, typval_T *rettv);
114static void f_col(typval_T *argvars, typval_T *rettv);
115#if defined(FEAT_INS_EXPAND)
116static void f_complete(typval_T *argvars, typval_T *rettv);
117static void f_complete_add(typval_T *argvars, typval_T *rettv);
118static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +0100119static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120#endif
121static void f_confirm(typval_T *argvars, typval_T *rettv);
122static void f_copy(typval_T *argvars, typval_T *rettv);
123#ifdef FEAT_FLOAT
124static void f_cos(typval_T *argvars, typval_T *rettv);
125static void f_cosh(typval_T *argvars, typval_T *rettv);
126#endif
127static void f_count(typval_T *argvars, typval_T *rettv);
128static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
129static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100130#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200131static void f_debugbreak(typval_T *argvars, typval_T *rettv);
132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_deepcopy(typval_T *argvars, typval_T *rettv);
134static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200135static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200136static void f_did_filetype(typval_T *argvars, typval_T *rettv);
137static void f_diff_filler(typval_T *argvars, typval_T *rettv);
138static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
139static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200140static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_escape(typval_T *argvars, typval_T *rettv);
142static void f_eval(typval_T *argvars, typval_T *rettv);
143static void f_eventhandler(typval_T *argvars, typval_T *rettv);
144static void f_executable(typval_T *argvars, typval_T *rettv);
145static void f_execute(typval_T *argvars, typval_T *rettv);
146static void f_exepath(typval_T *argvars, typval_T *rettv);
147static void f_exists(typval_T *argvars, typval_T *rettv);
148#ifdef FEAT_FLOAT
149static void f_exp(typval_T *argvars, typval_T *rettv);
150#endif
151static void f_expand(typval_T *argvars, typval_T *rettv);
152static void f_extend(typval_T *argvars, typval_T *rettv);
153static void f_feedkeys(typval_T *argvars, typval_T *rettv);
154static void f_filereadable(typval_T *argvars, typval_T *rettv);
155static void f_filewritable(typval_T *argvars, typval_T *rettv);
156static void f_filter(typval_T *argvars, typval_T *rettv);
157static void f_finddir(typval_T *argvars, typval_T *rettv);
158static void f_findfile(typval_T *argvars, typval_T *rettv);
159#ifdef FEAT_FLOAT
160static void f_float2nr(typval_T *argvars, typval_T *rettv);
161static void f_floor(typval_T *argvars, typval_T *rettv);
162static void f_fmod(typval_T *argvars, typval_T *rettv);
163#endif
164static void f_fnameescape(typval_T *argvars, typval_T *rettv);
165static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
166static void f_foldclosed(typval_T *argvars, typval_T *rettv);
167static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
168static void f_foldlevel(typval_T *argvars, typval_T *rettv);
169static void f_foldtext(typval_T *argvars, typval_T *rettv);
170static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
171static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200172static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200173static void f_function(typval_T *argvars, typval_T *rettv);
174static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
175static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200176static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_getbufline(typval_T *argvars, typval_T *rettv);
178static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100179static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_getchar(typval_T *argvars, typval_T *rettv);
181static void f_getcharmod(typval_T *argvars, typval_T *rettv);
182static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
183static void f_getcmdline(typval_T *argvars, typval_T *rettv);
184#if defined(FEAT_CMDL_COMPL)
185static void f_getcompletion(typval_T *argvars, typval_T *rettv);
186#endif
187static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
188static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
189static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
190static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200191static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getfontname(typval_T *argvars, typval_T *rettv);
193static void f_getfperm(typval_T *argvars, typval_T *rettv);
194static void f_getfsize(typval_T *argvars, typval_T *rettv);
195static void f_getftime(typval_T *argvars, typval_T *rettv);
196static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100197static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200198static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200199static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_getmatches(typval_T *argvars, typval_T *rettv);
201static void f_getpid(typval_T *argvars, typval_T *rettv);
202static void f_getcurpos(typval_T *argvars, typval_T *rettv);
203static void f_getpos(typval_T *argvars, typval_T *rettv);
204static void f_getqflist(typval_T *argvars, typval_T *rettv);
205static void f_getreg(typval_T *argvars, typval_T *rettv);
206static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200207static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200208static void f_gettabvar(typval_T *argvars, typval_T *rettv);
209static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100210static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200211static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100212static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200213static void f_getwinposx(typval_T *argvars, typval_T *rettv);
214static void f_getwinposy(typval_T *argvars, typval_T *rettv);
215static void f_getwinvar(typval_T *argvars, typval_T *rettv);
216static void f_glob(typval_T *argvars, typval_T *rettv);
217static void f_globpath(typval_T *argvars, typval_T *rettv);
218static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
219static void f_has(typval_T *argvars, typval_T *rettv);
220static void f_has_key(typval_T *argvars, typval_T *rettv);
221static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
222static void f_hasmapto(typval_T *argvars, typval_T *rettv);
223static void f_histadd(typval_T *argvars, typval_T *rettv);
224static void f_histdel(typval_T *argvars, typval_T *rettv);
225static void f_histget(typval_T *argvars, typval_T *rettv);
226static void f_histnr(typval_T *argvars, typval_T *rettv);
227static void f_hlID(typval_T *argvars, typval_T *rettv);
228static void f_hlexists(typval_T *argvars, typval_T *rettv);
229static void f_hostname(typval_T *argvars, typval_T *rettv);
230static void f_iconv(typval_T *argvars, typval_T *rettv);
231static void f_indent(typval_T *argvars, typval_T *rettv);
232static void f_index(typval_T *argvars, typval_T *rettv);
233static void f_input(typval_T *argvars, typval_T *rettv);
234static void f_inputdialog(typval_T *argvars, typval_T *rettv);
235static void f_inputlist(typval_T *argvars, typval_T *rettv);
236static void f_inputrestore(typval_T *argvars, typval_T *rettv);
237static void f_inputsave(typval_T *argvars, typval_T *rettv);
238static void f_inputsecret(typval_T *argvars, typval_T *rettv);
239static void f_insert(typval_T *argvars, typval_T *rettv);
240static void f_invert(typval_T *argvars, typval_T *rettv);
241static void f_isdirectory(typval_T *argvars, typval_T *rettv);
242static void f_islocked(typval_T *argvars, typval_T *rettv);
243#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200244static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_isnan(typval_T *argvars, typval_T *rettv);
246#endif
247static void f_items(typval_T *argvars, typval_T *rettv);
248#ifdef FEAT_JOB_CHANNEL
249static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
250static void f_job_info(typval_T *argvars, typval_T *rettv);
251static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
252static void f_job_start(typval_T *argvars, typval_T *rettv);
253static void f_job_stop(typval_T *argvars, typval_T *rettv);
254static void f_job_status(typval_T *argvars, typval_T *rettv);
255#endif
256static void f_join(typval_T *argvars, typval_T *rettv);
257static void f_js_decode(typval_T *argvars, typval_T *rettv);
258static void f_js_encode(typval_T *argvars, typval_T *rettv);
259static void f_json_decode(typval_T *argvars, typval_T *rettv);
260static void f_json_encode(typval_T *argvars, typval_T *rettv);
261static void f_keys(typval_T *argvars, typval_T *rettv);
262static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
263static void f_len(typval_T *argvars, typval_T *rettv);
264static void f_libcall(typval_T *argvars, typval_T *rettv);
265static void f_libcallnr(typval_T *argvars, typval_T *rettv);
266static void f_line(typval_T *argvars, typval_T *rettv);
267static void f_line2byte(typval_T *argvars, typval_T *rettv);
268static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200269static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200270static void f_localtime(typval_T *argvars, typval_T *rettv);
271#ifdef FEAT_FLOAT
272static void f_log(typval_T *argvars, typval_T *rettv);
273static void f_log10(typval_T *argvars, typval_T *rettv);
274#endif
275#ifdef FEAT_LUA
276static void f_luaeval(typval_T *argvars, typval_T *rettv);
277#endif
278static void f_map(typval_T *argvars, typval_T *rettv);
279static void f_maparg(typval_T *argvars, typval_T *rettv);
280static void f_mapcheck(typval_T *argvars, typval_T *rettv);
281static void f_match(typval_T *argvars, typval_T *rettv);
282static void f_matchadd(typval_T *argvars, typval_T *rettv);
283static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
284static void f_matcharg(typval_T *argvars, typval_T *rettv);
285static void f_matchdelete(typval_T *argvars, typval_T *rettv);
286static void f_matchend(typval_T *argvars, typval_T *rettv);
287static void f_matchlist(typval_T *argvars, typval_T *rettv);
288static void f_matchstr(typval_T *argvars, typval_T *rettv);
289static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
290static void f_max(typval_T *argvars, typval_T *rettv);
291static void f_min(typval_T *argvars, typval_T *rettv);
292#ifdef vim_mkdir
293static void f_mkdir(typval_T *argvars, typval_T *rettv);
294#endif
295static void f_mode(typval_T *argvars, typval_T *rettv);
296#ifdef FEAT_MZSCHEME
297static void f_mzeval(typval_T *argvars, typval_T *rettv);
298#endif
299static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
300static void f_nr2char(typval_T *argvars, typval_T *rettv);
301static void f_or(typval_T *argvars, typval_T *rettv);
302static void f_pathshorten(typval_T *argvars, typval_T *rettv);
303#ifdef FEAT_PERL
304static void f_perleval(typval_T *argvars, typval_T *rettv);
305#endif
306#ifdef FEAT_FLOAT
307static void f_pow(typval_T *argvars, typval_T *rettv);
308#endif
309static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
310static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200311#ifdef FEAT_JOB_CHANNEL
312static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200313static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200314static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
315#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200316static void f_pumvisible(typval_T *argvars, typval_T *rettv);
317#ifdef FEAT_PYTHON3
318static void f_py3eval(typval_T *argvars, typval_T *rettv);
319#endif
320#ifdef FEAT_PYTHON
321static void f_pyeval(typval_T *argvars, typval_T *rettv);
322#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100323#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
324static void f_pyxeval(typval_T *argvars, typval_T *rettv);
325#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200326static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200327static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200328static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200329static void f_reg_executing(typval_T *argvars, typval_T *rettv);
330static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200331static void f_reltime(typval_T *argvars, typval_T *rettv);
332#ifdef FEAT_FLOAT
333static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
334#endif
335static void f_reltimestr(typval_T *argvars, typval_T *rettv);
336static void f_remote_expr(typval_T *argvars, typval_T *rettv);
337static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
338static void f_remote_peek(typval_T *argvars, typval_T *rettv);
339static void f_remote_read(typval_T *argvars, typval_T *rettv);
340static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100341static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200342static void f_remove(typval_T *argvars, typval_T *rettv);
343static void f_rename(typval_T *argvars, typval_T *rettv);
344static void f_repeat(typval_T *argvars, typval_T *rettv);
345static void f_resolve(typval_T *argvars, typval_T *rettv);
346static void f_reverse(typval_T *argvars, typval_T *rettv);
347#ifdef FEAT_FLOAT
348static void f_round(typval_T *argvars, typval_T *rettv);
349#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100350#ifdef FEAT_RUBY
351static void f_rubyeval(typval_T *argvars, typval_T *rettv);
352#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200353static void f_screenattr(typval_T *argvars, typval_T *rettv);
354static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100355static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200356static void f_screencol(typval_T *argvars, typval_T *rettv);
357static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100358static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200359static void f_search(typval_T *argvars, typval_T *rettv);
360static void f_searchdecl(typval_T *argvars, typval_T *rettv);
361static void f_searchpair(typval_T *argvars, typval_T *rettv);
362static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
363static void f_searchpos(typval_T *argvars, typval_T *rettv);
364static void f_server2client(typval_T *argvars, typval_T *rettv);
365static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200366static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200367static void f_setbufvar(typval_T *argvars, typval_T *rettv);
368static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
369static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200370static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200371static void f_setfperm(typval_T *argvars, typval_T *rettv);
372static void f_setline(typval_T *argvars, typval_T *rettv);
373static void f_setloclist(typval_T *argvars, typval_T *rettv);
374static void f_setmatches(typval_T *argvars, typval_T *rettv);
375static void f_setpos(typval_T *argvars, typval_T *rettv);
376static void f_setqflist(typval_T *argvars, typval_T *rettv);
377static void f_setreg(typval_T *argvars, typval_T *rettv);
378static void f_settabvar(typval_T *argvars, typval_T *rettv);
379static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100380static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200381static void f_setwinvar(typval_T *argvars, typval_T *rettv);
382#ifdef FEAT_CRYPT
383static void f_sha256(typval_T *argvars, typval_T *rettv);
384#endif /* FEAT_CRYPT */
385static void f_shellescape(typval_T *argvars, typval_T *rettv);
386static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100387#ifdef FEAT_SIGNS
388static void f_sign_define(typval_T *argvars, typval_T *rettv);
389static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
390static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100391static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100392static void f_sign_place(typval_T *argvars, typval_T *rettv);
393static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
394static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
395#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200396static void f_simplify(typval_T *argvars, typval_T *rettv);
397#ifdef FEAT_FLOAT
398static void f_sin(typval_T *argvars, typval_T *rettv);
399static void f_sinh(typval_T *argvars, typval_T *rettv);
400#endif
401static void f_sort(typval_T *argvars, typval_T *rettv);
402static void f_soundfold(typval_T *argvars, typval_T *rettv);
403static void f_spellbadword(typval_T *argvars, typval_T *rettv);
404static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
405static void f_split(typval_T *argvars, typval_T *rettv);
406#ifdef FEAT_FLOAT
407static void f_sqrt(typval_T *argvars, typval_T *rettv);
408static void f_str2float(typval_T *argvars, typval_T *rettv);
409#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200410static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200411static void f_str2nr(typval_T *argvars, typval_T *rettv);
412static void f_strchars(typval_T *argvars, typval_T *rettv);
413#ifdef HAVE_STRFTIME
414static void f_strftime(typval_T *argvars, typval_T *rettv);
415#endif
416static void f_strgetchar(typval_T *argvars, typval_T *rettv);
417static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200418static void f_strlen(typval_T *argvars, typval_T *rettv);
419static void f_strcharpart(typval_T *argvars, typval_T *rettv);
420static void f_strpart(typval_T *argvars, typval_T *rettv);
421static void f_strridx(typval_T *argvars, typval_T *rettv);
422static void f_strtrans(typval_T *argvars, typval_T *rettv);
423static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
424static void f_strwidth(typval_T *argvars, typval_T *rettv);
425static void f_submatch(typval_T *argvars, typval_T *rettv);
426static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200427static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200428static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200429static void f_synID(typval_T *argvars, typval_T *rettv);
430static void f_synIDattr(typval_T *argvars, typval_T *rettv);
431static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
432static void f_synstack(typval_T *argvars, typval_T *rettv);
433static void f_synconcealed(typval_T *argvars, typval_T *rettv);
434static void f_system(typval_T *argvars, typval_T *rettv);
435static void f_systemlist(typval_T *argvars, typval_T *rettv);
436static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
437static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
438static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
439static void f_taglist(typval_T *argvars, typval_T *rettv);
440static void f_tagfiles(typval_T *argvars, typval_T *rettv);
441static void f_tempname(typval_T *argvars, typval_T *rettv);
442static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
443static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200444static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareda65222019-05-16 20:29:44 +0200445static void f_test_getvalue(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200446static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100447static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100448static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200449static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100450static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100451static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200452#ifdef FEAT_JOB_CHANNEL
453static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
454#endif
455static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
456#ifdef FEAT_JOB_CHANNEL
457static void f_test_null_job(typval_T *argvars, typval_T *rettv);
458#endif
459static void f_test_null_list(typval_T *argvars, typval_T *rettv);
460static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
461static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200462#ifdef FEAT_GUI
463static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
464#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200465#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200466static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200467#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200468static void f_test_settime(typval_T *argvars, typval_T *rettv);
469#ifdef FEAT_FLOAT
470static void f_tan(typval_T *argvars, typval_T *rettv);
471static void f_tanh(typval_T *argvars, typval_T *rettv);
472#endif
473#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200474static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200475static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200476static void f_timer_start(typval_T *argvars, typval_T *rettv);
477static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200478static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200479#endif
480static void f_tolower(typval_T *argvars, typval_T *rettv);
481static void f_toupper(typval_T *argvars, typval_T *rettv);
482static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100483static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200484#ifdef FEAT_FLOAT
485static void f_trunc(typval_T *argvars, typval_T *rettv);
486#endif
487static void f_type(typval_T *argvars, typval_T *rettv);
488static void f_undofile(typval_T *argvars, typval_T *rettv);
489static void f_undotree(typval_T *argvars, typval_T *rettv);
490static void f_uniq(typval_T *argvars, typval_T *rettv);
491static void f_values(typval_T *argvars, typval_T *rettv);
492static void f_virtcol(typval_T *argvars, typval_T *rettv);
493static void f_visualmode(typval_T *argvars, typval_T *rettv);
494static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
495static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
496static void f_win_getid(typval_T *argvars, typval_T *rettv);
497static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
498static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
499static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100500static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200501static void f_winbufnr(typval_T *argvars, typval_T *rettv);
502static void f_wincol(typval_T *argvars, typval_T *rettv);
503static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200504static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505static void f_winline(typval_T *argvars, typval_T *rettv);
506static void f_winnr(typval_T *argvars, typval_T *rettv);
507static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
508static void f_winrestview(typval_T *argvars, typval_T *rettv);
509static void f_winsaveview(typval_T *argvars, typval_T *rettv);
510static void f_winwidth(typval_T *argvars, typval_T *rettv);
511static void f_writefile(typval_T *argvars, typval_T *rettv);
512static void f_wordcount(typval_T *argvars, typval_T *rettv);
513static void f_xor(typval_T *argvars, typval_T *rettv);
514
515/*
516 * Array with names and number of arguments of all internal functions
517 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
518 */
519static struct fst
520{
521 char *f_name; /* function name */
522 char f_min_argc; /* minimal number of arguments */
523 char f_max_argc; /* maximal number of arguments */
524 void (*f_func)(typval_T *args, typval_T *rvar);
525 /* implementation of function */
526} functions[] =
527{
528#ifdef FEAT_FLOAT
529 {"abs", 1, 1, f_abs},
530 {"acos", 1, 1, f_acos}, /* WJMc */
531#endif
532 {"add", 2, 2, f_add},
533 {"and", 2, 2, f_and},
534 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200535 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200536 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200537 {"argidx", 0, 0, f_argidx},
538 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200539 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200540#ifdef FEAT_FLOAT
541 {"asin", 1, 1, f_asin}, /* WJMc */
542#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100543 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200544 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100545 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200546 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200547 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100549 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"assert_match", 2, 3, f_assert_match},
551 {"assert_notequal", 2, 3, f_assert_notequal},
552 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100553 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200554 {"assert_true", 1, 2, f_assert_true},
555#ifdef FEAT_FLOAT
556 {"atan", 1, 1, f_atan},
557 {"atan2", 2, 2, f_atan2},
558#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100559#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200560 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100561 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100562# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100563 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100564# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100565#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200566 {"browse", 4, 4, f_browse},
567 {"browsedir", 2, 2, f_browsedir},
568 {"bufexists", 1, 1, f_bufexists},
569 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
570 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
571 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
572 {"buflisted", 1, 1, f_buflisted},
573 {"bufloaded", 1, 1, f_bufloaded},
574 {"bufname", 1, 1, f_bufname},
575 {"bufnr", 1, 2, f_bufnr},
576 {"bufwinid", 1, 1, f_bufwinid},
577 {"bufwinnr", 1, 1, f_bufwinnr},
578 {"byte2line", 1, 1, f_byte2line},
579 {"byteidx", 2, 2, f_byteidx},
580 {"byteidxcomp", 2, 2, f_byteidxcomp},
581 {"call", 2, 3, f_call},
582#ifdef FEAT_FLOAT
583 {"ceil", 1, 1, f_ceil},
584#endif
585#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100586 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200587 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200588 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
590 {"ch_evalraw", 2, 3, f_ch_evalraw},
591 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
592 {"ch_getjob", 1, 1, f_ch_getjob},
593 {"ch_info", 1, 1, f_ch_info},
594 {"ch_log", 1, 2, f_ch_log},
595 {"ch_logfile", 1, 2, f_ch_logfile},
596 {"ch_open", 1, 2, f_ch_open},
597 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100598 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200599 {"ch_readraw", 1, 2, f_ch_readraw},
600 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
601 {"ch_sendraw", 2, 3, f_ch_sendraw},
602 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200603 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200604#endif
605 {"changenr", 0, 0, f_changenr},
606 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200607 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100609 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610 {"col", 1, 1, f_col},
611#if defined(FEAT_INS_EXPAND)
612 {"complete", 2, 2, f_complete},
613 {"complete_add", 1, 1, f_complete_add},
614 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100615 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200616#endif
617 {"confirm", 1, 4, f_confirm},
618 {"copy", 1, 1, f_copy},
619#ifdef FEAT_FLOAT
620 {"cos", 1, 1, f_cos},
621 {"cosh", 1, 1, f_cosh},
622#endif
623 {"count", 2, 4, f_count},
624 {"cscope_connection",0,3, f_cscope_connection},
625 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100626#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200627 {"debugbreak", 1, 1, f_debugbreak},
628#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200629 {"deepcopy", 1, 2, f_deepcopy},
630 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200631 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"did_filetype", 0, 0, f_did_filetype},
633 {"diff_filler", 1, 1, f_diff_filler},
634 {"diff_hlID", 2, 2, f_diff_hlID},
635 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200636 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200637 {"escape", 2, 2, f_escape},
638 {"eval", 1, 1, f_eval},
639 {"eventhandler", 0, 0, f_eventhandler},
640 {"executable", 1, 1, f_executable},
641 {"execute", 1, 2, f_execute},
642 {"exepath", 1, 1, f_exepath},
643 {"exists", 1, 1, f_exists},
644#ifdef FEAT_FLOAT
645 {"exp", 1, 1, f_exp},
646#endif
647 {"expand", 1, 3, f_expand},
648 {"extend", 2, 3, f_extend},
649 {"feedkeys", 1, 2, f_feedkeys},
650 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
651 {"filereadable", 1, 1, f_filereadable},
652 {"filewritable", 1, 1, f_filewritable},
653 {"filter", 2, 2, f_filter},
654 {"finddir", 1, 3, f_finddir},
655 {"findfile", 1, 3, f_findfile},
656#ifdef FEAT_FLOAT
657 {"float2nr", 1, 1, f_float2nr},
658 {"floor", 1, 1, f_floor},
659 {"fmod", 2, 2, f_fmod},
660#endif
661 {"fnameescape", 1, 1, f_fnameescape},
662 {"fnamemodify", 2, 2, f_fnamemodify},
663 {"foldclosed", 1, 1, f_foldclosed},
664 {"foldclosedend", 1, 1, f_foldclosedend},
665 {"foldlevel", 1, 1, f_foldlevel},
666 {"foldtext", 0, 0, f_foldtext},
667 {"foldtextresult", 1, 1, f_foldtextresult},
668 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200669 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200670 {"function", 1, 3, f_function},
671 {"garbagecollect", 0, 1, f_garbagecollect},
672 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200673 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200674 {"getbufline", 2, 3, f_getbufline},
675 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100676 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200677 {"getchar", 0, 1, f_getchar},
678 {"getcharmod", 0, 0, f_getcharmod},
679 {"getcharsearch", 0, 0, f_getcharsearch},
680 {"getcmdline", 0, 0, f_getcmdline},
681 {"getcmdpos", 0, 0, f_getcmdpos},
682 {"getcmdtype", 0, 0, f_getcmdtype},
683 {"getcmdwintype", 0, 0, f_getcmdwintype},
684#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200685 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200686#endif
687 {"getcurpos", 0, 0, f_getcurpos},
688 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200689 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200690 {"getfontname", 0, 1, f_getfontname},
691 {"getfperm", 1, 1, f_getfperm},
692 {"getfsize", 1, 1, f_getfsize},
693 {"getftime", 1, 1, f_getftime},
694 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100695 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200696 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200697 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100698 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200699 {"getpid", 0, 0, f_getpid},
700 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200701 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702 {"getreg", 0, 3, f_getreg},
703 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200704 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200705 {"gettabvar", 2, 3, f_gettabvar},
706 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100707 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200708 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100709 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200710 {"getwinposx", 0, 0, f_getwinposx},
711 {"getwinposy", 0, 0, f_getwinposy},
712 {"getwinvar", 2, 3, f_getwinvar},
713 {"glob", 1, 4, f_glob},
714 {"glob2regpat", 1, 1, f_glob2regpat},
715 {"globpath", 2, 5, f_globpath},
716 {"has", 1, 1, f_has},
717 {"has_key", 2, 2, f_has_key},
718 {"haslocaldir", 0, 2, f_haslocaldir},
719 {"hasmapto", 1, 3, f_hasmapto},
720 {"highlightID", 1, 1, f_hlID}, /* obsolete */
721 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
722 {"histadd", 2, 2, f_histadd},
723 {"histdel", 1, 2, f_histdel},
724 {"histget", 1, 2, f_histget},
725 {"histnr", 1, 1, f_histnr},
726 {"hlID", 1, 1, f_hlID},
727 {"hlexists", 1, 1, f_hlexists},
728 {"hostname", 0, 0, f_hostname},
729 {"iconv", 3, 3, f_iconv},
730 {"indent", 1, 1, f_indent},
731 {"index", 2, 4, f_index},
732 {"input", 1, 3, f_input},
733 {"inputdialog", 1, 3, f_inputdialog},
734 {"inputlist", 1, 1, f_inputlist},
735 {"inputrestore", 0, 0, f_inputrestore},
736 {"inputsave", 0, 0, f_inputsave},
737 {"inputsecret", 1, 2, f_inputsecret},
738 {"insert", 2, 3, f_insert},
739 {"invert", 1, 1, f_invert},
740 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200741#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
742 {"isinf", 1, 1, f_isinf},
743#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200744 {"islocked", 1, 1, f_islocked},
745#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
746 {"isnan", 1, 1, f_isnan},
747#endif
748 {"items", 1, 1, f_items},
749#ifdef FEAT_JOB_CHANNEL
750 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200751 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200752 {"job_setoptions", 2, 2, f_job_setoptions},
753 {"job_start", 1, 2, f_job_start},
754 {"job_status", 1, 1, f_job_status},
755 {"job_stop", 1, 2, f_job_stop},
756#endif
757 {"join", 1, 2, f_join},
758 {"js_decode", 1, 1, f_js_decode},
759 {"js_encode", 1, 1, f_js_encode},
760 {"json_decode", 1, 1, f_json_decode},
761 {"json_encode", 1, 1, f_json_encode},
762 {"keys", 1, 1, f_keys},
763 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
764 {"len", 1, 1, f_len},
765 {"libcall", 3, 3, f_libcall},
766 {"libcallnr", 3, 3, f_libcallnr},
767 {"line", 1, 1, f_line},
768 {"line2byte", 1, 1, f_line2byte},
769 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200770 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200771 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200772 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200773 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200774 {"localtime", 0, 0, f_localtime},
775#ifdef FEAT_FLOAT
776 {"log", 1, 1, f_log},
777 {"log10", 1, 1, f_log10},
778#endif
779#ifdef FEAT_LUA
780 {"luaeval", 1, 2, f_luaeval},
781#endif
782 {"map", 2, 2, f_map},
783 {"maparg", 1, 4, f_maparg},
784 {"mapcheck", 1, 3, f_mapcheck},
785 {"match", 2, 4, f_match},
786 {"matchadd", 2, 5, f_matchadd},
787 {"matchaddpos", 2, 5, f_matchaddpos},
788 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100789 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200790 {"matchend", 2, 4, f_matchend},
791 {"matchlist", 2, 4, f_matchlist},
792 {"matchstr", 2, 4, f_matchstr},
793 {"matchstrpos", 2, 4, f_matchstrpos},
794 {"max", 1, 1, f_max},
795 {"min", 1, 1, f_min},
796#ifdef vim_mkdir
797 {"mkdir", 1, 3, f_mkdir},
798#endif
799 {"mode", 0, 1, f_mode},
800#ifdef FEAT_MZSCHEME
801 {"mzeval", 1, 1, f_mzeval},
802#endif
803 {"nextnonblank", 1, 1, f_nextnonblank},
804 {"nr2char", 1, 2, f_nr2char},
805 {"or", 2, 2, f_or},
806 {"pathshorten", 1, 1, f_pathshorten},
807#ifdef FEAT_PERL
808 {"perleval", 1, 1, f_perleval},
809#endif
810#ifdef FEAT_FLOAT
811 {"pow", 2, 2, f_pow},
812#endif
813 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100814 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200815#ifdef FEAT_JOB_CHANNEL
816 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200817 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200818 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
819#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100820#ifdef FEAT_TEXT_PROP
821 {"prop_add", 3, 3, f_prop_add},
822 {"prop_clear", 1, 3, f_prop_clear},
823 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100824 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100825 {"prop_type_add", 2, 2, f_prop_type_add},
826 {"prop_type_change", 2, 2, f_prop_type_change},
827 {"prop_type_delete", 1, 2, f_prop_type_delete},
828 {"prop_type_get", 1, 2, f_prop_type_get},
829 {"prop_type_list", 0, 1, f_prop_type_list},
830#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200831 {"pumvisible", 0, 0, f_pumvisible},
832#ifdef FEAT_PYTHON3
833 {"py3eval", 1, 1, f_py3eval},
834#endif
835#ifdef FEAT_PYTHON
836 {"pyeval", 1, 1, f_pyeval},
837#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100838#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
839 {"pyxeval", 1, 1, f_pyxeval},
840#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200841 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200842 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200843 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200844 {"reg_executing", 0, 0, f_reg_executing},
845 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200846 {"reltime", 0, 2, f_reltime},
847#ifdef FEAT_FLOAT
848 {"reltimefloat", 1, 1, f_reltimefloat},
849#endif
850 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100851 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"remote_foreground", 1, 1, f_remote_foreground},
853 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100854 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100856 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200857 {"remove", 2, 3, f_remove},
858 {"rename", 2, 2, f_rename},
859 {"repeat", 2, 2, f_repeat},
860 {"resolve", 1, 1, f_resolve},
861 {"reverse", 1, 1, f_reverse},
862#ifdef FEAT_FLOAT
863 {"round", 1, 1, f_round},
864#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100865#ifdef FEAT_RUBY
866 {"rubyeval", 1, 1, f_rubyeval},
867#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200868 {"screenattr", 2, 2, f_screenattr},
869 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100870 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200871 {"screencol", 0, 0, f_screencol},
872 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100873 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200874 {"search", 1, 4, f_search},
875 {"searchdecl", 1, 3, f_searchdecl},
876 {"searchpair", 3, 7, f_searchpair},
877 {"searchpairpos", 3, 7, f_searchpairpos},
878 {"searchpos", 1, 4, f_searchpos},
879 {"server2client", 2, 2, f_server2client},
880 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200881 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882 {"setbufvar", 3, 3, f_setbufvar},
883 {"setcharsearch", 1, 1, f_setcharsearch},
884 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200885 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200886 {"setfperm", 2, 2, f_setfperm},
887 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200888 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100889 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200890 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200891 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200892 {"setreg", 2, 3, f_setreg},
893 {"settabvar", 3, 3, f_settabvar},
894 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100895 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200896 {"setwinvar", 3, 3, f_setwinvar},
897#ifdef FEAT_CRYPT
898 {"sha256", 1, 1, f_sha256},
899#endif
900 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100901 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100902#ifdef FEAT_SIGNS
903 {"sign_define", 1, 2, f_sign_define},
904 {"sign_getdefined", 0, 1, f_sign_getdefined},
905 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100906 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100907 {"sign_place", 4, 5, f_sign_place},
908 {"sign_undefine", 0, 1, f_sign_undefine},
909 {"sign_unplace", 1, 2, f_sign_unplace},
910#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200911 {"simplify", 1, 1, f_simplify},
912#ifdef FEAT_FLOAT
913 {"sin", 1, 1, f_sin},
914 {"sinh", 1, 1, f_sinh},
915#endif
916 {"sort", 1, 3, f_sort},
917 {"soundfold", 1, 1, f_soundfold},
918 {"spellbadword", 0, 1, f_spellbadword},
919 {"spellsuggest", 1, 3, f_spellsuggest},
920 {"split", 1, 3, f_split},
921#ifdef FEAT_FLOAT
922 {"sqrt", 1, 1, f_sqrt},
923 {"str2float", 1, 1, f_str2float},
924#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200925 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200926 {"str2nr", 1, 2, f_str2nr},
927 {"strcharpart", 2, 3, f_strcharpart},
928 {"strchars", 1, 2, f_strchars},
929 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
930#ifdef HAVE_STRFTIME
931 {"strftime", 1, 2, f_strftime},
932#endif
933 {"strgetchar", 2, 2, f_strgetchar},
934 {"stridx", 2, 3, f_stridx},
935 {"string", 1, 1, f_string},
936 {"strlen", 1, 1, f_strlen},
937 {"strpart", 2, 3, f_strpart},
938 {"strridx", 2, 3, f_strridx},
939 {"strtrans", 1, 1, f_strtrans},
940 {"strwidth", 1, 1, f_strwidth},
941 {"submatch", 1, 2, f_submatch},
942 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200943 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200944 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200945 {"synID", 3, 3, f_synID},
946 {"synIDattr", 2, 3, f_synIDattr},
947 {"synIDtrans", 1, 1, f_synIDtrans},
948 {"synconcealed", 2, 2, f_synconcealed},
949 {"synstack", 2, 2, f_synstack},
950 {"system", 1, 2, f_system},
951 {"systemlist", 1, 2, f_systemlist},
952 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
953 {"tabpagenr", 0, 1, f_tabpagenr},
954 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
955 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100956 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200957#ifdef FEAT_FLOAT
958 {"tan", 1, 1, f_tan},
959 {"tanh", 1, 1, f_tanh},
960#endif
961 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200962#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100963 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
964 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100965 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200966 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200967# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
968 {"term_getansicolors", 1, 1, f_term_getansicolors},
969# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200970 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200971 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200972 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200973 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200974 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200975 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200976 {"term_getstatus", 1, 1, f_term_getstatus},
977 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200978 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200979 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200980 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200981 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200982# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
983 {"term_setansicolors", 2, 2, f_term_setansicolors},
984# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100985 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100986 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200987 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200988 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200989 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200990#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200991 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
992 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200993 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200994 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +0200995 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100996 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100997 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200998#ifdef FEAT_JOB_CHANNEL
999 {"test_null_channel", 0, 0, f_test_null_channel},
1000#endif
1001 {"test_null_dict", 0, 0, f_test_null_dict},
1002#ifdef FEAT_JOB_CHANNEL
1003 {"test_null_job", 0, 0, f_test_null_job},
1004#endif
1005 {"test_null_list", 0, 0, f_test_null_list},
1006 {"test_null_partial", 0, 0, f_test_null_partial},
1007 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001008 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001009 {"test_override", 2, 2, f_test_override},
1010 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001011#ifdef FEAT_GUI
1012 {"test_scrollbar", 3, 3, f_test_scrollbar},
1013#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001014#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001015 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001016#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001017 {"test_settime", 1, 1, f_test_settime},
1018#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001019 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001020 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001021 {"timer_start", 2, 3, f_timer_start},
1022 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001023 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001024#endif
1025 {"tolower", 1, 1, f_tolower},
1026 {"toupper", 1, 1, f_toupper},
1027 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001028 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001029#ifdef FEAT_FLOAT
1030 {"trunc", 1, 1, f_trunc},
1031#endif
1032 {"type", 1, 1, f_type},
1033 {"undofile", 1, 1, f_undofile},
1034 {"undotree", 0, 0, f_undotree},
1035 {"uniq", 1, 3, f_uniq},
1036 {"values", 1, 1, f_values},
1037 {"virtcol", 1, 1, f_virtcol},
1038 {"visualmode", 0, 1, f_visualmode},
1039 {"wildmenumode", 0, 0, f_wildmenumode},
1040 {"win_findbuf", 1, 1, f_win_findbuf},
1041 {"win_getid", 0, 2, f_win_getid},
1042 {"win_gotoid", 1, 1, f_win_gotoid},
1043 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1044 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001045 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001046 {"winbufnr", 1, 1, f_winbufnr},
1047 {"wincol", 0, 0, f_wincol},
1048 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001049 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001050 {"winline", 0, 0, f_winline},
1051 {"winnr", 0, 1, f_winnr},
1052 {"winrestcmd", 0, 0, f_winrestcmd},
1053 {"winrestview", 1, 1, f_winrestview},
1054 {"winsaveview", 0, 0, f_winsaveview},
1055 {"winwidth", 1, 1, f_winwidth},
1056 {"wordcount", 0, 0, f_wordcount},
1057 {"writefile", 2, 3, f_writefile},
1058 {"xor", 2, 2, f_xor},
1059};
1060
1061#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1062
1063/*
1064 * Function given to ExpandGeneric() to obtain the list of internal
1065 * or user defined function names.
1066 */
1067 char_u *
1068get_function_name(expand_T *xp, int idx)
1069{
1070 static int intidx = -1;
1071 char_u *name;
1072
1073 if (idx == 0)
1074 intidx = -1;
1075 if (intidx < 0)
1076 {
1077 name = get_user_func_name(xp, idx);
1078 if (name != NULL)
1079 return name;
1080 }
1081 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1082 {
1083 STRCPY(IObuff, functions[intidx].f_name);
1084 STRCAT(IObuff, "(");
1085 if (functions[intidx].f_max_argc == 0)
1086 STRCAT(IObuff, ")");
1087 return IObuff;
1088 }
1089
1090 return NULL;
1091}
1092
1093/*
1094 * Function given to ExpandGeneric() to obtain the list of internal or
1095 * user defined variable or function names.
1096 */
1097 char_u *
1098get_expr_name(expand_T *xp, int idx)
1099{
1100 static int intidx = -1;
1101 char_u *name;
1102
1103 if (idx == 0)
1104 intidx = -1;
1105 if (intidx < 0)
1106 {
1107 name = get_function_name(xp, idx);
1108 if (name != NULL)
1109 return name;
1110 }
1111 return get_user_var_name(xp, ++intidx);
1112}
1113
1114#endif /* FEAT_CMDL_COMPL */
1115
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001116/*
1117 * Find internal function in table above.
1118 * Return index, or -1 if not found
1119 */
1120 int
1121find_internal_func(
1122 char_u *name) /* name of the function */
1123{
1124 int first = 0;
1125 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1126 int cmp;
1127 int x;
1128
1129 /*
1130 * Find the function name in the table. Binary search.
1131 */
1132 while (first <= last)
1133 {
1134 x = first + ((unsigned)(last - first) >> 1);
1135 cmp = STRCMP(name, functions[x].f_name);
1136 if (cmp < 0)
1137 last = x - 1;
1138 else if (cmp > 0)
1139 first = x + 1;
1140 else
1141 return x;
1142 }
1143 return -1;
1144}
1145
1146 int
1147call_internal_func(
1148 char_u *name,
1149 int argcount,
1150 typval_T *argvars,
1151 typval_T *rettv)
1152{
1153 int i;
1154
1155 i = find_internal_func(name);
1156 if (i < 0)
1157 return ERROR_UNKNOWN;
1158 if (argcount < functions[i].f_min_argc)
1159 return ERROR_TOOFEW;
1160 if (argcount > functions[i].f_max_argc)
1161 return ERROR_TOOMANY;
1162 argvars[argcount].v_type = VAR_UNKNOWN;
1163 functions[i].f_func(argvars, rettv);
1164 return ERROR_NONE;
1165}
1166
1167/*
1168 * Return TRUE for a non-zero Number and a non-empty String.
1169 */
1170 static int
1171non_zero_arg(typval_T *argvars)
1172{
1173 return ((argvars[0].v_type == VAR_NUMBER
1174 && argvars[0].vval.v_number != 0)
1175 || (argvars[0].v_type == VAR_SPECIAL
1176 && argvars[0].vval.v_number == VVAL_TRUE)
1177 || (argvars[0].v_type == VAR_STRING
1178 && argvars[0].vval.v_string != NULL
1179 && *argvars[0].vval.v_string != NUL));
1180}
1181
1182/*
1183 * Get the lnum from the first argument.
1184 * Also accepts ".", "$", etc., but that only works for the current buffer.
1185 * Returns -1 on error.
1186 */
1187 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001188tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001189{
1190 typval_T rettv;
1191 linenr_T lnum;
1192
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001193 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001194 if (lnum == 0) /* no valid number, try using line() */
1195 {
1196 rettv.v_type = VAR_NUMBER;
1197 f_line(argvars, &rettv);
1198 lnum = (linenr_T)rettv.vval.v_number;
1199 clear_tv(&rettv);
1200 }
1201 return lnum;
1202}
1203
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001204/*
1205 * Get the lnum from the first argument.
1206 * Also accepts "$", then "buf" is used.
1207 * Returns 0 on error.
1208 */
1209 static linenr_T
1210tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1211{
1212 if (argvars[0].v_type == VAR_STRING
1213 && argvars[0].vval.v_string != NULL
1214 && argvars[0].vval.v_string[0] == '$'
1215 && buf != NULL)
1216 return buf->b_ml.ml_line_count;
1217 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1218}
1219
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001220#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001221/*
1222 * Get the float value of "argvars[0]" into "f".
1223 * Returns FAIL when the argument is not a Number or Float.
1224 */
1225 static int
1226get_float_arg(typval_T *argvars, float_T *f)
1227{
1228 if (argvars[0].v_type == VAR_FLOAT)
1229 {
1230 *f = argvars[0].vval.v_float;
1231 return OK;
1232 }
1233 if (argvars[0].v_type == VAR_NUMBER)
1234 {
1235 *f = (float_T)argvars[0].vval.v_number;
1236 return OK;
1237 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001238 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001239 return FAIL;
1240}
1241
1242/*
1243 * "abs(expr)" function
1244 */
1245 static void
1246f_abs(typval_T *argvars, typval_T *rettv)
1247{
1248 if (argvars[0].v_type == VAR_FLOAT)
1249 {
1250 rettv->v_type = VAR_FLOAT;
1251 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1252 }
1253 else
1254 {
1255 varnumber_T n;
1256 int error = FALSE;
1257
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001258 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001259 if (error)
1260 rettv->vval.v_number = -1;
1261 else if (n > 0)
1262 rettv->vval.v_number = n;
1263 else
1264 rettv->vval.v_number = -n;
1265 }
1266}
1267
1268/*
1269 * "acos()" function
1270 */
1271 static void
1272f_acos(typval_T *argvars, typval_T *rettv)
1273{
1274 float_T f = 0.0;
1275
1276 rettv->v_type = VAR_FLOAT;
1277 if (get_float_arg(argvars, &f) == OK)
1278 rettv->vval.v_float = acos(f);
1279 else
1280 rettv->vval.v_float = 0.0;
1281}
1282#endif
1283
1284/*
1285 * "add(list, item)" function
1286 */
1287 static void
1288f_add(typval_T *argvars, typval_T *rettv)
1289{
1290 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001291 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001292
1293 rettv->vval.v_number = 1; /* Default: Failed */
1294 if (argvars[0].v_type == VAR_LIST)
1295 {
1296 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001297 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001298 (char_u *)N_("add() argument"), TRUE)
1299 && list_append_tv(l, &argvars[1]) == OK)
1300 copy_tv(&argvars[0], rettv);
1301 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001302 else if (argvars[0].v_type == VAR_BLOB)
1303 {
1304 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001305 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001306 (char_u *)N_("add() argument"), TRUE))
1307 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001308 int error = FALSE;
1309 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1310
1311 if (!error)
1312 {
1313 ga_append(&b->bv_ga, (int)n);
1314 copy_tv(&argvars[0], rettv);
1315 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001316 }
1317 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001318 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001319 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001320}
1321
1322/*
1323 * "and(expr, expr)" function
1324 */
1325 static void
1326f_and(typval_T *argvars, typval_T *rettv)
1327{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001328 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1329 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001330}
1331
1332/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001333 * If there is a window for "curbuf", make it the current window.
1334 */
1335 static void
1336find_win_for_curbuf(void)
1337{
1338 wininfo_T *wip;
1339
1340 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1341 {
1342 if (wip->wi_win != NULL)
1343 {
1344 curwin = wip->wi_win;
1345 break;
1346 }
1347 }
1348}
1349
1350/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001351 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001352 */
1353 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001354set_buffer_lines(
1355 buf_T *buf,
1356 linenr_T lnum_arg,
1357 int append,
1358 typval_T *lines,
1359 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001360{
Bram Moolenaarca851592018-06-06 21:04:07 +02001361 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1362 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001363 list_T *l = NULL;
1364 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001366 linenr_T append_lnum;
1367 buf_T *curbuf_save = NULL;
1368 win_T *curwin_save = NULL;
1369 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001370
Bram Moolenaarca851592018-06-06 21:04:07 +02001371 /* When using the current buffer ml_mfp will be set if needed. Useful when
1372 * setline() is used on startup. For other buffers the buffer must be
1373 * loaded. */
1374 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001375 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001376 rettv->vval.v_number = 1; /* FAIL */
1377 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001378 }
1379
Bram Moolenaarca851592018-06-06 21:04:07 +02001380 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001381 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001382 curbuf_save = curbuf;
1383 curwin_save = curwin;
1384 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001385 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001386 }
1387
1388 if (append)
1389 // appendbufline() uses the line number below which we insert
1390 append_lnum = lnum - 1;
1391 else
1392 // setbufline() uses the line number above which we insert, we only
1393 // append if it's below the last line
1394 append_lnum = curbuf->b_ml.ml_line_count;
1395
1396 if (lines->v_type == VAR_LIST)
1397 {
1398 l = lines->vval.v_list;
1399 li = l->lv_first;
1400 }
1401 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001402 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001403
1404 /* default result is zero == OK */
1405 for (;;)
1406 {
1407 if (l != NULL)
1408 {
1409 /* list argument, get next string */
1410 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001411 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001412 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001413 li = li->li_next;
1414 }
1415
Bram Moolenaarca851592018-06-06 21:04:07 +02001416 rettv->vval.v_number = 1; /* FAIL */
1417 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1418 break;
1419
1420 /* When coming here from Insert mode, sync undo, so that this can be
1421 * undone separately from what was previously inserted. */
1422 if (u_sync_once == 2)
1423 {
1424 u_sync_once = 1; /* notify that u_sync() was called */
1425 u_sync(TRUE);
1426 }
1427
1428 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1429 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001430 // Existing line, replace it.
1431 // Removes any existing text properties.
1432 if (u_savesub(lnum) == OK && ml_replace_len(
1433 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001434 {
1435 changed_bytes(lnum, 0);
1436 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1437 check_cursor_col();
1438 rettv->vval.v_number = 0; /* OK */
1439 }
1440 }
1441 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1442 {
1443 /* append the line */
1444 ++added;
1445 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1446 rettv->vval.v_number = 0; /* OK */
1447 }
1448
1449 if (l == NULL) /* only one string argument */
1450 break;
1451 ++lnum;
1452 }
1453
1454 if (added > 0)
1455 {
1456 win_T *wp;
1457 tabpage_T *tp;
1458
1459 appended_lines_mark(append_lnum, added);
1460 FOR_ALL_TAB_WINDOWS(tp, wp)
1461 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1462 wp->w_cursor.lnum += added;
1463 check_cursor_col();
1464
Bram Moolenaarf2732452018-06-03 14:47:35 +02001465#ifdef FEAT_JOB_CHANNEL
1466 if (bt_prompt(curbuf) && (State & INSERT))
1467 // show the line with the prompt
1468 update_topline();
1469#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001470 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001471
1472 if (!is_curbuf)
1473 {
1474 curbuf = curbuf_save;
1475 curwin = curwin_save;
1476 }
1477}
1478
1479/*
1480 * "append(lnum, string/list)" function
1481 */
1482 static void
1483f_append(typval_T *argvars, typval_T *rettv)
1484{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001485 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001486
1487 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1488}
1489
1490/*
1491 * "appendbufline(buf, lnum, string/list)" function
1492 */
1493 static void
1494f_appendbufline(typval_T *argvars, typval_T *rettv)
1495{
1496 linenr_T lnum;
1497 buf_T *buf;
1498
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001499 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001500 if (buf == NULL)
1501 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001502 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001503 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001504 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001505 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1506 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001507}
1508
1509/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001510 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001511 */
1512 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001513f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001514{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001515 win_T *wp;
1516
1517 if (argvars[0].v_type == VAR_UNKNOWN)
1518 // use the current window
1519 rettv->vval.v_number = ARGCOUNT;
1520 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001521 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001522 // use the global argument list
1523 rettv->vval.v_number = GARGCOUNT;
1524 else
1525 {
1526 // use the argument list of the specified window
1527 wp = find_win_by_nr_or_id(&argvars[0]);
1528 if (wp != NULL)
1529 rettv->vval.v_number = WARGCOUNT(wp);
1530 else
1531 rettv->vval.v_number = -1;
1532 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001533}
1534
1535/*
1536 * "argidx()" function
1537 */
1538 static void
1539f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1540{
1541 rettv->vval.v_number = curwin->w_arg_idx;
1542}
1543
1544/*
1545 * "arglistid()" function
1546 */
1547 static void
1548f_arglistid(typval_T *argvars, typval_T *rettv)
1549{
1550 win_T *wp;
1551
1552 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001553 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001554 if (wp != NULL)
1555 rettv->vval.v_number = wp->w_alist->id;
1556}
1557
1558/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001559 * Get the argument list for a given window
1560 */
1561 static void
1562get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1563{
1564 int idx;
1565
1566 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1567 for (idx = 0; idx < argcount; ++idx)
1568 list_append_string(rettv->vval.v_list,
1569 alist_name(&arglist[idx]), -1);
1570}
1571
1572/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001573 * "argv(nr)" function
1574 */
1575 static void
1576f_argv(typval_T *argvars, typval_T *rettv)
1577{
1578 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001579 aentry_T *arglist = NULL;
1580 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001581
1582 if (argvars[0].v_type != VAR_UNKNOWN)
1583 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001584 if (argvars[1].v_type == VAR_UNKNOWN)
1585 {
1586 arglist = ARGLIST;
1587 argcount = ARGCOUNT;
1588 }
1589 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001590 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001591 {
1592 arglist = GARGLIST;
1593 argcount = GARGCOUNT;
1594 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001595 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001596 {
1597 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1598
1599 if (wp != NULL)
1600 {
1601 /* Use the argument list of the specified window */
1602 arglist = WARGLIST(wp);
1603 argcount = WARGCOUNT(wp);
1604 }
1605 }
1606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001608 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001609 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001610 if (arglist != NULL && idx >= 0 && idx < argcount)
1611 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1612 else if (idx == -1)
1613 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001614 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001615 else
1616 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001617}
1618
1619/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001620 * "assert_beeps(cmd [, error])" function
1621 */
1622 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001623f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001624{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001625 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001626}
1627
1628/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001629 * "assert_equal(expected, actual[, msg])" function
1630 */
1631 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001632f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001633{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001634 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001635}
1636
1637/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001638 * "assert_equalfile(fname-one, fname-two)" function
1639 */
1640 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001641f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001642{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001643 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001644}
1645
1646/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001647 * "assert_notequal(expected, actual[, msg])" function
1648 */
1649 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001650f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001651{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001652 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001653}
1654
1655/*
1656 * "assert_exception(string[, msg])" function
1657 */
1658 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001659f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001660{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001661 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001662}
1663
1664/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001665 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001666 */
1667 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001668f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001669{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001670 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001671}
1672
1673/*
1674 * "assert_false(actual[, msg])" function
1675 */
1676 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001677f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001678{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001679 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001680}
1681
1682/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001683 * "assert_inrange(lower, upper[, msg])" function
1684 */
1685 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001686f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001687{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001688 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001689}
1690
1691/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001692 * "assert_match(pattern, actual[, msg])" function
1693 */
1694 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001695f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001696{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001697 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001698}
1699
1700/*
1701 * "assert_notmatch(pattern, actual[, msg])" function
1702 */
1703 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001704f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001705{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001706 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001707}
1708
1709/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001710 * "assert_report(msg)" function
1711 */
1712 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001713f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001714{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001715 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001716}
1717
1718/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001719 * "assert_true(actual[, msg])" function
1720 */
1721 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001722f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001723{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001724 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001725}
1726
1727#ifdef FEAT_FLOAT
1728/*
1729 * "asin()" function
1730 */
1731 static void
1732f_asin(typval_T *argvars, typval_T *rettv)
1733{
1734 float_T f = 0.0;
1735
1736 rettv->v_type = VAR_FLOAT;
1737 if (get_float_arg(argvars, &f) == OK)
1738 rettv->vval.v_float = asin(f);
1739 else
1740 rettv->vval.v_float = 0.0;
1741}
1742
1743/*
1744 * "atan()" function
1745 */
1746 static void
1747f_atan(typval_T *argvars, typval_T *rettv)
1748{
1749 float_T f = 0.0;
1750
1751 rettv->v_type = VAR_FLOAT;
1752 if (get_float_arg(argvars, &f) == OK)
1753 rettv->vval.v_float = atan(f);
1754 else
1755 rettv->vval.v_float = 0.0;
1756}
1757
1758/*
1759 * "atan2()" function
1760 */
1761 static void
1762f_atan2(typval_T *argvars, typval_T *rettv)
1763{
1764 float_T fx = 0.0, fy = 0.0;
1765
1766 rettv->v_type = VAR_FLOAT;
1767 if (get_float_arg(argvars, &fx) == OK
1768 && get_float_arg(&argvars[1], &fy) == OK)
1769 rettv->vval.v_float = atan2(fx, fy);
1770 else
1771 rettv->vval.v_float = 0.0;
1772}
1773#endif
1774
1775/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001776 * "balloon_show()" function
1777 */
1778#ifdef FEAT_BEVAL
1779 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001780f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1781{
1782 rettv->v_type = VAR_STRING;
1783 if (balloonEval != NULL)
1784 {
1785 if (balloonEval->msg == NULL)
1786 rettv->vval.v_string = NULL;
1787 else
1788 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1789 }
1790}
1791
1792 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001793f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1794{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001795 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001796 {
1797 if (argvars[0].v_type == VAR_LIST
1798# ifdef FEAT_GUI
1799 && !gui.in_use
1800# endif
1801 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001802 {
1803 list_T *l = argvars[0].vval.v_list;
1804
1805 // empty list removes the balloon
1806 post_balloon(balloonEval, NULL,
1807 l == NULL || l->lv_len == 0 ? NULL : l);
1808 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001809 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001810 {
1811 char_u *mesg = tv_get_string_chk(&argvars[0]);
1812
1813 if (mesg != NULL)
1814 // empty string removes the balloon
1815 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1816 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001817 }
1818}
1819
Bram Moolenaar669a8282017-11-19 20:13:05 +01001820# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001821 static void
1822f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1823{
1824 if (rettv_list_alloc(rettv) == OK)
1825 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001826 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001827
1828 if (msg != NULL)
1829 {
1830 pumitem_T *array;
1831 int size = split_message(msg, &array);
1832 int i;
1833
1834 /* Skip the first and last item, they are always empty. */
1835 for (i = 1; i < size - 1; ++i)
1836 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001837 while (size > 0)
1838 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001839 vim_free(array);
1840 }
1841 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001842}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001843# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001844#endif
1845
1846/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001847 * "browse(save, title, initdir, default)" function
1848 */
1849 static void
1850f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1851{
1852#ifdef FEAT_BROWSE
1853 int save;
1854 char_u *title;
1855 char_u *initdir;
1856 char_u *defname;
1857 char_u buf[NUMBUFLEN];
1858 char_u buf2[NUMBUFLEN];
1859 int error = FALSE;
1860
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001861 save = (int)tv_get_number_chk(&argvars[0], &error);
1862 title = tv_get_string_chk(&argvars[1]);
1863 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1864 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001865
1866 if (error || title == NULL || initdir == NULL || defname == NULL)
1867 rettv->vval.v_string = NULL;
1868 else
1869 rettv->vval.v_string =
1870 do_browse(save ? BROWSE_SAVE : 0,
1871 title, defname, NULL, initdir, NULL, curbuf);
1872#else
1873 rettv->vval.v_string = NULL;
1874#endif
1875 rettv->v_type = VAR_STRING;
1876}
1877
1878/*
1879 * "browsedir(title, initdir)" function
1880 */
1881 static void
1882f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1883{
1884#ifdef FEAT_BROWSE
1885 char_u *title;
1886 char_u *initdir;
1887 char_u buf[NUMBUFLEN];
1888
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001889 title = tv_get_string_chk(&argvars[0]);
1890 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001891
1892 if (title == NULL || initdir == NULL)
1893 rettv->vval.v_string = NULL;
1894 else
1895 rettv->vval.v_string = do_browse(BROWSE_DIR,
1896 title, NULL, NULL, initdir, NULL, curbuf);
1897#else
1898 rettv->vval.v_string = NULL;
1899#endif
1900 rettv->v_type = VAR_STRING;
1901}
1902
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001903/*
1904 * Find a buffer by number or exact name.
1905 */
1906 static buf_T *
1907find_buffer(typval_T *avar)
1908{
1909 buf_T *buf = NULL;
1910
1911 if (avar->v_type == VAR_NUMBER)
1912 buf = buflist_findnr((int)avar->vval.v_number);
1913 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1914 {
1915 buf = buflist_findname_exp(avar->vval.v_string);
1916 if (buf == NULL)
1917 {
1918 /* No full path name match, try a match with a URL or a "nofile"
1919 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001920 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001921 if (buf->b_fname != NULL
1922 && (path_with_url(buf->b_fname)
1923#ifdef FEAT_QUICKFIX
1924 || bt_nofile(buf)
1925#endif
1926 )
1927 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1928 break;
1929 }
1930 }
1931 return buf;
1932}
1933
1934/*
1935 * "bufexists(expr)" function
1936 */
1937 static void
1938f_bufexists(typval_T *argvars, typval_T *rettv)
1939{
1940 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1941}
1942
1943/*
1944 * "buflisted(expr)" function
1945 */
1946 static void
1947f_buflisted(typval_T *argvars, typval_T *rettv)
1948{
1949 buf_T *buf;
1950
1951 buf = find_buffer(&argvars[0]);
1952 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1953}
1954
1955/*
1956 * "bufloaded(expr)" function
1957 */
1958 static void
1959f_bufloaded(typval_T *argvars, typval_T *rettv)
1960{
1961 buf_T *buf;
1962
1963 buf = find_buffer(&argvars[0]);
1964 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1965}
1966
1967 buf_T *
1968buflist_find_by_name(char_u *name, int curtab_only)
1969{
1970 int save_magic;
1971 char_u *save_cpo;
1972 buf_T *buf;
1973
1974 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1975 save_magic = p_magic;
1976 p_magic = TRUE;
1977 save_cpo = p_cpo;
1978 p_cpo = (char_u *)"";
1979
1980 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1981 TRUE, FALSE, curtab_only));
1982
1983 p_magic = save_magic;
1984 p_cpo = save_cpo;
1985 return buf;
1986}
1987
1988/*
1989 * Get buffer by number or pattern.
1990 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001991 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001992tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001993{
1994 char_u *name = tv->vval.v_string;
1995 buf_T *buf;
1996
1997 if (tv->v_type == VAR_NUMBER)
1998 return buflist_findnr((int)tv->vval.v_number);
1999 if (tv->v_type != VAR_STRING)
2000 return NULL;
2001 if (name == NULL || *name == NUL)
2002 return curbuf;
2003 if (name[0] == '$' && name[1] == NUL)
2004 return lastbuf;
2005
2006 buf = buflist_find_by_name(name, curtab_only);
2007
2008 /* If not found, try expanding the name, like done for bufexists(). */
2009 if (buf == NULL)
2010 buf = find_buffer(tv);
2011
2012 return buf;
2013}
2014
2015/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002016 * Get the buffer from "arg" and give an error and return NULL if it is not
2017 * valid.
2018 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002019 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002020get_buf_arg(typval_T *arg)
2021{
2022 buf_T *buf;
2023
2024 ++emsg_off;
2025 buf = tv_get_buf(arg, FALSE);
2026 --emsg_off;
2027 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002028 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002029 return buf;
2030}
2031
2032/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002033 * "bufname(expr)" function
2034 */
2035 static void
2036f_bufname(typval_T *argvars, typval_T *rettv)
2037{
2038 buf_T *buf;
2039
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002040 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002041 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002042 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002043 rettv->v_type = VAR_STRING;
2044 if (buf != NULL && buf->b_fname != NULL)
2045 rettv->vval.v_string = vim_strsave(buf->b_fname);
2046 else
2047 rettv->vval.v_string = NULL;
2048 --emsg_off;
2049}
2050
2051/*
2052 * "bufnr(expr)" function
2053 */
2054 static void
2055f_bufnr(typval_T *argvars, typval_T *rettv)
2056{
2057 buf_T *buf;
2058 int error = FALSE;
2059 char_u *name;
2060
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002061 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002062 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002063 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002064 --emsg_off;
2065
2066 /* If the buffer isn't found and the second argument is not zero create a
2067 * new buffer. */
2068 if (buf == NULL
2069 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002070 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002071 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002072 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002073 && !error)
2074 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2075
2076 if (buf != NULL)
2077 rettv->vval.v_number = buf->b_fnum;
2078 else
2079 rettv->vval.v_number = -1;
2080}
2081
2082 static void
2083buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2084{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002085 win_T *wp;
2086 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002087 buf_T *buf;
2088
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002089 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002090 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002091 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002092 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002093 {
2094 ++winnr;
2095 if (wp->w_buffer == buf)
2096 break;
2097 }
2098 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002099 --emsg_off;
2100}
2101
2102/*
2103 * "bufwinid(nr)" function
2104 */
2105 static void
2106f_bufwinid(typval_T *argvars, typval_T *rettv)
2107{
2108 buf_win_common(argvars, rettv, FALSE);
2109}
2110
2111/*
2112 * "bufwinnr(nr)" function
2113 */
2114 static void
2115f_bufwinnr(typval_T *argvars, typval_T *rettv)
2116{
2117 buf_win_common(argvars, rettv, TRUE);
2118}
2119
2120/*
2121 * "byte2line(byte)" function
2122 */
2123 static void
2124f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2125{
2126#ifndef FEAT_BYTEOFF
2127 rettv->vval.v_number = -1;
2128#else
2129 long boff = 0;
2130
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002131 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002132 if (boff < 0)
2133 rettv->vval.v_number = -1;
2134 else
2135 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2136 (linenr_T)0, &boff);
2137#endif
2138}
2139
2140 static void
2141byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2142{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002143 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002144 char_u *str;
2145 varnumber_T idx;
2146
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002147 str = tv_get_string_chk(&argvars[0]);
2148 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002149 rettv->vval.v_number = -1;
2150 if (str == NULL || idx < 0)
2151 return;
2152
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002153 t = str;
2154 for ( ; idx > 0; idx--)
2155 {
2156 if (*t == NUL) /* EOL reached */
2157 return;
2158 if (enc_utf8 && comp)
2159 t += utf_ptr2len(t);
2160 else
2161 t += (*mb_ptr2len)(t);
2162 }
2163 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164}
2165
2166/*
2167 * "byteidx()" function
2168 */
2169 static void
2170f_byteidx(typval_T *argvars, typval_T *rettv)
2171{
2172 byteidx(argvars, rettv, FALSE);
2173}
2174
2175/*
2176 * "byteidxcomp()" function
2177 */
2178 static void
2179f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2180{
2181 byteidx(argvars, rettv, TRUE);
2182}
2183
2184/*
2185 * "call(func, arglist [, dict])" function
2186 */
2187 static void
2188f_call(typval_T *argvars, typval_T *rettv)
2189{
2190 char_u *func;
2191 partial_T *partial = NULL;
2192 dict_T *selfdict = NULL;
2193
2194 if (argvars[1].v_type != VAR_LIST)
2195 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002196 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002197 return;
2198 }
2199 if (argvars[1].vval.v_list == NULL)
2200 return;
2201
2202 if (argvars[0].v_type == VAR_FUNC)
2203 func = argvars[0].vval.v_string;
2204 else if (argvars[0].v_type == VAR_PARTIAL)
2205 {
2206 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002207 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002208 }
2209 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002210 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002211 if (*func == NUL)
2212 return; /* type error or empty name */
2213
2214 if (argvars[2].v_type != VAR_UNKNOWN)
2215 {
2216 if (argvars[2].v_type != VAR_DICT)
2217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002218 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002219 return;
2220 }
2221 selfdict = argvars[2].vval.v_dict;
2222 }
2223
2224 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2225}
2226
2227#ifdef FEAT_FLOAT
2228/*
2229 * "ceil({float})" function
2230 */
2231 static void
2232f_ceil(typval_T *argvars, typval_T *rettv)
2233{
2234 float_T f = 0.0;
2235
2236 rettv->v_type = VAR_FLOAT;
2237 if (get_float_arg(argvars, &f) == OK)
2238 rettv->vval.v_float = ceil(f);
2239 else
2240 rettv->vval.v_float = 0.0;
2241}
2242#endif
2243
2244#ifdef FEAT_JOB_CHANNEL
2245/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002246 * "ch_canread()" function
2247 */
2248 static void
2249f_ch_canread(typval_T *argvars, typval_T *rettv)
2250{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002251 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002252
2253 rettv->vval.v_number = 0;
2254 if (channel != NULL)
2255 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2256 || channel_has_readahead(channel, PART_OUT)
2257 || channel_has_readahead(channel, PART_ERR);
2258}
2259
2260/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002261 * "ch_close()" function
2262 */
2263 static void
2264f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2265{
2266 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2267
2268 if (channel != NULL)
2269 {
2270 channel_close(channel, FALSE);
2271 channel_clear(channel);
2272 }
2273}
2274
2275/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002276 * "ch_close()" function
2277 */
2278 static void
2279f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2280{
2281 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2282
2283 if (channel != NULL)
2284 channel_close_in(channel);
2285}
2286
2287/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002288 * "ch_getbufnr()" function
2289 */
2290 static void
2291f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2292{
2293 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2294
2295 rettv->vval.v_number = -1;
2296 if (channel != NULL)
2297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002298 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002299 int part;
2300
2301 if (STRCMP(what, "err") == 0)
2302 part = PART_ERR;
2303 else if (STRCMP(what, "out") == 0)
2304 part = PART_OUT;
2305 else if (STRCMP(what, "in") == 0)
2306 part = PART_IN;
2307 else
2308 part = PART_SOCK;
2309 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2310 rettv->vval.v_number =
2311 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2312 }
2313}
2314
2315/*
2316 * "ch_getjob()" function
2317 */
2318 static void
2319f_ch_getjob(typval_T *argvars, typval_T *rettv)
2320{
2321 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2322
2323 if (channel != NULL)
2324 {
2325 rettv->v_type = VAR_JOB;
2326 rettv->vval.v_job = channel->ch_job;
2327 if (channel->ch_job != NULL)
2328 ++channel->ch_job->jv_refcount;
2329 }
2330}
2331
2332/*
2333 * "ch_info()" function
2334 */
2335 static void
2336f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2337{
2338 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2339
2340 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2341 channel_info(channel, rettv->vval.v_dict);
2342}
2343
2344/*
2345 * "ch_log()" function
2346 */
2347 static void
2348f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2349{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002350 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002351 channel_T *channel = NULL;
2352
2353 if (argvars[1].v_type != VAR_UNKNOWN)
2354 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2355
Bram Moolenaard5359b22018-04-05 22:44:39 +02002356 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002357}
2358
2359/*
2360 * "ch_logfile()" function
2361 */
2362 static void
2363f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2364{
2365 char_u *fname;
2366 char_u *opt = (char_u *)"";
2367 char_u buf[NUMBUFLEN];
2368
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002369 /* Don't open a file in restricted mode. */
2370 if (check_restricted() || check_secure())
2371 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002372 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002373 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002374 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002375 ch_logfile(fname, opt);
2376}
2377
2378/*
2379 * "ch_open()" function
2380 */
2381 static void
2382f_ch_open(typval_T *argvars, typval_T *rettv)
2383{
2384 rettv->v_type = VAR_CHANNEL;
2385 if (check_restricted() || check_secure())
2386 return;
2387 rettv->vval.v_channel = channel_open_func(argvars);
2388}
2389
2390/*
2391 * "ch_read()" function
2392 */
2393 static void
2394f_ch_read(typval_T *argvars, typval_T *rettv)
2395{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002396 common_channel_read(argvars, rettv, FALSE, FALSE);
2397}
2398
2399/*
2400 * "ch_readblob()" function
2401 */
2402 static void
2403f_ch_readblob(typval_T *argvars, typval_T *rettv)
2404{
2405 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002406}
2407
2408/*
2409 * "ch_readraw()" function
2410 */
2411 static void
2412f_ch_readraw(typval_T *argvars, typval_T *rettv)
2413{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002414 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002415}
2416
2417/*
2418 * "ch_evalexpr()" function
2419 */
2420 static void
2421f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2422{
2423 ch_expr_common(argvars, rettv, TRUE);
2424}
2425
2426/*
2427 * "ch_sendexpr()" function
2428 */
2429 static void
2430f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2431{
2432 ch_expr_common(argvars, rettv, FALSE);
2433}
2434
2435/*
2436 * "ch_evalraw()" function
2437 */
2438 static void
2439f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2440{
2441 ch_raw_common(argvars, rettv, TRUE);
2442}
2443
2444/*
2445 * "ch_sendraw()" function
2446 */
2447 static void
2448f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2449{
2450 ch_raw_common(argvars, rettv, FALSE);
2451}
2452
2453/*
2454 * "ch_setoptions()" function
2455 */
2456 static void
2457f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2458{
2459 channel_T *channel;
2460 jobopt_T opt;
2461
2462 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2463 if (channel == NULL)
2464 return;
2465 clear_job_options(&opt);
2466 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002467 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002468 channel_set_options(channel, &opt);
2469 free_job_options(&opt);
2470}
2471
2472/*
2473 * "ch_status()" function
2474 */
2475 static void
2476f_ch_status(typval_T *argvars, typval_T *rettv)
2477{
2478 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002479 jobopt_T opt;
2480 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002481
2482 /* return an empty string by default */
2483 rettv->v_type = VAR_STRING;
2484 rettv->vval.v_string = NULL;
2485
2486 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002487
2488 if (argvars[1].v_type != VAR_UNKNOWN)
2489 {
2490 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002491 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002492 && (opt.jo_set & JO_PART))
2493 part = opt.jo_part;
2494 }
2495
2496 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002497}
2498#endif
2499
2500/*
2501 * "changenr()" function
2502 */
2503 static void
2504f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2505{
2506 rettv->vval.v_number = curbuf->b_u_seq_cur;
2507}
2508
2509/*
2510 * "char2nr(string)" function
2511 */
2512 static void
2513f_char2nr(typval_T *argvars, typval_T *rettv)
2514{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002515 if (has_mbyte)
2516 {
2517 int utf8 = 0;
2518
2519 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002520 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002521
2522 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002523 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002524 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002525 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002526 }
2527 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002528 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002529}
2530
2531/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002532 * "chdir(dir)" function
2533 */
2534 static void
2535f_chdir(typval_T *argvars, typval_T *rettv)
2536{
2537 char_u *cwd;
2538 cdscope_T scope = CDSCOPE_GLOBAL;
2539
2540 rettv->v_type = VAR_STRING;
2541 rettv->vval.v_string = NULL;
2542
2543 if (argvars[0].v_type != VAR_STRING)
2544 return;
2545
2546 // Return the current directory
2547 cwd = alloc(MAXPATHL);
2548 if (cwd != NULL)
2549 {
2550 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2551 {
2552#ifdef BACKSLASH_IN_FILENAME
2553 slash_adjust(cwd);
2554#endif
2555 rettv->vval.v_string = vim_strsave(cwd);
2556 }
2557 vim_free(cwd);
2558 }
2559
2560 if (curwin->w_localdir != NULL)
2561 scope = CDSCOPE_WINDOW;
2562 else if (curtab->tp_localdir != NULL)
2563 scope = CDSCOPE_TABPAGE;
2564
2565 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2566 // Directory change failed
2567 VIM_CLEAR(rettv->vval.v_string);
2568}
2569
2570/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002571 * "cindent(lnum)" function
2572 */
2573 static void
2574f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2575{
2576#ifdef FEAT_CINDENT
2577 pos_T pos;
2578 linenr_T lnum;
2579
2580 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002581 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002582 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2583 {
2584 curwin->w_cursor.lnum = lnum;
2585 rettv->vval.v_number = get_c_indent();
2586 curwin->w_cursor = pos;
2587 }
2588 else
2589#endif
2590 rettv->vval.v_number = -1;
2591}
2592
Bram Moolenaaraff74912019-03-30 18:11:49 +01002593 static win_T *
2594get_optional_window(typval_T *argvars, int idx)
2595{
2596 win_T *win = curwin;
2597
2598 if (argvars[idx].v_type != VAR_UNKNOWN)
2599 {
2600 win = find_win_by_nr_or_id(&argvars[idx]);
2601 if (win == NULL)
2602 {
2603 emsg(_(e_invalwindow));
2604 return NULL;
2605 }
2606 }
2607 return win;
2608}
2609
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002610/*
2611 * "clearmatches()" function
2612 */
2613 static void
2614f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2615{
2616#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002617 win_T *win = get_optional_window(argvars, 0);
2618
2619 if (win != NULL)
2620 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002621#endif
2622}
2623
2624/*
2625 * "col(string)" function
2626 */
2627 static void
2628f_col(typval_T *argvars, typval_T *rettv)
2629{
2630 colnr_T col = 0;
2631 pos_T *fp;
2632 int fnum = curbuf->b_fnum;
2633
2634 fp = var2fpos(&argvars[0], FALSE, &fnum);
2635 if (fp != NULL && fnum == curbuf->b_fnum)
2636 {
2637 if (fp->col == MAXCOL)
2638 {
2639 /* '> can be MAXCOL, get the length of the line then */
2640 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2641 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2642 else
2643 col = MAXCOL;
2644 }
2645 else
2646 {
2647 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002648 /* col(".") when the cursor is on the NUL at the end of the line
2649 * because of "coladd" can be seen as an extra column. */
2650 if (virtual_active() && fp == &curwin->w_cursor)
2651 {
2652 char_u *p = ml_get_cursor();
2653
2654 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2655 curwin->w_virtcol - curwin->w_cursor.coladd))
2656 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002657 int l;
2658
2659 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2660 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002661 }
2662 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002663 }
2664 }
2665 rettv->vval.v_number = col;
2666}
2667
2668#if defined(FEAT_INS_EXPAND)
2669/*
2670 * "complete()" function
2671 */
2672 static void
2673f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2674{
2675 int startcol;
2676
2677 if ((State & INSERT) == 0)
2678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002679 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002680 return;
2681 }
2682
2683 /* Check for undo allowed here, because if something was already inserted
2684 * the line was already saved for undo and this check isn't done. */
2685 if (!undo_allowed())
2686 return;
2687
2688 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2689 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002690 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002691 return;
2692 }
2693
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002694 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002695 if (startcol <= 0)
2696 return;
2697
2698 set_completion(startcol - 1, argvars[1].vval.v_list);
2699}
2700
2701/*
2702 * "complete_add()" function
2703 */
2704 static void
2705f_complete_add(typval_T *argvars, typval_T *rettv)
2706{
2707 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2708}
2709
2710/*
2711 * "complete_check()" function
2712 */
2713 static void
2714f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2715{
2716 int saved = RedrawingDisabled;
2717
2718 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002719 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002720 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002721 RedrawingDisabled = saved;
2722}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002723
2724/*
2725 * "complete_info()" function
2726 */
2727 static void
2728f_complete_info(typval_T *argvars, typval_T *rettv)
2729{
2730 list_T *what_list = NULL;
2731
2732 if (rettv_dict_alloc(rettv) != OK)
2733 return;
2734
2735 if (argvars[0].v_type != VAR_UNKNOWN)
2736 {
2737 if (argvars[0].v_type != VAR_LIST)
2738 {
2739 emsg(_(e_listreq));
2740 return;
2741 }
2742 what_list = argvars[0].vval.v_list;
2743 }
2744 get_complete_info(what_list, rettv->vval.v_dict);
2745}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002746#endif
2747
2748/*
2749 * "confirm(message, buttons[, default [, type]])" function
2750 */
2751 static void
2752f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2753{
2754#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2755 char_u *message;
2756 char_u *buttons = NULL;
2757 char_u buf[NUMBUFLEN];
2758 char_u buf2[NUMBUFLEN];
2759 int def = 1;
2760 int type = VIM_GENERIC;
2761 char_u *typestr;
2762 int error = FALSE;
2763
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002764 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002765 if (message == NULL)
2766 error = TRUE;
2767 if (argvars[1].v_type != VAR_UNKNOWN)
2768 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002769 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002770 if (buttons == NULL)
2771 error = TRUE;
2772 if (argvars[2].v_type != VAR_UNKNOWN)
2773 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002774 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002775 if (argvars[3].v_type != VAR_UNKNOWN)
2776 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002777 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 if (typestr == NULL)
2779 error = TRUE;
2780 else
2781 {
2782 switch (TOUPPER_ASC(*typestr))
2783 {
2784 case 'E': type = VIM_ERROR; break;
2785 case 'Q': type = VIM_QUESTION; break;
2786 case 'I': type = VIM_INFO; break;
2787 case 'W': type = VIM_WARNING; break;
2788 case 'G': type = VIM_GENERIC; break;
2789 }
2790 }
2791 }
2792 }
2793 }
2794
2795 if (buttons == NULL || *buttons == NUL)
2796 buttons = (char_u *)_("&Ok");
2797
2798 if (!error)
2799 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2800 def, NULL, FALSE);
2801#endif
2802}
2803
2804/*
2805 * "copy()" function
2806 */
2807 static void
2808f_copy(typval_T *argvars, typval_T *rettv)
2809{
2810 item_copy(&argvars[0], rettv, FALSE, 0);
2811}
2812
2813#ifdef FEAT_FLOAT
2814/*
2815 * "cos()" function
2816 */
2817 static void
2818f_cos(typval_T *argvars, typval_T *rettv)
2819{
2820 float_T f = 0.0;
2821
2822 rettv->v_type = VAR_FLOAT;
2823 if (get_float_arg(argvars, &f) == OK)
2824 rettv->vval.v_float = cos(f);
2825 else
2826 rettv->vval.v_float = 0.0;
2827}
2828
2829/*
2830 * "cosh()" function
2831 */
2832 static void
2833f_cosh(typval_T *argvars, typval_T *rettv)
2834{
2835 float_T f = 0.0;
2836
2837 rettv->v_type = VAR_FLOAT;
2838 if (get_float_arg(argvars, &f) == OK)
2839 rettv->vval.v_float = cosh(f);
2840 else
2841 rettv->vval.v_float = 0.0;
2842}
2843#endif
2844
2845/*
2846 * "count()" function
2847 */
2848 static void
2849f_count(typval_T *argvars, typval_T *rettv)
2850{
2851 long n = 0;
2852 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002853 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002854
Bram Moolenaar9966b212017-07-28 16:46:57 +02002855 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002856 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002857
2858 if (argvars[0].v_type == VAR_STRING)
2859 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002860 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002861 char_u *p = argvars[0].vval.v_string;
2862 char_u *next;
2863
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002864 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002865 {
2866 if (ic)
2867 {
2868 size_t len = STRLEN(expr);
2869
2870 while (*p != NUL)
2871 {
2872 if (MB_STRNICMP(p, expr, len) == 0)
2873 {
2874 ++n;
2875 p += len;
2876 }
2877 else
2878 MB_PTR_ADV(p);
2879 }
2880 }
2881 else
2882 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2883 != NULL)
2884 {
2885 ++n;
2886 p = next + STRLEN(expr);
2887 }
2888 }
2889
2890 }
2891 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002892 {
2893 listitem_T *li;
2894 list_T *l;
2895 long idx;
2896
2897 if ((l = argvars[0].vval.v_list) != NULL)
2898 {
2899 li = l->lv_first;
2900 if (argvars[2].v_type != VAR_UNKNOWN)
2901 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002902 if (argvars[3].v_type != VAR_UNKNOWN)
2903 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002904 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002905 if (!error)
2906 {
2907 li = list_find(l, idx);
2908 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002909 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002910 }
2911 }
2912 if (error)
2913 li = NULL;
2914 }
2915
2916 for ( ; li != NULL; li = li->li_next)
2917 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2918 ++n;
2919 }
2920 }
2921 else if (argvars[0].v_type == VAR_DICT)
2922 {
2923 int todo;
2924 dict_T *d;
2925 hashitem_T *hi;
2926
2927 if ((d = argvars[0].vval.v_dict) != NULL)
2928 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002929 if (argvars[2].v_type != VAR_UNKNOWN)
2930 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002931 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002932 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002933 }
2934
2935 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2936 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2937 {
2938 if (!HASHITEM_EMPTY(hi))
2939 {
2940 --todo;
2941 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2942 ++n;
2943 }
2944 }
2945 }
2946 }
2947 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002948 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002949 rettv->vval.v_number = n;
2950}
2951
2952/*
2953 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2954 *
2955 * Checks the existence of a cscope connection.
2956 */
2957 static void
2958f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2959{
2960#ifdef FEAT_CSCOPE
2961 int num = 0;
2962 char_u *dbpath = NULL;
2963 char_u *prepend = NULL;
2964 char_u buf[NUMBUFLEN];
2965
2966 if (argvars[0].v_type != VAR_UNKNOWN
2967 && argvars[1].v_type != VAR_UNKNOWN)
2968 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002969 num = (int)tv_get_number(&argvars[0]);
2970 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002972 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002973 }
2974
2975 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2976#endif
2977}
2978
2979/*
2980 * "cursor(lnum, col)" function, or
2981 * "cursor(list)"
2982 *
2983 * Moves the cursor to the specified line and column.
2984 * Returns 0 when the position could be set, -1 otherwise.
2985 */
2986 static void
2987f_cursor(typval_T *argvars, typval_T *rettv)
2988{
2989 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002990 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002991 int set_curswant = TRUE;
2992
2993 rettv->vval.v_number = -1;
2994 if (argvars[1].v_type == VAR_UNKNOWN)
2995 {
2996 pos_T pos;
2997 colnr_T curswant = -1;
2998
2999 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003001 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003002 return;
3003 }
3004 line = pos.lnum;
3005 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003006 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003007 if (curswant >= 0)
3008 {
3009 curwin->w_curswant = curswant - 1;
3010 set_curswant = FALSE;
3011 }
3012 }
3013 else
3014 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003015 line = tv_get_lnum(argvars);
3016 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003017 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003018 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003019 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003020 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003021 return; /* type error; errmsg already given */
3022 if (line > 0)
3023 curwin->w_cursor.lnum = line;
3024 if (col > 0)
3025 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003026 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027
3028 /* Make sure the cursor is in a valid position. */
3029 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003030 /* Correct cursor for multi-byte character. */
3031 if (has_mbyte)
3032 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033
3034 curwin->w_set_curswant = set_curswant;
3035 rettv->vval.v_number = 0;
3036}
3037
Bram Moolenaar4f974752019-02-17 17:44:42 +01003038#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003039/*
3040 * "debugbreak()" function
3041 */
3042 static void
3043f_debugbreak(typval_T *argvars, typval_T *rettv)
3044{
3045 int pid;
3046
3047 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003048 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003049 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003050 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003051 else
3052 {
3053 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3054
3055 if (hProcess != NULL)
3056 {
3057 DebugBreakProcess(hProcess);
3058 CloseHandle(hProcess);
3059 rettv->vval.v_number = OK;
3060 }
3061 }
3062}
3063#endif
3064
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003065/*
3066 * "deepcopy()" function
3067 */
3068 static void
3069f_deepcopy(typval_T *argvars, typval_T *rettv)
3070{
3071 int noref = 0;
3072 int copyID;
3073
3074 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003075 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003076 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003077 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003078 else
3079 {
3080 copyID = get_copyID();
3081 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3082 }
3083}
3084
3085/*
3086 * "delete()" function
3087 */
3088 static void
3089f_delete(typval_T *argvars, typval_T *rettv)
3090{
3091 char_u nbuf[NUMBUFLEN];
3092 char_u *name;
3093 char_u *flags;
3094
3095 rettv->vval.v_number = -1;
3096 if (check_restricted() || check_secure())
3097 return;
3098
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003099 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003100 if (name == NULL || *name == NUL)
3101 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003102 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003103 return;
3104 }
3105
3106 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003107 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003108 else
3109 flags = (char_u *)"";
3110
3111 if (*flags == NUL)
3112 /* delete a file */
3113 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3114 else if (STRCMP(flags, "d") == 0)
3115 /* delete an empty directory */
3116 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3117 else if (STRCMP(flags, "rf") == 0)
3118 /* delete a directory recursively */
3119 rettv->vval.v_number = delete_recursive(name);
3120 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003121 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003122}
3123
3124/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003125 * "deletebufline()" function
3126 */
3127 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003128f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003129{
3130 buf_T *buf;
3131 linenr_T first, last;
3132 linenr_T lnum;
3133 long count;
3134 int is_curbuf;
3135 buf_T *curbuf_save = NULL;
3136 win_T *curwin_save = NULL;
3137 tabpage_T *tp;
3138 win_T *wp;
3139
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003140 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003141 if (buf == NULL)
3142 {
3143 rettv->vval.v_number = 1; /* FAIL */
3144 return;
3145 }
3146 is_curbuf = buf == curbuf;
3147
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003148 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003149 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003150 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003151 else
3152 last = first;
3153
3154 if (buf->b_ml.ml_mfp == NULL || first < 1
3155 || first > buf->b_ml.ml_line_count || last < first)
3156 {
3157 rettv->vval.v_number = 1; /* FAIL */
3158 return;
3159 }
3160
3161 if (!is_curbuf)
3162 {
3163 curbuf_save = curbuf;
3164 curwin_save = curwin;
3165 curbuf = buf;
3166 find_win_for_curbuf();
3167 }
3168 if (last > curbuf->b_ml.ml_line_count)
3169 last = curbuf->b_ml.ml_line_count;
3170 count = last - first + 1;
3171
3172 // When coming here from Insert mode, sync undo, so that this can be
3173 // undone separately from what was previously inserted.
3174 if (u_sync_once == 2)
3175 {
3176 u_sync_once = 1; // notify that u_sync() was called
3177 u_sync(TRUE);
3178 }
3179
3180 if (u_save(first - 1, last + 1) == FAIL)
3181 {
3182 rettv->vval.v_number = 1; /* FAIL */
3183 return;
3184 }
3185
3186 for (lnum = first; lnum <= last; ++lnum)
3187 ml_delete(first, TRUE);
3188
3189 FOR_ALL_TAB_WINDOWS(tp, wp)
3190 if (wp->w_buffer == buf)
3191 {
3192 if (wp->w_cursor.lnum > last)
3193 wp->w_cursor.lnum -= count;
3194 else if (wp->w_cursor.lnum> first)
3195 wp->w_cursor.lnum = first;
3196 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3197 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3198 }
3199 check_cursor_col();
3200 deleted_lines_mark(first, count);
3201
3202 if (!is_curbuf)
3203 {
3204 curbuf = curbuf_save;
3205 curwin = curwin_save;
3206 }
3207}
3208
3209/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003210 * "did_filetype()" function
3211 */
3212 static void
3213f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3214{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003215 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003216}
3217
3218/*
3219 * "diff_filler()" function
3220 */
3221 static void
3222f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3223{
3224#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003225 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003226#endif
3227}
3228
3229/*
3230 * "diff_hlID()" function
3231 */
3232 static void
3233f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3234{
3235#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003236 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003237 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003238 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003239 static int fnum = 0;
3240 static int change_start = 0;
3241 static int change_end = 0;
3242 static hlf_T hlID = (hlf_T)0;
3243 int filler_lines;
3244 int col;
3245
3246 if (lnum < 0) /* ignore type error in {lnum} arg */
3247 lnum = 0;
3248 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003249 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003250 || fnum != curbuf->b_fnum)
3251 {
3252 /* New line, buffer, change: need to get the values. */
3253 filler_lines = diff_check(curwin, lnum);
3254 if (filler_lines < 0)
3255 {
3256 if (filler_lines == -1)
3257 {
3258 change_start = MAXCOL;
3259 change_end = -1;
3260 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3261 hlID = HLF_ADD; /* added line */
3262 else
3263 hlID = HLF_CHD; /* changed line */
3264 }
3265 else
3266 hlID = HLF_ADD; /* added line */
3267 }
3268 else
3269 hlID = (hlf_T)0;
3270 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003271 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003272 fnum = curbuf->b_fnum;
3273 }
3274
3275 if (hlID == HLF_CHD || hlID == HLF_TXD)
3276 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003277 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003278 if (col >= change_start && col <= change_end)
3279 hlID = HLF_TXD; /* changed text */
3280 else
3281 hlID = HLF_CHD; /* changed line */
3282 }
3283 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3284#endif
3285}
3286
3287/*
3288 * "empty({expr})" function
3289 */
3290 static void
3291f_empty(typval_T *argvars, typval_T *rettv)
3292{
3293 int n = FALSE;
3294
3295 switch (argvars[0].v_type)
3296 {
3297 case VAR_STRING:
3298 case VAR_FUNC:
3299 n = argvars[0].vval.v_string == NULL
3300 || *argvars[0].vval.v_string == NUL;
3301 break;
3302 case VAR_PARTIAL:
3303 n = FALSE;
3304 break;
3305 case VAR_NUMBER:
3306 n = argvars[0].vval.v_number == 0;
3307 break;
3308 case VAR_FLOAT:
3309#ifdef FEAT_FLOAT
3310 n = argvars[0].vval.v_float == 0.0;
3311 break;
3312#endif
3313 case VAR_LIST:
3314 n = argvars[0].vval.v_list == NULL
3315 || argvars[0].vval.v_list->lv_first == NULL;
3316 break;
3317 case VAR_DICT:
3318 n = argvars[0].vval.v_dict == NULL
3319 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3320 break;
3321 case VAR_SPECIAL:
3322 n = argvars[0].vval.v_number != VVAL_TRUE;
3323 break;
3324
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003325 case VAR_BLOB:
3326 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003327 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3328 break;
3329
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003330 case VAR_JOB:
3331#ifdef FEAT_JOB_CHANNEL
3332 n = argvars[0].vval.v_job == NULL
3333 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3334 break;
3335#endif
3336 case VAR_CHANNEL:
3337#ifdef FEAT_JOB_CHANNEL
3338 n = argvars[0].vval.v_channel == NULL
3339 || !channel_is_open(argvars[0].vval.v_channel);
3340 break;
3341#endif
3342 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003343 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003344 n = TRUE;
3345 break;
3346 }
3347
3348 rettv->vval.v_number = n;
3349}
3350
3351/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003352 * "environ()" function
3353 */
3354 static void
3355f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3356{
3357#if !defined(AMIGA)
3358 int i = 0;
3359 char_u *entry, *value;
3360# ifdef MSWIN
3361 extern wchar_t **_wenviron;
3362# else
3363 extern char **environ;
3364# endif
3365
3366 if (rettv_dict_alloc(rettv) != OK)
3367 return;
3368
3369# ifdef MSWIN
3370 if (*_wenviron == NULL)
3371 return;
3372# else
3373 if (*environ == NULL)
3374 return;
3375# endif
3376
3377 for (i = 0; ; ++i)
3378 {
3379# ifdef MSWIN
3380 short_u *p;
3381
3382 if ((p = (short_u *)_wenviron[i]) == NULL)
3383 return;
3384 entry = utf16_to_enc(p, NULL);
3385# else
3386 if ((entry = (char_u *)environ[i]) == NULL)
3387 return;
3388 entry = vim_strsave(entry);
3389# endif
3390 if (entry == NULL) // out of memory
3391 return;
3392 if ((value = vim_strchr(entry, '=')) == NULL)
3393 {
3394 vim_free(entry);
3395 continue;
3396 }
3397 *value++ = NUL;
3398 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3399 vim_free(entry);
3400 }
3401#endif
3402}
3403
3404/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405 * "escape({string}, {chars})" function
3406 */
3407 static void
3408f_escape(typval_T *argvars, typval_T *rettv)
3409{
3410 char_u buf[NUMBUFLEN];
3411
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003412 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3413 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003414 rettv->v_type = VAR_STRING;
3415}
3416
3417/*
3418 * "eval()" function
3419 */
3420 static void
3421f_eval(typval_T *argvars, typval_T *rettv)
3422{
3423 char_u *s, *p;
3424
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003425 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003426 if (s != NULL)
3427 s = skipwhite(s);
3428
3429 p = s;
3430 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3431 {
3432 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003433 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003434 need_clr_eos = FALSE;
3435 rettv->v_type = VAR_NUMBER;
3436 rettv->vval.v_number = 0;
3437 }
3438 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003439 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003440}
3441
3442/*
3443 * "eventhandler()" function
3444 */
3445 static void
3446f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3447{
3448 rettv->vval.v_number = vgetc_busy;
3449}
3450
3451/*
3452 * "executable()" function
3453 */
3454 static void
3455f_executable(typval_T *argvars, typval_T *rettv)
3456{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003457 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003458
3459 /* Check in $PATH and also check directly if there is a directory name. */
3460 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3461 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3462}
3463
3464static garray_T redir_execute_ga;
3465
3466/*
3467 * Append "value[value_len]" to the execute() output.
3468 */
3469 void
3470execute_redir_str(char_u *value, int value_len)
3471{
3472 int len;
3473
3474 if (value_len == -1)
3475 len = (int)STRLEN(value); /* Append the entire string */
3476 else
3477 len = value_len; /* Append only "value_len" characters */
3478 if (ga_grow(&redir_execute_ga, len) == OK)
3479 {
3480 mch_memmove((char *)redir_execute_ga.ga_data
3481 + redir_execute_ga.ga_len, value, len);
3482 redir_execute_ga.ga_len += len;
3483 }
3484}
3485
3486/*
3487 * Get next line from a list.
3488 * Called by do_cmdline() to get the next line.
3489 * Returns allocated string, or NULL for end of function.
3490 */
3491
3492 static char_u *
3493get_list_line(
3494 int c UNUSED,
3495 void *cookie,
3496 int indent UNUSED)
3497{
3498 listitem_T **p = (listitem_T **)cookie;
3499 listitem_T *item = *p;
3500 char_u buf[NUMBUFLEN];
3501 char_u *s;
3502
3503 if (item == NULL)
3504 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003505 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003506 *p = item->li_next;
3507 return s == NULL ? NULL : vim_strsave(s);
3508}
3509
3510/*
3511 * "execute()" function
3512 */
3513 static void
3514f_execute(typval_T *argvars, typval_T *rettv)
3515{
3516 char_u *cmd = NULL;
3517 list_T *list = NULL;
3518 int save_msg_silent = msg_silent;
3519 int save_emsg_silent = emsg_silent;
3520 int save_emsg_noredir = emsg_noredir;
3521 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003522 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003523 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003524 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003525 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003526
3527 rettv->vval.v_string = NULL;
3528 rettv->v_type = VAR_STRING;
3529
3530 if (argvars[0].v_type == VAR_LIST)
3531 {
3532 list = argvars[0].vval.v_list;
3533 if (list == NULL || list->lv_first == NULL)
3534 /* empty list, no commands, empty output */
3535 return;
3536 ++list->lv_refcount;
3537 }
3538 else
3539 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003540 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003541 if (cmd == NULL)
3542 return;
3543 }
3544
3545 if (argvars[1].v_type != VAR_UNKNOWN)
3546 {
3547 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003548 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003549
3550 if (s == NULL)
3551 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003552 if (*s == NUL)
3553 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003554 if (STRNCMP(s, "silent", 6) == 0)
3555 ++msg_silent;
3556 if (STRCMP(s, "silent!") == 0)
3557 {
3558 emsg_silent = TRUE;
3559 emsg_noredir = TRUE;
3560 }
3561 }
3562 else
3563 ++msg_silent;
3564
3565 if (redir_execute)
3566 save_ga = redir_execute_ga;
3567 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3568 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003569 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003570 if (!echo_output)
3571 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003572
3573 if (cmd != NULL)
3574 do_cmdline_cmd(cmd);
3575 else
3576 {
3577 listitem_T *item = list->lv_first;
3578
3579 do_cmdline(NULL, get_list_line, (void *)&item,
3580 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3581 --list->lv_refcount;
3582 }
3583
Bram Moolenaard297f352017-01-29 20:31:21 +01003584 /* Need to append a NUL to the result. */
3585 if (ga_grow(&redir_execute_ga, 1) == OK)
3586 {
3587 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3588 rettv->vval.v_string = redir_execute_ga.ga_data;
3589 }
3590 else
3591 {
3592 ga_clear(&redir_execute_ga);
3593 rettv->vval.v_string = NULL;
3594 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003595 msg_silent = save_msg_silent;
3596 emsg_silent = save_emsg_silent;
3597 emsg_noredir = save_emsg_noredir;
3598
3599 redir_execute = save_redir_execute;
3600 if (redir_execute)
3601 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003602 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003603
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003604 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003605 if (echo_output)
3606 // When not working silently: put it in column zero. A following
3607 // "echon" will overwrite the message, unavoidably.
3608 msg_col = 0;
3609 else
3610 // When working silently: Put it back where it was, since nothing
3611 // should have been written.
3612 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003613}
3614
3615/*
3616 * "exepath()" function
3617 */
3618 static void
3619f_exepath(typval_T *argvars, typval_T *rettv)
3620{
3621 char_u *p = NULL;
3622
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003623 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003624 rettv->v_type = VAR_STRING;
3625 rettv->vval.v_string = p;
3626}
3627
3628/*
3629 * "exists()" function
3630 */
3631 static void
3632f_exists(typval_T *argvars, typval_T *rettv)
3633{
3634 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003635 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003636
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003637 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003638 if (*p == '$') /* environment variable */
3639 {
3640 /* first try "normal" environment variables (fast) */
3641 if (mch_getenv(p + 1) != NULL)
3642 n = TRUE;
3643 else
3644 {
3645 /* try expanding things like $VIM and ${HOME} */
3646 p = expand_env_save(p);
3647 if (p != NULL && *p != '$')
3648 n = TRUE;
3649 vim_free(p);
3650 }
3651 }
3652 else if (*p == '&' || *p == '+') /* option */
3653 {
3654 n = (get_option_tv(&p, NULL, TRUE) == OK);
3655 if (*skipwhite(p) != NUL)
3656 n = FALSE; /* trailing garbage */
3657 }
3658 else if (*p == '*') /* internal or user defined function */
3659 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003660 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003661 }
3662 else if (*p == ':')
3663 {
3664 n = cmd_exists(p + 1);
3665 }
3666 else if (*p == '#')
3667 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003668 if (p[1] == '#')
3669 n = autocmd_supported(p + 2);
3670 else
3671 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003672 }
3673 else /* internal variable */
3674 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003675 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003676 }
3677
3678 rettv->vval.v_number = n;
3679}
3680
3681#ifdef FEAT_FLOAT
3682/*
3683 * "exp()" function
3684 */
3685 static void
3686f_exp(typval_T *argvars, typval_T *rettv)
3687{
3688 float_T f = 0.0;
3689
3690 rettv->v_type = VAR_FLOAT;
3691 if (get_float_arg(argvars, &f) == OK)
3692 rettv->vval.v_float = exp(f);
3693 else
3694 rettv->vval.v_float = 0.0;
3695}
3696#endif
3697
3698/*
3699 * "expand()" function
3700 */
3701 static void
3702f_expand(typval_T *argvars, typval_T *rettv)
3703{
3704 char_u *s;
3705 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003706 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003707 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3708 expand_T xpc;
3709 int error = FALSE;
3710 char_u *result;
3711
3712 rettv->v_type = VAR_STRING;
3713 if (argvars[1].v_type != VAR_UNKNOWN
3714 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003715 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003716 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003717 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003718
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003719 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003720 if (*s == '%' || *s == '#' || *s == '<')
3721 {
3722 ++emsg_off;
3723 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3724 --emsg_off;
3725 if (rettv->v_type == VAR_LIST)
3726 {
3727 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3728 list_append_string(rettv->vval.v_list, result, -1);
3729 else
3730 vim_free(result);
3731 }
3732 else
3733 rettv->vval.v_string = result;
3734 }
3735 else
3736 {
3737 /* When the optional second argument is non-zero, don't remove matches
3738 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3739 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003740 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003741 options |= WILD_KEEP_ALL;
3742 if (!error)
3743 {
3744 ExpandInit(&xpc);
3745 xpc.xp_context = EXPAND_FILES;
3746 if (p_wic)
3747 options += WILD_ICASE;
3748 if (rettv->v_type == VAR_STRING)
3749 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3750 options, WILD_ALL);
3751 else if (rettv_list_alloc(rettv) != FAIL)
3752 {
3753 int i;
3754
3755 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3756 for (i = 0; i < xpc.xp_numfiles; i++)
3757 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3758 ExpandCleanup(&xpc);
3759 }
3760 }
3761 else
3762 rettv->vval.v_string = NULL;
3763 }
3764}
3765
3766/*
3767 * "extend(list, list [, idx])" function
3768 * "extend(dict, dict [, action])" function
3769 */
3770 static void
3771f_extend(typval_T *argvars, typval_T *rettv)
3772{
3773 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3774
3775 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3776 {
3777 list_T *l1, *l2;
3778 listitem_T *item;
3779 long before;
3780 int error = FALSE;
3781
3782 l1 = argvars[0].vval.v_list;
3783 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003784 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003785 && l2 != NULL)
3786 {
3787 if (argvars[2].v_type != VAR_UNKNOWN)
3788 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003789 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003790 if (error)
3791 return; /* type error; errmsg already given */
3792
3793 if (before == l1->lv_len)
3794 item = NULL;
3795 else
3796 {
3797 item = list_find(l1, before);
3798 if (item == NULL)
3799 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003800 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003801 return;
3802 }
3803 }
3804 }
3805 else
3806 item = NULL;
3807 list_extend(l1, l2, item);
3808
3809 copy_tv(&argvars[0], rettv);
3810 }
3811 }
3812 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3813 {
3814 dict_T *d1, *d2;
3815 char_u *action;
3816 int i;
3817
3818 d1 = argvars[0].vval.v_dict;
3819 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003820 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003821 && d2 != NULL)
3822 {
3823 /* Check the third argument. */
3824 if (argvars[2].v_type != VAR_UNKNOWN)
3825 {
3826 static char *(av[]) = {"keep", "force", "error"};
3827
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003828 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003829 if (action == NULL)
3830 return; /* type error; errmsg already given */
3831 for (i = 0; i < 3; ++i)
3832 if (STRCMP(action, av[i]) == 0)
3833 break;
3834 if (i == 3)
3835 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003836 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003837 return;
3838 }
3839 }
3840 else
3841 action = (char_u *)"force";
3842
3843 dict_extend(d1, d2, action);
3844
3845 copy_tv(&argvars[0], rettv);
3846 }
3847 }
3848 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003849 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003850}
3851
3852/*
3853 * "feedkeys()" function
3854 */
3855 static void
3856f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3857{
3858 int remap = TRUE;
3859 int insert = FALSE;
3860 char_u *keys, *flags;
3861 char_u nbuf[NUMBUFLEN];
3862 int typed = FALSE;
3863 int execute = FALSE;
3864 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003865 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003866 char_u *keys_esc;
3867
3868 /* This is not allowed in the sandbox. If the commands would still be
3869 * executed in the sandbox it would be OK, but it probably happens later,
3870 * when "sandbox" is no longer set. */
3871 if (check_secure())
3872 return;
3873
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003874 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003875
3876 if (argvars[1].v_type != VAR_UNKNOWN)
3877 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003878 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003879 for ( ; *flags != NUL; ++flags)
3880 {
3881 switch (*flags)
3882 {
3883 case 'n': remap = FALSE; break;
3884 case 'm': remap = TRUE; break;
3885 case 't': typed = TRUE; break;
3886 case 'i': insert = TRUE; break;
3887 case 'x': execute = TRUE; break;
3888 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003889 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003890 }
3891 }
3892 }
3893
3894 if (*keys != NUL || execute)
3895 {
3896 /* Need to escape K_SPECIAL and CSI before putting the string in the
3897 * typeahead buffer. */
3898 keys_esc = vim_strsave_escape_csi(keys);
3899 if (keys_esc != NULL)
3900 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003901 if (lowlevel)
3902 {
3903#ifdef USE_INPUT_BUF
3904 add_to_input_buf(keys, (int)STRLEN(keys));
3905#else
3906 emsg(_("E980: lowlevel input not supported"));
3907#endif
3908 }
3909 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003910 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003911 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003912 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003913 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003914#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003915 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003916#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003917 )
3918 typebuf_was_filled = TRUE;
3919 }
3920 vim_free(keys_esc);
3921
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003922 if (execute)
3923 {
3924 int save_msg_scroll = msg_scroll;
3925
3926 /* Avoid a 1 second delay when the keys start Insert mode. */
3927 msg_scroll = FALSE;
3928
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003929 if (!dangerous)
3930 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003931 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003932 if (!dangerous)
3933 --ex_normal_busy;
3934
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003935 msg_scroll |= save_msg_scroll;
3936 }
3937 }
3938 }
3939}
3940
3941/*
3942 * "filereadable()" function
3943 */
3944 static void
3945f_filereadable(typval_T *argvars, typval_T *rettv)
3946{
3947 int fd;
3948 char_u *p;
3949 int n;
3950
3951#ifndef O_NONBLOCK
3952# define O_NONBLOCK 0
3953#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003954 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003955 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3956 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3957 {
3958 n = TRUE;
3959 close(fd);
3960 }
3961 else
3962 n = FALSE;
3963
3964 rettv->vval.v_number = n;
3965}
3966
3967/*
3968 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3969 * rights to write into.
3970 */
3971 static void
3972f_filewritable(typval_T *argvars, typval_T *rettv)
3973{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003974 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003975}
3976
3977 static void
3978findfilendir(
3979 typval_T *argvars UNUSED,
3980 typval_T *rettv,
3981 int find_what UNUSED)
3982{
3983#ifdef FEAT_SEARCHPATH
3984 char_u *fname;
3985 char_u *fresult = NULL;
3986 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3987 char_u *p;
3988 char_u pathbuf[NUMBUFLEN];
3989 int count = 1;
3990 int first = TRUE;
3991 int error = FALSE;
3992#endif
3993
3994 rettv->vval.v_string = NULL;
3995 rettv->v_type = VAR_STRING;
3996
3997#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003998 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003999
4000 if (argvars[1].v_type != VAR_UNKNOWN)
4001 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004002 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004003 if (p == NULL)
4004 error = TRUE;
4005 else
4006 {
4007 if (*p != NUL)
4008 path = p;
4009
4010 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004011 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004012 }
4013 }
4014
4015 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4016 error = TRUE;
4017
4018 if (*fname != NUL && !error)
4019 {
4020 do
4021 {
4022 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4023 vim_free(fresult);
4024 fresult = find_file_in_path_option(first ? fname : NULL,
4025 first ? (int)STRLEN(fname) : 0,
4026 0, first, path,
4027 find_what,
4028 curbuf->b_ffname,
4029 find_what == FINDFILE_DIR
4030 ? (char_u *)"" : curbuf->b_p_sua);
4031 first = FALSE;
4032
4033 if (fresult != NULL && rettv->v_type == VAR_LIST)
4034 list_append_string(rettv->vval.v_list, fresult, -1);
4035
4036 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4037 }
4038
4039 if (rettv->v_type == VAR_STRING)
4040 rettv->vval.v_string = fresult;
4041#endif
4042}
4043
4044/*
4045 * "filter()" function
4046 */
4047 static void
4048f_filter(typval_T *argvars, typval_T *rettv)
4049{
4050 filter_map(argvars, rettv, FALSE);
4051}
4052
4053/*
4054 * "finddir({fname}[, {path}[, {count}]])" function
4055 */
4056 static void
4057f_finddir(typval_T *argvars, typval_T *rettv)
4058{
4059 findfilendir(argvars, rettv, FINDFILE_DIR);
4060}
4061
4062/*
4063 * "findfile({fname}[, {path}[, {count}]])" function
4064 */
4065 static void
4066f_findfile(typval_T *argvars, typval_T *rettv)
4067{
4068 findfilendir(argvars, rettv, FINDFILE_FILE);
4069}
4070
4071#ifdef FEAT_FLOAT
4072/*
4073 * "float2nr({float})" function
4074 */
4075 static void
4076f_float2nr(typval_T *argvars, typval_T *rettv)
4077{
4078 float_T f = 0.0;
4079
4080 if (get_float_arg(argvars, &f) == OK)
4081 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004082 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004083 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004084 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004085 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004086 else
4087 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004088 }
4089}
4090
4091/*
4092 * "floor({float})" function
4093 */
4094 static void
4095f_floor(typval_T *argvars, typval_T *rettv)
4096{
4097 float_T f = 0.0;
4098
4099 rettv->v_type = VAR_FLOAT;
4100 if (get_float_arg(argvars, &f) == OK)
4101 rettv->vval.v_float = floor(f);
4102 else
4103 rettv->vval.v_float = 0.0;
4104}
4105
4106/*
4107 * "fmod()" function
4108 */
4109 static void
4110f_fmod(typval_T *argvars, typval_T *rettv)
4111{
4112 float_T fx = 0.0, fy = 0.0;
4113
4114 rettv->v_type = VAR_FLOAT;
4115 if (get_float_arg(argvars, &fx) == OK
4116 && get_float_arg(&argvars[1], &fy) == OK)
4117 rettv->vval.v_float = fmod(fx, fy);
4118 else
4119 rettv->vval.v_float = 0.0;
4120}
4121#endif
4122
4123/*
4124 * "fnameescape({string})" function
4125 */
4126 static void
4127f_fnameescape(typval_T *argvars, typval_T *rettv)
4128{
4129 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004130 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004131 rettv->v_type = VAR_STRING;
4132}
4133
4134/*
4135 * "fnamemodify({fname}, {mods})" function
4136 */
4137 static void
4138f_fnamemodify(typval_T *argvars, typval_T *rettv)
4139{
4140 char_u *fname;
4141 char_u *mods;
4142 int usedlen = 0;
4143 int len;
4144 char_u *fbuf = NULL;
4145 char_u buf[NUMBUFLEN];
4146
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004147 fname = tv_get_string_chk(&argvars[0]);
4148 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004149 if (fname == NULL || mods == NULL)
4150 fname = NULL;
4151 else
4152 {
4153 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004154 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004155 }
4156
4157 rettv->v_type = VAR_STRING;
4158 if (fname == NULL)
4159 rettv->vval.v_string = NULL;
4160 else
4161 rettv->vval.v_string = vim_strnsave(fname, len);
4162 vim_free(fbuf);
4163}
4164
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004165/*
4166 * "foldclosed()" function
4167 */
4168 static void
4169foldclosed_both(
4170 typval_T *argvars UNUSED,
4171 typval_T *rettv,
4172 int end UNUSED)
4173{
4174#ifdef FEAT_FOLDING
4175 linenr_T lnum;
4176 linenr_T first, last;
4177
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004178 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004179 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4180 {
4181 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4182 {
4183 if (end)
4184 rettv->vval.v_number = (varnumber_T)last;
4185 else
4186 rettv->vval.v_number = (varnumber_T)first;
4187 return;
4188 }
4189 }
4190#endif
4191 rettv->vval.v_number = -1;
4192}
4193
4194/*
4195 * "foldclosed()" function
4196 */
4197 static void
4198f_foldclosed(typval_T *argvars, typval_T *rettv)
4199{
4200 foldclosed_both(argvars, rettv, FALSE);
4201}
4202
4203/*
4204 * "foldclosedend()" function
4205 */
4206 static void
4207f_foldclosedend(typval_T *argvars, typval_T *rettv)
4208{
4209 foldclosed_both(argvars, rettv, TRUE);
4210}
4211
4212/*
4213 * "foldlevel()" function
4214 */
4215 static void
4216f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4217{
4218#ifdef FEAT_FOLDING
4219 linenr_T lnum;
4220
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004221 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004222 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4223 rettv->vval.v_number = foldLevel(lnum);
4224#endif
4225}
4226
4227/*
4228 * "foldtext()" function
4229 */
4230 static void
4231f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4232{
4233#ifdef FEAT_FOLDING
4234 linenr_T foldstart;
4235 linenr_T foldend;
4236 char_u *dashes;
4237 linenr_T lnum;
4238 char_u *s;
4239 char_u *r;
4240 int len;
4241 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004242 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004243#endif
4244
4245 rettv->v_type = VAR_STRING;
4246 rettv->vval.v_string = NULL;
4247#ifdef FEAT_FOLDING
4248 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4249 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4250 dashes = get_vim_var_str(VV_FOLDDASHES);
4251 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4252 && dashes != NULL)
4253 {
4254 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004255 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004256 if (!linewhite(lnum))
4257 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004258
4259 /* Find interesting text in this line. */
4260 s = skipwhite(ml_get(lnum));
4261 /* skip C comment-start */
4262 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4263 {
4264 s = skipwhite(s + 2);
4265 if (*skipwhite(s) == NUL
4266 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4267 {
4268 s = skipwhite(ml_get(lnum + 1));
4269 if (*s == '*')
4270 s = skipwhite(s + 1);
4271 }
4272 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004273 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004274 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004275 r = alloc((unsigned)(STRLEN(txt)
4276 + STRLEN(dashes) /* for %s */
4277 + 20 /* for %3ld */
4278 + STRLEN(s))); /* concatenated */
4279 if (r != NULL)
4280 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004281 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004282 len = (int)STRLEN(r);
4283 STRCAT(r, s);
4284 /* remove 'foldmarker' and 'commentstring' */
4285 foldtext_cleanup(r + len);
4286 rettv->vval.v_string = r;
4287 }
4288 }
4289#endif
4290}
4291
4292/*
4293 * "foldtextresult(lnum)" function
4294 */
4295 static void
4296f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4297{
4298#ifdef FEAT_FOLDING
4299 linenr_T lnum;
4300 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004301 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004302 foldinfo_T foldinfo;
4303 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004304 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004305#endif
4306
4307 rettv->v_type = VAR_STRING;
4308 rettv->vval.v_string = NULL;
4309#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004310 if (entered)
4311 return; /* reject recursive use */
4312 entered = TRUE;
4313
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004314 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004315 /* treat illegal types and illegal string values for {lnum} the same */
4316 if (lnum < 0)
4317 lnum = 0;
4318 fold_count = foldedCount(curwin, lnum, &foldinfo);
4319 if (fold_count > 0)
4320 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004321 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4322 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004323 if (text == buf)
4324 text = vim_strsave(text);
4325 rettv->vval.v_string = text;
4326 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004327
4328 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004329#endif
4330}
4331
4332/*
4333 * "foreground()" function
4334 */
4335 static void
4336f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4337{
4338#ifdef FEAT_GUI
4339 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004340 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004341 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004342 return;
4343 }
4344#endif
4345#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004346 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004347#endif
4348}
4349
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004350 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004351common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004352{
4353 char_u *s;
4354 char_u *name;
4355 int use_string = FALSE;
4356 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004357 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004358
4359 if (argvars[0].v_type == VAR_FUNC)
4360 {
4361 /* function(MyFunc, [arg], dict) */
4362 s = argvars[0].vval.v_string;
4363 }
4364 else if (argvars[0].v_type == VAR_PARTIAL
4365 && argvars[0].vval.v_partial != NULL)
4366 {
4367 /* function(dict.MyFunc, [arg]) */
4368 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004369 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004370 }
4371 else
4372 {
4373 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004374 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004375 use_string = TRUE;
4376 }
4377
Bram Moolenaar843b8842016-08-21 14:36:15 +02004378 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004379 {
4380 name = s;
4381 trans_name = trans_function_name(&name, FALSE,
4382 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4383 if (*name != NUL)
4384 s = NULL;
4385 }
4386
Bram Moolenaar843b8842016-08-21 14:36:15 +02004387 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4388 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004389 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004390 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004391 else if (trans_name != NULL && (is_funcref
4392 ? find_func(trans_name) == NULL
4393 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004394 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004395 else
4396 {
4397 int dict_idx = 0;
4398 int arg_idx = 0;
4399 list_T *list = NULL;
4400
4401 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4402 {
4403 char sid_buf[25];
4404 int off = *s == 's' ? 2 : 5;
4405
4406 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4407 * also be called from another script. Using trans_function_name()
4408 * would also work, but some plugins depend on the name being
4409 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004410 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004411 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4412 if (name != NULL)
4413 {
4414 STRCPY(name, sid_buf);
4415 STRCAT(name, s + off);
4416 }
4417 }
4418 else
4419 name = vim_strsave(s);
4420
4421 if (argvars[1].v_type != VAR_UNKNOWN)
4422 {
4423 if (argvars[2].v_type != VAR_UNKNOWN)
4424 {
4425 /* function(name, [args], dict) */
4426 arg_idx = 1;
4427 dict_idx = 2;
4428 }
4429 else if (argvars[1].v_type == VAR_DICT)
4430 /* function(name, dict) */
4431 dict_idx = 1;
4432 else
4433 /* function(name, [args]) */
4434 arg_idx = 1;
4435 if (dict_idx > 0)
4436 {
4437 if (argvars[dict_idx].v_type != VAR_DICT)
4438 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004439 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004440 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004441 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004442 }
4443 if (argvars[dict_idx].vval.v_dict == NULL)
4444 dict_idx = 0;
4445 }
4446 if (arg_idx > 0)
4447 {
4448 if (argvars[arg_idx].v_type != VAR_LIST)
4449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004450 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004451 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004452 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004453 }
4454 list = argvars[arg_idx].vval.v_list;
4455 if (list == NULL || list->lv_len == 0)
4456 arg_idx = 0;
4457 }
4458 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004459 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004460 {
4461 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4462
4463 /* result is a VAR_PARTIAL */
4464 if (pt == NULL)
4465 vim_free(name);
4466 else
4467 {
4468 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4469 {
4470 listitem_T *li;
4471 int i = 0;
4472 int arg_len = 0;
4473 int lv_len = 0;
4474
4475 if (arg_pt != NULL)
4476 arg_len = arg_pt->pt_argc;
4477 if (list != NULL)
4478 lv_len = list->lv_len;
4479 pt->pt_argc = arg_len + lv_len;
4480 pt->pt_argv = (typval_T *)alloc(
4481 sizeof(typval_T) * pt->pt_argc);
4482 if (pt->pt_argv == NULL)
4483 {
4484 vim_free(pt);
4485 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004486 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004487 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004488 for (i = 0; i < arg_len; i++)
4489 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4490 if (lv_len > 0)
4491 for (li = list->lv_first; li != NULL;
4492 li = li->li_next)
4493 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004494 }
4495
4496 /* For "function(dict.func, [], dict)" and "func" is a partial
4497 * use "dict". That is backwards compatible. */
4498 if (dict_idx > 0)
4499 {
4500 /* The dict is bound explicitly, pt_auto is FALSE. */
4501 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4502 ++pt->pt_dict->dv_refcount;
4503 }
4504 else if (arg_pt != NULL)
4505 {
4506 /* If the dict was bound automatically the result is also
4507 * bound automatically. */
4508 pt->pt_dict = arg_pt->pt_dict;
4509 pt->pt_auto = arg_pt->pt_auto;
4510 if (pt->pt_dict != NULL)
4511 ++pt->pt_dict->dv_refcount;
4512 }
4513
4514 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004515 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4516 {
4517 pt->pt_func = arg_pt->pt_func;
4518 func_ptr_ref(pt->pt_func);
4519 vim_free(name);
4520 }
4521 else if (is_funcref)
4522 {
4523 pt->pt_func = find_func(trans_name);
4524 func_ptr_ref(pt->pt_func);
4525 vim_free(name);
4526 }
4527 else
4528 {
4529 pt->pt_name = name;
4530 func_ref(name);
4531 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004532 }
4533 rettv->v_type = VAR_PARTIAL;
4534 rettv->vval.v_partial = pt;
4535 }
4536 else
4537 {
4538 /* result is a VAR_FUNC */
4539 rettv->v_type = VAR_FUNC;
4540 rettv->vval.v_string = name;
4541 func_ref(name);
4542 }
4543 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004544theend:
4545 vim_free(trans_name);
4546}
4547
4548/*
4549 * "funcref()" function
4550 */
4551 static void
4552f_funcref(typval_T *argvars, typval_T *rettv)
4553{
4554 common_function(argvars, rettv, TRUE);
4555}
4556
4557/*
4558 * "function()" function
4559 */
4560 static void
4561f_function(typval_T *argvars, typval_T *rettv)
4562{
4563 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004564}
4565
4566/*
4567 * "garbagecollect()" function
4568 */
4569 static void
4570f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4571{
4572 /* This is postponed until we are back at the toplevel, because we may be
4573 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4574 want_garbage_collect = TRUE;
4575
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004576 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004577 garbage_collect_at_exit = TRUE;
4578}
4579
4580/*
4581 * "get()" function
4582 */
4583 static void
4584f_get(typval_T *argvars, typval_T *rettv)
4585{
4586 listitem_T *li;
4587 list_T *l;
4588 dictitem_T *di;
4589 dict_T *d;
4590 typval_T *tv = NULL;
4591
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004592 if (argvars[0].v_type == VAR_BLOB)
4593 {
4594 int error = FALSE;
4595 int idx = tv_get_number_chk(&argvars[1], &error);
4596
4597 if (!error)
4598 {
4599 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004600 if (idx < 0)
4601 idx = blob_len(argvars[0].vval.v_blob) + idx;
4602 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4603 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004604 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004605 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004606 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004607 tv = rettv;
4608 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004609 }
4610 }
4611 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004612 {
4613 if ((l = argvars[0].vval.v_list) != NULL)
4614 {
4615 int error = FALSE;
4616
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004617 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004618 if (!error && li != NULL)
4619 tv = &li->li_tv;
4620 }
4621 }
4622 else if (argvars[0].v_type == VAR_DICT)
4623 {
4624 if ((d = argvars[0].vval.v_dict) != NULL)
4625 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004626 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004627 if (di != NULL)
4628 tv = &di->di_tv;
4629 }
4630 }
4631 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4632 {
4633 partial_T *pt;
4634 partial_T fref_pt;
4635
4636 if (argvars[0].v_type == VAR_PARTIAL)
4637 pt = argvars[0].vval.v_partial;
4638 else
4639 {
4640 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4641 fref_pt.pt_name = argvars[0].vval.v_string;
4642 pt = &fref_pt;
4643 }
4644
4645 if (pt != NULL)
4646 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004647 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004648 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004649
4650 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4651 {
4652 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004653 n = partial_name(pt);
4654 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004655 rettv->vval.v_string = NULL;
4656 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004657 {
4658 rettv->vval.v_string = vim_strsave(n);
4659 if (rettv->v_type == VAR_FUNC)
4660 func_ref(rettv->vval.v_string);
4661 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004662 }
4663 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004664 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004665 else if (STRCMP(what, "args") == 0)
4666 {
4667 rettv->v_type = VAR_LIST;
4668 if (rettv_list_alloc(rettv) == OK)
4669 {
4670 int i;
4671
4672 for (i = 0; i < pt->pt_argc; ++i)
4673 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4674 }
4675 }
4676 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004677 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004678 return;
4679 }
4680 }
4681 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004682 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004683
4684 if (tv == NULL)
4685 {
4686 if (argvars[2].v_type != VAR_UNKNOWN)
4687 copy_tv(&argvars[2], rettv);
4688 }
4689 else
4690 copy_tv(tv, rettv);
4691}
4692
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004693/*
4694 * Returns buffer options, variables and other attributes in a dictionary.
4695 */
4696 static dict_T *
4697get_buffer_info(buf_T *buf)
4698{
4699 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004700 tabpage_T *tp;
4701 win_T *wp;
4702 list_T *windows;
4703
4704 dict = dict_alloc();
4705 if (dict == NULL)
4706 return NULL;
4707
Bram Moolenaare0be1672018-07-08 16:50:37 +02004708 dict_add_number(dict, "bufnr", buf->b_fnum);
4709 dict_add_string(dict, "name", buf->b_ffname);
4710 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4711 : buflist_findlnum(buf));
4712 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4713 dict_add_number(dict, "listed", buf->b_p_bl);
4714 dict_add_number(dict, "changed", bufIsChanged(buf));
4715 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4716 dict_add_number(dict, "hidden",
4717 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004718
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004719 /* Get a reference to buffer variables */
4720 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004721
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004722 /* List of windows displaying this buffer */
4723 windows = list_alloc();
4724 if (windows != NULL)
4725 {
4726 FOR_ALL_TAB_WINDOWS(tp, wp)
4727 if (wp->w_buffer == buf)
4728 list_append_number(windows, (varnumber_T)wp->w_id);
4729 dict_add_list(dict, "windows", windows);
4730 }
4731
4732#ifdef FEAT_SIGNS
4733 if (buf->b_signlist != NULL)
4734 {
4735 /* List of signs placed in this buffer */
4736 list_T *signs = list_alloc();
4737 if (signs != NULL)
4738 {
4739 get_buffer_signs(buf, signs);
4740 dict_add_list(dict, "signs", signs);
4741 }
4742 }
4743#endif
4744
4745 return dict;
4746}
4747
4748/*
4749 * "getbufinfo()" function
4750 */
4751 static void
4752f_getbufinfo(typval_T *argvars, typval_T *rettv)
4753{
4754 buf_T *buf = NULL;
4755 buf_T *argbuf = NULL;
4756 dict_T *d;
4757 int filtered = FALSE;
4758 int sel_buflisted = FALSE;
4759 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004760 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004761
4762 if (rettv_list_alloc(rettv) != OK)
4763 return;
4764
4765 /* List of all the buffers or selected buffers */
4766 if (argvars[0].v_type == VAR_DICT)
4767 {
4768 dict_T *sel_d = argvars[0].vval.v_dict;
4769
4770 if (sel_d != NULL)
4771 {
4772 dictitem_T *di;
4773
4774 filtered = TRUE;
4775
4776 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004777 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004778 sel_buflisted = TRUE;
4779
4780 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004781 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004782 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004783
4784 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004785 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004786 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004787 }
4788 }
4789 else if (argvars[0].v_type != VAR_UNKNOWN)
4790 {
4791 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004792 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004793 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004794 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004795 --emsg_off;
4796 if (argbuf == NULL)
4797 return;
4798 }
4799
4800 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004801 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004802 {
4803 if (argbuf != NULL && argbuf != buf)
4804 continue;
4805 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004806 || (sel_buflisted && !buf->b_p_bl)
4807 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004808 continue;
4809
4810 d = get_buffer_info(buf);
4811 if (d != NULL)
4812 list_append_dict(rettv->vval.v_list, d);
4813 if (argbuf != NULL)
4814 return;
4815 }
4816}
4817
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004818/*
4819 * Get line or list of lines from buffer "buf" into "rettv".
4820 * Return a range (from start to end) of lines in rettv from the specified
4821 * buffer.
4822 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4823 */
4824 static void
4825get_buffer_lines(
4826 buf_T *buf,
4827 linenr_T start,
4828 linenr_T end,
4829 int retlist,
4830 typval_T *rettv)
4831{
4832 char_u *p;
4833
4834 rettv->v_type = VAR_STRING;
4835 rettv->vval.v_string = NULL;
4836 if (retlist && rettv_list_alloc(rettv) == FAIL)
4837 return;
4838
4839 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4840 return;
4841
4842 if (!retlist)
4843 {
4844 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4845 p = ml_get_buf(buf, start, FALSE);
4846 else
4847 p = (char_u *)"";
4848 rettv->vval.v_string = vim_strsave(p);
4849 }
4850 else
4851 {
4852 if (end < start)
4853 return;
4854
4855 if (start < 1)
4856 start = 1;
4857 if (end > buf->b_ml.ml_line_count)
4858 end = buf->b_ml.ml_line_count;
4859 while (start <= end)
4860 if (list_append_string(rettv->vval.v_list,
4861 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4862 break;
4863 }
4864}
4865
4866/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004867 * "getbufline()" function
4868 */
4869 static void
4870f_getbufline(typval_T *argvars, typval_T *rettv)
4871{
4872 linenr_T lnum;
4873 linenr_T end;
4874 buf_T *buf;
4875
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004876 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004877 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004878 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004879 --emsg_off;
4880
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004881 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004882 if (argvars[2].v_type == VAR_UNKNOWN)
4883 end = lnum;
4884 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004885 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004886
4887 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4888}
4889
4890/*
4891 * "getbufvar()" function
4892 */
4893 static void
4894f_getbufvar(typval_T *argvars, typval_T *rettv)
4895{
4896 buf_T *buf;
4897 buf_T *save_curbuf;
4898 char_u *varname;
4899 dictitem_T *v;
4900 int done = FALSE;
4901
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004902 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4903 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004904 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004905 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004906
4907 rettv->v_type = VAR_STRING;
4908 rettv->vval.v_string = NULL;
4909
4910 if (buf != NULL && varname != NULL)
4911 {
4912 /* set curbuf to be our buf, temporarily */
4913 save_curbuf = curbuf;
4914 curbuf = buf;
4915
Bram Moolenaar30567352016-08-27 21:25:44 +02004916 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004917 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004918 if (varname[1] == NUL)
4919 {
4920 /* get all buffer-local options in a dict */
4921 dict_T *opts = get_winbuf_options(TRUE);
4922
4923 if (opts != NULL)
4924 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004925 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004926 done = TRUE;
4927 }
4928 }
4929 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4930 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004931 done = TRUE;
4932 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004933 else
4934 {
4935 /* Look up the variable. */
4936 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4937 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4938 'b', varname, FALSE);
4939 if (v != NULL)
4940 {
4941 copy_tv(&v->di_tv, rettv);
4942 done = TRUE;
4943 }
4944 }
4945
4946 /* restore previous notion of curbuf */
4947 curbuf = save_curbuf;
4948 }
4949
4950 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4951 /* use the default value */
4952 copy_tv(&argvars[2], rettv);
4953
4954 --emsg_off;
4955}
4956
4957/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004958 * "getchangelist()" function
4959 */
4960 static void
4961f_getchangelist(typval_T *argvars, typval_T *rettv)
4962{
4963#ifdef FEAT_JUMPLIST
4964 buf_T *buf;
4965 int i;
4966 list_T *l;
4967 dict_T *d;
4968#endif
4969
4970 if (rettv_list_alloc(rettv) != OK)
4971 return;
4972
4973#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004974 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004975 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004976 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004977 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004978 if (buf == NULL)
4979 return;
4980
4981 l = list_alloc();
4982 if (l == NULL)
4983 return;
4984
4985 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4986 return;
4987 /*
4988 * The current window change list index tracks only the position in the
4989 * current buffer change list. For other buffers, use the change list
4990 * length as the current index.
4991 */
4992 list_append_number(rettv->vval.v_list,
4993 (varnumber_T)((buf == curwin->w_buffer)
4994 ? curwin->w_changelistidx : buf->b_changelistlen));
4995
4996 for (i = 0; i < buf->b_changelistlen; ++i)
4997 {
4998 if (buf->b_changelist[i].lnum == 0)
4999 continue;
5000 if ((d = dict_alloc()) == NULL)
5001 return;
5002 if (list_append_dict(l, d) == FAIL)
5003 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005004 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5005 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005006 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005007 }
5008#endif
5009}
5010/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005011 * "getchar()" function
5012 */
5013 static void
5014f_getchar(typval_T *argvars, typval_T *rettv)
5015{
5016 varnumber_T n;
5017 int error = FALSE;
5018
Bram Moolenaar84d93902018-09-11 20:10:20 +02005019#ifdef MESSAGE_QUEUE
5020 // vpeekc() used to check for messages, but that caused problems, invoking
5021 // a callback where it was not expected. Some plugins use getchar(1) in a
5022 // loop to await a message, therefore make sure we check for messages here.
5023 parse_queued_messages();
5024#endif
5025
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005026 /* Position the cursor. Needed after a message that ends in a space. */
5027 windgoto(msg_row, msg_col);
5028
5029 ++no_mapping;
5030 ++allow_keys;
5031 for (;;)
5032 {
5033 if (argvars[0].v_type == VAR_UNKNOWN)
5034 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005035 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005036 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005037 /* getchar(1): only check if char avail */
5038 n = vpeekc_any();
5039 else if (error || vpeekc_any() == NUL)
5040 /* illegal argument or getchar(0) and no char avail: return zero */
5041 n = 0;
5042 else
5043 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005044 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005045
5046 if (n == K_IGNORE)
5047 continue;
5048 break;
5049 }
5050 --no_mapping;
5051 --allow_keys;
5052
5053 set_vim_var_nr(VV_MOUSE_WIN, 0);
5054 set_vim_var_nr(VV_MOUSE_WINID, 0);
5055 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5056 set_vim_var_nr(VV_MOUSE_COL, 0);
5057
5058 rettv->vval.v_number = n;
5059 if (IS_SPECIAL(n) || mod_mask != 0)
5060 {
5061 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5062 int i = 0;
5063
5064 /* Turn a special key into three bytes, plus modifier. */
5065 if (mod_mask != 0)
5066 {
5067 temp[i++] = K_SPECIAL;
5068 temp[i++] = KS_MODIFIER;
5069 temp[i++] = mod_mask;
5070 }
5071 if (IS_SPECIAL(n))
5072 {
5073 temp[i++] = K_SPECIAL;
5074 temp[i++] = K_SECOND(n);
5075 temp[i++] = K_THIRD(n);
5076 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005077 else if (has_mbyte)
5078 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005079 else
5080 temp[i++] = n;
5081 temp[i++] = NUL;
5082 rettv->v_type = VAR_STRING;
5083 rettv->vval.v_string = vim_strsave(temp);
5084
5085#ifdef FEAT_MOUSE
5086 if (is_mouse_key(n))
5087 {
5088 int row = mouse_row;
5089 int col = mouse_col;
5090 win_T *win;
5091 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005092 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005093 int winnr = 1;
5094
5095 if (row >= 0 && col >= 0)
5096 {
5097 /* Find the window at the mouse coordinates and compute the
5098 * text position. */
5099 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005100 if (win == NULL)
5101 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005102 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005103 for (wp = firstwin; wp != win; wp = wp->w_next)
5104 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005105 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5106 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5107 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5108 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5109 }
5110 }
5111#endif
5112 }
5113}
5114
5115/*
5116 * "getcharmod()" function
5117 */
5118 static void
5119f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5120{
5121 rettv->vval.v_number = mod_mask;
5122}
5123
5124/*
5125 * "getcharsearch()" function
5126 */
5127 static void
5128f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5129{
5130 if (rettv_dict_alloc(rettv) != FAIL)
5131 {
5132 dict_T *dict = rettv->vval.v_dict;
5133
Bram Moolenaare0be1672018-07-08 16:50:37 +02005134 dict_add_string(dict, "char", last_csearch());
5135 dict_add_number(dict, "forward", last_csearch_forward());
5136 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005137 }
5138}
5139
5140/*
5141 * "getcmdline()" function
5142 */
5143 static void
5144f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5145{
5146 rettv->v_type = VAR_STRING;
5147 rettv->vval.v_string = get_cmdline_str();
5148}
5149
5150/*
5151 * "getcmdpos()" function
5152 */
5153 static void
5154f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5155{
5156 rettv->vval.v_number = get_cmdline_pos() + 1;
5157}
5158
5159/*
5160 * "getcmdtype()" function
5161 */
5162 static void
5163f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5164{
5165 rettv->v_type = VAR_STRING;
5166 rettv->vval.v_string = alloc(2);
5167 if (rettv->vval.v_string != NULL)
5168 {
5169 rettv->vval.v_string[0] = get_cmdline_type();
5170 rettv->vval.v_string[1] = NUL;
5171 }
5172}
5173
5174/*
5175 * "getcmdwintype()" function
5176 */
5177 static void
5178f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5179{
5180 rettv->v_type = VAR_STRING;
5181 rettv->vval.v_string = NULL;
5182#ifdef FEAT_CMDWIN
5183 rettv->vval.v_string = alloc(2);
5184 if (rettv->vval.v_string != NULL)
5185 {
5186 rettv->vval.v_string[0] = cmdwin_type;
5187 rettv->vval.v_string[1] = NUL;
5188 }
5189#endif
5190}
5191
5192#if defined(FEAT_CMDL_COMPL)
5193/*
5194 * "getcompletion()" function
5195 */
5196 static void
5197f_getcompletion(typval_T *argvars, typval_T *rettv)
5198{
5199 char_u *pat;
5200 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005201 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005202 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5203 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005204
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005205 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005206 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005207
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005208 if (p_wic)
5209 options |= WILD_ICASE;
5210
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005211 /* For filtered results, 'wildignore' is used */
5212 if (!filtered)
5213 options |= WILD_KEEP_ALL;
5214
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005215 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005216 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005217 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005218 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005219 if (xpc.xp_context == EXPAND_NOTHING)
5220 {
5221 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005222 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005223 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005224 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005225 return;
5226 }
5227
5228# if defined(FEAT_MENU)
5229 if (xpc.xp_context == EXPAND_MENUS)
5230 {
5231 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5232 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5233 }
5234# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005235#ifdef FEAT_CSCOPE
5236 if (xpc.xp_context == EXPAND_CSCOPE)
5237 {
5238 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5239 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5240 }
5241#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005242#ifdef FEAT_SIGNS
5243 if (xpc.xp_context == EXPAND_SIGN)
5244 {
5245 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5246 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5247 }
5248#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005249
5250 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5251 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5252 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005253 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005254
5255 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5256
5257 for (i = 0; i < xpc.xp_numfiles; i++)
5258 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5259 }
5260 vim_free(pat);
5261 ExpandCleanup(&xpc);
5262}
5263#endif
5264
5265/*
5266 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005267 *
5268 * Return the current working directory of a window in a tab page.
5269 * First optional argument 'winnr' is the window number or -1 and the second
5270 * optional argument 'tabnr' is the tab page number.
5271 *
5272 * If no arguments are supplied, then return the directory of the current
5273 * window.
5274 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5275 * the specified window.
5276 * If 'winnr' is 0 then return the directory of the current window.
5277 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5278 * directory of the specified tab page. Otherwise return the directory of the
5279 * specified window in the specified tab page.
5280 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005281 */
5282 static void
5283f_getcwd(typval_T *argvars, typval_T *rettv)
5284{
5285 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005286 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005287 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005288 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005289
5290 rettv->v_type = VAR_STRING;
5291 rettv->vval.v_string = NULL;
5292
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005293 if (argvars[0].v_type == VAR_NUMBER
5294 && argvars[0].vval.v_number == -1
5295 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005296 global = TRUE;
5297 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005298 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005299
5300 if (wp != NULL && wp->w_localdir != NULL)
5301 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005302 else if (tp != NULL && tp->tp_localdir != NULL)
5303 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5304 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005305 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005306 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005307 rettv->vval.v_string = vim_strsave(globaldir);
5308 else
5309 {
5310 cwd = alloc(MAXPATHL);
5311 if (cwd != NULL)
5312 {
5313 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5314 rettv->vval.v_string = vim_strsave(cwd);
5315 vim_free(cwd);
5316 }
5317 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005318 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005319#ifdef BACKSLASH_IN_FILENAME
5320 if (rettv->vval.v_string != NULL)
5321 slash_adjust(rettv->vval.v_string);
5322#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005323}
5324
5325/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005326 * "getenv()" function
5327 */
5328 static void
5329f_getenv(typval_T *argvars, typval_T *rettv)
5330{
5331 int mustfree = FALSE;
5332 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5333
5334 if (p == NULL)
5335 {
5336 rettv->v_type = VAR_SPECIAL;
5337 rettv->vval.v_number = VVAL_NULL;
5338 return;
5339 }
5340 if (!mustfree)
5341 p = vim_strsave(p);
5342 rettv->vval.v_string = p;
5343 rettv->v_type = VAR_STRING;
5344}
5345
5346/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005347 * "getfontname()" function
5348 */
5349 static void
5350f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5351{
5352 rettv->v_type = VAR_STRING;
5353 rettv->vval.v_string = NULL;
5354#ifdef FEAT_GUI
5355 if (gui.in_use)
5356 {
5357 GuiFont font;
5358 char_u *name = NULL;
5359
5360 if (argvars[0].v_type == VAR_UNKNOWN)
5361 {
5362 /* Get the "Normal" font. Either the name saved by
5363 * hl_set_font_name() or from the font ID. */
5364 font = gui.norm_font;
5365 name = hl_get_font_name();
5366 }
5367 else
5368 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005369 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005370 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5371 return;
5372 font = gui_mch_get_font(name, FALSE);
5373 if (font == NOFONT)
5374 return; /* Invalid font name, return empty string. */
5375 }
5376 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5377 if (argvars[0].v_type != VAR_UNKNOWN)
5378 gui_mch_free_font(font);
5379 }
5380#endif
5381}
5382
5383/*
5384 * "getfperm({fname})" function
5385 */
5386 static void
5387f_getfperm(typval_T *argvars, typval_T *rettv)
5388{
5389 char_u *fname;
5390 stat_T st;
5391 char_u *perm = NULL;
5392 char_u flags[] = "rwx";
5393 int i;
5394
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005395 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005396
5397 rettv->v_type = VAR_STRING;
5398 if (mch_stat((char *)fname, &st) >= 0)
5399 {
5400 perm = vim_strsave((char_u *)"---------");
5401 if (perm != NULL)
5402 {
5403 for (i = 0; i < 9; i++)
5404 {
5405 if (st.st_mode & (1 << (8 - i)))
5406 perm[i] = flags[i % 3];
5407 }
5408 }
5409 }
5410 rettv->vval.v_string = perm;
5411}
5412
5413/*
5414 * "getfsize({fname})" function
5415 */
5416 static void
5417f_getfsize(typval_T *argvars, typval_T *rettv)
5418{
5419 char_u *fname;
5420 stat_T st;
5421
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005422 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005423
5424 rettv->v_type = VAR_NUMBER;
5425
5426 if (mch_stat((char *)fname, &st) >= 0)
5427 {
5428 if (mch_isdir(fname))
5429 rettv->vval.v_number = 0;
5430 else
5431 {
5432 rettv->vval.v_number = (varnumber_T)st.st_size;
5433
5434 /* non-perfect check for overflow */
5435 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5436 rettv->vval.v_number = -2;
5437 }
5438 }
5439 else
5440 rettv->vval.v_number = -1;
5441}
5442
5443/*
5444 * "getftime({fname})" function
5445 */
5446 static void
5447f_getftime(typval_T *argvars, typval_T *rettv)
5448{
5449 char_u *fname;
5450 stat_T st;
5451
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005452 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005453
5454 if (mch_stat((char *)fname, &st) >= 0)
5455 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5456 else
5457 rettv->vval.v_number = -1;
5458}
5459
5460/*
5461 * "getftype({fname})" function
5462 */
5463 static void
5464f_getftype(typval_T *argvars, typval_T *rettv)
5465{
5466 char_u *fname;
5467 stat_T st;
5468 char_u *type = NULL;
5469 char *t;
5470
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005471 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005472
5473 rettv->v_type = VAR_STRING;
5474 if (mch_lstat((char *)fname, &st) >= 0)
5475 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005476 if (S_ISREG(st.st_mode))
5477 t = "file";
5478 else if (S_ISDIR(st.st_mode))
5479 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005480 else if (S_ISLNK(st.st_mode))
5481 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005482 else if (S_ISBLK(st.st_mode))
5483 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005484 else if (S_ISCHR(st.st_mode))
5485 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005486 else if (S_ISFIFO(st.st_mode))
5487 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005488 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005489 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005490 else
5491 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005492 type = vim_strsave((char_u *)t);
5493 }
5494 rettv->vval.v_string = type;
5495}
5496
5497/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005498 * "getjumplist()" function
5499 */
5500 static void
5501f_getjumplist(typval_T *argvars, typval_T *rettv)
5502{
5503#ifdef FEAT_JUMPLIST
5504 win_T *wp;
5505 int i;
5506 list_T *l;
5507 dict_T *d;
5508#endif
5509
5510 if (rettv_list_alloc(rettv) != OK)
5511 return;
5512
5513#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005514 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005515 if (wp == NULL)
5516 return;
5517
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005518 cleanup_jumplist(wp, TRUE);
5519
Bram Moolenaar4f505882018-02-10 21:06:32 +01005520 l = list_alloc();
5521 if (l == NULL)
5522 return;
5523
5524 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5525 return;
5526 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5527
5528 for (i = 0; i < wp->w_jumplistlen; ++i)
5529 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005530 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5531 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005532 if ((d = dict_alloc()) == NULL)
5533 return;
5534 if (list_append_dict(l, d) == FAIL)
5535 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005536 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5537 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005538 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005539 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005540 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005541 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005542 }
5543#endif
5544}
5545
5546/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005547 * "getline(lnum, [end])" function
5548 */
5549 static void
5550f_getline(typval_T *argvars, typval_T *rettv)
5551{
5552 linenr_T lnum;
5553 linenr_T end;
5554 int retlist;
5555
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005556 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557 if (argvars[1].v_type == VAR_UNKNOWN)
5558 {
5559 end = 0;
5560 retlist = FALSE;
5561 }
5562 else
5563 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005564 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005565 retlist = TRUE;
5566 }
5567
5568 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5569}
5570
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005571#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005572 static void
5573get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5574{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005575 if (what_arg->v_type == VAR_UNKNOWN)
5576 {
5577 if (rettv_list_alloc(rettv) == OK)
5578 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005579 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005580 }
5581 else
5582 {
5583 if (rettv_dict_alloc(rettv) == OK)
5584 if (is_qf || (wp != NULL))
5585 {
5586 if (what_arg->v_type == VAR_DICT)
5587 {
5588 dict_T *d = what_arg->vval.v_dict;
5589
5590 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005591 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005592 }
5593 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005594 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005595 }
5596 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005597}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005598#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005599
5600/*
5601 * "getloclist()" function
5602 */
5603 static void
5604f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5605{
5606#ifdef FEAT_QUICKFIX
5607 win_T *wp;
5608
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005609 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005610 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5611#endif
5612}
5613
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614/*
5615 * "getmatches()" function
5616 */
5617 static void
5618f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5619{
5620#ifdef FEAT_SEARCH_EXTRA
5621 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005622 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005623 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005624 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005625
Bram Moolenaaraff74912019-03-30 18:11:49 +01005626 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5627 return;
5628
5629 cur = win->w_match_head;
5630 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005631 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005632 dict = dict_alloc();
5633 if (dict == NULL)
5634 return;
5635 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005636 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005637 /* match added with matchaddpos() */
5638 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005639 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005640 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005641 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005642 list_T *l;
5643
5644 llpos = &cur->pos.pos[i];
5645 if (llpos->lnum == 0)
5646 break;
5647 l = list_alloc();
5648 if (l == NULL)
5649 break;
5650 list_append_number(l, (varnumber_T)llpos->lnum);
5651 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005652 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005653 list_append_number(l, (varnumber_T)llpos->col);
5654 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005655 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005656 sprintf(buf, "pos%d", i + 1);
5657 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005658 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005659 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005660 else
5661 {
5662 dict_add_string(dict, "pattern", cur->pattern);
5663 }
5664 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5665 dict_add_number(dict, "priority", (long)cur->priority);
5666 dict_add_number(dict, "id", (long)cur->id);
5667# if defined(FEAT_CONCEAL)
5668 if (cur->conceal_char)
5669 {
5670 char_u buf[MB_MAXBYTES + 1];
5671
5672 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5673 dict_add_string(dict, "conceal", (char_u *)&buf);
5674 }
5675# endif
5676 list_append_dict(rettv->vval.v_list, dict);
5677 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005678 }
5679#endif
5680}
5681
5682/*
5683 * "getpid()" function
5684 */
5685 static void
5686f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5687{
5688 rettv->vval.v_number = mch_get_pid();
5689}
5690
5691 static void
5692getpos_both(
5693 typval_T *argvars,
5694 typval_T *rettv,
5695 int getcurpos)
5696{
5697 pos_T *fp;
5698 list_T *l;
5699 int fnum = -1;
5700
5701 if (rettv_list_alloc(rettv) == OK)
5702 {
5703 l = rettv->vval.v_list;
5704 if (getcurpos)
5705 fp = &curwin->w_cursor;
5706 else
5707 fp = var2fpos(&argvars[0], TRUE, &fnum);
5708 if (fnum != -1)
5709 list_append_number(l, (varnumber_T)fnum);
5710 else
5711 list_append_number(l, (varnumber_T)0);
5712 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5713 : (varnumber_T)0);
5714 list_append_number(l, (fp != NULL)
5715 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5716 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005717 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005718 (varnumber_T)0);
5719 if (getcurpos)
5720 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005721 int save_set_curswant = curwin->w_set_curswant;
5722 colnr_T save_curswant = curwin->w_curswant;
5723 colnr_T save_virtcol = curwin->w_virtcol;
5724
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005725 update_curswant();
5726 list_append_number(l, curwin->w_curswant == MAXCOL ?
5727 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005728
5729 // Do not change "curswant", as it is unexpected that a get
5730 // function has a side effect.
5731 if (save_set_curswant)
5732 {
5733 curwin->w_set_curswant = save_set_curswant;
5734 curwin->w_curswant = save_curswant;
5735 curwin->w_virtcol = save_virtcol;
5736 curwin->w_valid &= ~VALID_VIRTCOL;
5737 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005738 }
5739 }
5740 else
5741 rettv->vval.v_number = FALSE;
5742}
5743
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005744/*
5745 * "getcurpos()" function
5746 */
5747 static void
5748f_getcurpos(typval_T *argvars, typval_T *rettv)
5749{
5750 getpos_both(argvars, rettv, TRUE);
5751}
5752
5753/*
5754 * "getpos(string)" function
5755 */
5756 static void
5757f_getpos(typval_T *argvars, typval_T *rettv)
5758{
5759 getpos_both(argvars, rettv, FALSE);
5760}
5761
5762/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005763 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005764 */
5765 static void
5766f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5767{
5768#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005769 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005770#endif
5771}
5772
5773/*
5774 * "getreg()" function
5775 */
5776 static void
5777f_getreg(typval_T *argvars, typval_T *rettv)
5778{
5779 char_u *strregname;
5780 int regname;
5781 int arg2 = FALSE;
5782 int return_list = FALSE;
5783 int error = FALSE;
5784
5785 if (argvars[0].v_type != VAR_UNKNOWN)
5786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005787 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005788 error = strregname == NULL;
5789 if (argvars[1].v_type != VAR_UNKNOWN)
5790 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005791 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005792 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005793 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005794 }
5795 }
5796 else
5797 strregname = get_vim_var_str(VV_REG);
5798
5799 if (error)
5800 return;
5801
5802 regname = (strregname == NULL ? '"' : *strregname);
5803 if (regname == 0)
5804 regname = '"';
5805
5806 if (return_list)
5807 {
5808 rettv->v_type = VAR_LIST;
5809 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5810 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5811 if (rettv->vval.v_list == NULL)
5812 (void)rettv_list_alloc(rettv);
5813 else
5814 ++rettv->vval.v_list->lv_refcount;
5815 }
5816 else
5817 {
5818 rettv->v_type = VAR_STRING;
5819 rettv->vval.v_string = get_reg_contents(regname,
5820 arg2 ? GREG_EXPR_SRC : 0);
5821 }
5822}
5823
5824/*
5825 * "getregtype()" function
5826 */
5827 static void
5828f_getregtype(typval_T *argvars, typval_T *rettv)
5829{
5830 char_u *strregname;
5831 int regname;
5832 char_u buf[NUMBUFLEN + 2];
5833 long reglen = 0;
5834
5835 if (argvars[0].v_type != VAR_UNKNOWN)
5836 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005837 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005838 if (strregname == NULL) /* type error; errmsg already given */
5839 {
5840 rettv->v_type = VAR_STRING;
5841 rettv->vval.v_string = NULL;
5842 return;
5843 }
5844 }
5845 else
5846 /* Default to v:register */
5847 strregname = get_vim_var_str(VV_REG);
5848
5849 regname = (strregname == NULL ? '"' : *strregname);
5850 if (regname == 0)
5851 regname = '"';
5852
5853 buf[0] = NUL;
5854 buf[1] = NUL;
5855 switch (get_reg_type(regname, &reglen))
5856 {
5857 case MLINE: buf[0] = 'V'; break;
5858 case MCHAR: buf[0] = 'v'; break;
5859 case MBLOCK:
5860 buf[0] = Ctrl_V;
5861 sprintf((char *)buf + 1, "%ld", reglen + 1);
5862 break;
5863 }
5864 rettv->v_type = VAR_STRING;
5865 rettv->vval.v_string = vim_strsave(buf);
5866}
5867
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005868/*
5869 * Returns information (variables, options, etc.) about a tab page
5870 * as a dictionary.
5871 */
5872 static dict_T *
5873get_tabpage_info(tabpage_T *tp, int tp_idx)
5874{
5875 win_T *wp;
5876 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005877 list_T *l;
5878
5879 dict = dict_alloc();
5880 if (dict == NULL)
5881 return NULL;
5882
Bram Moolenaare0be1672018-07-08 16:50:37 +02005883 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005884
5885 l = list_alloc();
5886 if (l != NULL)
5887 {
5888 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5889 wp; wp = wp->w_next)
5890 list_append_number(l, (varnumber_T)wp->w_id);
5891 dict_add_list(dict, "windows", l);
5892 }
5893
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005894 /* Make a reference to tabpage variables */
5895 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005896
5897 return dict;
5898}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005899
5900/*
5901 * "gettabinfo()" function
5902 */
5903 static void
5904f_gettabinfo(typval_T *argvars, typval_T *rettv)
5905{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005906 tabpage_T *tp, *tparg = NULL;
5907 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005908 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005909
5910 if (rettv_list_alloc(rettv) != OK)
5911 return;
5912
5913 if (argvars[0].v_type != VAR_UNKNOWN)
5914 {
5915 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005916 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005917 if (tparg == NULL)
5918 return;
5919 }
5920
5921 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005922 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005923 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005924 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005925 if (tparg != NULL && tp != tparg)
5926 continue;
5927 d = get_tabpage_info(tp, tpnr);
5928 if (d != NULL)
5929 list_append_dict(rettv->vval.v_list, d);
5930 if (tparg != NULL)
5931 return;
5932 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005933}
5934
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005935/*
5936 * "gettabvar()" function
5937 */
5938 static void
5939f_gettabvar(typval_T *argvars, typval_T *rettv)
5940{
5941 win_T *oldcurwin;
5942 tabpage_T *tp, *oldtabpage;
5943 dictitem_T *v;
5944 char_u *varname;
5945 int done = FALSE;
5946
5947 rettv->v_type = VAR_STRING;
5948 rettv->vval.v_string = NULL;
5949
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005950 varname = tv_get_string_chk(&argvars[1]);
5951 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005952 if (tp != NULL && varname != NULL)
5953 {
5954 /* Set tp to be our tabpage, temporarily. Also set the window to the
5955 * first window in the tabpage, otherwise the window is not valid. */
5956 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005957 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5958 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005959 {
5960 /* look up the variable */
5961 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5962 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5963 if (v != NULL)
5964 {
5965 copy_tv(&v->di_tv, rettv);
5966 done = TRUE;
5967 }
5968 }
5969
5970 /* restore previous notion of curwin */
5971 restore_win(oldcurwin, oldtabpage, TRUE);
5972 }
5973
5974 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5975 copy_tv(&argvars[2], rettv);
5976}
5977
5978/*
5979 * "gettabwinvar()" function
5980 */
5981 static void
5982f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5983{
5984 getwinvar(argvars, rettv, 1);
5985}
5986
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005987/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005988 * "gettagstack()" function
5989 */
5990 static void
5991f_gettagstack(typval_T *argvars, typval_T *rettv)
5992{
5993 win_T *wp = curwin; // default is current window
5994
5995 if (rettv_dict_alloc(rettv) != OK)
5996 return;
5997
5998 if (argvars[0].v_type != VAR_UNKNOWN)
5999 {
6000 wp = find_win_by_nr_or_id(&argvars[0]);
6001 if (wp == NULL)
6002 return;
6003 }
6004
6005 get_tagstack(wp, rettv->vval.v_dict);
6006}
6007
6008/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006009 * Returns information about a window as a dictionary.
6010 */
6011 static dict_T *
6012get_win_info(win_T *wp, short tpnr, short winnr)
6013{
6014 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006015
6016 dict = dict_alloc();
6017 if (dict == NULL)
6018 return NULL;
6019
Bram Moolenaare0be1672018-07-08 16:50:37 +02006020 dict_add_number(dict, "tabnr", tpnr);
6021 dict_add_number(dict, "winnr", winnr);
6022 dict_add_number(dict, "winid", wp->w_id);
6023 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006024 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006025 dict_add_number(dict, "topline", wp->w_topline);
6026 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006027#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006028 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006029#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006030 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006031 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006032 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006033
Bram Moolenaar69905d12017-08-13 18:14:47 +02006034#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006035 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006036#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006037#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006038 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6039 dict_add_number(dict, "loclist",
6040 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006041#endif
6042
Bram Moolenaar30567352016-08-27 21:25:44 +02006043 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006044 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006045
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006046 return dict;
6047}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006048
6049/*
6050 * "getwininfo()" function
6051 */
6052 static void
6053f_getwininfo(typval_T *argvars, typval_T *rettv)
6054{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006055 tabpage_T *tp;
6056 win_T *wp = NULL, *wparg = NULL;
6057 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006058 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006059
6060 if (rettv_list_alloc(rettv) != OK)
6061 return;
6062
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006063 if (argvars[0].v_type != VAR_UNKNOWN)
6064 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006065 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006066 if (wparg == NULL)
6067 return;
6068 }
6069
6070 /* Collect information about either all the windows across all the tab
6071 * pages or one particular window.
6072 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006073 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006074 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006075 tabnr++;
6076 winnr = 0;
6077 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006078 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006079 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006080 if (wparg != NULL && wp != wparg)
6081 continue;
6082 d = get_win_info(wp, tabnr, winnr);
6083 if (d != NULL)
6084 list_append_dict(rettv->vval.v_list, d);
6085 if (wparg != NULL)
6086 /* found information about a specific window */
6087 return;
6088 }
6089 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006090}
6091
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006092/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006093 * "win_findbuf()" function
6094 */
6095 static void
6096f_win_findbuf(typval_T *argvars, typval_T *rettv)
6097{
6098 if (rettv_list_alloc(rettv) != FAIL)
6099 win_findbuf(argvars, rettv->vval.v_list);
6100}
6101
6102/*
6103 * "win_getid()" function
6104 */
6105 static void
6106f_win_getid(typval_T *argvars, typval_T *rettv)
6107{
6108 rettv->vval.v_number = win_getid(argvars);
6109}
6110
6111/*
6112 * "win_gotoid()" function
6113 */
6114 static void
6115f_win_gotoid(typval_T *argvars, typval_T *rettv)
6116{
6117 rettv->vval.v_number = win_gotoid(argvars);
6118}
6119
6120/*
6121 * "win_id2tabwin()" function
6122 */
6123 static void
6124f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6125{
6126 if (rettv_list_alloc(rettv) != FAIL)
6127 win_id2tabwin(argvars, rettv->vval.v_list);
6128}
6129
6130/*
6131 * "win_id2win()" function
6132 */
6133 static void
6134f_win_id2win(typval_T *argvars, typval_T *rettv)
6135{
6136 rettv->vval.v_number = win_id2win(argvars);
6137}
6138
6139/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006140 * "win_screenpos()" function
6141 */
6142 static void
6143f_win_screenpos(typval_T *argvars, typval_T *rettv)
6144{
6145 win_T *wp;
6146
6147 if (rettv_list_alloc(rettv) == FAIL)
6148 return;
6149
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006150 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006151 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6152 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6153}
6154
6155/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006156 * "getwinpos({timeout})" function
6157 */
6158 static void
6159f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6160{
6161 int x = -1;
6162 int y = -1;
6163
6164 if (rettv_list_alloc(rettv) == FAIL)
6165 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006166#if defined(FEAT_GUI) \
6167 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6168 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006169 {
6170 varnumber_T timeout = 100;
6171
6172 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006173 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006174
6175 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006176 }
6177#endif
6178 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6179 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6180}
6181
6182
6183/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006184 * "getwinposx()" function
6185 */
6186 static void
6187f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6188{
6189 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006190#if defined(FEAT_GUI) \
6191 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6192 || defined(MSWIN)
6193
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006194 {
6195 int x, y;
6196
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006197 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006198 rettv->vval.v_number = x;
6199 }
6200#endif
6201}
6202
6203/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006204 * "getwinposy()" function
6205 */
6206 static void
6207f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6208{
6209 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006210#if defined(FEAT_GUI) \
6211 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6212 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006213 {
6214 int x, y;
6215
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006216 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006217 rettv->vval.v_number = y;
6218 }
6219#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220}
6221
6222/*
6223 * "getwinvar()" function
6224 */
6225 static void
6226f_getwinvar(typval_T *argvars, typval_T *rettv)
6227{
6228 getwinvar(argvars, rettv, 0);
6229}
6230
6231/*
6232 * "glob()" function
6233 */
6234 static void
6235f_glob(typval_T *argvars, typval_T *rettv)
6236{
6237 int options = WILD_SILENT|WILD_USE_NL;
6238 expand_T xpc;
6239 int error = FALSE;
6240
6241 /* When the optional second argument is non-zero, don't remove matches
6242 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6243 rettv->v_type = VAR_STRING;
6244 if (argvars[1].v_type != VAR_UNKNOWN)
6245 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006246 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247 options |= WILD_KEEP_ALL;
6248 if (argvars[2].v_type != VAR_UNKNOWN)
6249 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006250 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006251 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006252 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006253 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254 options |= WILD_ALLLINKS;
6255 }
6256 }
6257 if (!error)
6258 {
6259 ExpandInit(&xpc);
6260 xpc.xp_context = EXPAND_FILES;
6261 if (p_wic)
6262 options += WILD_ICASE;
6263 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006264 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006265 NULL, options, WILD_ALL);
6266 else if (rettv_list_alloc(rettv) != FAIL)
6267 {
6268 int i;
6269
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006270 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006271 NULL, options, WILD_ALL_KEEP);
6272 for (i = 0; i < xpc.xp_numfiles; i++)
6273 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6274
6275 ExpandCleanup(&xpc);
6276 }
6277 }
6278 else
6279 rettv->vval.v_string = NULL;
6280}
6281
6282/*
6283 * "globpath()" function
6284 */
6285 static void
6286f_globpath(typval_T *argvars, typval_T *rettv)
6287{
6288 int flags = 0;
6289 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006290 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006291 int error = FALSE;
6292 garray_T ga;
6293 int i;
6294
6295 /* When the optional second argument is non-zero, don't remove matches
6296 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6297 rettv->v_type = VAR_STRING;
6298 if (argvars[2].v_type != VAR_UNKNOWN)
6299 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006300 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006301 flags |= WILD_KEEP_ALL;
6302 if (argvars[3].v_type != VAR_UNKNOWN)
6303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006304 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006305 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006306 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006307 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308 flags |= WILD_ALLLINKS;
6309 }
6310 }
6311 if (file != NULL && !error)
6312 {
6313 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006314 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006315 if (rettv->v_type == VAR_STRING)
6316 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6317 else if (rettv_list_alloc(rettv) != FAIL)
6318 for (i = 0; i < ga.ga_len; ++i)
6319 list_append_string(rettv->vval.v_list,
6320 ((char_u **)(ga.ga_data))[i], -1);
6321 ga_clear_strings(&ga);
6322 }
6323 else
6324 rettv->vval.v_string = NULL;
6325}
6326
6327/*
6328 * "glob2regpat()" function
6329 */
6330 static void
6331f_glob2regpat(typval_T *argvars, typval_T *rettv)
6332{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006333 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006334
6335 rettv->v_type = VAR_STRING;
6336 rettv->vval.v_string = (pat == NULL)
6337 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6338}
6339
6340/* for VIM_VERSION_ defines */
6341#include "version.h"
6342
6343/*
6344 * "has()" function
6345 */
6346 static void
6347f_has(typval_T *argvars, typval_T *rettv)
6348{
6349 int i;
6350 char_u *name;
6351 int n = FALSE;
6352 static char *(has_list[]) =
6353 {
6354#ifdef AMIGA
6355 "amiga",
6356# ifdef FEAT_ARP
6357 "arp",
6358# endif
6359#endif
6360#ifdef __BEOS__
6361 "beos",
6362#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006363#if defined(BSD) && !defined(MACOS_X)
6364 "bsd",
6365#endif
6366#ifdef hpux
6367 "hpux",
6368#endif
6369#ifdef __linux__
6370 "linux",
6371#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006372#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006373 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6374 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006375# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006376 "macunix", /* Mac OS X, with the darwin feature */
6377 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006378# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006379#endif
6380#ifdef __QNX__
6381 "qnx",
6382#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006383#ifdef SUN_SYSTEM
6384 "sun",
6385#else
6386 "moon",
6387#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006388#ifdef UNIX
6389 "unix",
6390#endif
6391#ifdef VMS
6392 "vms",
6393#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006394#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006395 "win32",
6396#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006397#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006398 "win32unix",
6399#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006400#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006401 "win64",
6402#endif
6403#ifdef EBCDIC
6404 "ebcdic",
6405#endif
6406#ifndef CASE_INSENSITIVE_FILENAME
6407 "fname_case",
6408#endif
6409#ifdef HAVE_ACL
6410 "acl",
6411#endif
6412#ifdef FEAT_ARABIC
6413 "arabic",
6414#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006415 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006416#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006417 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006418#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006419#ifdef FEAT_AUTOSERVERNAME
6420 "autoservername",
6421#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006422#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006423 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006424# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006425 "balloon_multiline",
6426# endif
6427#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006428#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006429 "balloon_eval_term",
6430#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006431#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6432 "builtin_terms",
6433# ifdef ALL_BUILTIN_TCAPS
6434 "all_builtin_terms",
6435# endif
6436#endif
6437#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006438 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006439 || defined(FEAT_GUI_MOTIF))
6440 "browsefilter",
6441#endif
6442#ifdef FEAT_BYTEOFF
6443 "byte_offset",
6444#endif
6445#ifdef FEAT_JOB_CHANNEL
6446 "channel",
6447#endif
6448#ifdef FEAT_CINDENT
6449 "cindent",
6450#endif
6451#ifdef FEAT_CLIENTSERVER
6452 "clientserver",
6453#endif
6454#ifdef FEAT_CLIPBOARD
6455 "clipboard",
6456#endif
6457#ifdef FEAT_CMDL_COMPL
6458 "cmdline_compl",
6459#endif
6460#ifdef FEAT_CMDHIST
6461 "cmdline_hist",
6462#endif
6463#ifdef FEAT_COMMENTS
6464 "comments",
6465#endif
6466#ifdef FEAT_CONCEAL
6467 "conceal",
6468#endif
6469#ifdef FEAT_CRYPT
6470 "cryptv",
6471 "crypt-blowfish",
6472 "crypt-blowfish2",
6473#endif
6474#ifdef FEAT_CSCOPE
6475 "cscope",
6476#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006477 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006478#ifdef CURSOR_SHAPE
6479 "cursorshape",
6480#endif
6481#ifdef DEBUG
6482 "debug",
6483#endif
6484#ifdef FEAT_CON_DIALOG
6485 "dialog_con",
6486#endif
6487#ifdef FEAT_GUI_DIALOG
6488 "dialog_gui",
6489#endif
6490#ifdef FEAT_DIFF
6491 "diff",
6492#endif
6493#ifdef FEAT_DIGRAPHS
6494 "digraphs",
6495#endif
6496#ifdef FEAT_DIRECTX
6497 "directx",
6498#endif
6499#ifdef FEAT_DND
6500 "dnd",
6501#endif
6502#ifdef FEAT_EMACS_TAGS
6503 "emacs_tags",
6504#endif
6505 "eval", /* always present, of course! */
6506 "ex_extra", /* graduated feature */
6507#ifdef FEAT_SEARCH_EXTRA
6508 "extra_search",
6509#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006510#ifdef FEAT_SEARCHPATH
6511 "file_in_path",
6512#endif
6513#ifdef FEAT_FILTERPIPE
6514 "filterpipe",
6515#endif
6516#ifdef FEAT_FIND_ID
6517 "find_in_path",
6518#endif
6519#ifdef FEAT_FLOAT
6520 "float",
6521#endif
6522#ifdef FEAT_FOLDING
6523 "folding",
6524#endif
6525#ifdef FEAT_FOOTER
6526 "footer",
6527#endif
6528#if !defined(USE_SYSTEM) && defined(UNIX)
6529 "fork",
6530#endif
6531#ifdef FEAT_GETTEXT
6532 "gettext",
6533#endif
6534#ifdef FEAT_GUI
6535 "gui",
6536#endif
6537#ifdef FEAT_GUI_ATHENA
6538# ifdef FEAT_GUI_NEXTAW
6539 "gui_neXtaw",
6540# else
6541 "gui_athena",
6542# endif
6543#endif
6544#ifdef FEAT_GUI_GTK
6545 "gui_gtk",
6546# ifdef USE_GTK3
6547 "gui_gtk3",
6548# else
6549 "gui_gtk2",
6550# endif
6551#endif
6552#ifdef FEAT_GUI_GNOME
6553 "gui_gnome",
6554#endif
6555#ifdef FEAT_GUI_MAC
6556 "gui_mac",
6557#endif
6558#ifdef FEAT_GUI_MOTIF
6559 "gui_motif",
6560#endif
6561#ifdef FEAT_GUI_PHOTON
6562 "gui_photon",
6563#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006564#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565 "gui_win32",
6566#endif
6567#ifdef FEAT_HANGULIN
6568 "hangul_input",
6569#endif
6570#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6571 "iconv",
6572#endif
6573#ifdef FEAT_INS_EXPAND
6574 "insert_expand",
6575#endif
6576#ifdef FEAT_JOB_CHANNEL
6577 "job",
6578#endif
6579#ifdef FEAT_JUMPLIST
6580 "jumplist",
6581#endif
6582#ifdef FEAT_KEYMAP
6583 "keymap",
6584#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006585 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586#ifdef FEAT_LANGMAP
6587 "langmap",
6588#endif
6589#ifdef FEAT_LIBCALL
6590 "libcall",
6591#endif
6592#ifdef FEAT_LINEBREAK
6593 "linebreak",
6594#endif
6595#ifdef FEAT_LISP
6596 "lispindent",
6597#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006598 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006599#ifdef FEAT_LOCALMAP
6600 "localmap",
6601#endif
6602#ifdef FEAT_LUA
6603# ifndef DYNAMIC_LUA
6604 "lua",
6605# endif
6606#endif
6607#ifdef FEAT_MENU
6608 "menu",
6609#endif
6610#ifdef FEAT_SESSION
6611 "mksession",
6612#endif
6613#ifdef FEAT_MODIFY_FNAME
6614 "modify_fname",
6615#endif
6616#ifdef FEAT_MOUSE
6617 "mouse",
6618#endif
6619#ifdef FEAT_MOUSESHAPE
6620 "mouseshape",
6621#endif
6622#if defined(UNIX) || defined(VMS)
6623# ifdef FEAT_MOUSE_DEC
6624 "mouse_dec",
6625# endif
6626# ifdef FEAT_MOUSE_GPM
6627 "mouse_gpm",
6628# endif
6629# ifdef FEAT_MOUSE_JSB
6630 "mouse_jsbterm",
6631# endif
6632# ifdef FEAT_MOUSE_NET
6633 "mouse_netterm",
6634# endif
6635# ifdef FEAT_MOUSE_PTERM
6636 "mouse_pterm",
6637# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006638# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 "mouse_sgr",
6640# endif
6641# ifdef FEAT_SYSMOUSE
6642 "mouse_sysmouse",
6643# endif
6644# ifdef FEAT_MOUSE_URXVT
6645 "mouse_urxvt",
6646# endif
6647# ifdef FEAT_MOUSE_XTERM
6648 "mouse_xterm",
6649# endif
6650#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006651 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006652#ifdef FEAT_MBYTE_IME
6653 "multi_byte_ime",
6654#endif
6655#ifdef FEAT_MULTI_LANG
6656 "multi_lang",
6657#endif
6658#ifdef FEAT_MZSCHEME
6659#ifndef DYNAMIC_MZSCHEME
6660 "mzscheme",
6661#endif
6662#endif
6663#ifdef FEAT_NUM64
6664 "num64",
6665#endif
6666#ifdef FEAT_OLE
6667 "ole",
6668#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006669#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006670 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006671#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006672#ifdef FEAT_PATH_EXTRA
6673 "path_extra",
6674#endif
6675#ifdef FEAT_PERL
6676#ifndef DYNAMIC_PERL
6677 "perl",
6678#endif
6679#endif
6680#ifdef FEAT_PERSISTENT_UNDO
6681 "persistent_undo",
6682#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006683#if defined(FEAT_PYTHON)
6684 "python_compiled",
6685# if defined(DYNAMIC_PYTHON)
6686 "python_dynamic",
6687# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006688 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006689 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006690# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006691#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006692#if defined(FEAT_PYTHON3)
6693 "python3_compiled",
6694# if defined(DYNAMIC_PYTHON3)
6695 "python3_dynamic",
6696# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006697 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006698 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006699# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006700#endif
6701#ifdef FEAT_POSTSCRIPT
6702 "postscript",
6703#endif
6704#ifdef FEAT_PRINTER
6705 "printer",
6706#endif
6707#ifdef FEAT_PROFILE
6708 "profile",
6709#endif
6710#ifdef FEAT_RELTIME
6711 "reltime",
6712#endif
6713#ifdef FEAT_QUICKFIX
6714 "quickfix",
6715#endif
6716#ifdef FEAT_RIGHTLEFT
6717 "rightleft",
6718#endif
6719#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6720 "ruby",
6721#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006722 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006723#ifdef FEAT_CMDL_INFO
6724 "showcmd",
6725 "cmdline_info",
6726#endif
6727#ifdef FEAT_SIGNS
6728 "signs",
6729#endif
6730#ifdef FEAT_SMARTINDENT
6731 "smartindent",
6732#endif
6733#ifdef STARTUPTIME
6734 "startuptime",
6735#endif
6736#ifdef FEAT_STL_OPT
6737 "statusline",
6738#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739#ifdef FEAT_NETBEANS_INTG
6740 "netbeans_intg",
6741#endif
6742#ifdef FEAT_SPELL
6743 "spell",
6744#endif
6745#ifdef FEAT_SYN_HL
6746 "syntax",
6747#endif
6748#if defined(USE_SYSTEM) || !defined(UNIX)
6749 "system",
6750#endif
6751#ifdef FEAT_TAG_BINS
6752 "tag_binary",
6753#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006754#ifdef FEAT_TCL
6755# ifndef DYNAMIC_TCL
6756 "tcl",
6757# endif
6758#endif
6759#ifdef FEAT_TERMGUICOLORS
6760 "termguicolors",
6761#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006762#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006763 "terminal",
6764#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006765#ifdef TERMINFO
6766 "terminfo",
6767#endif
6768#ifdef FEAT_TERMRESPONSE
6769 "termresponse",
6770#endif
6771#ifdef FEAT_TEXTOBJ
6772 "textobjects",
6773#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006774#ifdef FEAT_TEXT_PROP
6775 "textprop",
6776#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006777#ifdef HAVE_TGETENT
6778 "tgetent",
6779#endif
6780#ifdef FEAT_TIMERS
6781 "timers",
6782#endif
6783#ifdef FEAT_TITLE
6784 "title",
6785#endif
6786#ifdef FEAT_TOOLBAR
6787 "toolbar",
6788#endif
6789#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6790 "unnamedplus",
6791#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006792 "user-commands", /* was accidentally included in 5.4 */
6793 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006794#ifdef FEAT_VARTABS
6795 "vartabs",
6796#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006797 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006798#ifdef FEAT_VIMINFO
6799 "viminfo",
6800#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006801 "vimscript-1",
6802 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006803 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006804 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006805 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006806 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006807 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006808#ifdef FEAT_VTP
6809 "vtp",
6810#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006811#ifdef FEAT_WILDIGN
6812 "wildignore",
6813#endif
6814#ifdef FEAT_WILDMENU
6815 "wildmenu",
6816#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006817 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006818#ifdef FEAT_WAK
6819 "winaltkeys",
6820#endif
6821#ifdef FEAT_WRITEBACKUP
6822 "writebackup",
6823#endif
6824#ifdef FEAT_XIM
6825 "xim",
6826#endif
6827#ifdef FEAT_XFONTSET
6828 "xfontset",
6829#endif
6830#ifdef FEAT_XPM_W32
6831 "xpm",
6832 "xpm_w32", /* for backward compatibility */
6833#else
6834# if defined(HAVE_XPM)
6835 "xpm",
6836# endif
6837#endif
6838#ifdef USE_XSMP
6839 "xsmp",
6840#endif
6841#ifdef USE_XSMP_INTERACT
6842 "xsmp_interact",
6843#endif
6844#ifdef FEAT_XCLIPBOARD
6845 "xterm_clipboard",
6846#endif
6847#ifdef FEAT_XTERM_SAVE
6848 "xterm_save",
6849#endif
6850#if defined(UNIX) && defined(FEAT_X11)
6851 "X11",
6852#endif
6853 NULL
6854 };
6855
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006856 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006857 for (i = 0; has_list[i] != NULL; ++i)
6858 if (STRICMP(name, has_list[i]) == 0)
6859 {
6860 n = TRUE;
6861 break;
6862 }
6863
6864 if (n == FALSE)
6865 {
6866 if (STRNICMP(name, "patch", 5) == 0)
6867 {
6868 if (name[5] == '-'
6869 && STRLEN(name) >= 11
6870 && vim_isdigit(name[6])
6871 && vim_isdigit(name[8])
6872 && vim_isdigit(name[10]))
6873 {
6874 int major = atoi((char *)name + 6);
6875 int minor = atoi((char *)name + 8);
6876
6877 /* Expect "patch-9.9.01234". */
6878 n = (major < VIM_VERSION_MAJOR
6879 || (major == VIM_VERSION_MAJOR
6880 && (minor < VIM_VERSION_MINOR
6881 || (minor == VIM_VERSION_MINOR
6882 && has_patch(atoi((char *)name + 10))))));
6883 }
6884 else
6885 n = has_patch(atoi((char *)name + 5));
6886 }
6887 else if (STRICMP(name, "vim_starting") == 0)
6888 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006889 else if (STRICMP(name, "ttyin") == 0)
6890 n = mch_input_isatty();
6891 else if (STRICMP(name, "ttyout") == 0)
6892 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006893 else if (STRICMP(name, "multi_byte_encoding") == 0)
6894 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006895#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006896 else if (STRICMP(name, "balloon_multiline") == 0)
6897 n = multiline_balloon_available();
6898#endif
6899#ifdef DYNAMIC_TCL
6900 else if (STRICMP(name, "tcl") == 0)
6901 n = tcl_enabled(FALSE);
6902#endif
6903#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6904 else if (STRICMP(name, "iconv") == 0)
6905 n = iconv_enabled(FALSE);
6906#endif
6907#ifdef DYNAMIC_LUA
6908 else if (STRICMP(name, "lua") == 0)
6909 n = lua_enabled(FALSE);
6910#endif
6911#ifdef DYNAMIC_MZSCHEME
6912 else if (STRICMP(name, "mzscheme") == 0)
6913 n = mzscheme_enabled(FALSE);
6914#endif
6915#ifdef DYNAMIC_RUBY
6916 else if (STRICMP(name, "ruby") == 0)
6917 n = ruby_enabled(FALSE);
6918#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006919#ifdef DYNAMIC_PYTHON
6920 else if (STRICMP(name, "python") == 0)
6921 n = python_enabled(FALSE);
6922#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923#ifdef DYNAMIC_PYTHON3
6924 else if (STRICMP(name, "python3") == 0)
6925 n = python3_enabled(FALSE);
6926#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006927#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6928 else if (STRICMP(name, "pythonx") == 0)
6929 {
6930# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6931 if (p_pyx == 0)
6932 n = python3_enabled(FALSE) || python_enabled(FALSE);
6933 else if (p_pyx == 3)
6934 n = python3_enabled(FALSE);
6935 else if (p_pyx == 2)
6936 n = python_enabled(FALSE);
6937# elif defined(DYNAMIC_PYTHON)
6938 n = python_enabled(FALSE);
6939# elif defined(DYNAMIC_PYTHON3)
6940 n = python3_enabled(FALSE);
6941# endif
6942 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006943#endif
6944#ifdef DYNAMIC_PERL
6945 else if (STRICMP(name, "perl") == 0)
6946 n = perl_enabled(FALSE);
6947#endif
6948#ifdef FEAT_GUI
6949 else if (STRICMP(name, "gui_running") == 0)
6950 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006951# ifdef FEAT_BROWSE
6952 else if (STRICMP(name, "browse") == 0)
6953 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6954# endif
6955#endif
6956#ifdef FEAT_SYN_HL
6957 else if (STRICMP(name, "syntax_items") == 0)
6958 n = syntax_present(curwin);
6959#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006960#ifdef FEAT_VTP
6961 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006962 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006963#endif
6964#ifdef FEAT_NETBEANS_INTG
6965 else if (STRICMP(name, "netbeans_enabled") == 0)
6966 n = netbeans_active();
6967#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006968#ifdef FEAT_MOUSE_GPM
6969 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6970 n = gpm_enabled();
6971#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006972#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006973 else if (STRICMP(name, "terminal") == 0)
6974 n = terminal_enabled();
6975#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006976#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006977 else if (STRICMP(name, "conpty") == 0)
6978 n = use_conpty();
6979#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006980 }
6981
6982 rettv->vval.v_number = n;
6983}
6984
6985/*
6986 * "has_key()" function
6987 */
6988 static void
6989f_has_key(typval_T *argvars, typval_T *rettv)
6990{
6991 if (argvars[0].v_type != VAR_DICT)
6992 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006993 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006994 return;
6995 }
6996 if (argvars[0].vval.v_dict == NULL)
6997 return;
6998
6999 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007000 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007001}
7002
7003/*
7004 * "haslocaldir()" function
7005 */
7006 static void
7007f_haslocaldir(typval_T *argvars, typval_T *rettv)
7008{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007009 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007010 win_T *wp = NULL;
7011
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007012 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7013
7014 // Check for window-local and tab-local directories
7015 if (wp != NULL && wp->w_localdir != NULL)
7016 rettv->vval.v_number = 1;
7017 else if (tp != NULL && tp->tp_localdir != NULL)
7018 rettv->vval.v_number = 2;
7019 else
7020 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007021}
7022
7023/*
7024 * "hasmapto()" function
7025 */
7026 static void
7027f_hasmapto(typval_T *argvars, typval_T *rettv)
7028{
7029 char_u *name;
7030 char_u *mode;
7031 char_u buf[NUMBUFLEN];
7032 int abbr = FALSE;
7033
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007034 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007035 if (argvars[1].v_type == VAR_UNKNOWN)
7036 mode = (char_u *)"nvo";
7037 else
7038 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007039 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007040 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007041 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042 }
7043
7044 if (map_to_exists(name, mode, abbr))
7045 rettv->vval.v_number = TRUE;
7046 else
7047 rettv->vval.v_number = FALSE;
7048}
7049
7050/*
7051 * "histadd()" function
7052 */
7053 static void
7054f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7055{
7056#ifdef FEAT_CMDHIST
7057 int histype;
7058 char_u *str;
7059 char_u buf[NUMBUFLEN];
7060#endif
7061
7062 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007063 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007064 return;
7065#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007066 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007067 histype = str != NULL ? get_histtype(str) : -1;
7068 if (histype >= 0)
7069 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007070 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007071 if (*str != NUL)
7072 {
7073 init_history();
7074 add_to_history(histype, str, FALSE, NUL);
7075 rettv->vval.v_number = TRUE;
7076 return;
7077 }
7078 }
7079#endif
7080}
7081
7082/*
7083 * "histdel()" function
7084 */
7085 static void
7086f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7087{
7088#ifdef FEAT_CMDHIST
7089 int n;
7090 char_u buf[NUMBUFLEN];
7091 char_u *str;
7092
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007093 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007094 if (str == NULL)
7095 n = 0;
7096 else if (argvars[1].v_type == VAR_UNKNOWN)
7097 /* only one argument: clear entire history */
7098 n = clr_history(get_histtype(str));
7099 else if (argvars[1].v_type == VAR_NUMBER)
7100 /* index given: remove that entry */
7101 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007102 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007103 else
7104 /* string given: remove all matching entries */
7105 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007106 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007107 rettv->vval.v_number = n;
7108#endif
7109}
7110
7111/*
7112 * "histget()" function
7113 */
7114 static void
7115f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7116{
7117#ifdef FEAT_CMDHIST
7118 int type;
7119 int idx;
7120 char_u *str;
7121
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007122 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007123 if (str == NULL)
7124 rettv->vval.v_string = NULL;
7125 else
7126 {
7127 type = get_histtype(str);
7128 if (argvars[1].v_type == VAR_UNKNOWN)
7129 idx = get_history_idx(type);
7130 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007131 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007132 /* -1 on type error */
7133 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7134 }
7135#else
7136 rettv->vval.v_string = NULL;
7137#endif
7138 rettv->v_type = VAR_STRING;
7139}
7140
7141/*
7142 * "histnr()" function
7143 */
7144 static void
7145f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7146{
7147 int i;
7148
7149#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007150 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007151
7152 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7153 if (i >= HIST_CMD && i < HIST_COUNT)
7154 i = get_history_idx(i);
7155 else
7156#endif
7157 i = -1;
7158 rettv->vval.v_number = i;
7159}
7160
7161/*
7162 * "highlightID(name)" function
7163 */
7164 static void
7165f_hlID(typval_T *argvars, typval_T *rettv)
7166{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007167 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007168}
7169
7170/*
7171 * "highlight_exists()" function
7172 */
7173 static void
7174f_hlexists(typval_T *argvars, typval_T *rettv)
7175{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007176 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007177}
7178
7179/*
7180 * "hostname()" function
7181 */
7182 static void
7183f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7184{
7185 char_u hostname[256];
7186
7187 mch_get_host_name(hostname, 256);
7188 rettv->v_type = VAR_STRING;
7189 rettv->vval.v_string = vim_strsave(hostname);
7190}
7191
7192/*
7193 * iconv() function
7194 */
7195 static void
7196f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7197{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007198 char_u buf1[NUMBUFLEN];
7199 char_u buf2[NUMBUFLEN];
7200 char_u *from, *to, *str;
7201 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007202
7203 rettv->v_type = VAR_STRING;
7204 rettv->vval.v_string = NULL;
7205
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007206 str = tv_get_string(&argvars[0]);
7207 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7208 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007209 vimconv.vc_type = CONV_NONE;
7210 convert_setup(&vimconv, from, to);
7211
7212 /* If the encodings are equal, no conversion needed. */
7213 if (vimconv.vc_type == CONV_NONE)
7214 rettv->vval.v_string = vim_strsave(str);
7215 else
7216 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7217
7218 convert_setup(&vimconv, NULL, NULL);
7219 vim_free(from);
7220 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007221}
7222
7223/*
7224 * "indent()" function
7225 */
7226 static void
7227f_indent(typval_T *argvars, typval_T *rettv)
7228{
7229 linenr_T lnum;
7230
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007231 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007232 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7233 rettv->vval.v_number = get_indent_lnum(lnum);
7234 else
7235 rettv->vval.v_number = -1;
7236}
7237
7238/*
7239 * "index()" function
7240 */
7241 static void
7242f_index(typval_T *argvars, typval_T *rettv)
7243{
7244 list_T *l;
7245 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007246 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007247 long idx = 0;
7248 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007249 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007250
7251 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007252 if (argvars[0].v_type == VAR_BLOB)
7253 {
7254 typval_T tv;
7255 int start = 0;
7256
7257 if (argvars[2].v_type != VAR_UNKNOWN)
7258 {
7259 start = tv_get_number_chk(&argvars[2], &error);
7260 if (error)
7261 return;
7262 }
7263 b = argvars[0].vval.v_blob;
7264 if (b == NULL)
7265 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007266 if (start < 0)
7267 {
7268 start = blob_len(b) + start;
7269 if (start < 0)
7270 start = 0;
7271 }
7272
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007273 for (idx = start; idx < blob_len(b); ++idx)
7274 {
7275 tv.v_type = VAR_NUMBER;
7276 tv.vval.v_number = blob_get(b, idx);
7277 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7278 {
7279 rettv->vval.v_number = idx;
7280 return;
7281 }
7282 }
7283 return;
7284 }
7285 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007286 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007287 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007288 return;
7289 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007290
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007291 l = argvars[0].vval.v_list;
7292 if (l != NULL)
7293 {
7294 item = l->lv_first;
7295 if (argvars[2].v_type != VAR_UNKNOWN)
7296 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007297 /* Start at specified item. Use the cached index that list_find()
7298 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007299 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007300 idx = l->lv_idx;
7301 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007302 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007303 if (error)
7304 item = NULL;
7305 }
7306
7307 for ( ; item != NULL; item = item->li_next, ++idx)
7308 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7309 {
7310 rettv->vval.v_number = idx;
7311 break;
7312 }
7313 }
7314}
7315
7316static int inputsecret_flag = 0;
7317
7318/*
7319 * "input()" function
7320 * Also handles inputsecret() when inputsecret is set.
7321 */
7322 static void
7323f_input(typval_T *argvars, typval_T *rettv)
7324{
7325 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7326}
7327
7328/*
7329 * "inputdialog()" function
7330 */
7331 static void
7332f_inputdialog(typval_T *argvars, typval_T *rettv)
7333{
7334#if defined(FEAT_GUI_TEXTDIALOG)
7335 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7336 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7337 {
7338 char_u *message;
7339 char_u buf[NUMBUFLEN];
7340 char_u *defstr = (char_u *)"";
7341
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007342 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007343 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007344 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007345 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7346 else
7347 IObuff[0] = NUL;
7348 if (message != NULL && defstr != NULL
7349 && do_dialog(VIM_QUESTION, NULL, message,
7350 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7351 rettv->vval.v_string = vim_strsave(IObuff);
7352 else
7353 {
7354 if (message != NULL && defstr != NULL
7355 && argvars[1].v_type != VAR_UNKNOWN
7356 && argvars[2].v_type != VAR_UNKNOWN)
7357 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007358 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007359 else
7360 rettv->vval.v_string = NULL;
7361 }
7362 rettv->v_type = VAR_STRING;
7363 }
7364 else
7365#endif
7366 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7367}
7368
7369/*
7370 * "inputlist()" function
7371 */
7372 static void
7373f_inputlist(typval_T *argvars, typval_T *rettv)
7374{
7375 listitem_T *li;
7376 int selected;
7377 int mouse_used;
7378
7379#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007380 /* While starting up, there is no place to enter text. When running tests
7381 * with --not-a-term we assume feedkeys() will be used. */
7382 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007383 return;
7384#endif
7385 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7386 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007387 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007388 return;
7389 }
7390
7391 msg_start();
7392 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7393 lines_left = Rows; /* avoid more prompt */
7394 msg_scroll = TRUE;
7395 msg_clr_eos();
7396
7397 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7398 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007399 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007400 msg_putchar('\n');
7401 }
7402
7403 /* Ask for choice. */
7404 selected = prompt_for_number(&mouse_used);
7405 if (mouse_used)
7406 selected -= lines_left;
7407
7408 rettv->vval.v_number = selected;
7409}
7410
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007411static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7412
7413/*
7414 * "inputrestore()" function
7415 */
7416 static void
7417f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7418{
7419 if (ga_userinput.ga_len > 0)
7420 {
7421 --ga_userinput.ga_len;
7422 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7423 + ga_userinput.ga_len);
7424 /* default return is zero == OK */
7425 }
7426 else if (p_verbose > 1)
7427 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007428 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007429 rettv->vval.v_number = 1; /* Failed */
7430 }
7431}
7432
7433/*
7434 * "inputsave()" function
7435 */
7436 static void
7437f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7438{
7439 /* Add an entry to the stack of typeahead storage. */
7440 if (ga_grow(&ga_userinput, 1) == OK)
7441 {
7442 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7443 + ga_userinput.ga_len);
7444 ++ga_userinput.ga_len;
7445 /* default return is zero == OK */
7446 }
7447 else
7448 rettv->vval.v_number = 1; /* Failed */
7449}
7450
7451/*
7452 * "inputsecret()" function
7453 */
7454 static void
7455f_inputsecret(typval_T *argvars, typval_T *rettv)
7456{
7457 ++cmdline_star;
7458 ++inputsecret_flag;
7459 f_input(argvars, rettv);
7460 --cmdline_star;
7461 --inputsecret_flag;
7462}
7463
7464/*
7465 * "insert()" function
7466 */
7467 static void
7468f_insert(typval_T *argvars, typval_T *rettv)
7469{
7470 long before = 0;
7471 listitem_T *item;
7472 list_T *l;
7473 int error = FALSE;
7474
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007475 if (argvars[0].v_type == VAR_BLOB)
7476 {
7477 int val, len;
7478 char_u *p;
7479
7480 len = blob_len(argvars[0].vval.v_blob);
7481 if (argvars[2].v_type != VAR_UNKNOWN)
7482 {
7483 before = (long)tv_get_number_chk(&argvars[2], &error);
7484 if (error)
7485 return; // type error; errmsg already given
7486 if (before < 0 || before > len)
7487 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007488 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007489 return;
7490 }
7491 }
7492 val = tv_get_number_chk(&argvars[1], &error);
7493 if (error)
7494 return;
7495 if (val < 0 || val > 255)
7496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007497 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007498 return;
7499 }
7500
7501 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7502 return;
7503 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7504 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7505 *(p + before) = val;
7506 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7507
7508 copy_tv(&argvars[0], rettv);
7509 }
7510 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007511 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007512 else if ((l = argvars[0].vval.v_list) != NULL
7513 && !var_check_lock(l->lv_lock,
7514 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007515 {
7516 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007517 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007518 if (error)
7519 return; /* type error; errmsg already given */
7520
7521 if (before == l->lv_len)
7522 item = NULL;
7523 else
7524 {
7525 item = list_find(l, before);
7526 if (item == NULL)
7527 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007528 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007529 l = NULL;
7530 }
7531 }
7532 if (l != NULL)
7533 {
7534 list_insert_tv(l, &argvars[1], item);
7535 copy_tv(&argvars[0], rettv);
7536 }
7537 }
7538}
7539
7540/*
7541 * "invert(expr)" function
7542 */
7543 static void
7544f_invert(typval_T *argvars, typval_T *rettv)
7545{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007546 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007547}
7548
7549/*
7550 * "isdirectory()" function
7551 */
7552 static void
7553f_isdirectory(typval_T *argvars, typval_T *rettv)
7554{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007555 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007556}
7557
7558/*
7559 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7560 * or it refers to a List or Dictionary that is locked.
7561 */
7562 static int
7563tv_islocked(typval_T *tv)
7564{
7565 return (tv->v_lock & VAR_LOCKED)
7566 || (tv->v_type == VAR_LIST
7567 && tv->vval.v_list != NULL
7568 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7569 || (tv->v_type == VAR_DICT
7570 && tv->vval.v_dict != NULL
7571 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7572}
7573
7574/*
7575 * "islocked()" function
7576 */
7577 static void
7578f_islocked(typval_T *argvars, typval_T *rettv)
7579{
7580 lval_T lv;
7581 char_u *end;
7582 dictitem_T *di;
7583
7584 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007585 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007586 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007587 if (end != NULL && lv.ll_name != NULL)
7588 {
7589 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007590 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007591 else
7592 {
7593 if (lv.ll_tv == NULL)
7594 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007595 di = find_var(lv.ll_name, NULL, TRUE);
7596 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007597 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007598 /* Consider a variable locked when:
7599 * 1. the variable itself is locked
7600 * 2. the value of the variable is locked.
7601 * 3. the List or Dict value is locked.
7602 */
7603 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7604 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007605 }
7606 }
7607 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007608 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007609 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007610 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007611 else if (lv.ll_list != NULL)
7612 /* List item. */
7613 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7614 else
7615 /* Dictionary item. */
7616 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7617 }
7618 }
7619
7620 clear_lval(&lv);
7621}
7622
7623#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7624/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007625 * "isinf()" function
7626 */
7627 static void
7628f_isinf(typval_T *argvars, typval_T *rettv)
7629{
7630 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7631 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7632}
7633
7634/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007635 * "isnan()" function
7636 */
7637 static void
7638f_isnan(typval_T *argvars, typval_T *rettv)
7639{
7640 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7641 && isnan(argvars[0].vval.v_float);
7642}
7643#endif
7644
7645/*
7646 * "items(dict)" function
7647 */
7648 static void
7649f_items(typval_T *argvars, typval_T *rettv)
7650{
7651 dict_list(argvars, rettv, 2);
7652}
7653
7654#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7655/*
7656 * Get the job from the argument.
7657 * Returns NULL if the job is invalid.
7658 */
7659 static job_T *
7660get_job_arg(typval_T *tv)
7661{
7662 job_T *job;
7663
7664 if (tv->v_type != VAR_JOB)
7665 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007666 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007667 return NULL;
7668 }
7669 job = tv->vval.v_job;
7670
7671 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007672 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007673 return job;
7674}
7675
7676/*
7677 * "job_getchannel()" function
7678 */
7679 static void
7680f_job_getchannel(typval_T *argvars, typval_T *rettv)
7681{
7682 job_T *job = get_job_arg(&argvars[0]);
7683
7684 if (job != NULL)
7685 {
7686 rettv->v_type = VAR_CHANNEL;
7687 rettv->vval.v_channel = job->jv_channel;
7688 if (job->jv_channel != NULL)
7689 ++job->jv_channel->ch_refcount;
7690 }
7691}
7692
7693/*
7694 * "job_info()" function
7695 */
7696 static void
7697f_job_info(typval_T *argvars, typval_T *rettv)
7698{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007699 if (argvars[0].v_type != VAR_UNKNOWN)
7700 {
7701 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007702
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007703 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7704 job_info(job, rettv->vval.v_dict);
7705 }
7706 else if (rettv_list_alloc(rettv) == OK)
7707 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007708}
7709
7710/*
7711 * "job_setoptions()" function
7712 */
7713 static void
7714f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7715{
7716 job_T *job = get_job_arg(&argvars[0]);
7717 jobopt_T opt;
7718
7719 if (job == NULL)
7720 return;
7721 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007722 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007723 job_set_options(job, &opt);
7724 free_job_options(&opt);
7725}
7726
7727/*
7728 * "job_start()" function
7729 */
7730 static void
7731f_job_start(typval_T *argvars, typval_T *rettv)
7732{
7733 rettv->v_type = VAR_JOB;
7734 if (check_restricted() || check_secure())
7735 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007736 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007737}
7738
7739/*
7740 * "job_status()" function
7741 */
7742 static void
7743f_job_status(typval_T *argvars, typval_T *rettv)
7744{
7745 job_T *job = get_job_arg(&argvars[0]);
7746
7747 if (job != NULL)
7748 {
7749 rettv->v_type = VAR_STRING;
7750 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7751 }
7752}
7753
7754/*
7755 * "job_stop()" function
7756 */
7757 static void
7758f_job_stop(typval_T *argvars, typval_T *rettv)
7759{
7760 job_T *job = get_job_arg(&argvars[0]);
7761
7762 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007763 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007764}
7765#endif
7766
7767/*
7768 * "join()" function
7769 */
7770 static void
7771f_join(typval_T *argvars, typval_T *rettv)
7772{
7773 garray_T ga;
7774 char_u *sep;
7775
7776 if (argvars[0].v_type != VAR_LIST)
7777 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007778 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007779 return;
7780 }
7781 if (argvars[0].vval.v_list == NULL)
7782 return;
7783 if (argvars[1].v_type == VAR_UNKNOWN)
7784 sep = (char_u *)" ";
7785 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007786 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007787
7788 rettv->v_type = VAR_STRING;
7789
7790 if (sep != NULL)
7791 {
7792 ga_init2(&ga, (int)sizeof(char), 80);
7793 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7794 ga_append(&ga, NUL);
7795 rettv->vval.v_string = (char_u *)ga.ga_data;
7796 }
7797 else
7798 rettv->vval.v_string = NULL;
7799}
7800
7801/*
7802 * "js_decode()" function
7803 */
7804 static void
7805f_js_decode(typval_T *argvars, typval_T *rettv)
7806{
7807 js_read_T reader;
7808
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007809 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007810 reader.js_fill = NULL;
7811 reader.js_used = 0;
7812 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007813 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007814}
7815
7816/*
7817 * "js_encode()" function
7818 */
7819 static void
7820f_js_encode(typval_T *argvars, typval_T *rettv)
7821{
7822 rettv->v_type = VAR_STRING;
7823 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7824}
7825
7826/*
7827 * "json_decode()" function
7828 */
7829 static void
7830f_json_decode(typval_T *argvars, typval_T *rettv)
7831{
7832 js_read_T reader;
7833
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007834 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007835 reader.js_fill = NULL;
7836 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007837 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007838}
7839
7840/*
7841 * "json_encode()" function
7842 */
7843 static void
7844f_json_encode(typval_T *argvars, typval_T *rettv)
7845{
7846 rettv->v_type = VAR_STRING;
7847 rettv->vval.v_string = json_encode(&argvars[0], 0);
7848}
7849
7850/*
7851 * "keys()" function
7852 */
7853 static void
7854f_keys(typval_T *argvars, typval_T *rettv)
7855{
7856 dict_list(argvars, rettv, 0);
7857}
7858
7859/*
7860 * "last_buffer_nr()" function.
7861 */
7862 static void
7863f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7864{
7865 int n = 0;
7866 buf_T *buf;
7867
Bram Moolenaar29323592016-07-24 22:04:11 +02007868 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007869 if (n < buf->b_fnum)
7870 n = buf->b_fnum;
7871
7872 rettv->vval.v_number = n;
7873}
7874
7875/*
7876 * "len()" function
7877 */
7878 static void
7879f_len(typval_T *argvars, typval_T *rettv)
7880{
7881 switch (argvars[0].v_type)
7882 {
7883 case VAR_STRING:
7884 case VAR_NUMBER:
7885 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007886 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007887 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007888 case VAR_BLOB:
7889 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7890 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007891 case VAR_LIST:
7892 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7893 break;
7894 case VAR_DICT:
7895 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7896 break;
7897 case VAR_UNKNOWN:
7898 case VAR_SPECIAL:
7899 case VAR_FLOAT:
7900 case VAR_FUNC:
7901 case VAR_PARTIAL:
7902 case VAR_JOB:
7903 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007904 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007905 break;
7906 }
7907}
7908
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007909 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007910libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007911{
7912#ifdef FEAT_LIBCALL
7913 char_u *string_in;
7914 char_u **string_result;
7915 int nr_result;
7916#endif
7917
7918 rettv->v_type = type;
7919 if (type != VAR_NUMBER)
7920 rettv->vval.v_string = NULL;
7921
7922 if (check_restricted() || check_secure())
7923 return;
7924
7925#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007926 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007927 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7928 {
7929 string_in = NULL;
7930 if (argvars[2].v_type == VAR_STRING)
7931 string_in = argvars[2].vval.v_string;
7932 if (type == VAR_NUMBER)
7933 string_result = NULL;
7934 else
7935 string_result = &rettv->vval.v_string;
7936 if (mch_libcall(argvars[0].vval.v_string,
7937 argvars[1].vval.v_string,
7938 string_in,
7939 argvars[2].vval.v_number,
7940 string_result,
7941 &nr_result) == OK
7942 && type == VAR_NUMBER)
7943 rettv->vval.v_number = nr_result;
7944 }
7945#endif
7946}
7947
7948/*
7949 * "libcall()" function
7950 */
7951 static void
7952f_libcall(typval_T *argvars, typval_T *rettv)
7953{
7954 libcall_common(argvars, rettv, VAR_STRING);
7955}
7956
7957/*
7958 * "libcallnr()" function
7959 */
7960 static void
7961f_libcallnr(typval_T *argvars, typval_T *rettv)
7962{
7963 libcall_common(argvars, rettv, VAR_NUMBER);
7964}
7965
7966/*
7967 * "line(string)" function
7968 */
7969 static void
7970f_line(typval_T *argvars, typval_T *rettv)
7971{
7972 linenr_T lnum = 0;
7973 pos_T *fp;
7974 int fnum;
7975
7976 fp = var2fpos(&argvars[0], TRUE, &fnum);
7977 if (fp != NULL)
7978 lnum = fp->lnum;
7979 rettv->vval.v_number = lnum;
7980}
7981
7982/*
7983 * "line2byte(lnum)" function
7984 */
7985 static void
7986f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7987{
7988#ifndef FEAT_BYTEOFF
7989 rettv->vval.v_number = -1;
7990#else
7991 linenr_T lnum;
7992
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007993 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007994 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7995 rettv->vval.v_number = -1;
7996 else
7997 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7998 if (rettv->vval.v_number >= 0)
7999 ++rettv->vval.v_number;
8000#endif
8001}
8002
8003/*
8004 * "lispindent(lnum)" function
8005 */
8006 static void
8007f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8008{
8009#ifdef FEAT_LISP
8010 pos_T pos;
8011 linenr_T lnum;
8012
8013 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008014 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008015 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8016 {
8017 curwin->w_cursor.lnum = lnum;
8018 rettv->vval.v_number = get_lisp_indent();
8019 curwin->w_cursor = pos;
8020 }
8021 else
8022#endif
8023 rettv->vval.v_number = -1;
8024}
8025
8026/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008027 * "list2str()" function
8028 */
8029 static void
8030f_list2str(typval_T *argvars, typval_T *rettv)
8031{
8032 list_T *l;
8033 listitem_T *li;
8034 garray_T ga;
8035 int utf8 = FALSE;
8036
8037 rettv->v_type = VAR_STRING;
8038 rettv->vval.v_string = NULL;
8039 if (argvars[0].v_type != VAR_LIST)
8040 {
8041 emsg(_(e_invarg));
8042 return;
8043 }
8044
8045 l = argvars[0].vval.v_list;
8046 if (l == NULL)
8047 return; // empty list results in empty string
8048
8049 if (argvars[1].v_type != VAR_UNKNOWN)
8050 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8051
8052 ga_init2(&ga, 1, 80);
8053 if (has_mbyte || utf8)
8054 {
8055 char_u buf[MB_MAXBYTES + 1];
8056 int (*char2bytes)(int, char_u *);
8057
8058 if (utf8 || enc_utf8)
8059 char2bytes = utf_char2bytes;
8060 else
8061 char2bytes = mb_char2bytes;
8062
8063 for (li = l->lv_first; li != NULL; li = li->li_next)
8064 {
8065 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8066 ga_concat(&ga, buf);
8067 }
8068 ga_append(&ga, NUL);
8069 }
8070 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8071 {
8072 for (li = l->lv_first; li != NULL; li = li->li_next)
8073 ga_append(&ga, tv_get_number(&li->li_tv));
8074 ga_append(&ga, NUL);
8075 }
8076
8077 rettv->v_type = VAR_STRING;
8078 rettv->vval.v_string = ga.ga_data;
8079}
8080
8081/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008082 * "localtime()" function
8083 */
8084 static void
8085f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8086{
8087 rettv->vval.v_number = (varnumber_T)time(NULL);
8088}
8089
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090 static void
8091get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8092{
8093 char_u *keys;
8094 char_u *which;
8095 char_u buf[NUMBUFLEN];
8096 char_u *keys_buf = NULL;
8097 char_u *rhs;
8098 int mode;
8099 int abbr = FALSE;
8100 int get_dict = FALSE;
8101 mapblock_T *mp;
8102 int buffer_local;
8103
8104 /* return empty string for failure */
8105 rettv->v_type = VAR_STRING;
8106 rettv->vval.v_string = NULL;
8107
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008108 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008109 if (*keys == NUL)
8110 return;
8111
8112 if (argvars[1].v_type != VAR_UNKNOWN)
8113 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008114 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115 if (argvars[2].v_type != VAR_UNKNOWN)
8116 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008117 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008119 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008120 }
8121 }
8122 else
8123 which = (char_u *)"";
8124 if (which == NULL)
8125 return;
8126
8127 mode = get_map_mode(&which, 0);
8128
8129 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8130 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8131 vim_free(keys_buf);
8132
8133 if (!get_dict)
8134 {
8135 /* Return a string. */
8136 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008137 {
8138 if (*rhs == NUL)
8139 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8140 else
8141 rettv->vval.v_string = str2special_save(rhs, FALSE);
8142 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008143
8144 }
8145 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8146 {
8147 /* Return a dictionary. */
8148 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8149 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8150 dict_T *dict = rettv->vval.v_dict;
8151
Bram Moolenaare0be1672018-07-08 16:50:37 +02008152 dict_add_string(dict, "lhs", lhs);
8153 dict_add_string(dict, "rhs", mp->m_orig_str);
8154 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8155 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8156 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008157 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8158 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008159 dict_add_number(dict, "buffer", (long)buffer_local);
8160 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8161 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008162
8163 vim_free(lhs);
8164 vim_free(mapmode);
8165 }
8166}
8167
8168#ifdef FEAT_FLOAT
8169/*
8170 * "log()" function
8171 */
8172 static void
8173f_log(typval_T *argvars, typval_T *rettv)
8174{
8175 float_T f = 0.0;
8176
8177 rettv->v_type = VAR_FLOAT;
8178 if (get_float_arg(argvars, &f) == OK)
8179 rettv->vval.v_float = log(f);
8180 else
8181 rettv->vval.v_float = 0.0;
8182}
8183
8184/*
8185 * "log10()" function
8186 */
8187 static void
8188f_log10(typval_T *argvars, typval_T *rettv)
8189{
8190 float_T f = 0.0;
8191
8192 rettv->v_type = VAR_FLOAT;
8193 if (get_float_arg(argvars, &f) == OK)
8194 rettv->vval.v_float = log10(f);
8195 else
8196 rettv->vval.v_float = 0.0;
8197}
8198#endif
8199
8200#ifdef FEAT_LUA
8201/*
8202 * "luaeval()" function
8203 */
8204 static void
8205f_luaeval(typval_T *argvars, typval_T *rettv)
8206{
8207 char_u *str;
8208 char_u buf[NUMBUFLEN];
8209
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008210 if (check_restricted() || check_secure())
8211 return;
8212
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008213 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008214 do_luaeval(str, argvars + 1, rettv);
8215}
8216#endif
8217
8218/*
8219 * "map()" function
8220 */
8221 static void
8222f_map(typval_T *argvars, typval_T *rettv)
8223{
8224 filter_map(argvars, rettv, TRUE);
8225}
8226
8227/*
8228 * "maparg()" function
8229 */
8230 static void
8231f_maparg(typval_T *argvars, typval_T *rettv)
8232{
8233 get_maparg(argvars, rettv, TRUE);
8234}
8235
8236/*
8237 * "mapcheck()" function
8238 */
8239 static void
8240f_mapcheck(typval_T *argvars, typval_T *rettv)
8241{
8242 get_maparg(argvars, rettv, FALSE);
8243}
8244
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008245typedef enum
8246{
8247 MATCH_END, /* matchend() */
8248 MATCH_MATCH, /* match() */
8249 MATCH_STR, /* matchstr() */
8250 MATCH_LIST, /* matchlist() */
8251 MATCH_POS /* matchstrpos() */
8252} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008253
8254 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008255find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008256{
8257 char_u *str = NULL;
8258 long len = 0;
8259 char_u *expr = NULL;
8260 char_u *pat;
8261 regmatch_T regmatch;
8262 char_u patbuf[NUMBUFLEN];
8263 char_u strbuf[NUMBUFLEN];
8264 char_u *save_cpo;
8265 long start = 0;
8266 long nth = 1;
8267 colnr_T startcol = 0;
8268 int match = 0;
8269 list_T *l = NULL;
8270 listitem_T *li = NULL;
8271 long idx = 0;
8272 char_u *tofree = NULL;
8273
8274 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8275 save_cpo = p_cpo;
8276 p_cpo = (char_u *)"";
8277
8278 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008279 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008280 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008281 /* type MATCH_LIST: return empty list when there are no matches.
8282 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008283 if (rettv_list_alloc(rettv) == FAIL)
8284 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008285 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008286 && (list_append_string(rettv->vval.v_list,
8287 (char_u *)"", 0) == FAIL
8288 || list_append_number(rettv->vval.v_list,
8289 (varnumber_T)-1) == FAIL
8290 || list_append_number(rettv->vval.v_list,
8291 (varnumber_T)-1) == FAIL
8292 || list_append_number(rettv->vval.v_list,
8293 (varnumber_T)-1) == FAIL))
8294 {
8295 list_free(rettv->vval.v_list);
8296 rettv->vval.v_list = NULL;
8297 goto theend;
8298 }
8299 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008300 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008301 {
8302 rettv->v_type = VAR_STRING;
8303 rettv->vval.v_string = NULL;
8304 }
8305
8306 if (argvars[0].v_type == VAR_LIST)
8307 {
8308 if ((l = argvars[0].vval.v_list) == NULL)
8309 goto theend;
8310 li = l->lv_first;
8311 }
8312 else
8313 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008314 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008315 len = (long)STRLEN(str);
8316 }
8317
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008318 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008319 if (pat == NULL)
8320 goto theend;
8321
8322 if (argvars[2].v_type != VAR_UNKNOWN)
8323 {
8324 int error = FALSE;
8325
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008326 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008327 if (error)
8328 goto theend;
8329 if (l != NULL)
8330 {
8331 li = list_find(l, start);
8332 if (li == NULL)
8333 goto theend;
8334 idx = l->lv_idx; /* use the cached index */
8335 }
8336 else
8337 {
8338 if (start < 0)
8339 start = 0;
8340 if (start > len)
8341 goto theend;
8342 /* When "count" argument is there ignore matches before "start",
8343 * otherwise skip part of the string. Differs when pattern is "^"
8344 * or "\<". */
8345 if (argvars[3].v_type != VAR_UNKNOWN)
8346 startcol = start;
8347 else
8348 {
8349 str += start;
8350 len -= start;
8351 }
8352 }
8353
8354 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008355 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008356 if (error)
8357 goto theend;
8358 }
8359
8360 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8361 if (regmatch.regprog != NULL)
8362 {
8363 regmatch.rm_ic = p_ic;
8364
8365 for (;;)
8366 {
8367 if (l != NULL)
8368 {
8369 if (li == NULL)
8370 {
8371 match = FALSE;
8372 break;
8373 }
8374 vim_free(tofree);
8375 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8376 if (str == NULL)
8377 break;
8378 }
8379
8380 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8381
8382 if (match && --nth <= 0)
8383 break;
8384 if (l == NULL && !match)
8385 break;
8386
8387 /* Advance to just after the match. */
8388 if (l != NULL)
8389 {
8390 li = li->li_next;
8391 ++idx;
8392 }
8393 else
8394 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008395 startcol = (colnr_T)(regmatch.startp[0]
8396 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008397 if (startcol > (colnr_T)len
8398 || str + startcol <= regmatch.startp[0])
8399 {
8400 match = FALSE;
8401 break;
8402 }
8403 }
8404 }
8405
8406 if (match)
8407 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008408 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409 {
8410 listitem_T *li1 = rettv->vval.v_list->lv_first;
8411 listitem_T *li2 = li1->li_next;
8412 listitem_T *li3 = li2->li_next;
8413 listitem_T *li4 = li3->li_next;
8414
8415 vim_free(li1->li_tv.vval.v_string);
8416 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8417 (int)(regmatch.endp[0] - regmatch.startp[0]));
8418 li3->li_tv.vval.v_number =
8419 (varnumber_T)(regmatch.startp[0] - expr);
8420 li4->li_tv.vval.v_number =
8421 (varnumber_T)(regmatch.endp[0] - expr);
8422 if (l != NULL)
8423 li2->li_tv.vval.v_number = (varnumber_T)idx;
8424 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008425 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426 {
8427 int i;
8428
8429 /* return list with matched string and submatches */
8430 for (i = 0; i < NSUBEXP; ++i)
8431 {
8432 if (regmatch.endp[i] == NULL)
8433 {
8434 if (list_append_string(rettv->vval.v_list,
8435 (char_u *)"", 0) == FAIL)
8436 break;
8437 }
8438 else if (list_append_string(rettv->vval.v_list,
8439 regmatch.startp[i],
8440 (int)(regmatch.endp[i] - regmatch.startp[i]))
8441 == FAIL)
8442 break;
8443 }
8444 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008445 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008446 {
8447 /* return matched string */
8448 if (l != NULL)
8449 copy_tv(&li->li_tv, rettv);
8450 else
8451 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8452 (int)(regmatch.endp[0] - regmatch.startp[0]));
8453 }
8454 else if (l != NULL)
8455 rettv->vval.v_number = idx;
8456 else
8457 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008458 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008459 rettv->vval.v_number =
8460 (varnumber_T)(regmatch.startp[0] - str);
8461 else
8462 rettv->vval.v_number =
8463 (varnumber_T)(regmatch.endp[0] - str);
8464 rettv->vval.v_number += (varnumber_T)(str - expr);
8465 }
8466 }
8467 vim_regfree(regmatch.regprog);
8468 }
8469
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008470theend:
8471 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008472 /* matchstrpos() without a list: drop the second item. */
8473 listitem_remove(rettv->vval.v_list,
8474 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008475 vim_free(tofree);
8476 p_cpo = save_cpo;
8477}
8478
8479/*
8480 * "match()" function
8481 */
8482 static void
8483f_match(typval_T *argvars, typval_T *rettv)
8484{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008485 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008486}
8487
Bram Moolenaar95e51472018-07-28 16:55:56 +02008488#ifdef FEAT_SEARCH_EXTRA
8489 static int
8490matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8491{
8492 dictitem_T *di;
8493
8494 if (tv->v_type != VAR_DICT)
8495 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008496 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008497 return FAIL;
8498 }
8499
8500 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008501 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008502 (char_u *)"conceal", FALSE);
8503
8504 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8505 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008506 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008507 if (*win == NULL)
8508 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008509 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008510 return FAIL;
8511 }
8512 }
8513
8514 return OK;
8515}
8516#endif
8517
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008518/*
8519 * "matchadd()" function
8520 */
8521 static void
8522f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8523{
8524#ifdef FEAT_SEARCH_EXTRA
8525 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008526 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8527 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008528 int prio = 10; /* default priority */
8529 int id = -1;
8530 int error = FALSE;
8531 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008532 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008533
8534 rettv->vval.v_number = -1;
8535
8536 if (grp == NULL || pat == NULL)
8537 return;
8538 if (argvars[2].v_type != VAR_UNKNOWN)
8539 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008540 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008541 if (argvars[3].v_type != VAR_UNKNOWN)
8542 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008543 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008544 if (argvars[4].v_type != VAR_UNKNOWN
8545 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8546 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547 }
8548 }
8549 if (error == TRUE)
8550 return;
8551 if (id >= 1 && id <= 3)
8552 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008553 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008554 return;
8555 }
8556
Bram Moolenaar95e51472018-07-28 16:55:56 +02008557 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008558 conceal_char);
8559#endif
8560}
8561
8562/*
8563 * "matchaddpos()" function
8564 */
8565 static void
8566f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8567{
8568#ifdef FEAT_SEARCH_EXTRA
8569 char_u buf[NUMBUFLEN];
8570 char_u *group;
8571 int prio = 10;
8572 int id = -1;
8573 int error = FALSE;
8574 list_T *l;
8575 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008576 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577
8578 rettv->vval.v_number = -1;
8579
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008580 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581 if (group == NULL)
8582 return;
8583
8584 if (argvars[1].v_type != VAR_LIST)
8585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008586 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587 return;
8588 }
8589 l = argvars[1].vval.v_list;
8590 if (l == NULL)
8591 return;
8592
8593 if (argvars[2].v_type != VAR_UNKNOWN)
8594 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008595 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008596 if (argvars[3].v_type != VAR_UNKNOWN)
8597 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008598 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008599
8600 if (argvars[4].v_type != VAR_UNKNOWN
8601 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8602 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 }
8604 }
8605 if (error == TRUE)
8606 return;
8607
8608 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8609 if (id == 1 || id == 2)
8610 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008611 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612 return;
8613 }
8614
Bram Moolenaar95e51472018-07-28 16:55:56 +02008615 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008616 conceal_char);
8617#endif
8618}
8619
8620/*
8621 * "matcharg()" function
8622 */
8623 static void
8624f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8625{
8626 if (rettv_list_alloc(rettv) == OK)
8627 {
8628#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008629 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630 matchitem_T *m;
8631
8632 if (id >= 1 && id <= 3)
8633 {
8634 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8635 {
8636 list_append_string(rettv->vval.v_list,
8637 syn_id2name(m->hlg_id), -1);
8638 list_append_string(rettv->vval.v_list, m->pattern, -1);
8639 }
8640 else
8641 {
8642 list_append_string(rettv->vval.v_list, NULL, -1);
8643 list_append_string(rettv->vval.v_list, NULL, -1);
8644 }
8645 }
8646#endif
8647 }
8648}
8649
8650/*
8651 * "matchdelete()" function
8652 */
8653 static void
8654f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8655{
8656#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008657 win_T *win = get_optional_window(argvars, 1);
8658
8659 if (win == NULL)
8660 rettv->vval.v_number = -1;
8661 else
8662 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008663 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008664#endif
8665}
8666
8667/*
8668 * "matchend()" function
8669 */
8670 static void
8671f_matchend(typval_T *argvars, typval_T *rettv)
8672{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008673 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674}
8675
8676/*
8677 * "matchlist()" function
8678 */
8679 static void
8680f_matchlist(typval_T *argvars, typval_T *rettv)
8681{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008682 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008683}
8684
8685/*
8686 * "matchstr()" function
8687 */
8688 static void
8689f_matchstr(typval_T *argvars, typval_T *rettv)
8690{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008691 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008692}
8693
8694/*
8695 * "matchstrpos()" function
8696 */
8697 static void
8698f_matchstrpos(typval_T *argvars, typval_T *rettv)
8699{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008700 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701}
8702
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008703 static void
8704max_min(typval_T *argvars, typval_T *rettv, int domax)
8705{
8706 varnumber_T n = 0;
8707 varnumber_T i;
8708 int error = FALSE;
8709
8710 if (argvars[0].v_type == VAR_LIST)
8711 {
8712 list_T *l;
8713 listitem_T *li;
8714
8715 l = argvars[0].vval.v_list;
8716 if (l != NULL)
8717 {
8718 li = l->lv_first;
8719 if (li != NULL)
8720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008721 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008722 for (;;)
8723 {
8724 li = li->li_next;
8725 if (li == NULL)
8726 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008727 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728 if (domax ? i > n : i < n)
8729 n = i;
8730 }
8731 }
8732 }
8733 }
8734 else if (argvars[0].v_type == VAR_DICT)
8735 {
8736 dict_T *d;
8737 int first = TRUE;
8738 hashitem_T *hi;
8739 int todo;
8740
8741 d = argvars[0].vval.v_dict;
8742 if (d != NULL)
8743 {
8744 todo = (int)d->dv_hashtab.ht_used;
8745 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8746 {
8747 if (!HASHITEM_EMPTY(hi))
8748 {
8749 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008750 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751 if (first)
8752 {
8753 n = i;
8754 first = FALSE;
8755 }
8756 else if (domax ? i > n : i < n)
8757 n = i;
8758 }
8759 }
8760 }
8761 }
8762 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008763 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764 rettv->vval.v_number = error ? 0 : n;
8765}
8766
8767/*
8768 * "max()" function
8769 */
8770 static void
8771f_max(typval_T *argvars, typval_T *rettv)
8772{
8773 max_min(argvars, rettv, TRUE);
8774}
8775
8776/*
8777 * "min()" function
8778 */
8779 static void
8780f_min(typval_T *argvars, typval_T *rettv)
8781{
8782 max_min(argvars, rettv, FALSE);
8783}
8784
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008785/*
8786 * Create the directory in which "dir" is located, and higher levels when
8787 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008788 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789 */
8790 static int
8791mkdir_recurse(char_u *dir, int prot)
8792{
8793 char_u *p;
8794 char_u *updir;
8795 int r = FAIL;
8796
8797 /* Get end of directory name in "dir".
8798 * We're done when it's "/" or "c:/". */
8799 p = gettail_sep(dir);
8800 if (p <= get_past_head(dir))
8801 return OK;
8802
8803 /* If the directory exists we're done. Otherwise: create it.*/
8804 updir = vim_strnsave(dir, (int)(p - dir));
8805 if (updir == NULL)
8806 return FAIL;
8807 if (mch_isdir(updir))
8808 r = OK;
8809 else if (mkdir_recurse(updir, prot) == OK)
8810 r = vim_mkdir_emsg(updir, prot);
8811 vim_free(updir);
8812 return r;
8813}
8814
8815#ifdef vim_mkdir
8816/*
8817 * "mkdir()" function
8818 */
8819 static void
8820f_mkdir(typval_T *argvars, typval_T *rettv)
8821{
8822 char_u *dir;
8823 char_u buf[NUMBUFLEN];
8824 int prot = 0755;
8825
8826 rettv->vval.v_number = FAIL;
8827 if (check_restricted() || check_secure())
8828 return;
8829
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008830 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008832 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008833
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008834 if (*gettail(dir) == NUL)
8835 /* remove trailing slashes */
8836 *gettail_sep(dir) = NUL;
8837
8838 if (argvars[1].v_type != VAR_UNKNOWN)
8839 {
8840 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008841 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008842 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008843 if (prot == -1)
8844 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008845 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008846 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008847 {
8848 if (mch_isdir(dir))
8849 {
8850 /* With the "p" flag it's OK if the dir already exists. */
8851 rettv->vval.v_number = OK;
8852 return;
8853 }
8854 mkdir_recurse(dir, prot);
8855 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008856 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008857 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008858}
8859#endif
8860
8861/*
8862 * "mode()" function
8863 */
8864 static void
8865f_mode(typval_T *argvars, typval_T *rettv)
8866{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008867 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008868
Bram Moolenaar612cc382018-07-29 15:34:26 +02008869 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008870
8871 if (time_for_testing == 93784)
8872 {
8873 /* Testing the two-character code. */
8874 buf[0] = 'x';
8875 buf[1] = '!';
8876 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008877#ifdef FEAT_TERMINAL
8878 else if (term_use_loop())
8879 buf[0] = 't';
8880#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008881 else if (VIsual_active)
8882 {
8883 if (VIsual_select)
8884 buf[0] = VIsual_mode + 's' - 'v';
8885 else
8886 buf[0] = VIsual_mode;
8887 }
8888 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8889 || State == CONFIRM)
8890 {
8891 buf[0] = 'r';
8892 if (State == ASKMORE)
8893 buf[1] = 'm';
8894 else if (State == CONFIRM)
8895 buf[1] = '?';
8896 }
8897 else if (State == EXTERNCMD)
8898 buf[0] = '!';
8899 else if (State & INSERT)
8900 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008901 if (State & VREPLACE_FLAG)
8902 {
8903 buf[0] = 'R';
8904 buf[1] = 'v';
8905 }
8906 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008907 {
8908 if (State & REPLACE_FLAG)
8909 buf[0] = 'R';
8910 else
8911 buf[0] = 'i';
8912#ifdef FEAT_INS_EXPAND
8913 if (ins_compl_active())
8914 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008915 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008916 buf[1] = 'x';
8917#endif
8918 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008919 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008920 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008921 {
8922 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008923 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008925 else if (exmode_active == EXMODE_NORMAL)
8926 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008927 }
8928 else
8929 {
8930 buf[0] = 'n';
8931 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008932 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008933 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008934 // to be able to detect force-linewise/blockwise/characterwise operations
8935 buf[2] = motion_force;
8936 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008937 else if (restart_edit == 'I' || restart_edit == 'R'
8938 || restart_edit == 'V')
8939 {
8940 buf[1] = 'i';
8941 buf[2] = restart_edit;
8942 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008943 }
8944
8945 /* Clear out the minor mode when the argument is not a non-zero number or
8946 * non-empty string. */
8947 if (!non_zero_arg(&argvars[0]))
8948 buf[1] = NUL;
8949
8950 rettv->vval.v_string = vim_strsave(buf);
8951 rettv->v_type = VAR_STRING;
8952}
8953
8954#if defined(FEAT_MZSCHEME) || defined(PROTO)
8955/*
8956 * "mzeval()" function
8957 */
8958 static void
8959f_mzeval(typval_T *argvars, typval_T *rettv)
8960{
8961 char_u *str;
8962 char_u buf[NUMBUFLEN];
8963
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008964 if (check_restricted() || check_secure())
8965 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008966 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008967 do_mzeval(str, rettv);
8968}
8969
8970 void
8971mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8972{
8973 typval_T argvars[3];
8974
8975 argvars[0].v_type = VAR_STRING;
8976 argvars[0].vval.v_string = name;
8977 copy_tv(args, &argvars[1]);
8978 argvars[2].v_type = VAR_UNKNOWN;
8979 f_call(argvars, rettv);
8980 clear_tv(&argvars[1]);
8981}
8982#endif
8983
8984/*
8985 * "nextnonblank()" function
8986 */
8987 static void
8988f_nextnonblank(typval_T *argvars, typval_T *rettv)
8989{
8990 linenr_T lnum;
8991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008992 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008993 {
8994 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8995 {
8996 lnum = 0;
8997 break;
8998 }
8999 if (*skipwhite(ml_get(lnum)) != NUL)
9000 break;
9001 }
9002 rettv->vval.v_number = lnum;
9003}
9004
9005/*
9006 * "nr2char()" function
9007 */
9008 static void
9009f_nr2char(typval_T *argvars, typval_T *rettv)
9010{
9011 char_u buf[NUMBUFLEN];
9012
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009013 if (has_mbyte)
9014 {
9015 int utf8 = 0;
9016
9017 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009018 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009020 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009021 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009022 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023 }
9024 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009025 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009026 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009027 buf[1] = NUL;
9028 }
9029 rettv->v_type = VAR_STRING;
9030 rettv->vval.v_string = vim_strsave(buf);
9031}
9032
9033/*
9034 * "or(expr, expr)" function
9035 */
9036 static void
9037f_or(typval_T *argvars, typval_T *rettv)
9038{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009039 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9040 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009041}
9042
9043/*
9044 * "pathshorten()" function
9045 */
9046 static void
9047f_pathshorten(typval_T *argvars, typval_T *rettv)
9048{
9049 char_u *p;
9050
9051 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009052 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009053 if (p == NULL)
9054 rettv->vval.v_string = NULL;
9055 else
9056 {
9057 p = vim_strsave(p);
9058 rettv->vval.v_string = p;
9059 if (p != NULL)
9060 shorten_dir(p);
9061 }
9062}
9063
9064#ifdef FEAT_PERL
9065/*
9066 * "perleval()" function
9067 */
9068 static void
9069f_perleval(typval_T *argvars, typval_T *rettv)
9070{
9071 char_u *str;
9072 char_u buf[NUMBUFLEN];
9073
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009074 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 do_perleval(str, rettv);
9076}
9077#endif
9078
9079#ifdef FEAT_FLOAT
9080/*
9081 * "pow()" function
9082 */
9083 static void
9084f_pow(typval_T *argvars, typval_T *rettv)
9085{
9086 float_T fx = 0.0, fy = 0.0;
9087
9088 rettv->v_type = VAR_FLOAT;
9089 if (get_float_arg(argvars, &fx) == OK
9090 && get_float_arg(&argvars[1], &fy) == OK)
9091 rettv->vval.v_float = pow(fx, fy);
9092 else
9093 rettv->vval.v_float = 0.0;
9094}
9095#endif
9096
9097/*
9098 * "prevnonblank()" function
9099 */
9100 static void
9101f_prevnonblank(typval_T *argvars, typval_T *rettv)
9102{
9103 linenr_T lnum;
9104
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009105 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009106 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9107 lnum = 0;
9108 else
9109 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9110 --lnum;
9111 rettv->vval.v_number = lnum;
9112}
9113
9114/* This dummy va_list is here because:
9115 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9116 * - locally in the function results in a "used before set" warning
9117 * - using va_start() to initialize it gives "function with fixed args" error */
9118static va_list ap;
9119
9120/*
9121 * "printf()" function
9122 */
9123 static void
9124f_printf(typval_T *argvars, typval_T *rettv)
9125{
9126 char_u buf[NUMBUFLEN];
9127 int len;
9128 char_u *s;
9129 int saved_did_emsg = did_emsg;
9130 char *fmt;
9131
9132 rettv->v_type = VAR_STRING;
9133 rettv->vval.v_string = NULL;
9134
9135 /* Get the required length, allocate the buffer and do it for real. */
9136 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009137 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009138 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009139 if (!did_emsg)
9140 {
9141 s = alloc(len + 1);
9142 if (s != NULL)
9143 {
9144 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009145 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9146 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009147 }
9148 }
9149 did_emsg |= saved_did_emsg;
9150}
9151
Bram Moolenaarf2732452018-06-03 14:47:35 +02009152#ifdef FEAT_JOB_CHANNEL
9153/*
9154 * "prompt_setcallback({buffer}, {callback})" function
9155 */
9156 static void
9157f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9158{
9159 buf_T *buf;
9160 char_u *callback;
9161 partial_T *partial;
9162
9163 if (check_secure())
9164 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009165 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009166 if (buf == NULL)
9167 return;
9168
9169 callback = get_callback(&argvars[1], &partial);
9170 if (callback == NULL)
9171 return;
9172
9173 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
9174 if (partial == NULL)
9175 buf->b_prompt_callback = vim_strsave(callback);
9176 else
9177 /* pointer into the partial */
9178 buf->b_prompt_callback = callback;
9179 buf->b_prompt_partial = partial;
9180}
9181
9182/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009183 * "prompt_setinterrupt({buffer}, {callback})" function
9184 */
9185 static void
9186f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9187{
9188 buf_T *buf;
9189 char_u *callback;
9190 partial_T *partial;
9191
9192 if (check_secure())
9193 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009194 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009195 if (buf == NULL)
9196 return;
9197
9198 callback = get_callback(&argvars[1], &partial);
9199 if (callback == NULL)
9200 return;
9201
9202 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9203 if (partial == NULL)
9204 buf->b_prompt_interrupt = vim_strsave(callback);
9205 else
9206 /* pointer into the partial */
9207 buf->b_prompt_interrupt = callback;
9208 buf->b_prompt_int_partial = partial;
9209}
9210
9211/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009212 * "prompt_setprompt({buffer}, {text})" function
9213 */
9214 static void
9215f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9216{
9217 buf_T *buf;
9218 char_u *text;
9219
9220 if (check_secure())
9221 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009222 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009223 if (buf == NULL)
9224 return;
9225
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009226 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009227 vim_free(buf->b_prompt_text);
9228 buf->b_prompt_text = vim_strsave(text);
9229}
9230#endif
9231
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009232/*
9233 * "pumvisible()" function
9234 */
9235 static void
9236f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9237{
9238#ifdef FEAT_INS_EXPAND
9239 if (pum_visible())
9240 rettv->vval.v_number = 1;
9241#endif
9242}
9243
9244#ifdef FEAT_PYTHON3
9245/*
9246 * "py3eval()" function
9247 */
9248 static void
9249f_py3eval(typval_T *argvars, typval_T *rettv)
9250{
9251 char_u *str;
9252 char_u buf[NUMBUFLEN];
9253
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009254 if (check_restricted() || check_secure())
9255 return;
9256
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009257 if (p_pyx == 0)
9258 p_pyx = 3;
9259
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009260 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009261 do_py3eval(str, rettv);
9262}
9263#endif
9264
9265#ifdef FEAT_PYTHON
9266/*
9267 * "pyeval()" function
9268 */
9269 static void
9270f_pyeval(typval_T *argvars, typval_T *rettv)
9271{
9272 char_u *str;
9273 char_u buf[NUMBUFLEN];
9274
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009275 if (check_restricted() || check_secure())
9276 return;
9277
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009278 if (p_pyx == 0)
9279 p_pyx = 2;
9280
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009281 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009282 do_pyeval(str, rettv);
9283}
9284#endif
9285
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009286#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9287/*
9288 * "pyxeval()" function
9289 */
9290 static void
9291f_pyxeval(typval_T *argvars, typval_T *rettv)
9292{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009293 if (check_restricted() || check_secure())
9294 return;
9295
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009296# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9297 init_pyxversion();
9298 if (p_pyx == 2)
9299 f_pyeval(argvars, rettv);
9300 else
9301 f_py3eval(argvars, rettv);
9302# elif defined(FEAT_PYTHON)
9303 f_pyeval(argvars, rettv);
9304# elif defined(FEAT_PYTHON3)
9305 f_py3eval(argvars, rettv);
9306# endif
9307}
9308#endif
9309
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009310/*
9311 * "range()" function
9312 */
9313 static void
9314f_range(typval_T *argvars, typval_T *rettv)
9315{
9316 varnumber_T start;
9317 varnumber_T end;
9318 varnumber_T stride = 1;
9319 varnumber_T i;
9320 int error = FALSE;
9321
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009322 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009323 if (argvars[1].v_type == VAR_UNKNOWN)
9324 {
9325 end = start - 1;
9326 start = 0;
9327 }
9328 else
9329 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009330 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009331 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009332 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009333 }
9334
9335 if (error)
9336 return; /* type error; errmsg already given */
9337 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009338 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009339 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009340 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341 else
9342 {
9343 if (rettv_list_alloc(rettv) == OK)
9344 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9345 if (list_append_number(rettv->vval.v_list,
9346 (varnumber_T)i) == FAIL)
9347 break;
9348 }
9349}
9350
9351/*
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009352 * Evaluate "expr" for readdir().
9353 */
9354 static int
9355readdir_checkitem(typval_T *expr, char_u *name)
9356{
9357 typval_T save_val;
9358 typval_T rettv;
9359 typval_T argv[2];
9360 int retval = 0;
9361 int error = FALSE;
9362
9363 prepare_vimvar(VV_VAL, &save_val);
9364 set_vim_var_string(VV_VAL, name, -1);
9365 argv[0].v_type = VAR_STRING;
9366 argv[0].vval.v_string = name;
9367
9368 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9369 goto theend;
9370
9371 retval = tv_get_number_chk(&rettv, &error);
9372 if (error)
9373 retval = -1;
9374 clear_tv(&rettv);
9375
9376theend:
9377 set_vim_var_string(VV_VAL, NULL, 0);
9378 restore_vimvar(VV_VAL, &save_val);
9379 return retval;
9380}
9381
9382/*
9383 * "readdir()" function
9384 */
9385 static void
9386f_readdir(typval_T *argvars, typval_T *rettv)
9387{
9388 typval_T *expr;
9389 int failed = FALSE;
9390 char_u *path;
9391 garray_T ga;
9392 int i;
9393#ifdef MSWIN
9394 char_u *buf, *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009395 int ok;
9396 HANDLE hFind = INVALID_HANDLE_VALUE;
9397 WIN32_FIND_DATAW wfb;
9398 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
9399#endif
9400
9401 if (rettv_list_alloc(rettv) == FAIL)
9402 return;
9403 path = tv_get_string(&argvars[0]);
9404 expr = &argvars[1];
9405 ga_init2(&ga, (int)sizeof(char *), 20);
9406
9407#ifdef MSWIN
9408 buf = alloc((int)MAXPATHL);
9409 if (buf == NULL)
9410 return;
9411 STRNCPY(buf, path, MAXPATHL-5);
9412 p = vim_strpbrk(path, (char_u *)"\\/");
9413 if (p != NULL)
9414 *p = NUL;
9415 STRCAT(buf, "\\*");
9416
9417 wn = enc_to_utf16(buf, NULL);
9418 if (wn != NULL)
9419 hFind = FindFirstFileW(wn, &wfb);
9420 ok = (hFind != INVALID_HANDLE_VALUE);
9421 if (!ok)
9422 smsg(_(e_notopen), path);
9423 else
9424 {
9425 while (ok)
9426 {
9427 int ignore;
9428
9429 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
9430 if (p == NULL)
9431 break; // out of memory
9432
9433 ignore = p[0] == '.' && (p[1] == NUL
9434 || (p[1] == '.' && p[2] == NUL));
9435 if (!ignore && expr->v_type != VAR_UNKNOWN)
9436 {
9437 int r = readdir_checkitem(expr, p);
9438
9439 if (r < 0)
9440 {
9441 vim_free(p);
9442 break;
9443 }
9444 if (r == 0)
9445 ignore = TRUE;
9446 }
9447
9448 if (!ignore)
9449 {
9450 if (ga_grow(&ga, 1) == OK)
9451 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9452 else
9453 {
9454 failed = TRUE;
9455 vim_free(p);
9456 break;
9457 }
9458 }
9459
9460 vim_free(p);
9461 ok = FindNextFileW(hFind, &wfb);
9462 }
9463 FindClose(hFind);
9464 }
9465
9466 vim_free(buf);
9467 vim_free(wn);
9468#else
9469 DIR *dirp;
9470 struct dirent *dp;
9471 char_u *p;
9472
9473 dirp = opendir((char *)path);
9474 if (dirp == NULL)
9475 smsg(_(e_notopen), path);
9476 else
9477 {
9478 for (;;)
9479 {
9480 int ignore;
9481
9482 dp = readdir(dirp);
9483 if (dp == NULL)
9484 break;
9485 p = (char_u *)dp->d_name;
9486
9487 ignore = p[0] == '.' &&
9488 (p[1] == NUL ||
9489 (p[1] == '.' && p[2] == NUL));
9490 if (!ignore && expr->v_type != VAR_UNKNOWN)
9491 {
9492 int r = readdir_checkitem(expr, p);
9493
9494 if (r < 0)
9495 break;
9496 if (r == 0)
9497 ignore = TRUE;
9498 }
9499
9500 if (!ignore)
9501 {
9502 if (ga_grow(&ga, 1) == OK)
9503 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9504 else
9505 {
9506 failed = TRUE;
9507 break;
9508 }
9509 }
9510 }
9511
9512 closedir(dirp);
9513 }
9514#endif
9515
Bram Moolenaar334ad412019-04-19 15:20:46 +02009516 if (!failed && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009517 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009518 sort_strings((char_u **)ga.ga_data, ga.ga_len);
9519 for (i = 0; i < ga.ga_len; i++)
9520 {
9521 p = ((char_u **)ga.ga_data)[i];
9522 list_append_string(rettv->vval.v_list, p, -1);
9523 }
9524 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009525 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009526}
9527
9528/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009529 * "readfile()" function
9530 */
9531 static void
9532f_readfile(typval_T *argvars, typval_T *rettv)
9533{
9534 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009535 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009536 int failed = FALSE;
9537 char_u *fname;
9538 FILE *fd;
9539 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9540 int io_size = sizeof(buf);
9541 int readlen; /* size of last fread() */
9542 char_u *prev = NULL; /* previously read bytes, if any */
9543 long prevlen = 0; /* length of data in prev */
9544 long prevsize = 0; /* size of prev buffer */
9545 long maxline = MAXLNUM;
9546 long cnt = 0;
9547 char_u *p; /* position in buf */
9548 char_u *start; /* start of current line */
9549
9550 if (argvars[1].v_type != VAR_UNKNOWN)
9551 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009552 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009553 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009554 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9555 blob = TRUE;
9556
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009557 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009558 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009559 }
9560
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009561 if (blob)
9562 {
9563 if (rettv_blob_alloc(rettv) == FAIL)
9564 return;
9565 }
9566 else
9567 {
9568 if (rettv_list_alloc(rettv) == FAIL)
9569 return;
9570 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009571
9572 /* Always open the file in binary mode, library functions have a mind of
9573 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009574 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009575 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009577 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009578 return;
9579 }
9580
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009581 if (blob)
9582 {
9583 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9584 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009585 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009586 blob_free(rettv->vval.v_blob);
9587 }
9588 fclose(fd);
9589 return;
9590 }
9591
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009592 while (cnt < maxline || maxline < 0)
9593 {
9594 readlen = (int)fread(buf, 1, io_size, fd);
9595
9596 /* This for loop processes what was read, but is also entered at end
9597 * of file so that either:
9598 * - an incomplete line gets written
9599 * - a "binary" file gets an empty line at the end if it ends in a
9600 * newline. */
9601 for (p = buf, start = buf;
9602 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9603 ++p)
9604 {
9605 if (*p == '\n' || readlen <= 0)
9606 {
9607 listitem_T *li;
9608 char_u *s = NULL;
9609 long_u len = p - start;
9610
9611 /* Finished a line. Remove CRs before NL. */
9612 if (readlen > 0 && !binary)
9613 {
9614 while (len > 0 && start[len - 1] == '\r')
9615 --len;
9616 /* removal may cross back to the "prev" string */
9617 if (len == 0)
9618 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9619 --prevlen;
9620 }
9621 if (prevlen == 0)
9622 s = vim_strnsave(start, (int)len);
9623 else
9624 {
9625 /* Change "prev" buffer to be the right size. This way
9626 * the bytes are only copied once, and very long lines are
9627 * allocated only once. */
9628 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9629 {
9630 mch_memmove(s + prevlen, start, len);
9631 s[prevlen + len] = NUL;
9632 prev = NULL; /* the list will own the string */
9633 prevlen = prevsize = 0;
9634 }
9635 }
9636 if (s == NULL)
9637 {
9638 do_outofmem_msg((long_u) prevlen + len + 1);
9639 failed = TRUE;
9640 break;
9641 }
9642
9643 if ((li = listitem_alloc()) == NULL)
9644 {
9645 vim_free(s);
9646 failed = TRUE;
9647 break;
9648 }
9649 li->li_tv.v_type = VAR_STRING;
9650 li->li_tv.v_lock = 0;
9651 li->li_tv.vval.v_string = s;
9652 list_append(rettv->vval.v_list, li);
9653
9654 start = p + 1; /* step over newline */
9655 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9656 break;
9657 }
9658 else if (*p == NUL)
9659 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009660 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9661 * when finding the BF and check the previous two bytes. */
9662 else if (*p == 0xbf && enc_utf8 && !binary)
9663 {
9664 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9665 * + 1, these may be in the "prev" string. */
9666 char_u back1 = p >= buf + 1 ? p[-1]
9667 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9668 char_u back2 = p >= buf + 2 ? p[-2]
9669 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9670 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9671
9672 if (back2 == 0xef && back1 == 0xbb)
9673 {
9674 char_u *dest = p - 2;
9675
9676 /* Usually a BOM is at the beginning of a file, and so at
9677 * the beginning of a line; then we can just step over it.
9678 */
9679 if (start == dest)
9680 start = p + 1;
9681 else
9682 {
9683 /* have to shuffle buf to close gap */
9684 int adjust_prevlen = 0;
9685
9686 if (dest < buf)
9687 {
9688 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9689 dest = buf;
9690 }
9691 if (readlen > p - buf + 1)
9692 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9693 readlen -= 3 - adjust_prevlen;
9694 prevlen -= adjust_prevlen;
9695 p = dest - 1;
9696 }
9697 }
9698 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009699 } /* for */
9700
9701 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9702 break;
9703 if (start < p)
9704 {
9705 /* There's part of a line in buf, store it in "prev". */
9706 if (p - start + prevlen >= prevsize)
9707 {
9708 /* need bigger "prev" buffer */
9709 char_u *newprev;
9710
9711 /* A common use case is ordinary text files and "prev" gets a
9712 * fragment of a line, so the first allocation is made
9713 * small, to avoid repeatedly 'allocing' large and
9714 * 'reallocing' small. */
9715 if (prevsize == 0)
9716 prevsize = (long)(p - start);
9717 else
9718 {
9719 long grow50pc = (prevsize * 3) / 2;
9720 long growmin = (long)((p - start) * 2 + prevlen);
9721 prevsize = grow50pc > growmin ? grow50pc : growmin;
9722 }
9723 newprev = prev == NULL ? alloc(prevsize)
9724 : vim_realloc(prev, prevsize);
9725 if (newprev == NULL)
9726 {
9727 do_outofmem_msg((long_u)prevsize);
9728 failed = TRUE;
9729 break;
9730 }
9731 prev = newprev;
9732 }
9733 /* Add the line part to end of "prev". */
9734 mch_memmove(prev + prevlen, start, p - start);
9735 prevlen += (long)(p - start);
9736 }
9737 } /* while */
9738
9739 /*
9740 * For a negative line count use only the lines at the end of the file,
9741 * free the rest.
9742 */
9743 if (!failed && maxline < 0)
9744 while (cnt > -maxline)
9745 {
9746 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9747 --cnt;
9748 }
9749
9750 if (failed)
9751 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009752 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009753 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009754 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009755 }
9756
9757 vim_free(prev);
9758 fclose(fd);
9759}
9760
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009761 static void
9762return_register(int regname, typval_T *rettv)
9763{
9764 char_u buf[2] = {0, 0};
9765
9766 buf[0] = (char_u)regname;
9767 rettv->v_type = VAR_STRING;
9768 rettv->vval.v_string = vim_strsave(buf);
9769}
9770
9771/*
9772 * "reg_executing()" function
9773 */
9774 static void
9775f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9776{
9777 return_register(reg_executing, rettv);
9778}
9779
9780/*
9781 * "reg_recording()" function
9782 */
9783 static void
9784f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9785{
9786 return_register(reg_recording, rettv);
9787}
9788
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009789#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009790/*
9791 * Convert a List to proftime_T.
9792 * Return FAIL when there is something wrong.
9793 */
9794 static int
9795list2proftime(typval_T *arg, proftime_T *tm)
9796{
9797 long n1, n2;
9798 int error = FALSE;
9799
9800 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9801 || arg->vval.v_list->lv_len != 2)
9802 return FAIL;
9803 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9804 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009805# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009806 tm->HighPart = n1;
9807 tm->LowPart = n2;
9808# else
9809 tm->tv_sec = n1;
9810 tm->tv_usec = n2;
9811# endif
9812 return error ? FAIL : OK;
9813}
9814#endif /* FEAT_RELTIME */
9815
9816/*
9817 * "reltime()" function
9818 */
9819 static void
9820f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9821{
9822#ifdef FEAT_RELTIME
9823 proftime_T res;
9824 proftime_T start;
9825
9826 if (argvars[0].v_type == VAR_UNKNOWN)
9827 {
9828 /* No arguments: get current time. */
9829 profile_start(&res);
9830 }
9831 else if (argvars[1].v_type == VAR_UNKNOWN)
9832 {
9833 if (list2proftime(&argvars[0], &res) == FAIL)
9834 return;
9835 profile_end(&res);
9836 }
9837 else
9838 {
9839 /* Two arguments: compute the difference. */
9840 if (list2proftime(&argvars[0], &start) == FAIL
9841 || list2proftime(&argvars[1], &res) == FAIL)
9842 return;
9843 profile_sub(&res, &start);
9844 }
9845
9846 if (rettv_list_alloc(rettv) == OK)
9847 {
9848 long n1, n2;
9849
Bram Moolenaar4f974752019-02-17 17:44:42 +01009850# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009851 n1 = res.HighPart;
9852 n2 = res.LowPart;
9853# else
9854 n1 = res.tv_sec;
9855 n2 = res.tv_usec;
9856# endif
9857 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9858 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9859 }
9860#endif
9861}
9862
9863#ifdef FEAT_FLOAT
9864/*
9865 * "reltimefloat()" function
9866 */
9867 static void
9868f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9869{
9870# ifdef FEAT_RELTIME
9871 proftime_T tm;
9872# endif
9873
9874 rettv->v_type = VAR_FLOAT;
9875 rettv->vval.v_float = 0;
9876# ifdef FEAT_RELTIME
9877 if (list2proftime(&argvars[0], &tm) == OK)
9878 rettv->vval.v_float = profile_float(&tm);
9879# endif
9880}
9881#endif
9882
9883/*
9884 * "reltimestr()" function
9885 */
9886 static void
9887f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9888{
9889#ifdef FEAT_RELTIME
9890 proftime_T tm;
9891#endif
9892
9893 rettv->v_type = VAR_STRING;
9894 rettv->vval.v_string = NULL;
9895#ifdef FEAT_RELTIME
9896 if (list2proftime(&argvars[0], &tm) == OK)
9897 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9898#endif
9899}
9900
9901#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009902 static void
9903make_connection(void)
9904{
9905 if (X_DISPLAY == NULL
9906# ifdef FEAT_GUI
9907 && !gui.in_use
9908# endif
9909 )
9910 {
9911 x_force_connect = TRUE;
9912 setup_term_clip();
9913 x_force_connect = FALSE;
9914 }
9915}
9916
9917 static int
9918check_connection(void)
9919{
9920 make_connection();
9921 if (X_DISPLAY == NULL)
9922 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009923 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009924 return FAIL;
9925 }
9926 return OK;
9927}
9928#endif
9929
9930#ifdef FEAT_CLIENTSERVER
9931 static void
9932remote_common(typval_T *argvars, typval_T *rettv, int expr)
9933{
9934 char_u *server_name;
9935 char_u *keys;
9936 char_u *r = NULL;
9937 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009938 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009939# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009940 HWND w;
9941# else
9942 Window w;
9943# endif
9944
9945 if (check_restricted() || check_secure())
9946 return;
9947
9948# ifdef FEAT_X11
9949 if (check_connection() == FAIL)
9950 return;
9951# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009952 if (argvars[2].v_type != VAR_UNKNOWN
9953 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009954 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009955
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009956 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009957 if (server_name == NULL)
9958 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009959 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009960# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009961 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009962# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009963 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9964 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009965# endif
9966 {
9967 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009968 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009969 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009970 vim_free(r);
9971 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009972 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009973 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009974 return;
9975 }
9976
9977 rettv->vval.v_string = r;
9978
9979 if (argvars[2].v_type != VAR_UNKNOWN)
9980 {
9981 dictitem_T v;
9982 char_u str[30];
9983 char_u *idvar;
9984
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009985 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009986 if (idvar != NULL && *idvar != NUL)
9987 {
9988 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9989 v.di_tv.v_type = VAR_STRING;
9990 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009991 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009992 vim_free(v.di_tv.vval.v_string);
9993 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009994 }
9995}
9996#endif
9997
9998/*
9999 * "remote_expr()" function
10000 */
10001 static void
10002f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
10003{
10004 rettv->v_type = VAR_STRING;
10005 rettv->vval.v_string = NULL;
10006#ifdef FEAT_CLIENTSERVER
10007 remote_common(argvars, rettv, TRUE);
10008#endif
10009}
10010
10011/*
10012 * "remote_foreground()" function
10013 */
10014 static void
10015f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10016{
10017#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010018# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010019 /* On Win32 it's done in this application. */
10020 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010021 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010022
10023 if (server_name != NULL)
10024 serverForeground(server_name);
10025 }
10026# else
10027 /* Send a foreground() expression to the server. */
10028 argvars[1].v_type = VAR_STRING;
10029 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
10030 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +020010031 rettv->v_type = VAR_STRING;
10032 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010033 remote_common(argvars, rettv, TRUE);
10034 vim_free(argvars[1].vval.v_string);
10035# endif
10036#endif
10037}
10038
10039 static void
10040f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
10041{
10042#ifdef FEAT_CLIENTSERVER
10043 dictitem_T v;
10044 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010045# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010046 long_u n = 0;
10047# endif
10048 char_u *serverid;
10049
10050 if (check_restricted() || check_secure())
10051 {
10052 rettv->vval.v_number = -1;
10053 return;
10054 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010055 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010056 if (serverid == NULL)
10057 {
10058 rettv->vval.v_number = -1;
10059 return; /* type error; errmsg already given */
10060 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010061# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010062 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
10063 if (n == 0)
10064 rettv->vval.v_number = -1;
10065 else
10066 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010067 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010068 rettv->vval.v_number = (s != NULL);
10069 }
10070# else
10071 if (check_connection() == FAIL)
10072 return;
10073
10074 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10075 serverStrToWin(serverid), &s);
10076# endif
10077
10078 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10079 {
10080 char_u *retvar;
10081
10082 v.di_tv.v_type = VAR_STRING;
10083 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010084 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010085 if (retvar != NULL)
10086 set_var(retvar, &v.di_tv, FALSE);
10087 vim_free(v.di_tv.vval.v_string);
10088 }
10089#else
10090 rettv->vval.v_number = -1;
10091#endif
10092}
10093
10094 static void
10095f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
10096{
10097 char_u *r = NULL;
10098
10099#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010100 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010101
10102 if (serverid != NULL && !check_restricted() && !check_secure())
10103 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010104 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010105# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010106 /* The server's HWND is encoded in the 'id' parameter */
10107 long_u n = 0;
10108# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010109
10110 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010111 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010112
Bram Moolenaar4f974752019-02-17 17:44:42 +010010113# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010114 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10115 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010116 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010117 if (r == NULL)
10118# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010119 if (check_connection() == FAIL
10120 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10121 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010122# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010123 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010124 }
10125#endif
10126 rettv->v_type = VAR_STRING;
10127 rettv->vval.v_string = r;
10128}
10129
10130/*
10131 * "remote_send()" function
10132 */
10133 static void
10134f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10135{
10136 rettv->v_type = VAR_STRING;
10137 rettv->vval.v_string = NULL;
10138#ifdef FEAT_CLIENTSERVER
10139 remote_common(argvars, rettv, FALSE);
10140#endif
10141}
10142
10143/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010144 * "remote_startserver()" function
10145 */
10146 static void
10147f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10148{
10149#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010150 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010151
10152 if (server == NULL)
10153 return; /* type error; errmsg already given */
10154 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010155 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010156 else
10157 {
10158# ifdef FEAT_X11
10159 if (check_connection() == OK)
10160 serverRegisterName(X_DISPLAY, server);
10161# else
10162 serverSetName(server);
10163# endif
10164 }
10165#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010166 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010167#endif
10168}
10169
10170/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010171 * "remove()" function
10172 */
10173 static void
10174f_remove(typval_T *argvars, typval_T *rettv)
10175{
10176 list_T *l;
10177 listitem_T *item, *item2;
10178 listitem_T *li;
10179 long idx;
10180 long end;
10181 char_u *key;
10182 dict_T *d;
10183 dictitem_T *di;
10184 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010185 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010186
10187 if (argvars[0].v_type == VAR_DICT)
10188 {
10189 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010190 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010191 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010192 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010193 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010194 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010195 if (key != NULL)
10196 {
10197 di = dict_find(d, key, -1);
10198 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010199 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010200 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10201 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10202 {
10203 *rettv = di->di_tv;
10204 init_tv(&di->di_tv);
10205 dictitem_remove(d, di);
10206 }
10207 }
10208 }
10209 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010210 else if (argvars[0].v_type == VAR_BLOB)
10211 {
10212 idx = (long)tv_get_number_chk(&argvars[1], &error);
10213 if (!error)
10214 {
10215 blob_T *b = argvars[0].vval.v_blob;
10216 int len = blob_len(b);
10217 char_u *p;
10218
10219 if (idx < 0)
10220 // count from the end
10221 idx = len + idx;
10222 if (idx < 0 || idx >= len)
10223 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010224 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010225 return;
10226 }
10227 if (argvars[2].v_type == VAR_UNKNOWN)
10228 {
10229 // Remove one item, return its value.
10230 p = (char_u *)b->bv_ga.ga_data;
10231 rettv->vval.v_number = (varnumber_T) *(p + idx);
10232 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10233 --b->bv_ga.ga_len;
10234 }
10235 else
10236 {
10237 blob_T *blob;
10238
10239 // Remove range of items, return list with values.
10240 end = (long)tv_get_number_chk(&argvars[2], &error);
10241 if (error)
10242 return;
10243 if (end < 0)
10244 // count from the end
10245 end = len + end;
10246 if (end >= len || idx > end)
10247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010248 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010249 return;
10250 }
10251 blob = blob_alloc();
10252 if (blob == NULL)
10253 return;
10254 blob->bv_ga.ga_len = end - idx + 1;
10255 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10256 {
10257 vim_free(blob);
10258 return;
10259 }
10260 p = (char_u *)b->bv_ga.ga_data;
10261 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10262 (size_t)(end - idx + 1));
10263 ++blob->bv_refcount;
10264 rettv->v_type = VAR_BLOB;
10265 rettv->vval.v_blob = blob;
10266
10267 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10268 b->bv_ga.ga_len -= end - idx + 1;
10269 }
10270 }
10271 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010272 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010273 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010274 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010275 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010276 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010277 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010278 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010279 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010280 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010281 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010282 else
10283 {
10284 if (argvars[2].v_type == VAR_UNKNOWN)
10285 {
10286 /* Remove one item, return its value. */
10287 vimlist_remove(l, item, item);
10288 *rettv = item->li_tv;
10289 vim_free(item);
10290 }
10291 else
10292 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010293 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010294 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010295 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010296 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010297 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010298 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010299 else
10300 {
10301 int cnt = 0;
10302
10303 for (li = item; li != NULL; li = li->li_next)
10304 {
10305 ++cnt;
10306 if (li == item2)
10307 break;
10308 }
10309 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010310 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010311 else
10312 {
10313 vimlist_remove(l, item, item2);
10314 if (rettv_list_alloc(rettv) == OK)
10315 {
10316 l = rettv->vval.v_list;
10317 l->lv_first = item;
10318 l->lv_last = item2;
10319 item->li_prev = NULL;
10320 item2->li_next = NULL;
10321 l->lv_len = cnt;
10322 }
10323 }
10324 }
10325 }
10326 }
10327 }
10328}
10329
10330/*
10331 * "rename({from}, {to})" function
10332 */
10333 static void
10334f_rename(typval_T *argvars, typval_T *rettv)
10335{
10336 char_u buf[NUMBUFLEN];
10337
10338 if (check_restricted() || check_secure())
10339 rettv->vval.v_number = -1;
10340 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010341 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10342 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010343}
10344
10345/*
10346 * "repeat()" function
10347 */
10348 static void
10349f_repeat(typval_T *argvars, typval_T *rettv)
10350{
10351 char_u *p;
10352 int n;
10353 int slen;
10354 int len;
10355 char_u *r;
10356 int i;
10357
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010358 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010359 if (argvars[0].v_type == VAR_LIST)
10360 {
10361 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10362 while (n-- > 0)
10363 if (list_extend(rettv->vval.v_list,
10364 argvars[0].vval.v_list, NULL) == FAIL)
10365 break;
10366 }
10367 else
10368 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010369 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010370 rettv->v_type = VAR_STRING;
10371 rettv->vval.v_string = NULL;
10372
10373 slen = (int)STRLEN(p);
10374 len = slen * n;
10375 if (len <= 0)
10376 return;
10377
10378 r = alloc(len + 1);
10379 if (r != NULL)
10380 {
10381 for (i = 0; i < n; i++)
10382 mch_memmove(r + i * slen, p, (size_t)slen);
10383 r[len] = NUL;
10384 }
10385
10386 rettv->vval.v_string = r;
10387 }
10388}
10389
10390/*
10391 * "resolve()" function
10392 */
10393 static void
10394f_resolve(typval_T *argvars, typval_T *rettv)
10395{
10396 char_u *p;
10397#ifdef HAVE_READLINK
10398 char_u *buf = NULL;
10399#endif
10400
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010401 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010402#ifdef FEAT_SHORTCUT
10403 {
10404 char_u *v = NULL;
10405
Bram Moolenaardce1e892019-02-10 23:18:53 +010010406 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010407 if (v != NULL)
10408 rettv->vval.v_string = v;
10409 else
10410 rettv->vval.v_string = vim_strsave(p);
10411 }
10412#else
10413# ifdef HAVE_READLINK
10414 {
10415 char_u *cpy;
10416 int len;
10417 char_u *remain = NULL;
10418 char_u *q;
10419 int is_relative_to_current = FALSE;
10420 int has_trailing_pathsep = FALSE;
10421 int limit = 100;
10422
10423 p = vim_strsave(p);
10424
10425 if (p[0] == '.' && (vim_ispathsep(p[1])
10426 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10427 is_relative_to_current = TRUE;
10428
10429 len = STRLEN(p);
10430 if (len > 0 && after_pathsep(p, p + len))
10431 {
10432 has_trailing_pathsep = TRUE;
10433 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10434 }
10435
10436 q = getnextcomp(p);
10437 if (*q != NUL)
10438 {
10439 /* Separate the first path component in "p", and keep the
10440 * remainder (beginning with the path separator). */
10441 remain = vim_strsave(q - 1);
10442 q[-1] = NUL;
10443 }
10444
10445 buf = alloc(MAXPATHL + 1);
10446 if (buf == NULL)
10447 goto fail;
10448
10449 for (;;)
10450 {
10451 for (;;)
10452 {
10453 len = readlink((char *)p, (char *)buf, MAXPATHL);
10454 if (len <= 0)
10455 break;
10456 buf[len] = NUL;
10457
10458 if (limit-- == 0)
10459 {
10460 vim_free(p);
10461 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010462 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010463 rettv->vval.v_string = NULL;
10464 goto fail;
10465 }
10466
10467 /* Ensure that the result will have a trailing path separator
10468 * if the argument has one. */
10469 if (remain == NULL && has_trailing_pathsep)
10470 add_pathsep(buf);
10471
10472 /* Separate the first path component in the link value and
10473 * concatenate the remainders. */
10474 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10475 if (*q != NUL)
10476 {
10477 if (remain == NULL)
10478 remain = vim_strsave(q - 1);
10479 else
10480 {
10481 cpy = concat_str(q - 1, remain);
10482 if (cpy != NULL)
10483 {
10484 vim_free(remain);
10485 remain = cpy;
10486 }
10487 }
10488 q[-1] = NUL;
10489 }
10490
10491 q = gettail(p);
10492 if (q > p && *q == NUL)
10493 {
10494 /* Ignore trailing path separator. */
10495 q[-1] = NUL;
10496 q = gettail(p);
10497 }
10498 if (q > p && !mch_isFullName(buf))
10499 {
10500 /* symlink is relative to directory of argument */
10501 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10502 if (cpy != NULL)
10503 {
10504 STRCPY(cpy, p);
10505 STRCPY(gettail(cpy), buf);
10506 vim_free(p);
10507 p = cpy;
10508 }
10509 }
10510 else
10511 {
10512 vim_free(p);
10513 p = vim_strsave(buf);
10514 }
10515 }
10516
10517 if (remain == NULL)
10518 break;
10519
10520 /* Append the first path component of "remain" to "p". */
10521 q = getnextcomp(remain + 1);
10522 len = q - remain - (*q != NUL);
10523 cpy = vim_strnsave(p, STRLEN(p) + len);
10524 if (cpy != NULL)
10525 {
10526 STRNCAT(cpy, remain, len);
10527 vim_free(p);
10528 p = cpy;
10529 }
10530 /* Shorten "remain". */
10531 if (*q != NUL)
10532 STRMOVE(remain, q - 1);
10533 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010534 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010535 }
10536
10537 /* If the result is a relative path name, make it explicitly relative to
10538 * the current directory if and only if the argument had this form. */
10539 if (!vim_ispathsep(*p))
10540 {
10541 if (is_relative_to_current
10542 && *p != NUL
10543 && !(p[0] == '.'
10544 && (p[1] == NUL
10545 || vim_ispathsep(p[1])
10546 || (p[1] == '.'
10547 && (p[2] == NUL
10548 || vim_ispathsep(p[2]))))))
10549 {
10550 /* Prepend "./". */
10551 cpy = concat_str((char_u *)"./", p);
10552 if (cpy != NULL)
10553 {
10554 vim_free(p);
10555 p = cpy;
10556 }
10557 }
10558 else if (!is_relative_to_current)
10559 {
10560 /* Strip leading "./". */
10561 q = p;
10562 while (q[0] == '.' && vim_ispathsep(q[1]))
10563 q += 2;
10564 if (q > p)
10565 STRMOVE(p, p + 2);
10566 }
10567 }
10568
10569 /* Ensure that the result will have no trailing path separator
10570 * if the argument had none. But keep "/" or "//". */
10571 if (!has_trailing_pathsep)
10572 {
10573 q = p + STRLEN(p);
10574 if (after_pathsep(p, q))
10575 *gettail_sep(p) = NUL;
10576 }
10577
10578 rettv->vval.v_string = p;
10579 }
10580# else
10581 rettv->vval.v_string = vim_strsave(p);
10582# endif
10583#endif
10584
10585 simplify_filename(rettv->vval.v_string);
10586
10587#ifdef HAVE_READLINK
10588fail:
10589 vim_free(buf);
10590#endif
10591 rettv->v_type = VAR_STRING;
10592}
10593
10594/*
10595 * "reverse({list})" function
10596 */
10597 static void
10598f_reverse(typval_T *argvars, typval_T *rettv)
10599{
10600 list_T *l;
10601 listitem_T *li, *ni;
10602
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010603 if (argvars[0].v_type == VAR_BLOB)
10604 {
10605 blob_T *b = argvars[0].vval.v_blob;
10606 int i, len = blob_len(b);
10607
10608 for (i = 0; i < len / 2; i++)
10609 {
10610 int tmp = blob_get(b, i);
10611
10612 blob_set(b, i, blob_get(b, len - i - 1));
10613 blob_set(b, len - i - 1, tmp);
10614 }
10615 rettv_blob_set(rettv, b);
10616 return;
10617 }
10618
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010620 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010621 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010622 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010623 (char_u *)N_("reverse() argument"), TRUE))
10624 {
10625 li = l->lv_last;
10626 l->lv_first = l->lv_last = NULL;
10627 l->lv_len = 0;
10628 while (li != NULL)
10629 {
10630 ni = li->li_prev;
10631 list_append(l, li);
10632 li = ni;
10633 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010634 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010635 l->lv_idx = l->lv_len - l->lv_idx - 1;
10636 }
10637}
10638
10639#define SP_NOMOVE 0x01 /* don't move cursor */
10640#define SP_REPEAT 0x02 /* repeat to find outer pair */
10641#define SP_RETCOUNT 0x04 /* return matchcount */
10642#define SP_SETPCMARK 0x08 /* set previous context mark */
10643#define SP_START 0x10 /* accept match at start position */
10644#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10645#define SP_END 0x40 /* leave cursor at end of match */
10646#define SP_COLUMN 0x80 /* start at cursor column */
10647
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010648/*
10649 * Get flags for a search function.
10650 * Possibly sets "p_ws".
10651 * Returns BACKWARD, FORWARD or zero (for an error).
10652 */
10653 static int
10654get_search_arg(typval_T *varp, int *flagsp)
10655{
10656 int dir = FORWARD;
10657 char_u *flags;
10658 char_u nbuf[NUMBUFLEN];
10659 int mask;
10660
10661 if (varp->v_type != VAR_UNKNOWN)
10662 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010663 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010664 if (flags == NULL)
10665 return 0; /* type error; errmsg already given */
10666 while (*flags != NUL)
10667 {
10668 switch (*flags)
10669 {
10670 case 'b': dir = BACKWARD; break;
10671 case 'w': p_ws = TRUE; break;
10672 case 'W': p_ws = FALSE; break;
10673 default: mask = 0;
10674 if (flagsp != NULL)
10675 switch (*flags)
10676 {
10677 case 'c': mask = SP_START; break;
10678 case 'e': mask = SP_END; break;
10679 case 'm': mask = SP_RETCOUNT; break;
10680 case 'n': mask = SP_NOMOVE; break;
10681 case 'p': mask = SP_SUBPAT; break;
10682 case 'r': mask = SP_REPEAT; break;
10683 case 's': mask = SP_SETPCMARK; break;
10684 case 'z': mask = SP_COLUMN; break;
10685 }
10686 if (mask == 0)
10687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010688 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010689 dir = 0;
10690 }
10691 else
10692 *flagsp |= mask;
10693 }
10694 if (dir == 0)
10695 break;
10696 ++flags;
10697 }
10698 }
10699 return dir;
10700}
10701
10702/*
10703 * Shared by search() and searchpos() functions.
10704 */
10705 static int
10706search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10707{
10708 int flags;
10709 char_u *pat;
10710 pos_T pos;
10711 pos_T save_cursor;
10712 int save_p_ws = p_ws;
10713 int dir;
10714 int retval = 0; /* default: FAIL */
10715 long lnum_stop = 0;
10716 proftime_T tm;
10717#ifdef FEAT_RELTIME
10718 long time_limit = 0;
10719#endif
10720 int options = SEARCH_KEEP;
10721 int subpatnum;
10722
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010723 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010724 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10725 if (dir == 0)
10726 goto theend;
10727 flags = *flagsp;
10728 if (flags & SP_START)
10729 options |= SEARCH_START;
10730 if (flags & SP_END)
10731 options |= SEARCH_END;
10732 if (flags & SP_COLUMN)
10733 options |= SEARCH_COL;
10734
10735 /* Optional arguments: line number to stop searching and timeout. */
10736 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10737 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010738 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010739 if (lnum_stop < 0)
10740 goto theend;
10741#ifdef FEAT_RELTIME
10742 if (argvars[3].v_type != VAR_UNKNOWN)
10743 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010744 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010745 if (time_limit < 0)
10746 goto theend;
10747 }
10748#endif
10749 }
10750
10751#ifdef FEAT_RELTIME
10752 /* Set the time limit, if there is one. */
10753 profile_setlimit(time_limit, &tm);
10754#endif
10755
10756 /*
10757 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10758 * Check to make sure only those flags are set.
10759 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10760 * flags cannot be set. Check for that condition also.
10761 */
10762 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10763 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10764 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010765 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010766 goto theend;
10767 }
10768
10769 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010770 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010771 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010772 if (subpatnum != FAIL)
10773 {
10774 if (flags & SP_SUBPAT)
10775 retval = subpatnum;
10776 else
10777 retval = pos.lnum;
10778 if (flags & SP_SETPCMARK)
10779 setpcmark();
10780 curwin->w_cursor = pos;
10781 if (match_pos != NULL)
10782 {
10783 /* Store the match cursor position */
10784 match_pos->lnum = pos.lnum;
10785 match_pos->col = pos.col + 1;
10786 }
10787 /* "/$" will put the cursor after the end of the line, may need to
10788 * correct that here */
10789 check_cursor();
10790 }
10791
10792 /* If 'n' flag is used: restore cursor position. */
10793 if (flags & SP_NOMOVE)
10794 curwin->w_cursor = save_cursor;
10795 else
10796 curwin->w_set_curswant = TRUE;
10797theend:
10798 p_ws = save_p_ws;
10799
10800 return retval;
10801}
10802
10803#ifdef FEAT_FLOAT
10804
10805/*
10806 * round() is not in C90, use ceil() or floor() instead.
10807 */
10808 float_T
10809vim_round(float_T f)
10810{
10811 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10812}
10813
10814/*
10815 * "round({float})" function
10816 */
10817 static void
10818f_round(typval_T *argvars, typval_T *rettv)
10819{
10820 float_T f = 0.0;
10821
10822 rettv->v_type = VAR_FLOAT;
10823 if (get_float_arg(argvars, &f) == OK)
10824 rettv->vval.v_float = vim_round(f);
10825 else
10826 rettv->vval.v_float = 0.0;
10827}
10828#endif
10829
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010830#ifdef FEAT_RUBY
10831/*
10832 * "rubyeval()" function
10833 */
10834 static void
10835f_rubyeval(typval_T *argvars, typval_T *rettv)
10836{
10837 char_u *str;
10838 char_u buf[NUMBUFLEN];
10839
10840 str = tv_get_string_buf(&argvars[0], buf);
10841 do_rubyeval(str, rettv);
10842}
10843#endif
10844
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010845/*
10846 * "screenattr()" function
10847 */
10848 static void
10849f_screenattr(typval_T *argvars, typval_T *rettv)
10850{
10851 int row;
10852 int col;
10853 int c;
10854
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010855 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10856 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010857 if (row < 0 || row >= screen_Rows
10858 || col < 0 || col >= screen_Columns)
10859 c = -1;
10860 else
10861 c = ScreenAttrs[LineOffset[row] + col];
10862 rettv->vval.v_number = c;
10863}
10864
10865/*
10866 * "screenchar()" function
10867 */
10868 static void
10869f_screenchar(typval_T *argvars, typval_T *rettv)
10870{
10871 int row;
10872 int col;
10873 int off;
10874 int c;
10875
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010876 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10877 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010878 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010879 c = -1;
10880 else
10881 {
10882 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010883 if (enc_utf8 && ScreenLinesUC[off] != 0)
10884 c = ScreenLinesUC[off];
10885 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010886 c = ScreenLines[off];
10887 }
10888 rettv->vval.v_number = c;
10889}
10890
10891/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010892 * "screenchars()" function
10893 */
10894 static void
10895f_screenchars(typval_T *argvars, typval_T *rettv)
10896{
10897 int row;
10898 int col;
10899 int off;
10900 int c;
10901 int i;
10902
10903 if (rettv_list_alloc(rettv) == FAIL)
10904 return;
10905 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10906 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10907 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10908 return;
10909
10910 off = LineOffset[row] + col;
10911 if (enc_utf8 && ScreenLinesUC[off] != 0)
10912 c = ScreenLinesUC[off];
10913 else
10914 c = ScreenLines[off];
10915 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10916
10917 if (enc_utf8)
10918
10919 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10920 list_append_number(rettv->vval.v_list,
10921 (varnumber_T)ScreenLinesC[i][off]);
10922}
10923
10924/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010925 * "screencol()" function
10926 *
10927 * First column is 1 to be consistent with virtcol().
10928 */
10929 static void
10930f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10931{
10932 rettv->vval.v_number = screen_screencol() + 1;
10933}
10934
10935/*
10936 * "screenrow()" function
10937 */
10938 static void
10939f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10940{
10941 rettv->vval.v_number = screen_screenrow() + 1;
10942}
10943
10944/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010945 * "screenstring()" function
10946 */
10947 static void
10948f_screenstring(typval_T *argvars, typval_T *rettv)
10949{
10950 int row;
10951 int col;
10952 int off;
10953 int c;
10954 int i;
10955 char_u buf[MB_MAXBYTES + 1];
10956 int buflen = 0;
10957
10958 rettv->vval.v_string = NULL;
10959 rettv->v_type = VAR_STRING;
10960
10961 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10962 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10963 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10964 return;
10965
10966 off = LineOffset[row] + col;
10967 if (enc_utf8 && ScreenLinesUC[off] != 0)
10968 c = ScreenLinesUC[off];
10969 else
10970 c = ScreenLines[off];
10971 buflen += mb_char2bytes(c, buf);
10972
10973 if (enc_utf8)
10974 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10975 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10976
10977 buf[buflen] = NUL;
10978 rettv->vval.v_string = vim_strsave(buf);
10979}
10980
10981/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010982 * "search()" function
10983 */
10984 static void
10985f_search(typval_T *argvars, typval_T *rettv)
10986{
10987 int flags = 0;
10988
10989 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10990}
10991
10992/*
10993 * "searchdecl()" function
10994 */
10995 static void
10996f_searchdecl(typval_T *argvars, typval_T *rettv)
10997{
10998 int locally = 1;
10999 int thisblock = 0;
11000 int error = FALSE;
11001 char_u *name;
11002
11003 rettv->vval.v_number = 1; /* default: FAIL */
11004
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011005 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011006 if (argvars[1].v_type != VAR_UNKNOWN)
11007 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011008 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011009 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011010 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011011 }
11012 if (!error && name != NULL)
11013 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
11014 locally, thisblock, SEARCH_KEEP) == FAIL;
11015}
11016
11017/*
11018 * Used by searchpair() and searchpairpos()
11019 */
11020 static int
11021searchpair_cmn(typval_T *argvars, pos_T *match_pos)
11022{
11023 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010011024 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011025 int save_p_ws = p_ws;
11026 int dir;
11027 int flags = 0;
11028 char_u nbuf1[NUMBUFLEN];
11029 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011030 int retval = 0; /* default: FAIL */
11031 long lnum_stop = 0;
11032 long time_limit = 0;
11033
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011034 /* Get the three pattern arguments: start, middle, end. Will result in an
11035 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011036 spat = tv_get_string_chk(&argvars[0]);
11037 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
11038 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011039 if (spat == NULL || mpat == NULL || epat == NULL)
11040 goto theend; /* type error */
11041
11042 /* Handle the optional fourth argument: flags */
11043 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
11044 if (dir == 0)
11045 goto theend;
11046
11047 /* Don't accept SP_END or SP_SUBPAT.
11048 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
11049 */
11050 if ((flags & (SP_END | SP_SUBPAT)) != 0
11051 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
11052 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011053 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011054 goto theend;
11055 }
11056
11057 /* Using 'r' implies 'W', otherwise it doesn't work. */
11058 if (flags & SP_REPEAT)
11059 p_ws = FALSE;
11060
11061 /* Optional fifth argument: skip expression */
11062 if (argvars[3].v_type == VAR_UNKNOWN
11063 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010011064 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011065 else
11066 {
Bram Moolenaar48570482017-10-30 21:48:41 +010011067 skip = &argvars[4];
11068 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
11069 && skip->v_type != VAR_STRING)
11070 {
11071 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011072 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010011073 goto theend;
11074 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011075 if (argvars[5].v_type != VAR_UNKNOWN)
11076 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011077 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011078 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011079 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011080 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011081 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011082 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011083#ifdef FEAT_RELTIME
11084 if (argvars[6].v_type != VAR_UNKNOWN)
11085 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011086 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011087 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011089 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011090 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011091 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011092 }
11093#endif
11094 }
11095 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011096
11097 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
11098 match_pos, lnum_stop, time_limit);
11099
11100theend:
11101 p_ws = save_p_ws;
11102
11103 return retval;
11104}
11105
11106/*
11107 * "searchpair()" function
11108 */
11109 static void
11110f_searchpair(typval_T *argvars, typval_T *rettv)
11111{
11112 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11113}
11114
11115/*
11116 * "searchpairpos()" function
11117 */
11118 static void
11119f_searchpairpos(typval_T *argvars, typval_T *rettv)
11120{
11121 pos_T match_pos;
11122 int lnum = 0;
11123 int col = 0;
11124
11125 if (rettv_list_alloc(rettv) == FAIL)
11126 return;
11127
11128 if (searchpair_cmn(argvars, &match_pos) > 0)
11129 {
11130 lnum = match_pos.lnum;
11131 col = match_pos.col;
11132 }
11133
11134 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11135 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11136}
11137
11138/*
11139 * Search for a start/middle/end thing.
11140 * Used by searchpair(), see its documentation for the details.
11141 * Returns 0 or -1 for no match,
11142 */
11143 long
11144do_searchpair(
11145 char_u *spat, /* start pattern */
11146 char_u *mpat, /* middle pattern */
11147 char_u *epat, /* end pattern */
11148 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011149 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011150 int flags, /* SP_SETPCMARK and other SP_ values */
11151 pos_T *match_pos,
11152 linenr_T lnum_stop, /* stop at this line if not zero */
11153 long time_limit UNUSED) /* stop after this many msec */
11154{
11155 char_u *save_cpo;
11156 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11157 long retval = 0;
11158 pos_T pos;
11159 pos_T firstpos;
11160 pos_T foundpos;
11161 pos_T save_cursor;
11162 pos_T save_pos;
11163 int n;
11164 int r;
11165 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011166 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011167 int err;
11168 int options = SEARCH_KEEP;
11169 proftime_T tm;
11170
11171 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11172 save_cpo = p_cpo;
11173 p_cpo = empty_option;
11174
11175#ifdef FEAT_RELTIME
11176 /* Set the time limit, if there is one. */
11177 profile_setlimit(time_limit, &tm);
11178#endif
11179
11180 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11181 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011182 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
11183 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011184 if (pat2 == NULL || pat3 == NULL)
11185 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011186 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011187 if (*mpat == NUL)
11188 STRCPY(pat3, pat2);
11189 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011190 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011191 spat, epat, mpat);
11192 if (flags & SP_START)
11193 options |= SEARCH_START;
11194
Bram Moolenaar48570482017-10-30 21:48:41 +010011195 if (skip != NULL)
11196 {
11197 /* Empty string means to not use the skip expression. */
11198 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11199 use_skip = skip->vval.v_string != NULL
11200 && *skip->vval.v_string != NUL;
11201 }
11202
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011203 save_cursor = curwin->w_cursor;
11204 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011205 CLEAR_POS(&firstpos);
11206 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011207 pat = pat3;
11208 for (;;)
11209 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011210 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011211 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011212 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011213 /* didn't find it or found the first match again: FAIL */
11214 break;
11215
11216 if (firstpos.lnum == 0)
11217 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011218 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011219 {
11220 /* Found the same position again. Can happen with a pattern that
11221 * has "\zs" at the end and searching backwards. Advance one
11222 * character and try again. */
11223 if (dir == BACKWARD)
11224 decl(&pos);
11225 else
11226 incl(&pos);
11227 }
11228 foundpos = pos;
11229
11230 /* clear the start flag to avoid getting stuck here */
11231 options &= ~SEARCH_START;
11232
11233 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011234 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011235 {
11236 save_pos = curwin->w_cursor;
11237 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011238 err = FALSE;
11239 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011240 curwin->w_cursor = save_pos;
11241 if (err)
11242 {
11243 /* Evaluating {skip} caused an error, break here. */
11244 curwin->w_cursor = save_cursor;
11245 retval = -1;
11246 break;
11247 }
11248 if (r)
11249 continue;
11250 }
11251
11252 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11253 {
11254 /* Found end when searching backwards or start when searching
11255 * forward: nested pair. */
11256 ++nest;
11257 pat = pat2; /* nested, don't search for middle */
11258 }
11259 else
11260 {
11261 /* Found end when searching forward or start when searching
11262 * backward: end of (nested) pair; or found middle in outer pair. */
11263 if (--nest == 1)
11264 pat = pat3; /* outer level, search for middle */
11265 }
11266
11267 if (nest == 0)
11268 {
11269 /* Found the match: return matchcount or line number. */
11270 if (flags & SP_RETCOUNT)
11271 ++retval;
11272 else
11273 retval = pos.lnum;
11274 if (flags & SP_SETPCMARK)
11275 setpcmark();
11276 curwin->w_cursor = pos;
11277 if (!(flags & SP_REPEAT))
11278 break;
11279 nest = 1; /* search for next unmatched */
11280 }
11281 }
11282
11283 if (match_pos != NULL)
11284 {
11285 /* Store the match cursor position */
11286 match_pos->lnum = curwin->w_cursor.lnum;
11287 match_pos->col = curwin->w_cursor.col + 1;
11288 }
11289
11290 /* If 'n' flag is used or search failed: restore cursor position. */
11291 if ((flags & SP_NOMOVE) || retval == 0)
11292 curwin->w_cursor = save_cursor;
11293
11294theend:
11295 vim_free(pat2);
11296 vim_free(pat3);
11297 if (p_cpo == empty_option)
11298 p_cpo = save_cpo;
11299 else
11300 /* Darn, evaluating the {skip} expression changed the value. */
11301 free_string_option(save_cpo);
11302
11303 return retval;
11304}
11305
11306/*
11307 * "searchpos()" function
11308 */
11309 static void
11310f_searchpos(typval_T *argvars, typval_T *rettv)
11311{
11312 pos_T match_pos;
11313 int lnum = 0;
11314 int col = 0;
11315 int n;
11316 int flags = 0;
11317
11318 if (rettv_list_alloc(rettv) == FAIL)
11319 return;
11320
11321 n = search_cmn(argvars, &match_pos, &flags);
11322 if (n > 0)
11323 {
11324 lnum = match_pos.lnum;
11325 col = match_pos.col;
11326 }
11327
11328 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11329 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11330 if (flags & SP_SUBPAT)
11331 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11332}
11333
11334 static void
11335f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11336{
11337#ifdef FEAT_CLIENTSERVER
11338 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011339 char_u *server = tv_get_string_chk(&argvars[0]);
11340 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011341
11342 rettv->vval.v_number = -1;
11343 if (server == NULL || reply == NULL)
11344 return;
11345 if (check_restricted() || check_secure())
11346 return;
11347# ifdef FEAT_X11
11348 if (check_connection() == FAIL)
11349 return;
11350# endif
11351
11352 if (serverSendReply(server, reply) < 0)
11353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011354 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011355 return;
11356 }
11357 rettv->vval.v_number = 0;
11358#else
11359 rettv->vval.v_number = -1;
11360#endif
11361}
11362
11363 static void
11364f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11365{
11366 char_u *r = NULL;
11367
11368#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011369# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011370 r = serverGetVimNames();
11371# else
11372 make_connection();
11373 if (X_DISPLAY != NULL)
11374 r = serverGetVimNames(X_DISPLAY);
11375# endif
11376#endif
11377 rettv->v_type = VAR_STRING;
11378 rettv->vval.v_string = r;
11379}
11380
11381/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011382 * "setbufline()" function
11383 */
11384 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011385f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011386{
11387 linenr_T lnum;
11388 buf_T *buf;
11389
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011390 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011391 if (buf == NULL)
11392 rettv->vval.v_number = 1; /* FAIL */
11393 else
11394 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011395 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011396 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011397 }
11398}
11399
11400/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011401 * "setbufvar()" function
11402 */
11403 static void
11404f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11405{
11406 buf_T *buf;
11407 char_u *varname, *bufvarname;
11408 typval_T *varp;
11409 char_u nbuf[NUMBUFLEN];
11410
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011411 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011412 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011413 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11414 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011415 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011416 varp = &argvars[2];
11417
11418 if (buf != NULL && varname != NULL && varp != NULL)
11419 {
11420 if (*varname == '&')
11421 {
11422 long numval;
11423 char_u *strval;
11424 int error = FALSE;
11425 aco_save_T aco;
11426
11427 /* set curbuf to be our buf, temporarily */
11428 aucmd_prepbuf(&aco, buf);
11429
11430 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011431 numval = (long)tv_get_number_chk(varp, &error);
11432 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011433 if (!error && strval != NULL)
11434 set_option_value(varname, numval, strval, OPT_LOCAL);
11435
11436 /* reset notion of buffer */
11437 aucmd_restbuf(&aco);
11438 }
11439 else
11440 {
11441 buf_T *save_curbuf = curbuf;
11442
11443 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11444 if (bufvarname != NULL)
11445 {
11446 curbuf = buf;
11447 STRCPY(bufvarname, "b:");
11448 STRCPY(bufvarname + 2, varname);
11449 set_var(bufvarname, varp, TRUE);
11450 vim_free(bufvarname);
11451 curbuf = save_curbuf;
11452 }
11453 }
11454 }
11455}
11456
11457 static void
11458f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11459{
11460 dict_T *d;
11461 dictitem_T *di;
11462 char_u *csearch;
11463
11464 if (argvars[0].v_type != VAR_DICT)
11465 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011466 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011467 return;
11468 }
11469
11470 if ((d = argvars[0].vval.v_dict) != NULL)
11471 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011472 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011473 if (csearch != NULL)
11474 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011475 if (enc_utf8)
11476 {
11477 int pcc[MAX_MCO];
11478 int c = utfc_ptr2char(csearch, pcc);
11479
11480 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11481 }
11482 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011483 set_last_csearch(PTR2CHAR(csearch),
11484 csearch, MB_PTR2LEN(csearch));
11485 }
11486
11487 di = dict_find(d, (char_u *)"forward", -1);
11488 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011489 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011490 ? FORWARD : BACKWARD);
11491
11492 di = dict_find(d, (char_u *)"until", -1);
11493 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011494 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011495 }
11496}
11497
11498/*
11499 * "setcmdpos()" function
11500 */
11501 static void
11502f_setcmdpos(typval_T *argvars, typval_T *rettv)
11503{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011504 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011505
11506 if (pos >= 0)
11507 rettv->vval.v_number = set_cmdline_pos(pos);
11508}
11509
11510/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011511 * "setenv()" function
11512 */
11513 static void
11514f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11515{
11516 char_u namebuf[NUMBUFLEN];
11517 char_u valbuf[NUMBUFLEN];
11518 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11519
11520 if (argvars[1].v_type == VAR_SPECIAL
11521 && argvars[1].vval.v_number == VVAL_NULL)
11522 vim_unsetenv(name);
11523 else
11524 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11525}
11526
11527/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011528 * "setfperm({fname}, {mode})" function
11529 */
11530 static void
11531f_setfperm(typval_T *argvars, typval_T *rettv)
11532{
11533 char_u *fname;
11534 char_u modebuf[NUMBUFLEN];
11535 char_u *mode_str;
11536 int i;
11537 int mask;
11538 int mode = 0;
11539
11540 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011541 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011542 if (fname == NULL)
11543 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011544 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011545 if (mode_str == NULL)
11546 return;
11547 if (STRLEN(mode_str) != 9)
11548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011549 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011550 return;
11551 }
11552
11553 mask = 1;
11554 for (i = 8; i >= 0; --i)
11555 {
11556 if (mode_str[i] != '-')
11557 mode |= mask;
11558 mask = mask << 1;
11559 }
11560 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11561}
11562
11563/*
11564 * "setline()" function
11565 */
11566 static void
11567f_setline(typval_T *argvars, typval_T *rettv)
11568{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011569 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011570
Bram Moolenaarca851592018-06-06 21:04:07 +020011571 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011572}
11573
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011574/*
11575 * Used by "setqflist()" and "setloclist()" functions
11576 */
11577 static void
11578set_qf_ll_list(
11579 win_T *wp UNUSED,
11580 typval_T *list_arg UNUSED,
11581 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011582 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011583 typval_T *rettv)
11584{
11585#ifdef FEAT_QUICKFIX
11586 static char *e_invact = N_("E927: Invalid action: '%s'");
11587 char_u *act;
11588 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011589 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011590#endif
11591
11592 rettv->vval.v_number = -1;
11593
11594#ifdef FEAT_QUICKFIX
11595 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011596 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011597 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011598 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011599 else
11600 {
11601 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011602 dict_T *d = NULL;
11603 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011604
11605 if (action_arg->v_type == VAR_STRING)
11606 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011607 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011608 if (act == NULL)
11609 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011610 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11611 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011612 action = *act;
11613 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011614 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011615 }
11616 else if (action_arg->v_type == VAR_UNKNOWN)
11617 action = ' ';
11618 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011619 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011620
Bram Moolenaard823fa92016-08-12 16:29:27 +020011621 if (action_arg->v_type != VAR_UNKNOWN
11622 && what_arg->v_type != VAR_UNKNOWN)
11623 {
11624 if (what_arg->v_type == VAR_DICT)
11625 d = what_arg->vval.v_dict;
11626 else
11627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011628 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011629 valid_dict = FALSE;
11630 }
11631 }
11632
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011633 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011634 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011635 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11636 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011637 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011638 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011639 }
11640#endif
11641}
11642
11643/*
11644 * "setloclist()" function
11645 */
11646 static void
11647f_setloclist(typval_T *argvars, typval_T *rettv)
11648{
11649 win_T *win;
11650
11651 rettv->vval.v_number = -1;
11652
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011653 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011654 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011655 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011656}
11657
11658/*
11659 * "setmatches()" function
11660 */
11661 static void
11662f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11663{
11664#ifdef FEAT_SEARCH_EXTRA
11665 list_T *l;
11666 listitem_T *li;
11667 dict_T *d;
11668 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011669 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011670
11671 rettv->vval.v_number = -1;
11672 if (argvars[0].v_type != VAR_LIST)
11673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011674 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011675 return;
11676 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011677 if (win == NULL)
11678 return;
11679
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011680 if ((l = argvars[0].vval.v_list) != NULL)
11681 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011682 /* To some extent make sure that we are dealing with a list from
11683 * "getmatches()". */
11684 li = l->lv_first;
11685 while (li != NULL)
11686 {
11687 if (li->li_tv.v_type != VAR_DICT
11688 || (d = li->li_tv.vval.v_dict) == NULL)
11689 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011690 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011691 return;
11692 }
11693 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11694 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11695 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11696 && dict_find(d, (char_u *)"priority", -1) != NULL
11697 && dict_find(d, (char_u *)"id", -1) != NULL))
11698 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011699 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011700 return;
11701 }
11702 li = li->li_next;
11703 }
11704
Bram Moolenaaraff74912019-03-30 18:11:49 +010011705 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011706 li = l->lv_first;
11707 while (li != NULL)
11708 {
11709 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011710 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011711 dictitem_T *di;
11712 char_u *group;
11713 int priority;
11714 int id;
11715 char_u *conceal;
11716
11717 d = li->li_tv.vval.v_dict;
11718 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11719 {
11720 if (s == NULL)
11721 {
11722 s = list_alloc();
11723 if (s == NULL)
11724 return;
11725 }
11726
11727 /* match from matchaddpos() */
11728 for (i = 1; i < 9; i++)
11729 {
11730 sprintf((char *)buf, (char *)"pos%d", i);
11731 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11732 {
11733 if (di->di_tv.v_type != VAR_LIST)
11734 return;
11735
11736 list_append_tv(s, &di->di_tv);
11737 s->lv_refcount++;
11738 }
11739 else
11740 break;
11741 }
11742 }
11743
Bram Moolenaar8f667172018-12-14 15:38:31 +010011744 group = dict_get_string(d, (char_u *)"group", TRUE);
11745 priority = (int)dict_get_number(d, (char_u *)"priority");
11746 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011747 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011748 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011749 : NULL;
11750 if (i == 0)
11751 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011752 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011753 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011754 priority, id, NULL, conceal);
11755 }
11756 else
11757 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011758 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011759 list_unref(s);
11760 s = NULL;
11761 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011762 vim_free(group);
11763 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011764
11765 li = li->li_next;
11766 }
11767 rettv->vval.v_number = 0;
11768 }
11769#endif
11770}
11771
11772/*
11773 * "setpos()" function
11774 */
11775 static void
11776f_setpos(typval_T *argvars, typval_T *rettv)
11777{
11778 pos_T pos;
11779 int fnum;
11780 char_u *name;
11781 colnr_T curswant = -1;
11782
11783 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011784 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011785 if (name != NULL)
11786 {
11787 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11788 {
11789 if (--pos.col < 0)
11790 pos.col = 0;
11791 if (name[0] == '.' && name[1] == NUL)
11792 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011793 /* set cursor; "fnum" is ignored */
11794 curwin->w_cursor = pos;
11795 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011796 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011797 curwin->w_curswant = curswant - 1;
11798 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011799 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011800 check_cursor();
11801 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011802 }
11803 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11804 {
11805 /* set mark */
11806 if (setmark_pos(name[1], &pos, fnum) == OK)
11807 rettv->vval.v_number = 0;
11808 }
11809 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011810 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011811 }
11812 }
11813}
11814
11815/*
11816 * "setqflist()" function
11817 */
11818 static void
11819f_setqflist(typval_T *argvars, typval_T *rettv)
11820{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011821 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011822}
11823
11824/*
11825 * "setreg()" function
11826 */
11827 static void
11828f_setreg(typval_T *argvars, typval_T *rettv)
11829{
11830 int regname;
11831 char_u *strregname;
11832 char_u *stropt;
11833 char_u *strval;
11834 int append;
11835 char_u yank_type;
11836 long block_len;
11837
11838 block_len = -1;
11839 yank_type = MAUTO;
11840 append = FALSE;
11841
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011842 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011843 rettv->vval.v_number = 1; /* FAIL is default */
11844
11845 if (strregname == NULL)
11846 return; /* type error; errmsg already given */
11847 regname = *strregname;
11848 if (regname == 0 || regname == '@')
11849 regname = '"';
11850
11851 if (argvars[2].v_type != VAR_UNKNOWN)
11852 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011853 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011854 if (stropt == NULL)
11855 return; /* type error */
11856 for (; *stropt != NUL; ++stropt)
11857 switch (*stropt)
11858 {
11859 case 'a': case 'A': /* append */
11860 append = TRUE;
11861 break;
11862 case 'v': case 'c': /* character-wise selection */
11863 yank_type = MCHAR;
11864 break;
11865 case 'V': case 'l': /* line-wise selection */
11866 yank_type = MLINE;
11867 break;
11868 case 'b': case Ctrl_V: /* block-wise selection */
11869 yank_type = MBLOCK;
11870 if (VIM_ISDIGIT(stropt[1]))
11871 {
11872 ++stropt;
11873 block_len = getdigits(&stropt) - 1;
11874 --stropt;
11875 }
11876 break;
11877 }
11878 }
11879
11880 if (argvars[1].v_type == VAR_LIST)
11881 {
11882 char_u **lstval;
11883 char_u **allocval;
11884 char_u buf[NUMBUFLEN];
11885 char_u **curval;
11886 char_u **curallocval;
11887 list_T *ll = argvars[1].vval.v_list;
11888 listitem_T *li;
11889 int len;
11890
11891 /* If the list is NULL handle like an empty list. */
11892 len = ll == NULL ? 0 : ll->lv_len;
11893
11894 /* First half: use for pointers to result lines; second half: use for
11895 * pointers to allocated copies. */
11896 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11897 if (lstval == NULL)
11898 return;
11899 curval = lstval;
11900 allocval = lstval + len + 2;
11901 curallocval = allocval;
11902
11903 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11904 li = li->li_next)
11905 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011906 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011907 if (strval == NULL)
11908 goto free_lstval;
11909 if (strval == buf)
11910 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011911 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011912 * overwrite the string. */
11913 strval = vim_strsave(buf);
11914 if (strval == NULL)
11915 goto free_lstval;
11916 *curallocval++ = strval;
11917 }
11918 *curval++ = strval;
11919 }
11920 *curval++ = NULL;
11921
11922 write_reg_contents_lst(regname, lstval, -1,
11923 append, yank_type, block_len);
11924free_lstval:
11925 while (curallocval > allocval)
11926 vim_free(*--curallocval);
11927 vim_free(lstval);
11928 }
11929 else
11930 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011931 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011932 if (strval == NULL)
11933 return;
11934 write_reg_contents_ex(regname, strval, -1,
11935 append, yank_type, block_len);
11936 }
11937 rettv->vval.v_number = 0;
11938}
11939
11940/*
11941 * "settabvar()" function
11942 */
11943 static void
11944f_settabvar(typval_T *argvars, typval_T *rettv)
11945{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011946 tabpage_T *save_curtab;
11947 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011948 char_u *varname, *tabvarname;
11949 typval_T *varp;
11950
11951 rettv->vval.v_number = 0;
11952
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011953 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011954 return;
11955
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011956 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11957 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011958 varp = &argvars[2];
11959
Bram Moolenaar4033c552017-09-16 20:54:51 +020011960 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011961 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011962 save_curtab = curtab;
11963 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011964
11965 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11966 if (tabvarname != NULL)
11967 {
11968 STRCPY(tabvarname, "t:");
11969 STRCPY(tabvarname + 2, varname);
11970 set_var(tabvarname, varp, TRUE);
11971 vim_free(tabvarname);
11972 }
11973
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011974 /* Restore current tabpage */
11975 if (valid_tabpage(save_curtab))
11976 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011977 }
11978}
11979
11980/*
11981 * "settabwinvar()" function
11982 */
11983 static void
11984f_settabwinvar(typval_T *argvars, typval_T *rettv)
11985{
11986 setwinvar(argvars, rettv, 1);
11987}
11988
11989/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011990 * "settagstack()" function
11991 */
11992 static void
11993f_settagstack(typval_T *argvars, typval_T *rettv)
11994{
11995 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11996 win_T *wp;
11997 dict_T *d;
11998 int action = 'r';
11999
12000 rettv->vval.v_number = -1;
12001
12002 // first argument: window number or id
12003 wp = find_win_by_nr_or_id(&argvars[0]);
12004 if (wp == NULL)
12005 return;
12006
12007 // second argument: dict with items to set in the tag stack
12008 if (argvars[1].v_type != VAR_DICT)
12009 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012010 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012011 return;
12012 }
12013 d = argvars[1].vval.v_dict;
12014 if (d == NULL)
12015 return;
12016
12017 // third argument: action - 'a' for append and 'r' for replace.
12018 // default is to replace the stack.
12019 if (argvars[2].v_type == VAR_UNKNOWN)
12020 action = 'r';
12021 else if (argvars[2].v_type == VAR_STRING)
12022 {
12023 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012024 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012025 if (actstr == NULL)
12026 return;
12027 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
12028 action = *actstr;
12029 else
12030 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012031 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012032 return;
12033 }
12034 }
12035 else
12036 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012037 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012038 return;
12039 }
12040
12041 if (set_tagstack(wp, d, action) == OK)
12042 rettv->vval.v_number = 0;
12043}
12044
12045/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012046 * "setwinvar()" function
12047 */
12048 static void
12049f_setwinvar(typval_T *argvars, typval_T *rettv)
12050{
12051 setwinvar(argvars, rettv, 0);
12052}
12053
12054#ifdef FEAT_CRYPT
12055/*
12056 * "sha256({string})" function
12057 */
12058 static void
12059f_sha256(typval_T *argvars, typval_T *rettv)
12060{
12061 char_u *p;
12062
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012063 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012064 rettv->vval.v_string = vim_strsave(
12065 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
12066 rettv->v_type = VAR_STRING;
12067}
12068#endif /* FEAT_CRYPT */
12069
12070/*
12071 * "shellescape({string})" function
12072 */
12073 static void
12074f_shellescape(typval_T *argvars, typval_T *rettv)
12075{
Bram Moolenaar20615522017-06-05 18:46:26 +020012076 int do_special = non_zero_arg(&argvars[1]);
12077
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012078 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012079 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012080 rettv->v_type = VAR_STRING;
12081}
12082
12083/*
12084 * shiftwidth() function
12085 */
12086 static void
12087f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
12088{
Bram Moolenaarf9514162018-11-22 03:08:29 +010012089 rettv->vval.v_number = 0;
12090
12091 if (argvars[0].v_type != VAR_UNKNOWN)
12092 {
12093 long col;
12094
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012095 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010012096 if (col < 0)
12097 return; // type error; errmsg already given
12098#ifdef FEAT_VARTABS
12099 rettv->vval.v_number = get_sw_value_col(curbuf, col);
12100 return;
12101#endif
12102 }
12103
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012104 rettv->vval.v_number = get_sw_value(curbuf);
12105}
12106
Bram Moolenaar162b7142018-12-21 15:17:36 +010012107#ifdef FEAT_SIGNS
12108/*
12109 * "sign_define()" function
12110 */
12111 static void
12112f_sign_define(typval_T *argvars, typval_T *rettv)
12113{
12114 char_u *name;
12115 dict_T *dict;
12116 char_u *icon = NULL;
12117 char_u *linehl = NULL;
12118 char_u *text = NULL;
12119 char_u *texthl = NULL;
12120
12121 rettv->vval.v_number = -1;
12122
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012123 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012124 if (name == NULL)
12125 return;
12126
12127 if (argvars[1].v_type != VAR_UNKNOWN)
12128 {
12129 if (argvars[1].v_type != VAR_DICT)
12130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012131 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012132 return;
12133 }
12134
12135 // sign attributes
12136 dict = argvars[1].vval.v_dict;
12137 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12138 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12139 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12140 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12141 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12142 text = dict_get_string(dict, (char_u *)"text", TRUE);
12143 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12144 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12145 }
12146
12147 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12148 rettv->vval.v_number = 0;
12149
12150 vim_free(icon);
12151 vim_free(linehl);
12152 vim_free(text);
12153 vim_free(texthl);
12154}
12155
12156/*
12157 * "sign_getdefined()" function
12158 */
12159 static void
12160f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12161{
12162 char_u *name = NULL;
12163
12164 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12165 return;
12166
12167 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012168 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012169
12170 sign_getlist(name, rettv->vval.v_list);
12171}
12172
12173/*
12174 * "sign_getplaced()" function
12175 */
12176 static void
12177f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12178{
12179 buf_T *buf = NULL;
12180 dict_T *dict;
12181 dictitem_T *di;
12182 linenr_T lnum = 0;
12183 int sign_id = 0;
12184 char_u *group = NULL;
12185 int notanum = FALSE;
12186
12187 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12188 return;
12189
12190 if (argvars[0].v_type != VAR_UNKNOWN)
12191 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012192 // get signs placed in the specified buffer
12193 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012194 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012195 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012196
12197 if (argvars[1].v_type != VAR_UNKNOWN)
12198 {
12199 if (argvars[1].v_type != VAR_DICT ||
12200 ((dict = argvars[1].vval.v_dict) == NULL))
12201 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012202 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012203 return;
12204 }
12205 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12206 {
12207 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012208 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012209 if (notanum)
12210 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012211 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012212 }
12213 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12214 {
12215 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012216 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012217 if (notanum)
12218 return;
12219 }
12220 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12221 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012222 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012223 if (group == NULL)
12224 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012225 if (*group == '\0') // empty string means global group
12226 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012227 }
12228 }
12229 }
12230
12231 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12232}
12233
12234/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012235 * "sign_jump()" function
12236 */
12237 static void
12238f_sign_jump(typval_T *argvars, typval_T *rettv)
12239{
12240 int sign_id;
12241 char_u *sign_group = NULL;
12242 buf_T *buf;
12243 int notanum = FALSE;
12244
12245 rettv->vval.v_number = -1;
12246
Bram Moolenaarbdace832019-03-02 10:13:42 +010012247 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012248 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12249 if (notanum)
12250 return;
12251 if (sign_id <= 0)
12252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012253 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012254 return;
12255 }
12256
12257 // Sign group
12258 sign_group = tv_get_string_chk(&argvars[1]);
12259 if (sign_group == NULL)
12260 return;
12261 if (sign_group[0] == '\0')
12262 sign_group = NULL; // global sign group
12263 else
12264 {
12265 sign_group = vim_strsave(sign_group);
12266 if (sign_group == NULL)
12267 return;
12268 }
12269
12270 // Buffer to place the sign
12271 buf = get_buf_arg(&argvars[2]);
12272 if (buf == NULL)
12273 goto cleanup;
12274
12275 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12276
12277cleanup:
12278 vim_free(sign_group);
12279}
12280
12281/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012282 * "sign_place()" function
12283 */
12284 static void
12285f_sign_place(typval_T *argvars, typval_T *rettv)
12286{
12287 int sign_id;
12288 char_u *group = NULL;
12289 char_u *sign_name;
12290 buf_T *buf;
12291 dict_T *dict;
12292 dictitem_T *di;
12293 linenr_T lnum = 0;
12294 int prio = SIGN_DEF_PRIO;
12295 int notanum = FALSE;
12296
12297 rettv->vval.v_number = -1;
12298
Bram Moolenaarbdace832019-03-02 10:13:42 +010012299 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012300 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012301 if (notanum)
12302 return;
12303 if (sign_id < 0)
12304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012305 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012306 return;
12307 }
12308
12309 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012310 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012311 if (group == NULL)
12312 return;
12313 if (group[0] == '\0')
12314 group = NULL; // global sign group
12315 else
12316 {
12317 group = vim_strsave(group);
12318 if (group == NULL)
12319 return;
12320 }
12321
12322 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012323 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012324 if (sign_name == NULL)
12325 goto cleanup;
12326
12327 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012328 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012329 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012330 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012331
12332 if (argvars[4].v_type != VAR_UNKNOWN)
12333 {
12334 if (argvars[4].v_type != VAR_DICT ||
12335 ((dict = argvars[4].vval.v_dict) == NULL))
12336 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012337 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012338 goto cleanup;
12339 }
12340
12341 // Line number where the sign is to be placed
12342 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12343 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012344 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012345 if (notanum)
12346 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012347 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012348 }
12349 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12350 {
12351 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012352 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012353 if (notanum)
12354 goto cleanup;
12355 }
12356 }
12357
12358 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12359 rettv->vval.v_number = sign_id;
12360
12361cleanup:
12362 vim_free(group);
12363}
12364
12365/*
12366 * "sign_undefine()" function
12367 */
12368 static void
12369f_sign_undefine(typval_T *argvars, typval_T *rettv)
12370{
12371 char_u *name;
12372
12373 rettv->vval.v_number = -1;
12374
12375 if (argvars[0].v_type == VAR_UNKNOWN)
12376 {
12377 // Free all the signs
12378 free_signs();
12379 rettv->vval.v_number = 0;
12380 }
12381 else
12382 {
12383 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012384 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012385 if (name == NULL)
12386 return;
12387
12388 if (sign_undefine_by_name(name) == OK)
12389 rettv->vval.v_number = 0;
12390 }
12391}
12392
12393/*
12394 * "sign_unplace()" function
12395 */
12396 static void
12397f_sign_unplace(typval_T *argvars, typval_T *rettv)
12398{
12399 dict_T *dict;
12400 dictitem_T *di;
12401 int sign_id = 0;
12402 buf_T *buf = NULL;
12403 char_u *group = NULL;
12404
12405 rettv->vval.v_number = -1;
12406
12407 if (argvars[0].v_type != VAR_STRING)
12408 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012409 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012410 return;
12411 }
12412
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012413 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012414 if (group[0] == '\0')
12415 group = NULL; // global sign group
12416 else
12417 {
12418 group = vim_strsave(group);
12419 if (group == NULL)
12420 return;
12421 }
12422
12423 if (argvars[1].v_type != VAR_UNKNOWN)
12424 {
12425 if (argvars[1].v_type != VAR_DICT)
12426 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012427 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012428 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012429 }
12430 dict = argvars[1].vval.v_dict;
12431
12432 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12433 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012434 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012435 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012436 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012437 }
12438 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12439 sign_id = dict_get_number(dict, (char_u *)"id");
12440 }
12441
12442 if (buf == NULL)
12443 {
12444 // Delete the sign in all the buffers
12445 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012446 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012447 rettv->vval.v_number = 0;
12448 }
12449 else
12450 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012451 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012452 rettv->vval.v_number = 0;
12453 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012454
12455cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012456 vim_free(group);
12457}
12458#endif
12459
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012460/*
12461 * "simplify()" function
12462 */
12463 static void
12464f_simplify(typval_T *argvars, typval_T *rettv)
12465{
12466 char_u *p;
12467
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012468 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012469 rettv->vval.v_string = vim_strsave(p);
12470 simplify_filename(rettv->vval.v_string); /* simplify in place */
12471 rettv->v_type = VAR_STRING;
12472}
12473
12474#ifdef FEAT_FLOAT
12475/*
12476 * "sin()" function
12477 */
12478 static void
12479f_sin(typval_T *argvars, typval_T *rettv)
12480{
12481 float_T f = 0.0;
12482
12483 rettv->v_type = VAR_FLOAT;
12484 if (get_float_arg(argvars, &f) == OK)
12485 rettv->vval.v_float = sin(f);
12486 else
12487 rettv->vval.v_float = 0.0;
12488}
12489
12490/*
12491 * "sinh()" function
12492 */
12493 static void
12494f_sinh(typval_T *argvars, typval_T *rettv)
12495{
12496 float_T f = 0.0;
12497
12498 rettv->v_type = VAR_FLOAT;
12499 if (get_float_arg(argvars, &f) == OK)
12500 rettv->vval.v_float = sinh(f);
12501 else
12502 rettv->vval.v_float = 0.0;
12503}
12504#endif
12505
Bram Moolenaareae1b912019-05-09 15:12:55 +020012506static int item_compare(const void *s1, const void *s2);
12507static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012508
12509/* struct used in the array that's given to qsort() */
12510typedef struct
12511{
12512 listitem_T *item;
12513 int idx;
12514} sortItem_T;
12515
12516/* struct storing information about current sort */
12517typedef struct
12518{
12519 int item_compare_ic;
12520 int item_compare_numeric;
12521 int item_compare_numbers;
12522#ifdef FEAT_FLOAT
12523 int item_compare_float;
12524#endif
12525 char_u *item_compare_func;
12526 partial_T *item_compare_partial;
12527 dict_T *item_compare_selfdict;
12528 int item_compare_func_err;
12529 int item_compare_keep_zero;
12530} sortinfo_T;
12531static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012532#define ITEM_COMPARE_FAIL 999
12533
12534/*
12535 * Compare functions for f_sort() and f_uniq() below.
12536 */
12537 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012538item_compare(const void *s1, const void *s2)
12539{
12540 sortItem_T *si1, *si2;
12541 typval_T *tv1, *tv2;
12542 char_u *p1, *p2;
12543 char_u *tofree1 = NULL, *tofree2 = NULL;
12544 int res;
12545 char_u numbuf1[NUMBUFLEN];
12546 char_u numbuf2[NUMBUFLEN];
12547
12548 si1 = (sortItem_T *)s1;
12549 si2 = (sortItem_T *)s2;
12550 tv1 = &si1->item->li_tv;
12551 tv2 = &si2->item->li_tv;
12552
12553 if (sortinfo->item_compare_numbers)
12554 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012555 varnumber_T v1 = tv_get_number(tv1);
12556 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012557
12558 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12559 }
12560
12561#ifdef FEAT_FLOAT
12562 if (sortinfo->item_compare_float)
12563 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012564 float_T v1 = tv_get_float(tv1);
12565 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012566
12567 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12568 }
12569#endif
12570
12571 /* tv2string() puts quotes around a string and allocates memory. Don't do
12572 * that for string variables. Use a single quote when comparing with a
12573 * non-string to do what the docs promise. */
12574 if (tv1->v_type == VAR_STRING)
12575 {
12576 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12577 p1 = (char_u *)"'";
12578 else
12579 p1 = tv1->vval.v_string;
12580 }
12581 else
12582 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12583 if (tv2->v_type == VAR_STRING)
12584 {
12585 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12586 p2 = (char_u *)"'";
12587 else
12588 p2 = tv2->vval.v_string;
12589 }
12590 else
12591 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12592 if (p1 == NULL)
12593 p1 = (char_u *)"";
12594 if (p2 == NULL)
12595 p2 = (char_u *)"";
12596 if (!sortinfo->item_compare_numeric)
12597 {
12598 if (sortinfo->item_compare_ic)
12599 res = STRICMP(p1, p2);
12600 else
12601 res = STRCMP(p1, p2);
12602 }
12603 else
12604 {
12605 double n1, n2;
12606 n1 = strtod((char *)p1, (char **)&p1);
12607 n2 = strtod((char *)p2, (char **)&p2);
12608 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12609 }
12610
12611 /* When the result would be zero, compare the item indexes. Makes the
12612 * sort stable. */
12613 if (res == 0 && !sortinfo->item_compare_keep_zero)
12614 res = si1->idx > si2->idx ? 1 : -1;
12615
12616 vim_free(tofree1);
12617 vim_free(tofree2);
12618 return res;
12619}
12620
12621 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012622item_compare2(const void *s1, const void *s2)
12623{
12624 sortItem_T *si1, *si2;
12625 int res;
12626 typval_T rettv;
12627 typval_T argv[3];
12628 int dummy;
12629 char_u *func_name;
12630 partial_T *partial = sortinfo->item_compare_partial;
12631
12632 /* shortcut after failure in previous call; compare all items equal */
12633 if (sortinfo->item_compare_func_err)
12634 return 0;
12635
12636 si1 = (sortItem_T *)s1;
12637 si2 = (sortItem_T *)s2;
12638
12639 if (partial == NULL)
12640 func_name = sortinfo->item_compare_func;
12641 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012642 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012643
12644 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12645 * in the copy without changing the original list items. */
12646 copy_tv(&si1->item->li_tv, &argv[0]);
12647 copy_tv(&si2->item->li_tv, &argv[1]);
12648
12649 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012650 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012651 partial, sortinfo->item_compare_selfdict);
12652 clear_tv(&argv[0]);
12653 clear_tv(&argv[1]);
12654
12655 if (res == FAIL)
12656 res = ITEM_COMPARE_FAIL;
12657 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012658 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012659 if (sortinfo->item_compare_func_err)
12660 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12661 clear_tv(&rettv);
12662
12663 /* When the result would be zero, compare the pointers themselves. Makes
12664 * the sort stable. */
12665 if (res == 0 && !sortinfo->item_compare_keep_zero)
12666 res = si1->idx > si2->idx ? 1 : -1;
12667
12668 return res;
12669}
12670
12671/*
12672 * "sort({list})" function
12673 */
12674 static void
12675do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12676{
12677 list_T *l;
12678 listitem_T *li;
12679 sortItem_T *ptrs;
12680 sortinfo_T *old_sortinfo;
12681 sortinfo_T info;
12682 long len;
12683 long i;
12684
12685 /* Pointer to current info struct used in compare function. Save and
12686 * restore the current one for nested calls. */
12687 old_sortinfo = sortinfo;
12688 sortinfo = &info;
12689
12690 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012691 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012692 else
12693 {
12694 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012695 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012696 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12697 TRUE))
12698 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012699 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012700
12701 len = list_len(l);
12702 if (len <= 1)
12703 goto theend; /* short list sorts pretty quickly */
12704
12705 info.item_compare_ic = FALSE;
12706 info.item_compare_numeric = FALSE;
12707 info.item_compare_numbers = FALSE;
12708#ifdef FEAT_FLOAT
12709 info.item_compare_float = FALSE;
12710#endif
12711 info.item_compare_func = NULL;
12712 info.item_compare_partial = NULL;
12713 info.item_compare_selfdict = NULL;
12714 if (argvars[1].v_type != VAR_UNKNOWN)
12715 {
12716 /* optional second argument: {func} */
12717 if (argvars[1].v_type == VAR_FUNC)
12718 info.item_compare_func = argvars[1].vval.v_string;
12719 else if (argvars[1].v_type == VAR_PARTIAL)
12720 info.item_compare_partial = argvars[1].vval.v_partial;
12721 else
12722 {
12723 int error = FALSE;
12724
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012725 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012726 if (error)
12727 goto theend; /* type error; errmsg already given */
12728 if (i == 1)
12729 info.item_compare_ic = TRUE;
12730 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012731 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012732 else if (i != 0)
12733 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012734 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012735 goto theend;
12736 }
12737 if (info.item_compare_func != NULL)
12738 {
12739 if (*info.item_compare_func == NUL)
12740 {
12741 /* empty string means default sort */
12742 info.item_compare_func = NULL;
12743 }
12744 else if (STRCMP(info.item_compare_func, "n") == 0)
12745 {
12746 info.item_compare_func = NULL;
12747 info.item_compare_numeric = TRUE;
12748 }
12749 else if (STRCMP(info.item_compare_func, "N") == 0)
12750 {
12751 info.item_compare_func = NULL;
12752 info.item_compare_numbers = TRUE;
12753 }
12754#ifdef FEAT_FLOAT
12755 else if (STRCMP(info.item_compare_func, "f") == 0)
12756 {
12757 info.item_compare_func = NULL;
12758 info.item_compare_float = TRUE;
12759 }
12760#endif
12761 else if (STRCMP(info.item_compare_func, "i") == 0)
12762 {
12763 info.item_compare_func = NULL;
12764 info.item_compare_ic = TRUE;
12765 }
12766 }
12767 }
12768
12769 if (argvars[2].v_type != VAR_UNKNOWN)
12770 {
12771 /* optional third argument: {dict} */
12772 if (argvars[2].v_type != VAR_DICT)
12773 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012774 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012775 goto theend;
12776 }
12777 info.item_compare_selfdict = argvars[2].vval.v_dict;
12778 }
12779 }
12780
12781 /* Make an array with each entry pointing to an item in the List. */
12782 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12783 if (ptrs == NULL)
12784 goto theend;
12785
12786 i = 0;
12787 if (sort)
12788 {
12789 /* sort(): ptrs will be the list to sort */
12790 for (li = l->lv_first; li != NULL; li = li->li_next)
12791 {
12792 ptrs[i].item = li;
12793 ptrs[i].idx = i;
12794 ++i;
12795 }
12796
12797 info.item_compare_func_err = FALSE;
12798 info.item_compare_keep_zero = FALSE;
12799 /* test the compare function */
12800 if ((info.item_compare_func != NULL
12801 || info.item_compare_partial != NULL)
12802 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12803 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012804 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012805 else
12806 {
12807 /* Sort the array with item pointers. */
12808 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12809 info.item_compare_func == NULL
12810 && info.item_compare_partial == NULL
12811 ? item_compare : item_compare2);
12812
12813 if (!info.item_compare_func_err)
12814 {
12815 /* Clear the List and append the items in sorted order. */
12816 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12817 l->lv_len = 0;
12818 for (i = 0; i < len; ++i)
12819 list_append(l, ptrs[i].item);
12820 }
12821 }
12822 }
12823 else
12824 {
12825 int (*item_compare_func_ptr)(const void *, const void *);
12826
12827 /* f_uniq(): ptrs will be a stack of items to remove */
12828 info.item_compare_func_err = FALSE;
12829 info.item_compare_keep_zero = TRUE;
12830 item_compare_func_ptr = info.item_compare_func != NULL
12831 || info.item_compare_partial != NULL
12832 ? item_compare2 : item_compare;
12833
12834 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12835 li = li->li_next)
12836 {
12837 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12838 == 0)
12839 ptrs[i++].item = li;
12840 if (info.item_compare_func_err)
12841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012842 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012843 break;
12844 }
12845 }
12846
12847 if (!info.item_compare_func_err)
12848 {
12849 while (--i >= 0)
12850 {
12851 li = ptrs[i].item->li_next;
12852 ptrs[i].item->li_next = li->li_next;
12853 if (li->li_next != NULL)
12854 li->li_next->li_prev = ptrs[i].item;
12855 else
12856 l->lv_last = ptrs[i].item;
12857 list_fix_watch(l, li);
12858 listitem_free(li);
12859 l->lv_len--;
12860 }
12861 }
12862 }
12863
12864 vim_free(ptrs);
12865 }
12866theend:
12867 sortinfo = old_sortinfo;
12868}
12869
12870/*
12871 * "sort({list})" function
12872 */
12873 static void
12874f_sort(typval_T *argvars, typval_T *rettv)
12875{
12876 do_sort_uniq(argvars, rettv, TRUE);
12877}
12878
12879/*
12880 * "uniq({list})" function
12881 */
12882 static void
12883f_uniq(typval_T *argvars, typval_T *rettv)
12884{
12885 do_sort_uniq(argvars, rettv, FALSE);
12886}
12887
12888/*
12889 * "soundfold({word})" function
12890 */
12891 static void
12892f_soundfold(typval_T *argvars, typval_T *rettv)
12893{
12894 char_u *s;
12895
12896 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012897 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012898#ifdef FEAT_SPELL
12899 rettv->vval.v_string = eval_soundfold(s);
12900#else
12901 rettv->vval.v_string = vim_strsave(s);
12902#endif
12903}
12904
12905/*
12906 * "spellbadword()" function
12907 */
12908 static void
12909f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12910{
12911 char_u *word = (char_u *)"";
12912 hlf_T attr = HLF_COUNT;
12913 int len = 0;
12914
12915 if (rettv_list_alloc(rettv) == FAIL)
12916 return;
12917
12918#ifdef FEAT_SPELL
12919 if (argvars[0].v_type == VAR_UNKNOWN)
12920 {
12921 /* Find the start and length of the badly spelled word. */
12922 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12923 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012924 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012925 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012926 curwin->w_set_curswant = TRUE;
12927 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012928 }
12929 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12930 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012931 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012932 int capcol = -1;
12933
12934 if (str != NULL)
12935 {
12936 /* Check the argument for spelling. */
12937 while (*str != NUL)
12938 {
12939 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12940 if (attr != HLF_COUNT)
12941 {
12942 word = str;
12943 break;
12944 }
12945 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012946 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012947 }
12948 }
12949 }
12950#endif
12951
12952 list_append_string(rettv->vval.v_list, word, len);
12953 list_append_string(rettv->vval.v_list, (char_u *)(
12954 attr == HLF_SPB ? "bad" :
12955 attr == HLF_SPR ? "rare" :
12956 attr == HLF_SPL ? "local" :
12957 attr == HLF_SPC ? "caps" :
12958 ""), -1);
12959}
12960
12961/*
12962 * "spellsuggest()" function
12963 */
12964 static void
12965f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12966{
12967#ifdef FEAT_SPELL
12968 char_u *str;
12969 int typeerr = FALSE;
12970 int maxcount;
12971 garray_T ga;
12972 int i;
12973 listitem_T *li;
12974 int need_capital = FALSE;
12975#endif
12976
12977 if (rettv_list_alloc(rettv) == FAIL)
12978 return;
12979
12980#ifdef FEAT_SPELL
12981 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12982 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012983 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012984 if (argvars[1].v_type != VAR_UNKNOWN)
12985 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012986 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012987 if (maxcount <= 0)
12988 return;
12989 if (argvars[2].v_type != VAR_UNKNOWN)
12990 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012991 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012992 if (typeerr)
12993 return;
12994 }
12995 }
12996 else
12997 maxcount = 25;
12998
12999 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
13000
13001 for (i = 0; i < ga.ga_len; ++i)
13002 {
13003 str = ((char_u **)ga.ga_data)[i];
13004
13005 li = listitem_alloc();
13006 if (li == NULL)
13007 vim_free(str);
13008 else
13009 {
13010 li->li_tv.v_type = VAR_STRING;
13011 li->li_tv.v_lock = 0;
13012 li->li_tv.vval.v_string = str;
13013 list_append(rettv->vval.v_list, li);
13014 }
13015 }
13016 ga_clear(&ga);
13017 }
13018#endif
13019}
13020
13021 static void
13022f_split(typval_T *argvars, typval_T *rettv)
13023{
13024 char_u *str;
13025 char_u *end;
13026 char_u *pat = NULL;
13027 regmatch_T regmatch;
13028 char_u patbuf[NUMBUFLEN];
13029 char_u *save_cpo;
13030 int match;
13031 colnr_T col = 0;
13032 int keepempty = FALSE;
13033 int typeerr = FALSE;
13034
13035 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13036 save_cpo = p_cpo;
13037 p_cpo = (char_u *)"";
13038
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013039 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013040 if (argvars[1].v_type != VAR_UNKNOWN)
13041 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013042 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013043 if (pat == NULL)
13044 typeerr = TRUE;
13045 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013046 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013047 }
13048 if (pat == NULL || *pat == NUL)
13049 pat = (char_u *)"[\\x01- ]\\+";
13050
13051 if (rettv_list_alloc(rettv) == FAIL)
13052 return;
13053 if (typeerr)
13054 return;
13055
13056 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13057 if (regmatch.regprog != NULL)
13058 {
13059 regmatch.rm_ic = FALSE;
13060 while (*str != NUL || keepempty)
13061 {
13062 if (*str == NUL)
13063 match = FALSE; /* empty item at the end */
13064 else
13065 match = vim_regexec_nl(&regmatch, str, col);
13066 if (match)
13067 end = regmatch.startp[0];
13068 else
13069 end = str + STRLEN(str);
13070 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
13071 && *str != NUL && match && end < regmatch.endp[0]))
13072 {
13073 if (list_append_string(rettv->vval.v_list, str,
13074 (int)(end - str)) == FAIL)
13075 break;
13076 }
13077 if (!match)
13078 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010013079 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013080 if (regmatch.endp[0] > str)
13081 col = 0;
13082 else
Bram Moolenaar13505972019-01-24 15:04:48 +010013083 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013084 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013085 str = regmatch.endp[0];
13086 }
13087
13088 vim_regfree(regmatch.regprog);
13089 }
13090
13091 p_cpo = save_cpo;
13092}
13093
13094#ifdef FEAT_FLOAT
13095/*
13096 * "sqrt()" function
13097 */
13098 static void
13099f_sqrt(typval_T *argvars, typval_T *rettv)
13100{
13101 float_T f = 0.0;
13102
13103 rettv->v_type = VAR_FLOAT;
13104 if (get_float_arg(argvars, &f) == OK)
13105 rettv->vval.v_float = sqrt(f);
13106 else
13107 rettv->vval.v_float = 0.0;
13108}
13109
13110/*
13111 * "str2float()" function
13112 */
13113 static void
13114f_str2float(typval_T *argvars, typval_T *rettv)
13115{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013116 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013117 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013118
Bram Moolenaar08243d22017-01-10 16:12:29 +010013119 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013120 p = skipwhite(p + 1);
13121 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013122 if (isneg)
13123 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013124 rettv->v_type = VAR_FLOAT;
13125}
13126#endif
13127
13128/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013129 * "str2list()" function
13130 */
13131 static void
13132f_str2list(typval_T *argvars, typval_T *rettv)
13133{
13134 char_u *p;
13135 int utf8 = FALSE;
13136
13137 if (rettv_list_alloc(rettv) == FAIL)
13138 return;
13139
13140 if (argvars[1].v_type != VAR_UNKNOWN)
13141 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13142
13143 p = tv_get_string(&argvars[0]);
13144
13145 if (has_mbyte || utf8)
13146 {
13147 int (*ptr2len)(char_u *);
13148 int (*ptr2char)(char_u *);
13149
13150 if (utf8 || enc_utf8)
13151 {
13152 ptr2len = utf_ptr2len;
13153 ptr2char = utf_ptr2char;
13154 }
13155 else
13156 {
13157 ptr2len = mb_ptr2len;
13158 ptr2char = mb_ptr2char;
13159 }
13160
13161 for ( ; *p != NUL; p += (*ptr2len)(p))
13162 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13163 }
13164 else
13165 for ( ; *p != NUL; ++p)
13166 list_append_number(rettv->vval.v_list, *p);
13167}
13168
13169/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013170 * "str2nr()" function
13171 */
13172 static void
13173f_str2nr(typval_T *argvars, typval_T *rettv)
13174{
13175 int base = 10;
13176 char_u *p;
13177 varnumber_T n;
13178 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013179 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180
13181 if (argvars[1].v_type != VAR_UNKNOWN)
13182 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013183 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013184 if (base != 2 && base != 8 && base != 10 && base != 16)
13185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013186 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013187 return;
13188 }
13189 }
13190
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013191 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013192 isneg = (*p == '-');
13193 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013194 p = skipwhite(p + 1);
13195 switch (base)
13196 {
13197 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13198 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13199 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13200 default: what = 0;
13201 }
13202 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013203 if (isneg)
13204 rettv->vval.v_number = -n;
13205 else
13206 rettv->vval.v_number = n;
13207
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013208}
13209
13210#ifdef HAVE_STRFTIME
13211/*
13212 * "strftime({format}[, {time}])" function
13213 */
13214 static void
13215f_strftime(typval_T *argvars, typval_T *rettv)
13216{
13217 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013218# ifdef HAVE_LOCALTIME_R
13219 struct tm tmval;
13220# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013221 struct tm *curtime;
13222 time_t seconds;
13223 char_u *p;
13224
13225 rettv->v_type = VAR_STRING;
13226
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013227 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013228 if (argvars[1].v_type == VAR_UNKNOWN)
13229 seconds = time(NULL);
13230 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013231 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013232# ifdef HAVE_LOCALTIME_R
13233 curtime = localtime_r(&seconds, &tmval);
13234# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013235 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013236# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013237 /* MSVC returns NULL for an invalid value of seconds. */
13238 if (curtime == NULL)
13239 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13240 else
13241 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013242 vimconv_T conv;
13243 char_u *enc;
13244
13245 conv.vc_type = CONV_NONE;
13246 enc = enc_locale();
13247 convert_setup(&conv, p_enc, enc);
13248 if (conv.vc_type != CONV_NONE)
13249 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013250 if (p != NULL)
13251 (void)strftime((char *)result_buf, sizeof(result_buf),
13252 (char *)p, curtime);
13253 else
13254 result_buf[0] = NUL;
13255
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013256 if (conv.vc_type != CONV_NONE)
13257 vim_free(p);
13258 convert_setup(&conv, enc, p_enc);
13259 if (conv.vc_type != CONV_NONE)
13260 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13261 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013262 rettv->vval.v_string = vim_strsave(result_buf);
13263
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013264 /* Release conversion descriptors */
13265 convert_setup(&conv, NULL, NULL);
13266 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013267 }
13268}
13269#endif
13270
13271/*
13272 * "strgetchar()" function
13273 */
13274 static void
13275f_strgetchar(typval_T *argvars, typval_T *rettv)
13276{
13277 char_u *str;
13278 int len;
13279 int error = FALSE;
13280 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013281 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013282
13283 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013284 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013285 if (str == NULL)
13286 return;
13287 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013288 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013289 if (error)
13290 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013291
Bram Moolenaar13505972019-01-24 15:04:48 +010013292 while (charidx >= 0 && byteidx < len)
13293 {
13294 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013295 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013296 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13297 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013298 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013299 --charidx;
13300 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013301 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013302}
13303
13304/*
13305 * "stridx()" function
13306 */
13307 static void
13308f_stridx(typval_T *argvars, typval_T *rettv)
13309{
13310 char_u buf[NUMBUFLEN];
13311 char_u *needle;
13312 char_u *haystack;
13313 char_u *save_haystack;
13314 char_u *pos;
13315 int start_idx;
13316
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013317 needle = tv_get_string_chk(&argvars[1]);
13318 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013319 rettv->vval.v_number = -1;
13320 if (needle == NULL || haystack == NULL)
13321 return; /* type error; errmsg already given */
13322
13323 if (argvars[2].v_type != VAR_UNKNOWN)
13324 {
13325 int error = FALSE;
13326
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013327 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013328 if (error || start_idx >= (int)STRLEN(haystack))
13329 return;
13330 if (start_idx >= 0)
13331 haystack += start_idx;
13332 }
13333
13334 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13335 if (pos != NULL)
13336 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13337}
13338
13339/*
13340 * "string()" function
13341 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013342 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013343f_string(typval_T *argvars, typval_T *rettv)
13344{
13345 char_u *tofree;
13346 char_u numbuf[NUMBUFLEN];
13347
13348 rettv->v_type = VAR_STRING;
13349 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13350 get_copyID());
13351 /* Make a copy if we have a value but it's not in allocated memory. */
13352 if (rettv->vval.v_string != NULL && tofree == NULL)
13353 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13354}
13355
13356/*
13357 * "strlen()" function
13358 */
13359 static void
13360f_strlen(typval_T *argvars, typval_T *rettv)
13361{
13362 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013363 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013364}
13365
13366/*
13367 * "strchars()" function
13368 */
13369 static void
13370f_strchars(typval_T *argvars, typval_T *rettv)
13371{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013372 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013373 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013374 varnumber_T len = 0;
13375 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013376
13377 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013378 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013379 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013380 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013381 else
13382 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013383 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13384 while (*s != NUL)
13385 {
13386 func_mb_ptr2char_adv(&s);
13387 ++len;
13388 }
13389 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013390 }
13391}
13392
13393/*
13394 * "strdisplaywidth()" function
13395 */
13396 static void
13397f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13398{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013399 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013400 int col = 0;
13401
13402 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013403 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013404
13405 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13406}
13407
13408/*
13409 * "strwidth()" function
13410 */
13411 static void
13412f_strwidth(typval_T *argvars, typval_T *rettv)
13413{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013414 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013415
Bram Moolenaar13505972019-01-24 15:04:48 +010013416 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013417}
13418
13419/*
13420 * "strcharpart()" function
13421 */
13422 static void
13423f_strcharpart(typval_T *argvars, typval_T *rettv)
13424{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013425 char_u *p;
13426 int nchar;
13427 int nbyte = 0;
13428 int charlen;
13429 int len = 0;
13430 int slen;
13431 int error = FALSE;
13432
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013433 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013434 slen = (int)STRLEN(p);
13435
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013436 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013437 if (!error)
13438 {
13439 if (nchar > 0)
13440 while (nchar > 0 && nbyte < slen)
13441 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013442 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013443 --nchar;
13444 }
13445 else
13446 nbyte = nchar;
13447 if (argvars[2].v_type != VAR_UNKNOWN)
13448 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013449 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013450 while (charlen > 0 && nbyte + len < slen)
13451 {
13452 int off = nbyte + len;
13453
13454 if (off < 0)
13455 len += 1;
13456 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013457 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013458 --charlen;
13459 }
13460 }
13461 else
13462 len = slen - nbyte; /* default: all bytes that are available. */
13463 }
13464
13465 /*
13466 * Only return the overlap between the specified part and the actual
13467 * string.
13468 */
13469 if (nbyte < 0)
13470 {
13471 len += nbyte;
13472 nbyte = 0;
13473 }
13474 else if (nbyte > slen)
13475 nbyte = slen;
13476 if (len < 0)
13477 len = 0;
13478 else if (nbyte + len > slen)
13479 len = slen - nbyte;
13480
13481 rettv->v_type = VAR_STRING;
13482 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013483}
13484
13485/*
13486 * "strpart()" function
13487 */
13488 static void
13489f_strpart(typval_T *argvars, typval_T *rettv)
13490{
13491 char_u *p;
13492 int n;
13493 int len;
13494 int slen;
13495 int error = FALSE;
13496
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013497 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013498 slen = (int)STRLEN(p);
13499
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013500 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013501 if (error)
13502 len = 0;
13503 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013504 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013505 else
13506 len = slen - n; /* default len: all bytes that are available. */
13507
13508 /*
13509 * Only return the overlap between the specified part and the actual
13510 * string.
13511 */
13512 if (n < 0)
13513 {
13514 len += n;
13515 n = 0;
13516 }
13517 else if (n > slen)
13518 n = slen;
13519 if (len < 0)
13520 len = 0;
13521 else if (n + len > slen)
13522 len = slen - n;
13523
13524 rettv->v_type = VAR_STRING;
13525 rettv->vval.v_string = vim_strnsave(p + n, len);
13526}
13527
13528/*
13529 * "strridx()" function
13530 */
13531 static void
13532f_strridx(typval_T *argvars, typval_T *rettv)
13533{
13534 char_u buf[NUMBUFLEN];
13535 char_u *needle;
13536 char_u *haystack;
13537 char_u *rest;
13538 char_u *lastmatch = NULL;
13539 int haystack_len, end_idx;
13540
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013541 needle = tv_get_string_chk(&argvars[1]);
13542 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013543
13544 rettv->vval.v_number = -1;
13545 if (needle == NULL || haystack == NULL)
13546 return; /* type error; errmsg already given */
13547
13548 haystack_len = (int)STRLEN(haystack);
13549 if (argvars[2].v_type != VAR_UNKNOWN)
13550 {
13551 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013552 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013553 if (end_idx < 0)
13554 return; /* can never find a match */
13555 }
13556 else
13557 end_idx = haystack_len;
13558
13559 if (*needle == NUL)
13560 {
13561 /* Empty string matches past the end. */
13562 lastmatch = haystack + end_idx;
13563 }
13564 else
13565 {
13566 for (rest = haystack; *rest != '\0'; ++rest)
13567 {
13568 rest = (char_u *)strstr((char *)rest, (char *)needle);
13569 if (rest == NULL || rest > haystack + end_idx)
13570 break;
13571 lastmatch = rest;
13572 }
13573 }
13574
13575 if (lastmatch == NULL)
13576 rettv->vval.v_number = -1;
13577 else
13578 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13579}
13580
13581/*
13582 * "strtrans()" function
13583 */
13584 static void
13585f_strtrans(typval_T *argvars, typval_T *rettv)
13586{
13587 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013588 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013589}
13590
13591/*
13592 * "submatch()" function
13593 */
13594 static void
13595f_submatch(typval_T *argvars, typval_T *rettv)
13596{
13597 int error = FALSE;
13598 int no;
13599 int retList = 0;
13600
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013601 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013602 if (error)
13603 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013604 if (no < 0 || no >= NSUBEXP)
13605 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013606 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013607 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013608 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013609 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013610 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013611 if (error)
13612 return;
13613
13614 if (retList == 0)
13615 {
13616 rettv->v_type = VAR_STRING;
13617 rettv->vval.v_string = reg_submatch(no);
13618 }
13619 else
13620 {
13621 rettv->v_type = VAR_LIST;
13622 rettv->vval.v_list = reg_submatch_list(no);
13623 }
13624}
13625
13626/*
13627 * "substitute()" function
13628 */
13629 static void
13630f_substitute(typval_T *argvars, typval_T *rettv)
13631{
13632 char_u patbuf[NUMBUFLEN];
13633 char_u subbuf[NUMBUFLEN];
13634 char_u flagsbuf[NUMBUFLEN];
13635
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013636 char_u *str = tv_get_string_chk(&argvars[0]);
13637 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013638 char_u *sub = NULL;
13639 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013640 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013641
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013642 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13643 expr = &argvars[2];
13644 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013645 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013646
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013647 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013648 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13649 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013650 rettv->vval.v_string = NULL;
13651 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013652 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013653}
13654
13655/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013656 * "swapinfo(swap_filename)" function
13657 */
13658 static void
13659f_swapinfo(typval_T *argvars, typval_T *rettv)
13660{
13661 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013662 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013663}
13664
13665/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013666 * "swapname(expr)" function
13667 */
13668 static void
13669f_swapname(typval_T *argvars, typval_T *rettv)
13670{
13671 buf_T *buf;
13672
13673 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013674 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013675 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13676 || buf->b_ml.ml_mfp->mf_fname == NULL)
13677 rettv->vval.v_string = NULL;
13678 else
13679 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13680}
13681
13682/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013683 * "synID(lnum, col, trans)" function
13684 */
13685 static void
13686f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13687{
13688 int id = 0;
13689#ifdef FEAT_SYN_HL
13690 linenr_T lnum;
13691 colnr_T col;
13692 int trans;
13693 int transerr = FALSE;
13694
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013695 lnum = tv_get_lnum(argvars); /* -1 on type error */
13696 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13697 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013698
13699 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13700 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13701 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13702#endif
13703
13704 rettv->vval.v_number = id;
13705}
13706
13707/*
13708 * "synIDattr(id, what [, mode])" function
13709 */
13710 static void
13711f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13712{
13713 char_u *p = NULL;
13714#ifdef FEAT_SYN_HL
13715 int id;
13716 char_u *what;
13717 char_u *mode;
13718 char_u modebuf[NUMBUFLEN];
13719 int modec;
13720
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013721 id = (int)tv_get_number(&argvars[0]);
13722 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013723 if (argvars[2].v_type != VAR_UNKNOWN)
13724 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013725 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013726 modec = TOLOWER_ASC(mode[0]);
13727 if (modec != 't' && modec != 'c' && modec != 'g')
13728 modec = 0; /* replace invalid with current */
13729 }
13730 else
13731 {
13732#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13733 if (USE_24BIT)
13734 modec = 'g';
13735 else
13736#endif
13737 if (t_colors > 1)
13738 modec = 'c';
13739 else
13740 modec = 't';
13741 }
13742
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013743 switch (TOLOWER_ASC(what[0]))
13744 {
13745 case 'b':
13746 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13747 p = highlight_color(id, what, modec);
13748 else /* bold */
13749 p = highlight_has_attr(id, HL_BOLD, modec);
13750 break;
13751
13752 case 'f': /* fg[#] or font */
13753 p = highlight_color(id, what, modec);
13754 break;
13755
13756 case 'i':
13757 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13758 p = highlight_has_attr(id, HL_INVERSE, modec);
13759 else /* italic */
13760 p = highlight_has_attr(id, HL_ITALIC, modec);
13761 break;
13762
13763 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013764 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013765 break;
13766
13767 case 'r': /* reverse */
13768 p = highlight_has_attr(id, HL_INVERSE, modec);
13769 break;
13770
13771 case 's':
13772 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13773 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013774 /* strikeout */
13775 else if (TOLOWER_ASC(what[1]) == 't' &&
13776 TOLOWER_ASC(what[2]) == 'r')
13777 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013778 else /* standout */
13779 p = highlight_has_attr(id, HL_STANDOUT, modec);
13780 break;
13781
13782 case 'u':
13783 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13784 /* underline */
13785 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13786 else
13787 /* undercurl */
13788 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13789 break;
13790 }
13791
13792 if (p != NULL)
13793 p = vim_strsave(p);
13794#endif
13795 rettv->v_type = VAR_STRING;
13796 rettv->vval.v_string = p;
13797}
13798
13799/*
13800 * "synIDtrans(id)" function
13801 */
13802 static void
13803f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13804{
13805 int id;
13806
13807#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013808 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013809
13810 if (id > 0)
13811 id = syn_get_final_id(id);
13812 else
13813#endif
13814 id = 0;
13815
13816 rettv->vval.v_number = id;
13817}
13818
13819/*
13820 * "synconcealed(lnum, col)" function
13821 */
13822 static void
13823f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13824{
13825#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13826 linenr_T lnum;
13827 colnr_T col;
13828 int syntax_flags = 0;
13829 int cchar;
13830 int matchid = 0;
13831 char_u str[NUMBUFLEN];
13832#endif
13833
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013834 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013835
13836#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013837 lnum = tv_get_lnum(argvars); /* -1 on type error */
13838 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013839
13840 vim_memset(str, NUL, sizeof(str));
13841
13842 if (rettv_list_alloc(rettv) != FAIL)
13843 {
13844 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13845 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13846 && curwin->w_p_cole > 0)
13847 {
13848 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13849 syntax_flags = get_syntax_info(&matchid);
13850
13851 /* get the conceal character */
13852 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13853 {
13854 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013855 if (cchar == NUL && curwin->w_p_cole == 1)
13856 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013857 if (cchar != NUL)
13858 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013859 if (has_mbyte)
13860 (*mb_char2bytes)(cchar, str);
13861 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013862 str[0] = cchar;
13863 }
13864 }
13865 }
13866
13867 list_append_number(rettv->vval.v_list,
13868 (syntax_flags & HL_CONCEAL) != 0);
13869 /* -1 to auto-determine strlen */
13870 list_append_string(rettv->vval.v_list, str, -1);
13871 list_append_number(rettv->vval.v_list, matchid);
13872 }
13873#endif
13874}
13875
13876/*
13877 * "synstack(lnum, col)" function
13878 */
13879 static void
13880f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13881{
13882#ifdef FEAT_SYN_HL
13883 linenr_T lnum;
13884 colnr_T col;
13885 int i;
13886 int id;
13887#endif
13888
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013889 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013890
13891#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013892 lnum = tv_get_lnum(argvars); /* -1 on type error */
13893 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013894
13895 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13896 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13897 && rettv_list_alloc(rettv) != FAIL)
13898 {
13899 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13900 for (i = 0; ; ++i)
13901 {
13902 id = syn_get_stack_item(i);
13903 if (id < 0)
13904 break;
13905 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13906 break;
13907 }
13908 }
13909#endif
13910}
13911
13912 static void
13913get_cmd_output_as_rettv(
13914 typval_T *argvars,
13915 typval_T *rettv,
13916 int retlist)
13917{
13918 char_u *res = NULL;
13919 char_u *p;
13920 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013921 int err = FALSE;
13922 FILE *fd;
13923 list_T *list = NULL;
13924 int flags = SHELL_SILENT;
13925
13926 rettv->v_type = VAR_STRING;
13927 rettv->vval.v_string = NULL;
13928 if (check_restricted() || check_secure())
13929 goto errret;
13930
13931 if (argvars[1].v_type != VAR_UNKNOWN)
13932 {
13933 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013934 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013935 * command.
13936 */
13937 if ((infile = vim_tempname('i', TRUE)) == NULL)
13938 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013939 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013940 goto errret;
13941 }
13942
13943 fd = mch_fopen((char *)infile, WRITEBIN);
13944 if (fd == NULL)
13945 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013946 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013947 goto errret;
13948 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013949 if (argvars[1].v_type == VAR_NUMBER)
13950 {
13951 linenr_T lnum;
13952 buf_T *buf;
13953
13954 buf = buflist_findnr(argvars[1].vval.v_number);
13955 if (buf == NULL)
13956 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013957 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013958 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013959 goto errret;
13960 }
13961
13962 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13963 {
13964 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13965 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13966 {
13967 err = TRUE;
13968 break;
13969 }
13970 if (putc(NL, fd) == EOF)
13971 {
13972 err = TRUE;
13973 break;
13974 }
13975 }
13976 }
13977 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013978 {
13979 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13980 err = TRUE;
13981 }
13982 else
13983 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013984 size_t len;
13985 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013986
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013987 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013988 if (p == NULL)
13989 {
13990 fclose(fd);
13991 goto errret; /* type error; errmsg already given */
13992 }
13993 len = STRLEN(p);
13994 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13995 err = TRUE;
13996 }
13997 if (fclose(fd) != 0)
13998 err = TRUE;
13999 if (err)
14000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014001 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014002 goto errret;
14003 }
14004 }
14005
14006 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
14007 * echoes typeahead, that messes up the display. */
14008 if (!msg_silent)
14009 flags += SHELL_COOKED;
14010
14011 if (retlist)
14012 {
14013 int len;
14014 listitem_T *li;
14015 char_u *s = NULL;
14016 char_u *start;
14017 char_u *end;
14018 int i;
14019
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014020 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014021 if (res == NULL)
14022 goto errret;
14023
14024 list = list_alloc();
14025 if (list == NULL)
14026 goto errret;
14027
14028 for (i = 0; i < len; ++i)
14029 {
14030 start = res + i;
14031 while (i < len && res[i] != NL)
14032 ++i;
14033 end = res + i;
14034
14035 s = alloc((unsigned)(end - start + 1));
14036 if (s == NULL)
14037 goto errret;
14038
14039 for (p = s; start < end; ++p, ++start)
14040 *p = *start == NUL ? NL : *start;
14041 *p = NUL;
14042
14043 li = listitem_alloc();
14044 if (li == NULL)
14045 {
14046 vim_free(s);
14047 goto errret;
14048 }
14049 li->li_tv.v_type = VAR_STRING;
14050 li->li_tv.v_lock = 0;
14051 li->li_tv.vval.v_string = s;
14052 list_append(list, li);
14053 }
14054
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014055 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014056 list = NULL;
14057 }
14058 else
14059 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014060 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010014061#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014062 /* translate <CR><NL> into <NL> */
14063 if (res != NULL)
14064 {
14065 char_u *s, *d;
14066
14067 d = res;
14068 for (s = res; *s; ++s)
14069 {
14070 if (s[0] == CAR && s[1] == NL)
14071 ++s;
14072 *d++ = *s;
14073 }
14074 *d = NUL;
14075 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014076#endif
14077 rettv->vval.v_string = res;
14078 res = NULL;
14079 }
14080
14081errret:
14082 if (infile != NULL)
14083 {
14084 mch_remove(infile);
14085 vim_free(infile);
14086 }
14087 if (res != NULL)
14088 vim_free(res);
14089 if (list != NULL)
14090 list_free(list);
14091}
14092
14093/*
14094 * "system()" function
14095 */
14096 static void
14097f_system(typval_T *argvars, typval_T *rettv)
14098{
14099 get_cmd_output_as_rettv(argvars, rettv, FALSE);
14100}
14101
14102/*
14103 * "systemlist()" function
14104 */
14105 static void
14106f_systemlist(typval_T *argvars, typval_T *rettv)
14107{
14108 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14109}
14110
14111/*
14112 * "tabpagebuflist()" function
14113 */
14114 static void
14115f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14116{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014117 tabpage_T *tp;
14118 win_T *wp = NULL;
14119
14120 if (argvars[0].v_type == VAR_UNKNOWN)
14121 wp = firstwin;
14122 else
14123 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014124 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014125 if (tp != NULL)
14126 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14127 }
14128 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14129 {
14130 for (; wp != NULL; wp = wp->w_next)
14131 if (list_append_number(rettv->vval.v_list,
14132 wp->w_buffer->b_fnum) == FAIL)
14133 break;
14134 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014135}
14136
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014137/*
14138 * "tabpagenr()" function
14139 */
14140 static void
14141f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14142{
14143 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014144 char_u *arg;
14145
14146 if (argvars[0].v_type != VAR_UNKNOWN)
14147 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014148 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014149 nr = 0;
14150 if (arg != NULL)
14151 {
14152 if (STRCMP(arg, "$") == 0)
14153 nr = tabpage_index(NULL) - 1;
14154 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014155 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014156 }
14157 }
14158 else
14159 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014160 rettv->vval.v_number = nr;
14161}
14162
14163
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014164/*
14165 * Common code for tabpagewinnr() and winnr().
14166 */
14167 static int
14168get_winnr(tabpage_T *tp, typval_T *argvar)
14169{
14170 win_T *twin;
14171 int nr = 1;
14172 win_T *wp;
14173 char_u *arg;
14174
14175 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14176 if (argvar->v_type != VAR_UNKNOWN)
14177 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014178 int invalid_arg = FALSE;
14179
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014180 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014181 if (arg == NULL)
14182 nr = 0; /* type error; errmsg already given */
14183 else if (STRCMP(arg, "$") == 0)
14184 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14185 else if (STRCMP(arg, "#") == 0)
14186 {
14187 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14188 if (twin == NULL)
14189 nr = 0;
14190 }
14191 else
14192 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014193 long count;
14194 char_u *endp;
14195
14196 // Extract the window count (if specified). e.g. winnr('3j')
14197 count = strtol((char *)arg, (char **)&endp, 10);
14198 if (count <= 0)
14199 count = 1; // if count is not specified, default to 1
14200 if (endp != NULL && *endp != '\0')
14201 {
14202 if (STRCMP(endp, "j") == 0)
14203 twin = win_vert_neighbor(tp, twin, FALSE, count);
14204 else if (STRCMP(endp, "k") == 0)
14205 twin = win_vert_neighbor(tp, twin, TRUE, count);
14206 else if (STRCMP(endp, "h") == 0)
14207 twin = win_horz_neighbor(tp, twin, TRUE, count);
14208 else if (STRCMP(endp, "l") == 0)
14209 twin = win_horz_neighbor(tp, twin, FALSE, count);
14210 else
14211 invalid_arg = TRUE;
14212 }
14213 else
14214 invalid_arg = TRUE;
14215 }
14216
14217 if (invalid_arg)
14218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014219 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014220 nr = 0;
14221 }
14222 }
14223
14224 if (nr > 0)
14225 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14226 wp != twin; wp = wp->w_next)
14227 {
14228 if (wp == NULL)
14229 {
14230 /* didn't find it in this tabpage */
14231 nr = 0;
14232 break;
14233 }
14234 ++nr;
14235 }
14236 return nr;
14237}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014238
14239/*
14240 * "tabpagewinnr()" function
14241 */
14242 static void
14243f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14244{
14245 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014246 tabpage_T *tp;
14247
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014248 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014249 if (tp == NULL)
14250 nr = 0;
14251 else
14252 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014253 rettv->vval.v_number = nr;
14254}
14255
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014256/*
14257 * "tagfiles()" function
14258 */
14259 static void
14260f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14261{
14262 char_u *fname;
14263 tagname_T tn;
14264 int first;
14265
14266 if (rettv_list_alloc(rettv) == FAIL)
14267 return;
14268 fname = alloc(MAXPATHL);
14269 if (fname == NULL)
14270 return;
14271
14272 for (first = TRUE; ; first = FALSE)
14273 if (get_tagfname(&tn, first, fname) == FAIL
14274 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14275 break;
14276 tagname_free(&tn);
14277 vim_free(fname);
14278}
14279
14280/*
14281 * "taglist()" function
14282 */
14283 static void
14284f_taglist(typval_T *argvars, typval_T *rettv)
14285{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014286 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014287 char_u *tag_pattern;
14288
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014289 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014290
14291 rettv->vval.v_number = FALSE;
14292 if (*tag_pattern == NUL)
14293 return;
14294
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014295 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014296 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014297 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014298 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014299}
14300
14301/*
14302 * "tempname()" function
14303 */
14304 static void
14305f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14306{
14307 static int x = 'A';
14308
14309 rettv->v_type = VAR_STRING;
14310 rettv->vval.v_string = vim_tempname(x, FALSE);
14311
14312 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14313 * names. Skip 'I' and 'O', they are used for shell redirection. */
14314 do
14315 {
14316 if (x == 'Z')
14317 x = '0';
14318 else if (x == '9')
14319 x = 'A';
14320 else
14321 {
14322#ifdef EBCDIC
14323 if (x == 'I')
14324 x = 'J';
14325 else if (x == 'R')
14326 x = 'S';
14327 else
14328#endif
14329 ++x;
14330 }
14331 } while (x == 'I' || x == 'O');
14332}
14333
14334#ifdef FEAT_FLOAT
14335/*
14336 * "tan()" function
14337 */
14338 static void
14339f_tan(typval_T *argvars, typval_T *rettv)
14340{
14341 float_T f = 0.0;
14342
14343 rettv->v_type = VAR_FLOAT;
14344 if (get_float_arg(argvars, &f) == OK)
14345 rettv->vval.v_float = tan(f);
14346 else
14347 rettv->vval.v_float = 0.0;
14348}
14349
14350/*
14351 * "tanh()" function
14352 */
14353 static void
14354f_tanh(typval_T *argvars, typval_T *rettv)
14355{
14356 float_T f = 0.0;
14357
14358 rettv->v_type = VAR_FLOAT;
14359 if (get_float_arg(argvars, &f) == OK)
14360 rettv->vval.v_float = tanh(f);
14361 else
14362 rettv->vval.v_float = 0.0;
14363}
14364#endif
14365
14366/*
14367 * "test_alloc_fail(id, countdown, repeat)" function
14368 */
14369 static void
14370f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14371{
14372 if (argvars[0].v_type != VAR_NUMBER
14373 || argvars[0].vval.v_number <= 0
14374 || argvars[1].v_type != VAR_NUMBER
14375 || argvars[1].vval.v_number < 0
14376 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014377 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014378 else
14379 {
14380 alloc_fail_id = argvars[0].vval.v_number;
14381 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014382 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014383 alloc_fail_countdown = argvars[1].vval.v_number;
14384 alloc_fail_repeat = argvars[2].vval.v_number;
14385 did_outofmem_msg = FALSE;
14386 }
14387}
14388
14389/*
14390 * "test_autochdir()"
14391 */
14392 static void
14393f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14394{
14395#if defined(FEAT_AUTOCHDIR)
14396 test_autochdir = TRUE;
14397#endif
14398}
14399
14400/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014401 * "test_feedinput()"
14402 */
14403 static void
14404f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14405{
14406#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014407 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014408
14409 if (val != NULL)
14410 {
14411 trash_input_buf();
14412 add_to_input_buf_csi(val, (int)STRLEN(val));
14413 }
14414#endif
14415}
14416
14417/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014418 * "test_getvalue({name})" function
14419 */
14420 static void
14421f_test_getvalue(typval_T *argvars, typval_T *rettv)
14422{
14423 if (argvars[0].v_type != VAR_STRING)
14424 emsg(_(e_invarg));
14425 else
14426 {
14427 char_u *name = tv_get_string(&argvars[0]);
14428
14429 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14430 rettv->vval.v_number = need_fileinfo;
14431 else
14432 semsg(_(e_invarg2), name);
14433 }
14434}
14435
14436/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014437 * "test_option_not_set({name})" function
14438 */
14439 static void
14440f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14441{
14442 char_u *name = (char_u *)"";
14443
14444 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014445 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014446 else
14447 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014448 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014449 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014450 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014451 }
14452}
14453
14454/*
14455 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014456 */
14457 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014458f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014459{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014460 char_u *name = (char_u *)"";
14461 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014462 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014463
14464 if (argvars[0].v_type != VAR_STRING
14465 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014466 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014467 else
14468 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014469 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014470 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014471
14472 if (STRCMP(name, (char_u *)"redraw") == 0)
14473 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014474 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14475 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014476 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14477 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014478 else if (STRCMP(name, (char_u *)"starting") == 0)
14479 {
14480 if (val)
14481 {
14482 if (save_starting < 0)
14483 save_starting = starting;
14484 starting = 0;
14485 }
14486 else
14487 {
14488 starting = save_starting;
14489 save_starting = -1;
14490 }
14491 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014492 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14493 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014494 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14495 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014496 else if (STRCMP(name, (char_u *)"ALL") == 0)
14497 {
14498 disable_char_avail_for_testing = FALSE;
14499 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014500 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014501 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014502 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014503 if (save_starting >= 0)
14504 {
14505 starting = save_starting;
14506 save_starting = -1;
14507 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014508 }
14509 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014510 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014511 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014512}
14513
14514/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014515 * "test_refcount({expr})" function
14516 */
14517 static void
14518f_test_refcount(typval_T *argvars, typval_T *rettv)
14519{
14520 int retval = -1;
14521
14522 switch (argvars[0].v_type)
14523 {
14524 case VAR_UNKNOWN:
14525 case VAR_NUMBER:
14526 case VAR_FLOAT:
14527 case VAR_SPECIAL:
14528 case VAR_STRING:
14529 break;
14530 case VAR_JOB:
14531#ifdef FEAT_JOB_CHANNEL
14532 if (argvars[0].vval.v_job != NULL)
14533 retval = argvars[0].vval.v_job->jv_refcount - 1;
14534#endif
14535 break;
14536 case VAR_CHANNEL:
14537#ifdef FEAT_JOB_CHANNEL
14538 if (argvars[0].vval.v_channel != NULL)
14539 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14540#endif
14541 break;
14542 case VAR_FUNC:
14543 if (argvars[0].vval.v_string != NULL)
14544 {
14545 ufunc_T *fp;
14546
14547 fp = find_func(argvars[0].vval.v_string);
14548 if (fp != NULL)
14549 retval = fp->uf_refcount;
14550 }
14551 break;
14552 case VAR_PARTIAL:
14553 if (argvars[0].vval.v_partial != NULL)
14554 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14555 break;
14556 case VAR_BLOB:
14557 if (argvars[0].vval.v_blob != NULL)
14558 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14559 break;
14560 case VAR_LIST:
14561 if (argvars[0].vval.v_list != NULL)
14562 retval = argvars[0].vval.v_list->lv_refcount - 1;
14563 break;
14564 case VAR_DICT:
14565 if (argvars[0].vval.v_dict != NULL)
14566 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14567 break;
14568 }
14569
14570 rettv->v_type = VAR_NUMBER;
14571 rettv->vval.v_number = retval;
14572
14573}
14574
14575/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014576 * "test_garbagecollect_now()" function
14577 */
14578 static void
14579f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14580{
14581 /* This is dangerous, any Lists and Dicts used internally may be freed
14582 * while still in use. */
14583 garbage_collect(TRUE);
14584}
14585
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014586/*
14587 * "test_ignore_error()" function
14588 */
14589 static void
14590f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14591{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014592 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014593}
14594
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014595 static void
14596f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14597{
14598 rettv->v_type = VAR_BLOB;
14599 rettv->vval.v_blob = NULL;
14600}
14601
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014602#ifdef FEAT_JOB_CHANNEL
14603 static void
14604f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14605{
14606 rettv->v_type = VAR_CHANNEL;
14607 rettv->vval.v_channel = NULL;
14608}
14609#endif
14610
14611 static void
14612f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14613{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014614 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014615}
14616
14617#ifdef FEAT_JOB_CHANNEL
14618 static void
14619f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14620{
14621 rettv->v_type = VAR_JOB;
14622 rettv->vval.v_job = NULL;
14623}
14624#endif
14625
14626 static void
14627f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14628{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014629 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014630}
14631
14632 static void
14633f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14634{
14635 rettv->v_type = VAR_PARTIAL;
14636 rettv->vval.v_partial = NULL;
14637}
14638
14639 static void
14640f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14641{
14642 rettv->v_type = VAR_STRING;
14643 rettv->vval.v_string = NULL;
14644}
14645
Bram Moolenaarab186732018-09-14 21:27:06 +020014646#ifdef FEAT_GUI
14647 static void
14648f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14649{
14650 char_u *which;
14651 long value;
14652 int dragging;
14653 scrollbar_T *sb = NULL;
14654
14655 if (argvars[0].v_type != VAR_STRING
14656 || (argvars[1].v_type) != VAR_NUMBER
14657 || (argvars[2].v_type) != VAR_NUMBER)
14658 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014659 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014660 return;
14661 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014662 which = tv_get_string(&argvars[0]);
14663 value = tv_get_number(&argvars[1]);
14664 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014665
14666 if (STRCMP(which, "left") == 0)
14667 sb = &curwin->w_scrollbars[SBAR_LEFT];
14668 else if (STRCMP(which, "right") == 0)
14669 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14670 else if (STRCMP(which, "hor") == 0)
14671 sb = &gui.bottom_sbar;
14672 if (sb == NULL)
14673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014674 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014675 return;
14676 }
14677 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014678# ifndef USE_ON_FLY_SCROLL
14679 // need to loop through normal_cmd() to handle the scroll events
14680 exec_normal(FALSE, TRUE, FALSE);
14681# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014682}
14683#endif
14684
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014685#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014686 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014687f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14688{
14689 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14690 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14691}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014692#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014693
14694 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014695f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14696{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014697 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014698}
14699
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014700/*
14701 * Get a callback from "arg". It can be a Funcref or a function name.
14702 * When "arg" is zero return an empty string.
14703 * Return NULL for an invalid argument.
14704 */
14705 char_u *
14706get_callback(typval_T *arg, partial_T **pp)
14707{
14708 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14709 {
14710 *pp = arg->vval.v_partial;
14711 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014712 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014713 }
14714 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014715 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014716 {
14717 func_ref(arg->vval.v_string);
14718 return arg->vval.v_string;
14719 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014720 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14721 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014722 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014723 return NULL;
14724}
14725
14726/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014727 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014728 */
14729 void
14730free_callback(char_u *callback, partial_T *partial)
14731{
14732 if (partial != NULL)
14733 partial_unref(partial);
14734 else if (callback != NULL)
14735 {
14736 func_unref(callback);
14737 vim_free(callback);
14738 }
14739}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014740
14741#ifdef FEAT_TIMERS
14742/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014743 * "timer_info([timer])" function
14744 */
14745 static void
14746f_timer_info(typval_T *argvars, typval_T *rettv)
14747{
14748 timer_T *timer = NULL;
14749
14750 if (rettv_list_alloc(rettv) != OK)
14751 return;
14752 if (argvars[0].v_type != VAR_UNKNOWN)
14753 {
14754 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014755 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014756 else
14757 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014758 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014759 if (timer != NULL)
14760 add_timer_info(rettv, timer);
14761 }
14762 }
14763 else
14764 add_timer_info_all(rettv);
14765}
14766
14767/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014768 * "timer_pause(timer, paused)" function
14769 */
14770 static void
14771f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14772{
14773 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014774 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014775
14776 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014777 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014778 else
14779 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014780 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014781 if (timer != NULL)
14782 timer->tr_paused = paused;
14783 }
14784}
14785
14786/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014787 * "timer_start(time, callback [, options])" function
14788 */
14789 static void
14790f_timer_start(typval_T *argvars, typval_T *rettv)
14791{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014792 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014793 timer_T *timer;
14794 int repeat = 0;
14795 char_u *callback;
14796 dict_T *dict;
14797 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014798
Bram Moolenaar75537a92016-09-05 22:45:28 +020014799 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014800 if (check_secure())
14801 return;
14802 if (argvars[2].v_type != VAR_UNKNOWN)
14803 {
14804 if (argvars[2].v_type != VAR_DICT
14805 || (dict = argvars[2].vval.v_dict) == NULL)
14806 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014807 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014808 return;
14809 }
14810 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014811 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014812 }
14813
Bram Moolenaar75537a92016-09-05 22:45:28 +020014814 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014815 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014816 return;
14817
14818 timer = create_timer(msec, repeat);
14819 if (timer == NULL)
14820 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014821 else
14822 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014823 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014824 timer->tr_callback = vim_strsave(callback);
14825 else
14826 /* pointer into the partial */
14827 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014828 timer->tr_partial = partial;
14829 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014830 }
14831}
14832
14833/*
14834 * "timer_stop(timer)" function
14835 */
14836 static void
14837f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14838{
14839 timer_T *timer;
14840
14841 if (argvars[0].v_type != VAR_NUMBER)
14842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014843 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014844 return;
14845 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014846 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014847 if (timer != NULL)
14848 stop_timer(timer);
14849}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014850
14851/*
14852 * "timer_stopall()" function
14853 */
14854 static void
14855f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14856{
14857 stop_all_timers();
14858}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014859#endif
14860
14861/*
14862 * "tolower(string)" function
14863 */
14864 static void
14865f_tolower(typval_T *argvars, typval_T *rettv)
14866{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014867 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014868 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014869}
14870
14871/*
14872 * "toupper(string)" function
14873 */
14874 static void
14875f_toupper(typval_T *argvars, typval_T *rettv)
14876{
14877 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014878 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014879}
14880
14881/*
14882 * "tr(string, fromstr, tostr)" function
14883 */
14884 static void
14885f_tr(typval_T *argvars, typval_T *rettv)
14886{
14887 char_u *in_str;
14888 char_u *fromstr;
14889 char_u *tostr;
14890 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014891 int inlen;
14892 int fromlen;
14893 int tolen;
14894 int idx;
14895 char_u *cpstr;
14896 int cplen;
14897 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014898 char_u buf[NUMBUFLEN];
14899 char_u buf2[NUMBUFLEN];
14900 garray_T ga;
14901
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014902 in_str = tv_get_string(&argvars[0]);
14903 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14904 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014905
14906 /* Default return value: empty string. */
14907 rettv->v_type = VAR_STRING;
14908 rettv->vval.v_string = NULL;
14909 if (fromstr == NULL || tostr == NULL)
14910 return; /* type error; errmsg already given */
14911 ga_init2(&ga, (int)sizeof(char), 80);
14912
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014913 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014914 /* not multi-byte: fromstr and tostr must be the same length */
14915 if (STRLEN(fromstr) != STRLEN(tostr))
14916 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014917error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014918 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014919 ga_clear(&ga);
14920 return;
14921 }
14922
14923 /* fromstr and tostr have to contain the same number of chars */
14924 while (*in_str != NUL)
14925 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014926 if (has_mbyte)
14927 {
14928 inlen = (*mb_ptr2len)(in_str);
14929 cpstr = in_str;
14930 cplen = inlen;
14931 idx = 0;
14932 for (p = fromstr; *p != NUL; p += fromlen)
14933 {
14934 fromlen = (*mb_ptr2len)(p);
14935 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14936 {
14937 for (p = tostr; *p != NUL; p += tolen)
14938 {
14939 tolen = (*mb_ptr2len)(p);
14940 if (idx-- == 0)
14941 {
14942 cplen = tolen;
14943 cpstr = p;
14944 break;
14945 }
14946 }
14947 if (*p == NUL) /* tostr is shorter than fromstr */
14948 goto error;
14949 break;
14950 }
14951 ++idx;
14952 }
14953
14954 if (first && cpstr == in_str)
14955 {
14956 /* Check that fromstr and tostr have the same number of
14957 * (multi-byte) characters. Done only once when a character
14958 * of in_str doesn't appear in fromstr. */
14959 first = FALSE;
14960 for (p = tostr; *p != NUL; p += tolen)
14961 {
14962 tolen = (*mb_ptr2len)(p);
14963 --idx;
14964 }
14965 if (idx != 0)
14966 goto error;
14967 }
14968
14969 (void)ga_grow(&ga, cplen);
14970 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14971 ga.ga_len += cplen;
14972
14973 in_str += inlen;
14974 }
14975 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014976 {
14977 /* When not using multi-byte chars we can do it faster. */
14978 p = vim_strchr(fromstr, *in_str);
14979 if (p != NULL)
14980 ga_append(&ga, tostr[p - fromstr]);
14981 else
14982 ga_append(&ga, *in_str);
14983 ++in_str;
14984 }
14985 }
14986
14987 /* add a terminating NUL */
14988 (void)ga_grow(&ga, 1);
14989 ga_append(&ga, NUL);
14990
14991 rettv->vval.v_string = ga.ga_data;
14992}
14993
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014994/*
14995 * "trim({expr})" function
14996 */
14997 static void
14998f_trim(typval_T *argvars, typval_T *rettv)
14999{
15000 char_u buf1[NUMBUFLEN];
15001 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015002 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015003 char_u *mask = NULL;
15004 char_u *tail;
15005 char_u *prev;
15006 char_u *p;
15007 int c1;
15008
15009 rettv->v_type = VAR_STRING;
15010 if (head == NULL)
15011 {
15012 rettv->vval.v_string = NULL;
15013 return;
15014 }
15015
15016 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015017 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015018
15019 while (*head != NUL)
15020 {
15021 c1 = PTR2CHAR(head);
15022 if (mask == NULL)
15023 {
15024 if (c1 > ' ' && c1 != 0xa0)
15025 break;
15026 }
15027 else
15028 {
15029 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15030 if (c1 == PTR2CHAR(p))
15031 break;
15032 if (*p == NUL)
15033 break;
15034 }
15035 MB_PTR_ADV(head);
15036 }
15037
15038 for (tail = head + STRLEN(head); tail > head; tail = prev)
15039 {
15040 prev = tail;
15041 MB_PTR_BACK(head, prev);
15042 c1 = PTR2CHAR(prev);
15043 if (mask == NULL)
15044 {
15045 if (c1 > ' ' && c1 != 0xa0)
15046 break;
15047 }
15048 else
15049 {
15050 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15051 if (c1 == PTR2CHAR(p))
15052 break;
15053 if (*p == NUL)
15054 break;
15055 }
15056 }
15057 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
15058}
15059
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015060#ifdef FEAT_FLOAT
15061/*
15062 * "trunc({float})" function
15063 */
15064 static void
15065f_trunc(typval_T *argvars, typval_T *rettv)
15066{
15067 float_T f = 0.0;
15068
15069 rettv->v_type = VAR_FLOAT;
15070 if (get_float_arg(argvars, &f) == OK)
15071 /* trunc() is not in C90, use floor() or ceil() instead. */
15072 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
15073 else
15074 rettv->vval.v_float = 0.0;
15075}
15076#endif
15077
15078/*
15079 * "type(expr)" function
15080 */
15081 static void
15082f_type(typval_T *argvars, typval_T *rettv)
15083{
15084 int n = -1;
15085
15086 switch (argvars[0].v_type)
15087 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020015088 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
15089 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015090 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020015091 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
15092 case VAR_LIST: n = VAR_TYPE_LIST; break;
15093 case VAR_DICT: n = VAR_TYPE_DICT; break;
15094 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015095 case VAR_SPECIAL:
15096 if (argvars[0].vval.v_number == VVAL_FALSE
15097 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020015098 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015099 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020015100 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015101 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020015102 case VAR_JOB: n = VAR_TYPE_JOB; break;
15103 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015104 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015105 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015106 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015107 n = -1;
15108 break;
15109 }
15110 rettv->vval.v_number = n;
15111}
15112
15113/*
15114 * "undofile(name)" function
15115 */
15116 static void
15117f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15118{
15119 rettv->v_type = VAR_STRING;
15120#ifdef FEAT_PERSISTENT_UNDO
15121 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015122 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015123
15124 if (*fname == NUL)
15125 {
15126 /* If there is no file name there will be no undo file. */
15127 rettv->vval.v_string = NULL;
15128 }
15129 else
15130 {
15131 char_u *ffname = FullName_save(fname, FALSE);
15132
15133 if (ffname != NULL)
15134 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15135 vim_free(ffname);
15136 }
15137 }
15138#else
15139 rettv->vval.v_string = NULL;
15140#endif
15141}
15142
15143/*
15144 * "undotree()" function
15145 */
15146 static void
15147f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15148{
15149 if (rettv_dict_alloc(rettv) == OK)
15150 {
15151 dict_T *dict = rettv->vval.v_dict;
15152 list_T *list;
15153
Bram Moolenaare0be1672018-07-08 16:50:37 +020015154 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15155 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15156 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15157 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15158 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15159 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015160
15161 list = list_alloc();
15162 if (list != NULL)
15163 {
15164 u_eval_tree(curbuf->b_u_oldhead, list);
15165 dict_add_list(dict, "entries", list);
15166 }
15167 }
15168}
15169
15170/*
15171 * "values(dict)" function
15172 */
15173 static void
15174f_values(typval_T *argvars, typval_T *rettv)
15175{
15176 dict_list(argvars, rettv, 1);
15177}
15178
15179/*
15180 * "virtcol(string)" function
15181 */
15182 static void
15183f_virtcol(typval_T *argvars, typval_T *rettv)
15184{
15185 colnr_T vcol = 0;
15186 pos_T *fp;
15187 int fnum = curbuf->b_fnum;
15188
15189 fp = var2fpos(&argvars[0], FALSE, &fnum);
15190 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15191 && fnum == curbuf->b_fnum)
15192 {
15193 getvvcol(curwin, fp, NULL, NULL, &vcol);
15194 ++vcol;
15195 }
15196
15197 rettv->vval.v_number = vcol;
15198}
15199
15200/*
15201 * "visualmode()" function
15202 */
15203 static void
15204f_visualmode(typval_T *argvars, typval_T *rettv)
15205{
15206 char_u str[2];
15207
15208 rettv->v_type = VAR_STRING;
15209 str[0] = curbuf->b_visual_mode_eval;
15210 str[1] = NUL;
15211 rettv->vval.v_string = vim_strsave(str);
15212
15213 /* A non-zero number or non-empty string argument: reset mode. */
15214 if (non_zero_arg(&argvars[0]))
15215 curbuf->b_visual_mode_eval = NUL;
15216}
15217
15218/*
15219 * "wildmenumode()" function
15220 */
15221 static void
15222f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15223{
15224#ifdef FEAT_WILDMENU
15225 if (wild_menu_showing)
15226 rettv->vval.v_number = 1;
15227#endif
15228}
15229
15230/*
15231 * "winbufnr(nr)" function
15232 */
15233 static void
15234f_winbufnr(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_buffer->b_fnum;
15243}
15244
15245/*
15246 * "wincol()" function
15247 */
15248 static void
15249f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15250{
15251 validate_cursor();
15252 rettv->vval.v_number = curwin->w_wcol + 1;
15253}
15254
15255/*
15256 * "winheight(nr)" function
15257 */
15258 static void
15259f_winheight(typval_T *argvars, typval_T *rettv)
15260{
15261 win_T *wp;
15262
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015263 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015264 if (wp == NULL)
15265 rettv->vval.v_number = -1;
15266 else
15267 rettv->vval.v_number = wp->w_height;
15268}
15269
15270/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015271 * "winlayout()" function
15272 */
15273 static void
15274f_winlayout(typval_T *argvars, typval_T *rettv)
15275{
15276 tabpage_T *tp;
15277
15278 if (rettv_list_alloc(rettv) != OK)
15279 return;
15280
15281 if (argvars[0].v_type == VAR_UNKNOWN)
15282 tp = curtab;
15283 else
15284 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015285 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015286 if (tp == NULL)
15287 return;
15288 }
15289
15290 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15291}
15292
15293/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015294 * "winline()" function
15295 */
15296 static void
15297f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15298{
15299 validate_cursor();
15300 rettv->vval.v_number = curwin->w_wrow + 1;
15301}
15302
15303/*
15304 * "winnr()" function
15305 */
15306 static void
15307f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15308{
15309 int nr = 1;
15310
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015311 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015312 rettv->vval.v_number = nr;
15313}
15314
15315/*
15316 * "winrestcmd()" function
15317 */
15318 static void
15319f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15320{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015321 win_T *wp;
15322 int winnr = 1;
15323 garray_T ga;
15324 char_u buf[50];
15325
15326 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015327 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015328 {
15329 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15330 ga_concat(&ga, buf);
15331 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15332 ga_concat(&ga, buf);
15333 ++winnr;
15334 }
15335 ga_append(&ga, NUL);
15336
15337 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015338 rettv->v_type = VAR_STRING;
15339}
15340
15341/*
15342 * "winrestview()" function
15343 */
15344 static void
15345f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15346{
15347 dict_T *dict;
15348
15349 if (argvars[0].v_type != VAR_DICT
15350 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015351 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015352 else
15353 {
15354 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015355 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015356 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015357 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015358 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015359 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015360 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15361 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015362 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015363 curwin->w_set_curswant = FALSE;
15364 }
15365
15366 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015367 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015368#ifdef FEAT_DIFF
15369 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015370 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015371#endif
15372 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015373 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015374 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015375 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015376
15377 check_cursor();
15378 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015379 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015380 changed_window_setting();
15381
15382 if (curwin->w_topline <= 0)
15383 curwin->w_topline = 1;
15384 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15385 curwin->w_topline = curbuf->b_ml.ml_line_count;
15386#ifdef FEAT_DIFF
15387 check_topfill(curwin, TRUE);
15388#endif
15389 }
15390}
15391
15392/*
15393 * "winsaveview()" function
15394 */
15395 static void
15396f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15397{
15398 dict_T *dict;
15399
15400 if (rettv_dict_alloc(rettv) == FAIL)
15401 return;
15402 dict = rettv->vval.v_dict;
15403
Bram Moolenaare0be1672018-07-08 16:50:37 +020015404 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15405 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015406 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015407 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015408 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015409
Bram Moolenaare0be1672018-07-08 16:50:37 +020015410 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015411#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015412 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015413#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015414 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15415 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015416}
15417
15418/*
15419 * "winwidth(nr)" function
15420 */
15421 static void
15422f_winwidth(typval_T *argvars, typval_T *rettv)
15423{
15424 win_T *wp;
15425
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015426 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015427 if (wp == NULL)
15428 rettv->vval.v_number = -1;
15429 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015430 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015431}
15432
15433/*
15434 * "wordcount()" function
15435 */
15436 static void
15437f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15438{
15439 if (rettv_dict_alloc(rettv) == FAIL)
15440 return;
15441 cursor_pos_info(rettv->vval.v_dict);
15442}
15443
15444/*
15445 * "writefile()" function
15446 */
15447 static void
15448f_writefile(typval_T *argvars, typval_T *rettv)
15449{
15450 int binary = FALSE;
15451 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015452#ifdef HAVE_FSYNC
15453 int do_fsync = p_fs;
15454#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015455 char_u *fname;
15456 FILE *fd;
15457 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015458 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015459 list_T *list = NULL;
15460 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015461
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015462 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015463 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015464 return;
15465
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015466 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015467 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015468 list = argvars[0].vval.v_list;
15469 if (list == NULL)
15470 return;
15471 for (li = list->lv_first; li != NULL; li = li->li_next)
15472 if (tv_get_string_chk(&li->li_tv) == NULL)
15473 return;
15474 }
15475 else if (argvars[0].v_type == VAR_BLOB)
15476 {
15477 blob = argvars[0].vval.v_blob;
15478 if (blob == NULL)
15479 return;
15480 }
15481 else
15482 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015483 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015484 return;
15485 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015486
15487 if (argvars[2].v_type != VAR_UNKNOWN)
15488 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015489 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015490
15491 if (arg2 == NULL)
15492 return;
15493 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015494 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015495 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015496 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015497#ifdef HAVE_FSYNC
15498 if (vim_strchr(arg2, 's') != NULL)
15499 do_fsync = TRUE;
15500 else if (vim_strchr(arg2, 'S') != NULL)
15501 do_fsync = FALSE;
15502#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015503 }
15504
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015505 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015506 if (fname == NULL)
15507 return;
15508
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015509 /* Always open the file in binary mode, library functions have a mind of
15510 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015511 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15512 append ? APPENDBIN : WRITEBIN)) == NULL)
15513 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015514 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015515 ret = -1;
15516 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015517 else if (blob)
15518 {
15519 if (write_blob(fd, blob) == FAIL)
15520 ret = -1;
15521#ifdef HAVE_FSYNC
15522 else if (do_fsync)
15523 // Ignore the error, the user wouldn't know what to do about it.
15524 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015525 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015526#endif
15527 fclose(fd);
15528 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015529 else
15530 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015531 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015532 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015533#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015534 else if (do_fsync)
15535 /* Ignore the error, the user wouldn't know what to do about it.
15536 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015537 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015538#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015539 fclose(fd);
15540 }
15541
15542 rettv->vval.v_number = ret;
15543}
15544
15545/*
15546 * "xor(expr, expr)" function
15547 */
15548 static void
15549f_xor(typval_T *argvars, typval_T *rettv)
15550{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015551 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15552 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015553}
15554
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015555#endif /* FEAT_EVAL */