blob: ca412f7549311d59ec4547791562853a54e7d8da [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
66static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010067# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010068static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010069# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010070#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_browse(typval_T *argvars, typval_T *rettv);
72static void f_browsedir(typval_T *argvars, typval_T *rettv);
73static void f_bufexists(typval_T *argvars, typval_T *rettv);
74static void f_buflisted(typval_T *argvars, typval_T *rettv);
75static void f_bufloaded(typval_T *argvars, typval_T *rettv);
76static void f_bufname(typval_T *argvars, typval_T *rettv);
77static void f_bufnr(typval_T *argvars, typval_T *rettv);
78static void f_bufwinid(typval_T *argvars, typval_T *rettv);
79static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
80static void f_byte2line(typval_T *argvars, typval_T *rettv);
81static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
82static void f_byteidx(typval_T *argvars, typval_T *rettv);
83static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
84static void f_call(typval_T *argvars, typval_T *rettv);
85#ifdef FEAT_FLOAT
86static void f_ceil(typval_T *argvars, typval_T *rettv);
87#endif
88#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010089static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020091static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
93static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
94static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
95static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
96static void f_ch_info(typval_T *argvars, typval_T *rettv);
97static void f_ch_log(typval_T *argvars, typval_T *rettv);
98static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
99static void f_ch_open(typval_T *argvars, typval_T *rettv);
100static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100101static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
103static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
104static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
105static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
106static void f_ch_status(typval_T *argvars, typval_T *rettv);
107#endif
108static void f_changenr(typval_T *argvars, typval_T *rettv);
109static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200110static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200111static void f_cindent(typval_T *argvars, typval_T *rettv);
112static void f_clearmatches(typval_T *argvars, typval_T *rettv);
113static void f_col(typval_T *argvars, typval_T *rettv);
114#if defined(FEAT_INS_EXPAND)
115static void f_complete(typval_T *argvars, typval_T *rettv);
116static void f_complete_add(typval_T *argvars, typval_T *rettv);
117static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +0100118static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119#endif
120static void f_confirm(typval_T *argvars, typval_T *rettv);
121static void f_copy(typval_T *argvars, typval_T *rettv);
122#ifdef FEAT_FLOAT
123static void f_cos(typval_T *argvars, typval_T *rettv);
124static void f_cosh(typval_T *argvars, typval_T *rettv);
125#endif
126static void f_count(typval_T *argvars, typval_T *rettv);
127static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
128static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100129#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200130static void f_debugbreak(typval_T *argvars, typval_T *rettv);
131#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200132static void f_deepcopy(typval_T *argvars, typval_T *rettv);
133static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200134static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200135static void f_did_filetype(typval_T *argvars, typval_T *rettv);
136static void f_diff_filler(typval_T *argvars, typval_T *rettv);
137static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
138static void f_empty(typval_T *argvars, typval_T *rettv);
139static void f_escape(typval_T *argvars, typval_T *rettv);
140static void f_eval(typval_T *argvars, typval_T *rettv);
141static void f_eventhandler(typval_T *argvars, typval_T *rettv);
142static void f_executable(typval_T *argvars, typval_T *rettv);
143static void f_execute(typval_T *argvars, typval_T *rettv);
144static void f_exepath(typval_T *argvars, typval_T *rettv);
145static void f_exists(typval_T *argvars, typval_T *rettv);
146#ifdef FEAT_FLOAT
147static void f_exp(typval_T *argvars, typval_T *rettv);
148#endif
149static void f_expand(typval_T *argvars, typval_T *rettv);
150static void f_extend(typval_T *argvars, typval_T *rettv);
151static void f_feedkeys(typval_T *argvars, typval_T *rettv);
152static void f_filereadable(typval_T *argvars, typval_T *rettv);
153static void f_filewritable(typval_T *argvars, typval_T *rettv);
154static void f_filter(typval_T *argvars, typval_T *rettv);
155static void f_finddir(typval_T *argvars, typval_T *rettv);
156static void f_findfile(typval_T *argvars, typval_T *rettv);
157#ifdef FEAT_FLOAT
158static void f_float2nr(typval_T *argvars, typval_T *rettv);
159static void f_floor(typval_T *argvars, typval_T *rettv);
160static void f_fmod(typval_T *argvars, typval_T *rettv);
161#endif
162static void f_fnameescape(typval_T *argvars, typval_T *rettv);
163static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
164static void f_foldclosed(typval_T *argvars, typval_T *rettv);
165static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
166static void f_foldlevel(typval_T *argvars, typval_T *rettv);
167static void f_foldtext(typval_T *argvars, typval_T *rettv);
168static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
169static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200170static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200171static void f_function(typval_T *argvars, typval_T *rettv);
172static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
173static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200174static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200175static void f_getbufline(typval_T *argvars, typval_T *rettv);
176static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100177static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200178static void f_getchar(typval_T *argvars, typval_T *rettv);
179static void f_getcharmod(typval_T *argvars, typval_T *rettv);
180static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
181static void f_getcmdline(typval_T *argvars, typval_T *rettv);
182#if defined(FEAT_CMDL_COMPL)
183static void f_getcompletion(typval_T *argvars, typval_T *rettv);
184#endif
185static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
186static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
187static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
188static void f_getcwd(typval_T *argvars, typval_T *rettv);
189static void f_getfontname(typval_T *argvars, typval_T *rettv);
190static void f_getfperm(typval_T *argvars, typval_T *rettv);
191static void f_getfsize(typval_T *argvars, typval_T *rettv);
192static void f_getftime(typval_T *argvars, typval_T *rettv);
193static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100194static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200195static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200196static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_getmatches(typval_T *argvars, typval_T *rettv);
198static void f_getpid(typval_T *argvars, typval_T *rettv);
199static void f_getcurpos(typval_T *argvars, typval_T *rettv);
200static void f_getpos(typval_T *argvars, typval_T *rettv);
201static void f_getqflist(typval_T *argvars, typval_T *rettv);
202static void f_getreg(typval_T *argvars, typval_T *rettv);
203static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200204static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200205static void f_gettabvar(typval_T *argvars, typval_T *rettv);
206static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100207static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200208static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100209static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200210static void f_getwinposx(typval_T *argvars, typval_T *rettv);
211static void f_getwinposy(typval_T *argvars, typval_T *rettv);
212static void f_getwinvar(typval_T *argvars, typval_T *rettv);
213static void f_glob(typval_T *argvars, typval_T *rettv);
214static void f_globpath(typval_T *argvars, typval_T *rettv);
215static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
216static void f_has(typval_T *argvars, typval_T *rettv);
217static void f_has_key(typval_T *argvars, typval_T *rettv);
218static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
219static void f_hasmapto(typval_T *argvars, typval_T *rettv);
220static void f_histadd(typval_T *argvars, typval_T *rettv);
221static void f_histdel(typval_T *argvars, typval_T *rettv);
222static void f_histget(typval_T *argvars, typval_T *rettv);
223static void f_histnr(typval_T *argvars, typval_T *rettv);
224static void f_hlID(typval_T *argvars, typval_T *rettv);
225static void f_hlexists(typval_T *argvars, typval_T *rettv);
226static void f_hostname(typval_T *argvars, typval_T *rettv);
227static void f_iconv(typval_T *argvars, typval_T *rettv);
228static void f_indent(typval_T *argvars, typval_T *rettv);
229static void f_index(typval_T *argvars, typval_T *rettv);
230static void f_input(typval_T *argvars, typval_T *rettv);
231static void f_inputdialog(typval_T *argvars, typval_T *rettv);
232static void f_inputlist(typval_T *argvars, typval_T *rettv);
233static void f_inputrestore(typval_T *argvars, typval_T *rettv);
234static void f_inputsave(typval_T *argvars, typval_T *rettv);
235static void f_inputsecret(typval_T *argvars, typval_T *rettv);
236static void f_insert(typval_T *argvars, typval_T *rettv);
237static void f_invert(typval_T *argvars, typval_T *rettv);
238static void f_isdirectory(typval_T *argvars, typval_T *rettv);
239static void f_islocked(typval_T *argvars, typval_T *rettv);
240#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200241static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200242static void f_isnan(typval_T *argvars, typval_T *rettv);
243#endif
244static void f_items(typval_T *argvars, typval_T *rettv);
245#ifdef FEAT_JOB_CHANNEL
246static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
247static void f_job_info(typval_T *argvars, typval_T *rettv);
248static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
249static void f_job_start(typval_T *argvars, typval_T *rettv);
250static void f_job_stop(typval_T *argvars, typval_T *rettv);
251static void f_job_status(typval_T *argvars, typval_T *rettv);
252#endif
253static void f_join(typval_T *argvars, typval_T *rettv);
254static void f_js_decode(typval_T *argvars, typval_T *rettv);
255static void f_js_encode(typval_T *argvars, typval_T *rettv);
256static void f_json_decode(typval_T *argvars, typval_T *rettv);
257static void f_json_encode(typval_T *argvars, typval_T *rettv);
258static void f_keys(typval_T *argvars, typval_T *rettv);
259static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
260static void f_len(typval_T *argvars, typval_T *rettv);
261static void f_libcall(typval_T *argvars, typval_T *rettv);
262static void f_libcallnr(typval_T *argvars, typval_T *rettv);
263static void f_line(typval_T *argvars, typval_T *rettv);
264static void f_line2byte(typval_T *argvars, typval_T *rettv);
265static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200266static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200267static void f_localtime(typval_T *argvars, typval_T *rettv);
268#ifdef FEAT_FLOAT
269static void f_log(typval_T *argvars, typval_T *rettv);
270static void f_log10(typval_T *argvars, typval_T *rettv);
271#endif
272#ifdef FEAT_LUA
273static void f_luaeval(typval_T *argvars, typval_T *rettv);
274#endif
275static void f_map(typval_T *argvars, typval_T *rettv);
276static void f_maparg(typval_T *argvars, typval_T *rettv);
277static void f_mapcheck(typval_T *argvars, typval_T *rettv);
278static void f_match(typval_T *argvars, typval_T *rettv);
279static void f_matchadd(typval_T *argvars, typval_T *rettv);
280static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
281static void f_matcharg(typval_T *argvars, typval_T *rettv);
282static void f_matchdelete(typval_T *argvars, typval_T *rettv);
283static void f_matchend(typval_T *argvars, typval_T *rettv);
284static void f_matchlist(typval_T *argvars, typval_T *rettv);
285static void f_matchstr(typval_T *argvars, typval_T *rettv);
286static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
287static void f_max(typval_T *argvars, typval_T *rettv);
288static void f_min(typval_T *argvars, typval_T *rettv);
289#ifdef vim_mkdir
290static void f_mkdir(typval_T *argvars, typval_T *rettv);
291#endif
292static void f_mode(typval_T *argvars, typval_T *rettv);
293#ifdef FEAT_MZSCHEME
294static void f_mzeval(typval_T *argvars, typval_T *rettv);
295#endif
296static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
297static void f_nr2char(typval_T *argvars, typval_T *rettv);
298static void f_or(typval_T *argvars, typval_T *rettv);
299static void f_pathshorten(typval_T *argvars, typval_T *rettv);
300#ifdef FEAT_PERL
301static void f_perleval(typval_T *argvars, typval_T *rettv);
302#endif
303#ifdef FEAT_FLOAT
304static void f_pow(typval_T *argvars, typval_T *rettv);
305#endif
306static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
307static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200308#ifdef FEAT_JOB_CHANNEL
309static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200310static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200311static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
312#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200313static void f_pumvisible(typval_T *argvars, typval_T *rettv);
314#ifdef FEAT_PYTHON3
315static void f_py3eval(typval_T *argvars, typval_T *rettv);
316#endif
317#ifdef FEAT_PYTHON
318static void f_pyeval(typval_T *argvars, typval_T *rettv);
319#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100320#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
321static void f_pyxeval(typval_T *argvars, typval_T *rettv);
322#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200323static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200324static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200325static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200326static void f_reg_executing(typval_T *argvars, typval_T *rettv);
327static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200328static void f_reltime(typval_T *argvars, typval_T *rettv);
329#ifdef FEAT_FLOAT
330static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
331#endif
332static void f_reltimestr(typval_T *argvars, typval_T *rettv);
333static void f_remote_expr(typval_T *argvars, typval_T *rettv);
334static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
335static void f_remote_peek(typval_T *argvars, typval_T *rettv);
336static void f_remote_read(typval_T *argvars, typval_T *rettv);
337static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100338static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200339static void f_remove(typval_T *argvars, typval_T *rettv);
340static void f_rename(typval_T *argvars, typval_T *rettv);
341static void f_repeat(typval_T *argvars, typval_T *rettv);
342static void f_resolve(typval_T *argvars, typval_T *rettv);
343static void f_reverse(typval_T *argvars, typval_T *rettv);
344#ifdef FEAT_FLOAT
345static void f_round(typval_T *argvars, typval_T *rettv);
346#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100347#ifdef FEAT_RUBY
348static void f_rubyeval(typval_T *argvars, typval_T *rettv);
349#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200350static void f_screenattr(typval_T *argvars, typval_T *rettv);
351static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100352static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200353static void f_screencol(typval_T *argvars, typval_T *rettv);
354static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100355static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200356static void f_search(typval_T *argvars, typval_T *rettv);
357static void f_searchdecl(typval_T *argvars, typval_T *rettv);
358static void f_searchpair(typval_T *argvars, typval_T *rettv);
359static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
360static void f_searchpos(typval_T *argvars, typval_T *rettv);
361static void f_server2client(typval_T *argvars, typval_T *rettv);
362static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200363static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200364static void f_setbufvar(typval_T *argvars, typval_T *rettv);
365static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
366static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
367static void f_setfperm(typval_T *argvars, typval_T *rettv);
368static void f_setline(typval_T *argvars, typval_T *rettv);
369static void f_setloclist(typval_T *argvars, typval_T *rettv);
370static void f_setmatches(typval_T *argvars, typval_T *rettv);
371static void f_setpos(typval_T *argvars, typval_T *rettv);
372static void f_setqflist(typval_T *argvars, typval_T *rettv);
373static void f_setreg(typval_T *argvars, typval_T *rettv);
374static void f_settabvar(typval_T *argvars, typval_T *rettv);
375static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100376static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200377static void f_setwinvar(typval_T *argvars, typval_T *rettv);
378#ifdef FEAT_CRYPT
379static void f_sha256(typval_T *argvars, typval_T *rettv);
380#endif /* FEAT_CRYPT */
381static void f_shellescape(typval_T *argvars, typval_T *rettv);
382static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100383#ifdef FEAT_SIGNS
384static void f_sign_define(typval_T *argvars, typval_T *rettv);
385static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
386static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100387static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100388static void f_sign_place(typval_T *argvars, typval_T *rettv);
389static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
390static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
391#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200392static void f_simplify(typval_T *argvars, typval_T *rettv);
393#ifdef FEAT_FLOAT
394static void f_sin(typval_T *argvars, typval_T *rettv);
395static void f_sinh(typval_T *argvars, typval_T *rettv);
396#endif
397static void f_sort(typval_T *argvars, typval_T *rettv);
398static void f_soundfold(typval_T *argvars, typval_T *rettv);
399static void f_spellbadword(typval_T *argvars, typval_T *rettv);
400static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
401static void f_split(typval_T *argvars, typval_T *rettv);
402#ifdef FEAT_FLOAT
403static void f_sqrt(typval_T *argvars, typval_T *rettv);
404static void f_str2float(typval_T *argvars, typval_T *rettv);
405#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200406static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200407static void f_str2nr(typval_T *argvars, typval_T *rettv);
408static void f_strchars(typval_T *argvars, typval_T *rettv);
409#ifdef HAVE_STRFTIME
410static void f_strftime(typval_T *argvars, typval_T *rettv);
411#endif
412static void f_strgetchar(typval_T *argvars, typval_T *rettv);
413static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200414static void f_strlen(typval_T *argvars, typval_T *rettv);
415static void f_strcharpart(typval_T *argvars, typval_T *rettv);
416static void f_strpart(typval_T *argvars, typval_T *rettv);
417static void f_strridx(typval_T *argvars, typval_T *rettv);
418static void f_strtrans(typval_T *argvars, typval_T *rettv);
419static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
420static void f_strwidth(typval_T *argvars, typval_T *rettv);
421static void f_submatch(typval_T *argvars, typval_T *rettv);
422static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200423static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200424static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200425static void f_synID(typval_T *argvars, typval_T *rettv);
426static void f_synIDattr(typval_T *argvars, typval_T *rettv);
427static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
428static void f_synstack(typval_T *argvars, typval_T *rettv);
429static void f_synconcealed(typval_T *argvars, typval_T *rettv);
430static void f_system(typval_T *argvars, typval_T *rettv);
431static void f_systemlist(typval_T *argvars, typval_T *rettv);
432static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
433static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
434static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
435static void f_taglist(typval_T *argvars, typval_T *rettv);
436static void f_tagfiles(typval_T *argvars, typval_T *rettv);
437static void f_tempname(typval_T *argvars, typval_T *rettv);
438static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
439static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200440static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200441static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100442static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100443static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200444static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100445static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100446static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200447#ifdef FEAT_JOB_CHANNEL
448static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
449#endif
450static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
451#ifdef FEAT_JOB_CHANNEL
452static void f_test_null_job(typval_T *argvars, typval_T *rettv);
453#endif
454static void f_test_null_list(typval_T *argvars, typval_T *rettv);
455static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
456static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200457#ifdef FEAT_GUI
458static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
459#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200460#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200461static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200462#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200463static void f_test_settime(typval_T *argvars, typval_T *rettv);
464#ifdef FEAT_FLOAT
465static void f_tan(typval_T *argvars, typval_T *rettv);
466static void f_tanh(typval_T *argvars, typval_T *rettv);
467#endif
468#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200469static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200470static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200471static void f_timer_start(typval_T *argvars, typval_T *rettv);
472static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200473static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200474#endif
475static void f_tolower(typval_T *argvars, typval_T *rettv);
476static void f_toupper(typval_T *argvars, typval_T *rettv);
477static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100478static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200479#ifdef FEAT_FLOAT
480static void f_trunc(typval_T *argvars, typval_T *rettv);
481#endif
482static void f_type(typval_T *argvars, typval_T *rettv);
483static void f_undofile(typval_T *argvars, typval_T *rettv);
484static void f_undotree(typval_T *argvars, typval_T *rettv);
485static void f_uniq(typval_T *argvars, typval_T *rettv);
486static void f_values(typval_T *argvars, typval_T *rettv);
487static void f_virtcol(typval_T *argvars, typval_T *rettv);
488static void f_visualmode(typval_T *argvars, typval_T *rettv);
489static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
490static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
491static void f_win_getid(typval_T *argvars, typval_T *rettv);
492static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
493static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
494static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100495static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200496static void f_winbufnr(typval_T *argvars, typval_T *rettv);
497static void f_wincol(typval_T *argvars, typval_T *rettv);
498static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200499static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200500static void f_winline(typval_T *argvars, typval_T *rettv);
501static void f_winnr(typval_T *argvars, typval_T *rettv);
502static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
503static void f_winrestview(typval_T *argvars, typval_T *rettv);
504static void f_winsaveview(typval_T *argvars, typval_T *rettv);
505static void f_winwidth(typval_T *argvars, typval_T *rettv);
506static void f_writefile(typval_T *argvars, typval_T *rettv);
507static void f_wordcount(typval_T *argvars, typval_T *rettv);
508static void f_xor(typval_T *argvars, typval_T *rettv);
509
510/*
511 * Array with names and number of arguments of all internal functions
512 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
513 */
514static struct fst
515{
516 char *f_name; /* function name */
517 char f_min_argc; /* minimal number of arguments */
518 char f_max_argc; /* maximal number of arguments */
519 void (*f_func)(typval_T *args, typval_T *rvar);
520 /* implementation of function */
521} functions[] =
522{
523#ifdef FEAT_FLOAT
524 {"abs", 1, 1, f_abs},
525 {"acos", 1, 1, f_acos}, /* WJMc */
526#endif
527 {"add", 2, 2, f_add},
528 {"and", 2, 2, f_and},
529 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200530 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200531 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200532 {"argidx", 0, 0, f_argidx},
533 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200534 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200535#ifdef FEAT_FLOAT
536 {"asin", 1, 1, f_asin}, /* WJMc */
537#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100538 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200539 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100540 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200541 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200542 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200543 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100544 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200545 {"assert_match", 2, 3, f_assert_match},
546 {"assert_notequal", 2, 3, f_assert_notequal},
547 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100548 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200549 {"assert_true", 1, 2, f_assert_true},
550#ifdef FEAT_FLOAT
551 {"atan", 1, 1, f_atan},
552 {"atan2", 2, 2, f_atan2},
553#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100554#ifdef FEAT_BEVAL
555 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100556# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100557 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100558# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100559#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200560 {"browse", 4, 4, f_browse},
561 {"browsedir", 2, 2, f_browsedir},
562 {"bufexists", 1, 1, f_bufexists},
563 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
564 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
565 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
566 {"buflisted", 1, 1, f_buflisted},
567 {"bufloaded", 1, 1, f_bufloaded},
568 {"bufname", 1, 1, f_bufname},
569 {"bufnr", 1, 2, f_bufnr},
570 {"bufwinid", 1, 1, f_bufwinid},
571 {"bufwinnr", 1, 1, f_bufwinnr},
572 {"byte2line", 1, 1, f_byte2line},
573 {"byteidx", 2, 2, f_byteidx},
574 {"byteidxcomp", 2, 2, f_byteidxcomp},
575 {"call", 2, 3, f_call},
576#ifdef FEAT_FLOAT
577 {"ceil", 1, 1, f_ceil},
578#endif
579#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100580 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200581 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200582 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200583 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
584 {"ch_evalraw", 2, 3, f_ch_evalraw},
585 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
586 {"ch_getjob", 1, 1, f_ch_getjob},
587 {"ch_info", 1, 1, f_ch_info},
588 {"ch_log", 1, 2, f_ch_log},
589 {"ch_logfile", 1, 2, f_ch_logfile},
590 {"ch_open", 1, 2, f_ch_open},
591 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100592 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200593 {"ch_readraw", 1, 2, f_ch_readraw},
594 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
595 {"ch_sendraw", 2, 3, f_ch_sendraw},
596 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200597 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200598#endif
599 {"changenr", 0, 0, f_changenr},
600 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200601 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200602 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100603 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200604 {"col", 1, 1, f_col},
605#if defined(FEAT_INS_EXPAND)
606 {"complete", 2, 2, f_complete},
607 {"complete_add", 1, 1, f_complete_add},
608 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100609 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610#endif
611 {"confirm", 1, 4, f_confirm},
612 {"copy", 1, 1, f_copy},
613#ifdef FEAT_FLOAT
614 {"cos", 1, 1, f_cos},
615 {"cosh", 1, 1, f_cosh},
616#endif
617 {"count", 2, 4, f_count},
618 {"cscope_connection",0,3, f_cscope_connection},
619 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100620#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200621 {"debugbreak", 1, 1, f_debugbreak},
622#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200623 {"deepcopy", 1, 2, f_deepcopy},
624 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200625 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200626 {"did_filetype", 0, 0, f_did_filetype},
627 {"diff_filler", 1, 1, f_diff_filler},
628 {"diff_hlID", 2, 2, f_diff_hlID},
629 {"empty", 1, 1, f_empty},
630 {"escape", 2, 2, f_escape},
631 {"eval", 1, 1, f_eval},
632 {"eventhandler", 0, 0, f_eventhandler},
633 {"executable", 1, 1, f_executable},
634 {"execute", 1, 2, f_execute},
635 {"exepath", 1, 1, f_exepath},
636 {"exists", 1, 1, f_exists},
637#ifdef FEAT_FLOAT
638 {"exp", 1, 1, f_exp},
639#endif
640 {"expand", 1, 3, f_expand},
641 {"extend", 2, 3, f_extend},
642 {"feedkeys", 1, 2, f_feedkeys},
643 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
644 {"filereadable", 1, 1, f_filereadable},
645 {"filewritable", 1, 1, f_filewritable},
646 {"filter", 2, 2, f_filter},
647 {"finddir", 1, 3, f_finddir},
648 {"findfile", 1, 3, f_findfile},
649#ifdef FEAT_FLOAT
650 {"float2nr", 1, 1, f_float2nr},
651 {"floor", 1, 1, f_floor},
652 {"fmod", 2, 2, f_fmod},
653#endif
654 {"fnameescape", 1, 1, f_fnameescape},
655 {"fnamemodify", 2, 2, f_fnamemodify},
656 {"foldclosed", 1, 1, f_foldclosed},
657 {"foldclosedend", 1, 1, f_foldclosedend},
658 {"foldlevel", 1, 1, f_foldlevel},
659 {"foldtext", 0, 0, f_foldtext},
660 {"foldtextresult", 1, 1, f_foldtextresult},
661 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200662 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200663 {"function", 1, 3, f_function},
664 {"garbagecollect", 0, 1, f_garbagecollect},
665 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200666 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200667 {"getbufline", 2, 3, f_getbufline},
668 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100669 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200670 {"getchar", 0, 1, f_getchar},
671 {"getcharmod", 0, 0, f_getcharmod},
672 {"getcharsearch", 0, 0, f_getcharsearch},
673 {"getcmdline", 0, 0, f_getcmdline},
674 {"getcmdpos", 0, 0, f_getcmdpos},
675 {"getcmdtype", 0, 0, f_getcmdtype},
676 {"getcmdwintype", 0, 0, f_getcmdwintype},
677#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200678 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200679#endif
680 {"getcurpos", 0, 0, f_getcurpos},
681 {"getcwd", 0, 2, f_getcwd},
682 {"getfontname", 0, 1, f_getfontname},
683 {"getfperm", 1, 1, f_getfperm},
684 {"getfsize", 1, 1, f_getfsize},
685 {"getftime", 1, 1, f_getftime},
686 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100687 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200688 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200689 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100690 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200691 {"getpid", 0, 0, f_getpid},
692 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200693 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200694 {"getreg", 0, 3, f_getreg},
695 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200696 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200697 {"gettabvar", 2, 3, f_gettabvar},
698 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100699 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200700 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100701 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702 {"getwinposx", 0, 0, f_getwinposx},
703 {"getwinposy", 0, 0, f_getwinposy},
704 {"getwinvar", 2, 3, f_getwinvar},
705 {"glob", 1, 4, f_glob},
706 {"glob2regpat", 1, 1, f_glob2regpat},
707 {"globpath", 2, 5, f_globpath},
708 {"has", 1, 1, f_has},
709 {"has_key", 2, 2, f_has_key},
710 {"haslocaldir", 0, 2, f_haslocaldir},
711 {"hasmapto", 1, 3, f_hasmapto},
712 {"highlightID", 1, 1, f_hlID}, /* obsolete */
713 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
714 {"histadd", 2, 2, f_histadd},
715 {"histdel", 1, 2, f_histdel},
716 {"histget", 1, 2, f_histget},
717 {"histnr", 1, 1, f_histnr},
718 {"hlID", 1, 1, f_hlID},
719 {"hlexists", 1, 1, f_hlexists},
720 {"hostname", 0, 0, f_hostname},
721 {"iconv", 3, 3, f_iconv},
722 {"indent", 1, 1, f_indent},
723 {"index", 2, 4, f_index},
724 {"input", 1, 3, f_input},
725 {"inputdialog", 1, 3, f_inputdialog},
726 {"inputlist", 1, 1, f_inputlist},
727 {"inputrestore", 0, 0, f_inputrestore},
728 {"inputsave", 0, 0, f_inputsave},
729 {"inputsecret", 1, 2, f_inputsecret},
730 {"insert", 2, 3, f_insert},
731 {"invert", 1, 1, f_invert},
732 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200733#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
734 {"isinf", 1, 1, f_isinf},
735#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200736 {"islocked", 1, 1, f_islocked},
737#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
738 {"isnan", 1, 1, f_isnan},
739#endif
740 {"items", 1, 1, f_items},
741#ifdef FEAT_JOB_CHANNEL
742 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200743 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200744 {"job_setoptions", 2, 2, f_job_setoptions},
745 {"job_start", 1, 2, f_job_start},
746 {"job_status", 1, 1, f_job_status},
747 {"job_stop", 1, 2, f_job_stop},
748#endif
749 {"join", 1, 2, f_join},
750 {"js_decode", 1, 1, f_js_decode},
751 {"js_encode", 1, 1, f_js_encode},
752 {"json_decode", 1, 1, f_json_decode},
753 {"json_encode", 1, 1, f_json_encode},
754 {"keys", 1, 1, f_keys},
755 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
756 {"len", 1, 1, f_len},
757 {"libcall", 3, 3, f_libcall},
758 {"libcallnr", 3, 3, f_libcallnr},
759 {"line", 1, 1, f_line},
760 {"line2byte", 1, 1, f_line2byte},
761 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200762 {"list2str", 1, 2, f_list2str},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200763 {"localtime", 0, 0, f_localtime},
764#ifdef FEAT_FLOAT
765 {"log", 1, 1, f_log},
766 {"log10", 1, 1, f_log10},
767#endif
768#ifdef FEAT_LUA
769 {"luaeval", 1, 2, f_luaeval},
770#endif
771 {"map", 2, 2, f_map},
772 {"maparg", 1, 4, f_maparg},
773 {"mapcheck", 1, 3, f_mapcheck},
774 {"match", 2, 4, f_match},
775 {"matchadd", 2, 5, f_matchadd},
776 {"matchaddpos", 2, 5, f_matchaddpos},
777 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100778 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200779 {"matchend", 2, 4, f_matchend},
780 {"matchlist", 2, 4, f_matchlist},
781 {"matchstr", 2, 4, f_matchstr},
782 {"matchstrpos", 2, 4, f_matchstrpos},
783 {"max", 1, 1, f_max},
784 {"min", 1, 1, f_min},
785#ifdef vim_mkdir
786 {"mkdir", 1, 3, f_mkdir},
787#endif
788 {"mode", 0, 1, f_mode},
789#ifdef FEAT_MZSCHEME
790 {"mzeval", 1, 1, f_mzeval},
791#endif
792 {"nextnonblank", 1, 1, f_nextnonblank},
793 {"nr2char", 1, 2, f_nr2char},
794 {"or", 2, 2, f_or},
795 {"pathshorten", 1, 1, f_pathshorten},
796#ifdef FEAT_PERL
797 {"perleval", 1, 1, f_perleval},
798#endif
799#ifdef FEAT_FLOAT
800 {"pow", 2, 2, f_pow},
801#endif
802 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100803 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200804#ifdef FEAT_JOB_CHANNEL
805 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200806 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200807 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
808#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100809#ifdef FEAT_TEXT_PROP
810 {"prop_add", 3, 3, f_prop_add},
811 {"prop_clear", 1, 3, f_prop_clear},
812 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100813 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100814 {"prop_type_add", 2, 2, f_prop_type_add},
815 {"prop_type_change", 2, 2, f_prop_type_change},
816 {"prop_type_delete", 1, 2, f_prop_type_delete},
817 {"prop_type_get", 1, 2, f_prop_type_get},
818 {"prop_type_list", 0, 1, f_prop_type_list},
819#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200820 {"pumvisible", 0, 0, f_pumvisible},
821#ifdef FEAT_PYTHON3
822 {"py3eval", 1, 1, f_py3eval},
823#endif
824#ifdef FEAT_PYTHON
825 {"pyeval", 1, 1, f_pyeval},
826#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100827#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
828 {"pyxeval", 1, 1, f_pyxeval},
829#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200830 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200831 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200832 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200833 {"reg_executing", 0, 0, f_reg_executing},
834 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200835 {"reltime", 0, 2, f_reltime},
836#ifdef FEAT_FLOAT
837 {"reltimefloat", 1, 1, f_reltimefloat},
838#endif
839 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100840 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200841 {"remote_foreground", 1, 1, f_remote_foreground},
842 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100843 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200844 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100845 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200846 {"remove", 2, 3, f_remove},
847 {"rename", 2, 2, f_rename},
848 {"repeat", 2, 2, f_repeat},
849 {"resolve", 1, 1, f_resolve},
850 {"reverse", 1, 1, f_reverse},
851#ifdef FEAT_FLOAT
852 {"round", 1, 1, f_round},
853#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100854#ifdef FEAT_RUBY
855 {"rubyeval", 1, 1, f_rubyeval},
856#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200857 {"screenattr", 2, 2, f_screenattr},
858 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100859 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200860 {"screencol", 0, 0, f_screencol},
861 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100862 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200863 {"search", 1, 4, f_search},
864 {"searchdecl", 1, 3, f_searchdecl},
865 {"searchpair", 3, 7, f_searchpair},
866 {"searchpairpos", 3, 7, f_searchpairpos},
867 {"searchpos", 1, 4, f_searchpos},
868 {"server2client", 2, 2, f_server2client},
869 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200870 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200871 {"setbufvar", 3, 3, f_setbufvar},
872 {"setcharsearch", 1, 1, f_setcharsearch},
873 {"setcmdpos", 1, 1, f_setcmdpos},
874 {"setfperm", 2, 2, f_setfperm},
875 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200876 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100877 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200878 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200879 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200880 {"setreg", 2, 3, f_setreg},
881 {"settabvar", 3, 3, f_settabvar},
882 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100883 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200884 {"setwinvar", 3, 3, f_setwinvar},
885#ifdef FEAT_CRYPT
886 {"sha256", 1, 1, f_sha256},
887#endif
888 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100889 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100890#ifdef FEAT_SIGNS
891 {"sign_define", 1, 2, f_sign_define},
892 {"sign_getdefined", 0, 1, f_sign_getdefined},
893 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100894 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100895 {"sign_place", 4, 5, f_sign_place},
896 {"sign_undefine", 0, 1, f_sign_undefine},
897 {"sign_unplace", 1, 2, f_sign_unplace},
898#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200899 {"simplify", 1, 1, f_simplify},
900#ifdef FEAT_FLOAT
901 {"sin", 1, 1, f_sin},
902 {"sinh", 1, 1, f_sinh},
903#endif
904 {"sort", 1, 3, f_sort},
905 {"soundfold", 1, 1, f_soundfold},
906 {"spellbadword", 0, 1, f_spellbadword},
907 {"spellsuggest", 1, 3, f_spellsuggest},
908 {"split", 1, 3, f_split},
909#ifdef FEAT_FLOAT
910 {"sqrt", 1, 1, f_sqrt},
911 {"str2float", 1, 1, f_str2float},
912#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200913 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200914 {"str2nr", 1, 2, f_str2nr},
915 {"strcharpart", 2, 3, f_strcharpart},
916 {"strchars", 1, 2, f_strchars},
917 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
918#ifdef HAVE_STRFTIME
919 {"strftime", 1, 2, f_strftime},
920#endif
921 {"strgetchar", 2, 2, f_strgetchar},
922 {"stridx", 2, 3, f_stridx},
923 {"string", 1, 1, f_string},
924 {"strlen", 1, 1, f_strlen},
925 {"strpart", 2, 3, f_strpart},
926 {"strridx", 2, 3, f_strridx},
927 {"strtrans", 1, 1, f_strtrans},
928 {"strwidth", 1, 1, f_strwidth},
929 {"submatch", 1, 2, f_submatch},
930 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200931 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200932 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200933 {"synID", 3, 3, f_synID},
934 {"synIDattr", 2, 3, f_synIDattr},
935 {"synIDtrans", 1, 1, f_synIDtrans},
936 {"synconcealed", 2, 2, f_synconcealed},
937 {"synstack", 2, 2, f_synstack},
938 {"system", 1, 2, f_system},
939 {"systemlist", 1, 2, f_systemlist},
940 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
941 {"tabpagenr", 0, 1, f_tabpagenr},
942 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
943 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100944 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200945#ifdef FEAT_FLOAT
946 {"tan", 1, 1, f_tan},
947 {"tanh", 1, 1, f_tanh},
948#endif
949 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200950#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100951 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
952 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100953 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200954 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200955# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
956 {"term_getansicolors", 1, 1, f_term_getansicolors},
957# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200958 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200959 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200960 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200961 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200962 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200963 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200964 {"term_getstatus", 1, 1, f_term_getstatus},
965 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200966 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200967 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200968 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200969 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200970# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
971 {"term_setansicolors", 2, 2, f_term_setansicolors},
972# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100973 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100974 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200975 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200976 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200977 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200978#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200979 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
980 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200981 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200982 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100983 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100984 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200985#ifdef FEAT_JOB_CHANNEL
986 {"test_null_channel", 0, 0, f_test_null_channel},
987#endif
988 {"test_null_dict", 0, 0, f_test_null_dict},
989#ifdef FEAT_JOB_CHANNEL
990 {"test_null_job", 0, 0, f_test_null_job},
991#endif
992 {"test_null_list", 0, 0, f_test_null_list},
993 {"test_null_partial", 0, 0, f_test_null_partial},
994 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200995 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100996 {"test_override", 2, 2, f_test_override},
997 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200998#ifdef FEAT_GUI
999 {"test_scrollbar", 3, 3, f_test_scrollbar},
1000#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001001#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001002 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001003#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001004 {"test_settime", 1, 1, f_test_settime},
1005#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001006 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001007 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001008 {"timer_start", 2, 3, f_timer_start},
1009 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001010 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001011#endif
1012 {"tolower", 1, 1, f_tolower},
1013 {"toupper", 1, 1, f_toupper},
1014 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001015 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001016#ifdef FEAT_FLOAT
1017 {"trunc", 1, 1, f_trunc},
1018#endif
1019 {"type", 1, 1, f_type},
1020 {"undofile", 1, 1, f_undofile},
1021 {"undotree", 0, 0, f_undotree},
1022 {"uniq", 1, 3, f_uniq},
1023 {"values", 1, 1, f_values},
1024 {"virtcol", 1, 1, f_virtcol},
1025 {"visualmode", 0, 1, f_visualmode},
1026 {"wildmenumode", 0, 0, f_wildmenumode},
1027 {"win_findbuf", 1, 1, f_win_findbuf},
1028 {"win_getid", 0, 2, f_win_getid},
1029 {"win_gotoid", 1, 1, f_win_gotoid},
1030 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1031 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001032 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001033 {"winbufnr", 1, 1, f_winbufnr},
1034 {"wincol", 0, 0, f_wincol},
1035 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001036 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001037 {"winline", 0, 0, f_winline},
1038 {"winnr", 0, 1, f_winnr},
1039 {"winrestcmd", 0, 0, f_winrestcmd},
1040 {"winrestview", 1, 1, f_winrestview},
1041 {"winsaveview", 0, 0, f_winsaveview},
1042 {"winwidth", 1, 1, f_winwidth},
1043 {"wordcount", 0, 0, f_wordcount},
1044 {"writefile", 2, 3, f_writefile},
1045 {"xor", 2, 2, f_xor},
1046};
1047
1048#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1049
1050/*
1051 * Function given to ExpandGeneric() to obtain the list of internal
1052 * or user defined function names.
1053 */
1054 char_u *
1055get_function_name(expand_T *xp, int idx)
1056{
1057 static int intidx = -1;
1058 char_u *name;
1059
1060 if (idx == 0)
1061 intidx = -1;
1062 if (intidx < 0)
1063 {
1064 name = get_user_func_name(xp, idx);
1065 if (name != NULL)
1066 return name;
1067 }
1068 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1069 {
1070 STRCPY(IObuff, functions[intidx].f_name);
1071 STRCAT(IObuff, "(");
1072 if (functions[intidx].f_max_argc == 0)
1073 STRCAT(IObuff, ")");
1074 return IObuff;
1075 }
1076
1077 return NULL;
1078}
1079
1080/*
1081 * Function given to ExpandGeneric() to obtain the list of internal or
1082 * user defined variable or function names.
1083 */
1084 char_u *
1085get_expr_name(expand_T *xp, int idx)
1086{
1087 static int intidx = -1;
1088 char_u *name;
1089
1090 if (idx == 0)
1091 intidx = -1;
1092 if (intidx < 0)
1093 {
1094 name = get_function_name(xp, idx);
1095 if (name != NULL)
1096 return name;
1097 }
1098 return get_user_var_name(xp, ++intidx);
1099}
1100
1101#endif /* FEAT_CMDL_COMPL */
1102
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001103/*
1104 * Find internal function in table above.
1105 * Return index, or -1 if not found
1106 */
1107 int
1108find_internal_func(
1109 char_u *name) /* name of the function */
1110{
1111 int first = 0;
1112 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1113 int cmp;
1114 int x;
1115
1116 /*
1117 * Find the function name in the table. Binary search.
1118 */
1119 while (first <= last)
1120 {
1121 x = first + ((unsigned)(last - first) >> 1);
1122 cmp = STRCMP(name, functions[x].f_name);
1123 if (cmp < 0)
1124 last = x - 1;
1125 else if (cmp > 0)
1126 first = x + 1;
1127 else
1128 return x;
1129 }
1130 return -1;
1131}
1132
1133 int
1134call_internal_func(
1135 char_u *name,
1136 int argcount,
1137 typval_T *argvars,
1138 typval_T *rettv)
1139{
1140 int i;
1141
1142 i = find_internal_func(name);
1143 if (i < 0)
1144 return ERROR_UNKNOWN;
1145 if (argcount < functions[i].f_min_argc)
1146 return ERROR_TOOFEW;
1147 if (argcount > functions[i].f_max_argc)
1148 return ERROR_TOOMANY;
1149 argvars[argcount].v_type = VAR_UNKNOWN;
1150 functions[i].f_func(argvars, rettv);
1151 return ERROR_NONE;
1152}
1153
1154/*
1155 * Return TRUE for a non-zero Number and a non-empty String.
1156 */
1157 static int
1158non_zero_arg(typval_T *argvars)
1159{
1160 return ((argvars[0].v_type == VAR_NUMBER
1161 && argvars[0].vval.v_number != 0)
1162 || (argvars[0].v_type == VAR_SPECIAL
1163 && argvars[0].vval.v_number == VVAL_TRUE)
1164 || (argvars[0].v_type == VAR_STRING
1165 && argvars[0].vval.v_string != NULL
1166 && *argvars[0].vval.v_string != NUL));
1167}
1168
1169/*
1170 * Get the lnum from the first argument.
1171 * Also accepts ".", "$", etc., but that only works for the current buffer.
1172 * Returns -1 on error.
1173 */
1174 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001175tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001176{
1177 typval_T rettv;
1178 linenr_T lnum;
1179
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001180 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001181 if (lnum == 0) /* no valid number, try using line() */
1182 {
1183 rettv.v_type = VAR_NUMBER;
1184 f_line(argvars, &rettv);
1185 lnum = (linenr_T)rettv.vval.v_number;
1186 clear_tv(&rettv);
1187 }
1188 return lnum;
1189}
1190
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001191/*
1192 * Get the lnum from the first argument.
1193 * Also accepts "$", then "buf" is used.
1194 * Returns 0 on error.
1195 */
1196 static linenr_T
1197tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1198{
1199 if (argvars[0].v_type == VAR_STRING
1200 && argvars[0].vval.v_string != NULL
1201 && argvars[0].vval.v_string[0] == '$'
1202 && buf != NULL)
1203 return buf->b_ml.ml_line_count;
1204 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1205}
1206
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001207#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001208/*
1209 * Get the float value of "argvars[0]" into "f".
1210 * Returns FAIL when the argument is not a Number or Float.
1211 */
1212 static int
1213get_float_arg(typval_T *argvars, float_T *f)
1214{
1215 if (argvars[0].v_type == VAR_FLOAT)
1216 {
1217 *f = argvars[0].vval.v_float;
1218 return OK;
1219 }
1220 if (argvars[0].v_type == VAR_NUMBER)
1221 {
1222 *f = (float_T)argvars[0].vval.v_number;
1223 return OK;
1224 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001225 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001226 return FAIL;
1227}
1228
1229/*
1230 * "abs(expr)" function
1231 */
1232 static void
1233f_abs(typval_T *argvars, typval_T *rettv)
1234{
1235 if (argvars[0].v_type == VAR_FLOAT)
1236 {
1237 rettv->v_type = VAR_FLOAT;
1238 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1239 }
1240 else
1241 {
1242 varnumber_T n;
1243 int error = FALSE;
1244
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001245 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001246 if (error)
1247 rettv->vval.v_number = -1;
1248 else if (n > 0)
1249 rettv->vval.v_number = n;
1250 else
1251 rettv->vval.v_number = -n;
1252 }
1253}
1254
1255/*
1256 * "acos()" function
1257 */
1258 static void
1259f_acos(typval_T *argvars, typval_T *rettv)
1260{
1261 float_T f = 0.0;
1262
1263 rettv->v_type = VAR_FLOAT;
1264 if (get_float_arg(argvars, &f) == OK)
1265 rettv->vval.v_float = acos(f);
1266 else
1267 rettv->vval.v_float = 0.0;
1268}
1269#endif
1270
1271/*
1272 * "add(list, item)" function
1273 */
1274 static void
1275f_add(typval_T *argvars, typval_T *rettv)
1276{
1277 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001278 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001279
1280 rettv->vval.v_number = 1; /* Default: Failed */
1281 if (argvars[0].v_type == VAR_LIST)
1282 {
1283 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001284 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001285 (char_u *)N_("add() argument"), TRUE)
1286 && list_append_tv(l, &argvars[1]) == OK)
1287 copy_tv(&argvars[0], rettv);
1288 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001289 else if (argvars[0].v_type == VAR_BLOB)
1290 {
1291 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001292 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001293 (char_u *)N_("add() argument"), TRUE))
1294 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001295 int error = FALSE;
1296 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1297
1298 if (!error)
1299 {
1300 ga_append(&b->bv_ga, (int)n);
1301 copy_tv(&argvars[0], rettv);
1302 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001303 }
1304 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001305 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001306 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001307}
1308
1309/*
1310 * "and(expr, expr)" function
1311 */
1312 static void
1313f_and(typval_T *argvars, typval_T *rettv)
1314{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001315 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1316 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001317}
1318
1319/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001320 * If there is a window for "curbuf", make it the current window.
1321 */
1322 static void
1323find_win_for_curbuf(void)
1324{
1325 wininfo_T *wip;
1326
1327 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1328 {
1329 if (wip->wi_win != NULL)
1330 {
1331 curwin = wip->wi_win;
1332 break;
1333 }
1334 }
1335}
1336
1337/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001338 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001339 */
1340 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001341set_buffer_lines(
1342 buf_T *buf,
1343 linenr_T lnum_arg,
1344 int append,
1345 typval_T *lines,
1346 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001347{
Bram Moolenaarca851592018-06-06 21:04:07 +02001348 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1349 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001350 list_T *l = NULL;
1351 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001352 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001353 linenr_T append_lnum;
1354 buf_T *curbuf_save = NULL;
1355 win_T *curwin_save = NULL;
1356 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001357
Bram Moolenaarca851592018-06-06 21:04:07 +02001358 /* When using the current buffer ml_mfp will be set if needed. Useful when
1359 * setline() is used on startup. For other buffers the buffer must be
1360 * loaded. */
1361 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001362 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001363 rettv->vval.v_number = 1; /* FAIL */
1364 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365 }
1366
Bram Moolenaarca851592018-06-06 21:04:07 +02001367 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001368 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001369 curbuf_save = curbuf;
1370 curwin_save = curwin;
1371 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001372 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001373 }
1374
1375 if (append)
1376 // appendbufline() uses the line number below which we insert
1377 append_lnum = lnum - 1;
1378 else
1379 // setbufline() uses the line number above which we insert, we only
1380 // append if it's below the last line
1381 append_lnum = curbuf->b_ml.ml_line_count;
1382
1383 if (lines->v_type == VAR_LIST)
1384 {
1385 l = lines->vval.v_list;
1386 li = l->lv_first;
1387 }
1388 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001389 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001390
1391 /* default result is zero == OK */
1392 for (;;)
1393 {
1394 if (l != NULL)
1395 {
1396 /* list argument, get next string */
1397 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001398 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001399 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001400 li = li->li_next;
1401 }
1402
Bram Moolenaarca851592018-06-06 21:04:07 +02001403 rettv->vval.v_number = 1; /* FAIL */
1404 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1405 break;
1406
1407 /* When coming here from Insert mode, sync undo, so that this can be
1408 * undone separately from what was previously inserted. */
1409 if (u_sync_once == 2)
1410 {
1411 u_sync_once = 1; /* notify that u_sync() was called */
1412 u_sync(TRUE);
1413 }
1414
1415 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1416 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001417 // Existing line, replace it.
1418 // Removes any existing text properties.
1419 if (u_savesub(lnum) == OK && ml_replace_len(
1420 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001421 {
1422 changed_bytes(lnum, 0);
1423 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1424 check_cursor_col();
1425 rettv->vval.v_number = 0; /* OK */
1426 }
1427 }
1428 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1429 {
1430 /* append the line */
1431 ++added;
1432 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1433 rettv->vval.v_number = 0; /* OK */
1434 }
1435
1436 if (l == NULL) /* only one string argument */
1437 break;
1438 ++lnum;
1439 }
1440
1441 if (added > 0)
1442 {
1443 win_T *wp;
1444 tabpage_T *tp;
1445
1446 appended_lines_mark(append_lnum, added);
1447 FOR_ALL_TAB_WINDOWS(tp, wp)
1448 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1449 wp->w_cursor.lnum += added;
1450 check_cursor_col();
1451
Bram Moolenaarf2732452018-06-03 14:47:35 +02001452#ifdef FEAT_JOB_CHANNEL
1453 if (bt_prompt(curbuf) && (State & INSERT))
1454 // show the line with the prompt
1455 update_topline();
1456#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001457 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001458
1459 if (!is_curbuf)
1460 {
1461 curbuf = curbuf_save;
1462 curwin = curwin_save;
1463 }
1464}
1465
1466/*
1467 * "append(lnum, string/list)" function
1468 */
1469 static void
1470f_append(typval_T *argvars, typval_T *rettv)
1471{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001472 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001473
1474 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1475}
1476
1477/*
1478 * "appendbufline(buf, lnum, string/list)" function
1479 */
1480 static void
1481f_appendbufline(typval_T *argvars, typval_T *rettv)
1482{
1483 linenr_T lnum;
1484 buf_T *buf;
1485
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001486 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001487 if (buf == NULL)
1488 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001489 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001490 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001491 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001492 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1493 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001494}
1495
1496/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001497 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001498 */
1499 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001500f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001501{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001502 win_T *wp;
1503
1504 if (argvars[0].v_type == VAR_UNKNOWN)
1505 // use the current window
1506 rettv->vval.v_number = ARGCOUNT;
1507 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001508 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001509 // use the global argument list
1510 rettv->vval.v_number = GARGCOUNT;
1511 else
1512 {
1513 // use the argument list of the specified window
1514 wp = find_win_by_nr_or_id(&argvars[0]);
1515 if (wp != NULL)
1516 rettv->vval.v_number = WARGCOUNT(wp);
1517 else
1518 rettv->vval.v_number = -1;
1519 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001520}
1521
1522/*
1523 * "argidx()" function
1524 */
1525 static void
1526f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1527{
1528 rettv->vval.v_number = curwin->w_arg_idx;
1529}
1530
1531/*
1532 * "arglistid()" function
1533 */
1534 static void
1535f_arglistid(typval_T *argvars, typval_T *rettv)
1536{
1537 win_T *wp;
1538
1539 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001540 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001541 if (wp != NULL)
1542 rettv->vval.v_number = wp->w_alist->id;
1543}
1544
1545/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001546 * Get the argument list for a given window
1547 */
1548 static void
1549get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1550{
1551 int idx;
1552
1553 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1554 for (idx = 0; idx < argcount; ++idx)
1555 list_append_string(rettv->vval.v_list,
1556 alist_name(&arglist[idx]), -1);
1557}
1558
1559/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001560 * "argv(nr)" function
1561 */
1562 static void
1563f_argv(typval_T *argvars, typval_T *rettv)
1564{
1565 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001566 aentry_T *arglist = NULL;
1567 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001568
1569 if (argvars[0].v_type != VAR_UNKNOWN)
1570 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001571 if (argvars[1].v_type == VAR_UNKNOWN)
1572 {
1573 arglist = ARGLIST;
1574 argcount = ARGCOUNT;
1575 }
1576 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001577 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001578 {
1579 arglist = GARGLIST;
1580 argcount = GARGCOUNT;
1581 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001582 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001583 {
1584 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1585
1586 if (wp != NULL)
1587 {
1588 /* Use the argument list of the specified window */
1589 arglist = WARGLIST(wp);
1590 argcount = WARGCOUNT(wp);
1591 }
1592 }
1593
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001594 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001595 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001596 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001597 if (arglist != NULL && idx >= 0 && idx < argcount)
1598 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1599 else if (idx == -1)
1600 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001601 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001602 else
1603 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001604}
1605
1606/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001607 * "assert_beeps(cmd [, error])" function
1608 */
1609 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001610f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001611{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001612 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001613}
1614
1615/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001616 * "assert_equal(expected, actual[, msg])" function
1617 */
1618 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001619f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001620{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001621 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001622}
1623
1624/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001625 * "assert_equalfile(fname-one, fname-two)" function
1626 */
1627 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001628f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001629{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001630 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001631}
1632
1633/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001634 * "assert_notequal(expected, actual[, msg])" function
1635 */
1636 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001637f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001638{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001639 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001640}
1641
1642/*
1643 * "assert_exception(string[, msg])" function
1644 */
1645 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001646f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001647{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001648 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001649}
1650
1651/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001652 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001653 */
1654 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001655f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001656{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001657 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001658}
1659
1660/*
1661 * "assert_false(actual[, msg])" function
1662 */
1663 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001664f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001665{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001666 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001667}
1668
1669/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001670 * "assert_inrange(lower, upper[, msg])" function
1671 */
1672 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001673f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001674{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001675 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001676}
1677
1678/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001679 * "assert_match(pattern, actual[, msg])" function
1680 */
1681 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001682f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001683{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001684 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001685}
1686
1687/*
1688 * "assert_notmatch(pattern, actual[, msg])" function
1689 */
1690 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001691f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001692{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001693 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001694}
1695
1696/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001697 * "assert_report(msg)" function
1698 */
1699 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001700f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001701{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001702 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001703}
1704
1705/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001706 * "assert_true(actual[, msg])" function
1707 */
1708 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001709f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001710{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001711 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001712}
1713
1714#ifdef FEAT_FLOAT
1715/*
1716 * "asin()" function
1717 */
1718 static void
1719f_asin(typval_T *argvars, typval_T *rettv)
1720{
1721 float_T f = 0.0;
1722
1723 rettv->v_type = VAR_FLOAT;
1724 if (get_float_arg(argvars, &f) == OK)
1725 rettv->vval.v_float = asin(f);
1726 else
1727 rettv->vval.v_float = 0.0;
1728}
1729
1730/*
1731 * "atan()" function
1732 */
1733 static void
1734f_atan(typval_T *argvars, typval_T *rettv)
1735{
1736 float_T f = 0.0;
1737
1738 rettv->v_type = VAR_FLOAT;
1739 if (get_float_arg(argvars, &f) == OK)
1740 rettv->vval.v_float = atan(f);
1741 else
1742 rettv->vval.v_float = 0.0;
1743}
1744
1745/*
1746 * "atan2()" function
1747 */
1748 static void
1749f_atan2(typval_T *argvars, typval_T *rettv)
1750{
1751 float_T fx = 0.0, fy = 0.0;
1752
1753 rettv->v_type = VAR_FLOAT;
1754 if (get_float_arg(argvars, &fx) == OK
1755 && get_float_arg(&argvars[1], &fy) == OK)
1756 rettv->vval.v_float = atan2(fx, fy);
1757 else
1758 rettv->vval.v_float = 0.0;
1759}
1760#endif
1761
1762/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001763 * "balloon_show()" function
1764 */
1765#ifdef FEAT_BEVAL
1766 static void
1767f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1768{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001769 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001770 {
1771 if (argvars[0].v_type == VAR_LIST
1772# ifdef FEAT_GUI
1773 && !gui.in_use
1774# endif
1775 )
1776 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1777 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001778 post_balloon(balloonEval, tv_get_string_chk(&argvars[0]), NULL);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001779 }
1780}
1781
Bram Moolenaar669a8282017-11-19 20:13:05 +01001782# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001783 static void
1784f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1785{
1786 if (rettv_list_alloc(rettv) == OK)
1787 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001788 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001789
1790 if (msg != NULL)
1791 {
1792 pumitem_T *array;
1793 int size = split_message(msg, &array);
1794 int i;
1795
1796 /* Skip the first and last item, they are always empty. */
1797 for (i = 1; i < size - 1; ++i)
1798 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001799 while (size > 0)
1800 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001801 vim_free(array);
1802 }
1803 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001804}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001805# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001806#endif
1807
1808/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001809 * "browse(save, title, initdir, default)" function
1810 */
1811 static void
1812f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1813{
1814#ifdef FEAT_BROWSE
1815 int save;
1816 char_u *title;
1817 char_u *initdir;
1818 char_u *defname;
1819 char_u buf[NUMBUFLEN];
1820 char_u buf2[NUMBUFLEN];
1821 int error = FALSE;
1822
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001823 save = (int)tv_get_number_chk(&argvars[0], &error);
1824 title = tv_get_string_chk(&argvars[1]);
1825 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1826 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001827
1828 if (error || title == NULL || initdir == NULL || defname == NULL)
1829 rettv->vval.v_string = NULL;
1830 else
1831 rettv->vval.v_string =
1832 do_browse(save ? BROWSE_SAVE : 0,
1833 title, defname, NULL, initdir, NULL, curbuf);
1834#else
1835 rettv->vval.v_string = NULL;
1836#endif
1837 rettv->v_type = VAR_STRING;
1838}
1839
1840/*
1841 * "browsedir(title, initdir)" function
1842 */
1843 static void
1844f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1845{
1846#ifdef FEAT_BROWSE
1847 char_u *title;
1848 char_u *initdir;
1849 char_u buf[NUMBUFLEN];
1850
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001851 title = tv_get_string_chk(&argvars[0]);
1852 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853
1854 if (title == NULL || initdir == NULL)
1855 rettv->vval.v_string = NULL;
1856 else
1857 rettv->vval.v_string = do_browse(BROWSE_DIR,
1858 title, NULL, NULL, initdir, NULL, curbuf);
1859#else
1860 rettv->vval.v_string = NULL;
1861#endif
1862 rettv->v_type = VAR_STRING;
1863}
1864
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001865/*
1866 * Find a buffer by number or exact name.
1867 */
1868 static buf_T *
1869find_buffer(typval_T *avar)
1870{
1871 buf_T *buf = NULL;
1872
1873 if (avar->v_type == VAR_NUMBER)
1874 buf = buflist_findnr((int)avar->vval.v_number);
1875 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1876 {
1877 buf = buflist_findname_exp(avar->vval.v_string);
1878 if (buf == NULL)
1879 {
1880 /* No full path name match, try a match with a URL or a "nofile"
1881 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001882 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001883 if (buf->b_fname != NULL
1884 && (path_with_url(buf->b_fname)
1885#ifdef FEAT_QUICKFIX
1886 || bt_nofile(buf)
1887#endif
1888 )
1889 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1890 break;
1891 }
1892 }
1893 return buf;
1894}
1895
1896/*
1897 * "bufexists(expr)" function
1898 */
1899 static void
1900f_bufexists(typval_T *argvars, typval_T *rettv)
1901{
1902 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1903}
1904
1905/*
1906 * "buflisted(expr)" function
1907 */
1908 static void
1909f_buflisted(typval_T *argvars, typval_T *rettv)
1910{
1911 buf_T *buf;
1912
1913 buf = find_buffer(&argvars[0]);
1914 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1915}
1916
1917/*
1918 * "bufloaded(expr)" function
1919 */
1920 static void
1921f_bufloaded(typval_T *argvars, typval_T *rettv)
1922{
1923 buf_T *buf;
1924
1925 buf = find_buffer(&argvars[0]);
1926 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1927}
1928
1929 buf_T *
1930buflist_find_by_name(char_u *name, int curtab_only)
1931{
1932 int save_magic;
1933 char_u *save_cpo;
1934 buf_T *buf;
1935
1936 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1937 save_magic = p_magic;
1938 p_magic = TRUE;
1939 save_cpo = p_cpo;
1940 p_cpo = (char_u *)"";
1941
1942 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1943 TRUE, FALSE, curtab_only));
1944
1945 p_magic = save_magic;
1946 p_cpo = save_cpo;
1947 return buf;
1948}
1949
1950/*
1951 * Get buffer by number or pattern.
1952 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001953 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001954tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001955{
1956 char_u *name = tv->vval.v_string;
1957 buf_T *buf;
1958
1959 if (tv->v_type == VAR_NUMBER)
1960 return buflist_findnr((int)tv->vval.v_number);
1961 if (tv->v_type != VAR_STRING)
1962 return NULL;
1963 if (name == NULL || *name == NUL)
1964 return curbuf;
1965 if (name[0] == '$' && name[1] == NUL)
1966 return lastbuf;
1967
1968 buf = buflist_find_by_name(name, curtab_only);
1969
1970 /* If not found, try expanding the name, like done for bufexists(). */
1971 if (buf == NULL)
1972 buf = find_buffer(tv);
1973
1974 return buf;
1975}
1976
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001977#ifdef FEAT_SIGNS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001978/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001979 * Get the buffer from "arg" and give an error and return NULL if it is not
1980 * valid.
1981 */
1982 static buf_T *
1983get_buf_arg(typval_T *arg)
1984{
1985 buf_T *buf;
1986
1987 ++emsg_off;
1988 buf = tv_get_buf(arg, FALSE);
1989 --emsg_off;
1990 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001991 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001992 return buf;
1993}
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001994#endif
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001995
1996/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001997 * "bufname(expr)" function
1998 */
1999 static void
2000f_bufname(typval_T *argvars, typval_T *rettv)
2001{
2002 buf_T *buf;
2003
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002004 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002005 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002006 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002007 rettv->v_type = VAR_STRING;
2008 if (buf != NULL && buf->b_fname != NULL)
2009 rettv->vval.v_string = vim_strsave(buf->b_fname);
2010 else
2011 rettv->vval.v_string = NULL;
2012 --emsg_off;
2013}
2014
2015/*
2016 * "bufnr(expr)" function
2017 */
2018 static void
2019f_bufnr(typval_T *argvars, typval_T *rettv)
2020{
2021 buf_T *buf;
2022 int error = FALSE;
2023 char_u *name;
2024
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002025 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002026 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002027 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002028 --emsg_off;
2029
2030 /* If the buffer isn't found and the second argument is not zero create a
2031 * new buffer. */
2032 if (buf == NULL
2033 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002034 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002035 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002036 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002037 && !error)
2038 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2039
2040 if (buf != NULL)
2041 rettv->vval.v_number = buf->b_fnum;
2042 else
2043 rettv->vval.v_number = -1;
2044}
2045
2046 static void
2047buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2048{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002049 win_T *wp;
2050 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002051 buf_T *buf;
2052
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002053 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002054 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002055 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002056 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002057 {
2058 ++winnr;
2059 if (wp->w_buffer == buf)
2060 break;
2061 }
2062 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002063 --emsg_off;
2064}
2065
2066/*
2067 * "bufwinid(nr)" function
2068 */
2069 static void
2070f_bufwinid(typval_T *argvars, typval_T *rettv)
2071{
2072 buf_win_common(argvars, rettv, FALSE);
2073}
2074
2075/*
2076 * "bufwinnr(nr)" function
2077 */
2078 static void
2079f_bufwinnr(typval_T *argvars, typval_T *rettv)
2080{
2081 buf_win_common(argvars, rettv, TRUE);
2082}
2083
2084/*
2085 * "byte2line(byte)" function
2086 */
2087 static void
2088f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2089{
2090#ifndef FEAT_BYTEOFF
2091 rettv->vval.v_number = -1;
2092#else
2093 long boff = 0;
2094
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002095 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002096 if (boff < 0)
2097 rettv->vval.v_number = -1;
2098 else
2099 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2100 (linenr_T)0, &boff);
2101#endif
2102}
2103
2104 static void
2105byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2106{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002107 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002108 char_u *str;
2109 varnumber_T idx;
2110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002111 str = tv_get_string_chk(&argvars[0]);
2112 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002113 rettv->vval.v_number = -1;
2114 if (str == NULL || idx < 0)
2115 return;
2116
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002117 t = str;
2118 for ( ; idx > 0; idx--)
2119 {
2120 if (*t == NUL) /* EOL reached */
2121 return;
2122 if (enc_utf8 && comp)
2123 t += utf_ptr2len(t);
2124 else
2125 t += (*mb_ptr2len)(t);
2126 }
2127 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002128}
2129
2130/*
2131 * "byteidx()" function
2132 */
2133 static void
2134f_byteidx(typval_T *argvars, typval_T *rettv)
2135{
2136 byteidx(argvars, rettv, FALSE);
2137}
2138
2139/*
2140 * "byteidxcomp()" function
2141 */
2142 static void
2143f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2144{
2145 byteidx(argvars, rettv, TRUE);
2146}
2147
2148/*
2149 * "call(func, arglist [, dict])" function
2150 */
2151 static void
2152f_call(typval_T *argvars, typval_T *rettv)
2153{
2154 char_u *func;
2155 partial_T *partial = NULL;
2156 dict_T *selfdict = NULL;
2157
2158 if (argvars[1].v_type != VAR_LIST)
2159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002160 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002161 return;
2162 }
2163 if (argvars[1].vval.v_list == NULL)
2164 return;
2165
2166 if (argvars[0].v_type == VAR_FUNC)
2167 func = argvars[0].vval.v_string;
2168 else if (argvars[0].v_type == VAR_PARTIAL)
2169 {
2170 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002171 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002172 }
2173 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002174 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002175 if (*func == NUL)
2176 return; /* type error or empty name */
2177
2178 if (argvars[2].v_type != VAR_UNKNOWN)
2179 {
2180 if (argvars[2].v_type != VAR_DICT)
2181 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002182 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002183 return;
2184 }
2185 selfdict = argvars[2].vval.v_dict;
2186 }
2187
2188 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2189}
2190
2191#ifdef FEAT_FLOAT
2192/*
2193 * "ceil({float})" function
2194 */
2195 static void
2196f_ceil(typval_T *argvars, typval_T *rettv)
2197{
2198 float_T f = 0.0;
2199
2200 rettv->v_type = VAR_FLOAT;
2201 if (get_float_arg(argvars, &f) == OK)
2202 rettv->vval.v_float = ceil(f);
2203 else
2204 rettv->vval.v_float = 0.0;
2205}
2206#endif
2207
2208#ifdef FEAT_JOB_CHANNEL
2209/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002210 * "ch_canread()" function
2211 */
2212 static void
2213f_ch_canread(typval_T *argvars, typval_T *rettv)
2214{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002215 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002216
2217 rettv->vval.v_number = 0;
2218 if (channel != NULL)
2219 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2220 || channel_has_readahead(channel, PART_OUT)
2221 || channel_has_readahead(channel, PART_ERR);
2222}
2223
2224/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002225 * "ch_close()" function
2226 */
2227 static void
2228f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2229{
2230 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2231
2232 if (channel != NULL)
2233 {
2234 channel_close(channel, FALSE);
2235 channel_clear(channel);
2236 }
2237}
2238
2239/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002240 * "ch_close()" function
2241 */
2242 static void
2243f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2244{
2245 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2246
2247 if (channel != NULL)
2248 channel_close_in(channel);
2249}
2250
2251/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002252 * "ch_getbufnr()" function
2253 */
2254 static void
2255f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2256{
2257 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2258
2259 rettv->vval.v_number = -1;
2260 if (channel != NULL)
2261 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002262 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002263 int part;
2264
2265 if (STRCMP(what, "err") == 0)
2266 part = PART_ERR;
2267 else if (STRCMP(what, "out") == 0)
2268 part = PART_OUT;
2269 else if (STRCMP(what, "in") == 0)
2270 part = PART_IN;
2271 else
2272 part = PART_SOCK;
2273 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2274 rettv->vval.v_number =
2275 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2276 }
2277}
2278
2279/*
2280 * "ch_getjob()" function
2281 */
2282 static void
2283f_ch_getjob(typval_T *argvars, typval_T *rettv)
2284{
2285 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2286
2287 if (channel != NULL)
2288 {
2289 rettv->v_type = VAR_JOB;
2290 rettv->vval.v_job = channel->ch_job;
2291 if (channel->ch_job != NULL)
2292 ++channel->ch_job->jv_refcount;
2293 }
2294}
2295
2296/*
2297 * "ch_info()" function
2298 */
2299 static void
2300f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2301{
2302 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2303
2304 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2305 channel_info(channel, rettv->vval.v_dict);
2306}
2307
2308/*
2309 * "ch_log()" function
2310 */
2311 static void
2312f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2313{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002314 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002315 channel_T *channel = NULL;
2316
2317 if (argvars[1].v_type != VAR_UNKNOWN)
2318 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2319
Bram Moolenaard5359b22018-04-05 22:44:39 +02002320 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002321}
2322
2323/*
2324 * "ch_logfile()" function
2325 */
2326 static void
2327f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2328{
2329 char_u *fname;
2330 char_u *opt = (char_u *)"";
2331 char_u buf[NUMBUFLEN];
2332
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002333 /* Don't open a file in restricted mode. */
2334 if (check_restricted() || check_secure())
2335 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002336 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002337 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002338 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002339 ch_logfile(fname, opt);
2340}
2341
2342/*
2343 * "ch_open()" function
2344 */
2345 static void
2346f_ch_open(typval_T *argvars, typval_T *rettv)
2347{
2348 rettv->v_type = VAR_CHANNEL;
2349 if (check_restricted() || check_secure())
2350 return;
2351 rettv->vval.v_channel = channel_open_func(argvars);
2352}
2353
2354/*
2355 * "ch_read()" function
2356 */
2357 static void
2358f_ch_read(typval_T *argvars, typval_T *rettv)
2359{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002360 common_channel_read(argvars, rettv, FALSE, FALSE);
2361}
2362
2363/*
2364 * "ch_readblob()" function
2365 */
2366 static void
2367f_ch_readblob(typval_T *argvars, typval_T *rettv)
2368{
2369 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002370}
2371
2372/*
2373 * "ch_readraw()" function
2374 */
2375 static void
2376f_ch_readraw(typval_T *argvars, typval_T *rettv)
2377{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002378 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379}
2380
2381/*
2382 * "ch_evalexpr()" function
2383 */
2384 static void
2385f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2386{
2387 ch_expr_common(argvars, rettv, TRUE);
2388}
2389
2390/*
2391 * "ch_sendexpr()" function
2392 */
2393 static void
2394f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2395{
2396 ch_expr_common(argvars, rettv, FALSE);
2397}
2398
2399/*
2400 * "ch_evalraw()" function
2401 */
2402 static void
2403f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2404{
2405 ch_raw_common(argvars, rettv, TRUE);
2406}
2407
2408/*
2409 * "ch_sendraw()" function
2410 */
2411 static void
2412f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2413{
2414 ch_raw_common(argvars, rettv, FALSE);
2415}
2416
2417/*
2418 * "ch_setoptions()" function
2419 */
2420 static void
2421f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2422{
2423 channel_T *channel;
2424 jobopt_T opt;
2425
2426 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2427 if (channel == NULL)
2428 return;
2429 clear_job_options(&opt);
2430 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002431 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002432 channel_set_options(channel, &opt);
2433 free_job_options(&opt);
2434}
2435
2436/*
2437 * "ch_status()" function
2438 */
2439 static void
2440f_ch_status(typval_T *argvars, typval_T *rettv)
2441{
2442 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002443 jobopt_T opt;
2444 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002445
2446 /* return an empty string by default */
2447 rettv->v_type = VAR_STRING;
2448 rettv->vval.v_string = NULL;
2449
2450 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002451
2452 if (argvars[1].v_type != VAR_UNKNOWN)
2453 {
2454 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002455 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002456 && (opt.jo_set & JO_PART))
2457 part = opt.jo_part;
2458 }
2459
2460 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002461}
2462#endif
2463
2464/*
2465 * "changenr()" function
2466 */
2467 static void
2468f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2469{
2470 rettv->vval.v_number = curbuf->b_u_seq_cur;
2471}
2472
2473/*
2474 * "char2nr(string)" function
2475 */
2476 static void
2477f_char2nr(typval_T *argvars, typval_T *rettv)
2478{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002479 if (has_mbyte)
2480 {
2481 int utf8 = 0;
2482
2483 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002484 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002485
2486 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002487 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002488 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002489 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002490 }
2491 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002492 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002493}
2494
2495/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002496 * "chdir(dir)" function
2497 */
2498 static void
2499f_chdir(typval_T *argvars, typval_T *rettv)
2500{
2501 char_u *cwd;
2502 cdscope_T scope = CDSCOPE_GLOBAL;
2503
2504 rettv->v_type = VAR_STRING;
2505 rettv->vval.v_string = NULL;
2506
2507 if (argvars[0].v_type != VAR_STRING)
2508 return;
2509
2510 // Return the current directory
2511 cwd = alloc(MAXPATHL);
2512 if (cwd != NULL)
2513 {
2514 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2515 {
2516#ifdef BACKSLASH_IN_FILENAME
2517 slash_adjust(cwd);
2518#endif
2519 rettv->vval.v_string = vim_strsave(cwd);
2520 }
2521 vim_free(cwd);
2522 }
2523
2524 if (curwin->w_localdir != NULL)
2525 scope = CDSCOPE_WINDOW;
2526 else if (curtab->tp_localdir != NULL)
2527 scope = CDSCOPE_TABPAGE;
2528
2529 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2530 // Directory change failed
2531 VIM_CLEAR(rettv->vval.v_string);
2532}
2533
2534/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002535 * "cindent(lnum)" function
2536 */
2537 static void
2538f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2539{
2540#ifdef FEAT_CINDENT
2541 pos_T pos;
2542 linenr_T lnum;
2543
2544 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002545 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002546 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2547 {
2548 curwin->w_cursor.lnum = lnum;
2549 rettv->vval.v_number = get_c_indent();
2550 curwin->w_cursor = pos;
2551 }
2552 else
2553#endif
2554 rettv->vval.v_number = -1;
2555}
2556
Bram Moolenaaraff74912019-03-30 18:11:49 +01002557 static win_T *
2558get_optional_window(typval_T *argvars, int idx)
2559{
2560 win_T *win = curwin;
2561
2562 if (argvars[idx].v_type != VAR_UNKNOWN)
2563 {
2564 win = find_win_by_nr_or_id(&argvars[idx]);
2565 if (win == NULL)
2566 {
2567 emsg(_(e_invalwindow));
2568 return NULL;
2569 }
2570 }
2571 return win;
2572}
2573
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002574/*
2575 * "clearmatches()" function
2576 */
2577 static void
2578f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2579{
2580#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002581 win_T *win = get_optional_window(argvars, 0);
2582
2583 if (win != NULL)
2584 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002585#endif
2586}
2587
2588/*
2589 * "col(string)" function
2590 */
2591 static void
2592f_col(typval_T *argvars, typval_T *rettv)
2593{
2594 colnr_T col = 0;
2595 pos_T *fp;
2596 int fnum = curbuf->b_fnum;
2597
2598 fp = var2fpos(&argvars[0], FALSE, &fnum);
2599 if (fp != NULL && fnum == curbuf->b_fnum)
2600 {
2601 if (fp->col == MAXCOL)
2602 {
2603 /* '> can be MAXCOL, get the length of the line then */
2604 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2605 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2606 else
2607 col = MAXCOL;
2608 }
2609 else
2610 {
2611 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002612 /* col(".") when the cursor is on the NUL at the end of the line
2613 * because of "coladd" can be seen as an extra column. */
2614 if (virtual_active() && fp == &curwin->w_cursor)
2615 {
2616 char_u *p = ml_get_cursor();
2617
2618 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2619 curwin->w_virtcol - curwin->w_cursor.coladd))
2620 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002621 int l;
2622
2623 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2624 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002625 }
2626 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002627 }
2628 }
2629 rettv->vval.v_number = col;
2630}
2631
2632#if defined(FEAT_INS_EXPAND)
2633/*
2634 * "complete()" function
2635 */
2636 static void
2637f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2638{
2639 int startcol;
2640
2641 if ((State & INSERT) == 0)
2642 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002643 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002644 return;
2645 }
2646
2647 /* Check for undo allowed here, because if something was already inserted
2648 * the line was already saved for undo and this check isn't done. */
2649 if (!undo_allowed())
2650 return;
2651
2652 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2653 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002654 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002655 return;
2656 }
2657
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002658 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002659 if (startcol <= 0)
2660 return;
2661
2662 set_completion(startcol - 1, argvars[1].vval.v_list);
2663}
2664
2665/*
2666 * "complete_add()" function
2667 */
2668 static void
2669f_complete_add(typval_T *argvars, typval_T *rettv)
2670{
2671 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2672}
2673
2674/*
2675 * "complete_check()" function
2676 */
2677 static void
2678f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2679{
2680 int saved = RedrawingDisabled;
2681
2682 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002683 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002684 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002685 RedrawingDisabled = saved;
2686}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002687
2688/*
2689 * "complete_info()" function
2690 */
2691 static void
2692f_complete_info(typval_T *argvars, typval_T *rettv)
2693{
2694 list_T *what_list = NULL;
2695
2696 if (rettv_dict_alloc(rettv) != OK)
2697 return;
2698
2699 if (argvars[0].v_type != VAR_UNKNOWN)
2700 {
2701 if (argvars[0].v_type != VAR_LIST)
2702 {
2703 emsg(_(e_listreq));
2704 return;
2705 }
2706 what_list = argvars[0].vval.v_list;
2707 }
2708 get_complete_info(what_list, rettv->vval.v_dict);
2709}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002710#endif
2711
2712/*
2713 * "confirm(message, buttons[, default [, type]])" function
2714 */
2715 static void
2716f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2717{
2718#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2719 char_u *message;
2720 char_u *buttons = NULL;
2721 char_u buf[NUMBUFLEN];
2722 char_u buf2[NUMBUFLEN];
2723 int def = 1;
2724 int type = VIM_GENERIC;
2725 char_u *typestr;
2726 int error = FALSE;
2727
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002728 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002729 if (message == NULL)
2730 error = TRUE;
2731 if (argvars[1].v_type != VAR_UNKNOWN)
2732 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002733 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002734 if (buttons == NULL)
2735 error = TRUE;
2736 if (argvars[2].v_type != VAR_UNKNOWN)
2737 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002738 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002739 if (argvars[3].v_type != VAR_UNKNOWN)
2740 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002741 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002742 if (typestr == NULL)
2743 error = TRUE;
2744 else
2745 {
2746 switch (TOUPPER_ASC(*typestr))
2747 {
2748 case 'E': type = VIM_ERROR; break;
2749 case 'Q': type = VIM_QUESTION; break;
2750 case 'I': type = VIM_INFO; break;
2751 case 'W': type = VIM_WARNING; break;
2752 case 'G': type = VIM_GENERIC; break;
2753 }
2754 }
2755 }
2756 }
2757 }
2758
2759 if (buttons == NULL || *buttons == NUL)
2760 buttons = (char_u *)_("&Ok");
2761
2762 if (!error)
2763 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2764 def, NULL, FALSE);
2765#endif
2766}
2767
2768/*
2769 * "copy()" function
2770 */
2771 static void
2772f_copy(typval_T *argvars, typval_T *rettv)
2773{
2774 item_copy(&argvars[0], rettv, FALSE, 0);
2775}
2776
2777#ifdef FEAT_FLOAT
2778/*
2779 * "cos()" function
2780 */
2781 static void
2782f_cos(typval_T *argvars, typval_T *rettv)
2783{
2784 float_T f = 0.0;
2785
2786 rettv->v_type = VAR_FLOAT;
2787 if (get_float_arg(argvars, &f) == OK)
2788 rettv->vval.v_float = cos(f);
2789 else
2790 rettv->vval.v_float = 0.0;
2791}
2792
2793/*
2794 * "cosh()" function
2795 */
2796 static void
2797f_cosh(typval_T *argvars, typval_T *rettv)
2798{
2799 float_T f = 0.0;
2800
2801 rettv->v_type = VAR_FLOAT;
2802 if (get_float_arg(argvars, &f) == OK)
2803 rettv->vval.v_float = cosh(f);
2804 else
2805 rettv->vval.v_float = 0.0;
2806}
2807#endif
2808
2809/*
2810 * "count()" function
2811 */
2812 static void
2813f_count(typval_T *argvars, typval_T *rettv)
2814{
2815 long n = 0;
2816 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002817 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002818
Bram Moolenaar9966b212017-07-28 16:46:57 +02002819 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002820 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002821
2822 if (argvars[0].v_type == VAR_STRING)
2823 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002824 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002825 char_u *p = argvars[0].vval.v_string;
2826 char_u *next;
2827
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002828 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002829 {
2830 if (ic)
2831 {
2832 size_t len = STRLEN(expr);
2833
2834 while (*p != NUL)
2835 {
2836 if (MB_STRNICMP(p, expr, len) == 0)
2837 {
2838 ++n;
2839 p += len;
2840 }
2841 else
2842 MB_PTR_ADV(p);
2843 }
2844 }
2845 else
2846 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2847 != NULL)
2848 {
2849 ++n;
2850 p = next + STRLEN(expr);
2851 }
2852 }
2853
2854 }
2855 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002856 {
2857 listitem_T *li;
2858 list_T *l;
2859 long idx;
2860
2861 if ((l = argvars[0].vval.v_list) != NULL)
2862 {
2863 li = l->lv_first;
2864 if (argvars[2].v_type != VAR_UNKNOWN)
2865 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866 if (argvars[3].v_type != VAR_UNKNOWN)
2867 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002868 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002869 if (!error)
2870 {
2871 li = list_find(l, idx);
2872 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002873 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002874 }
2875 }
2876 if (error)
2877 li = NULL;
2878 }
2879
2880 for ( ; li != NULL; li = li->li_next)
2881 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2882 ++n;
2883 }
2884 }
2885 else if (argvars[0].v_type == VAR_DICT)
2886 {
2887 int todo;
2888 dict_T *d;
2889 hashitem_T *hi;
2890
2891 if ((d = argvars[0].vval.v_dict) != NULL)
2892 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002893 if (argvars[2].v_type != VAR_UNKNOWN)
2894 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002895 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002896 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002897 }
2898
2899 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2900 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2901 {
2902 if (!HASHITEM_EMPTY(hi))
2903 {
2904 --todo;
2905 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2906 ++n;
2907 }
2908 }
2909 }
2910 }
2911 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002912 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002913 rettv->vval.v_number = n;
2914}
2915
2916/*
2917 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2918 *
2919 * Checks the existence of a cscope connection.
2920 */
2921 static void
2922f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2923{
2924#ifdef FEAT_CSCOPE
2925 int num = 0;
2926 char_u *dbpath = NULL;
2927 char_u *prepend = NULL;
2928 char_u buf[NUMBUFLEN];
2929
2930 if (argvars[0].v_type != VAR_UNKNOWN
2931 && argvars[1].v_type != VAR_UNKNOWN)
2932 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002933 num = (int)tv_get_number(&argvars[0]);
2934 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002935 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002936 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002937 }
2938
2939 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2940#endif
2941}
2942
2943/*
2944 * "cursor(lnum, col)" function, or
2945 * "cursor(list)"
2946 *
2947 * Moves the cursor to the specified line and column.
2948 * Returns 0 when the position could be set, -1 otherwise.
2949 */
2950 static void
2951f_cursor(typval_T *argvars, typval_T *rettv)
2952{
2953 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002955 int set_curswant = TRUE;
2956
2957 rettv->vval.v_number = -1;
2958 if (argvars[1].v_type == VAR_UNKNOWN)
2959 {
2960 pos_T pos;
2961 colnr_T curswant = -1;
2962
2963 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2964 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002965 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002966 return;
2967 }
2968 line = pos.lnum;
2969 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002970 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 if (curswant >= 0)
2972 {
2973 curwin->w_curswant = curswant - 1;
2974 set_curswant = FALSE;
2975 }
2976 }
2977 else
2978 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002979 line = tv_get_lnum(argvars);
2980 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002981 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002982 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002983 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002984 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002985 return; /* type error; errmsg already given */
2986 if (line > 0)
2987 curwin->w_cursor.lnum = line;
2988 if (col > 0)
2989 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002990 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002991
2992 /* Make sure the cursor is in a valid position. */
2993 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002994 /* Correct cursor for multi-byte character. */
2995 if (has_mbyte)
2996 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002997
2998 curwin->w_set_curswant = set_curswant;
2999 rettv->vval.v_number = 0;
3000}
3001
Bram Moolenaar4f974752019-02-17 17:44:42 +01003002#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003003/*
3004 * "debugbreak()" function
3005 */
3006 static void
3007f_debugbreak(typval_T *argvars, typval_T *rettv)
3008{
3009 int pid;
3010
3011 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003012 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003013 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003014 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003015 else
3016 {
3017 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3018
3019 if (hProcess != NULL)
3020 {
3021 DebugBreakProcess(hProcess);
3022 CloseHandle(hProcess);
3023 rettv->vval.v_number = OK;
3024 }
3025 }
3026}
3027#endif
3028
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003029/*
3030 * "deepcopy()" function
3031 */
3032 static void
3033f_deepcopy(typval_T *argvars, typval_T *rettv)
3034{
3035 int noref = 0;
3036 int copyID;
3037
3038 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003039 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003040 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003041 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003042 else
3043 {
3044 copyID = get_copyID();
3045 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3046 }
3047}
3048
3049/*
3050 * "delete()" function
3051 */
3052 static void
3053f_delete(typval_T *argvars, typval_T *rettv)
3054{
3055 char_u nbuf[NUMBUFLEN];
3056 char_u *name;
3057 char_u *flags;
3058
3059 rettv->vval.v_number = -1;
3060 if (check_restricted() || check_secure())
3061 return;
3062
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003063 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003064 if (name == NULL || *name == NUL)
3065 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003066 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003067 return;
3068 }
3069
3070 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003071 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003072 else
3073 flags = (char_u *)"";
3074
3075 if (*flags == NUL)
3076 /* delete a file */
3077 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3078 else if (STRCMP(flags, "d") == 0)
3079 /* delete an empty directory */
3080 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3081 else if (STRCMP(flags, "rf") == 0)
3082 /* delete a directory recursively */
3083 rettv->vval.v_number = delete_recursive(name);
3084 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003085 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003086}
3087
3088/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003089 * "deletebufline()" function
3090 */
3091 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003092f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003093{
3094 buf_T *buf;
3095 linenr_T first, last;
3096 linenr_T lnum;
3097 long count;
3098 int is_curbuf;
3099 buf_T *curbuf_save = NULL;
3100 win_T *curwin_save = NULL;
3101 tabpage_T *tp;
3102 win_T *wp;
3103
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003104 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003105 if (buf == NULL)
3106 {
3107 rettv->vval.v_number = 1; /* FAIL */
3108 return;
3109 }
3110 is_curbuf = buf == curbuf;
3111
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003112 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003113 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003114 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003115 else
3116 last = first;
3117
3118 if (buf->b_ml.ml_mfp == NULL || first < 1
3119 || first > buf->b_ml.ml_line_count || last < first)
3120 {
3121 rettv->vval.v_number = 1; /* FAIL */
3122 return;
3123 }
3124
3125 if (!is_curbuf)
3126 {
3127 curbuf_save = curbuf;
3128 curwin_save = curwin;
3129 curbuf = buf;
3130 find_win_for_curbuf();
3131 }
3132 if (last > curbuf->b_ml.ml_line_count)
3133 last = curbuf->b_ml.ml_line_count;
3134 count = last - first + 1;
3135
3136 // When coming here from Insert mode, sync undo, so that this can be
3137 // undone separately from what was previously inserted.
3138 if (u_sync_once == 2)
3139 {
3140 u_sync_once = 1; // notify that u_sync() was called
3141 u_sync(TRUE);
3142 }
3143
3144 if (u_save(first - 1, last + 1) == FAIL)
3145 {
3146 rettv->vval.v_number = 1; /* FAIL */
3147 return;
3148 }
3149
3150 for (lnum = first; lnum <= last; ++lnum)
3151 ml_delete(first, TRUE);
3152
3153 FOR_ALL_TAB_WINDOWS(tp, wp)
3154 if (wp->w_buffer == buf)
3155 {
3156 if (wp->w_cursor.lnum > last)
3157 wp->w_cursor.lnum -= count;
3158 else if (wp->w_cursor.lnum> first)
3159 wp->w_cursor.lnum = first;
3160 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3161 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3162 }
3163 check_cursor_col();
3164 deleted_lines_mark(first, count);
3165
3166 if (!is_curbuf)
3167 {
3168 curbuf = curbuf_save;
3169 curwin = curwin_save;
3170 }
3171}
3172
3173/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003174 * "did_filetype()" function
3175 */
3176 static void
3177f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3178{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003179 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003180}
3181
3182/*
3183 * "diff_filler()" function
3184 */
3185 static void
3186f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3187{
3188#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003189 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003190#endif
3191}
3192
3193/*
3194 * "diff_hlID()" function
3195 */
3196 static void
3197f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3198{
3199#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003200 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003201 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003202 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003203 static int fnum = 0;
3204 static int change_start = 0;
3205 static int change_end = 0;
3206 static hlf_T hlID = (hlf_T)0;
3207 int filler_lines;
3208 int col;
3209
3210 if (lnum < 0) /* ignore type error in {lnum} arg */
3211 lnum = 0;
3212 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003213 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003214 || fnum != curbuf->b_fnum)
3215 {
3216 /* New line, buffer, change: need to get the values. */
3217 filler_lines = diff_check(curwin, lnum);
3218 if (filler_lines < 0)
3219 {
3220 if (filler_lines == -1)
3221 {
3222 change_start = MAXCOL;
3223 change_end = -1;
3224 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3225 hlID = HLF_ADD; /* added line */
3226 else
3227 hlID = HLF_CHD; /* changed line */
3228 }
3229 else
3230 hlID = HLF_ADD; /* added line */
3231 }
3232 else
3233 hlID = (hlf_T)0;
3234 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003235 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236 fnum = curbuf->b_fnum;
3237 }
3238
3239 if (hlID == HLF_CHD || hlID == HLF_TXD)
3240 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003241 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003242 if (col >= change_start && col <= change_end)
3243 hlID = HLF_TXD; /* changed text */
3244 else
3245 hlID = HLF_CHD; /* changed line */
3246 }
3247 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3248#endif
3249}
3250
3251/*
3252 * "empty({expr})" function
3253 */
3254 static void
3255f_empty(typval_T *argvars, typval_T *rettv)
3256{
3257 int n = FALSE;
3258
3259 switch (argvars[0].v_type)
3260 {
3261 case VAR_STRING:
3262 case VAR_FUNC:
3263 n = argvars[0].vval.v_string == NULL
3264 || *argvars[0].vval.v_string == NUL;
3265 break;
3266 case VAR_PARTIAL:
3267 n = FALSE;
3268 break;
3269 case VAR_NUMBER:
3270 n = argvars[0].vval.v_number == 0;
3271 break;
3272 case VAR_FLOAT:
3273#ifdef FEAT_FLOAT
3274 n = argvars[0].vval.v_float == 0.0;
3275 break;
3276#endif
3277 case VAR_LIST:
3278 n = argvars[0].vval.v_list == NULL
3279 || argvars[0].vval.v_list->lv_first == NULL;
3280 break;
3281 case VAR_DICT:
3282 n = argvars[0].vval.v_dict == NULL
3283 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3284 break;
3285 case VAR_SPECIAL:
3286 n = argvars[0].vval.v_number != VVAL_TRUE;
3287 break;
3288
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003289 case VAR_BLOB:
3290 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003291 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3292 break;
3293
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003294 case VAR_JOB:
3295#ifdef FEAT_JOB_CHANNEL
3296 n = argvars[0].vval.v_job == NULL
3297 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3298 break;
3299#endif
3300 case VAR_CHANNEL:
3301#ifdef FEAT_JOB_CHANNEL
3302 n = argvars[0].vval.v_channel == NULL
3303 || !channel_is_open(argvars[0].vval.v_channel);
3304 break;
3305#endif
3306 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003307 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003308 n = TRUE;
3309 break;
3310 }
3311
3312 rettv->vval.v_number = n;
3313}
3314
3315/*
3316 * "escape({string}, {chars})" function
3317 */
3318 static void
3319f_escape(typval_T *argvars, typval_T *rettv)
3320{
3321 char_u buf[NUMBUFLEN];
3322
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003323 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3324 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003325 rettv->v_type = VAR_STRING;
3326}
3327
3328/*
3329 * "eval()" function
3330 */
3331 static void
3332f_eval(typval_T *argvars, typval_T *rettv)
3333{
3334 char_u *s, *p;
3335
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003336 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003337 if (s != NULL)
3338 s = skipwhite(s);
3339
3340 p = s;
3341 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3342 {
3343 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003344 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345 need_clr_eos = FALSE;
3346 rettv->v_type = VAR_NUMBER;
3347 rettv->vval.v_number = 0;
3348 }
3349 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003350 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003351}
3352
3353/*
3354 * "eventhandler()" function
3355 */
3356 static void
3357f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3358{
3359 rettv->vval.v_number = vgetc_busy;
3360}
3361
3362/*
3363 * "executable()" function
3364 */
3365 static void
3366f_executable(typval_T *argvars, typval_T *rettv)
3367{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003368 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003369
3370 /* Check in $PATH and also check directly if there is a directory name. */
3371 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3372 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3373}
3374
3375static garray_T redir_execute_ga;
3376
3377/*
3378 * Append "value[value_len]" to the execute() output.
3379 */
3380 void
3381execute_redir_str(char_u *value, int value_len)
3382{
3383 int len;
3384
3385 if (value_len == -1)
3386 len = (int)STRLEN(value); /* Append the entire string */
3387 else
3388 len = value_len; /* Append only "value_len" characters */
3389 if (ga_grow(&redir_execute_ga, len) == OK)
3390 {
3391 mch_memmove((char *)redir_execute_ga.ga_data
3392 + redir_execute_ga.ga_len, value, len);
3393 redir_execute_ga.ga_len += len;
3394 }
3395}
3396
3397/*
3398 * Get next line from a list.
3399 * Called by do_cmdline() to get the next line.
3400 * Returns allocated string, or NULL for end of function.
3401 */
3402
3403 static char_u *
3404get_list_line(
3405 int c UNUSED,
3406 void *cookie,
3407 int indent UNUSED)
3408{
3409 listitem_T **p = (listitem_T **)cookie;
3410 listitem_T *item = *p;
3411 char_u buf[NUMBUFLEN];
3412 char_u *s;
3413
3414 if (item == NULL)
3415 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003416 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003417 *p = item->li_next;
3418 return s == NULL ? NULL : vim_strsave(s);
3419}
3420
3421/*
3422 * "execute()" function
3423 */
3424 static void
3425f_execute(typval_T *argvars, typval_T *rettv)
3426{
3427 char_u *cmd = NULL;
3428 list_T *list = NULL;
3429 int save_msg_silent = msg_silent;
3430 int save_emsg_silent = emsg_silent;
3431 int save_emsg_noredir = emsg_noredir;
3432 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003433 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003434 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003435 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003436 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003437
3438 rettv->vval.v_string = NULL;
3439 rettv->v_type = VAR_STRING;
3440
3441 if (argvars[0].v_type == VAR_LIST)
3442 {
3443 list = argvars[0].vval.v_list;
3444 if (list == NULL || list->lv_first == NULL)
3445 /* empty list, no commands, empty output */
3446 return;
3447 ++list->lv_refcount;
3448 }
3449 else
3450 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003451 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003452 if (cmd == NULL)
3453 return;
3454 }
3455
3456 if (argvars[1].v_type != VAR_UNKNOWN)
3457 {
3458 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003459 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003460
3461 if (s == NULL)
3462 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003463 if (*s == NUL)
3464 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003465 if (STRNCMP(s, "silent", 6) == 0)
3466 ++msg_silent;
3467 if (STRCMP(s, "silent!") == 0)
3468 {
3469 emsg_silent = TRUE;
3470 emsg_noredir = TRUE;
3471 }
3472 }
3473 else
3474 ++msg_silent;
3475
3476 if (redir_execute)
3477 save_ga = redir_execute_ga;
3478 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3479 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003480 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003481 if (!echo_output)
3482 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483
3484 if (cmd != NULL)
3485 do_cmdline_cmd(cmd);
3486 else
3487 {
3488 listitem_T *item = list->lv_first;
3489
3490 do_cmdline(NULL, get_list_line, (void *)&item,
3491 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3492 --list->lv_refcount;
3493 }
3494
Bram Moolenaard297f352017-01-29 20:31:21 +01003495 /* Need to append a NUL to the result. */
3496 if (ga_grow(&redir_execute_ga, 1) == OK)
3497 {
3498 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3499 rettv->vval.v_string = redir_execute_ga.ga_data;
3500 }
3501 else
3502 {
3503 ga_clear(&redir_execute_ga);
3504 rettv->vval.v_string = NULL;
3505 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003506 msg_silent = save_msg_silent;
3507 emsg_silent = save_emsg_silent;
3508 emsg_noredir = save_emsg_noredir;
3509
3510 redir_execute = save_redir_execute;
3511 if (redir_execute)
3512 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003513 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003514
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003515 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003516 if (echo_output)
3517 // When not working silently: put it in column zero. A following
3518 // "echon" will overwrite the message, unavoidably.
3519 msg_col = 0;
3520 else
3521 // When working silently: Put it back where it was, since nothing
3522 // should have been written.
3523 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003524}
3525
3526/*
3527 * "exepath()" function
3528 */
3529 static void
3530f_exepath(typval_T *argvars, typval_T *rettv)
3531{
3532 char_u *p = NULL;
3533
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003534 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003535 rettv->v_type = VAR_STRING;
3536 rettv->vval.v_string = p;
3537}
3538
3539/*
3540 * "exists()" function
3541 */
3542 static void
3543f_exists(typval_T *argvars, typval_T *rettv)
3544{
3545 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003546 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003548 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003549 if (*p == '$') /* environment variable */
3550 {
3551 /* first try "normal" environment variables (fast) */
3552 if (mch_getenv(p + 1) != NULL)
3553 n = TRUE;
3554 else
3555 {
3556 /* try expanding things like $VIM and ${HOME} */
3557 p = expand_env_save(p);
3558 if (p != NULL && *p != '$')
3559 n = TRUE;
3560 vim_free(p);
3561 }
3562 }
3563 else if (*p == '&' || *p == '+') /* option */
3564 {
3565 n = (get_option_tv(&p, NULL, TRUE) == OK);
3566 if (*skipwhite(p) != NUL)
3567 n = FALSE; /* trailing garbage */
3568 }
3569 else if (*p == '*') /* internal or user defined function */
3570 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003571 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003572 }
3573 else if (*p == ':')
3574 {
3575 n = cmd_exists(p + 1);
3576 }
3577 else if (*p == '#')
3578 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003579 if (p[1] == '#')
3580 n = autocmd_supported(p + 2);
3581 else
3582 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003583 }
3584 else /* internal variable */
3585 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003586 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003587 }
3588
3589 rettv->vval.v_number = n;
3590}
3591
3592#ifdef FEAT_FLOAT
3593/*
3594 * "exp()" function
3595 */
3596 static void
3597f_exp(typval_T *argvars, typval_T *rettv)
3598{
3599 float_T f = 0.0;
3600
3601 rettv->v_type = VAR_FLOAT;
3602 if (get_float_arg(argvars, &f) == OK)
3603 rettv->vval.v_float = exp(f);
3604 else
3605 rettv->vval.v_float = 0.0;
3606}
3607#endif
3608
3609/*
3610 * "expand()" function
3611 */
3612 static void
3613f_expand(typval_T *argvars, typval_T *rettv)
3614{
3615 char_u *s;
3616 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003617 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003618 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3619 expand_T xpc;
3620 int error = FALSE;
3621 char_u *result;
3622
3623 rettv->v_type = VAR_STRING;
3624 if (argvars[1].v_type != VAR_UNKNOWN
3625 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003626 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003627 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003628 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003629
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003630 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003631 if (*s == '%' || *s == '#' || *s == '<')
3632 {
3633 ++emsg_off;
3634 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3635 --emsg_off;
3636 if (rettv->v_type == VAR_LIST)
3637 {
3638 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3639 list_append_string(rettv->vval.v_list, result, -1);
3640 else
3641 vim_free(result);
3642 }
3643 else
3644 rettv->vval.v_string = result;
3645 }
3646 else
3647 {
3648 /* When the optional second argument is non-zero, don't remove matches
3649 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3650 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003651 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003652 options |= WILD_KEEP_ALL;
3653 if (!error)
3654 {
3655 ExpandInit(&xpc);
3656 xpc.xp_context = EXPAND_FILES;
3657 if (p_wic)
3658 options += WILD_ICASE;
3659 if (rettv->v_type == VAR_STRING)
3660 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3661 options, WILD_ALL);
3662 else if (rettv_list_alloc(rettv) != FAIL)
3663 {
3664 int i;
3665
3666 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3667 for (i = 0; i < xpc.xp_numfiles; i++)
3668 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3669 ExpandCleanup(&xpc);
3670 }
3671 }
3672 else
3673 rettv->vval.v_string = NULL;
3674 }
3675}
3676
3677/*
3678 * "extend(list, list [, idx])" function
3679 * "extend(dict, dict [, action])" function
3680 */
3681 static void
3682f_extend(typval_T *argvars, typval_T *rettv)
3683{
3684 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3685
3686 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3687 {
3688 list_T *l1, *l2;
3689 listitem_T *item;
3690 long before;
3691 int error = FALSE;
3692
3693 l1 = argvars[0].vval.v_list;
3694 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003695 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003696 && l2 != NULL)
3697 {
3698 if (argvars[2].v_type != VAR_UNKNOWN)
3699 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003700 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003701 if (error)
3702 return; /* type error; errmsg already given */
3703
3704 if (before == l1->lv_len)
3705 item = NULL;
3706 else
3707 {
3708 item = list_find(l1, before);
3709 if (item == NULL)
3710 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003711 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003712 return;
3713 }
3714 }
3715 }
3716 else
3717 item = NULL;
3718 list_extend(l1, l2, item);
3719
3720 copy_tv(&argvars[0], rettv);
3721 }
3722 }
3723 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3724 {
3725 dict_T *d1, *d2;
3726 char_u *action;
3727 int i;
3728
3729 d1 = argvars[0].vval.v_dict;
3730 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003731 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003732 && d2 != NULL)
3733 {
3734 /* Check the third argument. */
3735 if (argvars[2].v_type != VAR_UNKNOWN)
3736 {
3737 static char *(av[]) = {"keep", "force", "error"};
3738
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003739 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003740 if (action == NULL)
3741 return; /* type error; errmsg already given */
3742 for (i = 0; i < 3; ++i)
3743 if (STRCMP(action, av[i]) == 0)
3744 break;
3745 if (i == 3)
3746 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003747 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003748 return;
3749 }
3750 }
3751 else
3752 action = (char_u *)"force";
3753
3754 dict_extend(d1, d2, action);
3755
3756 copy_tv(&argvars[0], rettv);
3757 }
3758 }
3759 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003760 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003761}
3762
3763/*
3764 * "feedkeys()" function
3765 */
3766 static void
3767f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3768{
3769 int remap = TRUE;
3770 int insert = FALSE;
3771 char_u *keys, *flags;
3772 char_u nbuf[NUMBUFLEN];
3773 int typed = FALSE;
3774 int execute = FALSE;
3775 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003776 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003777 char_u *keys_esc;
3778
3779 /* This is not allowed in the sandbox. If the commands would still be
3780 * executed in the sandbox it would be OK, but it probably happens later,
3781 * when "sandbox" is no longer set. */
3782 if (check_secure())
3783 return;
3784
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003785 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003786
3787 if (argvars[1].v_type != VAR_UNKNOWN)
3788 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003789 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003790 for ( ; *flags != NUL; ++flags)
3791 {
3792 switch (*flags)
3793 {
3794 case 'n': remap = FALSE; break;
3795 case 'm': remap = TRUE; break;
3796 case 't': typed = TRUE; break;
3797 case 'i': insert = TRUE; break;
3798 case 'x': execute = TRUE; break;
3799 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003800 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003801 }
3802 }
3803 }
3804
3805 if (*keys != NUL || execute)
3806 {
3807 /* Need to escape K_SPECIAL and CSI before putting the string in the
3808 * typeahead buffer. */
3809 keys_esc = vim_strsave_escape_csi(keys);
3810 if (keys_esc != NULL)
3811 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003812 if (lowlevel)
3813 {
3814#ifdef USE_INPUT_BUF
3815 add_to_input_buf(keys, (int)STRLEN(keys));
3816#else
3817 emsg(_("E980: lowlevel input not supported"));
3818#endif
3819 }
3820 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003821 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003822 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003823 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003824 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003825#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003826 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003827#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003828 )
3829 typebuf_was_filled = TRUE;
3830 }
3831 vim_free(keys_esc);
3832
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003833 if (execute)
3834 {
3835 int save_msg_scroll = msg_scroll;
3836
3837 /* Avoid a 1 second delay when the keys start Insert mode. */
3838 msg_scroll = FALSE;
3839
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003840 if (!dangerous)
3841 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003842 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003843 if (!dangerous)
3844 --ex_normal_busy;
3845
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003846 msg_scroll |= save_msg_scroll;
3847 }
3848 }
3849 }
3850}
3851
3852/*
3853 * "filereadable()" function
3854 */
3855 static void
3856f_filereadable(typval_T *argvars, typval_T *rettv)
3857{
3858 int fd;
3859 char_u *p;
3860 int n;
3861
3862#ifndef O_NONBLOCK
3863# define O_NONBLOCK 0
3864#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003865 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003866 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3867 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3868 {
3869 n = TRUE;
3870 close(fd);
3871 }
3872 else
3873 n = FALSE;
3874
3875 rettv->vval.v_number = n;
3876}
3877
3878/*
3879 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3880 * rights to write into.
3881 */
3882 static void
3883f_filewritable(typval_T *argvars, typval_T *rettv)
3884{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003885 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003886}
3887
3888 static void
3889findfilendir(
3890 typval_T *argvars UNUSED,
3891 typval_T *rettv,
3892 int find_what UNUSED)
3893{
3894#ifdef FEAT_SEARCHPATH
3895 char_u *fname;
3896 char_u *fresult = NULL;
3897 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3898 char_u *p;
3899 char_u pathbuf[NUMBUFLEN];
3900 int count = 1;
3901 int first = TRUE;
3902 int error = FALSE;
3903#endif
3904
3905 rettv->vval.v_string = NULL;
3906 rettv->v_type = VAR_STRING;
3907
3908#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003909 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003910
3911 if (argvars[1].v_type != VAR_UNKNOWN)
3912 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003913 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003914 if (p == NULL)
3915 error = TRUE;
3916 else
3917 {
3918 if (*p != NUL)
3919 path = p;
3920
3921 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003922 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003923 }
3924 }
3925
3926 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3927 error = TRUE;
3928
3929 if (*fname != NUL && !error)
3930 {
3931 do
3932 {
3933 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3934 vim_free(fresult);
3935 fresult = find_file_in_path_option(first ? fname : NULL,
3936 first ? (int)STRLEN(fname) : 0,
3937 0, first, path,
3938 find_what,
3939 curbuf->b_ffname,
3940 find_what == FINDFILE_DIR
3941 ? (char_u *)"" : curbuf->b_p_sua);
3942 first = FALSE;
3943
3944 if (fresult != NULL && rettv->v_type == VAR_LIST)
3945 list_append_string(rettv->vval.v_list, fresult, -1);
3946
3947 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3948 }
3949
3950 if (rettv->v_type == VAR_STRING)
3951 rettv->vval.v_string = fresult;
3952#endif
3953}
3954
3955/*
3956 * "filter()" function
3957 */
3958 static void
3959f_filter(typval_T *argvars, typval_T *rettv)
3960{
3961 filter_map(argvars, rettv, FALSE);
3962}
3963
3964/*
3965 * "finddir({fname}[, {path}[, {count}]])" function
3966 */
3967 static void
3968f_finddir(typval_T *argvars, typval_T *rettv)
3969{
3970 findfilendir(argvars, rettv, FINDFILE_DIR);
3971}
3972
3973/*
3974 * "findfile({fname}[, {path}[, {count}]])" function
3975 */
3976 static void
3977f_findfile(typval_T *argvars, typval_T *rettv)
3978{
3979 findfilendir(argvars, rettv, FINDFILE_FILE);
3980}
3981
3982#ifdef FEAT_FLOAT
3983/*
3984 * "float2nr({float})" function
3985 */
3986 static void
3987f_float2nr(typval_T *argvars, typval_T *rettv)
3988{
3989 float_T f = 0.0;
3990
3991 if (get_float_arg(argvars, &f) == OK)
3992 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003993 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003994 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003995 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003996 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003997 else
3998 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003999 }
4000}
4001
4002/*
4003 * "floor({float})" function
4004 */
4005 static void
4006f_floor(typval_T *argvars, typval_T *rettv)
4007{
4008 float_T f = 0.0;
4009
4010 rettv->v_type = VAR_FLOAT;
4011 if (get_float_arg(argvars, &f) == OK)
4012 rettv->vval.v_float = floor(f);
4013 else
4014 rettv->vval.v_float = 0.0;
4015}
4016
4017/*
4018 * "fmod()" function
4019 */
4020 static void
4021f_fmod(typval_T *argvars, typval_T *rettv)
4022{
4023 float_T fx = 0.0, fy = 0.0;
4024
4025 rettv->v_type = VAR_FLOAT;
4026 if (get_float_arg(argvars, &fx) == OK
4027 && get_float_arg(&argvars[1], &fy) == OK)
4028 rettv->vval.v_float = fmod(fx, fy);
4029 else
4030 rettv->vval.v_float = 0.0;
4031}
4032#endif
4033
4034/*
4035 * "fnameescape({string})" function
4036 */
4037 static void
4038f_fnameescape(typval_T *argvars, typval_T *rettv)
4039{
4040 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004041 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004042 rettv->v_type = VAR_STRING;
4043}
4044
4045/*
4046 * "fnamemodify({fname}, {mods})" function
4047 */
4048 static void
4049f_fnamemodify(typval_T *argvars, typval_T *rettv)
4050{
4051 char_u *fname;
4052 char_u *mods;
4053 int usedlen = 0;
4054 int len;
4055 char_u *fbuf = NULL;
4056 char_u buf[NUMBUFLEN];
4057
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004058 fname = tv_get_string_chk(&argvars[0]);
4059 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004060 if (fname == NULL || mods == NULL)
4061 fname = NULL;
4062 else
4063 {
4064 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004065 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004066 }
4067
4068 rettv->v_type = VAR_STRING;
4069 if (fname == NULL)
4070 rettv->vval.v_string = NULL;
4071 else
4072 rettv->vval.v_string = vim_strnsave(fname, len);
4073 vim_free(fbuf);
4074}
4075
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004076/*
4077 * "foldclosed()" function
4078 */
4079 static void
4080foldclosed_both(
4081 typval_T *argvars UNUSED,
4082 typval_T *rettv,
4083 int end UNUSED)
4084{
4085#ifdef FEAT_FOLDING
4086 linenr_T lnum;
4087 linenr_T first, last;
4088
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004089 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004090 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4091 {
4092 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4093 {
4094 if (end)
4095 rettv->vval.v_number = (varnumber_T)last;
4096 else
4097 rettv->vval.v_number = (varnumber_T)first;
4098 return;
4099 }
4100 }
4101#endif
4102 rettv->vval.v_number = -1;
4103}
4104
4105/*
4106 * "foldclosed()" function
4107 */
4108 static void
4109f_foldclosed(typval_T *argvars, typval_T *rettv)
4110{
4111 foldclosed_both(argvars, rettv, FALSE);
4112}
4113
4114/*
4115 * "foldclosedend()" function
4116 */
4117 static void
4118f_foldclosedend(typval_T *argvars, typval_T *rettv)
4119{
4120 foldclosed_both(argvars, rettv, TRUE);
4121}
4122
4123/*
4124 * "foldlevel()" function
4125 */
4126 static void
4127f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4128{
4129#ifdef FEAT_FOLDING
4130 linenr_T lnum;
4131
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004132 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004133 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4134 rettv->vval.v_number = foldLevel(lnum);
4135#endif
4136}
4137
4138/*
4139 * "foldtext()" function
4140 */
4141 static void
4142f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4143{
4144#ifdef FEAT_FOLDING
4145 linenr_T foldstart;
4146 linenr_T foldend;
4147 char_u *dashes;
4148 linenr_T lnum;
4149 char_u *s;
4150 char_u *r;
4151 int len;
4152 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004153 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004154#endif
4155
4156 rettv->v_type = VAR_STRING;
4157 rettv->vval.v_string = NULL;
4158#ifdef FEAT_FOLDING
4159 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4160 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4161 dashes = get_vim_var_str(VV_FOLDDASHES);
4162 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4163 && dashes != NULL)
4164 {
4165 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004166 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004167 if (!linewhite(lnum))
4168 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004169
4170 /* Find interesting text in this line. */
4171 s = skipwhite(ml_get(lnum));
4172 /* skip C comment-start */
4173 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4174 {
4175 s = skipwhite(s + 2);
4176 if (*skipwhite(s) == NUL
4177 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4178 {
4179 s = skipwhite(ml_get(lnum + 1));
4180 if (*s == '*')
4181 s = skipwhite(s + 1);
4182 }
4183 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004184 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004185 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004186 r = alloc((unsigned)(STRLEN(txt)
4187 + STRLEN(dashes) /* for %s */
4188 + 20 /* for %3ld */
4189 + STRLEN(s))); /* concatenated */
4190 if (r != NULL)
4191 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004192 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004193 len = (int)STRLEN(r);
4194 STRCAT(r, s);
4195 /* remove 'foldmarker' and 'commentstring' */
4196 foldtext_cleanup(r + len);
4197 rettv->vval.v_string = r;
4198 }
4199 }
4200#endif
4201}
4202
4203/*
4204 * "foldtextresult(lnum)" function
4205 */
4206 static void
4207f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4208{
4209#ifdef FEAT_FOLDING
4210 linenr_T lnum;
4211 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004212 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004213 foldinfo_T foldinfo;
4214 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004215 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004216#endif
4217
4218 rettv->v_type = VAR_STRING;
4219 rettv->vval.v_string = NULL;
4220#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004221 if (entered)
4222 return; /* reject recursive use */
4223 entered = TRUE;
4224
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004225 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004226 /* treat illegal types and illegal string values for {lnum} the same */
4227 if (lnum < 0)
4228 lnum = 0;
4229 fold_count = foldedCount(curwin, lnum, &foldinfo);
4230 if (fold_count > 0)
4231 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004232 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4233 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004234 if (text == buf)
4235 text = vim_strsave(text);
4236 rettv->vval.v_string = text;
4237 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004238
4239 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004240#endif
4241}
4242
4243/*
4244 * "foreground()" function
4245 */
4246 static void
4247f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4248{
4249#ifdef FEAT_GUI
4250 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004251 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004252 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004253 return;
4254 }
4255#endif
4256#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004257 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004258#endif
4259}
4260
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004261 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004262common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004263{
4264 char_u *s;
4265 char_u *name;
4266 int use_string = FALSE;
4267 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004268 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004269
4270 if (argvars[0].v_type == VAR_FUNC)
4271 {
4272 /* function(MyFunc, [arg], dict) */
4273 s = argvars[0].vval.v_string;
4274 }
4275 else if (argvars[0].v_type == VAR_PARTIAL
4276 && argvars[0].vval.v_partial != NULL)
4277 {
4278 /* function(dict.MyFunc, [arg]) */
4279 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004280 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004281 }
4282 else
4283 {
4284 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004285 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004286 use_string = TRUE;
4287 }
4288
Bram Moolenaar843b8842016-08-21 14:36:15 +02004289 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004290 {
4291 name = s;
4292 trans_name = trans_function_name(&name, FALSE,
4293 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4294 if (*name != NUL)
4295 s = NULL;
4296 }
4297
Bram Moolenaar843b8842016-08-21 14:36:15 +02004298 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4299 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004300 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004301 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004302 else if (trans_name != NULL && (is_funcref
4303 ? find_func(trans_name) == NULL
4304 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004305 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004306 else
4307 {
4308 int dict_idx = 0;
4309 int arg_idx = 0;
4310 list_T *list = NULL;
4311
4312 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4313 {
4314 char sid_buf[25];
4315 int off = *s == 's' ? 2 : 5;
4316
4317 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4318 * also be called from another script. Using trans_function_name()
4319 * would also work, but some plugins depend on the name being
4320 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004321 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004322 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4323 if (name != NULL)
4324 {
4325 STRCPY(name, sid_buf);
4326 STRCAT(name, s + off);
4327 }
4328 }
4329 else
4330 name = vim_strsave(s);
4331
4332 if (argvars[1].v_type != VAR_UNKNOWN)
4333 {
4334 if (argvars[2].v_type != VAR_UNKNOWN)
4335 {
4336 /* function(name, [args], dict) */
4337 arg_idx = 1;
4338 dict_idx = 2;
4339 }
4340 else if (argvars[1].v_type == VAR_DICT)
4341 /* function(name, dict) */
4342 dict_idx = 1;
4343 else
4344 /* function(name, [args]) */
4345 arg_idx = 1;
4346 if (dict_idx > 0)
4347 {
4348 if (argvars[dict_idx].v_type != VAR_DICT)
4349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004350 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004351 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004352 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004353 }
4354 if (argvars[dict_idx].vval.v_dict == NULL)
4355 dict_idx = 0;
4356 }
4357 if (arg_idx > 0)
4358 {
4359 if (argvars[arg_idx].v_type != VAR_LIST)
4360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004361 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004362 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004363 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004364 }
4365 list = argvars[arg_idx].vval.v_list;
4366 if (list == NULL || list->lv_len == 0)
4367 arg_idx = 0;
4368 }
4369 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004370 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004371 {
4372 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4373
4374 /* result is a VAR_PARTIAL */
4375 if (pt == NULL)
4376 vim_free(name);
4377 else
4378 {
4379 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4380 {
4381 listitem_T *li;
4382 int i = 0;
4383 int arg_len = 0;
4384 int lv_len = 0;
4385
4386 if (arg_pt != NULL)
4387 arg_len = arg_pt->pt_argc;
4388 if (list != NULL)
4389 lv_len = list->lv_len;
4390 pt->pt_argc = arg_len + lv_len;
4391 pt->pt_argv = (typval_T *)alloc(
4392 sizeof(typval_T) * pt->pt_argc);
4393 if (pt->pt_argv == NULL)
4394 {
4395 vim_free(pt);
4396 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004397 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004398 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004399 for (i = 0; i < arg_len; i++)
4400 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4401 if (lv_len > 0)
4402 for (li = list->lv_first; li != NULL;
4403 li = li->li_next)
4404 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004405 }
4406
4407 /* For "function(dict.func, [], dict)" and "func" is a partial
4408 * use "dict". That is backwards compatible. */
4409 if (dict_idx > 0)
4410 {
4411 /* The dict is bound explicitly, pt_auto is FALSE. */
4412 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4413 ++pt->pt_dict->dv_refcount;
4414 }
4415 else if (arg_pt != NULL)
4416 {
4417 /* If the dict was bound automatically the result is also
4418 * bound automatically. */
4419 pt->pt_dict = arg_pt->pt_dict;
4420 pt->pt_auto = arg_pt->pt_auto;
4421 if (pt->pt_dict != NULL)
4422 ++pt->pt_dict->dv_refcount;
4423 }
4424
4425 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004426 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4427 {
4428 pt->pt_func = arg_pt->pt_func;
4429 func_ptr_ref(pt->pt_func);
4430 vim_free(name);
4431 }
4432 else if (is_funcref)
4433 {
4434 pt->pt_func = find_func(trans_name);
4435 func_ptr_ref(pt->pt_func);
4436 vim_free(name);
4437 }
4438 else
4439 {
4440 pt->pt_name = name;
4441 func_ref(name);
4442 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004443 }
4444 rettv->v_type = VAR_PARTIAL;
4445 rettv->vval.v_partial = pt;
4446 }
4447 else
4448 {
4449 /* result is a VAR_FUNC */
4450 rettv->v_type = VAR_FUNC;
4451 rettv->vval.v_string = name;
4452 func_ref(name);
4453 }
4454 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004455theend:
4456 vim_free(trans_name);
4457}
4458
4459/*
4460 * "funcref()" function
4461 */
4462 static void
4463f_funcref(typval_T *argvars, typval_T *rettv)
4464{
4465 common_function(argvars, rettv, TRUE);
4466}
4467
4468/*
4469 * "function()" function
4470 */
4471 static void
4472f_function(typval_T *argvars, typval_T *rettv)
4473{
4474 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004475}
4476
4477/*
4478 * "garbagecollect()" function
4479 */
4480 static void
4481f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4482{
4483 /* This is postponed until we are back at the toplevel, because we may be
4484 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4485 want_garbage_collect = TRUE;
4486
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004487 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004488 garbage_collect_at_exit = TRUE;
4489}
4490
4491/*
4492 * "get()" function
4493 */
4494 static void
4495f_get(typval_T *argvars, typval_T *rettv)
4496{
4497 listitem_T *li;
4498 list_T *l;
4499 dictitem_T *di;
4500 dict_T *d;
4501 typval_T *tv = NULL;
4502
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004503 if (argvars[0].v_type == VAR_BLOB)
4504 {
4505 int error = FALSE;
4506 int idx = tv_get_number_chk(&argvars[1], &error);
4507
4508 if (!error)
4509 {
4510 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004511 if (idx < 0)
4512 idx = blob_len(argvars[0].vval.v_blob) + idx;
4513 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4514 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004515 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004516 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004517 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004518 tv = rettv;
4519 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004520 }
4521 }
4522 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004523 {
4524 if ((l = argvars[0].vval.v_list) != NULL)
4525 {
4526 int error = FALSE;
4527
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004528 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004529 if (!error && li != NULL)
4530 tv = &li->li_tv;
4531 }
4532 }
4533 else if (argvars[0].v_type == VAR_DICT)
4534 {
4535 if ((d = argvars[0].vval.v_dict) != NULL)
4536 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004537 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538 if (di != NULL)
4539 tv = &di->di_tv;
4540 }
4541 }
4542 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4543 {
4544 partial_T *pt;
4545 partial_T fref_pt;
4546
4547 if (argvars[0].v_type == VAR_PARTIAL)
4548 pt = argvars[0].vval.v_partial;
4549 else
4550 {
4551 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4552 fref_pt.pt_name = argvars[0].vval.v_string;
4553 pt = &fref_pt;
4554 }
4555
4556 if (pt != NULL)
4557 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004558 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004559 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004560
4561 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4562 {
4563 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004564 n = partial_name(pt);
4565 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004566 rettv->vval.v_string = NULL;
4567 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004568 {
4569 rettv->vval.v_string = vim_strsave(n);
4570 if (rettv->v_type == VAR_FUNC)
4571 func_ref(rettv->vval.v_string);
4572 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004573 }
4574 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004575 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004576 else if (STRCMP(what, "args") == 0)
4577 {
4578 rettv->v_type = VAR_LIST;
4579 if (rettv_list_alloc(rettv) == OK)
4580 {
4581 int i;
4582
4583 for (i = 0; i < pt->pt_argc; ++i)
4584 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4585 }
4586 }
4587 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004588 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004589 return;
4590 }
4591 }
4592 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004593 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004594
4595 if (tv == NULL)
4596 {
4597 if (argvars[2].v_type != VAR_UNKNOWN)
4598 copy_tv(&argvars[2], rettv);
4599 }
4600 else
4601 copy_tv(tv, rettv);
4602}
4603
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004604/*
4605 * Returns buffer options, variables and other attributes in a dictionary.
4606 */
4607 static dict_T *
4608get_buffer_info(buf_T *buf)
4609{
4610 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004611 tabpage_T *tp;
4612 win_T *wp;
4613 list_T *windows;
4614
4615 dict = dict_alloc();
4616 if (dict == NULL)
4617 return NULL;
4618
Bram Moolenaare0be1672018-07-08 16:50:37 +02004619 dict_add_number(dict, "bufnr", buf->b_fnum);
4620 dict_add_string(dict, "name", buf->b_ffname);
4621 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4622 : buflist_findlnum(buf));
4623 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4624 dict_add_number(dict, "listed", buf->b_p_bl);
4625 dict_add_number(dict, "changed", bufIsChanged(buf));
4626 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4627 dict_add_number(dict, "hidden",
4628 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004629
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004630 /* Get a reference to buffer variables */
4631 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004632
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004633 /* List of windows displaying this buffer */
4634 windows = list_alloc();
4635 if (windows != NULL)
4636 {
4637 FOR_ALL_TAB_WINDOWS(tp, wp)
4638 if (wp->w_buffer == buf)
4639 list_append_number(windows, (varnumber_T)wp->w_id);
4640 dict_add_list(dict, "windows", windows);
4641 }
4642
4643#ifdef FEAT_SIGNS
4644 if (buf->b_signlist != NULL)
4645 {
4646 /* List of signs placed in this buffer */
4647 list_T *signs = list_alloc();
4648 if (signs != NULL)
4649 {
4650 get_buffer_signs(buf, signs);
4651 dict_add_list(dict, "signs", signs);
4652 }
4653 }
4654#endif
4655
4656 return dict;
4657}
4658
4659/*
4660 * "getbufinfo()" function
4661 */
4662 static void
4663f_getbufinfo(typval_T *argvars, typval_T *rettv)
4664{
4665 buf_T *buf = NULL;
4666 buf_T *argbuf = NULL;
4667 dict_T *d;
4668 int filtered = FALSE;
4669 int sel_buflisted = FALSE;
4670 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004671 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004672
4673 if (rettv_list_alloc(rettv) != OK)
4674 return;
4675
4676 /* List of all the buffers or selected buffers */
4677 if (argvars[0].v_type == VAR_DICT)
4678 {
4679 dict_T *sel_d = argvars[0].vval.v_dict;
4680
4681 if (sel_d != NULL)
4682 {
4683 dictitem_T *di;
4684
4685 filtered = TRUE;
4686
4687 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004688 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004689 sel_buflisted = TRUE;
4690
4691 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004692 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004693 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004694
4695 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004696 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004697 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004698 }
4699 }
4700 else if (argvars[0].v_type != VAR_UNKNOWN)
4701 {
4702 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004703 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004704 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004705 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004706 --emsg_off;
4707 if (argbuf == NULL)
4708 return;
4709 }
4710
4711 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004712 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004713 {
4714 if (argbuf != NULL && argbuf != buf)
4715 continue;
4716 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004717 || (sel_buflisted && !buf->b_p_bl)
4718 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004719 continue;
4720
4721 d = get_buffer_info(buf);
4722 if (d != NULL)
4723 list_append_dict(rettv->vval.v_list, d);
4724 if (argbuf != NULL)
4725 return;
4726 }
4727}
4728
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004729/*
4730 * Get line or list of lines from buffer "buf" into "rettv".
4731 * Return a range (from start to end) of lines in rettv from the specified
4732 * buffer.
4733 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4734 */
4735 static void
4736get_buffer_lines(
4737 buf_T *buf,
4738 linenr_T start,
4739 linenr_T end,
4740 int retlist,
4741 typval_T *rettv)
4742{
4743 char_u *p;
4744
4745 rettv->v_type = VAR_STRING;
4746 rettv->vval.v_string = NULL;
4747 if (retlist && rettv_list_alloc(rettv) == FAIL)
4748 return;
4749
4750 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4751 return;
4752
4753 if (!retlist)
4754 {
4755 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4756 p = ml_get_buf(buf, start, FALSE);
4757 else
4758 p = (char_u *)"";
4759 rettv->vval.v_string = vim_strsave(p);
4760 }
4761 else
4762 {
4763 if (end < start)
4764 return;
4765
4766 if (start < 1)
4767 start = 1;
4768 if (end > buf->b_ml.ml_line_count)
4769 end = buf->b_ml.ml_line_count;
4770 while (start <= end)
4771 if (list_append_string(rettv->vval.v_list,
4772 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4773 break;
4774 }
4775}
4776
4777/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004778 * "getbufline()" function
4779 */
4780 static void
4781f_getbufline(typval_T *argvars, typval_T *rettv)
4782{
4783 linenr_T lnum;
4784 linenr_T end;
4785 buf_T *buf;
4786
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004787 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004788 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004789 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004790 --emsg_off;
4791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004792 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004793 if (argvars[2].v_type == VAR_UNKNOWN)
4794 end = lnum;
4795 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004796 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004797
4798 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4799}
4800
4801/*
4802 * "getbufvar()" function
4803 */
4804 static void
4805f_getbufvar(typval_T *argvars, typval_T *rettv)
4806{
4807 buf_T *buf;
4808 buf_T *save_curbuf;
4809 char_u *varname;
4810 dictitem_T *v;
4811 int done = FALSE;
4812
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004813 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4814 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004815 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004816 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004817
4818 rettv->v_type = VAR_STRING;
4819 rettv->vval.v_string = NULL;
4820
4821 if (buf != NULL && varname != NULL)
4822 {
4823 /* set curbuf to be our buf, temporarily */
4824 save_curbuf = curbuf;
4825 curbuf = buf;
4826
Bram Moolenaar30567352016-08-27 21:25:44 +02004827 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004828 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004829 if (varname[1] == NUL)
4830 {
4831 /* get all buffer-local options in a dict */
4832 dict_T *opts = get_winbuf_options(TRUE);
4833
4834 if (opts != NULL)
4835 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004836 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004837 done = TRUE;
4838 }
4839 }
4840 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4841 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004842 done = TRUE;
4843 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004844 else
4845 {
4846 /* Look up the variable. */
4847 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4848 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4849 'b', varname, FALSE);
4850 if (v != NULL)
4851 {
4852 copy_tv(&v->di_tv, rettv);
4853 done = TRUE;
4854 }
4855 }
4856
4857 /* restore previous notion of curbuf */
4858 curbuf = save_curbuf;
4859 }
4860
4861 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4862 /* use the default value */
4863 copy_tv(&argvars[2], rettv);
4864
4865 --emsg_off;
4866}
4867
4868/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004869 * "getchangelist()" function
4870 */
4871 static void
4872f_getchangelist(typval_T *argvars, typval_T *rettv)
4873{
4874#ifdef FEAT_JUMPLIST
4875 buf_T *buf;
4876 int i;
4877 list_T *l;
4878 dict_T *d;
4879#endif
4880
4881 if (rettv_list_alloc(rettv) != OK)
4882 return;
4883
4884#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004885 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004886 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004887 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004888 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004889 if (buf == NULL)
4890 return;
4891
4892 l = list_alloc();
4893 if (l == NULL)
4894 return;
4895
4896 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4897 return;
4898 /*
4899 * The current window change list index tracks only the position in the
4900 * current buffer change list. For other buffers, use the change list
4901 * length as the current index.
4902 */
4903 list_append_number(rettv->vval.v_list,
4904 (varnumber_T)((buf == curwin->w_buffer)
4905 ? curwin->w_changelistidx : buf->b_changelistlen));
4906
4907 for (i = 0; i < buf->b_changelistlen; ++i)
4908 {
4909 if (buf->b_changelist[i].lnum == 0)
4910 continue;
4911 if ((d = dict_alloc()) == NULL)
4912 return;
4913 if (list_append_dict(l, d) == FAIL)
4914 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004915 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4916 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004917 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004918 }
4919#endif
4920}
4921/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004922 * "getchar()" function
4923 */
4924 static void
4925f_getchar(typval_T *argvars, typval_T *rettv)
4926{
4927 varnumber_T n;
4928 int error = FALSE;
4929
Bram Moolenaar84d93902018-09-11 20:10:20 +02004930#ifdef MESSAGE_QUEUE
4931 // vpeekc() used to check for messages, but that caused problems, invoking
4932 // a callback where it was not expected. Some plugins use getchar(1) in a
4933 // loop to await a message, therefore make sure we check for messages here.
4934 parse_queued_messages();
4935#endif
4936
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004937 /* Position the cursor. Needed after a message that ends in a space. */
4938 windgoto(msg_row, msg_col);
4939
4940 ++no_mapping;
4941 ++allow_keys;
4942 for (;;)
4943 {
4944 if (argvars[0].v_type == VAR_UNKNOWN)
4945 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004946 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004947 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004948 /* getchar(1): only check if char avail */
4949 n = vpeekc_any();
4950 else if (error || vpeekc_any() == NUL)
4951 /* illegal argument or getchar(0) and no char avail: return zero */
4952 n = 0;
4953 else
4954 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004955 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004956
4957 if (n == K_IGNORE)
4958 continue;
4959 break;
4960 }
4961 --no_mapping;
4962 --allow_keys;
4963
4964 set_vim_var_nr(VV_MOUSE_WIN, 0);
4965 set_vim_var_nr(VV_MOUSE_WINID, 0);
4966 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4967 set_vim_var_nr(VV_MOUSE_COL, 0);
4968
4969 rettv->vval.v_number = n;
4970 if (IS_SPECIAL(n) || mod_mask != 0)
4971 {
4972 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4973 int i = 0;
4974
4975 /* Turn a special key into three bytes, plus modifier. */
4976 if (mod_mask != 0)
4977 {
4978 temp[i++] = K_SPECIAL;
4979 temp[i++] = KS_MODIFIER;
4980 temp[i++] = mod_mask;
4981 }
4982 if (IS_SPECIAL(n))
4983 {
4984 temp[i++] = K_SPECIAL;
4985 temp[i++] = K_SECOND(n);
4986 temp[i++] = K_THIRD(n);
4987 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004988 else if (has_mbyte)
4989 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004990 else
4991 temp[i++] = n;
4992 temp[i++] = NUL;
4993 rettv->v_type = VAR_STRING;
4994 rettv->vval.v_string = vim_strsave(temp);
4995
4996#ifdef FEAT_MOUSE
4997 if (is_mouse_key(n))
4998 {
4999 int row = mouse_row;
5000 int col = mouse_col;
5001 win_T *win;
5002 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005003 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005004 int winnr = 1;
5005
5006 if (row >= 0 && col >= 0)
5007 {
5008 /* Find the window at the mouse coordinates and compute the
5009 * text position. */
5010 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005011 if (win == NULL)
5012 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005013 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005014 for (wp = firstwin; wp != win; wp = wp->w_next)
5015 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005016 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5017 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5018 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5019 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5020 }
5021 }
5022#endif
5023 }
5024}
5025
5026/*
5027 * "getcharmod()" function
5028 */
5029 static void
5030f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5031{
5032 rettv->vval.v_number = mod_mask;
5033}
5034
5035/*
5036 * "getcharsearch()" function
5037 */
5038 static void
5039f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5040{
5041 if (rettv_dict_alloc(rettv) != FAIL)
5042 {
5043 dict_T *dict = rettv->vval.v_dict;
5044
Bram Moolenaare0be1672018-07-08 16:50:37 +02005045 dict_add_string(dict, "char", last_csearch());
5046 dict_add_number(dict, "forward", last_csearch_forward());
5047 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005048 }
5049}
5050
5051/*
5052 * "getcmdline()" function
5053 */
5054 static void
5055f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5056{
5057 rettv->v_type = VAR_STRING;
5058 rettv->vval.v_string = get_cmdline_str();
5059}
5060
5061/*
5062 * "getcmdpos()" function
5063 */
5064 static void
5065f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5066{
5067 rettv->vval.v_number = get_cmdline_pos() + 1;
5068}
5069
5070/*
5071 * "getcmdtype()" function
5072 */
5073 static void
5074f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5075{
5076 rettv->v_type = VAR_STRING;
5077 rettv->vval.v_string = alloc(2);
5078 if (rettv->vval.v_string != NULL)
5079 {
5080 rettv->vval.v_string[0] = get_cmdline_type();
5081 rettv->vval.v_string[1] = NUL;
5082 }
5083}
5084
5085/*
5086 * "getcmdwintype()" function
5087 */
5088 static void
5089f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5090{
5091 rettv->v_type = VAR_STRING;
5092 rettv->vval.v_string = NULL;
5093#ifdef FEAT_CMDWIN
5094 rettv->vval.v_string = alloc(2);
5095 if (rettv->vval.v_string != NULL)
5096 {
5097 rettv->vval.v_string[0] = cmdwin_type;
5098 rettv->vval.v_string[1] = NUL;
5099 }
5100#endif
5101}
5102
5103#if defined(FEAT_CMDL_COMPL)
5104/*
5105 * "getcompletion()" function
5106 */
5107 static void
5108f_getcompletion(typval_T *argvars, typval_T *rettv)
5109{
5110 char_u *pat;
5111 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005112 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005113 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5114 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005115
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005116 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005117 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005118
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005119 if (p_wic)
5120 options |= WILD_ICASE;
5121
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005122 /* For filtered results, 'wildignore' is used */
5123 if (!filtered)
5124 options |= WILD_KEEP_ALL;
5125
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005126 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005127 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005128 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005129 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005130 if (xpc.xp_context == EXPAND_NOTHING)
5131 {
5132 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005133 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005134 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005135 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005136 return;
5137 }
5138
5139# if defined(FEAT_MENU)
5140 if (xpc.xp_context == EXPAND_MENUS)
5141 {
5142 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5143 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5144 }
5145# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005146#ifdef FEAT_CSCOPE
5147 if (xpc.xp_context == EXPAND_CSCOPE)
5148 {
5149 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5150 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5151 }
5152#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005153#ifdef FEAT_SIGNS
5154 if (xpc.xp_context == EXPAND_SIGN)
5155 {
5156 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5157 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5158 }
5159#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005160
5161 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5162 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5163 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005164 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005165
5166 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5167
5168 for (i = 0; i < xpc.xp_numfiles; i++)
5169 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5170 }
5171 vim_free(pat);
5172 ExpandCleanup(&xpc);
5173}
5174#endif
5175
5176/*
5177 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005178 *
5179 * Return the current working directory of a window in a tab page.
5180 * First optional argument 'winnr' is the window number or -1 and the second
5181 * optional argument 'tabnr' is the tab page number.
5182 *
5183 * If no arguments are supplied, then return the directory of the current
5184 * window.
5185 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5186 * the specified window.
5187 * If 'winnr' is 0 then return the directory of the current window.
5188 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5189 * directory of the specified tab page. Otherwise return the directory of the
5190 * specified window in the specified tab page.
5191 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005192 */
5193 static void
5194f_getcwd(typval_T *argvars, typval_T *rettv)
5195{
5196 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005197 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005198 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005199 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005200
5201 rettv->v_type = VAR_STRING;
5202 rettv->vval.v_string = NULL;
5203
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005204 if (argvars[0].v_type == VAR_NUMBER
5205 && argvars[0].vval.v_number == -1
5206 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005207 global = TRUE;
5208 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005209 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005210
5211 if (wp != NULL && wp->w_localdir != NULL)
5212 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005213 else if (tp != NULL && tp->tp_localdir != NULL)
5214 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5215 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005216 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005217 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005218 rettv->vval.v_string = vim_strsave(globaldir);
5219 else
5220 {
5221 cwd = alloc(MAXPATHL);
5222 if (cwd != NULL)
5223 {
5224 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5225 rettv->vval.v_string = vim_strsave(cwd);
5226 vim_free(cwd);
5227 }
5228 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005229 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005230#ifdef BACKSLASH_IN_FILENAME
5231 if (rettv->vval.v_string != NULL)
5232 slash_adjust(rettv->vval.v_string);
5233#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005234}
5235
5236/*
5237 * "getfontname()" function
5238 */
5239 static void
5240f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5241{
5242 rettv->v_type = VAR_STRING;
5243 rettv->vval.v_string = NULL;
5244#ifdef FEAT_GUI
5245 if (gui.in_use)
5246 {
5247 GuiFont font;
5248 char_u *name = NULL;
5249
5250 if (argvars[0].v_type == VAR_UNKNOWN)
5251 {
5252 /* Get the "Normal" font. Either the name saved by
5253 * hl_set_font_name() or from the font ID. */
5254 font = gui.norm_font;
5255 name = hl_get_font_name();
5256 }
5257 else
5258 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005259 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005260 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5261 return;
5262 font = gui_mch_get_font(name, FALSE);
5263 if (font == NOFONT)
5264 return; /* Invalid font name, return empty string. */
5265 }
5266 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5267 if (argvars[0].v_type != VAR_UNKNOWN)
5268 gui_mch_free_font(font);
5269 }
5270#endif
5271}
5272
5273/*
5274 * "getfperm({fname})" function
5275 */
5276 static void
5277f_getfperm(typval_T *argvars, typval_T *rettv)
5278{
5279 char_u *fname;
5280 stat_T st;
5281 char_u *perm = NULL;
5282 char_u flags[] = "rwx";
5283 int i;
5284
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005285 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005286
5287 rettv->v_type = VAR_STRING;
5288 if (mch_stat((char *)fname, &st) >= 0)
5289 {
5290 perm = vim_strsave((char_u *)"---------");
5291 if (perm != NULL)
5292 {
5293 for (i = 0; i < 9; i++)
5294 {
5295 if (st.st_mode & (1 << (8 - i)))
5296 perm[i] = flags[i % 3];
5297 }
5298 }
5299 }
5300 rettv->vval.v_string = perm;
5301}
5302
5303/*
5304 * "getfsize({fname})" function
5305 */
5306 static void
5307f_getfsize(typval_T *argvars, typval_T *rettv)
5308{
5309 char_u *fname;
5310 stat_T st;
5311
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005312 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005313
5314 rettv->v_type = VAR_NUMBER;
5315
5316 if (mch_stat((char *)fname, &st) >= 0)
5317 {
5318 if (mch_isdir(fname))
5319 rettv->vval.v_number = 0;
5320 else
5321 {
5322 rettv->vval.v_number = (varnumber_T)st.st_size;
5323
5324 /* non-perfect check for overflow */
5325 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5326 rettv->vval.v_number = -2;
5327 }
5328 }
5329 else
5330 rettv->vval.v_number = -1;
5331}
5332
5333/*
5334 * "getftime({fname})" function
5335 */
5336 static void
5337f_getftime(typval_T *argvars, typval_T *rettv)
5338{
5339 char_u *fname;
5340 stat_T st;
5341
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005342 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005343
5344 if (mch_stat((char *)fname, &st) >= 0)
5345 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5346 else
5347 rettv->vval.v_number = -1;
5348}
5349
5350/*
5351 * "getftype({fname})" function
5352 */
5353 static void
5354f_getftype(typval_T *argvars, typval_T *rettv)
5355{
5356 char_u *fname;
5357 stat_T st;
5358 char_u *type = NULL;
5359 char *t;
5360
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005361 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005362
5363 rettv->v_type = VAR_STRING;
5364 if (mch_lstat((char *)fname, &st) >= 0)
5365 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005366 if (S_ISREG(st.st_mode))
5367 t = "file";
5368 else if (S_ISDIR(st.st_mode))
5369 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005370 else if (S_ISLNK(st.st_mode))
5371 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005372 else if (S_ISBLK(st.st_mode))
5373 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005374 else if (S_ISCHR(st.st_mode))
5375 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005376 else if (S_ISFIFO(st.st_mode))
5377 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005378 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005379 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005380 else
5381 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005382 type = vim_strsave((char_u *)t);
5383 }
5384 rettv->vval.v_string = type;
5385}
5386
5387/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005388 * "getjumplist()" function
5389 */
5390 static void
5391f_getjumplist(typval_T *argvars, typval_T *rettv)
5392{
5393#ifdef FEAT_JUMPLIST
5394 win_T *wp;
5395 int i;
5396 list_T *l;
5397 dict_T *d;
5398#endif
5399
5400 if (rettv_list_alloc(rettv) != OK)
5401 return;
5402
5403#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005404 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005405 if (wp == NULL)
5406 return;
5407
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005408 cleanup_jumplist(wp, TRUE);
5409
Bram Moolenaar4f505882018-02-10 21:06:32 +01005410 l = list_alloc();
5411 if (l == NULL)
5412 return;
5413
5414 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5415 return;
5416 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5417
5418 for (i = 0; i < wp->w_jumplistlen; ++i)
5419 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005420 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5421 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005422 if ((d = dict_alloc()) == NULL)
5423 return;
5424 if (list_append_dict(l, d) == FAIL)
5425 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005426 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5427 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005428 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005429 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005430 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005431 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005432 }
5433#endif
5434}
5435
5436/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005437 * "getline(lnum, [end])" function
5438 */
5439 static void
5440f_getline(typval_T *argvars, typval_T *rettv)
5441{
5442 linenr_T lnum;
5443 linenr_T end;
5444 int retlist;
5445
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005446 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005447 if (argvars[1].v_type == VAR_UNKNOWN)
5448 {
5449 end = 0;
5450 retlist = FALSE;
5451 }
5452 else
5453 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005454 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005455 retlist = TRUE;
5456 }
5457
5458 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5459}
5460
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005461#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005462 static void
5463get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5464{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005465 if (what_arg->v_type == VAR_UNKNOWN)
5466 {
5467 if (rettv_list_alloc(rettv) == OK)
5468 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005469 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005470 }
5471 else
5472 {
5473 if (rettv_dict_alloc(rettv) == OK)
5474 if (is_qf || (wp != NULL))
5475 {
5476 if (what_arg->v_type == VAR_DICT)
5477 {
5478 dict_T *d = what_arg->vval.v_dict;
5479
5480 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005481 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005482 }
5483 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005484 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005485 }
5486 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005487}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005488#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005489
5490/*
5491 * "getloclist()" function
5492 */
5493 static void
5494f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5495{
5496#ifdef FEAT_QUICKFIX
5497 win_T *wp;
5498
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005499 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005500 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5501#endif
5502}
5503
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005504/*
5505 * "getmatches()" function
5506 */
5507 static void
5508f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5509{
5510#ifdef FEAT_SEARCH_EXTRA
5511 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005512 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005513 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005514 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005515
Bram Moolenaaraff74912019-03-30 18:11:49 +01005516 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5517 return;
5518
5519 cur = win->w_match_head;
5520 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005521 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005522 dict = dict_alloc();
5523 if (dict == NULL)
5524 return;
5525 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005526 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005527 /* match added with matchaddpos() */
5528 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005529 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005530 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005531 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005532 list_T *l;
5533
5534 llpos = &cur->pos.pos[i];
5535 if (llpos->lnum == 0)
5536 break;
5537 l = list_alloc();
5538 if (l == NULL)
5539 break;
5540 list_append_number(l, (varnumber_T)llpos->lnum);
5541 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005542 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005543 list_append_number(l, (varnumber_T)llpos->col);
5544 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005545 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005546 sprintf(buf, "pos%d", i + 1);
5547 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005548 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005549 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005550 else
5551 {
5552 dict_add_string(dict, "pattern", cur->pattern);
5553 }
5554 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5555 dict_add_number(dict, "priority", (long)cur->priority);
5556 dict_add_number(dict, "id", (long)cur->id);
5557# if defined(FEAT_CONCEAL)
5558 if (cur->conceal_char)
5559 {
5560 char_u buf[MB_MAXBYTES + 1];
5561
5562 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5563 dict_add_string(dict, "conceal", (char_u *)&buf);
5564 }
5565# endif
5566 list_append_dict(rettv->vval.v_list, dict);
5567 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005568 }
5569#endif
5570}
5571
5572/*
5573 * "getpid()" function
5574 */
5575 static void
5576f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5577{
5578 rettv->vval.v_number = mch_get_pid();
5579}
5580
5581 static void
5582getpos_both(
5583 typval_T *argvars,
5584 typval_T *rettv,
5585 int getcurpos)
5586{
5587 pos_T *fp;
5588 list_T *l;
5589 int fnum = -1;
5590
5591 if (rettv_list_alloc(rettv) == OK)
5592 {
5593 l = rettv->vval.v_list;
5594 if (getcurpos)
5595 fp = &curwin->w_cursor;
5596 else
5597 fp = var2fpos(&argvars[0], TRUE, &fnum);
5598 if (fnum != -1)
5599 list_append_number(l, (varnumber_T)fnum);
5600 else
5601 list_append_number(l, (varnumber_T)0);
5602 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5603 : (varnumber_T)0);
5604 list_append_number(l, (fp != NULL)
5605 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5606 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005607 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005608 (varnumber_T)0);
5609 if (getcurpos)
5610 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005611 int save_set_curswant = curwin->w_set_curswant;
5612 colnr_T save_curswant = curwin->w_curswant;
5613 colnr_T save_virtcol = curwin->w_virtcol;
5614
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005615 update_curswant();
5616 list_append_number(l, curwin->w_curswant == MAXCOL ?
5617 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005618
5619 // Do not change "curswant", as it is unexpected that a get
5620 // function has a side effect.
5621 if (save_set_curswant)
5622 {
5623 curwin->w_set_curswant = save_set_curswant;
5624 curwin->w_curswant = save_curswant;
5625 curwin->w_virtcol = save_virtcol;
5626 curwin->w_valid &= ~VALID_VIRTCOL;
5627 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005628 }
5629 }
5630 else
5631 rettv->vval.v_number = FALSE;
5632}
5633
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005634/*
5635 * "getcurpos()" function
5636 */
5637 static void
5638f_getcurpos(typval_T *argvars, typval_T *rettv)
5639{
5640 getpos_both(argvars, rettv, TRUE);
5641}
5642
5643/*
5644 * "getpos(string)" function
5645 */
5646 static void
5647f_getpos(typval_T *argvars, typval_T *rettv)
5648{
5649 getpos_both(argvars, rettv, FALSE);
5650}
5651
5652/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005653 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005654 */
5655 static void
5656f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5657{
5658#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005659 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005660#endif
5661}
5662
5663/*
5664 * "getreg()" function
5665 */
5666 static void
5667f_getreg(typval_T *argvars, typval_T *rettv)
5668{
5669 char_u *strregname;
5670 int regname;
5671 int arg2 = FALSE;
5672 int return_list = FALSE;
5673 int error = FALSE;
5674
5675 if (argvars[0].v_type != VAR_UNKNOWN)
5676 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005677 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005678 error = strregname == NULL;
5679 if (argvars[1].v_type != VAR_UNKNOWN)
5680 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005681 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005682 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005683 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005684 }
5685 }
5686 else
5687 strregname = get_vim_var_str(VV_REG);
5688
5689 if (error)
5690 return;
5691
5692 regname = (strregname == NULL ? '"' : *strregname);
5693 if (regname == 0)
5694 regname = '"';
5695
5696 if (return_list)
5697 {
5698 rettv->v_type = VAR_LIST;
5699 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5700 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5701 if (rettv->vval.v_list == NULL)
5702 (void)rettv_list_alloc(rettv);
5703 else
5704 ++rettv->vval.v_list->lv_refcount;
5705 }
5706 else
5707 {
5708 rettv->v_type = VAR_STRING;
5709 rettv->vval.v_string = get_reg_contents(regname,
5710 arg2 ? GREG_EXPR_SRC : 0);
5711 }
5712}
5713
5714/*
5715 * "getregtype()" function
5716 */
5717 static void
5718f_getregtype(typval_T *argvars, typval_T *rettv)
5719{
5720 char_u *strregname;
5721 int regname;
5722 char_u buf[NUMBUFLEN + 2];
5723 long reglen = 0;
5724
5725 if (argvars[0].v_type != VAR_UNKNOWN)
5726 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005727 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005728 if (strregname == NULL) /* type error; errmsg already given */
5729 {
5730 rettv->v_type = VAR_STRING;
5731 rettv->vval.v_string = NULL;
5732 return;
5733 }
5734 }
5735 else
5736 /* Default to v:register */
5737 strregname = get_vim_var_str(VV_REG);
5738
5739 regname = (strregname == NULL ? '"' : *strregname);
5740 if (regname == 0)
5741 regname = '"';
5742
5743 buf[0] = NUL;
5744 buf[1] = NUL;
5745 switch (get_reg_type(regname, &reglen))
5746 {
5747 case MLINE: buf[0] = 'V'; break;
5748 case MCHAR: buf[0] = 'v'; break;
5749 case MBLOCK:
5750 buf[0] = Ctrl_V;
5751 sprintf((char *)buf + 1, "%ld", reglen + 1);
5752 break;
5753 }
5754 rettv->v_type = VAR_STRING;
5755 rettv->vval.v_string = vim_strsave(buf);
5756}
5757
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005758/*
5759 * Returns information (variables, options, etc.) about a tab page
5760 * as a dictionary.
5761 */
5762 static dict_T *
5763get_tabpage_info(tabpage_T *tp, int tp_idx)
5764{
5765 win_T *wp;
5766 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005767 list_T *l;
5768
5769 dict = dict_alloc();
5770 if (dict == NULL)
5771 return NULL;
5772
Bram Moolenaare0be1672018-07-08 16:50:37 +02005773 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005774
5775 l = list_alloc();
5776 if (l != NULL)
5777 {
5778 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5779 wp; wp = wp->w_next)
5780 list_append_number(l, (varnumber_T)wp->w_id);
5781 dict_add_list(dict, "windows", l);
5782 }
5783
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005784 /* Make a reference to tabpage variables */
5785 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005786
5787 return dict;
5788}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005789
5790/*
5791 * "gettabinfo()" function
5792 */
5793 static void
5794f_gettabinfo(typval_T *argvars, typval_T *rettv)
5795{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005796 tabpage_T *tp, *tparg = NULL;
5797 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005798 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005799
5800 if (rettv_list_alloc(rettv) != OK)
5801 return;
5802
5803 if (argvars[0].v_type != VAR_UNKNOWN)
5804 {
5805 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005806 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005807 if (tparg == NULL)
5808 return;
5809 }
5810
5811 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005812 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005813 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005814 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005815 if (tparg != NULL && tp != tparg)
5816 continue;
5817 d = get_tabpage_info(tp, tpnr);
5818 if (d != NULL)
5819 list_append_dict(rettv->vval.v_list, d);
5820 if (tparg != NULL)
5821 return;
5822 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005823}
5824
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005825/*
5826 * "gettabvar()" function
5827 */
5828 static void
5829f_gettabvar(typval_T *argvars, typval_T *rettv)
5830{
5831 win_T *oldcurwin;
5832 tabpage_T *tp, *oldtabpage;
5833 dictitem_T *v;
5834 char_u *varname;
5835 int done = FALSE;
5836
5837 rettv->v_type = VAR_STRING;
5838 rettv->vval.v_string = NULL;
5839
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005840 varname = tv_get_string_chk(&argvars[1]);
5841 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005842 if (tp != NULL && varname != NULL)
5843 {
5844 /* Set tp to be our tabpage, temporarily. Also set the window to the
5845 * first window in the tabpage, otherwise the window is not valid. */
5846 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005847 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5848 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005849 {
5850 /* look up the variable */
5851 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5852 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5853 if (v != NULL)
5854 {
5855 copy_tv(&v->di_tv, rettv);
5856 done = TRUE;
5857 }
5858 }
5859
5860 /* restore previous notion of curwin */
5861 restore_win(oldcurwin, oldtabpage, TRUE);
5862 }
5863
5864 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5865 copy_tv(&argvars[2], rettv);
5866}
5867
5868/*
5869 * "gettabwinvar()" function
5870 */
5871 static void
5872f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5873{
5874 getwinvar(argvars, rettv, 1);
5875}
5876
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005877/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005878 * "gettagstack()" function
5879 */
5880 static void
5881f_gettagstack(typval_T *argvars, typval_T *rettv)
5882{
5883 win_T *wp = curwin; // default is current window
5884
5885 if (rettv_dict_alloc(rettv) != OK)
5886 return;
5887
5888 if (argvars[0].v_type != VAR_UNKNOWN)
5889 {
5890 wp = find_win_by_nr_or_id(&argvars[0]);
5891 if (wp == NULL)
5892 return;
5893 }
5894
5895 get_tagstack(wp, rettv->vval.v_dict);
5896}
5897
5898/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005899 * Returns information about a window as a dictionary.
5900 */
5901 static dict_T *
5902get_win_info(win_T *wp, short tpnr, short winnr)
5903{
5904 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005905
5906 dict = dict_alloc();
5907 if (dict == NULL)
5908 return NULL;
5909
Bram Moolenaare0be1672018-07-08 16:50:37 +02005910 dict_add_number(dict, "tabnr", tpnr);
5911 dict_add_number(dict, "winnr", winnr);
5912 dict_add_number(dict, "winid", wp->w_id);
5913 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005914 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005915 dict_add_number(dict, "topline", wp->w_topline);
5916 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005917#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005918 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005919#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005920 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005921 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005922 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005923
Bram Moolenaar69905d12017-08-13 18:14:47 +02005924#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005925 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005926#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005927#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005928 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5929 dict_add_number(dict, "loclist",
5930 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005931#endif
5932
Bram Moolenaar30567352016-08-27 21:25:44 +02005933 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005934 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005935
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005936 return dict;
5937}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005938
5939/*
5940 * "getwininfo()" function
5941 */
5942 static void
5943f_getwininfo(typval_T *argvars, typval_T *rettv)
5944{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005945 tabpage_T *tp;
5946 win_T *wp = NULL, *wparg = NULL;
5947 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005948 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005949
5950 if (rettv_list_alloc(rettv) != OK)
5951 return;
5952
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005953 if (argvars[0].v_type != VAR_UNKNOWN)
5954 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005955 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005956 if (wparg == NULL)
5957 return;
5958 }
5959
5960 /* Collect information about either all the windows across all the tab
5961 * pages or one particular window.
5962 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005963 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005964 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005965 tabnr++;
5966 winnr = 0;
5967 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005968 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005969 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005970 if (wparg != NULL && wp != wparg)
5971 continue;
5972 d = get_win_info(wp, tabnr, winnr);
5973 if (d != NULL)
5974 list_append_dict(rettv->vval.v_list, d);
5975 if (wparg != NULL)
5976 /* found information about a specific window */
5977 return;
5978 }
5979 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005980}
5981
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005982/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005983 * "win_findbuf()" function
5984 */
5985 static void
5986f_win_findbuf(typval_T *argvars, typval_T *rettv)
5987{
5988 if (rettv_list_alloc(rettv) != FAIL)
5989 win_findbuf(argvars, rettv->vval.v_list);
5990}
5991
5992/*
5993 * "win_getid()" function
5994 */
5995 static void
5996f_win_getid(typval_T *argvars, typval_T *rettv)
5997{
5998 rettv->vval.v_number = win_getid(argvars);
5999}
6000
6001/*
6002 * "win_gotoid()" function
6003 */
6004 static void
6005f_win_gotoid(typval_T *argvars, typval_T *rettv)
6006{
6007 rettv->vval.v_number = win_gotoid(argvars);
6008}
6009
6010/*
6011 * "win_id2tabwin()" function
6012 */
6013 static void
6014f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6015{
6016 if (rettv_list_alloc(rettv) != FAIL)
6017 win_id2tabwin(argvars, rettv->vval.v_list);
6018}
6019
6020/*
6021 * "win_id2win()" function
6022 */
6023 static void
6024f_win_id2win(typval_T *argvars, typval_T *rettv)
6025{
6026 rettv->vval.v_number = win_id2win(argvars);
6027}
6028
6029/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006030 * "win_screenpos()" function
6031 */
6032 static void
6033f_win_screenpos(typval_T *argvars, typval_T *rettv)
6034{
6035 win_T *wp;
6036
6037 if (rettv_list_alloc(rettv) == FAIL)
6038 return;
6039
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006040 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006041 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6042 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6043}
6044
6045/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006046 * "getwinpos({timeout})" function
6047 */
6048 static void
6049f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6050{
6051 int x = -1;
6052 int y = -1;
6053
6054 if (rettv_list_alloc(rettv) == FAIL)
6055 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006056#if defined(FEAT_GUI) \
6057 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6058 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006059 {
6060 varnumber_T timeout = 100;
6061
6062 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006063 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006064
6065 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006066 }
6067#endif
6068 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6069 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6070}
6071
6072
6073/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006074 * "getwinposx()" function
6075 */
6076 static void
6077f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6078{
6079 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006080#if defined(FEAT_GUI) \
6081 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6082 || defined(MSWIN)
6083
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006084 {
6085 int x, y;
6086
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006087 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006088 rettv->vval.v_number = x;
6089 }
6090#endif
6091}
6092
6093/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006094 * "getwinposy()" function
6095 */
6096 static void
6097f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6098{
6099 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006100#if defined(FEAT_GUI) \
6101 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6102 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006103 {
6104 int x, y;
6105
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006106 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006107 rettv->vval.v_number = y;
6108 }
6109#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006110}
6111
6112/*
6113 * "getwinvar()" function
6114 */
6115 static void
6116f_getwinvar(typval_T *argvars, typval_T *rettv)
6117{
6118 getwinvar(argvars, rettv, 0);
6119}
6120
6121/*
6122 * "glob()" function
6123 */
6124 static void
6125f_glob(typval_T *argvars, typval_T *rettv)
6126{
6127 int options = WILD_SILENT|WILD_USE_NL;
6128 expand_T xpc;
6129 int error = FALSE;
6130
6131 /* When the optional second argument is non-zero, don't remove matches
6132 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6133 rettv->v_type = VAR_STRING;
6134 if (argvars[1].v_type != VAR_UNKNOWN)
6135 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006136 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006137 options |= WILD_KEEP_ALL;
6138 if (argvars[2].v_type != VAR_UNKNOWN)
6139 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006140 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006141 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006142 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006143 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006144 options |= WILD_ALLLINKS;
6145 }
6146 }
6147 if (!error)
6148 {
6149 ExpandInit(&xpc);
6150 xpc.xp_context = EXPAND_FILES;
6151 if (p_wic)
6152 options += WILD_ICASE;
6153 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006154 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006155 NULL, options, WILD_ALL);
6156 else if (rettv_list_alloc(rettv) != FAIL)
6157 {
6158 int i;
6159
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006160 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006161 NULL, options, WILD_ALL_KEEP);
6162 for (i = 0; i < xpc.xp_numfiles; i++)
6163 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6164
6165 ExpandCleanup(&xpc);
6166 }
6167 }
6168 else
6169 rettv->vval.v_string = NULL;
6170}
6171
6172/*
6173 * "globpath()" function
6174 */
6175 static void
6176f_globpath(typval_T *argvars, typval_T *rettv)
6177{
6178 int flags = 0;
6179 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006180 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006181 int error = FALSE;
6182 garray_T ga;
6183 int i;
6184
6185 /* When the optional second argument is non-zero, don't remove matches
6186 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6187 rettv->v_type = VAR_STRING;
6188 if (argvars[2].v_type != VAR_UNKNOWN)
6189 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006190 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006191 flags |= WILD_KEEP_ALL;
6192 if (argvars[3].v_type != VAR_UNKNOWN)
6193 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006194 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006195 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006196 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006197 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006198 flags |= WILD_ALLLINKS;
6199 }
6200 }
6201 if (file != NULL && !error)
6202 {
6203 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006204 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006205 if (rettv->v_type == VAR_STRING)
6206 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6207 else if (rettv_list_alloc(rettv) != FAIL)
6208 for (i = 0; i < ga.ga_len; ++i)
6209 list_append_string(rettv->vval.v_list,
6210 ((char_u **)(ga.ga_data))[i], -1);
6211 ga_clear_strings(&ga);
6212 }
6213 else
6214 rettv->vval.v_string = NULL;
6215}
6216
6217/*
6218 * "glob2regpat()" function
6219 */
6220 static void
6221f_glob2regpat(typval_T *argvars, typval_T *rettv)
6222{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006223 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006224
6225 rettv->v_type = VAR_STRING;
6226 rettv->vval.v_string = (pat == NULL)
6227 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6228}
6229
6230/* for VIM_VERSION_ defines */
6231#include "version.h"
6232
6233/*
6234 * "has()" function
6235 */
6236 static void
6237f_has(typval_T *argvars, typval_T *rettv)
6238{
6239 int i;
6240 char_u *name;
6241 int n = FALSE;
6242 static char *(has_list[]) =
6243 {
6244#ifdef AMIGA
6245 "amiga",
6246# ifdef FEAT_ARP
6247 "arp",
6248# endif
6249#endif
6250#ifdef __BEOS__
6251 "beos",
6252#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006253#if defined(BSD) && !defined(MACOS_X)
6254 "bsd",
6255#endif
6256#ifdef hpux
6257 "hpux",
6258#endif
6259#ifdef __linux__
6260 "linux",
6261#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006262#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006263 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6264 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006265# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006266 "macunix", /* Mac OS X, with the darwin feature */
6267 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006268# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006269#endif
6270#ifdef __QNX__
6271 "qnx",
6272#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006273#ifdef SUN_SYSTEM
6274 "sun",
6275#else
6276 "moon",
6277#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006278#ifdef UNIX
6279 "unix",
6280#endif
6281#ifdef VMS
6282 "vms",
6283#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006284#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006285 "win32",
6286#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006287#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006288 "win32unix",
6289#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006290#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006291 "win64",
6292#endif
6293#ifdef EBCDIC
6294 "ebcdic",
6295#endif
6296#ifndef CASE_INSENSITIVE_FILENAME
6297 "fname_case",
6298#endif
6299#ifdef HAVE_ACL
6300 "acl",
6301#endif
6302#ifdef FEAT_ARABIC
6303 "arabic",
6304#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006305 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006306#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006307 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006308#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006309#ifdef FEAT_AUTOSERVERNAME
6310 "autoservername",
6311#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006312#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006313 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006314# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006315 "balloon_multiline",
6316# endif
6317#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006318#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006319 "balloon_eval_term",
6320#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006321#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6322 "builtin_terms",
6323# ifdef ALL_BUILTIN_TCAPS
6324 "all_builtin_terms",
6325# endif
6326#endif
6327#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006328 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006329 || defined(FEAT_GUI_MOTIF))
6330 "browsefilter",
6331#endif
6332#ifdef FEAT_BYTEOFF
6333 "byte_offset",
6334#endif
6335#ifdef FEAT_JOB_CHANNEL
6336 "channel",
6337#endif
6338#ifdef FEAT_CINDENT
6339 "cindent",
6340#endif
6341#ifdef FEAT_CLIENTSERVER
6342 "clientserver",
6343#endif
6344#ifdef FEAT_CLIPBOARD
6345 "clipboard",
6346#endif
6347#ifdef FEAT_CMDL_COMPL
6348 "cmdline_compl",
6349#endif
6350#ifdef FEAT_CMDHIST
6351 "cmdline_hist",
6352#endif
6353#ifdef FEAT_COMMENTS
6354 "comments",
6355#endif
6356#ifdef FEAT_CONCEAL
6357 "conceal",
6358#endif
6359#ifdef FEAT_CRYPT
6360 "cryptv",
6361 "crypt-blowfish",
6362 "crypt-blowfish2",
6363#endif
6364#ifdef FEAT_CSCOPE
6365 "cscope",
6366#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006367 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006368#ifdef CURSOR_SHAPE
6369 "cursorshape",
6370#endif
6371#ifdef DEBUG
6372 "debug",
6373#endif
6374#ifdef FEAT_CON_DIALOG
6375 "dialog_con",
6376#endif
6377#ifdef FEAT_GUI_DIALOG
6378 "dialog_gui",
6379#endif
6380#ifdef FEAT_DIFF
6381 "diff",
6382#endif
6383#ifdef FEAT_DIGRAPHS
6384 "digraphs",
6385#endif
6386#ifdef FEAT_DIRECTX
6387 "directx",
6388#endif
6389#ifdef FEAT_DND
6390 "dnd",
6391#endif
6392#ifdef FEAT_EMACS_TAGS
6393 "emacs_tags",
6394#endif
6395 "eval", /* always present, of course! */
6396 "ex_extra", /* graduated feature */
6397#ifdef FEAT_SEARCH_EXTRA
6398 "extra_search",
6399#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006400#ifdef FEAT_SEARCHPATH
6401 "file_in_path",
6402#endif
6403#ifdef FEAT_FILTERPIPE
6404 "filterpipe",
6405#endif
6406#ifdef FEAT_FIND_ID
6407 "find_in_path",
6408#endif
6409#ifdef FEAT_FLOAT
6410 "float",
6411#endif
6412#ifdef FEAT_FOLDING
6413 "folding",
6414#endif
6415#ifdef FEAT_FOOTER
6416 "footer",
6417#endif
6418#if !defined(USE_SYSTEM) && defined(UNIX)
6419 "fork",
6420#endif
6421#ifdef FEAT_GETTEXT
6422 "gettext",
6423#endif
6424#ifdef FEAT_GUI
6425 "gui",
6426#endif
6427#ifdef FEAT_GUI_ATHENA
6428# ifdef FEAT_GUI_NEXTAW
6429 "gui_neXtaw",
6430# else
6431 "gui_athena",
6432# endif
6433#endif
6434#ifdef FEAT_GUI_GTK
6435 "gui_gtk",
6436# ifdef USE_GTK3
6437 "gui_gtk3",
6438# else
6439 "gui_gtk2",
6440# endif
6441#endif
6442#ifdef FEAT_GUI_GNOME
6443 "gui_gnome",
6444#endif
6445#ifdef FEAT_GUI_MAC
6446 "gui_mac",
6447#endif
6448#ifdef FEAT_GUI_MOTIF
6449 "gui_motif",
6450#endif
6451#ifdef FEAT_GUI_PHOTON
6452 "gui_photon",
6453#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006454#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006455 "gui_win32",
6456#endif
6457#ifdef FEAT_HANGULIN
6458 "hangul_input",
6459#endif
6460#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6461 "iconv",
6462#endif
6463#ifdef FEAT_INS_EXPAND
6464 "insert_expand",
6465#endif
6466#ifdef FEAT_JOB_CHANNEL
6467 "job",
6468#endif
6469#ifdef FEAT_JUMPLIST
6470 "jumplist",
6471#endif
6472#ifdef FEAT_KEYMAP
6473 "keymap",
6474#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006475 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006476#ifdef FEAT_LANGMAP
6477 "langmap",
6478#endif
6479#ifdef FEAT_LIBCALL
6480 "libcall",
6481#endif
6482#ifdef FEAT_LINEBREAK
6483 "linebreak",
6484#endif
6485#ifdef FEAT_LISP
6486 "lispindent",
6487#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006488 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006489#ifdef FEAT_LOCALMAP
6490 "localmap",
6491#endif
6492#ifdef FEAT_LUA
6493# ifndef DYNAMIC_LUA
6494 "lua",
6495# endif
6496#endif
6497#ifdef FEAT_MENU
6498 "menu",
6499#endif
6500#ifdef FEAT_SESSION
6501 "mksession",
6502#endif
6503#ifdef FEAT_MODIFY_FNAME
6504 "modify_fname",
6505#endif
6506#ifdef FEAT_MOUSE
6507 "mouse",
6508#endif
6509#ifdef FEAT_MOUSESHAPE
6510 "mouseshape",
6511#endif
6512#if defined(UNIX) || defined(VMS)
6513# ifdef FEAT_MOUSE_DEC
6514 "mouse_dec",
6515# endif
6516# ifdef FEAT_MOUSE_GPM
6517 "mouse_gpm",
6518# endif
6519# ifdef FEAT_MOUSE_JSB
6520 "mouse_jsbterm",
6521# endif
6522# ifdef FEAT_MOUSE_NET
6523 "mouse_netterm",
6524# endif
6525# ifdef FEAT_MOUSE_PTERM
6526 "mouse_pterm",
6527# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006528# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529 "mouse_sgr",
6530# endif
6531# ifdef FEAT_SYSMOUSE
6532 "mouse_sysmouse",
6533# endif
6534# ifdef FEAT_MOUSE_URXVT
6535 "mouse_urxvt",
6536# endif
6537# ifdef FEAT_MOUSE_XTERM
6538 "mouse_xterm",
6539# endif
6540#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006541 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006542#ifdef FEAT_MBYTE_IME
6543 "multi_byte_ime",
6544#endif
6545#ifdef FEAT_MULTI_LANG
6546 "multi_lang",
6547#endif
6548#ifdef FEAT_MZSCHEME
6549#ifndef DYNAMIC_MZSCHEME
6550 "mzscheme",
6551#endif
6552#endif
6553#ifdef FEAT_NUM64
6554 "num64",
6555#endif
6556#ifdef FEAT_OLE
6557 "ole",
6558#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006559#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006560 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006561#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006562#ifdef FEAT_PATH_EXTRA
6563 "path_extra",
6564#endif
6565#ifdef FEAT_PERL
6566#ifndef DYNAMIC_PERL
6567 "perl",
6568#endif
6569#endif
6570#ifdef FEAT_PERSISTENT_UNDO
6571 "persistent_undo",
6572#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006573#if defined(FEAT_PYTHON)
6574 "python_compiled",
6575# if defined(DYNAMIC_PYTHON)
6576 "python_dynamic",
6577# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006578 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006579 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006580# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006581#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006582#if defined(FEAT_PYTHON3)
6583 "python3_compiled",
6584# if defined(DYNAMIC_PYTHON3)
6585 "python3_dynamic",
6586# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006587 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006588 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006589# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006590#endif
6591#ifdef FEAT_POSTSCRIPT
6592 "postscript",
6593#endif
6594#ifdef FEAT_PRINTER
6595 "printer",
6596#endif
6597#ifdef FEAT_PROFILE
6598 "profile",
6599#endif
6600#ifdef FEAT_RELTIME
6601 "reltime",
6602#endif
6603#ifdef FEAT_QUICKFIX
6604 "quickfix",
6605#endif
6606#ifdef FEAT_RIGHTLEFT
6607 "rightleft",
6608#endif
6609#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6610 "ruby",
6611#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006612 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006613#ifdef FEAT_CMDL_INFO
6614 "showcmd",
6615 "cmdline_info",
6616#endif
6617#ifdef FEAT_SIGNS
6618 "signs",
6619#endif
6620#ifdef FEAT_SMARTINDENT
6621 "smartindent",
6622#endif
6623#ifdef STARTUPTIME
6624 "startuptime",
6625#endif
6626#ifdef FEAT_STL_OPT
6627 "statusline",
6628#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006629#ifdef FEAT_NETBEANS_INTG
6630 "netbeans_intg",
6631#endif
6632#ifdef FEAT_SPELL
6633 "spell",
6634#endif
6635#ifdef FEAT_SYN_HL
6636 "syntax",
6637#endif
6638#if defined(USE_SYSTEM) || !defined(UNIX)
6639 "system",
6640#endif
6641#ifdef FEAT_TAG_BINS
6642 "tag_binary",
6643#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006644#ifdef FEAT_TCL
6645# ifndef DYNAMIC_TCL
6646 "tcl",
6647# endif
6648#endif
6649#ifdef FEAT_TERMGUICOLORS
6650 "termguicolors",
6651#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006652#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006653 "terminal",
6654#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006655#ifdef TERMINFO
6656 "terminfo",
6657#endif
6658#ifdef FEAT_TERMRESPONSE
6659 "termresponse",
6660#endif
6661#ifdef FEAT_TEXTOBJ
6662 "textobjects",
6663#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006664#ifdef FEAT_TEXT_PROP
6665 "textprop",
6666#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006667#ifdef HAVE_TGETENT
6668 "tgetent",
6669#endif
6670#ifdef FEAT_TIMERS
6671 "timers",
6672#endif
6673#ifdef FEAT_TITLE
6674 "title",
6675#endif
6676#ifdef FEAT_TOOLBAR
6677 "toolbar",
6678#endif
6679#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6680 "unnamedplus",
6681#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006682 "user-commands", /* was accidentally included in 5.4 */
6683 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006684#ifdef FEAT_VARTABS
6685 "vartabs",
6686#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006687 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006688#ifdef FEAT_VIMINFO
6689 "viminfo",
6690#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006691 "vimscript-1",
6692 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006693 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006696 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006697 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006698#ifdef FEAT_VTP
6699 "vtp",
6700#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006701#ifdef FEAT_WILDIGN
6702 "wildignore",
6703#endif
6704#ifdef FEAT_WILDMENU
6705 "wildmenu",
6706#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006707 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006708#ifdef FEAT_WAK
6709 "winaltkeys",
6710#endif
6711#ifdef FEAT_WRITEBACKUP
6712 "writebackup",
6713#endif
6714#ifdef FEAT_XIM
6715 "xim",
6716#endif
6717#ifdef FEAT_XFONTSET
6718 "xfontset",
6719#endif
6720#ifdef FEAT_XPM_W32
6721 "xpm",
6722 "xpm_w32", /* for backward compatibility */
6723#else
6724# if defined(HAVE_XPM)
6725 "xpm",
6726# endif
6727#endif
6728#ifdef USE_XSMP
6729 "xsmp",
6730#endif
6731#ifdef USE_XSMP_INTERACT
6732 "xsmp_interact",
6733#endif
6734#ifdef FEAT_XCLIPBOARD
6735 "xterm_clipboard",
6736#endif
6737#ifdef FEAT_XTERM_SAVE
6738 "xterm_save",
6739#endif
6740#if defined(UNIX) && defined(FEAT_X11)
6741 "X11",
6742#endif
6743 NULL
6744 };
6745
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006746 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006747 for (i = 0; has_list[i] != NULL; ++i)
6748 if (STRICMP(name, has_list[i]) == 0)
6749 {
6750 n = TRUE;
6751 break;
6752 }
6753
6754 if (n == FALSE)
6755 {
6756 if (STRNICMP(name, "patch", 5) == 0)
6757 {
6758 if (name[5] == '-'
6759 && STRLEN(name) >= 11
6760 && vim_isdigit(name[6])
6761 && vim_isdigit(name[8])
6762 && vim_isdigit(name[10]))
6763 {
6764 int major = atoi((char *)name + 6);
6765 int minor = atoi((char *)name + 8);
6766
6767 /* Expect "patch-9.9.01234". */
6768 n = (major < VIM_VERSION_MAJOR
6769 || (major == VIM_VERSION_MAJOR
6770 && (minor < VIM_VERSION_MINOR
6771 || (minor == VIM_VERSION_MINOR
6772 && has_patch(atoi((char *)name + 10))))));
6773 }
6774 else
6775 n = has_patch(atoi((char *)name + 5));
6776 }
6777 else if (STRICMP(name, "vim_starting") == 0)
6778 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006779 else if (STRICMP(name, "ttyin") == 0)
6780 n = mch_input_isatty();
6781 else if (STRICMP(name, "ttyout") == 0)
6782 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006783 else if (STRICMP(name, "multi_byte_encoding") == 0)
6784 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006785#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006786 else if (STRICMP(name, "balloon_multiline") == 0)
6787 n = multiline_balloon_available();
6788#endif
6789#ifdef DYNAMIC_TCL
6790 else if (STRICMP(name, "tcl") == 0)
6791 n = tcl_enabled(FALSE);
6792#endif
6793#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6794 else if (STRICMP(name, "iconv") == 0)
6795 n = iconv_enabled(FALSE);
6796#endif
6797#ifdef DYNAMIC_LUA
6798 else if (STRICMP(name, "lua") == 0)
6799 n = lua_enabled(FALSE);
6800#endif
6801#ifdef DYNAMIC_MZSCHEME
6802 else if (STRICMP(name, "mzscheme") == 0)
6803 n = mzscheme_enabled(FALSE);
6804#endif
6805#ifdef DYNAMIC_RUBY
6806 else if (STRICMP(name, "ruby") == 0)
6807 n = ruby_enabled(FALSE);
6808#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006809#ifdef DYNAMIC_PYTHON
6810 else if (STRICMP(name, "python") == 0)
6811 n = python_enabled(FALSE);
6812#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006813#ifdef DYNAMIC_PYTHON3
6814 else if (STRICMP(name, "python3") == 0)
6815 n = python3_enabled(FALSE);
6816#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006817#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6818 else if (STRICMP(name, "pythonx") == 0)
6819 {
6820# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6821 if (p_pyx == 0)
6822 n = python3_enabled(FALSE) || python_enabled(FALSE);
6823 else if (p_pyx == 3)
6824 n = python3_enabled(FALSE);
6825 else if (p_pyx == 2)
6826 n = python_enabled(FALSE);
6827# elif defined(DYNAMIC_PYTHON)
6828 n = python_enabled(FALSE);
6829# elif defined(DYNAMIC_PYTHON3)
6830 n = python3_enabled(FALSE);
6831# endif
6832 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006833#endif
6834#ifdef DYNAMIC_PERL
6835 else if (STRICMP(name, "perl") == 0)
6836 n = perl_enabled(FALSE);
6837#endif
6838#ifdef FEAT_GUI
6839 else if (STRICMP(name, "gui_running") == 0)
6840 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006841# ifdef FEAT_BROWSE
6842 else if (STRICMP(name, "browse") == 0)
6843 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6844# endif
6845#endif
6846#ifdef FEAT_SYN_HL
6847 else if (STRICMP(name, "syntax_items") == 0)
6848 n = syntax_present(curwin);
6849#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006850#ifdef FEAT_VTP
6851 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006852 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006853#endif
6854#ifdef FEAT_NETBEANS_INTG
6855 else if (STRICMP(name, "netbeans_enabled") == 0)
6856 n = netbeans_active();
6857#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006858#ifdef FEAT_MOUSE_GPM
6859 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6860 n = gpm_enabled();
6861#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006862#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006863 else if (STRICMP(name, "terminal") == 0)
6864 n = terminal_enabled();
6865#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006866#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006867 else if (STRICMP(name, "conpty") == 0)
6868 n = use_conpty();
6869#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006870 }
6871
6872 rettv->vval.v_number = n;
6873}
6874
6875/*
6876 * "has_key()" function
6877 */
6878 static void
6879f_has_key(typval_T *argvars, typval_T *rettv)
6880{
6881 if (argvars[0].v_type != VAR_DICT)
6882 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006883 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006884 return;
6885 }
6886 if (argvars[0].vval.v_dict == NULL)
6887 return;
6888
6889 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006890 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006891}
6892
6893/*
6894 * "haslocaldir()" function
6895 */
6896 static void
6897f_haslocaldir(typval_T *argvars, typval_T *rettv)
6898{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006899 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 win_T *wp = NULL;
6901
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006902 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6903
6904 // Check for window-local and tab-local directories
6905 if (wp != NULL && wp->w_localdir != NULL)
6906 rettv->vval.v_number = 1;
6907 else if (tp != NULL && tp->tp_localdir != NULL)
6908 rettv->vval.v_number = 2;
6909 else
6910 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911}
6912
6913/*
6914 * "hasmapto()" function
6915 */
6916 static void
6917f_hasmapto(typval_T *argvars, typval_T *rettv)
6918{
6919 char_u *name;
6920 char_u *mode;
6921 char_u buf[NUMBUFLEN];
6922 int abbr = FALSE;
6923
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006924 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925 if (argvars[1].v_type == VAR_UNKNOWN)
6926 mode = (char_u *)"nvo";
6927 else
6928 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006929 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006930 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006931 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006932 }
6933
6934 if (map_to_exists(name, mode, abbr))
6935 rettv->vval.v_number = TRUE;
6936 else
6937 rettv->vval.v_number = FALSE;
6938}
6939
6940/*
6941 * "histadd()" function
6942 */
6943 static void
6944f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6945{
6946#ifdef FEAT_CMDHIST
6947 int histype;
6948 char_u *str;
6949 char_u buf[NUMBUFLEN];
6950#endif
6951
6952 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006953 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006954 return;
6955#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006956 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006957 histype = str != NULL ? get_histtype(str) : -1;
6958 if (histype >= 0)
6959 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006960 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006961 if (*str != NUL)
6962 {
6963 init_history();
6964 add_to_history(histype, str, FALSE, NUL);
6965 rettv->vval.v_number = TRUE;
6966 return;
6967 }
6968 }
6969#endif
6970}
6971
6972/*
6973 * "histdel()" function
6974 */
6975 static void
6976f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6977{
6978#ifdef FEAT_CMDHIST
6979 int n;
6980 char_u buf[NUMBUFLEN];
6981 char_u *str;
6982
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006983 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006984 if (str == NULL)
6985 n = 0;
6986 else if (argvars[1].v_type == VAR_UNKNOWN)
6987 /* only one argument: clear entire history */
6988 n = clr_history(get_histtype(str));
6989 else if (argvars[1].v_type == VAR_NUMBER)
6990 /* index given: remove that entry */
6991 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006992 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006993 else
6994 /* string given: remove all matching entries */
6995 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006996 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006997 rettv->vval.v_number = n;
6998#endif
6999}
7000
7001/*
7002 * "histget()" function
7003 */
7004 static void
7005f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7006{
7007#ifdef FEAT_CMDHIST
7008 int type;
7009 int idx;
7010 char_u *str;
7011
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007012 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007013 if (str == NULL)
7014 rettv->vval.v_string = NULL;
7015 else
7016 {
7017 type = get_histtype(str);
7018 if (argvars[1].v_type == VAR_UNKNOWN)
7019 idx = get_history_idx(type);
7020 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007021 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007022 /* -1 on type error */
7023 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7024 }
7025#else
7026 rettv->vval.v_string = NULL;
7027#endif
7028 rettv->v_type = VAR_STRING;
7029}
7030
7031/*
7032 * "histnr()" function
7033 */
7034 static void
7035f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7036{
7037 int i;
7038
7039#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007040 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007041
7042 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7043 if (i >= HIST_CMD && i < HIST_COUNT)
7044 i = get_history_idx(i);
7045 else
7046#endif
7047 i = -1;
7048 rettv->vval.v_number = i;
7049}
7050
7051/*
7052 * "highlightID(name)" function
7053 */
7054 static void
7055f_hlID(typval_T *argvars, typval_T *rettv)
7056{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007057 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007058}
7059
7060/*
7061 * "highlight_exists()" function
7062 */
7063 static void
7064f_hlexists(typval_T *argvars, typval_T *rettv)
7065{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007066 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007067}
7068
7069/*
7070 * "hostname()" function
7071 */
7072 static void
7073f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7074{
7075 char_u hostname[256];
7076
7077 mch_get_host_name(hostname, 256);
7078 rettv->v_type = VAR_STRING;
7079 rettv->vval.v_string = vim_strsave(hostname);
7080}
7081
7082/*
7083 * iconv() function
7084 */
7085 static void
7086f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7087{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007088 char_u buf1[NUMBUFLEN];
7089 char_u buf2[NUMBUFLEN];
7090 char_u *from, *to, *str;
7091 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007092
7093 rettv->v_type = VAR_STRING;
7094 rettv->vval.v_string = NULL;
7095
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007096 str = tv_get_string(&argvars[0]);
7097 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7098 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007099 vimconv.vc_type = CONV_NONE;
7100 convert_setup(&vimconv, from, to);
7101
7102 /* If the encodings are equal, no conversion needed. */
7103 if (vimconv.vc_type == CONV_NONE)
7104 rettv->vval.v_string = vim_strsave(str);
7105 else
7106 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7107
7108 convert_setup(&vimconv, NULL, NULL);
7109 vim_free(from);
7110 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007111}
7112
7113/*
7114 * "indent()" function
7115 */
7116 static void
7117f_indent(typval_T *argvars, typval_T *rettv)
7118{
7119 linenr_T lnum;
7120
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007121 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007122 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7123 rettv->vval.v_number = get_indent_lnum(lnum);
7124 else
7125 rettv->vval.v_number = -1;
7126}
7127
7128/*
7129 * "index()" function
7130 */
7131 static void
7132f_index(typval_T *argvars, typval_T *rettv)
7133{
7134 list_T *l;
7135 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007136 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007137 long idx = 0;
7138 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007139 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007140
7141 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007142 if (argvars[0].v_type == VAR_BLOB)
7143 {
7144 typval_T tv;
7145 int start = 0;
7146
7147 if (argvars[2].v_type != VAR_UNKNOWN)
7148 {
7149 start = tv_get_number_chk(&argvars[2], &error);
7150 if (error)
7151 return;
7152 }
7153 b = argvars[0].vval.v_blob;
7154 if (b == NULL)
7155 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007156 if (start < 0)
7157 {
7158 start = blob_len(b) + start;
7159 if (start < 0)
7160 start = 0;
7161 }
7162
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007163 for (idx = start; idx < blob_len(b); ++idx)
7164 {
7165 tv.v_type = VAR_NUMBER;
7166 tv.vval.v_number = blob_get(b, idx);
7167 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7168 {
7169 rettv->vval.v_number = idx;
7170 return;
7171 }
7172 }
7173 return;
7174 }
7175 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007176 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007177 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007178 return;
7179 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007180
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007181 l = argvars[0].vval.v_list;
7182 if (l != NULL)
7183 {
7184 item = l->lv_first;
7185 if (argvars[2].v_type != VAR_UNKNOWN)
7186 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007187 /* Start at specified item. Use the cached index that list_find()
7188 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007189 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007190 idx = l->lv_idx;
7191 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007192 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007193 if (error)
7194 item = NULL;
7195 }
7196
7197 for ( ; item != NULL; item = item->li_next, ++idx)
7198 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7199 {
7200 rettv->vval.v_number = idx;
7201 break;
7202 }
7203 }
7204}
7205
7206static int inputsecret_flag = 0;
7207
7208/*
7209 * "input()" function
7210 * Also handles inputsecret() when inputsecret is set.
7211 */
7212 static void
7213f_input(typval_T *argvars, typval_T *rettv)
7214{
7215 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7216}
7217
7218/*
7219 * "inputdialog()" function
7220 */
7221 static void
7222f_inputdialog(typval_T *argvars, typval_T *rettv)
7223{
7224#if defined(FEAT_GUI_TEXTDIALOG)
7225 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7226 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7227 {
7228 char_u *message;
7229 char_u buf[NUMBUFLEN];
7230 char_u *defstr = (char_u *)"";
7231
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007232 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007233 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007234 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007235 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7236 else
7237 IObuff[0] = NUL;
7238 if (message != NULL && defstr != NULL
7239 && do_dialog(VIM_QUESTION, NULL, message,
7240 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7241 rettv->vval.v_string = vim_strsave(IObuff);
7242 else
7243 {
7244 if (message != NULL && defstr != NULL
7245 && argvars[1].v_type != VAR_UNKNOWN
7246 && argvars[2].v_type != VAR_UNKNOWN)
7247 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007248 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007249 else
7250 rettv->vval.v_string = NULL;
7251 }
7252 rettv->v_type = VAR_STRING;
7253 }
7254 else
7255#endif
7256 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7257}
7258
7259/*
7260 * "inputlist()" function
7261 */
7262 static void
7263f_inputlist(typval_T *argvars, typval_T *rettv)
7264{
7265 listitem_T *li;
7266 int selected;
7267 int mouse_used;
7268
7269#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007270 /* While starting up, there is no place to enter text. When running tests
7271 * with --not-a-term we assume feedkeys() will be used. */
7272 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007273 return;
7274#endif
7275 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7276 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007277 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007278 return;
7279 }
7280
7281 msg_start();
7282 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7283 lines_left = Rows; /* avoid more prompt */
7284 msg_scroll = TRUE;
7285 msg_clr_eos();
7286
7287 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7288 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007289 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007290 msg_putchar('\n');
7291 }
7292
7293 /* Ask for choice. */
7294 selected = prompt_for_number(&mouse_used);
7295 if (mouse_used)
7296 selected -= lines_left;
7297
7298 rettv->vval.v_number = selected;
7299}
7300
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007301static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7302
7303/*
7304 * "inputrestore()" function
7305 */
7306 static void
7307f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7308{
7309 if (ga_userinput.ga_len > 0)
7310 {
7311 --ga_userinput.ga_len;
7312 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7313 + ga_userinput.ga_len);
7314 /* default return is zero == OK */
7315 }
7316 else if (p_verbose > 1)
7317 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007318 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007319 rettv->vval.v_number = 1; /* Failed */
7320 }
7321}
7322
7323/*
7324 * "inputsave()" function
7325 */
7326 static void
7327f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7328{
7329 /* Add an entry to the stack of typeahead storage. */
7330 if (ga_grow(&ga_userinput, 1) == OK)
7331 {
7332 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7333 + ga_userinput.ga_len);
7334 ++ga_userinput.ga_len;
7335 /* default return is zero == OK */
7336 }
7337 else
7338 rettv->vval.v_number = 1; /* Failed */
7339}
7340
7341/*
7342 * "inputsecret()" function
7343 */
7344 static void
7345f_inputsecret(typval_T *argvars, typval_T *rettv)
7346{
7347 ++cmdline_star;
7348 ++inputsecret_flag;
7349 f_input(argvars, rettv);
7350 --cmdline_star;
7351 --inputsecret_flag;
7352}
7353
7354/*
7355 * "insert()" function
7356 */
7357 static void
7358f_insert(typval_T *argvars, typval_T *rettv)
7359{
7360 long before = 0;
7361 listitem_T *item;
7362 list_T *l;
7363 int error = FALSE;
7364
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007365 if (argvars[0].v_type == VAR_BLOB)
7366 {
7367 int val, len;
7368 char_u *p;
7369
7370 len = blob_len(argvars[0].vval.v_blob);
7371 if (argvars[2].v_type != VAR_UNKNOWN)
7372 {
7373 before = (long)tv_get_number_chk(&argvars[2], &error);
7374 if (error)
7375 return; // type error; errmsg already given
7376 if (before < 0 || before > len)
7377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007378 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007379 return;
7380 }
7381 }
7382 val = tv_get_number_chk(&argvars[1], &error);
7383 if (error)
7384 return;
7385 if (val < 0 || val > 255)
7386 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007387 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007388 return;
7389 }
7390
7391 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7392 return;
7393 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7394 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7395 *(p + before) = val;
7396 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7397
7398 copy_tv(&argvars[0], rettv);
7399 }
7400 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007401 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007402 else if ((l = argvars[0].vval.v_list) != NULL
7403 && !var_check_lock(l->lv_lock,
7404 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007405 {
7406 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007407 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007408 if (error)
7409 return; /* type error; errmsg already given */
7410
7411 if (before == l->lv_len)
7412 item = NULL;
7413 else
7414 {
7415 item = list_find(l, before);
7416 if (item == NULL)
7417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007418 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007419 l = NULL;
7420 }
7421 }
7422 if (l != NULL)
7423 {
7424 list_insert_tv(l, &argvars[1], item);
7425 copy_tv(&argvars[0], rettv);
7426 }
7427 }
7428}
7429
7430/*
7431 * "invert(expr)" function
7432 */
7433 static void
7434f_invert(typval_T *argvars, typval_T *rettv)
7435{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007436 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007437}
7438
7439/*
7440 * "isdirectory()" function
7441 */
7442 static void
7443f_isdirectory(typval_T *argvars, typval_T *rettv)
7444{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007445 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007446}
7447
7448/*
7449 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7450 * or it refers to a List or Dictionary that is locked.
7451 */
7452 static int
7453tv_islocked(typval_T *tv)
7454{
7455 return (tv->v_lock & VAR_LOCKED)
7456 || (tv->v_type == VAR_LIST
7457 && tv->vval.v_list != NULL
7458 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7459 || (tv->v_type == VAR_DICT
7460 && tv->vval.v_dict != NULL
7461 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7462}
7463
7464/*
7465 * "islocked()" function
7466 */
7467 static void
7468f_islocked(typval_T *argvars, typval_T *rettv)
7469{
7470 lval_T lv;
7471 char_u *end;
7472 dictitem_T *di;
7473
7474 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007475 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007476 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007477 if (end != NULL && lv.ll_name != NULL)
7478 {
7479 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007480 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007481 else
7482 {
7483 if (lv.ll_tv == NULL)
7484 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007485 di = find_var(lv.ll_name, NULL, TRUE);
7486 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007488 /* Consider a variable locked when:
7489 * 1. the variable itself is locked
7490 * 2. the value of the variable is locked.
7491 * 3. the List or Dict value is locked.
7492 */
7493 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7494 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007495 }
7496 }
7497 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007498 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007499 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007500 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007501 else if (lv.ll_list != NULL)
7502 /* List item. */
7503 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7504 else
7505 /* Dictionary item. */
7506 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7507 }
7508 }
7509
7510 clear_lval(&lv);
7511}
7512
7513#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7514/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007515 * "isinf()" function
7516 */
7517 static void
7518f_isinf(typval_T *argvars, typval_T *rettv)
7519{
7520 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7521 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7522}
7523
7524/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007525 * "isnan()" function
7526 */
7527 static void
7528f_isnan(typval_T *argvars, typval_T *rettv)
7529{
7530 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7531 && isnan(argvars[0].vval.v_float);
7532}
7533#endif
7534
7535/*
7536 * "items(dict)" function
7537 */
7538 static void
7539f_items(typval_T *argvars, typval_T *rettv)
7540{
7541 dict_list(argvars, rettv, 2);
7542}
7543
7544#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7545/*
7546 * Get the job from the argument.
7547 * Returns NULL if the job is invalid.
7548 */
7549 static job_T *
7550get_job_arg(typval_T *tv)
7551{
7552 job_T *job;
7553
7554 if (tv->v_type != VAR_JOB)
7555 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007556 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007557 return NULL;
7558 }
7559 job = tv->vval.v_job;
7560
7561 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007562 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007563 return job;
7564}
7565
7566/*
7567 * "job_getchannel()" function
7568 */
7569 static void
7570f_job_getchannel(typval_T *argvars, typval_T *rettv)
7571{
7572 job_T *job = get_job_arg(&argvars[0]);
7573
7574 if (job != NULL)
7575 {
7576 rettv->v_type = VAR_CHANNEL;
7577 rettv->vval.v_channel = job->jv_channel;
7578 if (job->jv_channel != NULL)
7579 ++job->jv_channel->ch_refcount;
7580 }
7581}
7582
7583/*
7584 * "job_info()" function
7585 */
7586 static void
7587f_job_info(typval_T *argvars, typval_T *rettv)
7588{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007589 if (argvars[0].v_type != VAR_UNKNOWN)
7590 {
7591 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007592
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007593 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7594 job_info(job, rettv->vval.v_dict);
7595 }
7596 else if (rettv_list_alloc(rettv) == OK)
7597 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598}
7599
7600/*
7601 * "job_setoptions()" function
7602 */
7603 static void
7604f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7605{
7606 job_T *job = get_job_arg(&argvars[0]);
7607 jobopt_T opt;
7608
7609 if (job == NULL)
7610 return;
7611 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007612 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007613 job_set_options(job, &opt);
7614 free_job_options(&opt);
7615}
7616
7617/*
7618 * "job_start()" function
7619 */
7620 static void
7621f_job_start(typval_T *argvars, typval_T *rettv)
7622{
7623 rettv->v_type = VAR_JOB;
7624 if (check_restricted() || check_secure())
7625 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007626 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007627}
7628
7629/*
7630 * "job_status()" function
7631 */
7632 static void
7633f_job_status(typval_T *argvars, typval_T *rettv)
7634{
7635 job_T *job = get_job_arg(&argvars[0]);
7636
7637 if (job != NULL)
7638 {
7639 rettv->v_type = VAR_STRING;
7640 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7641 }
7642}
7643
7644/*
7645 * "job_stop()" function
7646 */
7647 static void
7648f_job_stop(typval_T *argvars, typval_T *rettv)
7649{
7650 job_T *job = get_job_arg(&argvars[0]);
7651
7652 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007653 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007654}
7655#endif
7656
7657/*
7658 * "join()" function
7659 */
7660 static void
7661f_join(typval_T *argvars, typval_T *rettv)
7662{
7663 garray_T ga;
7664 char_u *sep;
7665
7666 if (argvars[0].v_type != VAR_LIST)
7667 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007668 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007669 return;
7670 }
7671 if (argvars[0].vval.v_list == NULL)
7672 return;
7673 if (argvars[1].v_type == VAR_UNKNOWN)
7674 sep = (char_u *)" ";
7675 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007676 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007677
7678 rettv->v_type = VAR_STRING;
7679
7680 if (sep != NULL)
7681 {
7682 ga_init2(&ga, (int)sizeof(char), 80);
7683 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7684 ga_append(&ga, NUL);
7685 rettv->vval.v_string = (char_u *)ga.ga_data;
7686 }
7687 else
7688 rettv->vval.v_string = NULL;
7689}
7690
7691/*
7692 * "js_decode()" function
7693 */
7694 static void
7695f_js_decode(typval_T *argvars, typval_T *rettv)
7696{
7697 js_read_T reader;
7698
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007699 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007700 reader.js_fill = NULL;
7701 reader.js_used = 0;
7702 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007703 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007704}
7705
7706/*
7707 * "js_encode()" function
7708 */
7709 static void
7710f_js_encode(typval_T *argvars, typval_T *rettv)
7711{
7712 rettv->v_type = VAR_STRING;
7713 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7714}
7715
7716/*
7717 * "json_decode()" function
7718 */
7719 static void
7720f_json_decode(typval_T *argvars, typval_T *rettv)
7721{
7722 js_read_T reader;
7723
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007724 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007725 reader.js_fill = NULL;
7726 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007727 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007728}
7729
7730/*
7731 * "json_encode()" function
7732 */
7733 static void
7734f_json_encode(typval_T *argvars, typval_T *rettv)
7735{
7736 rettv->v_type = VAR_STRING;
7737 rettv->vval.v_string = json_encode(&argvars[0], 0);
7738}
7739
7740/*
7741 * "keys()" function
7742 */
7743 static void
7744f_keys(typval_T *argvars, typval_T *rettv)
7745{
7746 dict_list(argvars, rettv, 0);
7747}
7748
7749/*
7750 * "last_buffer_nr()" function.
7751 */
7752 static void
7753f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7754{
7755 int n = 0;
7756 buf_T *buf;
7757
Bram Moolenaar29323592016-07-24 22:04:11 +02007758 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007759 if (n < buf->b_fnum)
7760 n = buf->b_fnum;
7761
7762 rettv->vval.v_number = n;
7763}
7764
7765/*
7766 * "len()" function
7767 */
7768 static void
7769f_len(typval_T *argvars, typval_T *rettv)
7770{
7771 switch (argvars[0].v_type)
7772 {
7773 case VAR_STRING:
7774 case VAR_NUMBER:
7775 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007776 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007777 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007778 case VAR_BLOB:
7779 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7780 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007781 case VAR_LIST:
7782 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7783 break;
7784 case VAR_DICT:
7785 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7786 break;
7787 case VAR_UNKNOWN:
7788 case VAR_SPECIAL:
7789 case VAR_FLOAT:
7790 case VAR_FUNC:
7791 case VAR_PARTIAL:
7792 case VAR_JOB:
7793 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007794 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007795 break;
7796 }
7797}
7798
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007799 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007800libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007801{
7802#ifdef FEAT_LIBCALL
7803 char_u *string_in;
7804 char_u **string_result;
7805 int nr_result;
7806#endif
7807
7808 rettv->v_type = type;
7809 if (type != VAR_NUMBER)
7810 rettv->vval.v_string = NULL;
7811
7812 if (check_restricted() || check_secure())
7813 return;
7814
7815#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007816 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007817 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7818 {
7819 string_in = NULL;
7820 if (argvars[2].v_type == VAR_STRING)
7821 string_in = argvars[2].vval.v_string;
7822 if (type == VAR_NUMBER)
7823 string_result = NULL;
7824 else
7825 string_result = &rettv->vval.v_string;
7826 if (mch_libcall(argvars[0].vval.v_string,
7827 argvars[1].vval.v_string,
7828 string_in,
7829 argvars[2].vval.v_number,
7830 string_result,
7831 &nr_result) == OK
7832 && type == VAR_NUMBER)
7833 rettv->vval.v_number = nr_result;
7834 }
7835#endif
7836}
7837
7838/*
7839 * "libcall()" function
7840 */
7841 static void
7842f_libcall(typval_T *argvars, typval_T *rettv)
7843{
7844 libcall_common(argvars, rettv, VAR_STRING);
7845}
7846
7847/*
7848 * "libcallnr()" function
7849 */
7850 static void
7851f_libcallnr(typval_T *argvars, typval_T *rettv)
7852{
7853 libcall_common(argvars, rettv, VAR_NUMBER);
7854}
7855
7856/*
7857 * "line(string)" function
7858 */
7859 static void
7860f_line(typval_T *argvars, typval_T *rettv)
7861{
7862 linenr_T lnum = 0;
7863 pos_T *fp;
7864 int fnum;
7865
7866 fp = var2fpos(&argvars[0], TRUE, &fnum);
7867 if (fp != NULL)
7868 lnum = fp->lnum;
7869 rettv->vval.v_number = lnum;
7870}
7871
7872/*
7873 * "line2byte(lnum)" function
7874 */
7875 static void
7876f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7877{
7878#ifndef FEAT_BYTEOFF
7879 rettv->vval.v_number = -1;
7880#else
7881 linenr_T lnum;
7882
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007883 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007884 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7885 rettv->vval.v_number = -1;
7886 else
7887 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7888 if (rettv->vval.v_number >= 0)
7889 ++rettv->vval.v_number;
7890#endif
7891}
7892
7893/*
7894 * "lispindent(lnum)" function
7895 */
7896 static void
7897f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7898{
7899#ifdef FEAT_LISP
7900 pos_T pos;
7901 linenr_T lnum;
7902
7903 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007904 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007905 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7906 {
7907 curwin->w_cursor.lnum = lnum;
7908 rettv->vval.v_number = get_lisp_indent();
7909 curwin->w_cursor = pos;
7910 }
7911 else
7912#endif
7913 rettv->vval.v_number = -1;
7914}
7915
7916/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007917 * "list2str()" function
7918 */
7919 static void
7920f_list2str(typval_T *argvars, typval_T *rettv)
7921{
7922 list_T *l;
7923 listitem_T *li;
7924 garray_T ga;
7925 int utf8 = FALSE;
7926
7927 rettv->v_type = VAR_STRING;
7928 rettv->vval.v_string = NULL;
7929 if (argvars[0].v_type != VAR_LIST)
7930 {
7931 emsg(_(e_invarg));
7932 return;
7933 }
7934
7935 l = argvars[0].vval.v_list;
7936 if (l == NULL)
7937 return; // empty list results in empty string
7938
7939 if (argvars[1].v_type != VAR_UNKNOWN)
7940 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7941
7942 ga_init2(&ga, 1, 80);
7943 if (has_mbyte || utf8)
7944 {
7945 char_u buf[MB_MAXBYTES + 1];
7946 int (*char2bytes)(int, char_u *);
7947
7948 if (utf8 || enc_utf8)
7949 char2bytes = utf_char2bytes;
7950 else
7951 char2bytes = mb_char2bytes;
7952
7953 for (li = l->lv_first; li != NULL; li = li->li_next)
7954 {
7955 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7956 ga_concat(&ga, buf);
7957 }
7958 ga_append(&ga, NUL);
7959 }
7960 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7961 {
7962 for (li = l->lv_first; li != NULL; li = li->li_next)
7963 ga_append(&ga, tv_get_number(&li->li_tv));
7964 ga_append(&ga, NUL);
7965 }
7966
7967 rettv->v_type = VAR_STRING;
7968 rettv->vval.v_string = ga.ga_data;
7969}
7970
7971/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007972 * "localtime()" function
7973 */
7974 static void
7975f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7976{
7977 rettv->vval.v_number = (varnumber_T)time(NULL);
7978}
7979
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007980 static void
7981get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7982{
7983 char_u *keys;
7984 char_u *which;
7985 char_u buf[NUMBUFLEN];
7986 char_u *keys_buf = NULL;
7987 char_u *rhs;
7988 int mode;
7989 int abbr = FALSE;
7990 int get_dict = FALSE;
7991 mapblock_T *mp;
7992 int buffer_local;
7993
7994 /* return empty string for failure */
7995 rettv->v_type = VAR_STRING;
7996 rettv->vval.v_string = NULL;
7997
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007998 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007999 if (*keys == NUL)
8000 return;
8001
8002 if (argvars[1].v_type != VAR_UNKNOWN)
8003 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008004 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008005 if (argvars[2].v_type != VAR_UNKNOWN)
8006 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008007 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008008 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008009 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008010 }
8011 }
8012 else
8013 which = (char_u *)"";
8014 if (which == NULL)
8015 return;
8016
8017 mode = get_map_mode(&which, 0);
8018
8019 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8020 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8021 vim_free(keys_buf);
8022
8023 if (!get_dict)
8024 {
8025 /* Return a string. */
8026 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008027 {
8028 if (*rhs == NUL)
8029 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8030 else
8031 rettv->vval.v_string = str2special_save(rhs, FALSE);
8032 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008033
8034 }
8035 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8036 {
8037 /* Return a dictionary. */
8038 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8039 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8040 dict_T *dict = rettv->vval.v_dict;
8041
Bram Moolenaare0be1672018-07-08 16:50:37 +02008042 dict_add_string(dict, "lhs", lhs);
8043 dict_add_string(dict, "rhs", mp->m_orig_str);
8044 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8045 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8046 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008047 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8048 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008049 dict_add_number(dict, "buffer", (long)buffer_local);
8050 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8051 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008052
8053 vim_free(lhs);
8054 vim_free(mapmode);
8055 }
8056}
8057
8058#ifdef FEAT_FLOAT
8059/*
8060 * "log()" function
8061 */
8062 static void
8063f_log(typval_T *argvars, typval_T *rettv)
8064{
8065 float_T f = 0.0;
8066
8067 rettv->v_type = VAR_FLOAT;
8068 if (get_float_arg(argvars, &f) == OK)
8069 rettv->vval.v_float = log(f);
8070 else
8071 rettv->vval.v_float = 0.0;
8072}
8073
8074/*
8075 * "log10()" function
8076 */
8077 static void
8078f_log10(typval_T *argvars, typval_T *rettv)
8079{
8080 float_T f = 0.0;
8081
8082 rettv->v_type = VAR_FLOAT;
8083 if (get_float_arg(argvars, &f) == OK)
8084 rettv->vval.v_float = log10(f);
8085 else
8086 rettv->vval.v_float = 0.0;
8087}
8088#endif
8089
8090#ifdef FEAT_LUA
8091/*
8092 * "luaeval()" function
8093 */
8094 static void
8095f_luaeval(typval_T *argvars, typval_T *rettv)
8096{
8097 char_u *str;
8098 char_u buf[NUMBUFLEN];
8099
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008100 if (check_restricted() || check_secure())
8101 return;
8102
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008103 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008104 do_luaeval(str, argvars + 1, rettv);
8105}
8106#endif
8107
8108/*
8109 * "map()" function
8110 */
8111 static void
8112f_map(typval_T *argvars, typval_T *rettv)
8113{
8114 filter_map(argvars, rettv, TRUE);
8115}
8116
8117/*
8118 * "maparg()" function
8119 */
8120 static void
8121f_maparg(typval_T *argvars, typval_T *rettv)
8122{
8123 get_maparg(argvars, rettv, TRUE);
8124}
8125
8126/*
8127 * "mapcheck()" function
8128 */
8129 static void
8130f_mapcheck(typval_T *argvars, typval_T *rettv)
8131{
8132 get_maparg(argvars, rettv, FALSE);
8133}
8134
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008135typedef enum
8136{
8137 MATCH_END, /* matchend() */
8138 MATCH_MATCH, /* match() */
8139 MATCH_STR, /* matchstr() */
8140 MATCH_LIST, /* matchlist() */
8141 MATCH_POS /* matchstrpos() */
8142} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008143
8144 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008145find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146{
8147 char_u *str = NULL;
8148 long len = 0;
8149 char_u *expr = NULL;
8150 char_u *pat;
8151 regmatch_T regmatch;
8152 char_u patbuf[NUMBUFLEN];
8153 char_u strbuf[NUMBUFLEN];
8154 char_u *save_cpo;
8155 long start = 0;
8156 long nth = 1;
8157 colnr_T startcol = 0;
8158 int match = 0;
8159 list_T *l = NULL;
8160 listitem_T *li = NULL;
8161 long idx = 0;
8162 char_u *tofree = NULL;
8163
8164 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8165 save_cpo = p_cpo;
8166 p_cpo = (char_u *)"";
8167
8168 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008169 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008170 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008171 /* type MATCH_LIST: return empty list when there are no matches.
8172 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008173 if (rettv_list_alloc(rettv) == FAIL)
8174 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008175 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008176 && (list_append_string(rettv->vval.v_list,
8177 (char_u *)"", 0) == FAIL
8178 || list_append_number(rettv->vval.v_list,
8179 (varnumber_T)-1) == FAIL
8180 || list_append_number(rettv->vval.v_list,
8181 (varnumber_T)-1) == FAIL
8182 || list_append_number(rettv->vval.v_list,
8183 (varnumber_T)-1) == FAIL))
8184 {
8185 list_free(rettv->vval.v_list);
8186 rettv->vval.v_list = NULL;
8187 goto theend;
8188 }
8189 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008190 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008191 {
8192 rettv->v_type = VAR_STRING;
8193 rettv->vval.v_string = NULL;
8194 }
8195
8196 if (argvars[0].v_type == VAR_LIST)
8197 {
8198 if ((l = argvars[0].vval.v_list) == NULL)
8199 goto theend;
8200 li = l->lv_first;
8201 }
8202 else
8203 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008204 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008205 len = (long)STRLEN(str);
8206 }
8207
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008208 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008209 if (pat == NULL)
8210 goto theend;
8211
8212 if (argvars[2].v_type != VAR_UNKNOWN)
8213 {
8214 int error = FALSE;
8215
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008216 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008217 if (error)
8218 goto theend;
8219 if (l != NULL)
8220 {
8221 li = list_find(l, start);
8222 if (li == NULL)
8223 goto theend;
8224 idx = l->lv_idx; /* use the cached index */
8225 }
8226 else
8227 {
8228 if (start < 0)
8229 start = 0;
8230 if (start > len)
8231 goto theend;
8232 /* When "count" argument is there ignore matches before "start",
8233 * otherwise skip part of the string. Differs when pattern is "^"
8234 * or "\<". */
8235 if (argvars[3].v_type != VAR_UNKNOWN)
8236 startcol = start;
8237 else
8238 {
8239 str += start;
8240 len -= start;
8241 }
8242 }
8243
8244 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008245 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008246 if (error)
8247 goto theend;
8248 }
8249
8250 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8251 if (regmatch.regprog != NULL)
8252 {
8253 regmatch.rm_ic = p_ic;
8254
8255 for (;;)
8256 {
8257 if (l != NULL)
8258 {
8259 if (li == NULL)
8260 {
8261 match = FALSE;
8262 break;
8263 }
8264 vim_free(tofree);
8265 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8266 if (str == NULL)
8267 break;
8268 }
8269
8270 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8271
8272 if (match && --nth <= 0)
8273 break;
8274 if (l == NULL && !match)
8275 break;
8276
8277 /* Advance to just after the match. */
8278 if (l != NULL)
8279 {
8280 li = li->li_next;
8281 ++idx;
8282 }
8283 else
8284 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008285 startcol = (colnr_T)(regmatch.startp[0]
8286 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287 if (startcol > (colnr_T)len
8288 || str + startcol <= regmatch.startp[0])
8289 {
8290 match = FALSE;
8291 break;
8292 }
8293 }
8294 }
8295
8296 if (match)
8297 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008298 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299 {
8300 listitem_T *li1 = rettv->vval.v_list->lv_first;
8301 listitem_T *li2 = li1->li_next;
8302 listitem_T *li3 = li2->li_next;
8303 listitem_T *li4 = li3->li_next;
8304
8305 vim_free(li1->li_tv.vval.v_string);
8306 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8307 (int)(regmatch.endp[0] - regmatch.startp[0]));
8308 li3->li_tv.vval.v_number =
8309 (varnumber_T)(regmatch.startp[0] - expr);
8310 li4->li_tv.vval.v_number =
8311 (varnumber_T)(regmatch.endp[0] - expr);
8312 if (l != NULL)
8313 li2->li_tv.vval.v_number = (varnumber_T)idx;
8314 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008315 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008316 {
8317 int i;
8318
8319 /* return list with matched string and submatches */
8320 for (i = 0; i < NSUBEXP; ++i)
8321 {
8322 if (regmatch.endp[i] == NULL)
8323 {
8324 if (list_append_string(rettv->vval.v_list,
8325 (char_u *)"", 0) == FAIL)
8326 break;
8327 }
8328 else if (list_append_string(rettv->vval.v_list,
8329 regmatch.startp[i],
8330 (int)(regmatch.endp[i] - regmatch.startp[i]))
8331 == FAIL)
8332 break;
8333 }
8334 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008335 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008336 {
8337 /* return matched string */
8338 if (l != NULL)
8339 copy_tv(&li->li_tv, rettv);
8340 else
8341 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8342 (int)(regmatch.endp[0] - regmatch.startp[0]));
8343 }
8344 else if (l != NULL)
8345 rettv->vval.v_number = idx;
8346 else
8347 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008348 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008349 rettv->vval.v_number =
8350 (varnumber_T)(regmatch.startp[0] - str);
8351 else
8352 rettv->vval.v_number =
8353 (varnumber_T)(regmatch.endp[0] - str);
8354 rettv->vval.v_number += (varnumber_T)(str - expr);
8355 }
8356 }
8357 vim_regfree(regmatch.regprog);
8358 }
8359
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008360theend:
8361 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 /* matchstrpos() without a list: drop the second item. */
8363 listitem_remove(rettv->vval.v_list,
8364 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008365 vim_free(tofree);
8366 p_cpo = save_cpo;
8367}
8368
8369/*
8370 * "match()" function
8371 */
8372 static void
8373f_match(typval_T *argvars, typval_T *rettv)
8374{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008375 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008376}
8377
Bram Moolenaar95e51472018-07-28 16:55:56 +02008378#ifdef FEAT_SEARCH_EXTRA
8379 static int
8380matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8381{
8382 dictitem_T *di;
8383
8384 if (tv->v_type != VAR_DICT)
8385 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008386 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008387 return FAIL;
8388 }
8389
8390 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008391 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008392 (char_u *)"conceal", FALSE);
8393
8394 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8395 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008396 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008397 if (*win == NULL)
8398 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008399 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008400 return FAIL;
8401 }
8402 }
8403
8404 return OK;
8405}
8406#endif
8407
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408/*
8409 * "matchadd()" function
8410 */
8411 static void
8412f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8413{
8414#ifdef FEAT_SEARCH_EXTRA
8415 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008416 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8417 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008418 int prio = 10; /* default priority */
8419 int id = -1;
8420 int error = FALSE;
8421 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008422 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008423
8424 rettv->vval.v_number = -1;
8425
8426 if (grp == NULL || pat == NULL)
8427 return;
8428 if (argvars[2].v_type != VAR_UNKNOWN)
8429 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008430 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008431 if (argvars[3].v_type != VAR_UNKNOWN)
8432 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008433 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008434 if (argvars[4].v_type != VAR_UNKNOWN
8435 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8436 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008437 }
8438 }
8439 if (error == TRUE)
8440 return;
8441 if (id >= 1 && id <= 3)
8442 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008443 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008444 return;
8445 }
8446
Bram Moolenaar95e51472018-07-28 16:55:56 +02008447 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008448 conceal_char);
8449#endif
8450}
8451
8452/*
8453 * "matchaddpos()" function
8454 */
8455 static void
8456f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8457{
8458#ifdef FEAT_SEARCH_EXTRA
8459 char_u buf[NUMBUFLEN];
8460 char_u *group;
8461 int prio = 10;
8462 int id = -1;
8463 int error = FALSE;
8464 list_T *l;
8465 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008466 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008467
8468 rettv->vval.v_number = -1;
8469
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008470 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008471 if (group == NULL)
8472 return;
8473
8474 if (argvars[1].v_type != VAR_LIST)
8475 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008476 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008477 return;
8478 }
8479 l = argvars[1].vval.v_list;
8480 if (l == NULL)
8481 return;
8482
8483 if (argvars[2].v_type != VAR_UNKNOWN)
8484 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008485 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008486 if (argvars[3].v_type != VAR_UNKNOWN)
8487 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008488 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008489
8490 if (argvars[4].v_type != VAR_UNKNOWN
8491 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8492 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493 }
8494 }
8495 if (error == TRUE)
8496 return;
8497
8498 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8499 if (id == 1 || id == 2)
8500 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008501 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008502 return;
8503 }
8504
Bram Moolenaar95e51472018-07-28 16:55:56 +02008505 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008506 conceal_char);
8507#endif
8508}
8509
8510/*
8511 * "matcharg()" function
8512 */
8513 static void
8514f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8515{
8516 if (rettv_list_alloc(rettv) == OK)
8517 {
8518#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008519 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008520 matchitem_T *m;
8521
8522 if (id >= 1 && id <= 3)
8523 {
8524 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8525 {
8526 list_append_string(rettv->vval.v_list,
8527 syn_id2name(m->hlg_id), -1);
8528 list_append_string(rettv->vval.v_list, m->pattern, -1);
8529 }
8530 else
8531 {
8532 list_append_string(rettv->vval.v_list, NULL, -1);
8533 list_append_string(rettv->vval.v_list, NULL, -1);
8534 }
8535 }
8536#endif
8537 }
8538}
8539
8540/*
8541 * "matchdelete()" function
8542 */
8543 static void
8544f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8545{
8546#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008547 win_T *win = get_optional_window(argvars, 1);
8548
8549 if (win == NULL)
8550 rettv->vval.v_number = -1;
8551 else
8552 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008553 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008554#endif
8555}
8556
8557/*
8558 * "matchend()" function
8559 */
8560 static void
8561f_matchend(typval_T *argvars, typval_T *rettv)
8562{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008563 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008564}
8565
8566/*
8567 * "matchlist()" function
8568 */
8569 static void
8570f_matchlist(typval_T *argvars, typval_T *rettv)
8571{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008572 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008573}
8574
8575/*
8576 * "matchstr()" function
8577 */
8578 static void
8579f_matchstr(typval_T *argvars, typval_T *rettv)
8580{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008581 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582}
8583
8584/*
8585 * "matchstrpos()" function
8586 */
8587 static void
8588f_matchstrpos(typval_T *argvars, typval_T *rettv)
8589{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008590 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008591}
8592
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 static void
8594max_min(typval_T *argvars, typval_T *rettv, int domax)
8595{
8596 varnumber_T n = 0;
8597 varnumber_T i;
8598 int error = FALSE;
8599
8600 if (argvars[0].v_type == VAR_LIST)
8601 {
8602 list_T *l;
8603 listitem_T *li;
8604
8605 l = argvars[0].vval.v_list;
8606 if (l != NULL)
8607 {
8608 li = l->lv_first;
8609 if (li != NULL)
8610 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008611 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612 for (;;)
8613 {
8614 li = li->li_next;
8615 if (li == NULL)
8616 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008617 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618 if (domax ? i > n : i < n)
8619 n = i;
8620 }
8621 }
8622 }
8623 }
8624 else if (argvars[0].v_type == VAR_DICT)
8625 {
8626 dict_T *d;
8627 int first = TRUE;
8628 hashitem_T *hi;
8629 int todo;
8630
8631 d = argvars[0].vval.v_dict;
8632 if (d != NULL)
8633 {
8634 todo = (int)d->dv_hashtab.ht_used;
8635 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8636 {
8637 if (!HASHITEM_EMPTY(hi))
8638 {
8639 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008640 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008641 if (first)
8642 {
8643 n = i;
8644 first = FALSE;
8645 }
8646 else if (domax ? i > n : i < n)
8647 n = i;
8648 }
8649 }
8650 }
8651 }
8652 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008653 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008654 rettv->vval.v_number = error ? 0 : n;
8655}
8656
8657/*
8658 * "max()" function
8659 */
8660 static void
8661f_max(typval_T *argvars, typval_T *rettv)
8662{
8663 max_min(argvars, rettv, TRUE);
8664}
8665
8666/*
8667 * "min()" function
8668 */
8669 static void
8670f_min(typval_T *argvars, typval_T *rettv)
8671{
8672 max_min(argvars, rettv, FALSE);
8673}
8674
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008675/*
8676 * Create the directory in which "dir" is located, and higher levels when
8677 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008678 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008679 */
8680 static int
8681mkdir_recurse(char_u *dir, int prot)
8682{
8683 char_u *p;
8684 char_u *updir;
8685 int r = FAIL;
8686
8687 /* Get end of directory name in "dir".
8688 * We're done when it's "/" or "c:/". */
8689 p = gettail_sep(dir);
8690 if (p <= get_past_head(dir))
8691 return OK;
8692
8693 /* If the directory exists we're done. Otherwise: create it.*/
8694 updir = vim_strnsave(dir, (int)(p - dir));
8695 if (updir == NULL)
8696 return FAIL;
8697 if (mch_isdir(updir))
8698 r = OK;
8699 else if (mkdir_recurse(updir, prot) == OK)
8700 r = vim_mkdir_emsg(updir, prot);
8701 vim_free(updir);
8702 return r;
8703}
8704
8705#ifdef vim_mkdir
8706/*
8707 * "mkdir()" function
8708 */
8709 static void
8710f_mkdir(typval_T *argvars, typval_T *rettv)
8711{
8712 char_u *dir;
8713 char_u buf[NUMBUFLEN];
8714 int prot = 0755;
8715
8716 rettv->vval.v_number = FAIL;
8717 if (check_restricted() || check_secure())
8718 return;
8719
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008720 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008721 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008722 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008724 if (*gettail(dir) == NUL)
8725 /* remove trailing slashes */
8726 *gettail_sep(dir) = NUL;
8727
8728 if (argvars[1].v_type != VAR_UNKNOWN)
8729 {
8730 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008731 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008732 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008733 if (prot == -1)
8734 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008736 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008737 {
8738 if (mch_isdir(dir))
8739 {
8740 /* With the "p" flag it's OK if the dir already exists. */
8741 rettv->vval.v_number = OK;
8742 return;
8743 }
8744 mkdir_recurse(dir, prot);
8745 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008746 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008747 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008748}
8749#endif
8750
8751/*
8752 * "mode()" function
8753 */
8754 static void
8755f_mode(typval_T *argvars, typval_T *rettv)
8756{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008757 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758
Bram Moolenaar612cc382018-07-29 15:34:26 +02008759 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008760
8761 if (time_for_testing == 93784)
8762 {
8763 /* Testing the two-character code. */
8764 buf[0] = 'x';
8765 buf[1] = '!';
8766 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008767#ifdef FEAT_TERMINAL
8768 else if (term_use_loop())
8769 buf[0] = 't';
8770#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008771 else if (VIsual_active)
8772 {
8773 if (VIsual_select)
8774 buf[0] = VIsual_mode + 's' - 'v';
8775 else
8776 buf[0] = VIsual_mode;
8777 }
8778 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8779 || State == CONFIRM)
8780 {
8781 buf[0] = 'r';
8782 if (State == ASKMORE)
8783 buf[1] = 'm';
8784 else if (State == CONFIRM)
8785 buf[1] = '?';
8786 }
8787 else if (State == EXTERNCMD)
8788 buf[0] = '!';
8789 else if (State & INSERT)
8790 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008791 if (State & VREPLACE_FLAG)
8792 {
8793 buf[0] = 'R';
8794 buf[1] = 'v';
8795 }
8796 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008797 {
8798 if (State & REPLACE_FLAG)
8799 buf[0] = 'R';
8800 else
8801 buf[0] = 'i';
8802#ifdef FEAT_INS_EXPAND
8803 if (ins_compl_active())
8804 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008805 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008806 buf[1] = 'x';
8807#endif
8808 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008809 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008810 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008811 {
8812 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008813 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008814 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008815 else if (exmode_active == EXMODE_NORMAL)
8816 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008817 }
8818 else
8819 {
8820 buf[0] = 'n';
8821 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008822 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008823 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008824 // to be able to detect force-linewise/blockwise/characterwise operations
8825 buf[2] = motion_force;
8826 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008827 else if (restart_edit == 'I' || restart_edit == 'R'
8828 || restart_edit == 'V')
8829 {
8830 buf[1] = 'i';
8831 buf[2] = restart_edit;
8832 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008833 }
8834
8835 /* Clear out the minor mode when the argument is not a non-zero number or
8836 * non-empty string. */
8837 if (!non_zero_arg(&argvars[0]))
8838 buf[1] = NUL;
8839
8840 rettv->vval.v_string = vim_strsave(buf);
8841 rettv->v_type = VAR_STRING;
8842}
8843
8844#if defined(FEAT_MZSCHEME) || defined(PROTO)
8845/*
8846 * "mzeval()" function
8847 */
8848 static void
8849f_mzeval(typval_T *argvars, typval_T *rettv)
8850{
8851 char_u *str;
8852 char_u buf[NUMBUFLEN];
8853
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008854 if (check_restricted() || check_secure())
8855 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008856 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008857 do_mzeval(str, rettv);
8858}
8859
8860 void
8861mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8862{
8863 typval_T argvars[3];
8864
8865 argvars[0].v_type = VAR_STRING;
8866 argvars[0].vval.v_string = name;
8867 copy_tv(args, &argvars[1]);
8868 argvars[2].v_type = VAR_UNKNOWN;
8869 f_call(argvars, rettv);
8870 clear_tv(&argvars[1]);
8871}
8872#endif
8873
8874/*
8875 * "nextnonblank()" function
8876 */
8877 static void
8878f_nextnonblank(typval_T *argvars, typval_T *rettv)
8879{
8880 linenr_T lnum;
8881
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008882 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883 {
8884 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8885 {
8886 lnum = 0;
8887 break;
8888 }
8889 if (*skipwhite(ml_get(lnum)) != NUL)
8890 break;
8891 }
8892 rettv->vval.v_number = lnum;
8893}
8894
8895/*
8896 * "nr2char()" function
8897 */
8898 static void
8899f_nr2char(typval_T *argvars, typval_T *rettv)
8900{
8901 char_u buf[NUMBUFLEN];
8902
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008903 if (has_mbyte)
8904 {
8905 int utf8 = 0;
8906
8907 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008908 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008909 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008910 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008911 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008912 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008913 }
8914 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008916 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917 buf[1] = NUL;
8918 }
8919 rettv->v_type = VAR_STRING;
8920 rettv->vval.v_string = vim_strsave(buf);
8921}
8922
8923/*
8924 * "or(expr, expr)" function
8925 */
8926 static void
8927f_or(typval_T *argvars, typval_T *rettv)
8928{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008929 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8930 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008931}
8932
8933/*
8934 * "pathshorten()" function
8935 */
8936 static void
8937f_pathshorten(typval_T *argvars, typval_T *rettv)
8938{
8939 char_u *p;
8940
8941 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008942 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008943 if (p == NULL)
8944 rettv->vval.v_string = NULL;
8945 else
8946 {
8947 p = vim_strsave(p);
8948 rettv->vval.v_string = p;
8949 if (p != NULL)
8950 shorten_dir(p);
8951 }
8952}
8953
8954#ifdef FEAT_PERL
8955/*
8956 * "perleval()" function
8957 */
8958 static void
8959f_perleval(typval_T *argvars, typval_T *rettv)
8960{
8961 char_u *str;
8962 char_u buf[NUMBUFLEN];
8963
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008964 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008965 do_perleval(str, rettv);
8966}
8967#endif
8968
8969#ifdef FEAT_FLOAT
8970/*
8971 * "pow()" function
8972 */
8973 static void
8974f_pow(typval_T *argvars, typval_T *rettv)
8975{
8976 float_T fx = 0.0, fy = 0.0;
8977
8978 rettv->v_type = VAR_FLOAT;
8979 if (get_float_arg(argvars, &fx) == OK
8980 && get_float_arg(&argvars[1], &fy) == OK)
8981 rettv->vval.v_float = pow(fx, fy);
8982 else
8983 rettv->vval.v_float = 0.0;
8984}
8985#endif
8986
8987/*
8988 * "prevnonblank()" function
8989 */
8990 static void
8991f_prevnonblank(typval_T *argvars, typval_T *rettv)
8992{
8993 linenr_T lnum;
8994
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008995 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008996 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8997 lnum = 0;
8998 else
8999 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9000 --lnum;
9001 rettv->vval.v_number = lnum;
9002}
9003
9004/* This dummy va_list is here because:
9005 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9006 * - locally in the function results in a "used before set" warning
9007 * - using va_start() to initialize it gives "function with fixed args" error */
9008static va_list ap;
9009
9010/*
9011 * "printf()" function
9012 */
9013 static void
9014f_printf(typval_T *argvars, typval_T *rettv)
9015{
9016 char_u buf[NUMBUFLEN];
9017 int len;
9018 char_u *s;
9019 int saved_did_emsg = did_emsg;
9020 char *fmt;
9021
9022 rettv->v_type = VAR_STRING;
9023 rettv->vval.v_string = NULL;
9024
9025 /* Get the required length, allocate the buffer and do it for real. */
9026 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009027 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009028 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009029 if (!did_emsg)
9030 {
9031 s = alloc(len + 1);
9032 if (s != NULL)
9033 {
9034 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009035 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9036 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009037 }
9038 }
9039 did_emsg |= saved_did_emsg;
9040}
9041
Bram Moolenaarf2732452018-06-03 14:47:35 +02009042#ifdef FEAT_JOB_CHANNEL
9043/*
9044 * "prompt_setcallback({buffer}, {callback})" function
9045 */
9046 static void
9047f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9048{
9049 buf_T *buf;
9050 char_u *callback;
9051 partial_T *partial;
9052
9053 if (check_secure())
9054 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009055 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009056 if (buf == NULL)
9057 return;
9058
9059 callback = get_callback(&argvars[1], &partial);
9060 if (callback == NULL)
9061 return;
9062
9063 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
9064 if (partial == NULL)
9065 buf->b_prompt_callback = vim_strsave(callback);
9066 else
9067 /* pointer into the partial */
9068 buf->b_prompt_callback = callback;
9069 buf->b_prompt_partial = partial;
9070}
9071
9072/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009073 * "prompt_setinterrupt({buffer}, {callback})" function
9074 */
9075 static void
9076f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9077{
9078 buf_T *buf;
9079 char_u *callback;
9080 partial_T *partial;
9081
9082 if (check_secure())
9083 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009084 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009085 if (buf == NULL)
9086 return;
9087
9088 callback = get_callback(&argvars[1], &partial);
9089 if (callback == NULL)
9090 return;
9091
9092 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9093 if (partial == NULL)
9094 buf->b_prompt_interrupt = vim_strsave(callback);
9095 else
9096 /* pointer into the partial */
9097 buf->b_prompt_interrupt = callback;
9098 buf->b_prompt_int_partial = partial;
9099}
9100
9101/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009102 * "prompt_setprompt({buffer}, {text})" function
9103 */
9104 static void
9105f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9106{
9107 buf_T *buf;
9108 char_u *text;
9109
9110 if (check_secure())
9111 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009112 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009113 if (buf == NULL)
9114 return;
9115
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009116 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009117 vim_free(buf->b_prompt_text);
9118 buf->b_prompt_text = vim_strsave(text);
9119}
9120#endif
9121
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009122/*
9123 * "pumvisible()" function
9124 */
9125 static void
9126f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9127{
9128#ifdef FEAT_INS_EXPAND
9129 if (pum_visible())
9130 rettv->vval.v_number = 1;
9131#endif
9132}
9133
9134#ifdef FEAT_PYTHON3
9135/*
9136 * "py3eval()" function
9137 */
9138 static void
9139f_py3eval(typval_T *argvars, typval_T *rettv)
9140{
9141 char_u *str;
9142 char_u buf[NUMBUFLEN];
9143
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009144 if (check_restricted() || check_secure())
9145 return;
9146
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009147 if (p_pyx == 0)
9148 p_pyx = 3;
9149
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009150 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009151 do_py3eval(str, rettv);
9152}
9153#endif
9154
9155#ifdef FEAT_PYTHON
9156/*
9157 * "pyeval()" function
9158 */
9159 static void
9160f_pyeval(typval_T *argvars, typval_T *rettv)
9161{
9162 char_u *str;
9163 char_u buf[NUMBUFLEN];
9164
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009165 if (check_restricted() || check_secure())
9166 return;
9167
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009168 if (p_pyx == 0)
9169 p_pyx = 2;
9170
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009171 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009172 do_pyeval(str, rettv);
9173}
9174#endif
9175
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009176#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9177/*
9178 * "pyxeval()" function
9179 */
9180 static void
9181f_pyxeval(typval_T *argvars, typval_T *rettv)
9182{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009183 if (check_restricted() || check_secure())
9184 return;
9185
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009186# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9187 init_pyxversion();
9188 if (p_pyx == 2)
9189 f_pyeval(argvars, rettv);
9190 else
9191 f_py3eval(argvars, rettv);
9192# elif defined(FEAT_PYTHON)
9193 f_pyeval(argvars, rettv);
9194# elif defined(FEAT_PYTHON3)
9195 f_py3eval(argvars, rettv);
9196# endif
9197}
9198#endif
9199
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009200/*
9201 * "range()" function
9202 */
9203 static void
9204f_range(typval_T *argvars, typval_T *rettv)
9205{
9206 varnumber_T start;
9207 varnumber_T end;
9208 varnumber_T stride = 1;
9209 varnumber_T i;
9210 int error = FALSE;
9211
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009212 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009213 if (argvars[1].v_type == VAR_UNKNOWN)
9214 {
9215 end = start - 1;
9216 start = 0;
9217 }
9218 else
9219 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009220 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009221 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009222 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009223 }
9224
9225 if (error)
9226 return; /* type error; errmsg already given */
9227 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009228 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009229 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009230 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009231 else
9232 {
9233 if (rettv_list_alloc(rettv) == OK)
9234 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9235 if (list_append_number(rettv->vval.v_list,
9236 (varnumber_T)i) == FAIL)
9237 break;
9238 }
9239}
9240
9241/*
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009242 * Evaluate "expr" for readdir().
9243 */
9244 static int
9245readdir_checkitem(typval_T *expr, char_u *name)
9246{
9247 typval_T save_val;
9248 typval_T rettv;
9249 typval_T argv[2];
9250 int retval = 0;
9251 int error = FALSE;
9252
9253 prepare_vimvar(VV_VAL, &save_val);
9254 set_vim_var_string(VV_VAL, name, -1);
9255 argv[0].v_type = VAR_STRING;
9256 argv[0].vval.v_string = name;
9257
9258 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9259 goto theend;
9260
9261 retval = tv_get_number_chk(&rettv, &error);
9262 if (error)
9263 retval = -1;
9264 clear_tv(&rettv);
9265
9266theend:
9267 set_vim_var_string(VV_VAL, NULL, 0);
9268 restore_vimvar(VV_VAL, &save_val);
9269 return retval;
9270}
9271
9272/*
9273 * "readdir()" function
9274 */
9275 static void
9276f_readdir(typval_T *argvars, typval_T *rettv)
9277{
9278 typval_T *expr;
9279 int failed = FALSE;
9280 char_u *path;
9281 garray_T ga;
9282 int i;
9283#ifdef MSWIN
9284 char_u *buf, *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009285 int ok;
9286 HANDLE hFind = INVALID_HANDLE_VALUE;
9287 WIN32_FIND_DATAW wfb;
9288 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
9289#endif
9290
9291 if (rettv_list_alloc(rettv) == FAIL)
9292 return;
9293 path = tv_get_string(&argvars[0]);
9294 expr = &argvars[1];
9295 ga_init2(&ga, (int)sizeof(char *), 20);
9296
9297#ifdef MSWIN
9298 buf = alloc((int)MAXPATHL);
9299 if (buf == NULL)
9300 return;
9301 STRNCPY(buf, path, MAXPATHL-5);
9302 p = vim_strpbrk(path, (char_u *)"\\/");
9303 if (p != NULL)
9304 *p = NUL;
9305 STRCAT(buf, "\\*");
9306
9307 wn = enc_to_utf16(buf, NULL);
9308 if (wn != NULL)
9309 hFind = FindFirstFileW(wn, &wfb);
9310 ok = (hFind != INVALID_HANDLE_VALUE);
9311 if (!ok)
9312 smsg(_(e_notopen), path);
9313 else
9314 {
9315 while (ok)
9316 {
9317 int ignore;
9318
9319 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
9320 if (p == NULL)
9321 break; // out of memory
9322
9323 ignore = p[0] == '.' && (p[1] == NUL
9324 || (p[1] == '.' && p[2] == NUL));
9325 if (!ignore && expr->v_type != VAR_UNKNOWN)
9326 {
9327 int r = readdir_checkitem(expr, p);
9328
9329 if (r < 0)
9330 {
9331 vim_free(p);
9332 break;
9333 }
9334 if (r == 0)
9335 ignore = TRUE;
9336 }
9337
9338 if (!ignore)
9339 {
9340 if (ga_grow(&ga, 1) == OK)
9341 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9342 else
9343 {
9344 failed = TRUE;
9345 vim_free(p);
9346 break;
9347 }
9348 }
9349
9350 vim_free(p);
9351 ok = FindNextFileW(hFind, &wfb);
9352 }
9353 FindClose(hFind);
9354 }
9355
9356 vim_free(buf);
9357 vim_free(wn);
9358#else
9359 DIR *dirp;
9360 struct dirent *dp;
9361 char_u *p;
9362
9363 dirp = opendir((char *)path);
9364 if (dirp == NULL)
9365 smsg(_(e_notopen), path);
9366 else
9367 {
9368 for (;;)
9369 {
9370 int ignore;
9371
9372 dp = readdir(dirp);
9373 if (dp == NULL)
9374 break;
9375 p = (char_u *)dp->d_name;
9376
9377 ignore = p[0] == '.' &&
9378 (p[1] == NUL ||
9379 (p[1] == '.' && p[2] == NUL));
9380 if (!ignore && expr->v_type != VAR_UNKNOWN)
9381 {
9382 int r = readdir_checkitem(expr, p);
9383
9384 if (r < 0)
9385 break;
9386 if (r == 0)
9387 ignore = TRUE;
9388 }
9389
9390 if (!ignore)
9391 {
9392 if (ga_grow(&ga, 1) == OK)
9393 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9394 else
9395 {
9396 failed = TRUE;
9397 break;
9398 }
9399 }
9400 }
9401
9402 closedir(dirp);
9403 }
9404#endif
9405
Bram Moolenaar334ad412019-04-19 15:20:46 +02009406 if (!failed && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009407 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009408 sort_strings((char_u **)ga.ga_data, ga.ga_len);
9409 for (i = 0; i < ga.ga_len; i++)
9410 {
9411 p = ((char_u **)ga.ga_data)[i];
9412 list_append_string(rettv->vval.v_list, p, -1);
9413 }
9414 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009415 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009416}
9417
9418/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009419 * "readfile()" function
9420 */
9421 static void
9422f_readfile(typval_T *argvars, typval_T *rettv)
9423{
9424 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009425 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009426 int failed = FALSE;
9427 char_u *fname;
9428 FILE *fd;
9429 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9430 int io_size = sizeof(buf);
9431 int readlen; /* size of last fread() */
9432 char_u *prev = NULL; /* previously read bytes, if any */
9433 long prevlen = 0; /* length of data in prev */
9434 long prevsize = 0; /* size of prev buffer */
9435 long maxline = MAXLNUM;
9436 long cnt = 0;
9437 char_u *p; /* position in buf */
9438 char_u *start; /* start of current line */
9439
9440 if (argvars[1].v_type != VAR_UNKNOWN)
9441 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009442 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009443 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009444 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9445 blob = TRUE;
9446
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009447 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009448 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009449 }
9450
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009451 if (blob)
9452 {
9453 if (rettv_blob_alloc(rettv) == FAIL)
9454 return;
9455 }
9456 else
9457 {
9458 if (rettv_list_alloc(rettv) == FAIL)
9459 return;
9460 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009461
9462 /* Always open the file in binary mode, library functions have a mind of
9463 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009464 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009465 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9466 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009467 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009468 return;
9469 }
9470
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009471 if (blob)
9472 {
9473 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9474 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009475 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009476 blob_free(rettv->vval.v_blob);
9477 }
9478 fclose(fd);
9479 return;
9480 }
9481
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009482 while (cnt < maxline || maxline < 0)
9483 {
9484 readlen = (int)fread(buf, 1, io_size, fd);
9485
9486 /* This for loop processes what was read, but is also entered at end
9487 * of file so that either:
9488 * - an incomplete line gets written
9489 * - a "binary" file gets an empty line at the end if it ends in a
9490 * newline. */
9491 for (p = buf, start = buf;
9492 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9493 ++p)
9494 {
9495 if (*p == '\n' || readlen <= 0)
9496 {
9497 listitem_T *li;
9498 char_u *s = NULL;
9499 long_u len = p - start;
9500
9501 /* Finished a line. Remove CRs before NL. */
9502 if (readlen > 0 && !binary)
9503 {
9504 while (len > 0 && start[len - 1] == '\r')
9505 --len;
9506 /* removal may cross back to the "prev" string */
9507 if (len == 0)
9508 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9509 --prevlen;
9510 }
9511 if (prevlen == 0)
9512 s = vim_strnsave(start, (int)len);
9513 else
9514 {
9515 /* Change "prev" buffer to be the right size. This way
9516 * the bytes are only copied once, and very long lines are
9517 * allocated only once. */
9518 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9519 {
9520 mch_memmove(s + prevlen, start, len);
9521 s[prevlen + len] = NUL;
9522 prev = NULL; /* the list will own the string */
9523 prevlen = prevsize = 0;
9524 }
9525 }
9526 if (s == NULL)
9527 {
9528 do_outofmem_msg((long_u) prevlen + len + 1);
9529 failed = TRUE;
9530 break;
9531 }
9532
9533 if ((li = listitem_alloc()) == NULL)
9534 {
9535 vim_free(s);
9536 failed = TRUE;
9537 break;
9538 }
9539 li->li_tv.v_type = VAR_STRING;
9540 li->li_tv.v_lock = 0;
9541 li->li_tv.vval.v_string = s;
9542 list_append(rettv->vval.v_list, li);
9543
9544 start = p + 1; /* step over newline */
9545 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9546 break;
9547 }
9548 else if (*p == NUL)
9549 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009550 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9551 * when finding the BF and check the previous two bytes. */
9552 else if (*p == 0xbf && enc_utf8 && !binary)
9553 {
9554 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9555 * + 1, these may be in the "prev" string. */
9556 char_u back1 = p >= buf + 1 ? p[-1]
9557 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9558 char_u back2 = p >= buf + 2 ? p[-2]
9559 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9560 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9561
9562 if (back2 == 0xef && back1 == 0xbb)
9563 {
9564 char_u *dest = p - 2;
9565
9566 /* Usually a BOM is at the beginning of a file, and so at
9567 * the beginning of a line; then we can just step over it.
9568 */
9569 if (start == dest)
9570 start = p + 1;
9571 else
9572 {
9573 /* have to shuffle buf to close gap */
9574 int adjust_prevlen = 0;
9575
9576 if (dest < buf)
9577 {
9578 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9579 dest = buf;
9580 }
9581 if (readlen > p - buf + 1)
9582 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9583 readlen -= 3 - adjust_prevlen;
9584 prevlen -= adjust_prevlen;
9585 p = dest - 1;
9586 }
9587 }
9588 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009589 } /* for */
9590
9591 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9592 break;
9593 if (start < p)
9594 {
9595 /* There's part of a line in buf, store it in "prev". */
9596 if (p - start + prevlen >= prevsize)
9597 {
9598 /* need bigger "prev" buffer */
9599 char_u *newprev;
9600
9601 /* A common use case is ordinary text files and "prev" gets a
9602 * fragment of a line, so the first allocation is made
9603 * small, to avoid repeatedly 'allocing' large and
9604 * 'reallocing' small. */
9605 if (prevsize == 0)
9606 prevsize = (long)(p - start);
9607 else
9608 {
9609 long grow50pc = (prevsize * 3) / 2;
9610 long growmin = (long)((p - start) * 2 + prevlen);
9611 prevsize = grow50pc > growmin ? grow50pc : growmin;
9612 }
9613 newprev = prev == NULL ? alloc(prevsize)
9614 : vim_realloc(prev, prevsize);
9615 if (newprev == NULL)
9616 {
9617 do_outofmem_msg((long_u)prevsize);
9618 failed = TRUE;
9619 break;
9620 }
9621 prev = newprev;
9622 }
9623 /* Add the line part to end of "prev". */
9624 mch_memmove(prev + prevlen, start, p - start);
9625 prevlen += (long)(p - start);
9626 }
9627 } /* while */
9628
9629 /*
9630 * For a negative line count use only the lines at the end of the file,
9631 * free the rest.
9632 */
9633 if (!failed && maxline < 0)
9634 while (cnt > -maxline)
9635 {
9636 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9637 --cnt;
9638 }
9639
9640 if (failed)
9641 {
9642 list_free(rettv->vval.v_list);
9643 /* readfile doc says an empty list is returned on error */
9644 rettv->vval.v_list = list_alloc();
9645 }
9646
9647 vim_free(prev);
9648 fclose(fd);
9649}
9650
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009651 static void
9652return_register(int regname, typval_T *rettv)
9653{
9654 char_u buf[2] = {0, 0};
9655
9656 buf[0] = (char_u)regname;
9657 rettv->v_type = VAR_STRING;
9658 rettv->vval.v_string = vim_strsave(buf);
9659}
9660
9661/*
9662 * "reg_executing()" function
9663 */
9664 static void
9665f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9666{
9667 return_register(reg_executing, rettv);
9668}
9669
9670/*
9671 * "reg_recording()" function
9672 */
9673 static void
9674f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9675{
9676 return_register(reg_recording, rettv);
9677}
9678
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009679#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009680/*
9681 * Convert a List to proftime_T.
9682 * Return FAIL when there is something wrong.
9683 */
9684 static int
9685list2proftime(typval_T *arg, proftime_T *tm)
9686{
9687 long n1, n2;
9688 int error = FALSE;
9689
9690 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9691 || arg->vval.v_list->lv_len != 2)
9692 return FAIL;
9693 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9694 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009695# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009696 tm->HighPart = n1;
9697 tm->LowPart = n2;
9698# else
9699 tm->tv_sec = n1;
9700 tm->tv_usec = n2;
9701# endif
9702 return error ? FAIL : OK;
9703}
9704#endif /* FEAT_RELTIME */
9705
9706/*
9707 * "reltime()" function
9708 */
9709 static void
9710f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9711{
9712#ifdef FEAT_RELTIME
9713 proftime_T res;
9714 proftime_T start;
9715
9716 if (argvars[0].v_type == VAR_UNKNOWN)
9717 {
9718 /* No arguments: get current time. */
9719 profile_start(&res);
9720 }
9721 else if (argvars[1].v_type == VAR_UNKNOWN)
9722 {
9723 if (list2proftime(&argvars[0], &res) == FAIL)
9724 return;
9725 profile_end(&res);
9726 }
9727 else
9728 {
9729 /* Two arguments: compute the difference. */
9730 if (list2proftime(&argvars[0], &start) == FAIL
9731 || list2proftime(&argvars[1], &res) == FAIL)
9732 return;
9733 profile_sub(&res, &start);
9734 }
9735
9736 if (rettv_list_alloc(rettv) == OK)
9737 {
9738 long n1, n2;
9739
Bram Moolenaar4f974752019-02-17 17:44:42 +01009740# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009741 n1 = res.HighPart;
9742 n2 = res.LowPart;
9743# else
9744 n1 = res.tv_sec;
9745 n2 = res.tv_usec;
9746# endif
9747 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9748 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9749 }
9750#endif
9751}
9752
9753#ifdef FEAT_FLOAT
9754/*
9755 * "reltimefloat()" function
9756 */
9757 static void
9758f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9759{
9760# ifdef FEAT_RELTIME
9761 proftime_T tm;
9762# endif
9763
9764 rettv->v_type = VAR_FLOAT;
9765 rettv->vval.v_float = 0;
9766# ifdef FEAT_RELTIME
9767 if (list2proftime(&argvars[0], &tm) == OK)
9768 rettv->vval.v_float = profile_float(&tm);
9769# endif
9770}
9771#endif
9772
9773/*
9774 * "reltimestr()" function
9775 */
9776 static void
9777f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9778{
9779#ifdef FEAT_RELTIME
9780 proftime_T tm;
9781#endif
9782
9783 rettv->v_type = VAR_STRING;
9784 rettv->vval.v_string = NULL;
9785#ifdef FEAT_RELTIME
9786 if (list2proftime(&argvars[0], &tm) == OK)
9787 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9788#endif
9789}
9790
9791#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009792 static void
9793make_connection(void)
9794{
9795 if (X_DISPLAY == NULL
9796# ifdef FEAT_GUI
9797 && !gui.in_use
9798# endif
9799 )
9800 {
9801 x_force_connect = TRUE;
9802 setup_term_clip();
9803 x_force_connect = FALSE;
9804 }
9805}
9806
9807 static int
9808check_connection(void)
9809{
9810 make_connection();
9811 if (X_DISPLAY == NULL)
9812 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009813 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009814 return FAIL;
9815 }
9816 return OK;
9817}
9818#endif
9819
9820#ifdef FEAT_CLIENTSERVER
9821 static void
9822remote_common(typval_T *argvars, typval_T *rettv, int expr)
9823{
9824 char_u *server_name;
9825 char_u *keys;
9826 char_u *r = NULL;
9827 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009828 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009829# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009830 HWND w;
9831# else
9832 Window w;
9833# endif
9834
9835 if (check_restricted() || check_secure())
9836 return;
9837
9838# ifdef FEAT_X11
9839 if (check_connection() == FAIL)
9840 return;
9841# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009842 if (argvars[2].v_type != VAR_UNKNOWN
9843 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009844 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009845
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009846 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009847 if (server_name == NULL)
9848 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009849 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009850# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009851 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009852# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009853 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9854 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009855# endif
9856 {
9857 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009858 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009859 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009860 vim_free(r);
9861 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009862 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009863 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009864 return;
9865 }
9866
9867 rettv->vval.v_string = r;
9868
9869 if (argvars[2].v_type != VAR_UNKNOWN)
9870 {
9871 dictitem_T v;
9872 char_u str[30];
9873 char_u *idvar;
9874
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009875 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009876 if (idvar != NULL && *idvar != NUL)
9877 {
9878 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9879 v.di_tv.v_type = VAR_STRING;
9880 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009881 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009882 vim_free(v.di_tv.vval.v_string);
9883 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009884 }
9885}
9886#endif
9887
9888/*
9889 * "remote_expr()" function
9890 */
9891 static void
9892f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9893{
9894 rettv->v_type = VAR_STRING;
9895 rettv->vval.v_string = NULL;
9896#ifdef FEAT_CLIENTSERVER
9897 remote_common(argvars, rettv, TRUE);
9898#endif
9899}
9900
9901/*
9902 * "remote_foreground()" function
9903 */
9904 static void
9905f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9906{
9907#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009908# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009909 /* On Win32 it's done in this application. */
9910 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009911 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009912
9913 if (server_name != NULL)
9914 serverForeground(server_name);
9915 }
9916# else
9917 /* Send a foreground() expression to the server. */
9918 argvars[1].v_type = VAR_STRING;
9919 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9920 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009921 rettv->v_type = VAR_STRING;
9922 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009923 remote_common(argvars, rettv, TRUE);
9924 vim_free(argvars[1].vval.v_string);
9925# endif
9926#endif
9927}
9928
9929 static void
9930f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9931{
9932#ifdef FEAT_CLIENTSERVER
9933 dictitem_T v;
9934 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009935# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009936 long_u n = 0;
9937# endif
9938 char_u *serverid;
9939
9940 if (check_restricted() || check_secure())
9941 {
9942 rettv->vval.v_number = -1;
9943 return;
9944 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009945 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009946 if (serverid == NULL)
9947 {
9948 rettv->vval.v_number = -1;
9949 return; /* type error; errmsg already given */
9950 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009951# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009952 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9953 if (n == 0)
9954 rettv->vval.v_number = -1;
9955 else
9956 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009957 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009958 rettv->vval.v_number = (s != NULL);
9959 }
9960# else
9961 if (check_connection() == FAIL)
9962 return;
9963
9964 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9965 serverStrToWin(serverid), &s);
9966# endif
9967
9968 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9969 {
9970 char_u *retvar;
9971
9972 v.di_tv.v_type = VAR_STRING;
9973 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009974 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009975 if (retvar != NULL)
9976 set_var(retvar, &v.di_tv, FALSE);
9977 vim_free(v.di_tv.vval.v_string);
9978 }
9979#else
9980 rettv->vval.v_number = -1;
9981#endif
9982}
9983
9984 static void
9985f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9986{
9987 char_u *r = NULL;
9988
9989#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009990 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009991
9992 if (serverid != NULL && !check_restricted() && !check_secure())
9993 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009994 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009995# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009996 /* The server's HWND is encoded in the 'id' parameter */
9997 long_u n = 0;
9998# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009999
10000 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010001 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010002
Bram Moolenaar4f974752019-02-17 17:44:42 +010010003# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010004 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10005 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010006 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010007 if (r == NULL)
10008# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010009 if (check_connection() == FAIL
10010 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10011 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010012# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010013 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010014 }
10015#endif
10016 rettv->v_type = VAR_STRING;
10017 rettv->vval.v_string = r;
10018}
10019
10020/*
10021 * "remote_send()" function
10022 */
10023 static void
10024f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10025{
10026 rettv->v_type = VAR_STRING;
10027 rettv->vval.v_string = NULL;
10028#ifdef FEAT_CLIENTSERVER
10029 remote_common(argvars, rettv, FALSE);
10030#endif
10031}
10032
10033/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010034 * "remote_startserver()" function
10035 */
10036 static void
10037f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10038{
10039#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010040 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010041
10042 if (server == NULL)
10043 return; /* type error; errmsg already given */
10044 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010045 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010046 else
10047 {
10048# ifdef FEAT_X11
10049 if (check_connection() == OK)
10050 serverRegisterName(X_DISPLAY, server);
10051# else
10052 serverSetName(server);
10053# endif
10054 }
10055#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010056 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010057#endif
10058}
10059
10060/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010061 * "remove()" function
10062 */
10063 static void
10064f_remove(typval_T *argvars, typval_T *rettv)
10065{
10066 list_T *l;
10067 listitem_T *item, *item2;
10068 listitem_T *li;
10069 long idx;
10070 long end;
10071 char_u *key;
10072 dict_T *d;
10073 dictitem_T *di;
10074 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010075 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010076
10077 if (argvars[0].v_type == VAR_DICT)
10078 {
10079 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010080 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010081 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010082 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010083 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010084 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010085 if (key != NULL)
10086 {
10087 di = dict_find(d, key, -1);
10088 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010089 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010090 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10091 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10092 {
10093 *rettv = di->di_tv;
10094 init_tv(&di->di_tv);
10095 dictitem_remove(d, di);
10096 }
10097 }
10098 }
10099 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010100 else if (argvars[0].v_type == VAR_BLOB)
10101 {
10102 idx = (long)tv_get_number_chk(&argvars[1], &error);
10103 if (!error)
10104 {
10105 blob_T *b = argvars[0].vval.v_blob;
10106 int len = blob_len(b);
10107 char_u *p;
10108
10109 if (idx < 0)
10110 // count from the end
10111 idx = len + idx;
10112 if (idx < 0 || idx >= len)
10113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010114 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010115 return;
10116 }
10117 if (argvars[2].v_type == VAR_UNKNOWN)
10118 {
10119 // Remove one item, return its value.
10120 p = (char_u *)b->bv_ga.ga_data;
10121 rettv->vval.v_number = (varnumber_T) *(p + idx);
10122 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10123 --b->bv_ga.ga_len;
10124 }
10125 else
10126 {
10127 blob_T *blob;
10128
10129 // Remove range of items, return list with values.
10130 end = (long)tv_get_number_chk(&argvars[2], &error);
10131 if (error)
10132 return;
10133 if (end < 0)
10134 // count from the end
10135 end = len + end;
10136 if (end >= len || idx > end)
10137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010138 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010139 return;
10140 }
10141 blob = blob_alloc();
10142 if (blob == NULL)
10143 return;
10144 blob->bv_ga.ga_len = end - idx + 1;
10145 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10146 {
10147 vim_free(blob);
10148 return;
10149 }
10150 p = (char_u *)b->bv_ga.ga_data;
10151 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10152 (size_t)(end - idx + 1));
10153 ++blob->bv_refcount;
10154 rettv->v_type = VAR_BLOB;
10155 rettv->vval.v_blob = blob;
10156
10157 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10158 b->bv_ga.ga_len -= end - idx + 1;
10159 }
10160 }
10161 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010162 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010163 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010164 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010165 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010166 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010167 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010168 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010169 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010170 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010171 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010172 else
10173 {
10174 if (argvars[2].v_type == VAR_UNKNOWN)
10175 {
10176 /* Remove one item, return its value. */
10177 vimlist_remove(l, item, item);
10178 *rettv = item->li_tv;
10179 vim_free(item);
10180 }
10181 else
10182 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010183 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010184 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010185 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010186 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010187 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010188 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010189 else
10190 {
10191 int cnt = 0;
10192
10193 for (li = item; li != NULL; li = li->li_next)
10194 {
10195 ++cnt;
10196 if (li == item2)
10197 break;
10198 }
10199 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010200 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010201 else
10202 {
10203 vimlist_remove(l, item, item2);
10204 if (rettv_list_alloc(rettv) == OK)
10205 {
10206 l = rettv->vval.v_list;
10207 l->lv_first = item;
10208 l->lv_last = item2;
10209 item->li_prev = NULL;
10210 item2->li_next = NULL;
10211 l->lv_len = cnt;
10212 }
10213 }
10214 }
10215 }
10216 }
10217 }
10218}
10219
10220/*
10221 * "rename({from}, {to})" function
10222 */
10223 static void
10224f_rename(typval_T *argvars, typval_T *rettv)
10225{
10226 char_u buf[NUMBUFLEN];
10227
10228 if (check_restricted() || check_secure())
10229 rettv->vval.v_number = -1;
10230 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010231 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10232 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010233}
10234
10235/*
10236 * "repeat()" function
10237 */
10238 static void
10239f_repeat(typval_T *argvars, typval_T *rettv)
10240{
10241 char_u *p;
10242 int n;
10243 int slen;
10244 int len;
10245 char_u *r;
10246 int i;
10247
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010248 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010249 if (argvars[0].v_type == VAR_LIST)
10250 {
10251 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10252 while (n-- > 0)
10253 if (list_extend(rettv->vval.v_list,
10254 argvars[0].vval.v_list, NULL) == FAIL)
10255 break;
10256 }
10257 else
10258 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010259 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010260 rettv->v_type = VAR_STRING;
10261 rettv->vval.v_string = NULL;
10262
10263 slen = (int)STRLEN(p);
10264 len = slen * n;
10265 if (len <= 0)
10266 return;
10267
10268 r = alloc(len + 1);
10269 if (r != NULL)
10270 {
10271 for (i = 0; i < n; i++)
10272 mch_memmove(r + i * slen, p, (size_t)slen);
10273 r[len] = NUL;
10274 }
10275
10276 rettv->vval.v_string = r;
10277 }
10278}
10279
10280/*
10281 * "resolve()" function
10282 */
10283 static void
10284f_resolve(typval_T *argvars, typval_T *rettv)
10285{
10286 char_u *p;
10287#ifdef HAVE_READLINK
10288 char_u *buf = NULL;
10289#endif
10290
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010291 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010292#ifdef FEAT_SHORTCUT
10293 {
10294 char_u *v = NULL;
10295
Bram Moolenaardce1e892019-02-10 23:18:53 +010010296 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010297 if (v != NULL)
10298 rettv->vval.v_string = v;
10299 else
10300 rettv->vval.v_string = vim_strsave(p);
10301 }
10302#else
10303# ifdef HAVE_READLINK
10304 {
10305 char_u *cpy;
10306 int len;
10307 char_u *remain = NULL;
10308 char_u *q;
10309 int is_relative_to_current = FALSE;
10310 int has_trailing_pathsep = FALSE;
10311 int limit = 100;
10312
10313 p = vim_strsave(p);
10314
10315 if (p[0] == '.' && (vim_ispathsep(p[1])
10316 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10317 is_relative_to_current = TRUE;
10318
10319 len = STRLEN(p);
10320 if (len > 0 && after_pathsep(p, p + len))
10321 {
10322 has_trailing_pathsep = TRUE;
10323 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10324 }
10325
10326 q = getnextcomp(p);
10327 if (*q != NUL)
10328 {
10329 /* Separate the first path component in "p", and keep the
10330 * remainder (beginning with the path separator). */
10331 remain = vim_strsave(q - 1);
10332 q[-1] = NUL;
10333 }
10334
10335 buf = alloc(MAXPATHL + 1);
10336 if (buf == NULL)
10337 goto fail;
10338
10339 for (;;)
10340 {
10341 for (;;)
10342 {
10343 len = readlink((char *)p, (char *)buf, MAXPATHL);
10344 if (len <= 0)
10345 break;
10346 buf[len] = NUL;
10347
10348 if (limit-- == 0)
10349 {
10350 vim_free(p);
10351 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010352 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010353 rettv->vval.v_string = NULL;
10354 goto fail;
10355 }
10356
10357 /* Ensure that the result will have a trailing path separator
10358 * if the argument has one. */
10359 if (remain == NULL && has_trailing_pathsep)
10360 add_pathsep(buf);
10361
10362 /* Separate the first path component in the link value and
10363 * concatenate the remainders. */
10364 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10365 if (*q != NUL)
10366 {
10367 if (remain == NULL)
10368 remain = vim_strsave(q - 1);
10369 else
10370 {
10371 cpy = concat_str(q - 1, remain);
10372 if (cpy != NULL)
10373 {
10374 vim_free(remain);
10375 remain = cpy;
10376 }
10377 }
10378 q[-1] = NUL;
10379 }
10380
10381 q = gettail(p);
10382 if (q > p && *q == NUL)
10383 {
10384 /* Ignore trailing path separator. */
10385 q[-1] = NUL;
10386 q = gettail(p);
10387 }
10388 if (q > p && !mch_isFullName(buf))
10389 {
10390 /* symlink is relative to directory of argument */
10391 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10392 if (cpy != NULL)
10393 {
10394 STRCPY(cpy, p);
10395 STRCPY(gettail(cpy), buf);
10396 vim_free(p);
10397 p = cpy;
10398 }
10399 }
10400 else
10401 {
10402 vim_free(p);
10403 p = vim_strsave(buf);
10404 }
10405 }
10406
10407 if (remain == NULL)
10408 break;
10409
10410 /* Append the first path component of "remain" to "p". */
10411 q = getnextcomp(remain + 1);
10412 len = q - remain - (*q != NUL);
10413 cpy = vim_strnsave(p, STRLEN(p) + len);
10414 if (cpy != NULL)
10415 {
10416 STRNCAT(cpy, remain, len);
10417 vim_free(p);
10418 p = cpy;
10419 }
10420 /* Shorten "remain". */
10421 if (*q != NUL)
10422 STRMOVE(remain, q - 1);
10423 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010424 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010425 }
10426
10427 /* If the result is a relative path name, make it explicitly relative to
10428 * the current directory if and only if the argument had this form. */
10429 if (!vim_ispathsep(*p))
10430 {
10431 if (is_relative_to_current
10432 && *p != NUL
10433 && !(p[0] == '.'
10434 && (p[1] == NUL
10435 || vim_ispathsep(p[1])
10436 || (p[1] == '.'
10437 && (p[2] == NUL
10438 || vim_ispathsep(p[2]))))))
10439 {
10440 /* Prepend "./". */
10441 cpy = concat_str((char_u *)"./", p);
10442 if (cpy != NULL)
10443 {
10444 vim_free(p);
10445 p = cpy;
10446 }
10447 }
10448 else if (!is_relative_to_current)
10449 {
10450 /* Strip leading "./". */
10451 q = p;
10452 while (q[0] == '.' && vim_ispathsep(q[1]))
10453 q += 2;
10454 if (q > p)
10455 STRMOVE(p, p + 2);
10456 }
10457 }
10458
10459 /* Ensure that the result will have no trailing path separator
10460 * if the argument had none. But keep "/" or "//". */
10461 if (!has_trailing_pathsep)
10462 {
10463 q = p + STRLEN(p);
10464 if (after_pathsep(p, q))
10465 *gettail_sep(p) = NUL;
10466 }
10467
10468 rettv->vval.v_string = p;
10469 }
10470# else
10471 rettv->vval.v_string = vim_strsave(p);
10472# endif
10473#endif
10474
10475 simplify_filename(rettv->vval.v_string);
10476
10477#ifdef HAVE_READLINK
10478fail:
10479 vim_free(buf);
10480#endif
10481 rettv->v_type = VAR_STRING;
10482}
10483
10484/*
10485 * "reverse({list})" function
10486 */
10487 static void
10488f_reverse(typval_T *argvars, typval_T *rettv)
10489{
10490 list_T *l;
10491 listitem_T *li, *ni;
10492
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010493 if (argvars[0].v_type == VAR_BLOB)
10494 {
10495 blob_T *b = argvars[0].vval.v_blob;
10496 int i, len = blob_len(b);
10497
10498 for (i = 0; i < len / 2; i++)
10499 {
10500 int tmp = blob_get(b, i);
10501
10502 blob_set(b, i, blob_get(b, len - i - 1));
10503 blob_set(b, len - i - 1, tmp);
10504 }
10505 rettv_blob_set(rettv, b);
10506 return;
10507 }
10508
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010509 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010510 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010511 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010512 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010513 (char_u *)N_("reverse() argument"), TRUE))
10514 {
10515 li = l->lv_last;
10516 l->lv_first = l->lv_last = NULL;
10517 l->lv_len = 0;
10518 while (li != NULL)
10519 {
10520 ni = li->li_prev;
10521 list_append(l, li);
10522 li = ni;
10523 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010524 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010525 l->lv_idx = l->lv_len - l->lv_idx - 1;
10526 }
10527}
10528
10529#define SP_NOMOVE 0x01 /* don't move cursor */
10530#define SP_REPEAT 0x02 /* repeat to find outer pair */
10531#define SP_RETCOUNT 0x04 /* return matchcount */
10532#define SP_SETPCMARK 0x08 /* set previous context mark */
10533#define SP_START 0x10 /* accept match at start position */
10534#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10535#define SP_END 0x40 /* leave cursor at end of match */
10536#define SP_COLUMN 0x80 /* start at cursor column */
10537
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010538/*
10539 * Get flags for a search function.
10540 * Possibly sets "p_ws".
10541 * Returns BACKWARD, FORWARD or zero (for an error).
10542 */
10543 static int
10544get_search_arg(typval_T *varp, int *flagsp)
10545{
10546 int dir = FORWARD;
10547 char_u *flags;
10548 char_u nbuf[NUMBUFLEN];
10549 int mask;
10550
10551 if (varp->v_type != VAR_UNKNOWN)
10552 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010553 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010554 if (flags == NULL)
10555 return 0; /* type error; errmsg already given */
10556 while (*flags != NUL)
10557 {
10558 switch (*flags)
10559 {
10560 case 'b': dir = BACKWARD; break;
10561 case 'w': p_ws = TRUE; break;
10562 case 'W': p_ws = FALSE; break;
10563 default: mask = 0;
10564 if (flagsp != NULL)
10565 switch (*flags)
10566 {
10567 case 'c': mask = SP_START; break;
10568 case 'e': mask = SP_END; break;
10569 case 'm': mask = SP_RETCOUNT; break;
10570 case 'n': mask = SP_NOMOVE; break;
10571 case 'p': mask = SP_SUBPAT; break;
10572 case 'r': mask = SP_REPEAT; break;
10573 case 's': mask = SP_SETPCMARK; break;
10574 case 'z': mask = SP_COLUMN; break;
10575 }
10576 if (mask == 0)
10577 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010578 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010579 dir = 0;
10580 }
10581 else
10582 *flagsp |= mask;
10583 }
10584 if (dir == 0)
10585 break;
10586 ++flags;
10587 }
10588 }
10589 return dir;
10590}
10591
10592/*
10593 * Shared by search() and searchpos() functions.
10594 */
10595 static int
10596search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10597{
10598 int flags;
10599 char_u *pat;
10600 pos_T pos;
10601 pos_T save_cursor;
10602 int save_p_ws = p_ws;
10603 int dir;
10604 int retval = 0; /* default: FAIL */
10605 long lnum_stop = 0;
10606 proftime_T tm;
10607#ifdef FEAT_RELTIME
10608 long time_limit = 0;
10609#endif
10610 int options = SEARCH_KEEP;
10611 int subpatnum;
10612
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010613 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010614 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10615 if (dir == 0)
10616 goto theend;
10617 flags = *flagsp;
10618 if (flags & SP_START)
10619 options |= SEARCH_START;
10620 if (flags & SP_END)
10621 options |= SEARCH_END;
10622 if (flags & SP_COLUMN)
10623 options |= SEARCH_COL;
10624
10625 /* Optional arguments: line number to stop searching and timeout. */
10626 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10627 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010628 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010629 if (lnum_stop < 0)
10630 goto theend;
10631#ifdef FEAT_RELTIME
10632 if (argvars[3].v_type != VAR_UNKNOWN)
10633 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010634 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010635 if (time_limit < 0)
10636 goto theend;
10637 }
10638#endif
10639 }
10640
10641#ifdef FEAT_RELTIME
10642 /* Set the time limit, if there is one. */
10643 profile_setlimit(time_limit, &tm);
10644#endif
10645
10646 /*
10647 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10648 * Check to make sure only those flags are set.
10649 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10650 * flags cannot be set. Check for that condition also.
10651 */
10652 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10653 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10654 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010655 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010656 goto theend;
10657 }
10658
10659 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010660 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010661 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010662 if (subpatnum != FAIL)
10663 {
10664 if (flags & SP_SUBPAT)
10665 retval = subpatnum;
10666 else
10667 retval = pos.lnum;
10668 if (flags & SP_SETPCMARK)
10669 setpcmark();
10670 curwin->w_cursor = pos;
10671 if (match_pos != NULL)
10672 {
10673 /* Store the match cursor position */
10674 match_pos->lnum = pos.lnum;
10675 match_pos->col = pos.col + 1;
10676 }
10677 /* "/$" will put the cursor after the end of the line, may need to
10678 * correct that here */
10679 check_cursor();
10680 }
10681
10682 /* If 'n' flag is used: restore cursor position. */
10683 if (flags & SP_NOMOVE)
10684 curwin->w_cursor = save_cursor;
10685 else
10686 curwin->w_set_curswant = TRUE;
10687theend:
10688 p_ws = save_p_ws;
10689
10690 return retval;
10691}
10692
10693#ifdef FEAT_FLOAT
10694
10695/*
10696 * round() is not in C90, use ceil() or floor() instead.
10697 */
10698 float_T
10699vim_round(float_T f)
10700{
10701 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10702}
10703
10704/*
10705 * "round({float})" function
10706 */
10707 static void
10708f_round(typval_T *argvars, typval_T *rettv)
10709{
10710 float_T f = 0.0;
10711
10712 rettv->v_type = VAR_FLOAT;
10713 if (get_float_arg(argvars, &f) == OK)
10714 rettv->vval.v_float = vim_round(f);
10715 else
10716 rettv->vval.v_float = 0.0;
10717}
10718#endif
10719
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010720#ifdef FEAT_RUBY
10721/*
10722 * "rubyeval()" function
10723 */
10724 static void
10725f_rubyeval(typval_T *argvars, typval_T *rettv)
10726{
10727 char_u *str;
10728 char_u buf[NUMBUFLEN];
10729
10730 str = tv_get_string_buf(&argvars[0], buf);
10731 do_rubyeval(str, rettv);
10732}
10733#endif
10734
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010735/*
10736 * "screenattr()" function
10737 */
10738 static void
10739f_screenattr(typval_T *argvars, typval_T *rettv)
10740{
10741 int row;
10742 int col;
10743 int c;
10744
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010745 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10746 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010747 if (row < 0 || row >= screen_Rows
10748 || col < 0 || col >= screen_Columns)
10749 c = -1;
10750 else
10751 c = ScreenAttrs[LineOffset[row] + col];
10752 rettv->vval.v_number = c;
10753}
10754
10755/*
10756 * "screenchar()" function
10757 */
10758 static void
10759f_screenchar(typval_T *argvars, typval_T *rettv)
10760{
10761 int row;
10762 int col;
10763 int off;
10764 int c;
10765
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010766 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10767 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010768 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010769 c = -1;
10770 else
10771 {
10772 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010773 if (enc_utf8 && ScreenLinesUC[off] != 0)
10774 c = ScreenLinesUC[off];
10775 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010776 c = ScreenLines[off];
10777 }
10778 rettv->vval.v_number = c;
10779}
10780
10781/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010782 * "screenchars()" function
10783 */
10784 static void
10785f_screenchars(typval_T *argvars, typval_T *rettv)
10786{
10787 int row;
10788 int col;
10789 int off;
10790 int c;
10791 int i;
10792
10793 if (rettv_list_alloc(rettv) == FAIL)
10794 return;
10795 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10796 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10797 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10798 return;
10799
10800 off = LineOffset[row] + col;
10801 if (enc_utf8 && ScreenLinesUC[off] != 0)
10802 c = ScreenLinesUC[off];
10803 else
10804 c = ScreenLines[off];
10805 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10806
10807 if (enc_utf8)
10808
10809 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10810 list_append_number(rettv->vval.v_list,
10811 (varnumber_T)ScreenLinesC[i][off]);
10812}
10813
10814/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010815 * "screencol()" function
10816 *
10817 * First column is 1 to be consistent with virtcol().
10818 */
10819 static void
10820f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10821{
10822 rettv->vval.v_number = screen_screencol() + 1;
10823}
10824
10825/*
10826 * "screenrow()" function
10827 */
10828 static void
10829f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10830{
10831 rettv->vval.v_number = screen_screenrow() + 1;
10832}
10833
10834/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010835 * "screenstring()" function
10836 */
10837 static void
10838f_screenstring(typval_T *argvars, typval_T *rettv)
10839{
10840 int row;
10841 int col;
10842 int off;
10843 int c;
10844 int i;
10845 char_u buf[MB_MAXBYTES + 1];
10846 int buflen = 0;
10847
10848 rettv->vval.v_string = NULL;
10849 rettv->v_type = VAR_STRING;
10850
10851 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10852 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10853 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10854 return;
10855
10856 off = LineOffset[row] + col;
10857 if (enc_utf8 && ScreenLinesUC[off] != 0)
10858 c = ScreenLinesUC[off];
10859 else
10860 c = ScreenLines[off];
10861 buflen += mb_char2bytes(c, buf);
10862
10863 if (enc_utf8)
10864 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10865 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10866
10867 buf[buflen] = NUL;
10868 rettv->vval.v_string = vim_strsave(buf);
10869}
10870
10871/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010872 * "search()" function
10873 */
10874 static void
10875f_search(typval_T *argvars, typval_T *rettv)
10876{
10877 int flags = 0;
10878
10879 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10880}
10881
10882/*
10883 * "searchdecl()" function
10884 */
10885 static void
10886f_searchdecl(typval_T *argvars, typval_T *rettv)
10887{
10888 int locally = 1;
10889 int thisblock = 0;
10890 int error = FALSE;
10891 char_u *name;
10892
10893 rettv->vval.v_number = 1; /* default: FAIL */
10894
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010895 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010896 if (argvars[1].v_type != VAR_UNKNOWN)
10897 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010898 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010899 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010900 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010901 }
10902 if (!error && name != NULL)
10903 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10904 locally, thisblock, SEARCH_KEEP) == FAIL;
10905}
10906
10907/*
10908 * Used by searchpair() and searchpairpos()
10909 */
10910 static int
10911searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10912{
10913 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010914 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010915 int save_p_ws = p_ws;
10916 int dir;
10917 int flags = 0;
10918 char_u nbuf1[NUMBUFLEN];
10919 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010920 int retval = 0; /* default: FAIL */
10921 long lnum_stop = 0;
10922 long time_limit = 0;
10923
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010924 /* Get the three pattern arguments: start, middle, end. Will result in an
10925 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010926 spat = tv_get_string_chk(&argvars[0]);
10927 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10928 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010929 if (spat == NULL || mpat == NULL || epat == NULL)
10930 goto theend; /* type error */
10931
10932 /* Handle the optional fourth argument: flags */
10933 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10934 if (dir == 0)
10935 goto theend;
10936
10937 /* Don't accept SP_END or SP_SUBPAT.
10938 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10939 */
10940 if ((flags & (SP_END | SP_SUBPAT)) != 0
10941 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10942 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010943 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010944 goto theend;
10945 }
10946
10947 /* Using 'r' implies 'W', otherwise it doesn't work. */
10948 if (flags & SP_REPEAT)
10949 p_ws = FALSE;
10950
10951 /* Optional fifth argument: skip expression */
10952 if (argvars[3].v_type == VAR_UNKNOWN
10953 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010954 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010955 else
10956 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010957 skip = &argvars[4];
10958 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10959 && skip->v_type != VAR_STRING)
10960 {
10961 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010962 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010963 goto theend;
10964 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010965 if (argvars[5].v_type != VAR_UNKNOWN)
10966 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010967 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010968 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010969 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010970 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010971 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010972 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010973#ifdef FEAT_RELTIME
10974 if (argvars[6].v_type != VAR_UNKNOWN)
10975 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010976 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010977 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010978 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010979 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010980 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010981 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010982 }
10983#endif
10984 }
10985 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010986
10987 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10988 match_pos, lnum_stop, time_limit);
10989
10990theend:
10991 p_ws = save_p_ws;
10992
10993 return retval;
10994}
10995
10996/*
10997 * "searchpair()" function
10998 */
10999 static void
11000f_searchpair(typval_T *argvars, typval_T *rettv)
11001{
11002 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11003}
11004
11005/*
11006 * "searchpairpos()" function
11007 */
11008 static void
11009f_searchpairpos(typval_T *argvars, typval_T *rettv)
11010{
11011 pos_T match_pos;
11012 int lnum = 0;
11013 int col = 0;
11014
11015 if (rettv_list_alloc(rettv) == FAIL)
11016 return;
11017
11018 if (searchpair_cmn(argvars, &match_pos) > 0)
11019 {
11020 lnum = match_pos.lnum;
11021 col = match_pos.col;
11022 }
11023
11024 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11025 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11026}
11027
11028/*
11029 * Search for a start/middle/end thing.
11030 * Used by searchpair(), see its documentation for the details.
11031 * Returns 0 or -1 for no match,
11032 */
11033 long
11034do_searchpair(
11035 char_u *spat, /* start pattern */
11036 char_u *mpat, /* middle pattern */
11037 char_u *epat, /* end pattern */
11038 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011039 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011040 int flags, /* SP_SETPCMARK and other SP_ values */
11041 pos_T *match_pos,
11042 linenr_T lnum_stop, /* stop at this line if not zero */
11043 long time_limit UNUSED) /* stop after this many msec */
11044{
11045 char_u *save_cpo;
11046 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11047 long retval = 0;
11048 pos_T pos;
11049 pos_T firstpos;
11050 pos_T foundpos;
11051 pos_T save_cursor;
11052 pos_T save_pos;
11053 int n;
11054 int r;
11055 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011056 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011057 int err;
11058 int options = SEARCH_KEEP;
11059 proftime_T tm;
11060
11061 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11062 save_cpo = p_cpo;
11063 p_cpo = empty_option;
11064
11065#ifdef FEAT_RELTIME
11066 /* Set the time limit, if there is one. */
11067 profile_setlimit(time_limit, &tm);
11068#endif
11069
11070 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11071 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011072 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
11073 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011074 if (pat2 == NULL || pat3 == NULL)
11075 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011076 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011077 if (*mpat == NUL)
11078 STRCPY(pat3, pat2);
11079 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011080 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011081 spat, epat, mpat);
11082 if (flags & SP_START)
11083 options |= SEARCH_START;
11084
Bram Moolenaar48570482017-10-30 21:48:41 +010011085 if (skip != NULL)
11086 {
11087 /* Empty string means to not use the skip expression. */
11088 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11089 use_skip = skip->vval.v_string != NULL
11090 && *skip->vval.v_string != NUL;
11091 }
11092
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011093 save_cursor = curwin->w_cursor;
11094 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011095 CLEAR_POS(&firstpos);
11096 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011097 pat = pat3;
11098 for (;;)
11099 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011100 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011101 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011102 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011103 /* didn't find it or found the first match again: FAIL */
11104 break;
11105
11106 if (firstpos.lnum == 0)
11107 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011108 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011109 {
11110 /* Found the same position again. Can happen with a pattern that
11111 * has "\zs" at the end and searching backwards. Advance one
11112 * character and try again. */
11113 if (dir == BACKWARD)
11114 decl(&pos);
11115 else
11116 incl(&pos);
11117 }
11118 foundpos = pos;
11119
11120 /* clear the start flag to avoid getting stuck here */
11121 options &= ~SEARCH_START;
11122
11123 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011124 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011125 {
11126 save_pos = curwin->w_cursor;
11127 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011128 err = FALSE;
11129 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011130 curwin->w_cursor = save_pos;
11131 if (err)
11132 {
11133 /* Evaluating {skip} caused an error, break here. */
11134 curwin->w_cursor = save_cursor;
11135 retval = -1;
11136 break;
11137 }
11138 if (r)
11139 continue;
11140 }
11141
11142 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11143 {
11144 /* Found end when searching backwards or start when searching
11145 * forward: nested pair. */
11146 ++nest;
11147 pat = pat2; /* nested, don't search for middle */
11148 }
11149 else
11150 {
11151 /* Found end when searching forward or start when searching
11152 * backward: end of (nested) pair; or found middle in outer pair. */
11153 if (--nest == 1)
11154 pat = pat3; /* outer level, search for middle */
11155 }
11156
11157 if (nest == 0)
11158 {
11159 /* Found the match: return matchcount or line number. */
11160 if (flags & SP_RETCOUNT)
11161 ++retval;
11162 else
11163 retval = pos.lnum;
11164 if (flags & SP_SETPCMARK)
11165 setpcmark();
11166 curwin->w_cursor = pos;
11167 if (!(flags & SP_REPEAT))
11168 break;
11169 nest = 1; /* search for next unmatched */
11170 }
11171 }
11172
11173 if (match_pos != NULL)
11174 {
11175 /* Store the match cursor position */
11176 match_pos->lnum = curwin->w_cursor.lnum;
11177 match_pos->col = curwin->w_cursor.col + 1;
11178 }
11179
11180 /* If 'n' flag is used or search failed: restore cursor position. */
11181 if ((flags & SP_NOMOVE) || retval == 0)
11182 curwin->w_cursor = save_cursor;
11183
11184theend:
11185 vim_free(pat2);
11186 vim_free(pat3);
11187 if (p_cpo == empty_option)
11188 p_cpo = save_cpo;
11189 else
11190 /* Darn, evaluating the {skip} expression changed the value. */
11191 free_string_option(save_cpo);
11192
11193 return retval;
11194}
11195
11196/*
11197 * "searchpos()" function
11198 */
11199 static void
11200f_searchpos(typval_T *argvars, typval_T *rettv)
11201{
11202 pos_T match_pos;
11203 int lnum = 0;
11204 int col = 0;
11205 int n;
11206 int flags = 0;
11207
11208 if (rettv_list_alloc(rettv) == FAIL)
11209 return;
11210
11211 n = search_cmn(argvars, &match_pos, &flags);
11212 if (n > 0)
11213 {
11214 lnum = match_pos.lnum;
11215 col = match_pos.col;
11216 }
11217
11218 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11219 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11220 if (flags & SP_SUBPAT)
11221 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11222}
11223
11224 static void
11225f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11226{
11227#ifdef FEAT_CLIENTSERVER
11228 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011229 char_u *server = tv_get_string_chk(&argvars[0]);
11230 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011231
11232 rettv->vval.v_number = -1;
11233 if (server == NULL || reply == NULL)
11234 return;
11235 if (check_restricted() || check_secure())
11236 return;
11237# ifdef FEAT_X11
11238 if (check_connection() == FAIL)
11239 return;
11240# endif
11241
11242 if (serverSendReply(server, reply) < 0)
11243 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011244 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011245 return;
11246 }
11247 rettv->vval.v_number = 0;
11248#else
11249 rettv->vval.v_number = -1;
11250#endif
11251}
11252
11253 static void
11254f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11255{
11256 char_u *r = NULL;
11257
11258#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011259# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011260 r = serverGetVimNames();
11261# else
11262 make_connection();
11263 if (X_DISPLAY != NULL)
11264 r = serverGetVimNames(X_DISPLAY);
11265# endif
11266#endif
11267 rettv->v_type = VAR_STRING;
11268 rettv->vval.v_string = r;
11269}
11270
11271/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011272 * "setbufline()" function
11273 */
11274 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011275f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011276{
11277 linenr_T lnum;
11278 buf_T *buf;
11279
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011280 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011281 if (buf == NULL)
11282 rettv->vval.v_number = 1; /* FAIL */
11283 else
11284 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011285 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011286 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011287 }
11288}
11289
11290/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011291 * "setbufvar()" function
11292 */
11293 static void
11294f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11295{
11296 buf_T *buf;
11297 char_u *varname, *bufvarname;
11298 typval_T *varp;
11299 char_u nbuf[NUMBUFLEN];
11300
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011301 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011302 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011303 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11304 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011305 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011306 varp = &argvars[2];
11307
11308 if (buf != NULL && varname != NULL && varp != NULL)
11309 {
11310 if (*varname == '&')
11311 {
11312 long numval;
11313 char_u *strval;
11314 int error = FALSE;
11315 aco_save_T aco;
11316
11317 /* set curbuf to be our buf, temporarily */
11318 aucmd_prepbuf(&aco, buf);
11319
11320 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011321 numval = (long)tv_get_number_chk(varp, &error);
11322 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011323 if (!error && strval != NULL)
11324 set_option_value(varname, numval, strval, OPT_LOCAL);
11325
11326 /* reset notion of buffer */
11327 aucmd_restbuf(&aco);
11328 }
11329 else
11330 {
11331 buf_T *save_curbuf = curbuf;
11332
11333 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11334 if (bufvarname != NULL)
11335 {
11336 curbuf = buf;
11337 STRCPY(bufvarname, "b:");
11338 STRCPY(bufvarname + 2, varname);
11339 set_var(bufvarname, varp, TRUE);
11340 vim_free(bufvarname);
11341 curbuf = save_curbuf;
11342 }
11343 }
11344 }
11345}
11346
11347 static void
11348f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11349{
11350 dict_T *d;
11351 dictitem_T *di;
11352 char_u *csearch;
11353
11354 if (argvars[0].v_type != VAR_DICT)
11355 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011356 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011357 return;
11358 }
11359
11360 if ((d = argvars[0].vval.v_dict) != NULL)
11361 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011362 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011363 if (csearch != NULL)
11364 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011365 if (enc_utf8)
11366 {
11367 int pcc[MAX_MCO];
11368 int c = utfc_ptr2char(csearch, pcc);
11369
11370 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11371 }
11372 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011373 set_last_csearch(PTR2CHAR(csearch),
11374 csearch, MB_PTR2LEN(csearch));
11375 }
11376
11377 di = dict_find(d, (char_u *)"forward", -1);
11378 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011379 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011380 ? FORWARD : BACKWARD);
11381
11382 di = dict_find(d, (char_u *)"until", -1);
11383 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011384 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011385 }
11386}
11387
11388/*
11389 * "setcmdpos()" function
11390 */
11391 static void
11392f_setcmdpos(typval_T *argvars, typval_T *rettv)
11393{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011394 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011395
11396 if (pos >= 0)
11397 rettv->vval.v_number = set_cmdline_pos(pos);
11398}
11399
11400/*
11401 * "setfperm({fname}, {mode})" function
11402 */
11403 static void
11404f_setfperm(typval_T *argvars, typval_T *rettv)
11405{
11406 char_u *fname;
11407 char_u modebuf[NUMBUFLEN];
11408 char_u *mode_str;
11409 int i;
11410 int mask;
11411 int mode = 0;
11412
11413 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011414 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011415 if (fname == NULL)
11416 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011417 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011418 if (mode_str == NULL)
11419 return;
11420 if (STRLEN(mode_str) != 9)
11421 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011422 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011423 return;
11424 }
11425
11426 mask = 1;
11427 for (i = 8; i >= 0; --i)
11428 {
11429 if (mode_str[i] != '-')
11430 mode |= mask;
11431 mask = mask << 1;
11432 }
11433 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11434}
11435
11436/*
11437 * "setline()" function
11438 */
11439 static void
11440f_setline(typval_T *argvars, typval_T *rettv)
11441{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011442 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011443
Bram Moolenaarca851592018-06-06 21:04:07 +020011444 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011445}
11446
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011447/*
11448 * Used by "setqflist()" and "setloclist()" functions
11449 */
11450 static void
11451set_qf_ll_list(
11452 win_T *wp UNUSED,
11453 typval_T *list_arg UNUSED,
11454 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011455 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011456 typval_T *rettv)
11457{
11458#ifdef FEAT_QUICKFIX
11459 static char *e_invact = N_("E927: Invalid action: '%s'");
11460 char_u *act;
11461 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011462 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011463#endif
11464
11465 rettv->vval.v_number = -1;
11466
11467#ifdef FEAT_QUICKFIX
11468 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011469 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011470 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011471 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011472 else
11473 {
11474 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011475 dict_T *d = NULL;
11476 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011477
11478 if (action_arg->v_type == VAR_STRING)
11479 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011480 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011481 if (act == NULL)
11482 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011483 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11484 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011485 action = *act;
11486 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011487 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011488 }
11489 else if (action_arg->v_type == VAR_UNKNOWN)
11490 action = ' ';
11491 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011492 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011493
Bram Moolenaard823fa92016-08-12 16:29:27 +020011494 if (action_arg->v_type != VAR_UNKNOWN
11495 && what_arg->v_type != VAR_UNKNOWN)
11496 {
11497 if (what_arg->v_type == VAR_DICT)
11498 d = what_arg->vval.v_dict;
11499 else
11500 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011501 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011502 valid_dict = FALSE;
11503 }
11504 }
11505
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011506 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011507 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011508 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11509 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011510 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011511 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011512 }
11513#endif
11514}
11515
11516/*
11517 * "setloclist()" function
11518 */
11519 static void
11520f_setloclist(typval_T *argvars, typval_T *rettv)
11521{
11522 win_T *win;
11523
11524 rettv->vval.v_number = -1;
11525
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011526 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011527 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011528 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011529}
11530
11531/*
11532 * "setmatches()" function
11533 */
11534 static void
11535f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11536{
11537#ifdef FEAT_SEARCH_EXTRA
11538 list_T *l;
11539 listitem_T *li;
11540 dict_T *d;
11541 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011542 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011543
11544 rettv->vval.v_number = -1;
11545 if (argvars[0].v_type != VAR_LIST)
11546 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011547 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011548 return;
11549 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011550 if (win == NULL)
11551 return;
11552
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011553 if ((l = argvars[0].vval.v_list) != NULL)
11554 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011555 /* To some extent make sure that we are dealing with a list from
11556 * "getmatches()". */
11557 li = l->lv_first;
11558 while (li != NULL)
11559 {
11560 if (li->li_tv.v_type != VAR_DICT
11561 || (d = li->li_tv.vval.v_dict) == NULL)
11562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011563 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011564 return;
11565 }
11566 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11567 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11568 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11569 && dict_find(d, (char_u *)"priority", -1) != NULL
11570 && dict_find(d, (char_u *)"id", -1) != NULL))
11571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011572 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011573 return;
11574 }
11575 li = li->li_next;
11576 }
11577
Bram Moolenaaraff74912019-03-30 18:11:49 +010011578 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011579 li = l->lv_first;
11580 while (li != NULL)
11581 {
11582 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011583 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011584 dictitem_T *di;
11585 char_u *group;
11586 int priority;
11587 int id;
11588 char_u *conceal;
11589
11590 d = li->li_tv.vval.v_dict;
11591 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11592 {
11593 if (s == NULL)
11594 {
11595 s = list_alloc();
11596 if (s == NULL)
11597 return;
11598 }
11599
11600 /* match from matchaddpos() */
11601 for (i = 1; i < 9; i++)
11602 {
11603 sprintf((char *)buf, (char *)"pos%d", i);
11604 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11605 {
11606 if (di->di_tv.v_type != VAR_LIST)
11607 return;
11608
11609 list_append_tv(s, &di->di_tv);
11610 s->lv_refcount++;
11611 }
11612 else
11613 break;
11614 }
11615 }
11616
Bram Moolenaar8f667172018-12-14 15:38:31 +010011617 group = dict_get_string(d, (char_u *)"group", TRUE);
11618 priority = (int)dict_get_number(d, (char_u *)"priority");
11619 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011620 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011621 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011622 : NULL;
11623 if (i == 0)
11624 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011625 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011626 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011627 priority, id, NULL, conceal);
11628 }
11629 else
11630 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011631 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011632 list_unref(s);
11633 s = NULL;
11634 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011635 vim_free(group);
11636 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011637
11638 li = li->li_next;
11639 }
11640 rettv->vval.v_number = 0;
11641 }
11642#endif
11643}
11644
11645/*
11646 * "setpos()" function
11647 */
11648 static void
11649f_setpos(typval_T *argvars, typval_T *rettv)
11650{
11651 pos_T pos;
11652 int fnum;
11653 char_u *name;
11654 colnr_T curswant = -1;
11655
11656 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011657 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011658 if (name != NULL)
11659 {
11660 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11661 {
11662 if (--pos.col < 0)
11663 pos.col = 0;
11664 if (name[0] == '.' && name[1] == NUL)
11665 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011666 /* set cursor; "fnum" is ignored */
11667 curwin->w_cursor = pos;
11668 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011669 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011670 curwin->w_curswant = curswant - 1;
11671 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011672 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011673 check_cursor();
11674 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011675 }
11676 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11677 {
11678 /* set mark */
11679 if (setmark_pos(name[1], &pos, fnum) == OK)
11680 rettv->vval.v_number = 0;
11681 }
11682 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011683 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011684 }
11685 }
11686}
11687
11688/*
11689 * "setqflist()" function
11690 */
11691 static void
11692f_setqflist(typval_T *argvars, typval_T *rettv)
11693{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011694 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011695}
11696
11697/*
11698 * "setreg()" function
11699 */
11700 static void
11701f_setreg(typval_T *argvars, typval_T *rettv)
11702{
11703 int regname;
11704 char_u *strregname;
11705 char_u *stropt;
11706 char_u *strval;
11707 int append;
11708 char_u yank_type;
11709 long block_len;
11710
11711 block_len = -1;
11712 yank_type = MAUTO;
11713 append = FALSE;
11714
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011715 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011716 rettv->vval.v_number = 1; /* FAIL is default */
11717
11718 if (strregname == NULL)
11719 return; /* type error; errmsg already given */
11720 regname = *strregname;
11721 if (regname == 0 || regname == '@')
11722 regname = '"';
11723
11724 if (argvars[2].v_type != VAR_UNKNOWN)
11725 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011726 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011727 if (stropt == NULL)
11728 return; /* type error */
11729 for (; *stropt != NUL; ++stropt)
11730 switch (*stropt)
11731 {
11732 case 'a': case 'A': /* append */
11733 append = TRUE;
11734 break;
11735 case 'v': case 'c': /* character-wise selection */
11736 yank_type = MCHAR;
11737 break;
11738 case 'V': case 'l': /* line-wise selection */
11739 yank_type = MLINE;
11740 break;
11741 case 'b': case Ctrl_V: /* block-wise selection */
11742 yank_type = MBLOCK;
11743 if (VIM_ISDIGIT(stropt[1]))
11744 {
11745 ++stropt;
11746 block_len = getdigits(&stropt) - 1;
11747 --stropt;
11748 }
11749 break;
11750 }
11751 }
11752
11753 if (argvars[1].v_type == VAR_LIST)
11754 {
11755 char_u **lstval;
11756 char_u **allocval;
11757 char_u buf[NUMBUFLEN];
11758 char_u **curval;
11759 char_u **curallocval;
11760 list_T *ll = argvars[1].vval.v_list;
11761 listitem_T *li;
11762 int len;
11763
11764 /* If the list is NULL handle like an empty list. */
11765 len = ll == NULL ? 0 : ll->lv_len;
11766
11767 /* First half: use for pointers to result lines; second half: use for
11768 * pointers to allocated copies. */
11769 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11770 if (lstval == NULL)
11771 return;
11772 curval = lstval;
11773 allocval = lstval + len + 2;
11774 curallocval = allocval;
11775
11776 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11777 li = li->li_next)
11778 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011779 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011780 if (strval == NULL)
11781 goto free_lstval;
11782 if (strval == buf)
11783 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011784 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011785 * overwrite the string. */
11786 strval = vim_strsave(buf);
11787 if (strval == NULL)
11788 goto free_lstval;
11789 *curallocval++ = strval;
11790 }
11791 *curval++ = strval;
11792 }
11793 *curval++ = NULL;
11794
11795 write_reg_contents_lst(regname, lstval, -1,
11796 append, yank_type, block_len);
11797free_lstval:
11798 while (curallocval > allocval)
11799 vim_free(*--curallocval);
11800 vim_free(lstval);
11801 }
11802 else
11803 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011804 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011805 if (strval == NULL)
11806 return;
11807 write_reg_contents_ex(regname, strval, -1,
11808 append, yank_type, block_len);
11809 }
11810 rettv->vval.v_number = 0;
11811}
11812
11813/*
11814 * "settabvar()" function
11815 */
11816 static void
11817f_settabvar(typval_T *argvars, typval_T *rettv)
11818{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011819 tabpage_T *save_curtab;
11820 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011821 char_u *varname, *tabvarname;
11822 typval_T *varp;
11823
11824 rettv->vval.v_number = 0;
11825
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011826 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011827 return;
11828
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011829 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11830 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011831 varp = &argvars[2];
11832
Bram Moolenaar4033c552017-09-16 20:54:51 +020011833 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011834 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011835 save_curtab = curtab;
11836 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011837
11838 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11839 if (tabvarname != NULL)
11840 {
11841 STRCPY(tabvarname, "t:");
11842 STRCPY(tabvarname + 2, varname);
11843 set_var(tabvarname, varp, TRUE);
11844 vim_free(tabvarname);
11845 }
11846
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011847 /* Restore current tabpage */
11848 if (valid_tabpage(save_curtab))
11849 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011850 }
11851}
11852
11853/*
11854 * "settabwinvar()" function
11855 */
11856 static void
11857f_settabwinvar(typval_T *argvars, typval_T *rettv)
11858{
11859 setwinvar(argvars, rettv, 1);
11860}
11861
11862/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011863 * "settagstack()" function
11864 */
11865 static void
11866f_settagstack(typval_T *argvars, typval_T *rettv)
11867{
11868 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11869 win_T *wp;
11870 dict_T *d;
11871 int action = 'r';
11872
11873 rettv->vval.v_number = -1;
11874
11875 // first argument: window number or id
11876 wp = find_win_by_nr_or_id(&argvars[0]);
11877 if (wp == NULL)
11878 return;
11879
11880 // second argument: dict with items to set in the tag stack
11881 if (argvars[1].v_type != VAR_DICT)
11882 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011883 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011884 return;
11885 }
11886 d = argvars[1].vval.v_dict;
11887 if (d == NULL)
11888 return;
11889
11890 // third argument: action - 'a' for append and 'r' for replace.
11891 // default is to replace the stack.
11892 if (argvars[2].v_type == VAR_UNKNOWN)
11893 action = 'r';
11894 else if (argvars[2].v_type == VAR_STRING)
11895 {
11896 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011897 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011898 if (actstr == NULL)
11899 return;
11900 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11901 action = *actstr;
11902 else
11903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011904 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011905 return;
11906 }
11907 }
11908 else
11909 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011910 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011911 return;
11912 }
11913
11914 if (set_tagstack(wp, d, action) == OK)
11915 rettv->vval.v_number = 0;
11916}
11917
11918/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011919 * "setwinvar()" function
11920 */
11921 static void
11922f_setwinvar(typval_T *argvars, typval_T *rettv)
11923{
11924 setwinvar(argvars, rettv, 0);
11925}
11926
11927#ifdef FEAT_CRYPT
11928/*
11929 * "sha256({string})" function
11930 */
11931 static void
11932f_sha256(typval_T *argvars, typval_T *rettv)
11933{
11934 char_u *p;
11935
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011936 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011937 rettv->vval.v_string = vim_strsave(
11938 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11939 rettv->v_type = VAR_STRING;
11940}
11941#endif /* FEAT_CRYPT */
11942
11943/*
11944 * "shellescape({string})" function
11945 */
11946 static void
11947f_shellescape(typval_T *argvars, typval_T *rettv)
11948{
Bram Moolenaar20615522017-06-05 18:46:26 +020011949 int do_special = non_zero_arg(&argvars[1]);
11950
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011951 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011952 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011953 rettv->v_type = VAR_STRING;
11954}
11955
11956/*
11957 * shiftwidth() function
11958 */
11959 static void
11960f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11961{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011962 rettv->vval.v_number = 0;
11963
11964 if (argvars[0].v_type != VAR_UNKNOWN)
11965 {
11966 long col;
11967
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011968 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011969 if (col < 0)
11970 return; // type error; errmsg already given
11971#ifdef FEAT_VARTABS
11972 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11973 return;
11974#endif
11975 }
11976
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011977 rettv->vval.v_number = get_sw_value(curbuf);
11978}
11979
Bram Moolenaar162b7142018-12-21 15:17:36 +010011980#ifdef FEAT_SIGNS
11981/*
11982 * "sign_define()" function
11983 */
11984 static void
11985f_sign_define(typval_T *argvars, typval_T *rettv)
11986{
11987 char_u *name;
11988 dict_T *dict;
11989 char_u *icon = NULL;
11990 char_u *linehl = NULL;
11991 char_u *text = NULL;
11992 char_u *texthl = NULL;
11993
11994 rettv->vval.v_number = -1;
11995
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011996 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011997 if (name == NULL)
11998 return;
11999
12000 if (argvars[1].v_type != VAR_UNKNOWN)
12001 {
12002 if (argvars[1].v_type != VAR_DICT)
12003 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012004 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012005 return;
12006 }
12007
12008 // sign attributes
12009 dict = argvars[1].vval.v_dict;
12010 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12011 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12012 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12013 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12014 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12015 text = dict_get_string(dict, (char_u *)"text", TRUE);
12016 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12017 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12018 }
12019
12020 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12021 rettv->vval.v_number = 0;
12022
12023 vim_free(icon);
12024 vim_free(linehl);
12025 vim_free(text);
12026 vim_free(texthl);
12027}
12028
12029/*
12030 * "sign_getdefined()" function
12031 */
12032 static void
12033f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12034{
12035 char_u *name = NULL;
12036
12037 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12038 return;
12039
12040 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012041 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012042
12043 sign_getlist(name, rettv->vval.v_list);
12044}
12045
12046/*
12047 * "sign_getplaced()" function
12048 */
12049 static void
12050f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12051{
12052 buf_T *buf = NULL;
12053 dict_T *dict;
12054 dictitem_T *di;
12055 linenr_T lnum = 0;
12056 int sign_id = 0;
12057 char_u *group = NULL;
12058 int notanum = FALSE;
12059
12060 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12061 return;
12062
12063 if (argvars[0].v_type != VAR_UNKNOWN)
12064 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012065 // get signs placed in the specified buffer
12066 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012067 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012068 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012069
12070 if (argvars[1].v_type != VAR_UNKNOWN)
12071 {
12072 if (argvars[1].v_type != VAR_DICT ||
12073 ((dict = argvars[1].vval.v_dict) == NULL))
12074 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012075 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012076 return;
12077 }
12078 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12079 {
12080 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012081 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012082 if (notanum)
12083 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012084 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012085 }
12086 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12087 {
12088 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012089 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012090 if (notanum)
12091 return;
12092 }
12093 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12094 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012095 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012096 if (group == NULL)
12097 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012098 if (*group == '\0') // empty string means global group
12099 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012100 }
12101 }
12102 }
12103
12104 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12105}
12106
12107/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012108 * "sign_jump()" function
12109 */
12110 static void
12111f_sign_jump(typval_T *argvars, typval_T *rettv)
12112{
12113 int sign_id;
12114 char_u *sign_group = NULL;
12115 buf_T *buf;
12116 int notanum = FALSE;
12117
12118 rettv->vval.v_number = -1;
12119
Bram Moolenaarbdace832019-03-02 10:13:42 +010012120 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012121 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12122 if (notanum)
12123 return;
12124 if (sign_id <= 0)
12125 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012126 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012127 return;
12128 }
12129
12130 // Sign group
12131 sign_group = tv_get_string_chk(&argvars[1]);
12132 if (sign_group == NULL)
12133 return;
12134 if (sign_group[0] == '\0')
12135 sign_group = NULL; // global sign group
12136 else
12137 {
12138 sign_group = vim_strsave(sign_group);
12139 if (sign_group == NULL)
12140 return;
12141 }
12142
12143 // Buffer to place the sign
12144 buf = get_buf_arg(&argvars[2]);
12145 if (buf == NULL)
12146 goto cleanup;
12147
12148 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12149
12150cleanup:
12151 vim_free(sign_group);
12152}
12153
12154/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012155 * "sign_place()" function
12156 */
12157 static void
12158f_sign_place(typval_T *argvars, typval_T *rettv)
12159{
12160 int sign_id;
12161 char_u *group = NULL;
12162 char_u *sign_name;
12163 buf_T *buf;
12164 dict_T *dict;
12165 dictitem_T *di;
12166 linenr_T lnum = 0;
12167 int prio = SIGN_DEF_PRIO;
12168 int notanum = FALSE;
12169
12170 rettv->vval.v_number = -1;
12171
Bram Moolenaarbdace832019-03-02 10:13:42 +010012172 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012173 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012174 if (notanum)
12175 return;
12176 if (sign_id < 0)
12177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012178 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012179 return;
12180 }
12181
12182 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012183 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012184 if (group == NULL)
12185 return;
12186 if (group[0] == '\0')
12187 group = NULL; // global sign group
12188 else
12189 {
12190 group = vim_strsave(group);
12191 if (group == NULL)
12192 return;
12193 }
12194
12195 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012196 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012197 if (sign_name == NULL)
12198 goto cleanup;
12199
12200 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012201 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012202 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012203 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012204
12205 if (argvars[4].v_type != VAR_UNKNOWN)
12206 {
12207 if (argvars[4].v_type != VAR_DICT ||
12208 ((dict = argvars[4].vval.v_dict) == NULL))
12209 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012210 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012211 goto cleanup;
12212 }
12213
12214 // Line number where the sign is to be placed
12215 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12216 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012217 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012218 if (notanum)
12219 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012220 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012221 }
12222 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12223 {
12224 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012225 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012226 if (notanum)
12227 goto cleanup;
12228 }
12229 }
12230
12231 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12232 rettv->vval.v_number = sign_id;
12233
12234cleanup:
12235 vim_free(group);
12236}
12237
12238/*
12239 * "sign_undefine()" function
12240 */
12241 static void
12242f_sign_undefine(typval_T *argvars, typval_T *rettv)
12243{
12244 char_u *name;
12245
12246 rettv->vval.v_number = -1;
12247
12248 if (argvars[0].v_type == VAR_UNKNOWN)
12249 {
12250 // Free all the signs
12251 free_signs();
12252 rettv->vval.v_number = 0;
12253 }
12254 else
12255 {
12256 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012257 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012258 if (name == NULL)
12259 return;
12260
12261 if (sign_undefine_by_name(name) == OK)
12262 rettv->vval.v_number = 0;
12263 }
12264}
12265
12266/*
12267 * "sign_unplace()" function
12268 */
12269 static void
12270f_sign_unplace(typval_T *argvars, typval_T *rettv)
12271{
12272 dict_T *dict;
12273 dictitem_T *di;
12274 int sign_id = 0;
12275 buf_T *buf = NULL;
12276 char_u *group = NULL;
12277
12278 rettv->vval.v_number = -1;
12279
12280 if (argvars[0].v_type != VAR_STRING)
12281 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012282 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012283 return;
12284 }
12285
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012286 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012287 if (group[0] == '\0')
12288 group = NULL; // global sign group
12289 else
12290 {
12291 group = vim_strsave(group);
12292 if (group == NULL)
12293 return;
12294 }
12295
12296 if (argvars[1].v_type != VAR_UNKNOWN)
12297 {
12298 if (argvars[1].v_type != VAR_DICT)
12299 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012300 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012301 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012302 }
12303 dict = argvars[1].vval.v_dict;
12304
12305 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12306 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012307 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012308 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012309 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012310 }
12311 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12312 sign_id = dict_get_number(dict, (char_u *)"id");
12313 }
12314
12315 if (buf == NULL)
12316 {
12317 // Delete the sign in all the buffers
12318 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012319 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012320 rettv->vval.v_number = 0;
12321 }
12322 else
12323 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012324 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012325 rettv->vval.v_number = 0;
12326 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012327
12328cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012329 vim_free(group);
12330}
12331#endif
12332
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012333/*
12334 * "simplify()" function
12335 */
12336 static void
12337f_simplify(typval_T *argvars, typval_T *rettv)
12338{
12339 char_u *p;
12340
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012341 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012342 rettv->vval.v_string = vim_strsave(p);
12343 simplify_filename(rettv->vval.v_string); /* simplify in place */
12344 rettv->v_type = VAR_STRING;
12345}
12346
12347#ifdef FEAT_FLOAT
12348/*
12349 * "sin()" function
12350 */
12351 static void
12352f_sin(typval_T *argvars, typval_T *rettv)
12353{
12354 float_T f = 0.0;
12355
12356 rettv->v_type = VAR_FLOAT;
12357 if (get_float_arg(argvars, &f) == OK)
12358 rettv->vval.v_float = sin(f);
12359 else
12360 rettv->vval.v_float = 0.0;
12361}
12362
12363/*
12364 * "sinh()" function
12365 */
12366 static void
12367f_sinh(typval_T *argvars, typval_T *rettv)
12368{
12369 float_T f = 0.0;
12370
12371 rettv->v_type = VAR_FLOAT;
12372 if (get_float_arg(argvars, &f) == OK)
12373 rettv->vval.v_float = sinh(f);
12374 else
12375 rettv->vval.v_float = 0.0;
12376}
12377#endif
12378
12379static int
12380#ifdef __BORLANDC__
12381 _RTLENTRYF
12382#endif
12383 item_compare(const void *s1, const void *s2);
12384static int
12385#ifdef __BORLANDC__
12386 _RTLENTRYF
12387#endif
12388 item_compare2(const void *s1, const void *s2);
12389
12390/* struct used in the array that's given to qsort() */
12391typedef struct
12392{
12393 listitem_T *item;
12394 int idx;
12395} sortItem_T;
12396
12397/* struct storing information about current sort */
12398typedef struct
12399{
12400 int item_compare_ic;
12401 int item_compare_numeric;
12402 int item_compare_numbers;
12403#ifdef FEAT_FLOAT
12404 int item_compare_float;
12405#endif
12406 char_u *item_compare_func;
12407 partial_T *item_compare_partial;
12408 dict_T *item_compare_selfdict;
12409 int item_compare_func_err;
12410 int item_compare_keep_zero;
12411} sortinfo_T;
12412static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012413#define ITEM_COMPARE_FAIL 999
12414
12415/*
12416 * Compare functions for f_sort() and f_uniq() below.
12417 */
12418 static int
12419#ifdef __BORLANDC__
12420_RTLENTRYF
12421#endif
12422item_compare(const void *s1, const void *s2)
12423{
12424 sortItem_T *si1, *si2;
12425 typval_T *tv1, *tv2;
12426 char_u *p1, *p2;
12427 char_u *tofree1 = NULL, *tofree2 = NULL;
12428 int res;
12429 char_u numbuf1[NUMBUFLEN];
12430 char_u numbuf2[NUMBUFLEN];
12431
12432 si1 = (sortItem_T *)s1;
12433 si2 = (sortItem_T *)s2;
12434 tv1 = &si1->item->li_tv;
12435 tv2 = &si2->item->li_tv;
12436
12437 if (sortinfo->item_compare_numbers)
12438 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012439 varnumber_T v1 = tv_get_number(tv1);
12440 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012441
12442 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12443 }
12444
12445#ifdef FEAT_FLOAT
12446 if (sortinfo->item_compare_float)
12447 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012448 float_T v1 = tv_get_float(tv1);
12449 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450
12451 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12452 }
12453#endif
12454
12455 /* tv2string() puts quotes around a string and allocates memory. Don't do
12456 * that for string variables. Use a single quote when comparing with a
12457 * non-string to do what the docs promise. */
12458 if (tv1->v_type == VAR_STRING)
12459 {
12460 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12461 p1 = (char_u *)"'";
12462 else
12463 p1 = tv1->vval.v_string;
12464 }
12465 else
12466 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12467 if (tv2->v_type == VAR_STRING)
12468 {
12469 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12470 p2 = (char_u *)"'";
12471 else
12472 p2 = tv2->vval.v_string;
12473 }
12474 else
12475 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12476 if (p1 == NULL)
12477 p1 = (char_u *)"";
12478 if (p2 == NULL)
12479 p2 = (char_u *)"";
12480 if (!sortinfo->item_compare_numeric)
12481 {
12482 if (sortinfo->item_compare_ic)
12483 res = STRICMP(p1, p2);
12484 else
12485 res = STRCMP(p1, p2);
12486 }
12487 else
12488 {
12489 double n1, n2;
12490 n1 = strtod((char *)p1, (char **)&p1);
12491 n2 = strtod((char *)p2, (char **)&p2);
12492 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12493 }
12494
12495 /* When the result would be zero, compare the item indexes. Makes the
12496 * sort stable. */
12497 if (res == 0 && !sortinfo->item_compare_keep_zero)
12498 res = si1->idx > si2->idx ? 1 : -1;
12499
12500 vim_free(tofree1);
12501 vim_free(tofree2);
12502 return res;
12503}
12504
12505 static int
12506#ifdef __BORLANDC__
12507_RTLENTRYF
12508#endif
12509item_compare2(const void *s1, const void *s2)
12510{
12511 sortItem_T *si1, *si2;
12512 int res;
12513 typval_T rettv;
12514 typval_T argv[3];
12515 int dummy;
12516 char_u *func_name;
12517 partial_T *partial = sortinfo->item_compare_partial;
12518
12519 /* shortcut after failure in previous call; compare all items equal */
12520 if (sortinfo->item_compare_func_err)
12521 return 0;
12522
12523 si1 = (sortItem_T *)s1;
12524 si2 = (sortItem_T *)s2;
12525
12526 if (partial == NULL)
12527 func_name = sortinfo->item_compare_func;
12528 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012529 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012530
12531 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12532 * in the copy without changing the original list items. */
12533 copy_tv(&si1->item->li_tv, &argv[0]);
12534 copy_tv(&si2->item->li_tv, &argv[1]);
12535
12536 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12537 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012538 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012539 partial, sortinfo->item_compare_selfdict);
12540 clear_tv(&argv[0]);
12541 clear_tv(&argv[1]);
12542
12543 if (res == FAIL)
12544 res = ITEM_COMPARE_FAIL;
12545 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012546 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012547 if (sortinfo->item_compare_func_err)
12548 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12549 clear_tv(&rettv);
12550
12551 /* When the result would be zero, compare the pointers themselves. Makes
12552 * the sort stable. */
12553 if (res == 0 && !sortinfo->item_compare_keep_zero)
12554 res = si1->idx > si2->idx ? 1 : -1;
12555
12556 return res;
12557}
12558
12559/*
12560 * "sort({list})" function
12561 */
12562 static void
12563do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12564{
12565 list_T *l;
12566 listitem_T *li;
12567 sortItem_T *ptrs;
12568 sortinfo_T *old_sortinfo;
12569 sortinfo_T info;
12570 long len;
12571 long i;
12572
12573 /* Pointer to current info struct used in compare function. Save and
12574 * restore the current one for nested calls. */
12575 old_sortinfo = sortinfo;
12576 sortinfo = &info;
12577
12578 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012579 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012580 else
12581 {
12582 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012583 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012584 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12585 TRUE))
12586 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012587 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012588
12589 len = list_len(l);
12590 if (len <= 1)
12591 goto theend; /* short list sorts pretty quickly */
12592
12593 info.item_compare_ic = FALSE;
12594 info.item_compare_numeric = FALSE;
12595 info.item_compare_numbers = FALSE;
12596#ifdef FEAT_FLOAT
12597 info.item_compare_float = FALSE;
12598#endif
12599 info.item_compare_func = NULL;
12600 info.item_compare_partial = NULL;
12601 info.item_compare_selfdict = NULL;
12602 if (argvars[1].v_type != VAR_UNKNOWN)
12603 {
12604 /* optional second argument: {func} */
12605 if (argvars[1].v_type == VAR_FUNC)
12606 info.item_compare_func = argvars[1].vval.v_string;
12607 else if (argvars[1].v_type == VAR_PARTIAL)
12608 info.item_compare_partial = argvars[1].vval.v_partial;
12609 else
12610 {
12611 int error = FALSE;
12612
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012613 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012614 if (error)
12615 goto theend; /* type error; errmsg already given */
12616 if (i == 1)
12617 info.item_compare_ic = TRUE;
12618 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012619 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012620 else if (i != 0)
12621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012622 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012623 goto theend;
12624 }
12625 if (info.item_compare_func != NULL)
12626 {
12627 if (*info.item_compare_func == NUL)
12628 {
12629 /* empty string means default sort */
12630 info.item_compare_func = NULL;
12631 }
12632 else if (STRCMP(info.item_compare_func, "n") == 0)
12633 {
12634 info.item_compare_func = NULL;
12635 info.item_compare_numeric = TRUE;
12636 }
12637 else if (STRCMP(info.item_compare_func, "N") == 0)
12638 {
12639 info.item_compare_func = NULL;
12640 info.item_compare_numbers = TRUE;
12641 }
12642#ifdef FEAT_FLOAT
12643 else if (STRCMP(info.item_compare_func, "f") == 0)
12644 {
12645 info.item_compare_func = NULL;
12646 info.item_compare_float = TRUE;
12647 }
12648#endif
12649 else if (STRCMP(info.item_compare_func, "i") == 0)
12650 {
12651 info.item_compare_func = NULL;
12652 info.item_compare_ic = TRUE;
12653 }
12654 }
12655 }
12656
12657 if (argvars[2].v_type != VAR_UNKNOWN)
12658 {
12659 /* optional third argument: {dict} */
12660 if (argvars[2].v_type != VAR_DICT)
12661 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012662 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012663 goto theend;
12664 }
12665 info.item_compare_selfdict = argvars[2].vval.v_dict;
12666 }
12667 }
12668
12669 /* Make an array with each entry pointing to an item in the List. */
12670 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12671 if (ptrs == NULL)
12672 goto theend;
12673
12674 i = 0;
12675 if (sort)
12676 {
12677 /* sort(): ptrs will be the list to sort */
12678 for (li = l->lv_first; li != NULL; li = li->li_next)
12679 {
12680 ptrs[i].item = li;
12681 ptrs[i].idx = i;
12682 ++i;
12683 }
12684
12685 info.item_compare_func_err = FALSE;
12686 info.item_compare_keep_zero = FALSE;
12687 /* test the compare function */
12688 if ((info.item_compare_func != NULL
12689 || info.item_compare_partial != NULL)
12690 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12691 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012692 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012693 else
12694 {
12695 /* Sort the array with item pointers. */
12696 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12697 info.item_compare_func == NULL
12698 && info.item_compare_partial == NULL
12699 ? item_compare : item_compare2);
12700
12701 if (!info.item_compare_func_err)
12702 {
12703 /* Clear the List and append the items in sorted order. */
12704 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12705 l->lv_len = 0;
12706 for (i = 0; i < len; ++i)
12707 list_append(l, ptrs[i].item);
12708 }
12709 }
12710 }
12711 else
12712 {
12713 int (*item_compare_func_ptr)(const void *, const void *);
12714
12715 /* f_uniq(): ptrs will be a stack of items to remove */
12716 info.item_compare_func_err = FALSE;
12717 info.item_compare_keep_zero = TRUE;
12718 item_compare_func_ptr = info.item_compare_func != NULL
12719 || info.item_compare_partial != NULL
12720 ? item_compare2 : item_compare;
12721
12722 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12723 li = li->li_next)
12724 {
12725 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12726 == 0)
12727 ptrs[i++].item = li;
12728 if (info.item_compare_func_err)
12729 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012730 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012731 break;
12732 }
12733 }
12734
12735 if (!info.item_compare_func_err)
12736 {
12737 while (--i >= 0)
12738 {
12739 li = ptrs[i].item->li_next;
12740 ptrs[i].item->li_next = li->li_next;
12741 if (li->li_next != NULL)
12742 li->li_next->li_prev = ptrs[i].item;
12743 else
12744 l->lv_last = ptrs[i].item;
12745 list_fix_watch(l, li);
12746 listitem_free(li);
12747 l->lv_len--;
12748 }
12749 }
12750 }
12751
12752 vim_free(ptrs);
12753 }
12754theend:
12755 sortinfo = old_sortinfo;
12756}
12757
12758/*
12759 * "sort({list})" function
12760 */
12761 static void
12762f_sort(typval_T *argvars, typval_T *rettv)
12763{
12764 do_sort_uniq(argvars, rettv, TRUE);
12765}
12766
12767/*
12768 * "uniq({list})" function
12769 */
12770 static void
12771f_uniq(typval_T *argvars, typval_T *rettv)
12772{
12773 do_sort_uniq(argvars, rettv, FALSE);
12774}
12775
12776/*
12777 * "soundfold({word})" function
12778 */
12779 static void
12780f_soundfold(typval_T *argvars, typval_T *rettv)
12781{
12782 char_u *s;
12783
12784 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012785 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012786#ifdef FEAT_SPELL
12787 rettv->vval.v_string = eval_soundfold(s);
12788#else
12789 rettv->vval.v_string = vim_strsave(s);
12790#endif
12791}
12792
12793/*
12794 * "spellbadword()" function
12795 */
12796 static void
12797f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12798{
12799 char_u *word = (char_u *)"";
12800 hlf_T attr = HLF_COUNT;
12801 int len = 0;
12802
12803 if (rettv_list_alloc(rettv) == FAIL)
12804 return;
12805
12806#ifdef FEAT_SPELL
12807 if (argvars[0].v_type == VAR_UNKNOWN)
12808 {
12809 /* Find the start and length of the badly spelled word. */
12810 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12811 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012812 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012813 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012814 curwin->w_set_curswant = TRUE;
12815 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012816 }
12817 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12818 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012819 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012820 int capcol = -1;
12821
12822 if (str != NULL)
12823 {
12824 /* Check the argument for spelling. */
12825 while (*str != NUL)
12826 {
12827 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12828 if (attr != HLF_COUNT)
12829 {
12830 word = str;
12831 break;
12832 }
12833 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012834 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012835 }
12836 }
12837 }
12838#endif
12839
12840 list_append_string(rettv->vval.v_list, word, len);
12841 list_append_string(rettv->vval.v_list, (char_u *)(
12842 attr == HLF_SPB ? "bad" :
12843 attr == HLF_SPR ? "rare" :
12844 attr == HLF_SPL ? "local" :
12845 attr == HLF_SPC ? "caps" :
12846 ""), -1);
12847}
12848
12849/*
12850 * "spellsuggest()" function
12851 */
12852 static void
12853f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12854{
12855#ifdef FEAT_SPELL
12856 char_u *str;
12857 int typeerr = FALSE;
12858 int maxcount;
12859 garray_T ga;
12860 int i;
12861 listitem_T *li;
12862 int need_capital = FALSE;
12863#endif
12864
12865 if (rettv_list_alloc(rettv) == FAIL)
12866 return;
12867
12868#ifdef FEAT_SPELL
12869 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12870 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012871 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012872 if (argvars[1].v_type != VAR_UNKNOWN)
12873 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012874 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012875 if (maxcount <= 0)
12876 return;
12877 if (argvars[2].v_type != VAR_UNKNOWN)
12878 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012879 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012880 if (typeerr)
12881 return;
12882 }
12883 }
12884 else
12885 maxcount = 25;
12886
12887 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12888
12889 for (i = 0; i < ga.ga_len; ++i)
12890 {
12891 str = ((char_u **)ga.ga_data)[i];
12892
12893 li = listitem_alloc();
12894 if (li == NULL)
12895 vim_free(str);
12896 else
12897 {
12898 li->li_tv.v_type = VAR_STRING;
12899 li->li_tv.v_lock = 0;
12900 li->li_tv.vval.v_string = str;
12901 list_append(rettv->vval.v_list, li);
12902 }
12903 }
12904 ga_clear(&ga);
12905 }
12906#endif
12907}
12908
12909 static void
12910f_split(typval_T *argvars, typval_T *rettv)
12911{
12912 char_u *str;
12913 char_u *end;
12914 char_u *pat = NULL;
12915 regmatch_T regmatch;
12916 char_u patbuf[NUMBUFLEN];
12917 char_u *save_cpo;
12918 int match;
12919 colnr_T col = 0;
12920 int keepempty = FALSE;
12921 int typeerr = FALSE;
12922
12923 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12924 save_cpo = p_cpo;
12925 p_cpo = (char_u *)"";
12926
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012927 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012928 if (argvars[1].v_type != VAR_UNKNOWN)
12929 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012930 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012931 if (pat == NULL)
12932 typeerr = TRUE;
12933 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012934 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012935 }
12936 if (pat == NULL || *pat == NUL)
12937 pat = (char_u *)"[\\x01- ]\\+";
12938
12939 if (rettv_list_alloc(rettv) == FAIL)
12940 return;
12941 if (typeerr)
12942 return;
12943
12944 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12945 if (regmatch.regprog != NULL)
12946 {
12947 regmatch.rm_ic = FALSE;
12948 while (*str != NUL || keepempty)
12949 {
12950 if (*str == NUL)
12951 match = FALSE; /* empty item at the end */
12952 else
12953 match = vim_regexec_nl(&regmatch, str, col);
12954 if (match)
12955 end = regmatch.startp[0];
12956 else
12957 end = str + STRLEN(str);
12958 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12959 && *str != NUL && match && end < regmatch.endp[0]))
12960 {
12961 if (list_append_string(rettv->vval.v_list, str,
12962 (int)(end - str)) == FAIL)
12963 break;
12964 }
12965 if (!match)
12966 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012967 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012968 if (regmatch.endp[0] > str)
12969 col = 0;
12970 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012971 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012972 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012973 str = regmatch.endp[0];
12974 }
12975
12976 vim_regfree(regmatch.regprog);
12977 }
12978
12979 p_cpo = save_cpo;
12980}
12981
12982#ifdef FEAT_FLOAT
12983/*
12984 * "sqrt()" function
12985 */
12986 static void
12987f_sqrt(typval_T *argvars, typval_T *rettv)
12988{
12989 float_T f = 0.0;
12990
12991 rettv->v_type = VAR_FLOAT;
12992 if (get_float_arg(argvars, &f) == OK)
12993 rettv->vval.v_float = sqrt(f);
12994 else
12995 rettv->vval.v_float = 0.0;
12996}
12997
12998/*
12999 * "str2float()" function
13000 */
13001 static void
13002f_str2float(typval_T *argvars, typval_T *rettv)
13003{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013004 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013005 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013006
Bram Moolenaar08243d22017-01-10 16:12:29 +010013007 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013008 p = skipwhite(p + 1);
13009 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013010 if (isneg)
13011 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013012 rettv->v_type = VAR_FLOAT;
13013}
13014#endif
13015
13016/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013017 * "str2list()" function
13018 */
13019 static void
13020f_str2list(typval_T *argvars, typval_T *rettv)
13021{
13022 char_u *p;
13023 int utf8 = FALSE;
13024
13025 if (rettv_list_alloc(rettv) == FAIL)
13026 return;
13027
13028 if (argvars[1].v_type != VAR_UNKNOWN)
13029 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13030
13031 p = tv_get_string(&argvars[0]);
13032
13033 if (has_mbyte || utf8)
13034 {
13035 int (*ptr2len)(char_u *);
13036 int (*ptr2char)(char_u *);
13037
13038 if (utf8 || enc_utf8)
13039 {
13040 ptr2len = utf_ptr2len;
13041 ptr2char = utf_ptr2char;
13042 }
13043 else
13044 {
13045 ptr2len = mb_ptr2len;
13046 ptr2char = mb_ptr2char;
13047 }
13048
13049 for ( ; *p != NUL; p += (*ptr2len)(p))
13050 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13051 }
13052 else
13053 for ( ; *p != NUL; ++p)
13054 list_append_number(rettv->vval.v_list, *p);
13055}
13056
13057/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013058 * "str2nr()" function
13059 */
13060 static void
13061f_str2nr(typval_T *argvars, typval_T *rettv)
13062{
13063 int base = 10;
13064 char_u *p;
13065 varnumber_T n;
13066 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013067 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013068
13069 if (argvars[1].v_type != VAR_UNKNOWN)
13070 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013071 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013072 if (base != 2 && base != 8 && base != 10 && base != 16)
13073 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013074 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013075 return;
13076 }
13077 }
13078
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013079 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013080 isneg = (*p == '-');
13081 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013082 p = skipwhite(p + 1);
13083 switch (base)
13084 {
13085 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13086 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13087 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13088 default: what = 0;
13089 }
13090 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013091 if (isneg)
13092 rettv->vval.v_number = -n;
13093 else
13094 rettv->vval.v_number = n;
13095
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013096}
13097
13098#ifdef HAVE_STRFTIME
13099/*
13100 * "strftime({format}[, {time}])" function
13101 */
13102 static void
13103f_strftime(typval_T *argvars, typval_T *rettv)
13104{
13105 char_u result_buf[256];
13106 struct tm *curtime;
13107 time_t seconds;
13108 char_u *p;
13109
13110 rettv->v_type = VAR_STRING;
13111
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013112 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013113 if (argvars[1].v_type == VAR_UNKNOWN)
13114 seconds = time(NULL);
13115 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013116 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013117 curtime = localtime(&seconds);
13118 /* MSVC returns NULL for an invalid value of seconds. */
13119 if (curtime == NULL)
13120 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13121 else
13122 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013123 vimconv_T conv;
13124 char_u *enc;
13125
13126 conv.vc_type = CONV_NONE;
13127 enc = enc_locale();
13128 convert_setup(&conv, p_enc, enc);
13129 if (conv.vc_type != CONV_NONE)
13130 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013131 if (p != NULL)
13132 (void)strftime((char *)result_buf, sizeof(result_buf),
13133 (char *)p, curtime);
13134 else
13135 result_buf[0] = NUL;
13136
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013137 if (conv.vc_type != CONV_NONE)
13138 vim_free(p);
13139 convert_setup(&conv, enc, p_enc);
13140 if (conv.vc_type != CONV_NONE)
13141 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13142 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013143 rettv->vval.v_string = vim_strsave(result_buf);
13144
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145 /* Release conversion descriptors */
13146 convert_setup(&conv, NULL, NULL);
13147 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013148 }
13149}
13150#endif
13151
13152/*
13153 * "strgetchar()" function
13154 */
13155 static void
13156f_strgetchar(typval_T *argvars, typval_T *rettv)
13157{
13158 char_u *str;
13159 int len;
13160 int error = FALSE;
13161 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013162 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013163
13164 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013165 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013166 if (str == NULL)
13167 return;
13168 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013169 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013170 if (error)
13171 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013172
Bram Moolenaar13505972019-01-24 15:04:48 +010013173 while (charidx >= 0 && byteidx < len)
13174 {
13175 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013176 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013177 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13178 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013179 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013180 --charidx;
13181 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013182 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013183}
13184
13185/*
13186 * "stridx()" function
13187 */
13188 static void
13189f_stridx(typval_T *argvars, typval_T *rettv)
13190{
13191 char_u buf[NUMBUFLEN];
13192 char_u *needle;
13193 char_u *haystack;
13194 char_u *save_haystack;
13195 char_u *pos;
13196 int start_idx;
13197
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013198 needle = tv_get_string_chk(&argvars[1]);
13199 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013200 rettv->vval.v_number = -1;
13201 if (needle == NULL || haystack == NULL)
13202 return; /* type error; errmsg already given */
13203
13204 if (argvars[2].v_type != VAR_UNKNOWN)
13205 {
13206 int error = FALSE;
13207
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013208 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013209 if (error || start_idx >= (int)STRLEN(haystack))
13210 return;
13211 if (start_idx >= 0)
13212 haystack += start_idx;
13213 }
13214
13215 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13216 if (pos != NULL)
13217 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13218}
13219
13220/*
13221 * "string()" function
13222 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013223 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013224f_string(typval_T *argvars, typval_T *rettv)
13225{
13226 char_u *tofree;
13227 char_u numbuf[NUMBUFLEN];
13228
13229 rettv->v_type = VAR_STRING;
13230 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13231 get_copyID());
13232 /* Make a copy if we have a value but it's not in allocated memory. */
13233 if (rettv->vval.v_string != NULL && tofree == NULL)
13234 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13235}
13236
13237/*
13238 * "strlen()" function
13239 */
13240 static void
13241f_strlen(typval_T *argvars, typval_T *rettv)
13242{
13243 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013244 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013245}
13246
13247/*
13248 * "strchars()" function
13249 */
13250 static void
13251f_strchars(typval_T *argvars, typval_T *rettv)
13252{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013253 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013254 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013255 varnumber_T len = 0;
13256 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013257
13258 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013259 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013260 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013261 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013262 else
13263 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013264 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13265 while (*s != NUL)
13266 {
13267 func_mb_ptr2char_adv(&s);
13268 ++len;
13269 }
13270 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013271 }
13272}
13273
13274/*
13275 * "strdisplaywidth()" function
13276 */
13277 static void
13278f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13279{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013280 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013281 int col = 0;
13282
13283 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013284 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013285
13286 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13287}
13288
13289/*
13290 * "strwidth()" function
13291 */
13292 static void
13293f_strwidth(typval_T *argvars, typval_T *rettv)
13294{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013295 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013296
Bram Moolenaar13505972019-01-24 15:04:48 +010013297 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013298}
13299
13300/*
13301 * "strcharpart()" function
13302 */
13303 static void
13304f_strcharpart(typval_T *argvars, typval_T *rettv)
13305{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013306 char_u *p;
13307 int nchar;
13308 int nbyte = 0;
13309 int charlen;
13310 int len = 0;
13311 int slen;
13312 int error = FALSE;
13313
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013314 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013315 slen = (int)STRLEN(p);
13316
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013317 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013318 if (!error)
13319 {
13320 if (nchar > 0)
13321 while (nchar > 0 && nbyte < slen)
13322 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013323 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013324 --nchar;
13325 }
13326 else
13327 nbyte = nchar;
13328 if (argvars[2].v_type != VAR_UNKNOWN)
13329 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013330 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013331 while (charlen > 0 && nbyte + len < slen)
13332 {
13333 int off = nbyte + len;
13334
13335 if (off < 0)
13336 len += 1;
13337 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013338 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013339 --charlen;
13340 }
13341 }
13342 else
13343 len = slen - nbyte; /* default: all bytes that are available. */
13344 }
13345
13346 /*
13347 * Only return the overlap between the specified part and the actual
13348 * string.
13349 */
13350 if (nbyte < 0)
13351 {
13352 len += nbyte;
13353 nbyte = 0;
13354 }
13355 else if (nbyte > slen)
13356 nbyte = slen;
13357 if (len < 0)
13358 len = 0;
13359 else if (nbyte + len > slen)
13360 len = slen - nbyte;
13361
13362 rettv->v_type = VAR_STRING;
13363 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013364}
13365
13366/*
13367 * "strpart()" function
13368 */
13369 static void
13370f_strpart(typval_T *argvars, typval_T *rettv)
13371{
13372 char_u *p;
13373 int n;
13374 int len;
13375 int slen;
13376 int error = FALSE;
13377
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013378 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013379 slen = (int)STRLEN(p);
13380
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013381 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013382 if (error)
13383 len = 0;
13384 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013385 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013386 else
13387 len = slen - n; /* default len: all bytes that are available. */
13388
13389 /*
13390 * Only return the overlap between the specified part and the actual
13391 * string.
13392 */
13393 if (n < 0)
13394 {
13395 len += n;
13396 n = 0;
13397 }
13398 else if (n > slen)
13399 n = slen;
13400 if (len < 0)
13401 len = 0;
13402 else if (n + len > slen)
13403 len = slen - n;
13404
13405 rettv->v_type = VAR_STRING;
13406 rettv->vval.v_string = vim_strnsave(p + n, len);
13407}
13408
13409/*
13410 * "strridx()" function
13411 */
13412 static void
13413f_strridx(typval_T *argvars, typval_T *rettv)
13414{
13415 char_u buf[NUMBUFLEN];
13416 char_u *needle;
13417 char_u *haystack;
13418 char_u *rest;
13419 char_u *lastmatch = NULL;
13420 int haystack_len, end_idx;
13421
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013422 needle = tv_get_string_chk(&argvars[1]);
13423 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013424
13425 rettv->vval.v_number = -1;
13426 if (needle == NULL || haystack == NULL)
13427 return; /* type error; errmsg already given */
13428
13429 haystack_len = (int)STRLEN(haystack);
13430 if (argvars[2].v_type != VAR_UNKNOWN)
13431 {
13432 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013433 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013434 if (end_idx < 0)
13435 return; /* can never find a match */
13436 }
13437 else
13438 end_idx = haystack_len;
13439
13440 if (*needle == NUL)
13441 {
13442 /* Empty string matches past the end. */
13443 lastmatch = haystack + end_idx;
13444 }
13445 else
13446 {
13447 for (rest = haystack; *rest != '\0'; ++rest)
13448 {
13449 rest = (char_u *)strstr((char *)rest, (char *)needle);
13450 if (rest == NULL || rest > haystack + end_idx)
13451 break;
13452 lastmatch = rest;
13453 }
13454 }
13455
13456 if (lastmatch == NULL)
13457 rettv->vval.v_number = -1;
13458 else
13459 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13460}
13461
13462/*
13463 * "strtrans()" function
13464 */
13465 static void
13466f_strtrans(typval_T *argvars, typval_T *rettv)
13467{
13468 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013469 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013470}
13471
13472/*
13473 * "submatch()" function
13474 */
13475 static void
13476f_submatch(typval_T *argvars, typval_T *rettv)
13477{
13478 int error = FALSE;
13479 int no;
13480 int retList = 0;
13481
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013482 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013483 if (error)
13484 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013485 if (no < 0 || no >= NSUBEXP)
13486 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013487 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013488 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013489 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013490 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013491 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013492 if (error)
13493 return;
13494
13495 if (retList == 0)
13496 {
13497 rettv->v_type = VAR_STRING;
13498 rettv->vval.v_string = reg_submatch(no);
13499 }
13500 else
13501 {
13502 rettv->v_type = VAR_LIST;
13503 rettv->vval.v_list = reg_submatch_list(no);
13504 }
13505}
13506
13507/*
13508 * "substitute()" function
13509 */
13510 static void
13511f_substitute(typval_T *argvars, typval_T *rettv)
13512{
13513 char_u patbuf[NUMBUFLEN];
13514 char_u subbuf[NUMBUFLEN];
13515 char_u flagsbuf[NUMBUFLEN];
13516
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013517 char_u *str = tv_get_string_chk(&argvars[0]);
13518 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013519 char_u *sub = NULL;
13520 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013521 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013522
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013523 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13524 expr = &argvars[2];
13525 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013526 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013527
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013528 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013529 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13530 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013531 rettv->vval.v_string = NULL;
13532 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013533 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013534}
13535
13536/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013537 * "swapinfo(swap_filename)" function
13538 */
13539 static void
13540f_swapinfo(typval_T *argvars, typval_T *rettv)
13541{
13542 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013543 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013544}
13545
13546/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013547 * "swapname(expr)" function
13548 */
13549 static void
13550f_swapname(typval_T *argvars, typval_T *rettv)
13551{
13552 buf_T *buf;
13553
13554 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013555 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013556 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13557 || buf->b_ml.ml_mfp->mf_fname == NULL)
13558 rettv->vval.v_string = NULL;
13559 else
13560 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13561}
13562
13563/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013564 * "synID(lnum, col, trans)" function
13565 */
13566 static void
13567f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13568{
13569 int id = 0;
13570#ifdef FEAT_SYN_HL
13571 linenr_T lnum;
13572 colnr_T col;
13573 int trans;
13574 int transerr = FALSE;
13575
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013576 lnum = tv_get_lnum(argvars); /* -1 on type error */
13577 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13578 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013579
13580 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13581 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13582 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13583#endif
13584
13585 rettv->vval.v_number = id;
13586}
13587
13588/*
13589 * "synIDattr(id, what [, mode])" function
13590 */
13591 static void
13592f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13593{
13594 char_u *p = NULL;
13595#ifdef FEAT_SYN_HL
13596 int id;
13597 char_u *what;
13598 char_u *mode;
13599 char_u modebuf[NUMBUFLEN];
13600 int modec;
13601
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013602 id = (int)tv_get_number(&argvars[0]);
13603 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013604 if (argvars[2].v_type != VAR_UNKNOWN)
13605 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013606 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013607 modec = TOLOWER_ASC(mode[0]);
13608 if (modec != 't' && modec != 'c' && modec != 'g')
13609 modec = 0; /* replace invalid with current */
13610 }
13611 else
13612 {
13613#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13614 if (USE_24BIT)
13615 modec = 'g';
13616 else
13617#endif
13618 if (t_colors > 1)
13619 modec = 'c';
13620 else
13621 modec = 't';
13622 }
13623
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013624 switch (TOLOWER_ASC(what[0]))
13625 {
13626 case 'b':
13627 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13628 p = highlight_color(id, what, modec);
13629 else /* bold */
13630 p = highlight_has_attr(id, HL_BOLD, modec);
13631 break;
13632
13633 case 'f': /* fg[#] or font */
13634 p = highlight_color(id, what, modec);
13635 break;
13636
13637 case 'i':
13638 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13639 p = highlight_has_attr(id, HL_INVERSE, modec);
13640 else /* italic */
13641 p = highlight_has_attr(id, HL_ITALIC, modec);
13642 break;
13643
13644 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013645 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013646 break;
13647
13648 case 'r': /* reverse */
13649 p = highlight_has_attr(id, HL_INVERSE, modec);
13650 break;
13651
13652 case 's':
13653 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13654 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013655 /* strikeout */
13656 else if (TOLOWER_ASC(what[1]) == 't' &&
13657 TOLOWER_ASC(what[2]) == 'r')
13658 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013659 else /* standout */
13660 p = highlight_has_attr(id, HL_STANDOUT, modec);
13661 break;
13662
13663 case 'u':
13664 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13665 /* underline */
13666 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13667 else
13668 /* undercurl */
13669 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13670 break;
13671 }
13672
13673 if (p != NULL)
13674 p = vim_strsave(p);
13675#endif
13676 rettv->v_type = VAR_STRING;
13677 rettv->vval.v_string = p;
13678}
13679
13680/*
13681 * "synIDtrans(id)" function
13682 */
13683 static void
13684f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13685{
13686 int id;
13687
13688#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013689 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013690
13691 if (id > 0)
13692 id = syn_get_final_id(id);
13693 else
13694#endif
13695 id = 0;
13696
13697 rettv->vval.v_number = id;
13698}
13699
13700/*
13701 * "synconcealed(lnum, col)" function
13702 */
13703 static void
13704f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13705{
13706#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13707 linenr_T lnum;
13708 colnr_T col;
13709 int syntax_flags = 0;
13710 int cchar;
13711 int matchid = 0;
13712 char_u str[NUMBUFLEN];
13713#endif
13714
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013715 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013716
13717#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013718 lnum = tv_get_lnum(argvars); /* -1 on type error */
13719 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013720
13721 vim_memset(str, NUL, sizeof(str));
13722
13723 if (rettv_list_alloc(rettv) != FAIL)
13724 {
13725 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13726 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13727 && curwin->w_p_cole > 0)
13728 {
13729 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13730 syntax_flags = get_syntax_info(&matchid);
13731
13732 /* get the conceal character */
13733 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13734 {
13735 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013736 if (cchar == NUL && curwin->w_p_cole == 1)
13737 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013738 if (cchar != NUL)
13739 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013740 if (has_mbyte)
13741 (*mb_char2bytes)(cchar, str);
13742 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013743 str[0] = cchar;
13744 }
13745 }
13746 }
13747
13748 list_append_number(rettv->vval.v_list,
13749 (syntax_flags & HL_CONCEAL) != 0);
13750 /* -1 to auto-determine strlen */
13751 list_append_string(rettv->vval.v_list, str, -1);
13752 list_append_number(rettv->vval.v_list, matchid);
13753 }
13754#endif
13755}
13756
13757/*
13758 * "synstack(lnum, col)" function
13759 */
13760 static void
13761f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13762{
13763#ifdef FEAT_SYN_HL
13764 linenr_T lnum;
13765 colnr_T col;
13766 int i;
13767 int id;
13768#endif
13769
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013770 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013771
13772#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013773 lnum = tv_get_lnum(argvars); /* -1 on type error */
13774 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013775
13776 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13777 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13778 && rettv_list_alloc(rettv) != FAIL)
13779 {
13780 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13781 for (i = 0; ; ++i)
13782 {
13783 id = syn_get_stack_item(i);
13784 if (id < 0)
13785 break;
13786 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13787 break;
13788 }
13789 }
13790#endif
13791}
13792
13793 static void
13794get_cmd_output_as_rettv(
13795 typval_T *argvars,
13796 typval_T *rettv,
13797 int retlist)
13798{
13799 char_u *res = NULL;
13800 char_u *p;
13801 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013802 int err = FALSE;
13803 FILE *fd;
13804 list_T *list = NULL;
13805 int flags = SHELL_SILENT;
13806
13807 rettv->v_type = VAR_STRING;
13808 rettv->vval.v_string = NULL;
13809 if (check_restricted() || check_secure())
13810 goto errret;
13811
13812 if (argvars[1].v_type != VAR_UNKNOWN)
13813 {
13814 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013815 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013816 * command.
13817 */
13818 if ((infile = vim_tempname('i', TRUE)) == NULL)
13819 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013820 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013821 goto errret;
13822 }
13823
13824 fd = mch_fopen((char *)infile, WRITEBIN);
13825 if (fd == NULL)
13826 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013827 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013828 goto errret;
13829 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013830 if (argvars[1].v_type == VAR_NUMBER)
13831 {
13832 linenr_T lnum;
13833 buf_T *buf;
13834
13835 buf = buflist_findnr(argvars[1].vval.v_number);
13836 if (buf == NULL)
13837 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013838 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013839 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013840 goto errret;
13841 }
13842
13843 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13844 {
13845 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13846 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13847 {
13848 err = TRUE;
13849 break;
13850 }
13851 if (putc(NL, fd) == EOF)
13852 {
13853 err = TRUE;
13854 break;
13855 }
13856 }
13857 }
13858 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013859 {
13860 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13861 err = TRUE;
13862 }
13863 else
13864 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013865 size_t len;
13866 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013867
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013868 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013869 if (p == NULL)
13870 {
13871 fclose(fd);
13872 goto errret; /* type error; errmsg already given */
13873 }
13874 len = STRLEN(p);
13875 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13876 err = TRUE;
13877 }
13878 if (fclose(fd) != 0)
13879 err = TRUE;
13880 if (err)
13881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013882 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013883 goto errret;
13884 }
13885 }
13886
13887 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13888 * echoes typeahead, that messes up the display. */
13889 if (!msg_silent)
13890 flags += SHELL_COOKED;
13891
13892 if (retlist)
13893 {
13894 int len;
13895 listitem_T *li;
13896 char_u *s = NULL;
13897 char_u *start;
13898 char_u *end;
13899 int i;
13900
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013901 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013902 if (res == NULL)
13903 goto errret;
13904
13905 list = list_alloc();
13906 if (list == NULL)
13907 goto errret;
13908
13909 for (i = 0; i < len; ++i)
13910 {
13911 start = res + i;
13912 while (i < len && res[i] != NL)
13913 ++i;
13914 end = res + i;
13915
13916 s = alloc((unsigned)(end - start + 1));
13917 if (s == NULL)
13918 goto errret;
13919
13920 for (p = s; start < end; ++p, ++start)
13921 *p = *start == NUL ? NL : *start;
13922 *p = NUL;
13923
13924 li = listitem_alloc();
13925 if (li == NULL)
13926 {
13927 vim_free(s);
13928 goto errret;
13929 }
13930 li->li_tv.v_type = VAR_STRING;
13931 li->li_tv.v_lock = 0;
13932 li->li_tv.vval.v_string = s;
13933 list_append(list, li);
13934 }
13935
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013936 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013937 list = NULL;
13938 }
13939 else
13940 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013941 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013942#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013943 /* translate <CR><NL> into <NL> */
13944 if (res != NULL)
13945 {
13946 char_u *s, *d;
13947
13948 d = res;
13949 for (s = res; *s; ++s)
13950 {
13951 if (s[0] == CAR && s[1] == NL)
13952 ++s;
13953 *d++ = *s;
13954 }
13955 *d = NUL;
13956 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013957#endif
13958 rettv->vval.v_string = res;
13959 res = NULL;
13960 }
13961
13962errret:
13963 if (infile != NULL)
13964 {
13965 mch_remove(infile);
13966 vim_free(infile);
13967 }
13968 if (res != NULL)
13969 vim_free(res);
13970 if (list != NULL)
13971 list_free(list);
13972}
13973
13974/*
13975 * "system()" function
13976 */
13977 static void
13978f_system(typval_T *argvars, typval_T *rettv)
13979{
13980 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13981}
13982
13983/*
13984 * "systemlist()" function
13985 */
13986 static void
13987f_systemlist(typval_T *argvars, typval_T *rettv)
13988{
13989 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13990}
13991
13992/*
13993 * "tabpagebuflist()" function
13994 */
13995 static void
13996f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13997{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013998 tabpage_T *tp;
13999 win_T *wp = NULL;
14000
14001 if (argvars[0].v_type == VAR_UNKNOWN)
14002 wp = firstwin;
14003 else
14004 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014005 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014006 if (tp != NULL)
14007 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14008 }
14009 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14010 {
14011 for (; wp != NULL; wp = wp->w_next)
14012 if (list_append_number(rettv->vval.v_list,
14013 wp->w_buffer->b_fnum) == FAIL)
14014 break;
14015 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014016}
14017
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014018/*
14019 * "tabpagenr()" function
14020 */
14021 static void
14022f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14023{
14024 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014025 char_u *arg;
14026
14027 if (argvars[0].v_type != VAR_UNKNOWN)
14028 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014029 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014030 nr = 0;
14031 if (arg != NULL)
14032 {
14033 if (STRCMP(arg, "$") == 0)
14034 nr = tabpage_index(NULL) - 1;
14035 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014036 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014037 }
14038 }
14039 else
14040 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014041 rettv->vval.v_number = nr;
14042}
14043
14044
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014045/*
14046 * Common code for tabpagewinnr() and winnr().
14047 */
14048 static int
14049get_winnr(tabpage_T *tp, typval_T *argvar)
14050{
14051 win_T *twin;
14052 int nr = 1;
14053 win_T *wp;
14054 char_u *arg;
14055
14056 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14057 if (argvar->v_type != VAR_UNKNOWN)
14058 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014059 int invalid_arg = FALSE;
14060
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014061 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014062 if (arg == NULL)
14063 nr = 0; /* type error; errmsg already given */
14064 else if (STRCMP(arg, "$") == 0)
14065 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14066 else if (STRCMP(arg, "#") == 0)
14067 {
14068 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14069 if (twin == NULL)
14070 nr = 0;
14071 }
14072 else
14073 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014074 long count;
14075 char_u *endp;
14076
14077 // Extract the window count (if specified). e.g. winnr('3j')
14078 count = strtol((char *)arg, (char **)&endp, 10);
14079 if (count <= 0)
14080 count = 1; // if count is not specified, default to 1
14081 if (endp != NULL && *endp != '\0')
14082 {
14083 if (STRCMP(endp, "j") == 0)
14084 twin = win_vert_neighbor(tp, twin, FALSE, count);
14085 else if (STRCMP(endp, "k") == 0)
14086 twin = win_vert_neighbor(tp, twin, TRUE, count);
14087 else if (STRCMP(endp, "h") == 0)
14088 twin = win_horz_neighbor(tp, twin, TRUE, count);
14089 else if (STRCMP(endp, "l") == 0)
14090 twin = win_horz_neighbor(tp, twin, FALSE, count);
14091 else
14092 invalid_arg = TRUE;
14093 }
14094 else
14095 invalid_arg = TRUE;
14096 }
14097
14098 if (invalid_arg)
14099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014100 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014101 nr = 0;
14102 }
14103 }
14104
14105 if (nr > 0)
14106 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14107 wp != twin; wp = wp->w_next)
14108 {
14109 if (wp == NULL)
14110 {
14111 /* didn't find it in this tabpage */
14112 nr = 0;
14113 break;
14114 }
14115 ++nr;
14116 }
14117 return nr;
14118}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014119
14120/*
14121 * "tabpagewinnr()" function
14122 */
14123 static void
14124f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14125{
14126 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014127 tabpage_T *tp;
14128
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014129 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014130 if (tp == NULL)
14131 nr = 0;
14132 else
14133 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014134 rettv->vval.v_number = nr;
14135}
14136
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014137/*
14138 * "tagfiles()" function
14139 */
14140 static void
14141f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14142{
14143 char_u *fname;
14144 tagname_T tn;
14145 int first;
14146
14147 if (rettv_list_alloc(rettv) == FAIL)
14148 return;
14149 fname = alloc(MAXPATHL);
14150 if (fname == NULL)
14151 return;
14152
14153 for (first = TRUE; ; first = FALSE)
14154 if (get_tagfname(&tn, first, fname) == FAIL
14155 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14156 break;
14157 tagname_free(&tn);
14158 vim_free(fname);
14159}
14160
14161/*
14162 * "taglist()" function
14163 */
14164 static void
14165f_taglist(typval_T *argvars, typval_T *rettv)
14166{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014167 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014168 char_u *tag_pattern;
14169
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014170 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014171
14172 rettv->vval.v_number = FALSE;
14173 if (*tag_pattern == NUL)
14174 return;
14175
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014176 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014177 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014178 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014179 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014180}
14181
14182/*
14183 * "tempname()" function
14184 */
14185 static void
14186f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14187{
14188 static int x = 'A';
14189
14190 rettv->v_type = VAR_STRING;
14191 rettv->vval.v_string = vim_tempname(x, FALSE);
14192
14193 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14194 * names. Skip 'I' and 'O', they are used for shell redirection. */
14195 do
14196 {
14197 if (x == 'Z')
14198 x = '0';
14199 else if (x == '9')
14200 x = 'A';
14201 else
14202 {
14203#ifdef EBCDIC
14204 if (x == 'I')
14205 x = 'J';
14206 else if (x == 'R')
14207 x = 'S';
14208 else
14209#endif
14210 ++x;
14211 }
14212 } while (x == 'I' || x == 'O');
14213}
14214
14215#ifdef FEAT_FLOAT
14216/*
14217 * "tan()" function
14218 */
14219 static void
14220f_tan(typval_T *argvars, typval_T *rettv)
14221{
14222 float_T f = 0.0;
14223
14224 rettv->v_type = VAR_FLOAT;
14225 if (get_float_arg(argvars, &f) == OK)
14226 rettv->vval.v_float = tan(f);
14227 else
14228 rettv->vval.v_float = 0.0;
14229}
14230
14231/*
14232 * "tanh()" function
14233 */
14234 static void
14235f_tanh(typval_T *argvars, typval_T *rettv)
14236{
14237 float_T f = 0.0;
14238
14239 rettv->v_type = VAR_FLOAT;
14240 if (get_float_arg(argvars, &f) == OK)
14241 rettv->vval.v_float = tanh(f);
14242 else
14243 rettv->vval.v_float = 0.0;
14244}
14245#endif
14246
14247/*
14248 * "test_alloc_fail(id, countdown, repeat)" function
14249 */
14250 static void
14251f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14252{
14253 if (argvars[0].v_type != VAR_NUMBER
14254 || argvars[0].vval.v_number <= 0
14255 || argvars[1].v_type != VAR_NUMBER
14256 || argvars[1].vval.v_number < 0
14257 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014258 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014259 else
14260 {
14261 alloc_fail_id = argvars[0].vval.v_number;
14262 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014263 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014264 alloc_fail_countdown = argvars[1].vval.v_number;
14265 alloc_fail_repeat = argvars[2].vval.v_number;
14266 did_outofmem_msg = FALSE;
14267 }
14268}
14269
14270/*
14271 * "test_autochdir()"
14272 */
14273 static void
14274f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14275{
14276#if defined(FEAT_AUTOCHDIR)
14277 test_autochdir = TRUE;
14278#endif
14279}
14280
14281/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014282 * "test_feedinput()"
14283 */
14284 static void
14285f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14286{
14287#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014288 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014289
14290 if (val != NULL)
14291 {
14292 trash_input_buf();
14293 add_to_input_buf_csi(val, (int)STRLEN(val));
14294 }
14295#endif
14296}
14297
14298/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014299 * "test_option_not_set({name})" function
14300 */
14301 static void
14302f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14303{
14304 char_u *name = (char_u *)"";
14305
14306 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014307 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014308 else
14309 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014310 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014311 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014312 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014313 }
14314}
14315
14316/*
14317 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014318 */
14319 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014320f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014321{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014322 char_u *name = (char_u *)"";
14323 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014324 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014325
14326 if (argvars[0].v_type != VAR_STRING
14327 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014328 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014329 else
14330 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014331 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014332 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014333
14334 if (STRCMP(name, (char_u *)"redraw") == 0)
14335 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014336 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14337 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014338 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14339 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014340 else if (STRCMP(name, (char_u *)"starting") == 0)
14341 {
14342 if (val)
14343 {
14344 if (save_starting < 0)
14345 save_starting = starting;
14346 starting = 0;
14347 }
14348 else
14349 {
14350 starting = save_starting;
14351 save_starting = -1;
14352 }
14353 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014354 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14355 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014356 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14357 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014358 else if (STRCMP(name, (char_u *)"ALL") == 0)
14359 {
14360 disable_char_avail_for_testing = FALSE;
14361 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014362 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014363 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014364 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014365 if (save_starting >= 0)
14366 {
14367 starting = save_starting;
14368 save_starting = -1;
14369 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014370 }
14371 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014372 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014373 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014374}
14375
14376/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014377 * "test_refcount({expr})" function
14378 */
14379 static void
14380f_test_refcount(typval_T *argvars, typval_T *rettv)
14381{
14382 int retval = -1;
14383
14384 switch (argvars[0].v_type)
14385 {
14386 case VAR_UNKNOWN:
14387 case VAR_NUMBER:
14388 case VAR_FLOAT:
14389 case VAR_SPECIAL:
14390 case VAR_STRING:
14391 break;
14392 case VAR_JOB:
14393#ifdef FEAT_JOB_CHANNEL
14394 if (argvars[0].vval.v_job != NULL)
14395 retval = argvars[0].vval.v_job->jv_refcount - 1;
14396#endif
14397 break;
14398 case VAR_CHANNEL:
14399#ifdef FEAT_JOB_CHANNEL
14400 if (argvars[0].vval.v_channel != NULL)
14401 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14402#endif
14403 break;
14404 case VAR_FUNC:
14405 if (argvars[0].vval.v_string != NULL)
14406 {
14407 ufunc_T *fp;
14408
14409 fp = find_func(argvars[0].vval.v_string);
14410 if (fp != NULL)
14411 retval = fp->uf_refcount;
14412 }
14413 break;
14414 case VAR_PARTIAL:
14415 if (argvars[0].vval.v_partial != NULL)
14416 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14417 break;
14418 case VAR_BLOB:
14419 if (argvars[0].vval.v_blob != NULL)
14420 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14421 break;
14422 case VAR_LIST:
14423 if (argvars[0].vval.v_list != NULL)
14424 retval = argvars[0].vval.v_list->lv_refcount - 1;
14425 break;
14426 case VAR_DICT:
14427 if (argvars[0].vval.v_dict != NULL)
14428 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14429 break;
14430 }
14431
14432 rettv->v_type = VAR_NUMBER;
14433 rettv->vval.v_number = retval;
14434
14435}
14436
14437/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014438 * "test_garbagecollect_now()" function
14439 */
14440 static void
14441f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14442{
14443 /* This is dangerous, any Lists and Dicts used internally may be freed
14444 * while still in use. */
14445 garbage_collect(TRUE);
14446}
14447
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014448/*
14449 * "test_ignore_error()" function
14450 */
14451 static void
14452f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14453{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014454 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014455}
14456
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014457 static void
14458f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14459{
14460 rettv->v_type = VAR_BLOB;
14461 rettv->vval.v_blob = NULL;
14462}
14463
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014464#ifdef FEAT_JOB_CHANNEL
14465 static void
14466f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14467{
14468 rettv->v_type = VAR_CHANNEL;
14469 rettv->vval.v_channel = NULL;
14470}
14471#endif
14472
14473 static void
14474f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14475{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014476 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014477}
14478
14479#ifdef FEAT_JOB_CHANNEL
14480 static void
14481f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14482{
14483 rettv->v_type = VAR_JOB;
14484 rettv->vval.v_job = NULL;
14485}
14486#endif
14487
14488 static void
14489f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14490{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014491 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014492}
14493
14494 static void
14495f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14496{
14497 rettv->v_type = VAR_PARTIAL;
14498 rettv->vval.v_partial = NULL;
14499}
14500
14501 static void
14502f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14503{
14504 rettv->v_type = VAR_STRING;
14505 rettv->vval.v_string = NULL;
14506}
14507
Bram Moolenaarab186732018-09-14 21:27:06 +020014508#ifdef FEAT_GUI
14509 static void
14510f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14511{
14512 char_u *which;
14513 long value;
14514 int dragging;
14515 scrollbar_T *sb = NULL;
14516
14517 if (argvars[0].v_type != VAR_STRING
14518 || (argvars[1].v_type) != VAR_NUMBER
14519 || (argvars[2].v_type) != VAR_NUMBER)
14520 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014521 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014522 return;
14523 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014524 which = tv_get_string(&argvars[0]);
14525 value = tv_get_number(&argvars[1]);
14526 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014527
14528 if (STRCMP(which, "left") == 0)
14529 sb = &curwin->w_scrollbars[SBAR_LEFT];
14530 else if (STRCMP(which, "right") == 0)
14531 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14532 else if (STRCMP(which, "hor") == 0)
14533 sb = &gui.bottom_sbar;
14534 if (sb == NULL)
14535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014536 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014537 return;
14538 }
14539 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014540# ifndef USE_ON_FLY_SCROLL
14541 // need to loop through normal_cmd() to handle the scroll events
14542 exec_normal(FALSE, TRUE, FALSE);
14543# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014544}
14545#endif
14546
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014547#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014548 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014549f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14550{
14551 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14552 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14553}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014554#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014555
14556 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014557f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14558{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014559 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014560}
14561
14562#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
14563/*
14564 * Get a callback from "arg". It can be a Funcref or a function name.
14565 * When "arg" is zero return an empty string.
14566 * Return NULL for an invalid argument.
14567 */
14568 char_u *
14569get_callback(typval_T *arg, partial_T **pp)
14570{
14571 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14572 {
14573 *pp = arg->vval.v_partial;
14574 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014575 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014576 }
14577 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014578 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014579 {
14580 func_ref(arg->vval.v_string);
14581 return arg->vval.v_string;
14582 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014583 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14584 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014585 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014586 return NULL;
14587}
14588
14589/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014590 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014591 */
14592 void
14593free_callback(char_u *callback, partial_T *partial)
14594{
14595 if (partial != NULL)
14596 partial_unref(partial);
14597 else if (callback != NULL)
14598 {
14599 func_unref(callback);
14600 vim_free(callback);
14601 }
14602}
14603#endif
14604
14605#ifdef FEAT_TIMERS
14606/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014607 * "timer_info([timer])" function
14608 */
14609 static void
14610f_timer_info(typval_T *argvars, typval_T *rettv)
14611{
14612 timer_T *timer = NULL;
14613
14614 if (rettv_list_alloc(rettv) != OK)
14615 return;
14616 if (argvars[0].v_type != VAR_UNKNOWN)
14617 {
14618 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014619 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014620 else
14621 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014622 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014623 if (timer != NULL)
14624 add_timer_info(rettv, timer);
14625 }
14626 }
14627 else
14628 add_timer_info_all(rettv);
14629}
14630
14631/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014632 * "timer_pause(timer, paused)" function
14633 */
14634 static void
14635f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14636{
14637 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014638 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014639
14640 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014641 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014642 else
14643 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014644 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014645 if (timer != NULL)
14646 timer->tr_paused = paused;
14647 }
14648}
14649
14650/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014651 * "timer_start(time, callback [, options])" function
14652 */
14653 static void
14654f_timer_start(typval_T *argvars, typval_T *rettv)
14655{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014656 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014657 timer_T *timer;
14658 int repeat = 0;
14659 char_u *callback;
14660 dict_T *dict;
14661 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014662
Bram Moolenaar75537a92016-09-05 22:45:28 +020014663 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014664 if (check_secure())
14665 return;
14666 if (argvars[2].v_type != VAR_UNKNOWN)
14667 {
14668 if (argvars[2].v_type != VAR_DICT
14669 || (dict = argvars[2].vval.v_dict) == NULL)
14670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014671 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014672 return;
14673 }
14674 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014675 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014676 }
14677
Bram Moolenaar75537a92016-09-05 22:45:28 +020014678 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014679 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014680 return;
14681
14682 timer = create_timer(msec, repeat);
14683 if (timer == NULL)
14684 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014685 else
14686 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014687 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014688 timer->tr_callback = vim_strsave(callback);
14689 else
14690 /* pointer into the partial */
14691 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014692 timer->tr_partial = partial;
14693 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014694 }
14695}
14696
14697/*
14698 * "timer_stop(timer)" function
14699 */
14700 static void
14701f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14702{
14703 timer_T *timer;
14704
14705 if (argvars[0].v_type != VAR_NUMBER)
14706 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014707 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014708 return;
14709 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014710 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014711 if (timer != NULL)
14712 stop_timer(timer);
14713}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014714
14715/*
14716 * "timer_stopall()" function
14717 */
14718 static void
14719f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14720{
14721 stop_all_timers();
14722}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014723#endif
14724
14725/*
14726 * "tolower(string)" function
14727 */
14728 static void
14729f_tolower(typval_T *argvars, typval_T *rettv)
14730{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014731 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014732 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014733}
14734
14735/*
14736 * "toupper(string)" function
14737 */
14738 static void
14739f_toupper(typval_T *argvars, typval_T *rettv)
14740{
14741 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014742 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014743}
14744
14745/*
14746 * "tr(string, fromstr, tostr)" function
14747 */
14748 static void
14749f_tr(typval_T *argvars, typval_T *rettv)
14750{
14751 char_u *in_str;
14752 char_u *fromstr;
14753 char_u *tostr;
14754 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014755 int inlen;
14756 int fromlen;
14757 int tolen;
14758 int idx;
14759 char_u *cpstr;
14760 int cplen;
14761 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014762 char_u buf[NUMBUFLEN];
14763 char_u buf2[NUMBUFLEN];
14764 garray_T ga;
14765
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014766 in_str = tv_get_string(&argvars[0]);
14767 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14768 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014769
14770 /* Default return value: empty string. */
14771 rettv->v_type = VAR_STRING;
14772 rettv->vval.v_string = NULL;
14773 if (fromstr == NULL || tostr == NULL)
14774 return; /* type error; errmsg already given */
14775 ga_init2(&ga, (int)sizeof(char), 80);
14776
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014777 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014778 /* not multi-byte: fromstr and tostr must be the same length */
14779 if (STRLEN(fromstr) != STRLEN(tostr))
14780 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014781error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014782 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014783 ga_clear(&ga);
14784 return;
14785 }
14786
14787 /* fromstr and tostr have to contain the same number of chars */
14788 while (*in_str != NUL)
14789 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014790 if (has_mbyte)
14791 {
14792 inlen = (*mb_ptr2len)(in_str);
14793 cpstr = in_str;
14794 cplen = inlen;
14795 idx = 0;
14796 for (p = fromstr; *p != NUL; p += fromlen)
14797 {
14798 fromlen = (*mb_ptr2len)(p);
14799 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14800 {
14801 for (p = tostr; *p != NUL; p += tolen)
14802 {
14803 tolen = (*mb_ptr2len)(p);
14804 if (idx-- == 0)
14805 {
14806 cplen = tolen;
14807 cpstr = p;
14808 break;
14809 }
14810 }
14811 if (*p == NUL) /* tostr is shorter than fromstr */
14812 goto error;
14813 break;
14814 }
14815 ++idx;
14816 }
14817
14818 if (first && cpstr == in_str)
14819 {
14820 /* Check that fromstr and tostr have the same number of
14821 * (multi-byte) characters. Done only once when a character
14822 * of in_str doesn't appear in fromstr. */
14823 first = FALSE;
14824 for (p = tostr; *p != NUL; p += tolen)
14825 {
14826 tolen = (*mb_ptr2len)(p);
14827 --idx;
14828 }
14829 if (idx != 0)
14830 goto error;
14831 }
14832
14833 (void)ga_grow(&ga, cplen);
14834 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14835 ga.ga_len += cplen;
14836
14837 in_str += inlen;
14838 }
14839 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014840 {
14841 /* When not using multi-byte chars we can do it faster. */
14842 p = vim_strchr(fromstr, *in_str);
14843 if (p != NULL)
14844 ga_append(&ga, tostr[p - fromstr]);
14845 else
14846 ga_append(&ga, *in_str);
14847 ++in_str;
14848 }
14849 }
14850
14851 /* add a terminating NUL */
14852 (void)ga_grow(&ga, 1);
14853 ga_append(&ga, NUL);
14854
14855 rettv->vval.v_string = ga.ga_data;
14856}
14857
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014858/*
14859 * "trim({expr})" function
14860 */
14861 static void
14862f_trim(typval_T *argvars, typval_T *rettv)
14863{
14864 char_u buf1[NUMBUFLEN];
14865 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014866 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014867 char_u *mask = NULL;
14868 char_u *tail;
14869 char_u *prev;
14870 char_u *p;
14871 int c1;
14872
14873 rettv->v_type = VAR_STRING;
14874 if (head == NULL)
14875 {
14876 rettv->vval.v_string = NULL;
14877 return;
14878 }
14879
14880 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014881 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014882
14883 while (*head != NUL)
14884 {
14885 c1 = PTR2CHAR(head);
14886 if (mask == NULL)
14887 {
14888 if (c1 > ' ' && c1 != 0xa0)
14889 break;
14890 }
14891 else
14892 {
14893 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14894 if (c1 == PTR2CHAR(p))
14895 break;
14896 if (*p == NUL)
14897 break;
14898 }
14899 MB_PTR_ADV(head);
14900 }
14901
14902 for (tail = head + STRLEN(head); tail > head; tail = prev)
14903 {
14904 prev = tail;
14905 MB_PTR_BACK(head, prev);
14906 c1 = PTR2CHAR(prev);
14907 if (mask == NULL)
14908 {
14909 if (c1 > ' ' && c1 != 0xa0)
14910 break;
14911 }
14912 else
14913 {
14914 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14915 if (c1 == PTR2CHAR(p))
14916 break;
14917 if (*p == NUL)
14918 break;
14919 }
14920 }
14921 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14922}
14923
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014924#ifdef FEAT_FLOAT
14925/*
14926 * "trunc({float})" function
14927 */
14928 static void
14929f_trunc(typval_T *argvars, typval_T *rettv)
14930{
14931 float_T f = 0.0;
14932
14933 rettv->v_type = VAR_FLOAT;
14934 if (get_float_arg(argvars, &f) == OK)
14935 /* trunc() is not in C90, use floor() or ceil() instead. */
14936 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14937 else
14938 rettv->vval.v_float = 0.0;
14939}
14940#endif
14941
14942/*
14943 * "type(expr)" function
14944 */
14945 static void
14946f_type(typval_T *argvars, typval_T *rettv)
14947{
14948 int n = -1;
14949
14950 switch (argvars[0].v_type)
14951 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014952 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14953 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014954 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014955 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14956 case VAR_LIST: n = VAR_TYPE_LIST; break;
14957 case VAR_DICT: n = VAR_TYPE_DICT; break;
14958 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014959 case VAR_SPECIAL:
14960 if (argvars[0].vval.v_number == VVAL_FALSE
14961 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014962 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014963 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014964 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014965 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014966 case VAR_JOB: n = VAR_TYPE_JOB; break;
14967 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014968 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014969 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014970 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014971 n = -1;
14972 break;
14973 }
14974 rettv->vval.v_number = n;
14975}
14976
14977/*
14978 * "undofile(name)" function
14979 */
14980 static void
14981f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14982{
14983 rettv->v_type = VAR_STRING;
14984#ifdef FEAT_PERSISTENT_UNDO
14985 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014986 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014987
14988 if (*fname == NUL)
14989 {
14990 /* If there is no file name there will be no undo file. */
14991 rettv->vval.v_string = NULL;
14992 }
14993 else
14994 {
14995 char_u *ffname = FullName_save(fname, FALSE);
14996
14997 if (ffname != NULL)
14998 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14999 vim_free(ffname);
15000 }
15001 }
15002#else
15003 rettv->vval.v_string = NULL;
15004#endif
15005}
15006
15007/*
15008 * "undotree()" function
15009 */
15010 static void
15011f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15012{
15013 if (rettv_dict_alloc(rettv) == OK)
15014 {
15015 dict_T *dict = rettv->vval.v_dict;
15016 list_T *list;
15017
Bram Moolenaare0be1672018-07-08 16:50:37 +020015018 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15019 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15020 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15021 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15022 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15023 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015024
15025 list = list_alloc();
15026 if (list != NULL)
15027 {
15028 u_eval_tree(curbuf->b_u_oldhead, list);
15029 dict_add_list(dict, "entries", list);
15030 }
15031 }
15032}
15033
15034/*
15035 * "values(dict)" function
15036 */
15037 static void
15038f_values(typval_T *argvars, typval_T *rettv)
15039{
15040 dict_list(argvars, rettv, 1);
15041}
15042
15043/*
15044 * "virtcol(string)" function
15045 */
15046 static void
15047f_virtcol(typval_T *argvars, typval_T *rettv)
15048{
15049 colnr_T vcol = 0;
15050 pos_T *fp;
15051 int fnum = curbuf->b_fnum;
15052
15053 fp = var2fpos(&argvars[0], FALSE, &fnum);
15054 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15055 && fnum == curbuf->b_fnum)
15056 {
15057 getvvcol(curwin, fp, NULL, NULL, &vcol);
15058 ++vcol;
15059 }
15060
15061 rettv->vval.v_number = vcol;
15062}
15063
15064/*
15065 * "visualmode()" function
15066 */
15067 static void
15068f_visualmode(typval_T *argvars, typval_T *rettv)
15069{
15070 char_u str[2];
15071
15072 rettv->v_type = VAR_STRING;
15073 str[0] = curbuf->b_visual_mode_eval;
15074 str[1] = NUL;
15075 rettv->vval.v_string = vim_strsave(str);
15076
15077 /* A non-zero number or non-empty string argument: reset mode. */
15078 if (non_zero_arg(&argvars[0]))
15079 curbuf->b_visual_mode_eval = NUL;
15080}
15081
15082/*
15083 * "wildmenumode()" function
15084 */
15085 static void
15086f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15087{
15088#ifdef FEAT_WILDMENU
15089 if (wild_menu_showing)
15090 rettv->vval.v_number = 1;
15091#endif
15092}
15093
15094/*
15095 * "winbufnr(nr)" function
15096 */
15097 static void
15098f_winbufnr(typval_T *argvars, typval_T *rettv)
15099{
15100 win_T *wp;
15101
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015102 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015103 if (wp == NULL)
15104 rettv->vval.v_number = -1;
15105 else
15106 rettv->vval.v_number = wp->w_buffer->b_fnum;
15107}
15108
15109/*
15110 * "wincol()" function
15111 */
15112 static void
15113f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15114{
15115 validate_cursor();
15116 rettv->vval.v_number = curwin->w_wcol + 1;
15117}
15118
15119/*
15120 * "winheight(nr)" function
15121 */
15122 static void
15123f_winheight(typval_T *argvars, typval_T *rettv)
15124{
15125 win_T *wp;
15126
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015127 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015128 if (wp == NULL)
15129 rettv->vval.v_number = -1;
15130 else
15131 rettv->vval.v_number = wp->w_height;
15132}
15133
15134/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015135 * "winlayout()" function
15136 */
15137 static void
15138f_winlayout(typval_T *argvars, typval_T *rettv)
15139{
15140 tabpage_T *tp;
15141
15142 if (rettv_list_alloc(rettv) != OK)
15143 return;
15144
15145 if (argvars[0].v_type == VAR_UNKNOWN)
15146 tp = curtab;
15147 else
15148 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015149 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015150 if (tp == NULL)
15151 return;
15152 }
15153
15154 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15155}
15156
15157/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015158 * "winline()" function
15159 */
15160 static void
15161f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15162{
15163 validate_cursor();
15164 rettv->vval.v_number = curwin->w_wrow + 1;
15165}
15166
15167/*
15168 * "winnr()" function
15169 */
15170 static void
15171f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15172{
15173 int nr = 1;
15174
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015175 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015176 rettv->vval.v_number = nr;
15177}
15178
15179/*
15180 * "winrestcmd()" function
15181 */
15182 static void
15183f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15184{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015185 win_T *wp;
15186 int winnr = 1;
15187 garray_T ga;
15188 char_u buf[50];
15189
15190 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015191 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015192 {
15193 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15194 ga_concat(&ga, buf);
15195 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15196 ga_concat(&ga, buf);
15197 ++winnr;
15198 }
15199 ga_append(&ga, NUL);
15200
15201 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015202 rettv->v_type = VAR_STRING;
15203}
15204
15205/*
15206 * "winrestview()" function
15207 */
15208 static void
15209f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15210{
15211 dict_T *dict;
15212
15213 if (argvars[0].v_type != VAR_DICT
15214 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015215 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015216 else
15217 {
15218 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015219 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015220 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015221 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015222 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015223 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015224 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15225 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015226 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015227 curwin->w_set_curswant = FALSE;
15228 }
15229
15230 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015231 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015232#ifdef FEAT_DIFF
15233 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015234 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015235#endif
15236 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015237 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015238 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015239 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015240
15241 check_cursor();
15242 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015243 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015244 changed_window_setting();
15245
15246 if (curwin->w_topline <= 0)
15247 curwin->w_topline = 1;
15248 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15249 curwin->w_topline = curbuf->b_ml.ml_line_count;
15250#ifdef FEAT_DIFF
15251 check_topfill(curwin, TRUE);
15252#endif
15253 }
15254}
15255
15256/*
15257 * "winsaveview()" function
15258 */
15259 static void
15260f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15261{
15262 dict_T *dict;
15263
15264 if (rettv_dict_alloc(rettv) == FAIL)
15265 return;
15266 dict = rettv->vval.v_dict;
15267
Bram Moolenaare0be1672018-07-08 16:50:37 +020015268 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15269 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015270 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015271 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015272 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015273
Bram Moolenaare0be1672018-07-08 16:50:37 +020015274 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015275#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015276 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015277#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015278 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15279 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015280}
15281
15282/*
15283 * "winwidth(nr)" function
15284 */
15285 static void
15286f_winwidth(typval_T *argvars, typval_T *rettv)
15287{
15288 win_T *wp;
15289
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015290 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015291 if (wp == NULL)
15292 rettv->vval.v_number = -1;
15293 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015294 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015295}
15296
15297/*
15298 * "wordcount()" function
15299 */
15300 static void
15301f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15302{
15303 if (rettv_dict_alloc(rettv) == FAIL)
15304 return;
15305 cursor_pos_info(rettv->vval.v_dict);
15306}
15307
15308/*
15309 * "writefile()" function
15310 */
15311 static void
15312f_writefile(typval_T *argvars, typval_T *rettv)
15313{
15314 int binary = FALSE;
15315 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015316#ifdef HAVE_FSYNC
15317 int do_fsync = p_fs;
15318#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015319 char_u *fname;
15320 FILE *fd;
15321 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015322 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015323 list_T *list = NULL;
15324 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015325
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015326 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015327 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015328 return;
15329
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015330 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015331 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015332 list = argvars[0].vval.v_list;
15333 if (list == NULL)
15334 return;
15335 for (li = list->lv_first; li != NULL; li = li->li_next)
15336 if (tv_get_string_chk(&li->li_tv) == NULL)
15337 return;
15338 }
15339 else if (argvars[0].v_type == VAR_BLOB)
15340 {
15341 blob = argvars[0].vval.v_blob;
15342 if (blob == NULL)
15343 return;
15344 }
15345 else
15346 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015347 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015348 return;
15349 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015350
15351 if (argvars[2].v_type != VAR_UNKNOWN)
15352 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015353 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015354
15355 if (arg2 == NULL)
15356 return;
15357 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015358 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015359 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015360 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015361#ifdef HAVE_FSYNC
15362 if (vim_strchr(arg2, 's') != NULL)
15363 do_fsync = TRUE;
15364 else if (vim_strchr(arg2, 'S') != NULL)
15365 do_fsync = FALSE;
15366#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015367 }
15368
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015369 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015370 if (fname == NULL)
15371 return;
15372
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015373 /* Always open the file in binary mode, library functions have a mind of
15374 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015375 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15376 append ? APPENDBIN : WRITEBIN)) == NULL)
15377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015378 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015379 ret = -1;
15380 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015381 else if (blob)
15382 {
15383 if (write_blob(fd, blob) == FAIL)
15384 ret = -1;
15385#ifdef HAVE_FSYNC
15386 else if (do_fsync)
15387 // Ignore the error, the user wouldn't know what to do about it.
15388 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015389 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015390#endif
15391 fclose(fd);
15392 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015393 else
15394 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015395 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015396 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015397#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015398 else if (do_fsync)
15399 /* Ignore the error, the user wouldn't know what to do about it.
15400 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015401 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015402#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015403 fclose(fd);
15404 }
15405
15406 rettv->vval.v_number = ret;
15407}
15408
15409/*
15410 * "xor(expr, expr)" function
15411 */
15412 static void
15413f_xor(typval_T *argvars, typval_T *rettv)
15414{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015415 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15416 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015417}
15418
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015419#endif /* FEAT_EVAL */