blob: b15e74d438b6ac3ed89709d73813824bdeeb657a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010032static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033static char *e_stringreq = N_("E928: String required");
Bram Moolenaaraff74912019-03-30 18:11:49 +010034static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020035
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020066static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010067static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010069static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010070# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010071#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_browse(typval_T *argvars, typval_T *rettv);
73static void f_browsedir(typval_T *argvars, typval_T *rettv);
74static void f_bufexists(typval_T *argvars, typval_T *rettv);
75static void f_buflisted(typval_T *argvars, typval_T *rettv);
76static void f_bufloaded(typval_T *argvars, typval_T *rettv);
77static void f_bufname(typval_T *argvars, typval_T *rettv);
78static void f_bufnr(typval_T *argvars, typval_T *rettv);
79static void f_bufwinid(typval_T *argvars, typval_T *rettv);
80static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
81static void f_byte2line(typval_T *argvars, typval_T *rettv);
82static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
83static void f_byteidx(typval_T *argvars, typval_T *rettv);
84static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
85static void f_call(typval_T *argvars, typval_T *rettv);
86#ifdef FEAT_FLOAT
87static void f_ceil(typval_T *argvars, typval_T *rettv);
88#endif
89#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010090static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020092static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
94static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
95static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
96static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
97static void f_ch_info(typval_T *argvars, typval_T *rettv);
98static void f_ch_log(typval_T *argvars, typval_T *rettv);
99static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
100static void f_ch_open(typval_T *argvars, typval_T *rettv);
101static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100102static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200103static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
105static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
106static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
107static void f_ch_status(typval_T *argvars, typval_T *rettv);
108#endif
109static void f_changenr(typval_T *argvars, typval_T *rettv);
110static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200111static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200112static void f_cindent(typval_T *argvars, typval_T *rettv);
113static void f_clearmatches(typval_T *argvars, typval_T *rettv);
114static void f_col(typval_T *argvars, typval_T *rettv);
115#if defined(FEAT_INS_EXPAND)
116static void f_complete(typval_T *argvars, typval_T *rettv);
117static void f_complete_add(typval_T *argvars, typval_T *rettv);
118static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +0100119static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120#endif
121static void f_confirm(typval_T *argvars, typval_T *rettv);
122static void f_copy(typval_T *argvars, typval_T *rettv);
123#ifdef FEAT_FLOAT
124static void f_cos(typval_T *argvars, typval_T *rettv);
125static void f_cosh(typval_T *argvars, typval_T *rettv);
126#endif
127static void f_count(typval_T *argvars, typval_T *rettv);
128static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
129static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100130#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200131static void f_debugbreak(typval_T *argvars, typval_T *rettv);
132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_deepcopy(typval_T *argvars, typval_T *rettv);
134static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200135static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200136static void f_did_filetype(typval_T *argvars, typval_T *rettv);
137static void f_diff_filler(typval_T *argvars, typval_T *rettv);
138static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
139static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200140static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_escape(typval_T *argvars, typval_T *rettv);
142static void f_eval(typval_T *argvars, typval_T *rettv);
143static void f_eventhandler(typval_T *argvars, typval_T *rettv);
144static void f_executable(typval_T *argvars, typval_T *rettv);
145static void f_execute(typval_T *argvars, typval_T *rettv);
146static void f_exepath(typval_T *argvars, typval_T *rettv);
147static void f_exists(typval_T *argvars, typval_T *rettv);
148#ifdef FEAT_FLOAT
149static void f_exp(typval_T *argvars, typval_T *rettv);
150#endif
151static void f_expand(typval_T *argvars, typval_T *rettv);
152static void f_extend(typval_T *argvars, typval_T *rettv);
153static void f_feedkeys(typval_T *argvars, typval_T *rettv);
154static void f_filereadable(typval_T *argvars, typval_T *rettv);
155static void f_filewritable(typval_T *argvars, typval_T *rettv);
156static void f_filter(typval_T *argvars, typval_T *rettv);
157static void f_finddir(typval_T *argvars, typval_T *rettv);
158static void f_findfile(typval_T *argvars, typval_T *rettv);
159#ifdef FEAT_FLOAT
160static void f_float2nr(typval_T *argvars, typval_T *rettv);
161static void f_floor(typval_T *argvars, typval_T *rettv);
162static void f_fmod(typval_T *argvars, typval_T *rettv);
163#endif
164static void f_fnameescape(typval_T *argvars, typval_T *rettv);
165static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
166static void f_foldclosed(typval_T *argvars, typval_T *rettv);
167static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
168static void f_foldlevel(typval_T *argvars, typval_T *rettv);
169static void f_foldtext(typval_T *argvars, typval_T *rettv);
170static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
171static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200172static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200173static void f_function(typval_T *argvars, typval_T *rettv);
174static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
175static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200176static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_getbufline(typval_T *argvars, typval_T *rettv);
178static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100179static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_getchar(typval_T *argvars, typval_T *rettv);
181static void f_getcharmod(typval_T *argvars, typval_T *rettv);
182static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
183static void f_getcmdline(typval_T *argvars, typval_T *rettv);
184#if defined(FEAT_CMDL_COMPL)
185static void f_getcompletion(typval_T *argvars, typval_T *rettv);
186#endif
187static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
188static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
189static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
190static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200191static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getfontname(typval_T *argvars, typval_T *rettv);
193static void f_getfperm(typval_T *argvars, typval_T *rettv);
194static void f_getfsize(typval_T *argvars, typval_T *rettv);
195static void f_getftime(typval_T *argvars, typval_T *rettv);
196static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100197static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200198static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200199static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_getmatches(typval_T *argvars, typval_T *rettv);
201static void f_getpid(typval_T *argvars, typval_T *rettv);
202static void f_getcurpos(typval_T *argvars, typval_T *rettv);
203static void f_getpos(typval_T *argvars, typval_T *rettv);
204static void f_getqflist(typval_T *argvars, typval_T *rettv);
205static void f_getreg(typval_T *argvars, typval_T *rettv);
206static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200207static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200208static void f_gettabvar(typval_T *argvars, typval_T *rettv);
209static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100210static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200211static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100212static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200213static void f_getwinposx(typval_T *argvars, typval_T *rettv);
214static void f_getwinposy(typval_T *argvars, typval_T *rettv);
215static void f_getwinvar(typval_T *argvars, typval_T *rettv);
216static void f_glob(typval_T *argvars, typval_T *rettv);
217static void f_globpath(typval_T *argvars, typval_T *rettv);
218static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
219static void f_has(typval_T *argvars, typval_T *rettv);
220static void f_has_key(typval_T *argvars, typval_T *rettv);
221static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
222static void f_hasmapto(typval_T *argvars, typval_T *rettv);
223static void f_histadd(typval_T *argvars, typval_T *rettv);
224static void f_histdel(typval_T *argvars, typval_T *rettv);
225static void f_histget(typval_T *argvars, typval_T *rettv);
226static void f_histnr(typval_T *argvars, typval_T *rettv);
227static void f_hlID(typval_T *argvars, typval_T *rettv);
228static void f_hlexists(typval_T *argvars, typval_T *rettv);
229static void f_hostname(typval_T *argvars, typval_T *rettv);
230static void f_iconv(typval_T *argvars, typval_T *rettv);
231static void f_indent(typval_T *argvars, typval_T *rettv);
232static void f_index(typval_T *argvars, typval_T *rettv);
233static void f_input(typval_T *argvars, typval_T *rettv);
234static void f_inputdialog(typval_T *argvars, typval_T *rettv);
235static void f_inputlist(typval_T *argvars, typval_T *rettv);
236static void f_inputrestore(typval_T *argvars, typval_T *rettv);
237static void f_inputsave(typval_T *argvars, typval_T *rettv);
238static void f_inputsecret(typval_T *argvars, typval_T *rettv);
239static void f_insert(typval_T *argvars, typval_T *rettv);
240static void f_invert(typval_T *argvars, typval_T *rettv);
241static void f_isdirectory(typval_T *argvars, typval_T *rettv);
242static void f_islocked(typval_T *argvars, typval_T *rettv);
243#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200244static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_isnan(typval_T *argvars, typval_T *rettv);
246#endif
247static void f_items(typval_T *argvars, typval_T *rettv);
248#ifdef FEAT_JOB_CHANNEL
249static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
250static void f_job_info(typval_T *argvars, typval_T *rettv);
251static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
252static void f_job_start(typval_T *argvars, typval_T *rettv);
253static void f_job_stop(typval_T *argvars, typval_T *rettv);
254static void f_job_status(typval_T *argvars, typval_T *rettv);
255#endif
256static void f_join(typval_T *argvars, typval_T *rettv);
257static void f_js_decode(typval_T *argvars, typval_T *rettv);
258static void f_js_encode(typval_T *argvars, typval_T *rettv);
259static void f_json_decode(typval_T *argvars, typval_T *rettv);
260static void f_json_encode(typval_T *argvars, typval_T *rettv);
261static void f_keys(typval_T *argvars, typval_T *rettv);
262static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
263static void f_len(typval_T *argvars, typval_T *rettv);
264static void f_libcall(typval_T *argvars, typval_T *rettv);
265static void f_libcallnr(typval_T *argvars, typval_T *rettv);
266static void f_line(typval_T *argvars, typval_T *rettv);
267static void f_line2byte(typval_T *argvars, typval_T *rettv);
268static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200269static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200270static void f_localtime(typval_T *argvars, typval_T *rettv);
271#ifdef FEAT_FLOAT
272static void f_log(typval_T *argvars, typval_T *rettv);
273static void f_log10(typval_T *argvars, typval_T *rettv);
274#endif
275#ifdef FEAT_LUA
276static void f_luaeval(typval_T *argvars, typval_T *rettv);
277#endif
278static void f_map(typval_T *argvars, typval_T *rettv);
279static void f_maparg(typval_T *argvars, typval_T *rettv);
280static void f_mapcheck(typval_T *argvars, typval_T *rettv);
281static void f_match(typval_T *argvars, typval_T *rettv);
282static void f_matchadd(typval_T *argvars, typval_T *rettv);
283static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
284static void f_matcharg(typval_T *argvars, typval_T *rettv);
285static void f_matchdelete(typval_T *argvars, typval_T *rettv);
286static void f_matchend(typval_T *argvars, typval_T *rettv);
287static void f_matchlist(typval_T *argvars, typval_T *rettv);
288static void f_matchstr(typval_T *argvars, typval_T *rettv);
289static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
290static void f_max(typval_T *argvars, typval_T *rettv);
291static void f_min(typval_T *argvars, typval_T *rettv);
292#ifdef vim_mkdir
293static void f_mkdir(typval_T *argvars, typval_T *rettv);
294#endif
295static void f_mode(typval_T *argvars, typval_T *rettv);
296#ifdef FEAT_MZSCHEME
297static void f_mzeval(typval_T *argvars, typval_T *rettv);
298#endif
299static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
300static void f_nr2char(typval_T *argvars, typval_T *rettv);
301static void f_or(typval_T *argvars, typval_T *rettv);
302static void f_pathshorten(typval_T *argvars, typval_T *rettv);
303#ifdef FEAT_PERL
304static void f_perleval(typval_T *argvars, typval_T *rettv);
305#endif
306#ifdef FEAT_FLOAT
307static void f_pow(typval_T *argvars, typval_T *rettv);
308#endif
309static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
310static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200311#ifdef FEAT_JOB_CHANNEL
312static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200313static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200314static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
315#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200316static void f_pumvisible(typval_T *argvars, typval_T *rettv);
317#ifdef FEAT_PYTHON3
318static void f_py3eval(typval_T *argvars, typval_T *rettv);
319#endif
320#ifdef FEAT_PYTHON
321static void f_pyeval(typval_T *argvars, typval_T *rettv);
322#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100323#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
324static void f_pyxeval(typval_T *argvars, typval_T *rettv);
325#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200326static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200327static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200328static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200329static void f_reg_executing(typval_T *argvars, typval_T *rettv);
330static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200331static void f_reltime(typval_T *argvars, typval_T *rettv);
332#ifdef FEAT_FLOAT
333static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
334#endif
335static void f_reltimestr(typval_T *argvars, typval_T *rettv);
336static void f_remote_expr(typval_T *argvars, typval_T *rettv);
337static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
338static void f_remote_peek(typval_T *argvars, typval_T *rettv);
339static void f_remote_read(typval_T *argvars, typval_T *rettv);
340static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100341static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200342static void f_remove(typval_T *argvars, typval_T *rettv);
343static void f_rename(typval_T *argvars, typval_T *rettv);
344static void f_repeat(typval_T *argvars, typval_T *rettv);
345static void f_resolve(typval_T *argvars, typval_T *rettv);
346static void f_reverse(typval_T *argvars, typval_T *rettv);
347#ifdef FEAT_FLOAT
348static void f_round(typval_T *argvars, typval_T *rettv);
349#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100350#ifdef FEAT_RUBY
351static void f_rubyeval(typval_T *argvars, typval_T *rettv);
352#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200353static void f_screenattr(typval_T *argvars, typval_T *rettv);
354static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100355static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200356static void f_screencol(typval_T *argvars, typval_T *rettv);
357static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100358static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200359static void f_search(typval_T *argvars, typval_T *rettv);
360static void f_searchdecl(typval_T *argvars, typval_T *rettv);
361static void f_searchpair(typval_T *argvars, typval_T *rettv);
362static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
363static void f_searchpos(typval_T *argvars, typval_T *rettv);
364static void f_server2client(typval_T *argvars, typval_T *rettv);
365static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200366static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200367static void f_setbufvar(typval_T *argvars, typval_T *rettv);
368static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
369static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200370static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200371static void f_setfperm(typval_T *argvars, typval_T *rettv);
372static void f_setline(typval_T *argvars, typval_T *rettv);
373static void f_setloclist(typval_T *argvars, typval_T *rettv);
374static void f_setmatches(typval_T *argvars, typval_T *rettv);
375static void f_setpos(typval_T *argvars, typval_T *rettv);
376static void f_setqflist(typval_T *argvars, typval_T *rettv);
377static void f_setreg(typval_T *argvars, typval_T *rettv);
378static void f_settabvar(typval_T *argvars, typval_T *rettv);
379static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100380static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200381static void f_setwinvar(typval_T *argvars, typval_T *rettv);
382#ifdef FEAT_CRYPT
383static void f_sha256(typval_T *argvars, typval_T *rettv);
384#endif /* FEAT_CRYPT */
385static void f_shellescape(typval_T *argvars, typval_T *rettv);
386static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100387#ifdef FEAT_SIGNS
388static void f_sign_define(typval_T *argvars, typval_T *rettv);
389static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
390static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100391static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100392static void f_sign_place(typval_T *argvars, typval_T *rettv);
393static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
394static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
395#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200396static void f_simplify(typval_T *argvars, typval_T *rettv);
397#ifdef FEAT_FLOAT
398static void f_sin(typval_T *argvars, typval_T *rettv);
399static void f_sinh(typval_T *argvars, typval_T *rettv);
400#endif
401static void f_sort(typval_T *argvars, typval_T *rettv);
402static void f_soundfold(typval_T *argvars, typval_T *rettv);
403static void f_spellbadword(typval_T *argvars, typval_T *rettv);
404static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
405static void f_split(typval_T *argvars, typval_T *rettv);
406#ifdef FEAT_FLOAT
407static void f_sqrt(typval_T *argvars, typval_T *rettv);
408static void f_str2float(typval_T *argvars, typval_T *rettv);
409#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200410static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200411static void f_str2nr(typval_T *argvars, typval_T *rettv);
412static void f_strchars(typval_T *argvars, typval_T *rettv);
413#ifdef HAVE_STRFTIME
414static void f_strftime(typval_T *argvars, typval_T *rettv);
415#endif
416static void f_strgetchar(typval_T *argvars, typval_T *rettv);
417static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200418static void f_strlen(typval_T *argvars, typval_T *rettv);
419static void f_strcharpart(typval_T *argvars, typval_T *rettv);
420static void f_strpart(typval_T *argvars, typval_T *rettv);
421static void f_strridx(typval_T *argvars, typval_T *rettv);
422static void f_strtrans(typval_T *argvars, typval_T *rettv);
423static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
424static void f_strwidth(typval_T *argvars, typval_T *rettv);
425static void f_submatch(typval_T *argvars, typval_T *rettv);
426static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200427static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200428static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200429static void f_synID(typval_T *argvars, typval_T *rettv);
430static void f_synIDattr(typval_T *argvars, typval_T *rettv);
431static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
432static void f_synstack(typval_T *argvars, typval_T *rettv);
433static void f_synconcealed(typval_T *argvars, typval_T *rettv);
434static void f_system(typval_T *argvars, typval_T *rettv);
435static void f_systemlist(typval_T *argvars, typval_T *rettv);
436static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
437static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
438static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
439static void f_taglist(typval_T *argvars, typval_T *rettv);
440static void f_tagfiles(typval_T *argvars, typval_T *rettv);
441static void f_tempname(typval_T *argvars, typval_T *rettv);
442static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
443static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200444static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareda65222019-05-16 20:29:44 +0200445static void f_test_getvalue(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200446static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100447static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100448static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200449static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100450static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100451static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200452#ifdef FEAT_JOB_CHANNEL
453static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
454#endif
455static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
456#ifdef FEAT_JOB_CHANNEL
457static void f_test_null_job(typval_T *argvars, typval_T *rettv);
458#endif
459static void f_test_null_list(typval_T *argvars, typval_T *rettv);
460static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
461static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200462#ifdef FEAT_GUI
463static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
464#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200465#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200466static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200467#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200468static void f_test_settime(typval_T *argvars, typval_T *rettv);
469#ifdef FEAT_FLOAT
470static void f_tan(typval_T *argvars, typval_T *rettv);
471static void f_tanh(typval_T *argvars, typval_T *rettv);
472#endif
473#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200474static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200475static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200476static void f_timer_start(typval_T *argvars, typval_T *rettv);
477static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200478static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200479#endif
480static void f_tolower(typval_T *argvars, typval_T *rettv);
481static void f_toupper(typval_T *argvars, typval_T *rettv);
482static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100483static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200484#ifdef FEAT_FLOAT
485static void f_trunc(typval_T *argvars, typval_T *rettv);
486#endif
487static void f_type(typval_T *argvars, typval_T *rettv);
488static void f_undofile(typval_T *argvars, typval_T *rettv);
489static void f_undotree(typval_T *argvars, typval_T *rettv);
490static void f_uniq(typval_T *argvars, typval_T *rettv);
491static void f_values(typval_T *argvars, typval_T *rettv);
492static void f_virtcol(typval_T *argvars, typval_T *rettv);
493static void f_visualmode(typval_T *argvars, typval_T *rettv);
494static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
495static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
496static void f_win_getid(typval_T *argvars, typval_T *rettv);
497static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
498static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
499static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100500static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200501static void f_winbufnr(typval_T *argvars, typval_T *rettv);
502static void f_wincol(typval_T *argvars, typval_T *rettv);
503static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200504static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505static void f_winline(typval_T *argvars, typval_T *rettv);
506static void f_winnr(typval_T *argvars, typval_T *rettv);
507static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
508static void f_winrestview(typval_T *argvars, typval_T *rettv);
509static void f_winsaveview(typval_T *argvars, typval_T *rettv);
510static void f_winwidth(typval_T *argvars, typval_T *rettv);
511static void f_writefile(typval_T *argvars, typval_T *rettv);
512static void f_wordcount(typval_T *argvars, typval_T *rettv);
513static void f_xor(typval_T *argvars, typval_T *rettv);
514
515/*
516 * Array with names and number of arguments of all internal functions
517 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
518 */
519static struct fst
520{
521 char *f_name; /* function name */
522 char f_min_argc; /* minimal number of arguments */
523 char f_max_argc; /* maximal number of arguments */
524 void (*f_func)(typval_T *args, typval_T *rvar);
525 /* implementation of function */
526} functions[] =
527{
528#ifdef FEAT_FLOAT
529 {"abs", 1, 1, f_abs},
530 {"acos", 1, 1, f_acos}, /* WJMc */
531#endif
532 {"add", 2, 2, f_add},
533 {"and", 2, 2, f_and},
534 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200535 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200536 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200537 {"argidx", 0, 0, f_argidx},
538 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200539 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200540#ifdef FEAT_FLOAT
541 {"asin", 1, 1, f_asin}, /* WJMc */
542#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100543 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200544 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100545 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200546 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200547 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100549 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"assert_match", 2, 3, f_assert_match},
551 {"assert_notequal", 2, 3, f_assert_notequal},
552 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100553 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200554 {"assert_true", 1, 2, f_assert_true},
555#ifdef FEAT_FLOAT
556 {"atan", 1, 1, f_atan},
557 {"atan2", 2, 2, f_atan2},
558#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100559#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200560 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100561 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100562# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100563 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100564# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100565#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200566 {"browse", 4, 4, f_browse},
567 {"browsedir", 2, 2, f_browsedir},
568 {"bufexists", 1, 1, f_bufexists},
569 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
570 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
571 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
572 {"buflisted", 1, 1, f_buflisted},
573 {"bufloaded", 1, 1, f_bufloaded},
574 {"bufname", 1, 1, f_bufname},
575 {"bufnr", 1, 2, f_bufnr},
576 {"bufwinid", 1, 1, f_bufwinid},
577 {"bufwinnr", 1, 1, f_bufwinnr},
578 {"byte2line", 1, 1, f_byte2line},
579 {"byteidx", 2, 2, f_byteidx},
580 {"byteidxcomp", 2, 2, f_byteidxcomp},
581 {"call", 2, 3, f_call},
582#ifdef FEAT_FLOAT
583 {"ceil", 1, 1, f_ceil},
584#endif
585#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100586 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200587 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200588 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
590 {"ch_evalraw", 2, 3, f_ch_evalraw},
591 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
592 {"ch_getjob", 1, 1, f_ch_getjob},
593 {"ch_info", 1, 1, f_ch_info},
594 {"ch_log", 1, 2, f_ch_log},
595 {"ch_logfile", 1, 2, f_ch_logfile},
596 {"ch_open", 1, 2, f_ch_open},
597 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100598 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200599 {"ch_readraw", 1, 2, f_ch_readraw},
600 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
601 {"ch_sendraw", 2, 3, f_ch_sendraw},
602 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200603 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200604#endif
605 {"changenr", 0, 0, f_changenr},
606 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200607 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100609 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610 {"col", 1, 1, f_col},
611#if defined(FEAT_INS_EXPAND)
612 {"complete", 2, 2, f_complete},
613 {"complete_add", 1, 1, f_complete_add},
614 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100615 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200616#endif
617 {"confirm", 1, 4, f_confirm},
618 {"copy", 1, 1, f_copy},
619#ifdef FEAT_FLOAT
620 {"cos", 1, 1, f_cos},
621 {"cosh", 1, 1, f_cosh},
622#endif
623 {"count", 2, 4, f_count},
624 {"cscope_connection",0,3, f_cscope_connection},
625 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100626#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200627 {"debugbreak", 1, 1, f_debugbreak},
628#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200629 {"deepcopy", 1, 2, f_deepcopy},
630 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200631 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"did_filetype", 0, 0, f_did_filetype},
633 {"diff_filler", 1, 1, f_diff_filler},
634 {"diff_hlID", 2, 2, f_diff_hlID},
635 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200636 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200637 {"escape", 2, 2, f_escape},
638 {"eval", 1, 1, f_eval},
639 {"eventhandler", 0, 0, f_eventhandler},
640 {"executable", 1, 1, f_executable},
641 {"execute", 1, 2, f_execute},
642 {"exepath", 1, 1, f_exepath},
643 {"exists", 1, 1, f_exists},
644#ifdef FEAT_FLOAT
645 {"exp", 1, 1, f_exp},
646#endif
647 {"expand", 1, 3, f_expand},
648 {"extend", 2, 3, f_extend},
649 {"feedkeys", 1, 2, f_feedkeys},
650 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
651 {"filereadable", 1, 1, f_filereadable},
652 {"filewritable", 1, 1, f_filewritable},
653 {"filter", 2, 2, f_filter},
654 {"finddir", 1, 3, f_finddir},
655 {"findfile", 1, 3, f_findfile},
656#ifdef FEAT_FLOAT
657 {"float2nr", 1, 1, f_float2nr},
658 {"floor", 1, 1, f_floor},
659 {"fmod", 2, 2, f_fmod},
660#endif
661 {"fnameescape", 1, 1, f_fnameescape},
662 {"fnamemodify", 2, 2, f_fnamemodify},
663 {"foldclosed", 1, 1, f_foldclosed},
664 {"foldclosedend", 1, 1, f_foldclosedend},
665 {"foldlevel", 1, 1, f_foldlevel},
666 {"foldtext", 0, 0, f_foldtext},
667 {"foldtextresult", 1, 1, f_foldtextresult},
668 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200669 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200670 {"function", 1, 3, f_function},
671 {"garbagecollect", 0, 1, f_garbagecollect},
672 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200673 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200674 {"getbufline", 2, 3, f_getbufline},
675 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100676 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200677 {"getchar", 0, 1, f_getchar},
678 {"getcharmod", 0, 0, f_getcharmod},
679 {"getcharsearch", 0, 0, f_getcharsearch},
680 {"getcmdline", 0, 0, f_getcmdline},
681 {"getcmdpos", 0, 0, f_getcmdpos},
682 {"getcmdtype", 0, 0, f_getcmdtype},
683 {"getcmdwintype", 0, 0, f_getcmdwintype},
684#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200685 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200686#endif
687 {"getcurpos", 0, 0, f_getcurpos},
688 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200689 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200690 {"getfontname", 0, 1, f_getfontname},
691 {"getfperm", 1, 1, f_getfperm},
692 {"getfsize", 1, 1, f_getfsize},
693 {"getftime", 1, 1, f_getftime},
694 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100695 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200696 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200697 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100698 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200699 {"getpid", 0, 0, f_getpid},
700 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200701 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702 {"getreg", 0, 3, f_getreg},
703 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200704 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200705 {"gettabvar", 2, 3, f_gettabvar},
706 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100707 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200708 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100709 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200710 {"getwinposx", 0, 0, f_getwinposx},
711 {"getwinposy", 0, 0, f_getwinposy},
712 {"getwinvar", 2, 3, f_getwinvar},
713 {"glob", 1, 4, f_glob},
714 {"glob2regpat", 1, 1, f_glob2regpat},
715 {"globpath", 2, 5, f_globpath},
716 {"has", 1, 1, f_has},
717 {"has_key", 2, 2, f_has_key},
718 {"haslocaldir", 0, 2, f_haslocaldir},
719 {"hasmapto", 1, 3, f_hasmapto},
720 {"highlightID", 1, 1, f_hlID}, /* obsolete */
721 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
722 {"histadd", 2, 2, f_histadd},
723 {"histdel", 1, 2, f_histdel},
724 {"histget", 1, 2, f_histget},
725 {"histnr", 1, 1, f_histnr},
726 {"hlID", 1, 1, f_hlID},
727 {"hlexists", 1, 1, f_hlexists},
728 {"hostname", 0, 0, f_hostname},
729 {"iconv", 3, 3, f_iconv},
730 {"indent", 1, 1, f_indent},
731 {"index", 2, 4, f_index},
732 {"input", 1, 3, f_input},
733 {"inputdialog", 1, 3, f_inputdialog},
734 {"inputlist", 1, 1, f_inputlist},
735 {"inputrestore", 0, 0, f_inputrestore},
736 {"inputsave", 0, 0, f_inputsave},
737 {"inputsecret", 1, 2, f_inputsecret},
738 {"insert", 2, 3, f_insert},
739 {"invert", 1, 1, f_invert},
740 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200741#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
742 {"isinf", 1, 1, f_isinf},
743#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200744 {"islocked", 1, 1, f_islocked},
745#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
746 {"isnan", 1, 1, f_isnan},
747#endif
748 {"items", 1, 1, f_items},
749#ifdef FEAT_JOB_CHANNEL
750 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200751 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200752 {"job_setoptions", 2, 2, f_job_setoptions},
753 {"job_start", 1, 2, f_job_start},
754 {"job_status", 1, 1, f_job_status},
755 {"job_stop", 1, 2, f_job_stop},
756#endif
757 {"join", 1, 2, f_join},
758 {"js_decode", 1, 1, f_js_decode},
759 {"js_encode", 1, 1, f_js_encode},
760 {"json_decode", 1, 1, f_json_decode},
761 {"json_encode", 1, 1, f_json_encode},
762 {"keys", 1, 1, f_keys},
763 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
764 {"len", 1, 1, f_len},
765 {"libcall", 3, 3, f_libcall},
766 {"libcallnr", 3, 3, f_libcallnr},
767 {"line", 1, 1, f_line},
768 {"line2byte", 1, 1, f_line2byte},
769 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200770 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200771 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200772 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200773 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200774 {"localtime", 0, 0, f_localtime},
775#ifdef FEAT_FLOAT
776 {"log", 1, 1, f_log},
777 {"log10", 1, 1, f_log10},
778#endif
779#ifdef FEAT_LUA
780 {"luaeval", 1, 2, f_luaeval},
781#endif
782 {"map", 2, 2, f_map},
783 {"maparg", 1, 4, f_maparg},
784 {"mapcheck", 1, 3, f_mapcheck},
785 {"match", 2, 4, f_match},
786 {"matchadd", 2, 5, f_matchadd},
787 {"matchaddpos", 2, 5, f_matchaddpos},
788 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100789 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200790 {"matchend", 2, 4, f_matchend},
791 {"matchlist", 2, 4, f_matchlist},
792 {"matchstr", 2, 4, f_matchstr},
793 {"matchstrpos", 2, 4, f_matchstrpos},
794 {"max", 1, 1, f_max},
795 {"min", 1, 1, f_min},
796#ifdef vim_mkdir
797 {"mkdir", 1, 3, f_mkdir},
798#endif
799 {"mode", 0, 1, f_mode},
800#ifdef FEAT_MZSCHEME
801 {"mzeval", 1, 1, f_mzeval},
802#endif
803 {"nextnonblank", 1, 1, f_nextnonblank},
804 {"nr2char", 1, 2, f_nr2char},
805 {"or", 2, 2, f_or},
806 {"pathshorten", 1, 1, f_pathshorten},
807#ifdef FEAT_PERL
808 {"perleval", 1, 1, f_perleval},
809#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200810#ifdef FEAT_TEXT_PROP
811 {"popup_close", 1, 1, f_popup_close},
812 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200813 {"popup_hide", 1, 1, f_popup_hide},
814 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200815#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200816#ifdef FEAT_FLOAT
817 {"pow", 2, 2, f_pow},
818#endif
819 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100820 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200821#ifdef FEAT_JOB_CHANNEL
822 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200823 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200824 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
825#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100826#ifdef FEAT_TEXT_PROP
827 {"prop_add", 3, 3, f_prop_add},
828 {"prop_clear", 1, 3, f_prop_clear},
829 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100830 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100831 {"prop_type_add", 2, 2, f_prop_type_add},
832 {"prop_type_change", 2, 2, f_prop_type_change},
833 {"prop_type_delete", 1, 2, f_prop_type_delete},
834 {"prop_type_get", 1, 2, f_prop_type_get},
835 {"prop_type_list", 0, 1, f_prop_type_list},
836#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200837 {"pumvisible", 0, 0, f_pumvisible},
838#ifdef FEAT_PYTHON3
839 {"py3eval", 1, 1, f_py3eval},
840#endif
841#ifdef FEAT_PYTHON
842 {"pyeval", 1, 1, f_pyeval},
843#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100844#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
845 {"pyxeval", 1, 1, f_pyxeval},
846#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200847 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200848 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200849 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200850 {"reg_executing", 0, 0, f_reg_executing},
851 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"reltime", 0, 2, f_reltime},
853#ifdef FEAT_FLOAT
854 {"reltimefloat", 1, 1, f_reltimefloat},
855#endif
856 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100857 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200858 {"remote_foreground", 1, 1, f_remote_foreground},
859 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100860 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200861 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100862 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200863 {"remove", 2, 3, f_remove},
864 {"rename", 2, 2, f_rename},
865 {"repeat", 2, 2, f_repeat},
866 {"resolve", 1, 1, f_resolve},
867 {"reverse", 1, 1, f_reverse},
868#ifdef FEAT_FLOAT
869 {"round", 1, 1, f_round},
870#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100871#ifdef FEAT_RUBY
872 {"rubyeval", 1, 1, f_rubyeval},
873#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200874 {"screenattr", 2, 2, f_screenattr},
875 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100876 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200877 {"screencol", 0, 0, f_screencol},
878 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100879 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200880 {"search", 1, 4, f_search},
881 {"searchdecl", 1, 3, f_searchdecl},
882 {"searchpair", 3, 7, f_searchpair},
883 {"searchpairpos", 3, 7, f_searchpairpos},
884 {"searchpos", 1, 4, f_searchpos},
885 {"server2client", 2, 2, f_server2client},
886 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200887 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200888 {"setbufvar", 3, 3, f_setbufvar},
889 {"setcharsearch", 1, 1, f_setcharsearch},
890 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200891 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200892 {"setfperm", 2, 2, f_setfperm},
893 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200894 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100895 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200896 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200897 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200898 {"setreg", 2, 3, f_setreg},
899 {"settabvar", 3, 3, f_settabvar},
900 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100901 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200902 {"setwinvar", 3, 3, f_setwinvar},
903#ifdef FEAT_CRYPT
904 {"sha256", 1, 1, f_sha256},
905#endif
906 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100907 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100908#ifdef FEAT_SIGNS
909 {"sign_define", 1, 2, f_sign_define},
910 {"sign_getdefined", 0, 1, f_sign_getdefined},
911 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100912 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100913 {"sign_place", 4, 5, f_sign_place},
914 {"sign_undefine", 0, 1, f_sign_undefine},
915 {"sign_unplace", 1, 2, f_sign_unplace},
916#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200917 {"simplify", 1, 1, f_simplify},
918#ifdef FEAT_FLOAT
919 {"sin", 1, 1, f_sin},
920 {"sinh", 1, 1, f_sinh},
921#endif
922 {"sort", 1, 3, f_sort},
923 {"soundfold", 1, 1, f_soundfold},
924 {"spellbadword", 0, 1, f_spellbadword},
925 {"spellsuggest", 1, 3, f_spellsuggest},
926 {"split", 1, 3, f_split},
927#ifdef FEAT_FLOAT
928 {"sqrt", 1, 1, f_sqrt},
929 {"str2float", 1, 1, f_str2float},
930#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200931 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200932 {"str2nr", 1, 2, f_str2nr},
933 {"strcharpart", 2, 3, f_strcharpart},
934 {"strchars", 1, 2, f_strchars},
935 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
936#ifdef HAVE_STRFTIME
937 {"strftime", 1, 2, f_strftime},
938#endif
939 {"strgetchar", 2, 2, f_strgetchar},
940 {"stridx", 2, 3, f_stridx},
941 {"string", 1, 1, f_string},
942 {"strlen", 1, 1, f_strlen},
943 {"strpart", 2, 3, f_strpart},
944 {"strridx", 2, 3, f_strridx},
945 {"strtrans", 1, 1, f_strtrans},
946 {"strwidth", 1, 1, f_strwidth},
947 {"submatch", 1, 2, f_submatch},
948 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200949 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200950 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200951 {"synID", 3, 3, f_synID},
952 {"synIDattr", 2, 3, f_synIDattr},
953 {"synIDtrans", 1, 1, f_synIDtrans},
954 {"synconcealed", 2, 2, f_synconcealed},
955 {"synstack", 2, 2, f_synstack},
956 {"system", 1, 2, f_system},
957 {"systemlist", 1, 2, f_systemlist},
958 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
959 {"tabpagenr", 0, 1, f_tabpagenr},
960 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
961 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100962 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200963#ifdef FEAT_FLOAT
964 {"tan", 1, 1, f_tan},
965 {"tanh", 1, 1, f_tanh},
966#endif
967 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200968#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100969 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
970 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100971 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200972 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200973# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
974 {"term_getansicolors", 1, 1, f_term_getansicolors},
975# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200976 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200977 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200978 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200979 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200980 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200981 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200982 {"term_getstatus", 1, 1, f_term_getstatus},
983 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200984 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200985 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200986 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200987 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200988# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
989 {"term_setansicolors", 2, 2, f_term_setansicolors},
990# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100991 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100992 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200993 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200994 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200995 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200996#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200997 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
998 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200999 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001000 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +02001001 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +01001002 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001003 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001004#ifdef FEAT_JOB_CHANNEL
1005 {"test_null_channel", 0, 0, f_test_null_channel},
1006#endif
1007 {"test_null_dict", 0, 0, f_test_null_dict},
1008#ifdef FEAT_JOB_CHANNEL
1009 {"test_null_job", 0, 0, f_test_null_job},
1010#endif
1011 {"test_null_list", 0, 0, f_test_null_list},
1012 {"test_null_partial", 0, 0, f_test_null_partial},
1013 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001014 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001015 {"test_override", 2, 2, f_test_override},
1016 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001017#ifdef FEAT_GUI
1018 {"test_scrollbar", 3, 3, f_test_scrollbar},
1019#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001020#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001021 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001022#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001023 {"test_settime", 1, 1, f_test_settime},
1024#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001025 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001026 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001027 {"timer_start", 2, 3, f_timer_start},
1028 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001029 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001030#endif
1031 {"tolower", 1, 1, f_tolower},
1032 {"toupper", 1, 1, f_toupper},
1033 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001034 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001035#ifdef FEAT_FLOAT
1036 {"trunc", 1, 1, f_trunc},
1037#endif
1038 {"type", 1, 1, f_type},
1039 {"undofile", 1, 1, f_undofile},
1040 {"undotree", 0, 0, f_undotree},
1041 {"uniq", 1, 3, f_uniq},
1042 {"values", 1, 1, f_values},
1043 {"virtcol", 1, 1, f_virtcol},
1044 {"visualmode", 0, 1, f_visualmode},
1045 {"wildmenumode", 0, 0, f_wildmenumode},
1046 {"win_findbuf", 1, 1, f_win_findbuf},
1047 {"win_getid", 0, 2, f_win_getid},
1048 {"win_gotoid", 1, 1, f_win_gotoid},
1049 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1050 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001051 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001052 {"winbufnr", 1, 1, f_winbufnr},
1053 {"wincol", 0, 0, f_wincol},
1054 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001055 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001056 {"winline", 0, 0, f_winline},
1057 {"winnr", 0, 1, f_winnr},
1058 {"winrestcmd", 0, 0, f_winrestcmd},
1059 {"winrestview", 1, 1, f_winrestview},
1060 {"winsaveview", 0, 0, f_winsaveview},
1061 {"winwidth", 1, 1, f_winwidth},
1062 {"wordcount", 0, 0, f_wordcount},
1063 {"writefile", 2, 3, f_writefile},
1064 {"xor", 2, 2, f_xor},
1065};
1066
1067#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1068
1069/*
1070 * Function given to ExpandGeneric() to obtain the list of internal
1071 * or user defined function names.
1072 */
1073 char_u *
1074get_function_name(expand_T *xp, int idx)
1075{
1076 static int intidx = -1;
1077 char_u *name;
1078
1079 if (idx == 0)
1080 intidx = -1;
1081 if (intidx < 0)
1082 {
1083 name = get_user_func_name(xp, idx);
1084 if (name != NULL)
1085 return name;
1086 }
1087 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1088 {
1089 STRCPY(IObuff, functions[intidx].f_name);
1090 STRCAT(IObuff, "(");
1091 if (functions[intidx].f_max_argc == 0)
1092 STRCAT(IObuff, ")");
1093 return IObuff;
1094 }
1095
1096 return NULL;
1097}
1098
1099/*
1100 * Function given to ExpandGeneric() to obtain the list of internal or
1101 * user defined variable or function names.
1102 */
1103 char_u *
1104get_expr_name(expand_T *xp, int idx)
1105{
1106 static int intidx = -1;
1107 char_u *name;
1108
1109 if (idx == 0)
1110 intidx = -1;
1111 if (intidx < 0)
1112 {
1113 name = get_function_name(xp, idx);
1114 if (name != NULL)
1115 return name;
1116 }
1117 return get_user_var_name(xp, ++intidx);
1118}
1119
1120#endif /* FEAT_CMDL_COMPL */
1121
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001122/*
1123 * Find internal function in table above.
1124 * Return index, or -1 if not found
1125 */
1126 int
1127find_internal_func(
1128 char_u *name) /* name of the function */
1129{
1130 int first = 0;
1131 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1132 int cmp;
1133 int x;
1134
1135 /*
1136 * Find the function name in the table. Binary search.
1137 */
1138 while (first <= last)
1139 {
1140 x = first + ((unsigned)(last - first) >> 1);
1141 cmp = STRCMP(name, functions[x].f_name);
1142 if (cmp < 0)
1143 last = x - 1;
1144 else if (cmp > 0)
1145 first = x + 1;
1146 else
1147 return x;
1148 }
1149 return -1;
1150}
1151
1152 int
1153call_internal_func(
1154 char_u *name,
1155 int argcount,
1156 typval_T *argvars,
1157 typval_T *rettv)
1158{
1159 int i;
1160
1161 i = find_internal_func(name);
1162 if (i < 0)
1163 return ERROR_UNKNOWN;
1164 if (argcount < functions[i].f_min_argc)
1165 return ERROR_TOOFEW;
1166 if (argcount > functions[i].f_max_argc)
1167 return ERROR_TOOMANY;
1168 argvars[argcount].v_type = VAR_UNKNOWN;
1169 functions[i].f_func(argvars, rettv);
1170 return ERROR_NONE;
1171}
1172
1173/*
1174 * Return TRUE for a non-zero Number and a non-empty String.
1175 */
1176 static int
1177non_zero_arg(typval_T *argvars)
1178{
1179 return ((argvars[0].v_type == VAR_NUMBER
1180 && argvars[0].vval.v_number != 0)
1181 || (argvars[0].v_type == VAR_SPECIAL
1182 && argvars[0].vval.v_number == VVAL_TRUE)
1183 || (argvars[0].v_type == VAR_STRING
1184 && argvars[0].vval.v_string != NULL
1185 && *argvars[0].vval.v_string != NUL));
1186}
1187
1188/*
1189 * Get the lnum from the first argument.
1190 * Also accepts ".", "$", etc., but that only works for the current buffer.
1191 * Returns -1 on error.
1192 */
1193 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001194tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001195{
1196 typval_T rettv;
1197 linenr_T lnum;
1198
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001199 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001200 if (lnum == 0) /* no valid number, try using line() */
1201 {
1202 rettv.v_type = VAR_NUMBER;
1203 f_line(argvars, &rettv);
1204 lnum = (linenr_T)rettv.vval.v_number;
1205 clear_tv(&rettv);
1206 }
1207 return lnum;
1208}
1209
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001210/*
1211 * Get the lnum from the first argument.
1212 * Also accepts "$", then "buf" is used.
1213 * Returns 0 on error.
1214 */
1215 static linenr_T
1216tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1217{
1218 if (argvars[0].v_type == VAR_STRING
1219 && argvars[0].vval.v_string != NULL
1220 && argvars[0].vval.v_string[0] == '$'
1221 && buf != NULL)
1222 return buf->b_ml.ml_line_count;
1223 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1224}
1225
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001226#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001227/*
1228 * Get the float value of "argvars[0]" into "f".
1229 * Returns FAIL when the argument is not a Number or Float.
1230 */
1231 static int
1232get_float_arg(typval_T *argvars, float_T *f)
1233{
1234 if (argvars[0].v_type == VAR_FLOAT)
1235 {
1236 *f = argvars[0].vval.v_float;
1237 return OK;
1238 }
1239 if (argvars[0].v_type == VAR_NUMBER)
1240 {
1241 *f = (float_T)argvars[0].vval.v_number;
1242 return OK;
1243 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001244 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001245 return FAIL;
1246}
1247
1248/*
1249 * "abs(expr)" function
1250 */
1251 static void
1252f_abs(typval_T *argvars, typval_T *rettv)
1253{
1254 if (argvars[0].v_type == VAR_FLOAT)
1255 {
1256 rettv->v_type = VAR_FLOAT;
1257 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1258 }
1259 else
1260 {
1261 varnumber_T n;
1262 int error = FALSE;
1263
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001264 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001265 if (error)
1266 rettv->vval.v_number = -1;
1267 else if (n > 0)
1268 rettv->vval.v_number = n;
1269 else
1270 rettv->vval.v_number = -n;
1271 }
1272}
1273
1274/*
1275 * "acos()" function
1276 */
1277 static void
1278f_acos(typval_T *argvars, typval_T *rettv)
1279{
1280 float_T f = 0.0;
1281
1282 rettv->v_type = VAR_FLOAT;
1283 if (get_float_arg(argvars, &f) == OK)
1284 rettv->vval.v_float = acos(f);
1285 else
1286 rettv->vval.v_float = 0.0;
1287}
1288#endif
1289
1290/*
1291 * "add(list, item)" function
1292 */
1293 static void
1294f_add(typval_T *argvars, typval_T *rettv)
1295{
1296 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001297 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001298
1299 rettv->vval.v_number = 1; /* Default: Failed */
1300 if (argvars[0].v_type == VAR_LIST)
1301 {
1302 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001303 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001304 (char_u *)N_("add() argument"), TRUE)
1305 && list_append_tv(l, &argvars[1]) == OK)
1306 copy_tv(&argvars[0], rettv);
1307 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001308 else if (argvars[0].v_type == VAR_BLOB)
1309 {
1310 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001311 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001312 (char_u *)N_("add() argument"), TRUE))
1313 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001314 int error = FALSE;
1315 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1316
1317 if (!error)
1318 {
1319 ga_append(&b->bv_ga, (int)n);
1320 copy_tv(&argvars[0], rettv);
1321 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001322 }
1323 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001324 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001325 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001326}
1327
1328/*
1329 * "and(expr, expr)" function
1330 */
1331 static void
1332f_and(typval_T *argvars, typval_T *rettv)
1333{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001334 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1335 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001336}
1337
1338/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001339 * If there is a window for "curbuf", make it the current window.
1340 */
1341 static void
1342find_win_for_curbuf(void)
1343{
1344 wininfo_T *wip;
1345
1346 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1347 {
1348 if (wip->wi_win != NULL)
1349 {
1350 curwin = wip->wi_win;
1351 break;
1352 }
1353 }
1354}
1355
1356/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001357 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358 */
1359 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001360set_buffer_lines(
1361 buf_T *buf,
1362 linenr_T lnum_arg,
1363 int append,
1364 typval_T *lines,
1365 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001366{
Bram Moolenaarca851592018-06-06 21:04:07 +02001367 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1368 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001369 list_T *l = NULL;
1370 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001371 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001372 linenr_T append_lnum;
1373 buf_T *curbuf_save = NULL;
1374 win_T *curwin_save = NULL;
1375 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001376
Bram Moolenaarca851592018-06-06 21:04:07 +02001377 /* When using the current buffer ml_mfp will be set if needed. Useful when
1378 * setline() is used on startup. For other buffers the buffer must be
1379 * loaded. */
1380 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001381 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001382 rettv->vval.v_number = 1; /* FAIL */
1383 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001384 }
1385
Bram Moolenaarca851592018-06-06 21:04:07 +02001386 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001387 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001388 curbuf_save = curbuf;
1389 curwin_save = curwin;
1390 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001391 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001392 }
1393
1394 if (append)
1395 // appendbufline() uses the line number below which we insert
1396 append_lnum = lnum - 1;
1397 else
1398 // setbufline() uses the line number above which we insert, we only
1399 // append if it's below the last line
1400 append_lnum = curbuf->b_ml.ml_line_count;
1401
1402 if (lines->v_type == VAR_LIST)
1403 {
1404 l = lines->vval.v_list;
1405 li = l->lv_first;
1406 }
1407 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001408 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001409
1410 /* default result is zero == OK */
1411 for (;;)
1412 {
1413 if (l != NULL)
1414 {
1415 /* list argument, get next string */
1416 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001417 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001418 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001419 li = li->li_next;
1420 }
1421
Bram Moolenaarca851592018-06-06 21:04:07 +02001422 rettv->vval.v_number = 1; /* FAIL */
1423 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1424 break;
1425
1426 /* When coming here from Insert mode, sync undo, so that this can be
1427 * undone separately from what was previously inserted. */
1428 if (u_sync_once == 2)
1429 {
1430 u_sync_once = 1; /* notify that u_sync() was called */
1431 u_sync(TRUE);
1432 }
1433
1434 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1435 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001436 // Existing line, replace it.
1437 // Removes any existing text properties.
1438 if (u_savesub(lnum) == OK && ml_replace_len(
1439 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001440 {
1441 changed_bytes(lnum, 0);
1442 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1443 check_cursor_col();
1444 rettv->vval.v_number = 0; /* OK */
1445 }
1446 }
1447 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1448 {
1449 /* append the line */
1450 ++added;
1451 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1452 rettv->vval.v_number = 0; /* OK */
1453 }
1454
1455 if (l == NULL) /* only one string argument */
1456 break;
1457 ++lnum;
1458 }
1459
1460 if (added > 0)
1461 {
1462 win_T *wp;
1463 tabpage_T *tp;
1464
1465 appended_lines_mark(append_lnum, added);
1466 FOR_ALL_TAB_WINDOWS(tp, wp)
1467 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1468 wp->w_cursor.lnum += added;
1469 check_cursor_col();
1470
Bram Moolenaarf2732452018-06-03 14:47:35 +02001471#ifdef FEAT_JOB_CHANNEL
1472 if (bt_prompt(curbuf) && (State & INSERT))
1473 // show the line with the prompt
1474 update_topline();
1475#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001476 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001477
1478 if (!is_curbuf)
1479 {
1480 curbuf = curbuf_save;
1481 curwin = curwin_save;
1482 }
1483}
1484
1485/*
1486 * "append(lnum, string/list)" function
1487 */
1488 static void
1489f_append(typval_T *argvars, typval_T *rettv)
1490{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001491 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001492
1493 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1494}
1495
1496/*
1497 * "appendbufline(buf, lnum, string/list)" function
1498 */
1499 static void
1500f_appendbufline(typval_T *argvars, typval_T *rettv)
1501{
1502 linenr_T lnum;
1503 buf_T *buf;
1504
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001505 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001506 if (buf == NULL)
1507 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001508 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001509 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001510 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001511 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1512 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001513}
1514
1515/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001516 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001517 */
1518 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001519f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001520{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001521 win_T *wp;
1522
1523 if (argvars[0].v_type == VAR_UNKNOWN)
1524 // use the current window
1525 rettv->vval.v_number = ARGCOUNT;
1526 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001527 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001528 // use the global argument list
1529 rettv->vval.v_number = GARGCOUNT;
1530 else
1531 {
1532 // use the argument list of the specified window
1533 wp = find_win_by_nr_or_id(&argvars[0]);
1534 if (wp != NULL)
1535 rettv->vval.v_number = WARGCOUNT(wp);
1536 else
1537 rettv->vval.v_number = -1;
1538 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001539}
1540
1541/*
1542 * "argidx()" function
1543 */
1544 static void
1545f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1546{
1547 rettv->vval.v_number = curwin->w_arg_idx;
1548}
1549
1550/*
1551 * "arglistid()" function
1552 */
1553 static void
1554f_arglistid(typval_T *argvars, typval_T *rettv)
1555{
1556 win_T *wp;
1557
1558 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001559 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001560 if (wp != NULL)
1561 rettv->vval.v_number = wp->w_alist->id;
1562}
1563
1564/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001565 * Get the argument list for a given window
1566 */
1567 static void
1568get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1569{
1570 int idx;
1571
1572 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1573 for (idx = 0; idx < argcount; ++idx)
1574 list_append_string(rettv->vval.v_list,
1575 alist_name(&arglist[idx]), -1);
1576}
1577
1578/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001579 * "argv(nr)" function
1580 */
1581 static void
1582f_argv(typval_T *argvars, typval_T *rettv)
1583{
1584 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001585 aentry_T *arglist = NULL;
1586 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001587
1588 if (argvars[0].v_type != VAR_UNKNOWN)
1589 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001590 if (argvars[1].v_type == VAR_UNKNOWN)
1591 {
1592 arglist = ARGLIST;
1593 argcount = ARGCOUNT;
1594 }
1595 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001596 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001597 {
1598 arglist = GARGLIST;
1599 argcount = GARGCOUNT;
1600 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001601 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001602 {
1603 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1604
1605 if (wp != NULL)
1606 {
1607 /* Use the argument list of the specified window */
1608 arglist = WARGLIST(wp);
1609 argcount = WARGCOUNT(wp);
1610 }
1611 }
1612
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001613 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001614 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001615 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001616 if (arglist != NULL && idx >= 0 && idx < argcount)
1617 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1618 else if (idx == -1)
1619 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001620 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001621 else
1622 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001623}
1624
1625/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001626 * "assert_beeps(cmd [, error])" function
1627 */
1628 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001629f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001630{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001631 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001632}
1633
1634/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001635 * "assert_equal(expected, actual[, msg])" function
1636 */
1637 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001638f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001639{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001640 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641}
1642
1643/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001644 * "assert_equalfile(fname-one, fname-two)" function
1645 */
1646 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001647f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001648{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001649 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001650}
1651
1652/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001653 * "assert_notequal(expected, actual[, msg])" function
1654 */
1655 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001656f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001658 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659}
1660
1661/*
1662 * "assert_exception(string[, msg])" function
1663 */
1664 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001665f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001666{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001667 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001668}
1669
1670/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001671 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001672 */
1673 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001674f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001675{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001676 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001677}
1678
1679/*
1680 * "assert_false(actual[, msg])" function
1681 */
1682 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001683f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001684{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001685 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001686}
1687
1688/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001689 * "assert_inrange(lower, upper[, msg])" function
1690 */
1691 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001692f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001693{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001694 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001695}
1696
1697/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001698 * "assert_match(pattern, actual[, msg])" function
1699 */
1700 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001701f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001702{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001703 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001704}
1705
1706/*
1707 * "assert_notmatch(pattern, actual[, msg])" function
1708 */
1709 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001710f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001711{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001712 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001713}
1714
1715/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001716 * "assert_report(msg)" function
1717 */
1718 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001719f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001720{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001721 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001722}
1723
1724/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001725 * "assert_true(actual[, msg])" function
1726 */
1727 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001728f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001729{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001730 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001731}
1732
1733#ifdef FEAT_FLOAT
1734/*
1735 * "asin()" function
1736 */
1737 static void
1738f_asin(typval_T *argvars, typval_T *rettv)
1739{
1740 float_T f = 0.0;
1741
1742 rettv->v_type = VAR_FLOAT;
1743 if (get_float_arg(argvars, &f) == OK)
1744 rettv->vval.v_float = asin(f);
1745 else
1746 rettv->vval.v_float = 0.0;
1747}
1748
1749/*
1750 * "atan()" function
1751 */
1752 static void
1753f_atan(typval_T *argvars, typval_T *rettv)
1754{
1755 float_T f = 0.0;
1756
1757 rettv->v_type = VAR_FLOAT;
1758 if (get_float_arg(argvars, &f) == OK)
1759 rettv->vval.v_float = atan(f);
1760 else
1761 rettv->vval.v_float = 0.0;
1762}
1763
1764/*
1765 * "atan2()" function
1766 */
1767 static void
1768f_atan2(typval_T *argvars, typval_T *rettv)
1769{
1770 float_T fx = 0.0, fy = 0.0;
1771
1772 rettv->v_type = VAR_FLOAT;
1773 if (get_float_arg(argvars, &fx) == OK
1774 && get_float_arg(&argvars[1], &fy) == OK)
1775 rettv->vval.v_float = atan2(fx, fy);
1776 else
1777 rettv->vval.v_float = 0.0;
1778}
1779#endif
1780
1781/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001782 * "balloon_show()" function
1783 */
1784#ifdef FEAT_BEVAL
1785 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001786f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1787{
1788 rettv->v_type = VAR_STRING;
1789 if (balloonEval != NULL)
1790 {
1791 if (balloonEval->msg == NULL)
1792 rettv->vval.v_string = NULL;
1793 else
1794 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1795 }
1796}
1797
1798 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001799f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1800{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001801 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001802 {
1803 if (argvars[0].v_type == VAR_LIST
1804# ifdef FEAT_GUI
1805 && !gui.in_use
1806# endif
1807 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001808 {
1809 list_T *l = argvars[0].vval.v_list;
1810
1811 // empty list removes the balloon
1812 post_balloon(balloonEval, NULL,
1813 l == NULL || l->lv_len == 0 ? NULL : l);
1814 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001815 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001816 {
1817 char_u *mesg = tv_get_string_chk(&argvars[0]);
1818
1819 if (mesg != NULL)
1820 // empty string removes the balloon
1821 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1822 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001823 }
1824}
1825
Bram Moolenaar669a8282017-11-19 20:13:05 +01001826# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001827 static void
1828f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1829{
1830 if (rettv_list_alloc(rettv) == OK)
1831 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001832 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001833
1834 if (msg != NULL)
1835 {
1836 pumitem_T *array;
1837 int size = split_message(msg, &array);
1838 int i;
1839
1840 /* Skip the first and last item, they are always empty. */
1841 for (i = 1; i < size - 1; ++i)
1842 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001843 while (size > 0)
1844 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001845 vim_free(array);
1846 }
1847 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001848}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001849# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001850#endif
1851
1852/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853 * "browse(save, title, initdir, default)" function
1854 */
1855 static void
1856f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1857{
1858#ifdef FEAT_BROWSE
1859 int save;
1860 char_u *title;
1861 char_u *initdir;
1862 char_u *defname;
1863 char_u buf[NUMBUFLEN];
1864 char_u buf2[NUMBUFLEN];
1865 int error = FALSE;
1866
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001867 save = (int)tv_get_number_chk(&argvars[0], &error);
1868 title = tv_get_string_chk(&argvars[1]);
1869 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1870 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001871
1872 if (error || title == NULL || initdir == NULL || defname == NULL)
1873 rettv->vval.v_string = NULL;
1874 else
1875 rettv->vval.v_string =
1876 do_browse(save ? BROWSE_SAVE : 0,
1877 title, defname, NULL, initdir, NULL, curbuf);
1878#else
1879 rettv->vval.v_string = NULL;
1880#endif
1881 rettv->v_type = VAR_STRING;
1882}
1883
1884/*
1885 * "browsedir(title, initdir)" function
1886 */
1887 static void
1888f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1889{
1890#ifdef FEAT_BROWSE
1891 char_u *title;
1892 char_u *initdir;
1893 char_u buf[NUMBUFLEN];
1894
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001895 title = tv_get_string_chk(&argvars[0]);
1896 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001897
1898 if (title == NULL || initdir == NULL)
1899 rettv->vval.v_string = NULL;
1900 else
1901 rettv->vval.v_string = do_browse(BROWSE_DIR,
1902 title, NULL, NULL, initdir, NULL, curbuf);
1903#else
1904 rettv->vval.v_string = NULL;
1905#endif
1906 rettv->v_type = VAR_STRING;
1907}
1908
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001909/*
1910 * Find a buffer by number or exact name.
1911 */
1912 static buf_T *
1913find_buffer(typval_T *avar)
1914{
1915 buf_T *buf = NULL;
1916
1917 if (avar->v_type == VAR_NUMBER)
1918 buf = buflist_findnr((int)avar->vval.v_number);
1919 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1920 {
1921 buf = buflist_findname_exp(avar->vval.v_string);
1922 if (buf == NULL)
1923 {
1924 /* No full path name match, try a match with a URL or a "nofile"
1925 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001926 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 if (buf->b_fname != NULL
1928 && (path_with_url(buf->b_fname)
1929#ifdef FEAT_QUICKFIX
1930 || bt_nofile(buf)
1931#endif
1932 )
1933 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1934 break;
1935 }
1936 }
1937 return buf;
1938}
1939
1940/*
1941 * "bufexists(expr)" function
1942 */
1943 static void
1944f_bufexists(typval_T *argvars, typval_T *rettv)
1945{
1946 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1947}
1948
1949/*
1950 * "buflisted(expr)" function
1951 */
1952 static void
1953f_buflisted(typval_T *argvars, typval_T *rettv)
1954{
1955 buf_T *buf;
1956
1957 buf = find_buffer(&argvars[0]);
1958 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1959}
1960
1961/*
1962 * "bufloaded(expr)" function
1963 */
1964 static void
1965f_bufloaded(typval_T *argvars, typval_T *rettv)
1966{
1967 buf_T *buf;
1968
1969 buf = find_buffer(&argvars[0]);
1970 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1971}
1972
1973 buf_T *
1974buflist_find_by_name(char_u *name, int curtab_only)
1975{
1976 int save_magic;
1977 char_u *save_cpo;
1978 buf_T *buf;
1979
1980 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1981 save_magic = p_magic;
1982 p_magic = TRUE;
1983 save_cpo = p_cpo;
1984 p_cpo = (char_u *)"";
1985
1986 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1987 TRUE, FALSE, curtab_only));
1988
1989 p_magic = save_magic;
1990 p_cpo = save_cpo;
1991 return buf;
1992}
1993
1994/*
1995 * Get buffer by number or pattern.
1996 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001997 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001998tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001999{
2000 char_u *name = tv->vval.v_string;
2001 buf_T *buf;
2002
2003 if (tv->v_type == VAR_NUMBER)
2004 return buflist_findnr((int)tv->vval.v_number);
2005 if (tv->v_type != VAR_STRING)
2006 return NULL;
2007 if (name == NULL || *name == NUL)
2008 return curbuf;
2009 if (name[0] == '$' && name[1] == NUL)
2010 return lastbuf;
2011
2012 buf = buflist_find_by_name(name, curtab_only);
2013
2014 /* If not found, try expanding the name, like done for bufexists(). */
2015 if (buf == NULL)
2016 buf = find_buffer(tv);
2017
2018 return buf;
2019}
2020
2021/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002022 * Get the buffer from "arg" and give an error and return NULL if it is not
2023 * valid.
2024 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002025 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002026get_buf_arg(typval_T *arg)
2027{
2028 buf_T *buf;
2029
2030 ++emsg_off;
2031 buf = tv_get_buf(arg, FALSE);
2032 --emsg_off;
2033 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002034 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002035 return buf;
2036}
2037
2038/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002039 * "bufname(expr)" function
2040 */
2041 static void
2042f_bufname(typval_T *argvars, typval_T *rettv)
2043{
2044 buf_T *buf;
2045
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002046 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002047 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002048 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002049 rettv->v_type = VAR_STRING;
2050 if (buf != NULL && buf->b_fname != NULL)
2051 rettv->vval.v_string = vim_strsave(buf->b_fname);
2052 else
2053 rettv->vval.v_string = NULL;
2054 --emsg_off;
2055}
2056
2057/*
2058 * "bufnr(expr)" function
2059 */
2060 static void
2061f_bufnr(typval_T *argvars, typval_T *rettv)
2062{
2063 buf_T *buf;
2064 int error = FALSE;
2065 char_u *name;
2066
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002067 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002068 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002069 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002070 --emsg_off;
2071
2072 /* If the buffer isn't found and the second argument is not zero create a
2073 * new buffer. */
2074 if (buf == NULL
2075 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002076 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002077 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002078 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002079 && !error)
2080 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2081
2082 if (buf != NULL)
2083 rettv->vval.v_number = buf->b_fnum;
2084 else
2085 rettv->vval.v_number = -1;
2086}
2087
2088 static void
2089buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2090{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002091 win_T *wp;
2092 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002093 buf_T *buf;
2094
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002095 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002096 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002097 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002098 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002099 {
2100 ++winnr;
2101 if (wp->w_buffer == buf)
2102 break;
2103 }
2104 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002105 --emsg_off;
2106}
2107
2108/*
2109 * "bufwinid(nr)" function
2110 */
2111 static void
2112f_bufwinid(typval_T *argvars, typval_T *rettv)
2113{
2114 buf_win_common(argvars, rettv, FALSE);
2115}
2116
2117/*
2118 * "bufwinnr(nr)" function
2119 */
2120 static void
2121f_bufwinnr(typval_T *argvars, typval_T *rettv)
2122{
2123 buf_win_common(argvars, rettv, TRUE);
2124}
2125
2126/*
2127 * "byte2line(byte)" function
2128 */
2129 static void
2130f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2131{
2132#ifndef FEAT_BYTEOFF
2133 rettv->vval.v_number = -1;
2134#else
2135 long boff = 0;
2136
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002137 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002138 if (boff < 0)
2139 rettv->vval.v_number = -1;
2140 else
2141 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2142 (linenr_T)0, &boff);
2143#endif
2144}
2145
2146 static void
2147byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2148{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002149 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002150 char_u *str;
2151 varnumber_T idx;
2152
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002153 str = tv_get_string_chk(&argvars[0]);
2154 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002155 rettv->vval.v_number = -1;
2156 if (str == NULL || idx < 0)
2157 return;
2158
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002159 t = str;
2160 for ( ; idx > 0; idx--)
2161 {
2162 if (*t == NUL) /* EOL reached */
2163 return;
2164 if (enc_utf8 && comp)
2165 t += utf_ptr2len(t);
2166 else
2167 t += (*mb_ptr2len)(t);
2168 }
2169 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002170}
2171
2172/*
2173 * "byteidx()" function
2174 */
2175 static void
2176f_byteidx(typval_T *argvars, typval_T *rettv)
2177{
2178 byteidx(argvars, rettv, FALSE);
2179}
2180
2181/*
2182 * "byteidxcomp()" function
2183 */
2184 static void
2185f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2186{
2187 byteidx(argvars, rettv, TRUE);
2188}
2189
2190/*
2191 * "call(func, arglist [, dict])" function
2192 */
2193 static void
2194f_call(typval_T *argvars, typval_T *rettv)
2195{
2196 char_u *func;
2197 partial_T *partial = NULL;
2198 dict_T *selfdict = NULL;
2199
2200 if (argvars[1].v_type != VAR_LIST)
2201 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002202 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002203 return;
2204 }
2205 if (argvars[1].vval.v_list == NULL)
2206 return;
2207
2208 if (argvars[0].v_type == VAR_FUNC)
2209 func = argvars[0].vval.v_string;
2210 else if (argvars[0].v_type == VAR_PARTIAL)
2211 {
2212 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002213 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002214 }
2215 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002216 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002217 if (*func == NUL)
2218 return; /* type error or empty name */
2219
2220 if (argvars[2].v_type != VAR_UNKNOWN)
2221 {
2222 if (argvars[2].v_type != VAR_DICT)
2223 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002224 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002225 return;
2226 }
2227 selfdict = argvars[2].vval.v_dict;
2228 }
2229
2230 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2231}
2232
2233#ifdef FEAT_FLOAT
2234/*
2235 * "ceil({float})" function
2236 */
2237 static void
2238f_ceil(typval_T *argvars, typval_T *rettv)
2239{
2240 float_T f = 0.0;
2241
2242 rettv->v_type = VAR_FLOAT;
2243 if (get_float_arg(argvars, &f) == OK)
2244 rettv->vval.v_float = ceil(f);
2245 else
2246 rettv->vval.v_float = 0.0;
2247}
2248#endif
2249
2250#ifdef FEAT_JOB_CHANNEL
2251/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002252 * "ch_canread()" function
2253 */
2254 static void
2255f_ch_canread(typval_T *argvars, typval_T *rettv)
2256{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002257 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002258
2259 rettv->vval.v_number = 0;
2260 if (channel != NULL)
2261 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2262 || channel_has_readahead(channel, PART_OUT)
2263 || channel_has_readahead(channel, PART_ERR);
2264}
2265
2266/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002267 * "ch_close()" function
2268 */
2269 static void
2270f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2271{
2272 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2273
2274 if (channel != NULL)
2275 {
2276 channel_close(channel, FALSE);
2277 channel_clear(channel);
2278 }
2279}
2280
2281/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002282 * "ch_close()" function
2283 */
2284 static void
2285f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2286{
2287 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2288
2289 if (channel != NULL)
2290 channel_close_in(channel);
2291}
2292
2293/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002294 * "ch_getbufnr()" function
2295 */
2296 static void
2297f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2298{
2299 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2300
2301 rettv->vval.v_number = -1;
2302 if (channel != NULL)
2303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002304 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002305 int part;
2306
2307 if (STRCMP(what, "err") == 0)
2308 part = PART_ERR;
2309 else if (STRCMP(what, "out") == 0)
2310 part = PART_OUT;
2311 else if (STRCMP(what, "in") == 0)
2312 part = PART_IN;
2313 else
2314 part = PART_SOCK;
2315 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2316 rettv->vval.v_number =
2317 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2318 }
2319}
2320
2321/*
2322 * "ch_getjob()" function
2323 */
2324 static void
2325f_ch_getjob(typval_T *argvars, typval_T *rettv)
2326{
2327 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2328
2329 if (channel != NULL)
2330 {
2331 rettv->v_type = VAR_JOB;
2332 rettv->vval.v_job = channel->ch_job;
2333 if (channel->ch_job != NULL)
2334 ++channel->ch_job->jv_refcount;
2335 }
2336}
2337
2338/*
2339 * "ch_info()" function
2340 */
2341 static void
2342f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2343{
2344 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2345
2346 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2347 channel_info(channel, rettv->vval.v_dict);
2348}
2349
2350/*
2351 * "ch_log()" function
2352 */
2353 static void
2354f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2355{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002356 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002357 channel_T *channel = NULL;
2358
2359 if (argvars[1].v_type != VAR_UNKNOWN)
2360 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2361
Bram Moolenaard5359b22018-04-05 22:44:39 +02002362 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002363}
2364
2365/*
2366 * "ch_logfile()" function
2367 */
2368 static void
2369f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2370{
2371 char_u *fname;
2372 char_u *opt = (char_u *)"";
2373 char_u buf[NUMBUFLEN];
2374
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002375 /* Don't open a file in restricted mode. */
2376 if (check_restricted() || check_secure())
2377 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002378 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002380 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002381 ch_logfile(fname, opt);
2382}
2383
2384/*
2385 * "ch_open()" function
2386 */
2387 static void
2388f_ch_open(typval_T *argvars, typval_T *rettv)
2389{
2390 rettv->v_type = VAR_CHANNEL;
2391 if (check_restricted() || check_secure())
2392 return;
2393 rettv->vval.v_channel = channel_open_func(argvars);
2394}
2395
2396/*
2397 * "ch_read()" function
2398 */
2399 static void
2400f_ch_read(typval_T *argvars, typval_T *rettv)
2401{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002402 common_channel_read(argvars, rettv, FALSE, FALSE);
2403}
2404
2405/*
2406 * "ch_readblob()" function
2407 */
2408 static void
2409f_ch_readblob(typval_T *argvars, typval_T *rettv)
2410{
2411 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412}
2413
2414/*
2415 * "ch_readraw()" function
2416 */
2417 static void
2418f_ch_readraw(typval_T *argvars, typval_T *rettv)
2419{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002420 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002421}
2422
2423/*
2424 * "ch_evalexpr()" function
2425 */
2426 static void
2427f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2428{
2429 ch_expr_common(argvars, rettv, TRUE);
2430}
2431
2432/*
2433 * "ch_sendexpr()" function
2434 */
2435 static void
2436f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2437{
2438 ch_expr_common(argvars, rettv, FALSE);
2439}
2440
2441/*
2442 * "ch_evalraw()" function
2443 */
2444 static void
2445f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2446{
2447 ch_raw_common(argvars, rettv, TRUE);
2448}
2449
2450/*
2451 * "ch_sendraw()" function
2452 */
2453 static void
2454f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2455{
2456 ch_raw_common(argvars, rettv, FALSE);
2457}
2458
2459/*
2460 * "ch_setoptions()" function
2461 */
2462 static void
2463f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2464{
2465 channel_T *channel;
2466 jobopt_T opt;
2467
2468 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2469 if (channel == NULL)
2470 return;
2471 clear_job_options(&opt);
2472 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002473 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002474 channel_set_options(channel, &opt);
2475 free_job_options(&opt);
2476}
2477
2478/*
2479 * "ch_status()" function
2480 */
2481 static void
2482f_ch_status(typval_T *argvars, typval_T *rettv)
2483{
2484 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002485 jobopt_T opt;
2486 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002487
2488 /* return an empty string by default */
2489 rettv->v_type = VAR_STRING;
2490 rettv->vval.v_string = NULL;
2491
2492 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002493
2494 if (argvars[1].v_type != VAR_UNKNOWN)
2495 {
2496 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002497 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002498 && (opt.jo_set & JO_PART))
2499 part = opt.jo_part;
2500 }
2501
2502 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002503}
2504#endif
2505
2506/*
2507 * "changenr()" function
2508 */
2509 static void
2510f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2511{
2512 rettv->vval.v_number = curbuf->b_u_seq_cur;
2513}
2514
2515/*
2516 * "char2nr(string)" function
2517 */
2518 static void
2519f_char2nr(typval_T *argvars, typval_T *rettv)
2520{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002521 if (has_mbyte)
2522 {
2523 int utf8 = 0;
2524
2525 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002526 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002527
2528 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002529 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002530 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002531 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002532 }
2533 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002534 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002535}
2536
2537/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002538 * "chdir(dir)" function
2539 */
2540 static void
2541f_chdir(typval_T *argvars, typval_T *rettv)
2542{
2543 char_u *cwd;
2544 cdscope_T scope = CDSCOPE_GLOBAL;
2545
2546 rettv->v_type = VAR_STRING;
2547 rettv->vval.v_string = NULL;
2548
2549 if (argvars[0].v_type != VAR_STRING)
2550 return;
2551
2552 // Return the current directory
2553 cwd = alloc(MAXPATHL);
2554 if (cwd != NULL)
2555 {
2556 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2557 {
2558#ifdef BACKSLASH_IN_FILENAME
2559 slash_adjust(cwd);
2560#endif
2561 rettv->vval.v_string = vim_strsave(cwd);
2562 }
2563 vim_free(cwd);
2564 }
2565
2566 if (curwin->w_localdir != NULL)
2567 scope = CDSCOPE_WINDOW;
2568 else if (curtab->tp_localdir != NULL)
2569 scope = CDSCOPE_TABPAGE;
2570
2571 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2572 // Directory change failed
2573 VIM_CLEAR(rettv->vval.v_string);
2574}
2575
2576/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002577 * "cindent(lnum)" function
2578 */
2579 static void
2580f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2581{
2582#ifdef FEAT_CINDENT
2583 pos_T pos;
2584 linenr_T lnum;
2585
2586 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002587 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002588 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2589 {
2590 curwin->w_cursor.lnum = lnum;
2591 rettv->vval.v_number = get_c_indent();
2592 curwin->w_cursor = pos;
2593 }
2594 else
2595#endif
2596 rettv->vval.v_number = -1;
2597}
2598
Bram Moolenaaraff74912019-03-30 18:11:49 +01002599 static win_T *
2600get_optional_window(typval_T *argvars, int idx)
2601{
2602 win_T *win = curwin;
2603
2604 if (argvars[idx].v_type != VAR_UNKNOWN)
2605 {
2606 win = find_win_by_nr_or_id(&argvars[idx]);
2607 if (win == NULL)
2608 {
2609 emsg(_(e_invalwindow));
2610 return NULL;
2611 }
2612 }
2613 return win;
2614}
2615
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002616/*
2617 * "clearmatches()" function
2618 */
2619 static void
2620f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2621{
2622#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002623 win_T *win = get_optional_window(argvars, 0);
2624
2625 if (win != NULL)
2626 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002627#endif
2628}
2629
2630/*
2631 * "col(string)" function
2632 */
2633 static void
2634f_col(typval_T *argvars, typval_T *rettv)
2635{
2636 colnr_T col = 0;
2637 pos_T *fp;
2638 int fnum = curbuf->b_fnum;
2639
2640 fp = var2fpos(&argvars[0], FALSE, &fnum);
2641 if (fp != NULL && fnum == curbuf->b_fnum)
2642 {
2643 if (fp->col == MAXCOL)
2644 {
2645 /* '> can be MAXCOL, get the length of the line then */
2646 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2647 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2648 else
2649 col = MAXCOL;
2650 }
2651 else
2652 {
2653 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002654 /* col(".") when the cursor is on the NUL at the end of the line
2655 * because of "coladd" can be seen as an extra column. */
2656 if (virtual_active() && fp == &curwin->w_cursor)
2657 {
2658 char_u *p = ml_get_cursor();
2659
2660 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2661 curwin->w_virtcol - curwin->w_cursor.coladd))
2662 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002663 int l;
2664
2665 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2666 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002667 }
2668 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002669 }
2670 }
2671 rettv->vval.v_number = col;
2672}
2673
2674#if defined(FEAT_INS_EXPAND)
2675/*
2676 * "complete()" function
2677 */
2678 static void
2679f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2680{
2681 int startcol;
2682
2683 if ((State & INSERT) == 0)
2684 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002685 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002686 return;
2687 }
2688
2689 /* Check for undo allowed here, because if something was already inserted
2690 * the line was already saved for undo and this check isn't done. */
2691 if (!undo_allowed())
2692 return;
2693
2694 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2695 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002696 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002697 return;
2698 }
2699
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002700 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002701 if (startcol <= 0)
2702 return;
2703
2704 set_completion(startcol - 1, argvars[1].vval.v_list);
2705}
2706
2707/*
2708 * "complete_add()" function
2709 */
2710 static void
2711f_complete_add(typval_T *argvars, typval_T *rettv)
2712{
2713 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2714}
2715
2716/*
2717 * "complete_check()" function
2718 */
2719 static void
2720f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2721{
2722 int saved = RedrawingDisabled;
2723
2724 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002725 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002726 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002727 RedrawingDisabled = saved;
2728}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002729
2730/*
2731 * "complete_info()" function
2732 */
2733 static void
2734f_complete_info(typval_T *argvars, typval_T *rettv)
2735{
2736 list_T *what_list = NULL;
2737
2738 if (rettv_dict_alloc(rettv) != OK)
2739 return;
2740
2741 if (argvars[0].v_type != VAR_UNKNOWN)
2742 {
2743 if (argvars[0].v_type != VAR_LIST)
2744 {
2745 emsg(_(e_listreq));
2746 return;
2747 }
2748 what_list = argvars[0].vval.v_list;
2749 }
2750 get_complete_info(what_list, rettv->vval.v_dict);
2751}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002752#endif
2753
2754/*
2755 * "confirm(message, buttons[, default [, type]])" function
2756 */
2757 static void
2758f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2759{
2760#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2761 char_u *message;
2762 char_u *buttons = NULL;
2763 char_u buf[NUMBUFLEN];
2764 char_u buf2[NUMBUFLEN];
2765 int def = 1;
2766 int type = VIM_GENERIC;
2767 char_u *typestr;
2768 int error = FALSE;
2769
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002770 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002771 if (message == NULL)
2772 error = TRUE;
2773 if (argvars[1].v_type != VAR_UNKNOWN)
2774 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002775 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002776 if (buttons == NULL)
2777 error = TRUE;
2778 if (argvars[2].v_type != VAR_UNKNOWN)
2779 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002780 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002781 if (argvars[3].v_type != VAR_UNKNOWN)
2782 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002783 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 if (typestr == NULL)
2785 error = TRUE;
2786 else
2787 {
2788 switch (TOUPPER_ASC(*typestr))
2789 {
2790 case 'E': type = VIM_ERROR; break;
2791 case 'Q': type = VIM_QUESTION; break;
2792 case 'I': type = VIM_INFO; break;
2793 case 'W': type = VIM_WARNING; break;
2794 case 'G': type = VIM_GENERIC; break;
2795 }
2796 }
2797 }
2798 }
2799 }
2800
2801 if (buttons == NULL || *buttons == NUL)
2802 buttons = (char_u *)_("&Ok");
2803
2804 if (!error)
2805 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2806 def, NULL, FALSE);
2807#endif
2808}
2809
2810/*
2811 * "copy()" function
2812 */
2813 static void
2814f_copy(typval_T *argvars, typval_T *rettv)
2815{
2816 item_copy(&argvars[0], rettv, FALSE, 0);
2817}
2818
2819#ifdef FEAT_FLOAT
2820/*
2821 * "cos()" function
2822 */
2823 static void
2824f_cos(typval_T *argvars, typval_T *rettv)
2825{
2826 float_T f = 0.0;
2827
2828 rettv->v_type = VAR_FLOAT;
2829 if (get_float_arg(argvars, &f) == OK)
2830 rettv->vval.v_float = cos(f);
2831 else
2832 rettv->vval.v_float = 0.0;
2833}
2834
2835/*
2836 * "cosh()" function
2837 */
2838 static void
2839f_cosh(typval_T *argvars, typval_T *rettv)
2840{
2841 float_T f = 0.0;
2842
2843 rettv->v_type = VAR_FLOAT;
2844 if (get_float_arg(argvars, &f) == OK)
2845 rettv->vval.v_float = cosh(f);
2846 else
2847 rettv->vval.v_float = 0.0;
2848}
2849#endif
2850
2851/*
2852 * "count()" function
2853 */
2854 static void
2855f_count(typval_T *argvars, typval_T *rettv)
2856{
2857 long n = 0;
2858 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002859 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002860
Bram Moolenaar9966b212017-07-28 16:46:57 +02002861 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002862 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002863
2864 if (argvars[0].v_type == VAR_STRING)
2865 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002866 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002867 char_u *p = argvars[0].vval.v_string;
2868 char_u *next;
2869
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002870 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002871 {
2872 if (ic)
2873 {
2874 size_t len = STRLEN(expr);
2875
2876 while (*p != NUL)
2877 {
2878 if (MB_STRNICMP(p, expr, len) == 0)
2879 {
2880 ++n;
2881 p += len;
2882 }
2883 else
2884 MB_PTR_ADV(p);
2885 }
2886 }
2887 else
2888 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2889 != NULL)
2890 {
2891 ++n;
2892 p = next + STRLEN(expr);
2893 }
2894 }
2895
2896 }
2897 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002898 {
2899 listitem_T *li;
2900 list_T *l;
2901 long idx;
2902
2903 if ((l = argvars[0].vval.v_list) != NULL)
2904 {
2905 li = l->lv_first;
2906 if (argvars[2].v_type != VAR_UNKNOWN)
2907 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002908 if (argvars[3].v_type != VAR_UNKNOWN)
2909 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002910 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002911 if (!error)
2912 {
2913 li = list_find(l, idx);
2914 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002915 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002916 }
2917 }
2918 if (error)
2919 li = NULL;
2920 }
2921
2922 for ( ; li != NULL; li = li->li_next)
2923 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2924 ++n;
2925 }
2926 }
2927 else if (argvars[0].v_type == VAR_DICT)
2928 {
2929 int todo;
2930 dict_T *d;
2931 hashitem_T *hi;
2932
2933 if ((d = argvars[0].vval.v_dict) != NULL)
2934 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002935 if (argvars[2].v_type != VAR_UNKNOWN)
2936 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002937 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002938 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002939 }
2940
2941 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2942 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2943 {
2944 if (!HASHITEM_EMPTY(hi))
2945 {
2946 --todo;
2947 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2948 ++n;
2949 }
2950 }
2951 }
2952 }
2953 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002954 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002955 rettv->vval.v_number = n;
2956}
2957
2958/*
2959 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2960 *
2961 * Checks the existence of a cscope connection.
2962 */
2963 static void
2964f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2965{
2966#ifdef FEAT_CSCOPE
2967 int num = 0;
2968 char_u *dbpath = NULL;
2969 char_u *prepend = NULL;
2970 char_u buf[NUMBUFLEN];
2971
2972 if (argvars[0].v_type != VAR_UNKNOWN
2973 && argvars[1].v_type != VAR_UNKNOWN)
2974 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002975 num = (int)tv_get_number(&argvars[0]);
2976 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002977 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002978 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002979 }
2980
2981 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2982#endif
2983}
2984
2985/*
2986 * "cursor(lnum, col)" function, or
2987 * "cursor(list)"
2988 *
2989 * Moves the cursor to the specified line and column.
2990 * Returns 0 when the position could be set, -1 otherwise.
2991 */
2992 static void
2993f_cursor(typval_T *argvars, typval_T *rettv)
2994{
2995 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002996 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002997 int set_curswant = TRUE;
2998
2999 rettv->vval.v_number = -1;
3000 if (argvars[1].v_type == VAR_UNKNOWN)
3001 {
3002 pos_T pos;
3003 colnr_T curswant = -1;
3004
3005 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003007 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003008 return;
3009 }
3010 line = pos.lnum;
3011 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003012 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003013 if (curswant >= 0)
3014 {
3015 curwin->w_curswant = curswant - 1;
3016 set_curswant = FALSE;
3017 }
3018 }
3019 else
3020 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003021 line = tv_get_lnum(argvars);
3022 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003023 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003024 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003025 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003026 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027 return; /* type error; errmsg already given */
3028 if (line > 0)
3029 curwin->w_cursor.lnum = line;
3030 if (col > 0)
3031 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003032 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033
3034 /* Make sure the cursor is in a valid position. */
3035 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003036 /* Correct cursor for multi-byte character. */
3037 if (has_mbyte)
3038 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003039
3040 curwin->w_set_curswant = set_curswant;
3041 rettv->vval.v_number = 0;
3042}
3043
Bram Moolenaar4f974752019-02-17 17:44:42 +01003044#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003045/*
3046 * "debugbreak()" function
3047 */
3048 static void
3049f_debugbreak(typval_T *argvars, typval_T *rettv)
3050{
3051 int pid;
3052
3053 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003054 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003055 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003056 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003057 else
3058 {
3059 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3060
3061 if (hProcess != NULL)
3062 {
3063 DebugBreakProcess(hProcess);
3064 CloseHandle(hProcess);
3065 rettv->vval.v_number = OK;
3066 }
3067 }
3068}
3069#endif
3070
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003071/*
3072 * "deepcopy()" function
3073 */
3074 static void
3075f_deepcopy(typval_T *argvars, typval_T *rettv)
3076{
3077 int noref = 0;
3078 int copyID;
3079
3080 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003081 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003082 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003083 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003084 else
3085 {
3086 copyID = get_copyID();
3087 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3088 }
3089}
3090
3091/*
3092 * "delete()" function
3093 */
3094 static void
3095f_delete(typval_T *argvars, typval_T *rettv)
3096{
3097 char_u nbuf[NUMBUFLEN];
3098 char_u *name;
3099 char_u *flags;
3100
3101 rettv->vval.v_number = -1;
3102 if (check_restricted() || check_secure())
3103 return;
3104
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003105 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003106 if (name == NULL || *name == NUL)
3107 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003108 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003109 return;
3110 }
3111
3112 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003113 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003114 else
3115 flags = (char_u *)"";
3116
3117 if (*flags == NUL)
3118 /* delete a file */
3119 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3120 else if (STRCMP(flags, "d") == 0)
3121 /* delete an empty directory */
3122 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3123 else if (STRCMP(flags, "rf") == 0)
3124 /* delete a directory recursively */
3125 rettv->vval.v_number = delete_recursive(name);
3126 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003127 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003128}
3129
3130/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003131 * "deletebufline()" function
3132 */
3133 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003134f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003135{
3136 buf_T *buf;
3137 linenr_T first, last;
3138 linenr_T lnum;
3139 long count;
3140 int is_curbuf;
3141 buf_T *curbuf_save = NULL;
3142 win_T *curwin_save = NULL;
3143 tabpage_T *tp;
3144 win_T *wp;
3145
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003146 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003147 if (buf == NULL)
3148 {
3149 rettv->vval.v_number = 1; /* FAIL */
3150 return;
3151 }
3152 is_curbuf = buf == curbuf;
3153
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003154 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003155 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003156 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003157 else
3158 last = first;
3159
3160 if (buf->b_ml.ml_mfp == NULL || first < 1
3161 || first > buf->b_ml.ml_line_count || last < first)
3162 {
3163 rettv->vval.v_number = 1; /* FAIL */
3164 return;
3165 }
3166
3167 if (!is_curbuf)
3168 {
3169 curbuf_save = curbuf;
3170 curwin_save = curwin;
3171 curbuf = buf;
3172 find_win_for_curbuf();
3173 }
3174 if (last > curbuf->b_ml.ml_line_count)
3175 last = curbuf->b_ml.ml_line_count;
3176 count = last - first + 1;
3177
3178 // When coming here from Insert mode, sync undo, so that this can be
3179 // undone separately from what was previously inserted.
3180 if (u_sync_once == 2)
3181 {
3182 u_sync_once = 1; // notify that u_sync() was called
3183 u_sync(TRUE);
3184 }
3185
3186 if (u_save(first - 1, last + 1) == FAIL)
3187 {
3188 rettv->vval.v_number = 1; /* FAIL */
3189 return;
3190 }
3191
3192 for (lnum = first; lnum <= last; ++lnum)
3193 ml_delete(first, TRUE);
3194
3195 FOR_ALL_TAB_WINDOWS(tp, wp)
3196 if (wp->w_buffer == buf)
3197 {
3198 if (wp->w_cursor.lnum > last)
3199 wp->w_cursor.lnum -= count;
3200 else if (wp->w_cursor.lnum> first)
3201 wp->w_cursor.lnum = first;
3202 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3203 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3204 }
3205 check_cursor_col();
3206 deleted_lines_mark(first, count);
3207
3208 if (!is_curbuf)
3209 {
3210 curbuf = curbuf_save;
3211 curwin = curwin_save;
3212 }
3213}
3214
3215/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003216 * "did_filetype()" function
3217 */
3218 static void
3219f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3220{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003221 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003222}
3223
3224/*
3225 * "diff_filler()" function
3226 */
3227 static void
3228f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3229{
3230#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003231 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003232#endif
3233}
3234
3235/*
3236 * "diff_hlID()" function
3237 */
3238 static void
3239f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3240{
3241#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003242 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003243 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003244 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003245 static int fnum = 0;
3246 static int change_start = 0;
3247 static int change_end = 0;
3248 static hlf_T hlID = (hlf_T)0;
3249 int filler_lines;
3250 int col;
3251
3252 if (lnum < 0) /* ignore type error in {lnum} arg */
3253 lnum = 0;
3254 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003255 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003256 || fnum != curbuf->b_fnum)
3257 {
3258 /* New line, buffer, change: need to get the values. */
3259 filler_lines = diff_check(curwin, lnum);
3260 if (filler_lines < 0)
3261 {
3262 if (filler_lines == -1)
3263 {
3264 change_start = MAXCOL;
3265 change_end = -1;
3266 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3267 hlID = HLF_ADD; /* added line */
3268 else
3269 hlID = HLF_CHD; /* changed line */
3270 }
3271 else
3272 hlID = HLF_ADD; /* added line */
3273 }
3274 else
3275 hlID = (hlf_T)0;
3276 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003277 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003278 fnum = curbuf->b_fnum;
3279 }
3280
3281 if (hlID == HLF_CHD || hlID == HLF_TXD)
3282 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003283 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003284 if (col >= change_start && col <= change_end)
3285 hlID = HLF_TXD; /* changed text */
3286 else
3287 hlID = HLF_CHD; /* changed line */
3288 }
3289 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3290#endif
3291}
3292
3293/*
3294 * "empty({expr})" function
3295 */
3296 static void
3297f_empty(typval_T *argvars, typval_T *rettv)
3298{
3299 int n = FALSE;
3300
3301 switch (argvars[0].v_type)
3302 {
3303 case VAR_STRING:
3304 case VAR_FUNC:
3305 n = argvars[0].vval.v_string == NULL
3306 || *argvars[0].vval.v_string == NUL;
3307 break;
3308 case VAR_PARTIAL:
3309 n = FALSE;
3310 break;
3311 case VAR_NUMBER:
3312 n = argvars[0].vval.v_number == 0;
3313 break;
3314 case VAR_FLOAT:
3315#ifdef FEAT_FLOAT
3316 n = argvars[0].vval.v_float == 0.0;
3317 break;
3318#endif
3319 case VAR_LIST:
3320 n = argvars[0].vval.v_list == NULL
3321 || argvars[0].vval.v_list->lv_first == NULL;
3322 break;
3323 case VAR_DICT:
3324 n = argvars[0].vval.v_dict == NULL
3325 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3326 break;
3327 case VAR_SPECIAL:
3328 n = argvars[0].vval.v_number != VVAL_TRUE;
3329 break;
3330
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003331 case VAR_BLOB:
3332 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003333 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3334 break;
3335
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003336 case VAR_JOB:
3337#ifdef FEAT_JOB_CHANNEL
3338 n = argvars[0].vval.v_job == NULL
3339 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3340 break;
3341#endif
3342 case VAR_CHANNEL:
3343#ifdef FEAT_JOB_CHANNEL
3344 n = argvars[0].vval.v_channel == NULL
3345 || !channel_is_open(argvars[0].vval.v_channel);
3346 break;
3347#endif
3348 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003349 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003350 n = TRUE;
3351 break;
3352 }
3353
3354 rettv->vval.v_number = n;
3355}
3356
3357/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003358 * "environ()" function
3359 */
3360 static void
3361f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3362{
3363#if !defined(AMIGA)
3364 int i = 0;
3365 char_u *entry, *value;
3366# ifdef MSWIN
3367 extern wchar_t **_wenviron;
3368# else
3369 extern char **environ;
3370# endif
3371
3372 if (rettv_dict_alloc(rettv) != OK)
3373 return;
3374
3375# ifdef MSWIN
3376 if (*_wenviron == NULL)
3377 return;
3378# else
3379 if (*environ == NULL)
3380 return;
3381# endif
3382
3383 for (i = 0; ; ++i)
3384 {
3385# ifdef MSWIN
3386 short_u *p;
3387
3388 if ((p = (short_u *)_wenviron[i]) == NULL)
3389 return;
3390 entry = utf16_to_enc(p, NULL);
3391# else
3392 if ((entry = (char_u *)environ[i]) == NULL)
3393 return;
3394 entry = vim_strsave(entry);
3395# endif
3396 if (entry == NULL) // out of memory
3397 return;
3398 if ((value = vim_strchr(entry, '=')) == NULL)
3399 {
3400 vim_free(entry);
3401 continue;
3402 }
3403 *value++ = NUL;
3404 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3405 vim_free(entry);
3406 }
3407#endif
3408}
3409
3410/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003411 * "escape({string}, {chars})" function
3412 */
3413 static void
3414f_escape(typval_T *argvars, typval_T *rettv)
3415{
3416 char_u buf[NUMBUFLEN];
3417
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003418 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3419 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003420 rettv->v_type = VAR_STRING;
3421}
3422
3423/*
3424 * "eval()" function
3425 */
3426 static void
3427f_eval(typval_T *argvars, typval_T *rettv)
3428{
3429 char_u *s, *p;
3430
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003431 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003432 if (s != NULL)
3433 s = skipwhite(s);
3434
3435 p = s;
3436 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3437 {
3438 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003439 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003440 need_clr_eos = FALSE;
3441 rettv->v_type = VAR_NUMBER;
3442 rettv->vval.v_number = 0;
3443 }
3444 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003445 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003446}
3447
3448/*
3449 * "eventhandler()" function
3450 */
3451 static void
3452f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3453{
3454 rettv->vval.v_number = vgetc_busy;
3455}
3456
3457/*
3458 * "executable()" function
3459 */
3460 static void
3461f_executable(typval_T *argvars, typval_T *rettv)
3462{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003463 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003464
3465 /* Check in $PATH and also check directly if there is a directory name. */
3466 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3467 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3468}
3469
3470static garray_T redir_execute_ga;
3471
3472/*
3473 * Append "value[value_len]" to the execute() output.
3474 */
3475 void
3476execute_redir_str(char_u *value, int value_len)
3477{
3478 int len;
3479
3480 if (value_len == -1)
3481 len = (int)STRLEN(value); /* Append the entire string */
3482 else
3483 len = value_len; /* Append only "value_len" characters */
3484 if (ga_grow(&redir_execute_ga, len) == OK)
3485 {
3486 mch_memmove((char *)redir_execute_ga.ga_data
3487 + redir_execute_ga.ga_len, value, len);
3488 redir_execute_ga.ga_len += len;
3489 }
3490}
3491
3492/*
3493 * Get next line from a list.
3494 * Called by do_cmdline() to get the next line.
3495 * Returns allocated string, or NULL for end of function.
3496 */
3497
3498 static char_u *
3499get_list_line(
3500 int c UNUSED,
3501 void *cookie,
3502 int indent UNUSED)
3503{
3504 listitem_T **p = (listitem_T **)cookie;
3505 listitem_T *item = *p;
3506 char_u buf[NUMBUFLEN];
3507 char_u *s;
3508
3509 if (item == NULL)
3510 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003511 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003512 *p = item->li_next;
3513 return s == NULL ? NULL : vim_strsave(s);
3514}
3515
3516/*
3517 * "execute()" function
3518 */
3519 static void
3520f_execute(typval_T *argvars, typval_T *rettv)
3521{
3522 char_u *cmd = NULL;
3523 list_T *list = NULL;
3524 int save_msg_silent = msg_silent;
3525 int save_emsg_silent = emsg_silent;
3526 int save_emsg_noredir = emsg_noredir;
3527 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003528 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003529 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003530 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003531 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003532
3533 rettv->vval.v_string = NULL;
3534 rettv->v_type = VAR_STRING;
3535
3536 if (argvars[0].v_type == VAR_LIST)
3537 {
3538 list = argvars[0].vval.v_list;
3539 if (list == NULL || list->lv_first == NULL)
3540 /* empty list, no commands, empty output */
3541 return;
3542 ++list->lv_refcount;
3543 }
3544 else
3545 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003546 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547 if (cmd == NULL)
3548 return;
3549 }
3550
3551 if (argvars[1].v_type != VAR_UNKNOWN)
3552 {
3553 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003554 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003555
3556 if (s == NULL)
3557 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003558 if (*s == NUL)
3559 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003560 if (STRNCMP(s, "silent", 6) == 0)
3561 ++msg_silent;
3562 if (STRCMP(s, "silent!") == 0)
3563 {
3564 emsg_silent = TRUE;
3565 emsg_noredir = TRUE;
3566 }
3567 }
3568 else
3569 ++msg_silent;
3570
3571 if (redir_execute)
3572 save_ga = redir_execute_ga;
3573 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3574 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003575 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003576 if (!echo_output)
3577 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003578
3579 if (cmd != NULL)
3580 do_cmdline_cmd(cmd);
3581 else
3582 {
3583 listitem_T *item = list->lv_first;
3584
3585 do_cmdline(NULL, get_list_line, (void *)&item,
3586 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3587 --list->lv_refcount;
3588 }
3589
Bram Moolenaard297f352017-01-29 20:31:21 +01003590 /* Need to append a NUL to the result. */
3591 if (ga_grow(&redir_execute_ga, 1) == OK)
3592 {
3593 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3594 rettv->vval.v_string = redir_execute_ga.ga_data;
3595 }
3596 else
3597 {
3598 ga_clear(&redir_execute_ga);
3599 rettv->vval.v_string = NULL;
3600 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003601 msg_silent = save_msg_silent;
3602 emsg_silent = save_emsg_silent;
3603 emsg_noredir = save_emsg_noredir;
3604
3605 redir_execute = save_redir_execute;
3606 if (redir_execute)
3607 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003608 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003609
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003610 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003611 if (echo_output)
3612 // When not working silently: put it in column zero. A following
3613 // "echon" will overwrite the message, unavoidably.
3614 msg_col = 0;
3615 else
3616 // When working silently: Put it back where it was, since nothing
3617 // should have been written.
3618 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003619}
3620
3621/*
3622 * "exepath()" function
3623 */
3624 static void
3625f_exepath(typval_T *argvars, typval_T *rettv)
3626{
3627 char_u *p = NULL;
3628
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003629 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003630 rettv->v_type = VAR_STRING;
3631 rettv->vval.v_string = p;
3632}
3633
3634/*
3635 * "exists()" function
3636 */
3637 static void
3638f_exists(typval_T *argvars, typval_T *rettv)
3639{
3640 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003641 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003642
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003643 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003644 if (*p == '$') /* environment variable */
3645 {
3646 /* first try "normal" environment variables (fast) */
3647 if (mch_getenv(p + 1) != NULL)
3648 n = TRUE;
3649 else
3650 {
3651 /* try expanding things like $VIM and ${HOME} */
3652 p = expand_env_save(p);
3653 if (p != NULL && *p != '$')
3654 n = TRUE;
3655 vim_free(p);
3656 }
3657 }
3658 else if (*p == '&' || *p == '+') /* option */
3659 {
3660 n = (get_option_tv(&p, NULL, TRUE) == OK);
3661 if (*skipwhite(p) != NUL)
3662 n = FALSE; /* trailing garbage */
3663 }
3664 else if (*p == '*') /* internal or user defined function */
3665 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003666 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003667 }
3668 else if (*p == ':')
3669 {
3670 n = cmd_exists(p + 1);
3671 }
3672 else if (*p == '#')
3673 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003674 if (p[1] == '#')
3675 n = autocmd_supported(p + 2);
3676 else
3677 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003678 }
3679 else /* internal variable */
3680 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003681 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003682 }
3683
3684 rettv->vval.v_number = n;
3685}
3686
3687#ifdef FEAT_FLOAT
3688/*
3689 * "exp()" function
3690 */
3691 static void
3692f_exp(typval_T *argvars, typval_T *rettv)
3693{
3694 float_T f = 0.0;
3695
3696 rettv->v_type = VAR_FLOAT;
3697 if (get_float_arg(argvars, &f) == OK)
3698 rettv->vval.v_float = exp(f);
3699 else
3700 rettv->vval.v_float = 0.0;
3701}
3702#endif
3703
3704/*
3705 * "expand()" function
3706 */
3707 static void
3708f_expand(typval_T *argvars, typval_T *rettv)
3709{
3710 char_u *s;
3711 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003712 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003713 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3714 expand_T xpc;
3715 int error = FALSE;
3716 char_u *result;
3717
3718 rettv->v_type = VAR_STRING;
3719 if (argvars[1].v_type != VAR_UNKNOWN
3720 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003721 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003722 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003723 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003724
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003725 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003726 if (*s == '%' || *s == '#' || *s == '<')
3727 {
3728 ++emsg_off;
3729 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3730 --emsg_off;
3731 if (rettv->v_type == VAR_LIST)
3732 {
3733 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3734 list_append_string(rettv->vval.v_list, result, -1);
3735 else
3736 vim_free(result);
3737 }
3738 else
3739 rettv->vval.v_string = result;
3740 }
3741 else
3742 {
3743 /* When the optional second argument is non-zero, don't remove matches
3744 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3745 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003746 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003747 options |= WILD_KEEP_ALL;
3748 if (!error)
3749 {
3750 ExpandInit(&xpc);
3751 xpc.xp_context = EXPAND_FILES;
3752 if (p_wic)
3753 options += WILD_ICASE;
3754 if (rettv->v_type == VAR_STRING)
3755 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3756 options, WILD_ALL);
3757 else if (rettv_list_alloc(rettv) != FAIL)
3758 {
3759 int i;
3760
3761 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3762 for (i = 0; i < xpc.xp_numfiles; i++)
3763 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3764 ExpandCleanup(&xpc);
3765 }
3766 }
3767 else
3768 rettv->vval.v_string = NULL;
3769 }
3770}
3771
3772/*
3773 * "extend(list, list [, idx])" function
3774 * "extend(dict, dict [, action])" function
3775 */
3776 static void
3777f_extend(typval_T *argvars, typval_T *rettv)
3778{
3779 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3780
3781 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3782 {
3783 list_T *l1, *l2;
3784 listitem_T *item;
3785 long before;
3786 int error = FALSE;
3787
3788 l1 = argvars[0].vval.v_list;
3789 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003790 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003791 && l2 != NULL)
3792 {
3793 if (argvars[2].v_type != VAR_UNKNOWN)
3794 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003795 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003796 if (error)
3797 return; /* type error; errmsg already given */
3798
3799 if (before == l1->lv_len)
3800 item = NULL;
3801 else
3802 {
3803 item = list_find(l1, before);
3804 if (item == NULL)
3805 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003806 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003807 return;
3808 }
3809 }
3810 }
3811 else
3812 item = NULL;
3813 list_extend(l1, l2, item);
3814
3815 copy_tv(&argvars[0], rettv);
3816 }
3817 }
3818 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3819 {
3820 dict_T *d1, *d2;
3821 char_u *action;
3822 int i;
3823
3824 d1 = argvars[0].vval.v_dict;
3825 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003826 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003827 && d2 != NULL)
3828 {
3829 /* Check the third argument. */
3830 if (argvars[2].v_type != VAR_UNKNOWN)
3831 {
3832 static char *(av[]) = {"keep", "force", "error"};
3833
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003834 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003835 if (action == NULL)
3836 return; /* type error; errmsg already given */
3837 for (i = 0; i < 3; ++i)
3838 if (STRCMP(action, av[i]) == 0)
3839 break;
3840 if (i == 3)
3841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003842 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003843 return;
3844 }
3845 }
3846 else
3847 action = (char_u *)"force";
3848
3849 dict_extend(d1, d2, action);
3850
3851 copy_tv(&argvars[0], rettv);
3852 }
3853 }
3854 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003855 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003856}
3857
3858/*
3859 * "feedkeys()" function
3860 */
3861 static void
3862f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3863{
3864 int remap = TRUE;
3865 int insert = FALSE;
3866 char_u *keys, *flags;
3867 char_u nbuf[NUMBUFLEN];
3868 int typed = FALSE;
3869 int execute = FALSE;
3870 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003871 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003872 char_u *keys_esc;
3873
3874 /* This is not allowed in the sandbox. If the commands would still be
3875 * executed in the sandbox it would be OK, but it probably happens later,
3876 * when "sandbox" is no longer set. */
3877 if (check_secure())
3878 return;
3879
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003880 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003881
3882 if (argvars[1].v_type != VAR_UNKNOWN)
3883 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003884 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003885 for ( ; *flags != NUL; ++flags)
3886 {
3887 switch (*flags)
3888 {
3889 case 'n': remap = FALSE; break;
3890 case 'm': remap = TRUE; break;
3891 case 't': typed = TRUE; break;
3892 case 'i': insert = TRUE; break;
3893 case 'x': execute = TRUE; break;
3894 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003895 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003896 }
3897 }
3898 }
3899
3900 if (*keys != NUL || execute)
3901 {
3902 /* Need to escape K_SPECIAL and CSI before putting the string in the
3903 * typeahead buffer. */
3904 keys_esc = vim_strsave_escape_csi(keys);
3905 if (keys_esc != NULL)
3906 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003907 if (lowlevel)
3908 {
3909#ifdef USE_INPUT_BUF
3910 add_to_input_buf(keys, (int)STRLEN(keys));
3911#else
3912 emsg(_("E980: lowlevel input not supported"));
3913#endif
3914 }
3915 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003916 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003917 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003918 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003919 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003920#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003921 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003922#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003923 )
3924 typebuf_was_filled = TRUE;
3925 }
3926 vim_free(keys_esc);
3927
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003928 if (execute)
3929 {
3930 int save_msg_scroll = msg_scroll;
3931
3932 /* Avoid a 1 second delay when the keys start Insert mode. */
3933 msg_scroll = FALSE;
3934
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003935 if (!dangerous)
3936 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003937 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003938 if (!dangerous)
3939 --ex_normal_busy;
3940
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003941 msg_scroll |= save_msg_scroll;
3942 }
3943 }
3944 }
3945}
3946
3947/*
3948 * "filereadable()" function
3949 */
3950 static void
3951f_filereadable(typval_T *argvars, typval_T *rettv)
3952{
3953 int fd;
3954 char_u *p;
3955 int n;
3956
3957#ifndef O_NONBLOCK
3958# define O_NONBLOCK 0
3959#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003960 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003961 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3962 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3963 {
3964 n = TRUE;
3965 close(fd);
3966 }
3967 else
3968 n = FALSE;
3969
3970 rettv->vval.v_number = n;
3971}
3972
3973/*
3974 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3975 * rights to write into.
3976 */
3977 static void
3978f_filewritable(typval_T *argvars, typval_T *rettv)
3979{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003980 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003981}
3982
3983 static void
3984findfilendir(
3985 typval_T *argvars UNUSED,
3986 typval_T *rettv,
3987 int find_what UNUSED)
3988{
3989#ifdef FEAT_SEARCHPATH
3990 char_u *fname;
3991 char_u *fresult = NULL;
3992 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3993 char_u *p;
3994 char_u pathbuf[NUMBUFLEN];
3995 int count = 1;
3996 int first = TRUE;
3997 int error = FALSE;
3998#endif
3999
4000 rettv->vval.v_string = NULL;
4001 rettv->v_type = VAR_STRING;
4002
4003#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004004 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004005
4006 if (argvars[1].v_type != VAR_UNKNOWN)
4007 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004008 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004009 if (p == NULL)
4010 error = TRUE;
4011 else
4012 {
4013 if (*p != NUL)
4014 path = p;
4015
4016 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004017 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004018 }
4019 }
4020
4021 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4022 error = TRUE;
4023
4024 if (*fname != NUL && !error)
4025 {
4026 do
4027 {
4028 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4029 vim_free(fresult);
4030 fresult = find_file_in_path_option(first ? fname : NULL,
4031 first ? (int)STRLEN(fname) : 0,
4032 0, first, path,
4033 find_what,
4034 curbuf->b_ffname,
4035 find_what == FINDFILE_DIR
4036 ? (char_u *)"" : curbuf->b_p_sua);
4037 first = FALSE;
4038
4039 if (fresult != NULL && rettv->v_type == VAR_LIST)
4040 list_append_string(rettv->vval.v_list, fresult, -1);
4041
4042 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4043 }
4044
4045 if (rettv->v_type == VAR_STRING)
4046 rettv->vval.v_string = fresult;
4047#endif
4048}
4049
4050/*
4051 * "filter()" function
4052 */
4053 static void
4054f_filter(typval_T *argvars, typval_T *rettv)
4055{
4056 filter_map(argvars, rettv, FALSE);
4057}
4058
4059/*
4060 * "finddir({fname}[, {path}[, {count}]])" function
4061 */
4062 static void
4063f_finddir(typval_T *argvars, typval_T *rettv)
4064{
4065 findfilendir(argvars, rettv, FINDFILE_DIR);
4066}
4067
4068/*
4069 * "findfile({fname}[, {path}[, {count}]])" function
4070 */
4071 static void
4072f_findfile(typval_T *argvars, typval_T *rettv)
4073{
4074 findfilendir(argvars, rettv, FINDFILE_FILE);
4075}
4076
4077#ifdef FEAT_FLOAT
4078/*
4079 * "float2nr({float})" function
4080 */
4081 static void
4082f_float2nr(typval_T *argvars, typval_T *rettv)
4083{
4084 float_T f = 0.0;
4085
4086 if (get_float_arg(argvars, &f) == OK)
4087 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004088 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004089 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004090 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004091 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004092 else
4093 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004094 }
4095}
4096
4097/*
4098 * "floor({float})" function
4099 */
4100 static void
4101f_floor(typval_T *argvars, typval_T *rettv)
4102{
4103 float_T f = 0.0;
4104
4105 rettv->v_type = VAR_FLOAT;
4106 if (get_float_arg(argvars, &f) == OK)
4107 rettv->vval.v_float = floor(f);
4108 else
4109 rettv->vval.v_float = 0.0;
4110}
4111
4112/*
4113 * "fmod()" function
4114 */
4115 static void
4116f_fmod(typval_T *argvars, typval_T *rettv)
4117{
4118 float_T fx = 0.0, fy = 0.0;
4119
4120 rettv->v_type = VAR_FLOAT;
4121 if (get_float_arg(argvars, &fx) == OK
4122 && get_float_arg(&argvars[1], &fy) == OK)
4123 rettv->vval.v_float = fmod(fx, fy);
4124 else
4125 rettv->vval.v_float = 0.0;
4126}
4127#endif
4128
4129/*
4130 * "fnameescape({string})" function
4131 */
4132 static void
4133f_fnameescape(typval_T *argvars, typval_T *rettv)
4134{
4135 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004136 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004137 rettv->v_type = VAR_STRING;
4138}
4139
4140/*
4141 * "fnamemodify({fname}, {mods})" function
4142 */
4143 static void
4144f_fnamemodify(typval_T *argvars, typval_T *rettv)
4145{
4146 char_u *fname;
4147 char_u *mods;
4148 int usedlen = 0;
4149 int len;
4150 char_u *fbuf = NULL;
4151 char_u buf[NUMBUFLEN];
4152
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004153 fname = tv_get_string_chk(&argvars[0]);
4154 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004155 if (fname == NULL || mods == NULL)
4156 fname = NULL;
4157 else
4158 {
4159 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004160 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004161 }
4162
4163 rettv->v_type = VAR_STRING;
4164 if (fname == NULL)
4165 rettv->vval.v_string = NULL;
4166 else
4167 rettv->vval.v_string = vim_strnsave(fname, len);
4168 vim_free(fbuf);
4169}
4170
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004171/*
4172 * "foldclosed()" function
4173 */
4174 static void
4175foldclosed_both(
4176 typval_T *argvars UNUSED,
4177 typval_T *rettv,
4178 int end UNUSED)
4179{
4180#ifdef FEAT_FOLDING
4181 linenr_T lnum;
4182 linenr_T first, last;
4183
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004184 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004185 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4186 {
4187 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4188 {
4189 if (end)
4190 rettv->vval.v_number = (varnumber_T)last;
4191 else
4192 rettv->vval.v_number = (varnumber_T)first;
4193 return;
4194 }
4195 }
4196#endif
4197 rettv->vval.v_number = -1;
4198}
4199
4200/*
4201 * "foldclosed()" function
4202 */
4203 static void
4204f_foldclosed(typval_T *argvars, typval_T *rettv)
4205{
4206 foldclosed_both(argvars, rettv, FALSE);
4207}
4208
4209/*
4210 * "foldclosedend()" function
4211 */
4212 static void
4213f_foldclosedend(typval_T *argvars, typval_T *rettv)
4214{
4215 foldclosed_both(argvars, rettv, TRUE);
4216}
4217
4218/*
4219 * "foldlevel()" function
4220 */
4221 static void
4222f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4223{
4224#ifdef FEAT_FOLDING
4225 linenr_T lnum;
4226
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004227 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004228 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4229 rettv->vval.v_number = foldLevel(lnum);
4230#endif
4231}
4232
4233/*
4234 * "foldtext()" function
4235 */
4236 static void
4237f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4238{
4239#ifdef FEAT_FOLDING
4240 linenr_T foldstart;
4241 linenr_T foldend;
4242 char_u *dashes;
4243 linenr_T lnum;
4244 char_u *s;
4245 char_u *r;
4246 int len;
4247 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004248 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004249#endif
4250
4251 rettv->v_type = VAR_STRING;
4252 rettv->vval.v_string = NULL;
4253#ifdef FEAT_FOLDING
4254 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4255 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4256 dashes = get_vim_var_str(VV_FOLDDASHES);
4257 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4258 && dashes != NULL)
4259 {
4260 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004261 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004262 if (!linewhite(lnum))
4263 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004264
4265 /* Find interesting text in this line. */
4266 s = skipwhite(ml_get(lnum));
4267 /* skip C comment-start */
4268 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4269 {
4270 s = skipwhite(s + 2);
4271 if (*skipwhite(s) == NUL
4272 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4273 {
4274 s = skipwhite(ml_get(lnum + 1));
4275 if (*s == '*')
4276 s = skipwhite(s + 1);
4277 }
4278 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004279 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004280 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004281 r = alloc(STRLEN(txt)
4282 + STRLEN(dashes) // for %s
4283 + 20 // for %3ld
4284 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004285 if (r != NULL)
4286 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004287 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004288 len = (int)STRLEN(r);
4289 STRCAT(r, s);
4290 /* remove 'foldmarker' and 'commentstring' */
4291 foldtext_cleanup(r + len);
4292 rettv->vval.v_string = r;
4293 }
4294 }
4295#endif
4296}
4297
4298/*
4299 * "foldtextresult(lnum)" function
4300 */
4301 static void
4302f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4303{
4304#ifdef FEAT_FOLDING
4305 linenr_T lnum;
4306 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004307 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004308 foldinfo_T foldinfo;
4309 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004310 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004311#endif
4312
4313 rettv->v_type = VAR_STRING;
4314 rettv->vval.v_string = NULL;
4315#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004316 if (entered)
4317 return; /* reject recursive use */
4318 entered = TRUE;
4319
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004320 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004321 /* treat illegal types and illegal string values for {lnum} the same */
4322 if (lnum < 0)
4323 lnum = 0;
4324 fold_count = foldedCount(curwin, lnum, &foldinfo);
4325 if (fold_count > 0)
4326 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004327 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4328 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004329 if (text == buf)
4330 text = vim_strsave(text);
4331 rettv->vval.v_string = text;
4332 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004333
4334 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004335#endif
4336}
4337
4338/*
4339 * "foreground()" function
4340 */
4341 static void
4342f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4343{
4344#ifdef FEAT_GUI
4345 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004346 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004347 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004348 return;
4349 }
4350#endif
4351#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004352 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004353#endif
4354}
4355
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004356 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004357common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004358{
4359 char_u *s;
4360 char_u *name;
4361 int use_string = FALSE;
4362 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004363 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004364
4365 if (argvars[0].v_type == VAR_FUNC)
4366 {
4367 /* function(MyFunc, [arg], dict) */
4368 s = argvars[0].vval.v_string;
4369 }
4370 else if (argvars[0].v_type == VAR_PARTIAL
4371 && argvars[0].vval.v_partial != NULL)
4372 {
4373 /* function(dict.MyFunc, [arg]) */
4374 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004375 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004376 }
4377 else
4378 {
4379 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004380 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004381 use_string = TRUE;
4382 }
4383
Bram Moolenaar843b8842016-08-21 14:36:15 +02004384 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004385 {
4386 name = s;
4387 trans_name = trans_function_name(&name, FALSE,
4388 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4389 if (*name != NUL)
4390 s = NULL;
4391 }
4392
Bram Moolenaar843b8842016-08-21 14:36:15 +02004393 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4394 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004395 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004396 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004397 else if (trans_name != NULL && (is_funcref
4398 ? find_func(trans_name) == NULL
4399 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004400 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004401 else
4402 {
4403 int dict_idx = 0;
4404 int arg_idx = 0;
4405 list_T *list = NULL;
4406
4407 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4408 {
4409 char sid_buf[25];
4410 int off = *s == 's' ? 2 : 5;
4411
4412 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4413 * also be called from another script. Using trans_function_name()
4414 * would also work, but some plugins depend on the name being
4415 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004416 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004417 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004418 if (name != NULL)
4419 {
4420 STRCPY(name, sid_buf);
4421 STRCAT(name, s + off);
4422 }
4423 }
4424 else
4425 name = vim_strsave(s);
4426
4427 if (argvars[1].v_type != VAR_UNKNOWN)
4428 {
4429 if (argvars[2].v_type != VAR_UNKNOWN)
4430 {
4431 /* function(name, [args], dict) */
4432 arg_idx = 1;
4433 dict_idx = 2;
4434 }
4435 else if (argvars[1].v_type == VAR_DICT)
4436 /* function(name, dict) */
4437 dict_idx = 1;
4438 else
4439 /* function(name, [args]) */
4440 arg_idx = 1;
4441 if (dict_idx > 0)
4442 {
4443 if (argvars[dict_idx].v_type != VAR_DICT)
4444 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004445 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004446 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004447 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004448 }
4449 if (argvars[dict_idx].vval.v_dict == NULL)
4450 dict_idx = 0;
4451 }
4452 if (arg_idx > 0)
4453 {
4454 if (argvars[arg_idx].v_type != VAR_LIST)
4455 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004456 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004457 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004458 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004459 }
4460 list = argvars[arg_idx].vval.v_list;
4461 if (list == NULL || list->lv_len == 0)
4462 arg_idx = 0;
4463 }
4464 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004465 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004466 {
4467 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4468
4469 /* result is a VAR_PARTIAL */
4470 if (pt == NULL)
4471 vim_free(name);
4472 else
4473 {
4474 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4475 {
4476 listitem_T *li;
4477 int i = 0;
4478 int arg_len = 0;
4479 int lv_len = 0;
4480
4481 if (arg_pt != NULL)
4482 arg_len = arg_pt->pt_argc;
4483 if (list != NULL)
4484 lv_len = list->lv_len;
4485 pt->pt_argc = arg_len + lv_len;
4486 pt->pt_argv = (typval_T *)alloc(
4487 sizeof(typval_T) * pt->pt_argc);
4488 if (pt->pt_argv == NULL)
4489 {
4490 vim_free(pt);
4491 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004492 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004493 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004494 for (i = 0; i < arg_len; i++)
4495 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4496 if (lv_len > 0)
4497 for (li = list->lv_first; li != NULL;
4498 li = li->li_next)
4499 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004500 }
4501
4502 /* For "function(dict.func, [], dict)" and "func" is a partial
4503 * use "dict". That is backwards compatible. */
4504 if (dict_idx > 0)
4505 {
4506 /* The dict is bound explicitly, pt_auto is FALSE. */
4507 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4508 ++pt->pt_dict->dv_refcount;
4509 }
4510 else if (arg_pt != NULL)
4511 {
4512 /* If the dict was bound automatically the result is also
4513 * bound automatically. */
4514 pt->pt_dict = arg_pt->pt_dict;
4515 pt->pt_auto = arg_pt->pt_auto;
4516 if (pt->pt_dict != NULL)
4517 ++pt->pt_dict->dv_refcount;
4518 }
4519
4520 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004521 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4522 {
4523 pt->pt_func = arg_pt->pt_func;
4524 func_ptr_ref(pt->pt_func);
4525 vim_free(name);
4526 }
4527 else if (is_funcref)
4528 {
4529 pt->pt_func = find_func(trans_name);
4530 func_ptr_ref(pt->pt_func);
4531 vim_free(name);
4532 }
4533 else
4534 {
4535 pt->pt_name = name;
4536 func_ref(name);
4537 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538 }
4539 rettv->v_type = VAR_PARTIAL;
4540 rettv->vval.v_partial = pt;
4541 }
4542 else
4543 {
4544 /* result is a VAR_FUNC */
4545 rettv->v_type = VAR_FUNC;
4546 rettv->vval.v_string = name;
4547 func_ref(name);
4548 }
4549 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004550theend:
4551 vim_free(trans_name);
4552}
4553
4554/*
4555 * "funcref()" function
4556 */
4557 static void
4558f_funcref(typval_T *argvars, typval_T *rettv)
4559{
4560 common_function(argvars, rettv, TRUE);
4561}
4562
4563/*
4564 * "function()" function
4565 */
4566 static void
4567f_function(typval_T *argvars, typval_T *rettv)
4568{
4569 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004570}
4571
4572/*
4573 * "garbagecollect()" function
4574 */
4575 static void
4576f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4577{
4578 /* This is postponed until we are back at the toplevel, because we may be
4579 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4580 want_garbage_collect = TRUE;
4581
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004582 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004583 garbage_collect_at_exit = TRUE;
4584}
4585
4586/*
4587 * "get()" function
4588 */
4589 static void
4590f_get(typval_T *argvars, typval_T *rettv)
4591{
4592 listitem_T *li;
4593 list_T *l;
4594 dictitem_T *di;
4595 dict_T *d;
4596 typval_T *tv = NULL;
4597
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004598 if (argvars[0].v_type == VAR_BLOB)
4599 {
4600 int error = FALSE;
4601 int idx = tv_get_number_chk(&argvars[1], &error);
4602
4603 if (!error)
4604 {
4605 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004606 if (idx < 0)
4607 idx = blob_len(argvars[0].vval.v_blob) + idx;
4608 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4609 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004610 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004611 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004612 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004613 tv = rettv;
4614 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004615 }
4616 }
4617 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004618 {
4619 if ((l = argvars[0].vval.v_list) != NULL)
4620 {
4621 int error = FALSE;
4622
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004623 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004624 if (!error && li != NULL)
4625 tv = &li->li_tv;
4626 }
4627 }
4628 else if (argvars[0].v_type == VAR_DICT)
4629 {
4630 if ((d = argvars[0].vval.v_dict) != NULL)
4631 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004632 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004633 if (di != NULL)
4634 tv = &di->di_tv;
4635 }
4636 }
4637 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4638 {
4639 partial_T *pt;
4640 partial_T fref_pt;
4641
4642 if (argvars[0].v_type == VAR_PARTIAL)
4643 pt = argvars[0].vval.v_partial;
4644 else
4645 {
4646 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4647 fref_pt.pt_name = argvars[0].vval.v_string;
4648 pt = &fref_pt;
4649 }
4650
4651 if (pt != NULL)
4652 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004653 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004654 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004655
4656 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4657 {
4658 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004659 n = partial_name(pt);
4660 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004661 rettv->vval.v_string = NULL;
4662 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004663 {
4664 rettv->vval.v_string = vim_strsave(n);
4665 if (rettv->v_type == VAR_FUNC)
4666 func_ref(rettv->vval.v_string);
4667 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004668 }
4669 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004670 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004671 else if (STRCMP(what, "args") == 0)
4672 {
4673 rettv->v_type = VAR_LIST;
4674 if (rettv_list_alloc(rettv) == OK)
4675 {
4676 int i;
4677
4678 for (i = 0; i < pt->pt_argc; ++i)
4679 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4680 }
4681 }
4682 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004683 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004684 return;
4685 }
4686 }
4687 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004688 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004689
4690 if (tv == NULL)
4691 {
4692 if (argvars[2].v_type != VAR_UNKNOWN)
4693 copy_tv(&argvars[2], rettv);
4694 }
4695 else
4696 copy_tv(tv, rettv);
4697}
4698
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004699/*
4700 * Returns buffer options, variables and other attributes in a dictionary.
4701 */
4702 static dict_T *
4703get_buffer_info(buf_T *buf)
4704{
4705 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004706 tabpage_T *tp;
4707 win_T *wp;
4708 list_T *windows;
4709
4710 dict = dict_alloc();
4711 if (dict == NULL)
4712 return NULL;
4713
Bram Moolenaare0be1672018-07-08 16:50:37 +02004714 dict_add_number(dict, "bufnr", buf->b_fnum);
4715 dict_add_string(dict, "name", buf->b_ffname);
4716 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4717 : buflist_findlnum(buf));
4718 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4719 dict_add_number(dict, "listed", buf->b_p_bl);
4720 dict_add_number(dict, "changed", bufIsChanged(buf));
4721 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4722 dict_add_number(dict, "hidden",
4723 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004724
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004725 /* Get a reference to buffer variables */
4726 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004727
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004728 /* List of windows displaying this buffer */
4729 windows = list_alloc();
4730 if (windows != NULL)
4731 {
4732 FOR_ALL_TAB_WINDOWS(tp, wp)
4733 if (wp->w_buffer == buf)
4734 list_append_number(windows, (varnumber_T)wp->w_id);
4735 dict_add_list(dict, "windows", windows);
4736 }
4737
4738#ifdef FEAT_SIGNS
4739 if (buf->b_signlist != NULL)
4740 {
4741 /* List of signs placed in this buffer */
4742 list_T *signs = list_alloc();
4743 if (signs != NULL)
4744 {
4745 get_buffer_signs(buf, signs);
4746 dict_add_list(dict, "signs", signs);
4747 }
4748 }
4749#endif
4750
4751 return dict;
4752}
4753
4754/*
4755 * "getbufinfo()" function
4756 */
4757 static void
4758f_getbufinfo(typval_T *argvars, typval_T *rettv)
4759{
4760 buf_T *buf = NULL;
4761 buf_T *argbuf = NULL;
4762 dict_T *d;
4763 int filtered = FALSE;
4764 int sel_buflisted = FALSE;
4765 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004766 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004767
4768 if (rettv_list_alloc(rettv) != OK)
4769 return;
4770
4771 /* List of all the buffers or selected buffers */
4772 if (argvars[0].v_type == VAR_DICT)
4773 {
4774 dict_T *sel_d = argvars[0].vval.v_dict;
4775
4776 if (sel_d != NULL)
4777 {
4778 dictitem_T *di;
4779
4780 filtered = TRUE;
4781
4782 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004783 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004784 sel_buflisted = TRUE;
4785
4786 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004787 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004788 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004789
4790 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004791 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004792 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004793 }
4794 }
4795 else if (argvars[0].v_type != VAR_UNKNOWN)
4796 {
4797 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004798 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004799 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004800 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004801 --emsg_off;
4802 if (argbuf == NULL)
4803 return;
4804 }
4805
4806 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004807 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004808 {
4809 if (argbuf != NULL && argbuf != buf)
4810 continue;
4811 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004812 || (sel_buflisted && !buf->b_p_bl)
4813 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004814 continue;
4815
4816 d = get_buffer_info(buf);
4817 if (d != NULL)
4818 list_append_dict(rettv->vval.v_list, d);
4819 if (argbuf != NULL)
4820 return;
4821 }
4822}
4823
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004824/*
4825 * Get line or list of lines from buffer "buf" into "rettv".
4826 * Return a range (from start to end) of lines in rettv from the specified
4827 * buffer.
4828 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4829 */
4830 static void
4831get_buffer_lines(
4832 buf_T *buf,
4833 linenr_T start,
4834 linenr_T end,
4835 int retlist,
4836 typval_T *rettv)
4837{
4838 char_u *p;
4839
4840 rettv->v_type = VAR_STRING;
4841 rettv->vval.v_string = NULL;
4842 if (retlist && rettv_list_alloc(rettv) == FAIL)
4843 return;
4844
4845 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4846 return;
4847
4848 if (!retlist)
4849 {
4850 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4851 p = ml_get_buf(buf, start, FALSE);
4852 else
4853 p = (char_u *)"";
4854 rettv->vval.v_string = vim_strsave(p);
4855 }
4856 else
4857 {
4858 if (end < start)
4859 return;
4860
4861 if (start < 1)
4862 start = 1;
4863 if (end > buf->b_ml.ml_line_count)
4864 end = buf->b_ml.ml_line_count;
4865 while (start <= end)
4866 if (list_append_string(rettv->vval.v_list,
4867 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4868 break;
4869 }
4870}
4871
4872/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004873 * "getbufline()" function
4874 */
4875 static void
4876f_getbufline(typval_T *argvars, typval_T *rettv)
4877{
4878 linenr_T lnum;
4879 linenr_T end;
4880 buf_T *buf;
4881
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004882 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004883 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004884 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004885 --emsg_off;
4886
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004887 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004888 if (argvars[2].v_type == VAR_UNKNOWN)
4889 end = lnum;
4890 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004891 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004892
4893 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4894}
4895
4896/*
4897 * "getbufvar()" function
4898 */
4899 static void
4900f_getbufvar(typval_T *argvars, typval_T *rettv)
4901{
4902 buf_T *buf;
4903 buf_T *save_curbuf;
4904 char_u *varname;
4905 dictitem_T *v;
4906 int done = FALSE;
4907
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004908 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4909 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004910 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004911 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004912
4913 rettv->v_type = VAR_STRING;
4914 rettv->vval.v_string = NULL;
4915
4916 if (buf != NULL && varname != NULL)
4917 {
4918 /* set curbuf to be our buf, temporarily */
4919 save_curbuf = curbuf;
4920 curbuf = buf;
4921
Bram Moolenaar30567352016-08-27 21:25:44 +02004922 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004923 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004924 if (varname[1] == NUL)
4925 {
4926 /* get all buffer-local options in a dict */
4927 dict_T *opts = get_winbuf_options(TRUE);
4928
4929 if (opts != NULL)
4930 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004931 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004932 done = TRUE;
4933 }
4934 }
4935 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4936 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004937 done = TRUE;
4938 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004939 else
4940 {
4941 /* Look up the variable. */
4942 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4943 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4944 'b', varname, FALSE);
4945 if (v != NULL)
4946 {
4947 copy_tv(&v->di_tv, rettv);
4948 done = TRUE;
4949 }
4950 }
4951
4952 /* restore previous notion of curbuf */
4953 curbuf = save_curbuf;
4954 }
4955
4956 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4957 /* use the default value */
4958 copy_tv(&argvars[2], rettv);
4959
4960 --emsg_off;
4961}
4962
4963/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004964 * "getchangelist()" function
4965 */
4966 static void
4967f_getchangelist(typval_T *argvars, typval_T *rettv)
4968{
4969#ifdef FEAT_JUMPLIST
4970 buf_T *buf;
4971 int i;
4972 list_T *l;
4973 dict_T *d;
4974#endif
4975
4976 if (rettv_list_alloc(rettv) != OK)
4977 return;
4978
4979#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004980 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004981 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004982 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004983 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004984 if (buf == NULL)
4985 return;
4986
4987 l = list_alloc();
4988 if (l == NULL)
4989 return;
4990
4991 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4992 return;
4993 /*
4994 * The current window change list index tracks only the position in the
4995 * current buffer change list. For other buffers, use the change list
4996 * length as the current index.
4997 */
4998 list_append_number(rettv->vval.v_list,
4999 (varnumber_T)((buf == curwin->w_buffer)
5000 ? curwin->w_changelistidx : buf->b_changelistlen));
5001
5002 for (i = 0; i < buf->b_changelistlen; ++i)
5003 {
5004 if (buf->b_changelist[i].lnum == 0)
5005 continue;
5006 if ((d = dict_alloc()) == NULL)
5007 return;
5008 if (list_append_dict(l, d) == FAIL)
5009 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005010 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5011 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005012 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005013 }
5014#endif
5015}
5016/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005017 * "getchar()" function
5018 */
5019 static void
5020f_getchar(typval_T *argvars, typval_T *rettv)
5021{
5022 varnumber_T n;
5023 int error = FALSE;
5024
Bram Moolenaar84d93902018-09-11 20:10:20 +02005025#ifdef MESSAGE_QUEUE
5026 // vpeekc() used to check for messages, but that caused problems, invoking
5027 // a callback where it was not expected. Some plugins use getchar(1) in a
5028 // loop to await a message, therefore make sure we check for messages here.
5029 parse_queued_messages();
5030#endif
5031
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005032 /* Position the cursor. Needed after a message that ends in a space. */
5033 windgoto(msg_row, msg_col);
5034
5035 ++no_mapping;
5036 ++allow_keys;
5037 for (;;)
5038 {
5039 if (argvars[0].v_type == VAR_UNKNOWN)
5040 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005041 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005042 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005043 /* getchar(1): only check if char avail */
5044 n = vpeekc_any();
5045 else if (error || vpeekc_any() == NUL)
5046 /* illegal argument or getchar(0) and no char avail: return zero */
5047 n = 0;
5048 else
5049 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005050 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005051
5052 if (n == K_IGNORE)
5053 continue;
5054 break;
5055 }
5056 --no_mapping;
5057 --allow_keys;
5058
5059 set_vim_var_nr(VV_MOUSE_WIN, 0);
5060 set_vim_var_nr(VV_MOUSE_WINID, 0);
5061 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5062 set_vim_var_nr(VV_MOUSE_COL, 0);
5063
5064 rettv->vval.v_number = n;
5065 if (IS_SPECIAL(n) || mod_mask != 0)
5066 {
5067 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5068 int i = 0;
5069
5070 /* Turn a special key into three bytes, plus modifier. */
5071 if (mod_mask != 0)
5072 {
5073 temp[i++] = K_SPECIAL;
5074 temp[i++] = KS_MODIFIER;
5075 temp[i++] = mod_mask;
5076 }
5077 if (IS_SPECIAL(n))
5078 {
5079 temp[i++] = K_SPECIAL;
5080 temp[i++] = K_SECOND(n);
5081 temp[i++] = K_THIRD(n);
5082 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005083 else if (has_mbyte)
5084 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005085 else
5086 temp[i++] = n;
5087 temp[i++] = NUL;
5088 rettv->v_type = VAR_STRING;
5089 rettv->vval.v_string = vim_strsave(temp);
5090
5091#ifdef FEAT_MOUSE
5092 if (is_mouse_key(n))
5093 {
5094 int row = mouse_row;
5095 int col = mouse_col;
5096 win_T *win;
5097 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005098 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099 int winnr = 1;
5100
5101 if (row >= 0 && col >= 0)
5102 {
5103 /* Find the window at the mouse coordinates and compute the
5104 * text position. */
5105 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005106 if (win == NULL)
5107 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005108 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005109 for (wp = firstwin; wp != win; wp = wp->w_next)
5110 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005111 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5112 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5113 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5114 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5115 }
5116 }
5117#endif
5118 }
5119}
5120
5121/*
5122 * "getcharmod()" function
5123 */
5124 static void
5125f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5126{
5127 rettv->vval.v_number = mod_mask;
5128}
5129
5130/*
5131 * "getcharsearch()" function
5132 */
5133 static void
5134f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5135{
5136 if (rettv_dict_alloc(rettv) != FAIL)
5137 {
5138 dict_T *dict = rettv->vval.v_dict;
5139
Bram Moolenaare0be1672018-07-08 16:50:37 +02005140 dict_add_string(dict, "char", last_csearch());
5141 dict_add_number(dict, "forward", last_csearch_forward());
5142 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005143 }
5144}
5145
5146/*
5147 * "getcmdline()" function
5148 */
5149 static void
5150f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5151{
5152 rettv->v_type = VAR_STRING;
5153 rettv->vval.v_string = get_cmdline_str();
5154}
5155
5156/*
5157 * "getcmdpos()" function
5158 */
5159 static void
5160f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5161{
5162 rettv->vval.v_number = get_cmdline_pos() + 1;
5163}
5164
5165/*
5166 * "getcmdtype()" function
5167 */
5168 static void
5169f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5170{
5171 rettv->v_type = VAR_STRING;
5172 rettv->vval.v_string = alloc(2);
5173 if (rettv->vval.v_string != NULL)
5174 {
5175 rettv->vval.v_string[0] = get_cmdline_type();
5176 rettv->vval.v_string[1] = NUL;
5177 }
5178}
5179
5180/*
5181 * "getcmdwintype()" function
5182 */
5183 static void
5184f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5185{
5186 rettv->v_type = VAR_STRING;
5187 rettv->vval.v_string = NULL;
5188#ifdef FEAT_CMDWIN
5189 rettv->vval.v_string = alloc(2);
5190 if (rettv->vval.v_string != NULL)
5191 {
5192 rettv->vval.v_string[0] = cmdwin_type;
5193 rettv->vval.v_string[1] = NUL;
5194 }
5195#endif
5196}
5197
5198#if defined(FEAT_CMDL_COMPL)
5199/*
5200 * "getcompletion()" function
5201 */
5202 static void
5203f_getcompletion(typval_T *argvars, typval_T *rettv)
5204{
5205 char_u *pat;
5206 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005207 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005208 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5209 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005210
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005211 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005212 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005213
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005214 if (p_wic)
5215 options |= WILD_ICASE;
5216
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005217 /* For filtered results, 'wildignore' is used */
5218 if (!filtered)
5219 options |= WILD_KEEP_ALL;
5220
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005221 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005222 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005223 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005224 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005225 if (xpc.xp_context == EXPAND_NOTHING)
5226 {
5227 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005228 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005229 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005230 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005231 return;
5232 }
5233
5234# if defined(FEAT_MENU)
5235 if (xpc.xp_context == EXPAND_MENUS)
5236 {
5237 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5238 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5239 }
5240# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005241#ifdef FEAT_CSCOPE
5242 if (xpc.xp_context == EXPAND_CSCOPE)
5243 {
5244 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5245 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5246 }
5247#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005248#ifdef FEAT_SIGNS
5249 if (xpc.xp_context == EXPAND_SIGN)
5250 {
5251 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5252 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5253 }
5254#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005255
5256 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5257 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5258 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005259 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005260
5261 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5262
5263 for (i = 0; i < xpc.xp_numfiles; i++)
5264 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5265 }
5266 vim_free(pat);
5267 ExpandCleanup(&xpc);
5268}
5269#endif
5270
5271/*
5272 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005273 *
5274 * Return the current working directory of a window in a tab page.
5275 * First optional argument 'winnr' is the window number or -1 and the second
5276 * optional argument 'tabnr' is the tab page number.
5277 *
5278 * If no arguments are supplied, then return the directory of the current
5279 * window.
5280 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5281 * the specified window.
5282 * If 'winnr' is 0 then return the directory of the current window.
5283 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5284 * directory of the specified tab page. Otherwise return the directory of the
5285 * specified window in the specified tab page.
5286 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005287 */
5288 static void
5289f_getcwd(typval_T *argvars, typval_T *rettv)
5290{
5291 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005292 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005293 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005294 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005295
5296 rettv->v_type = VAR_STRING;
5297 rettv->vval.v_string = NULL;
5298
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005299 if (argvars[0].v_type == VAR_NUMBER
5300 && argvars[0].vval.v_number == -1
5301 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005302 global = TRUE;
5303 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005304 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005305
5306 if (wp != NULL && wp->w_localdir != NULL)
5307 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005308 else if (tp != NULL && tp->tp_localdir != NULL)
5309 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5310 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005311 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005312 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005313 rettv->vval.v_string = vim_strsave(globaldir);
5314 else
5315 {
5316 cwd = alloc(MAXPATHL);
5317 if (cwd != NULL)
5318 {
5319 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5320 rettv->vval.v_string = vim_strsave(cwd);
5321 vim_free(cwd);
5322 }
5323 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005324 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005325#ifdef BACKSLASH_IN_FILENAME
5326 if (rettv->vval.v_string != NULL)
5327 slash_adjust(rettv->vval.v_string);
5328#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005329}
5330
5331/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005332 * "getenv()" function
5333 */
5334 static void
5335f_getenv(typval_T *argvars, typval_T *rettv)
5336{
5337 int mustfree = FALSE;
5338 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5339
5340 if (p == NULL)
5341 {
5342 rettv->v_type = VAR_SPECIAL;
5343 rettv->vval.v_number = VVAL_NULL;
5344 return;
5345 }
5346 if (!mustfree)
5347 p = vim_strsave(p);
5348 rettv->vval.v_string = p;
5349 rettv->v_type = VAR_STRING;
5350}
5351
5352/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005353 * "getfontname()" function
5354 */
5355 static void
5356f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5357{
5358 rettv->v_type = VAR_STRING;
5359 rettv->vval.v_string = NULL;
5360#ifdef FEAT_GUI
5361 if (gui.in_use)
5362 {
5363 GuiFont font;
5364 char_u *name = NULL;
5365
5366 if (argvars[0].v_type == VAR_UNKNOWN)
5367 {
5368 /* Get the "Normal" font. Either the name saved by
5369 * hl_set_font_name() or from the font ID. */
5370 font = gui.norm_font;
5371 name = hl_get_font_name();
5372 }
5373 else
5374 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005375 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005376 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5377 return;
5378 font = gui_mch_get_font(name, FALSE);
5379 if (font == NOFONT)
5380 return; /* Invalid font name, return empty string. */
5381 }
5382 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5383 if (argvars[0].v_type != VAR_UNKNOWN)
5384 gui_mch_free_font(font);
5385 }
5386#endif
5387}
5388
5389/*
5390 * "getfperm({fname})" function
5391 */
5392 static void
5393f_getfperm(typval_T *argvars, typval_T *rettv)
5394{
5395 char_u *fname;
5396 stat_T st;
5397 char_u *perm = NULL;
5398 char_u flags[] = "rwx";
5399 int i;
5400
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005401 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005402
5403 rettv->v_type = VAR_STRING;
5404 if (mch_stat((char *)fname, &st) >= 0)
5405 {
5406 perm = vim_strsave((char_u *)"---------");
5407 if (perm != NULL)
5408 {
5409 for (i = 0; i < 9; i++)
5410 {
5411 if (st.st_mode & (1 << (8 - i)))
5412 perm[i] = flags[i % 3];
5413 }
5414 }
5415 }
5416 rettv->vval.v_string = perm;
5417}
5418
5419/*
5420 * "getfsize({fname})" function
5421 */
5422 static void
5423f_getfsize(typval_T *argvars, typval_T *rettv)
5424{
5425 char_u *fname;
5426 stat_T st;
5427
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005428 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005429
5430 rettv->v_type = VAR_NUMBER;
5431
5432 if (mch_stat((char *)fname, &st) >= 0)
5433 {
5434 if (mch_isdir(fname))
5435 rettv->vval.v_number = 0;
5436 else
5437 {
5438 rettv->vval.v_number = (varnumber_T)st.st_size;
5439
5440 /* non-perfect check for overflow */
5441 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5442 rettv->vval.v_number = -2;
5443 }
5444 }
5445 else
5446 rettv->vval.v_number = -1;
5447}
5448
5449/*
5450 * "getftime({fname})" function
5451 */
5452 static void
5453f_getftime(typval_T *argvars, typval_T *rettv)
5454{
5455 char_u *fname;
5456 stat_T st;
5457
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005458 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005459
5460 if (mch_stat((char *)fname, &st) >= 0)
5461 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5462 else
5463 rettv->vval.v_number = -1;
5464}
5465
5466/*
5467 * "getftype({fname})" function
5468 */
5469 static void
5470f_getftype(typval_T *argvars, typval_T *rettv)
5471{
5472 char_u *fname;
5473 stat_T st;
5474 char_u *type = NULL;
5475 char *t;
5476
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005477 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005478
5479 rettv->v_type = VAR_STRING;
5480 if (mch_lstat((char *)fname, &st) >= 0)
5481 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005482 if (S_ISREG(st.st_mode))
5483 t = "file";
5484 else if (S_ISDIR(st.st_mode))
5485 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005486 else if (S_ISLNK(st.st_mode))
5487 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005488 else if (S_ISBLK(st.st_mode))
5489 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005490 else if (S_ISCHR(st.st_mode))
5491 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005492 else if (S_ISFIFO(st.st_mode))
5493 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005494 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005495 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005496 else
5497 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005498 type = vim_strsave((char_u *)t);
5499 }
5500 rettv->vval.v_string = type;
5501}
5502
5503/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005504 * "getjumplist()" function
5505 */
5506 static void
5507f_getjumplist(typval_T *argvars, typval_T *rettv)
5508{
5509#ifdef FEAT_JUMPLIST
5510 win_T *wp;
5511 int i;
5512 list_T *l;
5513 dict_T *d;
5514#endif
5515
5516 if (rettv_list_alloc(rettv) != OK)
5517 return;
5518
5519#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005520 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005521 if (wp == NULL)
5522 return;
5523
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005524 cleanup_jumplist(wp, TRUE);
5525
Bram Moolenaar4f505882018-02-10 21:06:32 +01005526 l = list_alloc();
5527 if (l == NULL)
5528 return;
5529
5530 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5531 return;
5532 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5533
5534 for (i = 0; i < wp->w_jumplistlen; ++i)
5535 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005536 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5537 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005538 if ((d = dict_alloc()) == NULL)
5539 return;
5540 if (list_append_dict(l, d) == FAIL)
5541 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005542 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5543 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005544 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005545 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005546 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005547 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005548 }
5549#endif
5550}
5551
5552/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553 * "getline(lnum, [end])" function
5554 */
5555 static void
5556f_getline(typval_T *argvars, typval_T *rettv)
5557{
5558 linenr_T lnum;
5559 linenr_T end;
5560 int retlist;
5561
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005562 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005563 if (argvars[1].v_type == VAR_UNKNOWN)
5564 {
5565 end = 0;
5566 retlist = FALSE;
5567 }
5568 else
5569 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005570 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005571 retlist = TRUE;
5572 }
5573
5574 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5575}
5576
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005577#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005578 static void
5579get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5580{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005581 if (what_arg->v_type == VAR_UNKNOWN)
5582 {
5583 if (rettv_list_alloc(rettv) == OK)
5584 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005585 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005586 }
5587 else
5588 {
5589 if (rettv_dict_alloc(rettv) == OK)
5590 if (is_qf || (wp != NULL))
5591 {
5592 if (what_arg->v_type == VAR_DICT)
5593 {
5594 dict_T *d = what_arg->vval.v_dict;
5595
5596 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005597 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005598 }
5599 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005600 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005601 }
5602 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005603}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005604#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005605
5606/*
5607 * "getloclist()" function
5608 */
5609 static void
5610f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5611{
5612#ifdef FEAT_QUICKFIX
5613 win_T *wp;
5614
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005615 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005616 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5617#endif
5618}
5619
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005620/*
5621 * "getmatches()" function
5622 */
5623 static void
5624f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5625{
5626#ifdef FEAT_SEARCH_EXTRA
5627 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005628 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005629 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005630 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005631
Bram Moolenaaraff74912019-03-30 18:11:49 +01005632 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5633 return;
5634
5635 cur = win->w_match_head;
5636 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005637 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005638 dict = dict_alloc();
5639 if (dict == NULL)
5640 return;
5641 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005642 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005643 /* match added with matchaddpos() */
5644 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005645 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005646 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005647 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005648 list_T *l;
5649
5650 llpos = &cur->pos.pos[i];
5651 if (llpos->lnum == 0)
5652 break;
5653 l = list_alloc();
5654 if (l == NULL)
5655 break;
5656 list_append_number(l, (varnumber_T)llpos->lnum);
5657 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005658 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005659 list_append_number(l, (varnumber_T)llpos->col);
5660 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005661 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005662 sprintf(buf, "pos%d", i + 1);
5663 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005664 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005665 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005666 else
5667 {
5668 dict_add_string(dict, "pattern", cur->pattern);
5669 }
5670 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5671 dict_add_number(dict, "priority", (long)cur->priority);
5672 dict_add_number(dict, "id", (long)cur->id);
5673# if defined(FEAT_CONCEAL)
5674 if (cur->conceal_char)
5675 {
5676 char_u buf[MB_MAXBYTES + 1];
5677
5678 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5679 dict_add_string(dict, "conceal", (char_u *)&buf);
5680 }
5681# endif
5682 list_append_dict(rettv->vval.v_list, dict);
5683 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005684 }
5685#endif
5686}
5687
5688/*
5689 * "getpid()" function
5690 */
5691 static void
5692f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5693{
5694 rettv->vval.v_number = mch_get_pid();
5695}
5696
5697 static void
5698getpos_both(
5699 typval_T *argvars,
5700 typval_T *rettv,
5701 int getcurpos)
5702{
5703 pos_T *fp;
5704 list_T *l;
5705 int fnum = -1;
5706
5707 if (rettv_list_alloc(rettv) == OK)
5708 {
5709 l = rettv->vval.v_list;
5710 if (getcurpos)
5711 fp = &curwin->w_cursor;
5712 else
5713 fp = var2fpos(&argvars[0], TRUE, &fnum);
5714 if (fnum != -1)
5715 list_append_number(l, (varnumber_T)fnum);
5716 else
5717 list_append_number(l, (varnumber_T)0);
5718 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5719 : (varnumber_T)0);
5720 list_append_number(l, (fp != NULL)
5721 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5722 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005723 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005724 (varnumber_T)0);
5725 if (getcurpos)
5726 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005727 int save_set_curswant = curwin->w_set_curswant;
5728 colnr_T save_curswant = curwin->w_curswant;
5729 colnr_T save_virtcol = curwin->w_virtcol;
5730
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005731 update_curswant();
5732 list_append_number(l, curwin->w_curswant == MAXCOL ?
5733 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005734
5735 // Do not change "curswant", as it is unexpected that a get
5736 // function has a side effect.
5737 if (save_set_curswant)
5738 {
5739 curwin->w_set_curswant = save_set_curswant;
5740 curwin->w_curswant = save_curswant;
5741 curwin->w_virtcol = save_virtcol;
5742 curwin->w_valid &= ~VALID_VIRTCOL;
5743 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005744 }
5745 }
5746 else
5747 rettv->vval.v_number = FALSE;
5748}
5749
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005750/*
5751 * "getcurpos()" function
5752 */
5753 static void
5754f_getcurpos(typval_T *argvars, typval_T *rettv)
5755{
5756 getpos_both(argvars, rettv, TRUE);
5757}
5758
5759/*
5760 * "getpos(string)" function
5761 */
5762 static void
5763f_getpos(typval_T *argvars, typval_T *rettv)
5764{
5765 getpos_both(argvars, rettv, FALSE);
5766}
5767
5768/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005769 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005770 */
5771 static void
5772f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5773{
5774#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005775 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005776#endif
5777}
5778
5779/*
5780 * "getreg()" function
5781 */
5782 static void
5783f_getreg(typval_T *argvars, typval_T *rettv)
5784{
5785 char_u *strregname;
5786 int regname;
5787 int arg2 = FALSE;
5788 int return_list = FALSE;
5789 int error = FALSE;
5790
5791 if (argvars[0].v_type != VAR_UNKNOWN)
5792 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005793 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005794 error = strregname == NULL;
5795 if (argvars[1].v_type != VAR_UNKNOWN)
5796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005797 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005798 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005799 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005800 }
5801 }
5802 else
5803 strregname = get_vim_var_str(VV_REG);
5804
5805 if (error)
5806 return;
5807
5808 regname = (strregname == NULL ? '"' : *strregname);
5809 if (regname == 0)
5810 regname = '"';
5811
5812 if (return_list)
5813 {
5814 rettv->v_type = VAR_LIST;
5815 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5816 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5817 if (rettv->vval.v_list == NULL)
5818 (void)rettv_list_alloc(rettv);
5819 else
5820 ++rettv->vval.v_list->lv_refcount;
5821 }
5822 else
5823 {
5824 rettv->v_type = VAR_STRING;
5825 rettv->vval.v_string = get_reg_contents(regname,
5826 arg2 ? GREG_EXPR_SRC : 0);
5827 }
5828}
5829
5830/*
5831 * "getregtype()" function
5832 */
5833 static void
5834f_getregtype(typval_T *argvars, typval_T *rettv)
5835{
5836 char_u *strregname;
5837 int regname;
5838 char_u buf[NUMBUFLEN + 2];
5839 long reglen = 0;
5840
5841 if (argvars[0].v_type != VAR_UNKNOWN)
5842 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005843 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005844 if (strregname == NULL) /* type error; errmsg already given */
5845 {
5846 rettv->v_type = VAR_STRING;
5847 rettv->vval.v_string = NULL;
5848 return;
5849 }
5850 }
5851 else
5852 /* Default to v:register */
5853 strregname = get_vim_var_str(VV_REG);
5854
5855 regname = (strregname == NULL ? '"' : *strregname);
5856 if (regname == 0)
5857 regname = '"';
5858
5859 buf[0] = NUL;
5860 buf[1] = NUL;
5861 switch (get_reg_type(regname, &reglen))
5862 {
5863 case MLINE: buf[0] = 'V'; break;
5864 case MCHAR: buf[0] = 'v'; break;
5865 case MBLOCK:
5866 buf[0] = Ctrl_V;
5867 sprintf((char *)buf + 1, "%ld", reglen + 1);
5868 break;
5869 }
5870 rettv->v_type = VAR_STRING;
5871 rettv->vval.v_string = vim_strsave(buf);
5872}
5873
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005874/*
5875 * Returns information (variables, options, etc.) about a tab page
5876 * as a dictionary.
5877 */
5878 static dict_T *
5879get_tabpage_info(tabpage_T *tp, int tp_idx)
5880{
5881 win_T *wp;
5882 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005883 list_T *l;
5884
5885 dict = dict_alloc();
5886 if (dict == NULL)
5887 return NULL;
5888
Bram Moolenaare0be1672018-07-08 16:50:37 +02005889 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005890
5891 l = list_alloc();
5892 if (l != NULL)
5893 {
5894 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5895 wp; wp = wp->w_next)
5896 list_append_number(l, (varnumber_T)wp->w_id);
5897 dict_add_list(dict, "windows", l);
5898 }
5899
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005900 /* Make a reference to tabpage variables */
5901 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005902
5903 return dict;
5904}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005905
5906/*
5907 * "gettabinfo()" function
5908 */
5909 static void
5910f_gettabinfo(typval_T *argvars, typval_T *rettv)
5911{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005912 tabpage_T *tp, *tparg = NULL;
5913 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005914 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005915
5916 if (rettv_list_alloc(rettv) != OK)
5917 return;
5918
5919 if (argvars[0].v_type != VAR_UNKNOWN)
5920 {
5921 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005922 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005923 if (tparg == NULL)
5924 return;
5925 }
5926
5927 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005928 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005929 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005930 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005931 if (tparg != NULL && tp != tparg)
5932 continue;
5933 d = get_tabpage_info(tp, tpnr);
5934 if (d != NULL)
5935 list_append_dict(rettv->vval.v_list, d);
5936 if (tparg != NULL)
5937 return;
5938 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005939}
5940
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005941/*
5942 * "gettabvar()" function
5943 */
5944 static void
5945f_gettabvar(typval_T *argvars, typval_T *rettv)
5946{
5947 win_T *oldcurwin;
5948 tabpage_T *tp, *oldtabpage;
5949 dictitem_T *v;
5950 char_u *varname;
5951 int done = FALSE;
5952
5953 rettv->v_type = VAR_STRING;
5954 rettv->vval.v_string = NULL;
5955
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005956 varname = tv_get_string_chk(&argvars[1]);
5957 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005958 if (tp != NULL && varname != NULL)
5959 {
5960 /* Set tp to be our tabpage, temporarily. Also set the window to the
5961 * first window in the tabpage, otherwise the window is not valid. */
5962 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005963 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5964 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005965 {
5966 /* look up the variable */
5967 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5968 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5969 if (v != NULL)
5970 {
5971 copy_tv(&v->di_tv, rettv);
5972 done = TRUE;
5973 }
5974 }
5975
5976 /* restore previous notion of curwin */
5977 restore_win(oldcurwin, oldtabpage, TRUE);
5978 }
5979
5980 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5981 copy_tv(&argvars[2], rettv);
5982}
5983
5984/*
5985 * "gettabwinvar()" function
5986 */
5987 static void
5988f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5989{
5990 getwinvar(argvars, rettv, 1);
5991}
5992
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005993/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005994 * "gettagstack()" function
5995 */
5996 static void
5997f_gettagstack(typval_T *argvars, typval_T *rettv)
5998{
5999 win_T *wp = curwin; // default is current window
6000
6001 if (rettv_dict_alloc(rettv) != OK)
6002 return;
6003
6004 if (argvars[0].v_type != VAR_UNKNOWN)
6005 {
6006 wp = find_win_by_nr_or_id(&argvars[0]);
6007 if (wp == NULL)
6008 return;
6009 }
6010
6011 get_tagstack(wp, rettv->vval.v_dict);
6012}
6013
6014/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006015 * Returns information about a window as a dictionary.
6016 */
6017 static dict_T *
6018get_win_info(win_T *wp, short tpnr, short winnr)
6019{
6020 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006021
6022 dict = dict_alloc();
6023 if (dict == NULL)
6024 return NULL;
6025
Bram Moolenaare0be1672018-07-08 16:50:37 +02006026 dict_add_number(dict, "tabnr", tpnr);
6027 dict_add_number(dict, "winnr", winnr);
6028 dict_add_number(dict, "winid", wp->w_id);
6029 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006030 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006031 dict_add_number(dict, "topline", wp->w_topline);
6032 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006033#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006034 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006035#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006036 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006037 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006038 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006039
Bram Moolenaar69905d12017-08-13 18:14:47 +02006040#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006041 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006042#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006043#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006044 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6045 dict_add_number(dict, "loclist",
6046 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006047#endif
6048
Bram Moolenaar30567352016-08-27 21:25:44 +02006049 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006050 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006051
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006052 return dict;
6053}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006054
6055/*
6056 * "getwininfo()" function
6057 */
6058 static void
6059f_getwininfo(typval_T *argvars, typval_T *rettv)
6060{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006061 tabpage_T *tp;
6062 win_T *wp = NULL, *wparg = NULL;
6063 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006064 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006065
6066 if (rettv_list_alloc(rettv) != OK)
6067 return;
6068
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006069 if (argvars[0].v_type != VAR_UNKNOWN)
6070 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006071 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006072 if (wparg == NULL)
6073 return;
6074 }
6075
6076 /* Collect information about either all the windows across all the tab
6077 * pages or one particular window.
6078 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006079 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006080 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006081 tabnr++;
6082 winnr = 0;
6083 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006084 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006085 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006086 if (wparg != NULL && wp != wparg)
6087 continue;
6088 d = get_win_info(wp, tabnr, winnr);
6089 if (d != NULL)
6090 list_append_dict(rettv->vval.v_list, d);
6091 if (wparg != NULL)
6092 /* found information about a specific window */
6093 return;
6094 }
6095 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006096}
6097
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006098/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006099 * "win_findbuf()" function
6100 */
6101 static void
6102f_win_findbuf(typval_T *argvars, typval_T *rettv)
6103{
6104 if (rettv_list_alloc(rettv) != FAIL)
6105 win_findbuf(argvars, rettv->vval.v_list);
6106}
6107
6108/*
6109 * "win_getid()" function
6110 */
6111 static void
6112f_win_getid(typval_T *argvars, typval_T *rettv)
6113{
6114 rettv->vval.v_number = win_getid(argvars);
6115}
6116
6117/*
6118 * "win_gotoid()" function
6119 */
6120 static void
6121f_win_gotoid(typval_T *argvars, typval_T *rettv)
6122{
6123 rettv->vval.v_number = win_gotoid(argvars);
6124}
6125
6126/*
6127 * "win_id2tabwin()" function
6128 */
6129 static void
6130f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6131{
6132 if (rettv_list_alloc(rettv) != FAIL)
6133 win_id2tabwin(argvars, rettv->vval.v_list);
6134}
6135
6136/*
6137 * "win_id2win()" function
6138 */
6139 static void
6140f_win_id2win(typval_T *argvars, typval_T *rettv)
6141{
6142 rettv->vval.v_number = win_id2win(argvars);
6143}
6144
6145/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006146 * "win_screenpos()" function
6147 */
6148 static void
6149f_win_screenpos(typval_T *argvars, typval_T *rettv)
6150{
6151 win_T *wp;
6152
6153 if (rettv_list_alloc(rettv) == FAIL)
6154 return;
6155
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006156 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006157 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6158 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6159}
6160
6161/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006162 * "getwinpos({timeout})" function
6163 */
6164 static void
6165f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6166{
6167 int x = -1;
6168 int y = -1;
6169
6170 if (rettv_list_alloc(rettv) == FAIL)
6171 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006172#if defined(FEAT_GUI) \
6173 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6174 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006175 {
6176 varnumber_T timeout = 100;
6177
6178 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006179 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006180
6181 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006182 }
6183#endif
6184 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6185 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6186}
6187
6188
6189/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006190 * "getwinposx()" function
6191 */
6192 static void
6193f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6194{
6195 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006196#if defined(FEAT_GUI) \
6197 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6198 || defined(MSWIN)
6199
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006200 {
6201 int x, y;
6202
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006203 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006204 rettv->vval.v_number = x;
6205 }
6206#endif
6207}
6208
6209/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006210 * "getwinposy()" function
6211 */
6212 static void
6213f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6214{
6215 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006216#if defined(FEAT_GUI) \
6217 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6218 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006219 {
6220 int x, y;
6221
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006222 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006223 rettv->vval.v_number = y;
6224 }
6225#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006226}
6227
6228/*
6229 * "getwinvar()" function
6230 */
6231 static void
6232f_getwinvar(typval_T *argvars, typval_T *rettv)
6233{
6234 getwinvar(argvars, rettv, 0);
6235}
6236
6237/*
6238 * "glob()" function
6239 */
6240 static void
6241f_glob(typval_T *argvars, typval_T *rettv)
6242{
6243 int options = WILD_SILENT|WILD_USE_NL;
6244 expand_T xpc;
6245 int error = FALSE;
6246
6247 /* When the optional second argument is non-zero, don't remove matches
6248 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6249 rettv->v_type = VAR_STRING;
6250 if (argvars[1].v_type != VAR_UNKNOWN)
6251 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006252 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006253 options |= WILD_KEEP_ALL;
6254 if (argvars[2].v_type != VAR_UNKNOWN)
6255 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006256 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006257 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006258 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006259 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006260 options |= WILD_ALLLINKS;
6261 }
6262 }
6263 if (!error)
6264 {
6265 ExpandInit(&xpc);
6266 xpc.xp_context = EXPAND_FILES;
6267 if (p_wic)
6268 options += WILD_ICASE;
6269 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006270 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006271 NULL, options, WILD_ALL);
6272 else if (rettv_list_alloc(rettv) != FAIL)
6273 {
6274 int i;
6275
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006276 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006277 NULL, options, WILD_ALL_KEEP);
6278 for (i = 0; i < xpc.xp_numfiles; i++)
6279 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6280
6281 ExpandCleanup(&xpc);
6282 }
6283 }
6284 else
6285 rettv->vval.v_string = NULL;
6286}
6287
6288/*
6289 * "globpath()" function
6290 */
6291 static void
6292f_globpath(typval_T *argvars, typval_T *rettv)
6293{
6294 int flags = 0;
6295 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006296 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297 int error = FALSE;
6298 garray_T ga;
6299 int i;
6300
6301 /* When the optional second argument is non-zero, don't remove matches
6302 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6303 rettv->v_type = VAR_STRING;
6304 if (argvars[2].v_type != VAR_UNKNOWN)
6305 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006306 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006307 flags |= WILD_KEEP_ALL;
6308 if (argvars[3].v_type != VAR_UNKNOWN)
6309 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006310 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006311 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006312 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006313 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314 flags |= WILD_ALLLINKS;
6315 }
6316 }
6317 if (file != NULL && !error)
6318 {
6319 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006320 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006321 if (rettv->v_type == VAR_STRING)
6322 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6323 else if (rettv_list_alloc(rettv) != FAIL)
6324 for (i = 0; i < ga.ga_len; ++i)
6325 list_append_string(rettv->vval.v_list,
6326 ((char_u **)(ga.ga_data))[i], -1);
6327 ga_clear_strings(&ga);
6328 }
6329 else
6330 rettv->vval.v_string = NULL;
6331}
6332
6333/*
6334 * "glob2regpat()" function
6335 */
6336 static void
6337f_glob2regpat(typval_T *argvars, typval_T *rettv)
6338{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006339 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006340
6341 rettv->v_type = VAR_STRING;
6342 rettv->vval.v_string = (pat == NULL)
6343 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6344}
6345
6346/* for VIM_VERSION_ defines */
6347#include "version.h"
6348
6349/*
6350 * "has()" function
6351 */
6352 static void
6353f_has(typval_T *argvars, typval_T *rettv)
6354{
6355 int i;
6356 char_u *name;
6357 int n = FALSE;
6358 static char *(has_list[]) =
6359 {
6360#ifdef AMIGA
6361 "amiga",
6362# ifdef FEAT_ARP
6363 "arp",
6364# endif
6365#endif
6366#ifdef __BEOS__
6367 "beos",
6368#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006369#if defined(BSD) && !defined(MACOS_X)
6370 "bsd",
6371#endif
6372#ifdef hpux
6373 "hpux",
6374#endif
6375#ifdef __linux__
6376 "linux",
6377#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006378#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006379 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6380 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006381# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006382 "macunix", /* Mac OS X, with the darwin feature */
6383 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006384# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006385#endif
6386#ifdef __QNX__
6387 "qnx",
6388#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006389#ifdef SUN_SYSTEM
6390 "sun",
6391#else
6392 "moon",
6393#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006394#ifdef UNIX
6395 "unix",
6396#endif
6397#ifdef VMS
6398 "vms",
6399#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006400#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006401 "win32",
6402#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006403#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006404 "win32unix",
6405#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006406#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006407 "win64",
6408#endif
6409#ifdef EBCDIC
6410 "ebcdic",
6411#endif
6412#ifndef CASE_INSENSITIVE_FILENAME
6413 "fname_case",
6414#endif
6415#ifdef HAVE_ACL
6416 "acl",
6417#endif
6418#ifdef FEAT_ARABIC
6419 "arabic",
6420#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006421 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006422#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006423 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006424#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006425#ifdef FEAT_AUTOSERVERNAME
6426 "autoservername",
6427#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006428#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006429 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006430# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006431 "balloon_multiline",
6432# endif
6433#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006434#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006435 "balloon_eval_term",
6436#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006437#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6438 "builtin_terms",
6439# ifdef ALL_BUILTIN_TCAPS
6440 "all_builtin_terms",
6441# endif
6442#endif
6443#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006444 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006445 || defined(FEAT_GUI_MOTIF))
6446 "browsefilter",
6447#endif
6448#ifdef FEAT_BYTEOFF
6449 "byte_offset",
6450#endif
6451#ifdef FEAT_JOB_CHANNEL
6452 "channel",
6453#endif
6454#ifdef FEAT_CINDENT
6455 "cindent",
6456#endif
6457#ifdef FEAT_CLIENTSERVER
6458 "clientserver",
6459#endif
6460#ifdef FEAT_CLIPBOARD
6461 "clipboard",
6462#endif
6463#ifdef FEAT_CMDL_COMPL
6464 "cmdline_compl",
6465#endif
6466#ifdef FEAT_CMDHIST
6467 "cmdline_hist",
6468#endif
6469#ifdef FEAT_COMMENTS
6470 "comments",
6471#endif
6472#ifdef FEAT_CONCEAL
6473 "conceal",
6474#endif
6475#ifdef FEAT_CRYPT
6476 "cryptv",
6477 "crypt-blowfish",
6478 "crypt-blowfish2",
6479#endif
6480#ifdef FEAT_CSCOPE
6481 "cscope",
6482#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006483 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006484#ifdef CURSOR_SHAPE
6485 "cursorshape",
6486#endif
6487#ifdef DEBUG
6488 "debug",
6489#endif
6490#ifdef FEAT_CON_DIALOG
6491 "dialog_con",
6492#endif
6493#ifdef FEAT_GUI_DIALOG
6494 "dialog_gui",
6495#endif
6496#ifdef FEAT_DIFF
6497 "diff",
6498#endif
6499#ifdef FEAT_DIGRAPHS
6500 "digraphs",
6501#endif
6502#ifdef FEAT_DIRECTX
6503 "directx",
6504#endif
6505#ifdef FEAT_DND
6506 "dnd",
6507#endif
6508#ifdef FEAT_EMACS_TAGS
6509 "emacs_tags",
6510#endif
6511 "eval", /* always present, of course! */
6512 "ex_extra", /* graduated feature */
6513#ifdef FEAT_SEARCH_EXTRA
6514 "extra_search",
6515#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516#ifdef FEAT_SEARCHPATH
6517 "file_in_path",
6518#endif
6519#ifdef FEAT_FILTERPIPE
6520 "filterpipe",
6521#endif
6522#ifdef FEAT_FIND_ID
6523 "find_in_path",
6524#endif
6525#ifdef FEAT_FLOAT
6526 "float",
6527#endif
6528#ifdef FEAT_FOLDING
6529 "folding",
6530#endif
6531#ifdef FEAT_FOOTER
6532 "footer",
6533#endif
6534#if !defined(USE_SYSTEM) && defined(UNIX)
6535 "fork",
6536#endif
6537#ifdef FEAT_GETTEXT
6538 "gettext",
6539#endif
6540#ifdef FEAT_GUI
6541 "gui",
6542#endif
6543#ifdef FEAT_GUI_ATHENA
6544# ifdef FEAT_GUI_NEXTAW
6545 "gui_neXtaw",
6546# else
6547 "gui_athena",
6548# endif
6549#endif
6550#ifdef FEAT_GUI_GTK
6551 "gui_gtk",
6552# ifdef USE_GTK3
6553 "gui_gtk3",
6554# else
6555 "gui_gtk2",
6556# endif
6557#endif
6558#ifdef FEAT_GUI_GNOME
6559 "gui_gnome",
6560#endif
6561#ifdef FEAT_GUI_MAC
6562 "gui_mac",
6563#endif
6564#ifdef FEAT_GUI_MOTIF
6565 "gui_motif",
6566#endif
6567#ifdef FEAT_GUI_PHOTON
6568 "gui_photon",
6569#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006570#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571 "gui_win32",
6572#endif
6573#ifdef FEAT_HANGULIN
6574 "hangul_input",
6575#endif
6576#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6577 "iconv",
6578#endif
6579#ifdef FEAT_INS_EXPAND
6580 "insert_expand",
6581#endif
6582#ifdef FEAT_JOB_CHANNEL
6583 "job",
6584#endif
6585#ifdef FEAT_JUMPLIST
6586 "jumplist",
6587#endif
6588#ifdef FEAT_KEYMAP
6589 "keymap",
6590#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006591 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006592#ifdef FEAT_LANGMAP
6593 "langmap",
6594#endif
6595#ifdef FEAT_LIBCALL
6596 "libcall",
6597#endif
6598#ifdef FEAT_LINEBREAK
6599 "linebreak",
6600#endif
6601#ifdef FEAT_LISP
6602 "lispindent",
6603#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006604 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006605#ifdef FEAT_LOCALMAP
6606 "localmap",
6607#endif
6608#ifdef FEAT_LUA
6609# ifndef DYNAMIC_LUA
6610 "lua",
6611# endif
6612#endif
6613#ifdef FEAT_MENU
6614 "menu",
6615#endif
6616#ifdef FEAT_SESSION
6617 "mksession",
6618#endif
6619#ifdef FEAT_MODIFY_FNAME
6620 "modify_fname",
6621#endif
6622#ifdef FEAT_MOUSE
6623 "mouse",
6624#endif
6625#ifdef FEAT_MOUSESHAPE
6626 "mouseshape",
6627#endif
6628#if defined(UNIX) || defined(VMS)
6629# ifdef FEAT_MOUSE_DEC
6630 "mouse_dec",
6631# endif
6632# ifdef FEAT_MOUSE_GPM
6633 "mouse_gpm",
6634# endif
6635# ifdef FEAT_MOUSE_JSB
6636 "mouse_jsbterm",
6637# endif
6638# ifdef FEAT_MOUSE_NET
6639 "mouse_netterm",
6640# endif
6641# ifdef FEAT_MOUSE_PTERM
6642 "mouse_pterm",
6643# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006644# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006645 "mouse_sgr",
6646# endif
6647# ifdef FEAT_SYSMOUSE
6648 "mouse_sysmouse",
6649# endif
6650# ifdef FEAT_MOUSE_URXVT
6651 "mouse_urxvt",
6652# endif
6653# ifdef FEAT_MOUSE_XTERM
6654 "mouse_xterm",
6655# endif
6656#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006657 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006658#ifdef FEAT_MBYTE_IME
6659 "multi_byte_ime",
6660#endif
6661#ifdef FEAT_MULTI_LANG
6662 "multi_lang",
6663#endif
6664#ifdef FEAT_MZSCHEME
6665#ifndef DYNAMIC_MZSCHEME
6666 "mzscheme",
6667#endif
6668#endif
6669#ifdef FEAT_NUM64
6670 "num64",
6671#endif
6672#ifdef FEAT_OLE
6673 "ole",
6674#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006675#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006676 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006677#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006678#ifdef FEAT_PATH_EXTRA
6679 "path_extra",
6680#endif
6681#ifdef FEAT_PERL
6682#ifndef DYNAMIC_PERL
6683 "perl",
6684#endif
6685#endif
6686#ifdef FEAT_PERSISTENT_UNDO
6687 "persistent_undo",
6688#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006689#if defined(FEAT_PYTHON)
6690 "python_compiled",
6691# if defined(DYNAMIC_PYTHON)
6692 "python_dynamic",
6693# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006695 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006696# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006697#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006698#if defined(FEAT_PYTHON3)
6699 "python3_compiled",
6700# if defined(DYNAMIC_PYTHON3)
6701 "python3_dynamic",
6702# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006703 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006704 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006705# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006706#endif
6707#ifdef FEAT_POSTSCRIPT
6708 "postscript",
6709#endif
6710#ifdef FEAT_PRINTER
6711 "printer",
6712#endif
6713#ifdef FEAT_PROFILE
6714 "profile",
6715#endif
6716#ifdef FEAT_RELTIME
6717 "reltime",
6718#endif
6719#ifdef FEAT_QUICKFIX
6720 "quickfix",
6721#endif
6722#ifdef FEAT_RIGHTLEFT
6723 "rightleft",
6724#endif
6725#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6726 "ruby",
6727#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006728 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729#ifdef FEAT_CMDL_INFO
6730 "showcmd",
6731 "cmdline_info",
6732#endif
6733#ifdef FEAT_SIGNS
6734 "signs",
6735#endif
6736#ifdef FEAT_SMARTINDENT
6737 "smartindent",
6738#endif
6739#ifdef STARTUPTIME
6740 "startuptime",
6741#endif
6742#ifdef FEAT_STL_OPT
6743 "statusline",
6744#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006745#ifdef FEAT_NETBEANS_INTG
6746 "netbeans_intg",
6747#endif
6748#ifdef FEAT_SPELL
6749 "spell",
6750#endif
6751#ifdef FEAT_SYN_HL
6752 "syntax",
6753#endif
6754#if defined(USE_SYSTEM) || !defined(UNIX)
6755 "system",
6756#endif
6757#ifdef FEAT_TAG_BINS
6758 "tag_binary",
6759#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006760#ifdef FEAT_TCL
6761# ifndef DYNAMIC_TCL
6762 "tcl",
6763# endif
6764#endif
6765#ifdef FEAT_TERMGUICOLORS
6766 "termguicolors",
6767#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006768#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006769 "terminal",
6770#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006771#ifdef TERMINFO
6772 "terminfo",
6773#endif
6774#ifdef FEAT_TERMRESPONSE
6775 "termresponse",
6776#endif
6777#ifdef FEAT_TEXTOBJ
6778 "textobjects",
6779#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006780#ifdef FEAT_TEXT_PROP
6781 "textprop",
6782#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006783#ifdef HAVE_TGETENT
6784 "tgetent",
6785#endif
6786#ifdef FEAT_TIMERS
6787 "timers",
6788#endif
6789#ifdef FEAT_TITLE
6790 "title",
6791#endif
6792#ifdef FEAT_TOOLBAR
6793 "toolbar",
6794#endif
6795#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6796 "unnamedplus",
6797#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006798 "user-commands", /* was accidentally included in 5.4 */
6799 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006800#ifdef FEAT_VARTABS
6801 "vartabs",
6802#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006803 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006804#ifdef FEAT_VIMINFO
6805 "viminfo",
6806#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006807 "vimscript-1",
6808 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006809 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006810 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006811 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006813 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006814#ifdef FEAT_VTP
6815 "vtp",
6816#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006817#ifdef FEAT_WILDIGN
6818 "wildignore",
6819#endif
6820#ifdef FEAT_WILDMENU
6821 "wildmenu",
6822#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006823 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006824#ifdef FEAT_WAK
6825 "winaltkeys",
6826#endif
6827#ifdef FEAT_WRITEBACKUP
6828 "writebackup",
6829#endif
6830#ifdef FEAT_XIM
6831 "xim",
6832#endif
6833#ifdef FEAT_XFONTSET
6834 "xfontset",
6835#endif
6836#ifdef FEAT_XPM_W32
6837 "xpm",
6838 "xpm_w32", /* for backward compatibility */
6839#else
6840# if defined(HAVE_XPM)
6841 "xpm",
6842# endif
6843#endif
6844#ifdef USE_XSMP
6845 "xsmp",
6846#endif
6847#ifdef USE_XSMP_INTERACT
6848 "xsmp_interact",
6849#endif
6850#ifdef FEAT_XCLIPBOARD
6851 "xterm_clipboard",
6852#endif
6853#ifdef FEAT_XTERM_SAVE
6854 "xterm_save",
6855#endif
6856#if defined(UNIX) && defined(FEAT_X11)
6857 "X11",
6858#endif
6859 NULL
6860 };
6861
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006862 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006863 for (i = 0; has_list[i] != NULL; ++i)
6864 if (STRICMP(name, has_list[i]) == 0)
6865 {
6866 n = TRUE;
6867 break;
6868 }
6869
6870 if (n == FALSE)
6871 {
6872 if (STRNICMP(name, "patch", 5) == 0)
6873 {
6874 if (name[5] == '-'
6875 && STRLEN(name) >= 11
6876 && vim_isdigit(name[6])
6877 && vim_isdigit(name[8])
6878 && vim_isdigit(name[10]))
6879 {
6880 int major = atoi((char *)name + 6);
6881 int minor = atoi((char *)name + 8);
6882
6883 /* Expect "patch-9.9.01234". */
6884 n = (major < VIM_VERSION_MAJOR
6885 || (major == VIM_VERSION_MAJOR
6886 && (minor < VIM_VERSION_MINOR
6887 || (minor == VIM_VERSION_MINOR
6888 && has_patch(atoi((char *)name + 10))))));
6889 }
6890 else
6891 n = has_patch(atoi((char *)name + 5));
6892 }
6893 else if (STRICMP(name, "vim_starting") == 0)
6894 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006895 else if (STRICMP(name, "ttyin") == 0)
6896 n = mch_input_isatty();
6897 else if (STRICMP(name, "ttyout") == 0)
6898 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006899 else if (STRICMP(name, "multi_byte_encoding") == 0)
6900 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006901#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006902 else if (STRICMP(name, "balloon_multiline") == 0)
6903 n = multiline_balloon_available();
6904#endif
6905#ifdef DYNAMIC_TCL
6906 else if (STRICMP(name, "tcl") == 0)
6907 n = tcl_enabled(FALSE);
6908#endif
6909#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6910 else if (STRICMP(name, "iconv") == 0)
6911 n = iconv_enabled(FALSE);
6912#endif
6913#ifdef DYNAMIC_LUA
6914 else if (STRICMP(name, "lua") == 0)
6915 n = lua_enabled(FALSE);
6916#endif
6917#ifdef DYNAMIC_MZSCHEME
6918 else if (STRICMP(name, "mzscheme") == 0)
6919 n = mzscheme_enabled(FALSE);
6920#endif
6921#ifdef DYNAMIC_RUBY
6922 else if (STRICMP(name, "ruby") == 0)
6923 n = ruby_enabled(FALSE);
6924#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925#ifdef DYNAMIC_PYTHON
6926 else if (STRICMP(name, "python") == 0)
6927 n = python_enabled(FALSE);
6928#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006929#ifdef DYNAMIC_PYTHON3
6930 else if (STRICMP(name, "python3") == 0)
6931 n = python3_enabled(FALSE);
6932#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006933#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6934 else if (STRICMP(name, "pythonx") == 0)
6935 {
6936# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6937 if (p_pyx == 0)
6938 n = python3_enabled(FALSE) || python_enabled(FALSE);
6939 else if (p_pyx == 3)
6940 n = python3_enabled(FALSE);
6941 else if (p_pyx == 2)
6942 n = python_enabled(FALSE);
6943# elif defined(DYNAMIC_PYTHON)
6944 n = python_enabled(FALSE);
6945# elif defined(DYNAMIC_PYTHON3)
6946 n = python3_enabled(FALSE);
6947# endif
6948 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006949#endif
6950#ifdef DYNAMIC_PERL
6951 else if (STRICMP(name, "perl") == 0)
6952 n = perl_enabled(FALSE);
6953#endif
6954#ifdef FEAT_GUI
6955 else if (STRICMP(name, "gui_running") == 0)
6956 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006957# ifdef FEAT_BROWSE
6958 else if (STRICMP(name, "browse") == 0)
6959 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6960# endif
6961#endif
6962#ifdef FEAT_SYN_HL
6963 else if (STRICMP(name, "syntax_items") == 0)
6964 n = syntax_present(curwin);
6965#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006966#ifdef FEAT_VTP
6967 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006968 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006969#endif
6970#ifdef FEAT_NETBEANS_INTG
6971 else if (STRICMP(name, "netbeans_enabled") == 0)
6972 n = netbeans_active();
6973#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006974#ifdef FEAT_MOUSE_GPM
6975 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6976 n = gpm_enabled();
6977#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006978#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006979 else if (STRICMP(name, "terminal") == 0)
6980 n = terminal_enabled();
6981#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006982#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006983 else if (STRICMP(name, "conpty") == 0)
6984 n = use_conpty();
6985#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006986 }
6987
6988 rettv->vval.v_number = n;
6989}
6990
6991/*
6992 * "has_key()" function
6993 */
6994 static void
6995f_has_key(typval_T *argvars, typval_T *rettv)
6996{
6997 if (argvars[0].v_type != VAR_DICT)
6998 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006999 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007000 return;
7001 }
7002 if (argvars[0].vval.v_dict == NULL)
7003 return;
7004
7005 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007006 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007}
7008
7009/*
7010 * "haslocaldir()" function
7011 */
7012 static void
7013f_haslocaldir(typval_T *argvars, typval_T *rettv)
7014{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007015 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007016 win_T *wp = NULL;
7017
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007018 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7019
7020 // Check for window-local and tab-local directories
7021 if (wp != NULL && wp->w_localdir != NULL)
7022 rettv->vval.v_number = 1;
7023 else if (tp != NULL && tp->tp_localdir != NULL)
7024 rettv->vval.v_number = 2;
7025 else
7026 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027}
7028
7029/*
7030 * "hasmapto()" function
7031 */
7032 static void
7033f_hasmapto(typval_T *argvars, typval_T *rettv)
7034{
7035 char_u *name;
7036 char_u *mode;
7037 char_u buf[NUMBUFLEN];
7038 int abbr = FALSE;
7039
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007040 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007041 if (argvars[1].v_type == VAR_UNKNOWN)
7042 mode = (char_u *)"nvo";
7043 else
7044 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007045 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007047 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007048 }
7049
7050 if (map_to_exists(name, mode, abbr))
7051 rettv->vval.v_number = TRUE;
7052 else
7053 rettv->vval.v_number = FALSE;
7054}
7055
7056/*
7057 * "histadd()" function
7058 */
7059 static void
7060f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7061{
7062#ifdef FEAT_CMDHIST
7063 int histype;
7064 char_u *str;
7065 char_u buf[NUMBUFLEN];
7066#endif
7067
7068 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007069 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007070 return;
7071#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007072 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007073 histype = str != NULL ? get_histtype(str) : -1;
7074 if (histype >= 0)
7075 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007076 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007077 if (*str != NUL)
7078 {
7079 init_history();
7080 add_to_history(histype, str, FALSE, NUL);
7081 rettv->vval.v_number = TRUE;
7082 return;
7083 }
7084 }
7085#endif
7086}
7087
7088/*
7089 * "histdel()" function
7090 */
7091 static void
7092f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7093{
7094#ifdef FEAT_CMDHIST
7095 int n;
7096 char_u buf[NUMBUFLEN];
7097 char_u *str;
7098
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007099 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007100 if (str == NULL)
7101 n = 0;
7102 else if (argvars[1].v_type == VAR_UNKNOWN)
7103 /* only one argument: clear entire history */
7104 n = clr_history(get_histtype(str));
7105 else if (argvars[1].v_type == VAR_NUMBER)
7106 /* index given: remove that entry */
7107 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007108 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007109 else
7110 /* string given: remove all matching entries */
7111 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007112 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007113 rettv->vval.v_number = n;
7114#endif
7115}
7116
7117/*
7118 * "histget()" function
7119 */
7120 static void
7121f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7122{
7123#ifdef FEAT_CMDHIST
7124 int type;
7125 int idx;
7126 char_u *str;
7127
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007128 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007129 if (str == NULL)
7130 rettv->vval.v_string = NULL;
7131 else
7132 {
7133 type = get_histtype(str);
7134 if (argvars[1].v_type == VAR_UNKNOWN)
7135 idx = get_history_idx(type);
7136 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007137 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007138 /* -1 on type error */
7139 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7140 }
7141#else
7142 rettv->vval.v_string = NULL;
7143#endif
7144 rettv->v_type = VAR_STRING;
7145}
7146
7147/*
7148 * "histnr()" function
7149 */
7150 static void
7151f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7152{
7153 int i;
7154
7155#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007156 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007157
7158 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7159 if (i >= HIST_CMD && i < HIST_COUNT)
7160 i = get_history_idx(i);
7161 else
7162#endif
7163 i = -1;
7164 rettv->vval.v_number = i;
7165}
7166
7167/*
7168 * "highlightID(name)" function
7169 */
7170 static void
7171f_hlID(typval_T *argvars, typval_T *rettv)
7172{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007173 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007174}
7175
7176/*
7177 * "highlight_exists()" function
7178 */
7179 static void
7180f_hlexists(typval_T *argvars, typval_T *rettv)
7181{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007182 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007183}
7184
7185/*
7186 * "hostname()" function
7187 */
7188 static void
7189f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7190{
7191 char_u hostname[256];
7192
7193 mch_get_host_name(hostname, 256);
7194 rettv->v_type = VAR_STRING;
7195 rettv->vval.v_string = vim_strsave(hostname);
7196}
7197
7198/*
7199 * iconv() function
7200 */
7201 static void
7202f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7203{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007204 char_u buf1[NUMBUFLEN];
7205 char_u buf2[NUMBUFLEN];
7206 char_u *from, *to, *str;
7207 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007208
7209 rettv->v_type = VAR_STRING;
7210 rettv->vval.v_string = NULL;
7211
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007212 str = tv_get_string(&argvars[0]);
7213 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7214 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007215 vimconv.vc_type = CONV_NONE;
7216 convert_setup(&vimconv, from, to);
7217
7218 /* If the encodings are equal, no conversion needed. */
7219 if (vimconv.vc_type == CONV_NONE)
7220 rettv->vval.v_string = vim_strsave(str);
7221 else
7222 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7223
7224 convert_setup(&vimconv, NULL, NULL);
7225 vim_free(from);
7226 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007227}
7228
7229/*
7230 * "indent()" function
7231 */
7232 static void
7233f_indent(typval_T *argvars, typval_T *rettv)
7234{
7235 linenr_T lnum;
7236
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007237 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007238 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7239 rettv->vval.v_number = get_indent_lnum(lnum);
7240 else
7241 rettv->vval.v_number = -1;
7242}
7243
7244/*
7245 * "index()" function
7246 */
7247 static void
7248f_index(typval_T *argvars, typval_T *rettv)
7249{
7250 list_T *l;
7251 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007252 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007253 long idx = 0;
7254 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007255 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007256
7257 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007258 if (argvars[0].v_type == VAR_BLOB)
7259 {
7260 typval_T tv;
7261 int start = 0;
7262
7263 if (argvars[2].v_type != VAR_UNKNOWN)
7264 {
7265 start = tv_get_number_chk(&argvars[2], &error);
7266 if (error)
7267 return;
7268 }
7269 b = argvars[0].vval.v_blob;
7270 if (b == NULL)
7271 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007272 if (start < 0)
7273 {
7274 start = blob_len(b) + start;
7275 if (start < 0)
7276 start = 0;
7277 }
7278
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007279 for (idx = start; idx < blob_len(b); ++idx)
7280 {
7281 tv.v_type = VAR_NUMBER;
7282 tv.vval.v_number = blob_get(b, idx);
7283 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7284 {
7285 rettv->vval.v_number = idx;
7286 return;
7287 }
7288 }
7289 return;
7290 }
7291 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007292 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007293 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007294 return;
7295 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007296
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007297 l = argvars[0].vval.v_list;
7298 if (l != NULL)
7299 {
7300 item = l->lv_first;
7301 if (argvars[2].v_type != VAR_UNKNOWN)
7302 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007303 /* Start at specified item. Use the cached index that list_find()
7304 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007305 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007306 idx = l->lv_idx;
7307 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007308 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007309 if (error)
7310 item = NULL;
7311 }
7312
7313 for ( ; item != NULL; item = item->li_next, ++idx)
7314 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7315 {
7316 rettv->vval.v_number = idx;
7317 break;
7318 }
7319 }
7320}
7321
7322static int inputsecret_flag = 0;
7323
7324/*
7325 * "input()" function
7326 * Also handles inputsecret() when inputsecret is set.
7327 */
7328 static void
7329f_input(typval_T *argvars, typval_T *rettv)
7330{
7331 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7332}
7333
7334/*
7335 * "inputdialog()" function
7336 */
7337 static void
7338f_inputdialog(typval_T *argvars, typval_T *rettv)
7339{
7340#if defined(FEAT_GUI_TEXTDIALOG)
7341 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7342 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7343 {
7344 char_u *message;
7345 char_u buf[NUMBUFLEN];
7346 char_u *defstr = (char_u *)"";
7347
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007348 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007349 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007350 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007351 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7352 else
7353 IObuff[0] = NUL;
7354 if (message != NULL && defstr != NULL
7355 && do_dialog(VIM_QUESTION, NULL, message,
7356 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7357 rettv->vval.v_string = vim_strsave(IObuff);
7358 else
7359 {
7360 if (message != NULL && defstr != NULL
7361 && argvars[1].v_type != VAR_UNKNOWN
7362 && argvars[2].v_type != VAR_UNKNOWN)
7363 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007364 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007365 else
7366 rettv->vval.v_string = NULL;
7367 }
7368 rettv->v_type = VAR_STRING;
7369 }
7370 else
7371#endif
7372 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7373}
7374
7375/*
7376 * "inputlist()" function
7377 */
7378 static void
7379f_inputlist(typval_T *argvars, typval_T *rettv)
7380{
7381 listitem_T *li;
7382 int selected;
7383 int mouse_used;
7384
7385#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007386 /* While starting up, there is no place to enter text. When running tests
7387 * with --not-a-term we assume feedkeys() will be used. */
7388 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007389 return;
7390#endif
7391 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7392 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007393 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007394 return;
7395 }
7396
7397 msg_start();
7398 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7399 lines_left = Rows; /* avoid more prompt */
7400 msg_scroll = TRUE;
7401 msg_clr_eos();
7402
7403 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7404 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007405 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007406 msg_putchar('\n');
7407 }
7408
7409 /* Ask for choice. */
7410 selected = prompt_for_number(&mouse_used);
7411 if (mouse_used)
7412 selected -= lines_left;
7413
7414 rettv->vval.v_number = selected;
7415}
7416
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007417static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7418
7419/*
7420 * "inputrestore()" function
7421 */
7422 static void
7423f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7424{
7425 if (ga_userinput.ga_len > 0)
7426 {
7427 --ga_userinput.ga_len;
7428 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7429 + ga_userinput.ga_len);
7430 /* default return is zero == OK */
7431 }
7432 else if (p_verbose > 1)
7433 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007434 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435 rettv->vval.v_number = 1; /* Failed */
7436 }
7437}
7438
7439/*
7440 * "inputsave()" function
7441 */
7442 static void
7443f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7444{
7445 /* Add an entry to the stack of typeahead storage. */
7446 if (ga_grow(&ga_userinput, 1) == OK)
7447 {
7448 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7449 + ga_userinput.ga_len);
7450 ++ga_userinput.ga_len;
7451 /* default return is zero == OK */
7452 }
7453 else
7454 rettv->vval.v_number = 1; /* Failed */
7455}
7456
7457/*
7458 * "inputsecret()" function
7459 */
7460 static void
7461f_inputsecret(typval_T *argvars, typval_T *rettv)
7462{
7463 ++cmdline_star;
7464 ++inputsecret_flag;
7465 f_input(argvars, rettv);
7466 --cmdline_star;
7467 --inputsecret_flag;
7468}
7469
7470/*
7471 * "insert()" function
7472 */
7473 static void
7474f_insert(typval_T *argvars, typval_T *rettv)
7475{
7476 long before = 0;
7477 listitem_T *item;
7478 list_T *l;
7479 int error = FALSE;
7480
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007481 if (argvars[0].v_type == VAR_BLOB)
7482 {
7483 int val, len;
7484 char_u *p;
7485
7486 len = blob_len(argvars[0].vval.v_blob);
7487 if (argvars[2].v_type != VAR_UNKNOWN)
7488 {
7489 before = (long)tv_get_number_chk(&argvars[2], &error);
7490 if (error)
7491 return; // type error; errmsg already given
7492 if (before < 0 || before > len)
7493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007494 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007495 return;
7496 }
7497 }
7498 val = tv_get_number_chk(&argvars[1], &error);
7499 if (error)
7500 return;
7501 if (val < 0 || val > 255)
7502 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007503 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007504 return;
7505 }
7506
7507 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7508 return;
7509 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7510 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7511 *(p + before) = val;
7512 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7513
7514 copy_tv(&argvars[0], rettv);
7515 }
7516 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007517 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007518 else if ((l = argvars[0].vval.v_list) != NULL
7519 && !var_check_lock(l->lv_lock,
7520 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007521 {
7522 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007523 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007524 if (error)
7525 return; /* type error; errmsg already given */
7526
7527 if (before == l->lv_len)
7528 item = NULL;
7529 else
7530 {
7531 item = list_find(l, before);
7532 if (item == NULL)
7533 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007534 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007535 l = NULL;
7536 }
7537 }
7538 if (l != NULL)
7539 {
7540 list_insert_tv(l, &argvars[1], item);
7541 copy_tv(&argvars[0], rettv);
7542 }
7543 }
7544}
7545
7546/*
7547 * "invert(expr)" function
7548 */
7549 static void
7550f_invert(typval_T *argvars, typval_T *rettv)
7551{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007552 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007553}
7554
7555/*
7556 * "isdirectory()" function
7557 */
7558 static void
7559f_isdirectory(typval_T *argvars, typval_T *rettv)
7560{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007561 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007562}
7563
7564/*
7565 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7566 * or it refers to a List or Dictionary that is locked.
7567 */
7568 static int
7569tv_islocked(typval_T *tv)
7570{
7571 return (tv->v_lock & VAR_LOCKED)
7572 || (tv->v_type == VAR_LIST
7573 && tv->vval.v_list != NULL
7574 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7575 || (tv->v_type == VAR_DICT
7576 && tv->vval.v_dict != NULL
7577 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7578}
7579
7580/*
7581 * "islocked()" function
7582 */
7583 static void
7584f_islocked(typval_T *argvars, typval_T *rettv)
7585{
7586 lval_T lv;
7587 char_u *end;
7588 dictitem_T *di;
7589
7590 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007591 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007592 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007593 if (end != NULL && lv.ll_name != NULL)
7594 {
7595 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007596 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007597 else
7598 {
7599 if (lv.ll_tv == NULL)
7600 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007601 di = find_var(lv.ll_name, NULL, TRUE);
7602 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007604 /* Consider a variable locked when:
7605 * 1. the variable itself is locked
7606 * 2. the value of the variable is locked.
7607 * 3. the List or Dict value is locked.
7608 */
7609 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7610 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007611 }
7612 }
7613 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007614 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007615 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007616 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007617 else if (lv.ll_list != NULL)
7618 /* List item. */
7619 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7620 else
7621 /* Dictionary item. */
7622 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7623 }
7624 }
7625
7626 clear_lval(&lv);
7627}
7628
7629#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7630/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007631 * "isinf()" function
7632 */
7633 static void
7634f_isinf(typval_T *argvars, typval_T *rettv)
7635{
7636 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7637 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7638}
7639
7640/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007641 * "isnan()" function
7642 */
7643 static void
7644f_isnan(typval_T *argvars, typval_T *rettv)
7645{
7646 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7647 && isnan(argvars[0].vval.v_float);
7648}
7649#endif
7650
7651/*
7652 * "items(dict)" function
7653 */
7654 static void
7655f_items(typval_T *argvars, typval_T *rettv)
7656{
7657 dict_list(argvars, rettv, 2);
7658}
7659
7660#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7661/*
7662 * Get the job from the argument.
7663 * Returns NULL if the job is invalid.
7664 */
7665 static job_T *
7666get_job_arg(typval_T *tv)
7667{
7668 job_T *job;
7669
7670 if (tv->v_type != VAR_JOB)
7671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007672 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007673 return NULL;
7674 }
7675 job = tv->vval.v_job;
7676
7677 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007678 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007679 return job;
7680}
7681
7682/*
7683 * "job_getchannel()" function
7684 */
7685 static void
7686f_job_getchannel(typval_T *argvars, typval_T *rettv)
7687{
7688 job_T *job = get_job_arg(&argvars[0]);
7689
7690 if (job != NULL)
7691 {
7692 rettv->v_type = VAR_CHANNEL;
7693 rettv->vval.v_channel = job->jv_channel;
7694 if (job->jv_channel != NULL)
7695 ++job->jv_channel->ch_refcount;
7696 }
7697}
7698
7699/*
7700 * "job_info()" function
7701 */
7702 static void
7703f_job_info(typval_T *argvars, typval_T *rettv)
7704{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007705 if (argvars[0].v_type != VAR_UNKNOWN)
7706 {
7707 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007708
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007709 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7710 job_info(job, rettv->vval.v_dict);
7711 }
7712 else if (rettv_list_alloc(rettv) == OK)
7713 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007714}
7715
7716/*
7717 * "job_setoptions()" function
7718 */
7719 static void
7720f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7721{
7722 job_T *job = get_job_arg(&argvars[0]);
7723 jobopt_T opt;
7724
7725 if (job == NULL)
7726 return;
7727 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007728 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007729 job_set_options(job, &opt);
7730 free_job_options(&opt);
7731}
7732
7733/*
7734 * "job_start()" function
7735 */
7736 static void
7737f_job_start(typval_T *argvars, typval_T *rettv)
7738{
7739 rettv->v_type = VAR_JOB;
7740 if (check_restricted() || check_secure())
7741 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007742 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007743}
7744
7745/*
7746 * "job_status()" function
7747 */
7748 static void
7749f_job_status(typval_T *argvars, typval_T *rettv)
7750{
7751 job_T *job = get_job_arg(&argvars[0]);
7752
7753 if (job != NULL)
7754 {
7755 rettv->v_type = VAR_STRING;
7756 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7757 }
7758}
7759
7760/*
7761 * "job_stop()" function
7762 */
7763 static void
7764f_job_stop(typval_T *argvars, typval_T *rettv)
7765{
7766 job_T *job = get_job_arg(&argvars[0]);
7767
7768 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007769 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007770}
7771#endif
7772
7773/*
7774 * "join()" function
7775 */
7776 static void
7777f_join(typval_T *argvars, typval_T *rettv)
7778{
7779 garray_T ga;
7780 char_u *sep;
7781
7782 if (argvars[0].v_type != VAR_LIST)
7783 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007784 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007785 return;
7786 }
7787 if (argvars[0].vval.v_list == NULL)
7788 return;
7789 if (argvars[1].v_type == VAR_UNKNOWN)
7790 sep = (char_u *)" ";
7791 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007792 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007793
7794 rettv->v_type = VAR_STRING;
7795
7796 if (sep != NULL)
7797 {
7798 ga_init2(&ga, (int)sizeof(char), 80);
7799 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7800 ga_append(&ga, NUL);
7801 rettv->vval.v_string = (char_u *)ga.ga_data;
7802 }
7803 else
7804 rettv->vval.v_string = NULL;
7805}
7806
7807/*
7808 * "js_decode()" function
7809 */
7810 static void
7811f_js_decode(typval_T *argvars, typval_T *rettv)
7812{
7813 js_read_T reader;
7814
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007815 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007816 reader.js_fill = NULL;
7817 reader.js_used = 0;
7818 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007819 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007820}
7821
7822/*
7823 * "js_encode()" function
7824 */
7825 static void
7826f_js_encode(typval_T *argvars, typval_T *rettv)
7827{
7828 rettv->v_type = VAR_STRING;
7829 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7830}
7831
7832/*
7833 * "json_decode()" function
7834 */
7835 static void
7836f_json_decode(typval_T *argvars, typval_T *rettv)
7837{
7838 js_read_T reader;
7839
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007840 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007841 reader.js_fill = NULL;
7842 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007843 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007844}
7845
7846/*
7847 * "json_encode()" function
7848 */
7849 static void
7850f_json_encode(typval_T *argvars, typval_T *rettv)
7851{
7852 rettv->v_type = VAR_STRING;
7853 rettv->vval.v_string = json_encode(&argvars[0], 0);
7854}
7855
7856/*
7857 * "keys()" function
7858 */
7859 static void
7860f_keys(typval_T *argvars, typval_T *rettv)
7861{
7862 dict_list(argvars, rettv, 0);
7863}
7864
7865/*
7866 * "last_buffer_nr()" function.
7867 */
7868 static void
7869f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7870{
7871 int n = 0;
7872 buf_T *buf;
7873
Bram Moolenaar29323592016-07-24 22:04:11 +02007874 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007875 if (n < buf->b_fnum)
7876 n = buf->b_fnum;
7877
7878 rettv->vval.v_number = n;
7879}
7880
7881/*
7882 * "len()" function
7883 */
7884 static void
7885f_len(typval_T *argvars, typval_T *rettv)
7886{
7887 switch (argvars[0].v_type)
7888 {
7889 case VAR_STRING:
7890 case VAR_NUMBER:
7891 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007892 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007893 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007894 case VAR_BLOB:
7895 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7896 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007897 case VAR_LIST:
7898 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7899 break;
7900 case VAR_DICT:
7901 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7902 break;
7903 case VAR_UNKNOWN:
7904 case VAR_SPECIAL:
7905 case VAR_FLOAT:
7906 case VAR_FUNC:
7907 case VAR_PARTIAL:
7908 case VAR_JOB:
7909 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007910 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007911 break;
7912 }
7913}
7914
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007915 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007916libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007917{
7918#ifdef FEAT_LIBCALL
7919 char_u *string_in;
7920 char_u **string_result;
7921 int nr_result;
7922#endif
7923
7924 rettv->v_type = type;
7925 if (type != VAR_NUMBER)
7926 rettv->vval.v_string = NULL;
7927
7928 if (check_restricted() || check_secure())
7929 return;
7930
7931#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007932 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007933 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7934 {
7935 string_in = NULL;
7936 if (argvars[2].v_type == VAR_STRING)
7937 string_in = argvars[2].vval.v_string;
7938 if (type == VAR_NUMBER)
7939 string_result = NULL;
7940 else
7941 string_result = &rettv->vval.v_string;
7942 if (mch_libcall(argvars[0].vval.v_string,
7943 argvars[1].vval.v_string,
7944 string_in,
7945 argvars[2].vval.v_number,
7946 string_result,
7947 &nr_result) == OK
7948 && type == VAR_NUMBER)
7949 rettv->vval.v_number = nr_result;
7950 }
7951#endif
7952}
7953
7954/*
7955 * "libcall()" function
7956 */
7957 static void
7958f_libcall(typval_T *argvars, typval_T *rettv)
7959{
7960 libcall_common(argvars, rettv, VAR_STRING);
7961}
7962
7963/*
7964 * "libcallnr()" function
7965 */
7966 static void
7967f_libcallnr(typval_T *argvars, typval_T *rettv)
7968{
7969 libcall_common(argvars, rettv, VAR_NUMBER);
7970}
7971
7972/*
7973 * "line(string)" function
7974 */
7975 static void
7976f_line(typval_T *argvars, typval_T *rettv)
7977{
7978 linenr_T lnum = 0;
7979 pos_T *fp;
7980 int fnum;
7981
7982 fp = var2fpos(&argvars[0], TRUE, &fnum);
7983 if (fp != NULL)
7984 lnum = fp->lnum;
7985 rettv->vval.v_number = lnum;
7986}
7987
7988/*
7989 * "line2byte(lnum)" function
7990 */
7991 static void
7992f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7993{
7994#ifndef FEAT_BYTEOFF
7995 rettv->vval.v_number = -1;
7996#else
7997 linenr_T lnum;
7998
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007999 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008000 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
8001 rettv->vval.v_number = -1;
8002 else
8003 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8004 if (rettv->vval.v_number >= 0)
8005 ++rettv->vval.v_number;
8006#endif
8007}
8008
8009/*
8010 * "lispindent(lnum)" function
8011 */
8012 static void
8013f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8014{
8015#ifdef FEAT_LISP
8016 pos_T pos;
8017 linenr_T lnum;
8018
8019 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008020 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008021 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8022 {
8023 curwin->w_cursor.lnum = lnum;
8024 rettv->vval.v_number = get_lisp_indent();
8025 curwin->w_cursor = pos;
8026 }
8027 else
8028#endif
8029 rettv->vval.v_number = -1;
8030}
8031
8032/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008033 * "list2str()" function
8034 */
8035 static void
8036f_list2str(typval_T *argvars, typval_T *rettv)
8037{
8038 list_T *l;
8039 listitem_T *li;
8040 garray_T ga;
8041 int utf8 = FALSE;
8042
8043 rettv->v_type = VAR_STRING;
8044 rettv->vval.v_string = NULL;
8045 if (argvars[0].v_type != VAR_LIST)
8046 {
8047 emsg(_(e_invarg));
8048 return;
8049 }
8050
8051 l = argvars[0].vval.v_list;
8052 if (l == NULL)
8053 return; // empty list results in empty string
8054
8055 if (argvars[1].v_type != VAR_UNKNOWN)
8056 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8057
8058 ga_init2(&ga, 1, 80);
8059 if (has_mbyte || utf8)
8060 {
8061 char_u buf[MB_MAXBYTES + 1];
8062 int (*char2bytes)(int, char_u *);
8063
8064 if (utf8 || enc_utf8)
8065 char2bytes = utf_char2bytes;
8066 else
8067 char2bytes = mb_char2bytes;
8068
8069 for (li = l->lv_first; li != NULL; li = li->li_next)
8070 {
8071 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8072 ga_concat(&ga, buf);
8073 }
8074 ga_append(&ga, NUL);
8075 }
8076 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8077 {
8078 for (li = l->lv_first; li != NULL; li = li->li_next)
8079 ga_append(&ga, tv_get_number(&li->li_tv));
8080 ga_append(&ga, NUL);
8081 }
8082
8083 rettv->v_type = VAR_STRING;
8084 rettv->vval.v_string = ga.ga_data;
8085}
8086
8087/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088 * "localtime()" function
8089 */
8090 static void
8091f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8092{
8093 rettv->vval.v_number = (varnumber_T)time(NULL);
8094}
8095
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008096 static void
8097get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8098{
8099 char_u *keys;
8100 char_u *which;
8101 char_u buf[NUMBUFLEN];
8102 char_u *keys_buf = NULL;
8103 char_u *rhs;
8104 int mode;
8105 int abbr = FALSE;
8106 int get_dict = FALSE;
8107 mapblock_T *mp;
8108 int buffer_local;
8109
8110 /* return empty string for failure */
8111 rettv->v_type = VAR_STRING;
8112 rettv->vval.v_string = NULL;
8113
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008114 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115 if (*keys == NUL)
8116 return;
8117
8118 if (argvars[1].v_type != VAR_UNKNOWN)
8119 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008120 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008121 if (argvars[2].v_type != VAR_UNKNOWN)
8122 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008123 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008124 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008125 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008126 }
8127 }
8128 else
8129 which = (char_u *)"";
8130 if (which == NULL)
8131 return;
8132
8133 mode = get_map_mode(&which, 0);
8134
8135 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8136 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8137 vim_free(keys_buf);
8138
8139 if (!get_dict)
8140 {
8141 /* Return a string. */
8142 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008143 {
8144 if (*rhs == NUL)
8145 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8146 else
8147 rettv->vval.v_string = str2special_save(rhs, FALSE);
8148 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149
8150 }
8151 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8152 {
8153 /* Return a dictionary. */
8154 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8155 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8156 dict_T *dict = rettv->vval.v_dict;
8157
Bram Moolenaare0be1672018-07-08 16:50:37 +02008158 dict_add_string(dict, "lhs", lhs);
8159 dict_add_string(dict, "rhs", mp->m_orig_str);
8160 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8161 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8162 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008163 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8164 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008165 dict_add_number(dict, "buffer", (long)buffer_local);
8166 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8167 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008168
8169 vim_free(lhs);
8170 vim_free(mapmode);
8171 }
8172}
8173
8174#ifdef FEAT_FLOAT
8175/*
8176 * "log()" function
8177 */
8178 static void
8179f_log(typval_T *argvars, typval_T *rettv)
8180{
8181 float_T f = 0.0;
8182
8183 rettv->v_type = VAR_FLOAT;
8184 if (get_float_arg(argvars, &f) == OK)
8185 rettv->vval.v_float = log(f);
8186 else
8187 rettv->vval.v_float = 0.0;
8188}
8189
8190/*
8191 * "log10()" function
8192 */
8193 static void
8194f_log10(typval_T *argvars, typval_T *rettv)
8195{
8196 float_T f = 0.0;
8197
8198 rettv->v_type = VAR_FLOAT;
8199 if (get_float_arg(argvars, &f) == OK)
8200 rettv->vval.v_float = log10(f);
8201 else
8202 rettv->vval.v_float = 0.0;
8203}
8204#endif
8205
8206#ifdef FEAT_LUA
8207/*
8208 * "luaeval()" function
8209 */
8210 static void
8211f_luaeval(typval_T *argvars, typval_T *rettv)
8212{
8213 char_u *str;
8214 char_u buf[NUMBUFLEN];
8215
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008216 if (check_restricted() || check_secure())
8217 return;
8218
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008219 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008220 do_luaeval(str, argvars + 1, rettv);
8221}
8222#endif
8223
8224/*
8225 * "map()" function
8226 */
8227 static void
8228f_map(typval_T *argvars, typval_T *rettv)
8229{
8230 filter_map(argvars, rettv, TRUE);
8231}
8232
8233/*
8234 * "maparg()" function
8235 */
8236 static void
8237f_maparg(typval_T *argvars, typval_T *rettv)
8238{
8239 get_maparg(argvars, rettv, TRUE);
8240}
8241
8242/*
8243 * "mapcheck()" function
8244 */
8245 static void
8246f_mapcheck(typval_T *argvars, typval_T *rettv)
8247{
8248 get_maparg(argvars, rettv, FALSE);
8249}
8250
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008251typedef enum
8252{
8253 MATCH_END, /* matchend() */
8254 MATCH_MATCH, /* match() */
8255 MATCH_STR, /* matchstr() */
8256 MATCH_LIST, /* matchlist() */
8257 MATCH_POS /* matchstrpos() */
8258} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008259
8260 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008261find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008262{
8263 char_u *str = NULL;
8264 long len = 0;
8265 char_u *expr = NULL;
8266 char_u *pat;
8267 regmatch_T regmatch;
8268 char_u patbuf[NUMBUFLEN];
8269 char_u strbuf[NUMBUFLEN];
8270 char_u *save_cpo;
8271 long start = 0;
8272 long nth = 1;
8273 colnr_T startcol = 0;
8274 int match = 0;
8275 list_T *l = NULL;
8276 listitem_T *li = NULL;
8277 long idx = 0;
8278 char_u *tofree = NULL;
8279
8280 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8281 save_cpo = p_cpo;
8282 p_cpo = (char_u *)"";
8283
8284 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008285 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008286 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008287 /* type MATCH_LIST: return empty list when there are no matches.
8288 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 if (rettv_list_alloc(rettv) == FAIL)
8290 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008291 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008292 && (list_append_string(rettv->vval.v_list,
8293 (char_u *)"", 0) == FAIL
8294 || list_append_number(rettv->vval.v_list,
8295 (varnumber_T)-1) == FAIL
8296 || list_append_number(rettv->vval.v_list,
8297 (varnumber_T)-1) == FAIL
8298 || list_append_number(rettv->vval.v_list,
8299 (varnumber_T)-1) == FAIL))
8300 {
8301 list_free(rettv->vval.v_list);
8302 rettv->vval.v_list = NULL;
8303 goto theend;
8304 }
8305 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008306 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008307 {
8308 rettv->v_type = VAR_STRING;
8309 rettv->vval.v_string = NULL;
8310 }
8311
8312 if (argvars[0].v_type == VAR_LIST)
8313 {
8314 if ((l = argvars[0].vval.v_list) == NULL)
8315 goto theend;
8316 li = l->lv_first;
8317 }
8318 else
8319 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008320 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008321 len = (long)STRLEN(str);
8322 }
8323
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008324 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008325 if (pat == NULL)
8326 goto theend;
8327
8328 if (argvars[2].v_type != VAR_UNKNOWN)
8329 {
8330 int error = FALSE;
8331
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008332 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008333 if (error)
8334 goto theend;
8335 if (l != NULL)
8336 {
8337 li = list_find(l, start);
8338 if (li == NULL)
8339 goto theend;
8340 idx = l->lv_idx; /* use the cached index */
8341 }
8342 else
8343 {
8344 if (start < 0)
8345 start = 0;
8346 if (start > len)
8347 goto theend;
8348 /* When "count" argument is there ignore matches before "start",
8349 * otherwise skip part of the string. Differs when pattern is "^"
8350 * or "\<". */
8351 if (argvars[3].v_type != VAR_UNKNOWN)
8352 startcol = start;
8353 else
8354 {
8355 str += start;
8356 len -= start;
8357 }
8358 }
8359
8360 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008361 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 if (error)
8363 goto theend;
8364 }
8365
8366 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8367 if (regmatch.regprog != NULL)
8368 {
8369 regmatch.rm_ic = p_ic;
8370
8371 for (;;)
8372 {
8373 if (l != NULL)
8374 {
8375 if (li == NULL)
8376 {
8377 match = FALSE;
8378 break;
8379 }
8380 vim_free(tofree);
8381 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8382 if (str == NULL)
8383 break;
8384 }
8385
8386 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8387
8388 if (match && --nth <= 0)
8389 break;
8390 if (l == NULL && !match)
8391 break;
8392
8393 /* Advance to just after the match. */
8394 if (l != NULL)
8395 {
8396 li = li->li_next;
8397 ++idx;
8398 }
8399 else
8400 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008401 startcol = (colnr_T)(regmatch.startp[0]
8402 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403 if (startcol > (colnr_T)len
8404 || str + startcol <= regmatch.startp[0])
8405 {
8406 match = FALSE;
8407 break;
8408 }
8409 }
8410 }
8411
8412 if (match)
8413 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008414 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008415 {
8416 listitem_T *li1 = rettv->vval.v_list->lv_first;
8417 listitem_T *li2 = li1->li_next;
8418 listitem_T *li3 = li2->li_next;
8419 listitem_T *li4 = li3->li_next;
8420
8421 vim_free(li1->li_tv.vval.v_string);
8422 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8423 (int)(regmatch.endp[0] - regmatch.startp[0]));
8424 li3->li_tv.vval.v_number =
8425 (varnumber_T)(regmatch.startp[0] - expr);
8426 li4->li_tv.vval.v_number =
8427 (varnumber_T)(regmatch.endp[0] - expr);
8428 if (l != NULL)
8429 li2->li_tv.vval.v_number = (varnumber_T)idx;
8430 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008431 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008432 {
8433 int i;
8434
8435 /* return list with matched string and submatches */
8436 for (i = 0; i < NSUBEXP; ++i)
8437 {
8438 if (regmatch.endp[i] == NULL)
8439 {
8440 if (list_append_string(rettv->vval.v_list,
8441 (char_u *)"", 0) == FAIL)
8442 break;
8443 }
8444 else if (list_append_string(rettv->vval.v_list,
8445 regmatch.startp[i],
8446 (int)(regmatch.endp[i] - regmatch.startp[i]))
8447 == FAIL)
8448 break;
8449 }
8450 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008451 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008452 {
8453 /* return matched string */
8454 if (l != NULL)
8455 copy_tv(&li->li_tv, rettv);
8456 else
8457 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8458 (int)(regmatch.endp[0] - regmatch.startp[0]));
8459 }
8460 else if (l != NULL)
8461 rettv->vval.v_number = idx;
8462 else
8463 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008464 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008465 rettv->vval.v_number =
8466 (varnumber_T)(regmatch.startp[0] - str);
8467 else
8468 rettv->vval.v_number =
8469 (varnumber_T)(regmatch.endp[0] - str);
8470 rettv->vval.v_number += (varnumber_T)(str - expr);
8471 }
8472 }
8473 vim_regfree(regmatch.regprog);
8474 }
8475
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008476theend:
8477 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008478 /* matchstrpos() without a list: drop the second item. */
8479 listitem_remove(rettv->vval.v_list,
8480 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008481 vim_free(tofree);
8482 p_cpo = save_cpo;
8483}
8484
8485/*
8486 * "match()" function
8487 */
8488 static void
8489f_match(typval_T *argvars, typval_T *rettv)
8490{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008491 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008492}
8493
Bram Moolenaar95e51472018-07-28 16:55:56 +02008494#ifdef FEAT_SEARCH_EXTRA
8495 static int
8496matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8497{
8498 dictitem_T *di;
8499
8500 if (tv->v_type != VAR_DICT)
8501 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008502 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008503 return FAIL;
8504 }
8505
8506 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008507 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008508 (char_u *)"conceal", FALSE);
8509
8510 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8511 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008512 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008513 if (*win == NULL)
8514 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008515 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008516 return FAIL;
8517 }
8518 }
8519
8520 return OK;
8521}
8522#endif
8523
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008524/*
8525 * "matchadd()" function
8526 */
8527 static void
8528f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8529{
8530#ifdef FEAT_SEARCH_EXTRA
8531 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008532 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8533 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534 int prio = 10; /* default priority */
8535 int id = -1;
8536 int error = FALSE;
8537 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008538 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008539
8540 rettv->vval.v_number = -1;
8541
8542 if (grp == NULL || pat == NULL)
8543 return;
8544 if (argvars[2].v_type != VAR_UNKNOWN)
8545 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008546 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547 if (argvars[3].v_type != VAR_UNKNOWN)
8548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008549 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008550 if (argvars[4].v_type != VAR_UNKNOWN
8551 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8552 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008553 }
8554 }
8555 if (error == TRUE)
8556 return;
8557 if (id >= 1 && id <= 3)
8558 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008559 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560 return;
8561 }
8562
Bram Moolenaar95e51472018-07-28 16:55:56 +02008563 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008564 conceal_char);
8565#endif
8566}
8567
8568/*
8569 * "matchaddpos()" function
8570 */
8571 static void
8572f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8573{
8574#ifdef FEAT_SEARCH_EXTRA
8575 char_u buf[NUMBUFLEN];
8576 char_u *group;
8577 int prio = 10;
8578 int id = -1;
8579 int error = FALSE;
8580 list_T *l;
8581 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008582 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583
8584 rettv->vval.v_number = -1;
8585
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008586 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587 if (group == NULL)
8588 return;
8589
8590 if (argvars[1].v_type != VAR_LIST)
8591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008592 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 return;
8594 }
8595 l = argvars[1].vval.v_list;
8596 if (l == NULL)
8597 return;
8598
8599 if (argvars[2].v_type != VAR_UNKNOWN)
8600 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008601 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602 if (argvars[3].v_type != VAR_UNKNOWN)
8603 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008604 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008605
8606 if (argvars[4].v_type != VAR_UNKNOWN
8607 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8608 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008609 }
8610 }
8611 if (error == TRUE)
8612 return;
8613
8614 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8615 if (id == 1 || id == 2)
8616 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008617 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618 return;
8619 }
8620
Bram Moolenaar95e51472018-07-28 16:55:56 +02008621 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622 conceal_char);
8623#endif
8624}
8625
8626/*
8627 * "matcharg()" function
8628 */
8629 static void
8630f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8631{
8632 if (rettv_list_alloc(rettv) == OK)
8633 {
8634#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008635 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008636 matchitem_T *m;
8637
8638 if (id >= 1 && id <= 3)
8639 {
8640 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8641 {
8642 list_append_string(rettv->vval.v_list,
8643 syn_id2name(m->hlg_id), -1);
8644 list_append_string(rettv->vval.v_list, m->pattern, -1);
8645 }
8646 else
8647 {
8648 list_append_string(rettv->vval.v_list, NULL, -1);
8649 list_append_string(rettv->vval.v_list, NULL, -1);
8650 }
8651 }
8652#endif
8653 }
8654}
8655
8656/*
8657 * "matchdelete()" function
8658 */
8659 static void
8660f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8661{
8662#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008663 win_T *win = get_optional_window(argvars, 1);
8664
8665 if (win == NULL)
8666 rettv->vval.v_number = -1;
8667 else
8668 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008669 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008670#endif
8671}
8672
8673/*
8674 * "matchend()" function
8675 */
8676 static void
8677f_matchend(typval_T *argvars, typval_T *rettv)
8678{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008679 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680}
8681
8682/*
8683 * "matchlist()" function
8684 */
8685 static void
8686f_matchlist(typval_T *argvars, typval_T *rettv)
8687{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008688 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008689}
8690
8691/*
8692 * "matchstr()" function
8693 */
8694 static void
8695f_matchstr(typval_T *argvars, typval_T *rettv)
8696{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008697 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008698}
8699
8700/*
8701 * "matchstrpos()" function
8702 */
8703 static void
8704f_matchstrpos(typval_T *argvars, typval_T *rettv)
8705{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008706 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008707}
8708
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709 static void
8710max_min(typval_T *argvars, typval_T *rettv, int domax)
8711{
8712 varnumber_T n = 0;
8713 varnumber_T i;
8714 int error = FALSE;
8715
8716 if (argvars[0].v_type == VAR_LIST)
8717 {
8718 list_T *l;
8719 listitem_T *li;
8720
8721 l = argvars[0].vval.v_list;
8722 if (l != NULL)
8723 {
8724 li = l->lv_first;
8725 if (li != NULL)
8726 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008727 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728 for (;;)
8729 {
8730 li = li->li_next;
8731 if (li == NULL)
8732 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008733 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734 if (domax ? i > n : i < n)
8735 n = i;
8736 }
8737 }
8738 }
8739 }
8740 else if (argvars[0].v_type == VAR_DICT)
8741 {
8742 dict_T *d;
8743 int first = TRUE;
8744 hashitem_T *hi;
8745 int todo;
8746
8747 d = argvars[0].vval.v_dict;
8748 if (d != NULL)
8749 {
8750 todo = (int)d->dv_hashtab.ht_used;
8751 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8752 {
8753 if (!HASHITEM_EMPTY(hi))
8754 {
8755 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008756 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757 if (first)
8758 {
8759 n = i;
8760 first = FALSE;
8761 }
8762 else if (domax ? i > n : i < n)
8763 n = i;
8764 }
8765 }
8766 }
8767 }
8768 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008769 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770 rettv->vval.v_number = error ? 0 : n;
8771}
8772
8773/*
8774 * "max()" function
8775 */
8776 static void
8777f_max(typval_T *argvars, typval_T *rettv)
8778{
8779 max_min(argvars, rettv, TRUE);
8780}
8781
8782/*
8783 * "min()" function
8784 */
8785 static void
8786f_min(typval_T *argvars, typval_T *rettv)
8787{
8788 max_min(argvars, rettv, FALSE);
8789}
8790
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008791/*
8792 * Create the directory in which "dir" is located, and higher levels when
8793 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008794 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008795 */
8796 static int
8797mkdir_recurse(char_u *dir, int prot)
8798{
8799 char_u *p;
8800 char_u *updir;
8801 int r = FAIL;
8802
8803 /* Get end of directory name in "dir".
8804 * We're done when it's "/" or "c:/". */
8805 p = gettail_sep(dir);
8806 if (p <= get_past_head(dir))
8807 return OK;
8808
8809 /* If the directory exists we're done. Otherwise: create it.*/
8810 updir = vim_strnsave(dir, (int)(p - dir));
8811 if (updir == NULL)
8812 return FAIL;
8813 if (mch_isdir(updir))
8814 r = OK;
8815 else if (mkdir_recurse(updir, prot) == OK)
8816 r = vim_mkdir_emsg(updir, prot);
8817 vim_free(updir);
8818 return r;
8819}
8820
8821#ifdef vim_mkdir
8822/*
8823 * "mkdir()" function
8824 */
8825 static void
8826f_mkdir(typval_T *argvars, typval_T *rettv)
8827{
8828 char_u *dir;
8829 char_u buf[NUMBUFLEN];
8830 int prot = 0755;
8831
8832 rettv->vval.v_number = FAIL;
8833 if (check_restricted() || check_secure())
8834 return;
8835
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008836 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008837 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008838 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008840 if (*gettail(dir) == NUL)
8841 /* remove trailing slashes */
8842 *gettail_sep(dir) = NUL;
8843
8844 if (argvars[1].v_type != VAR_UNKNOWN)
8845 {
8846 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008847 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008848 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008849 if (prot == -1)
8850 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008851 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008852 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008853 {
8854 if (mch_isdir(dir))
8855 {
8856 /* With the "p" flag it's OK if the dir already exists. */
8857 rettv->vval.v_number = OK;
8858 return;
8859 }
8860 mkdir_recurse(dir, prot);
8861 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008862 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008863 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864}
8865#endif
8866
8867/*
8868 * "mode()" function
8869 */
8870 static void
8871f_mode(typval_T *argvars, typval_T *rettv)
8872{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008873 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874
Bram Moolenaar612cc382018-07-29 15:34:26 +02008875 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876
8877 if (time_for_testing == 93784)
8878 {
8879 /* Testing the two-character code. */
8880 buf[0] = 'x';
8881 buf[1] = '!';
8882 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008883#ifdef FEAT_TERMINAL
8884 else if (term_use_loop())
8885 buf[0] = 't';
8886#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008887 else if (VIsual_active)
8888 {
8889 if (VIsual_select)
8890 buf[0] = VIsual_mode + 's' - 'v';
8891 else
8892 buf[0] = VIsual_mode;
8893 }
8894 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8895 || State == CONFIRM)
8896 {
8897 buf[0] = 'r';
8898 if (State == ASKMORE)
8899 buf[1] = 'm';
8900 else if (State == CONFIRM)
8901 buf[1] = '?';
8902 }
8903 else if (State == EXTERNCMD)
8904 buf[0] = '!';
8905 else if (State & INSERT)
8906 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008907 if (State & VREPLACE_FLAG)
8908 {
8909 buf[0] = 'R';
8910 buf[1] = 'v';
8911 }
8912 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008913 {
8914 if (State & REPLACE_FLAG)
8915 buf[0] = 'R';
8916 else
8917 buf[0] = 'i';
8918#ifdef FEAT_INS_EXPAND
8919 if (ins_compl_active())
8920 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008921 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008922 buf[1] = 'x';
8923#endif
8924 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008925 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008926 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008927 {
8928 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008929 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008930 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008931 else if (exmode_active == EXMODE_NORMAL)
8932 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008933 }
8934 else
8935 {
8936 buf[0] = 'n';
8937 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008938 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008939 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008940 // to be able to detect force-linewise/blockwise/characterwise operations
8941 buf[2] = motion_force;
8942 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008943 else if (restart_edit == 'I' || restart_edit == 'R'
8944 || restart_edit == 'V')
8945 {
8946 buf[1] = 'i';
8947 buf[2] = restart_edit;
8948 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008949 }
8950
8951 /* Clear out the minor mode when the argument is not a non-zero number or
8952 * non-empty string. */
8953 if (!non_zero_arg(&argvars[0]))
8954 buf[1] = NUL;
8955
8956 rettv->vval.v_string = vim_strsave(buf);
8957 rettv->v_type = VAR_STRING;
8958}
8959
8960#if defined(FEAT_MZSCHEME) || defined(PROTO)
8961/*
8962 * "mzeval()" function
8963 */
8964 static void
8965f_mzeval(typval_T *argvars, typval_T *rettv)
8966{
8967 char_u *str;
8968 char_u buf[NUMBUFLEN];
8969
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008970 if (check_restricted() || check_secure())
8971 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008972 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008973 do_mzeval(str, rettv);
8974}
8975
8976 void
8977mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8978{
8979 typval_T argvars[3];
8980
8981 argvars[0].v_type = VAR_STRING;
8982 argvars[0].vval.v_string = name;
8983 copy_tv(args, &argvars[1]);
8984 argvars[2].v_type = VAR_UNKNOWN;
8985 f_call(argvars, rettv);
8986 clear_tv(&argvars[1]);
8987}
8988#endif
8989
8990/*
8991 * "nextnonblank()" function
8992 */
8993 static void
8994f_nextnonblank(typval_T *argvars, typval_T *rettv)
8995{
8996 linenr_T lnum;
8997
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008998 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008999 {
9000 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
9001 {
9002 lnum = 0;
9003 break;
9004 }
9005 if (*skipwhite(ml_get(lnum)) != NUL)
9006 break;
9007 }
9008 rettv->vval.v_number = lnum;
9009}
9010
9011/*
9012 * "nr2char()" function
9013 */
9014 static void
9015f_nr2char(typval_T *argvars, typval_T *rettv)
9016{
9017 char_u buf[NUMBUFLEN];
9018
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019 if (has_mbyte)
9020 {
9021 int utf8 = 0;
9022
9023 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009024 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009025 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009026 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009027 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009028 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009029 }
9030 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009031 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009032 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 buf[1] = NUL;
9034 }
9035 rettv->v_type = VAR_STRING;
9036 rettv->vval.v_string = vim_strsave(buf);
9037}
9038
9039/*
9040 * "or(expr, expr)" function
9041 */
9042 static void
9043f_or(typval_T *argvars, typval_T *rettv)
9044{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009045 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9046 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009047}
9048
9049/*
9050 * "pathshorten()" function
9051 */
9052 static void
9053f_pathshorten(typval_T *argvars, typval_T *rettv)
9054{
9055 char_u *p;
9056
9057 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009058 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009059 if (p == NULL)
9060 rettv->vval.v_string = NULL;
9061 else
9062 {
9063 p = vim_strsave(p);
9064 rettv->vval.v_string = p;
9065 if (p != NULL)
9066 shorten_dir(p);
9067 }
9068}
9069
9070#ifdef FEAT_PERL
9071/*
9072 * "perleval()" function
9073 */
9074 static void
9075f_perleval(typval_T *argvars, typval_T *rettv)
9076{
9077 char_u *str;
9078 char_u buf[NUMBUFLEN];
9079
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009080 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009081 do_perleval(str, rettv);
9082}
9083#endif
9084
9085#ifdef FEAT_FLOAT
9086/*
9087 * "pow()" function
9088 */
9089 static void
9090f_pow(typval_T *argvars, typval_T *rettv)
9091{
9092 float_T fx = 0.0, fy = 0.0;
9093
9094 rettv->v_type = VAR_FLOAT;
9095 if (get_float_arg(argvars, &fx) == OK
9096 && get_float_arg(&argvars[1], &fy) == OK)
9097 rettv->vval.v_float = pow(fx, fy);
9098 else
9099 rettv->vval.v_float = 0.0;
9100}
9101#endif
9102
9103/*
9104 * "prevnonblank()" function
9105 */
9106 static void
9107f_prevnonblank(typval_T *argvars, typval_T *rettv)
9108{
9109 linenr_T lnum;
9110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009111 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9113 lnum = 0;
9114 else
9115 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9116 --lnum;
9117 rettv->vval.v_number = lnum;
9118}
9119
9120/* This dummy va_list is here because:
9121 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9122 * - locally in the function results in a "used before set" warning
9123 * - using va_start() to initialize it gives "function with fixed args" error */
9124static va_list ap;
9125
9126/*
9127 * "printf()" function
9128 */
9129 static void
9130f_printf(typval_T *argvars, typval_T *rettv)
9131{
9132 char_u buf[NUMBUFLEN];
9133 int len;
9134 char_u *s;
9135 int saved_did_emsg = did_emsg;
9136 char *fmt;
9137
9138 rettv->v_type = VAR_STRING;
9139 rettv->vval.v_string = NULL;
9140
9141 /* Get the required length, allocate the buffer and do it for real. */
9142 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009143 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009144 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145 if (!did_emsg)
9146 {
9147 s = alloc(len + 1);
9148 if (s != NULL)
9149 {
9150 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009151 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9152 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009153 }
9154 }
9155 did_emsg |= saved_did_emsg;
9156}
9157
Bram Moolenaarf2732452018-06-03 14:47:35 +02009158#ifdef FEAT_JOB_CHANNEL
9159/*
9160 * "prompt_setcallback({buffer}, {callback})" function
9161 */
9162 static void
9163f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9164{
9165 buf_T *buf;
9166 char_u *callback;
9167 partial_T *partial;
9168
9169 if (check_secure())
9170 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009171 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009172 if (buf == NULL)
9173 return;
9174
9175 callback = get_callback(&argvars[1], &partial);
9176 if (callback == NULL)
9177 return;
9178
9179 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
9180 if (partial == NULL)
9181 buf->b_prompt_callback = vim_strsave(callback);
9182 else
9183 /* pointer into the partial */
9184 buf->b_prompt_callback = callback;
9185 buf->b_prompt_partial = partial;
9186}
9187
9188/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009189 * "prompt_setinterrupt({buffer}, {callback})" function
9190 */
9191 static void
9192f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9193{
9194 buf_T *buf;
9195 char_u *callback;
9196 partial_T *partial;
9197
9198 if (check_secure())
9199 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009200 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009201 if (buf == NULL)
9202 return;
9203
9204 callback = get_callback(&argvars[1], &partial);
9205 if (callback == NULL)
9206 return;
9207
9208 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9209 if (partial == NULL)
9210 buf->b_prompt_interrupt = vim_strsave(callback);
9211 else
9212 /* pointer into the partial */
9213 buf->b_prompt_interrupt = callback;
9214 buf->b_prompt_int_partial = partial;
9215}
9216
9217/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009218 * "prompt_setprompt({buffer}, {text})" function
9219 */
9220 static void
9221f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9222{
9223 buf_T *buf;
9224 char_u *text;
9225
9226 if (check_secure())
9227 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009228 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009229 if (buf == NULL)
9230 return;
9231
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009232 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009233 vim_free(buf->b_prompt_text);
9234 buf->b_prompt_text = vim_strsave(text);
9235}
9236#endif
9237
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009238/*
9239 * "pumvisible()" function
9240 */
9241 static void
9242f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9243{
9244#ifdef FEAT_INS_EXPAND
9245 if (pum_visible())
9246 rettv->vval.v_number = 1;
9247#endif
9248}
9249
9250#ifdef FEAT_PYTHON3
9251/*
9252 * "py3eval()" function
9253 */
9254 static void
9255f_py3eval(typval_T *argvars, typval_T *rettv)
9256{
9257 char_u *str;
9258 char_u buf[NUMBUFLEN];
9259
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009260 if (check_restricted() || check_secure())
9261 return;
9262
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009263 if (p_pyx == 0)
9264 p_pyx = 3;
9265
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009266 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009267 do_py3eval(str, rettv);
9268}
9269#endif
9270
9271#ifdef FEAT_PYTHON
9272/*
9273 * "pyeval()" function
9274 */
9275 static void
9276f_pyeval(typval_T *argvars, typval_T *rettv)
9277{
9278 char_u *str;
9279 char_u buf[NUMBUFLEN];
9280
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009281 if (check_restricted() || check_secure())
9282 return;
9283
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009284 if (p_pyx == 0)
9285 p_pyx = 2;
9286
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009287 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009288 do_pyeval(str, rettv);
9289}
9290#endif
9291
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009292#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9293/*
9294 * "pyxeval()" function
9295 */
9296 static void
9297f_pyxeval(typval_T *argvars, typval_T *rettv)
9298{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009299 if (check_restricted() || check_secure())
9300 return;
9301
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009302# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9303 init_pyxversion();
9304 if (p_pyx == 2)
9305 f_pyeval(argvars, rettv);
9306 else
9307 f_py3eval(argvars, rettv);
9308# elif defined(FEAT_PYTHON)
9309 f_pyeval(argvars, rettv);
9310# elif defined(FEAT_PYTHON3)
9311 f_py3eval(argvars, rettv);
9312# endif
9313}
9314#endif
9315
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009316/*
9317 * "range()" function
9318 */
9319 static void
9320f_range(typval_T *argvars, typval_T *rettv)
9321{
9322 varnumber_T start;
9323 varnumber_T end;
9324 varnumber_T stride = 1;
9325 varnumber_T i;
9326 int error = FALSE;
9327
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009328 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009329 if (argvars[1].v_type == VAR_UNKNOWN)
9330 {
9331 end = start - 1;
9332 start = 0;
9333 }
9334 else
9335 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009336 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009337 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009338 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009339 }
9340
9341 if (error)
9342 return; /* type error; errmsg already given */
9343 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009344 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009345 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009346 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009347 else
9348 {
9349 if (rettv_list_alloc(rettv) == OK)
9350 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9351 if (list_append_number(rettv->vval.v_list,
9352 (varnumber_T)i) == FAIL)
9353 break;
9354 }
9355}
9356
9357/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009358 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009359 */
9360 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009361readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009362{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009363 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009364 typval_T save_val;
9365 typval_T rettv;
9366 typval_T argv[2];
9367 int retval = 0;
9368 int error = FALSE;
9369
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009370 if (expr->v_type == VAR_UNKNOWN)
9371 return 1;
9372
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009373 prepare_vimvar(VV_VAL, &save_val);
9374 set_vim_var_string(VV_VAL, name, -1);
9375 argv[0].v_type = VAR_STRING;
9376 argv[0].vval.v_string = name;
9377
9378 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9379 goto theend;
9380
9381 retval = tv_get_number_chk(&rettv, &error);
9382 if (error)
9383 retval = -1;
9384 clear_tv(&rettv);
9385
9386theend:
9387 set_vim_var_string(VV_VAL, NULL, 0);
9388 restore_vimvar(VV_VAL, &save_val);
9389 return retval;
9390}
9391
9392/*
9393 * "readdir()" function
9394 */
9395 static void
9396f_readdir(typval_T *argvars, typval_T *rettv)
9397{
9398 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009399 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009400 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009401 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009402 garray_T ga;
9403 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009404
9405 if (rettv_list_alloc(rettv) == FAIL)
9406 return;
9407 path = tv_get_string(&argvars[0]);
9408 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009409
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009410 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9411 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009412 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009413 for (i = 0; i < ga.ga_len; i++)
9414 {
9415 p = ((char_u **)ga.ga_data)[i];
9416 list_append_string(rettv->vval.v_list, p, -1);
9417 }
9418 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009419 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009420}
9421
9422/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009423 * "readfile()" function
9424 */
9425 static void
9426f_readfile(typval_T *argvars, typval_T *rettv)
9427{
9428 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009429 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009430 int failed = FALSE;
9431 char_u *fname;
9432 FILE *fd;
9433 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9434 int io_size = sizeof(buf);
9435 int readlen; /* size of last fread() */
9436 char_u *prev = NULL; /* previously read bytes, if any */
9437 long prevlen = 0; /* length of data in prev */
9438 long prevsize = 0; /* size of prev buffer */
9439 long maxline = MAXLNUM;
9440 long cnt = 0;
9441 char_u *p; /* position in buf */
9442 char_u *start; /* start of current line */
9443
9444 if (argvars[1].v_type != VAR_UNKNOWN)
9445 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009446 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009447 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009448 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9449 blob = TRUE;
9450
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009451 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009452 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009453 }
9454
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009455 if (blob)
9456 {
9457 if (rettv_blob_alloc(rettv) == FAIL)
9458 return;
9459 }
9460 else
9461 {
9462 if (rettv_list_alloc(rettv) == FAIL)
9463 return;
9464 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009465
9466 /* Always open the file in binary mode, library functions have a mind of
9467 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009468 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009469 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009471 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009472 return;
9473 }
9474
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009475 if (blob)
9476 {
9477 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009479 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009480 blob_free(rettv->vval.v_blob);
9481 }
9482 fclose(fd);
9483 return;
9484 }
9485
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009486 while (cnt < maxline || maxline < 0)
9487 {
9488 readlen = (int)fread(buf, 1, io_size, fd);
9489
9490 /* This for loop processes what was read, but is also entered at end
9491 * of file so that either:
9492 * - an incomplete line gets written
9493 * - a "binary" file gets an empty line at the end if it ends in a
9494 * newline. */
9495 for (p = buf, start = buf;
9496 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9497 ++p)
9498 {
9499 if (*p == '\n' || readlen <= 0)
9500 {
9501 listitem_T *li;
9502 char_u *s = NULL;
9503 long_u len = p - start;
9504
9505 /* Finished a line. Remove CRs before NL. */
9506 if (readlen > 0 && !binary)
9507 {
9508 while (len > 0 && start[len - 1] == '\r')
9509 --len;
9510 /* removal may cross back to the "prev" string */
9511 if (len == 0)
9512 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9513 --prevlen;
9514 }
9515 if (prevlen == 0)
9516 s = vim_strnsave(start, (int)len);
9517 else
9518 {
9519 /* Change "prev" buffer to be the right size. This way
9520 * the bytes are only copied once, and very long lines are
9521 * allocated only once. */
9522 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9523 {
9524 mch_memmove(s + prevlen, start, len);
9525 s[prevlen + len] = NUL;
9526 prev = NULL; /* the list will own the string */
9527 prevlen = prevsize = 0;
9528 }
9529 }
9530 if (s == NULL)
9531 {
9532 do_outofmem_msg((long_u) prevlen + len + 1);
9533 failed = TRUE;
9534 break;
9535 }
9536
9537 if ((li = listitem_alloc()) == NULL)
9538 {
9539 vim_free(s);
9540 failed = TRUE;
9541 break;
9542 }
9543 li->li_tv.v_type = VAR_STRING;
9544 li->li_tv.v_lock = 0;
9545 li->li_tv.vval.v_string = s;
9546 list_append(rettv->vval.v_list, li);
9547
9548 start = p + 1; /* step over newline */
9549 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9550 break;
9551 }
9552 else if (*p == NUL)
9553 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009554 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9555 * when finding the BF and check the previous two bytes. */
9556 else if (*p == 0xbf && enc_utf8 && !binary)
9557 {
9558 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9559 * + 1, these may be in the "prev" string. */
9560 char_u back1 = p >= buf + 1 ? p[-1]
9561 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9562 char_u back2 = p >= buf + 2 ? p[-2]
9563 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9564 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9565
9566 if (back2 == 0xef && back1 == 0xbb)
9567 {
9568 char_u *dest = p - 2;
9569
9570 /* Usually a BOM is at the beginning of a file, and so at
9571 * the beginning of a line; then we can just step over it.
9572 */
9573 if (start == dest)
9574 start = p + 1;
9575 else
9576 {
9577 /* have to shuffle buf to close gap */
9578 int adjust_prevlen = 0;
9579
9580 if (dest < buf)
9581 {
9582 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9583 dest = buf;
9584 }
9585 if (readlen > p - buf + 1)
9586 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9587 readlen -= 3 - adjust_prevlen;
9588 prevlen -= adjust_prevlen;
9589 p = dest - 1;
9590 }
9591 }
9592 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009593 } /* for */
9594
9595 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9596 break;
9597 if (start < p)
9598 {
9599 /* There's part of a line in buf, store it in "prev". */
9600 if (p - start + prevlen >= prevsize)
9601 {
9602 /* need bigger "prev" buffer */
9603 char_u *newprev;
9604
9605 /* A common use case is ordinary text files and "prev" gets a
9606 * fragment of a line, so the first allocation is made
9607 * small, to avoid repeatedly 'allocing' large and
9608 * 'reallocing' small. */
9609 if (prevsize == 0)
9610 prevsize = (long)(p - start);
9611 else
9612 {
9613 long grow50pc = (prevsize * 3) / 2;
9614 long growmin = (long)((p - start) * 2 + prevlen);
9615 prevsize = grow50pc > growmin ? grow50pc : growmin;
9616 }
9617 newprev = prev == NULL ? alloc(prevsize)
9618 : vim_realloc(prev, prevsize);
9619 if (newprev == NULL)
9620 {
9621 do_outofmem_msg((long_u)prevsize);
9622 failed = TRUE;
9623 break;
9624 }
9625 prev = newprev;
9626 }
9627 /* Add the line part to end of "prev". */
9628 mch_memmove(prev + prevlen, start, p - start);
9629 prevlen += (long)(p - start);
9630 }
9631 } /* while */
9632
9633 /*
9634 * For a negative line count use only the lines at the end of the file,
9635 * free the rest.
9636 */
9637 if (!failed && maxline < 0)
9638 while (cnt > -maxline)
9639 {
9640 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9641 --cnt;
9642 }
9643
9644 if (failed)
9645 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009646 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009647 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009648 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009649 }
9650
9651 vim_free(prev);
9652 fclose(fd);
9653}
9654
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009655 static void
9656return_register(int regname, typval_T *rettv)
9657{
9658 char_u buf[2] = {0, 0};
9659
9660 buf[0] = (char_u)regname;
9661 rettv->v_type = VAR_STRING;
9662 rettv->vval.v_string = vim_strsave(buf);
9663}
9664
9665/*
9666 * "reg_executing()" function
9667 */
9668 static void
9669f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9670{
9671 return_register(reg_executing, rettv);
9672}
9673
9674/*
9675 * "reg_recording()" function
9676 */
9677 static void
9678f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9679{
9680 return_register(reg_recording, rettv);
9681}
9682
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009684/*
9685 * Convert a List to proftime_T.
9686 * Return FAIL when there is something wrong.
9687 */
9688 static int
9689list2proftime(typval_T *arg, proftime_T *tm)
9690{
9691 long n1, n2;
9692 int error = FALSE;
9693
9694 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9695 || arg->vval.v_list->lv_len != 2)
9696 return FAIL;
9697 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9698 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009699# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009700 tm->HighPart = n1;
9701 tm->LowPart = n2;
9702# else
9703 tm->tv_sec = n1;
9704 tm->tv_usec = n2;
9705# endif
9706 return error ? FAIL : OK;
9707}
9708#endif /* FEAT_RELTIME */
9709
9710/*
9711 * "reltime()" function
9712 */
9713 static void
9714f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9715{
9716#ifdef FEAT_RELTIME
9717 proftime_T res;
9718 proftime_T start;
9719
9720 if (argvars[0].v_type == VAR_UNKNOWN)
9721 {
9722 /* No arguments: get current time. */
9723 profile_start(&res);
9724 }
9725 else if (argvars[1].v_type == VAR_UNKNOWN)
9726 {
9727 if (list2proftime(&argvars[0], &res) == FAIL)
9728 return;
9729 profile_end(&res);
9730 }
9731 else
9732 {
9733 /* Two arguments: compute the difference. */
9734 if (list2proftime(&argvars[0], &start) == FAIL
9735 || list2proftime(&argvars[1], &res) == FAIL)
9736 return;
9737 profile_sub(&res, &start);
9738 }
9739
9740 if (rettv_list_alloc(rettv) == OK)
9741 {
9742 long n1, n2;
9743
Bram Moolenaar4f974752019-02-17 17:44:42 +01009744# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009745 n1 = res.HighPart;
9746 n2 = res.LowPart;
9747# else
9748 n1 = res.tv_sec;
9749 n2 = res.tv_usec;
9750# endif
9751 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9752 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9753 }
9754#endif
9755}
9756
9757#ifdef FEAT_FLOAT
9758/*
9759 * "reltimefloat()" function
9760 */
9761 static void
9762f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9763{
9764# ifdef FEAT_RELTIME
9765 proftime_T tm;
9766# endif
9767
9768 rettv->v_type = VAR_FLOAT;
9769 rettv->vval.v_float = 0;
9770# ifdef FEAT_RELTIME
9771 if (list2proftime(&argvars[0], &tm) == OK)
9772 rettv->vval.v_float = profile_float(&tm);
9773# endif
9774}
9775#endif
9776
9777/*
9778 * "reltimestr()" function
9779 */
9780 static void
9781f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9782{
9783#ifdef FEAT_RELTIME
9784 proftime_T tm;
9785#endif
9786
9787 rettv->v_type = VAR_STRING;
9788 rettv->vval.v_string = NULL;
9789#ifdef FEAT_RELTIME
9790 if (list2proftime(&argvars[0], &tm) == OK)
9791 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9792#endif
9793}
9794
9795#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796 static void
9797make_connection(void)
9798{
9799 if (X_DISPLAY == NULL
9800# ifdef FEAT_GUI
9801 && !gui.in_use
9802# endif
9803 )
9804 {
9805 x_force_connect = TRUE;
9806 setup_term_clip();
9807 x_force_connect = FALSE;
9808 }
9809}
9810
9811 static int
9812check_connection(void)
9813{
9814 make_connection();
9815 if (X_DISPLAY == NULL)
9816 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009817 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818 return FAIL;
9819 }
9820 return OK;
9821}
9822#endif
9823
9824#ifdef FEAT_CLIENTSERVER
9825 static void
9826remote_common(typval_T *argvars, typval_T *rettv, int expr)
9827{
9828 char_u *server_name;
9829 char_u *keys;
9830 char_u *r = NULL;
9831 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009832 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009833# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009834 HWND w;
9835# else
9836 Window w;
9837# endif
9838
9839 if (check_restricted() || check_secure())
9840 return;
9841
9842# ifdef FEAT_X11
9843 if (check_connection() == FAIL)
9844 return;
9845# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009846 if (argvars[2].v_type != VAR_UNKNOWN
9847 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009848 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009849
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009850 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009851 if (server_name == NULL)
9852 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009853 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009854# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009855 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009856# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009857 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9858 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009859# endif
9860 {
9861 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009863 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009864 vim_free(r);
9865 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009866 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009867 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009868 return;
9869 }
9870
9871 rettv->vval.v_string = r;
9872
9873 if (argvars[2].v_type != VAR_UNKNOWN)
9874 {
9875 dictitem_T v;
9876 char_u str[30];
9877 char_u *idvar;
9878
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009879 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009880 if (idvar != NULL && *idvar != NUL)
9881 {
9882 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9883 v.di_tv.v_type = VAR_STRING;
9884 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009885 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009886 vim_free(v.di_tv.vval.v_string);
9887 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009888 }
9889}
9890#endif
9891
9892/*
9893 * "remote_expr()" function
9894 */
9895 static void
9896f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9897{
9898 rettv->v_type = VAR_STRING;
9899 rettv->vval.v_string = NULL;
9900#ifdef FEAT_CLIENTSERVER
9901 remote_common(argvars, rettv, TRUE);
9902#endif
9903}
9904
9905/*
9906 * "remote_foreground()" function
9907 */
9908 static void
9909f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9910{
9911#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009912# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009913 /* On Win32 it's done in this application. */
9914 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009915 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009916
9917 if (server_name != NULL)
9918 serverForeground(server_name);
9919 }
9920# else
9921 /* Send a foreground() expression to the server. */
9922 argvars[1].v_type = VAR_STRING;
9923 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9924 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009925 rettv->v_type = VAR_STRING;
9926 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009927 remote_common(argvars, rettv, TRUE);
9928 vim_free(argvars[1].vval.v_string);
9929# endif
9930#endif
9931}
9932
9933 static void
9934f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9935{
9936#ifdef FEAT_CLIENTSERVER
9937 dictitem_T v;
9938 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009939# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009940 long_u n = 0;
9941# endif
9942 char_u *serverid;
9943
9944 if (check_restricted() || check_secure())
9945 {
9946 rettv->vval.v_number = -1;
9947 return;
9948 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009949 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009950 if (serverid == NULL)
9951 {
9952 rettv->vval.v_number = -1;
9953 return; /* type error; errmsg already given */
9954 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009955# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009956 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9957 if (n == 0)
9958 rettv->vval.v_number = -1;
9959 else
9960 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009961 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009962 rettv->vval.v_number = (s != NULL);
9963 }
9964# else
9965 if (check_connection() == FAIL)
9966 return;
9967
9968 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9969 serverStrToWin(serverid), &s);
9970# endif
9971
9972 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9973 {
9974 char_u *retvar;
9975
9976 v.di_tv.v_type = VAR_STRING;
9977 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009978 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009979 if (retvar != NULL)
9980 set_var(retvar, &v.di_tv, FALSE);
9981 vim_free(v.di_tv.vval.v_string);
9982 }
9983#else
9984 rettv->vval.v_number = -1;
9985#endif
9986}
9987
9988 static void
9989f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9990{
9991 char_u *r = NULL;
9992
9993#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009994 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009995
9996 if (serverid != NULL && !check_restricted() && !check_secure())
9997 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009998 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009999# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010000 /* The server's HWND is encoded in the 'id' parameter */
10001 long_u n = 0;
10002# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010003
10004 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010005 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010006
Bram Moolenaar4f974752019-02-17 17:44:42 +010010007# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010008 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10009 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010010 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010011 if (r == NULL)
10012# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010013 if (check_connection() == FAIL
10014 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10015 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010016# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010017 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010018 }
10019#endif
10020 rettv->v_type = VAR_STRING;
10021 rettv->vval.v_string = r;
10022}
10023
10024/*
10025 * "remote_send()" function
10026 */
10027 static void
10028f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10029{
10030 rettv->v_type = VAR_STRING;
10031 rettv->vval.v_string = NULL;
10032#ifdef FEAT_CLIENTSERVER
10033 remote_common(argvars, rettv, FALSE);
10034#endif
10035}
10036
10037/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010038 * "remote_startserver()" function
10039 */
10040 static void
10041f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10042{
10043#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010044 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010045
10046 if (server == NULL)
10047 return; /* type error; errmsg already given */
10048 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010049 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010050 else
10051 {
10052# ifdef FEAT_X11
10053 if (check_connection() == OK)
10054 serverRegisterName(X_DISPLAY, server);
10055# else
10056 serverSetName(server);
10057# endif
10058 }
10059#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010060 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010061#endif
10062}
10063
10064/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010065 * "remove()" function
10066 */
10067 static void
10068f_remove(typval_T *argvars, typval_T *rettv)
10069{
10070 list_T *l;
10071 listitem_T *item, *item2;
10072 listitem_T *li;
10073 long idx;
10074 long end;
10075 char_u *key;
10076 dict_T *d;
10077 dictitem_T *di;
10078 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010079 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010080
10081 if (argvars[0].v_type == VAR_DICT)
10082 {
10083 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010084 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010085 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010086 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010087 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010088 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010089 if (key != NULL)
10090 {
10091 di = dict_find(d, key, -1);
10092 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010093 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010094 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10095 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10096 {
10097 *rettv = di->di_tv;
10098 init_tv(&di->di_tv);
10099 dictitem_remove(d, di);
10100 }
10101 }
10102 }
10103 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010104 else if (argvars[0].v_type == VAR_BLOB)
10105 {
10106 idx = (long)tv_get_number_chk(&argvars[1], &error);
10107 if (!error)
10108 {
10109 blob_T *b = argvars[0].vval.v_blob;
10110 int len = blob_len(b);
10111 char_u *p;
10112
10113 if (idx < 0)
10114 // count from the end
10115 idx = len + idx;
10116 if (idx < 0 || idx >= len)
10117 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010118 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010119 return;
10120 }
10121 if (argvars[2].v_type == VAR_UNKNOWN)
10122 {
10123 // Remove one item, return its value.
10124 p = (char_u *)b->bv_ga.ga_data;
10125 rettv->vval.v_number = (varnumber_T) *(p + idx);
10126 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10127 --b->bv_ga.ga_len;
10128 }
10129 else
10130 {
10131 blob_T *blob;
10132
10133 // Remove range of items, return list with values.
10134 end = (long)tv_get_number_chk(&argvars[2], &error);
10135 if (error)
10136 return;
10137 if (end < 0)
10138 // count from the end
10139 end = len + end;
10140 if (end >= len || idx > end)
10141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010142 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010143 return;
10144 }
10145 blob = blob_alloc();
10146 if (blob == NULL)
10147 return;
10148 blob->bv_ga.ga_len = end - idx + 1;
10149 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10150 {
10151 vim_free(blob);
10152 return;
10153 }
10154 p = (char_u *)b->bv_ga.ga_data;
10155 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10156 (size_t)(end - idx + 1));
10157 ++blob->bv_refcount;
10158 rettv->v_type = VAR_BLOB;
10159 rettv->vval.v_blob = blob;
10160
10161 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10162 b->bv_ga.ga_len -= end - idx + 1;
10163 }
10164 }
10165 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010166 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010167 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010168 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010169 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010170 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010171 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010172 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010173 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010174 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010175 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010176 else
10177 {
10178 if (argvars[2].v_type == VAR_UNKNOWN)
10179 {
10180 /* Remove one item, return its value. */
10181 vimlist_remove(l, item, item);
10182 *rettv = item->li_tv;
10183 vim_free(item);
10184 }
10185 else
10186 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010187 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010188 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010189 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010190 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010191 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010192 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010193 else
10194 {
10195 int cnt = 0;
10196
10197 for (li = item; li != NULL; li = li->li_next)
10198 {
10199 ++cnt;
10200 if (li == item2)
10201 break;
10202 }
10203 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010204 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010205 else
10206 {
10207 vimlist_remove(l, item, item2);
10208 if (rettv_list_alloc(rettv) == OK)
10209 {
10210 l = rettv->vval.v_list;
10211 l->lv_first = item;
10212 l->lv_last = item2;
10213 item->li_prev = NULL;
10214 item2->li_next = NULL;
10215 l->lv_len = cnt;
10216 }
10217 }
10218 }
10219 }
10220 }
10221 }
10222}
10223
10224/*
10225 * "rename({from}, {to})" function
10226 */
10227 static void
10228f_rename(typval_T *argvars, typval_T *rettv)
10229{
10230 char_u buf[NUMBUFLEN];
10231
10232 if (check_restricted() || check_secure())
10233 rettv->vval.v_number = -1;
10234 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010235 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10236 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010237}
10238
10239/*
10240 * "repeat()" function
10241 */
10242 static void
10243f_repeat(typval_T *argvars, typval_T *rettv)
10244{
10245 char_u *p;
10246 int n;
10247 int slen;
10248 int len;
10249 char_u *r;
10250 int i;
10251
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010252 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010253 if (argvars[0].v_type == VAR_LIST)
10254 {
10255 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10256 while (n-- > 0)
10257 if (list_extend(rettv->vval.v_list,
10258 argvars[0].vval.v_list, NULL) == FAIL)
10259 break;
10260 }
10261 else
10262 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010263 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010264 rettv->v_type = VAR_STRING;
10265 rettv->vval.v_string = NULL;
10266
10267 slen = (int)STRLEN(p);
10268 len = slen * n;
10269 if (len <= 0)
10270 return;
10271
10272 r = alloc(len + 1);
10273 if (r != NULL)
10274 {
10275 for (i = 0; i < n; i++)
10276 mch_memmove(r + i * slen, p, (size_t)slen);
10277 r[len] = NUL;
10278 }
10279
10280 rettv->vval.v_string = r;
10281 }
10282}
10283
10284/*
10285 * "resolve()" function
10286 */
10287 static void
10288f_resolve(typval_T *argvars, typval_T *rettv)
10289{
10290 char_u *p;
10291#ifdef HAVE_READLINK
10292 char_u *buf = NULL;
10293#endif
10294
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010295 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010296#ifdef FEAT_SHORTCUT
10297 {
10298 char_u *v = NULL;
10299
Bram Moolenaardce1e892019-02-10 23:18:53 +010010300 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010301 if (v != NULL)
10302 rettv->vval.v_string = v;
10303 else
10304 rettv->vval.v_string = vim_strsave(p);
10305 }
10306#else
10307# ifdef HAVE_READLINK
10308 {
10309 char_u *cpy;
10310 int len;
10311 char_u *remain = NULL;
10312 char_u *q;
10313 int is_relative_to_current = FALSE;
10314 int has_trailing_pathsep = FALSE;
10315 int limit = 100;
10316
10317 p = vim_strsave(p);
10318
10319 if (p[0] == '.' && (vim_ispathsep(p[1])
10320 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10321 is_relative_to_current = TRUE;
10322
10323 len = STRLEN(p);
10324 if (len > 0 && after_pathsep(p, p + len))
10325 {
10326 has_trailing_pathsep = TRUE;
10327 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10328 }
10329
10330 q = getnextcomp(p);
10331 if (*q != NUL)
10332 {
10333 /* Separate the first path component in "p", and keep the
10334 * remainder (beginning with the path separator). */
10335 remain = vim_strsave(q - 1);
10336 q[-1] = NUL;
10337 }
10338
10339 buf = alloc(MAXPATHL + 1);
10340 if (buf == NULL)
10341 goto fail;
10342
10343 for (;;)
10344 {
10345 for (;;)
10346 {
10347 len = readlink((char *)p, (char *)buf, MAXPATHL);
10348 if (len <= 0)
10349 break;
10350 buf[len] = NUL;
10351
10352 if (limit-- == 0)
10353 {
10354 vim_free(p);
10355 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010356 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010357 rettv->vval.v_string = NULL;
10358 goto fail;
10359 }
10360
10361 /* Ensure that the result will have a trailing path separator
10362 * if the argument has one. */
10363 if (remain == NULL && has_trailing_pathsep)
10364 add_pathsep(buf);
10365
10366 /* Separate the first path component in the link value and
10367 * concatenate the remainders. */
10368 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10369 if (*q != NUL)
10370 {
10371 if (remain == NULL)
10372 remain = vim_strsave(q - 1);
10373 else
10374 {
10375 cpy = concat_str(q - 1, remain);
10376 if (cpy != NULL)
10377 {
10378 vim_free(remain);
10379 remain = cpy;
10380 }
10381 }
10382 q[-1] = NUL;
10383 }
10384
10385 q = gettail(p);
10386 if (q > p && *q == NUL)
10387 {
10388 /* Ignore trailing path separator. */
10389 q[-1] = NUL;
10390 q = gettail(p);
10391 }
10392 if (q > p && !mch_isFullName(buf))
10393 {
10394 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010395 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010396 if (cpy != NULL)
10397 {
10398 STRCPY(cpy, p);
10399 STRCPY(gettail(cpy), buf);
10400 vim_free(p);
10401 p = cpy;
10402 }
10403 }
10404 else
10405 {
10406 vim_free(p);
10407 p = vim_strsave(buf);
10408 }
10409 }
10410
10411 if (remain == NULL)
10412 break;
10413
10414 /* Append the first path component of "remain" to "p". */
10415 q = getnextcomp(remain + 1);
10416 len = q - remain - (*q != NUL);
10417 cpy = vim_strnsave(p, STRLEN(p) + len);
10418 if (cpy != NULL)
10419 {
10420 STRNCAT(cpy, remain, len);
10421 vim_free(p);
10422 p = cpy;
10423 }
10424 /* Shorten "remain". */
10425 if (*q != NUL)
10426 STRMOVE(remain, q - 1);
10427 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010428 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010429 }
10430
10431 /* If the result is a relative path name, make it explicitly relative to
10432 * the current directory if and only if the argument had this form. */
10433 if (!vim_ispathsep(*p))
10434 {
10435 if (is_relative_to_current
10436 && *p != NUL
10437 && !(p[0] == '.'
10438 && (p[1] == NUL
10439 || vim_ispathsep(p[1])
10440 || (p[1] == '.'
10441 && (p[2] == NUL
10442 || vim_ispathsep(p[2]))))))
10443 {
10444 /* Prepend "./". */
10445 cpy = concat_str((char_u *)"./", p);
10446 if (cpy != NULL)
10447 {
10448 vim_free(p);
10449 p = cpy;
10450 }
10451 }
10452 else if (!is_relative_to_current)
10453 {
10454 /* Strip leading "./". */
10455 q = p;
10456 while (q[0] == '.' && vim_ispathsep(q[1]))
10457 q += 2;
10458 if (q > p)
10459 STRMOVE(p, p + 2);
10460 }
10461 }
10462
10463 /* Ensure that the result will have no trailing path separator
10464 * if the argument had none. But keep "/" or "//". */
10465 if (!has_trailing_pathsep)
10466 {
10467 q = p + STRLEN(p);
10468 if (after_pathsep(p, q))
10469 *gettail_sep(p) = NUL;
10470 }
10471
10472 rettv->vval.v_string = p;
10473 }
10474# else
10475 rettv->vval.v_string = vim_strsave(p);
10476# endif
10477#endif
10478
10479 simplify_filename(rettv->vval.v_string);
10480
10481#ifdef HAVE_READLINK
10482fail:
10483 vim_free(buf);
10484#endif
10485 rettv->v_type = VAR_STRING;
10486}
10487
10488/*
10489 * "reverse({list})" function
10490 */
10491 static void
10492f_reverse(typval_T *argvars, typval_T *rettv)
10493{
10494 list_T *l;
10495 listitem_T *li, *ni;
10496
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010497 if (argvars[0].v_type == VAR_BLOB)
10498 {
10499 blob_T *b = argvars[0].vval.v_blob;
10500 int i, len = blob_len(b);
10501
10502 for (i = 0; i < len / 2; i++)
10503 {
10504 int tmp = blob_get(b, i);
10505
10506 blob_set(b, i, blob_get(b, len - i - 1));
10507 blob_set(b, len - i - 1, tmp);
10508 }
10509 rettv_blob_set(rettv, b);
10510 return;
10511 }
10512
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010513 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010514 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010515 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010516 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010517 (char_u *)N_("reverse() argument"), TRUE))
10518 {
10519 li = l->lv_last;
10520 l->lv_first = l->lv_last = NULL;
10521 l->lv_len = 0;
10522 while (li != NULL)
10523 {
10524 ni = li->li_prev;
10525 list_append(l, li);
10526 li = ni;
10527 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010528 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010529 l->lv_idx = l->lv_len - l->lv_idx - 1;
10530 }
10531}
10532
10533#define SP_NOMOVE 0x01 /* don't move cursor */
10534#define SP_REPEAT 0x02 /* repeat to find outer pair */
10535#define SP_RETCOUNT 0x04 /* return matchcount */
10536#define SP_SETPCMARK 0x08 /* set previous context mark */
10537#define SP_START 0x10 /* accept match at start position */
10538#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10539#define SP_END 0x40 /* leave cursor at end of match */
10540#define SP_COLUMN 0x80 /* start at cursor column */
10541
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010542/*
10543 * Get flags for a search function.
10544 * Possibly sets "p_ws".
10545 * Returns BACKWARD, FORWARD or zero (for an error).
10546 */
10547 static int
10548get_search_arg(typval_T *varp, int *flagsp)
10549{
10550 int dir = FORWARD;
10551 char_u *flags;
10552 char_u nbuf[NUMBUFLEN];
10553 int mask;
10554
10555 if (varp->v_type != VAR_UNKNOWN)
10556 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010557 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010558 if (flags == NULL)
10559 return 0; /* type error; errmsg already given */
10560 while (*flags != NUL)
10561 {
10562 switch (*flags)
10563 {
10564 case 'b': dir = BACKWARD; break;
10565 case 'w': p_ws = TRUE; break;
10566 case 'W': p_ws = FALSE; break;
10567 default: mask = 0;
10568 if (flagsp != NULL)
10569 switch (*flags)
10570 {
10571 case 'c': mask = SP_START; break;
10572 case 'e': mask = SP_END; break;
10573 case 'm': mask = SP_RETCOUNT; break;
10574 case 'n': mask = SP_NOMOVE; break;
10575 case 'p': mask = SP_SUBPAT; break;
10576 case 'r': mask = SP_REPEAT; break;
10577 case 's': mask = SP_SETPCMARK; break;
10578 case 'z': mask = SP_COLUMN; break;
10579 }
10580 if (mask == 0)
10581 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010582 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010583 dir = 0;
10584 }
10585 else
10586 *flagsp |= mask;
10587 }
10588 if (dir == 0)
10589 break;
10590 ++flags;
10591 }
10592 }
10593 return dir;
10594}
10595
10596/*
10597 * Shared by search() and searchpos() functions.
10598 */
10599 static int
10600search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10601{
10602 int flags;
10603 char_u *pat;
10604 pos_T pos;
10605 pos_T save_cursor;
10606 int save_p_ws = p_ws;
10607 int dir;
10608 int retval = 0; /* default: FAIL */
10609 long lnum_stop = 0;
10610 proftime_T tm;
10611#ifdef FEAT_RELTIME
10612 long time_limit = 0;
10613#endif
10614 int options = SEARCH_KEEP;
10615 int subpatnum;
10616
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010617 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010618 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10619 if (dir == 0)
10620 goto theend;
10621 flags = *flagsp;
10622 if (flags & SP_START)
10623 options |= SEARCH_START;
10624 if (flags & SP_END)
10625 options |= SEARCH_END;
10626 if (flags & SP_COLUMN)
10627 options |= SEARCH_COL;
10628
10629 /* Optional arguments: line number to stop searching and timeout. */
10630 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10631 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010632 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010633 if (lnum_stop < 0)
10634 goto theend;
10635#ifdef FEAT_RELTIME
10636 if (argvars[3].v_type != VAR_UNKNOWN)
10637 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010638 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010639 if (time_limit < 0)
10640 goto theend;
10641 }
10642#endif
10643 }
10644
10645#ifdef FEAT_RELTIME
10646 /* Set the time limit, if there is one. */
10647 profile_setlimit(time_limit, &tm);
10648#endif
10649
10650 /*
10651 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10652 * Check to make sure only those flags are set.
10653 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10654 * flags cannot be set. Check for that condition also.
10655 */
10656 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10657 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10658 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010659 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010660 goto theend;
10661 }
10662
10663 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010664 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010665 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010666 if (subpatnum != FAIL)
10667 {
10668 if (flags & SP_SUBPAT)
10669 retval = subpatnum;
10670 else
10671 retval = pos.lnum;
10672 if (flags & SP_SETPCMARK)
10673 setpcmark();
10674 curwin->w_cursor = pos;
10675 if (match_pos != NULL)
10676 {
10677 /* Store the match cursor position */
10678 match_pos->lnum = pos.lnum;
10679 match_pos->col = pos.col + 1;
10680 }
10681 /* "/$" will put the cursor after the end of the line, may need to
10682 * correct that here */
10683 check_cursor();
10684 }
10685
10686 /* If 'n' flag is used: restore cursor position. */
10687 if (flags & SP_NOMOVE)
10688 curwin->w_cursor = save_cursor;
10689 else
10690 curwin->w_set_curswant = TRUE;
10691theend:
10692 p_ws = save_p_ws;
10693
10694 return retval;
10695}
10696
10697#ifdef FEAT_FLOAT
10698
10699/*
10700 * round() is not in C90, use ceil() or floor() instead.
10701 */
10702 float_T
10703vim_round(float_T f)
10704{
10705 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10706}
10707
10708/*
10709 * "round({float})" function
10710 */
10711 static void
10712f_round(typval_T *argvars, typval_T *rettv)
10713{
10714 float_T f = 0.0;
10715
10716 rettv->v_type = VAR_FLOAT;
10717 if (get_float_arg(argvars, &f) == OK)
10718 rettv->vval.v_float = vim_round(f);
10719 else
10720 rettv->vval.v_float = 0.0;
10721}
10722#endif
10723
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010724#ifdef FEAT_RUBY
10725/*
10726 * "rubyeval()" function
10727 */
10728 static void
10729f_rubyeval(typval_T *argvars, typval_T *rettv)
10730{
10731 char_u *str;
10732 char_u buf[NUMBUFLEN];
10733
10734 str = tv_get_string_buf(&argvars[0], buf);
10735 do_rubyeval(str, rettv);
10736}
10737#endif
10738
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010739/*
10740 * "screenattr()" function
10741 */
10742 static void
10743f_screenattr(typval_T *argvars, typval_T *rettv)
10744{
10745 int row;
10746 int col;
10747 int c;
10748
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010749 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10750 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010751 if (row < 0 || row >= screen_Rows
10752 || col < 0 || col >= screen_Columns)
10753 c = -1;
10754 else
10755 c = ScreenAttrs[LineOffset[row] + col];
10756 rettv->vval.v_number = c;
10757}
10758
10759/*
10760 * "screenchar()" function
10761 */
10762 static void
10763f_screenchar(typval_T *argvars, typval_T *rettv)
10764{
10765 int row;
10766 int col;
10767 int off;
10768 int c;
10769
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010770 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10771 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010772 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010773 c = -1;
10774 else
10775 {
10776 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010777 if (enc_utf8 && ScreenLinesUC[off] != 0)
10778 c = ScreenLinesUC[off];
10779 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010780 c = ScreenLines[off];
10781 }
10782 rettv->vval.v_number = c;
10783}
10784
10785/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010786 * "screenchars()" function
10787 */
10788 static void
10789f_screenchars(typval_T *argvars, typval_T *rettv)
10790{
10791 int row;
10792 int col;
10793 int off;
10794 int c;
10795 int i;
10796
10797 if (rettv_list_alloc(rettv) == FAIL)
10798 return;
10799 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10800 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10801 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10802 return;
10803
10804 off = LineOffset[row] + col;
10805 if (enc_utf8 && ScreenLinesUC[off] != 0)
10806 c = ScreenLinesUC[off];
10807 else
10808 c = ScreenLines[off];
10809 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10810
10811 if (enc_utf8)
10812
10813 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10814 list_append_number(rettv->vval.v_list,
10815 (varnumber_T)ScreenLinesC[i][off]);
10816}
10817
10818/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010819 * "screencol()" function
10820 *
10821 * First column is 1 to be consistent with virtcol().
10822 */
10823 static void
10824f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10825{
10826 rettv->vval.v_number = screen_screencol() + 1;
10827}
10828
10829/*
10830 * "screenrow()" function
10831 */
10832 static void
10833f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10834{
10835 rettv->vval.v_number = screen_screenrow() + 1;
10836}
10837
10838/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010839 * "screenstring()" function
10840 */
10841 static void
10842f_screenstring(typval_T *argvars, typval_T *rettv)
10843{
10844 int row;
10845 int col;
10846 int off;
10847 int c;
10848 int i;
10849 char_u buf[MB_MAXBYTES + 1];
10850 int buflen = 0;
10851
10852 rettv->vval.v_string = NULL;
10853 rettv->v_type = VAR_STRING;
10854
10855 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10856 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10857 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10858 return;
10859
10860 off = LineOffset[row] + col;
10861 if (enc_utf8 && ScreenLinesUC[off] != 0)
10862 c = ScreenLinesUC[off];
10863 else
10864 c = ScreenLines[off];
10865 buflen += mb_char2bytes(c, buf);
10866
10867 if (enc_utf8)
10868 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10869 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10870
10871 buf[buflen] = NUL;
10872 rettv->vval.v_string = vim_strsave(buf);
10873}
10874
10875/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010876 * "search()" function
10877 */
10878 static void
10879f_search(typval_T *argvars, typval_T *rettv)
10880{
10881 int flags = 0;
10882
10883 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10884}
10885
10886/*
10887 * "searchdecl()" function
10888 */
10889 static void
10890f_searchdecl(typval_T *argvars, typval_T *rettv)
10891{
10892 int locally = 1;
10893 int thisblock = 0;
10894 int error = FALSE;
10895 char_u *name;
10896
10897 rettv->vval.v_number = 1; /* default: FAIL */
10898
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010899 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010900 if (argvars[1].v_type != VAR_UNKNOWN)
10901 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010902 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010903 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010904 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010905 }
10906 if (!error && name != NULL)
10907 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10908 locally, thisblock, SEARCH_KEEP) == FAIL;
10909}
10910
10911/*
10912 * Used by searchpair() and searchpairpos()
10913 */
10914 static int
10915searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10916{
10917 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010918 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010919 int save_p_ws = p_ws;
10920 int dir;
10921 int flags = 0;
10922 char_u nbuf1[NUMBUFLEN];
10923 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010924 int retval = 0; /* default: FAIL */
10925 long lnum_stop = 0;
10926 long time_limit = 0;
10927
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010928 /* Get the three pattern arguments: start, middle, end. Will result in an
10929 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010930 spat = tv_get_string_chk(&argvars[0]);
10931 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10932 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010933 if (spat == NULL || mpat == NULL || epat == NULL)
10934 goto theend; /* type error */
10935
10936 /* Handle the optional fourth argument: flags */
10937 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10938 if (dir == 0)
10939 goto theend;
10940
10941 /* Don't accept SP_END or SP_SUBPAT.
10942 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10943 */
10944 if ((flags & (SP_END | SP_SUBPAT)) != 0
10945 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10946 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010947 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010948 goto theend;
10949 }
10950
10951 /* Using 'r' implies 'W', otherwise it doesn't work. */
10952 if (flags & SP_REPEAT)
10953 p_ws = FALSE;
10954
10955 /* Optional fifth argument: skip expression */
10956 if (argvars[3].v_type == VAR_UNKNOWN
10957 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010958 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010959 else
10960 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010961 skip = &argvars[4];
10962 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10963 && skip->v_type != VAR_STRING)
10964 {
10965 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010966 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010967 goto theend;
10968 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010969 if (argvars[5].v_type != VAR_UNKNOWN)
10970 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010971 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010972 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010974 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010975 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010976 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010977#ifdef FEAT_RELTIME
10978 if (argvars[6].v_type != VAR_UNKNOWN)
10979 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010980 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010981 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010983 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010984 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010985 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010986 }
10987#endif
10988 }
10989 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010990
10991 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10992 match_pos, lnum_stop, time_limit);
10993
10994theend:
10995 p_ws = save_p_ws;
10996
10997 return retval;
10998}
10999
11000/*
11001 * "searchpair()" function
11002 */
11003 static void
11004f_searchpair(typval_T *argvars, typval_T *rettv)
11005{
11006 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11007}
11008
11009/*
11010 * "searchpairpos()" function
11011 */
11012 static void
11013f_searchpairpos(typval_T *argvars, typval_T *rettv)
11014{
11015 pos_T match_pos;
11016 int lnum = 0;
11017 int col = 0;
11018
11019 if (rettv_list_alloc(rettv) == FAIL)
11020 return;
11021
11022 if (searchpair_cmn(argvars, &match_pos) > 0)
11023 {
11024 lnum = match_pos.lnum;
11025 col = match_pos.col;
11026 }
11027
11028 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11029 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11030}
11031
11032/*
11033 * Search for a start/middle/end thing.
11034 * Used by searchpair(), see its documentation for the details.
11035 * Returns 0 or -1 for no match,
11036 */
11037 long
11038do_searchpair(
11039 char_u *spat, /* start pattern */
11040 char_u *mpat, /* middle pattern */
11041 char_u *epat, /* end pattern */
11042 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011043 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011044 int flags, /* SP_SETPCMARK and other SP_ values */
11045 pos_T *match_pos,
11046 linenr_T lnum_stop, /* stop at this line if not zero */
11047 long time_limit UNUSED) /* stop after this many msec */
11048{
11049 char_u *save_cpo;
11050 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11051 long retval = 0;
11052 pos_T pos;
11053 pos_T firstpos;
11054 pos_T foundpos;
11055 pos_T save_cursor;
11056 pos_T save_pos;
11057 int n;
11058 int r;
11059 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011060 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011061 int err;
11062 int options = SEARCH_KEEP;
11063 proftime_T tm;
11064
11065 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11066 save_cpo = p_cpo;
11067 p_cpo = empty_option;
11068
11069#ifdef FEAT_RELTIME
11070 /* Set the time limit, if there is one. */
11071 profile_setlimit(time_limit, &tm);
11072#endif
11073
11074 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11075 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011076 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11077 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011078 if (pat2 == NULL || pat3 == NULL)
11079 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011080 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011081 if (*mpat == NUL)
11082 STRCPY(pat3, pat2);
11083 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011084 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011085 spat, epat, mpat);
11086 if (flags & SP_START)
11087 options |= SEARCH_START;
11088
Bram Moolenaar48570482017-10-30 21:48:41 +010011089 if (skip != NULL)
11090 {
11091 /* Empty string means to not use the skip expression. */
11092 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11093 use_skip = skip->vval.v_string != NULL
11094 && *skip->vval.v_string != NUL;
11095 }
11096
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011097 save_cursor = curwin->w_cursor;
11098 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011099 CLEAR_POS(&firstpos);
11100 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011101 pat = pat3;
11102 for (;;)
11103 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011104 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011105 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011106 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011107 /* didn't find it or found the first match again: FAIL */
11108 break;
11109
11110 if (firstpos.lnum == 0)
11111 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011112 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011113 {
11114 /* Found the same position again. Can happen with a pattern that
11115 * has "\zs" at the end and searching backwards. Advance one
11116 * character and try again. */
11117 if (dir == BACKWARD)
11118 decl(&pos);
11119 else
11120 incl(&pos);
11121 }
11122 foundpos = pos;
11123
11124 /* clear the start flag to avoid getting stuck here */
11125 options &= ~SEARCH_START;
11126
11127 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011128 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011129 {
11130 save_pos = curwin->w_cursor;
11131 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011132 err = FALSE;
11133 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011134 curwin->w_cursor = save_pos;
11135 if (err)
11136 {
11137 /* Evaluating {skip} caused an error, break here. */
11138 curwin->w_cursor = save_cursor;
11139 retval = -1;
11140 break;
11141 }
11142 if (r)
11143 continue;
11144 }
11145
11146 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11147 {
11148 /* Found end when searching backwards or start when searching
11149 * forward: nested pair. */
11150 ++nest;
11151 pat = pat2; /* nested, don't search for middle */
11152 }
11153 else
11154 {
11155 /* Found end when searching forward or start when searching
11156 * backward: end of (nested) pair; or found middle in outer pair. */
11157 if (--nest == 1)
11158 pat = pat3; /* outer level, search for middle */
11159 }
11160
11161 if (nest == 0)
11162 {
11163 /* Found the match: return matchcount or line number. */
11164 if (flags & SP_RETCOUNT)
11165 ++retval;
11166 else
11167 retval = pos.lnum;
11168 if (flags & SP_SETPCMARK)
11169 setpcmark();
11170 curwin->w_cursor = pos;
11171 if (!(flags & SP_REPEAT))
11172 break;
11173 nest = 1; /* search for next unmatched */
11174 }
11175 }
11176
11177 if (match_pos != NULL)
11178 {
11179 /* Store the match cursor position */
11180 match_pos->lnum = curwin->w_cursor.lnum;
11181 match_pos->col = curwin->w_cursor.col + 1;
11182 }
11183
11184 /* If 'n' flag is used or search failed: restore cursor position. */
11185 if ((flags & SP_NOMOVE) || retval == 0)
11186 curwin->w_cursor = save_cursor;
11187
11188theend:
11189 vim_free(pat2);
11190 vim_free(pat3);
11191 if (p_cpo == empty_option)
11192 p_cpo = save_cpo;
11193 else
11194 /* Darn, evaluating the {skip} expression changed the value. */
11195 free_string_option(save_cpo);
11196
11197 return retval;
11198}
11199
11200/*
11201 * "searchpos()" function
11202 */
11203 static void
11204f_searchpos(typval_T *argvars, typval_T *rettv)
11205{
11206 pos_T match_pos;
11207 int lnum = 0;
11208 int col = 0;
11209 int n;
11210 int flags = 0;
11211
11212 if (rettv_list_alloc(rettv) == FAIL)
11213 return;
11214
11215 n = search_cmn(argvars, &match_pos, &flags);
11216 if (n > 0)
11217 {
11218 lnum = match_pos.lnum;
11219 col = match_pos.col;
11220 }
11221
11222 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11223 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11224 if (flags & SP_SUBPAT)
11225 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11226}
11227
11228 static void
11229f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11230{
11231#ifdef FEAT_CLIENTSERVER
11232 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011233 char_u *server = tv_get_string_chk(&argvars[0]);
11234 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011235
11236 rettv->vval.v_number = -1;
11237 if (server == NULL || reply == NULL)
11238 return;
11239 if (check_restricted() || check_secure())
11240 return;
11241# ifdef FEAT_X11
11242 if (check_connection() == FAIL)
11243 return;
11244# endif
11245
11246 if (serverSendReply(server, reply) < 0)
11247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011248 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011249 return;
11250 }
11251 rettv->vval.v_number = 0;
11252#else
11253 rettv->vval.v_number = -1;
11254#endif
11255}
11256
11257 static void
11258f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11259{
11260 char_u *r = NULL;
11261
11262#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011263# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011264 r = serverGetVimNames();
11265# else
11266 make_connection();
11267 if (X_DISPLAY != NULL)
11268 r = serverGetVimNames(X_DISPLAY);
11269# endif
11270#endif
11271 rettv->v_type = VAR_STRING;
11272 rettv->vval.v_string = r;
11273}
11274
11275/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011276 * "setbufline()" function
11277 */
11278 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011279f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011280{
11281 linenr_T lnum;
11282 buf_T *buf;
11283
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011284 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011285 if (buf == NULL)
11286 rettv->vval.v_number = 1; /* FAIL */
11287 else
11288 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011289 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011290 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011291 }
11292}
11293
11294/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011295 * "setbufvar()" function
11296 */
11297 static void
11298f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11299{
11300 buf_T *buf;
11301 char_u *varname, *bufvarname;
11302 typval_T *varp;
11303 char_u nbuf[NUMBUFLEN];
11304
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011305 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011306 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011307 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11308 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011309 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011310 varp = &argvars[2];
11311
11312 if (buf != NULL && varname != NULL && varp != NULL)
11313 {
11314 if (*varname == '&')
11315 {
11316 long numval;
11317 char_u *strval;
11318 int error = FALSE;
11319 aco_save_T aco;
11320
11321 /* set curbuf to be our buf, temporarily */
11322 aucmd_prepbuf(&aco, buf);
11323
11324 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011325 numval = (long)tv_get_number_chk(varp, &error);
11326 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011327 if (!error && strval != NULL)
11328 set_option_value(varname, numval, strval, OPT_LOCAL);
11329
11330 /* reset notion of buffer */
11331 aucmd_restbuf(&aco);
11332 }
11333 else
11334 {
11335 buf_T *save_curbuf = curbuf;
11336
Bram Moolenaar964b3742019-05-24 18:54:09 +020011337 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011338 if (bufvarname != NULL)
11339 {
11340 curbuf = buf;
11341 STRCPY(bufvarname, "b:");
11342 STRCPY(bufvarname + 2, varname);
11343 set_var(bufvarname, varp, TRUE);
11344 vim_free(bufvarname);
11345 curbuf = save_curbuf;
11346 }
11347 }
11348 }
11349}
11350
11351 static void
11352f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11353{
11354 dict_T *d;
11355 dictitem_T *di;
11356 char_u *csearch;
11357
11358 if (argvars[0].v_type != VAR_DICT)
11359 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011360 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011361 return;
11362 }
11363
11364 if ((d = argvars[0].vval.v_dict) != NULL)
11365 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011366 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011367 if (csearch != NULL)
11368 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011369 if (enc_utf8)
11370 {
11371 int pcc[MAX_MCO];
11372 int c = utfc_ptr2char(csearch, pcc);
11373
11374 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11375 }
11376 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011377 set_last_csearch(PTR2CHAR(csearch),
11378 csearch, MB_PTR2LEN(csearch));
11379 }
11380
11381 di = dict_find(d, (char_u *)"forward", -1);
11382 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011383 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011384 ? FORWARD : BACKWARD);
11385
11386 di = dict_find(d, (char_u *)"until", -1);
11387 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011388 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011389 }
11390}
11391
11392/*
11393 * "setcmdpos()" function
11394 */
11395 static void
11396f_setcmdpos(typval_T *argvars, typval_T *rettv)
11397{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011398 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011399
11400 if (pos >= 0)
11401 rettv->vval.v_number = set_cmdline_pos(pos);
11402}
11403
11404/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011405 * "setenv()" function
11406 */
11407 static void
11408f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11409{
11410 char_u namebuf[NUMBUFLEN];
11411 char_u valbuf[NUMBUFLEN];
11412 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11413
11414 if (argvars[1].v_type == VAR_SPECIAL
11415 && argvars[1].vval.v_number == VVAL_NULL)
11416 vim_unsetenv(name);
11417 else
11418 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11419}
11420
11421/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011422 * "setfperm({fname}, {mode})" function
11423 */
11424 static void
11425f_setfperm(typval_T *argvars, typval_T *rettv)
11426{
11427 char_u *fname;
11428 char_u modebuf[NUMBUFLEN];
11429 char_u *mode_str;
11430 int i;
11431 int mask;
11432 int mode = 0;
11433
11434 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011435 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011436 if (fname == NULL)
11437 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011438 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011439 if (mode_str == NULL)
11440 return;
11441 if (STRLEN(mode_str) != 9)
11442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011443 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011444 return;
11445 }
11446
11447 mask = 1;
11448 for (i = 8; i >= 0; --i)
11449 {
11450 if (mode_str[i] != '-')
11451 mode |= mask;
11452 mask = mask << 1;
11453 }
11454 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11455}
11456
11457/*
11458 * "setline()" function
11459 */
11460 static void
11461f_setline(typval_T *argvars, typval_T *rettv)
11462{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011463 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011464
Bram Moolenaarca851592018-06-06 21:04:07 +020011465 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011466}
11467
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011468/*
11469 * Used by "setqflist()" and "setloclist()" functions
11470 */
11471 static void
11472set_qf_ll_list(
11473 win_T *wp UNUSED,
11474 typval_T *list_arg UNUSED,
11475 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011476 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011477 typval_T *rettv)
11478{
11479#ifdef FEAT_QUICKFIX
11480 static char *e_invact = N_("E927: Invalid action: '%s'");
11481 char_u *act;
11482 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011483 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011484#endif
11485
11486 rettv->vval.v_number = -1;
11487
11488#ifdef FEAT_QUICKFIX
11489 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011490 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011491 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011492 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011493 else
11494 {
11495 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011496 dict_T *d = NULL;
11497 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011498
11499 if (action_arg->v_type == VAR_STRING)
11500 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011501 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011502 if (act == NULL)
11503 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011504 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11505 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011506 action = *act;
11507 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011508 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011509 }
11510 else if (action_arg->v_type == VAR_UNKNOWN)
11511 action = ' ';
11512 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011513 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011514
Bram Moolenaard823fa92016-08-12 16:29:27 +020011515 if (action_arg->v_type != VAR_UNKNOWN
11516 && what_arg->v_type != VAR_UNKNOWN)
11517 {
11518 if (what_arg->v_type == VAR_DICT)
11519 d = what_arg->vval.v_dict;
11520 else
11521 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011522 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011523 valid_dict = FALSE;
11524 }
11525 }
11526
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011527 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011528 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011529 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11530 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011531 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011532 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011533 }
11534#endif
11535}
11536
11537/*
11538 * "setloclist()" function
11539 */
11540 static void
11541f_setloclist(typval_T *argvars, typval_T *rettv)
11542{
11543 win_T *win;
11544
11545 rettv->vval.v_number = -1;
11546
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011547 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011548 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011549 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011550}
11551
11552/*
11553 * "setmatches()" function
11554 */
11555 static void
11556f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11557{
11558#ifdef FEAT_SEARCH_EXTRA
11559 list_T *l;
11560 listitem_T *li;
11561 dict_T *d;
11562 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011563 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011564
11565 rettv->vval.v_number = -1;
11566 if (argvars[0].v_type != VAR_LIST)
11567 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011568 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011569 return;
11570 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011571 if (win == NULL)
11572 return;
11573
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011574 if ((l = argvars[0].vval.v_list) != NULL)
11575 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011576 /* To some extent make sure that we are dealing with a list from
11577 * "getmatches()". */
11578 li = l->lv_first;
11579 while (li != NULL)
11580 {
11581 if (li->li_tv.v_type != VAR_DICT
11582 || (d = li->li_tv.vval.v_dict) == NULL)
11583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011584 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011585 return;
11586 }
11587 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11588 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11589 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11590 && dict_find(d, (char_u *)"priority", -1) != NULL
11591 && dict_find(d, (char_u *)"id", -1) != NULL))
11592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011593 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011594 return;
11595 }
11596 li = li->li_next;
11597 }
11598
Bram Moolenaaraff74912019-03-30 18:11:49 +010011599 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011600 li = l->lv_first;
11601 while (li != NULL)
11602 {
11603 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011604 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011605 dictitem_T *di;
11606 char_u *group;
11607 int priority;
11608 int id;
11609 char_u *conceal;
11610
11611 d = li->li_tv.vval.v_dict;
11612 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11613 {
11614 if (s == NULL)
11615 {
11616 s = list_alloc();
11617 if (s == NULL)
11618 return;
11619 }
11620
11621 /* match from matchaddpos() */
11622 for (i = 1; i < 9; i++)
11623 {
11624 sprintf((char *)buf, (char *)"pos%d", i);
11625 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11626 {
11627 if (di->di_tv.v_type != VAR_LIST)
11628 return;
11629
11630 list_append_tv(s, &di->di_tv);
11631 s->lv_refcount++;
11632 }
11633 else
11634 break;
11635 }
11636 }
11637
Bram Moolenaar8f667172018-12-14 15:38:31 +010011638 group = dict_get_string(d, (char_u *)"group", TRUE);
11639 priority = (int)dict_get_number(d, (char_u *)"priority");
11640 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011641 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011642 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011643 : NULL;
11644 if (i == 0)
11645 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011646 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011647 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011648 priority, id, NULL, conceal);
11649 }
11650 else
11651 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011652 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011653 list_unref(s);
11654 s = NULL;
11655 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011656 vim_free(group);
11657 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011658
11659 li = li->li_next;
11660 }
11661 rettv->vval.v_number = 0;
11662 }
11663#endif
11664}
11665
11666/*
11667 * "setpos()" function
11668 */
11669 static void
11670f_setpos(typval_T *argvars, typval_T *rettv)
11671{
11672 pos_T pos;
11673 int fnum;
11674 char_u *name;
11675 colnr_T curswant = -1;
11676
11677 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011678 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011679 if (name != NULL)
11680 {
11681 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11682 {
11683 if (--pos.col < 0)
11684 pos.col = 0;
11685 if (name[0] == '.' && name[1] == NUL)
11686 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011687 /* set cursor; "fnum" is ignored */
11688 curwin->w_cursor = pos;
11689 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011690 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011691 curwin->w_curswant = curswant - 1;
11692 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011693 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011694 check_cursor();
11695 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011696 }
11697 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11698 {
11699 /* set mark */
11700 if (setmark_pos(name[1], &pos, fnum) == OK)
11701 rettv->vval.v_number = 0;
11702 }
11703 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011704 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011705 }
11706 }
11707}
11708
11709/*
11710 * "setqflist()" function
11711 */
11712 static void
11713f_setqflist(typval_T *argvars, typval_T *rettv)
11714{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011715 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011716}
11717
11718/*
11719 * "setreg()" function
11720 */
11721 static void
11722f_setreg(typval_T *argvars, typval_T *rettv)
11723{
11724 int regname;
11725 char_u *strregname;
11726 char_u *stropt;
11727 char_u *strval;
11728 int append;
11729 char_u yank_type;
11730 long block_len;
11731
11732 block_len = -1;
11733 yank_type = MAUTO;
11734 append = FALSE;
11735
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011736 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011737 rettv->vval.v_number = 1; /* FAIL is default */
11738
11739 if (strregname == NULL)
11740 return; /* type error; errmsg already given */
11741 regname = *strregname;
11742 if (regname == 0 || regname == '@')
11743 regname = '"';
11744
11745 if (argvars[2].v_type != VAR_UNKNOWN)
11746 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011747 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011748 if (stropt == NULL)
11749 return; /* type error */
11750 for (; *stropt != NUL; ++stropt)
11751 switch (*stropt)
11752 {
11753 case 'a': case 'A': /* append */
11754 append = TRUE;
11755 break;
11756 case 'v': case 'c': /* character-wise selection */
11757 yank_type = MCHAR;
11758 break;
11759 case 'V': case 'l': /* line-wise selection */
11760 yank_type = MLINE;
11761 break;
11762 case 'b': case Ctrl_V: /* block-wise selection */
11763 yank_type = MBLOCK;
11764 if (VIM_ISDIGIT(stropt[1]))
11765 {
11766 ++stropt;
11767 block_len = getdigits(&stropt) - 1;
11768 --stropt;
11769 }
11770 break;
11771 }
11772 }
11773
11774 if (argvars[1].v_type == VAR_LIST)
11775 {
11776 char_u **lstval;
11777 char_u **allocval;
11778 char_u buf[NUMBUFLEN];
11779 char_u **curval;
11780 char_u **curallocval;
11781 list_T *ll = argvars[1].vval.v_list;
11782 listitem_T *li;
11783 int len;
11784
11785 /* If the list is NULL handle like an empty list. */
11786 len = ll == NULL ? 0 : ll->lv_len;
11787
11788 /* First half: use for pointers to result lines; second half: use for
11789 * pointers to allocated copies. */
11790 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11791 if (lstval == NULL)
11792 return;
11793 curval = lstval;
11794 allocval = lstval + len + 2;
11795 curallocval = allocval;
11796
11797 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11798 li = li->li_next)
11799 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011800 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011801 if (strval == NULL)
11802 goto free_lstval;
11803 if (strval == buf)
11804 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011805 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011806 * overwrite the string. */
11807 strval = vim_strsave(buf);
11808 if (strval == NULL)
11809 goto free_lstval;
11810 *curallocval++ = strval;
11811 }
11812 *curval++ = strval;
11813 }
11814 *curval++ = NULL;
11815
11816 write_reg_contents_lst(regname, lstval, -1,
11817 append, yank_type, block_len);
11818free_lstval:
11819 while (curallocval > allocval)
11820 vim_free(*--curallocval);
11821 vim_free(lstval);
11822 }
11823 else
11824 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011825 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011826 if (strval == NULL)
11827 return;
11828 write_reg_contents_ex(regname, strval, -1,
11829 append, yank_type, block_len);
11830 }
11831 rettv->vval.v_number = 0;
11832}
11833
11834/*
11835 * "settabvar()" function
11836 */
11837 static void
11838f_settabvar(typval_T *argvars, typval_T *rettv)
11839{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011840 tabpage_T *save_curtab;
11841 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011842 char_u *varname, *tabvarname;
11843 typval_T *varp;
11844
11845 rettv->vval.v_number = 0;
11846
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011847 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011848 return;
11849
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011850 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11851 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011852 varp = &argvars[2];
11853
Bram Moolenaar4033c552017-09-16 20:54:51 +020011854 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011855 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011856 save_curtab = curtab;
11857 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011858
Bram Moolenaar964b3742019-05-24 18:54:09 +020011859 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011860 if (tabvarname != NULL)
11861 {
11862 STRCPY(tabvarname, "t:");
11863 STRCPY(tabvarname + 2, varname);
11864 set_var(tabvarname, varp, TRUE);
11865 vim_free(tabvarname);
11866 }
11867
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011868 /* Restore current tabpage */
11869 if (valid_tabpage(save_curtab))
11870 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011871 }
11872}
11873
11874/*
11875 * "settabwinvar()" function
11876 */
11877 static void
11878f_settabwinvar(typval_T *argvars, typval_T *rettv)
11879{
11880 setwinvar(argvars, rettv, 1);
11881}
11882
11883/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011884 * "settagstack()" function
11885 */
11886 static void
11887f_settagstack(typval_T *argvars, typval_T *rettv)
11888{
11889 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11890 win_T *wp;
11891 dict_T *d;
11892 int action = 'r';
11893
11894 rettv->vval.v_number = -1;
11895
11896 // first argument: window number or id
11897 wp = find_win_by_nr_or_id(&argvars[0]);
11898 if (wp == NULL)
11899 return;
11900
11901 // second argument: dict with items to set in the tag stack
11902 if (argvars[1].v_type != VAR_DICT)
11903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011904 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011905 return;
11906 }
11907 d = argvars[1].vval.v_dict;
11908 if (d == NULL)
11909 return;
11910
11911 // third argument: action - 'a' for append and 'r' for replace.
11912 // default is to replace the stack.
11913 if (argvars[2].v_type == VAR_UNKNOWN)
11914 action = 'r';
11915 else if (argvars[2].v_type == VAR_STRING)
11916 {
11917 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011918 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011919 if (actstr == NULL)
11920 return;
11921 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11922 action = *actstr;
11923 else
11924 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011925 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011926 return;
11927 }
11928 }
11929 else
11930 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011931 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011932 return;
11933 }
11934
11935 if (set_tagstack(wp, d, action) == OK)
11936 rettv->vval.v_number = 0;
11937}
11938
11939/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011940 * "setwinvar()" function
11941 */
11942 static void
11943f_setwinvar(typval_T *argvars, typval_T *rettv)
11944{
11945 setwinvar(argvars, rettv, 0);
11946}
11947
11948#ifdef FEAT_CRYPT
11949/*
11950 * "sha256({string})" function
11951 */
11952 static void
11953f_sha256(typval_T *argvars, typval_T *rettv)
11954{
11955 char_u *p;
11956
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011957 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011958 rettv->vval.v_string = vim_strsave(
11959 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11960 rettv->v_type = VAR_STRING;
11961}
11962#endif /* FEAT_CRYPT */
11963
11964/*
11965 * "shellescape({string})" function
11966 */
11967 static void
11968f_shellescape(typval_T *argvars, typval_T *rettv)
11969{
Bram Moolenaar20615522017-06-05 18:46:26 +020011970 int do_special = non_zero_arg(&argvars[1]);
11971
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011972 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011973 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011974 rettv->v_type = VAR_STRING;
11975}
11976
11977/*
11978 * shiftwidth() function
11979 */
11980 static void
11981f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11982{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011983 rettv->vval.v_number = 0;
11984
11985 if (argvars[0].v_type != VAR_UNKNOWN)
11986 {
11987 long col;
11988
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011989 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011990 if (col < 0)
11991 return; // type error; errmsg already given
11992#ifdef FEAT_VARTABS
11993 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11994 return;
11995#endif
11996 }
11997
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011998 rettv->vval.v_number = get_sw_value(curbuf);
11999}
12000
Bram Moolenaar162b7142018-12-21 15:17:36 +010012001#ifdef FEAT_SIGNS
12002/*
12003 * "sign_define()" function
12004 */
12005 static void
12006f_sign_define(typval_T *argvars, typval_T *rettv)
12007{
12008 char_u *name;
12009 dict_T *dict;
12010 char_u *icon = NULL;
12011 char_u *linehl = NULL;
12012 char_u *text = NULL;
12013 char_u *texthl = NULL;
12014
12015 rettv->vval.v_number = -1;
12016
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012017 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012018 if (name == NULL)
12019 return;
12020
12021 if (argvars[1].v_type != VAR_UNKNOWN)
12022 {
12023 if (argvars[1].v_type != VAR_DICT)
12024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012025 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012026 return;
12027 }
12028
12029 // sign attributes
12030 dict = argvars[1].vval.v_dict;
12031 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12032 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12033 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12034 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12035 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12036 text = dict_get_string(dict, (char_u *)"text", TRUE);
12037 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12038 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12039 }
12040
12041 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12042 rettv->vval.v_number = 0;
12043
12044 vim_free(icon);
12045 vim_free(linehl);
12046 vim_free(text);
12047 vim_free(texthl);
12048}
12049
12050/*
12051 * "sign_getdefined()" function
12052 */
12053 static void
12054f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12055{
12056 char_u *name = NULL;
12057
12058 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12059 return;
12060
12061 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012062 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012063
12064 sign_getlist(name, rettv->vval.v_list);
12065}
12066
12067/*
12068 * "sign_getplaced()" function
12069 */
12070 static void
12071f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12072{
12073 buf_T *buf = NULL;
12074 dict_T *dict;
12075 dictitem_T *di;
12076 linenr_T lnum = 0;
12077 int sign_id = 0;
12078 char_u *group = NULL;
12079 int notanum = FALSE;
12080
12081 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12082 return;
12083
12084 if (argvars[0].v_type != VAR_UNKNOWN)
12085 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012086 // get signs placed in the specified buffer
12087 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012088 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012089 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012090
12091 if (argvars[1].v_type != VAR_UNKNOWN)
12092 {
12093 if (argvars[1].v_type != VAR_DICT ||
12094 ((dict = argvars[1].vval.v_dict) == NULL))
12095 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012096 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012097 return;
12098 }
12099 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12100 {
12101 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012102 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012103 if (notanum)
12104 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012105 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012106 }
12107 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12108 {
12109 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012110 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012111 if (notanum)
12112 return;
12113 }
12114 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12115 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012116 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012117 if (group == NULL)
12118 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012119 if (*group == '\0') // empty string means global group
12120 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012121 }
12122 }
12123 }
12124
12125 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12126}
12127
12128/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012129 * "sign_jump()" function
12130 */
12131 static void
12132f_sign_jump(typval_T *argvars, typval_T *rettv)
12133{
12134 int sign_id;
12135 char_u *sign_group = NULL;
12136 buf_T *buf;
12137 int notanum = FALSE;
12138
12139 rettv->vval.v_number = -1;
12140
Bram Moolenaarbdace832019-03-02 10:13:42 +010012141 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012142 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12143 if (notanum)
12144 return;
12145 if (sign_id <= 0)
12146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012147 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012148 return;
12149 }
12150
12151 // Sign group
12152 sign_group = tv_get_string_chk(&argvars[1]);
12153 if (sign_group == NULL)
12154 return;
12155 if (sign_group[0] == '\0')
12156 sign_group = NULL; // global sign group
12157 else
12158 {
12159 sign_group = vim_strsave(sign_group);
12160 if (sign_group == NULL)
12161 return;
12162 }
12163
12164 // Buffer to place the sign
12165 buf = get_buf_arg(&argvars[2]);
12166 if (buf == NULL)
12167 goto cleanup;
12168
12169 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12170
12171cleanup:
12172 vim_free(sign_group);
12173}
12174
12175/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012176 * "sign_place()" function
12177 */
12178 static void
12179f_sign_place(typval_T *argvars, typval_T *rettv)
12180{
12181 int sign_id;
12182 char_u *group = NULL;
12183 char_u *sign_name;
12184 buf_T *buf;
12185 dict_T *dict;
12186 dictitem_T *di;
12187 linenr_T lnum = 0;
12188 int prio = SIGN_DEF_PRIO;
12189 int notanum = FALSE;
12190
12191 rettv->vval.v_number = -1;
12192
Bram Moolenaarbdace832019-03-02 10:13:42 +010012193 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012194 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012195 if (notanum)
12196 return;
12197 if (sign_id < 0)
12198 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012199 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012200 return;
12201 }
12202
12203 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012204 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012205 if (group == NULL)
12206 return;
12207 if (group[0] == '\0')
12208 group = NULL; // global sign group
12209 else
12210 {
12211 group = vim_strsave(group);
12212 if (group == NULL)
12213 return;
12214 }
12215
12216 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012217 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012218 if (sign_name == NULL)
12219 goto cleanup;
12220
12221 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012222 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012223 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012224 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012225
12226 if (argvars[4].v_type != VAR_UNKNOWN)
12227 {
12228 if (argvars[4].v_type != VAR_DICT ||
12229 ((dict = argvars[4].vval.v_dict) == NULL))
12230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012231 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012232 goto cleanup;
12233 }
12234
12235 // Line number where the sign is to be placed
12236 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12237 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012238 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012239 if (notanum)
12240 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012241 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012242 }
12243 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12244 {
12245 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012246 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012247 if (notanum)
12248 goto cleanup;
12249 }
12250 }
12251
12252 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12253 rettv->vval.v_number = sign_id;
12254
12255cleanup:
12256 vim_free(group);
12257}
12258
12259/*
12260 * "sign_undefine()" function
12261 */
12262 static void
12263f_sign_undefine(typval_T *argvars, typval_T *rettv)
12264{
12265 char_u *name;
12266
12267 rettv->vval.v_number = -1;
12268
12269 if (argvars[0].v_type == VAR_UNKNOWN)
12270 {
12271 // Free all the signs
12272 free_signs();
12273 rettv->vval.v_number = 0;
12274 }
12275 else
12276 {
12277 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012278 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012279 if (name == NULL)
12280 return;
12281
12282 if (sign_undefine_by_name(name) == OK)
12283 rettv->vval.v_number = 0;
12284 }
12285}
12286
12287/*
12288 * "sign_unplace()" function
12289 */
12290 static void
12291f_sign_unplace(typval_T *argvars, typval_T *rettv)
12292{
12293 dict_T *dict;
12294 dictitem_T *di;
12295 int sign_id = 0;
12296 buf_T *buf = NULL;
12297 char_u *group = NULL;
12298
12299 rettv->vval.v_number = -1;
12300
12301 if (argvars[0].v_type != VAR_STRING)
12302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012303 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012304 return;
12305 }
12306
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012307 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012308 if (group[0] == '\0')
12309 group = NULL; // global sign group
12310 else
12311 {
12312 group = vim_strsave(group);
12313 if (group == NULL)
12314 return;
12315 }
12316
12317 if (argvars[1].v_type != VAR_UNKNOWN)
12318 {
12319 if (argvars[1].v_type != VAR_DICT)
12320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012321 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012322 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012323 }
12324 dict = argvars[1].vval.v_dict;
12325
12326 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12327 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012328 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012329 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012330 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012331 }
12332 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12333 sign_id = dict_get_number(dict, (char_u *)"id");
12334 }
12335
12336 if (buf == NULL)
12337 {
12338 // Delete the sign in all the buffers
12339 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012340 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012341 rettv->vval.v_number = 0;
12342 }
12343 else
12344 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012345 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012346 rettv->vval.v_number = 0;
12347 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012348
12349cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012350 vim_free(group);
12351}
12352#endif
12353
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012354/*
12355 * "simplify()" function
12356 */
12357 static void
12358f_simplify(typval_T *argvars, typval_T *rettv)
12359{
12360 char_u *p;
12361
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012362 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012363 rettv->vval.v_string = vim_strsave(p);
12364 simplify_filename(rettv->vval.v_string); /* simplify in place */
12365 rettv->v_type = VAR_STRING;
12366}
12367
12368#ifdef FEAT_FLOAT
12369/*
12370 * "sin()" function
12371 */
12372 static void
12373f_sin(typval_T *argvars, typval_T *rettv)
12374{
12375 float_T f = 0.0;
12376
12377 rettv->v_type = VAR_FLOAT;
12378 if (get_float_arg(argvars, &f) == OK)
12379 rettv->vval.v_float = sin(f);
12380 else
12381 rettv->vval.v_float = 0.0;
12382}
12383
12384/*
12385 * "sinh()" function
12386 */
12387 static void
12388f_sinh(typval_T *argvars, typval_T *rettv)
12389{
12390 float_T f = 0.0;
12391
12392 rettv->v_type = VAR_FLOAT;
12393 if (get_float_arg(argvars, &f) == OK)
12394 rettv->vval.v_float = sinh(f);
12395 else
12396 rettv->vval.v_float = 0.0;
12397}
12398#endif
12399
Bram Moolenaareae1b912019-05-09 15:12:55 +020012400static int item_compare(const void *s1, const void *s2);
12401static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012402
12403/* struct used in the array that's given to qsort() */
12404typedef struct
12405{
12406 listitem_T *item;
12407 int idx;
12408} sortItem_T;
12409
12410/* struct storing information about current sort */
12411typedef struct
12412{
12413 int item_compare_ic;
12414 int item_compare_numeric;
12415 int item_compare_numbers;
12416#ifdef FEAT_FLOAT
12417 int item_compare_float;
12418#endif
12419 char_u *item_compare_func;
12420 partial_T *item_compare_partial;
12421 dict_T *item_compare_selfdict;
12422 int item_compare_func_err;
12423 int item_compare_keep_zero;
12424} sortinfo_T;
12425static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012426#define ITEM_COMPARE_FAIL 999
12427
12428/*
12429 * Compare functions for f_sort() and f_uniq() below.
12430 */
12431 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012432item_compare(const void *s1, const void *s2)
12433{
12434 sortItem_T *si1, *si2;
12435 typval_T *tv1, *tv2;
12436 char_u *p1, *p2;
12437 char_u *tofree1 = NULL, *tofree2 = NULL;
12438 int res;
12439 char_u numbuf1[NUMBUFLEN];
12440 char_u numbuf2[NUMBUFLEN];
12441
12442 si1 = (sortItem_T *)s1;
12443 si2 = (sortItem_T *)s2;
12444 tv1 = &si1->item->li_tv;
12445 tv2 = &si2->item->li_tv;
12446
12447 if (sortinfo->item_compare_numbers)
12448 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012449 varnumber_T v1 = tv_get_number(tv1);
12450 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012451
12452 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12453 }
12454
12455#ifdef FEAT_FLOAT
12456 if (sortinfo->item_compare_float)
12457 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012458 float_T v1 = tv_get_float(tv1);
12459 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012460
12461 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12462 }
12463#endif
12464
12465 /* tv2string() puts quotes around a string and allocates memory. Don't do
12466 * that for string variables. Use a single quote when comparing with a
12467 * non-string to do what the docs promise. */
12468 if (tv1->v_type == VAR_STRING)
12469 {
12470 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12471 p1 = (char_u *)"'";
12472 else
12473 p1 = tv1->vval.v_string;
12474 }
12475 else
12476 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12477 if (tv2->v_type == VAR_STRING)
12478 {
12479 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12480 p2 = (char_u *)"'";
12481 else
12482 p2 = tv2->vval.v_string;
12483 }
12484 else
12485 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12486 if (p1 == NULL)
12487 p1 = (char_u *)"";
12488 if (p2 == NULL)
12489 p2 = (char_u *)"";
12490 if (!sortinfo->item_compare_numeric)
12491 {
12492 if (sortinfo->item_compare_ic)
12493 res = STRICMP(p1, p2);
12494 else
12495 res = STRCMP(p1, p2);
12496 }
12497 else
12498 {
12499 double n1, n2;
12500 n1 = strtod((char *)p1, (char **)&p1);
12501 n2 = strtod((char *)p2, (char **)&p2);
12502 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12503 }
12504
12505 /* When the result would be zero, compare the item indexes. Makes the
12506 * sort stable. */
12507 if (res == 0 && !sortinfo->item_compare_keep_zero)
12508 res = si1->idx > si2->idx ? 1 : -1;
12509
12510 vim_free(tofree1);
12511 vim_free(tofree2);
12512 return res;
12513}
12514
12515 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012516item_compare2(const void *s1, const void *s2)
12517{
12518 sortItem_T *si1, *si2;
12519 int res;
12520 typval_T rettv;
12521 typval_T argv[3];
12522 int dummy;
12523 char_u *func_name;
12524 partial_T *partial = sortinfo->item_compare_partial;
12525
12526 /* shortcut after failure in previous call; compare all items equal */
12527 if (sortinfo->item_compare_func_err)
12528 return 0;
12529
12530 si1 = (sortItem_T *)s1;
12531 si2 = (sortItem_T *)s2;
12532
12533 if (partial == NULL)
12534 func_name = sortinfo->item_compare_func;
12535 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012536 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012537
12538 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12539 * in the copy without changing the original list items. */
12540 copy_tv(&si1->item->li_tv, &argv[0]);
12541 copy_tv(&si2->item->li_tv, &argv[1]);
12542
12543 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012544 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012545 partial, sortinfo->item_compare_selfdict);
12546 clear_tv(&argv[0]);
12547 clear_tv(&argv[1]);
12548
12549 if (res == FAIL)
12550 res = ITEM_COMPARE_FAIL;
12551 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012552 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012553 if (sortinfo->item_compare_func_err)
12554 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12555 clear_tv(&rettv);
12556
12557 /* When the result would be zero, compare the pointers themselves. Makes
12558 * the sort stable. */
12559 if (res == 0 && !sortinfo->item_compare_keep_zero)
12560 res = si1->idx > si2->idx ? 1 : -1;
12561
12562 return res;
12563}
12564
12565/*
12566 * "sort({list})" function
12567 */
12568 static void
12569do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12570{
12571 list_T *l;
12572 listitem_T *li;
12573 sortItem_T *ptrs;
12574 sortinfo_T *old_sortinfo;
12575 sortinfo_T info;
12576 long len;
12577 long i;
12578
12579 /* Pointer to current info struct used in compare function. Save and
12580 * restore the current one for nested calls. */
12581 old_sortinfo = sortinfo;
12582 sortinfo = &info;
12583
12584 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012585 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012586 else
12587 {
12588 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012589 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012590 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12591 TRUE))
12592 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012593 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012594
12595 len = list_len(l);
12596 if (len <= 1)
12597 goto theend; /* short list sorts pretty quickly */
12598
12599 info.item_compare_ic = FALSE;
12600 info.item_compare_numeric = FALSE;
12601 info.item_compare_numbers = FALSE;
12602#ifdef FEAT_FLOAT
12603 info.item_compare_float = FALSE;
12604#endif
12605 info.item_compare_func = NULL;
12606 info.item_compare_partial = NULL;
12607 info.item_compare_selfdict = NULL;
12608 if (argvars[1].v_type != VAR_UNKNOWN)
12609 {
12610 /* optional second argument: {func} */
12611 if (argvars[1].v_type == VAR_FUNC)
12612 info.item_compare_func = argvars[1].vval.v_string;
12613 else if (argvars[1].v_type == VAR_PARTIAL)
12614 info.item_compare_partial = argvars[1].vval.v_partial;
12615 else
12616 {
12617 int error = FALSE;
12618
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012619 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012620 if (error)
12621 goto theend; /* type error; errmsg already given */
12622 if (i == 1)
12623 info.item_compare_ic = TRUE;
12624 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012625 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012626 else if (i != 0)
12627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012628 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012629 goto theend;
12630 }
12631 if (info.item_compare_func != NULL)
12632 {
12633 if (*info.item_compare_func == NUL)
12634 {
12635 /* empty string means default sort */
12636 info.item_compare_func = NULL;
12637 }
12638 else if (STRCMP(info.item_compare_func, "n") == 0)
12639 {
12640 info.item_compare_func = NULL;
12641 info.item_compare_numeric = TRUE;
12642 }
12643 else if (STRCMP(info.item_compare_func, "N") == 0)
12644 {
12645 info.item_compare_func = NULL;
12646 info.item_compare_numbers = TRUE;
12647 }
12648#ifdef FEAT_FLOAT
12649 else if (STRCMP(info.item_compare_func, "f") == 0)
12650 {
12651 info.item_compare_func = NULL;
12652 info.item_compare_float = TRUE;
12653 }
12654#endif
12655 else if (STRCMP(info.item_compare_func, "i") == 0)
12656 {
12657 info.item_compare_func = NULL;
12658 info.item_compare_ic = TRUE;
12659 }
12660 }
12661 }
12662
12663 if (argvars[2].v_type != VAR_UNKNOWN)
12664 {
12665 /* optional third argument: {dict} */
12666 if (argvars[2].v_type != VAR_DICT)
12667 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012668 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012669 goto theend;
12670 }
12671 info.item_compare_selfdict = argvars[2].vval.v_dict;
12672 }
12673 }
12674
12675 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar51e14382019-05-25 20:21:28 +020012676 ptrs = (sortItem_T *)alloc(len * sizeof(sortItem_T));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012677 if (ptrs == NULL)
12678 goto theend;
12679
12680 i = 0;
12681 if (sort)
12682 {
12683 /* sort(): ptrs will be the list to sort */
12684 for (li = l->lv_first; li != NULL; li = li->li_next)
12685 {
12686 ptrs[i].item = li;
12687 ptrs[i].idx = i;
12688 ++i;
12689 }
12690
12691 info.item_compare_func_err = FALSE;
12692 info.item_compare_keep_zero = FALSE;
12693 /* test the compare function */
12694 if ((info.item_compare_func != NULL
12695 || info.item_compare_partial != NULL)
12696 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12697 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012698 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012699 else
12700 {
12701 /* Sort the array with item pointers. */
12702 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12703 info.item_compare_func == NULL
12704 && info.item_compare_partial == NULL
12705 ? item_compare : item_compare2);
12706
12707 if (!info.item_compare_func_err)
12708 {
12709 /* Clear the List and append the items in sorted order. */
12710 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12711 l->lv_len = 0;
12712 for (i = 0; i < len; ++i)
12713 list_append(l, ptrs[i].item);
12714 }
12715 }
12716 }
12717 else
12718 {
12719 int (*item_compare_func_ptr)(const void *, const void *);
12720
12721 /* f_uniq(): ptrs will be a stack of items to remove */
12722 info.item_compare_func_err = FALSE;
12723 info.item_compare_keep_zero = TRUE;
12724 item_compare_func_ptr = info.item_compare_func != NULL
12725 || info.item_compare_partial != NULL
12726 ? item_compare2 : item_compare;
12727
12728 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12729 li = li->li_next)
12730 {
12731 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12732 == 0)
12733 ptrs[i++].item = li;
12734 if (info.item_compare_func_err)
12735 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012736 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012737 break;
12738 }
12739 }
12740
12741 if (!info.item_compare_func_err)
12742 {
12743 while (--i >= 0)
12744 {
12745 li = ptrs[i].item->li_next;
12746 ptrs[i].item->li_next = li->li_next;
12747 if (li->li_next != NULL)
12748 li->li_next->li_prev = ptrs[i].item;
12749 else
12750 l->lv_last = ptrs[i].item;
12751 list_fix_watch(l, li);
12752 listitem_free(li);
12753 l->lv_len--;
12754 }
12755 }
12756 }
12757
12758 vim_free(ptrs);
12759 }
12760theend:
12761 sortinfo = old_sortinfo;
12762}
12763
12764/*
12765 * "sort({list})" function
12766 */
12767 static void
12768f_sort(typval_T *argvars, typval_T *rettv)
12769{
12770 do_sort_uniq(argvars, rettv, TRUE);
12771}
12772
12773/*
12774 * "uniq({list})" function
12775 */
12776 static void
12777f_uniq(typval_T *argvars, typval_T *rettv)
12778{
12779 do_sort_uniq(argvars, rettv, FALSE);
12780}
12781
12782/*
12783 * "soundfold({word})" function
12784 */
12785 static void
12786f_soundfold(typval_T *argvars, typval_T *rettv)
12787{
12788 char_u *s;
12789
12790 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012791 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012792#ifdef FEAT_SPELL
12793 rettv->vval.v_string = eval_soundfold(s);
12794#else
12795 rettv->vval.v_string = vim_strsave(s);
12796#endif
12797}
12798
12799/*
12800 * "spellbadword()" function
12801 */
12802 static void
12803f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12804{
12805 char_u *word = (char_u *)"";
12806 hlf_T attr = HLF_COUNT;
12807 int len = 0;
12808
12809 if (rettv_list_alloc(rettv) == FAIL)
12810 return;
12811
12812#ifdef FEAT_SPELL
12813 if (argvars[0].v_type == VAR_UNKNOWN)
12814 {
12815 /* Find the start and length of the badly spelled word. */
12816 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12817 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012818 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012819 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012820 curwin->w_set_curswant = TRUE;
12821 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012822 }
12823 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12824 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012825 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012826 int capcol = -1;
12827
12828 if (str != NULL)
12829 {
12830 /* Check the argument for spelling. */
12831 while (*str != NUL)
12832 {
12833 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12834 if (attr != HLF_COUNT)
12835 {
12836 word = str;
12837 break;
12838 }
12839 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012840 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012841 }
12842 }
12843 }
12844#endif
12845
12846 list_append_string(rettv->vval.v_list, word, len);
12847 list_append_string(rettv->vval.v_list, (char_u *)(
12848 attr == HLF_SPB ? "bad" :
12849 attr == HLF_SPR ? "rare" :
12850 attr == HLF_SPL ? "local" :
12851 attr == HLF_SPC ? "caps" :
12852 ""), -1);
12853}
12854
12855/*
12856 * "spellsuggest()" function
12857 */
12858 static void
12859f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12860{
12861#ifdef FEAT_SPELL
12862 char_u *str;
12863 int typeerr = FALSE;
12864 int maxcount;
12865 garray_T ga;
12866 int i;
12867 listitem_T *li;
12868 int need_capital = FALSE;
12869#endif
12870
12871 if (rettv_list_alloc(rettv) == FAIL)
12872 return;
12873
12874#ifdef FEAT_SPELL
12875 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12876 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012877 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012878 if (argvars[1].v_type != VAR_UNKNOWN)
12879 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012880 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012881 if (maxcount <= 0)
12882 return;
12883 if (argvars[2].v_type != VAR_UNKNOWN)
12884 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012885 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012886 if (typeerr)
12887 return;
12888 }
12889 }
12890 else
12891 maxcount = 25;
12892
12893 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12894
12895 for (i = 0; i < ga.ga_len; ++i)
12896 {
12897 str = ((char_u **)ga.ga_data)[i];
12898
12899 li = listitem_alloc();
12900 if (li == NULL)
12901 vim_free(str);
12902 else
12903 {
12904 li->li_tv.v_type = VAR_STRING;
12905 li->li_tv.v_lock = 0;
12906 li->li_tv.vval.v_string = str;
12907 list_append(rettv->vval.v_list, li);
12908 }
12909 }
12910 ga_clear(&ga);
12911 }
12912#endif
12913}
12914
12915 static void
12916f_split(typval_T *argvars, typval_T *rettv)
12917{
12918 char_u *str;
12919 char_u *end;
12920 char_u *pat = NULL;
12921 regmatch_T regmatch;
12922 char_u patbuf[NUMBUFLEN];
12923 char_u *save_cpo;
12924 int match;
12925 colnr_T col = 0;
12926 int keepempty = FALSE;
12927 int typeerr = FALSE;
12928
12929 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12930 save_cpo = p_cpo;
12931 p_cpo = (char_u *)"";
12932
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012933 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012934 if (argvars[1].v_type != VAR_UNKNOWN)
12935 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012936 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012937 if (pat == NULL)
12938 typeerr = TRUE;
12939 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012940 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012941 }
12942 if (pat == NULL || *pat == NUL)
12943 pat = (char_u *)"[\\x01- ]\\+";
12944
12945 if (rettv_list_alloc(rettv) == FAIL)
12946 return;
12947 if (typeerr)
12948 return;
12949
12950 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12951 if (regmatch.regprog != NULL)
12952 {
12953 regmatch.rm_ic = FALSE;
12954 while (*str != NUL || keepempty)
12955 {
12956 if (*str == NUL)
12957 match = FALSE; /* empty item at the end */
12958 else
12959 match = vim_regexec_nl(&regmatch, str, col);
12960 if (match)
12961 end = regmatch.startp[0];
12962 else
12963 end = str + STRLEN(str);
12964 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12965 && *str != NUL && match && end < regmatch.endp[0]))
12966 {
12967 if (list_append_string(rettv->vval.v_list, str,
12968 (int)(end - str)) == FAIL)
12969 break;
12970 }
12971 if (!match)
12972 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012973 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012974 if (regmatch.endp[0] > str)
12975 col = 0;
12976 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012977 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012978 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012979 str = regmatch.endp[0];
12980 }
12981
12982 vim_regfree(regmatch.regprog);
12983 }
12984
12985 p_cpo = save_cpo;
12986}
12987
12988#ifdef FEAT_FLOAT
12989/*
12990 * "sqrt()" function
12991 */
12992 static void
12993f_sqrt(typval_T *argvars, typval_T *rettv)
12994{
12995 float_T f = 0.0;
12996
12997 rettv->v_type = VAR_FLOAT;
12998 if (get_float_arg(argvars, &f) == OK)
12999 rettv->vval.v_float = sqrt(f);
13000 else
13001 rettv->vval.v_float = 0.0;
13002}
13003
13004/*
13005 * "str2float()" function
13006 */
13007 static void
13008f_str2float(typval_T *argvars, typval_T *rettv)
13009{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013010 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013011 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013012
Bram Moolenaar08243d22017-01-10 16:12:29 +010013013 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013014 p = skipwhite(p + 1);
13015 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013016 if (isneg)
13017 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013018 rettv->v_type = VAR_FLOAT;
13019}
13020#endif
13021
13022/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013023 * "str2list()" function
13024 */
13025 static void
13026f_str2list(typval_T *argvars, typval_T *rettv)
13027{
13028 char_u *p;
13029 int utf8 = FALSE;
13030
13031 if (rettv_list_alloc(rettv) == FAIL)
13032 return;
13033
13034 if (argvars[1].v_type != VAR_UNKNOWN)
13035 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13036
13037 p = tv_get_string(&argvars[0]);
13038
13039 if (has_mbyte || utf8)
13040 {
13041 int (*ptr2len)(char_u *);
13042 int (*ptr2char)(char_u *);
13043
13044 if (utf8 || enc_utf8)
13045 {
13046 ptr2len = utf_ptr2len;
13047 ptr2char = utf_ptr2char;
13048 }
13049 else
13050 {
13051 ptr2len = mb_ptr2len;
13052 ptr2char = mb_ptr2char;
13053 }
13054
13055 for ( ; *p != NUL; p += (*ptr2len)(p))
13056 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13057 }
13058 else
13059 for ( ; *p != NUL; ++p)
13060 list_append_number(rettv->vval.v_list, *p);
13061}
13062
13063/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013064 * "str2nr()" function
13065 */
13066 static void
13067f_str2nr(typval_T *argvars, typval_T *rettv)
13068{
13069 int base = 10;
13070 char_u *p;
13071 varnumber_T n;
13072 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013073 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013074
13075 if (argvars[1].v_type != VAR_UNKNOWN)
13076 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013077 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013078 if (base != 2 && base != 8 && base != 10 && base != 16)
13079 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013080 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013081 return;
13082 }
13083 }
13084
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013085 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013086 isneg = (*p == '-');
13087 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013088 p = skipwhite(p + 1);
13089 switch (base)
13090 {
13091 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13092 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13093 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13094 default: what = 0;
13095 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013096 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13097 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013098 if (isneg)
13099 rettv->vval.v_number = -n;
13100 else
13101 rettv->vval.v_number = n;
13102
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013103}
13104
13105#ifdef HAVE_STRFTIME
13106/*
13107 * "strftime({format}[, {time}])" function
13108 */
13109 static void
13110f_strftime(typval_T *argvars, typval_T *rettv)
13111{
13112 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013113# ifdef HAVE_LOCALTIME_R
13114 struct tm tmval;
13115# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013116 struct tm *curtime;
13117 time_t seconds;
13118 char_u *p;
13119
13120 rettv->v_type = VAR_STRING;
13121
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013122 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013123 if (argvars[1].v_type == VAR_UNKNOWN)
13124 seconds = time(NULL);
13125 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013126 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013127# ifdef HAVE_LOCALTIME_R
13128 curtime = localtime_r(&seconds, &tmval);
13129# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013130 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013131# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013132 /* MSVC returns NULL for an invalid value of seconds. */
13133 if (curtime == NULL)
13134 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13135 else
13136 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013137 vimconv_T conv;
13138 char_u *enc;
13139
13140 conv.vc_type = CONV_NONE;
13141 enc = enc_locale();
13142 convert_setup(&conv, p_enc, enc);
13143 if (conv.vc_type != CONV_NONE)
13144 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145 if (p != NULL)
13146 (void)strftime((char *)result_buf, sizeof(result_buf),
13147 (char *)p, curtime);
13148 else
13149 result_buf[0] = NUL;
13150
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013151 if (conv.vc_type != CONV_NONE)
13152 vim_free(p);
13153 convert_setup(&conv, enc, p_enc);
13154 if (conv.vc_type != CONV_NONE)
13155 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13156 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013157 rettv->vval.v_string = vim_strsave(result_buf);
13158
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013159 /* Release conversion descriptors */
13160 convert_setup(&conv, NULL, NULL);
13161 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013162 }
13163}
13164#endif
13165
13166/*
13167 * "strgetchar()" function
13168 */
13169 static void
13170f_strgetchar(typval_T *argvars, typval_T *rettv)
13171{
13172 char_u *str;
13173 int len;
13174 int error = FALSE;
13175 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013176 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013177
13178 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013179 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180 if (str == NULL)
13181 return;
13182 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013183 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013184 if (error)
13185 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013186
Bram Moolenaar13505972019-01-24 15:04:48 +010013187 while (charidx >= 0 && byteidx < len)
13188 {
13189 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013190 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013191 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13192 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013193 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013194 --charidx;
13195 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013196 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013197}
13198
13199/*
13200 * "stridx()" function
13201 */
13202 static void
13203f_stridx(typval_T *argvars, typval_T *rettv)
13204{
13205 char_u buf[NUMBUFLEN];
13206 char_u *needle;
13207 char_u *haystack;
13208 char_u *save_haystack;
13209 char_u *pos;
13210 int start_idx;
13211
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013212 needle = tv_get_string_chk(&argvars[1]);
13213 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013214 rettv->vval.v_number = -1;
13215 if (needle == NULL || haystack == NULL)
13216 return; /* type error; errmsg already given */
13217
13218 if (argvars[2].v_type != VAR_UNKNOWN)
13219 {
13220 int error = FALSE;
13221
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013222 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013223 if (error || start_idx >= (int)STRLEN(haystack))
13224 return;
13225 if (start_idx >= 0)
13226 haystack += start_idx;
13227 }
13228
13229 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13230 if (pos != NULL)
13231 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13232}
13233
13234/*
13235 * "string()" function
13236 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013237 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013238f_string(typval_T *argvars, typval_T *rettv)
13239{
13240 char_u *tofree;
13241 char_u numbuf[NUMBUFLEN];
13242
13243 rettv->v_type = VAR_STRING;
13244 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13245 get_copyID());
13246 /* Make a copy if we have a value but it's not in allocated memory. */
13247 if (rettv->vval.v_string != NULL && tofree == NULL)
13248 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13249}
13250
13251/*
13252 * "strlen()" function
13253 */
13254 static void
13255f_strlen(typval_T *argvars, typval_T *rettv)
13256{
13257 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013258 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013259}
13260
13261/*
13262 * "strchars()" function
13263 */
13264 static void
13265f_strchars(typval_T *argvars, typval_T *rettv)
13266{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013267 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013268 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013269 varnumber_T len = 0;
13270 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013271
13272 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013273 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013274 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013275 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013276 else
13277 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013278 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13279 while (*s != NUL)
13280 {
13281 func_mb_ptr2char_adv(&s);
13282 ++len;
13283 }
13284 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013285 }
13286}
13287
13288/*
13289 * "strdisplaywidth()" function
13290 */
13291 static void
13292f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13293{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013294 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013295 int col = 0;
13296
13297 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013298 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013299
13300 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13301}
13302
13303/*
13304 * "strwidth()" function
13305 */
13306 static void
13307f_strwidth(typval_T *argvars, typval_T *rettv)
13308{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013309 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013310
Bram Moolenaar13505972019-01-24 15:04:48 +010013311 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013312}
13313
13314/*
13315 * "strcharpart()" function
13316 */
13317 static void
13318f_strcharpart(typval_T *argvars, typval_T *rettv)
13319{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013320 char_u *p;
13321 int nchar;
13322 int nbyte = 0;
13323 int charlen;
13324 int len = 0;
13325 int slen;
13326 int error = FALSE;
13327
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013328 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013329 slen = (int)STRLEN(p);
13330
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013331 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013332 if (!error)
13333 {
13334 if (nchar > 0)
13335 while (nchar > 0 && nbyte < slen)
13336 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013337 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013338 --nchar;
13339 }
13340 else
13341 nbyte = nchar;
13342 if (argvars[2].v_type != VAR_UNKNOWN)
13343 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013344 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013345 while (charlen > 0 && nbyte + len < slen)
13346 {
13347 int off = nbyte + len;
13348
13349 if (off < 0)
13350 len += 1;
13351 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013352 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013353 --charlen;
13354 }
13355 }
13356 else
13357 len = slen - nbyte; /* default: all bytes that are available. */
13358 }
13359
13360 /*
13361 * Only return the overlap between the specified part and the actual
13362 * string.
13363 */
13364 if (nbyte < 0)
13365 {
13366 len += nbyte;
13367 nbyte = 0;
13368 }
13369 else if (nbyte > slen)
13370 nbyte = slen;
13371 if (len < 0)
13372 len = 0;
13373 else if (nbyte + len > slen)
13374 len = slen - nbyte;
13375
13376 rettv->v_type = VAR_STRING;
13377 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013378}
13379
13380/*
13381 * "strpart()" function
13382 */
13383 static void
13384f_strpart(typval_T *argvars, typval_T *rettv)
13385{
13386 char_u *p;
13387 int n;
13388 int len;
13389 int slen;
13390 int error = FALSE;
13391
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013392 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013393 slen = (int)STRLEN(p);
13394
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013395 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013396 if (error)
13397 len = 0;
13398 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013399 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013400 else
13401 len = slen - n; /* default len: all bytes that are available. */
13402
13403 /*
13404 * Only return the overlap between the specified part and the actual
13405 * string.
13406 */
13407 if (n < 0)
13408 {
13409 len += n;
13410 n = 0;
13411 }
13412 else if (n > slen)
13413 n = slen;
13414 if (len < 0)
13415 len = 0;
13416 else if (n + len > slen)
13417 len = slen - n;
13418
13419 rettv->v_type = VAR_STRING;
13420 rettv->vval.v_string = vim_strnsave(p + n, len);
13421}
13422
13423/*
13424 * "strridx()" function
13425 */
13426 static void
13427f_strridx(typval_T *argvars, typval_T *rettv)
13428{
13429 char_u buf[NUMBUFLEN];
13430 char_u *needle;
13431 char_u *haystack;
13432 char_u *rest;
13433 char_u *lastmatch = NULL;
13434 int haystack_len, end_idx;
13435
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013436 needle = tv_get_string_chk(&argvars[1]);
13437 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013438
13439 rettv->vval.v_number = -1;
13440 if (needle == NULL || haystack == NULL)
13441 return; /* type error; errmsg already given */
13442
13443 haystack_len = (int)STRLEN(haystack);
13444 if (argvars[2].v_type != VAR_UNKNOWN)
13445 {
13446 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013447 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013448 if (end_idx < 0)
13449 return; /* can never find a match */
13450 }
13451 else
13452 end_idx = haystack_len;
13453
13454 if (*needle == NUL)
13455 {
13456 /* Empty string matches past the end. */
13457 lastmatch = haystack + end_idx;
13458 }
13459 else
13460 {
13461 for (rest = haystack; *rest != '\0'; ++rest)
13462 {
13463 rest = (char_u *)strstr((char *)rest, (char *)needle);
13464 if (rest == NULL || rest > haystack + end_idx)
13465 break;
13466 lastmatch = rest;
13467 }
13468 }
13469
13470 if (lastmatch == NULL)
13471 rettv->vval.v_number = -1;
13472 else
13473 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13474}
13475
13476/*
13477 * "strtrans()" function
13478 */
13479 static void
13480f_strtrans(typval_T *argvars, typval_T *rettv)
13481{
13482 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013483 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013484}
13485
13486/*
13487 * "submatch()" function
13488 */
13489 static void
13490f_submatch(typval_T *argvars, typval_T *rettv)
13491{
13492 int error = FALSE;
13493 int no;
13494 int retList = 0;
13495
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013496 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013497 if (error)
13498 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013499 if (no < 0 || no >= NSUBEXP)
13500 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013501 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013502 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013503 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013504 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013505 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013506 if (error)
13507 return;
13508
13509 if (retList == 0)
13510 {
13511 rettv->v_type = VAR_STRING;
13512 rettv->vval.v_string = reg_submatch(no);
13513 }
13514 else
13515 {
13516 rettv->v_type = VAR_LIST;
13517 rettv->vval.v_list = reg_submatch_list(no);
13518 }
13519}
13520
13521/*
13522 * "substitute()" function
13523 */
13524 static void
13525f_substitute(typval_T *argvars, typval_T *rettv)
13526{
13527 char_u patbuf[NUMBUFLEN];
13528 char_u subbuf[NUMBUFLEN];
13529 char_u flagsbuf[NUMBUFLEN];
13530
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013531 char_u *str = tv_get_string_chk(&argvars[0]);
13532 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013533 char_u *sub = NULL;
13534 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013535 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013536
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013537 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13538 expr = &argvars[2];
13539 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013540 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013541
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013542 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013543 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13544 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013545 rettv->vval.v_string = NULL;
13546 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013547 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013548}
13549
13550/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013551 * "swapinfo(swap_filename)" function
13552 */
13553 static void
13554f_swapinfo(typval_T *argvars, typval_T *rettv)
13555{
13556 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013557 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013558}
13559
13560/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013561 * "swapname(expr)" function
13562 */
13563 static void
13564f_swapname(typval_T *argvars, typval_T *rettv)
13565{
13566 buf_T *buf;
13567
13568 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013569 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013570 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13571 || buf->b_ml.ml_mfp->mf_fname == NULL)
13572 rettv->vval.v_string = NULL;
13573 else
13574 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13575}
13576
13577/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013578 * "synID(lnum, col, trans)" function
13579 */
13580 static void
13581f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13582{
13583 int id = 0;
13584#ifdef FEAT_SYN_HL
13585 linenr_T lnum;
13586 colnr_T col;
13587 int trans;
13588 int transerr = FALSE;
13589
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013590 lnum = tv_get_lnum(argvars); /* -1 on type error */
13591 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13592 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013593
13594 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13595 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13596 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13597#endif
13598
13599 rettv->vval.v_number = id;
13600}
13601
13602/*
13603 * "synIDattr(id, what [, mode])" function
13604 */
13605 static void
13606f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13607{
13608 char_u *p = NULL;
13609#ifdef FEAT_SYN_HL
13610 int id;
13611 char_u *what;
13612 char_u *mode;
13613 char_u modebuf[NUMBUFLEN];
13614 int modec;
13615
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013616 id = (int)tv_get_number(&argvars[0]);
13617 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013618 if (argvars[2].v_type != VAR_UNKNOWN)
13619 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013620 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013621 modec = TOLOWER_ASC(mode[0]);
13622 if (modec != 't' && modec != 'c' && modec != 'g')
13623 modec = 0; /* replace invalid with current */
13624 }
13625 else
13626 {
13627#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13628 if (USE_24BIT)
13629 modec = 'g';
13630 else
13631#endif
13632 if (t_colors > 1)
13633 modec = 'c';
13634 else
13635 modec = 't';
13636 }
13637
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013638 switch (TOLOWER_ASC(what[0]))
13639 {
13640 case 'b':
13641 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13642 p = highlight_color(id, what, modec);
13643 else /* bold */
13644 p = highlight_has_attr(id, HL_BOLD, modec);
13645 break;
13646
13647 case 'f': /* fg[#] or font */
13648 p = highlight_color(id, what, modec);
13649 break;
13650
13651 case 'i':
13652 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13653 p = highlight_has_attr(id, HL_INVERSE, modec);
13654 else /* italic */
13655 p = highlight_has_attr(id, HL_ITALIC, modec);
13656 break;
13657
13658 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013659 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013660 break;
13661
13662 case 'r': /* reverse */
13663 p = highlight_has_attr(id, HL_INVERSE, modec);
13664 break;
13665
13666 case 's':
13667 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13668 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013669 /* strikeout */
13670 else if (TOLOWER_ASC(what[1]) == 't' &&
13671 TOLOWER_ASC(what[2]) == 'r')
13672 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013673 else /* standout */
13674 p = highlight_has_attr(id, HL_STANDOUT, modec);
13675 break;
13676
13677 case 'u':
13678 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13679 /* underline */
13680 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13681 else
13682 /* undercurl */
13683 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13684 break;
13685 }
13686
13687 if (p != NULL)
13688 p = vim_strsave(p);
13689#endif
13690 rettv->v_type = VAR_STRING;
13691 rettv->vval.v_string = p;
13692}
13693
13694/*
13695 * "synIDtrans(id)" function
13696 */
13697 static void
13698f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13699{
13700 int id;
13701
13702#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013703 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013704
13705 if (id > 0)
13706 id = syn_get_final_id(id);
13707 else
13708#endif
13709 id = 0;
13710
13711 rettv->vval.v_number = id;
13712}
13713
13714/*
13715 * "synconcealed(lnum, col)" function
13716 */
13717 static void
13718f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13719{
13720#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13721 linenr_T lnum;
13722 colnr_T col;
13723 int syntax_flags = 0;
13724 int cchar;
13725 int matchid = 0;
13726 char_u str[NUMBUFLEN];
13727#endif
13728
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013729 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013730
13731#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013732 lnum = tv_get_lnum(argvars); /* -1 on type error */
13733 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013734
13735 vim_memset(str, NUL, sizeof(str));
13736
13737 if (rettv_list_alloc(rettv) != FAIL)
13738 {
13739 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13740 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13741 && curwin->w_p_cole > 0)
13742 {
13743 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13744 syntax_flags = get_syntax_info(&matchid);
13745
13746 /* get the conceal character */
13747 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13748 {
13749 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013750 if (cchar == NUL && curwin->w_p_cole == 1)
13751 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013752 if (cchar != NUL)
13753 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013754 if (has_mbyte)
13755 (*mb_char2bytes)(cchar, str);
13756 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013757 str[0] = cchar;
13758 }
13759 }
13760 }
13761
13762 list_append_number(rettv->vval.v_list,
13763 (syntax_flags & HL_CONCEAL) != 0);
13764 /* -1 to auto-determine strlen */
13765 list_append_string(rettv->vval.v_list, str, -1);
13766 list_append_number(rettv->vval.v_list, matchid);
13767 }
13768#endif
13769}
13770
13771/*
13772 * "synstack(lnum, col)" function
13773 */
13774 static void
13775f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13776{
13777#ifdef FEAT_SYN_HL
13778 linenr_T lnum;
13779 colnr_T col;
13780 int i;
13781 int id;
13782#endif
13783
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013784 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013785
13786#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013787 lnum = tv_get_lnum(argvars); /* -1 on type error */
13788 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013789
13790 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13791 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13792 && rettv_list_alloc(rettv) != FAIL)
13793 {
13794 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13795 for (i = 0; ; ++i)
13796 {
13797 id = syn_get_stack_item(i);
13798 if (id < 0)
13799 break;
13800 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13801 break;
13802 }
13803 }
13804#endif
13805}
13806
13807 static void
13808get_cmd_output_as_rettv(
13809 typval_T *argvars,
13810 typval_T *rettv,
13811 int retlist)
13812{
13813 char_u *res = NULL;
13814 char_u *p;
13815 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013816 int err = FALSE;
13817 FILE *fd;
13818 list_T *list = NULL;
13819 int flags = SHELL_SILENT;
13820
13821 rettv->v_type = VAR_STRING;
13822 rettv->vval.v_string = NULL;
13823 if (check_restricted() || check_secure())
13824 goto errret;
13825
13826 if (argvars[1].v_type != VAR_UNKNOWN)
13827 {
13828 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013829 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013830 * command.
13831 */
13832 if ((infile = vim_tempname('i', TRUE)) == NULL)
13833 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013834 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013835 goto errret;
13836 }
13837
13838 fd = mch_fopen((char *)infile, WRITEBIN);
13839 if (fd == NULL)
13840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013841 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013842 goto errret;
13843 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013844 if (argvars[1].v_type == VAR_NUMBER)
13845 {
13846 linenr_T lnum;
13847 buf_T *buf;
13848
13849 buf = buflist_findnr(argvars[1].vval.v_number);
13850 if (buf == NULL)
13851 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013852 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013853 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013854 goto errret;
13855 }
13856
13857 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13858 {
13859 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13860 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13861 {
13862 err = TRUE;
13863 break;
13864 }
13865 if (putc(NL, fd) == EOF)
13866 {
13867 err = TRUE;
13868 break;
13869 }
13870 }
13871 }
13872 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013873 {
13874 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13875 err = TRUE;
13876 }
13877 else
13878 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013879 size_t len;
13880 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013881
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013882 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013883 if (p == NULL)
13884 {
13885 fclose(fd);
13886 goto errret; /* type error; errmsg already given */
13887 }
13888 len = STRLEN(p);
13889 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13890 err = TRUE;
13891 }
13892 if (fclose(fd) != 0)
13893 err = TRUE;
13894 if (err)
13895 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013896 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013897 goto errret;
13898 }
13899 }
13900
13901 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13902 * echoes typeahead, that messes up the display. */
13903 if (!msg_silent)
13904 flags += SHELL_COOKED;
13905
13906 if (retlist)
13907 {
13908 int len;
13909 listitem_T *li;
13910 char_u *s = NULL;
13911 char_u *start;
13912 char_u *end;
13913 int i;
13914
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013915 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013916 if (res == NULL)
13917 goto errret;
13918
13919 list = list_alloc();
13920 if (list == NULL)
13921 goto errret;
13922
13923 for (i = 0; i < len; ++i)
13924 {
13925 start = res + i;
13926 while (i < len && res[i] != NL)
13927 ++i;
13928 end = res + i;
13929
Bram Moolenaar964b3742019-05-24 18:54:09 +020013930 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013931 if (s == NULL)
13932 goto errret;
13933
13934 for (p = s; start < end; ++p, ++start)
13935 *p = *start == NUL ? NL : *start;
13936 *p = NUL;
13937
13938 li = listitem_alloc();
13939 if (li == NULL)
13940 {
13941 vim_free(s);
13942 goto errret;
13943 }
13944 li->li_tv.v_type = VAR_STRING;
13945 li->li_tv.v_lock = 0;
13946 li->li_tv.vval.v_string = s;
13947 list_append(list, li);
13948 }
13949
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013950 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013951 list = NULL;
13952 }
13953 else
13954 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013955 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013956#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013957 /* translate <CR><NL> into <NL> */
13958 if (res != NULL)
13959 {
13960 char_u *s, *d;
13961
13962 d = res;
13963 for (s = res; *s; ++s)
13964 {
13965 if (s[0] == CAR && s[1] == NL)
13966 ++s;
13967 *d++ = *s;
13968 }
13969 *d = NUL;
13970 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013971#endif
13972 rettv->vval.v_string = res;
13973 res = NULL;
13974 }
13975
13976errret:
13977 if (infile != NULL)
13978 {
13979 mch_remove(infile);
13980 vim_free(infile);
13981 }
13982 if (res != NULL)
13983 vim_free(res);
13984 if (list != NULL)
13985 list_free(list);
13986}
13987
13988/*
13989 * "system()" function
13990 */
13991 static void
13992f_system(typval_T *argvars, typval_T *rettv)
13993{
13994 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13995}
13996
13997/*
13998 * "systemlist()" function
13999 */
14000 static void
14001f_systemlist(typval_T *argvars, typval_T *rettv)
14002{
14003 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14004}
14005
14006/*
14007 * "tabpagebuflist()" function
14008 */
14009 static void
14010f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14011{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014012 tabpage_T *tp;
14013 win_T *wp = NULL;
14014
14015 if (argvars[0].v_type == VAR_UNKNOWN)
14016 wp = firstwin;
14017 else
14018 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014019 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014020 if (tp != NULL)
14021 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14022 }
14023 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14024 {
14025 for (; wp != NULL; wp = wp->w_next)
14026 if (list_append_number(rettv->vval.v_list,
14027 wp->w_buffer->b_fnum) == FAIL)
14028 break;
14029 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014030}
14031
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014032/*
14033 * "tabpagenr()" function
14034 */
14035 static void
14036f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14037{
14038 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014039 char_u *arg;
14040
14041 if (argvars[0].v_type != VAR_UNKNOWN)
14042 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014043 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014044 nr = 0;
14045 if (arg != NULL)
14046 {
14047 if (STRCMP(arg, "$") == 0)
14048 nr = tabpage_index(NULL) - 1;
14049 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014050 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014051 }
14052 }
14053 else
14054 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014055 rettv->vval.v_number = nr;
14056}
14057
14058
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014059/*
14060 * Common code for tabpagewinnr() and winnr().
14061 */
14062 static int
14063get_winnr(tabpage_T *tp, typval_T *argvar)
14064{
14065 win_T *twin;
14066 int nr = 1;
14067 win_T *wp;
14068 char_u *arg;
14069
14070 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14071 if (argvar->v_type != VAR_UNKNOWN)
14072 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014073 int invalid_arg = FALSE;
14074
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014075 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014076 if (arg == NULL)
14077 nr = 0; /* type error; errmsg already given */
14078 else if (STRCMP(arg, "$") == 0)
14079 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14080 else if (STRCMP(arg, "#") == 0)
14081 {
14082 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14083 if (twin == NULL)
14084 nr = 0;
14085 }
14086 else
14087 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014088 long count;
14089 char_u *endp;
14090
14091 // Extract the window count (if specified). e.g. winnr('3j')
14092 count = strtol((char *)arg, (char **)&endp, 10);
14093 if (count <= 0)
14094 count = 1; // if count is not specified, default to 1
14095 if (endp != NULL && *endp != '\0')
14096 {
14097 if (STRCMP(endp, "j") == 0)
14098 twin = win_vert_neighbor(tp, twin, FALSE, count);
14099 else if (STRCMP(endp, "k") == 0)
14100 twin = win_vert_neighbor(tp, twin, TRUE, count);
14101 else if (STRCMP(endp, "h") == 0)
14102 twin = win_horz_neighbor(tp, twin, TRUE, count);
14103 else if (STRCMP(endp, "l") == 0)
14104 twin = win_horz_neighbor(tp, twin, FALSE, count);
14105 else
14106 invalid_arg = TRUE;
14107 }
14108 else
14109 invalid_arg = TRUE;
14110 }
14111
14112 if (invalid_arg)
14113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014114 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014115 nr = 0;
14116 }
14117 }
14118
14119 if (nr > 0)
14120 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14121 wp != twin; wp = wp->w_next)
14122 {
14123 if (wp == NULL)
14124 {
14125 /* didn't find it in this tabpage */
14126 nr = 0;
14127 break;
14128 }
14129 ++nr;
14130 }
14131 return nr;
14132}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014133
14134/*
14135 * "tabpagewinnr()" function
14136 */
14137 static void
14138f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14139{
14140 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014141 tabpage_T *tp;
14142
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014143 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014144 if (tp == NULL)
14145 nr = 0;
14146 else
14147 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014148 rettv->vval.v_number = nr;
14149}
14150
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014151/*
14152 * "tagfiles()" function
14153 */
14154 static void
14155f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14156{
14157 char_u *fname;
14158 tagname_T tn;
14159 int first;
14160
14161 if (rettv_list_alloc(rettv) == FAIL)
14162 return;
14163 fname = alloc(MAXPATHL);
14164 if (fname == NULL)
14165 return;
14166
14167 for (first = TRUE; ; first = FALSE)
14168 if (get_tagfname(&tn, first, fname) == FAIL
14169 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14170 break;
14171 tagname_free(&tn);
14172 vim_free(fname);
14173}
14174
14175/*
14176 * "taglist()" function
14177 */
14178 static void
14179f_taglist(typval_T *argvars, typval_T *rettv)
14180{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014181 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014182 char_u *tag_pattern;
14183
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014184 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014185
14186 rettv->vval.v_number = FALSE;
14187 if (*tag_pattern == NUL)
14188 return;
14189
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014190 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014191 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014192 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014193 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014194}
14195
14196/*
14197 * "tempname()" function
14198 */
14199 static void
14200f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14201{
14202 static int x = 'A';
14203
14204 rettv->v_type = VAR_STRING;
14205 rettv->vval.v_string = vim_tempname(x, FALSE);
14206
14207 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14208 * names. Skip 'I' and 'O', they are used for shell redirection. */
14209 do
14210 {
14211 if (x == 'Z')
14212 x = '0';
14213 else if (x == '9')
14214 x = 'A';
14215 else
14216 {
14217#ifdef EBCDIC
14218 if (x == 'I')
14219 x = 'J';
14220 else if (x == 'R')
14221 x = 'S';
14222 else
14223#endif
14224 ++x;
14225 }
14226 } while (x == 'I' || x == 'O');
14227}
14228
14229#ifdef FEAT_FLOAT
14230/*
14231 * "tan()" function
14232 */
14233 static void
14234f_tan(typval_T *argvars, typval_T *rettv)
14235{
14236 float_T f = 0.0;
14237
14238 rettv->v_type = VAR_FLOAT;
14239 if (get_float_arg(argvars, &f) == OK)
14240 rettv->vval.v_float = tan(f);
14241 else
14242 rettv->vval.v_float = 0.0;
14243}
14244
14245/*
14246 * "tanh()" function
14247 */
14248 static void
14249f_tanh(typval_T *argvars, typval_T *rettv)
14250{
14251 float_T f = 0.0;
14252
14253 rettv->v_type = VAR_FLOAT;
14254 if (get_float_arg(argvars, &f) == OK)
14255 rettv->vval.v_float = tanh(f);
14256 else
14257 rettv->vval.v_float = 0.0;
14258}
14259#endif
14260
14261/*
14262 * "test_alloc_fail(id, countdown, repeat)" function
14263 */
14264 static void
14265f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14266{
14267 if (argvars[0].v_type != VAR_NUMBER
14268 || argvars[0].vval.v_number <= 0
14269 || argvars[1].v_type != VAR_NUMBER
14270 || argvars[1].vval.v_number < 0
14271 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014272 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014273 else
14274 {
14275 alloc_fail_id = argvars[0].vval.v_number;
14276 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014277 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014278 alloc_fail_countdown = argvars[1].vval.v_number;
14279 alloc_fail_repeat = argvars[2].vval.v_number;
14280 did_outofmem_msg = FALSE;
14281 }
14282}
14283
14284/*
14285 * "test_autochdir()"
14286 */
14287 static void
14288f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14289{
14290#if defined(FEAT_AUTOCHDIR)
14291 test_autochdir = TRUE;
14292#endif
14293}
14294
14295/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014296 * "test_feedinput()"
14297 */
14298 static void
14299f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14300{
14301#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014302 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014303
14304 if (val != NULL)
14305 {
14306 trash_input_buf();
14307 add_to_input_buf_csi(val, (int)STRLEN(val));
14308 }
14309#endif
14310}
14311
14312/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014313 * "test_getvalue({name})" function
14314 */
14315 static void
14316f_test_getvalue(typval_T *argvars, typval_T *rettv)
14317{
14318 if (argvars[0].v_type != VAR_STRING)
14319 emsg(_(e_invarg));
14320 else
14321 {
14322 char_u *name = tv_get_string(&argvars[0]);
14323
14324 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14325 rettv->vval.v_number = need_fileinfo;
14326 else
14327 semsg(_(e_invarg2), name);
14328 }
14329}
14330
14331/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014332 * "test_option_not_set({name})" function
14333 */
14334 static void
14335f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14336{
14337 char_u *name = (char_u *)"";
14338
14339 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014340 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014341 else
14342 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014343 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014344 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014345 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014346 }
14347}
14348
14349/*
14350 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014351 */
14352 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014353f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014354{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014355 char_u *name = (char_u *)"";
14356 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014357 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014358
14359 if (argvars[0].v_type != VAR_STRING
14360 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014361 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014362 else
14363 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014364 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014365 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014366
14367 if (STRCMP(name, (char_u *)"redraw") == 0)
14368 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014369 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14370 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014371 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14372 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014373 else if (STRCMP(name, (char_u *)"starting") == 0)
14374 {
14375 if (val)
14376 {
14377 if (save_starting < 0)
14378 save_starting = starting;
14379 starting = 0;
14380 }
14381 else
14382 {
14383 starting = save_starting;
14384 save_starting = -1;
14385 }
14386 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014387 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14388 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014389 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14390 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014391 else if (STRCMP(name, (char_u *)"ALL") == 0)
14392 {
14393 disable_char_avail_for_testing = FALSE;
14394 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014395 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014396 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014397 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014398 if (save_starting >= 0)
14399 {
14400 starting = save_starting;
14401 save_starting = -1;
14402 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014403 }
14404 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014405 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014406 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014407}
14408
14409/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014410 * "test_refcount({expr})" function
14411 */
14412 static void
14413f_test_refcount(typval_T *argvars, typval_T *rettv)
14414{
14415 int retval = -1;
14416
14417 switch (argvars[0].v_type)
14418 {
14419 case VAR_UNKNOWN:
14420 case VAR_NUMBER:
14421 case VAR_FLOAT:
14422 case VAR_SPECIAL:
14423 case VAR_STRING:
14424 break;
14425 case VAR_JOB:
14426#ifdef FEAT_JOB_CHANNEL
14427 if (argvars[0].vval.v_job != NULL)
14428 retval = argvars[0].vval.v_job->jv_refcount - 1;
14429#endif
14430 break;
14431 case VAR_CHANNEL:
14432#ifdef FEAT_JOB_CHANNEL
14433 if (argvars[0].vval.v_channel != NULL)
14434 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14435#endif
14436 break;
14437 case VAR_FUNC:
14438 if (argvars[0].vval.v_string != NULL)
14439 {
14440 ufunc_T *fp;
14441
14442 fp = find_func(argvars[0].vval.v_string);
14443 if (fp != NULL)
14444 retval = fp->uf_refcount;
14445 }
14446 break;
14447 case VAR_PARTIAL:
14448 if (argvars[0].vval.v_partial != NULL)
14449 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14450 break;
14451 case VAR_BLOB:
14452 if (argvars[0].vval.v_blob != NULL)
14453 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14454 break;
14455 case VAR_LIST:
14456 if (argvars[0].vval.v_list != NULL)
14457 retval = argvars[0].vval.v_list->lv_refcount - 1;
14458 break;
14459 case VAR_DICT:
14460 if (argvars[0].vval.v_dict != NULL)
14461 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14462 break;
14463 }
14464
14465 rettv->v_type = VAR_NUMBER;
14466 rettv->vval.v_number = retval;
14467
14468}
14469
14470/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014471 * "test_garbagecollect_now()" function
14472 */
14473 static void
14474f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14475{
14476 /* This is dangerous, any Lists and Dicts used internally may be freed
14477 * while still in use. */
14478 garbage_collect(TRUE);
14479}
14480
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014481/*
14482 * "test_ignore_error()" function
14483 */
14484 static void
14485f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14486{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014487 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014488}
14489
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014490 static void
14491f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14492{
14493 rettv->v_type = VAR_BLOB;
14494 rettv->vval.v_blob = NULL;
14495}
14496
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014497#ifdef FEAT_JOB_CHANNEL
14498 static void
14499f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14500{
14501 rettv->v_type = VAR_CHANNEL;
14502 rettv->vval.v_channel = NULL;
14503}
14504#endif
14505
14506 static void
14507f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14508{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014509 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014510}
14511
14512#ifdef FEAT_JOB_CHANNEL
14513 static void
14514f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14515{
14516 rettv->v_type = VAR_JOB;
14517 rettv->vval.v_job = NULL;
14518}
14519#endif
14520
14521 static void
14522f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14523{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014524 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014525}
14526
14527 static void
14528f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14529{
14530 rettv->v_type = VAR_PARTIAL;
14531 rettv->vval.v_partial = NULL;
14532}
14533
14534 static void
14535f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14536{
14537 rettv->v_type = VAR_STRING;
14538 rettv->vval.v_string = NULL;
14539}
14540
Bram Moolenaarab186732018-09-14 21:27:06 +020014541#ifdef FEAT_GUI
14542 static void
14543f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14544{
14545 char_u *which;
14546 long value;
14547 int dragging;
14548 scrollbar_T *sb = NULL;
14549
14550 if (argvars[0].v_type != VAR_STRING
14551 || (argvars[1].v_type) != VAR_NUMBER
14552 || (argvars[2].v_type) != VAR_NUMBER)
14553 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014554 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014555 return;
14556 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014557 which = tv_get_string(&argvars[0]);
14558 value = tv_get_number(&argvars[1]);
14559 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014560
14561 if (STRCMP(which, "left") == 0)
14562 sb = &curwin->w_scrollbars[SBAR_LEFT];
14563 else if (STRCMP(which, "right") == 0)
14564 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14565 else if (STRCMP(which, "hor") == 0)
14566 sb = &gui.bottom_sbar;
14567 if (sb == NULL)
14568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014569 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014570 return;
14571 }
14572 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014573# ifndef USE_ON_FLY_SCROLL
14574 // need to loop through normal_cmd() to handle the scroll events
14575 exec_normal(FALSE, TRUE, FALSE);
14576# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014577}
14578#endif
14579
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014580#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014581 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014582f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14583{
14584 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14585 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14586}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014587#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014588
14589 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014590f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14591{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014592 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014593}
14594
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014595/*
14596 * Get a callback from "arg". It can be a Funcref or a function name.
14597 * When "arg" is zero return an empty string.
14598 * Return NULL for an invalid argument.
14599 */
14600 char_u *
14601get_callback(typval_T *arg, partial_T **pp)
14602{
14603 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14604 {
14605 *pp = arg->vval.v_partial;
14606 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014607 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014608 }
14609 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014610 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014611 {
14612 func_ref(arg->vval.v_string);
14613 return arg->vval.v_string;
14614 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014615 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14616 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014617 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014618 return NULL;
14619}
14620
14621/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014622 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014623 */
14624 void
14625free_callback(char_u *callback, partial_T *partial)
14626{
14627 if (partial != NULL)
14628 partial_unref(partial);
14629 else if (callback != NULL)
14630 {
14631 func_unref(callback);
14632 vim_free(callback);
14633 }
14634}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014635
14636#ifdef FEAT_TIMERS
14637/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014638 * "timer_info([timer])" function
14639 */
14640 static void
14641f_timer_info(typval_T *argvars, typval_T *rettv)
14642{
14643 timer_T *timer = NULL;
14644
14645 if (rettv_list_alloc(rettv) != OK)
14646 return;
14647 if (argvars[0].v_type != VAR_UNKNOWN)
14648 {
14649 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014650 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014651 else
14652 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014653 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014654 if (timer != NULL)
14655 add_timer_info(rettv, timer);
14656 }
14657 }
14658 else
14659 add_timer_info_all(rettv);
14660}
14661
14662/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014663 * "timer_pause(timer, paused)" function
14664 */
14665 static void
14666f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14667{
14668 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014669 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014670
14671 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014672 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014673 else
14674 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014675 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014676 if (timer != NULL)
14677 timer->tr_paused = paused;
14678 }
14679}
14680
14681/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014682 * "timer_start(time, callback [, options])" function
14683 */
14684 static void
14685f_timer_start(typval_T *argvars, typval_T *rettv)
14686{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014687 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014688 timer_T *timer;
14689 int repeat = 0;
14690 char_u *callback;
14691 dict_T *dict;
14692 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014693
Bram Moolenaar75537a92016-09-05 22:45:28 +020014694 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014695 if (check_secure())
14696 return;
14697 if (argvars[2].v_type != VAR_UNKNOWN)
14698 {
14699 if (argvars[2].v_type != VAR_DICT
14700 || (dict = argvars[2].vval.v_dict) == NULL)
14701 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014702 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014703 return;
14704 }
14705 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014706 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014707 }
14708
Bram Moolenaar75537a92016-09-05 22:45:28 +020014709 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014710 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014711 return;
14712
14713 timer = create_timer(msec, repeat);
14714 if (timer == NULL)
14715 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014716 else
14717 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014718 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014719 timer->tr_callback = vim_strsave(callback);
14720 else
14721 /* pointer into the partial */
14722 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014723 timer->tr_partial = partial;
14724 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014725 }
14726}
14727
14728/*
14729 * "timer_stop(timer)" function
14730 */
14731 static void
14732f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14733{
14734 timer_T *timer;
14735
14736 if (argvars[0].v_type != VAR_NUMBER)
14737 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014738 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014739 return;
14740 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014741 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014742 if (timer != NULL)
14743 stop_timer(timer);
14744}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014745
14746/*
14747 * "timer_stopall()" function
14748 */
14749 static void
14750f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14751{
14752 stop_all_timers();
14753}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014754#endif
14755
14756/*
14757 * "tolower(string)" function
14758 */
14759 static void
14760f_tolower(typval_T *argvars, typval_T *rettv)
14761{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014762 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014763 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014764}
14765
14766/*
14767 * "toupper(string)" function
14768 */
14769 static void
14770f_toupper(typval_T *argvars, typval_T *rettv)
14771{
14772 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014773 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014774}
14775
14776/*
14777 * "tr(string, fromstr, tostr)" function
14778 */
14779 static void
14780f_tr(typval_T *argvars, typval_T *rettv)
14781{
14782 char_u *in_str;
14783 char_u *fromstr;
14784 char_u *tostr;
14785 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014786 int inlen;
14787 int fromlen;
14788 int tolen;
14789 int idx;
14790 char_u *cpstr;
14791 int cplen;
14792 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014793 char_u buf[NUMBUFLEN];
14794 char_u buf2[NUMBUFLEN];
14795 garray_T ga;
14796
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014797 in_str = tv_get_string(&argvars[0]);
14798 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14799 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014800
14801 /* Default return value: empty string. */
14802 rettv->v_type = VAR_STRING;
14803 rettv->vval.v_string = NULL;
14804 if (fromstr == NULL || tostr == NULL)
14805 return; /* type error; errmsg already given */
14806 ga_init2(&ga, (int)sizeof(char), 80);
14807
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014808 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014809 /* not multi-byte: fromstr and tostr must be the same length */
14810 if (STRLEN(fromstr) != STRLEN(tostr))
14811 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014812error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014813 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014814 ga_clear(&ga);
14815 return;
14816 }
14817
14818 /* fromstr and tostr have to contain the same number of chars */
14819 while (*in_str != NUL)
14820 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014821 if (has_mbyte)
14822 {
14823 inlen = (*mb_ptr2len)(in_str);
14824 cpstr = in_str;
14825 cplen = inlen;
14826 idx = 0;
14827 for (p = fromstr; *p != NUL; p += fromlen)
14828 {
14829 fromlen = (*mb_ptr2len)(p);
14830 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14831 {
14832 for (p = tostr; *p != NUL; p += tolen)
14833 {
14834 tolen = (*mb_ptr2len)(p);
14835 if (idx-- == 0)
14836 {
14837 cplen = tolen;
14838 cpstr = p;
14839 break;
14840 }
14841 }
14842 if (*p == NUL) /* tostr is shorter than fromstr */
14843 goto error;
14844 break;
14845 }
14846 ++idx;
14847 }
14848
14849 if (first && cpstr == in_str)
14850 {
14851 /* Check that fromstr and tostr have the same number of
14852 * (multi-byte) characters. Done only once when a character
14853 * of in_str doesn't appear in fromstr. */
14854 first = FALSE;
14855 for (p = tostr; *p != NUL; p += tolen)
14856 {
14857 tolen = (*mb_ptr2len)(p);
14858 --idx;
14859 }
14860 if (idx != 0)
14861 goto error;
14862 }
14863
14864 (void)ga_grow(&ga, cplen);
14865 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14866 ga.ga_len += cplen;
14867
14868 in_str += inlen;
14869 }
14870 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014871 {
14872 /* When not using multi-byte chars we can do it faster. */
14873 p = vim_strchr(fromstr, *in_str);
14874 if (p != NULL)
14875 ga_append(&ga, tostr[p - fromstr]);
14876 else
14877 ga_append(&ga, *in_str);
14878 ++in_str;
14879 }
14880 }
14881
14882 /* add a terminating NUL */
14883 (void)ga_grow(&ga, 1);
14884 ga_append(&ga, NUL);
14885
14886 rettv->vval.v_string = ga.ga_data;
14887}
14888
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014889/*
14890 * "trim({expr})" function
14891 */
14892 static void
14893f_trim(typval_T *argvars, typval_T *rettv)
14894{
14895 char_u buf1[NUMBUFLEN];
14896 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014897 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014898 char_u *mask = NULL;
14899 char_u *tail;
14900 char_u *prev;
14901 char_u *p;
14902 int c1;
14903
14904 rettv->v_type = VAR_STRING;
14905 if (head == NULL)
14906 {
14907 rettv->vval.v_string = NULL;
14908 return;
14909 }
14910
14911 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014912 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014913
14914 while (*head != NUL)
14915 {
14916 c1 = PTR2CHAR(head);
14917 if (mask == NULL)
14918 {
14919 if (c1 > ' ' && c1 != 0xa0)
14920 break;
14921 }
14922 else
14923 {
14924 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14925 if (c1 == PTR2CHAR(p))
14926 break;
14927 if (*p == NUL)
14928 break;
14929 }
14930 MB_PTR_ADV(head);
14931 }
14932
14933 for (tail = head + STRLEN(head); tail > head; tail = prev)
14934 {
14935 prev = tail;
14936 MB_PTR_BACK(head, prev);
14937 c1 = PTR2CHAR(prev);
14938 if (mask == NULL)
14939 {
14940 if (c1 > ' ' && c1 != 0xa0)
14941 break;
14942 }
14943 else
14944 {
14945 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14946 if (c1 == PTR2CHAR(p))
14947 break;
14948 if (*p == NUL)
14949 break;
14950 }
14951 }
14952 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14953}
14954
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014955#ifdef FEAT_FLOAT
14956/*
14957 * "trunc({float})" function
14958 */
14959 static void
14960f_trunc(typval_T *argvars, typval_T *rettv)
14961{
14962 float_T f = 0.0;
14963
14964 rettv->v_type = VAR_FLOAT;
14965 if (get_float_arg(argvars, &f) == OK)
14966 /* trunc() is not in C90, use floor() or ceil() instead. */
14967 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14968 else
14969 rettv->vval.v_float = 0.0;
14970}
14971#endif
14972
14973/*
14974 * "type(expr)" function
14975 */
14976 static void
14977f_type(typval_T *argvars, typval_T *rettv)
14978{
14979 int n = -1;
14980
14981 switch (argvars[0].v_type)
14982 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014983 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14984 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014985 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014986 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14987 case VAR_LIST: n = VAR_TYPE_LIST; break;
14988 case VAR_DICT: n = VAR_TYPE_DICT; break;
14989 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014990 case VAR_SPECIAL:
14991 if (argvars[0].vval.v_number == VVAL_FALSE
14992 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014993 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014994 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014995 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014996 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014997 case VAR_JOB: n = VAR_TYPE_JOB; break;
14998 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014999 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015000 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015001 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015002 n = -1;
15003 break;
15004 }
15005 rettv->vval.v_number = n;
15006}
15007
15008/*
15009 * "undofile(name)" function
15010 */
15011 static void
15012f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15013{
15014 rettv->v_type = VAR_STRING;
15015#ifdef FEAT_PERSISTENT_UNDO
15016 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015017 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015018
15019 if (*fname == NUL)
15020 {
15021 /* If there is no file name there will be no undo file. */
15022 rettv->vval.v_string = NULL;
15023 }
15024 else
15025 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015026 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015027
15028 if (ffname != NULL)
15029 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15030 vim_free(ffname);
15031 }
15032 }
15033#else
15034 rettv->vval.v_string = NULL;
15035#endif
15036}
15037
15038/*
15039 * "undotree()" function
15040 */
15041 static void
15042f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15043{
15044 if (rettv_dict_alloc(rettv) == OK)
15045 {
15046 dict_T *dict = rettv->vval.v_dict;
15047 list_T *list;
15048
Bram Moolenaare0be1672018-07-08 16:50:37 +020015049 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15050 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15051 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15052 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15053 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15054 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015055
15056 list = list_alloc();
15057 if (list != NULL)
15058 {
15059 u_eval_tree(curbuf->b_u_oldhead, list);
15060 dict_add_list(dict, "entries", list);
15061 }
15062 }
15063}
15064
15065/*
15066 * "values(dict)" function
15067 */
15068 static void
15069f_values(typval_T *argvars, typval_T *rettv)
15070{
15071 dict_list(argvars, rettv, 1);
15072}
15073
15074/*
15075 * "virtcol(string)" function
15076 */
15077 static void
15078f_virtcol(typval_T *argvars, typval_T *rettv)
15079{
15080 colnr_T vcol = 0;
15081 pos_T *fp;
15082 int fnum = curbuf->b_fnum;
15083
15084 fp = var2fpos(&argvars[0], FALSE, &fnum);
15085 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15086 && fnum == curbuf->b_fnum)
15087 {
15088 getvvcol(curwin, fp, NULL, NULL, &vcol);
15089 ++vcol;
15090 }
15091
15092 rettv->vval.v_number = vcol;
15093}
15094
15095/*
15096 * "visualmode()" function
15097 */
15098 static void
15099f_visualmode(typval_T *argvars, typval_T *rettv)
15100{
15101 char_u str[2];
15102
15103 rettv->v_type = VAR_STRING;
15104 str[0] = curbuf->b_visual_mode_eval;
15105 str[1] = NUL;
15106 rettv->vval.v_string = vim_strsave(str);
15107
15108 /* A non-zero number or non-empty string argument: reset mode. */
15109 if (non_zero_arg(&argvars[0]))
15110 curbuf->b_visual_mode_eval = NUL;
15111}
15112
15113/*
15114 * "wildmenumode()" function
15115 */
15116 static void
15117f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15118{
15119#ifdef FEAT_WILDMENU
15120 if (wild_menu_showing)
15121 rettv->vval.v_number = 1;
15122#endif
15123}
15124
15125/*
15126 * "winbufnr(nr)" function
15127 */
15128 static void
15129f_winbufnr(typval_T *argvars, typval_T *rettv)
15130{
15131 win_T *wp;
15132
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015133 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015134 if (wp == NULL)
15135 rettv->vval.v_number = -1;
15136 else
15137 rettv->vval.v_number = wp->w_buffer->b_fnum;
15138}
15139
15140/*
15141 * "wincol()" function
15142 */
15143 static void
15144f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15145{
15146 validate_cursor();
15147 rettv->vval.v_number = curwin->w_wcol + 1;
15148}
15149
15150/*
15151 * "winheight(nr)" function
15152 */
15153 static void
15154f_winheight(typval_T *argvars, typval_T *rettv)
15155{
15156 win_T *wp;
15157
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015158 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015159 if (wp == NULL)
15160 rettv->vval.v_number = -1;
15161 else
15162 rettv->vval.v_number = wp->w_height;
15163}
15164
15165/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015166 * "winlayout()" function
15167 */
15168 static void
15169f_winlayout(typval_T *argvars, typval_T *rettv)
15170{
15171 tabpage_T *tp;
15172
15173 if (rettv_list_alloc(rettv) != OK)
15174 return;
15175
15176 if (argvars[0].v_type == VAR_UNKNOWN)
15177 tp = curtab;
15178 else
15179 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015180 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015181 if (tp == NULL)
15182 return;
15183 }
15184
15185 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15186}
15187
15188/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015189 * "winline()" function
15190 */
15191 static void
15192f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15193{
15194 validate_cursor();
15195 rettv->vval.v_number = curwin->w_wrow + 1;
15196}
15197
15198/*
15199 * "winnr()" function
15200 */
15201 static void
15202f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15203{
15204 int nr = 1;
15205
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015206 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015207 rettv->vval.v_number = nr;
15208}
15209
15210/*
15211 * "winrestcmd()" function
15212 */
15213 static void
15214f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15215{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015216 win_T *wp;
15217 int winnr = 1;
15218 garray_T ga;
15219 char_u buf[50];
15220
15221 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015222 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015223 {
15224 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15225 ga_concat(&ga, buf);
15226 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15227 ga_concat(&ga, buf);
15228 ++winnr;
15229 }
15230 ga_append(&ga, NUL);
15231
15232 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015233 rettv->v_type = VAR_STRING;
15234}
15235
15236/*
15237 * "winrestview()" function
15238 */
15239 static void
15240f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15241{
15242 dict_T *dict;
15243
15244 if (argvars[0].v_type != VAR_DICT
15245 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015246 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015247 else
15248 {
15249 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015250 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015251 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015252 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015253 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015254 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015255 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15256 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015257 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015258 curwin->w_set_curswant = FALSE;
15259 }
15260
15261 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015262 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015263#ifdef FEAT_DIFF
15264 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015265 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015266#endif
15267 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015268 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015269 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015270 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015271
15272 check_cursor();
15273 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015274 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015275 changed_window_setting();
15276
15277 if (curwin->w_topline <= 0)
15278 curwin->w_topline = 1;
15279 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15280 curwin->w_topline = curbuf->b_ml.ml_line_count;
15281#ifdef FEAT_DIFF
15282 check_topfill(curwin, TRUE);
15283#endif
15284 }
15285}
15286
15287/*
15288 * "winsaveview()" function
15289 */
15290 static void
15291f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15292{
15293 dict_T *dict;
15294
15295 if (rettv_dict_alloc(rettv) == FAIL)
15296 return;
15297 dict = rettv->vval.v_dict;
15298
Bram Moolenaare0be1672018-07-08 16:50:37 +020015299 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15300 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015301 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015302 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015303 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015304
Bram Moolenaare0be1672018-07-08 16:50:37 +020015305 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015306#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015307 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015308#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015309 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15310 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015311}
15312
15313/*
15314 * "winwidth(nr)" function
15315 */
15316 static void
15317f_winwidth(typval_T *argvars, typval_T *rettv)
15318{
15319 win_T *wp;
15320
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015321 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015322 if (wp == NULL)
15323 rettv->vval.v_number = -1;
15324 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015325 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015326}
15327
15328/*
15329 * "wordcount()" function
15330 */
15331 static void
15332f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15333{
15334 if (rettv_dict_alloc(rettv) == FAIL)
15335 return;
15336 cursor_pos_info(rettv->vval.v_dict);
15337}
15338
15339/*
15340 * "writefile()" function
15341 */
15342 static void
15343f_writefile(typval_T *argvars, typval_T *rettv)
15344{
15345 int binary = FALSE;
15346 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015347#ifdef HAVE_FSYNC
15348 int do_fsync = p_fs;
15349#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015350 char_u *fname;
15351 FILE *fd;
15352 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015353 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015354 list_T *list = NULL;
15355 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015356
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015357 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015358 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015359 return;
15360
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015361 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015362 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015363 list = argvars[0].vval.v_list;
15364 if (list == NULL)
15365 return;
15366 for (li = list->lv_first; li != NULL; li = li->li_next)
15367 if (tv_get_string_chk(&li->li_tv) == NULL)
15368 return;
15369 }
15370 else if (argvars[0].v_type == VAR_BLOB)
15371 {
15372 blob = argvars[0].vval.v_blob;
15373 if (blob == NULL)
15374 return;
15375 }
15376 else
15377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015378 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015379 return;
15380 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015381
15382 if (argvars[2].v_type != VAR_UNKNOWN)
15383 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015384 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015385
15386 if (arg2 == NULL)
15387 return;
15388 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015389 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015390 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015391 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015392#ifdef HAVE_FSYNC
15393 if (vim_strchr(arg2, 's') != NULL)
15394 do_fsync = TRUE;
15395 else if (vim_strchr(arg2, 'S') != NULL)
15396 do_fsync = FALSE;
15397#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015398 }
15399
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015400 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015401 if (fname == NULL)
15402 return;
15403
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015404 /* Always open the file in binary mode, library functions have a mind of
15405 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015406 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15407 append ? APPENDBIN : WRITEBIN)) == NULL)
15408 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015409 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015410 ret = -1;
15411 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015412 else if (blob)
15413 {
15414 if (write_blob(fd, blob) == FAIL)
15415 ret = -1;
15416#ifdef HAVE_FSYNC
15417 else if (do_fsync)
15418 // Ignore the error, the user wouldn't know what to do about it.
15419 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015420 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015421#endif
15422 fclose(fd);
15423 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015424 else
15425 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015426 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015427 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015428#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015429 else if (do_fsync)
15430 /* Ignore the error, the user wouldn't know what to do about it.
15431 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015432 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015433#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015434 fclose(fd);
15435 }
15436
15437 rettv->vval.v_number = ret;
15438}
15439
15440/*
15441 * "xor(expr, expr)" function
15442 */
15443 static void
15444f_xor(typval_T *argvars, typval_T *rettv)
15445{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015446 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15447 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015448}
15449
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015450#endif /* FEAT_EVAL */