blob: b6998993f61a5c5788ae075196537665b280725a [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
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaard0573012017-10-28 21:11:06 +020023#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020024# include <time.h> /* for time_t */
25#endif
26
27static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010028static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020029static char *e_stringreq = N_("E928: String required");
Bram Moolenaaraff74912019-03-30 18:11:49 +010030static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020031
32#ifdef FEAT_FLOAT
33static void f_abs(typval_T *argvars, typval_T *rettv);
34static void f_acos(typval_T *argvars, typval_T *rettv);
35#endif
36static void f_add(typval_T *argvars, typval_T *rettv);
37static void f_and(typval_T *argvars, typval_T *rettv);
38static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020039static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_argc(typval_T *argvars, typval_T *rettv);
41static void f_argidx(typval_T *argvars, typval_T *rettv);
42static void f_arglistid(typval_T *argvars, typval_T *rettv);
43static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010044static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020045static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010046static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020047static void f_assert_exception(typval_T *argvars, typval_T *rettv);
48static void f_assert_fails(typval_T *argvars, typval_T *rettv);
49static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020050static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_match(typval_T *argvars, typval_T *rettv);
52static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
53static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010054static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_true(typval_T *argvars, typval_T *rettv);
56#ifdef FEAT_FLOAT
57static void f_asin(typval_T *argvars, typval_T *rettv);
58static void f_atan(typval_T *argvars, typval_T *rettv);
59static void f_atan2(typval_T *argvars, typval_T *rettv);
60#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010061#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020062static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010063static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010064# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010065static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010067#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_browse(typval_T *argvars, typval_T *rettv);
69static void f_browsedir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020070static void f_bufadd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_bufexists(typval_T *argvars, typval_T *rettv);
72static void f_buflisted(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020073static void f_bufload(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020074static void f_bufloaded(typval_T *argvars, typval_T *rettv);
75static void f_bufname(typval_T *argvars, typval_T *rettv);
76static void f_bufnr(typval_T *argvars, typval_T *rettv);
77static void f_bufwinid(typval_T *argvars, typval_T *rettv);
78static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
79static void f_byte2line(typval_T *argvars, typval_T *rettv);
80static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
81static void f_byteidx(typval_T *argvars, typval_T *rettv);
82static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
83static void f_call(typval_T *argvars, typval_T *rettv);
84#ifdef FEAT_FLOAT
85static void f_ceil(typval_T *argvars, typval_T *rettv);
86#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_changenr(typval_T *argvars, typval_T *rettv);
88static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020089static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_cindent(typval_T *argvars, typval_T *rettv);
91static void f_clearmatches(typval_T *argvars, typval_T *rettv);
92static void f_col(typval_T *argvars, typval_T *rettv);
93#if defined(FEAT_INS_EXPAND)
94static void f_complete(typval_T *argvars, typval_T *rettv);
95static void f_complete_add(typval_T *argvars, typval_T *rettv);
96static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +010097static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020098#endif
99static void f_confirm(typval_T *argvars, typval_T *rettv);
100static void f_copy(typval_T *argvars, typval_T *rettv);
101#ifdef FEAT_FLOAT
102static void f_cos(typval_T *argvars, typval_T *rettv);
103static void f_cosh(typval_T *argvars, typval_T *rettv);
104#endif
105static void f_count(typval_T *argvars, typval_T *rettv);
106static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
107static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100108#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200109static void f_debugbreak(typval_T *argvars, typval_T *rettv);
110#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200111static void f_deepcopy(typval_T *argvars, typval_T *rettv);
112static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200113static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200114static void f_did_filetype(typval_T *argvars, typval_T *rettv);
115static void f_diff_filler(typval_T *argvars, typval_T *rettv);
116static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
117static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200118static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119static void f_escape(typval_T *argvars, typval_T *rettv);
120static void f_eval(typval_T *argvars, typval_T *rettv);
121static void f_eventhandler(typval_T *argvars, typval_T *rettv);
122static void f_executable(typval_T *argvars, typval_T *rettv);
123static void f_execute(typval_T *argvars, typval_T *rettv);
124static void f_exepath(typval_T *argvars, typval_T *rettv);
125static void f_exists(typval_T *argvars, typval_T *rettv);
126#ifdef FEAT_FLOAT
127static void f_exp(typval_T *argvars, typval_T *rettv);
128#endif
129static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200130static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131static void f_extend(typval_T *argvars, typval_T *rettv);
132static void f_feedkeys(typval_T *argvars, typval_T *rettv);
133static void f_filereadable(typval_T *argvars, typval_T *rettv);
134static void f_filewritable(typval_T *argvars, typval_T *rettv);
135static void f_filter(typval_T *argvars, typval_T *rettv);
136static void f_finddir(typval_T *argvars, typval_T *rettv);
137static void f_findfile(typval_T *argvars, typval_T *rettv);
138#ifdef FEAT_FLOAT
139static void f_float2nr(typval_T *argvars, typval_T *rettv);
140static void f_floor(typval_T *argvars, typval_T *rettv);
141static void f_fmod(typval_T *argvars, typval_T *rettv);
142#endif
143static void f_fnameescape(typval_T *argvars, typval_T *rettv);
144static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
145static void f_foldclosed(typval_T *argvars, typval_T *rettv);
146static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
147static void f_foldlevel(typval_T *argvars, typval_T *rettv);
148static void f_foldtext(typval_T *argvars, typval_T *rettv);
149static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
150static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200151static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200152static void f_function(typval_T *argvars, typval_T *rettv);
153static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
154static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200155static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200156static void f_getbufline(typval_T *argvars, typval_T *rettv);
157static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100158static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200159static void f_getchar(typval_T *argvars, typval_T *rettv);
160static void f_getcharmod(typval_T *argvars, typval_T *rettv);
161static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
162static void f_getcmdline(typval_T *argvars, typval_T *rettv);
163#if defined(FEAT_CMDL_COMPL)
164static void f_getcompletion(typval_T *argvars, typval_T *rettv);
165#endif
166static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
167static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
168static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
169static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200170static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200171static void f_getfontname(typval_T *argvars, typval_T *rettv);
172static void f_getfperm(typval_T *argvars, typval_T *rettv);
173static void f_getfsize(typval_T *argvars, typval_T *rettv);
174static void f_getftime(typval_T *argvars, typval_T *rettv);
175static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100176static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200178static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179static void f_getmatches(typval_T *argvars, typval_T *rettv);
180static void f_getpid(typval_T *argvars, typval_T *rettv);
181static void f_getcurpos(typval_T *argvars, typval_T *rettv);
182static void f_getpos(typval_T *argvars, typval_T *rettv);
183static void f_getqflist(typval_T *argvars, typval_T *rettv);
184static void f_getreg(typval_T *argvars, typval_T *rettv);
185static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200186static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200187static void f_gettabvar(typval_T *argvars, typval_T *rettv);
188static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100189static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200190static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100191static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getwinposx(typval_T *argvars, typval_T *rettv);
193static void f_getwinposy(typval_T *argvars, typval_T *rettv);
194static void f_getwinvar(typval_T *argvars, typval_T *rettv);
195static void f_glob(typval_T *argvars, typval_T *rettv);
196static void f_globpath(typval_T *argvars, typval_T *rettv);
197static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
198static void f_has(typval_T *argvars, typval_T *rettv);
199static void f_has_key(typval_T *argvars, typval_T *rettv);
200static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
201static void f_hasmapto(typval_T *argvars, typval_T *rettv);
202static void f_histadd(typval_T *argvars, typval_T *rettv);
203static void f_histdel(typval_T *argvars, typval_T *rettv);
204static void f_histget(typval_T *argvars, typval_T *rettv);
205static void f_histnr(typval_T *argvars, typval_T *rettv);
206static void f_hlID(typval_T *argvars, typval_T *rettv);
207static void f_hlexists(typval_T *argvars, typval_T *rettv);
208static void f_hostname(typval_T *argvars, typval_T *rettv);
209static void f_iconv(typval_T *argvars, typval_T *rettv);
210static void f_indent(typval_T *argvars, typval_T *rettv);
211static void f_index(typval_T *argvars, typval_T *rettv);
212static void f_input(typval_T *argvars, typval_T *rettv);
213static void f_inputdialog(typval_T *argvars, typval_T *rettv);
214static void f_inputlist(typval_T *argvars, typval_T *rettv);
215static void f_inputrestore(typval_T *argvars, typval_T *rettv);
216static void f_inputsave(typval_T *argvars, typval_T *rettv);
217static void f_inputsecret(typval_T *argvars, typval_T *rettv);
218static void f_insert(typval_T *argvars, typval_T *rettv);
219static void f_invert(typval_T *argvars, typval_T *rettv);
220static void f_isdirectory(typval_T *argvars, typval_T *rettv);
221static void f_islocked(typval_T *argvars, typval_T *rettv);
222#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200223static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200224static void f_isnan(typval_T *argvars, typval_T *rettv);
225#endif
226static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200227static void f_join(typval_T *argvars, typval_T *rettv);
228static void f_js_decode(typval_T *argvars, typval_T *rettv);
229static void f_js_encode(typval_T *argvars, typval_T *rettv);
230static void f_json_decode(typval_T *argvars, typval_T *rettv);
231static void f_json_encode(typval_T *argvars, typval_T *rettv);
232static void f_keys(typval_T *argvars, typval_T *rettv);
233static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
234static void f_len(typval_T *argvars, typval_T *rettv);
235static void f_libcall(typval_T *argvars, typval_T *rettv);
236static void f_libcallnr(typval_T *argvars, typval_T *rettv);
237static void f_line(typval_T *argvars, typval_T *rettv);
238static void f_line2byte(typval_T *argvars, typval_T *rettv);
239static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200240static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200241static void f_localtime(typval_T *argvars, typval_T *rettv);
242#ifdef FEAT_FLOAT
243static void f_log(typval_T *argvars, typval_T *rettv);
244static void f_log10(typval_T *argvars, typval_T *rettv);
245#endif
246#ifdef FEAT_LUA
247static void f_luaeval(typval_T *argvars, typval_T *rettv);
248#endif
249static void f_map(typval_T *argvars, typval_T *rettv);
250static void f_maparg(typval_T *argvars, typval_T *rettv);
251static void f_mapcheck(typval_T *argvars, typval_T *rettv);
252static void f_match(typval_T *argvars, typval_T *rettv);
253static void f_matchadd(typval_T *argvars, typval_T *rettv);
254static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
255static void f_matcharg(typval_T *argvars, typval_T *rettv);
256static void f_matchdelete(typval_T *argvars, typval_T *rettv);
257static void f_matchend(typval_T *argvars, typval_T *rettv);
258static void f_matchlist(typval_T *argvars, typval_T *rettv);
259static void f_matchstr(typval_T *argvars, typval_T *rettv);
260static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
261static void f_max(typval_T *argvars, typval_T *rettv);
262static void f_min(typval_T *argvars, typval_T *rettv);
263#ifdef vim_mkdir
264static void f_mkdir(typval_T *argvars, typval_T *rettv);
265#endif
266static void f_mode(typval_T *argvars, typval_T *rettv);
267#ifdef FEAT_MZSCHEME
268static void f_mzeval(typval_T *argvars, typval_T *rettv);
269#endif
270static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
271static void f_nr2char(typval_T *argvars, typval_T *rettv);
272static void f_or(typval_T *argvars, typval_T *rettv);
273static void f_pathshorten(typval_T *argvars, typval_T *rettv);
274#ifdef FEAT_PERL
275static void f_perleval(typval_T *argvars, typval_T *rettv);
276#endif
277#ifdef FEAT_FLOAT
278static void f_pow(typval_T *argvars, typval_T *rettv);
279#endif
280static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
281static void f_printf(typval_T *argvars, typval_T *rettv);
282static void f_pumvisible(typval_T *argvars, typval_T *rettv);
283#ifdef FEAT_PYTHON3
284static void f_py3eval(typval_T *argvars, typval_T *rettv);
285#endif
286#ifdef FEAT_PYTHON
287static void f_pyeval(typval_T *argvars, typval_T *rettv);
288#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100289#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
290static void f_pyxeval(typval_T *argvars, typval_T *rettv);
291#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200292static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200293static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200294static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200295static void f_reg_executing(typval_T *argvars, typval_T *rettv);
296static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200297static void f_reltime(typval_T *argvars, typval_T *rettv);
298#ifdef FEAT_FLOAT
299static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
300#endif
301static void f_reltimestr(typval_T *argvars, typval_T *rettv);
302static void f_remote_expr(typval_T *argvars, typval_T *rettv);
303static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
304static void f_remote_peek(typval_T *argvars, typval_T *rettv);
305static void f_remote_read(typval_T *argvars, typval_T *rettv);
306static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100307static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200308static void f_remove(typval_T *argvars, typval_T *rettv);
309static void f_rename(typval_T *argvars, typval_T *rettv);
310static void f_repeat(typval_T *argvars, typval_T *rettv);
311static void f_resolve(typval_T *argvars, typval_T *rettv);
312static void f_reverse(typval_T *argvars, typval_T *rettv);
313#ifdef FEAT_FLOAT
314static void f_round(typval_T *argvars, typval_T *rettv);
315#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100316#ifdef FEAT_RUBY
317static void f_rubyeval(typval_T *argvars, typval_T *rettv);
318#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200319static void f_screenattr(typval_T *argvars, typval_T *rettv);
320static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100321static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200322static void f_screencol(typval_T *argvars, typval_T *rettv);
323static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100324static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200325static void f_search(typval_T *argvars, typval_T *rettv);
326static void f_searchdecl(typval_T *argvars, typval_T *rettv);
327static void f_searchpair(typval_T *argvars, typval_T *rettv);
328static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
329static void f_searchpos(typval_T *argvars, typval_T *rettv);
330static void f_server2client(typval_T *argvars, typval_T *rettv);
331static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200332static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200333static void f_setbufvar(typval_T *argvars, typval_T *rettv);
334static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
335static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200336static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200337static void f_setfperm(typval_T *argvars, typval_T *rettv);
338static void f_setline(typval_T *argvars, typval_T *rettv);
339static void f_setloclist(typval_T *argvars, typval_T *rettv);
340static void f_setmatches(typval_T *argvars, typval_T *rettv);
341static void f_setpos(typval_T *argvars, typval_T *rettv);
342static void f_setqflist(typval_T *argvars, typval_T *rettv);
343static void f_setreg(typval_T *argvars, typval_T *rettv);
344static void f_settabvar(typval_T *argvars, typval_T *rettv);
345static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100346static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200347static void f_setwinvar(typval_T *argvars, typval_T *rettv);
348#ifdef FEAT_CRYPT
349static void f_sha256(typval_T *argvars, typval_T *rettv);
350#endif /* FEAT_CRYPT */
351static void f_shellescape(typval_T *argvars, typval_T *rettv);
352static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
353static void f_simplify(typval_T *argvars, typval_T *rettv);
354#ifdef FEAT_FLOAT
355static void f_sin(typval_T *argvars, typval_T *rettv);
356static void f_sinh(typval_T *argvars, typval_T *rettv);
357#endif
358static void f_sort(typval_T *argvars, typval_T *rettv);
359static void f_soundfold(typval_T *argvars, typval_T *rettv);
360static void f_spellbadword(typval_T *argvars, typval_T *rettv);
361static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
362static void f_split(typval_T *argvars, typval_T *rettv);
363#ifdef FEAT_FLOAT
364static void f_sqrt(typval_T *argvars, typval_T *rettv);
365static void f_str2float(typval_T *argvars, typval_T *rettv);
366#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200367static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200368static void f_str2nr(typval_T *argvars, typval_T *rettv);
369static void f_strchars(typval_T *argvars, typval_T *rettv);
370#ifdef HAVE_STRFTIME
371static void f_strftime(typval_T *argvars, typval_T *rettv);
372#endif
373static void f_strgetchar(typval_T *argvars, typval_T *rettv);
374static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200375static void f_strlen(typval_T *argvars, typval_T *rettv);
376static void f_strcharpart(typval_T *argvars, typval_T *rettv);
377static void f_strpart(typval_T *argvars, typval_T *rettv);
378static void f_strridx(typval_T *argvars, typval_T *rettv);
379static void f_strtrans(typval_T *argvars, typval_T *rettv);
380static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
381static void f_strwidth(typval_T *argvars, typval_T *rettv);
382static void f_submatch(typval_T *argvars, typval_T *rettv);
383static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200384static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200385static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200386static void f_synID(typval_T *argvars, typval_T *rettv);
387static void f_synIDattr(typval_T *argvars, typval_T *rettv);
388static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
389static void f_synstack(typval_T *argvars, typval_T *rettv);
390static void f_synconcealed(typval_T *argvars, typval_T *rettv);
391static void f_system(typval_T *argvars, typval_T *rettv);
392static void f_systemlist(typval_T *argvars, typval_T *rettv);
393static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
394static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
395static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
396static void f_taglist(typval_T *argvars, typval_T *rettv);
397static void f_tagfiles(typval_T *argvars, typval_T *rettv);
398static void f_tempname(typval_T *argvars, typval_T *rettv);
399static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
400static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200401static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareda65222019-05-16 20:29:44 +0200402static void f_test_getvalue(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200403static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100404static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100405static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200406static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaaradc67142019-06-22 01:40:42 +0200407static void f_test_garbagecollect_soon(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100408static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100409static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200410#ifdef FEAT_JOB_CHANNEL
411static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
412#endif
413static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
414#ifdef FEAT_JOB_CHANNEL
415static void f_test_null_job(typval_T *argvars, typval_T *rettv);
416#endif
417static void f_test_null_list(typval_T *argvars, typval_T *rettv);
418static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
419static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200420#ifdef FEAT_GUI
421static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
422#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200423#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200424static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200425#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200426static void f_test_settime(typval_T *argvars, typval_T *rettv);
427#ifdef FEAT_FLOAT
428static void f_tan(typval_T *argvars, typval_T *rettv);
429static void f_tanh(typval_T *argvars, typval_T *rettv);
430#endif
431#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200432static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200433static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200434static void f_timer_start(typval_T *argvars, typval_T *rettv);
435static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200436static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200437#endif
438static void f_tolower(typval_T *argvars, typval_T *rettv);
439static void f_toupper(typval_T *argvars, typval_T *rettv);
440static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100441static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200442#ifdef FEAT_FLOAT
443static void f_trunc(typval_T *argvars, typval_T *rettv);
444#endif
445static void f_type(typval_T *argvars, typval_T *rettv);
446static void f_undofile(typval_T *argvars, typval_T *rettv);
447static void f_undotree(typval_T *argvars, typval_T *rettv);
448static void f_uniq(typval_T *argvars, typval_T *rettv);
449static void f_values(typval_T *argvars, typval_T *rettv);
450static void f_virtcol(typval_T *argvars, typval_T *rettv);
451static void f_visualmode(typval_T *argvars, typval_T *rettv);
452static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200453static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200454static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
455static void f_win_getid(typval_T *argvars, typval_T *rettv);
456static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
457static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
458static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100459static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200460static void f_winbufnr(typval_T *argvars, typval_T *rettv);
461static void f_wincol(typval_T *argvars, typval_T *rettv);
462static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200463static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200464static void f_winline(typval_T *argvars, typval_T *rettv);
465static void f_winnr(typval_T *argvars, typval_T *rettv);
466static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
467static void f_winrestview(typval_T *argvars, typval_T *rettv);
468static void f_winsaveview(typval_T *argvars, typval_T *rettv);
469static void f_winwidth(typval_T *argvars, typval_T *rettv);
470static void f_writefile(typval_T *argvars, typval_T *rettv);
471static void f_wordcount(typval_T *argvars, typval_T *rettv);
472static void f_xor(typval_T *argvars, typval_T *rettv);
473
474/*
475 * Array with names and number of arguments of all internal functions
476 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
477 */
478static struct fst
479{
480 char *f_name; /* function name */
481 char f_min_argc; /* minimal number of arguments */
482 char f_max_argc; /* maximal number of arguments */
483 void (*f_func)(typval_T *args, typval_T *rvar);
484 /* implementation of function */
485} functions[] =
486{
487#ifdef FEAT_FLOAT
488 {"abs", 1, 1, f_abs},
489 {"acos", 1, 1, f_acos}, /* WJMc */
490#endif
491 {"add", 2, 2, f_add},
492 {"and", 2, 2, f_and},
493 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200494 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200495 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200496 {"argidx", 0, 0, f_argidx},
497 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200498 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200499#ifdef FEAT_FLOAT
500 {"asin", 1, 1, f_asin}, /* WJMc */
501#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100502 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200503 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100504 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200506 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100508 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200509 {"assert_match", 2, 3, f_assert_match},
510 {"assert_notequal", 2, 3, f_assert_notequal},
511 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100512 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200513 {"assert_true", 1, 2, f_assert_true},
514#ifdef FEAT_FLOAT
515 {"atan", 1, 1, f_atan},
516 {"atan2", 2, 2, f_atan2},
517#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100518#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200519 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100520 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100521# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100522 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100523# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100524#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200525 {"browse", 4, 4, f_browse},
526 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200527 {"bufadd", 1, 1, f_bufadd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200528 {"bufexists", 1, 1, f_bufexists},
529 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
530 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
531 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
532 {"buflisted", 1, 1, f_buflisted},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200533 {"bufload", 1, 1, f_bufload},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200534 {"bufloaded", 1, 1, f_bufloaded},
535 {"bufname", 1, 1, f_bufname},
536 {"bufnr", 1, 2, f_bufnr},
537 {"bufwinid", 1, 1, f_bufwinid},
538 {"bufwinnr", 1, 1, f_bufwinnr},
539 {"byte2line", 1, 1, f_byte2line},
540 {"byteidx", 2, 2, f_byteidx},
541 {"byteidxcomp", 2, 2, f_byteidxcomp},
542 {"call", 2, 3, f_call},
543#ifdef FEAT_FLOAT
544 {"ceil", 1, 1, f_ceil},
545#endif
546#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100547 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200549 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
551 {"ch_evalraw", 2, 3, f_ch_evalraw},
552 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
553 {"ch_getjob", 1, 1, f_ch_getjob},
554 {"ch_info", 1, 1, f_ch_info},
555 {"ch_log", 1, 2, f_ch_log},
556 {"ch_logfile", 1, 2, f_ch_logfile},
557 {"ch_open", 1, 2, f_ch_open},
558 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100559 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200560 {"ch_readraw", 1, 2, f_ch_readraw},
561 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
562 {"ch_sendraw", 2, 3, f_ch_sendraw},
563 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200564 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200565#endif
566 {"changenr", 0, 0, f_changenr},
567 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200568 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200569 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100570 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200571 {"col", 1, 1, f_col},
572#if defined(FEAT_INS_EXPAND)
573 {"complete", 2, 2, f_complete},
574 {"complete_add", 1, 1, f_complete_add},
575 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100576 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200577#endif
578 {"confirm", 1, 4, f_confirm},
579 {"copy", 1, 1, f_copy},
580#ifdef FEAT_FLOAT
581 {"cos", 1, 1, f_cos},
582 {"cosh", 1, 1, f_cosh},
583#endif
584 {"count", 2, 4, f_count},
585 {"cscope_connection",0,3, f_cscope_connection},
586 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100587#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200588 {"debugbreak", 1, 1, f_debugbreak},
589#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200590 {"deepcopy", 1, 2, f_deepcopy},
591 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200592 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200593 {"did_filetype", 0, 0, f_did_filetype},
594 {"diff_filler", 1, 1, f_diff_filler},
595 {"diff_hlID", 2, 2, f_diff_hlID},
596 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200597 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200598 {"escape", 2, 2, f_escape},
599 {"eval", 1, 1, f_eval},
600 {"eventhandler", 0, 0, f_eventhandler},
601 {"executable", 1, 1, f_executable},
602 {"execute", 1, 2, f_execute},
603 {"exepath", 1, 1, f_exepath},
604 {"exists", 1, 1, f_exists},
605#ifdef FEAT_FLOAT
606 {"exp", 1, 1, f_exp},
607#endif
608 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200609 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610 {"extend", 2, 3, f_extend},
611 {"feedkeys", 1, 2, f_feedkeys},
612 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
613 {"filereadable", 1, 1, f_filereadable},
614 {"filewritable", 1, 1, f_filewritable},
615 {"filter", 2, 2, f_filter},
616 {"finddir", 1, 3, f_finddir},
617 {"findfile", 1, 3, f_findfile},
618#ifdef FEAT_FLOAT
619 {"float2nr", 1, 1, f_float2nr},
620 {"floor", 1, 1, f_floor},
621 {"fmod", 2, 2, f_fmod},
622#endif
623 {"fnameescape", 1, 1, f_fnameescape},
624 {"fnamemodify", 2, 2, f_fnamemodify},
625 {"foldclosed", 1, 1, f_foldclosed},
626 {"foldclosedend", 1, 1, f_foldclosedend},
627 {"foldlevel", 1, 1, f_foldlevel},
628 {"foldtext", 0, 0, f_foldtext},
629 {"foldtextresult", 1, 1, f_foldtextresult},
630 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200631 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"function", 1, 3, f_function},
633 {"garbagecollect", 0, 1, f_garbagecollect},
634 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200635 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636 {"getbufline", 2, 3, f_getbufline},
637 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100638 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"getchar", 0, 1, f_getchar},
640 {"getcharmod", 0, 0, f_getcharmod},
641 {"getcharsearch", 0, 0, f_getcharsearch},
642 {"getcmdline", 0, 0, f_getcmdline},
643 {"getcmdpos", 0, 0, f_getcmdpos},
644 {"getcmdtype", 0, 0, f_getcmdtype},
645 {"getcmdwintype", 0, 0, f_getcmdwintype},
646#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200647 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200648#endif
649 {"getcurpos", 0, 0, f_getcurpos},
650 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200651 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200652 {"getfontname", 0, 1, f_getfontname},
653 {"getfperm", 1, 1, f_getfperm},
654 {"getfsize", 1, 1, f_getfsize},
655 {"getftime", 1, 1, f_getftime},
656 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100657 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200658 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200659 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100660 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200661 {"getpid", 0, 0, f_getpid},
662 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200663 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200664 {"getreg", 0, 3, f_getreg},
665 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200666 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200667 {"gettabvar", 2, 3, f_gettabvar},
668 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100669 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200670 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100671 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200672 {"getwinposx", 0, 0, f_getwinposx},
673 {"getwinposy", 0, 0, f_getwinposy},
674 {"getwinvar", 2, 3, f_getwinvar},
675 {"glob", 1, 4, f_glob},
676 {"glob2regpat", 1, 1, f_glob2regpat},
677 {"globpath", 2, 5, f_globpath},
678 {"has", 1, 1, f_has},
679 {"has_key", 2, 2, f_has_key},
680 {"haslocaldir", 0, 2, f_haslocaldir},
681 {"hasmapto", 1, 3, f_hasmapto},
682 {"highlightID", 1, 1, f_hlID}, /* obsolete */
683 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
684 {"histadd", 2, 2, f_histadd},
685 {"histdel", 1, 2, f_histdel},
686 {"histget", 1, 2, f_histget},
687 {"histnr", 1, 1, f_histnr},
688 {"hlID", 1, 1, f_hlID},
689 {"hlexists", 1, 1, f_hlexists},
690 {"hostname", 0, 0, f_hostname},
691 {"iconv", 3, 3, f_iconv},
692 {"indent", 1, 1, f_indent},
693 {"index", 2, 4, f_index},
694 {"input", 1, 3, f_input},
695 {"inputdialog", 1, 3, f_inputdialog},
696 {"inputlist", 1, 1, f_inputlist},
697 {"inputrestore", 0, 0, f_inputrestore},
698 {"inputsave", 0, 0, f_inputsave},
699 {"inputsecret", 1, 2, f_inputsecret},
700 {"insert", 2, 3, f_insert},
701 {"invert", 1, 1, f_invert},
702 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200703#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
704 {"isinf", 1, 1, f_isinf},
705#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200706 {"islocked", 1, 1, f_islocked},
707#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
708 {"isnan", 1, 1, f_isnan},
709#endif
710 {"items", 1, 1, f_items},
711#ifdef FEAT_JOB_CHANNEL
712 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200713 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200714 {"job_setoptions", 2, 2, f_job_setoptions},
715 {"job_start", 1, 2, f_job_start},
716 {"job_status", 1, 1, f_job_status},
717 {"job_stop", 1, 2, f_job_stop},
718#endif
719 {"join", 1, 2, f_join},
720 {"js_decode", 1, 1, f_js_decode},
721 {"js_encode", 1, 1, f_js_encode},
722 {"json_decode", 1, 1, f_json_decode},
723 {"json_encode", 1, 1, f_json_encode},
724 {"keys", 1, 1, f_keys},
725 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
726 {"len", 1, 1, f_len},
727 {"libcall", 3, 3, f_libcall},
728 {"libcallnr", 3, 3, f_libcallnr},
729 {"line", 1, 1, f_line},
730 {"line2byte", 1, 1, f_line2byte},
731 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200732 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200733 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200734 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200735 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200736 {"localtime", 0, 0, f_localtime},
737#ifdef FEAT_FLOAT
738 {"log", 1, 1, f_log},
739 {"log10", 1, 1, f_log10},
740#endif
741#ifdef FEAT_LUA
742 {"luaeval", 1, 2, f_luaeval},
743#endif
744 {"map", 2, 2, f_map},
745 {"maparg", 1, 4, f_maparg},
746 {"mapcheck", 1, 3, f_mapcheck},
747 {"match", 2, 4, f_match},
748 {"matchadd", 2, 5, f_matchadd},
749 {"matchaddpos", 2, 5, f_matchaddpos},
750 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100751 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200752 {"matchend", 2, 4, f_matchend},
753 {"matchlist", 2, 4, f_matchlist},
754 {"matchstr", 2, 4, f_matchstr},
755 {"matchstrpos", 2, 4, f_matchstrpos},
756 {"max", 1, 1, f_max},
757 {"min", 1, 1, f_min},
758#ifdef vim_mkdir
759 {"mkdir", 1, 3, f_mkdir},
760#endif
761 {"mode", 0, 1, f_mode},
762#ifdef FEAT_MZSCHEME
763 {"mzeval", 1, 1, f_mzeval},
764#endif
765 {"nextnonblank", 1, 1, f_nextnonblank},
766 {"nr2char", 1, 2, f_nr2char},
767 {"or", 2, 2, f_or},
768 {"pathshorten", 1, 1, f_pathshorten},
769#ifdef FEAT_PERL
770 {"perleval", 1, 1, f_perleval},
771#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200772#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200773 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200774 {"popup_beval", 2, 2, f_popup_beval},
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200775 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200776 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200777 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200778 {"popup_dialog", 2, 2, f_popup_dialog},
Bram Moolenaara730e552019-06-16 19:05:31 +0200779 {"popup_filter_menu", 2, 2, f_popup_filter_menu},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200780 {"popup_filter_yesno", 2, 2, f_popup_filter_yesno},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200781 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200782 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200783 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaara730e552019-06-16 19:05:31 +0200784 {"popup_menu", 2, 2, f_popup_menu},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200785 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200786 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaarae943152019-06-16 22:54:14 +0200787 {"popup_setoptions", 2, 2, f_popup_setoptions},
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200788 {"popup_settext", 2, 2, f_popup_settext},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200789 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200790#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200791#ifdef FEAT_FLOAT
792 {"pow", 2, 2, f_pow},
793#endif
794 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100795 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200796#ifdef FEAT_JOB_CHANNEL
797 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200798 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200799 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
800#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100801#ifdef FEAT_TEXT_PROP
802 {"prop_add", 3, 3, f_prop_add},
803 {"prop_clear", 1, 3, f_prop_clear},
804 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100805 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100806 {"prop_type_add", 2, 2, f_prop_type_add},
807 {"prop_type_change", 2, 2, f_prop_type_change},
808 {"prop_type_delete", 1, 2, f_prop_type_delete},
809 {"prop_type_get", 1, 2, f_prop_type_get},
810 {"prop_type_list", 0, 1, f_prop_type_list},
811#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200812 {"pumvisible", 0, 0, f_pumvisible},
813#ifdef FEAT_PYTHON3
814 {"py3eval", 1, 1, f_py3eval},
815#endif
816#ifdef FEAT_PYTHON
817 {"pyeval", 1, 1, f_pyeval},
818#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100819#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
820 {"pyxeval", 1, 1, f_pyxeval},
821#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200822 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200823 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200824 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200825 {"reg_executing", 0, 0, f_reg_executing},
826 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200827 {"reltime", 0, 2, f_reltime},
828#ifdef FEAT_FLOAT
829 {"reltimefloat", 1, 1, f_reltimefloat},
830#endif
831 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100832 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200833 {"remote_foreground", 1, 1, f_remote_foreground},
834 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100835 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200836 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100837 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200838 {"remove", 2, 3, f_remove},
839 {"rename", 2, 2, f_rename},
840 {"repeat", 2, 2, f_repeat},
841 {"resolve", 1, 1, f_resolve},
842 {"reverse", 1, 1, f_reverse},
843#ifdef FEAT_FLOAT
844 {"round", 1, 1, f_round},
845#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100846#ifdef FEAT_RUBY
847 {"rubyeval", 1, 1, f_rubyeval},
848#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200849 {"screenattr", 2, 2, f_screenattr},
850 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100851 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"screencol", 0, 0, f_screencol},
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200853 {"screenpos", 3, 3, f_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200854 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100855 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200856 {"search", 1, 4, f_search},
857 {"searchdecl", 1, 3, f_searchdecl},
858 {"searchpair", 3, 7, f_searchpair},
859 {"searchpairpos", 3, 7, f_searchpairpos},
860 {"searchpos", 1, 4, f_searchpos},
861 {"server2client", 2, 2, f_server2client},
862 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200863 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200864 {"setbufvar", 3, 3, f_setbufvar},
865 {"setcharsearch", 1, 1, f_setcharsearch},
866 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200867 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200868 {"setfperm", 2, 2, f_setfperm},
869 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200870 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100871 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200872 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200873 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200874 {"setreg", 2, 3, f_setreg},
875 {"settabvar", 3, 3, f_settabvar},
876 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100877 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200878 {"setwinvar", 3, 3, f_setwinvar},
879#ifdef FEAT_CRYPT
880 {"sha256", 1, 1, f_sha256},
881#endif
882 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100883 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100884#ifdef FEAT_SIGNS
885 {"sign_define", 1, 2, f_sign_define},
886 {"sign_getdefined", 0, 1, f_sign_getdefined},
887 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100888 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100889 {"sign_place", 4, 5, f_sign_place},
890 {"sign_undefine", 0, 1, f_sign_undefine},
891 {"sign_unplace", 1, 2, f_sign_unplace},
892#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200893 {"simplify", 1, 1, f_simplify},
894#ifdef FEAT_FLOAT
895 {"sin", 1, 1, f_sin},
896 {"sinh", 1, 1, f_sinh},
897#endif
898 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200899#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200900 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200901 {"sound_playevent", 1, 2, f_sound_playevent},
902 {"sound_playfile", 1, 2, f_sound_playfile},
903 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200904#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200905 {"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 Moolenaaradc67142019-06-22 01:40:42 +0200983 {"test_garbagecollect_soon", 0, 0, f_test_garbagecollect_soon},
Bram Moolenaareda65222019-05-16 20:29:44 +0200984 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100985 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100986 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200987#ifdef FEAT_JOB_CHANNEL
988 {"test_null_channel", 0, 0, f_test_null_channel},
989#endif
990 {"test_null_dict", 0, 0, f_test_null_dict},
991#ifdef FEAT_JOB_CHANNEL
992 {"test_null_job", 0, 0, f_test_null_job},
993#endif
994 {"test_null_list", 0, 0, f_test_null_list},
995 {"test_null_partial", 0, 0, f_test_null_partial},
996 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200997 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100998 {"test_override", 2, 2, f_test_override},
999 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001000#ifdef FEAT_GUI
1001 {"test_scrollbar", 3, 3, f_test_scrollbar},
1002#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001003#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001004 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001005#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001006 {"test_settime", 1, 1, f_test_settime},
1007#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001008 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001009 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001010 {"timer_start", 2, 3, f_timer_start},
1011 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001012 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001013#endif
1014 {"tolower", 1, 1, f_tolower},
1015 {"toupper", 1, 1, f_toupper},
1016 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001017 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001018#ifdef FEAT_FLOAT
1019 {"trunc", 1, 1, f_trunc},
1020#endif
1021 {"type", 1, 1, f_type},
1022 {"undofile", 1, 1, f_undofile},
1023 {"undotree", 0, 0, f_undotree},
1024 {"uniq", 1, 3, f_uniq},
1025 {"values", 1, 1, f_values},
1026 {"virtcol", 1, 1, f_virtcol},
1027 {"visualmode", 0, 1, f_visualmode},
1028 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001029 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001030 {"win_findbuf", 1, 1, f_win_findbuf},
1031 {"win_getid", 0, 2, f_win_getid},
1032 {"win_gotoid", 1, 1, f_win_gotoid},
1033 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1034 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001035 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001036 {"winbufnr", 1, 1, f_winbufnr},
1037 {"wincol", 0, 0, f_wincol},
1038 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001039 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001040 {"winline", 0, 0, f_winline},
1041 {"winnr", 0, 1, f_winnr},
1042 {"winrestcmd", 0, 0, f_winrestcmd},
1043 {"winrestview", 1, 1, f_winrestview},
1044 {"winsaveview", 0, 0, f_winsaveview},
1045 {"winwidth", 1, 1, f_winwidth},
1046 {"wordcount", 0, 0, f_wordcount},
1047 {"writefile", 2, 3, f_writefile},
1048 {"xor", 2, 2, f_xor},
1049};
1050
1051#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1052
1053/*
1054 * Function given to ExpandGeneric() to obtain the list of internal
1055 * or user defined function names.
1056 */
1057 char_u *
1058get_function_name(expand_T *xp, int idx)
1059{
1060 static int intidx = -1;
1061 char_u *name;
1062
1063 if (idx == 0)
1064 intidx = -1;
1065 if (intidx < 0)
1066 {
1067 name = get_user_func_name(xp, idx);
1068 if (name != NULL)
1069 return name;
1070 }
1071 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1072 {
1073 STRCPY(IObuff, functions[intidx].f_name);
1074 STRCAT(IObuff, "(");
1075 if (functions[intidx].f_max_argc == 0)
1076 STRCAT(IObuff, ")");
1077 return IObuff;
1078 }
1079
1080 return NULL;
1081}
1082
1083/*
1084 * Function given to ExpandGeneric() to obtain the list of internal or
1085 * user defined variable or function names.
1086 */
1087 char_u *
1088get_expr_name(expand_T *xp, int idx)
1089{
1090 static int intidx = -1;
1091 char_u *name;
1092
1093 if (idx == 0)
1094 intidx = -1;
1095 if (intidx < 0)
1096 {
1097 name = get_function_name(xp, idx);
1098 if (name != NULL)
1099 return name;
1100 }
1101 return get_user_var_name(xp, ++intidx);
1102}
1103
1104#endif /* FEAT_CMDL_COMPL */
1105
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001106/*
1107 * Find internal function in table above.
1108 * Return index, or -1 if not found
1109 */
1110 int
1111find_internal_func(
1112 char_u *name) /* name of the function */
1113{
1114 int first = 0;
1115 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1116 int cmp;
1117 int x;
1118
1119 /*
1120 * Find the function name in the table. Binary search.
1121 */
1122 while (first <= last)
1123 {
1124 x = first + ((unsigned)(last - first) >> 1);
1125 cmp = STRCMP(name, functions[x].f_name);
1126 if (cmp < 0)
1127 last = x - 1;
1128 else if (cmp > 0)
1129 first = x + 1;
1130 else
1131 return x;
1132 }
1133 return -1;
1134}
1135
1136 int
1137call_internal_func(
1138 char_u *name,
1139 int argcount,
1140 typval_T *argvars,
1141 typval_T *rettv)
1142{
1143 int i;
1144
1145 i = find_internal_func(name);
1146 if (i < 0)
1147 return ERROR_UNKNOWN;
1148 if (argcount < functions[i].f_min_argc)
1149 return ERROR_TOOFEW;
1150 if (argcount > functions[i].f_max_argc)
1151 return ERROR_TOOMANY;
1152 argvars[argcount].v_type = VAR_UNKNOWN;
1153 functions[i].f_func(argvars, rettv);
1154 return ERROR_NONE;
1155}
1156
1157/*
1158 * Return TRUE for a non-zero Number and a non-empty String.
1159 */
1160 static int
1161non_zero_arg(typval_T *argvars)
1162{
1163 return ((argvars[0].v_type == VAR_NUMBER
1164 && argvars[0].vval.v_number != 0)
1165 || (argvars[0].v_type == VAR_SPECIAL
1166 && argvars[0].vval.v_number == VVAL_TRUE)
1167 || (argvars[0].v_type == VAR_STRING
1168 && argvars[0].vval.v_string != NULL
1169 && *argvars[0].vval.v_string != NUL));
1170}
1171
1172/*
1173 * Get the lnum from the first argument.
1174 * Also accepts ".", "$", etc., but that only works for the current buffer.
1175 * Returns -1 on error.
1176 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001177 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001178tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001179{
1180 typval_T rettv;
1181 linenr_T lnum;
1182
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001183 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001184 if (lnum == 0) /* no valid number, try using line() */
1185 {
1186 rettv.v_type = VAR_NUMBER;
1187 f_line(argvars, &rettv);
1188 lnum = (linenr_T)rettv.vval.v_number;
1189 clear_tv(&rettv);
1190 }
1191 return lnum;
1192}
1193
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001194/*
1195 * Get the lnum from the first argument.
1196 * Also accepts "$", then "buf" is used.
1197 * Returns 0 on error.
1198 */
1199 static linenr_T
1200tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1201{
1202 if (argvars[0].v_type == VAR_STRING
1203 && argvars[0].vval.v_string != NULL
1204 && argvars[0].vval.v_string[0] == '$'
1205 && buf != NULL)
1206 return buf->b_ml.ml_line_count;
1207 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1208}
1209
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001210#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001211/*
1212 * Get the float value of "argvars[0]" into "f".
1213 * Returns FAIL when the argument is not a Number or Float.
1214 */
1215 static int
1216get_float_arg(typval_T *argvars, float_T *f)
1217{
1218 if (argvars[0].v_type == VAR_FLOAT)
1219 {
1220 *f = argvars[0].vval.v_float;
1221 return OK;
1222 }
1223 if (argvars[0].v_type == VAR_NUMBER)
1224 {
1225 *f = (float_T)argvars[0].vval.v_number;
1226 return OK;
1227 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001228 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001229 return FAIL;
1230}
1231
1232/*
1233 * "abs(expr)" function
1234 */
1235 static void
1236f_abs(typval_T *argvars, typval_T *rettv)
1237{
1238 if (argvars[0].v_type == VAR_FLOAT)
1239 {
1240 rettv->v_type = VAR_FLOAT;
1241 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1242 }
1243 else
1244 {
1245 varnumber_T n;
1246 int error = FALSE;
1247
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001248 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001249 if (error)
1250 rettv->vval.v_number = -1;
1251 else if (n > 0)
1252 rettv->vval.v_number = n;
1253 else
1254 rettv->vval.v_number = -n;
1255 }
1256}
1257
1258/*
1259 * "acos()" function
1260 */
1261 static void
1262f_acos(typval_T *argvars, typval_T *rettv)
1263{
1264 float_T f = 0.0;
1265
1266 rettv->v_type = VAR_FLOAT;
1267 if (get_float_arg(argvars, &f) == OK)
1268 rettv->vval.v_float = acos(f);
1269 else
1270 rettv->vval.v_float = 0.0;
1271}
1272#endif
1273
1274/*
1275 * "add(list, item)" function
1276 */
1277 static void
1278f_add(typval_T *argvars, typval_T *rettv)
1279{
1280 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001281 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001282
1283 rettv->vval.v_number = 1; /* Default: Failed */
1284 if (argvars[0].v_type == VAR_LIST)
1285 {
1286 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001287 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001288 (char_u *)N_("add() argument"), TRUE)
1289 && list_append_tv(l, &argvars[1]) == OK)
1290 copy_tv(&argvars[0], rettv);
1291 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001292 else if (argvars[0].v_type == VAR_BLOB)
1293 {
1294 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001295 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001296 (char_u *)N_("add() argument"), TRUE))
1297 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001298 int error = FALSE;
1299 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1300
1301 if (!error)
1302 {
1303 ga_append(&b->bv_ga, (int)n);
1304 copy_tv(&argvars[0], rettv);
1305 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001306 }
1307 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001308 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001309 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001310}
1311
1312/*
1313 * "and(expr, expr)" function
1314 */
1315 static void
1316f_and(typval_T *argvars, typval_T *rettv)
1317{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001318 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1319 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001320}
1321
1322/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001323 * If there is a window for "curbuf", make it the current window.
1324 */
1325 static void
1326find_win_for_curbuf(void)
1327{
1328 wininfo_T *wip;
1329
1330 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1331 {
1332 if (wip->wi_win != NULL)
1333 {
1334 curwin = wip->wi_win;
1335 break;
1336 }
1337 }
1338}
1339
1340/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001341 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001342 */
1343 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001344set_buffer_lines(
1345 buf_T *buf,
1346 linenr_T lnum_arg,
1347 int append,
1348 typval_T *lines,
1349 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001350{
Bram Moolenaarca851592018-06-06 21:04:07 +02001351 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1352 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353 list_T *l = NULL;
1354 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001355 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001356 linenr_T append_lnum;
1357 buf_T *curbuf_save = NULL;
1358 win_T *curwin_save = NULL;
1359 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001360
Bram Moolenaarca851592018-06-06 21:04:07 +02001361 /* When using the current buffer ml_mfp will be set if needed. Useful when
1362 * setline() is used on startup. For other buffers the buffer must be
1363 * loaded. */
1364 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001366 rettv->vval.v_number = 1; /* FAIL */
1367 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001368 }
1369
Bram Moolenaarca851592018-06-06 21:04:07 +02001370 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001371 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001372 curbuf_save = curbuf;
1373 curwin_save = curwin;
1374 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001375 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001376 }
1377
1378 if (append)
1379 // appendbufline() uses the line number below which we insert
1380 append_lnum = lnum - 1;
1381 else
1382 // setbufline() uses the line number above which we insert, we only
1383 // append if it's below the last line
1384 append_lnum = curbuf->b_ml.ml_line_count;
1385
1386 if (lines->v_type == VAR_LIST)
1387 {
1388 l = lines->vval.v_list;
1389 li = l->lv_first;
1390 }
1391 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001392 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001393
1394 /* default result is zero == OK */
1395 for (;;)
1396 {
1397 if (l != NULL)
1398 {
1399 /* list argument, get next string */
1400 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001401 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001402 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001403 li = li->li_next;
1404 }
1405
Bram Moolenaarca851592018-06-06 21:04:07 +02001406 rettv->vval.v_number = 1; /* FAIL */
1407 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1408 break;
1409
1410 /* When coming here from Insert mode, sync undo, so that this can be
1411 * undone separately from what was previously inserted. */
1412 if (u_sync_once == 2)
1413 {
1414 u_sync_once = 1; /* notify that u_sync() was called */
1415 u_sync(TRUE);
1416 }
1417
1418 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1419 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001420 // Existing line, replace it.
1421 // Removes any existing text properties.
1422 if (u_savesub(lnum) == OK && ml_replace_len(
1423 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001424 {
1425 changed_bytes(lnum, 0);
1426 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1427 check_cursor_col();
1428 rettv->vval.v_number = 0; /* OK */
1429 }
1430 }
1431 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1432 {
1433 /* append the line */
1434 ++added;
1435 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1436 rettv->vval.v_number = 0; /* OK */
1437 }
1438
1439 if (l == NULL) /* only one string argument */
1440 break;
1441 ++lnum;
1442 }
1443
1444 if (added > 0)
1445 {
1446 win_T *wp;
1447 tabpage_T *tp;
1448
1449 appended_lines_mark(append_lnum, added);
1450 FOR_ALL_TAB_WINDOWS(tp, wp)
1451 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1452 wp->w_cursor.lnum += added;
1453 check_cursor_col();
1454
Bram Moolenaarf2732452018-06-03 14:47:35 +02001455#ifdef FEAT_JOB_CHANNEL
1456 if (bt_prompt(curbuf) && (State & INSERT))
1457 // show the line with the prompt
1458 update_topline();
1459#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001460 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001461
1462 if (!is_curbuf)
1463 {
1464 curbuf = curbuf_save;
1465 curwin = curwin_save;
1466 }
1467}
1468
1469/*
1470 * "append(lnum, string/list)" function
1471 */
1472 static void
1473f_append(typval_T *argvars, typval_T *rettv)
1474{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001475 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001476
1477 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1478}
1479
1480/*
1481 * "appendbufline(buf, lnum, string/list)" function
1482 */
1483 static void
1484f_appendbufline(typval_T *argvars, typval_T *rettv)
1485{
1486 linenr_T lnum;
1487 buf_T *buf;
1488
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001489 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001490 if (buf == NULL)
1491 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001492 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001493 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001494 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001495 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1496 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001497}
1498
1499/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001500 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001501 */
1502 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001503f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001504{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001505 win_T *wp;
1506
1507 if (argvars[0].v_type == VAR_UNKNOWN)
1508 // use the current window
1509 rettv->vval.v_number = ARGCOUNT;
1510 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001511 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001512 // use the global argument list
1513 rettv->vval.v_number = GARGCOUNT;
1514 else
1515 {
1516 // use the argument list of the specified window
1517 wp = find_win_by_nr_or_id(&argvars[0]);
1518 if (wp != NULL)
1519 rettv->vval.v_number = WARGCOUNT(wp);
1520 else
1521 rettv->vval.v_number = -1;
1522 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001523}
1524
1525/*
1526 * "argidx()" function
1527 */
1528 static void
1529f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1530{
1531 rettv->vval.v_number = curwin->w_arg_idx;
1532}
1533
1534/*
1535 * "arglistid()" function
1536 */
1537 static void
1538f_arglistid(typval_T *argvars, typval_T *rettv)
1539{
1540 win_T *wp;
1541
1542 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001543 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001544 if (wp != NULL)
1545 rettv->vval.v_number = wp->w_alist->id;
1546}
1547
1548/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001549 * Get the argument list for a given window
1550 */
1551 static void
1552get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1553{
1554 int idx;
1555
1556 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1557 for (idx = 0; idx < argcount; ++idx)
1558 list_append_string(rettv->vval.v_list,
1559 alist_name(&arglist[idx]), -1);
1560}
1561
1562/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001563 * "argv(nr)" function
1564 */
1565 static void
1566f_argv(typval_T *argvars, typval_T *rettv)
1567{
1568 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001569 aentry_T *arglist = NULL;
1570 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001571
1572 if (argvars[0].v_type != VAR_UNKNOWN)
1573 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001574 if (argvars[1].v_type == VAR_UNKNOWN)
1575 {
1576 arglist = ARGLIST;
1577 argcount = ARGCOUNT;
1578 }
1579 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001580 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001581 {
1582 arglist = GARGLIST;
1583 argcount = GARGCOUNT;
1584 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001585 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001586 {
1587 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1588
1589 if (wp != NULL)
1590 {
1591 /* Use the argument list of the specified window */
1592 arglist = WARGLIST(wp);
1593 argcount = WARGCOUNT(wp);
1594 }
1595 }
1596
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001597 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001598 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001599 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001600 if (arglist != NULL && idx >= 0 && idx < argcount)
1601 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1602 else if (idx == -1)
1603 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001604 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001605 else
1606 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607}
1608
1609/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001610 * "assert_beeps(cmd [, error])" function
1611 */
1612 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001613f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001614{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001615 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001616}
1617
1618/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001619 * "assert_equal(expected, actual[, msg])" function
1620 */
1621 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001622f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001623{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001624 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001625}
1626
1627/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001628 * "assert_equalfile(fname-one, fname-two)" function
1629 */
1630 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001631f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001632{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001633 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001634}
1635
1636/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001637 * "assert_notequal(expected, actual[, msg])" function
1638 */
1639 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001640f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001642 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001643}
1644
1645/*
1646 * "assert_exception(string[, msg])" function
1647 */
1648 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001649f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001650{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001651 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001652}
1653
1654/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001655 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001656 */
1657 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001658f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001660 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001661}
1662
1663/*
1664 * "assert_false(actual[, msg])" function
1665 */
1666 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001667f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001668{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001669 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001670}
1671
1672/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001673 * "assert_inrange(lower, upper[, msg])" function
1674 */
1675 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001676f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001677{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001678 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001679}
1680
1681/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001682 * "assert_match(pattern, actual[, msg])" function
1683 */
1684 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001685f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001686{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001687 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001688}
1689
1690/*
1691 * "assert_notmatch(pattern, actual[, msg])" function
1692 */
1693 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001694f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001695{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001696 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001697}
1698
1699/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001700 * "assert_report(msg)" function
1701 */
1702 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001703f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001704{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001705 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001706}
1707
1708/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001709 * "assert_true(actual[, msg])" function
1710 */
1711 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001712f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001713{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001714 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001715}
1716
1717#ifdef FEAT_FLOAT
1718/*
1719 * "asin()" function
1720 */
1721 static void
1722f_asin(typval_T *argvars, typval_T *rettv)
1723{
1724 float_T f = 0.0;
1725
1726 rettv->v_type = VAR_FLOAT;
1727 if (get_float_arg(argvars, &f) == OK)
1728 rettv->vval.v_float = asin(f);
1729 else
1730 rettv->vval.v_float = 0.0;
1731}
1732
1733/*
1734 * "atan()" function
1735 */
1736 static void
1737f_atan(typval_T *argvars, typval_T *rettv)
1738{
1739 float_T f = 0.0;
1740
1741 rettv->v_type = VAR_FLOAT;
1742 if (get_float_arg(argvars, &f) == OK)
1743 rettv->vval.v_float = atan(f);
1744 else
1745 rettv->vval.v_float = 0.0;
1746}
1747
1748/*
1749 * "atan2()" function
1750 */
1751 static void
1752f_atan2(typval_T *argvars, typval_T *rettv)
1753{
1754 float_T fx = 0.0, fy = 0.0;
1755
1756 rettv->v_type = VAR_FLOAT;
1757 if (get_float_arg(argvars, &fx) == OK
1758 && get_float_arg(&argvars[1], &fy) == OK)
1759 rettv->vval.v_float = atan2(fx, fy);
1760 else
1761 rettv->vval.v_float = 0.0;
1762}
1763#endif
1764
1765/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001766 * "balloon_show()" function
1767 */
1768#ifdef FEAT_BEVAL
1769 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001770f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1771{
1772 rettv->v_type = VAR_STRING;
1773 if (balloonEval != NULL)
1774 {
1775 if (balloonEval->msg == NULL)
1776 rettv->vval.v_string = NULL;
1777 else
1778 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1779 }
1780}
1781
1782 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001783f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1784{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001785 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001786 {
1787 if (argvars[0].v_type == VAR_LIST
1788# ifdef FEAT_GUI
1789 && !gui.in_use
1790# endif
1791 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001792 {
1793 list_T *l = argvars[0].vval.v_list;
1794
1795 // empty list removes the balloon
1796 post_balloon(balloonEval, NULL,
1797 l == NULL || l->lv_len == 0 ? NULL : l);
1798 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001799 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001800 {
1801 char_u *mesg = tv_get_string_chk(&argvars[0]);
1802
1803 if (mesg != NULL)
1804 // empty string removes the balloon
1805 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1806 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001807 }
1808}
1809
Bram Moolenaar669a8282017-11-19 20:13:05 +01001810# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001811 static void
1812f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1813{
1814 if (rettv_list_alloc(rettv) == OK)
1815 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001816 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001817
1818 if (msg != NULL)
1819 {
1820 pumitem_T *array;
1821 int size = split_message(msg, &array);
1822 int i;
1823
1824 /* Skip the first and last item, they are always empty. */
1825 for (i = 1; i < size - 1; ++i)
1826 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001827 while (size > 0)
1828 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001829 vim_free(array);
1830 }
1831 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001832}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001833# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001834#endif
1835
1836/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001837 * "browse(save, title, initdir, default)" function
1838 */
1839 static void
1840f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1841{
1842#ifdef FEAT_BROWSE
1843 int save;
1844 char_u *title;
1845 char_u *initdir;
1846 char_u *defname;
1847 char_u buf[NUMBUFLEN];
1848 char_u buf2[NUMBUFLEN];
1849 int error = FALSE;
1850
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001851 save = (int)tv_get_number_chk(&argvars[0], &error);
1852 title = tv_get_string_chk(&argvars[1]);
1853 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1854 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001855
1856 if (error || title == NULL || initdir == NULL || defname == NULL)
1857 rettv->vval.v_string = NULL;
1858 else
1859 rettv->vval.v_string =
1860 do_browse(save ? BROWSE_SAVE : 0,
1861 title, defname, NULL, initdir, NULL, curbuf);
1862#else
1863 rettv->vval.v_string = NULL;
1864#endif
1865 rettv->v_type = VAR_STRING;
1866}
1867
1868/*
1869 * "browsedir(title, initdir)" function
1870 */
1871 static void
1872f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1873{
1874#ifdef FEAT_BROWSE
1875 char_u *title;
1876 char_u *initdir;
1877 char_u buf[NUMBUFLEN];
1878
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001879 title = tv_get_string_chk(&argvars[0]);
1880 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001881
1882 if (title == NULL || initdir == NULL)
1883 rettv->vval.v_string = NULL;
1884 else
1885 rettv->vval.v_string = do_browse(BROWSE_DIR,
1886 title, NULL, NULL, initdir, NULL, curbuf);
1887#else
1888 rettv->vval.v_string = NULL;
1889#endif
1890 rettv->v_type = VAR_STRING;
1891}
1892
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001893/*
1894 * Find a buffer by number or exact name.
1895 */
1896 static buf_T *
1897find_buffer(typval_T *avar)
1898{
1899 buf_T *buf = NULL;
1900
1901 if (avar->v_type == VAR_NUMBER)
1902 buf = buflist_findnr((int)avar->vval.v_number);
1903 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1904 {
1905 buf = buflist_findname_exp(avar->vval.v_string);
1906 if (buf == NULL)
1907 {
1908 /* No full path name match, try a match with a URL or a "nofile"
1909 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001910 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911 if (buf->b_fname != NULL
1912 && (path_with_url(buf->b_fname)
1913#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001914 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915#endif
1916 )
1917 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1918 break;
1919 }
1920 }
1921 return buf;
1922}
1923
1924/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001925 * "bufadd(expr)" function
1926 */
1927 static void
1928f_bufadd(typval_T *argvars, typval_T *rettv)
1929{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001930 char_u *name = tv_get_string(&argvars[0]);
1931
1932 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001933}
1934
1935/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001936 * "bufexists(expr)" function
1937 */
1938 static void
1939f_bufexists(typval_T *argvars, typval_T *rettv)
1940{
1941 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1942}
1943
1944/*
1945 * "buflisted(expr)" function
1946 */
1947 static void
1948f_buflisted(typval_T *argvars, typval_T *rettv)
1949{
1950 buf_T *buf;
1951
1952 buf = find_buffer(&argvars[0]);
1953 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1954}
1955
1956/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001957 * "bufload(expr)" function
1958 */
1959 static void
1960f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1961{
1962 buf_T *buf = get_buf_arg(&argvars[0]);
1963
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001964 if (buf != NULL)
1965 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001966}
1967
1968/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001969 * "bufloaded(expr)" function
1970 */
1971 static void
1972f_bufloaded(typval_T *argvars, typval_T *rettv)
1973{
1974 buf_T *buf;
1975
1976 buf = find_buffer(&argvars[0]);
1977 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1978}
1979
1980 buf_T *
1981buflist_find_by_name(char_u *name, int curtab_only)
1982{
1983 int save_magic;
1984 char_u *save_cpo;
1985 buf_T *buf;
1986
1987 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1988 save_magic = p_magic;
1989 p_magic = TRUE;
1990 save_cpo = p_cpo;
1991 p_cpo = (char_u *)"";
1992
1993 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1994 TRUE, FALSE, curtab_only));
1995
1996 p_magic = save_magic;
1997 p_cpo = save_cpo;
1998 return buf;
1999}
2000
2001/*
2002 * Get buffer by number or pattern.
2003 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002004 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002005tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002006{
2007 char_u *name = tv->vval.v_string;
2008 buf_T *buf;
2009
2010 if (tv->v_type == VAR_NUMBER)
2011 return buflist_findnr((int)tv->vval.v_number);
2012 if (tv->v_type != VAR_STRING)
2013 return NULL;
2014 if (name == NULL || *name == NUL)
2015 return curbuf;
2016 if (name[0] == '$' && name[1] == NUL)
2017 return lastbuf;
2018
2019 buf = buflist_find_by_name(name, curtab_only);
2020
2021 /* If not found, try expanding the name, like done for bufexists(). */
2022 if (buf == NULL)
2023 buf = find_buffer(tv);
2024
2025 return buf;
2026}
2027
2028/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002029 * Get the buffer from "arg" and give an error and return NULL if it is not
2030 * valid.
2031 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002032 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002033get_buf_arg(typval_T *arg)
2034{
2035 buf_T *buf;
2036
2037 ++emsg_off;
2038 buf = tv_get_buf(arg, FALSE);
2039 --emsg_off;
2040 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002041 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002042 return buf;
2043}
2044
2045/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002046 * "bufname(expr)" function
2047 */
2048 static void
2049f_bufname(typval_T *argvars, typval_T *rettv)
2050{
2051 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], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002056 rettv->v_type = VAR_STRING;
2057 if (buf != NULL && buf->b_fname != NULL)
2058 rettv->vval.v_string = vim_strsave(buf->b_fname);
2059 else
2060 rettv->vval.v_string = NULL;
2061 --emsg_off;
2062}
2063
2064/*
2065 * "bufnr(expr)" function
2066 */
2067 static void
2068f_bufnr(typval_T *argvars, typval_T *rettv)
2069{
2070 buf_T *buf;
2071 int error = FALSE;
2072 char_u *name;
2073
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002074 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002075 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002076 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002077 --emsg_off;
2078
2079 /* If the buffer isn't found and the second argument is not zero create a
2080 * new buffer. */
2081 if (buf == NULL
2082 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002083 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002084 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002085 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002086 && !error)
2087 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2088
2089 if (buf != NULL)
2090 rettv->vval.v_number = buf->b_fnum;
2091 else
2092 rettv->vval.v_number = -1;
2093}
2094
2095 static void
2096buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2097{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002098 win_T *wp;
2099 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002100 buf_T *buf;
2101
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002102 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002103 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002104 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002105 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002106 {
2107 ++winnr;
2108 if (wp->w_buffer == buf)
2109 break;
2110 }
2111 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002112 --emsg_off;
2113}
2114
2115/*
2116 * "bufwinid(nr)" function
2117 */
2118 static void
2119f_bufwinid(typval_T *argvars, typval_T *rettv)
2120{
2121 buf_win_common(argvars, rettv, FALSE);
2122}
2123
2124/*
2125 * "bufwinnr(nr)" function
2126 */
2127 static void
2128f_bufwinnr(typval_T *argvars, typval_T *rettv)
2129{
2130 buf_win_common(argvars, rettv, TRUE);
2131}
2132
2133/*
2134 * "byte2line(byte)" function
2135 */
2136 static void
2137f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2138{
2139#ifndef FEAT_BYTEOFF
2140 rettv->vval.v_number = -1;
2141#else
2142 long boff = 0;
2143
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002144 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002145 if (boff < 0)
2146 rettv->vval.v_number = -1;
2147 else
2148 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2149 (linenr_T)0, &boff);
2150#endif
2151}
2152
2153 static void
2154byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2155{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002156 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157 char_u *str;
2158 varnumber_T idx;
2159
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002160 str = tv_get_string_chk(&argvars[0]);
2161 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002162 rettv->vval.v_number = -1;
2163 if (str == NULL || idx < 0)
2164 return;
2165
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002166 t = str;
2167 for ( ; idx > 0; idx--)
2168 {
2169 if (*t == NUL) /* EOL reached */
2170 return;
2171 if (enc_utf8 && comp)
2172 t += utf_ptr2len(t);
2173 else
2174 t += (*mb_ptr2len)(t);
2175 }
2176 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002177}
2178
2179/*
2180 * "byteidx()" function
2181 */
2182 static void
2183f_byteidx(typval_T *argvars, typval_T *rettv)
2184{
2185 byteidx(argvars, rettv, FALSE);
2186}
2187
2188/*
2189 * "byteidxcomp()" function
2190 */
2191 static void
2192f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2193{
2194 byteidx(argvars, rettv, TRUE);
2195}
2196
2197/*
2198 * "call(func, arglist [, dict])" function
2199 */
2200 static void
2201f_call(typval_T *argvars, typval_T *rettv)
2202{
2203 char_u *func;
2204 partial_T *partial = NULL;
2205 dict_T *selfdict = NULL;
2206
2207 if (argvars[1].v_type != VAR_LIST)
2208 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002209 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002210 return;
2211 }
2212 if (argvars[1].vval.v_list == NULL)
2213 return;
2214
2215 if (argvars[0].v_type == VAR_FUNC)
2216 func = argvars[0].vval.v_string;
2217 else if (argvars[0].v_type == VAR_PARTIAL)
2218 {
2219 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002220 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002221 }
2222 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002223 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002224 if (*func == NUL)
2225 return; /* type error or empty name */
2226
2227 if (argvars[2].v_type != VAR_UNKNOWN)
2228 {
2229 if (argvars[2].v_type != VAR_DICT)
2230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002231 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002232 return;
2233 }
2234 selfdict = argvars[2].vval.v_dict;
2235 }
2236
2237 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2238}
2239
2240#ifdef FEAT_FLOAT
2241/*
2242 * "ceil({float})" function
2243 */
2244 static void
2245f_ceil(typval_T *argvars, typval_T *rettv)
2246{
2247 float_T f = 0.0;
2248
2249 rettv->v_type = VAR_FLOAT;
2250 if (get_float_arg(argvars, &f) == OK)
2251 rettv->vval.v_float = ceil(f);
2252 else
2253 rettv->vval.v_float = 0.0;
2254}
2255#endif
2256
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002257/*
2258 * "changenr()" function
2259 */
2260 static void
2261f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2262{
2263 rettv->vval.v_number = curbuf->b_u_seq_cur;
2264}
2265
2266/*
2267 * "char2nr(string)" function
2268 */
2269 static void
2270f_char2nr(typval_T *argvars, typval_T *rettv)
2271{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002272 if (has_mbyte)
2273 {
2274 int utf8 = 0;
2275
2276 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002277 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002278
2279 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002280 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002281 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002282 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002283 }
2284 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002285 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002286}
2287
2288/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002289 * "chdir(dir)" function
2290 */
2291 static void
2292f_chdir(typval_T *argvars, typval_T *rettv)
2293{
2294 char_u *cwd;
2295 cdscope_T scope = CDSCOPE_GLOBAL;
2296
2297 rettv->v_type = VAR_STRING;
2298 rettv->vval.v_string = NULL;
2299
2300 if (argvars[0].v_type != VAR_STRING)
2301 return;
2302
2303 // Return the current directory
2304 cwd = alloc(MAXPATHL);
2305 if (cwd != NULL)
2306 {
2307 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2308 {
2309#ifdef BACKSLASH_IN_FILENAME
2310 slash_adjust(cwd);
2311#endif
2312 rettv->vval.v_string = vim_strsave(cwd);
2313 }
2314 vim_free(cwd);
2315 }
2316
2317 if (curwin->w_localdir != NULL)
2318 scope = CDSCOPE_WINDOW;
2319 else if (curtab->tp_localdir != NULL)
2320 scope = CDSCOPE_TABPAGE;
2321
2322 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2323 // Directory change failed
2324 VIM_CLEAR(rettv->vval.v_string);
2325}
2326
2327/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002328 * "cindent(lnum)" function
2329 */
2330 static void
2331f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2332{
2333#ifdef FEAT_CINDENT
2334 pos_T pos;
2335 linenr_T lnum;
2336
2337 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002338 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002339 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2340 {
2341 curwin->w_cursor.lnum = lnum;
2342 rettv->vval.v_number = get_c_indent();
2343 curwin->w_cursor = pos;
2344 }
2345 else
2346#endif
2347 rettv->vval.v_number = -1;
2348}
2349
Bram Moolenaaraff74912019-03-30 18:11:49 +01002350 static win_T *
2351get_optional_window(typval_T *argvars, int idx)
2352{
2353 win_T *win = curwin;
2354
2355 if (argvars[idx].v_type != VAR_UNKNOWN)
2356 {
2357 win = find_win_by_nr_or_id(&argvars[idx]);
2358 if (win == NULL)
2359 {
2360 emsg(_(e_invalwindow));
2361 return NULL;
2362 }
2363 }
2364 return win;
2365}
2366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002367/*
2368 * "clearmatches()" function
2369 */
2370 static void
2371f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2372{
2373#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002374 win_T *win = get_optional_window(argvars, 0);
2375
2376 if (win != NULL)
2377 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002378#endif
2379}
2380
2381/*
2382 * "col(string)" function
2383 */
2384 static void
2385f_col(typval_T *argvars, typval_T *rettv)
2386{
2387 colnr_T col = 0;
2388 pos_T *fp;
2389 int fnum = curbuf->b_fnum;
2390
2391 fp = var2fpos(&argvars[0], FALSE, &fnum);
2392 if (fp != NULL && fnum == curbuf->b_fnum)
2393 {
2394 if (fp->col == MAXCOL)
2395 {
2396 /* '> can be MAXCOL, get the length of the line then */
2397 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2398 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2399 else
2400 col = MAXCOL;
2401 }
2402 else
2403 {
2404 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002405 /* col(".") when the cursor is on the NUL at the end of the line
2406 * because of "coladd" can be seen as an extra column. */
2407 if (virtual_active() && fp == &curwin->w_cursor)
2408 {
2409 char_u *p = ml_get_cursor();
2410
2411 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2412 curwin->w_virtcol - curwin->w_cursor.coladd))
2413 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002414 int l;
2415
2416 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2417 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002418 }
2419 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002420 }
2421 }
2422 rettv->vval.v_number = col;
2423}
2424
2425#if defined(FEAT_INS_EXPAND)
2426/*
2427 * "complete()" function
2428 */
2429 static void
2430f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2431{
2432 int startcol;
2433
2434 if ((State & INSERT) == 0)
2435 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002436 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002437 return;
2438 }
2439
2440 /* Check for undo allowed here, because if something was already inserted
2441 * the line was already saved for undo and this check isn't done. */
2442 if (!undo_allowed())
2443 return;
2444
2445 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2446 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002447 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002448 return;
2449 }
2450
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002451 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002452 if (startcol <= 0)
2453 return;
2454
2455 set_completion(startcol - 1, argvars[1].vval.v_list);
2456}
2457
2458/*
2459 * "complete_add()" function
2460 */
2461 static void
2462f_complete_add(typval_T *argvars, typval_T *rettv)
2463{
2464 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2465}
2466
2467/*
2468 * "complete_check()" function
2469 */
2470 static void
2471f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2472{
2473 int saved = RedrawingDisabled;
2474
2475 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002476 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002477 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002478 RedrawingDisabled = saved;
2479}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002480
2481/*
2482 * "complete_info()" function
2483 */
2484 static void
2485f_complete_info(typval_T *argvars, typval_T *rettv)
2486{
2487 list_T *what_list = NULL;
2488
2489 if (rettv_dict_alloc(rettv) != OK)
2490 return;
2491
2492 if (argvars[0].v_type != VAR_UNKNOWN)
2493 {
2494 if (argvars[0].v_type != VAR_LIST)
2495 {
2496 emsg(_(e_listreq));
2497 return;
2498 }
2499 what_list = argvars[0].vval.v_list;
2500 }
2501 get_complete_info(what_list, rettv->vval.v_dict);
2502}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002503#endif
2504
2505/*
2506 * "confirm(message, buttons[, default [, type]])" function
2507 */
2508 static void
2509f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2510{
2511#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2512 char_u *message;
2513 char_u *buttons = NULL;
2514 char_u buf[NUMBUFLEN];
2515 char_u buf2[NUMBUFLEN];
2516 int def = 1;
2517 int type = VIM_GENERIC;
2518 char_u *typestr;
2519 int error = FALSE;
2520
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002521 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002522 if (message == NULL)
2523 error = TRUE;
2524 if (argvars[1].v_type != VAR_UNKNOWN)
2525 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002526 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002527 if (buttons == NULL)
2528 error = TRUE;
2529 if (argvars[2].v_type != VAR_UNKNOWN)
2530 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002531 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002532 if (argvars[3].v_type != VAR_UNKNOWN)
2533 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002534 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002535 if (typestr == NULL)
2536 error = TRUE;
2537 else
2538 {
2539 switch (TOUPPER_ASC(*typestr))
2540 {
2541 case 'E': type = VIM_ERROR; break;
2542 case 'Q': type = VIM_QUESTION; break;
2543 case 'I': type = VIM_INFO; break;
2544 case 'W': type = VIM_WARNING; break;
2545 case 'G': type = VIM_GENERIC; break;
2546 }
2547 }
2548 }
2549 }
2550 }
2551
2552 if (buttons == NULL || *buttons == NUL)
2553 buttons = (char_u *)_("&Ok");
2554
2555 if (!error)
2556 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2557 def, NULL, FALSE);
2558#endif
2559}
2560
2561/*
2562 * "copy()" function
2563 */
2564 static void
2565f_copy(typval_T *argvars, typval_T *rettv)
2566{
2567 item_copy(&argvars[0], rettv, FALSE, 0);
2568}
2569
2570#ifdef FEAT_FLOAT
2571/*
2572 * "cos()" function
2573 */
2574 static void
2575f_cos(typval_T *argvars, typval_T *rettv)
2576{
2577 float_T f = 0.0;
2578
2579 rettv->v_type = VAR_FLOAT;
2580 if (get_float_arg(argvars, &f) == OK)
2581 rettv->vval.v_float = cos(f);
2582 else
2583 rettv->vval.v_float = 0.0;
2584}
2585
2586/*
2587 * "cosh()" function
2588 */
2589 static void
2590f_cosh(typval_T *argvars, typval_T *rettv)
2591{
2592 float_T f = 0.0;
2593
2594 rettv->v_type = VAR_FLOAT;
2595 if (get_float_arg(argvars, &f) == OK)
2596 rettv->vval.v_float = cosh(f);
2597 else
2598 rettv->vval.v_float = 0.0;
2599}
2600#endif
2601
2602/*
2603 * "count()" function
2604 */
2605 static void
2606f_count(typval_T *argvars, typval_T *rettv)
2607{
2608 long n = 0;
2609 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002610 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002611
Bram Moolenaar9966b212017-07-28 16:46:57 +02002612 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002613 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002614
2615 if (argvars[0].v_type == VAR_STRING)
2616 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002617 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002618 char_u *p = argvars[0].vval.v_string;
2619 char_u *next;
2620
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002621 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002622 {
2623 if (ic)
2624 {
2625 size_t len = STRLEN(expr);
2626
2627 while (*p != NUL)
2628 {
2629 if (MB_STRNICMP(p, expr, len) == 0)
2630 {
2631 ++n;
2632 p += len;
2633 }
2634 else
2635 MB_PTR_ADV(p);
2636 }
2637 }
2638 else
2639 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2640 != NULL)
2641 {
2642 ++n;
2643 p = next + STRLEN(expr);
2644 }
2645 }
2646
2647 }
2648 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002649 {
2650 listitem_T *li;
2651 list_T *l;
2652 long idx;
2653
2654 if ((l = argvars[0].vval.v_list) != NULL)
2655 {
2656 li = l->lv_first;
2657 if (argvars[2].v_type != VAR_UNKNOWN)
2658 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002659 if (argvars[3].v_type != VAR_UNKNOWN)
2660 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002661 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002662 if (!error)
2663 {
2664 li = list_find(l, idx);
2665 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002666 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002667 }
2668 }
2669 if (error)
2670 li = NULL;
2671 }
2672
2673 for ( ; li != NULL; li = li->li_next)
2674 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2675 ++n;
2676 }
2677 }
2678 else if (argvars[0].v_type == VAR_DICT)
2679 {
2680 int todo;
2681 dict_T *d;
2682 hashitem_T *hi;
2683
2684 if ((d = argvars[0].vval.v_dict) != NULL)
2685 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002686 if (argvars[2].v_type != VAR_UNKNOWN)
2687 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002689 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002690 }
2691
2692 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2693 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2694 {
2695 if (!HASHITEM_EMPTY(hi))
2696 {
2697 --todo;
2698 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2699 ++n;
2700 }
2701 }
2702 }
2703 }
2704 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002705 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002706 rettv->vval.v_number = n;
2707}
2708
2709/*
2710 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2711 *
2712 * Checks the existence of a cscope connection.
2713 */
2714 static void
2715f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2716{
2717#ifdef FEAT_CSCOPE
2718 int num = 0;
2719 char_u *dbpath = NULL;
2720 char_u *prepend = NULL;
2721 char_u buf[NUMBUFLEN];
2722
2723 if (argvars[0].v_type != VAR_UNKNOWN
2724 && argvars[1].v_type != VAR_UNKNOWN)
2725 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002726 num = (int)tv_get_number(&argvars[0]);
2727 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002728 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002729 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002730 }
2731
2732 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2733#endif
2734}
2735
2736/*
2737 * "cursor(lnum, col)" function, or
2738 * "cursor(list)"
2739 *
2740 * Moves the cursor to the specified line and column.
2741 * Returns 0 when the position could be set, -1 otherwise.
2742 */
2743 static void
2744f_cursor(typval_T *argvars, typval_T *rettv)
2745{
2746 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002747 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002748 int set_curswant = TRUE;
2749
2750 rettv->vval.v_number = -1;
2751 if (argvars[1].v_type == VAR_UNKNOWN)
2752 {
2753 pos_T pos;
2754 colnr_T curswant = -1;
2755
2756 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2757 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002758 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002759 return;
2760 }
2761 line = pos.lnum;
2762 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002763 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002764 if (curswant >= 0)
2765 {
2766 curwin->w_curswant = curswant - 1;
2767 set_curswant = FALSE;
2768 }
2769 }
2770 else
2771 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002772 line = tv_get_lnum(argvars);
2773 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002774 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002775 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002776 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002777 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 return; /* type error; errmsg already given */
2779 if (line > 0)
2780 curwin->w_cursor.lnum = line;
2781 if (col > 0)
2782 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002783 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784
2785 /* Make sure the cursor is in a valid position. */
2786 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002787 /* Correct cursor for multi-byte character. */
2788 if (has_mbyte)
2789 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790
2791 curwin->w_set_curswant = set_curswant;
2792 rettv->vval.v_number = 0;
2793}
2794
Bram Moolenaar4f974752019-02-17 17:44:42 +01002795#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002796/*
2797 * "debugbreak()" function
2798 */
2799 static void
2800f_debugbreak(typval_T *argvars, typval_T *rettv)
2801{
2802 int pid;
2803
2804 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002805 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002806 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002807 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002808 else
2809 {
2810 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2811
2812 if (hProcess != NULL)
2813 {
2814 DebugBreakProcess(hProcess);
2815 CloseHandle(hProcess);
2816 rettv->vval.v_number = OK;
2817 }
2818 }
2819}
2820#endif
2821
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002822/*
2823 * "deepcopy()" function
2824 */
2825 static void
2826f_deepcopy(typval_T *argvars, typval_T *rettv)
2827{
2828 int noref = 0;
2829 int copyID;
2830
2831 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002832 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002833 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002834 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002835 else
2836 {
2837 copyID = get_copyID();
2838 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2839 }
2840}
2841
2842/*
2843 * "delete()" function
2844 */
2845 static void
2846f_delete(typval_T *argvars, typval_T *rettv)
2847{
2848 char_u nbuf[NUMBUFLEN];
2849 char_u *name;
2850 char_u *flags;
2851
2852 rettv->vval.v_number = -1;
2853 if (check_restricted() || check_secure())
2854 return;
2855
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002856 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002857 if (name == NULL || *name == NUL)
2858 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002859 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002860 return;
2861 }
2862
2863 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002864 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002865 else
2866 flags = (char_u *)"";
2867
2868 if (*flags == NUL)
2869 /* delete a file */
2870 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2871 else if (STRCMP(flags, "d") == 0)
2872 /* delete an empty directory */
2873 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2874 else if (STRCMP(flags, "rf") == 0)
2875 /* delete a directory recursively */
2876 rettv->vval.v_number = delete_recursive(name);
2877 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002878 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002879}
2880
2881/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002882 * "deletebufline()" function
2883 */
2884 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002885f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002886{
2887 buf_T *buf;
2888 linenr_T first, last;
2889 linenr_T lnum;
2890 long count;
2891 int is_curbuf;
2892 buf_T *curbuf_save = NULL;
2893 win_T *curwin_save = NULL;
2894 tabpage_T *tp;
2895 win_T *wp;
2896
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002897 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002898 if (buf == NULL)
2899 {
2900 rettv->vval.v_number = 1; /* FAIL */
2901 return;
2902 }
2903 is_curbuf = buf == curbuf;
2904
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002905 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002906 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002907 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002908 else
2909 last = first;
2910
2911 if (buf->b_ml.ml_mfp == NULL || first < 1
2912 || first > buf->b_ml.ml_line_count || last < first)
2913 {
2914 rettv->vval.v_number = 1; /* FAIL */
2915 return;
2916 }
2917
2918 if (!is_curbuf)
2919 {
2920 curbuf_save = curbuf;
2921 curwin_save = curwin;
2922 curbuf = buf;
2923 find_win_for_curbuf();
2924 }
2925 if (last > curbuf->b_ml.ml_line_count)
2926 last = curbuf->b_ml.ml_line_count;
2927 count = last - first + 1;
2928
2929 // When coming here from Insert mode, sync undo, so that this can be
2930 // undone separately from what was previously inserted.
2931 if (u_sync_once == 2)
2932 {
2933 u_sync_once = 1; // notify that u_sync() was called
2934 u_sync(TRUE);
2935 }
2936
2937 if (u_save(first - 1, last + 1) == FAIL)
2938 {
2939 rettv->vval.v_number = 1; /* FAIL */
2940 return;
2941 }
2942
2943 for (lnum = first; lnum <= last; ++lnum)
2944 ml_delete(first, TRUE);
2945
2946 FOR_ALL_TAB_WINDOWS(tp, wp)
2947 if (wp->w_buffer == buf)
2948 {
2949 if (wp->w_cursor.lnum > last)
2950 wp->w_cursor.lnum -= count;
2951 else if (wp->w_cursor.lnum> first)
2952 wp->w_cursor.lnum = first;
2953 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2954 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2955 }
2956 check_cursor_col();
2957 deleted_lines_mark(first, count);
2958
2959 if (!is_curbuf)
2960 {
2961 curbuf = curbuf_save;
2962 curwin = curwin_save;
2963 }
2964}
2965
2966/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002967 * "did_filetype()" function
2968 */
2969 static void
2970f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2971{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002972 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002973}
2974
2975/*
2976 * "diff_filler()" function
2977 */
2978 static void
2979f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2980{
2981#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002982 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002983#endif
2984}
2985
2986/*
2987 * "diff_hlID()" function
2988 */
2989 static void
2990f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2991{
2992#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002993 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002994 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002995 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002996 static int fnum = 0;
2997 static int change_start = 0;
2998 static int change_end = 0;
2999 static hlf_T hlID = (hlf_T)0;
3000 int filler_lines;
3001 int col;
3002
3003 if (lnum < 0) /* ignore type error in {lnum} arg */
3004 lnum = 0;
3005 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003006 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003007 || fnum != curbuf->b_fnum)
3008 {
3009 /* New line, buffer, change: need to get the values. */
3010 filler_lines = diff_check(curwin, lnum);
3011 if (filler_lines < 0)
3012 {
3013 if (filler_lines == -1)
3014 {
3015 change_start = MAXCOL;
3016 change_end = -1;
3017 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3018 hlID = HLF_ADD; /* added line */
3019 else
3020 hlID = HLF_CHD; /* changed line */
3021 }
3022 else
3023 hlID = HLF_ADD; /* added line */
3024 }
3025 else
3026 hlID = (hlf_T)0;
3027 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003028 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003029 fnum = curbuf->b_fnum;
3030 }
3031
3032 if (hlID == HLF_CHD || hlID == HLF_TXD)
3033 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003034 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003035 if (col >= change_start && col <= change_end)
3036 hlID = HLF_TXD; /* changed text */
3037 else
3038 hlID = HLF_CHD; /* changed line */
3039 }
3040 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3041#endif
3042}
3043
3044/*
3045 * "empty({expr})" function
3046 */
3047 static void
3048f_empty(typval_T *argvars, typval_T *rettv)
3049{
3050 int n = FALSE;
3051
3052 switch (argvars[0].v_type)
3053 {
3054 case VAR_STRING:
3055 case VAR_FUNC:
3056 n = argvars[0].vval.v_string == NULL
3057 || *argvars[0].vval.v_string == NUL;
3058 break;
3059 case VAR_PARTIAL:
3060 n = FALSE;
3061 break;
3062 case VAR_NUMBER:
3063 n = argvars[0].vval.v_number == 0;
3064 break;
3065 case VAR_FLOAT:
3066#ifdef FEAT_FLOAT
3067 n = argvars[0].vval.v_float == 0.0;
3068 break;
3069#endif
3070 case VAR_LIST:
3071 n = argvars[0].vval.v_list == NULL
3072 || argvars[0].vval.v_list->lv_first == NULL;
3073 break;
3074 case VAR_DICT:
3075 n = argvars[0].vval.v_dict == NULL
3076 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3077 break;
3078 case VAR_SPECIAL:
3079 n = argvars[0].vval.v_number != VVAL_TRUE;
3080 break;
3081
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003082 case VAR_BLOB:
3083 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003084 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3085 break;
3086
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003087 case VAR_JOB:
3088#ifdef FEAT_JOB_CHANNEL
3089 n = argvars[0].vval.v_job == NULL
3090 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3091 break;
3092#endif
3093 case VAR_CHANNEL:
3094#ifdef FEAT_JOB_CHANNEL
3095 n = argvars[0].vval.v_channel == NULL
3096 || !channel_is_open(argvars[0].vval.v_channel);
3097 break;
3098#endif
3099 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003100 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003101 n = TRUE;
3102 break;
3103 }
3104
3105 rettv->vval.v_number = n;
3106}
3107
3108/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003109 * "environ()" function
3110 */
3111 static void
3112f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3113{
3114#if !defined(AMIGA)
3115 int i = 0;
3116 char_u *entry, *value;
3117# ifdef MSWIN
3118 extern wchar_t **_wenviron;
3119# else
3120 extern char **environ;
3121# endif
3122
3123 if (rettv_dict_alloc(rettv) != OK)
3124 return;
3125
3126# ifdef MSWIN
3127 if (*_wenviron == NULL)
3128 return;
3129# else
3130 if (*environ == NULL)
3131 return;
3132# endif
3133
3134 for (i = 0; ; ++i)
3135 {
3136# ifdef MSWIN
3137 short_u *p;
3138
3139 if ((p = (short_u *)_wenviron[i]) == NULL)
3140 return;
3141 entry = utf16_to_enc(p, NULL);
3142# else
3143 if ((entry = (char_u *)environ[i]) == NULL)
3144 return;
3145 entry = vim_strsave(entry);
3146# endif
3147 if (entry == NULL) // out of memory
3148 return;
3149 if ((value = vim_strchr(entry, '=')) == NULL)
3150 {
3151 vim_free(entry);
3152 continue;
3153 }
3154 *value++ = NUL;
3155 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3156 vim_free(entry);
3157 }
3158#endif
3159}
3160
3161/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003162 * "escape({string}, {chars})" function
3163 */
3164 static void
3165f_escape(typval_T *argvars, typval_T *rettv)
3166{
3167 char_u buf[NUMBUFLEN];
3168
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003169 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3170 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003171 rettv->v_type = VAR_STRING;
3172}
3173
3174/*
3175 * "eval()" function
3176 */
3177 static void
3178f_eval(typval_T *argvars, typval_T *rettv)
3179{
3180 char_u *s, *p;
3181
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003182 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003183 if (s != NULL)
3184 s = skipwhite(s);
3185
3186 p = s;
3187 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3188 {
3189 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003190 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003191 need_clr_eos = FALSE;
3192 rettv->v_type = VAR_NUMBER;
3193 rettv->vval.v_number = 0;
3194 }
3195 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003196 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003197}
3198
3199/*
3200 * "eventhandler()" function
3201 */
3202 static void
3203f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3204{
3205 rettv->vval.v_number = vgetc_busy;
3206}
3207
3208/*
3209 * "executable()" function
3210 */
3211 static void
3212f_executable(typval_T *argvars, typval_T *rettv)
3213{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003214 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003215
3216 /* Check in $PATH and also check directly if there is a directory name. */
3217 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3218 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3219}
3220
3221static garray_T redir_execute_ga;
3222
3223/*
3224 * Append "value[value_len]" to the execute() output.
3225 */
3226 void
3227execute_redir_str(char_u *value, int value_len)
3228{
3229 int len;
3230
3231 if (value_len == -1)
3232 len = (int)STRLEN(value); /* Append the entire string */
3233 else
3234 len = value_len; /* Append only "value_len" characters */
3235 if (ga_grow(&redir_execute_ga, len) == OK)
3236 {
3237 mch_memmove((char *)redir_execute_ga.ga_data
3238 + redir_execute_ga.ga_len, value, len);
3239 redir_execute_ga.ga_len += len;
3240 }
3241}
3242
3243/*
3244 * Get next line from a list.
3245 * Called by do_cmdline() to get the next line.
3246 * Returns allocated string, or NULL for end of function.
3247 */
3248
3249 static char_u *
3250get_list_line(
3251 int c UNUSED,
3252 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003253 int indent UNUSED,
3254 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003255{
3256 listitem_T **p = (listitem_T **)cookie;
3257 listitem_T *item = *p;
3258 char_u buf[NUMBUFLEN];
3259 char_u *s;
3260
3261 if (item == NULL)
3262 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003263 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003264 *p = item->li_next;
3265 return s == NULL ? NULL : vim_strsave(s);
3266}
3267
3268/*
3269 * "execute()" function
3270 */
3271 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003272execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003273{
3274 char_u *cmd = NULL;
3275 list_T *list = NULL;
3276 int save_msg_silent = msg_silent;
3277 int save_emsg_silent = emsg_silent;
3278 int save_emsg_noredir = emsg_noredir;
3279 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003280 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003281 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003282 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003283 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003284
3285 rettv->vval.v_string = NULL;
3286 rettv->v_type = VAR_STRING;
3287
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003288 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003289 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003290 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003291 if (list == NULL || list->lv_first == NULL)
3292 /* empty list, no commands, empty output */
3293 return;
3294 ++list->lv_refcount;
3295 }
3296 else
3297 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003298 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 if (cmd == NULL)
3300 return;
3301 }
3302
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003303 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003304 {
3305 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003306 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003307
3308 if (s == NULL)
3309 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003310 if (*s == NUL)
3311 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003312 if (STRNCMP(s, "silent", 6) == 0)
3313 ++msg_silent;
3314 if (STRCMP(s, "silent!") == 0)
3315 {
3316 emsg_silent = TRUE;
3317 emsg_noredir = TRUE;
3318 }
3319 }
3320 else
3321 ++msg_silent;
3322
3323 if (redir_execute)
3324 save_ga = redir_execute_ga;
3325 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3326 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003327 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003328 if (!echo_output)
3329 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003330
3331 if (cmd != NULL)
3332 do_cmdline_cmd(cmd);
3333 else
3334 {
3335 listitem_T *item = list->lv_first;
3336
3337 do_cmdline(NULL, get_list_line, (void *)&item,
3338 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3339 --list->lv_refcount;
3340 }
3341
Bram Moolenaard297f352017-01-29 20:31:21 +01003342 /* Need to append a NUL to the result. */
3343 if (ga_grow(&redir_execute_ga, 1) == OK)
3344 {
3345 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3346 rettv->vval.v_string = redir_execute_ga.ga_data;
3347 }
3348 else
3349 {
3350 ga_clear(&redir_execute_ga);
3351 rettv->vval.v_string = NULL;
3352 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003353 msg_silent = save_msg_silent;
3354 emsg_silent = save_emsg_silent;
3355 emsg_noredir = save_emsg_noredir;
3356
3357 redir_execute = save_redir_execute;
3358 if (redir_execute)
3359 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003360 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003361
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003362 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003363 if (echo_output)
3364 // When not working silently: put it in column zero. A following
3365 // "echon" will overwrite the message, unavoidably.
3366 msg_col = 0;
3367 else
3368 // When working silently: Put it back where it was, since nothing
3369 // should have been written.
3370 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003371}
3372
3373/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003374 * "execute()" function
3375 */
3376 static void
3377f_execute(typval_T *argvars, typval_T *rettv)
3378{
3379 execute_common(argvars, rettv, 0);
3380}
3381
3382/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003383 * "exepath()" function
3384 */
3385 static void
3386f_exepath(typval_T *argvars, typval_T *rettv)
3387{
3388 char_u *p = NULL;
3389
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003390 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003391 rettv->v_type = VAR_STRING;
3392 rettv->vval.v_string = p;
3393}
3394
3395/*
3396 * "exists()" function
3397 */
3398 static void
3399f_exists(typval_T *argvars, typval_T *rettv)
3400{
3401 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003402 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003403
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003404 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405 if (*p == '$') /* environment variable */
3406 {
3407 /* first try "normal" environment variables (fast) */
3408 if (mch_getenv(p + 1) != NULL)
3409 n = TRUE;
3410 else
3411 {
3412 /* try expanding things like $VIM and ${HOME} */
3413 p = expand_env_save(p);
3414 if (p != NULL && *p != '$')
3415 n = TRUE;
3416 vim_free(p);
3417 }
3418 }
3419 else if (*p == '&' || *p == '+') /* option */
3420 {
3421 n = (get_option_tv(&p, NULL, TRUE) == OK);
3422 if (*skipwhite(p) != NUL)
3423 n = FALSE; /* trailing garbage */
3424 }
3425 else if (*p == '*') /* internal or user defined function */
3426 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003427 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003428 }
3429 else if (*p == ':')
3430 {
3431 n = cmd_exists(p + 1);
3432 }
3433 else if (*p == '#')
3434 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003435 if (p[1] == '#')
3436 n = autocmd_supported(p + 2);
3437 else
3438 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003439 }
3440 else /* internal variable */
3441 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003442 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003443 }
3444
3445 rettv->vval.v_number = n;
3446}
3447
3448#ifdef FEAT_FLOAT
3449/*
3450 * "exp()" function
3451 */
3452 static void
3453f_exp(typval_T *argvars, typval_T *rettv)
3454{
3455 float_T f = 0.0;
3456
3457 rettv->v_type = VAR_FLOAT;
3458 if (get_float_arg(argvars, &f) == OK)
3459 rettv->vval.v_float = exp(f);
3460 else
3461 rettv->vval.v_float = 0.0;
3462}
3463#endif
3464
3465/*
3466 * "expand()" function
3467 */
3468 static void
3469f_expand(typval_T *argvars, typval_T *rettv)
3470{
3471 char_u *s;
3472 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003473 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003474 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3475 expand_T xpc;
3476 int error = FALSE;
3477 char_u *result;
3478
3479 rettv->v_type = VAR_STRING;
3480 if (argvars[1].v_type != VAR_UNKNOWN
3481 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003482 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003484 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003485
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003486 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003487 if (*s == '%' || *s == '#' || *s == '<')
3488 {
3489 ++emsg_off;
3490 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3491 --emsg_off;
3492 if (rettv->v_type == VAR_LIST)
3493 {
3494 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3495 list_append_string(rettv->vval.v_list, result, -1);
3496 else
3497 vim_free(result);
3498 }
3499 else
3500 rettv->vval.v_string = result;
3501 }
3502 else
3503 {
3504 /* When the optional second argument is non-zero, don't remove matches
3505 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3506 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003507 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003508 options |= WILD_KEEP_ALL;
3509 if (!error)
3510 {
3511 ExpandInit(&xpc);
3512 xpc.xp_context = EXPAND_FILES;
3513 if (p_wic)
3514 options += WILD_ICASE;
3515 if (rettv->v_type == VAR_STRING)
3516 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3517 options, WILD_ALL);
3518 else if (rettv_list_alloc(rettv) != FAIL)
3519 {
3520 int i;
3521
3522 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3523 for (i = 0; i < xpc.xp_numfiles; i++)
3524 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3525 ExpandCleanup(&xpc);
3526 }
3527 }
3528 else
3529 rettv->vval.v_string = NULL;
3530 }
3531}
3532
3533/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003534 * "expandcmd()" function
3535 * Expand all the special characters in a command string.
3536 */
3537 static void
3538f_expandcmd(typval_T *argvars, typval_T *rettv)
3539{
3540 exarg_T eap;
3541 char_u *cmdstr;
3542 char *errormsg = NULL;
3543
3544 rettv->v_type = VAR_STRING;
3545 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3546
3547 memset(&eap, 0, sizeof(eap));
3548 eap.cmd = cmdstr;
3549 eap.arg = cmdstr;
3550 eap.argt |= NOSPC;
3551 eap.usefilter = FALSE;
3552 eap.nextcmd = NULL;
3553 eap.cmdidx = CMD_USER;
3554
3555 expand_filename(&eap, &cmdstr, &errormsg);
3556 if (errormsg != NULL && *errormsg != NUL)
3557 emsg(errormsg);
3558
3559 rettv->vval.v_string = cmdstr;
3560}
3561
3562/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003563 * "extend(list, list [, idx])" function
3564 * "extend(dict, dict [, action])" function
3565 */
3566 static void
3567f_extend(typval_T *argvars, typval_T *rettv)
3568{
3569 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3570
3571 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3572 {
3573 list_T *l1, *l2;
3574 listitem_T *item;
3575 long before;
3576 int error = FALSE;
3577
3578 l1 = argvars[0].vval.v_list;
3579 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003580 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003581 && l2 != NULL)
3582 {
3583 if (argvars[2].v_type != VAR_UNKNOWN)
3584 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003585 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003586 if (error)
3587 return; /* type error; errmsg already given */
3588
3589 if (before == l1->lv_len)
3590 item = NULL;
3591 else
3592 {
3593 item = list_find(l1, before);
3594 if (item == NULL)
3595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003596 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003597 return;
3598 }
3599 }
3600 }
3601 else
3602 item = NULL;
3603 list_extend(l1, l2, item);
3604
3605 copy_tv(&argvars[0], rettv);
3606 }
3607 }
3608 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3609 {
3610 dict_T *d1, *d2;
3611 char_u *action;
3612 int i;
3613
3614 d1 = argvars[0].vval.v_dict;
3615 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003616 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003617 && d2 != NULL)
3618 {
3619 /* Check the third argument. */
3620 if (argvars[2].v_type != VAR_UNKNOWN)
3621 {
3622 static char *(av[]) = {"keep", "force", "error"};
3623
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003624 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003625 if (action == NULL)
3626 return; /* type error; errmsg already given */
3627 for (i = 0; i < 3; ++i)
3628 if (STRCMP(action, av[i]) == 0)
3629 break;
3630 if (i == 3)
3631 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003632 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003633 return;
3634 }
3635 }
3636 else
3637 action = (char_u *)"force";
3638
3639 dict_extend(d1, d2, action);
3640
3641 copy_tv(&argvars[0], rettv);
3642 }
3643 }
3644 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003645 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003646}
3647
3648/*
3649 * "feedkeys()" function
3650 */
3651 static void
3652f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3653{
3654 int remap = TRUE;
3655 int insert = FALSE;
3656 char_u *keys, *flags;
3657 char_u nbuf[NUMBUFLEN];
3658 int typed = FALSE;
3659 int execute = FALSE;
3660 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003661 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003662 char_u *keys_esc;
3663
3664 /* This is not allowed in the sandbox. If the commands would still be
3665 * executed in the sandbox it would be OK, but it probably happens later,
3666 * when "sandbox" is no longer set. */
3667 if (check_secure())
3668 return;
3669
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003670 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003671
3672 if (argvars[1].v_type != VAR_UNKNOWN)
3673 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003674 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003675 for ( ; *flags != NUL; ++flags)
3676 {
3677 switch (*flags)
3678 {
3679 case 'n': remap = FALSE; break;
3680 case 'm': remap = TRUE; break;
3681 case 't': typed = TRUE; break;
3682 case 'i': insert = TRUE; break;
3683 case 'x': execute = TRUE; break;
3684 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003685 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003686 }
3687 }
3688 }
3689
3690 if (*keys != NUL || execute)
3691 {
3692 /* Need to escape K_SPECIAL and CSI before putting the string in the
3693 * typeahead buffer. */
3694 keys_esc = vim_strsave_escape_csi(keys);
3695 if (keys_esc != NULL)
3696 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003697 if (lowlevel)
3698 {
3699#ifdef USE_INPUT_BUF
3700 add_to_input_buf(keys, (int)STRLEN(keys));
3701#else
3702 emsg(_("E980: lowlevel input not supported"));
3703#endif
3704 }
3705 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003706 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003707 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003708 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003709 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003710#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003711 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003712#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003713 )
3714 typebuf_was_filled = TRUE;
3715 }
3716 vim_free(keys_esc);
3717
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003718 if (execute)
3719 {
3720 int save_msg_scroll = msg_scroll;
3721
3722 /* Avoid a 1 second delay when the keys start Insert mode. */
3723 msg_scroll = FALSE;
3724
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003725 if (!dangerous)
3726 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003727 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003728 if (!dangerous)
3729 --ex_normal_busy;
3730
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003731 msg_scroll |= save_msg_scroll;
3732 }
3733 }
3734 }
3735}
3736
3737/*
3738 * "filereadable()" function
3739 */
3740 static void
3741f_filereadable(typval_T *argvars, typval_T *rettv)
3742{
3743 int fd;
3744 char_u *p;
3745 int n;
3746
3747#ifndef O_NONBLOCK
3748# define O_NONBLOCK 0
3749#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003750 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003751 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3752 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3753 {
3754 n = TRUE;
3755 close(fd);
3756 }
3757 else
3758 n = FALSE;
3759
3760 rettv->vval.v_number = n;
3761}
3762
3763/*
3764 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3765 * rights to write into.
3766 */
3767 static void
3768f_filewritable(typval_T *argvars, typval_T *rettv)
3769{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003770 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003771}
3772
3773 static void
3774findfilendir(
3775 typval_T *argvars UNUSED,
3776 typval_T *rettv,
3777 int find_what UNUSED)
3778{
3779#ifdef FEAT_SEARCHPATH
3780 char_u *fname;
3781 char_u *fresult = NULL;
3782 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3783 char_u *p;
3784 char_u pathbuf[NUMBUFLEN];
3785 int count = 1;
3786 int first = TRUE;
3787 int error = FALSE;
3788#endif
3789
3790 rettv->vval.v_string = NULL;
3791 rettv->v_type = VAR_STRING;
3792
3793#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003794 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003795
3796 if (argvars[1].v_type != VAR_UNKNOWN)
3797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003798 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003799 if (p == NULL)
3800 error = TRUE;
3801 else
3802 {
3803 if (*p != NUL)
3804 path = p;
3805
3806 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003807 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003808 }
3809 }
3810
3811 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3812 error = TRUE;
3813
3814 if (*fname != NUL && !error)
3815 {
3816 do
3817 {
3818 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3819 vim_free(fresult);
3820 fresult = find_file_in_path_option(first ? fname : NULL,
3821 first ? (int)STRLEN(fname) : 0,
3822 0, first, path,
3823 find_what,
3824 curbuf->b_ffname,
3825 find_what == FINDFILE_DIR
3826 ? (char_u *)"" : curbuf->b_p_sua);
3827 first = FALSE;
3828
3829 if (fresult != NULL && rettv->v_type == VAR_LIST)
3830 list_append_string(rettv->vval.v_list, fresult, -1);
3831
3832 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3833 }
3834
3835 if (rettv->v_type == VAR_STRING)
3836 rettv->vval.v_string = fresult;
3837#endif
3838}
3839
3840/*
3841 * "filter()" function
3842 */
3843 static void
3844f_filter(typval_T *argvars, typval_T *rettv)
3845{
3846 filter_map(argvars, rettv, FALSE);
3847}
3848
3849/*
3850 * "finddir({fname}[, {path}[, {count}]])" function
3851 */
3852 static void
3853f_finddir(typval_T *argvars, typval_T *rettv)
3854{
3855 findfilendir(argvars, rettv, FINDFILE_DIR);
3856}
3857
3858/*
3859 * "findfile({fname}[, {path}[, {count}]])" function
3860 */
3861 static void
3862f_findfile(typval_T *argvars, typval_T *rettv)
3863{
3864 findfilendir(argvars, rettv, FINDFILE_FILE);
3865}
3866
3867#ifdef FEAT_FLOAT
3868/*
3869 * "float2nr({float})" function
3870 */
3871 static void
3872f_float2nr(typval_T *argvars, typval_T *rettv)
3873{
3874 float_T f = 0.0;
3875
3876 if (get_float_arg(argvars, &f) == OK)
3877 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003878 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003879 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003880 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003881 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003882 else
3883 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003884 }
3885}
3886
3887/*
3888 * "floor({float})" function
3889 */
3890 static void
3891f_floor(typval_T *argvars, typval_T *rettv)
3892{
3893 float_T f = 0.0;
3894
3895 rettv->v_type = VAR_FLOAT;
3896 if (get_float_arg(argvars, &f) == OK)
3897 rettv->vval.v_float = floor(f);
3898 else
3899 rettv->vval.v_float = 0.0;
3900}
3901
3902/*
3903 * "fmod()" function
3904 */
3905 static void
3906f_fmod(typval_T *argvars, typval_T *rettv)
3907{
3908 float_T fx = 0.0, fy = 0.0;
3909
3910 rettv->v_type = VAR_FLOAT;
3911 if (get_float_arg(argvars, &fx) == OK
3912 && get_float_arg(&argvars[1], &fy) == OK)
3913 rettv->vval.v_float = fmod(fx, fy);
3914 else
3915 rettv->vval.v_float = 0.0;
3916}
3917#endif
3918
3919/*
3920 * "fnameescape({string})" function
3921 */
3922 static void
3923f_fnameescape(typval_T *argvars, typval_T *rettv)
3924{
3925 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003926 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003927 rettv->v_type = VAR_STRING;
3928}
3929
3930/*
3931 * "fnamemodify({fname}, {mods})" function
3932 */
3933 static void
3934f_fnamemodify(typval_T *argvars, typval_T *rettv)
3935{
3936 char_u *fname;
3937 char_u *mods;
3938 int usedlen = 0;
3939 int len;
3940 char_u *fbuf = NULL;
3941 char_u buf[NUMBUFLEN];
3942
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003943 fname = tv_get_string_chk(&argvars[0]);
3944 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003945 if (fname == NULL || mods == NULL)
3946 fname = NULL;
3947 else
3948 {
3949 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003950 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003951 }
3952
3953 rettv->v_type = VAR_STRING;
3954 if (fname == NULL)
3955 rettv->vval.v_string = NULL;
3956 else
3957 rettv->vval.v_string = vim_strnsave(fname, len);
3958 vim_free(fbuf);
3959}
3960
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003961/*
3962 * "foldclosed()" function
3963 */
3964 static void
3965foldclosed_both(
3966 typval_T *argvars UNUSED,
3967 typval_T *rettv,
3968 int end UNUSED)
3969{
3970#ifdef FEAT_FOLDING
3971 linenr_T lnum;
3972 linenr_T first, last;
3973
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003974 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003975 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3976 {
3977 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3978 {
3979 if (end)
3980 rettv->vval.v_number = (varnumber_T)last;
3981 else
3982 rettv->vval.v_number = (varnumber_T)first;
3983 return;
3984 }
3985 }
3986#endif
3987 rettv->vval.v_number = -1;
3988}
3989
3990/*
3991 * "foldclosed()" function
3992 */
3993 static void
3994f_foldclosed(typval_T *argvars, typval_T *rettv)
3995{
3996 foldclosed_both(argvars, rettv, FALSE);
3997}
3998
3999/*
4000 * "foldclosedend()" function
4001 */
4002 static void
4003f_foldclosedend(typval_T *argvars, typval_T *rettv)
4004{
4005 foldclosed_both(argvars, rettv, TRUE);
4006}
4007
4008/*
4009 * "foldlevel()" function
4010 */
4011 static void
4012f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4013{
4014#ifdef FEAT_FOLDING
4015 linenr_T lnum;
4016
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004017 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004018 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4019 rettv->vval.v_number = foldLevel(lnum);
4020#endif
4021}
4022
4023/*
4024 * "foldtext()" function
4025 */
4026 static void
4027f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4028{
4029#ifdef FEAT_FOLDING
4030 linenr_T foldstart;
4031 linenr_T foldend;
4032 char_u *dashes;
4033 linenr_T lnum;
4034 char_u *s;
4035 char_u *r;
4036 int len;
4037 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004038 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004039#endif
4040
4041 rettv->v_type = VAR_STRING;
4042 rettv->vval.v_string = NULL;
4043#ifdef FEAT_FOLDING
4044 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4045 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4046 dashes = get_vim_var_str(VV_FOLDDASHES);
4047 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4048 && dashes != NULL)
4049 {
4050 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004051 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004052 if (!linewhite(lnum))
4053 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004054
4055 /* Find interesting text in this line. */
4056 s = skipwhite(ml_get(lnum));
4057 /* skip C comment-start */
4058 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4059 {
4060 s = skipwhite(s + 2);
4061 if (*skipwhite(s) == NUL
4062 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4063 {
4064 s = skipwhite(ml_get(lnum + 1));
4065 if (*s == '*')
4066 s = skipwhite(s + 1);
4067 }
4068 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004069 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004070 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004071 r = alloc(STRLEN(txt)
4072 + STRLEN(dashes) // for %s
4073 + 20 // for %3ld
4074 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004075 if (r != NULL)
4076 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004077 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004078 len = (int)STRLEN(r);
4079 STRCAT(r, s);
4080 /* remove 'foldmarker' and 'commentstring' */
4081 foldtext_cleanup(r + len);
4082 rettv->vval.v_string = r;
4083 }
4084 }
4085#endif
4086}
4087
4088/*
4089 * "foldtextresult(lnum)" function
4090 */
4091 static void
4092f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4093{
4094#ifdef FEAT_FOLDING
4095 linenr_T lnum;
4096 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004097 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004098 foldinfo_T foldinfo;
4099 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004100 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004101#endif
4102
4103 rettv->v_type = VAR_STRING;
4104 rettv->vval.v_string = NULL;
4105#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004106 if (entered)
4107 return; /* reject recursive use */
4108 entered = TRUE;
4109
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004110 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004111 /* treat illegal types and illegal string values for {lnum} the same */
4112 if (lnum < 0)
4113 lnum = 0;
4114 fold_count = foldedCount(curwin, lnum, &foldinfo);
4115 if (fold_count > 0)
4116 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004117 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4118 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004119 if (text == buf)
4120 text = vim_strsave(text);
4121 rettv->vval.v_string = text;
4122 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004123
4124 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004125#endif
4126}
4127
4128/*
4129 * "foreground()" function
4130 */
4131 static void
4132f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4133{
4134#ifdef FEAT_GUI
4135 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004136 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004137 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004138 return;
4139 }
4140#endif
4141#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004142 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004143#endif
4144}
4145
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004146 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004147common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004148{
4149 char_u *s;
4150 char_u *name;
4151 int use_string = FALSE;
4152 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004153 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004154
4155 if (argvars[0].v_type == VAR_FUNC)
4156 {
4157 /* function(MyFunc, [arg], dict) */
4158 s = argvars[0].vval.v_string;
4159 }
4160 else if (argvars[0].v_type == VAR_PARTIAL
4161 && argvars[0].vval.v_partial != NULL)
4162 {
4163 /* function(dict.MyFunc, [arg]) */
4164 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004165 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004166 }
4167 else
4168 {
4169 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004170 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004171 use_string = TRUE;
4172 }
4173
Bram Moolenaar843b8842016-08-21 14:36:15 +02004174 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004175 {
4176 name = s;
4177 trans_name = trans_function_name(&name, FALSE,
4178 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4179 if (*name != NUL)
4180 s = NULL;
4181 }
4182
Bram Moolenaar843b8842016-08-21 14:36:15 +02004183 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4184 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004185 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004186 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004187 else if (trans_name != NULL && (is_funcref
4188 ? find_func(trans_name) == NULL
4189 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004190 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004191 else
4192 {
4193 int dict_idx = 0;
4194 int arg_idx = 0;
4195 list_T *list = NULL;
4196
4197 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4198 {
4199 char sid_buf[25];
4200 int off = *s == 's' ? 2 : 5;
4201
4202 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4203 * also be called from another script. Using trans_function_name()
4204 * would also work, but some plugins depend on the name being
4205 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004206 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004207 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004208 if (name != NULL)
4209 {
4210 STRCPY(name, sid_buf);
4211 STRCAT(name, s + off);
4212 }
4213 }
4214 else
4215 name = vim_strsave(s);
4216
4217 if (argvars[1].v_type != VAR_UNKNOWN)
4218 {
4219 if (argvars[2].v_type != VAR_UNKNOWN)
4220 {
4221 /* function(name, [args], dict) */
4222 arg_idx = 1;
4223 dict_idx = 2;
4224 }
4225 else if (argvars[1].v_type == VAR_DICT)
4226 /* function(name, dict) */
4227 dict_idx = 1;
4228 else
4229 /* function(name, [args]) */
4230 arg_idx = 1;
4231 if (dict_idx > 0)
4232 {
4233 if (argvars[dict_idx].v_type != VAR_DICT)
4234 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004235 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004236 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004237 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004238 }
4239 if (argvars[dict_idx].vval.v_dict == NULL)
4240 dict_idx = 0;
4241 }
4242 if (arg_idx > 0)
4243 {
4244 if (argvars[arg_idx].v_type != VAR_LIST)
4245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004246 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004247 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004248 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004249 }
4250 list = argvars[arg_idx].vval.v_list;
4251 if (list == NULL || list->lv_len == 0)
4252 arg_idx = 0;
4253 }
4254 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004255 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004256 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004257 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004258
4259 /* result is a VAR_PARTIAL */
4260 if (pt == NULL)
4261 vim_free(name);
4262 else
4263 {
4264 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4265 {
4266 listitem_T *li;
4267 int i = 0;
4268 int arg_len = 0;
4269 int lv_len = 0;
4270
4271 if (arg_pt != NULL)
4272 arg_len = arg_pt->pt_argc;
4273 if (list != NULL)
4274 lv_len = list->lv_len;
4275 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004276 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004277 if (pt->pt_argv == NULL)
4278 {
4279 vim_free(pt);
4280 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004281 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004282 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004283 for (i = 0; i < arg_len; i++)
4284 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4285 if (lv_len > 0)
4286 for (li = list->lv_first; li != NULL;
4287 li = li->li_next)
4288 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004289 }
4290
4291 /* For "function(dict.func, [], dict)" and "func" is a partial
4292 * use "dict". That is backwards compatible. */
4293 if (dict_idx > 0)
4294 {
4295 /* The dict is bound explicitly, pt_auto is FALSE. */
4296 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4297 ++pt->pt_dict->dv_refcount;
4298 }
4299 else if (arg_pt != NULL)
4300 {
4301 /* If the dict was bound automatically the result is also
4302 * bound automatically. */
4303 pt->pt_dict = arg_pt->pt_dict;
4304 pt->pt_auto = arg_pt->pt_auto;
4305 if (pt->pt_dict != NULL)
4306 ++pt->pt_dict->dv_refcount;
4307 }
4308
4309 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004310 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4311 {
4312 pt->pt_func = arg_pt->pt_func;
4313 func_ptr_ref(pt->pt_func);
4314 vim_free(name);
4315 }
4316 else if (is_funcref)
4317 {
4318 pt->pt_func = find_func(trans_name);
4319 func_ptr_ref(pt->pt_func);
4320 vim_free(name);
4321 }
4322 else
4323 {
4324 pt->pt_name = name;
4325 func_ref(name);
4326 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004327 }
4328 rettv->v_type = VAR_PARTIAL;
4329 rettv->vval.v_partial = pt;
4330 }
4331 else
4332 {
4333 /* result is a VAR_FUNC */
4334 rettv->v_type = VAR_FUNC;
4335 rettv->vval.v_string = name;
4336 func_ref(name);
4337 }
4338 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004339theend:
4340 vim_free(trans_name);
4341}
4342
4343/*
4344 * "funcref()" function
4345 */
4346 static void
4347f_funcref(typval_T *argvars, typval_T *rettv)
4348{
4349 common_function(argvars, rettv, TRUE);
4350}
4351
4352/*
4353 * "function()" function
4354 */
4355 static void
4356f_function(typval_T *argvars, typval_T *rettv)
4357{
4358 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004359}
4360
4361/*
4362 * "garbagecollect()" function
4363 */
4364 static void
4365f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4366{
4367 /* This is postponed until we are back at the toplevel, because we may be
4368 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4369 want_garbage_collect = TRUE;
4370
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004371 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004372 garbage_collect_at_exit = TRUE;
4373}
4374
4375/*
4376 * "get()" function
4377 */
4378 static void
4379f_get(typval_T *argvars, typval_T *rettv)
4380{
4381 listitem_T *li;
4382 list_T *l;
4383 dictitem_T *di;
4384 dict_T *d;
4385 typval_T *tv = NULL;
4386
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004387 if (argvars[0].v_type == VAR_BLOB)
4388 {
4389 int error = FALSE;
4390 int idx = tv_get_number_chk(&argvars[1], &error);
4391
4392 if (!error)
4393 {
4394 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004395 if (idx < 0)
4396 idx = blob_len(argvars[0].vval.v_blob) + idx;
4397 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4398 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004399 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004400 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004401 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004402 tv = rettv;
4403 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004404 }
4405 }
4406 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004407 {
4408 if ((l = argvars[0].vval.v_list) != NULL)
4409 {
4410 int error = FALSE;
4411
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004412 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004413 if (!error && li != NULL)
4414 tv = &li->li_tv;
4415 }
4416 }
4417 else if (argvars[0].v_type == VAR_DICT)
4418 {
4419 if ((d = argvars[0].vval.v_dict) != NULL)
4420 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004421 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004422 if (di != NULL)
4423 tv = &di->di_tv;
4424 }
4425 }
4426 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4427 {
4428 partial_T *pt;
4429 partial_T fref_pt;
4430
4431 if (argvars[0].v_type == VAR_PARTIAL)
4432 pt = argvars[0].vval.v_partial;
4433 else
4434 {
4435 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4436 fref_pt.pt_name = argvars[0].vval.v_string;
4437 pt = &fref_pt;
4438 }
4439
4440 if (pt != NULL)
4441 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004442 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004443 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004444
4445 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4446 {
4447 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004448 n = partial_name(pt);
4449 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004450 rettv->vval.v_string = NULL;
4451 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004452 {
4453 rettv->vval.v_string = vim_strsave(n);
4454 if (rettv->v_type == VAR_FUNC)
4455 func_ref(rettv->vval.v_string);
4456 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004457 }
4458 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004459 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004460 else if (STRCMP(what, "args") == 0)
4461 {
4462 rettv->v_type = VAR_LIST;
4463 if (rettv_list_alloc(rettv) == OK)
4464 {
4465 int i;
4466
4467 for (i = 0; i < pt->pt_argc; ++i)
4468 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4469 }
4470 }
4471 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004472 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004473 return;
4474 }
4475 }
4476 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004477 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004478
4479 if (tv == NULL)
4480 {
4481 if (argvars[2].v_type != VAR_UNKNOWN)
4482 copy_tv(&argvars[2], rettv);
4483 }
4484 else
4485 copy_tv(tv, rettv);
4486}
4487
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004488/*
4489 * Returns buffer options, variables and other attributes in a dictionary.
4490 */
4491 static dict_T *
4492get_buffer_info(buf_T *buf)
4493{
4494 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004495 tabpage_T *tp;
4496 win_T *wp;
4497 list_T *windows;
4498
4499 dict = dict_alloc();
4500 if (dict == NULL)
4501 return NULL;
4502
Bram Moolenaare0be1672018-07-08 16:50:37 +02004503 dict_add_number(dict, "bufnr", buf->b_fnum);
4504 dict_add_string(dict, "name", buf->b_ffname);
4505 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4506 : buflist_findlnum(buf));
4507 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4508 dict_add_number(dict, "listed", buf->b_p_bl);
4509 dict_add_number(dict, "changed", bufIsChanged(buf));
4510 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4511 dict_add_number(dict, "hidden",
4512 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004513
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004514 // Get a reference to buffer variables
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004515 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004516
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004517 // List of windows displaying this buffer
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004518 windows = list_alloc();
4519 if (windows != NULL)
4520 {
4521 FOR_ALL_TAB_WINDOWS(tp, wp)
4522 if (wp->w_buffer == buf)
4523 list_append_number(windows, (varnumber_T)wp->w_id);
4524 dict_add_list(dict, "windows", windows);
4525 }
4526
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004527#ifdef FEAT_TEXT_PROP
4528 // List of popup windows displaying this buffer
4529 windows = list_alloc();
4530 if (windows != NULL)
4531 {
4532 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4533 if (wp->w_buffer == buf)
4534 list_append_number(windows, (varnumber_T)wp->w_id);
4535 FOR_ALL_TABPAGES(tp)
4536 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
4537 if (wp->w_buffer == buf)
4538 list_append_number(windows, (varnumber_T)wp->w_id);
4539
4540 dict_add_list(dict, "popups", windows);
4541 }
4542#endif
4543
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004544#ifdef FEAT_SIGNS
4545 if (buf->b_signlist != NULL)
4546 {
4547 /* List of signs placed in this buffer */
4548 list_T *signs = list_alloc();
4549 if (signs != NULL)
4550 {
4551 get_buffer_signs(buf, signs);
4552 dict_add_list(dict, "signs", signs);
4553 }
4554 }
4555#endif
4556
4557 return dict;
4558}
4559
4560/*
4561 * "getbufinfo()" function
4562 */
4563 static void
4564f_getbufinfo(typval_T *argvars, typval_T *rettv)
4565{
4566 buf_T *buf = NULL;
4567 buf_T *argbuf = NULL;
4568 dict_T *d;
4569 int filtered = FALSE;
4570 int sel_buflisted = FALSE;
4571 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004572 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004573
4574 if (rettv_list_alloc(rettv) != OK)
4575 return;
4576
4577 /* List of all the buffers or selected buffers */
4578 if (argvars[0].v_type == VAR_DICT)
4579 {
4580 dict_T *sel_d = argvars[0].vval.v_dict;
4581
4582 if (sel_d != NULL)
4583 {
4584 dictitem_T *di;
4585
4586 filtered = TRUE;
4587
4588 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004589 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004590 sel_buflisted = TRUE;
4591
4592 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004593 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004594 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004595
4596 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004597 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004598 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004599 }
4600 }
4601 else if (argvars[0].v_type != VAR_UNKNOWN)
4602 {
4603 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004604 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004605 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004606 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004607 --emsg_off;
4608 if (argbuf == NULL)
4609 return;
4610 }
4611
4612 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004613 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004614 {
4615 if (argbuf != NULL && argbuf != buf)
4616 continue;
4617 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004618 || (sel_buflisted && !buf->b_p_bl)
4619 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004620 continue;
4621
4622 d = get_buffer_info(buf);
4623 if (d != NULL)
4624 list_append_dict(rettv->vval.v_list, d);
4625 if (argbuf != NULL)
4626 return;
4627 }
4628}
4629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004630/*
4631 * Get line or list of lines from buffer "buf" into "rettv".
4632 * Return a range (from start to end) of lines in rettv from the specified
4633 * buffer.
4634 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4635 */
4636 static void
4637get_buffer_lines(
4638 buf_T *buf,
4639 linenr_T start,
4640 linenr_T end,
4641 int retlist,
4642 typval_T *rettv)
4643{
4644 char_u *p;
4645
4646 rettv->v_type = VAR_STRING;
4647 rettv->vval.v_string = NULL;
4648 if (retlist && rettv_list_alloc(rettv) == FAIL)
4649 return;
4650
4651 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4652 return;
4653
4654 if (!retlist)
4655 {
4656 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4657 p = ml_get_buf(buf, start, FALSE);
4658 else
4659 p = (char_u *)"";
4660 rettv->vval.v_string = vim_strsave(p);
4661 }
4662 else
4663 {
4664 if (end < start)
4665 return;
4666
4667 if (start < 1)
4668 start = 1;
4669 if (end > buf->b_ml.ml_line_count)
4670 end = buf->b_ml.ml_line_count;
4671 while (start <= end)
4672 if (list_append_string(rettv->vval.v_list,
4673 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4674 break;
4675 }
4676}
4677
4678/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004679 * "getbufline()" function
4680 */
4681 static void
4682f_getbufline(typval_T *argvars, typval_T *rettv)
4683{
4684 linenr_T lnum;
4685 linenr_T end;
4686 buf_T *buf;
4687
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004688 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004689 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004690 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004691 --emsg_off;
4692
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004693 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004694 if (argvars[2].v_type == VAR_UNKNOWN)
4695 end = lnum;
4696 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004697 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004698
4699 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4700}
4701
4702/*
4703 * "getbufvar()" function
4704 */
4705 static void
4706f_getbufvar(typval_T *argvars, typval_T *rettv)
4707{
4708 buf_T *buf;
4709 buf_T *save_curbuf;
4710 char_u *varname;
4711 dictitem_T *v;
4712 int done = FALSE;
4713
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004714 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4715 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004716 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004717 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004718
4719 rettv->v_type = VAR_STRING;
4720 rettv->vval.v_string = NULL;
4721
4722 if (buf != NULL && varname != NULL)
4723 {
4724 /* set curbuf to be our buf, temporarily */
4725 save_curbuf = curbuf;
4726 curbuf = buf;
4727
Bram Moolenaar30567352016-08-27 21:25:44 +02004728 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004729 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004730 if (varname[1] == NUL)
4731 {
4732 /* get all buffer-local options in a dict */
4733 dict_T *opts = get_winbuf_options(TRUE);
4734
4735 if (opts != NULL)
4736 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004737 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004738 done = TRUE;
4739 }
4740 }
4741 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4742 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004743 done = TRUE;
4744 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004745 else
4746 {
4747 /* Look up the variable. */
4748 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4749 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4750 'b', varname, FALSE);
4751 if (v != NULL)
4752 {
4753 copy_tv(&v->di_tv, rettv);
4754 done = TRUE;
4755 }
4756 }
4757
4758 /* restore previous notion of curbuf */
4759 curbuf = save_curbuf;
4760 }
4761
4762 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4763 /* use the default value */
4764 copy_tv(&argvars[2], rettv);
4765
4766 --emsg_off;
4767}
4768
4769/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004770 * "getchangelist()" function
4771 */
4772 static void
4773f_getchangelist(typval_T *argvars, typval_T *rettv)
4774{
4775#ifdef FEAT_JUMPLIST
4776 buf_T *buf;
4777 int i;
4778 list_T *l;
4779 dict_T *d;
4780#endif
4781
4782 if (rettv_list_alloc(rettv) != OK)
4783 return;
4784
4785#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004786 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004787 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004788 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004789 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004790 if (buf == NULL)
4791 return;
4792
4793 l = list_alloc();
4794 if (l == NULL)
4795 return;
4796
4797 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4798 return;
4799 /*
4800 * The current window change list index tracks only the position in the
4801 * current buffer change list. For other buffers, use the change list
4802 * length as the current index.
4803 */
4804 list_append_number(rettv->vval.v_list,
4805 (varnumber_T)((buf == curwin->w_buffer)
4806 ? curwin->w_changelistidx : buf->b_changelistlen));
4807
4808 for (i = 0; i < buf->b_changelistlen; ++i)
4809 {
4810 if (buf->b_changelist[i].lnum == 0)
4811 continue;
4812 if ((d = dict_alloc()) == NULL)
4813 return;
4814 if (list_append_dict(l, d) == FAIL)
4815 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004816 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4817 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004818 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004819 }
4820#endif
4821}
4822/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004823 * "getchar()" function
4824 */
4825 static void
4826f_getchar(typval_T *argvars, typval_T *rettv)
4827{
4828 varnumber_T n;
4829 int error = FALSE;
4830
Bram Moolenaar84d93902018-09-11 20:10:20 +02004831#ifdef MESSAGE_QUEUE
4832 // vpeekc() used to check for messages, but that caused problems, invoking
4833 // a callback where it was not expected. Some plugins use getchar(1) in a
4834 // loop to await a message, therefore make sure we check for messages here.
4835 parse_queued_messages();
4836#endif
4837
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004838 /* Position the cursor. Needed after a message that ends in a space. */
4839 windgoto(msg_row, msg_col);
4840
4841 ++no_mapping;
4842 ++allow_keys;
4843 for (;;)
4844 {
4845 if (argvars[0].v_type == VAR_UNKNOWN)
4846 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004847 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004848 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004849 /* getchar(1): only check if char avail */
4850 n = vpeekc_any();
4851 else if (error || vpeekc_any() == NUL)
4852 /* illegal argument or getchar(0) and no char avail: return zero */
4853 n = 0;
4854 else
4855 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004856 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004857
4858 if (n == K_IGNORE)
4859 continue;
4860 break;
4861 }
4862 --no_mapping;
4863 --allow_keys;
4864
4865 set_vim_var_nr(VV_MOUSE_WIN, 0);
4866 set_vim_var_nr(VV_MOUSE_WINID, 0);
4867 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4868 set_vim_var_nr(VV_MOUSE_COL, 0);
4869
4870 rettv->vval.v_number = n;
4871 if (IS_SPECIAL(n) || mod_mask != 0)
4872 {
4873 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4874 int i = 0;
4875
4876 /* Turn a special key into three bytes, plus modifier. */
4877 if (mod_mask != 0)
4878 {
4879 temp[i++] = K_SPECIAL;
4880 temp[i++] = KS_MODIFIER;
4881 temp[i++] = mod_mask;
4882 }
4883 if (IS_SPECIAL(n))
4884 {
4885 temp[i++] = K_SPECIAL;
4886 temp[i++] = K_SECOND(n);
4887 temp[i++] = K_THIRD(n);
4888 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004889 else if (has_mbyte)
4890 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004891 else
4892 temp[i++] = n;
4893 temp[i++] = NUL;
4894 rettv->v_type = VAR_STRING;
4895 rettv->vval.v_string = vim_strsave(temp);
4896
4897#ifdef FEAT_MOUSE
4898 if (is_mouse_key(n))
4899 {
4900 int row = mouse_row;
4901 int col = mouse_col;
4902 win_T *win;
4903 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004904 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004905 int winnr = 1;
4906
4907 if (row >= 0 && col >= 0)
4908 {
4909 /* Find the window at the mouse coordinates and compute the
4910 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004911 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004912 if (win == NULL)
4913 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004914 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004915# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004916 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004917 winnr = 0;
4918 else
4919# endif
4920 for (wp = firstwin; wp != win && wp != NULL;
4921 wp = wp->w_next)
4922 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004923 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4924 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4925 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4926 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4927 }
4928 }
4929#endif
4930 }
4931}
4932
4933/*
4934 * "getcharmod()" function
4935 */
4936 static void
4937f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4938{
4939 rettv->vval.v_number = mod_mask;
4940}
4941
4942/*
4943 * "getcharsearch()" function
4944 */
4945 static void
4946f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4947{
4948 if (rettv_dict_alloc(rettv) != FAIL)
4949 {
4950 dict_T *dict = rettv->vval.v_dict;
4951
Bram Moolenaare0be1672018-07-08 16:50:37 +02004952 dict_add_string(dict, "char", last_csearch());
4953 dict_add_number(dict, "forward", last_csearch_forward());
4954 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004955 }
4956}
4957
4958/*
4959 * "getcmdline()" function
4960 */
4961 static void
4962f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4963{
4964 rettv->v_type = VAR_STRING;
4965 rettv->vval.v_string = get_cmdline_str();
4966}
4967
4968/*
4969 * "getcmdpos()" function
4970 */
4971 static void
4972f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4973{
4974 rettv->vval.v_number = get_cmdline_pos() + 1;
4975}
4976
4977/*
4978 * "getcmdtype()" function
4979 */
4980 static void
4981f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4982{
4983 rettv->v_type = VAR_STRING;
4984 rettv->vval.v_string = alloc(2);
4985 if (rettv->vval.v_string != NULL)
4986 {
4987 rettv->vval.v_string[0] = get_cmdline_type();
4988 rettv->vval.v_string[1] = NUL;
4989 }
4990}
4991
4992/*
4993 * "getcmdwintype()" function
4994 */
4995 static void
4996f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4997{
4998 rettv->v_type = VAR_STRING;
4999 rettv->vval.v_string = NULL;
5000#ifdef FEAT_CMDWIN
5001 rettv->vval.v_string = alloc(2);
5002 if (rettv->vval.v_string != NULL)
5003 {
5004 rettv->vval.v_string[0] = cmdwin_type;
5005 rettv->vval.v_string[1] = NUL;
5006 }
5007#endif
5008}
5009
5010#if defined(FEAT_CMDL_COMPL)
5011/*
5012 * "getcompletion()" function
5013 */
5014 static void
5015f_getcompletion(typval_T *argvars, typval_T *rettv)
5016{
5017 char_u *pat;
5018 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005019 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005020 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5021 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005022
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005023 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005024 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005025
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005026 if (p_wic)
5027 options |= WILD_ICASE;
5028
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005029 /* For filtered results, 'wildignore' is used */
5030 if (!filtered)
5031 options |= WILD_KEEP_ALL;
5032
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005033 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005034 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005035 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005036 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005037 if (xpc.xp_context == EXPAND_NOTHING)
5038 {
5039 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005040 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005041 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005042 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005043 return;
5044 }
5045
5046# if defined(FEAT_MENU)
5047 if (xpc.xp_context == EXPAND_MENUS)
5048 {
5049 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5050 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5051 }
5052# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005053#ifdef FEAT_CSCOPE
5054 if (xpc.xp_context == EXPAND_CSCOPE)
5055 {
5056 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5057 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5058 }
5059#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005060#ifdef FEAT_SIGNS
5061 if (xpc.xp_context == EXPAND_SIGN)
5062 {
5063 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5064 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5065 }
5066#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005067
5068 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5069 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5070 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005071 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005072
5073 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5074
5075 for (i = 0; i < xpc.xp_numfiles; i++)
5076 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5077 }
5078 vim_free(pat);
5079 ExpandCleanup(&xpc);
5080}
5081#endif
5082
5083/*
5084 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005085 *
5086 * Return the current working directory of a window in a tab page.
5087 * First optional argument 'winnr' is the window number or -1 and the second
5088 * optional argument 'tabnr' is the tab page number.
5089 *
5090 * If no arguments are supplied, then return the directory of the current
5091 * window.
5092 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5093 * the specified window.
5094 * If 'winnr' is 0 then return the directory of the current window.
5095 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5096 * directory of the specified tab page. Otherwise return the directory of the
5097 * specified window in the specified tab page.
5098 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099 */
5100 static void
5101f_getcwd(typval_T *argvars, typval_T *rettv)
5102{
5103 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005104 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005105 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005106 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005107
5108 rettv->v_type = VAR_STRING;
5109 rettv->vval.v_string = NULL;
5110
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005111 if (argvars[0].v_type == VAR_NUMBER
5112 && argvars[0].vval.v_number == -1
5113 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005114 global = TRUE;
5115 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005116 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005117
5118 if (wp != NULL && wp->w_localdir != NULL)
5119 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005120 else if (tp != NULL && tp->tp_localdir != NULL)
5121 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5122 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005123 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005124 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005125 rettv->vval.v_string = vim_strsave(globaldir);
5126 else
5127 {
5128 cwd = alloc(MAXPATHL);
5129 if (cwd != NULL)
5130 {
5131 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5132 rettv->vval.v_string = vim_strsave(cwd);
5133 vim_free(cwd);
5134 }
5135 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005136 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005137#ifdef BACKSLASH_IN_FILENAME
5138 if (rettv->vval.v_string != NULL)
5139 slash_adjust(rettv->vval.v_string);
5140#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005141}
5142
5143/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005144 * "getenv()" function
5145 */
5146 static void
5147f_getenv(typval_T *argvars, typval_T *rettv)
5148{
5149 int mustfree = FALSE;
5150 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5151
5152 if (p == NULL)
5153 {
5154 rettv->v_type = VAR_SPECIAL;
5155 rettv->vval.v_number = VVAL_NULL;
5156 return;
5157 }
5158 if (!mustfree)
5159 p = vim_strsave(p);
5160 rettv->vval.v_string = p;
5161 rettv->v_type = VAR_STRING;
5162}
5163
5164/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005165 * "getfontname()" function
5166 */
5167 static void
5168f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5169{
5170 rettv->v_type = VAR_STRING;
5171 rettv->vval.v_string = NULL;
5172#ifdef FEAT_GUI
5173 if (gui.in_use)
5174 {
5175 GuiFont font;
5176 char_u *name = NULL;
5177
5178 if (argvars[0].v_type == VAR_UNKNOWN)
5179 {
5180 /* Get the "Normal" font. Either the name saved by
5181 * hl_set_font_name() or from the font ID. */
5182 font = gui.norm_font;
5183 name = hl_get_font_name();
5184 }
5185 else
5186 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005187 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005188 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5189 return;
5190 font = gui_mch_get_font(name, FALSE);
5191 if (font == NOFONT)
5192 return; /* Invalid font name, return empty string. */
5193 }
5194 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5195 if (argvars[0].v_type != VAR_UNKNOWN)
5196 gui_mch_free_font(font);
5197 }
5198#endif
5199}
5200
5201/*
5202 * "getfperm({fname})" function
5203 */
5204 static void
5205f_getfperm(typval_T *argvars, typval_T *rettv)
5206{
5207 char_u *fname;
5208 stat_T st;
5209 char_u *perm = NULL;
5210 char_u flags[] = "rwx";
5211 int i;
5212
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005213 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005214
5215 rettv->v_type = VAR_STRING;
5216 if (mch_stat((char *)fname, &st) >= 0)
5217 {
5218 perm = vim_strsave((char_u *)"---------");
5219 if (perm != NULL)
5220 {
5221 for (i = 0; i < 9; i++)
5222 {
5223 if (st.st_mode & (1 << (8 - i)))
5224 perm[i] = flags[i % 3];
5225 }
5226 }
5227 }
5228 rettv->vval.v_string = perm;
5229}
5230
5231/*
5232 * "getfsize({fname})" function
5233 */
5234 static void
5235f_getfsize(typval_T *argvars, typval_T *rettv)
5236{
5237 char_u *fname;
5238 stat_T st;
5239
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005240 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005241
5242 rettv->v_type = VAR_NUMBER;
5243
5244 if (mch_stat((char *)fname, &st) >= 0)
5245 {
5246 if (mch_isdir(fname))
5247 rettv->vval.v_number = 0;
5248 else
5249 {
5250 rettv->vval.v_number = (varnumber_T)st.st_size;
5251
5252 /* non-perfect check for overflow */
5253 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5254 rettv->vval.v_number = -2;
5255 }
5256 }
5257 else
5258 rettv->vval.v_number = -1;
5259}
5260
5261/*
5262 * "getftime({fname})" function
5263 */
5264 static void
5265f_getftime(typval_T *argvars, typval_T *rettv)
5266{
5267 char_u *fname;
5268 stat_T st;
5269
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005270 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005271
5272 if (mch_stat((char *)fname, &st) >= 0)
5273 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5274 else
5275 rettv->vval.v_number = -1;
5276}
5277
5278/*
5279 * "getftype({fname})" function
5280 */
5281 static void
5282f_getftype(typval_T *argvars, typval_T *rettv)
5283{
5284 char_u *fname;
5285 stat_T st;
5286 char_u *type = NULL;
5287 char *t;
5288
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005289 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005290
5291 rettv->v_type = VAR_STRING;
5292 if (mch_lstat((char *)fname, &st) >= 0)
5293 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005294 if (S_ISREG(st.st_mode))
5295 t = "file";
5296 else if (S_ISDIR(st.st_mode))
5297 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005298 else if (S_ISLNK(st.st_mode))
5299 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005300 else if (S_ISBLK(st.st_mode))
5301 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005302 else if (S_ISCHR(st.st_mode))
5303 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005304 else if (S_ISFIFO(st.st_mode))
5305 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005306 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005307 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005308 else
5309 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005310 type = vim_strsave((char_u *)t);
5311 }
5312 rettv->vval.v_string = type;
5313}
5314
5315/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005316 * "getjumplist()" function
5317 */
5318 static void
5319f_getjumplist(typval_T *argvars, typval_T *rettv)
5320{
5321#ifdef FEAT_JUMPLIST
5322 win_T *wp;
5323 int i;
5324 list_T *l;
5325 dict_T *d;
5326#endif
5327
5328 if (rettv_list_alloc(rettv) != OK)
5329 return;
5330
5331#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005332 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005333 if (wp == NULL)
5334 return;
5335
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005336 cleanup_jumplist(wp, TRUE);
5337
Bram Moolenaar4f505882018-02-10 21:06:32 +01005338 l = list_alloc();
5339 if (l == NULL)
5340 return;
5341
5342 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5343 return;
5344 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5345
5346 for (i = 0; i < wp->w_jumplistlen; ++i)
5347 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005348 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5349 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005350 if ((d = dict_alloc()) == NULL)
5351 return;
5352 if (list_append_dict(l, d) == FAIL)
5353 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005354 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5355 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005356 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005357 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005358 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005359 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005360 }
5361#endif
5362}
5363
5364/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005365 * "getline(lnum, [end])" function
5366 */
5367 static void
5368f_getline(typval_T *argvars, typval_T *rettv)
5369{
5370 linenr_T lnum;
5371 linenr_T end;
5372 int retlist;
5373
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005374 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005375 if (argvars[1].v_type == VAR_UNKNOWN)
5376 {
5377 end = 0;
5378 retlist = FALSE;
5379 }
5380 else
5381 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005382 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005383 retlist = TRUE;
5384 }
5385
5386 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5387}
5388
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005389#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005390 static void
5391get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5392{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005393 if (what_arg->v_type == VAR_UNKNOWN)
5394 {
5395 if (rettv_list_alloc(rettv) == OK)
5396 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005397 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005398 }
5399 else
5400 {
5401 if (rettv_dict_alloc(rettv) == OK)
5402 if (is_qf || (wp != NULL))
5403 {
5404 if (what_arg->v_type == VAR_DICT)
5405 {
5406 dict_T *d = what_arg->vval.v_dict;
5407
5408 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005409 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005410 }
5411 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005412 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005413 }
5414 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005415}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005416#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005417
5418/*
5419 * "getloclist()" function
5420 */
5421 static void
5422f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5423{
5424#ifdef FEAT_QUICKFIX
5425 win_T *wp;
5426
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005427 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005428 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5429#endif
5430}
5431
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005432/*
5433 * "getmatches()" function
5434 */
5435 static void
5436f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5437{
5438#ifdef FEAT_SEARCH_EXTRA
5439 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005440 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005441 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005442 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005443
Bram Moolenaaraff74912019-03-30 18:11:49 +01005444 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5445 return;
5446
5447 cur = win->w_match_head;
5448 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005449 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005450 dict = dict_alloc();
5451 if (dict == NULL)
5452 return;
5453 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005454 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005455 /* match added with matchaddpos() */
5456 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005457 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005458 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005459 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005460 list_T *l;
5461
5462 llpos = &cur->pos.pos[i];
5463 if (llpos->lnum == 0)
5464 break;
5465 l = list_alloc();
5466 if (l == NULL)
5467 break;
5468 list_append_number(l, (varnumber_T)llpos->lnum);
5469 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005470 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005471 list_append_number(l, (varnumber_T)llpos->col);
5472 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005473 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005474 sprintf(buf, "pos%d", i + 1);
5475 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005476 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005477 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005478 else
5479 {
5480 dict_add_string(dict, "pattern", cur->pattern);
5481 }
5482 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5483 dict_add_number(dict, "priority", (long)cur->priority);
5484 dict_add_number(dict, "id", (long)cur->id);
5485# if defined(FEAT_CONCEAL)
5486 if (cur->conceal_char)
5487 {
5488 char_u buf[MB_MAXBYTES + 1];
5489
5490 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5491 dict_add_string(dict, "conceal", (char_u *)&buf);
5492 }
5493# endif
5494 list_append_dict(rettv->vval.v_list, dict);
5495 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005496 }
5497#endif
5498}
5499
5500/*
5501 * "getpid()" function
5502 */
5503 static void
5504f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5505{
5506 rettv->vval.v_number = mch_get_pid();
5507}
5508
5509 static void
5510getpos_both(
5511 typval_T *argvars,
5512 typval_T *rettv,
5513 int getcurpos)
5514{
5515 pos_T *fp;
5516 list_T *l;
5517 int fnum = -1;
5518
5519 if (rettv_list_alloc(rettv) == OK)
5520 {
5521 l = rettv->vval.v_list;
5522 if (getcurpos)
5523 fp = &curwin->w_cursor;
5524 else
5525 fp = var2fpos(&argvars[0], TRUE, &fnum);
5526 if (fnum != -1)
5527 list_append_number(l, (varnumber_T)fnum);
5528 else
5529 list_append_number(l, (varnumber_T)0);
5530 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5531 : (varnumber_T)0);
5532 list_append_number(l, (fp != NULL)
5533 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5534 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005535 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005536 (varnumber_T)0);
5537 if (getcurpos)
5538 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005539 int save_set_curswant = curwin->w_set_curswant;
5540 colnr_T save_curswant = curwin->w_curswant;
5541 colnr_T save_virtcol = curwin->w_virtcol;
5542
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005543 update_curswant();
5544 list_append_number(l, curwin->w_curswant == MAXCOL ?
5545 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005546
5547 // Do not change "curswant", as it is unexpected that a get
5548 // function has a side effect.
5549 if (save_set_curswant)
5550 {
5551 curwin->w_set_curswant = save_set_curswant;
5552 curwin->w_curswant = save_curswant;
5553 curwin->w_virtcol = save_virtcol;
5554 curwin->w_valid &= ~VALID_VIRTCOL;
5555 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005556 }
5557 }
5558 else
5559 rettv->vval.v_number = FALSE;
5560}
5561
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005562/*
5563 * "getcurpos()" function
5564 */
5565 static void
5566f_getcurpos(typval_T *argvars, typval_T *rettv)
5567{
5568 getpos_both(argvars, rettv, TRUE);
5569}
5570
5571/*
5572 * "getpos(string)" function
5573 */
5574 static void
5575f_getpos(typval_T *argvars, typval_T *rettv)
5576{
5577 getpos_both(argvars, rettv, FALSE);
5578}
5579
5580/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005581 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005582 */
5583 static void
5584f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5585{
5586#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005587 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005588#endif
5589}
5590
5591/*
5592 * "getreg()" function
5593 */
5594 static void
5595f_getreg(typval_T *argvars, typval_T *rettv)
5596{
5597 char_u *strregname;
5598 int regname;
5599 int arg2 = FALSE;
5600 int return_list = FALSE;
5601 int error = FALSE;
5602
5603 if (argvars[0].v_type != VAR_UNKNOWN)
5604 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005605 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005606 error = strregname == NULL;
5607 if (argvars[1].v_type != VAR_UNKNOWN)
5608 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005609 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005610 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005611 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005612 }
5613 }
5614 else
5615 strregname = get_vim_var_str(VV_REG);
5616
5617 if (error)
5618 return;
5619
5620 regname = (strregname == NULL ? '"' : *strregname);
5621 if (regname == 0)
5622 regname = '"';
5623
5624 if (return_list)
5625 {
5626 rettv->v_type = VAR_LIST;
5627 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5628 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5629 if (rettv->vval.v_list == NULL)
5630 (void)rettv_list_alloc(rettv);
5631 else
5632 ++rettv->vval.v_list->lv_refcount;
5633 }
5634 else
5635 {
5636 rettv->v_type = VAR_STRING;
5637 rettv->vval.v_string = get_reg_contents(regname,
5638 arg2 ? GREG_EXPR_SRC : 0);
5639 }
5640}
5641
5642/*
5643 * "getregtype()" function
5644 */
5645 static void
5646f_getregtype(typval_T *argvars, typval_T *rettv)
5647{
5648 char_u *strregname;
5649 int regname;
5650 char_u buf[NUMBUFLEN + 2];
5651 long reglen = 0;
5652
5653 if (argvars[0].v_type != VAR_UNKNOWN)
5654 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005655 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005656 if (strregname == NULL) /* type error; errmsg already given */
5657 {
5658 rettv->v_type = VAR_STRING;
5659 rettv->vval.v_string = NULL;
5660 return;
5661 }
5662 }
5663 else
5664 /* Default to v:register */
5665 strregname = get_vim_var_str(VV_REG);
5666
5667 regname = (strregname == NULL ? '"' : *strregname);
5668 if (regname == 0)
5669 regname = '"';
5670
5671 buf[0] = NUL;
5672 buf[1] = NUL;
5673 switch (get_reg_type(regname, &reglen))
5674 {
5675 case MLINE: buf[0] = 'V'; break;
5676 case MCHAR: buf[0] = 'v'; break;
5677 case MBLOCK:
5678 buf[0] = Ctrl_V;
5679 sprintf((char *)buf + 1, "%ld", reglen + 1);
5680 break;
5681 }
5682 rettv->v_type = VAR_STRING;
5683 rettv->vval.v_string = vim_strsave(buf);
5684}
5685
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005686/*
5687 * Returns information (variables, options, etc.) about a tab page
5688 * as a dictionary.
5689 */
5690 static dict_T *
5691get_tabpage_info(tabpage_T *tp, int tp_idx)
5692{
5693 win_T *wp;
5694 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005695 list_T *l;
5696
5697 dict = dict_alloc();
5698 if (dict == NULL)
5699 return NULL;
5700
Bram Moolenaare0be1672018-07-08 16:50:37 +02005701 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005702
5703 l = list_alloc();
5704 if (l != NULL)
5705 {
5706 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02005707 wp != NULL; wp = wp->w_next)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005708 list_append_number(l, (varnumber_T)wp->w_id);
5709 dict_add_list(dict, "windows", l);
5710 }
5711
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005712 /* Make a reference to tabpage variables */
5713 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005714
5715 return dict;
5716}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005717
5718/*
5719 * "gettabinfo()" function
5720 */
5721 static void
5722f_gettabinfo(typval_T *argvars, typval_T *rettv)
5723{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005724 tabpage_T *tp, *tparg = NULL;
5725 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005726 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005727
5728 if (rettv_list_alloc(rettv) != OK)
5729 return;
5730
5731 if (argvars[0].v_type != VAR_UNKNOWN)
5732 {
5733 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005734 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005735 if (tparg == NULL)
5736 return;
5737 }
5738
5739 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005740 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005741 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005742 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005743 if (tparg != NULL && tp != tparg)
5744 continue;
5745 d = get_tabpage_info(tp, tpnr);
5746 if (d != NULL)
5747 list_append_dict(rettv->vval.v_list, d);
5748 if (tparg != NULL)
5749 return;
5750 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005751}
5752
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005753/*
5754 * "gettabvar()" function
5755 */
5756 static void
5757f_gettabvar(typval_T *argvars, typval_T *rettv)
5758{
5759 win_T *oldcurwin;
5760 tabpage_T *tp, *oldtabpage;
5761 dictitem_T *v;
5762 char_u *varname;
5763 int done = FALSE;
5764
5765 rettv->v_type = VAR_STRING;
5766 rettv->vval.v_string = NULL;
5767
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005768 varname = tv_get_string_chk(&argvars[1]);
5769 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005770 if (tp != NULL && varname != NULL)
5771 {
5772 /* Set tp to be our tabpage, temporarily. Also set the window to the
5773 * first window in the tabpage, otherwise the window is not valid. */
5774 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005775 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5776 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005777 {
5778 /* look up the variable */
5779 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5780 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5781 if (v != NULL)
5782 {
5783 copy_tv(&v->di_tv, rettv);
5784 done = TRUE;
5785 }
5786 }
5787
5788 /* restore previous notion of curwin */
5789 restore_win(oldcurwin, oldtabpage, TRUE);
5790 }
5791
5792 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5793 copy_tv(&argvars[2], rettv);
5794}
5795
5796/*
5797 * "gettabwinvar()" function
5798 */
5799 static void
5800f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5801{
5802 getwinvar(argvars, rettv, 1);
5803}
5804
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005805/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005806 * "gettagstack()" function
5807 */
5808 static void
5809f_gettagstack(typval_T *argvars, typval_T *rettv)
5810{
5811 win_T *wp = curwin; // default is current window
5812
5813 if (rettv_dict_alloc(rettv) != OK)
5814 return;
5815
5816 if (argvars[0].v_type != VAR_UNKNOWN)
5817 {
5818 wp = find_win_by_nr_or_id(&argvars[0]);
5819 if (wp == NULL)
5820 return;
5821 }
5822
5823 get_tagstack(wp, rettv->vval.v_dict);
5824}
5825
5826/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005827 * Returns information about a window as a dictionary.
5828 */
5829 static dict_T *
5830get_win_info(win_T *wp, short tpnr, short winnr)
5831{
5832 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005833
5834 dict = dict_alloc();
5835 if (dict == NULL)
5836 return NULL;
5837
Bram Moolenaare0be1672018-07-08 16:50:37 +02005838 dict_add_number(dict, "tabnr", tpnr);
5839 dict_add_number(dict, "winnr", winnr);
5840 dict_add_number(dict, "winid", wp->w_id);
5841 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005842 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005843 dict_add_number(dict, "topline", wp->w_topline);
5844 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005845#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005846 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005847#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005848 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005849 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005850 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005851
Bram Moolenaar69905d12017-08-13 18:14:47 +02005852#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005853 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005854#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005855#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005856 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5857 dict_add_number(dict, "loclist",
5858 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005859#endif
5860
Bram Moolenaar30567352016-08-27 21:25:44 +02005861 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005862 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005863
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005864 return dict;
5865}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005866
5867/*
5868 * "getwininfo()" function
5869 */
5870 static void
5871f_getwininfo(typval_T *argvars, typval_T *rettv)
5872{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005873 tabpage_T *tp;
5874 win_T *wp = NULL, *wparg = NULL;
5875 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005876 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005877
5878 if (rettv_list_alloc(rettv) != OK)
5879 return;
5880
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005881 if (argvars[0].v_type != VAR_UNKNOWN)
5882 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005883 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005884 if (wparg == NULL)
5885 return;
5886 }
5887
5888 /* Collect information about either all the windows across all the tab
5889 * pages or one particular window.
5890 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005891 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005892 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005893 tabnr++;
5894 winnr = 0;
5895 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005896 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005897 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005898 if (wparg != NULL && wp != wparg)
5899 continue;
5900 d = get_win_info(wp, tabnr, winnr);
5901 if (d != NULL)
5902 list_append_dict(rettv->vval.v_list, d);
5903 if (wparg != NULL)
5904 /* found information about a specific window */
5905 return;
5906 }
5907 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005908}
5909
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005910/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005911 * "win_execute()" function
5912 */
5913 static void
5914f_win_execute(typval_T *argvars, typval_T *rettv)
5915{
5916 int id = (int)tv_get_number(argvars);
5917 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005918 win_T *save_curwin;
5919 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005920
5921 if (wp != NULL)
5922 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005923 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5924 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005925 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005926 check_cursor();
5927 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005928 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005929 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005930 }
5931}
5932
5933/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005934 * "win_findbuf()" function
5935 */
5936 static void
5937f_win_findbuf(typval_T *argvars, typval_T *rettv)
5938{
5939 if (rettv_list_alloc(rettv) != FAIL)
5940 win_findbuf(argvars, rettv->vval.v_list);
5941}
5942
5943/*
5944 * "win_getid()" function
5945 */
5946 static void
5947f_win_getid(typval_T *argvars, typval_T *rettv)
5948{
5949 rettv->vval.v_number = win_getid(argvars);
5950}
5951
5952/*
5953 * "win_gotoid()" function
5954 */
5955 static void
5956f_win_gotoid(typval_T *argvars, typval_T *rettv)
5957{
5958 rettv->vval.v_number = win_gotoid(argvars);
5959}
5960
5961/*
5962 * "win_id2tabwin()" function
5963 */
5964 static void
5965f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5966{
5967 if (rettv_list_alloc(rettv) != FAIL)
5968 win_id2tabwin(argvars, rettv->vval.v_list);
5969}
5970
5971/*
5972 * "win_id2win()" function
5973 */
5974 static void
5975f_win_id2win(typval_T *argvars, typval_T *rettv)
5976{
5977 rettv->vval.v_number = win_id2win(argvars);
5978}
5979
5980/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005981 * "win_screenpos()" function
5982 */
5983 static void
5984f_win_screenpos(typval_T *argvars, typval_T *rettv)
5985{
5986 win_T *wp;
5987
5988 if (rettv_list_alloc(rettv) == FAIL)
5989 return;
5990
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005991 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005992 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5993 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5994}
5995
5996/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005997 * "getwinpos({timeout})" function
5998 */
5999 static void
6000f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6001{
6002 int x = -1;
6003 int y = -1;
6004
6005 if (rettv_list_alloc(rettv) == FAIL)
6006 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006007#if defined(FEAT_GUI) \
6008 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6009 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006010 {
6011 varnumber_T timeout = 100;
6012
6013 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006014 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006015
6016 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006017 }
6018#endif
6019 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6020 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6021}
6022
6023
6024/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006025 * "getwinposx()" function
6026 */
6027 static void
6028f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6029{
6030 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006031#if defined(FEAT_GUI) \
6032 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6033 || defined(MSWIN)
6034
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006035 {
6036 int x, y;
6037
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006038 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006039 rettv->vval.v_number = x;
6040 }
6041#endif
6042}
6043
6044/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006045 * "getwinposy()" function
6046 */
6047 static void
6048f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6049{
6050 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006051#if defined(FEAT_GUI) \
6052 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6053 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006054 {
6055 int x, y;
6056
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006057 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006058 rettv->vval.v_number = y;
6059 }
6060#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006061}
6062
6063/*
6064 * "getwinvar()" function
6065 */
6066 static void
6067f_getwinvar(typval_T *argvars, typval_T *rettv)
6068{
6069 getwinvar(argvars, rettv, 0);
6070}
6071
6072/*
6073 * "glob()" function
6074 */
6075 static void
6076f_glob(typval_T *argvars, typval_T *rettv)
6077{
6078 int options = WILD_SILENT|WILD_USE_NL;
6079 expand_T xpc;
6080 int error = FALSE;
6081
6082 /* When the optional second argument is non-zero, don't remove matches
6083 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6084 rettv->v_type = VAR_STRING;
6085 if (argvars[1].v_type != VAR_UNKNOWN)
6086 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006087 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006088 options |= WILD_KEEP_ALL;
6089 if (argvars[2].v_type != VAR_UNKNOWN)
6090 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006091 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006092 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006093 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006094 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006095 options |= WILD_ALLLINKS;
6096 }
6097 }
6098 if (!error)
6099 {
6100 ExpandInit(&xpc);
6101 xpc.xp_context = EXPAND_FILES;
6102 if (p_wic)
6103 options += WILD_ICASE;
6104 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006105 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006106 NULL, options, WILD_ALL);
6107 else if (rettv_list_alloc(rettv) != FAIL)
6108 {
6109 int i;
6110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006111 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006112 NULL, options, WILD_ALL_KEEP);
6113 for (i = 0; i < xpc.xp_numfiles; i++)
6114 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6115
6116 ExpandCleanup(&xpc);
6117 }
6118 }
6119 else
6120 rettv->vval.v_string = NULL;
6121}
6122
6123/*
6124 * "globpath()" function
6125 */
6126 static void
6127f_globpath(typval_T *argvars, typval_T *rettv)
6128{
6129 int flags = 0;
6130 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006131 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006132 int error = FALSE;
6133 garray_T ga;
6134 int i;
6135
6136 /* When the optional second argument is non-zero, don't remove matches
6137 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6138 rettv->v_type = VAR_STRING;
6139 if (argvars[2].v_type != VAR_UNKNOWN)
6140 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006141 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006142 flags |= WILD_KEEP_ALL;
6143 if (argvars[3].v_type != VAR_UNKNOWN)
6144 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006145 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006146 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006147 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006148 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006149 flags |= WILD_ALLLINKS;
6150 }
6151 }
6152 if (file != NULL && !error)
6153 {
6154 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006155 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006156 if (rettv->v_type == VAR_STRING)
6157 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6158 else if (rettv_list_alloc(rettv) != FAIL)
6159 for (i = 0; i < ga.ga_len; ++i)
6160 list_append_string(rettv->vval.v_list,
6161 ((char_u **)(ga.ga_data))[i], -1);
6162 ga_clear_strings(&ga);
6163 }
6164 else
6165 rettv->vval.v_string = NULL;
6166}
6167
6168/*
6169 * "glob2regpat()" function
6170 */
6171 static void
6172f_glob2regpat(typval_T *argvars, typval_T *rettv)
6173{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006174 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006175
6176 rettv->v_type = VAR_STRING;
6177 rettv->vval.v_string = (pat == NULL)
6178 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6179}
6180
6181/* for VIM_VERSION_ defines */
6182#include "version.h"
6183
6184/*
6185 * "has()" function
6186 */
6187 static void
6188f_has(typval_T *argvars, typval_T *rettv)
6189{
6190 int i;
6191 char_u *name;
6192 int n = FALSE;
6193 static char *(has_list[]) =
6194 {
6195#ifdef AMIGA
6196 "amiga",
6197# ifdef FEAT_ARP
6198 "arp",
6199# endif
6200#endif
6201#ifdef __BEOS__
6202 "beos",
6203#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006204#if defined(BSD) && !defined(MACOS_X)
6205 "bsd",
6206#endif
6207#ifdef hpux
6208 "hpux",
6209#endif
6210#ifdef __linux__
6211 "linux",
6212#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006213#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006214 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6215 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006216# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006217 "macunix", /* Mac OS X, with the darwin feature */
6218 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006219# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220#endif
6221#ifdef __QNX__
6222 "qnx",
6223#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006224#ifdef SUN_SYSTEM
6225 "sun",
6226#else
6227 "moon",
6228#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006229#ifdef UNIX
6230 "unix",
6231#endif
6232#ifdef VMS
6233 "vms",
6234#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006235#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006236 "win32",
6237#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006238#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006239 "win32unix",
6240#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006241#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006242 "win64",
6243#endif
6244#ifdef EBCDIC
6245 "ebcdic",
6246#endif
6247#ifndef CASE_INSENSITIVE_FILENAME
6248 "fname_case",
6249#endif
6250#ifdef HAVE_ACL
6251 "acl",
6252#endif
6253#ifdef FEAT_ARABIC
6254 "arabic",
6255#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006256 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006257#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006258 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006259#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006260#ifdef FEAT_AUTOSERVERNAME
6261 "autoservername",
6262#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006263#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006264 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006265# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006266 "balloon_multiline",
6267# endif
6268#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006269#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006270 "balloon_eval_term",
6271#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006272#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6273 "builtin_terms",
6274# ifdef ALL_BUILTIN_TCAPS
6275 "all_builtin_terms",
6276# endif
6277#endif
6278#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006279 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006280 || defined(FEAT_GUI_MOTIF))
6281 "browsefilter",
6282#endif
6283#ifdef FEAT_BYTEOFF
6284 "byte_offset",
6285#endif
6286#ifdef FEAT_JOB_CHANNEL
6287 "channel",
6288#endif
6289#ifdef FEAT_CINDENT
6290 "cindent",
6291#endif
6292#ifdef FEAT_CLIENTSERVER
6293 "clientserver",
6294#endif
6295#ifdef FEAT_CLIPBOARD
6296 "clipboard",
6297#endif
6298#ifdef FEAT_CMDL_COMPL
6299 "cmdline_compl",
6300#endif
6301#ifdef FEAT_CMDHIST
6302 "cmdline_hist",
6303#endif
6304#ifdef FEAT_COMMENTS
6305 "comments",
6306#endif
6307#ifdef FEAT_CONCEAL
6308 "conceal",
6309#endif
6310#ifdef FEAT_CRYPT
6311 "cryptv",
6312 "crypt-blowfish",
6313 "crypt-blowfish2",
6314#endif
6315#ifdef FEAT_CSCOPE
6316 "cscope",
6317#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006318 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006319#ifdef CURSOR_SHAPE
6320 "cursorshape",
6321#endif
6322#ifdef DEBUG
6323 "debug",
6324#endif
6325#ifdef FEAT_CON_DIALOG
6326 "dialog_con",
6327#endif
6328#ifdef FEAT_GUI_DIALOG
6329 "dialog_gui",
6330#endif
6331#ifdef FEAT_DIFF
6332 "diff",
6333#endif
6334#ifdef FEAT_DIGRAPHS
6335 "digraphs",
6336#endif
6337#ifdef FEAT_DIRECTX
6338 "directx",
6339#endif
6340#ifdef FEAT_DND
6341 "dnd",
6342#endif
6343#ifdef FEAT_EMACS_TAGS
6344 "emacs_tags",
6345#endif
6346 "eval", /* always present, of course! */
6347 "ex_extra", /* graduated feature */
6348#ifdef FEAT_SEARCH_EXTRA
6349 "extra_search",
6350#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006351#ifdef FEAT_SEARCHPATH
6352 "file_in_path",
6353#endif
6354#ifdef FEAT_FILTERPIPE
6355 "filterpipe",
6356#endif
6357#ifdef FEAT_FIND_ID
6358 "find_in_path",
6359#endif
6360#ifdef FEAT_FLOAT
6361 "float",
6362#endif
6363#ifdef FEAT_FOLDING
6364 "folding",
6365#endif
6366#ifdef FEAT_FOOTER
6367 "footer",
6368#endif
6369#if !defined(USE_SYSTEM) && defined(UNIX)
6370 "fork",
6371#endif
6372#ifdef FEAT_GETTEXT
6373 "gettext",
6374#endif
6375#ifdef FEAT_GUI
6376 "gui",
6377#endif
6378#ifdef FEAT_GUI_ATHENA
6379# ifdef FEAT_GUI_NEXTAW
6380 "gui_neXtaw",
6381# else
6382 "gui_athena",
6383# endif
6384#endif
6385#ifdef FEAT_GUI_GTK
6386 "gui_gtk",
6387# ifdef USE_GTK3
6388 "gui_gtk3",
6389# else
6390 "gui_gtk2",
6391# endif
6392#endif
6393#ifdef FEAT_GUI_GNOME
6394 "gui_gnome",
6395#endif
6396#ifdef FEAT_GUI_MAC
6397 "gui_mac",
6398#endif
6399#ifdef FEAT_GUI_MOTIF
6400 "gui_motif",
6401#endif
6402#ifdef FEAT_GUI_PHOTON
6403 "gui_photon",
6404#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006405#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006406 "gui_win32",
6407#endif
6408#ifdef FEAT_HANGULIN
6409 "hangul_input",
6410#endif
6411#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6412 "iconv",
6413#endif
6414#ifdef FEAT_INS_EXPAND
6415 "insert_expand",
6416#endif
6417#ifdef FEAT_JOB_CHANNEL
6418 "job",
6419#endif
6420#ifdef FEAT_JUMPLIST
6421 "jumplist",
6422#endif
6423#ifdef FEAT_KEYMAP
6424 "keymap",
6425#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006426 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006427#ifdef FEAT_LANGMAP
6428 "langmap",
6429#endif
6430#ifdef FEAT_LIBCALL
6431 "libcall",
6432#endif
6433#ifdef FEAT_LINEBREAK
6434 "linebreak",
6435#endif
6436#ifdef FEAT_LISP
6437 "lispindent",
6438#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006439 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006440#ifdef FEAT_LOCALMAP
6441 "localmap",
6442#endif
6443#ifdef FEAT_LUA
6444# ifndef DYNAMIC_LUA
6445 "lua",
6446# endif
6447#endif
6448#ifdef FEAT_MENU
6449 "menu",
6450#endif
6451#ifdef FEAT_SESSION
6452 "mksession",
6453#endif
6454#ifdef FEAT_MODIFY_FNAME
6455 "modify_fname",
6456#endif
6457#ifdef FEAT_MOUSE
6458 "mouse",
6459#endif
6460#ifdef FEAT_MOUSESHAPE
6461 "mouseshape",
6462#endif
6463#if defined(UNIX) || defined(VMS)
6464# ifdef FEAT_MOUSE_DEC
6465 "mouse_dec",
6466# endif
6467# ifdef FEAT_MOUSE_GPM
6468 "mouse_gpm",
6469# endif
6470# ifdef FEAT_MOUSE_JSB
6471 "mouse_jsbterm",
6472# endif
6473# ifdef FEAT_MOUSE_NET
6474 "mouse_netterm",
6475# endif
6476# ifdef FEAT_MOUSE_PTERM
6477 "mouse_pterm",
6478# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006479# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006480 "mouse_sgr",
6481# endif
6482# ifdef FEAT_SYSMOUSE
6483 "mouse_sysmouse",
6484# endif
6485# ifdef FEAT_MOUSE_URXVT
6486 "mouse_urxvt",
6487# endif
6488# ifdef FEAT_MOUSE_XTERM
6489 "mouse_xterm",
6490# endif
6491#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006492 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006493#ifdef FEAT_MBYTE_IME
6494 "multi_byte_ime",
6495#endif
6496#ifdef FEAT_MULTI_LANG
6497 "multi_lang",
6498#endif
6499#ifdef FEAT_MZSCHEME
6500#ifndef DYNAMIC_MZSCHEME
6501 "mzscheme",
6502#endif
6503#endif
6504#ifdef FEAT_NUM64
6505 "num64",
6506#endif
6507#ifdef FEAT_OLE
6508 "ole",
6509#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006510#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006511 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006512#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513#ifdef FEAT_PATH_EXTRA
6514 "path_extra",
6515#endif
6516#ifdef FEAT_PERL
6517#ifndef DYNAMIC_PERL
6518 "perl",
6519#endif
6520#endif
6521#ifdef FEAT_PERSISTENT_UNDO
6522 "persistent_undo",
6523#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006524#if defined(FEAT_PYTHON)
6525 "python_compiled",
6526# if defined(DYNAMIC_PYTHON)
6527 "python_dynamic",
6528# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006530 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006531# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006532#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006533#if defined(FEAT_PYTHON3)
6534 "python3_compiled",
6535# if defined(DYNAMIC_PYTHON3)
6536 "python3_dynamic",
6537# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006538 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006539 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006540# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006541#endif
6542#ifdef FEAT_POSTSCRIPT
6543 "postscript",
6544#endif
6545#ifdef FEAT_PRINTER
6546 "printer",
6547#endif
6548#ifdef FEAT_PROFILE
6549 "profile",
6550#endif
6551#ifdef FEAT_RELTIME
6552 "reltime",
6553#endif
6554#ifdef FEAT_QUICKFIX
6555 "quickfix",
6556#endif
6557#ifdef FEAT_RIGHTLEFT
6558 "rightleft",
6559#endif
6560#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6561 "ruby",
6562#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006563 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006564#ifdef FEAT_CMDL_INFO
6565 "showcmd",
6566 "cmdline_info",
6567#endif
6568#ifdef FEAT_SIGNS
6569 "signs",
6570#endif
6571#ifdef FEAT_SMARTINDENT
6572 "smartindent",
6573#endif
6574#ifdef STARTUPTIME
6575 "startuptime",
6576#endif
6577#ifdef FEAT_STL_OPT
6578 "statusline",
6579#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006580#ifdef FEAT_NETBEANS_INTG
6581 "netbeans_intg",
6582#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006583#ifdef FEAT_SOUND
6584 "sound",
6585#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586#ifdef FEAT_SPELL
6587 "spell",
6588#endif
6589#ifdef FEAT_SYN_HL
6590 "syntax",
6591#endif
6592#if defined(USE_SYSTEM) || !defined(UNIX)
6593 "system",
6594#endif
6595#ifdef FEAT_TAG_BINS
6596 "tag_binary",
6597#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006598#ifdef FEAT_TCL
6599# ifndef DYNAMIC_TCL
6600 "tcl",
6601# endif
6602#endif
6603#ifdef FEAT_TERMGUICOLORS
6604 "termguicolors",
6605#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006606#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006607 "terminal",
6608#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006609#ifdef TERMINFO
6610 "terminfo",
6611#endif
6612#ifdef FEAT_TERMRESPONSE
6613 "termresponse",
6614#endif
6615#ifdef FEAT_TEXTOBJ
6616 "textobjects",
6617#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006618#ifdef FEAT_TEXT_PROP
6619 "textprop",
6620#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006621#ifdef HAVE_TGETENT
6622 "tgetent",
6623#endif
6624#ifdef FEAT_TIMERS
6625 "timers",
6626#endif
6627#ifdef FEAT_TITLE
6628 "title",
6629#endif
6630#ifdef FEAT_TOOLBAR
6631 "toolbar",
6632#endif
6633#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6634 "unnamedplus",
6635#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006636 "user-commands", /* was accidentally included in 5.4 */
6637 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006638#ifdef FEAT_VARTABS
6639 "vartabs",
6640#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006641 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642#ifdef FEAT_VIMINFO
6643 "viminfo",
6644#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006645 "vimscript-1",
6646 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006647 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006648 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006649 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006651 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006652#ifdef FEAT_VTP
6653 "vtp",
6654#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006655#ifdef FEAT_WILDIGN
6656 "wildignore",
6657#endif
6658#ifdef FEAT_WILDMENU
6659 "wildmenu",
6660#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006661 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006662#ifdef FEAT_WAK
6663 "winaltkeys",
6664#endif
6665#ifdef FEAT_WRITEBACKUP
6666 "writebackup",
6667#endif
6668#ifdef FEAT_XIM
6669 "xim",
6670#endif
6671#ifdef FEAT_XFONTSET
6672 "xfontset",
6673#endif
6674#ifdef FEAT_XPM_W32
6675 "xpm",
6676 "xpm_w32", /* for backward compatibility */
6677#else
6678# if defined(HAVE_XPM)
6679 "xpm",
6680# endif
6681#endif
6682#ifdef USE_XSMP
6683 "xsmp",
6684#endif
6685#ifdef USE_XSMP_INTERACT
6686 "xsmp_interact",
6687#endif
6688#ifdef FEAT_XCLIPBOARD
6689 "xterm_clipboard",
6690#endif
6691#ifdef FEAT_XTERM_SAVE
6692 "xterm_save",
6693#endif
6694#if defined(UNIX) && defined(FEAT_X11)
6695 "X11",
6696#endif
6697 NULL
6698 };
6699
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006700 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006701 for (i = 0; has_list[i] != NULL; ++i)
6702 if (STRICMP(name, has_list[i]) == 0)
6703 {
6704 n = TRUE;
6705 break;
6706 }
6707
6708 if (n == FALSE)
6709 {
6710 if (STRNICMP(name, "patch", 5) == 0)
6711 {
6712 if (name[5] == '-'
6713 && STRLEN(name) >= 11
6714 && vim_isdigit(name[6])
6715 && vim_isdigit(name[8])
6716 && vim_isdigit(name[10]))
6717 {
6718 int major = atoi((char *)name + 6);
6719 int minor = atoi((char *)name + 8);
6720
6721 /* Expect "patch-9.9.01234". */
6722 n = (major < VIM_VERSION_MAJOR
6723 || (major == VIM_VERSION_MAJOR
6724 && (minor < VIM_VERSION_MINOR
6725 || (minor == VIM_VERSION_MINOR
6726 && has_patch(atoi((char *)name + 10))))));
6727 }
6728 else
6729 n = has_patch(atoi((char *)name + 5));
6730 }
6731 else if (STRICMP(name, "vim_starting") == 0)
6732 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006733 else if (STRICMP(name, "ttyin") == 0)
6734 n = mch_input_isatty();
6735 else if (STRICMP(name, "ttyout") == 0)
6736 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006737 else if (STRICMP(name, "multi_byte_encoding") == 0)
6738 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006739#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006740 else if (STRICMP(name, "balloon_multiline") == 0)
6741 n = multiline_balloon_available();
6742#endif
6743#ifdef DYNAMIC_TCL
6744 else if (STRICMP(name, "tcl") == 0)
6745 n = tcl_enabled(FALSE);
6746#endif
6747#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6748 else if (STRICMP(name, "iconv") == 0)
6749 n = iconv_enabled(FALSE);
6750#endif
6751#ifdef DYNAMIC_LUA
6752 else if (STRICMP(name, "lua") == 0)
6753 n = lua_enabled(FALSE);
6754#endif
6755#ifdef DYNAMIC_MZSCHEME
6756 else if (STRICMP(name, "mzscheme") == 0)
6757 n = mzscheme_enabled(FALSE);
6758#endif
6759#ifdef DYNAMIC_RUBY
6760 else if (STRICMP(name, "ruby") == 0)
6761 n = ruby_enabled(FALSE);
6762#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006763#ifdef DYNAMIC_PYTHON
6764 else if (STRICMP(name, "python") == 0)
6765 n = python_enabled(FALSE);
6766#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006767#ifdef DYNAMIC_PYTHON3
6768 else if (STRICMP(name, "python3") == 0)
6769 n = python3_enabled(FALSE);
6770#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006771#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6772 else if (STRICMP(name, "pythonx") == 0)
6773 {
6774# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6775 if (p_pyx == 0)
6776 n = python3_enabled(FALSE) || python_enabled(FALSE);
6777 else if (p_pyx == 3)
6778 n = python3_enabled(FALSE);
6779 else if (p_pyx == 2)
6780 n = python_enabled(FALSE);
6781# elif defined(DYNAMIC_PYTHON)
6782 n = python_enabled(FALSE);
6783# elif defined(DYNAMIC_PYTHON3)
6784 n = python3_enabled(FALSE);
6785# endif
6786 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006787#endif
6788#ifdef DYNAMIC_PERL
6789 else if (STRICMP(name, "perl") == 0)
6790 n = perl_enabled(FALSE);
6791#endif
6792#ifdef FEAT_GUI
6793 else if (STRICMP(name, "gui_running") == 0)
6794 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006795# ifdef FEAT_BROWSE
6796 else if (STRICMP(name, "browse") == 0)
6797 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6798# endif
6799#endif
6800#ifdef FEAT_SYN_HL
6801 else if (STRICMP(name, "syntax_items") == 0)
6802 n = syntax_present(curwin);
6803#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006804#ifdef FEAT_VTP
6805 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006806 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006807#endif
6808#ifdef FEAT_NETBEANS_INTG
6809 else if (STRICMP(name, "netbeans_enabled") == 0)
6810 n = netbeans_active();
6811#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006812#ifdef FEAT_MOUSE_GPM
6813 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6814 n = gpm_enabled();
6815#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006816#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006817 else if (STRICMP(name, "terminal") == 0)
6818 n = terminal_enabled();
6819#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006820#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006821 else if (STRICMP(name, "conpty") == 0)
6822 n = use_conpty();
6823#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006824 }
6825
6826 rettv->vval.v_number = n;
6827}
6828
6829/*
6830 * "has_key()" function
6831 */
6832 static void
6833f_has_key(typval_T *argvars, typval_T *rettv)
6834{
6835 if (argvars[0].v_type != VAR_DICT)
6836 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006837 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006838 return;
6839 }
6840 if (argvars[0].vval.v_dict == NULL)
6841 return;
6842
6843 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006844 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006845}
6846
6847/*
6848 * "haslocaldir()" function
6849 */
6850 static void
6851f_haslocaldir(typval_T *argvars, typval_T *rettv)
6852{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006853 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006854 win_T *wp = NULL;
6855
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006856 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6857
6858 // Check for window-local and tab-local directories
6859 if (wp != NULL && wp->w_localdir != NULL)
6860 rettv->vval.v_number = 1;
6861 else if (tp != NULL && tp->tp_localdir != NULL)
6862 rettv->vval.v_number = 2;
6863 else
6864 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006865}
6866
6867/*
6868 * "hasmapto()" function
6869 */
6870 static void
6871f_hasmapto(typval_T *argvars, typval_T *rettv)
6872{
6873 char_u *name;
6874 char_u *mode;
6875 char_u buf[NUMBUFLEN];
6876 int abbr = FALSE;
6877
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006878 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006879 if (argvars[1].v_type == VAR_UNKNOWN)
6880 mode = (char_u *)"nvo";
6881 else
6882 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006883 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006884 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006885 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006886 }
6887
6888 if (map_to_exists(name, mode, abbr))
6889 rettv->vval.v_number = TRUE;
6890 else
6891 rettv->vval.v_number = FALSE;
6892}
6893
6894/*
6895 * "histadd()" function
6896 */
6897 static void
6898f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6899{
6900#ifdef FEAT_CMDHIST
6901 int histype;
6902 char_u *str;
6903 char_u buf[NUMBUFLEN];
6904#endif
6905
6906 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006907 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006908 return;
6909#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006910 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911 histype = str != NULL ? get_histtype(str) : -1;
6912 if (histype >= 0)
6913 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006914 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006915 if (*str != NUL)
6916 {
6917 init_history();
6918 add_to_history(histype, str, FALSE, NUL);
6919 rettv->vval.v_number = TRUE;
6920 return;
6921 }
6922 }
6923#endif
6924}
6925
6926/*
6927 * "histdel()" function
6928 */
6929 static void
6930f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6931{
6932#ifdef FEAT_CMDHIST
6933 int n;
6934 char_u buf[NUMBUFLEN];
6935 char_u *str;
6936
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006937 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006938 if (str == NULL)
6939 n = 0;
6940 else if (argvars[1].v_type == VAR_UNKNOWN)
6941 /* only one argument: clear entire history */
6942 n = clr_history(get_histtype(str));
6943 else if (argvars[1].v_type == VAR_NUMBER)
6944 /* index given: remove that entry */
6945 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006946 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006947 else
6948 /* string given: remove all matching entries */
6949 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006950 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006951 rettv->vval.v_number = n;
6952#endif
6953}
6954
6955/*
6956 * "histget()" function
6957 */
6958 static void
6959f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6960{
6961#ifdef FEAT_CMDHIST
6962 int type;
6963 int idx;
6964 char_u *str;
6965
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006966 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006967 if (str == NULL)
6968 rettv->vval.v_string = NULL;
6969 else
6970 {
6971 type = get_histtype(str);
6972 if (argvars[1].v_type == VAR_UNKNOWN)
6973 idx = get_history_idx(type);
6974 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006975 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006976 /* -1 on type error */
6977 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6978 }
6979#else
6980 rettv->vval.v_string = NULL;
6981#endif
6982 rettv->v_type = VAR_STRING;
6983}
6984
6985/*
6986 * "histnr()" function
6987 */
6988 static void
6989f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6990{
6991 int i;
6992
6993#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006994 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006995
6996 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6997 if (i >= HIST_CMD && i < HIST_COUNT)
6998 i = get_history_idx(i);
6999 else
7000#endif
7001 i = -1;
7002 rettv->vval.v_number = i;
7003}
7004
7005/*
7006 * "highlightID(name)" function
7007 */
7008 static void
7009f_hlID(typval_T *argvars, typval_T *rettv)
7010{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007011 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007012}
7013
7014/*
7015 * "highlight_exists()" function
7016 */
7017 static void
7018f_hlexists(typval_T *argvars, typval_T *rettv)
7019{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007020 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007021}
7022
7023/*
7024 * "hostname()" function
7025 */
7026 static void
7027f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7028{
7029 char_u hostname[256];
7030
7031 mch_get_host_name(hostname, 256);
7032 rettv->v_type = VAR_STRING;
7033 rettv->vval.v_string = vim_strsave(hostname);
7034}
7035
7036/*
7037 * iconv() function
7038 */
7039 static void
7040f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7041{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042 char_u buf1[NUMBUFLEN];
7043 char_u buf2[NUMBUFLEN];
7044 char_u *from, *to, *str;
7045 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046
7047 rettv->v_type = VAR_STRING;
7048 rettv->vval.v_string = NULL;
7049
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007050 str = tv_get_string(&argvars[0]);
7051 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7052 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007053 vimconv.vc_type = CONV_NONE;
7054 convert_setup(&vimconv, from, to);
7055
7056 /* If the encodings are equal, no conversion needed. */
7057 if (vimconv.vc_type == CONV_NONE)
7058 rettv->vval.v_string = vim_strsave(str);
7059 else
7060 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7061
7062 convert_setup(&vimconv, NULL, NULL);
7063 vim_free(from);
7064 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007065}
7066
7067/*
7068 * "indent()" function
7069 */
7070 static void
7071f_indent(typval_T *argvars, typval_T *rettv)
7072{
7073 linenr_T lnum;
7074
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007075 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007076 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7077 rettv->vval.v_number = get_indent_lnum(lnum);
7078 else
7079 rettv->vval.v_number = -1;
7080}
7081
7082/*
7083 * "index()" function
7084 */
7085 static void
7086f_index(typval_T *argvars, typval_T *rettv)
7087{
7088 list_T *l;
7089 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007090 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007091 long idx = 0;
7092 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007093 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007094
7095 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007096 if (argvars[0].v_type == VAR_BLOB)
7097 {
7098 typval_T tv;
7099 int start = 0;
7100
7101 if (argvars[2].v_type != VAR_UNKNOWN)
7102 {
7103 start = tv_get_number_chk(&argvars[2], &error);
7104 if (error)
7105 return;
7106 }
7107 b = argvars[0].vval.v_blob;
7108 if (b == NULL)
7109 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007110 if (start < 0)
7111 {
7112 start = blob_len(b) + start;
7113 if (start < 0)
7114 start = 0;
7115 }
7116
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007117 for (idx = start; idx < blob_len(b); ++idx)
7118 {
7119 tv.v_type = VAR_NUMBER;
7120 tv.vval.v_number = blob_get(b, idx);
7121 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7122 {
7123 rettv->vval.v_number = idx;
7124 return;
7125 }
7126 }
7127 return;
7128 }
7129 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007130 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007131 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007132 return;
7133 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007134
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007135 l = argvars[0].vval.v_list;
7136 if (l != NULL)
7137 {
7138 item = l->lv_first;
7139 if (argvars[2].v_type != VAR_UNKNOWN)
7140 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007141 /* Start at specified item. Use the cached index that list_find()
7142 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007143 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007144 idx = l->lv_idx;
7145 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007146 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007147 if (error)
7148 item = NULL;
7149 }
7150
7151 for ( ; item != NULL; item = item->li_next, ++idx)
7152 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7153 {
7154 rettv->vval.v_number = idx;
7155 break;
7156 }
7157 }
7158}
7159
7160static int inputsecret_flag = 0;
7161
7162/*
7163 * "input()" function
7164 * Also handles inputsecret() when inputsecret is set.
7165 */
7166 static void
7167f_input(typval_T *argvars, typval_T *rettv)
7168{
7169 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7170}
7171
7172/*
7173 * "inputdialog()" function
7174 */
7175 static void
7176f_inputdialog(typval_T *argvars, typval_T *rettv)
7177{
7178#if defined(FEAT_GUI_TEXTDIALOG)
7179 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7180 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7181 {
7182 char_u *message;
7183 char_u buf[NUMBUFLEN];
7184 char_u *defstr = (char_u *)"";
7185
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007186 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007187 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007188 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007189 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7190 else
7191 IObuff[0] = NUL;
7192 if (message != NULL && defstr != NULL
7193 && do_dialog(VIM_QUESTION, NULL, message,
7194 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7195 rettv->vval.v_string = vim_strsave(IObuff);
7196 else
7197 {
7198 if (message != NULL && defstr != NULL
7199 && argvars[1].v_type != VAR_UNKNOWN
7200 && argvars[2].v_type != VAR_UNKNOWN)
7201 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007202 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007203 else
7204 rettv->vval.v_string = NULL;
7205 }
7206 rettv->v_type = VAR_STRING;
7207 }
7208 else
7209#endif
7210 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7211}
7212
7213/*
7214 * "inputlist()" function
7215 */
7216 static void
7217f_inputlist(typval_T *argvars, typval_T *rettv)
7218{
7219 listitem_T *li;
7220 int selected;
7221 int mouse_used;
7222
7223#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007224 /* While starting up, there is no place to enter text. When running tests
7225 * with --not-a-term we assume feedkeys() will be used. */
7226 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007227 return;
7228#endif
7229 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007231 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007232 return;
7233 }
7234
7235 msg_start();
7236 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7237 lines_left = Rows; /* avoid more prompt */
7238 msg_scroll = TRUE;
7239 msg_clr_eos();
7240
7241 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7242 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007243 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007244 msg_putchar('\n');
7245 }
7246
7247 /* Ask for choice. */
7248 selected = prompt_for_number(&mouse_used);
7249 if (mouse_used)
7250 selected -= lines_left;
7251
7252 rettv->vval.v_number = selected;
7253}
7254
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007255static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7256
7257/*
7258 * "inputrestore()" function
7259 */
7260 static void
7261f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7262{
7263 if (ga_userinput.ga_len > 0)
7264 {
7265 --ga_userinput.ga_len;
7266 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7267 + ga_userinput.ga_len);
7268 /* default return is zero == OK */
7269 }
7270 else if (p_verbose > 1)
7271 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007272 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007273 rettv->vval.v_number = 1; /* Failed */
7274 }
7275}
7276
7277/*
7278 * "inputsave()" function
7279 */
7280 static void
7281f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7282{
7283 /* Add an entry to the stack of typeahead storage. */
7284 if (ga_grow(&ga_userinput, 1) == OK)
7285 {
7286 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7287 + ga_userinput.ga_len);
7288 ++ga_userinput.ga_len;
7289 /* default return is zero == OK */
7290 }
7291 else
7292 rettv->vval.v_number = 1; /* Failed */
7293}
7294
7295/*
7296 * "inputsecret()" function
7297 */
7298 static void
7299f_inputsecret(typval_T *argvars, typval_T *rettv)
7300{
7301 ++cmdline_star;
7302 ++inputsecret_flag;
7303 f_input(argvars, rettv);
7304 --cmdline_star;
7305 --inputsecret_flag;
7306}
7307
7308/*
7309 * "insert()" function
7310 */
7311 static void
7312f_insert(typval_T *argvars, typval_T *rettv)
7313{
7314 long before = 0;
7315 listitem_T *item;
7316 list_T *l;
7317 int error = FALSE;
7318
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007319 if (argvars[0].v_type == VAR_BLOB)
7320 {
7321 int val, len;
7322 char_u *p;
7323
7324 len = blob_len(argvars[0].vval.v_blob);
7325 if (argvars[2].v_type != VAR_UNKNOWN)
7326 {
7327 before = (long)tv_get_number_chk(&argvars[2], &error);
7328 if (error)
7329 return; // type error; errmsg already given
7330 if (before < 0 || before > len)
7331 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007332 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007333 return;
7334 }
7335 }
7336 val = tv_get_number_chk(&argvars[1], &error);
7337 if (error)
7338 return;
7339 if (val < 0 || val > 255)
7340 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007341 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007342 return;
7343 }
7344
7345 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7346 return;
7347 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7348 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7349 *(p + before) = val;
7350 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7351
7352 copy_tv(&argvars[0], rettv);
7353 }
7354 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007355 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007356 else if ((l = argvars[0].vval.v_list) != NULL
7357 && !var_check_lock(l->lv_lock,
7358 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007359 {
7360 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007361 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007362 if (error)
7363 return; /* type error; errmsg already given */
7364
7365 if (before == l->lv_len)
7366 item = NULL;
7367 else
7368 {
7369 item = list_find(l, before);
7370 if (item == NULL)
7371 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007372 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007373 l = NULL;
7374 }
7375 }
7376 if (l != NULL)
7377 {
7378 list_insert_tv(l, &argvars[1], item);
7379 copy_tv(&argvars[0], rettv);
7380 }
7381 }
7382}
7383
7384/*
7385 * "invert(expr)" function
7386 */
7387 static void
7388f_invert(typval_T *argvars, typval_T *rettv)
7389{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007390 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007391}
7392
7393/*
7394 * "isdirectory()" function
7395 */
7396 static void
7397f_isdirectory(typval_T *argvars, typval_T *rettv)
7398{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007399 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007400}
7401
7402/*
7403 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7404 * or it refers to a List or Dictionary that is locked.
7405 */
7406 static int
7407tv_islocked(typval_T *tv)
7408{
7409 return (tv->v_lock & VAR_LOCKED)
7410 || (tv->v_type == VAR_LIST
7411 && tv->vval.v_list != NULL
7412 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7413 || (tv->v_type == VAR_DICT
7414 && tv->vval.v_dict != NULL
7415 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7416}
7417
7418/*
7419 * "islocked()" function
7420 */
7421 static void
7422f_islocked(typval_T *argvars, typval_T *rettv)
7423{
7424 lval_T lv;
7425 char_u *end;
7426 dictitem_T *di;
7427
7428 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007429 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007430 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007431 if (end != NULL && lv.ll_name != NULL)
7432 {
7433 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007434 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435 else
7436 {
7437 if (lv.ll_tv == NULL)
7438 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007439 di = find_var(lv.ll_name, NULL, TRUE);
7440 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007441 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007442 /* Consider a variable locked when:
7443 * 1. the variable itself is locked
7444 * 2. the value of the variable is locked.
7445 * 3. the List or Dict value is locked.
7446 */
7447 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7448 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007449 }
7450 }
7451 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007452 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007453 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007454 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007455 else if (lv.ll_list != NULL)
7456 /* List item. */
7457 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7458 else
7459 /* Dictionary item. */
7460 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7461 }
7462 }
7463
7464 clear_lval(&lv);
7465}
7466
7467#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7468/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007469 * "isinf()" function
7470 */
7471 static void
7472f_isinf(typval_T *argvars, typval_T *rettv)
7473{
7474 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7475 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7476}
7477
7478/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007479 * "isnan()" function
7480 */
7481 static void
7482f_isnan(typval_T *argvars, typval_T *rettv)
7483{
7484 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7485 && isnan(argvars[0].vval.v_float);
7486}
7487#endif
7488
7489/*
7490 * "items(dict)" function
7491 */
7492 static void
7493f_items(typval_T *argvars, typval_T *rettv)
7494{
7495 dict_list(argvars, rettv, 2);
7496}
7497
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007498/*
7499 * "join()" function
7500 */
7501 static void
7502f_join(typval_T *argvars, typval_T *rettv)
7503{
7504 garray_T ga;
7505 char_u *sep;
7506
7507 if (argvars[0].v_type != VAR_LIST)
7508 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007509 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007510 return;
7511 }
7512 if (argvars[0].vval.v_list == NULL)
7513 return;
7514 if (argvars[1].v_type == VAR_UNKNOWN)
7515 sep = (char_u *)" ";
7516 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007517 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007518
7519 rettv->v_type = VAR_STRING;
7520
7521 if (sep != NULL)
7522 {
7523 ga_init2(&ga, (int)sizeof(char), 80);
7524 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7525 ga_append(&ga, NUL);
7526 rettv->vval.v_string = (char_u *)ga.ga_data;
7527 }
7528 else
7529 rettv->vval.v_string = NULL;
7530}
7531
7532/*
7533 * "js_decode()" function
7534 */
7535 static void
7536f_js_decode(typval_T *argvars, typval_T *rettv)
7537{
7538 js_read_T reader;
7539
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007540 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007541 reader.js_fill = NULL;
7542 reader.js_used = 0;
7543 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007544 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007545}
7546
7547/*
7548 * "js_encode()" function
7549 */
7550 static void
7551f_js_encode(typval_T *argvars, typval_T *rettv)
7552{
7553 rettv->v_type = VAR_STRING;
7554 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7555}
7556
7557/*
7558 * "json_decode()" function
7559 */
7560 static void
7561f_json_decode(typval_T *argvars, typval_T *rettv)
7562{
7563 js_read_T reader;
7564
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007565 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007566 reader.js_fill = NULL;
7567 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007568 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007569}
7570
7571/*
7572 * "json_encode()" function
7573 */
7574 static void
7575f_json_encode(typval_T *argvars, typval_T *rettv)
7576{
7577 rettv->v_type = VAR_STRING;
7578 rettv->vval.v_string = json_encode(&argvars[0], 0);
7579}
7580
7581/*
7582 * "keys()" function
7583 */
7584 static void
7585f_keys(typval_T *argvars, typval_T *rettv)
7586{
7587 dict_list(argvars, rettv, 0);
7588}
7589
7590/*
7591 * "last_buffer_nr()" function.
7592 */
7593 static void
7594f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7595{
7596 int n = 0;
7597 buf_T *buf;
7598
Bram Moolenaar29323592016-07-24 22:04:11 +02007599 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007600 if (n < buf->b_fnum)
7601 n = buf->b_fnum;
7602
7603 rettv->vval.v_number = n;
7604}
7605
7606/*
7607 * "len()" function
7608 */
7609 static void
7610f_len(typval_T *argvars, typval_T *rettv)
7611{
7612 switch (argvars[0].v_type)
7613 {
7614 case VAR_STRING:
7615 case VAR_NUMBER:
7616 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007617 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007618 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007619 case VAR_BLOB:
7620 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7621 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007622 case VAR_LIST:
7623 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7624 break;
7625 case VAR_DICT:
7626 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7627 break;
7628 case VAR_UNKNOWN:
7629 case VAR_SPECIAL:
7630 case VAR_FLOAT:
7631 case VAR_FUNC:
7632 case VAR_PARTIAL:
7633 case VAR_JOB:
7634 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007635 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007636 break;
7637 }
7638}
7639
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007640 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007641libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007642{
7643#ifdef FEAT_LIBCALL
7644 char_u *string_in;
7645 char_u **string_result;
7646 int nr_result;
7647#endif
7648
7649 rettv->v_type = type;
7650 if (type != VAR_NUMBER)
7651 rettv->vval.v_string = NULL;
7652
7653 if (check_restricted() || check_secure())
7654 return;
7655
7656#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007657 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007658 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7659 {
7660 string_in = NULL;
7661 if (argvars[2].v_type == VAR_STRING)
7662 string_in = argvars[2].vval.v_string;
7663 if (type == VAR_NUMBER)
7664 string_result = NULL;
7665 else
7666 string_result = &rettv->vval.v_string;
7667 if (mch_libcall(argvars[0].vval.v_string,
7668 argvars[1].vval.v_string,
7669 string_in,
7670 argvars[2].vval.v_number,
7671 string_result,
7672 &nr_result) == OK
7673 && type == VAR_NUMBER)
7674 rettv->vval.v_number = nr_result;
7675 }
7676#endif
7677}
7678
7679/*
7680 * "libcall()" function
7681 */
7682 static void
7683f_libcall(typval_T *argvars, typval_T *rettv)
7684{
7685 libcall_common(argvars, rettv, VAR_STRING);
7686}
7687
7688/*
7689 * "libcallnr()" function
7690 */
7691 static void
7692f_libcallnr(typval_T *argvars, typval_T *rettv)
7693{
7694 libcall_common(argvars, rettv, VAR_NUMBER);
7695}
7696
7697/*
7698 * "line(string)" function
7699 */
7700 static void
7701f_line(typval_T *argvars, typval_T *rettv)
7702{
7703 linenr_T lnum = 0;
7704 pos_T *fp;
7705 int fnum;
7706
7707 fp = var2fpos(&argvars[0], TRUE, &fnum);
7708 if (fp != NULL)
7709 lnum = fp->lnum;
7710 rettv->vval.v_number = lnum;
7711}
7712
7713/*
7714 * "line2byte(lnum)" function
7715 */
7716 static void
7717f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7718{
7719#ifndef FEAT_BYTEOFF
7720 rettv->vval.v_number = -1;
7721#else
7722 linenr_T lnum;
7723
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007724 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007725 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7726 rettv->vval.v_number = -1;
7727 else
7728 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7729 if (rettv->vval.v_number >= 0)
7730 ++rettv->vval.v_number;
7731#endif
7732}
7733
7734/*
7735 * "lispindent(lnum)" function
7736 */
7737 static void
7738f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7739{
7740#ifdef FEAT_LISP
7741 pos_T pos;
7742 linenr_T lnum;
7743
7744 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007745 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007746 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7747 {
7748 curwin->w_cursor.lnum = lnum;
7749 rettv->vval.v_number = get_lisp_indent();
7750 curwin->w_cursor = pos;
7751 }
7752 else
7753#endif
7754 rettv->vval.v_number = -1;
7755}
7756
7757/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007758 * "list2str()" function
7759 */
7760 static void
7761f_list2str(typval_T *argvars, typval_T *rettv)
7762{
7763 list_T *l;
7764 listitem_T *li;
7765 garray_T ga;
7766 int utf8 = FALSE;
7767
7768 rettv->v_type = VAR_STRING;
7769 rettv->vval.v_string = NULL;
7770 if (argvars[0].v_type != VAR_LIST)
7771 {
7772 emsg(_(e_invarg));
7773 return;
7774 }
7775
7776 l = argvars[0].vval.v_list;
7777 if (l == NULL)
7778 return; // empty list results in empty string
7779
7780 if (argvars[1].v_type != VAR_UNKNOWN)
7781 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7782
7783 ga_init2(&ga, 1, 80);
7784 if (has_mbyte || utf8)
7785 {
7786 char_u buf[MB_MAXBYTES + 1];
7787 int (*char2bytes)(int, char_u *);
7788
7789 if (utf8 || enc_utf8)
7790 char2bytes = utf_char2bytes;
7791 else
7792 char2bytes = mb_char2bytes;
7793
7794 for (li = l->lv_first; li != NULL; li = li->li_next)
7795 {
7796 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7797 ga_concat(&ga, buf);
7798 }
7799 ga_append(&ga, NUL);
7800 }
7801 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7802 {
7803 for (li = l->lv_first; li != NULL; li = li->li_next)
7804 ga_append(&ga, tv_get_number(&li->li_tv));
7805 ga_append(&ga, NUL);
7806 }
7807
7808 rettv->v_type = VAR_STRING;
7809 rettv->vval.v_string = ga.ga_data;
7810}
7811
7812/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007813 * "localtime()" function
7814 */
7815 static void
7816f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7817{
7818 rettv->vval.v_number = (varnumber_T)time(NULL);
7819}
7820
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007821 static void
7822get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7823{
7824 char_u *keys;
7825 char_u *which;
7826 char_u buf[NUMBUFLEN];
7827 char_u *keys_buf = NULL;
7828 char_u *rhs;
7829 int mode;
7830 int abbr = FALSE;
7831 int get_dict = FALSE;
7832 mapblock_T *mp;
7833 int buffer_local;
7834
7835 /* return empty string for failure */
7836 rettv->v_type = VAR_STRING;
7837 rettv->vval.v_string = NULL;
7838
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007839 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007840 if (*keys == NUL)
7841 return;
7842
7843 if (argvars[1].v_type != VAR_UNKNOWN)
7844 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007845 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007846 if (argvars[2].v_type != VAR_UNKNOWN)
7847 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007848 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007849 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007850 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007851 }
7852 }
7853 else
7854 which = (char_u *)"";
7855 if (which == NULL)
7856 return;
7857
7858 mode = get_map_mode(&which, 0);
7859
7860 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7861 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7862 vim_free(keys_buf);
7863
7864 if (!get_dict)
7865 {
7866 /* Return a string. */
7867 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007868 {
7869 if (*rhs == NUL)
7870 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7871 else
7872 rettv->vval.v_string = str2special_save(rhs, FALSE);
7873 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007874
7875 }
7876 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7877 {
7878 /* Return a dictionary. */
7879 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7880 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7881 dict_T *dict = rettv->vval.v_dict;
7882
Bram Moolenaare0be1672018-07-08 16:50:37 +02007883 dict_add_string(dict, "lhs", lhs);
7884 dict_add_string(dict, "rhs", mp->m_orig_str);
7885 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7886 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7887 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007888 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7889 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007890 dict_add_number(dict, "buffer", (long)buffer_local);
7891 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7892 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007893
7894 vim_free(lhs);
7895 vim_free(mapmode);
7896 }
7897}
7898
7899#ifdef FEAT_FLOAT
7900/*
7901 * "log()" function
7902 */
7903 static void
7904f_log(typval_T *argvars, typval_T *rettv)
7905{
7906 float_T f = 0.0;
7907
7908 rettv->v_type = VAR_FLOAT;
7909 if (get_float_arg(argvars, &f) == OK)
7910 rettv->vval.v_float = log(f);
7911 else
7912 rettv->vval.v_float = 0.0;
7913}
7914
7915/*
7916 * "log10()" function
7917 */
7918 static void
7919f_log10(typval_T *argvars, typval_T *rettv)
7920{
7921 float_T f = 0.0;
7922
7923 rettv->v_type = VAR_FLOAT;
7924 if (get_float_arg(argvars, &f) == OK)
7925 rettv->vval.v_float = log10(f);
7926 else
7927 rettv->vval.v_float = 0.0;
7928}
7929#endif
7930
7931#ifdef FEAT_LUA
7932/*
7933 * "luaeval()" function
7934 */
7935 static void
7936f_luaeval(typval_T *argvars, typval_T *rettv)
7937{
7938 char_u *str;
7939 char_u buf[NUMBUFLEN];
7940
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007941 if (check_restricted() || check_secure())
7942 return;
7943
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007944 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007945 do_luaeval(str, argvars + 1, rettv);
7946}
7947#endif
7948
7949/*
7950 * "map()" function
7951 */
7952 static void
7953f_map(typval_T *argvars, typval_T *rettv)
7954{
7955 filter_map(argvars, rettv, TRUE);
7956}
7957
7958/*
7959 * "maparg()" function
7960 */
7961 static void
7962f_maparg(typval_T *argvars, typval_T *rettv)
7963{
7964 get_maparg(argvars, rettv, TRUE);
7965}
7966
7967/*
7968 * "mapcheck()" function
7969 */
7970 static void
7971f_mapcheck(typval_T *argvars, typval_T *rettv)
7972{
7973 get_maparg(argvars, rettv, FALSE);
7974}
7975
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007976typedef enum
7977{
7978 MATCH_END, /* matchend() */
7979 MATCH_MATCH, /* match() */
7980 MATCH_STR, /* matchstr() */
7981 MATCH_LIST, /* matchlist() */
7982 MATCH_POS /* matchstrpos() */
7983} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007984
7985 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007986find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007987{
7988 char_u *str = NULL;
7989 long len = 0;
7990 char_u *expr = NULL;
7991 char_u *pat;
7992 regmatch_T regmatch;
7993 char_u patbuf[NUMBUFLEN];
7994 char_u strbuf[NUMBUFLEN];
7995 char_u *save_cpo;
7996 long start = 0;
7997 long nth = 1;
7998 colnr_T startcol = 0;
7999 int match = 0;
8000 list_T *l = NULL;
8001 listitem_T *li = NULL;
8002 long idx = 0;
8003 char_u *tofree = NULL;
8004
8005 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8006 save_cpo = p_cpo;
8007 p_cpo = (char_u *)"";
8008
8009 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008010 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008011 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008012 /* type MATCH_LIST: return empty list when there are no matches.
8013 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008014 if (rettv_list_alloc(rettv) == FAIL)
8015 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008016 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008017 && (list_append_string(rettv->vval.v_list,
8018 (char_u *)"", 0) == FAIL
8019 || list_append_number(rettv->vval.v_list,
8020 (varnumber_T)-1) == FAIL
8021 || list_append_number(rettv->vval.v_list,
8022 (varnumber_T)-1) == FAIL
8023 || list_append_number(rettv->vval.v_list,
8024 (varnumber_T)-1) == FAIL))
8025 {
8026 list_free(rettv->vval.v_list);
8027 rettv->vval.v_list = NULL;
8028 goto theend;
8029 }
8030 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008031 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008032 {
8033 rettv->v_type = VAR_STRING;
8034 rettv->vval.v_string = NULL;
8035 }
8036
8037 if (argvars[0].v_type == VAR_LIST)
8038 {
8039 if ((l = argvars[0].vval.v_list) == NULL)
8040 goto theend;
8041 li = l->lv_first;
8042 }
8043 else
8044 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008045 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008046 len = (long)STRLEN(str);
8047 }
8048
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008049 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008050 if (pat == NULL)
8051 goto theend;
8052
8053 if (argvars[2].v_type != VAR_UNKNOWN)
8054 {
8055 int error = FALSE;
8056
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008057 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008058 if (error)
8059 goto theend;
8060 if (l != NULL)
8061 {
8062 li = list_find(l, start);
8063 if (li == NULL)
8064 goto theend;
8065 idx = l->lv_idx; /* use the cached index */
8066 }
8067 else
8068 {
8069 if (start < 0)
8070 start = 0;
8071 if (start > len)
8072 goto theend;
8073 /* When "count" argument is there ignore matches before "start",
8074 * otherwise skip part of the string. Differs when pattern is "^"
8075 * or "\<". */
8076 if (argvars[3].v_type != VAR_UNKNOWN)
8077 startcol = start;
8078 else
8079 {
8080 str += start;
8081 len -= start;
8082 }
8083 }
8084
8085 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008086 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087 if (error)
8088 goto theend;
8089 }
8090
8091 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8092 if (regmatch.regprog != NULL)
8093 {
8094 regmatch.rm_ic = p_ic;
8095
8096 for (;;)
8097 {
8098 if (l != NULL)
8099 {
8100 if (li == NULL)
8101 {
8102 match = FALSE;
8103 break;
8104 }
8105 vim_free(tofree);
8106 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8107 if (str == NULL)
8108 break;
8109 }
8110
8111 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8112
8113 if (match && --nth <= 0)
8114 break;
8115 if (l == NULL && !match)
8116 break;
8117
8118 /* Advance to just after the match. */
8119 if (l != NULL)
8120 {
8121 li = li->li_next;
8122 ++idx;
8123 }
8124 else
8125 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008126 startcol = (colnr_T)(regmatch.startp[0]
8127 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008128 if (startcol > (colnr_T)len
8129 || str + startcol <= regmatch.startp[0])
8130 {
8131 match = FALSE;
8132 break;
8133 }
8134 }
8135 }
8136
8137 if (match)
8138 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008139 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008140 {
8141 listitem_T *li1 = rettv->vval.v_list->lv_first;
8142 listitem_T *li2 = li1->li_next;
8143 listitem_T *li3 = li2->li_next;
8144 listitem_T *li4 = li3->li_next;
8145
8146 vim_free(li1->li_tv.vval.v_string);
8147 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8148 (int)(regmatch.endp[0] - regmatch.startp[0]));
8149 li3->li_tv.vval.v_number =
8150 (varnumber_T)(regmatch.startp[0] - expr);
8151 li4->li_tv.vval.v_number =
8152 (varnumber_T)(regmatch.endp[0] - expr);
8153 if (l != NULL)
8154 li2->li_tv.vval.v_number = (varnumber_T)idx;
8155 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008156 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008157 {
8158 int i;
8159
8160 /* return list with matched string and submatches */
8161 for (i = 0; i < NSUBEXP; ++i)
8162 {
8163 if (regmatch.endp[i] == NULL)
8164 {
8165 if (list_append_string(rettv->vval.v_list,
8166 (char_u *)"", 0) == FAIL)
8167 break;
8168 }
8169 else if (list_append_string(rettv->vval.v_list,
8170 regmatch.startp[i],
8171 (int)(regmatch.endp[i] - regmatch.startp[i]))
8172 == FAIL)
8173 break;
8174 }
8175 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008176 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008177 {
8178 /* return matched string */
8179 if (l != NULL)
8180 copy_tv(&li->li_tv, rettv);
8181 else
8182 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8183 (int)(regmatch.endp[0] - regmatch.startp[0]));
8184 }
8185 else if (l != NULL)
8186 rettv->vval.v_number = idx;
8187 else
8188 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008189 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190 rettv->vval.v_number =
8191 (varnumber_T)(regmatch.startp[0] - str);
8192 else
8193 rettv->vval.v_number =
8194 (varnumber_T)(regmatch.endp[0] - str);
8195 rettv->vval.v_number += (varnumber_T)(str - expr);
8196 }
8197 }
8198 vim_regfree(regmatch.regprog);
8199 }
8200
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008201theend:
8202 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008203 /* matchstrpos() without a list: drop the second item. */
8204 listitem_remove(rettv->vval.v_list,
8205 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008206 vim_free(tofree);
8207 p_cpo = save_cpo;
8208}
8209
8210/*
8211 * "match()" function
8212 */
8213 static void
8214f_match(typval_T *argvars, typval_T *rettv)
8215{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008216 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008217}
8218
Bram Moolenaar95e51472018-07-28 16:55:56 +02008219#ifdef FEAT_SEARCH_EXTRA
8220 static int
8221matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8222{
8223 dictitem_T *di;
8224
8225 if (tv->v_type != VAR_DICT)
8226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008227 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008228 return FAIL;
8229 }
8230
8231 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008232 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008233 (char_u *)"conceal", FALSE);
8234
8235 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8236 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008237 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008238 if (*win == NULL)
8239 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008240 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008241 return FAIL;
8242 }
8243 }
8244
8245 return OK;
8246}
8247#endif
8248
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008249/*
8250 * "matchadd()" function
8251 */
8252 static void
8253f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8254{
8255#ifdef FEAT_SEARCH_EXTRA
8256 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008257 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8258 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008259 int prio = 10; /* default priority */
8260 int id = -1;
8261 int error = FALSE;
8262 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008263 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008264
8265 rettv->vval.v_number = -1;
8266
8267 if (grp == NULL || pat == NULL)
8268 return;
8269 if (argvars[2].v_type != VAR_UNKNOWN)
8270 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008271 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272 if (argvars[3].v_type != VAR_UNKNOWN)
8273 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008274 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008275 if (argvars[4].v_type != VAR_UNKNOWN
8276 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8277 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008278 }
8279 }
8280 if (error == TRUE)
8281 return;
8282 if (id >= 1 && id <= 3)
8283 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008284 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008285 return;
8286 }
8287
Bram Moolenaar95e51472018-07-28 16:55:56 +02008288 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 conceal_char);
8290#endif
8291}
8292
8293/*
8294 * "matchaddpos()" function
8295 */
8296 static void
8297f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8298{
8299#ifdef FEAT_SEARCH_EXTRA
8300 char_u buf[NUMBUFLEN];
8301 char_u *group;
8302 int prio = 10;
8303 int id = -1;
8304 int error = FALSE;
8305 list_T *l;
8306 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008307 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308
8309 rettv->vval.v_number = -1;
8310
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008311 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008312 if (group == NULL)
8313 return;
8314
8315 if (argvars[1].v_type != VAR_LIST)
8316 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008317 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008318 return;
8319 }
8320 l = argvars[1].vval.v_list;
8321 if (l == NULL)
8322 return;
8323
8324 if (argvars[2].v_type != VAR_UNKNOWN)
8325 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008326 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008327 if (argvars[3].v_type != VAR_UNKNOWN)
8328 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008329 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008330
8331 if (argvars[4].v_type != VAR_UNKNOWN
8332 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8333 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008334 }
8335 }
8336 if (error == TRUE)
8337 return;
8338
8339 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8340 if (id == 1 || id == 2)
8341 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008342 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008343 return;
8344 }
8345
Bram Moolenaar95e51472018-07-28 16:55:56 +02008346 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008347 conceal_char);
8348#endif
8349}
8350
8351/*
8352 * "matcharg()" function
8353 */
8354 static void
8355f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8356{
8357 if (rettv_list_alloc(rettv) == OK)
8358 {
8359#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008360 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008361 matchitem_T *m;
8362
8363 if (id >= 1 && id <= 3)
8364 {
8365 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8366 {
8367 list_append_string(rettv->vval.v_list,
8368 syn_id2name(m->hlg_id), -1);
8369 list_append_string(rettv->vval.v_list, m->pattern, -1);
8370 }
8371 else
8372 {
8373 list_append_string(rettv->vval.v_list, NULL, -1);
8374 list_append_string(rettv->vval.v_list, NULL, -1);
8375 }
8376 }
8377#endif
8378 }
8379}
8380
8381/*
8382 * "matchdelete()" function
8383 */
8384 static void
8385f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8386{
8387#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008388 win_T *win = get_optional_window(argvars, 1);
8389
8390 if (win == NULL)
8391 rettv->vval.v_number = -1;
8392 else
8393 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008394 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008395#endif
8396}
8397
8398/*
8399 * "matchend()" function
8400 */
8401 static void
8402f_matchend(typval_T *argvars, typval_T *rettv)
8403{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008404 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008405}
8406
8407/*
8408 * "matchlist()" function
8409 */
8410 static void
8411f_matchlist(typval_T *argvars, typval_T *rettv)
8412{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008413 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008414}
8415
8416/*
8417 * "matchstr()" function
8418 */
8419 static void
8420f_matchstr(typval_T *argvars, typval_T *rettv)
8421{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008422 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008423}
8424
8425/*
8426 * "matchstrpos()" function
8427 */
8428 static void
8429f_matchstrpos(typval_T *argvars, typval_T *rettv)
8430{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008431 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008432}
8433
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008434 static void
8435max_min(typval_T *argvars, typval_T *rettv, int domax)
8436{
8437 varnumber_T n = 0;
8438 varnumber_T i;
8439 int error = FALSE;
8440
8441 if (argvars[0].v_type == VAR_LIST)
8442 {
8443 list_T *l;
8444 listitem_T *li;
8445
8446 l = argvars[0].vval.v_list;
8447 if (l != NULL)
8448 {
8449 li = l->lv_first;
8450 if (li != NULL)
8451 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008452 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008453 for (;;)
8454 {
8455 li = li->li_next;
8456 if (li == NULL)
8457 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008458 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008459 if (domax ? i > n : i < n)
8460 n = i;
8461 }
8462 }
8463 }
8464 }
8465 else if (argvars[0].v_type == VAR_DICT)
8466 {
8467 dict_T *d;
8468 int first = TRUE;
8469 hashitem_T *hi;
8470 int todo;
8471
8472 d = argvars[0].vval.v_dict;
8473 if (d != NULL)
8474 {
8475 todo = (int)d->dv_hashtab.ht_used;
8476 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8477 {
8478 if (!HASHITEM_EMPTY(hi))
8479 {
8480 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008481 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008482 if (first)
8483 {
8484 n = i;
8485 first = FALSE;
8486 }
8487 else if (domax ? i > n : i < n)
8488 n = i;
8489 }
8490 }
8491 }
8492 }
8493 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008494 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008495 rettv->vval.v_number = error ? 0 : n;
8496}
8497
8498/*
8499 * "max()" function
8500 */
8501 static void
8502f_max(typval_T *argvars, typval_T *rettv)
8503{
8504 max_min(argvars, rettv, TRUE);
8505}
8506
8507/*
8508 * "min()" function
8509 */
8510 static void
8511f_min(typval_T *argvars, typval_T *rettv)
8512{
8513 max_min(argvars, rettv, FALSE);
8514}
8515
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008516/*
8517 * Create the directory in which "dir" is located, and higher levels when
8518 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008519 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008520 */
8521 static int
8522mkdir_recurse(char_u *dir, int prot)
8523{
8524 char_u *p;
8525 char_u *updir;
8526 int r = FAIL;
8527
8528 /* Get end of directory name in "dir".
8529 * We're done when it's "/" or "c:/". */
8530 p = gettail_sep(dir);
8531 if (p <= get_past_head(dir))
8532 return OK;
8533
8534 /* If the directory exists we're done. Otherwise: create it.*/
8535 updir = vim_strnsave(dir, (int)(p - dir));
8536 if (updir == NULL)
8537 return FAIL;
8538 if (mch_isdir(updir))
8539 r = OK;
8540 else if (mkdir_recurse(updir, prot) == OK)
8541 r = vim_mkdir_emsg(updir, prot);
8542 vim_free(updir);
8543 return r;
8544}
8545
8546#ifdef vim_mkdir
8547/*
8548 * "mkdir()" function
8549 */
8550 static void
8551f_mkdir(typval_T *argvars, typval_T *rettv)
8552{
8553 char_u *dir;
8554 char_u buf[NUMBUFLEN];
8555 int prot = 0755;
8556
8557 rettv->vval.v_number = FAIL;
8558 if (check_restricted() || check_secure())
8559 return;
8560
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008561 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008563 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008564
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008565 if (*gettail(dir) == NUL)
8566 /* remove trailing slashes */
8567 *gettail_sep(dir) = NUL;
8568
8569 if (argvars[1].v_type != VAR_UNKNOWN)
8570 {
8571 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008572 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008573 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008574 if (prot == -1)
8575 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008577 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008578 {
8579 if (mch_isdir(dir))
8580 {
8581 /* With the "p" flag it's OK if the dir already exists. */
8582 rettv->vval.v_number = OK;
8583 return;
8584 }
8585 mkdir_recurse(dir, prot);
8586 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008588 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008589}
8590#endif
8591
8592/*
8593 * "mode()" function
8594 */
8595 static void
8596f_mode(typval_T *argvars, typval_T *rettv)
8597{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008598 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008599
Bram Moolenaar612cc382018-07-29 15:34:26 +02008600 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008601
8602 if (time_for_testing == 93784)
8603 {
8604 /* Testing the two-character code. */
8605 buf[0] = 'x';
8606 buf[1] = '!';
8607 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008608#ifdef FEAT_TERMINAL
8609 else if (term_use_loop())
8610 buf[0] = 't';
8611#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612 else if (VIsual_active)
8613 {
8614 if (VIsual_select)
8615 buf[0] = VIsual_mode + 's' - 'v';
8616 else
8617 buf[0] = VIsual_mode;
8618 }
8619 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8620 || State == CONFIRM)
8621 {
8622 buf[0] = 'r';
8623 if (State == ASKMORE)
8624 buf[1] = 'm';
8625 else if (State == CONFIRM)
8626 buf[1] = '?';
8627 }
8628 else if (State == EXTERNCMD)
8629 buf[0] = '!';
8630 else if (State & INSERT)
8631 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008632 if (State & VREPLACE_FLAG)
8633 {
8634 buf[0] = 'R';
8635 buf[1] = 'v';
8636 }
8637 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008638 {
8639 if (State & REPLACE_FLAG)
8640 buf[0] = 'R';
8641 else
8642 buf[0] = 'i';
8643#ifdef FEAT_INS_EXPAND
8644 if (ins_compl_active())
8645 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008646 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008647 buf[1] = 'x';
8648#endif
8649 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008650 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008651 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008652 {
8653 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008654 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008656 else if (exmode_active == EXMODE_NORMAL)
8657 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008658 }
8659 else
8660 {
8661 buf[0] = 'n';
8662 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008663 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008664 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008665 // to be able to detect force-linewise/blockwise/characterwise operations
8666 buf[2] = motion_force;
8667 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008668 else if (restart_edit == 'I' || restart_edit == 'R'
8669 || restart_edit == 'V')
8670 {
8671 buf[1] = 'i';
8672 buf[2] = restart_edit;
8673 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674 }
8675
8676 /* Clear out the minor mode when the argument is not a non-zero number or
8677 * non-empty string. */
8678 if (!non_zero_arg(&argvars[0]))
8679 buf[1] = NUL;
8680
8681 rettv->vval.v_string = vim_strsave(buf);
8682 rettv->v_type = VAR_STRING;
8683}
8684
8685#if defined(FEAT_MZSCHEME) || defined(PROTO)
8686/*
8687 * "mzeval()" function
8688 */
8689 static void
8690f_mzeval(typval_T *argvars, typval_T *rettv)
8691{
8692 char_u *str;
8693 char_u buf[NUMBUFLEN];
8694
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008695 if (check_restricted() || check_secure())
8696 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008697 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008698 do_mzeval(str, rettv);
8699}
8700
8701 void
8702mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8703{
8704 typval_T argvars[3];
8705
8706 argvars[0].v_type = VAR_STRING;
8707 argvars[0].vval.v_string = name;
8708 copy_tv(args, &argvars[1]);
8709 argvars[2].v_type = VAR_UNKNOWN;
8710 f_call(argvars, rettv);
8711 clear_tv(&argvars[1]);
8712}
8713#endif
8714
8715/*
8716 * "nextnonblank()" function
8717 */
8718 static void
8719f_nextnonblank(typval_T *argvars, typval_T *rettv)
8720{
8721 linenr_T lnum;
8722
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008723 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008724 {
8725 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8726 {
8727 lnum = 0;
8728 break;
8729 }
8730 if (*skipwhite(ml_get(lnum)) != NUL)
8731 break;
8732 }
8733 rettv->vval.v_number = lnum;
8734}
8735
8736/*
8737 * "nr2char()" function
8738 */
8739 static void
8740f_nr2char(typval_T *argvars, typval_T *rettv)
8741{
8742 char_u buf[NUMBUFLEN];
8743
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008744 if (has_mbyte)
8745 {
8746 int utf8 = 0;
8747
8748 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008749 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008750 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008751 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008752 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008753 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754 }
8755 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008756 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008757 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758 buf[1] = NUL;
8759 }
8760 rettv->v_type = VAR_STRING;
8761 rettv->vval.v_string = vim_strsave(buf);
8762}
8763
8764/*
8765 * "or(expr, expr)" function
8766 */
8767 static void
8768f_or(typval_T *argvars, typval_T *rettv)
8769{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008770 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8771 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008772}
8773
8774/*
8775 * "pathshorten()" function
8776 */
8777 static void
8778f_pathshorten(typval_T *argvars, typval_T *rettv)
8779{
8780 char_u *p;
8781
8782 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008783 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008784 if (p == NULL)
8785 rettv->vval.v_string = NULL;
8786 else
8787 {
8788 p = vim_strsave(p);
8789 rettv->vval.v_string = p;
8790 if (p != NULL)
8791 shorten_dir(p);
8792 }
8793}
8794
8795#ifdef FEAT_PERL
8796/*
8797 * "perleval()" function
8798 */
8799 static void
8800f_perleval(typval_T *argvars, typval_T *rettv)
8801{
8802 char_u *str;
8803 char_u buf[NUMBUFLEN];
8804
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008805 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008806 do_perleval(str, rettv);
8807}
8808#endif
8809
8810#ifdef FEAT_FLOAT
8811/*
8812 * "pow()" function
8813 */
8814 static void
8815f_pow(typval_T *argvars, typval_T *rettv)
8816{
8817 float_T fx = 0.0, fy = 0.0;
8818
8819 rettv->v_type = VAR_FLOAT;
8820 if (get_float_arg(argvars, &fx) == OK
8821 && get_float_arg(&argvars[1], &fy) == OK)
8822 rettv->vval.v_float = pow(fx, fy);
8823 else
8824 rettv->vval.v_float = 0.0;
8825}
8826#endif
8827
8828/*
8829 * "prevnonblank()" function
8830 */
8831 static void
8832f_prevnonblank(typval_T *argvars, typval_T *rettv)
8833{
8834 linenr_T lnum;
8835
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008836 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008837 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8838 lnum = 0;
8839 else
8840 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8841 --lnum;
8842 rettv->vval.v_number = lnum;
8843}
8844
8845/* This dummy va_list is here because:
8846 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8847 * - locally in the function results in a "used before set" warning
8848 * - using va_start() to initialize it gives "function with fixed args" error */
8849static va_list ap;
8850
8851/*
8852 * "printf()" function
8853 */
8854 static void
8855f_printf(typval_T *argvars, typval_T *rettv)
8856{
8857 char_u buf[NUMBUFLEN];
8858 int len;
8859 char_u *s;
8860 int saved_did_emsg = did_emsg;
8861 char *fmt;
8862
8863 rettv->v_type = VAR_STRING;
8864 rettv->vval.v_string = NULL;
8865
8866 /* Get the required length, allocate the buffer and do it for real. */
8867 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008868 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008869 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008870 if (!did_emsg)
8871 {
8872 s = alloc(len + 1);
8873 if (s != NULL)
8874 {
8875 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008876 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8877 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008878 }
8879 }
8880 did_emsg |= saved_did_emsg;
8881}
8882
8883/*
8884 * "pumvisible()" function
8885 */
8886 static void
8887f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8888{
8889#ifdef FEAT_INS_EXPAND
8890 if (pum_visible())
8891 rettv->vval.v_number = 1;
8892#endif
8893}
8894
8895#ifdef FEAT_PYTHON3
8896/*
8897 * "py3eval()" function
8898 */
8899 static void
8900f_py3eval(typval_T *argvars, typval_T *rettv)
8901{
8902 char_u *str;
8903 char_u buf[NUMBUFLEN];
8904
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008905 if (check_restricted() || check_secure())
8906 return;
8907
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008908 if (p_pyx == 0)
8909 p_pyx = 3;
8910
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008911 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008912 do_py3eval(str, rettv);
8913}
8914#endif
8915
8916#ifdef FEAT_PYTHON
8917/*
8918 * "pyeval()" function
8919 */
8920 static void
8921f_pyeval(typval_T *argvars, typval_T *rettv)
8922{
8923 char_u *str;
8924 char_u buf[NUMBUFLEN];
8925
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008926 if (check_restricted() || check_secure())
8927 return;
8928
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008929 if (p_pyx == 0)
8930 p_pyx = 2;
8931
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008932 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008933 do_pyeval(str, rettv);
8934}
8935#endif
8936
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008937#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8938/*
8939 * "pyxeval()" function
8940 */
8941 static void
8942f_pyxeval(typval_T *argvars, typval_T *rettv)
8943{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008944 if (check_restricted() || check_secure())
8945 return;
8946
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008947# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8948 init_pyxversion();
8949 if (p_pyx == 2)
8950 f_pyeval(argvars, rettv);
8951 else
8952 f_py3eval(argvars, rettv);
8953# elif defined(FEAT_PYTHON)
8954 f_pyeval(argvars, rettv);
8955# elif defined(FEAT_PYTHON3)
8956 f_py3eval(argvars, rettv);
8957# endif
8958}
8959#endif
8960
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961/*
8962 * "range()" function
8963 */
8964 static void
8965f_range(typval_T *argvars, typval_T *rettv)
8966{
8967 varnumber_T start;
8968 varnumber_T end;
8969 varnumber_T stride = 1;
8970 varnumber_T i;
8971 int error = FALSE;
8972
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008973 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008974 if (argvars[1].v_type == VAR_UNKNOWN)
8975 {
8976 end = start - 1;
8977 start = 0;
8978 }
8979 else
8980 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008981 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008982 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008983 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008984 }
8985
8986 if (error)
8987 return; /* type error; errmsg already given */
8988 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008989 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008990 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008991 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008992 else
8993 {
8994 if (rettv_list_alloc(rettv) == OK)
8995 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8996 if (list_append_number(rettv->vval.v_list,
8997 (varnumber_T)i) == FAIL)
8998 break;
8999 }
9000}
9001
9002/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009003 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009004 */
9005 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009006readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009007{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009008 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009009 typval_T save_val;
9010 typval_T rettv;
9011 typval_T argv[2];
9012 int retval = 0;
9013 int error = FALSE;
9014
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009015 if (expr->v_type == VAR_UNKNOWN)
9016 return 1;
9017
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009018 prepare_vimvar(VV_VAL, &save_val);
9019 set_vim_var_string(VV_VAL, name, -1);
9020 argv[0].v_type = VAR_STRING;
9021 argv[0].vval.v_string = name;
9022
9023 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9024 goto theend;
9025
9026 retval = tv_get_number_chk(&rettv, &error);
9027 if (error)
9028 retval = -1;
9029 clear_tv(&rettv);
9030
9031theend:
9032 set_vim_var_string(VV_VAL, NULL, 0);
9033 restore_vimvar(VV_VAL, &save_val);
9034 return retval;
9035}
9036
9037/*
9038 * "readdir()" function
9039 */
9040 static void
9041f_readdir(typval_T *argvars, typval_T *rettv)
9042{
9043 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009044 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009045 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009046 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009047 garray_T ga;
9048 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009049
9050 if (rettv_list_alloc(rettv) == FAIL)
9051 return;
9052 path = tv_get_string(&argvars[0]);
9053 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009054
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009055 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9056 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009057 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009058 for (i = 0; i < ga.ga_len; i++)
9059 {
9060 p = ((char_u **)ga.ga_data)[i];
9061 list_append_string(rettv->vval.v_list, p, -1);
9062 }
9063 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009064 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009065}
9066
9067/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009068 * "readfile()" function
9069 */
9070 static void
9071f_readfile(typval_T *argvars, typval_T *rettv)
9072{
9073 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009074 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 int failed = FALSE;
9076 char_u *fname;
9077 FILE *fd;
9078 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9079 int io_size = sizeof(buf);
9080 int readlen; /* size of last fread() */
9081 char_u *prev = NULL; /* previously read bytes, if any */
9082 long prevlen = 0; /* length of data in prev */
9083 long prevsize = 0; /* size of prev buffer */
9084 long maxline = MAXLNUM;
9085 long cnt = 0;
9086 char_u *p; /* position in buf */
9087 char_u *start; /* start of current line */
9088
9089 if (argvars[1].v_type != VAR_UNKNOWN)
9090 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009091 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009092 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009093 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9094 blob = TRUE;
9095
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009096 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009097 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009098 }
9099
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009100 if (blob)
9101 {
9102 if (rettv_blob_alloc(rettv) == FAIL)
9103 return;
9104 }
9105 else
9106 {
9107 if (rettv_list_alloc(rettv) == FAIL)
9108 return;
9109 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009110
9111 /* Always open the file in binary mode, library functions have a mind of
9112 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009113 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009114 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9115 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009116 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009117 return;
9118 }
9119
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009120 if (blob)
9121 {
9122 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009124 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009125 blob_free(rettv->vval.v_blob);
9126 }
9127 fclose(fd);
9128 return;
9129 }
9130
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009131 while (cnt < maxline || maxline < 0)
9132 {
9133 readlen = (int)fread(buf, 1, io_size, fd);
9134
9135 /* This for loop processes what was read, but is also entered at end
9136 * of file so that either:
9137 * - an incomplete line gets written
9138 * - a "binary" file gets an empty line at the end if it ends in a
9139 * newline. */
9140 for (p = buf, start = buf;
9141 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9142 ++p)
9143 {
9144 if (*p == '\n' || readlen <= 0)
9145 {
9146 listitem_T *li;
9147 char_u *s = NULL;
9148 long_u len = p - start;
9149
9150 /* Finished a line. Remove CRs before NL. */
9151 if (readlen > 0 && !binary)
9152 {
9153 while (len > 0 && start[len - 1] == '\r')
9154 --len;
9155 /* removal may cross back to the "prev" string */
9156 if (len == 0)
9157 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9158 --prevlen;
9159 }
9160 if (prevlen == 0)
9161 s = vim_strnsave(start, (int)len);
9162 else
9163 {
9164 /* Change "prev" buffer to be the right size. This way
9165 * the bytes are only copied once, and very long lines are
9166 * allocated only once. */
9167 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9168 {
9169 mch_memmove(s + prevlen, start, len);
9170 s[prevlen + len] = NUL;
9171 prev = NULL; /* the list will own the string */
9172 prevlen = prevsize = 0;
9173 }
9174 }
9175 if (s == NULL)
9176 {
9177 do_outofmem_msg((long_u) prevlen + len + 1);
9178 failed = TRUE;
9179 break;
9180 }
9181
9182 if ((li = listitem_alloc()) == NULL)
9183 {
9184 vim_free(s);
9185 failed = TRUE;
9186 break;
9187 }
9188 li->li_tv.v_type = VAR_STRING;
9189 li->li_tv.v_lock = 0;
9190 li->li_tv.vval.v_string = s;
9191 list_append(rettv->vval.v_list, li);
9192
9193 start = p + 1; /* step over newline */
9194 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9195 break;
9196 }
9197 else if (*p == NUL)
9198 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009199 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9200 * when finding the BF and check the previous two bytes. */
9201 else if (*p == 0xbf && enc_utf8 && !binary)
9202 {
9203 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9204 * + 1, these may be in the "prev" string. */
9205 char_u back1 = p >= buf + 1 ? p[-1]
9206 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9207 char_u back2 = p >= buf + 2 ? p[-2]
9208 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9209 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9210
9211 if (back2 == 0xef && back1 == 0xbb)
9212 {
9213 char_u *dest = p - 2;
9214
9215 /* Usually a BOM is at the beginning of a file, and so at
9216 * the beginning of a line; then we can just step over it.
9217 */
9218 if (start == dest)
9219 start = p + 1;
9220 else
9221 {
9222 /* have to shuffle buf to close gap */
9223 int adjust_prevlen = 0;
9224
9225 if (dest < buf)
9226 {
9227 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9228 dest = buf;
9229 }
9230 if (readlen > p - buf + 1)
9231 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9232 readlen -= 3 - adjust_prevlen;
9233 prevlen -= adjust_prevlen;
9234 p = dest - 1;
9235 }
9236 }
9237 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009238 } /* for */
9239
9240 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9241 break;
9242 if (start < p)
9243 {
9244 /* There's part of a line in buf, store it in "prev". */
9245 if (p - start + prevlen >= prevsize)
9246 {
9247 /* need bigger "prev" buffer */
9248 char_u *newprev;
9249
9250 /* A common use case is ordinary text files and "prev" gets a
9251 * fragment of a line, so the first allocation is made
9252 * small, to avoid repeatedly 'allocing' large and
9253 * 'reallocing' small. */
9254 if (prevsize == 0)
9255 prevsize = (long)(p - start);
9256 else
9257 {
9258 long grow50pc = (prevsize * 3) / 2;
9259 long growmin = (long)((p - start) * 2 + prevlen);
9260 prevsize = grow50pc > growmin ? grow50pc : growmin;
9261 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009262 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009263 if (newprev == NULL)
9264 {
9265 do_outofmem_msg((long_u)prevsize);
9266 failed = TRUE;
9267 break;
9268 }
9269 prev = newprev;
9270 }
9271 /* Add the line part to end of "prev". */
9272 mch_memmove(prev + prevlen, start, p - start);
9273 prevlen += (long)(p - start);
9274 }
9275 } /* while */
9276
9277 /*
9278 * For a negative line count use only the lines at the end of the file,
9279 * free the rest.
9280 */
9281 if (!failed && maxline < 0)
9282 while (cnt > -maxline)
9283 {
9284 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9285 --cnt;
9286 }
9287
9288 if (failed)
9289 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009290 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009291 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009292 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009293 }
9294
9295 vim_free(prev);
9296 fclose(fd);
9297}
9298
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009299 static void
9300return_register(int regname, typval_T *rettv)
9301{
9302 char_u buf[2] = {0, 0};
9303
9304 buf[0] = (char_u)regname;
9305 rettv->v_type = VAR_STRING;
9306 rettv->vval.v_string = vim_strsave(buf);
9307}
9308
9309/*
9310 * "reg_executing()" function
9311 */
9312 static void
9313f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9314{
9315 return_register(reg_executing, rettv);
9316}
9317
9318/*
9319 * "reg_recording()" function
9320 */
9321 static void
9322f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9323{
9324 return_register(reg_recording, rettv);
9325}
9326
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009327#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009328/*
9329 * Convert a List to proftime_T.
9330 * Return FAIL when there is something wrong.
9331 */
9332 static int
9333list2proftime(typval_T *arg, proftime_T *tm)
9334{
9335 long n1, n2;
9336 int error = FALSE;
9337
9338 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9339 || arg->vval.v_list->lv_len != 2)
9340 return FAIL;
9341 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9342 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009343# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009344 tm->HighPart = n1;
9345 tm->LowPart = n2;
9346# else
9347 tm->tv_sec = n1;
9348 tm->tv_usec = n2;
9349# endif
9350 return error ? FAIL : OK;
9351}
9352#endif /* FEAT_RELTIME */
9353
9354/*
9355 * "reltime()" function
9356 */
9357 static void
9358f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9359{
9360#ifdef FEAT_RELTIME
9361 proftime_T res;
9362 proftime_T start;
9363
9364 if (argvars[0].v_type == VAR_UNKNOWN)
9365 {
9366 /* No arguments: get current time. */
9367 profile_start(&res);
9368 }
9369 else if (argvars[1].v_type == VAR_UNKNOWN)
9370 {
9371 if (list2proftime(&argvars[0], &res) == FAIL)
9372 return;
9373 profile_end(&res);
9374 }
9375 else
9376 {
9377 /* Two arguments: compute the difference. */
9378 if (list2proftime(&argvars[0], &start) == FAIL
9379 || list2proftime(&argvars[1], &res) == FAIL)
9380 return;
9381 profile_sub(&res, &start);
9382 }
9383
9384 if (rettv_list_alloc(rettv) == OK)
9385 {
9386 long n1, n2;
9387
Bram Moolenaar4f974752019-02-17 17:44:42 +01009388# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389 n1 = res.HighPart;
9390 n2 = res.LowPart;
9391# else
9392 n1 = res.tv_sec;
9393 n2 = res.tv_usec;
9394# endif
9395 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9396 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9397 }
9398#endif
9399}
9400
9401#ifdef FEAT_FLOAT
9402/*
9403 * "reltimefloat()" function
9404 */
9405 static void
9406f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9407{
9408# ifdef FEAT_RELTIME
9409 proftime_T tm;
9410# endif
9411
9412 rettv->v_type = VAR_FLOAT;
9413 rettv->vval.v_float = 0;
9414# ifdef FEAT_RELTIME
9415 if (list2proftime(&argvars[0], &tm) == OK)
9416 rettv->vval.v_float = profile_float(&tm);
9417# endif
9418}
9419#endif
9420
9421/*
9422 * "reltimestr()" function
9423 */
9424 static void
9425f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9426{
9427#ifdef FEAT_RELTIME
9428 proftime_T tm;
9429#endif
9430
9431 rettv->v_type = VAR_STRING;
9432 rettv->vval.v_string = NULL;
9433#ifdef FEAT_RELTIME
9434 if (list2proftime(&argvars[0], &tm) == OK)
9435 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9436#endif
9437}
9438
9439#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009440 static void
9441make_connection(void)
9442{
9443 if (X_DISPLAY == NULL
9444# ifdef FEAT_GUI
9445 && !gui.in_use
9446# endif
9447 )
9448 {
9449 x_force_connect = TRUE;
9450 setup_term_clip();
9451 x_force_connect = FALSE;
9452 }
9453}
9454
9455 static int
9456check_connection(void)
9457{
9458 make_connection();
9459 if (X_DISPLAY == NULL)
9460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009461 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009462 return FAIL;
9463 }
9464 return OK;
9465}
9466#endif
9467
9468#ifdef FEAT_CLIENTSERVER
9469 static void
9470remote_common(typval_T *argvars, typval_T *rettv, int expr)
9471{
9472 char_u *server_name;
9473 char_u *keys;
9474 char_u *r = NULL;
9475 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009476 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009477# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009478 HWND w;
9479# else
9480 Window w;
9481# endif
9482
9483 if (check_restricted() || check_secure())
9484 return;
9485
9486# ifdef FEAT_X11
9487 if (check_connection() == FAIL)
9488 return;
9489# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009490 if (argvars[2].v_type != VAR_UNKNOWN
9491 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009492 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009493
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009494 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009495 if (server_name == NULL)
9496 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009497 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009498# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009499 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009500# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009501 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9502 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009503# endif
9504 {
9505 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009507 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009508 vim_free(r);
9509 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009510 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009511 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009512 return;
9513 }
9514
9515 rettv->vval.v_string = r;
9516
9517 if (argvars[2].v_type != VAR_UNKNOWN)
9518 {
9519 dictitem_T v;
9520 char_u str[30];
9521 char_u *idvar;
9522
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009523 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009524 if (idvar != NULL && *idvar != NUL)
9525 {
9526 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9527 v.di_tv.v_type = VAR_STRING;
9528 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009529 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009530 vim_free(v.di_tv.vval.v_string);
9531 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009532 }
9533}
9534#endif
9535
9536/*
9537 * "remote_expr()" function
9538 */
9539 static void
9540f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9541{
9542 rettv->v_type = VAR_STRING;
9543 rettv->vval.v_string = NULL;
9544#ifdef FEAT_CLIENTSERVER
9545 remote_common(argvars, rettv, TRUE);
9546#endif
9547}
9548
9549/*
9550 * "remote_foreground()" function
9551 */
9552 static void
9553f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9554{
9555#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009556# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009557 /* On Win32 it's done in this application. */
9558 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009559 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009560
9561 if (server_name != NULL)
9562 serverForeground(server_name);
9563 }
9564# else
9565 /* Send a foreground() expression to the server. */
9566 argvars[1].v_type = VAR_STRING;
9567 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9568 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009569 rettv->v_type = VAR_STRING;
9570 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009571 remote_common(argvars, rettv, TRUE);
9572 vim_free(argvars[1].vval.v_string);
9573# endif
9574#endif
9575}
9576
9577 static void
9578f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9579{
9580#ifdef FEAT_CLIENTSERVER
9581 dictitem_T v;
9582 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009583# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009584 long_u n = 0;
9585# endif
9586 char_u *serverid;
9587
9588 if (check_restricted() || check_secure())
9589 {
9590 rettv->vval.v_number = -1;
9591 return;
9592 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009593 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009594 if (serverid == NULL)
9595 {
9596 rettv->vval.v_number = -1;
9597 return; /* type error; errmsg already given */
9598 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009599# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009600 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9601 if (n == 0)
9602 rettv->vval.v_number = -1;
9603 else
9604 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009605 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009606 rettv->vval.v_number = (s != NULL);
9607 }
9608# else
9609 if (check_connection() == FAIL)
9610 return;
9611
9612 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9613 serverStrToWin(serverid), &s);
9614# endif
9615
9616 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9617 {
9618 char_u *retvar;
9619
9620 v.di_tv.v_type = VAR_STRING;
9621 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009622 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009623 if (retvar != NULL)
9624 set_var(retvar, &v.di_tv, FALSE);
9625 vim_free(v.di_tv.vval.v_string);
9626 }
9627#else
9628 rettv->vval.v_number = -1;
9629#endif
9630}
9631
9632 static void
9633f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9634{
9635 char_u *r = NULL;
9636
9637#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009638 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009639
9640 if (serverid != NULL && !check_restricted() && !check_secure())
9641 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009642 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009643# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009644 /* The server's HWND is encoded in the 'id' parameter */
9645 long_u n = 0;
9646# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009647
9648 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009649 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009650
Bram Moolenaar4f974752019-02-17 17:44:42 +01009651# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009652 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9653 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009654 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009655 if (r == NULL)
9656# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009657 if (check_connection() == FAIL
9658 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9659 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009660# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009661 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009662 }
9663#endif
9664 rettv->v_type = VAR_STRING;
9665 rettv->vval.v_string = r;
9666}
9667
9668/*
9669 * "remote_send()" function
9670 */
9671 static void
9672f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9673{
9674 rettv->v_type = VAR_STRING;
9675 rettv->vval.v_string = NULL;
9676#ifdef FEAT_CLIENTSERVER
9677 remote_common(argvars, rettv, FALSE);
9678#endif
9679}
9680
9681/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009682 * "remote_startserver()" function
9683 */
9684 static void
9685f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9686{
9687#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009688 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009689
9690 if (server == NULL)
9691 return; /* type error; errmsg already given */
9692 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009693 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009694 else
9695 {
9696# ifdef FEAT_X11
9697 if (check_connection() == OK)
9698 serverRegisterName(X_DISPLAY, server);
9699# else
9700 serverSetName(server);
9701# endif
9702 }
9703#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009704 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009705#endif
9706}
9707
9708/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009709 * "remove()" function
9710 */
9711 static void
9712f_remove(typval_T *argvars, typval_T *rettv)
9713{
9714 list_T *l;
9715 listitem_T *item, *item2;
9716 listitem_T *li;
9717 long idx;
9718 long end;
9719 char_u *key;
9720 dict_T *d;
9721 dictitem_T *di;
9722 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009723 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009724
9725 if (argvars[0].v_type == VAR_DICT)
9726 {
9727 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009728 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009729 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009730 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009731 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009732 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009733 if (key != NULL)
9734 {
9735 di = dict_find(d, key, -1);
9736 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009737 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009738 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9739 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9740 {
9741 *rettv = di->di_tv;
9742 init_tv(&di->di_tv);
9743 dictitem_remove(d, di);
9744 }
9745 }
9746 }
9747 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009748 else if (argvars[0].v_type == VAR_BLOB)
9749 {
9750 idx = (long)tv_get_number_chk(&argvars[1], &error);
9751 if (!error)
9752 {
9753 blob_T *b = argvars[0].vval.v_blob;
9754 int len = blob_len(b);
9755 char_u *p;
9756
9757 if (idx < 0)
9758 // count from the end
9759 idx = len + idx;
9760 if (idx < 0 || idx >= len)
9761 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009762 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009763 return;
9764 }
9765 if (argvars[2].v_type == VAR_UNKNOWN)
9766 {
9767 // Remove one item, return its value.
9768 p = (char_u *)b->bv_ga.ga_data;
9769 rettv->vval.v_number = (varnumber_T) *(p + idx);
9770 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9771 --b->bv_ga.ga_len;
9772 }
9773 else
9774 {
9775 blob_T *blob;
9776
9777 // Remove range of items, return list with values.
9778 end = (long)tv_get_number_chk(&argvars[2], &error);
9779 if (error)
9780 return;
9781 if (end < 0)
9782 // count from the end
9783 end = len + end;
9784 if (end >= len || idx > end)
9785 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009786 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009787 return;
9788 }
9789 blob = blob_alloc();
9790 if (blob == NULL)
9791 return;
9792 blob->bv_ga.ga_len = end - idx + 1;
9793 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9794 {
9795 vim_free(blob);
9796 return;
9797 }
9798 p = (char_u *)b->bv_ga.ga_data;
9799 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9800 (size_t)(end - idx + 1));
9801 ++blob->bv_refcount;
9802 rettv->v_type = VAR_BLOB;
9803 rettv->vval.v_blob = blob;
9804
9805 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9806 b->bv_ga.ga_len -= end - idx + 1;
9807 }
9808 }
9809 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009810 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009811 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009812 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009813 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009814 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009815 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009816 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009817 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009819 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009820 else
9821 {
9822 if (argvars[2].v_type == VAR_UNKNOWN)
9823 {
9824 /* Remove one item, return its value. */
9825 vimlist_remove(l, item, item);
9826 *rettv = item->li_tv;
9827 vim_free(item);
9828 }
9829 else
9830 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009831 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009832 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009833 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009834 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009835 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009836 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009837 else
9838 {
9839 int cnt = 0;
9840
9841 for (li = item; li != NULL; li = li->li_next)
9842 {
9843 ++cnt;
9844 if (li == item2)
9845 break;
9846 }
9847 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009848 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009849 else
9850 {
9851 vimlist_remove(l, item, item2);
9852 if (rettv_list_alloc(rettv) == OK)
9853 {
9854 l = rettv->vval.v_list;
9855 l->lv_first = item;
9856 l->lv_last = item2;
9857 item->li_prev = NULL;
9858 item2->li_next = NULL;
9859 l->lv_len = cnt;
9860 }
9861 }
9862 }
9863 }
9864 }
9865 }
9866}
9867
9868/*
9869 * "rename({from}, {to})" function
9870 */
9871 static void
9872f_rename(typval_T *argvars, typval_T *rettv)
9873{
9874 char_u buf[NUMBUFLEN];
9875
9876 if (check_restricted() || check_secure())
9877 rettv->vval.v_number = -1;
9878 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009879 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9880 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009881}
9882
9883/*
9884 * "repeat()" function
9885 */
9886 static void
9887f_repeat(typval_T *argvars, typval_T *rettv)
9888{
9889 char_u *p;
9890 int n;
9891 int slen;
9892 int len;
9893 char_u *r;
9894 int i;
9895
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009896 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009897 if (argvars[0].v_type == VAR_LIST)
9898 {
9899 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9900 while (n-- > 0)
9901 if (list_extend(rettv->vval.v_list,
9902 argvars[0].vval.v_list, NULL) == FAIL)
9903 break;
9904 }
9905 else
9906 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009907 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009908 rettv->v_type = VAR_STRING;
9909 rettv->vval.v_string = NULL;
9910
9911 slen = (int)STRLEN(p);
9912 len = slen * n;
9913 if (len <= 0)
9914 return;
9915
9916 r = alloc(len + 1);
9917 if (r != NULL)
9918 {
9919 for (i = 0; i < n; i++)
9920 mch_memmove(r + i * slen, p, (size_t)slen);
9921 r[len] = NUL;
9922 }
9923
9924 rettv->vval.v_string = r;
9925 }
9926}
9927
9928/*
9929 * "resolve()" function
9930 */
9931 static void
9932f_resolve(typval_T *argvars, typval_T *rettv)
9933{
9934 char_u *p;
9935#ifdef HAVE_READLINK
9936 char_u *buf = NULL;
9937#endif
9938
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009939 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009940#ifdef FEAT_SHORTCUT
9941 {
9942 char_u *v = NULL;
9943
Bram Moolenaardce1e892019-02-10 23:18:53 +01009944 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009945 if (v != NULL)
9946 rettv->vval.v_string = v;
9947 else
9948 rettv->vval.v_string = vim_strsave(p);
9949 }
9950#else
9951# ifdef HAVE_READLINK
9952 {
9953 char_u *cpy;
9954 int len;
9955 char_u *remain = NULL;
9956 char_u *q;
9957 int is_relative_to_current = FALSE;
9958 int has_trailing_pathsep = FALSE;
9959 int limit = 100;
9960
9961 p = vim_strsave(p);
9962
9963 if (p[0] == '.' && (vim_ispathsep(p[1])
9964 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9965 is_relative_to_current = TRUE;
9966
9967 len = STRLEN(p);
9968 if (len > 0 && after_pathsep(p, p + len))
9969 {
9970 has_trailing_pathsep = TRUE;
9971 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9972 }
9973
9974 q = getnextcomp(p);
9975 if (*q != NUL)
9976 {
9977 /* Separate the first path component in "p", and keep the
9978 * remainder (beginning with the path separator). */
9979 remain = vim_strsave(q - 1);
9980 q[-1] = NUL;
9981 }
9982
9983 buf = alloc(MAXPATHL + 1);
9984 if (buf == NULL)
9985 goto fail;
9986
9987 for (;;)
9988 {
9989 for (;;)
9990 {
9991 len = readlink((char *)p, (char *)buf, MAXPATHL);
9992 if (len <= 0)
9993 break;
9994 buf[len] = NUL;
9995
9996 if (limit-- == 0)
9997 {
9998 vim_free(p);
9999 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010000 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010001 rettv->vval.v_string = NULL;
10002 goto fail;
10003 }
10004
10005 /* Ensure that the result will have a trailing path separator
10006 * if the argument has one. */
10007 if (remain == NULL && has_trailing_pathsep)
10008 add_pathsep(buf);
10009
10010 /* Separate the first path component in the link value and
10011 * concatenate the remainders. */
10012 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10013 if (*q != NUL)
10014 {
10015 if (remain == NULL)
10016 remain = vim_strsave(q - 1);
10017 else
10018 {
10019 cpy = concat_str(q - 1, remain);
10020 if (cpy != NULL)
10021 {
10022 vim_free(remain);
10023 remain = cpy;
10024 }
10025 }
10026 q[-1] = NUL;
10027 }
10028
10029 q = gettail(p);
10030 if (q > p && *q == NUL)
10031 {
10032 /* Ignore trailing path separator. */
10033 q[-1] = NUL;
10034 q = gettail(p);
10035 }
10036 if (q > p && !mch_isFullName(buf))
10037 {
10038 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010039 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010040 if (cpy != NULL)
10041 {
10042 STRCPY(cpy, p);
10043 STRCPY(gettail(cpy), buf);
10044 vim_free(p);
10045 p = cpy;
10046 }
10047 }
10048 else
10049 {
10050 vim_free(p);
10051 p = vim_strsave(buf);
10052 }
10053 }
10054
10055 if (remain == NULL)
10056 break;
10057
10058 /* Append the first path component of "remain" to "p". */
10059 q = getnextcomp(remain + 1);
10060 len = q - remain - (*q != NUL);
10061 cpy = vim_strnsave(p, STRLEN(p) + len);
10062 if (cpy != NULL)
10063 {
10064 STRNCAT(cpy, remain, len);
10065 vim_free(p);
10066 p = cpy;
10067 }
10068 /* Shorten "remain". */
10069 if (*q != NUL)
10070 STRMOVE(remain, q - 1);
10071 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010072 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010073 }
10074
10075 /* If the result is a relative path name, make it explicitly relative to
10076 * the current directory if and only if the argument had this form. */
10077 if (!vim_ispathsep(*p))
10078 {
10079 if (is_relative_to_current
10080 && *p != NUL
10081 && !(p[0] == '.'
10082 && (p[1] == NUL
10083 || vim_ispathsep(p[1])
10084 || (p[1] == '.'
10085 && (p[2] == NUL
10086 || vim_ispathsep(p[2]))))))
10087 {
10088 /* Prepend "./". */
10089 cpy = concat_str((char_u *)"./", p);
10090 if (cpy != NULL)
10091 {
10092 vim_free(p);
10093 p = cpy;
10094 }
10095 }
10096 else if (!is_relative_to_current)
10097 {
10098 /* Strip leading "./". */
10099 q = p;
10100 while (q[0] == '.' && vim_ispathsep(q[1]))
10101 q += 2;
10102 if (q > p)
10103 STRMOVE(p, p + 2);
10104 }
10105 }
10106
10107 /* Ensure that the result will have no trailing path separator
10108 * if the argument had none. But keep "/" or "//". */
10109 if (!has_trailing_pathsep)
10110 {
10111 q = p + STRLEN(p);
10112 if (after_pathsep(p, q))
10113 *gettail_sep(p) = NUL;
10114 }
10115
10116 rettv->vval.v_string = p;
10117 }
10118# else
10119 rettv->vval.v_string = vim_strsave(p);
10120# endif
10121#endif
10122
10123 simplify_filename(rettv->vval.v_string);
10124
10125#ifdef HAVE_READLINK
10126fail:
10127 vim_free(buf);
10128#endif
10129 rettv->v_type = VAR_STRING;
10130}
10131
10132/*
10133 * "reverse({list})" function
10134 */
10135 static void
10136f_reverse(typval_T *argvars, typval_T *rettv)
10137{
10138 list_T *l;
10139 listitem_T *li, *ni;
10140
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010141 if (argvars[0].v_type == VAR_BLOB)
10142 {
10143 blob_T *b = argvars[0].vval.v_blob;
10144 int i, len = blob_len(b);
10145
10146 for (i = 0; i < len / 2; i++)
10147 {
10148 int tmp = blob_get(b, i);
10149
10150 blob_set(b, i, blob_get(b, len - i - 1));
10151 blob_set(b, len - i - 1, tmp);
10152 }
10153 rettv_blob_set(rettv, b);
10154 return;
10155 }
10156
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010157 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010158 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010159 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010160 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010161 (char_u *)N_("reverse() argument"), TRUE))
10162 {
10163 li = l->lv_last;
10164 l->lv_first = l->lv_last = NULL;
10165 l->lv_len = 0;
10166 while (li != NULL)
10167 {
10168 ni = li->li_prev;
10169 list_append(l, li);
10170 li = ni;
10171 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010172 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010173 l->lv_idx = l->lv_len - l->lv_idx - 1;
10174 }
10175}
10176
10177#define SP_NOMOVE 0x01 /* don't move cursor */
10178#define SP_REPEAT 0x02 /* repeat to find outer pair */
10179#define SP_RETCOUNT 0x04 /* return matchcount */
10180#define SP_SETPCMARK 0x08 /* set previous context mark */
10181#define SP_START 0x10 /* accept match at start position */
10182#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10183#define SP_END 0x40 /* leave cursor at end of match */
10184#define SP_COLUMN 0x80 /* start at cursor column */
10185
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010186/*
10187 * Get flags for a search function.
10188 * Possibly sets "p_ws".
10189 * Returns BACKWARD, FORWARD or zero (for an error).
10190 */
10191 static int
10192get_search_arg(typval_T *varp, int *flagsp)
10193{
10194 int dir = FORWARD;
10195 char_u *flags;
10196 char_u nbuf[NUMBUFLEN];
10197 int mask;
10198
10199 if (varp->v_type != VAR_UNKNOWN)
10200 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010201 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010202 if (flags == NULL)
10203 return 0; /* type error; errmsg already given */
10204 while (*flags != NUL)
10205 {
10206 switch (*flags)
10207 {
10208 case 'b': dir = BACKWARD; break;
10209 case 'w': p_ws = TRUE; break;
10210 case 'W': p_ws = FALSE; break;
10211 default: mask = 0;
10212 if (flagsp != NULL)
10213 switch (*flags)
10214 {
10215 case 'c': mask = SP_START; break;
10216 case 'e': mask = SP_END; break;
10217 case 'm': mask = SP_RETCOUNT; break;
10218 case 'n': mask = SP_NOMOVE; break;
10219 case 'p': mask = SP_SUBPAT; break;
10220 case 'r': mask = SP_REPEAT; break;
10221 case 's': mask = SP_SETPCMARK; break;
10222 case 'z': mask = SP_COLUMN; break;
10223 }
10224 if (mask == 0)
10225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010226 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010227 dir = 0;
10228 }
10229 else
10230 *flagsp |= mask;
10231 }
10232 if (dir == 0)
10233 break;
10234 ++flags;
10235 }
10236 }
10237 return dir;
10238}
10239
10240/*
10241 * Shared by search() and searchpos() functions.
10242 */
10243 static int
10244search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10245{
10246 int flags;
10247 char_u *pat;
10248 pos_T pos;
10249 pos_T save_cursor;
10250 int save_p_ws = p_ws;
10251 int dir;
10252 int retval = 0; /* default: FAIL */
10253 long lnum_stop = 0;
10254 proftime_T tm;
10255#ifdef FEAT_RELTIME
10256 long time_limit = 0;
10257#endif
10258 int options = SEARCH_KEEP;
10259 int subpatnum;
10260
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010261 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010262 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10263 if (dir == 0)
10264 goto theend;
10265 flags = *flagsp;
10266 if (flags & SP_START)
10267 options |= SEARCH_START;
10268 if (flags & SP_END)
10269 options |= SEARCH_END;
10270 if (flags & SP_COLUMN)
10271 options |= SEARCH_COL;
10272
10273 /* Optional arguments: line number to stop searching and timeout. */
10274 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10275 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010276 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010277 if (lnum_stop < 0)
10278 goto theend;
10279#ifdef FEAT_RELTIME
10280 if (argvars[3].v_type != VAR_UNKNOWN)
10281 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010282 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010283 if (time_limit < 0)
10284 goto theend;
10285 }
10286#endif
10287 }
10288
10289#ifdef FEAT_RELTIME
10290 /* Set the time limit, if there is one. */
10291 profile_setlimit(time_limit, &tm);
10292#endif
10293
10294 /*
10295 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10296 * Check to make sure only those flags are set.
10297 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10298 * flags cannot be set. Check for that condition also.
10299 */
10300 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10301 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010303 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010304 goto theend;
10305 }
10306
10307 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010308 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010309 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010310 if (subpatnum != FAIL)
10311 {
10312 if (flags & SP_SUBPAT)
10313 retval = subpatnum;
10314 else
10315 retval = pos.lnum;
10316 if (flags & SP_SETPCMARK)
10317 setpcmark();
10318 curwin->w_cursor = pos;
10319 if (match_pos != NULL)
10320 {
10321 /* Store the match cursor position */
10322 match_pos->lnum = pos.lnum;
10323 match_pos->col = pos.col + 1;
10324 }
10325 /* "/$" will put the cursor after the end of the line, may need to
10326 * correct that here */
10327 check_cursor();
10328 }
10329
10330 /* If 'n' flag is used: restore cursor position. */
10331 if (flags & SP_NOMOVE)
10332 curwin->w_cursor = save_cursor;
10333 else
10334 curwin->w_set_curswant = TRUE;
10335theend:
10336 p_ws = save_p_ws;
10337
10338 return retval;
10339}
10340
10341#ifdef FEAT_FLOAT
10342
10343/*
10344 * round() is not in C90, use ceil() or floor() instead.
10345 */
10346 float_T
10347vim_round(float_T f)
10348{
10349 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10350}
10351
10352/*
10353 * "round({float})" function
10354 */
10355 static void
10356f_round(typval_T *argvars, typval_T *rettv)
10357{
10358 float_T f = 0.0;
10359
10360 rettv->v_type = VAR_FLOAT;
10361 if (get_float_arg(argvars, &f) == OK)
10362 rettv->vval.v_float = vim_round(f);
10363 else
10364 rettv->vval.v_float = 0.0;
10365}
10366#endif
10367
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010368#ifdef FEAT_RUBY
10369/*
10370 * "rubyeval()" function
10371 */
10372 static void
10373f_rubyeval(typval_T *argvars, typval_T *rettv)
10374{
10375 char_u *str;
10376 char_u buf[NUMBUFLEN];
10377
10378 str = tv_get_string_buf(&argvars[0], buf);
10379 do_rubyeval(str, rettv);
10380}
10381#endif
10382
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010383/*
10384 * "screenattr()" function
10385 */
10386 static void
10387f_screenattr(typval_T *argvars, typval_T *rettv)
10388{
10389 int row;
10390 int col;
10391 int c;
10392
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010393 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10394 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010395 if (row < 0 || row >= screen_Rows
10396 || col < 0 || col >= screen_Columns)
10397 c = -1;
10398 else
10399 c = ScreenAttrs[LineOffset[row] + col];
10400 rettv->vval.v_number = c;
10401}
10402
10403/*
10404 * "screenchar()" function
10405 */
10406 static void
10407f_screenchar(typval_T *argvars, typval_T *rettv)
10408{
10409 int row;
10410 int col;
10411 int off;
10412 int c;
10413
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010414 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10415 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010416 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010417 c = -1;
10418 else
10419 {
10420 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010421 if (enc_utf8 && ScreenLinesUC[off] != 0)
10422 c = ScreenLinesUC[off];
10423 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010424 c = ScreenLines[off];
10425 }
10426 rettv->vval.v_number = c;
10427}
10428
10429/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010430 * "screenchars()" function
10431 */
10432 static void
10433f_screenchars(typval_T *argvars, typval_T *rettv)
10434{
10435 int row;
10436 int col;
10437 int off;
10438 int c;
10439 int i;
10440
10441 if (rettv_list_alloc(rettv) == FAIL)
10442 return;
10443 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10444 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10445 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10446 return;
10447
10448 off = LineOffset[row] + col;
10449 if (enc_utf8 && ScreenLinesUC[off] != 0)
10450 c = ScreenLinesUC[off];
10451 else
10452 c = ScreenLines[off];
10453 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10454
10455 if (enc_utf8)
10456
10457 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10458 list_append_number(rettv->vval.v_list,
10459 (varnumber_T)ScreenLinesC[i][off]);
10460}
10461
10462/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010463 * "screencol()" function
10464 *
10465 * First column is 1 to be consistent with virtcol().
10466 */
10467 static void
10468f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10469{
10470 rettv->vval.v_number = screen_screencol() + 1;
10471}
10472
10473/*
10474 * "screenrow()" function
10475 */
10476 static void
10477f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10478{
10479 rettv->vval.v_number = screen_screenrow() + 1;
10480}
10481
10482/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010483 * "screenstring()" function
10484 */
10485 static void
10486f_screenstring(typval_T *argvars, typval_T *rettv)
10487{
10488 int row;
10489 int col;
10490 int off;
10491 int c;
10492 int i;
10493 char_u buf[MB_MAXBYTES + 1];
10494 int buflen = 0;
10495
10496 rettv->vval.v_string = NULL;
10497 rettv->v_type = VAR_STRING;
10498
10499 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10500 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10501 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10502 return;
10503
10504 off = LineOffset[row] + col;
10505 if (enc_utf8 && ScreenLinesUC[off] != 0)
10506 c = ScreenLinesUC[off];
10507 else
10508 c = ScreenLines[off];
10509 buflen += mb_char2bytes(c, buf);
10510
10511 if (enc_utf8)
10512 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10513 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10514
10515 buf[buflen] = NUL;
10516 rettv->vval.v_string = vim_strsave(buf);
10517}
10518
10519/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010520 * "search()" function
10521 */
10522 static void
10523f_search(typval_T *argvars, typval_T *rettv)
10524{
10525 int flags = 0;
10526
10527 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10528}
10529
10530/*
10531 * "searchdecl()" function
10532 */
10533 static void
10534f_searchdecl(typval_T *argvars, typval_T *rettv)
10535{
10536 int locally = 1;
10537 int thisblock = 0;
10538 int error = FALSE;
10539 char_u *name;
10540
10541 rettv->vval.v_number = 1; /* default: FAIL */
10542
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010543 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010544 if (argvars[1].v_type != VAR_UNKNOWN)
10545 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010546 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010547 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010548 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010549 }
10550 if (!error && name != NULL)
10551 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10552 locally, thisblock, SEARCH_KEEP) == FAIL;
10553}
10554
10555/*
10556 * Used by searchpair() and searchpairpos()
10557 */
10558 static int
10559searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10560{
10561 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010562 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010563 int save_p_ws = p_ws;
10564 int dir;
10565 int flags = 0;
10566 char_u nbuf1[NUMBUFLEN];
10567 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010568 int retval = 0; /* default: FAIL */
10569 long lnum_stop = 0;
10570 long time_limit = 0;
10571
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010572 /* Get the three pattern arguments: start, middle, end. Will result in an
10573 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010574 spat = tv_get_string_chk(&argvars[0]);
10575 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10576 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010577 if (spat == NULL || mpat == NULL || epat == NULL)
10578 goto theend; /* type error */
10579
10580 /* Handle the optional fourth argument: flags */
10581 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10582 if (dir == 0)
10583 goto theend;
10584
10585 /* Don't accept SP_END or SP_SUBPAT.
10586 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10587 */
10588 if ((flags & (SP_END | SP_SUBPAT)) != 0
10589 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010591 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010592 goto theend;
10593 }
10594
10595 /* Using 'r' implies 'W', otherwise it doesn't work. */
10596 if (flags & SP_REPEAT)
10597 p_ws = FALSE;
10598
10599 /* Optional fifth argument: skip expression */
10600 if (argvars[3].v_type == VAR_UNKNOWN
10601 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010602 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010603 else
10604 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010605 skip = &argvars[4];
10606 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10607 && skip->v_type != VAR_STRING)
10608 {
10609 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010610 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010611 goto theend;
10612 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010613 if (argvars[5].v_type != VAR_UNKNOWN)
10614 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010615 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010616 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010618 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010620 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010621#ifdef FEAT_RELTIME
10622 if (argvars[6].v_type != VAR_UNKNOWN)
10623 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010624 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010625 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010627 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010628 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010629 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010630 }
10631#endif
10632 }
10633 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010634
10635 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10636 match_pos, lnum_stop, time_limit);
10637
10638theend:
10639 p_ws = save_p_ws;
10640
10641 return retval;
10642}
10643
10644/*
10645 * "searchpair()" function
10646 */
10647 static void
10648f_searchpair(typval_T *argvars, typval_T *rettv)
10649{
10650 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10651}
10652
10653/*
10654 * "searchpairpos()" function
10655 */
10656 static void
10657f_searchpairpos(typval_T *argvars, typval_T *rettv)
10658{
10659 pos_T match_pos;
10660 int lnum = 0;
10661 int col = 0;
10662
10663 if (rettv_list_alloc(rettv) == FAIL)
10664 return;
10665
10666 if (searchpair_cmn(argvars, &match_pos) > 0)
10667 {
10668 lnum = match_pos.lnum;
10669 col = match_pos.col;
10670 }
10671
10672 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10673 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10674}
10675
10676/*
10677 * Search for a start/middle/end thing.
10678 * Used by searchpair(), see its documentation for the details.
10679 * Returns 0 or -1 for no match,
10680 */
10681 long
10682do_searchpair(
10683 char_u *spat, /* start pattern */
10684 char_u *mpat, /* middle pattern */
10685 char_u *epat, /* end pattern */
10686 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010687 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010688 int flags, /* SP_SETPCMARK and other SP_ values */
10689 pos_T *match_pos,
10690 linenr_T lnum_stop, /* stop at this line if not zero */
10691 long time_limit UNUSED) /* stop after this many msec */
10692{
10693 char_u *save_cpo;
10694 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10695 long retval = 0;
10696 pos_T pos;
10697 pos_T firstpos;
10698 pos_T foundpos;
10699 pos_T save_cursor;
10700 pos_T save_pos;
10701 int n;
10702 int r;
10703 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010704 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010705 int err;
10706 int options = SEARCH_KEEP;
10707 proftime_T tm;
10708
10709 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10710 save_cpo = p_cpo;
10711 p_cpo = empty_option;
10712
10713#ifdef FEAT_RELTIME
10714 /* Set the time limit, if there is one. */
10715 profile_setlimit(time_limit, &tm);
10716#endif
10717
10718 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10719 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010720 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
10721 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010722 if (pat2 == NULL || pat3 == NULL)
10723 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010724 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010725 if (*mpat == NUL)
10726 STRCPY(pat3, pat2);
10727 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010728 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010729 spat, epat, mpat);
10730 if (flags & SP_START)
10731 options |= SEARCH_START;
10732
Bram Moolenaar48570482017-10-30 21:48:41 +010010733 if (skip != NULL)
10734 {
10735 /* Empty string means to not use the skip expression. */
10736 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10737 use_skip = skip->vval.v_string != NULL
10738 && *skip->vval.v_string != NUL;
10739 }
10740
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010741 save_cursor = curwin->w_cursor;
10742 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010743 CLEAR_POS(&firstpos);
10744 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010745 pat = pat3;
10746 for (;;)
10747 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010748 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010749 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010750 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010751 /* didn't find it or found the first match again: FAIL */
10752 break;
10753
10754 if (firstpos.lnum == 0)
10755 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010756 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010757 {
10758 /* Found the same position again. Can happen with a pattern that
10759 * has "\zs" at the end and searching backwards. Advance one
10760 * character and try again. */
10761 if (dir == BACKWARD)
10762 decl(&pos);
10763 else
10764 incl(&pos);
10765 }
10766 foundpos = pos;
10767
10768 /* clear the start flag to avoid getting stuck here */
10769 options &= ~SEARCH_START;
10770
10771 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010772 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010773 {
10774 save_pos = curwin->w_cursor;
10775 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010776 err = FALSE;
10777 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010778 curwin->w_cursor = save_pos;
10779 if (err)
10780 {
10781 /* Evaluating {skip} caused an error, break here. */
10782 curwin->w_cursor = save_cursor;
10783 retval = -1;
10784 break;
10785 }
10786 if (r)
10787 continue;
10788 }
10789
10790 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10791 {
10792 /* Found end when searching backwards or start when searching
10793 * forward: nested pair. */
10794 ++nest;
10795 pat = pat2; /* nested, don't search for middle */
10796 }
10797 else
10798 {
10799 /* Found end when searching forward or start when searching
10800 * backward: end of (nested) pair; or found middle in outer pair. */
10801 if (--nest == 1)
10802 pat = pat3; /* outer level, search for middle */
10803 }
10804
10805 if (nest == 0)
10806 {
10807 /* Found the match: return matchcount or line number. */
10808 if (flags & SP_RETCOUNT)
10809 ++retval;
10810 else
10811 retval = pos.lnum;
10812 if (flags & SP_SETPCMARK)
10813 setpcmark();
10814 curwin->w_cursor = pos;
10815 if (!(flags & SP_REPEAT))
10816 break;
10817 nest = 1; /* search for next unmatched */
10818 }
10819 }
10820
10821 if (match_pos != NULL)
10822 {
10823 /* Store the match cursor position */
10824 match_pos->lnum = curwin->w_cursor.lnum;
10825 match_pos->col = curwin->w_cursor.col + 1;
10826 }
10827
10828 /* If 'n' flag is used or search failed: restore cursor position. */
10829 if ((flags & SP_NOMOVE) || retval == 0)
10830 curwin->w_cursor = save_cursor;
10831
10832theend:
10833 vim_free(pat2);
10834 vim_free(pat3);
10835 if (p_cpo == empty_option)
10836 p_cpo = save_cpo;
10837 else
10838 /* Darn, evaluating the {skip} expression changed the value. */
10839 free_string_option(save_cpo);
10840
10841 return retval;
10842}
10843
10844/*
10845 * "searchpos()" function
10846 */
10847 static void
10848f_searchpos(typval_T *argvars, typval_T *rettv)
10849{
10850 pos_T match_pos;
10851 int lnum = 0;
10852 int col = 0;
10853 int n;
10854 int flags = 0;
10855
10856 if (rettv_list_alloc(rettv) == FAIL)
10857 return;
10858
10859 n = search_cmn(argvars, &match_pos, &flags);
10860 if (n > 0)
10861 {
10862 lnum = match_pos.lnum;
10863 col = match_pos.col;
10864 }
10865
10866 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10867 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10868 if (flags & SP_SUBPAT)
10869 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10870}
10871
10872 static void
10873f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10874{
10875#ifdef FEAT_CLIENTSERVER
10876 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010877 char_u *server = tv_get_string_chk(&argvars[0]);
10878 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010879
10880 rettv->vval.v_number = -1;
10881 if (server == NULL || reply == NULL)
10882 return;
10883 if (check_restricted() || check_secure())
10884 return;
10885# ifdef FEAT_X11
10886 if (check_connection() == FAIL)
10887 return;
10888# endif
10889
10890 if (serverSendReply(server, reply) < 0)
10891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010892 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010893 return;
10894 }
10895 rettv->vval.v_number = 0;
10896#else
10897 rettv->vval.v_number = -1;
10898#endif
10899}
10900
10901 static void
10902f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10903{
10904 char_u *r = NULL;
10905
10906#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010907# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010908 r = serverGetVimNames();
10909# else
10910 make_connection();
10911 if (X_DISPLAY != NULL)
10912 r = serverGetVimNames(X_DISPLAY);
10913# endif
10914#endif
10915 rettv->v_type = VAR_STRING;
10916 rettv->vval.v_string = r;
10917}
10918
10919/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010920 * "setbufline()" function
10921 */
10922 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010923f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010924{
10925 linenr_T lnum;
10926 buf_T *buf;
10927
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010928 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010929 if (buf == NULL)
10930 rettv->vval.v_number = 1; /* FAIL */
10931 else
10932 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010933 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010934 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010935 }
10936}
10937
10938/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010939 * "setbufvar()" function
10940 */
10941 static void
10942f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10943{
10944 buf_T *buf;
10945 char_u *varname, *bufvarname;
10946 typval_T *varp;
10947 char_u nbuf[NUMBUFLEN];
10948
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010949 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010950 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010951 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10952 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010953 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010954 varp = &argvars[2];
10955
10956 if (buf != NULL && varname != NULL && varp != NULL)
10957 {
10958 if (*varname == '&')
10959 {
10960 long numval;
10961 char_u *strval;
10962 int error = FALSE;
10963 aco_save_T aco;
10964
10965 /* set curbuf to be our buf, temporarily */
10966 aucmd_prepbuf(&aco, buf);
10967
10968 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010969 numval = (long)tv_get_number_chk(varp, &error);
10970 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010971 if (!error && strval != NULL)
10972 set_option_value(varname, numval, strval, OPT_LOCAL);
10973
10974 /* reset notion of buffer */
10975 aucmd_restbuf(&aco);
10976 }
10977 else
10978 {
10979 buf_T *save_curbuf = curbuf;
10980
Bram Moolenaar964b3742019-05-24 18:54:09 +020010981 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010982 if (bufvarname != NULL)
10983 {
10984 curbuf = buf;
10985 STRCPY(bufvarname, "b:");
10986 STRCPY(bufvarname + 2, varname);
10987 set_var(bufvarname, varp, TRUE);
10988 vim_free(bufvarname);
10989 curbuf = save_curbuf;
10990 }
10991 }
10992 }
10993}
10994
10995 static void
10996f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10997{
10998 dict_T *d;
10999 dictitem_T *di;
11000 char_u *csearch;
11001
11002 if (argvars[0].v_type != VAR_DICT)
11003 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011004 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011005 return;
11006 }
11007
11008 if ((d = argvars[0].vval.v_dict) != NULL)
11009 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011010 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011011 if (csearch != NULL)
11012 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011013 if (enc_utf8)
11014 {
11015 int pcc[MAX_MCO];
11016 int c = utfc_ptr2char(csearch, pcc);
11017
11018 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11019 }
11020 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011021 set_last_csearch(PTR2CHAR(csearch),
11022 csearch, MB_PTR2LEN(csearch));
11023 }
11024
11025 di = dict_find(d, (char_u *)"forward", -1);
11026 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011027 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011028 ? FORWARD : BACKWARD);
11029
11030 di = dict_find(d, (char_u *)"until", -1);
11031 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011032 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011033 }
11034}
11035
11036/*
11037 * "setcmdpos()" function
11038 */
11039 static void
11040f_setcmdpos(typval_T *argvars, typval_T *rettv)
11041{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011042 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011043
11044 if (pos >= 0)
11045 rettv->vval.v_number = set_cmdline_pos(pos);
11046}
11047
11048/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011049 * "setenv()" function
11050 */
11051 static void
11052f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11053{
11054 char_u namebuf[NUMBUFLEN];
11055 char_u valbuf[NUMBUFLEN];
11056 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11057
11058 if (argvars[1].v_type == VAR_SPECIAL
11059 && argvars[1].vval.v_number == VVAL_NULL)
11060 vim_unsetenv(name);
11061 else
11062 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11063}
11064
11065/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011066 * "setfperm({fname}, {mode})" function
11067 */
11068 static void
11069f_setfperm(typval_T *argvars, typval_T *rettv)
11070{
11071 char_u *fname;
11072 char_u modebuf[NUMBUFLEN];
11073 char_u *mode_str;
11074 int i;
11075 int mask;
11076 int mode = 0;
11077
11078 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011079 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011080 if (fname == NULL)
11081 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011082 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011083 if (mode_str == NULL)
11084 return;
11085 if (STRLEN(mode_str) != 9)
11086 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011087 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011088 return;
11089 }
11090
11091 mask = 1;
11092 for (i = 8; i >= 0; --i)
11093 {
11094 if (mode_str[i] != '-')
11095 mode |= mask;
11096 mask = mask << 1;
11097 }
11098 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11099}
11100
11101/*
11102 * "setline()" function
11103 */
11104 static void
11105f_setline(typval_T *argvars, typval_T *rettv)
11106{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011107 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011108
Bram Moolenaarca851592018-06-06 21:04:07 +020011109 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011110}
11111
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011112/*
11113 * Used by "setqflist()" and "setloclist()" functions
11114 */
11115 static void
11116set_qf_ll_list(
11117 win_T *wp UNUSED,
11118 typval_T *list_arg UNUSED,
11119 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011120 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011121 typval_T *rettv)
11122{
11123#ifdef FEAT_QUICKFIX
11124 static char *e_invact = N_("E927: Invalid action: '%s'");
11125 char_u *act;
11126 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011127 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011128#endif
11129
11130 rettv->vval.v_number = -1;
11131
11132#ifdef FEAT_QUICKFIX
11133 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011134 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011135 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011136 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011137 else
11138 {
11139 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011140 dict_T *d = NULL;
11141 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011142
11143 if (action_arg->v_type == VAR_STRING)
11144 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011145 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011146 if (act == NULL)
11147 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011148 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11149 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011150 action = *act;
11151 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011152 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011153 }
11154 else if (action_arg->v_type == VAR_UNKNOWN)
11155 action = ' ';
11156 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011157 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011158
Bram Moolenaard823fa92016-08-12 16:29:27 +020011159 if (action_arg->v_type != VAR_UNKNOWN
11160 && what_arg->v_type != VAR_UNKNOWN)
11161 {
11162 if (what_arg->v_type == VAR_DICT)
11163 d = what_arg->vval.v_dict;
11164 else
11165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011166 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011167 valid_dict = FALSE;
11168 }
11169 }
11170
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011171 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011172 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011173 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11174 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011175 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011176 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011177 }
11178#endif
11179}
11180
11181/*
11182 * "setloclist()" function
11183 */
11184 static void
11185f_setloclist(typval_T *argvars, typval_T *rettv)
11186{
11187 win_T *win;
11188
11189 rettv->vval.v_number = -1;
11190
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011191 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011192 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011193 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011194}
11195
11196/*
11197 * "setmatches()" function
11198 */
11199 static void
11200f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11201{
11202#ifdef FEAT_SEARCH_EXTRA
11203 list_T *l;
11204 listitem_T *li;
11205 dict_T *d;
11206 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011207 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011208
11209 rettv->vval.v_number = -1;
11210 if (argvars[0].v_type != VAR_LIST)
11211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011212 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011213 return;
11214 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011215 if (win == NULL)
11216 return;
11217
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011218 if ((l = argvars[0].vval.v_list) != NULL)
11219 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011220 /* To some extent make sure that we are dealing with a list from
11221 * "getmatches()". */
11222 li = l->lv_first;
11223 while (li != NULL)
11224 {
11225 if (li->li_tv.v_type != VAR_DICT
11226 || (d = li->li_tv.vval.v_dict) == NULL)
11227 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011228 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011229 return;
11230 }
11231 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11232 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11233 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11234 && dict_find(d, (char_u *)"priority", -1) != NULL
11235 && dict_find(d, (char_u *)"id", -1) != NULL))
11236 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011237 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011238 return;
11239 }
11240 li = li->li_next;
11241 }
11242
Bram Moolenaaraff74912019-03-30 18:11:49 +010011243 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011244 li = l->lv_first;
11245 while (li != NULL)
11246 {
11247 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011248 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011249 dictitem_T *di;
11250 char_u *group;
11251 int priority;
11252 int id;
11253 char_u *conceal;
11254
11255 d = li->li_tv.vval.v_dict;
11256 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11257 {
11258 if (s == NULL)
11259 {
11260 s = list_alloc();
11261 if (s == NULL)
11262 return;
11263 }
11264
11265 /* match from matchaddpos() */
11266 for (i = 1; i < 9; i++)
11267 {
11268 sprintf((char *)buf, (char *)"pos%d", i);
11269 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11270 {
11271 if (di->di_tv.v_type != VAR_LIST)
11272 return;
11273
11274 list_append_tv(s, &di->di_tv);
11275 s->lv_refcount++;
11276 }
11277 else
11278 break;
11279 }
11280 }
11281
Bram Moolenaar8f667172018-12-14 15:38:31 +010011282 group = dict_get_string(d, (char_u *)"group", TRUE);
11283 priority = (int)dict_get_number(d, (char_u *)"priority");
11284 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011285 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011286 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011287 : NULL;
11288 if (i == 0)
11289 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011290 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011291 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011292 priority, id, NULL, conceal);
11293 }
11294 else
11295 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011296 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011297 list_unref(s);
11298 s = NULL;
11299 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011300 vim_free(group);
11301 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011302
11303 li = li->li_next;
11304 }
11305 rettv->vval.v_number = 0;
11306 }
11307#endif
11308}
11309
11310/*
11311 * "setpos()" function
11312 */
11313 static void
11314f_setpos(typval_T *argvars, typval_T *rettv)
11315{
11316 pos_T pos;
11317 int fnum;
11318 char_u *name;
11319 colnr_T curswant = -1;
11320
11321 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011322 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011323 if (name != NULL)
11324 {
11325 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11326 {
11327 if (--pos.col < 0)
11328 pos.col = 0;
11329 if (name[0] == '.' && name[1] == NUL)
11330 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011331 /* set cursor; "fnum" is ignored */
11332 curwin->w_cursor = pos;
11333 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011334 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011335 curwin->w_curswant = curswant - 1;
11336 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011337 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011338 check_cursor();
11339 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011340 }
11341 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11342 {
11343 /* set mark */
11344 if (setmark_pos(name[1], &pos, fnum) == OK)
11345 rettv->vval.v_number = 0;
11346 }
11347 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011348 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011349 }
11350 }
11351}
11352
11353/*
11354 * "setqflist()" function
11355 */
11356 static void
11357f_setqflist(typval_T *argvars, typval_T *rettv)
11358{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011359 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011360}
11361
11362/*
11363 * "setreg()" function
11364 */
11365 static void
11366f_setreg(typval_T *argvars, typval_T *rettv)
11367{
11368 int regname;
11369 char_u *strregname;
11370 char_u *stropt;
11371 char_u *strval;
11372 int append;
11373 char_u yank_type;
11374 long block_len;
11375
11376 block_len = -1;
11377 yank_type = MAUTO;
11378 append = FALSE;
11379
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011380 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011381 rettv->vval.v_number = 1; /* FAIL is default */
11382
11383 if (strregname == NULL)
11384 return; /* type error; errmsg already given */
11385 regname = *strregname;
11386 if (regname == 0 || regname == '@')
11387 regname = '"';
11388
11389 if (argvars[2].v_type != VAR_UNKNOWN)
11390 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011391 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011392 if (stropt == NULL)
11393 return; /* type error */
11394 for (; *stropt != NUL; ++stropt)
11395 switch (*stropt)
11396 {
11397 case 'a': case 'A': /* append */
11398 append = TRUE;
11399 break;
11400 case 'v': case 'c': /* character-wise selection */
11401 yank_type = MCHAR;
11402 break;
11403 case 'V': case 'l': /* line-wise selection */
11404 yank_type = MLINE;
11405 break;
11406 case 'b': case Ctrl_V: /* block-wise selection */
11407 yank_type = MBLOCK;
11408 if (VIM_ISDIGIT(stropt[1]))
11409 {
11410 ++stropt;
11411 block_len = getdigits(&stropt) - 1;
11412 --stropt;
11413 }
11414 break;
11415 }
11416 }
11417
11418 if (argvars[1].v_type == VAR_LIST)
11419 {
11420 char_u **lstval;
11421 char_u **allocval;
11422 char_u buf[NUMBUFLEN];
11423 char_u **curval;
11424 char_u **curallocval;
11425 list_T *ll = argvars[1].vval.v_list;
11426 listitem_T *li;
11427 int len;
11428
11429 /* If the list is NULL handle like an empty list. */
11430 len = ll == NULL ? 0 : ll->lv_len;
11431
11432 /* First half: use for pointers to result lines; second half: use for
11433 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011434 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011435 if (lstval == NULL)
11436 return;
11437 curval = lstval;
11438 allocval = lstval + len + 2;
11439 curallocval = allocval;
11440
11441 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11442 li = li->li_next)
11443 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011444 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011445 if (strval == NULL)
11446 goto free_lstval;
11447 if (strval == buf)
11448 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011449 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011450 * overwrite the string. */
11451 strval = vim_strsave(buf);
11452 if (strval == NULL)
11453 goto free_lstval;
11454 *curallocval++ = strval;
11455 }
11456 *curval++ = strval;
11457 }
11458 *curval++ = NULL;
11459
11460 write_reg_contents_lst(regname, lstval, -1,
11461 append, yank_type, block_len);
11462free_lstval:
11463 while (curallocval > allocval)
11464 vim_free(*--curallocval);
11465 vim_free(lstval);
11466 }
11467 else
11468 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011469 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011470 if (strval == NULL)
11471 return;
11472 write_reg_contents_ex(regname, strval, -1,
11473 append, yank_type, block_len);
11474 }
11475 rettv->vval.v_number = 0;
11476}
11477
11478/*
11479 * "settabvar()" function
11480 */
11481 static void
11482f_settabvar(typval_T *argvars, typval_T *rettv)
11483{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011484 tabpage_T *save_curtab;
11485 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011486 char_u *varname, *tabvarname;
11487 typval_T *varp;
11488
11489 rettv->vval.v_number = 0;
11490
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011491 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011492 return;
11493
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011494 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11495 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011496 varp = &argvars[2];
11497
Bram Moolenaar4033c552017-09-16 20:54:51 +020011498 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011499 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011500 save_curtab = curtab;
11501 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011502
Bram Moolenaar964b3742019-05-24 18:54:09 +020011503 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011504 if (tabvarname != NULL)
11505 {
11506 STRCPY(tabvarname, "t:");
11507 STRCPY(tabvarname + 2, varname);
11508 set_var(tabvarname, varp, TRUE);
11509 vim_free(tabvarname);
11510 }
11511
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011512 /* Restore current tabpage */
11513 if (valid_tabpage(save_curtab))
11514 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011515 }
11516}
11517
11518/*
11519 * "settabwinvar()" function
11520 */
11521 static void
11522f_settabwinvar(typval_T *argvars, typval_T *rettv)
11523{
11524 setwinvar(argvars, rettv, 1);
11525}
11526
11527/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011528 * "settagstack()" function
11529 */
11530 static void
11531f_settagstack(typval_T *argvars, typval_T *rettv)
11532{
11533 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11534 win_T *wp;
11535 dict_T *d;
11536 int action = 'r';
11537
11538 rettv->vval.v_number = -1;
11539
11540 // first argument: window number or id
11541 wp = find_win_by_nr_or_id(&argvars[0]);
11542 if (wp == NULL)
11543 return;
11544
11545 // second argument: dict with items to set in the tag stack
11546 if (argvars[1].v_type != VAR_DICT)
11547 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011548 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011549 return;
11550 }
11551 d = argvars[1].vval.v_dict;
11552 if (d == NULL)
11553 return;
11554
11555 // third argument: action - 'a' for append and 'r' for replace.
11556 // default is to replace the stack.
11557 if (argvars[2].v_type == VAR_UNKNOWN)
11558 action = 'r';
11559 else if (argvars[2].v_type == VAR_STRING)
11560 {
11561 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011562 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011563 if (actstr == NULL)
11564 return;
11565 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11566 action = *actstr;
11567 else
11568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011569 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011570 return;
11571 }
11572 }
11573 else
11574 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011575 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011576 return;
11577 }
11578
11579 if (set_tagstack(wp, d, action) == OK)
11580 rettv->vval.v_number = 0;
11581}
11582
11583/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011584 * "setwinvar()" function
11585 */
11586 static void
11587f_setwinvar(typval_T *argvars, typval_T *rettv)
11588{
11589 setwinvar(argvars, rettv, 0);
11590}
11591
11592#ifdef FEAT_CRYPT
11593/*
11594 * "sha256({string})" function
11595 */
11596 static void
11597f_sha256(typval_T *argvars, typval_T *rettv)
11598{
11599 char_u *p;
11600
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011601 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011602 rettv->vval.v_string = vim_strsave(
11603 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11604 rettv->v_type = VAR_STRING;
11605}
11606#endif /* FEAT_CRYPT */
11607
11608/*
11609 * "shellescape({string})" function
11610 */
11611 static void
11612f_shellescape(typval_T *argvars, typval_T *rettv)
11613{
Bram Moolenaar20615522017-06-05 18:46:26 +020011614 int do_special = non_zero_arg(&argvars[1]);
11615
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011616 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011617 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011618 rettv->v_type = VAR_STRING;
11619}
11620
11621/*
11622 * shiftwidth() function
11623 */
11624 static void
11625f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11626{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011627 rettv->vval.v_number = 0;
11628
11629 if (argvars[0].v_type != VAR_UNKNOWN)
11630 {
11631 long col;
11632
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011633 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011634 if (col < 0)
11635 return; // type error; errmsg already given
11636#ifdef FEAT_VARTABS
11637 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11638 return;
11639#endif
11640 }
11641
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011642 rettv->vval.v_number = get_sw_value(curbuf);
11643}
11644
11645/*
11646 * "simplify()" function
11647 */
11648 static void
11649f_simplify(typval_T *argvars, typval_T *rettv)
11650{
11651 char_u *p;
11652
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011653 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011654 rettv->vval.v_string = vim_strsave(p);
11655 simplify_filename(rettv->vval.v_string); /* simplify in place */
11656 rettv->v_type = VAR_STRING;
11657}
11658
11659#ifdef FEAT_FLOAT
11660/*
11661 * "sin()" function
11662 */
11663 static void
11664f_sin(typval_T *argvars, typval_T *rettv)
11665{
11666 float_T f = 0.0;
11667
11668 rettv->v_type = VAR_FLOAT;
11669 if (get_float_arg(argvars, &f) == OK)
11670 rettv->vval.v_float = sin(f);
11671 else
11672 rettv->vval.v_float = 0.0;
11673}
11674
11675/*
11676 * "sinh()" function
11677 */
11678 static void
11679f_sinh(typval_T *argvars, typval_T *rettv)
11680{
11681 float_T f = 0.0;
11682
11683 rettv->v_type = VAR_FLOAT;
11684 if (get_float_arg(argvars, &f) == OK)
11685 rettv->vval.v_float = sinh(f);
11686 else
11687 rettv->vval.v_float = 0.0;
11688}
11689#endif
11690
Bram Moolenaareae1b912019-05-09 15:12:55 +020011691static int item_compare(const void *s1, const void *s2);
11692static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011693
11694/* struct used in the array that's given to qsort() */
11695typedef struct
11696{
11697 listitem_T *item;
11698 int idx;
11699} sortItem_T;
11700
11701/* struct storing information about current sort */
11702typedef struct
11703{
11704 int item_compare_ic;
11705 int item_compare_numeric;
11706 int item_compare_numbers;
11707#ifdef FEAT_FLOAT
11708 int item_compare_float;
11709#endif
11710 char_u *item_compare_func;
11711 partial_T *item_compare_partial;
11712 dict_T *item_compare_selfdict;
11713 int item_compare_func_err;
11714 int item_compare_keep_zero;
11715} sortinfo_T;
11716static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011717#define ITEM_COMPARE_FAIL 999
11718
11719/*
11720 * Compare functions for f_sort() and f_uniq() below.
11721 */
11722 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011723item_compare(const void *s1, const void *s2)
11724{
11725 sortItem_T *si1, *si2;
11726 typval_T *tv1, *tv2;
11727 char_u *p1, *p2;
11728 char_u *tofree1 = NULL, *tofree2 = NULL;
11729 int res;
11730 char_u numbuf1[NUMBUFLEN];
11731 char_u numbuf2[NUMBUFLEN];
11732
11733 si1 = (sortItem_T *)s1;
11734 si2 = (sortItem_T *)s2;
11735 tv1 = &si1->item->li_tv;
11736 tv2 = &si2->item->li_tv;
11737
11738 if (sortinfo->item_compare_numbers)
11739 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011740 varnumber_T v1 = tv_get_number(tv1);
11741 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011742
11743 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11744 }
11745
11746#ifdef FEAT_FLOAT
11747 if (sortinfo->item_compare_float)
11748 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011749 float_T v1 = tv_get_float(tv1);
11750 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011751
11752 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11753 }
11754#endif
11755
11756 /* tv2string() puts quotes around a string and allocates memory. Don't do
11757 * that for string variables. Use a single quote when comparing with a
11758 * non-string to do what the docs promise. */
11759 if (tv1->v_type == VAR_STRING)
11760 {
11761 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11762 p1 = (char_u *)"'";
11763 else
11764 p1 = tv1->vval.v_string;
11765 }
11766 else
11767 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11768 if (tv2->v_type == VAR_STRING)
11769 {
11770 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11771 p2 = (char_u *)"'";
11772 else
11773 p2 = tv2->vval.v_string;
11774 }
11775 else
11776 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11777 if (p1 == NULL)
11778 p1 = (char_u *)"";
11779 if (p2 == NULL)
11780 p2 = (char_u *)"";
11781 if (!sortinfo->item_compare_numeric)
11782 {
11783 if (sortinfo->item_compare_ic)
11784 res = STRICMP(p1, p2);
11785 else
11786 res = STRCMP(p1, p2);
11787 }
11788 else
11789 {
11790 double n1, n2;
11791 n1 = strtod((char *)p1, (char **)&p1);
11792 n2 = strtod((char *)p2, (char **)&p2);
11793 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11794 }
11795
11796 /* When the result would be zero, compare the item indexes. Makes the
11797 * sort stable. */
11798 if (res == 0 && !sortinfo->item_compare_keep_zero)
11799 res = si1->idx > si2->idx ? 1 : -1;
11800
11801 vim_free(tofree1);
11802 vim_free(tofree2);
11803 return res;
11804}
11805
11806 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011807item_compare2(const void *s1, const void *s2)
11808{
11809 sortItem_T *si1, *si2;
11810 int res;
11811 typval_T rettv;
11812 typval_T argv[3];
11813 int dummy;
11814 char_u *func_name;
11815 partial_T *partial = sortinfo->item_compare_partial;
11816
11817 /* shortcut after failure in previous call; compare all items equal */
11818 if (sortinfo->item_compare_func_err)
11819 return 0;
11820
11821 si1 = (sortItem_T *)s1;
11822 si2 = (sortItem_T *)s2;
11823
11824 if (partial == NULL)
11825 func_name = sortinfo->item_compare_func;
11826 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011827 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011828
11829 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11830 * in the copy without changing the original list items. */
11831 copy_tv(&si1->item->li_tv, &argv[0]);
11832 copy_tv(&si2->item->li_tv, &argv[1]);
11833
11834 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020011835 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011836 partial, sortinfo->item_compare_selfdict);
11837 clear_tv(&argv[0]);
11838 clear_tv(&argv[1]);
11839
11840 if (res == FAIL)
11841 res = ITEM_COMPARE_FAIL;
11842 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011843 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011844 if (sortinfo->item_compare_func_err)
11845 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11846 clear_tv(&rettv);
11847
11848 /* When the result would be zero, compare the pointers themselves. Makes
11849 * the sort stable. */
11850 if (res == 0 && !sortinfo->item_compare_keep_zero)
11851 res = si1->idx > si2->idx ? 1 : -1;
11852
11853 return res;
11854}
11855
11856/*
11857 * "sort({list})" function
11858 */
11859 static void
11860do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11861{
11862 list_T *l;
11863 listitem_T *li;
11864 sortItem_T *ptrs;
11865 sortinfo_T *old_sortinfo;
11866 sortinfo_T info;
11867 long len;
11868 long i;
11869
11870 /* Pointer to current info struct used in compare function. Save and
11871 * restore the current one for nested calls. */
11872 old_sortinfo = sortinfo;
11873 sortinfo = &info;
11874
11875 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011876 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011877 else
11878 {
11879 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011880 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011881 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11882 TRUE))
11883 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011884 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011885
11886 len = list_len(l);
11887 if (len <= 1)
11888 goto theend; /* short list sorts pretty quickly */
11889
11890 info.item_compare_ic = FALSE;
11891 info.item_compare_numeric = FALSE;
11892 info.item_compare_numbers = FALSE;
11893#ifdef FEAT_FLOAT
11894 info.item_compare_float = FALSE;
11895#endif
11896 info.item_compare_func = NULL;
11897 info.item_compare_partial = NULL;
11898 info.item_compare_selfdict = NULL;
11899 if (argvars[1].v_type != VAR_UNKNOWN)
11900 {
11901 /* optional second argument: {func} */
11902 if (argvars[1].v_type == VAR_FUNC)
11903 info.item_compare_func = argvars[1].vval.v_string;
11904 else if (argvars[1].v_type == VAR_PARTIAL)
11905 info.item_compare_partial = argvars[1].vval.v_partial;
11906 else
11907 {
11908 int error = FALSE;
11909
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011910 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011911 if (error)
11912 goto theend; /* type error; errmsg already given */
11913 if (i == 1)
11914 info.item_compare_ic = TRUE;
11915 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011916 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011917 else if (i != 0)
11918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011919 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011920 goto theend;
11921 }
11922 if (info.item_compare_func != NULL)
11923 {
11924 if (*info.item_compare_func == NUL)
11925 {
11926 /* empty string means default sort */
11927 info.item_compare_func = NULL;
11928 }
11929 else if (STRCMP(info.item_compare_func, "n") == 0)
11930 {
11931 info.item_compare_func = NULL;
11932 info.item_compare_numeric = TRUE;
11933 }
11934 else if (STRCMP(info.item_compare_func, "N") == 0)
11935 {
11936 info.item_compare_func = NULL;
11937 info.item_compare_numbers = TRUE;
11938 }
11939#ifdef FEAT_FLOAT
11940 else if (STRCMP(info.item_compare_func, "f") == 0)
11941 {
11942 info.item_compare_func = NULL;
11943 info.item_compare_float = TRUE;
11944 }
11945#endif
11946 else if (STRCMP(info.item_compare_func, "i") == 0)
11947 {
11948 info.item_compare_func = NULL;
11949 info.item_compare_ic = TRUE;
11950 }
11951 }
11952 }
11953
11954 if (argvars[2].v_type != VAR_UNKNOWN)
11955 {
11956 /* optional third argument: {dict} */
11957 if (argvars[2].v_type != VAR_DICT)
11958 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011959 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011960 goto theend;
11961 }
11962 info.item_compare_selfdict = argvars[2].vval.v_dict;
11963 }
11964 }
11965
11966 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011967 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011968 if (ptrs == NULL)
11969 goto theend;
11970
11971 i = 0;
11972 if (sort)
11973 {
11974 /* sort(): ptrs will be the list to sort */
11975 for (li = l->lv_first; li != NULL; li = li->li_next)
11976 {
11977 ptrs[i].item = li;
11978 ptrs[i].idx = i;
11979 ++i;
11980 }
11981
11982 info.item_compare_func_err = FALSE;
11983 info.item_compare_keep_zero = FALSE;
11984 /* test the compare function */
11985 if ((info.item_compare_func != NULL
11986 || info.item_compare_partial != NULL)
11987 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11988 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011989 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011990 else
11991 {
11992 /* Sort the array with item pointers. */
11993 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11994 info.item_compare_func == NULL
11995 && info.item_compare_partial == NULL
11996 ? item_compare : item_compare2);
11997
11998 if (!info.item_compare_func_err)
11999 {
12000 /* Clear the List and append the items in sorted order. */
12001 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12002 l->lv_len = 0;
12003 for (i = 0; i < len; ++i)
12004 list_append(l, ptrs[i].item);
12005 }
12006 }
12007 }
12008 else
12009 {
12010 int (*item_compare_func_ptr)(const void *, const void *);
12011
12012 /* f_uniq(): ptrs will be a stack of items to remove */
12013 info.item_compare_func_err = FALSE;
12014 info.item_compare_keep_zero = TRUE;
12015 item_compare_func_ptr = info.item_compare_func != NULL
12016 || info.item_compare_partial != NULL
12017 ? item_compare2 : item_compare;
12018
12019 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12020 li = li->li_next)
12021 {
12022 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12023 == 0)
12024 ptrs[i++].item = li;
12025 if (info.item_compare_func_err)
12026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012027 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012028 break;
12029 }
12030 }
12031
12032 if (!info.item_compare_func_err)
12033 {
12034 while (--i >= 0)
12035 {
12036 li = ptrs[i].item->li_next;
12037 ptrs[i].item->li_next = li->li_next;
12038 if (li->li_next != NULL)
12039 li->li_next->li_prev = ptrs[i].item;
12040 else
12041 l->lv_last = ptrs[i].item;
12042 list_fix_watch(l, li);
12043 listitem_free(li);
12044 l->lv_len--;
12045 }
12046 }
12047 }
12048
12049 vim_free(ptrs);
12050 }
12051theend:
12052 sortinfo = old_sortinfo;
12053}
12054
12055/*
12056 * "sort({list})" function
12057 */
12058 static void
12059f_sort(typval_T *argvars, typval_T *rettv)
12060{
12061 do_sort_uniq(argvars, rettv, TRUE);
12062}
12063
12064/*
12065 * "uniq({list})" function
12066 */
12067 static void
12068f_uniq(typval_T *argvars, typval_T *rettv)
12069{
12070 do_sort_uniq(argvars, rettv, FALSE);
12071}
12072
12073/*
12074 * "soundfold({word})" function
12075 */
12076 static void
12077f_soundfold(typval_T *argvars, typval_T *rettv)
12078{
12079 char_u *s;
12080
12081 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012082 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012083#ifdef FEAT_SPELL
12084 rettv->vval.v_string = eval_soundfold(s);
12085#else
12086 rettv->vval.v_string = vim_strsave(s);
12087#endif
12088}
12089
12090/*
12091 * "spellbadword()" function
12092 */
12093 static void
12094f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12095{
12096 char_u *word = (char_u *)"";
12097 hlf_T attr = HLF_COUNT;
12098 int len = 0;
12099
12100 if (rettv_list_alloc(rettv) == FAIL)
12101 return;
12102
12103#ifdef FEAT_SPELL
12104 if (argvars[0].v_type == VAR_UNKNOWN)
12105 {
12106 /* Find the start and length of the badly spelled word. */
12107 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12108 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012109 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012110 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012111 curwin->w_set_curswant = TRUE;
12112 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012113 }
12114 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12115 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012116 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012117 int capcol = -1;
12118
12119 if (str != NULL)
12120 {
12121 /* Check the argument for spelling. */
12122 while (*str != NUL)
12123 {
12124 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12125 if (attr != HLF_COUNT)
12126 {
12127 word = str;
12128 break;
12129 }
12130 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012131 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012132 }
12133 }
12134 }
12135#endif
12136
12137 list_append_string(rettv->vval.v_list, word, len);
12138 list_append_string(rettv->vval.v_list, (char_u *)(
12139 attr == HLF_SPB ? "bad" :
12140 attr == HLF_SPR ? "rare" :
12141 attr == HLF_SPL ? "local" :
12142 attr == HLF_SPC ? "caps" :
12143 ""), -1);
12144}
12145
12146/*
12147 * "spellsuggest()" function
12148 */
12149 static void
12150f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12151{
12152#ifdef FEAT_SPELL
12153 char_u *str;
12154 int typeerr = FALSE;
12155 int maxcount;
12156 garray_T ga;
12157 int i;
12158 listitem_T *li;
12159 int need_capital = FALSE;
12160#endif
12161
12162 if (rettv_list_alloc(rettv) == FAIL)
12163 return;
12164
12165#ifdef FEAT_SPELL
12166 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12167 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012168 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012169 if (argvars[1].v_type != VAR_UNKNOWN)
12170 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012171 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012172 if (maxcount <= 0)
12173 return;
12174 if (argvars[2].v_type != VAR_UNKNOWN)
12175 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012176 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012177 if (typeerr)
12178 return;
12179 }
12180 }
12181 else
12182 maxcount = 25;
12183
12184 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12185
12186 for (i = 0; i < ga.ga_len; ++i)
12187 {
12188 str = ((char_u **)ga.ga_data)[i];
12189
12190 li = listitem_alloc();
12191 if (li == NULL)
12192 vim_free(str);
12193 else
12194 {
12195 li->li_tv.v_type = VAR_STRING;
12196 li->li_tv.v_lock = 0;
12197 li->li_tv.vval.v_string = str;
12198 list_append(rettv->vval.v_list, li);
12199 }
12200 }
12201 ga_clear(&ga);
12202 }
12203#endif
12204}
12205
12206 static void
12207f_split(typval_T *argvars, typval_T *rettv)
12208{
12209 char_u *str;
12210 char_u *end;
12211 char_u *pat = NULL;
12212 regmatch_T regmatch;
12213 char_u patbuf[NUMBUFLEN];
12214 char_u *save_cpo;
12215 int match;
12216 colnr_T col = 0;
12217 int keepempty = FALSE;
12218 int typeerr = FALSE;
12219
12220 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12221 save_cpo = p_cpo;
12222 p_cpo = (char_u *)"";
12223
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012224 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012225 if (argvars[1].v_type != VAR_UNKNOWN)
12226 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012227 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012228 if (pat == NULL)
12229 typeerr = TRUE;
12230 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012231 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012232 }
12233 if (pat == NULL || *pat == NUL)
12234 pat = (char_u *)"[\\x01- ]\\+";
12235
12236 if (rettv_list_alloc(rettv) == FAIL)
12237 return;
12238 if (typeerr)
12239 return;
12240
12241 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12242 if (regmatch.regprog != NULL)
12243 {
12244 regmatch.rm_ic = FALSE;
12245 while (*str != NUL || keepempty)
12246 {
12247 if (*str == NUL)
12248 match = FALSE; /* empty item at the end */
12249 else
12250 match = vim_regexec_nl(&regmatch, str, col);
12251 if (match)
12252 end = regmatch.startp[0];
12253 else
12254 end = str + STRLEN(str);
12255 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12256 && *str != NUL && match && end < regmatch.endp[0]))
12257 {
12258 if (list_append_string(rettv->vval.v_list, str,
12259 (int)(end - str)) == FAIL)
12260 break;
12261 }
12262 if (!match)
12263 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012264 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012265 if (regmatch.endp[0] > str)
12266 col = 0;
12267 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012268 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012269 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012270 str = regmatch.endp[0];
12271 }
12272
12273 vim_regfree(regmatch.regprog);
12274 }
12275
12276 p_cpo = save_cpo;
12277}
12278
12279#ifdef FEAT_FLOAT
12280/*
12281 * "sqrt()" function
12282 */
12283 static void
12284f_sqrt(typval_T *argvars, typval_T *rettv)
12285{
12286 float_T f = 0.0;
12287
12288 rettv->v_type = VAR_FLOAT;
12289 if (get_float_arg(argvars, &f) == OK)
12290 rettv->vval.v_float = sqrt(f);
12291 else
12292 rettv->vval.v_float = 0.0;
12293}
12294
12295/*
12296 * "str2float()" function
12297 */
12298 static void
12299f_str2float(typval_T *argvars, typval_T *rettv)
12300{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012301 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012302 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012303
Bram Moolenaar08243d22017-01-10 16:12:29 +010012304 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012305 p = skipwhite(p + 1);
12306 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012307 if (isneg)
12308 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012309 rettv->v_type = VAR_FLOAT;
12310}
12311#endif
12312
12313/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012314 * "str2list()" function
12315 */
12316 static void
12317f_str2list(typval_T *argvars, typval_T *rettv)
12318{
12319 char_u *p;
12320 int utf8 = FALSE;
12321
12322 if (rettv_list_alloc(rettv) == FAIL)
12323 return;
12324
12325 if (argvars[1].v_type != VAR_UNKNOWN)
12326 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12327
12328 p = tv_get_string(&argvars[0]);
12329
12330 if (has_mbyte || utf8)
12331 {
12332 int (*ptr2len)(char_u *);
12333 int (*ptr2char)(char_u *);
12334
12335 if (utf8 || enc_utf8)
12336 {
12337 ptr2len = utf_ptr2len;
12338 ptr2char = utf_ptr2char;
12339 }
12340 else
12341 {
12342 ptr2len = mb_ptr2len;
12343 ptr2char = mb_ptr2char;
12344 }
12345
12346 for ( ; *p != NUL; p += (*ptr2len)(p))
12347 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
12348 }
12349 else
12350 for ( ; *p != NUL; ++p)
12351 list_append_number(rettv->vval.v_list, *p);
12352}
12353
12354/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012355 * "str2nr()" function
12356 */
12357 static void
12358f_str2nr(typval_T *argvars, typval_T *rettv)
12359{
12360 int base = 10;
12361 char_u *p;
12362 varnumber_T n;
12363 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012364 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012365
12366 if (argvars[1].v_type != VAR_UNKNOWN)
12367 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012368 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012369 if (base != 2 && base != 8 && base != 10 && base != 16)
12370 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012371 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012372 return;
12373 }
12374 }
12375
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012376 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012377 isneg = (*p == '-');
12378 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012379 p = skipwhite(p + 1);
12380 switch (base)
12381 {
12382 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12383 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12384 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12385 default: what = 0;
12386 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020012387 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
12388 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010012389 if (isneg)
12390 rettv->vval.v_number = -n;
12391 else
12392 rettv->vval.v_number = n;
12393
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012394}
12395
12396#ifdef HAVE_STRFTIME
12397/*
12398 * "strftime({format}[, {time}])" function
12399 */
12400 static void
12401f_strftime(typval_T *argvars, typval_T *rettv)
12402{
12403 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020012404 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012405 struct tm *curtime;
12406 time_t seconds;
12407 char_u *p;
12408
12409 rettv->v_type = VAR_STRING;
12410
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012411 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012412 if (argvars[1].v_type == VAR_UNKNOWN)
12413 seconds = time(NULL);
12414 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012415 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020012416 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012417 /* MSVC returns NULL for an invalid value of seconds. */
12418 if (curtime == NULL)
12419 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12420 else
12421 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012422 vimconv_T conv;
12423 char_u *enc;
12424
12425 conv.vc_type = CONV_NONE;
12426 enc = enc_locale();
12427 convert_setup(&conv, p_enc, enc);
12428 if (conv.vc_type != CONV_NONE)
12429 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012430 if (p != NULL)
12431 (void)strftime((char *)result_buf, sizeof(result_buf),
12432 (char *)p, curtime);
12433 else
12434 result_buf[0] = NUL;
12435
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012436 if (conv.vc_type != CONV_NONE)
12437 vim_free(p);
12438 convert_setup(&conv, enc, p_enc);
12439 if (conv.vc_type != CONV_NONE)
12440 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12441 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012442 rettv->vval.v_string = vim_strsave(result_buf);
12443
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012444 /* Release conversion descriptors */
12445 convert_setup(&conv, NULL, NULL);
12446 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012447 }
12448}
12449#endif
12450
12451/*
12452 * "strgetchar()" function
12453 */
12454 static void
12455f_strgetchar(typval_T *argvars, typval_T *rettv)
12456{
12457 char_u *str;
12458 int len;
12459 int error = FALSE;
12460 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012461 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012462
12463 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012464 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012465 if (str == NULL)
12466 return;
12467 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012468 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012469 if (error)
12470 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012471
Bram Moolenaar13505972019-01-24 15:04:48 +010012472 while (charidx >= 0 && byteidx < len)
12473 {
12474 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012475 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012476 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12477 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012478 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012479 --charidx;
12480 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012481 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012482}
12483
12484/*
12485 * "stridx()" function
12486 */
12487 static void
12488f_stridx(typval_T *argvars, typval_T *rettv)
12489{
12490 char_u buf[NUMBUFLEN];
12491 char_u *needle;
12492 char_u *haystack;
12493 char_u *save_haystack;
12494 char_u *pos;
12495 int start_idx;
12496
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012497 needle = tv_get_string_chk(&argvars[1]);
12498 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012499 rettv->vval.v_number = -1;
12500 if (needle == NULL || haystack == NULL)
12501 return; /* type error; errmsg already given */
12502
12503 if (argvars[2].v_type != VAR_UNKNOWN)
12504 {
12505 int error = FALSE;
12506
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012507 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012508 if (error || start_idx >= (int)STRLEN(haystack))
12509 return;
12510 if (start_idx >= 0)
12511 haystack += start_idx;
12512 }
12513
12514 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12515 if (pos != NULL)
12516 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12517}
12518
12519/*
12520 * "string()" function
12521 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012522 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012523f_string(typval_T *argvars, typval_T *rettv)
12524{
12525 char_u *tofree;
12526 char_u numbuf[NUMBUFLEN];
12527
12528 rettv->v_type = VAR_STRING;
12529 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12530 get_copyID());
12531 /* Make a copy if we have a value but it's not in allocated memory. */
12532 if (rettv->vval.v_string != NULL && tofree == NULL)
12533 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12534}
12535
12536/*
12537 * "strlen()" function
12538 */
12539 static void
12540f_strlen(typval_T *argvars, typval_T *rettv)
12541{
12542 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012543 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012544}
12545
12546/*
12547 * "strchars()" function
12548 */
12549 static void
12550f_strchars(typval_T *argvars, typval_T *rettv)
12551{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012552 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012553 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012554 varnumber_T len = 0;
12555 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012556
12557 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012558 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012559 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012560 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012561 else
12562 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012563 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12564 while (*s != NUL)
12565 {
12566 func_mb_ptr2char_adv(&s);
12567 ++len;
12568 }
12569 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012570 }
12571}
12572
12573/*
12574 * "strdisplaywidth()" function
12575 */
12576 static void
12577f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12578{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012579 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012580 int col = 0;
12581
12582 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012583 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012584
12585 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12586}
12587
12588/*
12589 * "strwidth()" function
12590 */
12591 static void
12592f_strwidth(typval_T *argvars, typval_T *rettv)
12593{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012594 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012595
Bram Moolenaar13505972019-01-24 15:04:48 +010012596 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012597}
12598
12599/*
12600 * "strcharpart()" function
12601 */
12602 static void
12603f_strcharpart(typval_T *argvars, typval_T *rettv)
12604{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012605 char_u *p;
12606 int nchar;
12607 int nbyte = 0;
12608 int charlen;
12609 int len = 0;
12610 int slen;
12611 int error = FALSE;
12612
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012613 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012614 slen = (int)STRLEN(p);
12615
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012616 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012617 if (!error)
12618 {
12619 if (nchar > 0)
12620 while (nchar > 0 && nbyte < slen)
12621 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012622 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012623 --nchar;
12624 }
12625 else
12626 nbyte = nchar;
12627 if (argvars[2].v_type != VAR_UNKNOWN)
12628 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012629 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012630 while (charlen > 0 && nbyte + len < slen)
12631 {
12632 int off = nbyte + len;
12633
12634 if (off < 0)
12635 len += 1;
12636 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012637 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012638 --charlen;
12639 }
12640 }
12641 else
12642 len = slen - nbyte; /* default: all bytes that are available. */
12643 }
12644
12645 /*
12646 * Only return the overlap between the specified part and the actual
12647 * string.
12648 */
12649 if (nbyte < 0)
12650 {
12651 len += nbyte;
12652 nbyte = 0;
12653 }
12654 else if (nbyte > slen)
12655 nbyte = slen;
12656 if (len < 0)
12657 len = 0;
12658 else if (nbyte + len > slen)
12659 len = slen - nbyte;
12660
12661 rettv->v_type = VAR_STRING;
12662 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012663}
12664
12665/*
12666 * "strpart()" function
12667 */
12668 static void
12669f_strpart(typval_T *argvars, typval_T *rettv)
12670{
12671 char_u *p;
12672 int n;
12673 int len;
12674 int slen;
12675 int error = FALSE;
12676
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012677 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012678 slen = (int)STRLEN(p);
12679
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012680 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012681 if (error)
12682 len = 0;
12683 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012684 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012685 else
12686 len = slen - n; /* default len: all bytes that are available. */
12687
12688 /*
12689 * Only return the overlap between the specified part and the actual
12690 * string.
12691 */
12692 if (n < 0)
12693 {
12694 len += n;
12695 n = 0;
12696 }
12697 else if (n > slen)
12698 n = slen;
12699 if (len < 0)
12700 len = 0;
12701 else if (n + len > slen)
12702 len = slen - n;
12703
12704 rettv->v_type = VAR_STRING;
12705 rettv->vval.v_string = vim_strnsave(p + n, len);
12706}
12707
12708/*
12709 * "strridx()" function
12710 */
12711 static void
12712f_strridx(typval_T *argvars, typval_T *rettv)
12713{
12714 char_u buf[NUMBUFLEN];
12715 char_u *needle;
12716 char_u *haystack;
12717 char_u *rest;
12718 char_u *lastmatch = NULL;
12719 int haystack_len, end_idx;
12720
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012721 needle = tv_get_string_chk(&argvars[1]);
12722 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012723
12724 rettv->vval.v_number = -1;
12725 if (needle == NULL || haystack == NULL)
12726 return; /* type error; errmsg already given */
12727
12728 haystack_len = (int)STRLEN(haystack);
12729 if (argvars[2].v_type != VAR_UNKNOWN)
12730 {
12731 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012732 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012733 if (end_idx < 0)
12734 return; /* can never find a match */
12735 }
12736 else
12737 end_idx = haystack_len;
12738
12739 if (*needle == NUL)
12740 {
12741 /* Empty string matches past the end. */
12742 lastmatch = haystack + end_idx;
12743 }
12744 else
12745 {
12746 for (rest = haystack; *rest != '\0'; ++rest)
12747 {
12748 rest = (char_u *)strstr((char *)rest, (char *)needle);
12749 if (rest == NULL || rest > haystack + end_idx)
12750 break;
12751 lastmatch = rest;
12752 }
12753 }
12754
12755 if (lastmatch == NULL)
12756 rettv->vval.v_number = -1;
12757 else
12758 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12759}
12760
12761/*
12762 * "strtrans()" function
12763 */
12764 static void
12765f_strtrans(typval_T *argvars, typval_T *rettv)
12766{
12767 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012768 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012769}
12770
12771/*
12772 * "submatch()" function
12773 */
12774 static void
12775f_submatch(typval_T *argvars, typval_T *rettv)
12776{
12777 int error = FALSE;
12778 int no;
12779 int retList = 0;
12780
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012781 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012782 if (error)
12783 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012784 if (no < 0 || no >= NSUBEXP)
12785 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012786 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012787 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012788 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012789 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012790 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012791 if (error)
12792 return;
12793
12794 if (retList == 0)
12795 {
12796 rettv->v_type = VAR_STRING;
12797 rettv->vval.v_string = reg_submatch(no);
12798 }
12799 else
12800 {
12801 rettv->v_type = VAR_LIST;
12802 rettv->vval.v_list = reg_submatch_list(no);
12803 }
12804}
12805
12806/*
12807 * "substitute()" function
12808 */
12809 static void
12810f_substitute(typval_T *argvars, typval_T *rettv)
12811{
12812 char_u patbuf[NUMBUFLEN];
12813 char_u subbuf[NUMBUFLEN];
12814 char_u flagsbuf[NUMBUFLEN];
12815
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012816 char_u *str = tv_get_string_chk(&argvars[0]);
12817 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012818 char_u *sub = NULL;
12819 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012820 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012821
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012822 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12823 expr = &argvars[2];
12824 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012825 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012826
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012827 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012828 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12829 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012830 rettv->vval.v_string = NULL;
12831 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012832 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012833}
12834
12835/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012836 * "swapinfo(swap_filename)" function
12837 */
12838 static void
12839f_swapinfo(typval_T *argvars, typval_T *rettv)
12840{
12841 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012842 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012843}
12844
12845/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012846 * "swapname(expr)" function
12847 */
12848 static void
12849f_swapname(typval_T *argvars, typval_T *rettv)
12850{
12851 buf_T *buf;
12852
12853 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010012854 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020012855 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12856 || buf->b_ml.ml_mfp->mf_fname == NULL)
12857 rettv->vval.v_string = NULL;
12858 else
12859 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12860}
12861
12862/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012863 * "synID(lnum, col, trans)" function
12864 */
12865 static void
12866f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12867{
12868 int id = 0;
12869#ifdef FEAT_SYN_HL
12870 linenr_T lnum;
12871 colnr_T col;
12872 int trans;
12873 int transerr = FALSE;
12874
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012875 lnum = tv_get_lnum(argvars); /* -1 on type error */
12876 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12877 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012878
12879 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12880 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12881 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12882#endif
12883
12884 rettv->vval.v_number = id;
12885}
12886
12887/*
12888 * "synIDattr(id, what [, mode])" function
12889 */
12890 static void
12891f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12892{
12893 char_u *p = NULL;
12894#ifdef FEAT_SYN_HL
12895 int id;
12896 char_u *what;
12897 char_u *mode;
12898 char_u modebuf[NUMBUFLEN];
12899 int modec;
12900
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012901 id = (int)tv_get_number(&argvars[0]);
12902 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012903 if (argvars[2].v_type != VAR_UNKNOWN)
12904 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012905 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012906 modec = TOLOWER_ASC(mode[0]);
12907 if (modec != 't' && modec != 'c' && modec != 'g')
12908 modec = 0; /* replace invalid with current */
12909 }
12910 else
12911 {
12912#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12913 if (USE_24BIT)
12914 modec = 'g';
12915 else
12916#endif
12917 if (t_colors > 1)
12918 modec = 'c';
12919 else
12920 modec = 't';
12921 }
12922
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012923 switch (TOLOWER_ASC(what[0]))
12924 {
12925 case 'b':
12926 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12927 p = highlight_color(id, what, modec);
12928 else /* bold */
12929 p = highlight_has_attr(id, HL_BOLD, modec);
12930 break;
12931
12932 case 'f': /* fg[#] or font */
12933 p = highlight_color(id, what, modec);
12934 break;
12935
12936 case 'i':
12937 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12938 p = highlight_has_attr(id, HL_INVERSE, modec);
12939 else /* italic */
12940 p = highlight_has_attr(id, HL_ITALIC, modec);
12941 break;
12942
12943 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012944 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012945 break;
12946
12947 case 'r': /* reverse */
12948 p = highlight_has_attr(id, HL_INVERSE, modec);
12949 break;
12950
12951 case 's':
12952 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12953 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012954 /* strikeout */
12955 else if (TOLOWER_ASC(what[1]) == 't' &&
12956 TOLOWER_ASC(what[2]) == 'r')
12957 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012958 else /* standout */
12959 p = highlight_has_attr(id, HL_STANDOUT, modec);
12960 break;
12961
12962 case 'u':
12963 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12964 /* underline */
12965 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12966 else
12967 /* undercurl */
12968 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12969 break;
12970 }
12971
12972 if (p != NULL)
12973 p = vim_strsave(p);
12974#endif
12975 rettv->v_type = VAR_STRING;
12976 rettv->vval.v_string = p;
12977}
12978
12979/*
12980 * "synIDtrans(id)" function
12981 */
12982 static void
12983f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12984{
12985 int id;
12986
12987#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012988 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012989
12990 if (id > 0)
12991 id = syn_get_final_id(id);
12992 else
12993#endif
12994 id = 0;
12995
12996 rettv->vval.v_number = id;
12997}
12998
12999/*
13000 * "synconcealed(lnum, col)" function
13001 */
13002 static void
13003f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13004{
13005#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13006 linenr_T lnum;
13007 colnr_T col;
13008 int syntax_flags = 0;
13009 int cchar;
13010 int matchid = 0;
13011 char_u str[NUMBUFLEN];
13012#endif
13013
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013014 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013015
13016#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013017 lnum = tv_get_lnum(argvars); /* -1 on type error */
13018 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013019
13020 vim_memset(str, NUL, sizeof(str));
13021
13022 if (rettv_list_alloc(rettv) != FAIL)
13023 {
13024 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13025 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13026 && curwin->w_p_cole > 0)
13027 {
13028 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13029 syntax_flags = get_syntax_info(&matchid);
13030
13031 /* get the conceal character */
13032 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13033 {
13034 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013035 if (cchar == NUL && curwin->w_p_cole == 1)
13036 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013037 if (cchar != NUL)
13038 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013039 if (has_mbyte)
13040 (*mb_char2bytes)(cchar, str);
13041 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013042 str[0] = cchar;
13043 }
13044 }
13045 }
13046
13047 list_append_number(rettv->vval.v_list,
13048 (syntax_flags & HL_CONCEAL) != 0);
13049 /* -1 to auto-determine strlen */
13050 list_append_string(rettv->vval.v_list, str, -1);
13051 list_append_number(rettv->vval.v_list, matchid);
13052 }
13053#endif
13054}
13055
13056/*
13057 * "synstack(lnum, col)" function
13058 */
13059 static void
13060f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13061{
13062#ifdef FEAT_SYN_HL
13063 linenr_T lnum;
13064 colnr_T col;
13065 int i;
13066 int id;
13067#endif
13068
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013069 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013070
13071#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013072 lnum = tv_get_lnum(argvars); /* -1 on type error */
13073 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013074
13075 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13076 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13077 && rettv_list_alloc(rettv) != FAIL)
13078 {
13079 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13080 for (i = 0; ; ++i)
13081 {
13082 id = syn_get_stack_item(i);
13083 if (id < 0)
13084 break;
13085 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13086 break;
13087 }
13088 }
13089#endif
13090}
13091
13092 static void
13093get_cmd_output_as_rettv(
13094 typval_T *argvars,
13095 typval_T *rettv,
13096 int retlist)
13097{
13098 char_u *res = NULL;
13099 char_u *p;
13100 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013101 int err = FALSE;
13102 FILE *fd;
13103 list_T *list = NULL;
13104 int flags = SHELL_SILENT;
13105
13106 rettv->v_type = VAR_STRING;
13107 rettv->vval.v_string = NULL;
13108 if (check_restricted() || check_secure())
13109 goto errret;
13110
13111 if (argvars[1].v_type != VAR_UNKNOWN)
13112 {
13113 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013114 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013115 * command.
13116 */
13117 if ((infile = vim_tempname('i', TRUE)) == NULL)
13118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013119 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013120 goto errret;
13121 }
13122
13123 fd = mch_fopen((char *)infile, WRITEBIN);
13124 if (fd == NULL)
13125 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013126 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013127 goto errret;
13128 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013129 if (argvars[1].v_type == VAR_NUMBER)
13130 {
13131 linenr_T lnum;
13132 buf_T *buf;
13133
13134 buf = buflist_findnr(argvars[1].vval.v_number);
13135 if (buf == NULL)
13136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013137 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013138 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013139 goto errret;
13140 }
13141
13142 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13143 {
13144 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13145 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13146 {
13147 err = TRUE;
13148 break;
13149 }
13150 if (putc(NL, fd) == EOF)
13151 {
13152 err = TRUE;
13153 break;
13154 }
13155 }
13156 }
13157 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013158 {
13159 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13160 err = TRUE;
13161 }
13162 else
13163 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013164 size_t len;
13165 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013166
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013167 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013168 if (p == NULL)
13169 {
13170 fclose(fd);
13171 goto errret; /* type error; errmsg already given */
13172 }
13173 len = STRLEN(p);
13174 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13175 err = TRUE;
13176 }
13177 if (fclose(fd) != 0)
13178 err = TRUE;
13179 if (err)
13180 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013181 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013182 goto errret;
13183 }
13184 }
13185
13186 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13187 * echoes typeahead, that messes up the display. */
13188 if (!msg_silent)
13189 flags += SHELL_COOKED;
13190
13191 if (retlist)
13192 {
13193 int len;
13194 listitem_T *li;
13195 char_u *s = NULL;
13196 char_u *start;
13197 char_u *end;
13198 int i;
13199
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013200 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013201 if (res == NULL)
13202 goto errret;
13203
13204 list = list_alloc();
13205 if (list == NULL)
13206 goto errret;
13207
13208 for (i = 0; i < len; ++i)
13209 {
13210 start = res + i;
13211 while (i < len && res[i] != NL)
13212 ++i;
13213 end = res + i;
13214
Bram Moolenaar964b3742019-05-24 18:54:09 +020013215 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013216 if (s == NULL)
13217 goto errret;
13218
13219 for (p = s; start < end; ++p, ++start)
13220 *p = *start == NUL ? NL : *start;
13221 *p = NUL;
13222
13223 li = listitem_alloc();
13224 if (li == NULL)
13225 {
13226 vim_free(s);
13227 goto errret;
13228 }
13229 li->li_tv.v_type = VAR_STRING;
13230 li->li_tv.v_lock = 0;
13231 li->li_tv.vval.v_string = s;
13232 list_append(list, li);
13233 }
13234
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013235 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013236 list = NULL;
13237 }
13238 else
13239 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013240 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013241#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013242 /* translate <CR><NL> into <NL> */
13243 if (res != NULL)
13244 {
13245 char_u *s, *d;
13246
13247 d = res;
13248 for (s = res; *s; ++s)
13249 {
13250 if (s[0] == CAR && s[1] == NL)
13251 ++s;
13252 *d++ = *s;
13253 }
13254 *d = NUL;
13255 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013256#endif
13257 rettv->vval.v_string = res;
13258 res = NULL;
13259 }
13260
13261errret:
13262 if (infile != NULL)
13263 {
13264 mch_remove(infile);
13265 vim_free(infile);
13266 }
13267 if (res != NULL)
13268 vim_free(res);
13269 if (list != NULL)
13270 list_free(list);
13271}
13272
13273/*
13274 * "system()" function
13275 */
13276 static void
13277f_system(typval_T *argvars, typval_T *rettv)
13278{
13279 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13280}
13281
13282/*
13283 * "systemlist()" function
13284 */
13285 static void
13286f_systemlist(typval_T *argvars, typval_T *rettv)
13287{
13288 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13289}
13290
13291/*
13292 * "tabpagebuflist()" function
13293 */
13294 static void
13295f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13296{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013297 tabpage_T *tp;
13298 win_T *wp = NULL;
13299
13300 if (argvars[0].v_type == VAR_UNKNOWN)
13301 wp = firstwin;
13302 else
13303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013304 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013305 if (tp != NULL)
13306 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13307 }
13308 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13309 {
13310 for (; wp != NULL; wp = wp->w_next)
13311 if (list_append_number(rettv->vval.v_list,
13312 wp->w_buffer->b_fnum) == FAIL)
13313 break;
13314 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013315}
13316
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013317/*
13318 * "tabpagenr()" function
13319 */
13320 static void
13321f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13322{
13323 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013324 char_u *arg;
13325
13326 if (argvars[0].v_type != VAR_UNKNOWN)
13327 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013328 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013329 nr = 0;
13330 if (arg != NULL)
13331 {
13332 if (STRCMP(arg, "$") == 0)
13333 nr = tabpage_index(NULL) - 1;
13334 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013335 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013336 }
13337 }
13338 else
13339 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013340 rettv->vval.v_number = nr;
13341}
13342
13343
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013344/*
13345 * Common code for tabpagewinnr() and winnr().
13346 */
13347 static int
13348get_winnr(tabpage_T *tp, typval_T *argvar)
13349{
13350 win_T *twin;
13351 int nr = 1;
13352 win_T *wp;
13353 char_u *arg;
13354
13355 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13356 if (argvar->v_type != VAR_UNKNOWN)
13357 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013358 int invalid_arg = FALSE;
13359
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013360 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013361 if (arg == NULL)
13362 nr = 0; /* type error; errmsg already given */
13363 else if (STRCMP(arg, "$") == 0)
13364 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13365 else if (STRCMP(arg, "#") == 0)
13366 {
13367 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13368 if (twin == NULL)
13369 nr = 0;
13370 }
13371 else
13372 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013373 long count;
13374 char_u *endp;
13375
13376 // Extract the window count (if specified). e.g. winnr('3j')
13377 count = strtol((char *)arg, (char **)&endp, 10);
13378 if (count <= 0)
13379 count = 1; // if count is not specified, default to 1
13380 if (endp != NULL && *endp != '\0')
13381 {
13382 if (STRCMP(endp, "j") == 0)
13383 twin = win_vert_neighbor(tp, twin, FALSE, count);
13384 else if (STRCMP(endp, "k") == 0)
13385 twin = win_vert_neighbor(tp, twin, TRUE, count);
13386 else if (STRCMP(endp, "h") == 0)
13387 twin = win_horz_neighbor(tp, twin, TRUE, count);
13388 else if (STRCMP(endp, "l") == 0)
13389 twin = win_horz_neighbor(tp, twin, FALSE, count);
13390 else
13391 invalid_arg = TRUE;
13392 }
13393 else
13394 invalid_arg = TRUE;
13395 }
13396
13397 if (invalid_arg)
13398 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013399 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013400 nr = 0;
13401 }
13402 }
13403
13404 if (nr > 0)
13405 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13406 wp != twin; wp = wp->w_next)
13407 {
13408 if (wp == NULL)
13409 {
13410 /* didn't find it in this tabpage */
13411 nr = 0;
13412 break;
13413 }
13414 ++nr;
13415 }
13416 return nr;
13417}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013418
13419/*
13420 * "tabpagewinnr()" function
13421 */
13422 static void
13423f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13424{
13425 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013426 tabpage_T *tp;
13427
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013428 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013429 if (tp == NULL)
13430 nr = 0;
13431 else
13432 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013433 rettv->vval.v_number = nr;
13434}
13435
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013436/*
13437 * "tagfiles()" function
13438 */
13439 static void
13440f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13441{
13442 char_u *fname;
13443 tagname_T tn;
13444 int first;
13445
13446 if (rettv_list_alloc(rettv) == FAIL)
13447 return;
13448 fname = alloc(MAXPATHL);
13449 if (fname == NULL)
13450 return;
13451
13452 for (first = TRUE; ; first = FALSE)
13453 if (get_tagfname(&tn, first, fname) == FAIL
13454 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13455 break;
13456 tagname_free(&tn);
13457 vim_free(fname);
13458}
13459
13460/*
13461 * "taglist()" function
13462 */
13463 static void
13464f_taglist(typval_T *argvars, typval_T *rettv)
13465{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013466 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013467 char_u *tag_pattern;
13468
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013469 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013470
13471 rettv->vval.v_number = FALSE;
13472 if (*tag_pattern == NUL)
13473 return;
13474
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013475 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013476 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013477 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013478 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013479}
13480
13481/*
13482 * "tempname()" function
13483 */
13484 static void
13485f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13486{
13487 static int x = 'A';
13488
13489 rettv->v_type = VAR_STRING;
13490 rettv->vval.v_string = vim_tempname(x, FALSE);
13491
13492 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13493 * names. Skip 'I' and 'O', they are used for shell redirection. */
13494 do
13495 {
13496 if (x == 'Z')
13497 x = '0';
13498 else if (x == '9')
13499 x = 'A';
13500 else
13501 {
13502#ifdef EBCDIC
13503 if (x == 'I')
13504 x = 'J';
13505 else if (x == 'R')
13506 x = 'S';
13507 else
13508#endif
13509 ++x;
13510 }
13511 } while (x == 'I' || x == 'O');
13512}
13513
13514#ifdef FEAT_FLOAT
13515/*
13516 * "tan()" function
13517 */
13518 static void
13519f_tan(typval_T *argvars, typval_T *rettv)
13520{
13521 float_T f = 0.0;
13522
13523 rettv->v_type = VAR_FLOAT;
13524 if (get_float_arg(argvars, &f) == OK)
13525 rettv->vval.v_float = tan(f);
13526 else
13527 rettv->vval.v_float = 0.0;
13528}
13529
13530/*
13531 * "tanh()" function
13532 */
13533 static void
13534f_tanh(typval_T *argvars, typval_T *rettv)
13535{
13536 float_T f = 0.0;
13537
13538 rettv->v_type = VAR_FLOAT;
13539 if (get_float_arg(argvars, &f) == OK)
13540 rettv->vval.v_float = tanh(f);
13541 else
13542 rettv->vval.v_float = 0.0;
13543}
13544#endif
13545
13546/*
13547 * "test_alloc_fail(id, countdown, repeat)" function
13548 */
13549 static void
13550f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13551{
13552 if (argvars[0].v_type != VAR_NUMBER
13553 || argvars[0].vval.v_number <= 0
13554 || argvars[1].v_type != VAR_NUMBER
13555 || argvars[1].vval.v_number < 0
13556 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013557 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013558 else
13559 {
13560 alloc_fail_id = argvars[0].vval.v_number;
13561 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013562 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013563 alloc_fail_countdown = argvars[1].vval.v_number;
13564 alloc_fail_repeat = argvars[2].vval.v_number;
13565 did_outofmem_msg = FALSE;
13566 }
13567}
13568
13569/*
13570 * "test_autochdir()"
13571 */
13572 static void
13573f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13574{
13575#if defined(FEAT_AUTOCHDIR)
13576 test_autochdir = TRUE;
13577#endif
13578}
13579
13580/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013581 * "test_feedinput()"
13582 */
13583 static void
13584f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13585{
13586#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013587 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013588
13589 if (val != NULL)
13590 {
13591 trash_input_buf();
13592 add_to_input_buf_csi(val, (int)STRLEN(val));
13593 }
13594#endif
13595}
13596
13597/*
Bram Moolenaareda65222019-05-16 20:29:44 +020013598 * "test_getvalue({name})" function
13599 */
13600 static void
13601f_test_getvalue(typval_T *argvars, typval_T *rettv)
13602{
13603 if (argvars[0].v_type != VAR_STRING)
13604 emsg(_(e_invarg));
13605 else
13606 {
13607 char_u *name = tv_get_string(&argvars[0]);
13608
13609 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
13610 rettv->vval.v_number = need_fileinfo;
13611 else
13612 semsg(_(e_invarg2), name);
13613 }
13614}
13615
13616/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013617 * "test_option_not_set({name})" function
13618 */
13619 static void
13620f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13621{
13622 char_u *name = (char_u *)"";
13623
13624 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013625 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013626 else
13627 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013628 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013629 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013630 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013631 }
13632}
13633
13634/*
13635 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013636 */
13637 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013638f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013639{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013640 char_u *name = (char_u *)"";
13641 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013642 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013643
13644 if (argvars[0].v_type != VAR_STRING
13645 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013646 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013647 else
13648 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013649 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013650 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013651
13652 if (STRCMP(name, (char_u *)"redraw") == 0)
13653 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013654 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13655 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013656 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13657 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013658 else if (STRCMP(name, (char_u *)"starting") == 0)
13659 {
13660 if (val)
13661 {
13662 if (save_starting < 0)
13663 save_starting = starting;
13664 starting = 0;
13665 }
13666 else
13667 {
13668 starting = save_starting;
13669 save_starting = -1;
13670 }
13671 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013672 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13673 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013674 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
13675 no_query_mouse_for_testing = val;
Bram Moolenaaradc67142019-06-22 01:40:42 +020013676 else if (STRCMP(name, (char_u *)"no_wait_return") == 0)
13677 no_wait_return = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013678 else if (STRCMP(name, (char_u *)"ALL") == 0)
13679 {
13680 disable_char_avail_for_testing = FALSE;
13681 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013682 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013683 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013684 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013685 if (save_starting >= 0)
13686 {
13687 starting = save_starting;
13688 save_starting = -1;
13689 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013690 }
13691 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013692 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013693 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013694}
13695
13696/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010013697 * "test_refcount({expr})" function
13698 */
13699 static void
13700f_test_refcount(typval_T *argvars, typval_T *rettv)
13701{
13702 int retval = -1;
13703
13704 switch (argvars[0].v_type)
13705 {
13706 case VAR_UNKNOWN:
13707 case VAR_NUMBER:
13708 case VAR_FLOAT:
13709 case VAR_SPECIAL:
13710 case VAR_STRING:
13711 break;
13712 case VAR_JOB:
13713#ifdef FEAT_JOB_CHANNEL
13714 if (argvars[0].vval.v_job != NULL)
13715 retval = argvars[0].vval.v_job->jv_refcount - 1;
13716#endif
13717 break;
13718 case VAR_CHANNEL:
13719#ifdef FEAT_JOB_CHANNEL
13720 if (argvars[0].vval.v_channel != NULL)
13721 retval = argvars[0].vval.v_channel->ch_refcount - 1;
13722#endif
13723 break;
13724 case VAR_FUNC:
13725 if (argvars[0].vval.v_string != NULL)
13726 {
13727 ufunc_T *fp;
13728
13729 fp = find_func(argvars[0].vval.v_string);
13730 if (fp != NULL)
13731 retval = fp->uf_refcount;
13732 }
13733 break;
13734 case VAR_PARTIAL:
13735 if (argvars[0].vval.v_partial != NULL)
13736 retval = argvars[0].vval.v_partial->pt_refcount - 1;
13737 break;
13738 case VAR_BLOB:
13739 if (argvars[0].vval.v_blob != NULL)
13740 retval = argvars[0].vval.v_blob->bv_refcount - 1;
13741 break;
13742 case VAR_LIST:
13743 if (argvars[0].vval.v_list != NULL)
13744 retval = argvars[0].vval.v_list->lv_refcount - 1;
13745 break;
13746 case VAR_DICT:
13747 if (argvars[0].vval.v_dict != NULL)
13748 retval = argvars[0].vval.v_dict->dv_refcount - 1;
13749 break;
13750 }
13751
13752 rettv->v_type = VAR_NUMBER;
13753 rettv->vval.v_number = retval;
13754
13755}
13756
13757/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013758 * "test_garbagecollect_now()" function
13759 */
13760 static void
13761f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13762{
13763 /* This is dangerous, any Lists and Dicts used internally may be freed
13764 * while still in use. */
13765 garbage_collect(TRUE);
13766}
13767
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013768/*
Bram Moolenaaradc67142019-06-22 01:40:42 +020013769 * "test_garbagecollect_soon()" function
13770 */
13771 static void
13772f_test_garbagecollect_soon(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13773{
13774 may_garbage_collect = TRUE;
13775}
13776
13777/*
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013778 * "test_ignore_error()" function
13779 */
13780 static void
13781f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13782{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013783 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013784}
13785
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013786 static void
13787f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13788{
13789 rettv->v_type = VAR_BLOB;
13790 rettv->vval.v_blob = NULL;
13791}
13792
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013793#ifdef FEAT_JOB_CHANNEL
13794 static void
13795f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13796{
13797 rettv->v_type = VAR_CHANNEL;
13798 rettv->vval.v_channel = NULL;
13799}
13800#endif
13801
13802 static void
13803f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13804{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013805 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013806}
13807
13808#ifdef FEAT_JOB_CHANNEL
13809 static void
13810f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13811{
13812 rettv->v_type = VAR_JOB;
13813 rettv->vval.v_job = NULL;
13814}
13815#endif
13816
13817 static void
13818f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13819{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013820 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013821}
13822
13823 static void
13824f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13825{
13826 rettv->v_type = VAR_PARTIAL;
13827 rettv->vval.v_partial = NULL;
13828}
13829
13830 static void
13831f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13832{
13833 rettv->v_type = VAR_STRING;
13834 rettv->vval.v_string = NULL;
13835}
13836
Bram Moolenaarab186732018-09-14 21:27:06 +020013837#ifdef FEAT_GUI
13838 static void
13839f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13840{
13841 char_u *which;
13842 long value;
13843 int dragging;
13844 scrollbar_T *sb = NULL;
13845
13846 if (argvars[0].v_type != VAR_STRING
13847 || (argvars[1].v_type) != VAR_NUMBER
13848 || (argvars[2].v_type) != VAR_NUMBER)
13849 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013850 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013851 return;
13852 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013853 which = tv_get_string(&argvars[0]);
13854 value = tv_get_number(&argvars[1]);
13855 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013856
13857 if (STRCMP(which, "left") == 0)
13858 sb = &curwin->w_scrollbars[SBAR_LEFT];
13859 else if (STRCMP(which, "right") == 0)
13860 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13861 else if (STRCMP(which, "hor") == 0)
13862 sb = &gui.bottom_sbar;
13863 if (sb == NULL)
13864 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013865 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020013866 return;
13867 }
13868 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013869# ifndef USE_ON_FLY_SCROLL
13870 // need to loop through normal_cmd() to handle the scroll events
13871 exec_normal(FALSE, TRUE, FALSE);
13872# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013873}
13874#endif
13875
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013876#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013877 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013878f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
13879{
13880 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
13881 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
13882}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013883#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013884
13885 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013886f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13887{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013888 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013889}
13890
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013891/*
13892 * Get a callback from "arg". It can be a Funcref or a function name.
13893 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013894 * "cb_name" is not allocated.
13895 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013896 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013897 callback_T
13898get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013899{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013900 callback_T res;
13901
13902 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013903 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13904 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013905 res.cb_partial = arg->vval.v_partial;
13906 ++res.cb_partial->pt_refcount;
13907 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013908 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013909 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013910 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013911 res.cb_partial = NULL;
13912 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
13913 {
13914 // Note that we don't make a copy of the string.
13915 res.cb_name = arg->vval.v_string;
13916 func_ref(res.cb_name);
13917 }
13918 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13919 {
13920 res.cb_name = (char_u *)"";
13921 }
13922 else
13923 {
13924 emsg(_("E921: Invalid callback argument"));
13925 res.cb_name = NULL;
13926 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013927 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013928 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013929}
13930
13931/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013932 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013933 */
13934 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013935put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013936{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013937 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013938 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013939 tv->v_type = VAR_PARTIAL;
13940 tv->vval.v_partial = cb->cb_partial;
13941 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013942 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013943 else
13944 {
13945 tv->v_type = VAR_FUNC;
13946 tv->vval.v_string = vim_strsave(cb->cb_name);
13947 func_ref(cb->cb_name);
13948 }
13949}
13950
13951/*
13952 * Make a copy of "src" into "dest", allocating the function name if needed,
13953 * without incrementing the refcount.
13954 */
13955 void
13956set_callback(callback_T *dest, callback_T *src)
13957{
13958 if (src->cb_partial == NULL)
13959 {
13960 // just a function name, make a copy
13961 dest->cb_name = vim_strsave(src->cb_name);
13962 dest->cb_free_name = TRUE;
13963 }
13964 else
13965 {
13966 // cb_name is a pointer into cb_partial
13967 dest->cb_name = src->cb_name;
13968 dest->cb_free_name = FALSE;
13969 }
13970 dest->cb_partial = src->cb_partial;
13971}
13972
13973/*
13974 * Unref/free "callback" returned by get_callback() or set_callback().
13975 */
13976 void
13977free_callback(callback_T *callback)
13978{
13979 if (callback->cb_partial != NULL)
13980 {
13981 partial_unref(callback->cb_partial);
13982 callback->cb_partial = NULL;
13983 }
13984 else if (callback->cb_name != NULL)
13985 func_unref(callback->cb_name);
13986 if (callback->cb_free_name)
13987 {
13988 vim_free(callback->cb_name);
13989 callback->cb_free_name = FALSE;
13990 }
13991 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013992}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013993
13994#ifdef FEAT_TIMERS
13995/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013996 * "timer_info([timer])" function
13997 */
13998 static void
13999f_timer_info(typval_T *argvars, typval_T *rettv)
14000{
14001 timer_T *timer = NULL;
14002
14003 if (rettv_list_alloc(rettv) != OK)
14004 return;
14005 if (argvars[0].v_type != VAR_UNKNOWN)
14006 {
14007 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014008 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014009 else
14010 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014011 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014012 if (timer != NULL)
14013 add_timer_info(rettv, timer);
14014 }
14015 }
14016 else
14017 add_timer_info_all(rettv);
14018}
14019
14020/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014021 * "timer_pause(timer, paused)" function
14022 */
14023 static void
14024f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14025{
14026 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014027 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014028
14029 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014030 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014031 else
14032 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014033 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014034 if (timer != NULL)
14035 timer->tr_paused = paused;
14036 }
14037}
14038
14039/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014040 * "timer_start(time, callback [, options])" function
14041 */
14042 static void
14043f_timer_start(typval_T *argvars, typval_T *rettv)
14044{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014045 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014046 timer_T *timer;
14047 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014048 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014049 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014050
Bram Moolenaar75537a92016-09-05 22:45:28 +020014051 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014052 if (check_secure())
14053 return;
14054 if (argvars[2].v_type != VAR_UNKNOWN)
14055 {
14056 if (argvars[2].v_type != VAR_DICT
14057 || (dict = argvars[2].vval.v_dict) == NULL)
14058 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014059 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014060 return;
14061 }
14062 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014063 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014064 }
14065
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014066 callback = get_callback(&argvars[1]);
14067 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014068 return;
14069
14070 timer = create_timer(msec, repeat);
14071 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014072 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014073 else
14074 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014075 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014076 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014077 }
14078}
14079
14080/*
14081 * "timer_stop(timer)" function
14082 */
14083 static void
14084f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14085{
14086 timer_T *timer;
14087
14088 if (argvars[0].v_type != VAR_NUMBER)
14089 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014090 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014091 return;
14092 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014093 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014094 if (timer != NULL)
14095 stop_timer(timer);
14096}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014097
14098/*
14099 * "timer_stopall()" function
14100 */
14101 static void
14102f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14103{
14104 stop_all_timers();
14105}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014106#endif
14107
14108/*
14109 * "tolower(string)" function
14110 */
14111 static void
14112f_tolower(typval_T *argvars, typval_T *rettv)
14113{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014114 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014115 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014116}
14117
14118/*
14119 * "toupper(string)" function
14120 */
14121 static void
14122f_toupper(typval_T *argvars, typval_T *rettv)
14123{
14124 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014125 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014126}
14127
14128/*
14129 * "tr(string, fromstr, tostr)" function
14130 */
14131 static void
14132f_tr(typval_T *argvars, typval_T *rettv)
14133{
14134 char_u *in_str;
14135 char_u *fromstr;
14136 char_u *tostr;
14137 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014138 int inlen;
14139 int fromlen;
14140 int tolen;
14141 int idx;
14142 char_u *cpstr;
14143 int cplen;
14144 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014145 char_u buf[NUMBUFLEN];
14146 char_u buf2[NUMBUFLEN];
14147 garray_T ga;
14148
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014149 in_str = tv_get_string(&argvars[0]);
14150 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14151 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014152
14153 /* Default return value: empty string. */
14154 rettv->v_type = VAR_STRING;
14155 rettv->vval.v_string = NULL;
14156 if (fromstr == NULL || tostr == NULL)
14157 return; /* type error; errmsg already given */
14158 ga_init2(&ga, (int)sizeof(char), 80);
14159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014160 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014161 /* not multi-byte: fromstr and tostr must be the same length */
14162 if (STRLEN(fromstr) != STRLEN(tostr))
14163 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014164error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014165 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014166 ga_clear(&ga);
14167 return;
14168 }
14169
14170 /* fromstr and tostr have to contain the same number of chars */
14171 while (*in_str != NUL)
14172 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014173 if (has_mbyte)
14174 {
14175 inlen = (*mb_ptr2len)(in_str);
14176 cpstr = in_str;
14177 cplen = inlen;
14178 idx = 0;
14179 for (p = fromstr; *p != NUL; p += fromlen)
14180 {
14181 fromlen = (*mb_ptr2len)(p);
14182 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14183 {
14184 for (p = tostr; *p != NUL; p += tolen)
14185 {
14186 tolen = (*mb_ptr2len)(p);
14187 if (idx-- == 0)
14188 {
14189 cplen = tolen;
14190 cpstr = p;
14191 break;
14192 }
14193 }
14194 if (*p == NUL) /* tostr is shorter than fromstr */
14195 goto error;
14196 break;
14197 }
14198 ++idx;
14199 }
14200
14201 if (first && cpstr == in_str)
14202 {
14203 /* Check that fromstr and tostr have the same number of
14204 * (multi-byte) characters. Done only once when a character
14205 * of in_str doesn't appear in fromstr. */
14206 first = FALSE;
14207 for (p = tostr; *p != NUL; p += tolen)
14208 {
14209 tolen = (*mb_ptr2len)(p);
14210 --idx;
14211 }
14212 if (idx != 0)
14213 goto error;
14214 }
14215
14216 (void)ga_grow(&ga, cplen);
14217 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14218 ga.ga_len += cplen;
14219
14220 in_str += inlen;
14221 }
14222 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014223 {
14224 /* When not using multi-byte chars we can do it faster. */
14225 p = vim_strchr(fromstr, *in_str);
14226 if (p != NULL)
14227 ga_append(&ga, tostr[p - fromstr]);
14228 else
14229 ga_append(&ga, *in_str);
14230 ++in_str;
14231 }
14232 }
14233
14234 /* add a terminating NUL */
14235 (void)ga_grow(&ga, 1);
14236 ga_append(&ga, NUL);
14237
14238 rettv->vval.v_string = ga.ga_data;
14239}
14240
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014241/*
14242 * "trim({expr})" function
14243 */
14244 static void
14245f_trim(typval_T *argvars, typval_T *rettv)
14246{
14247 char_u buf1[NUMBUFLEN];
14248 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014249 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014250 char_u *mask = NULL;
14251 char_u *tail;
14252 char_u *prev;
14253 char_u *p;
14254 int c1;
14255
14256 rettv->v_type = VAR_STRING;
14257 if (head == NULL)
14258 {
14259 rettv->vval.v_string = NULL;
14260 return;
14261 }
14262
14263 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014264 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014265
14266 while (*head != NUL)
14267 {
14268 c1 = PTR2CHAR(head);
14269 if (mask == NULL)
14270 {
14271 if (c1 > ' ' && c1 != 0xa0)
14272 break;
14273 }
14274 else
14275 {
14276 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14277 if (c1 == PTR2CHAR(p))
14278 break;
14279 if (*p == NUL)
14280 break;
14281 }
14282 MB_PTR_ADV(head);
14283 }
14284
14285 for (tail = head + STRLEN(head); tail > head; tail = prev)
14286 {
14287 prev = tail;
14288 MB_PTR_BACK(head, prev);
14289 c1 = PTR2CHAR(prev);
14290 if (mask == NULL)
14291 {
14292 if (c1 > ' ' && c1 != 0xa0)
14293 break;
14294 }
14295 else
14296 {
14297 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14298 if (c1 == PTR2CHAR(p))
14299 break;
14300 if (*p == NUL)
14301 break;
14302 }
14303 }
14304 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14305}
14306
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014307#ifdef FEAT_FLOAT
14308/*
14309 * "trunc({float})" function
14310 */
14311 static void
14312f_trunc(typval_T *argvars, typval_T *rettv)
14313{
14314 float_T f = 0.0;
14315
14316 rettv->v_type = VAR_FLOAT;
14317 if (get_float_arg(argvars, &f) == OK)
14318 /* trunc() is not in C90, use floor() or ceil() instead. */
14319 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14320 else
14321 rettv->vval.v_float = 0.0;
14322}
14323#endif
14324
14325/*
14326 * "type(expr)" function
14327 */
14328 static void
14329f_type(typval_T *argvars, typval_T *rettv)
14330{
14331 int n = -1;
14332
14333 switch (argvars[0].v_type)
14334 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014335 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14336 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014337 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014338 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14339 case VAR_LIST: n = VAR_TYPE_LIST; break;
14340 case VAR_DICT: n = VAR_TYPE_DICT; break;
14341 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014342 case VAR_SPECIAL:
14343 if (argvars[0].vval.v_number == VVAL_FALSE
14344 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014345 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014346 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014347 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014348 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014349 case VAR_JOB: n = VAR_TYPE_JOB; break;
14350 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014351 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014352 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014353 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014354 n = -1;
14355 break;
14356 }
14357 rettv->vval.v_number = n;
14358}
14359
14360/*
14361 * "undofile(name)" function
14362 */
14363 static void
14364f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14365{
14366 rettv->v_type = VAR_STRING;
14367#ifdef FEAT_PERSISTENT_UNDO
14368 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014369 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014370
14371 if (*fname == NUL)
14372 {
14373 /* If there is no file name there will be no undo file. */
14374 rettv->vval.v_string = NULL;
14375 }
14376 else
14377 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020014378 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014379
14380 if (ffname != NULL)
14381 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14382 vim_free(ffname);
14383 }
14384 }
14385#else
14386 rettv->vval.v_string = NULL;
14387#endif
14388}
14389
14390/*
14391 * "undotree()" function
14392 */
14393 static void
14394f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14395{
14396 if (rettv_dict_alloc(rettv) == OK)
14397 {
14398 dict_T *dict = rettv->vval.v_dict;
14399 list_T *list;
14400
Bram Moolenaare0be1672018-07-08 16:50:37 +020014401 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14402 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14403 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14404 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14405 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14406 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014407
14408 list = list_alloc();
14409 if (list != NULL)
14410 {
14411 u_eval_tree(curbuf->b_u_oldhead, list);
14412 dict_add_list(dict, "entries", list);
14413 }
14414 }
14415}
14416
14417/*
14418 * "values(dict)" function
14419 */
14420 static void
14421f_values(typval_T *argvars, typval_T *rettv)
14422{
14423 dict_list(argvars, rettv, 1);
14424}
14425
14426/*
14427 * "virtcol(string)" function
14428 */
14429 static void
14430f_virtcol(typval_T *argvars, typval_T *rettv)
14431{
14432 colnr_T vcol = 0;
14433 pos_T *fp;
14434 int fnum = curbuf->b_fnum;
14435
14436 fp = var2fpos(&argvars[0], FALSE, &fnum);
14437 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14438 && fnum == curbuf->b_fnum)
14439 {
14440 getvvcol(curwin, fp, NULL, NULL, &vcol);
14441 ++vcol;
14442 }
14443
14444 rettv->vval.v_number = vcol;
14445}
14446
14447/*
14448 * "visualmode()" function
14449 */
14450 static void
14451f_visualmode(typval_T *argvars, typval_T *rettv)
14452{
14453 char_u str[2];
14454
14455 rettv->v_type = VAR_STRING;
14456 str[0] = curbuf->b_visual_mode_eval;
14457 str[1] = NUL;
14458 rettv->vval.v_string = vim_strsave(str);
14459
14460 /* A non-zero number or non-empty string argument: reset mode. */
14461 if (non_zero_arg(&argvars[0]))
14462 curbuf->b_visual_mode_eval = NUL;
14463}
14464
14465/*
14466 * "wildmenumode()" function
14467 */
14468 static void
14469f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14470{
14471#ifdef FEAT_WILDMENU
14472 if (wild_menu_showing)
14473 rettv->vval.v_number = 1;
14474#endif
14475}
14476
14477/*
14478 * "winbufnr(nr)" function
14479 */
14480 static void
14481f_winbufnr(typval_T *argvars, typval_T *rettv)
14482{
14483 win_T *wp;
14484
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014485 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014486 if (wp == NULL)
14487 rettv->vval.v_number = -1;
14488 else
14489 rettv->vval.v_number = wp->w_buffer->b_fnum;
14490}
14491
14492/*
14493 * "wincol()" function
14494 */
14495 static void
14496f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14497{
14498 validate_cursor();
14499 rettv->vval.v_number = curwin->w_wcol + 1;
14500}
14501
14502/*
14503 * "winheight(nr)" function
14504 */
14505 static void
14506f_winheight(typval_T *argvars, typval_T *rettv)
14507{
14508 win_T *wp;
14509
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014510 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014511 if (wp == NULL)
14512 rettv->vval.v_number = -1;
14513 else
14514 rettv->vval.v_number = wp->w_height;
14515}
14516
14517/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014518 * "winlayout()" function
14519 */
14520 static void
14521f_winlayout(typval_T *argvars, typval_T *rettv)
14522{
14523 tabpage_T *tp;
14524
14525 if (rettv_list_alloc(rettv) != OK)
14526 return;
14527
14528 if (argvars[0].v_type == VAR_UNKNOWN)
14529 tp = curtab;
14530 else
14531 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014532 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014533 if (tp == NULL)
14534 return;
14535 }
14536
14537 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14538}
14539
14540/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014541 * "winline()" function
14542 */
14543 static void
14544f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14545{
14546 validate_cursor();
14547 rettv->vval.v_number = curwin->w_wrow + 1;
14548}
14549
14550/*
14551 * "winnr()" function
14552 */
14553 static void
14554f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14555{
14556 int nr = 1;
14557
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014558 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014559 rettv->vval.v_number = nr;
14560}
14561
14562/*
14563 * "winrestcmd()" function
14564 */
14565 static void
14566f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14567{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014568 win_T *wp;
14569 int winnr = 1;
14570 garray_T ga;
14571 char_u buf[50];
14572
14573 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014574 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014575 {
14576 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14577 ga_concat(&ga, buf);
14578 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14579 ga_concat(&ga, buf);
14580 ++winnr;
14581 }
14582 ga_append(&ga, NUL);
14583
14584 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014585 rettv->v_type = VAR_STRING;
14586}
14587
14588/*
14589 * "winrestview()" function
14590 */
14591 static void
14592f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14593{
14594 dict_T *dict;
14595
14596 if (argvars[0].v_type != VAR_DICT
14597 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014598 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014599 else
14600 {
14601 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014602 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014603 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014604 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014605 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014606 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014607 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14608 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014609 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014610 curwin->w_set_curswant = FALSE;
14611 }
14612
14613 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014614 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014615#ifdef FEAT_DIFF
14616 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014617 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014618#endif
14619 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014620 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014621 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014622 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014623
14624 check_cursor();
14625 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014626 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014627 changed_window_setting();
14628
14629 if (curwin->w_topline <= 0)
14630 curwin->w_topline = 1;
14631 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14632 curwin->w_topline = curbuf->b_ml.ml_line_count;
14633#ifdef FEAT_DIFF
14634 check_topfill(curwin, TRUE);
14635#endif
14636 }
14637}
14638
14639/*
14640 * "winsaveview()" function
14641 */
14642 static void
14643f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14644{
14645 dict_T *dict;
14646
14647 if (rettv_dict_alloc(rettv) == FAIL)
14648 return;
14649 dict = rettv->vval.v_dict;
14650
Bram Moolenaare0be1672018-07-08 16:50:37 +020014651 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14652 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020014653 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014654 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014655 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014656
Bram Moolenaare0be1672018-07-08 16:50:37 +020014657 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014658#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014659 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014660#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014661 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14662 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014663}
14664
14665/*
14666 * "winwidth(nr)" function
14667 */
14668 static void
14669f_winwidth(typval_T *argvars, typval_T *rettv)
14670{
14671 win_T *wp;
14672
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014673 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014674 if (wp == NULL)
14675 rettv->vval.v_number = -1;
14676 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014677 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014678}
14679
14680/*
14681 * "wordcount()" function
14682 */
14683 static void
14684f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14685{
14686 if (rettv_dict_alloc(rettv) == FAIL)
14687 return;
14688 cursor_pos_info(rettv->vval.v_dict);
14689}
14690
14691/*
14692 * "writefile()" function
14693 */
14694 static void
14695f_writefile(typval_T *argvars, typval_T *rettv)
14696{
14697 int binary = FALSE;
14698 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014699#ifdef HAVE_FSYNC
14700 int do_fsync = p_fs;
14701#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014702 char_u *fname;
14703 FILE *fd;
14704 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014705 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014706 list_T *list = NULL;
14707 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014708
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014709 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010014710 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014711 return;
14712
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014713 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014714 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014715 list = argvars[0].vval.v_list;
14716 if (list == NULL)
14717 return;
14718 for (li = list->lv_first; li != NULL; li = li->li_next)
14719 if (tv_get_string_chk(&li->li_tv) == NULL)
14720 return;
14721 }
14722 else if (argvars[0].v_type == VAR_BLOB)
14723 {
14724 blob = argvars[0].vval.v_blob;
14725 if (blob == NULL)
14726 return;
14727 }
14728 else
14729 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014730 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014731 return;
14732 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014733
14734 if (argvars[2].v_type != VAR_UNKNOWN)
14735 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014736 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014737
14738 if (arg2 == NULL)
14739 return;
14740 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014741 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014742 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014743 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014744#ifdef HAVE_FSYNC
14745 if (vim_strchr(arg2, 's') != NULL)
14746 do_fsync = TRUE;
14747 else if (vim_strchr(arg2, 'S') != NULL)
14748 do_fsync = FALSE;
14749#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014750 }
14751
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014752 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014753 if (fname == NULL)
14754 return;
14755
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014756 /* Always open the file in binary mode, library functions have a mind of
14757 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014758 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14759 append ? APPENDBIN : WRITEBIN)) == NULL)
14760 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014761 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014762 ret = -1;
14763 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014764 else if (blob)
14765 {
14766 if (write_blob(fd, blob) == FAIL)
14767 ret = -1;
14768#ifdef HAVE_FSYNC
14769 else if (do_fsync)
14770 // Ignore the error, the user wouldn't know what to do about it.
14771 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010014772 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014773#endif
14774 fclose(fd);
14775 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014776 else
14777 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014778 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014779 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014780#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014781 else if (do_fsync)
14782 /* Ignore the error, the user wouldn't know what to do about it.
14783 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010014784 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014785#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014786 fclose(fd);
14787 }
14788
14789 rettv->vval.v_number = ret;
14790}
14791
14792/*
14793 * "xor(expr, expr)" function
14794 */
14795 static void
14796f_xor(typval_T *argvars, typval_T *rettv)
14797{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014798 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14799 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014800}
14801
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014802#endif /* FEAT_EVAL */