blob: 7c9860f8a1ee6dd88c2ee2f398d01ddf3a48090d [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 Moolenaar3ff5f0f2019-06-10 13:11:22 +0200774 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200775 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200776 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200777 {"popup_dialog", 2, 2, f_popup_dialog},
Bram Moolenaara730e552019-06-16 19:05:31 +0200778 {"popup_filter_menu", 2, 2, f_popup_filter_menu},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200779 {"popup_filter_yesno", 2, 2, f_popup_filter_yesno},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200780 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200781 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200782 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaara730e552019-06-16 19:05:31 +0200783 {"popup_menu", 2, 2, f_popup_menu},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200784 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200785 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaarae943152019-06-16 22:54:14 +0200786 {"popup_setoptions", 2, 2, f_popup_setoptions},
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200787 {"popup_settext", 2, 2, f_popup_settext},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200788 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200789#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200790#ifdef FEAT_FLOAT
791 {"pow", 2, 2, f_pow},
792#endif
793 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100794 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200795#ifdef FEAT_JOB_CHANNEL
796 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200797 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200798 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
799#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100800#ifdef FEAT_TEXT_PROP
801 {"prop_add", 3, 3, f_prop_add},
802 {"prop_clear", 1, 3, f_prop_clear},
803 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100804 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100805 {"prop_type_add", 2, 2, f_prop_type_add},
806 {"prop_type_change", 2, 2, f_prop_type_change},
807 {"prop_type_delete", 1, 2, f_prop_type_delete},
808 {"prop_type_get", 1, 2, f_prop_type_get},
809 {"prop_type_list", 0, 1, f_prop_type_list},
810#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200811 {"pumvisible", 0, 0, f_pumvisible},
812#ifdef FEAT_PYTHON3
813 {"py3eval", 1, 1, f_py3eval},
814#endif
815#ifdef FEAT_PYTHON
816 {"pyeval", 1, 1, f_pyeval},
817#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100818#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
819 {"pyxeval", 1, 1, f_pyxeval},
820#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200821 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200822 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200823 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200824 {"reg_executing", 0, 0, f_reg_executing},
825 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200826 {"reltime", 0, 2, f_reltime},
827#ifdef FEAT_FLOAT
828 {"reltimefloat", 1, 1, f_reltimefloat},
829#endif
830 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100831 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200832 {"remote_foreground", 1, 1, f_remote_foreground},
833 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100834 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200835 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100836 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200837 {"remove", 2, 3, f_remove},
838 {"rename", 2, 2, f_rename},
839 {"repeat", 2, 2, f_repeat},
840 {"resolve", 1, 1, f_resolve},
841 {"reverse", 1, 1, f_reverse},
842#ifdef FEAT_FLOAT
843 {"round", 1, 1, f_round},
844#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100845#ifdef FEAT_RUBY
846 {"rubyeval", 1, 1, f_rubyeval},
847#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200848 {"screenattr", 2, 2, f_screenattr},
849 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100850 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200851 {"screencol", 0, 0, f_screencol},
852 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100853 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200854 {"search", 1, 4, f_search},
855 {"searchdecl", 1, 3, f_searchdecl},
856 {"searchpair", 3, 7, f_searchpair},
857 {"searchpairpos", 3, 7, f_searchpairpos},
858 {"searchpos", 1, 4, f_searchpos},
859 {"server2client", 2, 2, f_server2client},
860 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200861 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200862 {"setbufvar", 3, 3, f_setbufvar},
863 {"setcharsearch", 1, 1, f_setcharsearch},
864 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200865 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200866 {"setfperm", 2, 2, f_setfperm},
867 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200868 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100869 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200870 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200871 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200872 {"setreg", 2, 3, f_setreg},
873 {"settabvar", 3, 3, f_settabvar},
874 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100875 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200876 {"setwinvar", 3, 3, f_setwinvar},
877#ifdef FEAT_CRYPT
878 {"sha256", 1, 1, f_sha256},
879#endif
880 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100881 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100882#ifdef FEAT_SIGNS
883 {"sign_define", 1, 2, f_sign_define},
884 {"sign_getdefined", 0, 1, f_sign_getdefined},
885 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100886 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100887 {"sign_place", 4, 5, f_sign_place},
888 {"sign_undefine", 0, 1, f_sign_undefine},
889 {"sign_unplace", 1, 2, f_sign_unplace},
890#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200891 {"simplify", 1, 1, f_simplify},
892#ifdef FEAT_FLOAT
893 {"sin", 1, 1, f_sin},
894 {"sinh", 1, 1, f_sinh},
895#endif
896 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200897#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200898 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200899 {"sound_playevent", 1, 2, f_sound_playevent},
900 {"sound_playfile", 1, 2, f_sound_playfile},
901 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200902#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200903 {"soundfold", 1, 1, f_soundfold},
904 {"spellbadword", 0, 1, f_spellbadword},
905 {"spellsuggest", 1, 3, f_spellsuggest},
906 {"split", 1, 3, f_split},
907#ifdef FEAT_FLOAT
908 {"sqrt", 1, 1, f_sqrt},
909 {"str2float", 1, 1, f_str2float},
910#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200911 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200912 {"str2nr", 1, 2, f_str2nr},
913 {"strcharpart", 2, 3, f_strcharpart},
914 {"strchars", 1, 2, f_strchars},
915 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
916#ifdef HAVE_STRFTIME
917 {"strftime", 1, 2, f_strftime},
918#endif
919 {"strgetchar", 2, 2, f_strgetchar},
920 {"stridx", 2, 3, f_stridx},
921 {"string", 1, 1, f_string},
922 {"strlen", 1, 1, f_strlen},
923 {"strpart", 2, 3, f_strpart},
924 {"strridx", 2, 3, f_strridx},
925 {"strtrans", 1, 1, f_strtrans},
926 {"strwidth", 1, 1, f_strwidth},
927 {"submatch", 1, 2, f_submatch},
928 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200929 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200930 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200931 {"synID", 3, 3, f_synID},
932 {"synIDattr", 2, 3, f_synIDattr},
933 {"synIDtrans", 1, 1, f_synIDtrans},
934 {"synconcealed", 2, 2, f_synconcealed},
935 {"synstack", 2, 2, f_synstack},
936 {"system", 1, 2, f_system},
937 {"systemlist", 1, 2, f_systemlist},
938 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
939 {"tabpagenr", 0, 1, f_tabpagenr},
940 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
941 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100942 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200943#ifdef FEAT_FLOAT
944 {"tan", 1, 1, f_tan},
945 {"tanh", 1, 1, f_tanh},
946#endif
947 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200948#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100949 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
950 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100951 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200952 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200953# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
954 {"term_getansicolors", 1, 1, f_term_getansicolors},
955# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200956 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200957 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200958 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200959 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200960 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200961 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200962 {"term_getstatus", 1, 1, f_term_getstatus},
963 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200964 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200965 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200966 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200967 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200968# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
969 {"term_setansicolors", 2, 2, f_term_setansicolors},
970# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100971 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100972 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200973 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200974 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200975 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200976#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200977 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
978 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200979 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200980 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaaradc67142019-06-22 01:40:42 +0200981 {"test_garbagecollect_soon", 0, 0, f_test_garbagecollect_soon},
Bram Moolenaareda65222019-05-16 20:29:44 +0200982 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100983 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100984 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200985#ifdef FEAT_JOB_CHANNEL
986 {"test_null_channel", 0, 0, f_test_null_channel},
987#endif
988 {"test_null_dict", 0, 0, f_test_null_dict},
989#ifdef FEAT_JOB_CHANNEL
990 {"test_null_job", 0, 0, f_test_null_job},
991#endif
992 {"test_null_list", 0, 0, f_test_null_list},
993 {"test_null_partial", 0, 0, f_test_null_partial},
994 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200995 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100996 {"test_override", 2, 2, f_test_override},
997 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200998#ifdef FEAT_GUI
999 {"test_scrollbar", 3, 3, f_test_scrollbar},
1000#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001001#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001002 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001003#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001004 {"test_settime", 1, 1, f_test_settime},
1005#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001006 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001007 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001008 {"timer_start", 2, 3, f_timer_start},
1009 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001010 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001011#endif
1012 {"tolower", 1, 1, f_tolower},
1013 {"toupper", 1, 1, f_toupper},
1014 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001015 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001016#ifdef FEAT_FLOAT
1017 {"trunc", 1, 1, f_trunc},
1018#endif
1019 {"type", 1, 1, f_type},
1020 {"undofile", 1, 1, f_undofile},
1021 {"undotree", 0, 0, f_undotree},
1022 {"uniq", 1, 3, f_uniq},
1023 {"values", 1, 1, f_values},
1024 {"virtcol", 1, 1, f_virtcol},
1025 {"visualmode", 0, 1, f_visualmode},
1026 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001027 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001028 {"win_findbuf", 1, 1, f_win_findbuf},
1029 {"win_getid", 0, 2, f_win_getid},
1030 {"win_gotoid", 1, 1, f_win_gotoid},
1031 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1032 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001033 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001034 {"winbufnr", 1, 1, f_winbufnr},
1035 {"wincol", 0, 0, f_wincol},
1036 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001037 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001038 {"winline", 0, 0, f_winline},
1039 {"winnr", 0, 1, f_winnr},
1040 {"winrestcmd", 0, 0, f_winrestcmd},
1041 {"winrestview", 1, 1, f_winrestview},
1042 {"winsaveview", 0, 0, f_winsaveview},
1043 {"winwidth", 1, 1, f_winwidth},
1044 {"wordcount", 0, 0, f_wordcount},
1045 {"writefile", 2, 3, f_writefile},
1046 {"xor", 2, 2, f_xor},
1047};
1048
1049#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1050
1051/*
1052 * Function given to ExpandGeneric() to obtain the list of internal
1053 * or user defined function names.
1054 */
1055 char_u *
1056get_function_name(expand_T *xp, int idx)
1057{
1058 static int intidx = -1;
1059 char_u *name;
1060
1061 if (idx == 0)
1062 intidx = -1;
1063 if (intidx < 0)
1064 {
1065 name = get_user_func_name(xp, idx);
1066 if (name != NULL)
1067 return name;
1068 }
1069 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1070 {
1071 STRCPY(IObuff, functions[intidx].f_name);
1072 STRCAT(IObuff, "(");
1073 if (functions[intidx].f_max_argc == 0)
1074 STRCAT(IObuff, ")");
1075 return IObuff;
1076 }
1077
1078 return NULL;
1079}
1080
1081/*
1082 * Function given to ExpandGeneric() to obtain the list of internal or
1083 * user defined variable or function names.
1084 */
1085 char_u *
1086get_expr_name(expand_T *xp, int idx)
1087{
1088 static int intidx = -1;
1089 char_u *name;
1090
1091 if (idx == 0)
1092 intidx = -1;
1093 if (intidx < 0)
1094 {
1095 name = get_function_name(xp, idx);
1096 if (name != NULL)
1097 return name;
1098 }
1099 return get_user_var_name(xp, ++intidx);
1100}
1101
1102#endif /* FEAT_CMDL_COMPL */
1103
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001104/*
1105 * Find internal function in table above.
1106 * Return index, or -1 if not found
1107 */
1108 int
1109find_internal_func(
1110 char_u *name) /* name of the function */
1111{
1112 int first = 0;
1113 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1114 int cmp;
1115 int x;
1116
1117 /*
1118 * Find the function name in the table. Binary search.
1119 */
1120 while (first <= last)
1121 {
1122 x = first + ((unsigned)(last - first) >> 1);
1123 cmp = STRCMP(name, functions[x].f_name);
1124 if (cmp < 0)
1125 last = x - 1;
1126 else if (cmp > 0)
1127 first = x + 1;
1128 else
1129 return x;
1130 }
1131 return -1;
1132}
1133
1134 int
1135call_internal_func(
1136 char_u *name,
1137 int argcount,
1138 typval_T *argvars,
1139 typval_T *rettv)
1140{
1141 int i;
1142
1143 i = find_internal_func(name);
1144 if (i < 0)
1145 return ERROR_UNKNOWN;
1146 if (argcount < functions[i].f_min_argc)
1147 return ERROR_TOOFEW;
1148 if (argcount > functions[i].f_max_argc)
1149 return ERROR_TOOMANY;
1150 argvars[argcount].v_type = VAR_UNKNOWN;
1151 functions[i].f_func(argvars, rettv);
1152 return ERROR_NONE;
1153}
1154
1155/*
1156 * Return TRUE for a non-zero Number and a non-empty String.
1157 */
1158 static int
1159non_zero_arg(typval_T *argvars)
1160{
1161 return ((argvars[0].v_type == VAR_NUMBER
1162 && argvars[0].vval.v_number != 0)
1163 || (argvars[0].v_type == VAR_SPECIAL
1164 && argvars[0].vval.v_number == VVAL_TRUE)
1165 || (argvars[0].v_type == VAR_STRING
1166 && argvars[0].vval.v_string != NULL
1167 && *argvars[0].vval.v_string != NUL));
1168}
1169
1170/*
1171 * Get the lnum from the first argument.
1172 * Also accepts ".", "$", etc., but that only works for the current buffer.
1173 * Returns -1 on error.
1174 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001175 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001176tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001177{
1178 typval_T rettv;
1179 linenr_T lnum;
1180
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001181 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001182 if (lnum == 0) /* no valid number, try using line() */
1183 {
1184 rettv.v_type = VAR_NUMBER;
1185 f_line(argvars, &rettv);
1186 lnum = (linenr_T)rettv.vval.v_number;
1187 clear_tv(&rettv);
1188 }
1189 return lnum;
1190}
1191
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001192/*
1193 * Get the lnum from the first argument.
1194 * Also accepts "$", then "buf" is used.
1195 * Returns 0 on error.
1196 */
1197 static linenr_T
1198tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1199{
1200 if (argvars[0].v_type == VAR_STRING
1201 && argvars[0].vval.v_string != NULL
1202 && argvars[0].vval.v_string[0] == '$'
1203 && buf != NULL)
1204 return buf->b_ml.ml_line_count;
1205 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1206}
1207
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001208#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001209/*
1210 * Get the float value of "argvars[0]" into "f".
1211 * Returns FAIL when the argument is not a Number or Float.
1212 */
1213 static int
1214get_float_arg(typval_T *argvars, float_T *f)
1215{
1216 if (argvars[0].v_type == VAR_FLOAT)
1217 {
1218 *f = argvars[0].vval.v_float;
1219 return OK;
1220 }
1221 if (argvars[0].v_type == VAR_NUMBER)
1222 {
1223 *f = (float_T)argvars[0].vval.v_number;
1224 return OK;
1225 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001226 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001227 return FAIL;
1228}
1229
1230/*
1231 * "abs(expr)" function
1232 */
1233 static void
1234f_abs(typval_T *argvars, typval_T *rettv)
1235{
1236 if (argvars[0].v_type == VAR_FLOAT)
1237 {
1238 rettv->v_type = VAR_FLOAT;
1239 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1240 }
1241 else
1242 {
1243 varnumber_T n;
1244 int error = FALSE;
1245
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001246 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001247 if (error)
1248 rettv->vval.v_number = -1;
1249 else if (n > 0)
1250 rettv->vval.v_number = n;
1251 else
1252 rettv->vval.v_number = -n;
1253 }
1254}
1255
1256/*
1257 * "acos()" function
1258 */
1259 static void
1260f_acos(typval_T *argvars, typval_T *rettv)
1261{
1262 float_T f = 0.0;
1263
1264 rettv->v_type = VAR_FLOAT;
1265 if (get_float_arg(argvars, &f) == OK)
1266 rettv->vval.v_float = acos(f);
1267 else
1268 rettv->vval.v_float = 0.0;
1269}
1270#endif
1271
1272/*
1273 * "add(list, item)" function
1274 */
1275 static void
1276f_add(typval_T *argvars, typval_T *rettv)
1277{
1278 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001279 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001280
1281 rettv->vval.v_number = 1; /* Default: Failed */
1282 if (argvars[0].v_type == VAR_LIST)
1283 {
1284 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001285 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001286 (char_u *)N_("add() argument"), TRUE)
1287 && list_append_tv(l, &argvars[1]) == OK)
1288 copy_tv(&argvars[0], rettv);
1289 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001290 else if (argvars[0].v_type == VAR_BLOB)
1291 {
1292 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001293 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294 (char_u *)N_("add() argument"), TRUE))
1295 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001296 int error = FALSE;
1297 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1298
1299 if (!error)
1300 {
1301 ga_append(&b->bv_ga, (int)n);
1302 copy_tv(&argvars[0], rettv);
1303 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001304 }
1305 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001306 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001307 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001308}
1309
1310/*
1311 * "and(expr, expr)" function
1312 */
1313 static void
1314f_and(typval_T *argvars, typval_T *rettv)
1315{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001316 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1317 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001318}
1319
1320/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001321 * If there is a window for "curbuf", make it the current window.
1322 */
1323 static void
1324find_win_for_curbuf(void)
1325{
1326 wininfo_T *wip;
1327
1328 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1329 {
1330 if (wip->wi_win != NULL)
1331 {
1332 curwin = wip->wi_win;
1333 break;
1334 }
1335 }
1336}
1337
1338/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001339 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001340 */
1341 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001342set_buffer_lines(
1343 buf_T *buf,
1344 linenr_T lnum_arg,
1345 int append,
1346 typval_T *lines,
1347 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001348{
Bram Moolenaarca851592018-06-06 21:04:07 +02001349 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1350 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001351 list_T *l = NULL;
1352 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001354 linenr_T append_lnum;
1355 buf_T *curbuf_save = NULL;
1356 win_T *curwin_save = NULL;
1357 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358
Bram Moolenaarca851592018-06-06 21:04:07 +02001359 /* When using the current buffer ml_mfp will be set if needed. Useful when
1360 * setline() is used on startup. For other buffers the buffer must be
1361 * loaded. */
1362 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001363 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001364 rettv->vval.v_number = 1; /* FAIL */
1365 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001366 }
1367
Bram Moolenaarca851592018-06-06 21:04:07 +02001368 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001369 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001370 curbuf_save = curbuf;
1371 curwin_save = curwin;
1372 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001373 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001374 }
1375
1376 if (append)
1377 // appendbufline() uses the line number below which we insert
1378 append_lnum = lnum - 1;
1379 else
1380 // setbufline() uses the line number above which we insert, we only
1381 // append if it's below the last line
1382 append_lnum = curbuf->b_ml.ml_line_count;
1383
1384 if (lines->v_type == VAR_LIST)
1385 {
1386 l = lines->vval.v_list;
1387 li = l->lv_first;
1388 }
1389 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001390 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001391
1392 /* default result is zero == OK */
1393 for (;;)
1394 {
1395 if (l != NULL)
1396 {
1397 /* list argument, get next string */
1398 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001399 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001400 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001401 li = li->li_next;
1402 }
1403
Bram Moolenaarca851592018-06-06 21:04:07 +02001404 rettv->vval.v_number = 1; /* FAIL */
1405 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1406 break;
1407
1408 /* When coming here from Insert mode, sync undo, so that this can be
1409 * undone separately from what was previously inserted. */
1410 if (u_sync_once == 2)
1411 {
1412 u_sync_once = 1; /* notify that u_sync() was called */
1413 u_sync(TRUE);
1414 }
1415
1416 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1417 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001418 // Existing line, replace it.
1419 // Removes any existing text properties.
1420 if (u_savesub(lnum) == OK && ml_replace_len(
1421 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001422 {
1423 changed_bytes(lnum, 0);
1424 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1425 check_cursor_col();
1426 rettv->vval.v_number = 0; /* OK */
1427 }
1428 }
1429 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1430 {
1431 /* append the line */
1432 ++added;
1433 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1434 rettv->vval.v_number = 0; /* OK */
1435 }
1436
1437 if (l == NULL) /* only one string argument */
1438 break;
1439 ++lnum;
1440 }
1441
1442 if (added > 0)
1443 {
1444 win_T *wp;
1445 tabpage_T *tp;
1446
1447 appended_lines_mark(append_lnum, added);
1448 FOR_ALL_TAB_WINDOWS(tp, wp)
1449 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1450 wp->w_cursor.lnum += added;
1451 check_cursor_col();
1452
Bram Moolenaarf2732452018-06-03 14:47:35 +02001453#ifdef FEAT_JOB_CHANNEL
1454 if (bt_prompt(curbuf) && (State & INSERT))
1455 // show the line with the prompt
1456 update_topline();
1457#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001458 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001459
1460 if (!is_curbuf)
1461 {
1462 curbuf = curbuf_save;
1463 curwin = curwin_save;
1464 }
1465}
1466
1467/*
1468 * "append(lnum, string/list)" function
1469 */
1470 static void
1471f_append(typval_T *argvars, typval_T *rettv)
1472{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001473 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001474
1475 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1476}
1477
1478/*
1479 * "appendbufline(buf, lnum, string/list)" function
1480 */
1481 static void
1482f_appendbufline(typval_T *argvars, typval_T *rettv)
1483{
1484 linenr_T lnum;
1485 buf_T *buf;
1486
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001487 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001488 if (buf == NULL)
1489 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001490 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001491 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001492 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001493 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1494 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001495}
1496
1497/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001498 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001499 */
1500 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001501f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001502{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001503 win_T *wp;
1504
1505 if (argvars[0].v_type == VAR_UNKNOWN)
1506 // use the current window
1507 rettv->vval.v_number = ARGCOUNT;
1508 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001509 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001510 // use the global argument list
1511 rettv->vval.v_number = GARGCOUNT;
1512 else
1513 {
1514 // use the argument list of the specified window
1515 wp = find_win_by_nr_or_id(&argvars[0]);
1516 if (wp != NULL)
1517 rettv->vval.v_number = WARGCOUNT(wp);
1518 else
1519 rettv->vval.v_number = -1;
1520 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001521}
1522
1523/*
1524 * "argidx()" function
1525 */
1526 static void
1527f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1528{
1529 rettv->vval.v_number = curwin->w_arg_idx;
1530}
1531
1532/*
1533 * "arglistid()" function
1534 */
1535 static void
1536f_arglistid(typval_T *argvars, typval_T *rettv)
1537{
1538 win_T *wp;
1539
1540 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001541 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001542 if (wp != NULL)
1543 rettv->vval.v_number = wp->w_alist->id;
1544}
1545
1546/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001547 * Get the argument list for a given window
1548 */
1549 static void
1550get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1551{
1552 int idx;
1553
1554 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1555 for (idx = 0; idx < argcount; ++idx)
1556 list_append_string(rettv->vval.v_list,
1557 alist_name(&arglist[idx]), -1);
1558}
1559
1560/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561 * "argv(nr)" function
1562 */
1563 static void
1564f_argv(typval_T *argvars, typval_T *rettv)
1565{
1566 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001567 aentry_T *arglist = NULL;
1568 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001569
1570 if (argvars[0].v_type != VAR_UNKNOWN)
1571 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001572 if (argvars[1].v_type == VAR_UNKNOWN)
1573 {
1574 arglist = ARGLIST;
1575 argcount = ARGCOUNT;
1576 }
1577 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001578 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001579 {
1580 arglist = GARGLIST;
1581 argcount = GARGCOUNT;
1582 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001584 {
1585 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1586
1587 if (wp != NULL)
1588 {
1589 /* Use the argument list of the specified window */
1590 arglist = WARGLIST(wp);
1591 argcount = WARGCOUNT(wp);
1592 }
1593 }
1594
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001595 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001596 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001597 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001598 if (arglist != NULL && idx >= 0 && idx < argcount)
1599 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1600 else if (idx == -1)
1601 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001602 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001603 else
1604 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605}
1606
1607/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001608 * "assert_beeps(cmd [, error])" function
1609 */
1610 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001611f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001612{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001613 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001614}
1615
1616/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001617 * "assert_equal(expected, actual[, msg])" function
1618 */
1619 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001620f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001621{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001622 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001623}
1624
1625/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001626 * "assert_equalfile(fname-one, fname-two)" function
1627 */
1628 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001629f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001630{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001631 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001632}
1633
1634/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001635 * "assert_notequal(expected, actual[, msg])" function
1636 */
1637 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001638f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001639{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001640 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641}
1642
1643/*
1644 * "assert_exception(string[, msg])" function
1645 */
1646 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001647f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001648{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001649 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001650}
1651
1652/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001653 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001654 */
1655 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001656f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001658 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659}
1660
1661/*
1662 * "assert_false(actual[, msg])" function
1663 */
1664 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001665f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001666{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001667 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001668}
1669
1670/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001671 * "assert_inrange(lower, upper[, msg])" function
1672 */
1673 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001674f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001675{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001676 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001677}
1678
1679/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001680 * "assert_match(pattern, actual[, msg])" function
1681 */
1682 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001683f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001684{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001685 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001686}
1687
1688/*
1689 * "assert_notmatch(pattern, actual[, msg])" function
1690 */
1691 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001692f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001693{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001694 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001695}
1696
1697/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001698 * "assert_report(msg)" function
1699 */
1700 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001701f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001702{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001703 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001704}
1705
1706/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001707 * "assert_true(actual[, msg])" function
1708 */
1709 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001710f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001711{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001712 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001713}
1714
1715#ifdef FEAT_FLOAT
1716/*
1717 * "asin()" function
1718 */
1719 static void
1720f_asin(typval_T *argvars, typval_T *rettv)
1721{
1722 float_T f = 0.0;
1723
1724 rettv->v_type = VAR_FLOAT;
1725 if (get_float_arg(argvars, &f) == OK)
1726 rettv->vval.v_float = asin(f);
1727 else
1728 rettv->vval.v_float = 0.0;
1729}
1730
1731/*
1732 * "atan()" function
1733 */
1734 static void
1735f_atan(typval_T *argvars, typval_T *rettv)
1736{
1737 float_T f = 0.0;
1738
1739 rettv->v_type = VAR_FLOAT;
1740 if (get_float_arg(argvars, &f) == OK)
1741 rettv->vval.v_float = atan(f);
1742 else
1743 rettv->vval.v_float = 0.0;
1744}
1745
1746/*
1747 * "atan2()" function
1748 */
1749 static void
1750f_atan2(typval_T *argvars, typval_T *rettv)
1751{
1752 float_T fx = 0.0, fy = 0.0;
1753
1754 rettv->v_type = VAR_FLOAT;
1755 if (get_float_arg(argvars, &fx) == OK
1756 && get_float_arg(&argvars[1], &fy) == OK)
1757 rettv->vval.v_float = atan2(fx, fy);
1758 else
1759 rettv->vval.v_float = 0.0;
1760}
1761#endif
1762
1763/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001764 * "balloon_show()" function
1765 */
1766#ifdef FEAT_BEVAL
1767 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001768f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1769{
1770 rettv->v_type = VAR_STRING;
1771 if (balloonEval != NULL)
1772 {
1773 if (balloonEval->msg == NULL)
1774 rettv->vval.v_string = NULL;
1775 else
1776 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1777 }
1778}
1779
1780 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001781f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1782{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001783 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001784 {
1785 if (argvars[0].v_type == VAR_LIST
1786# ifdef FEAT_GUI
1787 && !gui.in_use
1788# endif
1789 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001790 {
1791 list_T *l = argvars[0].vval.v_list;
1792
1793 // empty list removes the balloon
1794 post_balloon(balloonEval, NULL,
1795 l == NULL || l->lv_len == 0 ? NULL : l);
1796 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001797 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001798 {
1799 char_u *mesg = tv_get_string_chk(&argvars[0]);
1800
1801 if (mesg != NULL)
1802 // empty string removes the balloon
1803 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1804 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001805 }
1806}
1807
Bram Moolenaar669a8282017-11-19 20:13:05 +01001808# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001809 static void
1810f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1811{
1812 if (rettv_list_alloc(rettv) == OK)
1813 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001814 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001815
1816 if (msg != NULL)
1817 {
1818 pumitem_T *array;
1819 int size = split_message(msg, &array);
1820 int i;
1821
1822 /* Skip the first and last item, they are always empty. */
1823 for (i = 1; i < size - 1; ++i)
1824 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001825 while (size > 0)
1826 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001827 vim_free(array);
1828 }
1829 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001830}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001831# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001832#endif
1833
1834/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001835 * "browse(save, title, initdir, default)" function
1836 */
1837 static void
1838f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1839{
1840#ifdef FEAT_BROWSE
1841 int save;
1842 char_u *title;
1843 char_u *initdir;
1844 char_u *defname;
1845 char_u buf[NUMBUFLEN];
1846 char_u buf2[NUMBUFLEN];
1847 int error = FALSE;
1848
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001849 save = (int)tv_get_number_chk(&argvars[0], &error);
1850 title = tv_get_string_chk(&argvars[1]);
1851 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1852 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853
1854 if (error || title == NULL || initdir == NULL || defname == NULL)
1855 rettv->vval.v_string = NULL;
1856 else
1857 rettv->vval.v_string =
1858 do_browse(save ? BROWSE_SAVE : 0,
1859 title, defname, NULL, initdir, NULL, curbuf);
1860#else
1861 rettv->vval.v_string = NULL;
1862#endif
1863 rettv->v_type = VAR_STRING;
1864}
1865
1866/*
1867 * "browsedir(title, initdir)" function
1868 */
1869 static void
1870f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1871{
1872#ifdef FEAT_BROWSE
1873 char_u *title;
1874 char_u *initdir;
1875 char_u buf[NUMBUFLEN];
1876
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001877 title = tv_get_string_chk(&argvars[0]);
1878 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001879
1880 if (title == NULL || initdir == NULL)
1881 rettv->vval.v_string = NULL;
1882 else
1883 rettv->vval.v_string = do_browse(BROWSE_DIR,
1884 title, NULL, NULL, initdir, NULL, curbuf);
1885#else
1886 rettv->vval.v_string = NULL;
1887#endif
1888 rettv->v_type = VAR_STRING;
1889}
1890
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001891/*
1892 * Find a buffer by number or exact name.
1893 */
1894 static buf_T *
1895find_buffer(typval_T *avar)
1896{
1897 buf_T *buf = NULL;
1898
1899 if (avar->v_type == VAR_NUMBER)
1900 buf = buflist_findnr((int)avar->vval.v_number);
1901 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1902 {
1903 buf = buflist_findname_exp(avar->vval.v_string);
1904 if (buf == NULL)
1905 {
1906 /* No full path name match, try a match with a URL or a "nofile"
1907 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001908 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001909 if (buf->b_fname != NULL
1910 && (path_with_url(buf->b_fname)
1911#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001912 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913#endif
1914 )
1915 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1916 break;
1917 }
1918 }
1919 return buf;
1920}
1921
1922/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001923 * "bufadd(expr)" function
1924 */
1925 static void
1926f_bufadd(typval_T *argvars, typval_T *rettv)
1927{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001928 char_u *name = tv_get_string(&argvars[0]);
1929
1930 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001931}
1932
1933/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001934 * "bufexists(expr)" function
1935 */
1936 static void
1937f_bufexists(typval_T *argvars, typval_T *rettv)
1938{
1939 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1940}
1941
1942/*
1943 * "buflisted(expr)" function
1944 */
1945 static void
1946f_buflisted(typval_T *argvars, typval_T *rettv)
1947{
1948 buf_T *buf;
1949
1950 buf = find_buffer(&argvars[0]);
1951 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1952}
1953
1954/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001955 * "bufload(expr)" function
1956 */
1957 static void
1958f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1959{
1960 buf_T *buf = get_buf_arg(&argvars[0]);
1961
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001962 if (buf != NULL)
1963 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001964}
1965
1966/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001967 * "bufloaded(expr)" function
1968 */
1969 static void
1970f_bufloaded(typval_T *argvars, typval_T *rettv)
1971{
1972 buf_T *buf;
1973
1974 buf = find_buffer(&argvars[0]);
1975 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1976}
1977
1978 buf_T *
1979buflist_find_by_name(char_u *name, int curtab_only)
1980{
1981 int save_magic;
1982 char_u *save_cpo;
1983 buf_T *buf;
1984
1985 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1986 save_magic = p_magic;
1987 p_magic = TRUE;
1988 save_cpo = p_cpo;
1989 p_cpo = (char_u *)"";
1990
1991 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1992 TRUE, FALSE, curtab_only));
1993
1994 p_magic = save_magic;
1995 p_cpo = save_cpo;
1996 return buf;
1997}
1998
1999/*
2000 * Get buffer by number or pattern.
2001 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002002 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002003tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002004{
2005 char_u *name = tv->vval.v_string;
2006 buf_T *buf;
2007
2008 if (tv->v_type == VAR_NUMBER)
2009 return buflist_findnr((int)tv->vval.v_number);
2010 if (tv->v_type != VAR_STRING)
2011 return NULL;
2012 if (name == NULL || *name == NUL)
2013 return curbuf;
2014 if (name[0] == '$' && name[1] == NUL)
2015 return lastbuf;
2016
2017 buf = buflist_find_by_name(name, curtab_only);
2018
2019 /* If not found, try expanding the name, like done for bufexists(). */
2020 if (buf == NULL)
2021 buf = find_buffer(tv);
2022
2023 return buf;
2024}
2025
2026/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002027 * Get the buffer from "arg" and give an error and return NULL if it is not
2028 * valid.
2029 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002030 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002031get_buf_arg(typval_T *arg)
2032{
2033 buf_T *buf;
2034
2035 ++emsg_off;
2036 buf = tv_get_buf(arg, FALSE);
2037 --emsg_off;
2038 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002039 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002040 return buf;
2041}
2042
2043/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002044 * "bufname(expr)" function
2045 */
2046 static void
2047f_bufname(typval_T *argvars, typval_T *rettv)
2048{
2049 buf_T *buf;
2050
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002051 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002052 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002053 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002054 rettv->v_type = VAR_STRING;
2055 if (buf != NULL && buf->b_fname != NULL)
2056 rettv->vval.v_string = vim_strsave(buf->b_fname);
2057 else
2058 rettv->vval.v_string = NULL;
2059 --emsg_off;
2060}
2061
2062/*
2063 * "bufnr(expr)" function
2064 */
2065 static void
2066f_bufnr(typval_T *argvars, typval_T *rettv)
2067{
2068 buf_T *buf;
2069 int error = FALSE;
2070 char_u *name;
2071
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002072 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002073 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002074 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002075 --emsg_off;
2076
2077 /* If the buffer isn't found and the second argument is not zero create a
2078 * new buffer. */
2079 if (buf == NULL
2080 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002081 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002082 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002083 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002084 && !error)
2085 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2086
2087 if (buf != NULL)
2088 rettv->vval.v_number = buf->b_fnum;
2089 else
2090 rettv->vval.v_number = -1;
2091}
2092
2093 static void
2094buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2095{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002096 win_T *wp;
2097 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002098 buf_T *buf;
2099
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002100 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002101 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002102 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002103 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002104 {
2105 ++winnr;
2106 if (wp->w_buffer == buf)
2107 break;
2108 }
2109 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002110 --emsg_off;
2111}
2112
2113/*
2114 * "bufwinid(nr)" function
2115 */
2116 static void
2117f_bufwinid(typval_T *argvars, typval_T *rettv)
2118{
2119 buf_win_common(argvars, rettv, FALSE);
2120}
2121
2122/*
2123 * "bufwinnr(nr)" function
2124 */
2125 static void
2126f_bufwinnr(typval_T *argvars, typval_T *rettv)
2127{
2128 buf_win_common(argvars, rettv, TRUE);
2129}
2130
2131/*
2132 * "byte2line(byte)" function
2133 */
2134 static void
2135f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2136{
2137#ifndef FEAT_BYTEOFF
2138 rettv->vval.v_number = -1;
2139#else
2140 long boff = 0;
2141
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002142 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002143 if (boff < 0)
2144 rettv->vval.v_number = -1;
2145 else
2146 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2147 (linenr_T)0, &boff);
2148#endif
2149}
2150
2151 static void
2152byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2153{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002155 char_u *str;
2156 varnumber_T idx;
2157
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002158 str = tv_get_string_chk(&argvars[0]);
2159 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002160 rettv->vval.v_number = -1;
2161 if (str == NULL || idx < 0)
2162 return;
2163
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164 t = str;
2165 for ( ; idx > 0; idx--)
2166 {
2167 if (*t == NUL) /* EOL reached */
2168 return;
2169 if (enc_utf8 && comp)
2170 t += utf_ptr2len(t);
2171 else
2172 t += (*mb_ptr2len)(t);
2173 }
2174 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002175}
2176
2177/*
2178 * "byteidx()" function
2179 */
2180 static void
2181f_byteidx(typval_T *argvars, typval_T *rettv)
2182{
2183 byteidx(argvars, rettv, FALSE);
2184}
2185
2186/*
2187 * "byteidxcomp()" function
2188 */
2189 static void
2190f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2191{
2192 byteidx(argvars, rettv, TRUE);
2193}
2194
2195/*
2196 * "call(func, arglist [, dict])" function
2197 */
2198 static void
2199f_call(typval_T *argvars, typval_T *rettv)
2200{
2201 char_u *func;
2202 partial_T *partial = NULL;
2203 dict_T *selfdict = NULL;
2204
2205 if (argvars[1].v_type != VAR_LIST)
2206 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002207 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002208 return;
2209 }
2210 if (argvars[1].vval.v_list == NULL)
2211 return;
2212
2213 if (argvars[0].v_type == VAR_FUNC)
2214 func = argvars[0].vval.v_string;
2215 else if (argvars[0].v_type == VAR_PARTIAL)
2216 {
2217 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002218 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002219 }
2220 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002221 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002222 if (*func == NUL)
2223 return; /* type error or empty name */
2224
2225 if (argvars[2].v_type != VAR_UNKNOWN)
2226 {
2227 if (argvars[2].v_type != VAR_DICT)
2228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002229 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002230 return;
2231 }
2232 selfdict = argvars[2].vval.v_dict;
2233 }
2234
2235 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2236}
2237
2238#ifdef FEAT_FLOAT
2239/*
2240 * "ceil({float})" function
2241 */
2242 static void
2243f_ceil(typval_T *argvars, typval_T *rettv)
2244{
2245 float_T f = 0.0;
2246
2247 rettv->v_type = VAR_FLOAT;
2248 if (get_float_arg(argvars, &f) == OK)
2249 rettv->vval.v_float = ceil(f);
2250 else
2251 rettv->vval.v_float = 0.0;
2252}
2253#endif
2254
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002255/*
2256 * "changenr()" function
2257 */
2258 static void
2259f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2260{
2261 rettv->vval.v_number = curbuf->b_u_seq_cur;
2262}
2263
2264/*
2265 * "char2nr(string)" function
2266 */
2267 static void
2268f_char2nr(typval_T *argvars, typval_T *rettv)
2269{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002270 if (has_mbyte)
2271 {
2272 int utf8 = 0;
2273
2274 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002275 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002276
2277 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002278 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002279 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002280 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002281 }
2282 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002283 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002284}
2285
2286/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002287 * "chdir(dir)" function
2288 */
2289 static void
2290f_chdir(typval_T *argvars, typval_T *rettv)
2291{
2292 char_u *cwd;
2293 cdscope_T scope = CDSCOPE_GLOBAL;
2294
2295 rettv->v_type = VAR_STRING;
2296 rettv->vval.v_string = NULL;
2297
2298 if (argvars[0].v_type != VAR_STRING)
2299 return;
2300
2301 // Return the current directory
2302 cwd = alloc(MAXPATHL);
2303 if (cwd != NULL)
2304 {
2305 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2306 {
2307#ifdef BACKSLASH_IN_FILENAME
2308 slash_adjust(cwd);
2309#endif
2310 rettv->vval.v_string = vim_strsave(cwd);
2311 }
2312 vim_free(cwd);
2313 }
2314
2315 if (curwin->w_localdir != NULL)
2316 scope = CDSCOPE_WINDOW;
2317 else if (curtab->tp_localdir != NULL)
2318 scope = CDSCOPE_TABPAGE;
2319
2320 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2321 // Directory change failed
2322 VIM_CLEAR(rettv->vval.v_string);
2323}
2324
2325/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002326 * "cindent(lnum)" function
2327 */
2328 static void
2329f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2330{
2331#ifdef FEAT_CINDENT
2332 pos_T pos;
2333 linenr_T lnum;
2334
2335 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002336 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002337 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2338 {
2339 curwin->w_cursor.lnum = lnum;
2340 rettv->vval.v_number = get_c_indent();
2341 curwin->w_cursor = pos;
2342 }
2343 else
2344#endif
2345 rettv->vval.v_number = -1;
2346}
2347
Bram Moolenaaraff74912019-03-30 18:11:49 +01002348 static win_T *
2349get_optional_window(typval_T *argvars, int idx)
2350{
2351 win_T *win = curwin;
2352
2353 if (argvars[idx].v_type != VAR_UNKNOWN)
2354 {
2355 win = find_win_by_nr_or_id(&argvars[idx]);
2356 if (win == NULL)
2357 {
2358 emsg(_(e_invalwindow));
2359 return NULL;
2360 }
2361 }
2362 return win;
2363}
2364
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002365/*
2366 * "clearmatches()" function
2367 */
2368 static void
2369f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2370{
2371#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002372 win_T *win = get_optional_window(argvars, 0);
2373
2374 if (win != NULL)
2375 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002376#endif
2377}
2378
2379/*
2380 * "col(string)" function
2381 */
2382 static void
2383f_col(typval_T *argvars, typval_T *rettv)
2384{
2385 colnr_T col = 0;
2386 pos_T *fp;
2387 int fnum = curbuf->b_fnum;
2388
2389 fp = var2fpos(&argvars[0], FALSE, &fnum);
2390 if (fp != NULL && fnum == curbuf->b_fnum)
2391 {
2392 if (fp->col == MAXCOL)
2393 {
2394 /* '> can be MAXCOL, get the length of the line then */
2395 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2396 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2397 else
2398 col = MAXCOL;
2399 }
2400 else
2401 {
2402 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002403 /* col(".") when the cursor is on the NUL at the end of the line
2404 * because of "coladd" can be seen as an extra column. */
2405 if (virtual_active() && fp == &curwin->w_cursor)
2406 {
2407 char_u *p = ml_get_cursor();
2408
2409 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2410 curwin->w_virtcol - curwin->w_cursor.coladd))
2411 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412 int l;
2413
2414 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2415 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002416 }
2417 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002418 }
2419 }
2420 rettv->vval.v_number = col;
2421}
2422
2423#if defined(FEAT_INS_EXPAND)
2424/*
2425 * "complete()" function
2426 */
2427 static void
2428f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2429{
2430 int startcol;
2431
2432 if ((State & INSERT) == 0)
2433 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002434 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002435 return;
2436 }
2437
2438 /* Check for undo allowed here, because if something was already inserted
2439 * the line was already saved for undo and this check isn't done. */
2440 if (!undo_allowed())
2441 return;
2442
2443 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2444 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002445 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002446 return;
2447 }
2448
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002449 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002450 if (startcol <= 0)
2451 return;
2452
2453 set_completion(startcol - 1, argvars[1].vval.v_list);
2454}
2455
2456/*
2457 * "complete_add()" function
2458 */
2459 static void
2460f_complete_add(typval_T *argvars, typval_T *rettv)
2461{
2462 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2463}
2464
2465/*
2466 * "complete_check()" function
2467 */
2468 static void
2469f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2470{
2471 int saved = RedrawingDisabled;
2472
2473 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002474 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002475 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002476 RedrawingDisabled = saved;
2477}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002478
2479/*
2480 * "complete_info()" function
2481 */
2482 static void
2483f_complete_info(typval_T *argvars, typval_T *rettv)
2484{
2485 list_T *what_list = NULL;
2486
2487 if (rettv_dict_alloc(rettv) != OK)
2488 return;
2489
2490 if (argvars[0].v_type != VAR_UNKNOWN)
2491 {
2492 if (argvars[0].v_type != VAR_LIST)
2493 {
2494 emsg(_(e_listreq));
2495 return;
2496 }
2497 what_list = argvars[0].vval.v_list;
2498 }
2499 get_complete_info(what_list, rettv->vval.v_dict);
2500}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002501#endif
2502
2503/*
2504 * "confirm(message, buttons[, default [, type]])" function
2505 */
2506 static void
2507f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2508{
2509#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2510 char_u *message;
2511 char_u *buttons = NULL;
2512 char_u buf[NUMBUFLEN];
2513 char_u buf2[NUMBUFLEN];
2514 int def = 1;
2515 int type = VIM_GENERIC;
2516 char_u *typestr;
2517 int error = FALSE;
2518
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002519 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002520 if (message == NULL)
2521 error = TRUE;
2522 if (argvars[1].v_type != VAR_UNKNOWN)
2523 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002524 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002525 if (buttons == NULL)
2526 error = TRUE;
2527 if (argvars[2].v_type != VAR_UNKNOWN)
2528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002529 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002530 if (argvars[3].v_type != VAR_UNKNOWN)
2531 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002532 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002533 if (typestr == NULL)
2534 error = TRUE;
2535 else
2536 {
2537 switch (TOUPPER_ASC(*typestr))
2538 {
2539 case 'E': type = VIM_ERROR; break;
2540 case 'Q': type = VIM_QUESTION; break;
2541 case 'I': type = VIM_INFO; break;
2542 case 'W': type = VIM_WARNING; break;
2543 case 'G': type = VIM_GENERIC; break;
2544 }
2545 }
2546 }
2547 }
2548 }
2549
2550 if (buttons == NULL || *buttons == NUL)
2551 buttons = (char_u *)_("&Ok");
2552
2553 if (!error)
2554 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2555 def, NULL, FALSE);
2556#endif
2557}
2558
2559/*
2560 * "copy()" function
2561 */
2562 static void
2563f_copy(typval_T *argvars, typval_T *rettv)
2564{
2565 item_copy(&argvars[0], rettv, FALSE, 0);
2566}
2567
2568#ifdef FEAT_FLOAT
2569/*
2570 * "cos()" function
2571 */
2572 static void
2573f_cos(typval_T *argvars, typval_T *rettv)
2574{
2575 float_T f = 0.0;
2576
2577 rettv->v_type = VAR_FLOAT;
2578 if (get_float_arg(argvars, &f) == OK)
2579 rettv->vval.v_float = cos(f);
2580 else
2581 rettv->vval.v_float = 0.0;
2582}
2583
2584/*
2585 * "cosh()" function
2586 */
2587 static void
2588f_cosh(typval_T *argvars, typval_T *rettv)
2589{
2590 float_T f = 0.0;
2591
2592 rettv->v_type = VAR_FLOAT;
2593 if (get_float_arg(argvars, &f) == OK)
2594 rettv->vval.v_float = cosh(f);
2595 else
2596 rettv->vval.v_float = 0.0;
2597}
2598#endif
2599
2600/*
2601 * "count()" function
2602 */
2603 static void
2604f_count(typval_T *argvars, typval_T *rettv)
2605{
2606 long n = 0;
2607 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002608 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002609
Bram Moolenaar9966b212017-07-28 16:46:57 +02002610 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002611 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002612
2613 if (argvars[0].v_type == VAR_STRING)
2614 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002615 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002616 char_u *p = argvars[0].vval.v_string;
2617 char_u *next;
2618
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002619 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002620 {
2621 if (ic)
2622 {
2623 size_t len = STRLEN(expr);
2624
2625 while (*p != NUL)
2626 {
2627 if (MB_STRNICMP(p, expr, len) == 0)
2628 {
2629 ++n;
2630 p += len;
2631 }
2632 else
2633 MB_PTR_ADV(p);
2634 }
2635 }
2636 else
2637 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2638 != NULL)
2639 {
2640 ++n;
2641 p = next + STRLEN(expr);
2642 }
2643 }
2644
2645 }
2646 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002647 {
2648 listitem_T *li;
2649 list_T *l;
2650 long idx;
2651
2652 if ((l = argvars[0].vval.v_list) != NULL)
2653 {
2654 li = l->lv_first;
2655 if (argvars[2].v_type != VAR_UNKNOWN)
2656 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002657 if (argvars[3].v_type != VAR_UNKNOWN)
2658 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002659 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660 if (!error)
2661 {
2662 li = list_find(l, idx);
2663 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002664 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002665 }
2666 }
2667 if (error)
2668 li = NULL;
2669 }
2670
2671 for ( ; li != NULL; li = li->li_next)
2672 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2673 ++n;
2674 }
2675 }
2676 else if (argvars[0].v_type == VAR_DICT)
2677 {
2678 int todo;
2679 dict_T *d;
2680 hashitem_T *hi;
2681
2682 if ((d = argvars[0].vval.v_dict) != NULL)
2683 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002684 if (argvars[2].v_type != VAR_UNKNOWN)
2685 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002686 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002687 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 }
2689
2690 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2691 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2692 {
2693 if (!HASHITEM_EMPTY(hi))
2694 {
2695 --todo;
2696 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2697 ++n;
2698 }
2699 }
2700 }
2701 }
2702 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002703 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002704 rettv->vval.v_number = n;
2705}
2706
2707/*
2708 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2709 *
2710 * Checks the existence of a cscope connection.
2711 */
2712 static void
2713f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2714{
2715#ifdef FEAT_CSCOPE
2716 int num = 0;
2717 char_u *dbpath = NULL;
2718 char_u *prepend = NULL;
2719 char_u buf[NUMBUFLEN];
2720
2721 if (argvars[0].v_type != VAR_UNKNOWN
2722 && argvars[1].v_type != VAR_UNKNOWN)
2723 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002724 num = (int)tv_get_number(&argvars[0]);
2725 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002726 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002727 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002728 }
2729
2730 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2731#endif
2732}
2733
2734/*
2735 * "cursor(lnum, col)" function, or
2736 * "cursor(list)"
2737 *
2738 * Moves the cursor to the specified line and column.
2739 * Returns 0 when the position could be set, -1 otherwise.
2740 */
2741 static void
2742f_cursor(typval_T *argvars, typval_T *rettv)
2743{
2744 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002745 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002746 int set_curswant = TRUE;
2747
2748 rettv->vval.v_number = -1;
2749 if (argvars[1].v_type == VAR_UNKNOWN)
2750 {
2751 pos_T pos;
2752 colnr_T curswant = -1;
2753
2754 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002756 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002757 return;
2758 }
2759 line = pos.lnum;
2760 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002761 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002762 if (curswant >= 0)
2763 {
2764 curwin->w_curswant = curswant - 1;
2765 set_curswant = FALSE;
2766 }
2767 }
2768 else
2769 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002770 line = tv_get_lnum(argvars);
2771 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002772 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002773 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002774 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002775 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002776 return; /* type error; errmsg already given */
2777 if (line > 0)
2778 curwin->w_cursor.lnum = line;
2779 if (col > 0)
2780 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002781 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782
2783 /* Make sure the cursor is in a valid position. */
2784 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785 /* Correct cursor for multi-byte character. */
2786 if (has_mbyte)
2787 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002788
2789 curwin->w_set_curswant = set_curswant;
2790 rettv->vval.v_number = 0;
2791}
2792
Bram Moolenaar4f974752019-02-17 17:44:42 +01002793#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002794/*
2795 * "debugbreak()" function
2796 */
2797 static void
2798f_debugbreak(typval_T *argvars, typval_T *rettv)
2799{
2800 int pid;
2801
2802 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002803 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002804 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002805 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002806 else
2807 {
2808 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2809
2810 if (hProcess != NULL)
2811 {
2812 DebugBreakProcess(hProcess);
2813 CloseHandle(hProcess);
2814 rettv->vval.v_number = OK;
2815 }
2816 }
2817}
2818#endif
2819
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002820/*
2821 * "deepcopy()" function
2822 */
2823 static void
2824f_deepcopy(typval_T *argvars, typval_T *rettv)
2825{
2826 int noref = 0;
2827 int copyID;
2828
2829 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002830 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002831 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002832 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002833 else
2834 {
2835 copyID = get_copyID();
2836 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2837 }
2838}
2839
2840/*
2841 * "delete()" function
2842 */
2843 static void
2844f_delete(typval_T *argvars, typval_T *rettv)
2845{
2846 char_u nbuf[NUMBUFLEN];
2847 char_u *name;
2848 char_u *flags;
2849
2850 rettv->vval.v_number = -1;
2851 if (check_restricted() || check_secure())
2852 return;
2853
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002854 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002855 if (name == NULL || *name == NUL)
2856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002857 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002858 return;
2859 }
2860
2861 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002862 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002863 else
2864 flags = (char_u *)"";
2865
2866 if (*flags == NUL)
2867 /* delete a file */
2868 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2869 else if (STRCMP(flags, "d") == 0)
2870 /* delete an empty directory */
2871 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2872 else if (STRCMP(flags, "rf") == 0)
2873 /* delete a directory recursively */
2874 rettv->vval.v_number = delete_recursive(name);
2875 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002876 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002877}
2878
2879/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002880 * "deletebufline()" function
2881 */
2882 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002883f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002884{
2885 buf_T *buf;
2886 linenr_T first, last;
2887 linenr_T lnum;
2888 long count;
2889 int is_curbuf;
2890 buf_T *curbuf_save = NULL;
2891 win_T *curwin_save = NULL;
2892 tabpage_T *tp;
2893 win_T *wp;
2894
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002895 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002896 if (buf == NULL)
2897 {
2898 rettv->vval.v_number = 1; /* FAIL */
2899 return;
2900 }
2901 is_curbuf = buf == curbuf;
2902
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002903 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002904 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002905 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002906 else
2907 last = first;
2908
2909 if (buf->b_ml.ml_mfp == NULL || first < 1
2910 || first > buf->b_ml.ml_line_count || last < first)
2911 {
2912 rettv->vval.v_number = 1; /* FAIL */
2913 return;
2914 }
2915
2916 if (!is_curbuf)
2917 {
2918 curbuf_save = curbuf;
2919 curwin_save = curwin;
2920 curbuf = buf;
2921 find_win_for_curbuf();
2922 }
2923 if (last > curbuf->b_ml.ml_line_count)
2924 last = curbuf->b_ml.ml_line_count;
2925 count = last - first + 1;
2926
2927 // When coming here from Insert mode, sync undo, so that this can be
2928 // undone separately from what was previously inserted.
2929 if (u_sync_once == 2)
2930 {
2931 u_sync_once = 1; // notify that u_sync() was called
2932 u_sync(TRUE);
2933 }
2934
2935 if (u_save(first - 1, last + 1) == FAIL)
2936 {
2937 rettv->vval.v_number = 1; /* FAIL */
2938 return;
2939 }
2940
2941 for (lnum = first; lnum <= last; ++lnum)
2942 ml_delete(first, TRUE);
2943
2944 FOR_ALL_TAB_WINDOWS(tp, wp)
2945 if (wp->w_buffer == buf)
2946 {
2947 if (wp->w_cursor.lnum > last)
2948 wp->w_cursor.lnum -= count;
2949 else if (wp->w_cursor.lnum> first)
2950 wp->w_cursor.lnum = first;
2951 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2952 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2953 }
2954 check_cursor_col();
2955 deleted_lines_mark(first, count);
2956
2957 if (!is_curbuf)
2958 {
2959 curbuf = curbuf_save;
2960 curwin = curwin_save;
2961 }
2962}
2963
2964/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002965 * "did_filetype()" function
2966 */
2967 static void
2968f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2969{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002970 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971}
2972
2973/*
2974 * "diff_filler()" function
2975 */
2976 static void
2977f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2978{
2979#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002980 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002981#endif
2982}
2983
2984/*
2985 * "diff_hlID()" function
2986 */
2987 static void
2988f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2989{
2990#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002991 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002992 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002993 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002994 static int fnum = 0;
2995 static int change_start = 0;
2996 static int change_end = 0;
2997 static hlf_T hlID = (hlf_T)0;
2998 int filler_lines;
2999 int col;
3000
3001 if (lnum < 0) /* ignore type error in {lnum} arg */
3002 lnum = 0;
3003 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003004 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003005 || fnum != curbuf->b_fnum)
3006 {
3007 /* New line, buffer, change: need to get the values. */
3008 filler_lines = diff_check(curwin, lnum);
3009 if (filler_lines < 0)
3010 {
3011 if (filler_lines == -1)
3012 {
3013 change_start = MAXCOL;
3014 change_end = -1;
3015 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3016 hlID = HLF_ADD; /* added line */
3017 else
3018 hlID = HLF_CHD; /* changed line */
3019 }
3020 else
3021 hlID = HLF_ADD; /* added line */
3022 }
3023 else
3024 hlID = (hlf_T)0;
3025 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003026 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027 fnum = curbuf->b_fnum;
3028 }
3029
3030 if (hlID == HLF_CHD || hlID == HLF_TXD)
3031 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003032 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033 if (col >= change_start && col <= change_end)
3034 hlID = HLF_TXD; /* changed text */
3035 else
3036 hlID = HLF_CHD; /* changed line */
3037 }
3038 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3039#endif
3040}
3041
3042/*
3043 * "empty({expr})" function
3044 */
3045 static void
3046f_empty(typval_T *argvars, typval_T *rettv)
3047{
3048 int n = FALSE;
3049
3050 switch (argvars[0].v_type)
3051 {
3052 case VAR_STRING:
3053 case VAR_FUNC:
3054 n = argvars[0].vval.v_string == NULL
3055 || *argvars[0].vval.v_string == NUL;
3056 break;
3057 case VAR_PARTIAL:
3058 n = FALSE;
3059 break;
3060 case VAR_NUMBER:
3061 n = argvars[0].vval.v_number == 0;
3062 break;
3063 case VAR_FLOAT:
3064#ifdef FEAT_FLOAT
3065 n = argvars[0].vval.v_float == 0.0;
3066 break;
3067#endif
3068 case VAR_LIST:
3069 n = argvars[0].vval.v_list == NULL
3070 || argvars[0].vval.v_list->lv_first == NULL;
3071 break;
3072 case VAR_DICT:
3073 n = argvars[0].vval.v_dict == NULL
3074 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3075 break;
3076 case VAR_SPECIAL:
3077 n = argvars[0].vval.v_number != VVAL_TRUE;
3078 break;
3079
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003080 case VAR_BLOB:
3081 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003082 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3083 break;
3084
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003085 case VAR_JOB:
3086#ifdef FEAT_JOB_CHANNEL
3087 n = argvars[0].vval.v_job == NULL
3088 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3089 break;
3090#endif
3091 case VAR_CHANNEL:
3092#ifdef FEAT_JOB_CHANNEL
3093 n = argvars[0].vval.v_channel == NULL
3094 || !channel_is_open(argvars[0].vval.v_channel);
3095 break;
3096#endif
3097 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003098 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003099 n = TRUE;
3100 break;
3101 }
3102
3103 rettv->vval.v_number = n;
3104}
3105
3106/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003107 * "environ()" function
3108 */
3109 static void
3110f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3111{
3112#if !defined(AMIGA)
3113 int i = 0;
3114 char_u *entry, *value;
3115# ifdef MSWIN
3116 extern wchar_t **_wenviron;
3117# else
3118 extern char **environ;
3119# endif
3120
3121 if (rettv_dict_alloc(rettv) != OK)
3122 return;
3123
3124# ifdef MSWIN
3125 if (*_wenviron == NULL)
3126 return;
3127# else
3128 if (*environ == NULL)
3129 return;
3130# endif
3131
3132 for (i = 0; ; ++i)
3133 {
3134# ifdef MSWIN
3135 short_u *p;
3136
3137 if ((p = (short_u *)_wenviron[i]) == NULL)
3138 return;
3139 entry = utf16_to_enc(p, NULL);
3140# else
3141 if ((entry = (char_u *)environ[i]) == NULL)
3142 return;
3143 entry = vim_strsave(entry);
3144# endif
3145 if (entry == NULL) // out of memory
3146 return;
3147 if ((value = vim_strchr(entry, '=')) == NULL)
3148 {
3149 vim_free(entry);
3150 continue;
3151 }
3152 *value++ = NUL;
3153 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3154 vim_free(entry);
3155 }
3156#endif
3157}
3158
3159/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003160 * "escape({string}, {chars})" function
3161 */
3162 static void
3163f_escape(typval_T *argvars, typval_T *rettv)
3164{
3165 char_u buf[NUMBUFLEN];
3166
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003167 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3168 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003169 rettv->v_type = VAR_STRING;
3170}
3171
3172/*
3173 * "eval()" function
3174 */
3175 static void
3176f_eval(typval_T *argvars, typval_T *rettv)
3177{
3178 char_u *s, *p;
3179
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003180 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003181 if (s != NULL)
3182 s = skipwhite(s);
3183
3184 p = s;
3185 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3186 {
3187 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003188 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003189 need_clr_eos = FALSE;
3190 rettv->v_type = VAR_NUMBER;
3191 rettv->vval.v_number = 0;
3192 }
3193 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003194 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003195}
3196
3197/*
3198 * "eventhandler()" function
3199 */
3200 static void
3201f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3202{
3203 rettv->vval.v_number = vgetc_busy;
3204}
3205
3206/*
3207 * "executable()" function
3208 */
3209 static void
3210f_executable(typval_T *argvars, typval_T *rettv)
3211{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003212 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003213
3214 /* Check in $PATH and also check directly if there is a directory name. */
3215 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3216 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3217}
3218
3219static garray_T redir_execute_ga;
3220
3221/*
3222 * Append "value[value_len]" to the execute() output.
3223 */
3224 void
3225execute_redir_str(char_u *value, int value_len)
3226{
3227 int len;
3228
3229 if (value_len == -1)
3230 len = (int)STRLEN(value); /* Append the entire string */
3231 else
3232 len = value_len; /* Append only "value_len" characters */
3233 if (ga_grow(&redir_execute_ga, len) == OK)
3234 {
3235 mch_memmove((char *)redir_execute_ga.ga_data
3236 + redir_execute_ga.ga_len, value, len);
3237 redir_execute_ga.ga_len += len;
3238 }
3239}
3240
3241/*
3242 * Get next line from a list.
3243 * Called by do_cmdline() to get the next line.
3244 * Returns allocated string, or NULL for end of function.
3245 */
3246
3247 static char_u *
3248get_list_line(
3249 int c UNUSED,
3250 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003251 int indent UNUSED,
3252 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003253{
3254 listitem_T **p = (listitem_T **)cookie;
3255 listitem_T *item = *p;
3256 char_u buf[NUMBUFLEN];
3257 char_u *s;
3258
3259 if (item == NULL)
3260 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003261 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003262 *p = item->li_next;
3263 return s == NULL ? NULL : vim_strsave(s);
3264}
3265
3266/*
3267 * "execute()" function
3268 */
3269 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003270execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003271{
3272 char_u *cmd = NULL;
3273 list_T *list = NULL;
3274 int save_msg_silent = msg_silent;
3275 int save_emsg_silent = emsg_silent;
3276 int save_emsg_noredir = emsg_noredir;
3277 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003278 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003280 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003281 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003282
3283 rettv->vval.v_string = NULL;
3284 rettv->v_type = VAR_STRING;
3285
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003286 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003287 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003288 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003289 if (list == NULL || list->lv_first == NULL)
3290 /* empty list, no commands, empty output */
3291 return;
3292 ++list->lv_refcount;
3293 }
3294 else
3295 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003296 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 if (cmd == NULL)
3298 return;
3299 }
3300
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003301 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003302 {
3303 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003304 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003305
3306 if (s == NULL)
3307 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003308 if (*s == NUL)
3309 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003310 if (STRNCMP(s, "silent", 6) == 0)
3311 ++msg_silent;
3312 if (STRCMP(s, "silent!") == 0)
3313 {
3314 emsg_silent = TRUE;
3315 emsg_noredir = TRUE;
3316 }
3317 }
3318 else
3319 ++msg_silent;
3320
3321 if (redir_execute)
3322 save_ga = redir_execute_ga;
3323 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3324 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003325 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003326 if (!echo_output)
3327 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003328
3329 if (cmd != NULL)
3330 do_cmdline_cmd(cmd);
3331 else
3332 {
3333 listitem_T *item = list->lv_first;
3334
3335 do_cmdline(NULL, get_list_line, (void *)&item,
3336 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3337 --list->lv_refcount;
3338 }
3339
Bram Moolenaard297f352017-01-29 20:31:21 +01003340 /* Need to append a NUL to the result. */
3341 if (ga_grow(&redir_execute_ga, 1) == OK)
3342 {
3343 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3344 rettv->vval.v_string = redir_execute_ga.ga_data;
3345 }
3346 else
3347 {
3348 ga_clear(&redir_execute_ga);
3349 rettv->vval.v_string = NULL;
3350 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003351 msg_silent = save_msg_silent;
3352 emsg_silent = save_emsg_silent;
3353 emsg_noredir = save_emsg_noredir;
3354
3355 redir_execute = save_redir_execute;
3356 if (redir_execute)
3357 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003358 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003359
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003360 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003361 if (echo_output)
3362 // When not working silently: put it in column zero. A following
3363 // "echon" will overwrite the message, unavoidably.
3364 msg_col = 0;
3365 else
3366 // When working silently: Put it back where it was, since nothing
3367 // should have been written.
3368 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003369}
3370
3371/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003372 * "execute()" function
3373 */
3374 static void
3375f_execute(typval_T *argvars, typval_T *rettv)
3376{
3377 execute_common(argvars, rettv, 0);
3378}
3379
3380/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003381 * "exepath()" function
3382 */
3383 static void
3384f_exepath(typval_T *argvars, typval_T *rettv)
3385{
3386 char_u *p = NULL;
3387
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003388 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003389 rettv->v_type = VAR_STRING;
3390 rettv->vval.v_string = p;
3391}
3392
3393/*
3394 * "exists()" function
3395 */
3396 static void
3397f_exists(typval_T *argvars, typval_T *rettv)
3398{
3399 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003400 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003401
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003402 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003403 if (*p == '$') /* environment variable */
3404 {
3405 /* first try "normal" environment variables (fast) */
3406 if (mch_getenv(p + 1) != NULL)
3407 n = TRUE;
3408 else
3409 {
3410 /* try expanding things like $VIM and ${HOME} */
3411 p = expand_env_save(p);
3412 if (p != NULL && *p != '$')
3413 n = TRUE;
3414 vim_free(p);
3415 }
3416 }
3417 else if (*p == '&' || *p == '+') /* option */
3418 {
3419 n = (get_option_tv(&p, NULL, TRUE) == OK);
3420 if (*skipwhite(p) != NUL)
3421 n = FALSE; /* trailing garbage */
3422 }
3423 else if (*p == '*') /* internal or user defined function */
3424 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003425 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003426 }
3427 else if (*p == ':')
3428 {
3429 n = cmd_exists(p + 1);
3430 }
3431 else if (*p == '#')
3432 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003433 if (p[1] == '#')
3434 n = autocmd_supported(p + 2);
3435 else
3436 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003437 }
3438 else /* internal variable */
3439 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003440 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441 }
3442
3443 rettv->vval.v_number = n;
3444}
3445
3446#ifdef FEAT_FLOAT
3447/*
3448 * "exp()" function
3449 */
3450 static void
3451f_exp(typval_T *argvars, typval_T *rettv)
3452{
3453 float_T f = 0.0;
3454
3455 rettv->v_type = VAR_FLOAT;
3456 if (get_float_arg(argvars, &f) == OK)
3457 rettv->vval.v_float = exp(f);
3458 else
3459 rettv->vval.v_float = 0.0;
3460}
3461#endif
3462
3463/*
3464 * "expand()" function
3465 */
3466 static void
3467f_expand(typval_T *argvars, typval_T *rettv)
3468{
3469 char_u *s;
3470 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003471 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3473 expand_T xpc;
3474 int error = FALSE;
3475 char_u *result;
3476
3477 rettv->v_type = VAR_STRING;
3478 if (argvars[1].v_type != VAR_UNKNOWN
3479 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003480 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003481 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003482 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003484 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003485 if (*s == '%' || *s == '#' || *s == '<')
3486 {
3487 ++emsg_off;
3488 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3489 --emsg_off;
3490 if (rettv->v_type == VAR_LIST)
3491 {
3492 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3493 list_append_string(rettv->vval.v_list, result, -1);
3494 else
3495 vim_free(result);
3496 }
3497 else
3498 rettv->vval.v_string = result;
3499 }
3500 else
3501 {
3502 /* When the optional second argument is non-zero, don't remove matches
3503 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3504 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003505 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003506 options |= WILD_KEEP_ALL;
3507 if (!error)
3508 {
3509 ExpandInit(&xpc);
3510 xpc.xp_context = EXPAND_FILES;
3511 if (p_wic)
3512 options += WILD_ICASE;
3513 if (rettv->v_type == VAR_STRING)
3514 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3515 options, WILD_ALL);
3516 else if (rettv_list_alloc(rettv) != FAIL)
3517 {
3518 int i;
3519
3520 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3521 for (i = 0; i < xpc.xp_numfiles; i++)
3522 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3523 ExpandCleanup(&xpc);
3524 }
3525 }
3526 else
3527 rettv->vval.v_string = NULL;
3528 }
3529}
3530
3531/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003532 * "expandcmd()" function
3533 * Expand all the special characters in a command string.
3534 */
3535 static void
3536f_expandcmd(typval_T *argvars, typval_T *rettv)
3537{
3538 exarg_T eap;
3539 char_u *cmdstr;
3540 char *errormsg = NULL;
3541
3542 rettv->v_type = VAR_STRING;
3543 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3544
3545 memset(&eap, 0, sizeof(eap));
3546 eap.cmd = cmdstr;
3547 eap.arg = cmdstr;
3548 eap.argt |= NOSPC;
3549 eap.usefilter = FALSE;
3550 eap.nextcmd = NULL;
3551 eap.cmdidx = CMD_USER;
3552
3553 expand_filename(&eap, &cmdstr, &errormsg);
3554 if (errormsg != NULL && *errormsg != NUL)
3555 emsg(errormsg);
3556
3557 rettv->vval.v_string = cmdstr;
3558}
3559
3560/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561 * "extend(list, list [, idx])" function
3562 * "extend(dict, dict [, action])" function
3563 */
3564 static void
3565f_extend(typval_T *argvars, typval_T *rettv)
3566{
3567 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3568
3569 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3570 {
3571 list_T *l1, *l2;
3572 listitem_T *item;
3573 long before;
3574 int error = FALSE;
3575
3576 l1 = argvars[0].vval.v_list;
3577 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003578 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003579 && l2 != NULL)
3580 {
3581 if (argvars[2].v_type != VAR_UNKNOWN)
3582 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003583 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003584 if (error)
3585 return; /* type error; errmsg already given */
3586
3587 if (before == l1->lv_len)
3588 item = NULL;
3589 else
3590 {
3591 item = list_find(l1, before);
3592 if (item == NULL)
3593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003594 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003595 return;
3596 }
3597 }
3598 }
3599 else
3600 item = NULL;
3601 list_extend(l1, l2, item);
3602
3603 copy_tv(&argvars[0], rettv);
3604 }
3605 }
3606 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3607 {
3608 dict_T *d1, *d2;
3609 char_u *action;
3610 int i;
3611
3612 d1 = argvars[0].vval.v_dict;
3613 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003614 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003615 && d2 != NULL)
3616 {
3617 /* Check the third argument. */
3618 if (argvars[2].v_type != VAR_UNKNOWN)
3619 {
3620 static char *(av[]) = {"keep", "force", "error"};
3621
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003622 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003623 if (action == NULL)
3624 return; /* type error; errmsg already given */
3625 for (i = 0; i < 3; ++i)
3626 if (STRCMP(action, av[i]) == 0)
3627 break;
3628 if (i == 3)
3629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003630 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003631 return;
3632 }
3633 }
3634 else
3635 action = (char_u *)"force";
3636
3637 dict_extend(d1, d2, action);
3638
3639 copy_tv(&argvars[0], rettv);
3640 }
3641 }
3642 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003643 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003644}
3645
3646/*
3647 * "feedkeys()" function
3648 */
3649 static void
3650f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3651{
3652 int remap = TRUE;
3653 int insert = FALSE;
3654 char_u *keys, *flags;
3655 char_u nbuf[NUMBUFLEN];
3656 int typed = FALSE;
3657 int execute = FALSE;
3658 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003659 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003660 char_u *keys_esc;
3661
3662 /* This is not allowed in the sandbox. If the commands would still be
3663 * executed in the sandbox it would be OK, but it probably happens later,
3664 * when "sandbox" is no longer set. */
3665 if (check_secure())
3666 return;
3667
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003668 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003669
3670 if (argvars[1].v_type != VAR_UNKNOWN)
3671 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003672 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003673 for ( ; *flags != NUL; ++flags)
3674 {
3675 switch (*flags)
3676 {
3677 case 'n': remap = FALSE; break;
3678 case 'm': remap = TRUE; break;
3679 case 't': typed = TRUE; break;
3680 case 'i': insert = TRUE; break;
3681 case 'x': execute = TRUE; break;
3682 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003683 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003684 }
3685 }
3686 }
3687
3688 if (*keys != NUL || execute)
3689 {
3690 /* Need to escape K_SPECIAL and CSI before putting the string in the
3691 * typeahead buffer. */
3692 keys_esc = vim_strsave_escape_csi(keys);
3693 if (keys_esc != NULL)
3694 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003695 if (lowlevel)
3696 {
3697#ifdef USE_INPUT_BUF
3698 add_to_input_buf(keys, (int)STRLEN(keys));
3699#else
3700 emsg(_("E980: lowlevel input not supported"));
3701#endif
3702 }
3703 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003704 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003705 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003706 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003707 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003708#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003709 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003710#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003711 )
3712 typebuf_was_filled = TRUE;
3713 }
3714 vim_free(keys_esc);
3715
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003716 if (execute)
3717 {
3718 int save_msg_scroll = msg_scroll;
3719
3720 /* Avoid a 1 second delay when the keys start Insert mode. */
3721 msg_scroll = FALSE;
3722
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003723 if (!dangerous)
3724 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003725 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003726 if (!dangerous)
3727 --ex_normal_busy;
3728
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003729 msg_scroll |= save_msg_scroll;
3730 }
3731 }
3732 }
3733}
3734
3735/*
3736 * "filereadable()" function
3737 */
3738 static void
3739f_filereadable(typval_T *argvars, typval_T *rettv)
3740{
3741 int fd;
3742 char_u *p;
3743 int n;
3744
3745#ifndef O_NONBLOCK
3746# define O_NONBLOCK 0
3747#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003748 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3750 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3751 {
3752 n = TRUE;
3753 close(fd);
3754 }
3755 else
3756 n = FALSE;
3757
3758 rettv->vval.v_number = n;
3759}
3760
3761/*
3762 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3763 * rights to write into.
3764 */
3765 static void
3766f_filewritable(typval_T *argvars, typval_T *rettv)
3767{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003768 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003769}
3770
3771 static void
3772findfilendir(
3773 typval_T *argvars UNUSED,
3774 typval_T *rettv,
3775 int find_what UNUSED)
3776{
3777#ifdef FEAT_SEARCHPATH
3778 char_u *fname;
3779 char_u *fresult = NULL;
3780 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3781 char_u *p;
3782 char_u pathbuf[NUMBUFLEN];
3783 int count = 1;
3784 int first = TRUE;
3785 int error = FALSE;
3786#endif
3787
3788 rettv->vval.v_string = NULL;
3789 rettv->v_type = VAR_STRING;
3790
3791#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003792 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003793
3794 if (argvars[1].v_type != VAR_UNKNOWN)
3795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003796 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003797 if (p == NULL)
3798 error = TRUE;
3799 else
3800 {
3801 if (*p != NUL)
3802 path = p;
3803
3804 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003805 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003806 }
3807 }
3808
3809 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3810 error = TRUE;
3811
3812 if (*fname != NUL && !error)
3813 {
3814 do
3815 {
3816 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3817 vim_free(fresult);
3818 fresult = find_file_in_path_option(first ? fname : NULL,
3819 first ? (int)STRLEN(fname) : 0,
3820 0, first, path,
3821 find_what,
3822 curbuf->b_ffname,
3823 find_what == FINDFILE_DIR
3824 ? (char_u *)"" : curbuf->b_p_sua);
3825 first = FALSE;
3826
3827 if (fresult != NULL && rettv->v_type == VAR_LIST)
3828 list_append_string(rettv->vval.v_list, fresult, -1);
3829
3830 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3831 }
3832
3833 if (rettv->v_type == VAR_STRING)
3834 rettv->vval.v_string = fresult;
3835#endif
3836}
3837
3838/*
3839 * "filter()" function
3840 */
3841 static void
3842f_filter(typval_T *argvars, typval_T *rettv)
3843{
3844 filter_map(argvars, rettv, FALSE);
3845}
3846
3847/*
3848 * "finddir({fname}[, {path}[, {count}]])" function
3849 */
3850 static void
3851f_finddir(typval_T *argvars, typval_T *rettv)
3852{
3853 findfilendir(argvars, rettv, FINDFILE_DIR);
3854}
3855
3856/*
3857 * "findfile({fname}[, {path}[, {count}]])" function
3858 */
3859 static void
3860f_findfile(typval_T *argvars, typval_T *rettv)
3861{
3862 findfilendir(argvars, rettv, FINDFILE_FILE);
3863}
3864
3865#ifdef FEAT_FLOAT
3866/*
3867 * "float2nr({float})" function
3868 */
3869 static void
3870f_float2nr(typval_T *argvars, typval_T *rettv)
3871{
3872 float_T f = 0.0;
3873
3874 if (get_float_arg(argvars, &f) == OK)
3875 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003876 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003877 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003878 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003879 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003880 else
3881 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003882 }
3883}
3884
3885/*
3886 * "floor({float})" function
3887 */
3888 static void
3889f_floor(typval_T *argvars, typval_T *rettv)
3890{
3891 float_T f = 0.0;
3892
3893 rettv->v_type = VAR_FLOAT;
3894 if (get_float_arg(argvars, &f) == OK)
3895 rettv->vval.v_float = floor(f);
3896 else
3897 rettv->vval.v_float = 0.0;
3898}
3899
3900/*
3901 * "fmod()" function
3902 */
3903 static void
3904f_fmod(typval_T *argvars, typval_T *rettv)
3905{
3906 float_T fx = 0.0, fy = 0.0;
3907
3908 rettv->v_type = VAR_FLOAT;
3909 if (get_float_arg(argvars, &fx) == OK
3910 && get_float_arg(&argvars[1], &fy) == OK)
3911 rettv->vval.v_float = fmod(fx, fy);
3912 else
3913 rettv->vval.v_float = 0.0;
3914}
3915#endif
3916
3917/*
3918 * "fnameescape({string})" function
3919 */
3920 static void
3921f_fnameescape(typval_T *argvars, typval_T *rettv)
3922{
3923 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003924 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003925 rettv->v_type = VAR_STRING;
3926}
3927
3928/*
3929 * "fnamemodify({fname}, {mods})" function
3930 */
3931 static void
3932f_fnamemodify(typval_T *argvars, typval_T *rettv)
3933{
3934 char_u *fname;
3935 char_u *mods;
3936 int usedlen = 0;
3937 int len;
3938 char_u *fbuf = NULL;
3939 char_u buf[NUMBUFLEN];
3940
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003941 fname = tv_get_string_chk(&argvars[0]);
3942 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003943 if (fname == NULL || mods == NULL)
3944 fname = NULL;
3945 else
3946 {
3947 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003948 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003949 }
3950
3951 rettv->v_type = VAR_STRING;
3952 if (fname == NULL)
3953 rettv->vval.v_string = NULL;
3954 else
3955 rettv->vval.v_string = vim_strnsave(fname, len);
3956 vim_free(fbuf);
3957}
3958
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003959/*
3960 * "foldclosed()" function
3961 */
3962 static void
3963foldclosed_both(
3964 typval_T *argvars UNUSED,
3965 typval_T *rettv,
3966 int end UNUSED)
3967{
3968#ifdef FEAT_FOLDING
3969 linenr_T lnum;
3970 linenr_T first, last;
3971
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003972 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003973 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3974 {
3975 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3976 {
3977 if (end)
3978 rettv->vval.v_number = (varnumber_T)last;
3979 else
3980 rettv->vval.v_number = (varnumber_T)first;
3981 return;
3982 }
3983 }
3984#endif
3985 rettv->vval.v_number = -1;
3986}
3987
3988/*
3989 * "foldclosed()" function
3990 */
3991 static void
3992f_foldclosed(typval_T *argvars, typval_T *rettv)
3993{
3994 foldclosed_both(argvars, rettv, FALSE);
3995}
3996
3997/*
3998 * "foldclosedend()" function
3999 */
4000 static void
4001f_foldclosedend(typval_T *argvars, typval_T *rettv)
4002{
4003 foldclosed_both(argvars, rettv, TRUE);
4004}
4005
4006/*
4007 * "foldlevel()" function
4008 */
4009 static void
4010f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4011{
4012#ifdef FEAT_FOLDING
4013 linenr_T lnum;
4014
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004015 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004016 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4017 rettv->vval.v_number = foldLevel(lnum);
4018#endif
4019}
4020
4021/*
4022 * "foldtext()" function
4023 */
4024 static void
4025f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4026{
4027#ifdef FEAT_FOLDING
4028 linenr_T foldstart;
4029 linenr_T foldend;
4030 char_u *dashes;
4031 linenr_T lnum;
4032 char_u *s;
4033 char_u *r;
4034 int len;
4035 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004036 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004037#endif
4038
4039 rettv->v_type = VAR_STRING;
4040 rettv->vval.v_string = NULL;
4041#ifdef FEAT_FOLDING
4042 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4043 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4044 dashes = get_vim_var_str(VV_FOLDDASHES);
4045 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4046 && dashes != NULL)
4047 {
4048 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004049 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004050 if (!linewhite(lnum))
4051 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004052
4053 /* Find interesting text in this line. */
4054 s = skipwhite(ml_get(lnum));
4055 /* skip C comment-start */
4056 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4057 {
4058 s = skipwhite(s + 2);
4059 if (*skipwhite(s) == NUL
4060 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4061 {
4062 s = skipwhite(ml_get(lnum + 1));
4063 if (*s == '*')
4064 s = skipwhite(s + 1);
4065 }
4066 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004067 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004068 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004069 r = alloc(STRLEN(txt)
4070 + STRLEN(dashes) // for %s
4071 + 20 // for %3ld
4072 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004073 if (r != NULL)
4074 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004075 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004076 len = (int)STRLEN(r);
4077 STRCAT(r, s);
4078 /* remove 'foldmarker' and 'commentstring' */
4079 foldtext_cleanup(r + len);
4080 rettv->vval.v_string = r;
4081 }
4082 }
4083#endif
4084}
4085
4086/*
4087 * "foldtextresult(lnum)" function
4088 */
4089 static void
4090f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4091{
4092#ifdef FEAT_FOLDING
4093 linenr_T lnum;
4094 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004095 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004096 foldinfo_T foldinfo;
4097 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004098 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004099#endif
4100
4101 rettv->v_type = VAR_STRING;
4102 rettv->vval.v_string = NULL;
4103#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004104 if (entered)
4105 return; /* reject recursive use */
4106 entered = TRUE;
4107
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004108 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004109 /* treat illegal types and illegal string values for {lnum} the same */
4110 if (lnum < 0)
4111 lnum = 0;
4112 fold_count = foldedCount(curwin, lnum, &foldinfo);
4113 if (fold_count > 0)
4114 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004115 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4116 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004117 if (text == buf)
4118 text = vim_strsave(text);
4119 rettv->vval.v_string = text;
4120 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004121
4122 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004123#endif
4124}
4125
4126/*
4127 * "foreground()" function
4128 */
4129 static void
4130f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4131{
4132#ifdef FEAT_GUI
4133 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004134 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004135 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004136 return;
4137 }
4138#endif
4139#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004140 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004141#endif
4142}
4143
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004144 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004145common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004146{
4147 char_u *s;
4148 char_u *name;
4149 int use_string = FALSE;
4150 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004151 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004152
4153 if (argvars[0].v_type == VAR_FUNC)
4154 {
4155 /* function(MyFunc, [arg], dict) */
4156 s = argvars[0].vval.v_string;
4157 }
4158 else if (argvars[0].v_type == VAR_PARTIAL
4159 && argvars[0].vval.v_partial != NULL)
4160 {
4161 /* function(dict.MyFunc, [arg]) */
4162 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004163 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004164 }
4165 else
4166 {
4167 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004168 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004169 use_string = TRUE;
4170 }
4171
Bram Moolenaar843b8842016-08-21 14:36:15 +02004172 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004173 {
4174 name = s;
4175 trans_name = trans_function_name(&name, FALSE,
4176 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4177 if (*name != NUL)
4178 s = NULL;
4179 }
4180
Bram Moolenaar843b8842016-08-21 14:36:15 +02004181 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4182 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004183 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004184 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004185 else if (trans_name != NULL && (is_funcref
4186 ? find_func(trans_name) == NULL
4187 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004188 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004189 else
4190 {
4191 int dict_idx = 0;
4192 int arg_idx = 0;
4193 list_T *list = NULL;
4194
4195 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4196 {
4197 char sid_buf[25];
4198 int off = *s == 's' ? 2 : 5;
4199
4200 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4201 * also be called from another script. Using trans_function_name()
4202 * would also work, but some plugins depend on the name being
4203 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004204 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004205 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004206 if (name != NULL)
4207 {
4208 STRCPY(name, sid_buf);
4209 STRCAT(name, s + off);
4210 }
4211 }
4212 else
4213 name = vim_strsave(s);
4214
4215 if (argvars[1].v_type != VAR_UNKNOWN)
4216 {
4217 if (argvars[2].v_type != VAR_UNKNOWN)
4218 {
4219 /* function(name, [args], dict) */
4220 arg_idx = 1;
4221 dict_idx = 2;
4222 }
4223 else if (argvars[1].v_type == VAR_DICT)
4224 /* function(name, dict) */
4225 dict_idx = 1;
4226 else
4227 /* function(name, [args]) */
4228 arg_idx = 1;
4229 if (dict_idx > 0)
4230 {
4231 if (argvars[dict_idx].v_type != VAR_DICT)
4232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004233 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004234 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004235 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004236 }
4237 if (argvars[dict_idx].vval.v_dict == NULL)
4238 dict_idx = 0;
4239 }
4240 if (arg_idx > 0)
4241 {
4242 if (argvars[arg_idx].v_type != VAR_LIST)
4243 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004244 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004245 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004246 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004247 }
4248 list = argvars[arg_idx].vval.v_list;
4249 if (list == NULL || list->lv_len == 0)
4250 arg_idx = 0;
4251 }
4252 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004253 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004254 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004255 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004256
4257 /* result is a VAR_PARTIAL */
4258 if (pt == NULL)
4259 vim_free(name);
4260 else
4261 {
4262 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4263 {
4264 listitem_T *li;
4265 int i = 0;
4266 int arg_len = 0;
4267 int lv_len = 0;
4268
4269 if (arg_pt != NULL)
4270 arg_len = arg_pt->pt_argc;
4271 if (list != NULL)
4272 lv_len = list->lv_len;
4273 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004274 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004275 if (pt->pt_argv == NULL)
4276 {
4277 vim_free(pt);
4278 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004279 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004280 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004281 for (i = 0; i < arg_len; i++)
4282 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4283 if (lv_len > 0)
4284 for (li = list->lv_first; li != NULL;
4285 li = li->li_next)
4286 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004287 }
4288
4289 /* For "function(dict.func, [], dict)" and "func" is a partial
4290 * use "dict". That is backwards compatible. */
4291 if (dict_idx > 0)
4292 {
4293 /* The dict is bound explicitly, pt_auto is FALSE. */
4294 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4295 ++pt->pt_dict->dv_refcount;
4296 }
4297 else if (arg_pt != NULL)
4298 {
4299 /* If the dict was bound automatically the result is also
4300 * bound automatically. */
4301 pt->pt_dict = arg_pt->pt_dict;
4302 pt->pt_auto = arg_pt->pt_auto;
4303 if (pt->pt_dict != NULL)
4304 ++pt->pt_dict->dv_refcount;
4305 }
4306
4307 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004308 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4309 {
4310 pt->pt_func = arg_pt->pt_func;
4311 func_ptr_ref(pt->pt_func);
4312 vim_free(name);
4313 }
4314 else if (is_funcref)
4315 {
4316 pt->pt_func = find_func(trans_name);
4317 func_ptr_ref(pt->pt_func);
4318 vim_free(name);
4319 }
4320 else
4321 {
4322 pt->pt_name = name;
4323 func_ref(name);
4324 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004325 }
4326 rettv->v_type = VAR_PARTIAL;
4327 rettv->vval.v_partial = pt;
4328 }
4329 else
4330 {
4331 /* result is a VAR_FUNC */
4332 rettv->v_type = VAR_FUNC;
4333 rettv->vval.v_string = name;
4334 func_ref(name);
4335 }
4336 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004337theend:
4338 vim_free(trans_name);
4339}
4340
4341/*
4342 * "funcref()" function
4343 */
4344 static void
4345f_funcref(typval_T *argvars, typval_T *rettv)
4346{
4347 common_function(argvars, rettv, TRUE);
4348}
4349
4350/*
4351 * "function()" function
4352 */
4353 static void
4354f_function(typval_T *argvars, typval_T *rettv)
4355{
4356 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004357}
4358
4359/*
4360 * "garbagecollect()" function
4361 */
4362 static void
4363f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4364{
4365 /* This is postponed until we are back at the toplevel, because we may be
4366 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4367 want_garbage_collect = TRUE;
4368
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004369 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004370 garbage_collect_at_exit = TRUE;
4371}
4372
4373/*
4374 * "get()" function
4375 */
4376 static void
4377f_get(typval_T *argvars, typval_T *rettv)
4378{
4379 listitem_T *li;
4380 list_T *l;
4381 dictitem_T *di;
4382 dict_T *d;
4383 typval_T *tv = NULL;
4384
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004385 if (argvars[0].v_type == VAR_BLOB)
4386 {
4387 int error = FALSE;
4388 int idx = tv_get_number_chk(&argvars[1], &error);
4389
4390 if (!error)
4391 {
4392 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004393 if (idx < 0)
4394 idx = blob_len(argvars[0].vval.v_blob) + idx;
4395 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4396 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004397 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004398 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004399 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004400 tv = rettv;
4401 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004402 }
4403 }
4404 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004405 {
4406 if ((l = argvars[0].vval.v_list) != NULL)
4407 {
4408 int error = FALSE;
4409
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004410 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004411 if (!error && li != NULL)
4412 tv = &li->li_tv;
4413 }
4414 }
4415 else if (argvars[0].v_type == VAR_DICT)
4416 {
4417 if ((d = argvars[0].vval.v_dict) != NULL)
4418 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004419 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004420 if (di != NULL)
4421 tv = &di->di_tv;
4422 }
4423 }
4424 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4425 {
4426 partial_T *pt;
4427 partial_T fref_pt;
4428
4429 if (argvars[0].v_type == VAR_PARTIAL)
4430 pt = argvars[0].vval.v_partial;
4431 else
4432 {
4433 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4434 fref_pt.pt_name = argvars[0].vval.v_string;
4435 pt = &fref_pt;
4436 }
4437
4438 if (pt != NULL)
4439 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004440 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004441 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004442
4443 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4444 {
4445 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004446 n = partial_name(pt);
4447 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004448 rettv->vval.v_string = NULL;
4449 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004450 {
4451 rettv->vval.v_string = vim_strsave(n);
4452 if (rettv->v_type == VAR_FUNC)
4453 func_ref(rettv->vval.v_string);
4454 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004455 }
4456 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004457 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004458 else if (STRCMP(what, "args") == 0)
4459 {
4460 rettv->v_type = VAR_LIST;
4461 if (rettv_list_alloc(rettv) == OK)
4462 {
4463 int i;
4464
4465 for (i = 0; i < pt->pt_argc; ++i)
4466 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4467 }
4468 }
4469 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004470 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004471 return;
4472 }
4473 }
4474 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004475 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004476
4477 if (tv == NULL)
4478 {
4479 if (argvars[2].v_type != VAR_UNKNOWN)
4480 copy_tv(&argvars[2], rettv);
4481 }
4482 else
4483 copy_tv(tv, rettv);
4484}
4485
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004486/*
4487 * Returns buffer options, variables and other attributes in a dictionary.
4488 */
4489 static dict_T *
4490get_buffer_info(buf_T *buf)
4491{
4492 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004493 tabpage_T *tp;
4494 win_T *wp;
4495 list_T *windows;
4496
4497 dict = dict_alloc();
4498 if (dict == NULL)
4499 return NULL;
4500
Bram Moolenaare0be1672018-07-08 16:50:37 +02004501 dict_add_number(dict, "bufnr", buf->b_fnum);
4502 dict_add_string(dict, "name", buf->b_ffname);
4503 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4504 : buflist_findlnum(buf));
4505 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4506 dict_add_number(dict, "listed", buf->b_p_bl);
4507 dict_add_number(dict, "changed", bufIsChanged(buf));
4508 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4509 dict_add_number(dict, "hidden",
4510 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004511
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004512 /* Get a reference to buffer variables */
4513 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004514
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004515 /* List of windows displaying this buffer */
4516 windows = list_alloc();
4517 if (windows != NULL)
4518 {
4519 FOR_ALL_TAB_WINDOWS(tp, wp)
4520 if (wp->w_buffer == buf)
4521 list_append_number(windows, (varnumber_T)wp->w_id);
4522 dict_add_list(dict, "windows", windows);
4523 }
4524
4525#ifdef FEAT_SIGNS
4526 if (buf->b_signlist != NULL)
4527 {
4528 /* List of signs placed in this buffer */
4529 list_T *signs = list_alloc();
4530 if (signs != NULL)
4531 {
4532 get_buffer_signs(buf, signs);
4533 dict_add_list(dict, "signs", signs);
4534 }
4535 }
4536#endif
4537
4538 return dict;
4539}
4540
4541/*
4542 * "getbufinfo()" function
4543 */
4544 static void
4545f_getbufinfo(typval_T *argvars, typval_T *rettv)
4546{
4547 buf_T *buf = NULL;
4548 buf_T *argbuf = NULL;
4549 dict_T *d;
4550 int filtered = FALSE;
4551 int sel_buflisted = FALSE;
4552 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004553 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004554
4555 if (rettv_list_alloc(rettv) != OK)
4556 return;
4557
4558 /* List of all the buffers or selected buffers */
4559 if (argvars[0].v_type == VAR_DICT)
4560 {
4561 dict_T *sel_d = argvars[0].vval.v_dict;
4562
4563 if (sel_d != NULL)
4564 {
4565 dictitem_T *di;
4566
4567 filtered = TRUE;
4568
4569 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004570 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004571 sel_buflisted = TRUE;
4572
4573 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004574 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004575 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004576
4577 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004578 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004579 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004580 }
4581 }
4582 else if (argvars[0].v_type != VAR_UNKNOWN)
4583 {
4584 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004585 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004586 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004587 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004588 --emsg_off;
4589 if (argbuf == NULL)
4590 return;
4591 }
4592
4593 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004594 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004595 {
4596 if (argbuf != NULL && argbuf != buf)
4597 continue;
4598 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004599 || (sel_buflisted && !buf->b_p_bl)
4600 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004601 continue;
4602
4603 d = get_buffer_info(buf);
4604 if (d != NULL)
4605 list_append_dict(rettv->vval.v_list, d);
4606 if (argbuf != NULL)
4607 return;
4608 }
4609}
4610
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004611/*
4612 * Get line or list of lines from buffer "buf" into "rettv".
4613 * Return a range (from start to end) of lines in rettv from the specified
4614 * buffer.
4615 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4616 */
4617 static void
4618get_buffer_lines(
4619 buf_T *buf,
4620 linenr_T start,
4621 linenr_T end,
4622 int retlist,
4623 typval_T *rettv)
4624{
4625 char_u *p;
4626
4627 rettv->v_type = VAR_STRING;
4628 rettv->vval.v_string = NULL;
4629 if (retlist && rettv_list_alloc(rettv) == FAIL)
4630 return;
4631
4632 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4633 return;
4634
4635 if (!retlist)
4636 {
4637 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4638 p = ml_get_buf(buf, start, FALSE);
4639 else
4640 p = (char_u *)"";
4641 rettv->vval.v_string = vim_strsave(p);
4642 }
4643 else
4644 {
4645 if (end < start)
4646 return;
4647
4648 if (start < 1)
4649 start = 1;
4650 if (end > buf->b_ml.ml_line_count)
4651 end = buf->b_ml.ml_line_count;
4652 while (start <= end)
4653 if (list_append_string(rettv->vval.v_list,
4654 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4655 break;
4656 }
4657}
4658
4659/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004660 * "getbufline()" function
4661 */
4662 static void
4663f_getbufline(typval_T *argvars, typval_T *rettv)
4664{
4665 linenr_T lnum;
4666 linenr_T end;
4667 buf_T *buf;
4668
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004669 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004670 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004671 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004672 --emsg_off;
4673
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004674 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004675 if (argvars[2].v_type == VAR_UNKNOWN)
4676 end = lnum;
4677 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004678 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004679
4680 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4681}
4682
4683/*
4684 * "getbufvar()" function
4685 */
4686 static void
4687f_getbufvar(typval_T *argvars, typval_T *rettv)
4688{
4689 buf_T *buf;
4690 buf_T *save_curbuf;
4691 char_u *varname;
4692 dictitem_T *v;
4693 int done = FALSE;
4694
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004695 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4696 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004697 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004698 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004699
4700 rettv->v_type = VAR_STRING;
4701 rettv->vval.v_string = NULL;
4702
4703 if (buf != NULL && varname != NULL)
4704 {
4705 /* set curbuf to be our buf, temporarily */
4706 save_curbuf = curbuf;
4707 curbuf = buf;
4708
Bram Moolenaar30567352016-08-27 21:25:44 +02004709 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004710 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004711 if (varname[1] == NUL)
4712 {
4713 /* get all buffer-local options in a dict */
4714 dict_T *opts = get_winbuf_options(TRUE);
4715
4716 if (opts != NULL)
4717 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004718 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004719 done = TRUE;
4720 }
4721 }
4722 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4723 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004724 done = TRUE;
4725 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004726 else
4727 {
4728 /* Look up the variable. */
4729 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4730 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4731 'b', varname, FALSE);
4732 if (v != NULL)
4733 {
4734 copy_tv(&v->di_tv, rettv);
4735 done = TRUE;
4736 }
4737 }
4738
4739 /* restore previous notion of curbuf */
4740 curbuf = save_curbuf;
4741 }
4742
4743 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4744 /* use the default value */
4745 copy_tv(&argvars[2], rettv);
4746
4747 --emsg_off;
4748}
4749
4750/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004751 * "getchangelist()" function
4752 */
4753 static void
4754f_getchangelist(typval_T *argvars, typval_T *rettv)
4755{
4756#ifdef FEAT_JUMPLIST
4757 buf_T *buf;
4758 int i;
4759 list_T *l;
4760 dict_T *d;
4761#endif
4762
4763 if (rettv_list_alloc(rettv) != OK)
4764 return;
4765
4766#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004767 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004768 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004769 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004770 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004771 if (buf == NULL)
4772 return;
4773
4774 l = list_alloc();
4775 if (l == NULL)
4776 return;
4777
4778 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4779 return;
4780 /*
4781 * The current window change list index tracks only the position in the
4782 * current buffer change list. For other buffers, use the change list
4783 * length as the current index.
4784 */
4785 list_append_number(rettv->vval.v_list,
4786 (varnumber_T)((buf == curwin->w_buffer)
4787 ? curwin->w_changelistidx : buf->b_changelistlen));
4788
4789 for (i = 0; i < buf->b_changelistlen; ++i)
4790 {
4791 if (buf->b_changelist[i].lnum == 0)
4792 continue;
4793 if ((d = dict_alloc()) == NULL)
4794 return;
4795 if (list_append_dict(l, d) == FAIL)
4796 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004797 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4798 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004799 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004800 }
4801#endif
4802}
4803/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004804 * "getchar()" function
4805 */
4806 static void
4807f_getchar(typval_T *argvars, typval_T *rettv)
4808{
4809 varnumber_T n;
4810 int error = FALSE;
4811
Bram Moolenaar84d93902018-09-11 20:10:20 +02004812#ifdef MESSAGE_QUEUE
4813 // vpeekc() used to check for messages, but that caused problems, invoking
4814 // a callback where it was not expected. Some plugins use getchar(1) in a
4815 // loop to await a message, therefore make sure we check for messages here.
4816 parse_queued_messages();
4817#endif
4818
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004819 /* Position the cursor. Needed after a message that ends in a space. */
4820 windgoto(msg_row, msg_col);
4821
4822 ++no_mapping;
4823 ++allow_keys;
4824 for (;;)
4825 {
4826 if (argvars[0].v_type == VAR_UNKNOWN)
4827 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004828 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004829 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004830 /* getchar(1): only check if char avail */
4831 n = vpeekc_any();
4832 else if (error || vpeekc_any() == NUL)
4833 /* illegal argument or getchar(0) and no char avail: return zero */
4834 n = 0;
4835 else
4836 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004837 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004838
4839 if (n == K_IGNORE)
4840 continue;
4841 break;
4842 }
4843 --no_mapping;
4844 --allow_keys;
4845
4846 set_vim_var_nr(VV_MOUSE_WIN, 0);
4847 set_vim_var_nr(VV_MOUSE_WINID, 0);
4848 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4849 set_vim_var_nr(VV_MOUSE_COL, 0);
4850
4851 rettv->vval.v_number = n;
4852 if (IS_SPECIAL(n) || mod_mask != 0)
4853 {
4854 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4855 int i = 0;
4856
4857 /* Turn a special key into three bytes, plus modifier. */
4858 if (mod_mask != 0)
4859 {
4860 temp[i++] = K_SPECIAL;
4861 temp[i++] = KS_MODIFIER;
4862 temp[i++] = mod_mask;
4863 }
4864 if (IS_SPECIAL(n))
4865 {
4866 temp[i++] = K_SPECIAL;
4867 temp[i++] = K_SECOND(n);
4868 temp[i++] = K_THIRD(n);
4869 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004870 else if (has_mbyte)
4871 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004872 else
4873 temp[i++] = n;
4874 temp[i++] = NUL;
4875 rettv->v_type = VAR_STRING;
4876 rettv->vval.v_string = vim_strsave(temp);
4877
4878#ifdef FEAT_MOUSE
4879 if (is_mouse_key(n))
4880 {
4881 int row = mouse_row;
4882 int col = mouse_col;
4883 win_T *win;
4884 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004885 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004886 int winnr = 1;
4887
4888 if (row >= 0 && col >= 0)
4889 {
4890 /* Find the window at the mouse coordinates and compute the
4891 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004892 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004893 if (win == NULL)
4894 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004895 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004896# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004897 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004898 winnr = 0;
4899 else
4900# endif
4901 for (wp = firstwin; wp != win && wp != NULL;
4902 wp = wp->w_next)
4903 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004904 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4905 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4906 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4907 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4908 }
4909 }
4910#endif
4911 }
4912}
4913
4914/*
4915 * "getcharmod()" function
4916 */
4917 static void
4918f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4919{
4920 rettv->vval.v_number = mod_mask;
4921}
4922
4923/*
4924 * "getcharsearch()" function
4925 */
4926 static void
4927f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4928{
4929 if (rettv_dict_alloc(rettv) != FAIL)
4930 {
4931 dict_T *dict = rettv->vval.v_dict;
4932
Bram Moolenaare0be1672018-07-08 16:50:37 +02004933 dict_add_string(dict, "char", last_csearch());
4934 dict_add_number(dict, "forward", last_csearch_forward());
4935 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004936 }
4937}
4938
4939/*
4940 * "getcmdline()" function
4941 */
4942 static void
4943f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4944{
4945 rettv->v_type = VAR_STRING;
4946 rettv->vval.v_string = get_cmdline_str();
4947}
4948
4949/*
4950 * "getcmdpos()" function
4951 */
4952 static void
4953f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4954{
4955 rettv->vval.v_number = get_cmdline_pos() + 1;
4956}
4957
4958/*
4959 * "getcmdtype()" function
4960 */
4961 static void
4962f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4963{
4964 rettv->v_type = VAR_STRING;
4965 rettv->vval.v_string = alloc(2);
4966 if (rettv->vval.v_string != NULL)
4967 {
4968 rettv->vval.v_string[0] = get_cmdline_type();
4969 rettv->vval.v_string[1] = NUL;
4970 }
4971}
4972
4973/*
4974 * "getcmdwintype()" function
4975 */
4976 static void
4977f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4978{
4979 rettv->v_type = VAR_STRING;
4980 rettv->vval.v_string = NULL;
4981#ifdef FEAT_CMDWIN
4982 rettv->vval.v_string = alloc(2);
4983 if (rettv->vval.v_string != NULL)
4984 {
4985 rettv->vval.v_string[0] = cmdwin_type;
4986 rettv->vval.v_string[1] = NUL;
4987 }
4988#endif
4989}
4990
4991#if defined(FEAT_CMDL_COMPL)
4992/*
4993 * "getcompletion()" function
4994 */
4995 static void
4996f_getcompletion(typval_T *argvars, typval_T *rettv)
4997{
4998 char_u *pat;
4999 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005000 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005001 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5002 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005003
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005004 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005005 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005006
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005007 if (p_wic)
5008 options |= WILD_ICASE;
5009
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005010 /* For filtered results, 'wildignore' is used */
5011 if (!filtered)
5012 options |= WILD_KEEP_ALL;
5013
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005014 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005015 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005016 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005017 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005018 if (xpc.xp_context == EXPAND_NOTHING)
5019 {
5020 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005021 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005022 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005023 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005024 return;
5025 }
5026
5027# if defined(FEAT_MENU)
5028 if (xpc.xp_context == EXPAND_MENUS)
5029 {
5030 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5031 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5032 }
5033# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005034#ifdef FEAT_CSCOPE
5035 if (xpc.xp_context == EXPAND_CSCOPE)
5036 {
5037 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5038 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5039 }
5040#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005041#ifdef FEAT_SIGNS
5042 if (xpc.xp_context == EXPAND_SIGN)
5043 {
5044 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5045 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5046 }
5047#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005048
5049 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5050 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5051 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005052 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005053
5054 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5055
5056 for (i = 0; i < xpc.xp_numfiles; i++)
5057 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5058 }
5059 vim_free(pat);
5060 ExpandCleanup(&xpc);
5061}
5062#endif
5063
5064/*
5065 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005066 *
5067 * Return the current working directory of a window in a tab page.
5068 * First optional argument 'winnr' is the window number or -1 and the second
5069 * optional argument 'tabnr' is the tab page number.
5070 *
5071 * If no arguments are supplied, then return the directory of the current
5072 * window.
5073 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5074 * the specified window.
5075 * If 'winnr' is 0 then return the directory of the current window.
5076 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5077 * directory of the specified tab page. Otherwise return the directory of the
5078 * specified window in the specified tab page.
5079 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005080 */
5081 static void
5082f_getcwd(typval_T *argvars, typval_T *rettv)
5083{
5084 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005085 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005086 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005087 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005088
5089 rettv->v_type = VAR_STRING;
5090 rettv->vval.v_string = NULL;
5091
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005092 if (argvars[0].v_type == VAR_NUMBER
5093 && argvars[0].vval.v_number == -1
5094 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005095 global = TRUE;
5096 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005097 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005098
5099 if (wp != NULL && wp->w_localdir != NULL)
5100 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005101 else if (tp != NULL && tp->tp_localdir != NULL)
5102 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5103 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005104 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005105 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005106 rettv->vval.v_string = vim_strsave(globaldir);
5107 else
5108 {
5109 cwd = alloc(MAXPATHL);
5110 if (cwd != NULL)
5111 {
5112 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5113 rettv->vval.v_string = vim_strsave(cwd);
5114 vim_free(cwd);
5115 }
5116 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005117 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005118#ifdef BACKSLASH_IN_FILENAME
5119 if (rettv->vval.v_string != NULL)
5120 slash_adjust(rettv->vval.v_string);
5121#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005122}
5123
5124/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005125 * "getenv()" function
5126 */
5127 static void
5128f_getenv(typval_T *argvars, typval_T *rettv)
5129{
5130 int mustfree = FALSE;
5131 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5132
5133 if (p == NULL)
5134 {
5135 rettv->v_type = VAR_SPECIAL;
5136 rettv->vval.v_number = VVAL_NULL;
5137 return;
5138 }
5139 if (!mustfree)
5140 p = vim_strsave(p);
5141 rettv->vval.v_string = p;
5142 rettv->v_type = VAR_STRING;
5143}
5144
5145/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005146 * "getfontname()" function
5147 */
5148 static void
5149f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5150{
5151 rettv->v_type = VAR_STRING;
5152 rettv->vval.v_string = NULL;
5153#ifdef FEAT_GUI
5154 if (gui.in_use)
5155 {
5156 GuiFont font;
5157 char_u *name = NULL;
5158
5159 if (argvars[0].v_type == VAR_UNKNOWN)
5160 {
5161 /* Get the "Normal" font. Either the name saved by
5162 * hl_set_font_name() or from the font ID. */
5163 font = gui.norm_font;
5164 name = hl_get_font_name();
5165 }
5166 else
5167 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005168 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5170 return;
5171 font = gui_mch_get_font(name, FALSE);
5172 if (font == NOFONT)
5173 return; /* Invalid font name, return empty string. */
5174 }
5175 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5176 if (argvars[0].v_type != VAR_UNKNOWN)
5177 gui_mch_free_font(font);
5178 }
5179#endif
5180}
5181
5182/*
5183 * "getfperm({fname})" function
5184 */
5185 static void
5186f_getfperm(typval_T *argvars, typval_T *rettv)
5187{
5188 char_u *fname;
5189 stat_T st;
5190 char_u *perm = NULL;
5191 char_u flags[] = "rwx";
5192 int i;
5193
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005194 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005195
5196 rettv->v_type = VAR_STRING;
5197 if (mch_stat((char *)fname, &st) >= 0)
5198 {
5199 perm = vim_strsave((char_u *)"---------");
5200 if (perm != NULL)
5201 {
5202 for (i = 0; i < 9; i++)
5203 {
5204 if (st.st_mode & (1 << (8 - i)))
5205 perm[i] = flags[i % 3];
5206 }
5207 }
5208 }
5209 rettv->vval.v_string = perm;
5210}
5211
5212/*
5213 * "getfsize({fname})" function
5214 */
5215 static void
5216f_getfsize(typval_T *argvars, typval_T *rettv)
5217{
5218 char_u *fname;
5219 stat_T st;
5220
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005221 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005222
5223 rettv->v_type = VAR_NUMBER;
5224
5225 if (mch_stat((char *)fname, &st) >= 0)
5226 {
5227 if (mch_isdir(fname))
5228 rettv->vval.v_number = 0;
5229 else
5230 {
5231 rettv->vval.v_number = (varnumber_T)st.st_size;
5232
5233 /* non-perfect check for overflow */
5234 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5235 rettv->vval.v_number = -2;
5236 }
5237 }
5238 else
5239 rettv->vval.v_number = -1;
5240}
5241
5242/*
5243 * "getftime({fname})" function
5244 */
5245 static void
5246f_getftime(typval_T *argvars, typval_T *rettv)
5247{
5248 char_u *fname;
5249 stat_T st;
5250
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005251 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005252
5253 if (mch_stat((char *)fname, &st) >= 0)
5254 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5255 else
5256 rettv->vval.v_number = -1;
5257}
5258
5259/*
5260 * "getftype({fname})" function
5261 */
5262 static void
5263f_getftype(typval_T *argvars, typval_T *rettv)
5264{
5265 char_u *fname;
5266 stat_T st;
5267 char_u *type = NULL;
5268 char *t;
5269
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005270 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005271
5272 rettv->v_type = VAR_STRING;
5273 if (mch_lstat((char *)fname, &st) >= 0)
5274 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005275 if (S_ISREG(st.st_mode))
5276 t = "file";
5277 else if (S_ISDIR(st.st_mode))
5278 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005279 else if (S_ISLNK(st.st_mode))
5280 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005281 else if (S_ISBLK(st.st_mode))
5282 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005283 else if (S_ISCHR(st.st_mode))
5284 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005285 else if (S_ISFIFO(st.st_mode))
5286 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005287 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005288 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005289 else
5290 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005291 type = vim_strsave((char_u *)t);
5292 }
5293 rettv->vval.v_string = type;
5294}
5295
5296/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005297 * "getjumplist()" function
5298 */
5299 static void
5300f_getjumplist(typval_T *argvars, typval_T *rettv)
5301{
5302#ifdef FEAT_JUMPLIST
5303 win_T *wp;
5304 int i;
5305 list_T *l;
5306 dict_T *d;
5307#endif
5308
5309 if (rettv_list_alloc(rettv) != OK)
5310 return;
5311
5312#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005313 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005314 if (wp == NULL)
5315 return;
5316
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005317 cleanup_jumplist(wp, TRUE);
5318
Bram Moolenaar4f505882018-02-10 21:06:32 +01005319 l = list_alloc();
5320 if (l == NULL)
5321 return;
5322
5323 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5324 return;
5325 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5326
5327 for (i = 0; i < wp->w_jumplistlen; ++i)
5328 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005329 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5330 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005331 if ((d = dict_alloc()) == NULL)
5332 return;
5333 if (list_append_dict(l, d) == FAIL)
5334 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005335 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5336 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005337 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005338 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005339 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005340 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005341 }
5342#endif
5343}
5344
5345/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005346 * "getline(lnum, [end])" function
5347 */
5348 static void
5349f_getline(typval_T *argvars, typval_T *rettv)
5350{
5351 linenr_T lnum;
5352 linenr_T end;
5353 int retlist;
5354
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005355 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005356 if (argvars[1].v_type == VAR_UNKNOWN)
5357 {
5358 end = 0;
5359 retlist = FALSE;
5360 }
5361 else
5362 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005363 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005364 retlist = TRUE;
5365 }
5366
5367 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5368}
5369
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005370#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005371 static void
5372get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5373{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005374 if (what_arg->v_type == VAR_UNKNOWN)
5375 {
5376 if (rettv_list_alloc(rettv) == OK)
5377 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005378 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005379 }
5380 else
5381 {
5382 if (rettv_dict_alloc(rettv) == OK)
5383 if (is_qf || (wp != NULL))
5384 {
5385 if (what_arg->v_type == VAR_DICT)
5386 {
5387 dict_T *d = what_arg->vval.v_dict;
5388
5389 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005390 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005391 }
5392 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005393 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005394 }
5395 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005396}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005397#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005398
5399/*
5400 * "getloclist()" function
5401 */
5402 static void
5403f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5404{
5405#ifdef FEAT_QUICKFIX
5406 win_T *wp;
5407
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005408 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005409 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5410#endif
5411}
5412
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005413/*
5414 * "getmatches()" function
5415 */
5416 static void
5417f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5418{
5419#ifdef FEAT_SEARCH_EXTRA
5420 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005421 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005422 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005423 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005424
Bram Moolenaaraff74912019-03-30 18:11:49 +01005425 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5426 return;
5427
5428 cur = win->w_match_head;
5429 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005430 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005431 dict = dict_alloc();
5432 if (dict == NULL)
5433 return;
5434 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005435 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005436 /* match added with matchaddpos() */
5437 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005438 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005439 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005440 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005441 list_T *l;
5442
5443 llpos = &cur->pos.pos[i];
5444 if (llpos->lnum == 0)
5445 break;
5446 l = list_alloc();
5447 if (l == NULL)
5448 break;
5449 list_append_number(l, (varnumber_T)llpos->lnum);
5450 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005451 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005452 list_append_number(l, (varnumber_T)llpos->col);
5453 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005454 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005455 sprintf(buf, "pos%d", i + 1);
5456 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005457 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005458 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005459 else
5460 {
5461 dict_add_string(dict, "pattern", cur->pattern);
5462 }
5463 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5464 dict_add_number(dict, "priority", (long)cur->priority);
5465 dict_add_number(dict, "id", (long)cur->id);
5466# if defined(FEAT_CONCEAL)
5467 if (cur->conceal_char)
5468 {
5469 char_u buf[MB_MAXBYTES + 1];
5470
5471 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5472 dict_add_string(dict, "conceal", (char_u *)&buf);
5473 }
5474# endif
5475 list_append_dict(rettv->vval.v_list, dict);
5476 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005477 }
5478#endif
5479}
5480
5481/*
5482 * "getpid()" function
5483 */
5484 static void
5485f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5486{
5487 rettv->vval.v_number = mch_get_pid();
5488}
5489
5490 static void
5491getpos_both(
5492 typval_T *argvars,
5493 typval_T *rettv,
5494 int getcurpos)
5495{
5496 pos_T *fp;
5497 list_T *l;
5498 int fnum = -1;
5499
5500 if (rettv_list_alloc(rettv) == OK)
5501 {
5502 l = rettv->vval.v_list;
5503 if (getcurpos)
5504 fp = &curwin->w_cursor;
5505 else
5506 fp = var2fpos(&argvars[0], TRUE, &fnum);
5507 if (fnum != -1)
5508 list_append_number(l, (varnumber_T)fnum);
5509 else
5510 list_append_number(l, (varnumber_T)0);
5511 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5512 : (varnumber_T)0);
5513 list_append_number(l, (fp != NULL)
5514 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5515 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005516 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005517 (varnumber_T)0);
5518 if (getcurpos)
5519 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005520 int save_set_curswant = curwin->w_set_curswant;
5521 colnr_T save_curswant = curwin->w_curswant;
5522 colnr_T save_virtcol = curwin->w_virtcol;
5523
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005524 update_curswant();
5525 list_append_number(l, curwin->w_curswant == MAXCOL ?
5526 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005527
5528 // Do not change "curswant", as it is unexpected that a get
5529 // function has a side effect.
5530 if (save_set_curswant)
5531 {
5532 curwin->w_set_curswant = save_set_curswant;
5533 curwin->w_curswant = save_curswant;
5534 curwin->w_virtcol = save_virtcol;
5535 curwin->w_valid &= ~VALID_VIRTCOL;
5536 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005537 }
5538 }
5539 else
5540 rettv->vval.v_number = FALSE;
5541}
5542
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005543/*
5544 * "getcurpos()" function
5545 */
5546 static void
5547f_getcurpos(typval_T *argvars, typval_T *rettv)
5548{
5549 getpos_both(argvars, rettv, TRUE);
5550}
5551
5552/*
5553 * "getpos(string)" function
5554 */
5555 static void
5556f_getpos(typval_T *argvars, typval_T *rettv)
5557{
5558 getpos_both(argvars, rettv, FALSE);
5559}
5560
5561/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005562 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005563 */
5564 static void
5565f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5566{
5567#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005568 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005569#endif
5570}
5571
5572/*
5573 * "getreg()" function
5574 */
5575 static void
5576f_getreg(typval_T *argvars, typval_T *rettv)
5577{
5578 char_u *strregname;
5579 int regname;
5580 int arg2 = FALSE;
5581 int return_list = FALSE;
5582 int error = FALSE;
5583
5584 if (argvars[0].v_type != VAR_UNKNOWN)
5585 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005586 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005587 error = strregname == NULL;
5588 if (argvars[1].v_type != VAR_UNKNOWN)
5589 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005590 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005591 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005592 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005593 }
5594 }
5595 else
5596 strregname = get_vim_var_str(VV_REG);
5597
5598 if (error)
5599 return;
5600
5601 regname = (strregname == NULL ? '"' : *strregname);
5602 if (regname == 0)
5603 regname = '"';
5604
5605 if (return_list)
5606 {
5607 rettv->v_type = VAR_LIST;
5608 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5609 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5610 if (rettv->vval.v_list == NULL)
5611 (void)rettv_list_alloc(rettv);
5612 else
5613 ++rettv->vval.v_list->lv_refcount;
5614 }
5615 else
5616 {
5617 rettv->v_type = VAR_STRING;
5618 rettv->vval.v_string = get_reg_contents(regname,
5619 arg2 ? GREG_EXPR_SRC : 0);
5620 }
5621}
5622
5623/*
5624 * "getregtype()" function
5625 */
5626 static void
5627f_getregtype(typval_T *argvars, typval_T *rettv)
5628{
5629 char_u *strregname;
5630 int regname;
5631 char_u buf[NUMBUFLEN + 2];
5632 long reglen = 0;
5633
5634 if (argvars[0].v_type != VAR_UNKNOWN)
5635 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005636 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005637 if (strregname == NULL) /* type error; errmsg already given */
5638 {
5639 rettv->v_type = VAR_STRING;
5640 rettv->vval.v_string = NULL;
5641 return;
5642 }
5643 }
5644 else
5645 /* Default to v:register */
5646 strregname = get_vim_var_str(VV_REG);
5647
5648 regname = (strregname == NULL ? '"' : *strregname);
5649 if (regname == 0)
5650 regname = '"';
5651
5652 buf[0] = NUL;
5653 buf[1] = NUL;
5654 switch (get_reg_type(regname, &reglen))
5655 {
5656 case MLINE: buf[0] = 'V'; break;
5657 case MCHAR: buf[0] = 'v'; break;
5658 case MBLOCK:
5659 buf[0] = Ctrl_V;
5660 sprintf((char *)buf + 1, "%ld", reglen + 1);
5661 break;
5662 }
5663 rettv->v_type = VAR_STRING;
5664 rettv->vval.v_string = vim_strsave(buf);
5665}
5666
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005667/*
5668 * Returns information (variables, options, etc.) about a tab page
5669 * as a dictionary.
5670 */
5671 static dict_T *
5672get_tabpage_info(tabpage_T *tp, int tp_idx)
5673{
5674 win_T *wp;
5675 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005676 list_T *l;
5677
5678 dict = dict_alloc();
5679 if (dict == NULL)
5680 return NULL;
5681
Bram Moolenaare0be1672018-07-08 16:50:37 +02005682 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005683
5684 l = list_alloc();
5685 if (l != NULL)
5686 {
5687 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5688 wp; wp = wp->w_next)
5689 list_append_number(l, (varnumber_T)wp->w_id);
5690 dict_add_list(dict, "windows", l);
5691 }
5692
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005693 /* Make a reference to tabpage variables */
5694 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005695
5696 return dict;
5697}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005698
5699/*
5700 * "gettabinfo()" function
5701 */
5702 static void
5703f_gettabinfo(typval_T *argvars, typval_T *rettv)
5704{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005705 tabpage_T *tp, *tparg = NULL;
5706 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005707 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005708
5709 if (rettv_list_alloc(rettv) != OK)
5710 return;
5711
5712 if (argvars[0].v_type != VAR_UNKNOWN)
5713 {
5714 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005715 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005716 if (tparg == NULL)
5717 return;
5718 }
5719
5720 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005721 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005722 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005723 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005724 if (tparg != NULL && tp != tparg)
5725 continue;
5726 d = get_tabpage_info(tp, tpnr);
5727 if (d != NULL)
5728 list_append_dict(rettv->vval.v_list, d);
5729 if (tparg != NULL)
5730 return;
5731 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005732}
5733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005734/*
5735 * "gettabvar()" function
5736 */
5737 static void
5738f_gettabvar(typval_T *argvars, typval_T *rettv)
5739{
5740 win_T *oldcurwin;
5741 tabpage_T *tp, *oldtabpage;
5742 dictitem_T *v;
5743 char_u *varname;
5744 int done = FALSE;
5745
5746 rettv->v_type = VAR_STRING;
5747 rettv->vval.v_string = NULL;
5748
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005749 varname = tv_get_string_chk(&argvars[1]);
5750 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005751 if (tp != NULL && varname != NULL)
5752 {
5753 /* Set tp to be our tabpage, temporarily. Also set the window to the
5754 * first window in the tabpage, otherwise the window is not valid. */
5755 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005756 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5757 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005758 {
5759 /* look up the variable */
5760 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5761 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5762 if (v != NULL)
5763 {
5764 copy_tv(&v->di_tv, rettv);
5765 done = TRUE;
5766 }
5767 }
5768
5769 /* restore previous notion of curwin */
5770 restore_win(oldcurwin, oldtabpage, TRUE);
5771 }
5772
5773 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5774 copy_tv(&argvars[2], rettv);
5775}
5776
5777/*
5778 * "gettabwinvar()" function
5779 */
5780 static void
5781f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5782{
5783 getwinvar(argvars, rettv, 1);
5784}
5785
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005786/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005787 * "gettagstack()" function
5788 */
5789 static void
5790f_gettagstack(typval_T *argvars, typval_T *rettv)
5791{
5792 win_T *wp = curwin; // default is current window
5793
5794 if (rettv_dict_alloc(rettv) != OK)
5795 return;
5796
5797 if (argvars[0].v_type != VAR_UNKNOWN)
5798 {
5799 wp = find_win_by_nr_or_id(&argvars[0]);
5800 if (wp == NULL)
5801 return;
5802 }
5803
5804 get_tagstack(wp, rettv->vval.v_dict);
5805}
5806
5807/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005808 * Returns information about a window as a dictionary.
5809 */
5810 static dict_T *
5811get_win_info(win_T *wp, short tpnr, short winnr)
5812{
5813 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005814
5815 dict = dict_alloc();
5816 if (dict == NULL)
5817 return NULL;
5818
Bram Moolenaare0be1672018-07-08 16:50:37 +02005819 dict_add_number(dict, "tabnr", tpnr);
5820 dict_add_number(dict, "winnr", winnr);
5821 dict_add_number(dict, "winid", wp->w_id);
5822 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005823 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005824 dict_add_number(dict, "topline", wp->w_topline);
5825 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005826#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005827 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005828#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005829 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005830 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005831 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005832
Bram Moolenaar69905d12017-08-13 18:14:47 +02005833#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005834 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005835#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005836#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005837 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5838 dict_add_number(dict, "loclist",
5839 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005840#endif
5841
Bram Moolenaar30567352016-08-27 21:25:44 +02005842 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005843 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005844
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005845 return dict;
5846}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005847
5848/*
5849 * "getwininfo()" function
5850 */
5851 static void
5852f_getwininfo(typval_T *argvars, typval_T *rettv)
5853{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005854 tabpage_T *tp;
5855 win_T *wp = NULL, *wparg = NULL;
5856 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005857 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005858
5859 if (rettv_list_alloc(rettv) != OK)
5860 return;
5861
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005862 if (argvars[0].v_type != VAR_UNKNOWN)
5863 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005864 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005865 if (wparg == NULL)
5866 return;
5867 }
5868
5869 /* Collect information about either all the windows across all the tab
5870 * pages or one particular window.
5871 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005872 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005873 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005874 tabnr++;
5875 winnr = 0;
5876 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005877 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005878 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005879 if (wparg != NULL && wp != wparg)
5880 continue;
5881 d = get_win_info(wp, tabnr, winnr);
5882 if (d != NULL)
5883 list_append_dict(rettv->vval.v_list, d);
5884 if (wparg != NULL)
5885 /* found information about a specific window */
5886 return;
5887 }
5888 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005889}
5890
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005891/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005892 * "win_execute()" function
5893 */
5894 static void
5895f_win_execute(typval_T *argvars, typval_T *rettv)
5896{
5897 int id = (int)tv_get_number(argvars);
5898 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005899 win_T *save_curwin;
5900 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005901
5902 if (wp != NULL)
5903 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005904 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5905 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005906 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005907 check_cursor();
5908 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005909 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005910 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005911 }
5912}
5913
5914/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005915 * "win_findbuf()" function
5916 */
5917 static void
5918f_win_findbuf(typval_T *argvars, typval_T *rettv)
5919{
5920 if (rettv_list_alloc(rettv) != FAIL)
5921 win_findbuf(argvars, rettv->vval.v_list);
5922}
5923
5924/*
5925 * "win_getid()" function
5926 */
5927 static void
5928f_win_getid(typval_T *argvars, typval_T *rettv)
5929{
5930 rettv->vval.v_number = win_getid(argvars);
5931}
5932
5933/*
5934 * "win_gotoid()" function
5935 */
5936 static void
5937f_win_gotoid(typval_T *argvars, typval_T *rettv)
5938{
5939 rettv->vval.v_number = win_gotoid(argvars);
5940}
5941
5942/*
5943 * "win_id2tabwin()" function
5944 */
5945 static void
5946f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5947{
5948 if (rettv_list_alloc(rettv) != FAIL)
5949 win_id2tabwin(argvars, rettv->vval.v_list);
5950}
5951
5952/*
5953 * "win_id2win()" function
5954 */
5955 static void
5956f_win_id2win(typval_T *argvars, typval_T *rettv)
5957{
5958 rettv->vval.v_number = win_id2win(argvars);
5959}
5960
5961/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005962 * "win_screenpos()" function
5963 */
5964 static void
5965f_win_screenpos(typval_T *argvars, typval_T *rettv)
5966{
5967 win_T *wp;
5968
5969 if (rettv_list_alloc(rettv) == FAIL)
5970 return;
5971
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005972 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005973 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5974 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5975}
5976
5977/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005978 * "getwinpos({timeout})" function
5979 */
5980 static void
5981f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5982{
5983 int x = -1;
5984 int y = -1;
5985
5986 if (rettv_list_alloc(rettv) == FAIL)
5987 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005988#if defined(FEAT_GUI) \
5989 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5990 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005991 {
5992 varnumber_T timeout = 100;
5993
5994 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005995 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005996
5997 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005998 }
5999#endif
6000 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6001 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6002}
6003
6004
6005/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006006 * "getwinposx()" function
6007 */
6008 static void
6009f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6010{
6011 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006012#if defined(FEAT_GUI) \
6013 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6014 || defined(MSWIN)
6015
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006016 {
6017 int x, y;
6018
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006019 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006020 rettv->vval.v_number = x;
6021 }
6022#endif
6023}
6024
6025/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006026 * "getwinposy()" function
6027 */
6028 static void
6029f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6030{
6031 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006032#if defined(FEAT_GUI) \
6033 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6034 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +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 = y;
6040 }
6041#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006042}
6043
6044/*
6045 * "getwinvar()" function
6046 */
6047 static void
6048f_getwinvar(typval_T *argvars, typval_T *rettv)
6049{
6050 getwinvar(argvars, rettv, 0);
6051}
6052
6053/*
6054 * "glob()" function
6055 */
6056 static void
6057f_glob(typval_T *argvars, typval_T *rettv)
6058{
6059 int options = WILD_SILENT|WILD_USE_NL;
6060 expand_T xpc;
6061 int error = FALSE;
6062
6063 /* When the optional second argument is non-zero, don't remove matches
6064 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6065 rettv->v_type = VAR_STRING;
6066 if (argvars[1].v_type != VAR_UNKNOWN)
6067 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006068 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006069 options |= WILD_KEEP_ALL;
6070 if (argvars[2].v_type != VAR_UNKNOWN)
6071 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006072 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006073 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006074 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006075 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006076 options |= WILD_ALLLINKS;
6077 }
6078 }
6079 if (!error)
6080 {
6081 ExpandInit(&xpc);
6082 xpc.xp_context = EXPAND_FILES;
6083 if (p_wic)
6084 options += WILD_ICASE;
6085 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006086 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006087 NULL, options, WILD_ALL);
6088 else if (rettv_list_alloc(rettv) != FAIL)
6089 {
6090 int i;
6091
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006092 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006093 NULL, options, WILD_ALL_KEEP);
6094 for (i = 0; i < xpc.xp_numfiles; i++)
6095 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6096
6097 ExpandCleanup(&xpc);
6098 }
6099 }
6100 else
6101 rettv->vval.v_string = NULL;
6102}
6103
6104/*
6105 * "globpath()" function
6106 */
6107 static void
6108f_globpath(typval_T *argvars, typval_T *rettv)
6109{
6110 int flags = 0;
6111 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006112 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006113 int error = FALSE;
6114 garray_T ga;
6115 int i;
6116
6117 /* When the optional second argument is non-zero, don't remove matches
6118 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6119 rettv->v_type = VAR_STRING;
6120 if (argvars[2].v_type != VAR_UNKNOWN)
6121 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006122 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006123 flags |= WILD_KEEP_ALL;
6124 if (argvars[3].v_type != VAR_UNKNOWN)
6125 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006126 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006127 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006128 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006129 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006130 flags |= WILD_ALLLINKS;
6131 }
6132 }
6133 if (file != NULL && !error)
6134 {
6135 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006136 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006137 if (rettv->v_type == VAR_STRING)
6138 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6139 else if (rettv_list_alloc(rettv) != FAIL)
6140 for (i = 0; i < ga.ga_len; ++i)
6141 list_append_string(rettv->vval.v_list,
6142 ((char_u **)(ga.ga_data))[i], -1);
6143 ga_clear_strings(&ga);
6144 }
6145 else
6146 rettv->vval.v_string = NULL;
6147}
6148
6149/*
6150 * "glob2regpat()" function
6151 */
6152 static void
6153f_glob2regpat(typval_T *argvars, typval_T *rettv)
6154{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006155 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006156
6157 rettv->v_type = VAR_STRING;
6158 rettv->vval.v_string = (pat == NULL)
6159 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6160}
6161
6162/* for VIM_VERSION_ defines */
6163#include "version.h"
6164
6165/*
6166 * "has()" function
6167 */
6168 static void
6169f_has(typval_T *argvars, typval_T *rettv)
6170{
6171 int i;
6172 char_u *name;
6173 int n = FALSE;
6174 static char *(has_list[]) =
6175 {
6176#ifdef AMIGA
6177 "amiga",
6178# ifdef FEAT_ARP
6179 "arp",
6180# endif
6181#endif
6182#ifdef __BEOS__
6183 "beos",
6184#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006185#if defined(BSD) && !defined(MACOS_X)
6186 "bsd",
6187#endif
6188#ifdef hpux
6189 "hpux",
6190#endif
6191#ifdef __linux__
6192 "linux",
6193#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006194#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006195 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6196 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006197# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006198 "macunix", /* Mac OS X, with the darwin feature */
6199 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006200# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006201#endif
6202#ifdef __QNX__
6203 "qnx",
6204#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006205#ifdef SUN_SYSTEM
6206 "sun",
6207#else
6208 "moon",
6209#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006210#ifdef UNIX
6211 "unix",
6212#endif
6213#ifdef VMS
6214 "vms",
6215#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006216#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006217 "win32",
6218#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006219#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220 "win32unix",
6221#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006222#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006223 "win64",
6224#endif
6225#ifdef EBCDIC
6226 "ebcdic",
6227#endif
6228#ifndef CASE_INSENSITIVE_FILENAME
6229 "fname_case",
6230#endif
6231#ifdef HAVE_ACL
6232 "acl",
6233#endif
6234#ifdef FEAT_ARABIC
6235 "arabic",
6236#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006237 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006238#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006239 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006240#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006241#ifdef FEAT_AUTOSERVERNAME
6242 "autoservername",
6243#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006244#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006245 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006246# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247 "balloon_multiline",
6248# endif
6249#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006250#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006251 "balloon_eval_term",
6252#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006253#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6254 "builtin_terms",
6255# ifdef ALL_BUILTIN_TCAPS
6256 "all_builtin_terms",
6257# endif
6258#endif
6259#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006260 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006261 || defined(FEAT_GUI_MOTIF))
6262 "browsefilter",
6263#endif
6264#ifdef FEAT_BYTEOFF
6265 "byte_offset",
6266#endif
6267#ifdef FEAT_JOB_CHANNEL
6268 "channel",
6269#endif
6270#ifdef FEAT_CINDENT
6271 "cindent",
6272#endif
6273#ifdef FEAT_CLIENTSERVER
6274 "clientserver",
6275#endif
6276#ifdef FEAT_CLIPBOARD
6277 "clipboard",
6278#endif
6279#ifdef FEAT_CMDL_COMPL
6280 "cmdline_compl",
6281#endif
6282#ifdef FEAT_CMDHIST
6283 "cmdline_hist",
6284#endif
6285#ifdef FEAT_COMMENTS
6286 "comments",
6287#endif
6288#ifdef FEAT_CONCEAL
6289 "conceal",
6290#endif
6291#ifdef FEAT_CRYPT
6292 "cryptv",
6293 "crypt-blowfish",
6294 "crypt-blowfish2",
6295#endif
6296#ifdef FEAT_CSCOPE
6297 "cscope",
6298#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006299 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006300#ifdef CURSOR_SHAPE
6301 "cursorshape",
6302#endif
6303#ifdef DEBUG
6304 "debug",
6305#endif
6306#ifdef FEAT_CON_DIALOG
6307 "dialog_con",
6308#endif
6309#ifdef FEAT_GUI_DIALOG
6310 "dialog_gui",
6311#endif
6312#ifdef FEAT_DIFF
6313 "diff",
6314#endif
6315#ifdef FEAT_DIGRAPHS
6316 "digraphs",
6317#endif
6318#ifdef FEAT_DIRECTX
6319 "directx",
6320#endif
6321#ifdef FEAT_DND
6322 "dnd",
6323#endif
6324#ifdef FEAT_EMACS_TAGS
6325 "emacs_tags",
6326#endif
6327 "eval", /* always present, of course! */
6328 "ex_extra", /* graduated feature */
6329#ifdef FEAT_SEARCH_EXTRA
6330 "extra_search",
6331#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006332#ifdef FEAT_SEARCHPATH
6333 "file_in_path",
6334#endif
6335#ifdef FEAT_FILTERPIPE
6336 "filterpipe",
6337#endif
6338#ifdef FEAT_FIND_ID
6339 "find_in_path",
6340#endif
6341#ifdef FEAT_FLOAT
6342 "float",
6343#endif
6344#ifdef FEAT_FOLDING
6345 "folding",
6346#endif
6347#ifdef FEAT_FOOTER
6348 "footer",
6349#endif
6350#if !defined(USE_SYSTEM) && defined(UNIX)
6351 "fork",
6352#endif
6353#ifdef FEAT_GETTEXT
6354 "gettext",
6355#endif
6356#ifdef FEAT_GUI
6357 "gui",
6358#endif
6359#ifdef FEAT_GUI_ATHENA
6360# ifdef FEAT_GUI_NEXTAW
6361 "gui_neXtaw",
6362# else
6363 "gui_athena",
6364# endif
6365#endif
6366#ifdef FEAT_GUI_GTK
6367 "gui_gtk",
6368# ifdef USE_GTK3
6369 "gui_gtk3",
6370# else
6371 "gui_gtk2",
6372# endif
6373#endif
6374#ifdef FEAT_GUI_GNOME
6375 "gui_gnome",
6376#endif
6377#ifdef FEAT_GUI_MAC
6378 "gui_mac",
6379#endif
6380#ifdef FEAT_GUI_MOTIF
6381 "gui_motif",
6382#endif
6383#ifdef FEAT_GUI_PHOTON
6384 "gui_photon",
6385#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006386#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006387 "gui_win32",
6388#endif
6389#ifdef FEAT_HANGULIN
6390 "hangul_input",
6391#endif
6392#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6393 "iconv",
6394#endif
6395#ifdef FEAT_INS_EXPAND
6396 "insert_expand",
6397#endif
6398#ifdef FEAT_JOB_CHANNEL
6399 "job",
6400#endif
6401#ifdef FEAT_JUMPLIST
6402 "jumplist",
6403#endif
6404#ifdef FEAT_KEYMAP
6405 "keymap",
6406#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006407 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408#ifdef FEAT_LANGMAP
6409 "langmap",
6410#endif
6411#ifdef FEAT_LIBCALL
6412 "libcall",
6413#endif
6414#ifdef FEAT_LINEBREAK
6415 "linebreak",
6416#endif
6417#ifdef FEAT_LISP
6418 "lispindent",
6419#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006420 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006421#ifdef FEAT_LOCALMAP
6422 "localmap",
6423#endif
6424#ifdef FEAT_LUA
6425# ifndef DYNAMIC_LUA
6426 "lua",
6427# endif
6428#endif
6429#ifdef FEAT_MENU
6430 "menu",
6431#endif
6432#ifdef FEAT_SESSION
6433 "mksession",
6434#endif
6435#ifdef FEAT_MODIFY_FNAME
6436 "modify_fname",
6437#endif
6438#ifdef FEAT_MOUSE
6439 "mouse",
6440#endif
6441#ifdef FEAT_MOUSESHAPE
6442 "mouseshape",
6443#endif
6444#if defined(UNIX) || defined(VMS)
6445# ifdef FEAT_MOUSE_DEC
6446 "mouse_dec",
6447# endif
6448# ifdef FEAT_MOUSE_GPM
6449 "mouse_gpm",
6450# endif
6451# ifdef FEAT_MOUSE_JSB
6452 "mouse_jsbterm",
6453# endif
6454# ifdef FEAT_MOUSE_NET
6455 "mouse_netterm",
6456# endif
6457# ifdef FEAT_MOUSE_PTERM
6458 "mouse_pterm",
6459# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006460# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006461 "mouse_sgr",
6462# endif
6463# ifdef FEAT_SYSMOUSE
6464 "mouse_sysmouse",
6465# endif
6466# ifdef FEAT_MOUSE_URXVT
6467 "mouse_urxvt",
6468# endif
6469# ifdef FEAT_MOUSE_XTERM
6470 "mouse_xterm",
6471# endif
6472#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006473 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006474#ifdef FEAT_MBYTE_IME
6475 "multi_byte_ime",
6476#endif
6477#ifdef FEAT_MULTI_LANG
6478 "multi_lang",
6479#endif
6480#ifdef FEAT_MZSCHEME
6481#ifndef DYNAMIC_MZSCHEME
6482 "mzscheme",
6483#endif
6484#endif
6485#ifdef FEAT_NUM64
6486 "num64",
6487#endif
6488#ifdef FEAT_OLE
6489 "ole",
6490#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006491#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006492 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006493#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006494#ifdef FEAT_PATH_EXTRA
6495 "path_extra",
6496#endif
6497#ifdef FEAT_PERL
6498#ifndef DYNAMIC_PERL
6499 "perl",
6500#endif
6501#endif
6502#ifdef FEAT_PERSISTENT_UNDO
6503 "persistent_undo",
6504#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006505#if defined(FEAT_PYTHON)
6506 "python_compiled",
6507# if defined(DYNAMIC_PYTHON)
6508 "python_dynamic",
6509# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006510 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006511 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006512# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006514#if defined(FEAT_PYTHON3)
6515 "python3_compiled",
6516# if defined(DYNAMIC_PYTHON3)
6517 "python3_dynamic",
6518# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006519 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006520 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006521# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006522#endif
6523#ifdef FEAT_POSTSCRIPT
6524 "postscript",
6525#endif
6526#ifdef FEAT_PRINTER
6527 "printer",
6528#endif
6529#ifdef FEAT_PROFILE
6530 "profile",
6531#endif
6532#ifdef FEAT_RELTIME
6533 "reltime",
6534#endif
6535#ifdef FEAT_QUICKFIX
6536 "quickfix",
6537#endif
6538#ifdef FEAT_RIGHTLEFT
6539 "rightleft",
6540#endif
6541#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6542 "ruby",
6543#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006544 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006545#ifdef FEAT_CMDL_INFO
6546 "showcmd",
6547 "cmdline_info",
6548#endif
6549#ifdef FEAT_SIGNS
6550 "signs",
6551#endif
6552#ifdef FEAT_SMARTINDENT
6553 "smartindent",
6554#endif
6555#ifdef STARTUPTIME
6556 "startuptime",
6557#endif
6558#ifdef FEAT_STL_OPT
6559 "statusline",
6560#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006561#ifdef FEAT_NETBEANS_INTG
6562 "netbeans_intg",
6563#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006564#ifdef FEAT_SOUND
6565 "sound",
6566#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006567#ifdef FEAT_SPELL
6568 "spell",
6569#endif
6570#ifdef FEAT_SYN_HL
6571 "syntax",
6572#endif
6573#if defined(USE_SYSTEM) || !defined(UNIX)
6574 "system",
6575#endif
6576#ifdef FEAT_TAG_BINS
6577 "tag_binary",
6578#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006579#ifdef FEAT_TCL
6580# ifndef DYNAMIC_TCL
6581 "tcl",
6582# endif
6583#endif
6584#ifdef FEAT_TERMGUICOLORS
6585 "termguicolors",
6586#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006587#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006588 "terminal",
6589#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006590#ifdef TERMINFO
6591 "terminfo",
6592#endif
6593#ifdef FEAT_TERMRESPONSE
6594 "termresponse",
6595#endif
6596#ifdef FEAT_TEXTOBJ
6597 "textobjects",
6598#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006599#ifdef FEAT_TEXT_PROP
6600 "textprop",
6601#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006602#ifdef HAVE_TGETENT
6603 "tgetent",
6604#endif
6605#ifdef FEAT_TIMERS
6606 "timers",
6607#endif
6608#ifdef FEAT_TITLE
6609 "title",
6610#endif
6611#ifdef FEAT_TOOLBAR
6612 "toolbar",
6613#endif
6614#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6615 "unnamedplus",
6616#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006617 "user-commands", /* was accidentally included in 5.4 */
6618 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006619#ifdef FEAT_VARTABS
6620 "vartabs",
6621#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006622 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006623#ifdef FEAT_VIMINFO
6624 "viminfo",
6625#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006626 "vimscript-1",
6627 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006628 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006629 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006630 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006631 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006632 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006633#ifdef FEAT_VTP
6634 "vtp",
6635#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006636#ifdef FEAT_WILDIGN
6637 "wildignore",
6638#endif
6639#ifdef FEAT_WILDMENU
6640 "wildmenu",
6641#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006643#ifdef FEAT_WAK
6644 "winaltkeys",
6645#endif
6646#ifdef FEAT_WRITEBACKUP
6647 "writebackup",
6648#endif
6649#ifdef FEAT_XIM
6650 "xim",
6651#endif
6652#ifdef FEAT_XFONTSET
6653 "xfontset",
6654#endif
6655#ifdef FEAT_XPM_W32
6656 "xpm",
6657 "xpm_w32", /* for backward compatibility */
6658#else
6659# if defined(HAVE_XPM)
6660 "xpm",
6661# endif
6662#endif
6663#ifdef USE_XSMP
6664 "xsmp",
6665#endif
6666#ifdef USE_XSMP_INTERACT
6667 "xsmp_interact",
6668#endif
6669#ifdef FEAT_XCLIPBOARD
6670 "xterm_clipboard",
6671#endif
6672#ifdef FEAT_XTERM_SAVE
6673 "xterm_save",
6674#endif
6675#if defined(UNIX) && defined(FEAT_X11)
6676 "X11",
6677#endif
6678 NULL
6679 };
6680
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006681 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006682 for (i = 0; has_list[i] != NULL; ++i)
6683 if (STRICMP(name, has_list[i]) == 0)
6684 {
6685 n = TRUE;
6686 break;
6687 }
6688
6689 if (n == FALSE)
6690 {
6691 if (STRNICMP(name, "patch", 5) == 0)
6692 {
6693 if (name[5] == '-'
6694 && STRLEN(name) >= 11
6695 && vim_isdigit(name[6])
6696 && vim_isdigit(name[8])
6697 && vim_isdigit(name[10]))
6698 {
6699 int major = atoi((char *)name + 6);
6700 int minor = atoi((char *)name + 8);
6701
6702 /* Expect "patch-9.9.01234". */
6703 n = (major < VIM_VERSION_MAJOR
6704 || (major == VIM_VERSION_MAJOR
6705 && (minor < VIM_VERSION_MINOR
6706 || (minor == VIM_VERSION_MINOR
6707 && has_patch(atoi((char *)name + 10))))));
6708 }
6709 else
6710 n = has_patch(atoi((char *)name + 5));
6711 }
6712 else if (STRICMP(name, "vim_starting") == 0)
6713 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006714 else if (STRICMP(name, "ttyin") == 0)
6715 n = mch_input_isatty();
6716 else if (STRICMP(name, "ttyout") == 0)
6717 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006718 else if (STRICMP(name, "multi_byte_encoding") == 0)
6719 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006720#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006721 else if (STRICMP(name, "balloon_multiline") == 0)
6722 n = multiline_balloon_available();
6723#endif
6724#ifdef DYNAMIC_TCL
6725 else if (STRICMP(name, "tcl") == 0)
6726 n = tcl_enabled(FALSE);
6727#endif
6728#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6729 else if (STRICMP(name, "iconv") == 0)
6730 n = iconv_enabled(FALSE);
6731#endif
6732#ifdef DYNAMIC_LUA
6733 else if (STRICMP(name, "lua") == 0)
6734 n = lua_enabled(FALSE);
6735#endif
6736#ifdef DYNAMIC_MZSCHEME
6737 else if (STRICMP(name, "mzscheme") == 0)
6738 n = mzscheme_enabled(FALSE);
6739#endif
6740#ifdef DYNAMIC_RUBY
6741 else if (STRICMP(name, "ruby") == 0)
6742 n = ruby_enabled(FALSE);
6743#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006744#ifdef DYNAMIC_PYTHON
6745 else if (STRICMP(name, "python") == 0)
6746 n = python_enabled(FALSE);
6747#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006748#ifdef DYNAMIC_PYTHON3
6749 else if (STRICMP(name, "python3") == 0)
6750 n = python3_enabled(FALSE);
6751#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006752#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6753 else if (STRICMP(name, "pythonx") == 0)
6754 {
6755# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6756 if (p_pyx == 0)
6757 n = python3_enabled(FALSE) || python_enabled(FALSE);
6758 else if (p_pyx == 3)
6759 n = python3_enabled(FALSE);
6760 else if (p_pyx == 2)
6761 n = python_enabled(FALSE);
6762# elif defined(DYNAMIC_PYTHON)
6763 n = python_enabled(FALSE);
6764# elif defined(DYNAMIC_PYTHON3)
6765 n = python3_enabled(FALSE);
6766# endif
6767 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006768#endif
6769#ifdef DYNAMIC_PERL
6770 else if (STRICMP(name, "perl") == 0)
6771 n = perl_enabled(FALSE);
6772#endif
6773#ifdef FEAT_GUI
6774 else if (STRICMP(name, "gui_running") == 0)
6775 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006776# ifdef FEAT_BROWSE
6777 else if (STRICMP(name, "browse") == 0)
6778 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6779# endif
6780#endif
6781#ifdef FEAT_SYN_HL
6782 else if (STRICMP(name, "syntax_items") == 0)
6783 n = syntax_present(curwin);
6784#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006785#ifdef FEAT_VTP
6786 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006787 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006788#endif
6789#ifdef FEAT_NETBEANS_INTG
6790 else if (STRICMP(name, "netbeans_enabled") == 0)
6791 n = netbeans_active();
6792#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006793#ifdef FEAT_MOUSE_GPM
6794 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6795 n = gpm_enabled();
6796#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006797#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006798 else if (STRICMP(name, "terminal") == 0)
6799 n = terminal_enabled();
6800#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006801#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006802 else if (STRICMP(name, "conpty") == 0)
6803 n = use_conpty();
6804#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006805 }
6806
6807 rettv->vval.v_number = n;
6808}
6809
6810/*
6811 * "has_key()" function
6812 */
6813 static void
6814f_has_key(typval_T *argvars, typval_T *rettv)
6815{
6816 if (argvars[0].v_type != VAR_DICT)
6817 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006818 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006819 return;
6820 }
6821 if (argvars[0].vval.v_dict == NULL)
6822 return;
6823
6824 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006825 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006826}
6827
6828/*
6829 * "haslocaldir()" function
6830 */
6831 static void
6832f_haslocaldir(typval_T *argvars, typval_T *rettv)
6833{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006834 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006835 win_T *wp = NULL;
6836
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006837 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6838
6839 // Check for window-local and tab-local directories
6840 if (wp != NULL && wp->w_localdir != NULL)
6841 rettv->vval.v_number = 1;
6842 else if (tp != NULL && tp->tp_localdir != NULL)
6843 rettv->vval.v_number = 2;
6844 else
6845 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006846}
6847
6848/*
6849 * "hasmapto()" function
6850 */
6851 static void
6852f_hasmapto(typval_T *argvars, typval_T *rettv)
6853{
6854 char_u *name;
6855 char_u *mode;
6856 char_u buf[NUMBUFLEN];
6857 int abbr = FALSE;
6858
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006859 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006860 if (argvars[1].v_type == VAR_UNKNOWN)
6861 mode = (char_u *)"nvo";
6862 else
6863 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006864 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006865 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006866 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006867 }
6868
6869 if (map_to_exists(name, mode, abbr))
6870 rettv->vval.v_number = TRUE;
6871 else
6872 rettv->vval.v_number = FALSE;
6873}
6874
6875/*
6876 * "histadd()" function
6877 */
6878 static void
6879f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6880{
6881#ifdef FEAT_CMDHIST
6882 int histype;
6883 char_u *str;
6884 char_u buf[NUMBUFLEN];
6885#endif
6886
6887 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006888 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006889 return;
6890#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006891 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006892 histype = str != NULL ? get_histtype(str) : -1;
6893 if (histype >= 0)
6894 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006895 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006896 if (*str != NUL)
6897 {
6898 init_history();
6899 add_to_history(histype, str, FALSE, NUL);
6900 rettv->vval.v_number = TRUE;
6901 return;
6902 }
6903 }
6904#endif
6905}
6906
6907/*
6908 * "histdel()" function
6909 */
6910 static void
6911f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6912{
6913#ifdef FEAT_CMDHIST
6914 int n;
6915 char_u buf[NUMBUFLEN];
6916 char_u *str;
6917
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006918 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006919 if (str == NULL)
6920 n = 0;
6921 else if (argvars[1].v_type == VAR_UNKNOWN)
6922 /* only one argument: clear entire history */
6923 n = clr_history(get_histtype(str));
6924 else if (argvars[1].v_type == VAR_NUMBER)
6925 /* index given: remove that entry */
6926 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006927 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006928 else
6929 /* string given: remove all matching entries */
6930 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006931 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006932 rettv->vval.v_number = n;
6933#endif
6934}
6935
6936/*
6937 * "histget()" function
6938 */
6939 static void
6940f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6941{
6942#ifdef FEAT_CMDHIST
6943 int type;
6944 int idx;
6945 char_u *str;
6946
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006947 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006948 if (str == NULL)
6949 rettv->vval.v_string = NULL;
6950 else
6951 {
6952 type = get_histtype(str);
6953 if (argvars[1].v_type == VAR_UNKNOWN)
6954 idx = get_history_idx(type);
6955 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006956 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006957 /* -1 on type error */
6958 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6959 }
6960#else
6961 rettv->vval.v_string = NULL;
6962#endif
6963 rettv->v_type = VAR_STRING;
6964}
6965
6966/*
6967 * "histnr()" function
6968 */
6969 static void
6970f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6971{
6972 int i;
6973
6974#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006975 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006976
6977 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6978 if (i >= HIST_CMD && i < HIST_COUNT)
6979 i = get_history_idx(i);
6980 else
6981#endif
6982 i = -1;
6983 rettv->vval.v_number = i;
6984}
6985
6986/*
6987 * "highlightID(name)" function
6988 */
6989 static void
6990f_hlID(typval_T *argvars, typval_T *rettv)
6991{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006992 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006993}
6994
6995/*
6996 * "highlight_exists()" function
6997 */
6998 static void
6999f_hlexists(typval_T *argvars, typval_T *rettv)
7000{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007001 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007002}
7003
7004/*
7005 * "hostname()" function
7006 */
7007 static void
7008f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7009{
7010 char_u hostname[256];
7011
7012 mch_get_host_name(hostname, 256);
7013 rettv->v_type = VAR_STRING;
7014 rettv->vval.v_string = vim_strsave(hostname);
7015}
7016
7017/*
7018 * iconv() function
7019 */
7020 static void
7021f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7022{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023 char_u buf1[NUMBUFLEN];
7024 char_u buf2[NUMBUFLEN];
7025 char_u *from, *to, *str;
7026 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027
7028 rettv->v_type = VAR_STRING;
7029 rettv->vval.v_string = NULL;
7030
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007031 str = tv_get_string(&argvars[0]);
7032 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7033 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007034 vimconv.vc_type = CONV_NONE;
7035 convert_setup(&vimconv, from, to);
7036
7037 /* If the encodings are equal, no conversion needed. */
7038 if (vimconv.vc_type == CONV_NONE)
7039 rettv->vval.v_string = vim_strsave(str);
7040 else
7041 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7042
7043 convert_setup(&vimconv, NULL, NULL);
7044 vim_free(from);
7045 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046}
7047
7048/*
7049 * "indent()" function
7050 */
7051 static void
7052f_indent(typval_T *argvars, typval_T *rettv)
7053{
7054 linenr_T lnum;
7055
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007056 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007057 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7058 rettv->vval.v_number = get_indent_lnum(lnum);
7059 else
7060 rettv->vval.v_number = -1;
7061}
7062
7063/*
7064 * "index()" function
7065 */
7066 static void
7067f_index(typval_T *argvars, typval_T *rettv)
7068{
7069 list_T *l;
7070 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007071 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007072 long idx = 0;
7073 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007074 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007075
7076 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007077 if (argvars[0].v_type == VAR_BLOB)
7078 {
7079 typval_T tv;
7080 int start = 0;
7081
7082 if (argvars[2].v_type != VAR_UNKNOWN)
7083 {
7084 start = tv_get_number_chk(&argvars[2], &error);
7085 if (error)
7086 return;
7087 }
7088 b = argvars[0].vval.v_blob;
7089 if (b == NULL)
7090 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007091 if (start < 0)
7092 {
7093 start = blob_len(b) + start;
7094 if (start < 0)
7095 start = 0;
7096 }
7097
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007098 for (idx = start; idx < blob_len(b); ++idx)
7099 {
7100 tv.v_type = VAR_NUMBER;
7101 tv.vval.v_number = blob_get(b, idx);
7102 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7103 {
7104 rettv->vval.v_number = idx;
7105 return;
7106 }
7107 }
7108 return;
7109 }
7110 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007111 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007112 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007113 return;
7114 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007115
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007116 l = argvars[0].vval.v_list;
7117 if (l != NULL)
7118 {
7119 item = l->lv_first;
7120 if (argvars[2].v_type != VAR_UNKNOWN)
7121 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007122 /* Start at specified item. Use the cached index that list_find()
7123 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007124 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007125 idx = l->lv_idx;
7126 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007127 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007128 if (error)
7129 item = NULL;
7130 }
7131
7132 for ( ; item != NULL; item = item->li_next, ++idx)
7133 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7134 {
7135 rettv->vval.v_number = idx;
7136 break;
7137 }
7138 }
7139}
7140
7141static int inputsecret_flag = 0;
7142
7143/*
7144 * "input()" function
7145 * Also handles inputsecret() when inputsecret is set.
7146 */
7147 static void
7148f_input(typval_T *argvars, typval_T *rettv)
7149{
7150 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7151}
7152
7153/*
7154 * "inputdialog()" function
7155 */
7156 static void
7157f_inputdialog(typval_T *argvars, typval_T *rettv)
7158{
7159#if defined(FEAT_GUI_TEXTDIALOG)
7160 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7161 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7162 {
7163 char_u *message;
7164 char_u buf[NUMBUFLEN];
7165 char_u *defstr = (char_u *)"";
7166
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007167 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007168 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007169 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007170 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7171 else
7172 IObuff[0] = NUL;
7173 if (message != NULL && defstr != NULL
7174 && do_dialog(VIM_QUESTION, NULL, message,
7175 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7176 rettv->vval.v_string = vim_strsave(IObuff);
7177 else
7178 {
7179 if (message != NULL && defstr != NULL
7180 && argvars[1].v_type != VAR_UNKNOWN
7181 && argvars[2].v_type != VAR_UNKNOWN)
7182 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007183 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007184 else
7185 rettv->vval.v_string = NULL;
7186 }
7187 rettv->v_type = VAR_STRING;
7188 }
7189 else
7190#endif
7191 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7192}
7193
7194/*
7195 * "inputlist()" function
7196 */
7197 static void
7198f_inputlist(typval_T *argvars, typval_T *rettv)
7199{
7200 listitem_T *li;
7201 int selected;
7202 int mouse_used;
7203
7204#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007205 /* While starting up, there is no place to enter text. When running tests
7206 * with --not-a-term we assume feedkeys() will be used. */
7207 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007208 return;
7209#endif
7210 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007212 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007213 return;
7214 }
7215
7216 msg_start();
7217 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7218 lines_left = Rows; /* avoid more prompt */
7219 msg_scroll = TRUE;
7220 msg_clr_eos();
7221
7222 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7223 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007224 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007225 msg_putchar('\n');
7226 }
7227
7228 /* Ask for choice. */
7229 selected = prompt_for_number(&mouse_used);
7230 if (mouse_used)
7231 selected -= lines_left;
7232
7233 rettv->vval.v_number = selected;
7234}
7235
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007236static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7237
7238/*
7239 * "inputrestore()" function
7240 */
7241 static void
7242f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7243{
7244 if (ga_userinput.ga_len > 0)
7245 {
7246 --ga_userinput.ga_len;
7247 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7248 + ga_userinput.ga_len);
7249 /* default return is zero == OK */
7250 }
7251 else if (p_verbose > 1)
7252 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007253 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007254 rettv->vval.v_number = 1; /* Failed */
7255 }
7256}
7257
7258/*
7259 * "inputsave()" function
7260 */
7261 static void
7262f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7263{
7264 /* Add an entry to the stack of typeahead storage. */
7265 if (ga_grow(&ga_userinput, 1) == OK)
7266 {
7267 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7268 + ga_userinput.ga_len);
7269 ++ga_userinput.ga_len;
7270 /* default return is zero == OK */
7271 }
7272 else
7273 rettv->vval.v_number = 1; /* Failed */
7274}
7275
7276/*
7277 * "inputsecret()" function
7278 */
7279 static void
7280f_inputsecret(typval_T *argvars, typval_T *rettv)
7281{
7282 ++cmdline_star;
7283 ++inputsecret_flag;
7284 f_input(argvars, rettv);
7285 --cmdline_star;
7286 --inputsecret_flag;
7287}
7288
7289/*
7290 * "insert()" function
7291 */
7292 static void
7293f_insert(typval_T *argvars, typval_T *rettv)
7294{
7295 long before = 0;
7296 listitem_T *item;
7297 list_T *l;
7298 int error = FALSE;
7299
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007300 if (argvars[0].v_type == VAR_BLOB)
7301 {
7302 int val, len;
7303 char_u *p;
7304
7305 len = blob_len(argvars[0].vval.v_blob);
7306 if (argvars[2].v_type != VAR_UNKNOWN)
7307 {
7308 before = (long)tv_get_number_chk(&argvars[2], &error);
7309 if (error)
7310 return; // type error; errmsg already given
7311 if (before < 0 || before > len)
7312 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007313 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007314 return;
7315 }
7316 }
7317 val = tv_get_number_chk(&argvars[1], &error);
7318 if (error)
7319 return;
7320 if (val < 0 || val > 255)
7321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007322 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007323 return;
7324 }
7325
7326 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7327 return;
7328 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7329 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7330 *(p + before) = val;
7331 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7332
7333 copy_tv(&argvars[0], rettv);
7334 }
7335 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007336 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007337 else if ((l = argvars[0].vval.v_list) != NULL
7338 && !var_check_lock(l->lv_lock,
7339 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007340 {
7341 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007342 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007343 if (error)
7344 return; /* type error; errmsg already given */
7345
7346 if (before == l->lv_len)
7347 item = NULL;
7348 else
7349 {
7350 item = list_find(l, before);
7351 if (item == NULL)
7352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007353 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007354 l = NULL;
7355 }
7356 }
7357 if (l != NULL)
7358 {
7359 list_insert_tv(l, &argvars[1], item);
7360 copy_tv(&argvars[0], rettv);
7361 }
7362 }
7363}
7364
7365/*
7366 * "invert(expr)" function
7367 */
7368 static void
7369f_invert(typval_T *argvars, typval_T *rettv)
7370{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007371 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007372}
7373
7374/*
7375 * "isdirectory()" function
7376 */
7377 static void
7378f_isdirectory(typval_T *argvars, typval_T *rettv)
7379{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007380 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007381}
7382
7383/*
7384 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7385 * or it refers to a List or Dictionary that is locked.
7386 */
7387 static int
7388tv_islocked(typval_T *tv)
7389{
7390 return (tv->v_lock & VAR_LOCKED)
7391 || (tv->v_type == VAR_LIST
7392 && tv->vval.v_list != NULL
7393 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7394 || (tv->v_type == VAR_DICT
7395 && tv->vval.v_dict != NULL
7396 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7397}
7398
7399/*
7400 * "islocked()" function
7401 */
7402 static void
7403f_islocked(typval_T *argvars, typval_T *rettv)
7404{
7405 lval_T lv;
7406 char_u *end;
7407 dictitem_T *di;
7408
7409 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007410 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007411 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007412 if (end != NULL && lv.ll_name != NULL)
7413 {
7414 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007415 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007416 else
7417 {
7418 if (lv.ll_tv == NULL)
7419 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007420 di = find_var(lv.ll_name, NULL, TRUE);
7421 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007422 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007423 /* Consider a variable locked when:
7424 * 1. the variable itself is locked
7425 * 2. the value of the variable is locked.
7426 * 3. the List or Dict value is locked.
7427 */
7428 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7429 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007430 }
7431 }
7432 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007433 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007434 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007435 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436 else if (lv.ll_list != NULL)
7437 /* List item. */
7438 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7439 else
7440 /* Dictionary item. */
7441 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7442 }
7443 }
7444
7445 clear_lval(&lv);
7446}
7447
7448#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7449/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007450 * "isinf()" function
7451 */
7452 static void
7453f_isinf(typval_T *argvars, typval_T *rettv)
7454{
7455 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7456 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7457}
7458
7459/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007460 * "isnan()" function
7461 */
7462 static void
7463f_isnan(typval_T *argvars, typval_T *rettv)
7464{
7465 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7466 && isnan(argvars[0].vval.v_float);
7467}
7468#endif
7469
7470/*
7471 * "items(dict)" function
7472 */
7473 static void
7474f_items(typval_T *argvars, typval_T *rettv)
7475{
7476 dict_list(argvars, rettv, 2);
7477}
7478
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007479/*
7480 * "join()" function
7481 */
7482 static void
7483f_join(typval_T *argvars, typval_T *rettv)
7484{
7485 garray_T ga;
7486 char_u *sep;
7487
7488 if (argvars[0].v_type != VAR_LIST)
7489 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007490 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007491 return;
7492 }
7493 if (argvars[0].vval.v_list == NULL)
7494 return;
7495 if (argvars[1].v_type == VAR_UNKNOWN)
7496 sep = (char_u *)" ";
7497 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007498 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007499
7500 rettv->v_type = VAR_STRING;
7501
7502 if (sep != NULL)
7503 {
7504 ga_init2(&ga, (int)sizeof(char), 80);
7505 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7506 ga_append(&ga, NUL);
7507 rettv->vval.v_string = (char_u *)ga.ga_data;
7508 }
7509 else
7510 rettv->vval.v_string = NULL;
7511}
7512
7513/*
7514 * "js_decode()" function
7515 */
7516 static void
7517f_js_decode(typval_T *argvars, typval_T *rettv)
7518{
7519 js_read_T reader;
7520
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007521 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007522 reader.js_fill = NULL;
7523 reader.js_used = 0;
7524 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007525 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007526}
7527
7528/*
7529 * "js_encode()" function
7530 */
7531 static void
7532f_js_encode(typval_T *argvars, typval_T *rettv)
7533{
7534 rettv->v_type = VAR_STRING;
7535 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7536}
7537
7538/*
7539 * "json_decode()" function
7540 */
7541 static void
7542f_json_decode(typval_T *argvars, typval_T *rettv)
7543{
7544 js_read_T reader;
7545
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007546 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007547 reader.js_fill = NULL;
7548 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007549 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007550}
7551
7552/*
7553 * "json_encode()" function
7554 */
7555 static void
7556f_json_encode(typval_T *argvars, typval_T *rettv)
7557{
7558 rettv->v_type = VAR_STRING;
7559 rettv->vval.v_string = json_encode(&argvars[0], 0);
7560}
7561
7562/*
7563 * "keys()" function
7564 */
7565 static void
7566f_keys(typval_T *argvars, typval_T *rettv)
7567{
7568 dict_list(argvars, rettv, 0);
7569}
7570
7571/*
7572 * "last_buffer_nr()" function.
7573 */
7574 static void
7575f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7576{
7577 int n = 0;
7578 buf_T *buf;
7579
Bram Moolenaar29323592016-07-24 22:04:11 +02007580 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007581 if (n < buf->b_fnum)
7582 n = buf->b_fnum;
7583
7584 rettv->vval.v_number = n;
7585}
7586
7587/*
7588 * "len()" function
7589 */
7590 static void
7591f_len(typval_T *argvars, typval_T *rettv)
7592{
7593 switch (argvars[0].v_type)
7594 {
7595 case VAR_STRING:
7596 case VAR_NUMBER:
7597 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007598 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007599 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007600 case VAR_BLOB:
7601 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7602 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603 case VAR_LIST:
7604 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7605 break;
7606 case VAR_DICT:
7607 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7608 break;
7609 case VAR_UNKNOWN:
7610 case VAR_SPECIAL:
7611 case VAR_FLOAT:
7612 case VAR_FUNC:
7613 case VAR_PARTIAL:
7614 case VAR_JOB:
7615 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007616 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007617 break;
7618 }
7619}
7620
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007621 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007622libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007623{
7624#ifdef FEAT_LIBCALL
7625 char_u *string_in;
7626 char_u **string_result;
7627 int nr_result;
7628#endif
7629
7630 rettv->v_type = type;
7631 if (type != VAR_NUMBER)
7632 rettv->vval.v_string = NULL;
7633
7634 if (check_restricted() || check_secure())
7635 return;
7636
7637#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007638 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007639 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7640 {
7641 string_in = NULL;
7642 if (argvars[2].v_type == VAR_STRING)
7643 string_in = argvars[2].vval.v_string;
7644 if (type == VAR_NUMBER)
7645 string_result = NULL;
7646 else
7647 string_result = &rettv->vval.v_string;
7648 if (mch_libcall(argvars[0].vval.v_string,
7649 argvars[1].vval.v_string,
7650 string_in,
7651 argvars[2].vval.v_number,
7652 string_result,
7653 &nr_result) == OK
7654 && type == VAR_NUMBER)
7655 rettv->vval.v_number = nr_result;
7656 }
7657#endif
7658}
7659
7660/*
7661 * "libcall()" function
7662 */
7663 static void
7664f_libcall(typval_T *argvars, typval_T *rettv)
7665{
7666 libcall_common(argvars, rettv, VAR_STRING);
7667}
7668
7669/*
7670 * "libcallnr()" function
7671 */
7672 static void
7673f_libcallnr(typval_T *argvars, typval_T *rettv)
7674{
7675 libcall_common(argvars, rettv, VAR_NUMBER);
7676}
7677
7678/*
7679 * "line(string)" function
7680 */
7681 static void
7682f_line(typval_T *argvars, typval_T *rettv)
7683{
7684 linenr_T lnum = 0;
7685 pos_T *fp;
7686 int fnum;
7687
7688 fp = var2fpos(&argvars[0], TRUE, &fnum);
7689 if (fp != NULL)
7690 lnum = fp->lnum;
7691 rettv->vval.v_number = lnum;
7692}
7693
7694/*
7695 * "line2byte(lnum)" function
7696 */
7697 static void
7698f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7699{
7700#ifndef FEAT_BYTEOFF
7701 rettv->vval.v_number = -1;
7702#else
7703 linenr_T lnum;
7704
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007705 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007706 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7707 rettv->vval.v_number = -1;
7708 else
7709 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7710 if (rettv->vval.v_number >= 0)
7711 ++rettv->vval.v_number;
7712#endif
7713}
7714
7715/*
7716 * "lispindent(lnum)" function
7717 */
7718 static void
7719f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7720{
7721#ifdef FEAT_LISP
7722 pos_T pos;
7723 linenr_T lnum;
7724
7725 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007726 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007727 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7728 {
7729 curwin->w_cursor.lnum = lnum;
7730 rettv->vval.v_number = get_lisp_indent();
7731 curwin->w_cursor = pos;
7732 }
7733 else
7734#endif
7735 rettv->vval.v_number = -1;
7736}
7737
7738/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007739 * "list2str()" function
7740 */
7741 static void
7742f_list2str(typval_T *argvars, typval_T *rettv)
7743{
7744 list_T *l;
7745 listitem_T *li;
7746 garray_T ga;
7747 int utf8 = FALSE;
7748
7749 rettv->v_type = VAR_STRING;
7750 rettv->vval.v_string = NULL;
7751 if (argvars[0].v_type != VAR_LIST)
7752 {
7753 emsg(_(e_invarg));
7754 return;
7755 }
7756
7757 l = argvars[0].vval.v_list;
7758 if (l == NULL)
7759 return; // empty list results in empty string
7760
7761 if (argvars[1].v_type != VAR_UNKNOWN)
7762 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7763
7764 ga_init2(&ga, 1, 80);
7765 if (has_mbyte || utf8)
7766 {
7767 char_u buf[MB_MAXBYTES + 1];
7768 int (*char2bytes)(int, char_u *);
7769
7770 if (utf8 || enc_utf8)
7771 char2bytes = utf_char2bytes;
7772 else
7773 char2bytes = mb_char2bytes;
7774
7775 for (li = l->lv_first; li != NULL; li = li->li_next)
7776 {
7777 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7778 ga_concat(&ga, buf);
7779 }
7780 ga_append(&ga, NUL);
7781 }
7782 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7783 {
7784 for (li = l->lv_first; li != NULL; li = li->li_next)
7785 ga_append(&ga, tv_get_number(&li->li_tv));
7786 ga_append(&ga, NUL);
7787 }
7788
7789 rettv->v_type = VAR_STRING;
7790 rettv->vval.v_string = ga.ga_data;
7791}
7792
7793/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007794 * "localtime()" function
7795 */
7796 static void
7797f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7798{
7799 rettv->vval.v_number = (varnumber_T)time(NULL);
7800}
7801
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007802 static void
7803get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7804{
7805 char_u *keys;
7806 char_u *which;
7807 char_u buf[NUMBUFLEN];
7808 char_u *keys_buf = NULL;
7809 char_u *rhs;
7810 int mode;
7811 int abbr = FALSE;
7812 int get_dict = FALSE;
7813 mapblock_T *mp;
7814 int buffer_local;
7815
7816 /* return empty string for failure */
7817 rettv->v_type = VAR_STRING;
7818 rettv->vval.v_string = NULL;
7819
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007820 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007821 if (*keys == NUL)
7822 return;
7823
7824 if (argvars[1].v_type != VAR_UNKNOWN)
7825 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007826 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007827 if (argvars[2].v_type != VAR_UNKNOWN)
7828 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007829 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007830 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007831 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007832 }
7833 }
7834 else
7835 which = (char_u *)"";
7836 if (which == NULL)
7837 return;
7838
7839 mode = get_map_mode(&which, 0);
7840
7841 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7842 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7843 vim_free(keys_buf);
7844
7845 if (!get_dict)
7846 {
7847 /* Return a string. */
7848 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007849 {
7850 if (*rhs == NUL)
7851 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7852 else
7853 rettv->vval.v_string = str2special_save(rhs, FALSE);
7854 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007855
7856 }
7857 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7858 {
7859 /* Return a dictionary. */
7860 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7861 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7862 dict_T *dict = rettv->vval.v_dict;
7863
Bram Moolenaare0be1672018-07-08 16:50:37 +02007864 dict_add_string(dict, "lhs", lhs);
7865 dict_add_string(dict, "rhs", mp->m_orig_str);
7866 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7867 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7868 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007869 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7870 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007871 dict_add_number(dict, "buffer", (long)buffer_local);
7872 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7873 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007874
7875 vim_free(lhs);
7876 vim_free(mapmode);
7877 }
7878}
7879
7880#ifdef FEAT_FLOAT
7881/*
7882 * "log()" function
7883 */
7884 static void
7885f_log(typval_T *argvars, typval_T *rettv)
7886{
7887 float_T f = 0.0;
7888
7889 rettv->v_type = VAR_FLOAT;
7890 if (get_float_arg(argvars, &f) == OK)
7891 rettv->vval.v_float = log(f);
7892 else
7893 rettv->vval.v_float = 0.0;
7894}
7895
7896/*
7897 * "log10()" function
7898 */
7899 static void
7900f_log10(typval_T *argvars, typval_T *rettv)
7901{
7902 float_T f = 0.0;
7903
7904 rettv->v_type = VAR_FLOAT;
7905 if (get_float_arg(argvars, &f) == OK)
7906 rettv->vval.v_float = log10(f);
7907 else
7908 rettv->vval.v_float = 0.0;
7909}
7910#endif
7911
7912#ifdef FEAT_LUA
7913/*
7914 * "luaeval()" function
7915 */
7916 static void
7917f_luaeval(typval_T *argvars, typval_T *rettv)
7918{
7919 char_u *str;
7920 char_u buf[NUMBUFLEN];
7921
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007922 if (check_restricted() || check_secure())
7923 return;
7924
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007925 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007926 do_luaeval(str, argvars + 1, rettv);
7927}
7928#endif
7929
7930/*
7931 * "map()" function
7932 */
7933 static void
7934f_map(typval_T *argvars, typval_T *rettv)
7935{
7936 filter_map(argvars, rettv, TRUE);
7937}
7938
7939/*
7940 * "maparg()" function
7941 */
7942 static void
7943f_maparg(typval_T *argvars, typval_T *rettv)
7944{
7945 get_maparg(argvars, rettv, TRUE);
7946}
7947
7948/*
7949 * "mapcheck()" function
7950 */
7951 static void
7952f_mapcheck(typval_T *argvars, typval_T *rettv)
7953{
7954 get_maparg(argvars, rettv, FALSE);
7955}
7956
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007957typedef enum
7958{
7959 MATCH_END, /* matchend() */
7960 MATCH_MATCH, /* match() */
7961 MATCH_STR, /* matchstr() */
7962 MATCH_LIST, /* matchlist() */
7963 MATCH_POS /* matchstrpos() */
7964} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007965
7966 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007967find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007968{
7969 char_u *str = NULL;
7970 long len = 0;
7971 char_u *expr = NULL;
7972 char_u *pat;
7973 regmatch_T regmatch;
7974 char_u patbuf[NUMBUFLEN];
7975 char_u strbuf[NUMBUFLEN];
7976 char_u *save_cpo;
7977 long start = 0;
7978 long nth = 1;
7979 colnr_T startcol = 0;
7980 int match = 0;
7981 list_T *l = NULL;
7982 listitem_T *li = NULL;
7983 long idx = 0;
7984 char_u *tofree = NULL;
7985
7986 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7987 save_cpo = p_cpo;
7988 p_cpo = (char_u *)"";
7989
7990 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007991 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007993 /* type MATCH_LIST: return empty list when there are no matches.
7994 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007995 if (rettv_list_alloc(rettv) == FAIL)
7996 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007997 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007998 && (list_append_string(rettv->vval.v_list,
7999 (char_u *)"", 0) == FAIL
8000 || list_append_number(rettv->vval.v_list,
8001 (varnumber_T)-1) == FAIL
8002 || list_append_number(rettv->vval.v_list,
8003 (varnumber_T)-1) == FAIL
8004 || list_append_number(rettv->vval.v_list,
8005 (varnumber_T)-1) == FAIL))
8006 {
8007 list_free(rettv->vval.v_list);
8008 rettv->vval.v_list = NULL;
8009 goto theend;
8010 }
8011 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008012 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008013 {
8014 rettv->v_type = VAR_STRING;
8015 rettv->vval.v_string = NULL;
8016 }
8017
8018 if (argvars[0].v_type == VAR_LIST)
8019 {
8020 if ((l = argvars[0].vval.v_list) == NULL)
8021 goto theend;
8022 li = l->lv_first;
8023 }
8024 else
8025 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008026 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008027 len = (long)STRLEN(str);
8028 }
8029
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008030 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008031 if (pat == NULL)
8032 goto theend;
8033
8034 if (argvars[2].v_type != VAR_UNKNOWN)
8035 {
8036 int error = FALSE;
8037
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008038 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008039 if (error)
8040 goto theend;
8041 if (l != NULL)
8042 {
8043 li = list_find(l, start);
8044 if (li == NULL)
8045 goto theend;
8046 idx = l->lv_idx; /* use the cached index */
8047 }
8048 else
8049 {
8050 if (start < 0)
8051 start = 0;
8052 if (start > len)
8053 goto theend;
8054 /* When "count" argument is there ignore matches before "start",
8055 * otherwise skip part of the string. Differs when pattern is "^"
8056 * or "\<". */
8057 if (argvars[3].v_type != VAR_UNKNOWN)
8058 startcol = start;
8059 else
8060 {
8061 str += start;
8062 len -= start;
8063 }
8064 }
8065
8066 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008067 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068 if (error)
8069 goto theend;
8070 }
8071
8072 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8073 if (regmatch.regprog != NULL)
8074 {
8075 regmatch.rm_ic = p_ic;
8076
8077 for (;;)
8078 {
8079 if (l != NULL)
8080 {
8081 if (li == NULL)
8082 {
8083 match = FALSE;
8084 break;
8085 }
8086 vim_free(tofree);
8087 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8088 if (str == NULL)
8089 break;
8090 }
8091
8092 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8093
8094 if (match && --nth <= 0)
8095 break;
8096 if (l == NULL && !match)
8097 break;
8098
8099 /* Advance to just after the match. */
8100 if (l != NULL)
8101 {
8102 li = li->li_next;
8103 ++idx;
8104 }
8105 else
8106 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008107 startcol = (colnr_T)(regmatch.startp[0]
8108 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008109 if (startcol > (colnr_T)len
8110 || str + startcol <= regmatch.startp[0])
8111 {
8112 match = FALSE;
8113 break;
8114 }
8115 }
8116 }
8117
8118 if (match)
8119 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008120 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008121 {
8122 listitem_T *li1 = rettv->vval.v_list->lv_first;
8123 listitem_T *li2 = li1->li_next;
8124 listitem_T *li3 = li2->li_next;
8125 listitem_T *li4 = li3->li_next;
8126
8127 vim_free(li1->li_tv.vval.v_string);
8128 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8129 (int)(regmatch.endp[0] - regmatch.startp[0]));
8130 li3->li_tv.vval.v_number =
8131 (varnumber_T)(regmatch.startp[0] - expr);
8132 li4->li_tv.vval.v_number =
8133 (varnumber_T)(regmatch.endp[0] - expr);
8134 if (l != NULL)
8135 li2->li_tv.vval.v_number = (varnumber_T)idx;
8136 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008137 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008138 {
8139 int i;
8140
8141 /* return list with matched string and submatches */
8142 for (i = 0; i < NSUBEXP; ++i)
8143 {
8144 if (regmatch.endp[i] == NULL)
8145 {
8146 if (list_append_string(rettv->vval.v_list,
8147 (char_u *)"", 0) == FAIL)
8148 break;
8149 }
8150 else if (list_append_string(rettv->vval.v_list,
8151 regmatch.startp[i],
8152 (int)(regmatch.endp[i] - regmatch.startp[i]))
8153 == FAIL)
8154 break;
8155 }
8156 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008157 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158 {
8159 /* return matched string */
8160 if (l != NULL)
8161 copy_tv(&li->li_tv, rettv);
8162 else
8163 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8164 (int)(regmatch.endp[0] - regmatch.startp[0]));
8165 }
8166 else if (l != NULL)
8167 rettv->vval.v_number = idx;
8168 else
8169 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008170 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008171 rettv->vval.v_number =
8172 (varnumber_T)(regmatch.startp[0] - str);
8173 else
8174 rettv->vval.v_number =
8175 (varnumber_T)(regmatch.endp[0] - str);
8176 rettv->vval.v_number += (varnumber_T)(str - expr);
8177 }
8178 }
8179 vim_regfree(regmatch.regprog);
8180 }
8181
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008182theend:
8183 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008184 /* matchstrpos() without a list: drop the second item. */
8185 listitem_remove(rettv->vval.v_list,
8186 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008187 vim_free(tofree);
8188 p_cpo = save_cpo;
8189}
8190
8191/*
8192 * "match()" function
8193 */
8194 static void
8195f_match(typval_T *argvars, typval_T *rettv)
8196{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008197 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008198}
8199
Bram Moolenaar95e51472018-07-28 16:55:56 +02008200#ifdef FEAT_SEARCH_EXTRA
8201 static int
8202matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8203{
8204 dictitem_T *di;
8205
8206 if (tv->v_type != VAR_DICT)
8207 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008208 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008209 return FAIL;
8210 }
8211
8212 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008213 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008214 (char_u *)"conceal", FALSE);
8215
8216 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8217 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008218 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008219 if (*win == NULL)
8220 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008221 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008222 return FAIL;
8223 }
8224 }
8225
8226 return OK;
8227}
8228#endif
8229
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008230/*
8231 * "matchadd()" function
8232 */
8233 static void
8234f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8235{
8236#ifdef FEAT_SEARCH_EXTRA
8237 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008238 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8239 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008240 int prio = 10; /* default priority */
8241 int id = -1;
8242 int error = FALSE;
8243 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008244 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008245
8246 rettv->vval.v_number = -1;
8247
8248 if (grp == NULL || pat == NULL)
8249 return;
8250 if (argvars[2].v_type != VAR_UNKNOWN)
8251 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008252 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008253 if (argvars[3].v_type != VAR_UNKNOWN)
8254 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008255 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008256 if (argvars[4].v_type != VAR_UNKNOWN
8257 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8258 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008259 }
8260 }
8261 if (error == TRUE)
8262 return;
8263 if (id >= 1 && id <= 3)
8264 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008265 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008266 return;
8267 }
8268
Bram Moolenaar95e51472018-07-28 16:55:56 +02008269 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008270 conceal_char);
8271#endif
8272}
8273
8274/*
8275 * "matchaddpos()" function
8276 */
8277 static void
8278f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8279{
8280#ifdef FEAT_SEARCH_EXTRA
8281 char_u buf[NUMBUFLEN];
8282 char_u *group;
8283 int prio = 10;
8284 int id = -1;
8285 int error = FALSE;
8286 list_T *l;
8287 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008288 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289
8290 rettv->vval.v_number = -1;
8291
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008292 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293 if (group == NULL)
8294 return;
8295
8296 if (argvars[1].v_type != VAR_LIST)
8297 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008298 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299 return;
8300 }
8301 l = argvars[1].vval.v_list;
8302 if (l == NULL)
8303 return;
8304
8305 if (argvars[2].v_type != VAR_UNKNOWN)
8306 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008307 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308 if (argvars[3].v_type != VAR_UNKNOWN)
8309 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008310 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008311
8312 if (argvars[4].v_type != VAR_UNKNOWN
8313 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8314 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008315 }
8316 }
8317 if (error == TRUE)
8318 return;
8319
8320 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8321 if (id == 1 || id == 2)
8322 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008323 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008324 return;
8325 }
8326
Bram Moolenaar95e51472018-07-28 16:55:56 +02008327 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 conceal_char);
8329#endif
8330}
8331
8332/*
8333 * "matcharg()" function
8334 */
8335 static void
8336f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8337{
8338 if (rettv_list_alloc(rettv) == OK)
8339 {
8340#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008341 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008342 matchitem_T *m;
8343
8344 if (id >= 1 && id <= 3)
8345 {
8346 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8347 {
8348 list_append_string(rettv->vval.v_list,
8349 syn_id2name(m->hlg_id), -1);
8350 list_append_string(rettv->vval.v_list, m->pattern, -1);
8351 }
8352 else
8353 {
8354 list_append_string(rettv->vval.v_list, NULL, -1);
8355 list_append_string(rettv->vval.v_list, NULL, -1);
8356 }
8357 }
8358#endif
8359 }
8360}
8361
8362/*
8363 * "matchdelete()" function
8364 */
8365 static void
8366f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8367{
8368#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008369 win_T *win = get_optional_window(argvars, 1);
8370
8371 if (win == NULL)
8372 rettv->vval.v_number = -1;
8373 else
8374 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008375 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008376#endif
8377}
8378
8379/*
8380 * "matchend()" function
8381 */
8382 static void
8383f_matchend(typval_T *argvars, typval_T *rettv)
8384{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008385 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386}
8387
8388/*
8389 * "matchlist()" function
8390 */
8391 static void
8392f_matchlist(typval_T *argvars, typval_T *rettv)
8393{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008394 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008395}
8396
8397/*
8398 * "matchstr()" function
8399 */
8400 static void
8401f_matchstr(typval_T *argvars, typval_T *rettv)
8402{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008403 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404}
8405
8406/*
8407 * "matchstrpos()" function
8408 */
8409 static void
8410f_matchstrpos(typval_T *argvars, typval_T *rettv)
8411{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008412 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413}
8414
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008415 static void
8416max_min(typval_T *argvars, typval_T *rettv, int domax)
8417{
8418 varnumber_T n = 0;
8419 varnumber_T i;
8420 int error = FALSE;
8421
8422 if (argvars[0].v_type == VAR_LIST)
8423 {
8424 list_T *l;
8425 listitem_T *li;
8426
8427 l = argvars[0].vval.v_list;
8428 if (l != NULL)
8429 {
8430 li = l->lv_first;
8431 if (li != NULL)
8432 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008433 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008434 for (;;)
8435 {
8436 li = li->li_next;
8437 if (li == NULL)
8438 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008439 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008440 if (domax ? i > n : i < n)
8441 n = i;
8442 }
8443 }
8444 }
8445 }
8446 else if (argvars[0].v_type == VAR_DICT)
8447 {
8448 dict_T *d;
8449 int first = TRUE;
8450 hashitem_T *hi;
8451 int todo;
8452
8453 d = argvars[0].vval.v_dict;
8454 if (d != NULL)
8455 {
8456 todo = (int)d->dv_hashtab.ht_used;
8457 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8458 {
8459 if (!HASHITEM_EMPTY(hi))
8460 {
8461 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008462 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008463 if (first)
8464 {
8465 n = i;
8466 first = FALSE;
8467 }
8468 else if (domax ? i > n : i < n)
8469 n = i;
8470 }
8471 }
8472 }
8473 }
8474 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008475 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008476 rettv->vval.v_number = error ? 0 : n;
8477}
8478
8479/*
8480 * "max()" function
8481 */
8482 static void
8483f_max(typval_T *argvars, typval_T *rettv)
8484{
8485 max_min(argvars, rettv, TRUE);
8486}
8487
8488/*
8489 * "min()" function
8490 */
8491 static void
8492f_min(typval_T *argvars, typval_T *rettv)
8493{
8494 max_min(argvars, rettv, FALSE);
8495}
8496
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008497/*
8498 * Create the directory in which "dir" is located, and higher levels when
8499 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008500 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008501 */
8502 static int
8503mkdir_recurse(char_u *dir, int prot)
8504{
8505 char_u *p;
8506 char_u *updir;
8507 int r = FAIL;
8508
8509 /* Get end of directory name in "dir".
8510 * We're done when it's "/" or "c:/". */
8511 p = gettail_sep(dir);
8512 if (p <= get_past_head(dir))
8513 return OK;
8514
8515 /* If the directory exists we're done. Otherwise: create it.*/
8516 updir = vim_strnsave(dir, (int)(p - dir));
8517 if (updir == NULL)
8518 return FAIL;
8519 if (mch_isdir(updir))
8520 r = OK;
8521 else if (mkdir_recurse(updir, prot) == OK)
8522 r = vim_mkdir_emsg(updir, prot);
8523 vim_free(updir);
8524 return r;
8525}
8526
8527#ifdef vim_mkdir
8528/*
8529 * "mkdir()" function
8530 */
8531 static void
8532f_mkdir(typval_T *argvars, typval_T *rettv)
8533{
8534 char_u *dir;
8535 char_u buf[NUMBUFLEN];
8536 int prot = 0755;
8537
8538 rettv->vval.v_number = FAIL;
8539 if (check_restricted() || check_secure())
8540 return;
8541
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008542 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008543 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008544 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008545
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008546 if (*gettail(dir) == NUL)
8547 /* remove trailing slashes */
8548 *gettail_sep(dir) = NUL;
8549
8550 if (argvars[1].v_type != VAR_UNKNOWN)
8551 {
8552 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008553 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008554 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008555 if (prot == -1)
8556 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008557 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008558 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008559 {
8560 if (mch_isdir(dir))
8561 {
8562 /* With the "p" flag it's OK if the dir already exists. */
8563 rettv->vval.v_number = OK;
8564 return;
8565 }
8566 mkdir_recurse(dir, prot);
8567 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008568 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008569 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008570}
8571#endif
8572
8573/*
8574 * "mode()" function
8575 */
8576 static void
8577f_mode(typval_T *argvars, typval_T *rettv)
8578{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008579 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008580
Bram Moolenaar612cc382018-07-29 15:34:26 +02008581 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582
8583 if (time_for_testing == 93784)
8584 {
8585 /* Testing the two-character code. */
8586 buf[0] = 'x';
8587 buf[1] = '!';
8588 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008589#ifdef FEAT_TERMINAL
8590 else if (term_use_loop())
8591 buf[0] = 't';
8592#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 else if (VIsual_active)
8594 {
8595 if (VIsual_select)
8596 buf[0] = VIsual_mode + 's' - 'v';
8597 else
8598 buf[0] = VIsual_mode;
8599 }
8600 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8601 || State == CONFIRM)
8602 {
8603 buf[0] = 'r';
8604 if (State == ASKMORE)
8605 buf[1] = 'm';
8606 else if (State == CONFIRM)
8607 buf[1] = '?';
8608 }
8609 else if (State == EXTERNCMD)
8610 buf[0] = '!';
8611 else if (State & INSERT)
8612 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008613 if (State & VREPLACE_FLAG)
8614 {
8615 buf[0] = 'R';
8616 buf[1] = 'v';
8617 }
8618 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008619 {
8620 if (State & REPLACE_FLAG)
8621 buf[0] = 'R';
8622 else
8623 buf[0] = 'i';
8624#ifdef FEAT_INS_EXPAND
8625 if (ins_compl_active())
8626 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008627 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008628 buf[1] = 'x';
8629#endif
8630 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008632 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008633 {
8634 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008635 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008636 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008637 else if (exmode_active == EXMODE_NORMAL)
8638 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008639 }
8640 else
8641 {
8642 buf[0] = 'n';
8643 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008644 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008646 // to be able to detect force-linewise/blockwise/characterwise operations
8647 buf[2] = motion_force;
8648 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008649 else if (restart_edit == 'I' || restart_edit == 'R'
8650 || restart_edit == 'V')
8651 {
8652 buf[1] = 'i';
8653 buf[2] = restart_edit;
8654 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 }
8656
8657 /* Clear out the minor mode when the argument is not a non-zero number or
8658 * non-empty string. */
8659 if (!non_zero_arg(&argvars[0]))
8660 buf[1] = NUL;
8661
8662 rettv->vval.v_string = vim_strsave(buf);
8663 rettv->v_type = VAR_STRING;
8664}
8665
8666#if defined(FEAT_MZSCHEME) || defined(PROTO)
8667/*
8668 * "mzeval()" function
8669 */
8670 static void
8671f_mzeval(typval_T *argvars, typval_T *rettv)
8672{
8673 char_u *str;
8674 char_u buf[NUMBUFLEN];
8675
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008676 if (check_restricted() || check_secure())
8677 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008678 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008679 do_mzeval(str, rettv);
8680}
8681
8682 void
8683mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8684{
8685 typval_T argvars[3];
8686
8687 argvars[0].v_type = VAR_STRING;
8688 argvars[0].vval.v_string = name;
8689 copy_tv(args, &argvars[1]);
8690 argvars[2].v_type = VAR_UNKNOWN;
8691 f_call(argvars, rettv);
8692 clear_tv(&argvars[1]);
8693}
8694#endif
8695
8696/*
8697 * "nextnonblank()" function
8698 */
8699 static void
8700f_nextnonblank(typval_T *argvars, typval_T *rettv)
8701{
8702 linenr_T lnum;
8703
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008704 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008705 {
8706 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8707 {
8708 lnum = 0;
8709 break;
8710 }
8711 if (*skipwhite(ml_get(lnum)) != NUL)
8712 break;
8713 }
8714 rettv->vval.v_number = lnum;
8715}
8716
8717/*
8718 * "nr2char()" function
8719 */
8720 static void
8721f_nr2char(typval_T *argvars, typval_T *rettv)
8722{
8723 char_u buf[NUMBUFLEN];
8724
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725 if (has_mbyte)
8726 {
8727 int utf8 = 0;
8728
8729 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008730 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008731 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008732 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008734 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735 }
8736 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008737 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008738 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008739 buf[1] = NUL;
8740 }
8741 rettv->v_type = VAR_STRING;
8742 rettv->vval.v_string = vim_strsave(buf);
8743}
8744
8745/*
8746 * "or(expr, expr)" function
8747 */
8748 static void
8749f_or(typval_T *argvars, typval_T *rettv)
8750{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008751 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8752 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753}
8754
8755/*
8756 * "pathshorten()" function
8757 */
8758 static void
8759f_pathshorten(typval_T *argvars, typval_T *rettv)
8760{
8761 char_u *p;
8762
8763 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008764 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008765 if (p == NULL)
8766 rettv->vval.v_string = NULL;
8767 else
8768 {
8769 p = vim_strsave(p);
8770 rettv->vval.v_string = p;
8771 if (p != NULL)
8772 shorten_dir(p);
8773 }
8774}
8775
8776#ifdef FEAT_PERL
8777/*
8778 * "perleval()" function
8779 */
8780 static void
8781f_perleval(typval_T *argvars, typval_T *rettv)
8782{
8783 char_u *str;
8784 char_u buf[NUMBUFLEN];
8785
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008786 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008787 do_perleval(str, rettv);
8788}
8789#endif
8790
8791#ifdef FEAT_FLOAT
8792/*
8793 * "pow()" function
8794 */
8795 static void
8796f_pow(typval_T *argvars, typval_T *rettv)
8797{
8798 float_T fx = 0.0, fy = 0.0;
8799
8800 rettv->v_type = VAR_FLOAT;
8801 if (get_float_arg(argvars, &fx) == OK
8802 && get_float_arg(&argvars[1], &fy) == OK)
8803 rettv->vval.v_float = pow(fx, fy);
8804 else
8805 rettv->vval.v_float = 0.0;
8806}
8807#endif
8808
8809/*
8810 * "prevnonblank()" function
8811 */
8812 static void
8813f_prevnonblank(typval_T *argvars, typval_T *rettv)
8814{
8815 linenr_T lnum;
8816
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008817 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8819 lnum = 0;
8820 else
8821 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8822 --lnum;
8823 rettv->vval.v_number = lnum;
8824}
8825
8826/* This dummy va_list is here because:
8827 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8828 * - locally in the function results in a "used before set" warning
8829 * - using va_start() to initialize it gives "function with fixed args" error */
8830static va_list ap;
8831
8832/*
8833 * "printf()" function
8834 */
8835 static void
8836f_printf(typval_T *argvars, typval_T *rettv)
8837{
8838 char_u buf[NUMBUFLEN];
8839 int len;
8840 char_u *s;
8841 int saved_did_emsg = did_emsg;
8842 char *fmt;
8843
8844 rettv->v_type = VAR_STRING;
8845 rettv->vval.v_string = NULL;
8846
8847 /* Get the required length, allocate the buffer and do it for real. */
8848 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008849 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008850 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008851 if (!did_emsg)
8852 {
8853 s = alloc(len + 1);
8854 if (s != NULL)
8855 {
8856 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008857 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8858 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008859 }
8860 }
8861 did_emsg |= saved_did_emsg;
8862}
8863
8864/*
8865 * "pumvisible()" function
8866 */
8867 static void
8868f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8869{
8870#ifdef FEAT_INS_EXPAND
8871 if (pum_visible())
8872 rettv->vval.v_number = 1;
8873#endif
8874}
8875
8876#ifdef FEAT_PYTHON3
8877/*
8878 * "py3eval()" function
8879 */
8880 static void
8881f_py3eval(typval_T *argvars, typval_T *rettv)
8882{
8883 char_u *str;
8884 char_u buf[NUMBUFLEN];
8885
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008886 if (check_restricted() || check_secure())
8887 return;
8888
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008889 if (p_pyx == 0)
8890 p_pyx = 3;
8891
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008892 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008893 do_py3eval(str, rettv);
8894}
8895#endif
8896
8897#ifdef FEAT_PYTHON
8898/*
8899 * "pyeval()" function
8900 */
8901 static void
8902f_pyeval(typval_T *argvars, typval_T *rettv)
8903{
8904 char_u *str;
8905 char_u buf[NUMBUFLEN];
8906
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008907 if (check_restricted() || check_secure())
8908 return;
8909
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008910 if (p_pyx == 0)
8911 p_pyx = 2;
8912
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008913 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008914 do_pyeval(str, rettv);
8915}
8916#endif
8917
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008918#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8919/*
8920 * "pyxeval()" function
8921 */
8922 static void
8923f_pyxeval(typval_T *argvars, typval_T *rettv)
8924{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008925 if (check_restricted() || check_secure())
8926 return;
8927
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008928# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8929 init_pyxversion();
8930 if (p_pyx == 2)
8931 f_pyeval(argvars, rettv);
8932 else
8933 f_py3eval(argvars, rettv);
8934# elif defined(FEAT_PYTHON)
8935 f_pyeval(argvars, rettv);
8936# elif defined(FEAT_PYTHON3)
8937 f_py3eval(argvars, rettv);
8938# endif
8939}
8940#endif
8941
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008942/*
8943 * "range()" function
8944 */
8945 static void
8946f_range(typval_T *argvars, typval_T *rettv)
8947{
8948 varnumber_T start;
8949 varnumber_T end;
8950 varnumber_T stride = 1;
8951 varnumber_T i;
8952 int error = FALSE;
8953
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008954 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008955 if (argvars[1].v_type == VAR_UNKNOWN)
8956 {
8957 end = start - 1;
8958 start = 0;
8959 }
8960 else
8961 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008962 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008963 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008964 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008965 }
8966
8967 if (error)
8968 return; /* type error; errmsg already given */
8969 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008970 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008971 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008972 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008973 else
8974 {
8975 if (rettv_list_alloc(rettv) == OK)
8976 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8977 if (list_append_number(rettv->vval.v_list,
8978 (varnumber_T)i) == FAIL)
8979 break;
8980 }
8981}
8982
8983/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008984 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008985 */
8986 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008987readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008988{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008989 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008990 typval_T save_val;
8991 typval_T rettv;
8992 typval_T argv[2];
8993 int retval = 0;
8994 int error = FALSE;
8995
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008996 if (expr->v_type == VAR_UNKNOWN)
8997 return 1;
8998
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008999 prepare_vimvar(VV_VAL, &save_val);
9000 set_vim_var_string(VV_VAL, name, -1);
9001 argv[0].v_type = VAR_STRING;
9002 argv[0].vval.v_string = name;
9003
9004 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9005 goto theend;
9006
9007 retval = tv_get_number_chk(&rettv, &error);
9008 if (error)
9009 retval = -1;
9010 clear_tv(&rettv);
9011
9012theend:
9013 set_vim_var_string(VV_VAL, NULL, 0);
9014 restore_vimvar(VV_VAL, &save_val);
9015 return retval;
9016}
9017
9018/*
9019 * "readdir()" function
9020 */
9021 static void
9022f_readdir(typval_T *argvars, typval_T *rettv)
9023{
9024 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009025 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009026 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009027 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009028 garray_T ga;
9029 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009030
9031 if (rettv_list_alloc(rettv) == FAIL)
9032 return;
9033 path = tv_get_string(&argvars[0]);
9034 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009035
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009036 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9037 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009038 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009039 for (i = 0; i < ga.ga_len; i++)
9040 {
9041 p = ((char_u **)ga.ga_data)[i];
9042 list_append_string(rettv->vval.v_list, p, -1);
9043 }
9044 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009045 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009046}
9047
9048/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009049 * "readfile()" function
9050 */
9051 static void
9052f_readfile(typval_T *argvars, typval_T *rettv)
9053{
9054 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009055 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 int failed = FALSE;
9057 char_u *fname;
9058 FILE *fd;
9059 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9060 int io_size = sizeof(buf);
9061 int readlen; /* size of last fread() */
9062 char_u *prev = NULL; /* previously read bytes, if any */
9063 long prevlen = 0; /* length of data in prev */
9064 long prevsize = 0; /* size of prev buffer */
9065 long maxline = MAXLNUM;
9066 long cnt = 0;
9067 char_u *p; /* position in buf */
9068 char_u *start; /* start of current line */
9069
9070 if (argvars[1].v_type != VAR_UNKNOWN)
9071 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009072 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009073 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009074 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9075 blob = TRUE;
9076
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009077 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009078 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009079 }
9080
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009081 if (blob)
9082 {
9083 if (rettv_blob_alloc(rettv) == FAIL)
9084 return;
9085 }
9086 else
9087 {
9088 if (rettv_list_alloc(rettv) == FAIL)
9089 return;
9090 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009091
9092 /* Always open the file in binary mode, library functions have a mind of
9093 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009094 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009095 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009097 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009098 return;
9099 }
9100
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009101 if (blob)
9102 {
9103 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9104 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009105 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009106 blob_free(rettv->vval.v_blob);
9107 }
9108 fclose(fd);
9109 return;
9110 }
9111
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 while (cnt < maxline || maxline < 0)
9113 {
9114 readlen = (int)fread(buf, 1, io_size, fd);
9115
9116 /* This for loop processes what was read, but is also entered at end
9117 * of file so that either:
9118 * - an incomplete line gets written
9119 * - a "binary" file gets an empty line at the end if it ends in a
9120 * newline. */
9121 for (p = buf, start = buf;
9122 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9123 ++p)
9124 {
9125 if (*p == '\n' || readlen <= 0)
9126 {
9127 listitem_T *li;
9128 char_u *s = NULL;
9129 long_u len = p - start;
9130
9131 /* Finished a line. Remove CRs before NL. */
9132 if (readlen > 0 && !binary)
9133 {
9134 while (len > 0 && start[len - 1] == '\r')
9135 --len;
9136 /* removal may cross back to the "prev" string */
9137 if (len == 0)
9138 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9139 --prevlen;
9140 }
9141 if (prevlen == 0)
9142 s = vim_strnsave(start, (int)len);
9143 else
9144 {
9145 /* Change "prev" buffer to be the right size. This way
9146 * the bytes are only copied once, and very long lines are
9147 * allocated only once. */
9148 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9149 {
9150 mch_memmove(s + prevlen, start, len);
9151 s[prevlen + len] = NUL;
9152 prev = NULL; /* the list will own the string */
9153 prevlen = prevsize = 0;
9154 }
9155 }
9156 if (s == NULL)
9157 {
9158 do_outofmem_msg((long_u) prevlen + len + 1);
9159 failed = TRUE;
9160 break;
9161 }
9162
9163 if ((li = listitem_alloc()) == NULL)
9164 {
9165 vim_free(s);
9166 failed = TRUE;
9167 break;
9168 }
9169 li->li_tv.v_type = VAR_STRING;
9170 li->li_tv.v_lock = 0;
9171 li->li_tv.vval.v_string = s;
9172 list_append(rettv->vval.v_list, li);
9173
9174 start = p + 1; /* step over newline */
9175 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9176 break;
9177 }
9178 else if (*p == NUL)
9179 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009180 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9181 * when finding the BF and check the previous two bytes. */
9182 else if (*p == 0xbf && enc_utf8 && !binary)
9183 {
9184 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9185 * + 1, these may be in the "prev" string. */
9186 char_u back1 = p >= buf + 1 ? p[-1]
9187 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9188 char_u back2 = p >= buf + 2 ? p[-2]
9189 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9190 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9191
9192 if (back2 == 0xef && back1 == 0xbb)
9193 {
9194 char_u *dest = p - 2;
9195
9196 /* Usually a BOM is at the beginning of a file, and so at
9197 * the beginning of a line; then we can just step over it.
9198 */
9199 if (start == dest)
9200 start = p + 1;
9201 else
9202 {
9203 /* have to shuffle buf to close gap */
9204 int adjust_prevlen = 0;
9205
9206 if (dest < buf)
9207 {
9208 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9209 dest = buf;
9210 }
9211 if (readlen > p - buf + 1)
9212 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9213 readlen -= 3 - adjust_prevlen;
9214 prevlen -= adjust_prevlen;
9215 p = dest - 1;
9216 }
9217 }
9218 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009219 } /* for */
9220
9221 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9222 break;
9223 if (start < p)
9224 {
9225 /* There's part of a line in buf, store it in "prev". */
9226 if (p - start + prevlen >= prevsize)
9227 {
9228 /* need bigger "prev" buffer */
9229 char_u *newprev;
9230
9231 /* A common use case is ordinary text files and "prev" gets a
9232 * fragment of a line, so the first allocation is made
9233 * small, to avoid repeatedly 'allocing' large and
9234 * 'reallocing' small. */
9235 if (prevsize == 0)
9236 prevsize = (long)(p - start);
9237 else
9238 {
9239 long grow50pc = (prevsize * 3) / 2;
9240 long growmin = (long)((p - start) * 2 + prevlen);
9241 prevsize = grow50pc > growmin ? grow50pc : growmin;
9242 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009243 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009244 if (newprev == NULL)
9245 {
9246 do_outofmem_msg((long_u)prevsize);
9247 failed = TRUE;
9248 break;
9249 }
9250 prev = newprev;
9251 }
9252 /* Add the line part to end of "prev". */
9253 mch_memmove(prev + prevlen, start, p - start);
9254 prevlen += (long)(p - start);
9255 }
9256 } /* while */
9257
9258 /*
9259 * For a negative line count use only the lines at the end of the file,
9260 * free the rest.
9261 */
9262 if (!failed && maxline < 0)
9263 while (cnt > -maxline)
9264 {
9265 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9266 --cnt;
9267 }
9268
9269 if (failed)
9270 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009271 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009272 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009273 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009274 }
9275
9276 vim_free(prev);
9277 fclose(fd);
9278}
9279
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009280 static void
9281return_register(int regname, typval_T *rettv)
9282{
9283 char_u buf[2] = {0, 0};
9284
9285 buf[0] = (char_u)regname;
9286 rettv->v_type = VAR_STRING;
9287 rettv->vval.v_string = vim_strsave(buf);
9288}
9289
9290/*
9291 * "reg_executing()" function
9292 */
9293 static void
9294f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9295{
9296 return_register(reg_executing, rettv);
9297}
9298
9299/*
9300 * "reg_recording()" function
9301 */
9302 static void
9303f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9304{
9305 return_register(reg_recording, rettv);
9306}
9307
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009308#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009309/*
9310 * Convert a List to proftime_T.
9311 * Return FAIL when there is something wrong.
9312 */
9313 static int
9314list2proftime(typval_T *arg, proftime_T *tm)
9315{
9316 long n1, n2;
9317 int error = FALSE;
9318
9319 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9320 || arg->vval.v_list->lv_len != 2)
9321 return FAIL;
9322 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9323 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009324# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325 tm->HighPart = n1;
9326 tm->LowPart = n2;
9327# else
9328 tm->tv_sec = n1;
9329 tm->tv_usec = n2;
9330# endif
9331 return error ? FAIL : OK;
9332}
9333#endif /* FEAT_RELTIME */
9334
9335/*
9336 * "reltime()" function
9337 */
9338 static void
9339f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9340{
9341#ifdef FEAT_RELTIME
9342 proftime_T res;
9343 proftime_T start;
9344
9345 if (argvars[0].v_type == VAR_UNKNOWN)
9346 {
9347 /* No arguments: get current time. */
9348 profile_start(&res);
9349 }
9350 else if (argvars[1].v_type == VAR_UNKNOWN)
9351 {
9352 if (list2proftime(&argvars[0], &res) == FAIL)
9353 return;
9354 profile_end(&res);
9355 }
9356 else
9357 {
9358 /* Two arguments: compute the difference. */
9359 if (list2proftime(&argvars[0], &start) == FAIL
9360 || list2proftime(&argvars[1], &res) == FAIL)
9361 return;
9362 profile_sub(&res, &start);
9363 }
9364
9365 if (rettv_list_alloc(rettv) == OK)
9366 {
9367 long n1, n2;
9368
Bram Moolenaar4f974752019-02-17 17:44:42 +01009369# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009370 n1 = res.HighPart;
9371 n2 = res.LowPart;
9372# else
9373 n1 = res.tv_sec;
9374 n2 = res.tv_usec;
9375# endif
9376 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9377 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9378 }
9379#endif
9380}
9381
9382#ifdef FEAT_FLOAT
9383/*
9384 * "reltimefloat()" function
9385 */
9386 static void
9387f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9388{
9389# ifdef FEAT_RELTIME
9390 proftime_T tm;
9391# endif
9392
9393 rettv->v_type = VAR_FLOAT;
9394 rettv->vval.v_float = 0;
9395# ifdef FEAT_RELTIME
9396 if (list2proftime(&argvars[0], &tm) == OK)
9397 rettv->vval.v_float = profile_float(&tm);
9398# endif
9399}
9400#endif
9401
9402/*
9403 * "reltimestr()" function
9404 */
9405 static void
9406f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9407{
9408#ifdef FEAT_RELTIME
9409 proftime_T tm;
9410#endif
9411
9412 rettv->v_type = VAR_STRING;
9413 rettv->vval.v_string = NULL;
9414#ifdef FEAT_RELTIME
9415 if (list2proftime(&argvars[0], &tm) == OK)
9416 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9417#endif
9418}
9419
9420#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009421 static void
9422make_connection(void)
9423{
9424 if (X_DISPLAY == NULL
9425# ifdef FEAT_GUI
9426 && !gui.in_use
9427# endif
9428 )
9429 {
9430 x_force_connect = TRUE;
9431 setup_term_clip();
9432 x_force_connect = FALSE;
9433 }
9434}
9435
9436 static int
9437check_connection(void)
9438{
9439 make_connection();
9440 if (X_DISPLAY == NULL)
9441 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009442 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009443 return FAIL;
9444 }
9445 return OK;
9446}
9447#endif
9448
9449#ifdef FEAT_CLIENTSERVER
9450 static void
9451remote_common(typval_T *argvars, typval_T *rettv, int expr)
9452{
9453 char_u *server_name;
9454 char_u *keys;
9455 char_u *r = NULL;
9456 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009457 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009458# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009459 HWND w;
9460# else
9461 Window w;
9462# endif
9463
9464 if (check_restricted() || check_secure())
9465 return;
9466
9467# ifdef FEAT_X11
9468 if (check_connection() == FAIL)
9469 return;
9470# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009471 if (argvars[2].v_type != VAR_UNKNOWN
9472 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009473 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009474
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009475 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009476 if (server_name == NULL)
9477 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009478 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009479# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009480 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009481# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009482 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9483 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009484# endif
9485 {
9486 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009487 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009488 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009489 vim_free(r);
9490 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009491 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009492 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009493 return;
9494 }
9495
9496 rettv->vval.v_string = r;
9497
9498 if (argvars[2].v_type != VAR_UNKNOWN)
9499 {
9500 dictitem_T v;
9501 char_u str[30];
9502 char_u *idvar;
9503
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009504 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009505 if (idvar != NULL && *idvar != NUL)
9506 {
9507 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9508 v.di_tv.v_type = VAR_STRING;
9509 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009510 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009511 vim_free(v.di_tv.vval.v_string);
9512 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009513 }
9514}
9515#endif
9516
9517/*
9518 * "remote_expr()" function
9519 */
9520 static void
9521f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9522{
9523 rettv->v_type = VAR_STRING;
9524 rettv->vval.v_string = NULL;
9525#ifdef FEAT_CLIENTSERVER
9526 remote_common(argvars, rettv, TRUE);
9527#endif
9528}
9529
9530/*
9531 * "remote_foreground()" function
9532 */
9533 static void
9534f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9535{
9536#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009537# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009538 /* On Win32 it's done in this application. */
9539 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009540 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009541
9542 if (server_name != NULL)
9543 serverForeground(server_name);
9544 }
9545# else
9546 /* Send a foreground() expression to the server. */
9547 argvars[1].v_type = VAR_STRING;
9548 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9549 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009550 rettv->v_type = VAR_STRING;
9551 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009552 remote_common(argvars, rettv, TRUE);
9553 vim_free(argvars[1].vval.v_string);
9554# endif
9555#endif
9556}
9557
9558 static void
9559f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9560{
9561#ifdef FEAT_CLIENTSERVER
9562 dictitem_T v;
9563 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009564# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009565 long_u n = 0;
9566# endif
9567 char_u *serverid;
9568
9569 if (check_restricted() || check_secure())
9570 {
9571 rettv->vval.v_number = -1;
9572 return;
9573 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009574 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009575 if (serverid == NULL)
9576 {
9577 rettv->vval.v_number = -1;
9578 return; /* type error; errmsg already given */
9579 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009580# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009581 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9582 if (n == 0)
9583 rettv->vval.v_number = -1;
9584 else
9585 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009586 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009587 rettv->vval.v_number = (s != NULL);
9588 }
9589# else
9590 if (check_connection() == FAIL)
9591 return;
9592
9593 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9594 serverStrToWin(serverid), &s);
9595# endif
9596
9597 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9598 {
9599 char_u *retvar;
9600
9601 v.di_tv.v_type = VAR_STRING;
9602 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009603 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009604 if (retvar != NULL)
9605 set_var(retvar, &v.di_tv, FALSE);
9606 vim_free(v.di_tv.vval.v_string);
9607 }
9608#else
9609 rettv->vval.v_number = -1;
9610#endif
9611}
9612
9613 static void
9614f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9615{
9616 char_u *r = NULL;
9617
9618#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009619 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009620
9621 if (serverid != NULL && !check_restricted() && !check_secure())
9622 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009623 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009624# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009625 /* The server's HWND is encoded in the 'id' parameter */
9626 long_u n = 0;
9627# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009628
9629 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009630 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009631
Bram Moolenaar4f974752019-02-17 17:44:42 +01009632# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009633 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9634 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009635 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009636 if (r == NULL)
9637# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009638 if (check_connection() == FAIL
9639 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9640 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009641# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009642 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009643 }
9644#endif
9645 rettv->v_type = VAR_STRING;
9646 rettv->vval.v_string = r;
9647}
9648
9649/*
9650 * "remote_send()" function
9651 */
9652 static void
9653f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9654{
9655 rettv->v_type = VAR_STRING;
9656 rettv->vval.v_string = NULL;
9657#ifdef FEAT_CLIENTSERVER
9658 remote_common(argvars, rettv, FALSE);
9659#endif
9660}
9661
9662/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009663 * "remote_startserver()" function
9664 */
9665 static void
9666f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9667{
9668#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009669 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009670
9671 if (server == NULL)
9672 return; /* type error; errmsg already given */
9673 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009674 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009675 else
9676 {
9677# ifdef FEAT_X11
9678 if (check_connection() == OK)
9679 serverRegisterName(X_DISPLAY, server);
9680# else
9681 serverSetName(server);
9682# endif
9683 }
9684#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009685 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009686#endif
9687}
9688
9689/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009690 * "remove()" function
9691 */
9692 static void
9693f_remove(typval_T *argvars, typval_T *rettv)
9694{
9695 list_T *l;
9696 listitem_T *item, *item2;
9697 listitem_T *li;
9698 long idx;
9699 long end;
9700 char_u *key;
9701 dict_T *d;
9702 dictitem_T *di;
9703 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009704 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009705
9706 if (argvars[0].v_type == VAR_DICT)
9707 {
9708 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009709 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009710 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009711 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009712 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009713 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009714 if (key != NULL)
9715 {
9716 di = dict_find(d, key, -1);
9717 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009718 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009719 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9720 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9721 {
9722 *rettv = di->di_tv;
9723 init_tv(&di->di_tv);
9724 dictitem_remove(d, di);
9725 }
9726 }
9727 }
9728 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009729 else if (argvars[0].v_type == VAR_BLOB)
9730 {
9731 idx = (long)tv_get_number_chk(&argvars[1], &error);
9732 if (!error)
9733 {
9734 blob_T *b = argvars[0].vval.v_blob;
9735 int len = blob_len(b);
9736 char_u *p;
9737
9738 if (idx < 0)
9739 // count from the end
9740 idx = len + idx;
9741 if (idx < 0 || idx >= len)
9742 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009743 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009744 return;
9745 }
9746 if (argvars[2].v_type == VAR_UNKNOWN)
9747 {
9748 // Remove one item, return its value.
9749 p = (char_u *)b->bv_ga.ga_data;
9750 rettv->vval.v_number = (varnumber_T) *(p + idx);
9751 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9752 --b->bv_ga.ga_len;
9753 }
9754 else
9755 {
9756 blob_T *blob;
9757
9758 // Remove range of items, return list with values.
9759 end = (long)tv_get_number_chk(&argvars[2], &error);
9760 if (error)
9761 return;
9762 if (end < 0)
9763 // count from the end
9764 end = len + end;
9765 if (end >= len || idx > end)
9766 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009767 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009768 return;
9769 }
9770 blob = blob_alloc();
9771 if (blob == NULL)
9772 return;
9773 blob->bv_ga.ga_len = end - idx + 1;
9774 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9775 {
9776 vim_free(blob);
9777 return;
9778 }
9779 p = (char_u *)b->bv_ga.ga_data;
9780 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9781 (size_t)(end - idx + 1));
9782 ++blob->bv_refcount;
9783 rettv->v_type = VAR_BLOB;
9784 rettv->vval.v_blob = blob;
9785
9786 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9787 b->bv_ga.ga_len -= end - idx + 1;
9788 }
9789 }
9790 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009791 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009792 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009793 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009794 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009796 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009797 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009798 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009800 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009801 else
9802 {
9803 if (argvars[2].v_type == VAR_UNKNOWN)
9804 {
9805 /* Remove one item, return its value. */
9806 vimlist_remove(l, item, item);
9807 *rettv = item->li_tv;
9808 vim_free(item);
9809 }
9810 else
9811 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009812 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009813 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009814 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009815 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009816 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009817 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818 else
9819 {
9820 int cnt = 0;
9821
9822 for (li = item; li != NULL; li = li->li_next)
9823 {
9824 ++cnt;
9825 if (li == item2)
9826 break;
9827 }
9828 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009829 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009830 else
9831 {
9832 vimlist_remove(l, item, item2);
9833 if (rettv_list_alloc(rettv) == OK)
9834 {
9835 l = rettv->vval.v_list;
9836 l->lv_first = item;
9837 l->lv_last = item2;
9838 item->li_prev = NULL;
9839 item2->li_next = NULL;
9840 l->lv_len = cnt;
9841 }
9842 }
9843 }
9844 }
9845 }
9846 }
9847}
9848
9849/*
9850 * "rename({from}, {to})" function
9851 */
9852 static void
9853f_rename(typval_T *argvars, typval_T *rettv)
9854{
9855 char_u buf[NUMBUFLEN];
9856
9857 if (check_restricted() || check_secure())
9858 rettv->vval.v_number = -1;
9859 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009860 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9861 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009862}
9863
9864/*
9865 * "repeat()" function
9866 */
9867 static void
9868f_repeat(typval_T *argvars, typval_T *rettv)
9869{
9870 char_u *p;
9871 int n;
9872 int slen;
9873 int len;
9874 char_u *r;
9875 int i;
9876
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009877 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009878 if (argvars[0].v_type == VAR_LIST)
9879 {
9880 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9881 while (n-- > 0)
9882 if (list_extend(rettv->vval.v_list,
9883 argvars[0].vval.v_list, NULL) == FAIL)
9884 break;
9885 }
9886 else
9887 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009888 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009889 rettv->v_type = VAR_STRING;
9890 rettv->vval.v_string = NULL;
9891
9892 slen = (int)STRLEN(p);
9893 len = slen * n;
9894 if (len <= 0)
9895 return;
9896
9897 r = alloc(len + 1);
9898 if (r != NULL)
9899 {
9900 for (i = 0; i < n; i++)
9901 mch_memmove(r + i * slen, p, (size_t)slen);
9902 r[len] = NUL;
9903 }
9904
9905 rettv->vval.v_string = r;
9906 }
9907}
9908
9909/*
9910 * "resolve()" function
9911 */
9912 static void
9913f_resolve(typval_T *argvars, typval_T *rettv)
9914{
9915 char_u *p;
9916#ifdef HAVE_READLINK
9917 char_u *buf = NULL;
9918#endif
9919
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009920 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921#ifdef FEAT_SHORTCUT
9922 {
9923 char_u *v = NULL;
9924
Bram Moolenaardce1e892019-02-10 23:18:53 +01009925 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009926 if (v != NULL)
9927 rettv->vval.v_string = v;
9928 else
9929 rettv->vval.v_string = vim_strsave(p);
9930 }
9931#else
9932# ifdef HAVE_READLINK
9933 {
9934 char_u *cpy;
9935 int len;
9936 char_u *remain = NULL;
9937 char_u *q;
9938 int is_relative_to_current = FALSE;
9939 int has_trailing_pathsep = FALSE;
9940 int limit = 100;
9941
9942 p = vim_strsave(p);
9943
9944 if (p[0] == '.' && (vim_ispathsep(p[1])
9945 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9946 is_relative_to_current = TRUE;
9947
9948 len = STRLEN(p);
9949 if (len > 0 && after_pathsep(p, p + len))
9950 {
9951 has_trailing_pathsep = TRUE;
9952 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9953 }
9954
9955 q = getnextcomp(p);
9956 if (*q != NUL)
9957 {
9958 /* Separate the first path component in "p", and keep the
9959 * remainder (beginning with the path separator). */
9960 remain = vim_strsave(q - 1);
9961 q[-1] = NUL;
9962 }
9963
9964 buf = alloc(MAXPATHL + 1);
9965 if (buf == NULL)
9966 goto fail;
9967
9968 for (;;)
9969 {
9970 for (;;)
9971 {
9972 len = readlink((char *)p, (char *)buf, MAXPATHL);
9973 if (len <= 0)
9974 break;
9975 buf[len] = NUL;
9976
9977 if (limit-- == 0)
9978 {
9979 vim_free(p);
9980 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009981 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009982 rettv->vval.v_string = NULL;
9983 goto fail;
9984 }
9985
9986 /* Ensure that the result will have a trailing path separator
9987 * if the argument has one. */
9988 if (remain == NULL && has_trailing_pathsep)
9989 add_pathsep(buf);
9990
9991 /* Separate the first path component in the link value and
9992 * concatenate the remainders. */
9993 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9994 if (*q != NUL)
9995 {
9996 if (remain == NULL)
9997 remain = vim_strsave(q - 1);
9998 else
9999 {
10000 cpy = concat_str(q - 1, remain);
10001 if (cpy != NULL)
10002 {
10003 vim_free(remain);
10004 remain = cpy;
10005 }
10006 }
10007 q[-1] = NUL;
10008 }
10009
10010 q = gettail(p);
10011 if (q > p && *q == NUL)
10012 {
10013 /* Ignore trailing path separator. */
10014 q[-1] = NUL;
10015 q = gettail(p);
10016 }
10017 if (q > p && !mch_isFullName(buf))
10018 {
10019 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010020 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010021 if (cpy != NULL)
10022 {
10023 STRCPY(cpy, p);
10024 STRCPY(gettail(cpy), buf);
10025 vim_free(p);
10026 p = cpy;
10027 }
10028 }
10029 else
10030 {
10031 vim_free(p);
10032 p = vim_strsave(buf);
10033 }
10034 }
10035
10036 if (remain == NULL)
10037 break;
10038
10039 /* Append the first path component of "remain" to "p". */
10040 q = getnextcomp(remain + 1);
10041 len = q - remain - (*q != NUL);
10042 cpy = vim_strnsave(p, STRLEN(p) + len);
10043 if (cpy != NULL)
10044 {
10045 STRNCAT(cpy, remain, len);
10046 vim_free(p);
10047 p = cpy;
10048 }
10049 /* Shorten "remain". */
10050 if (*q != NUL)
10051 STRMOVE(remain, q - 1);
10052 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010053 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010054 }
10055
10056 /* If the result is a relative path name, make it explicitly relative to
10057 * the current directory if and only if the argument had this form. */
10058 if (!vim_ispathsep(*p))
10059 {
10060 if (is_relative_to_current
10061 && *p != NUL
10062 && !(p[0] == '.'
10063 && (p[1] == NUL
10064 || vim_ispathsep(p[1])
10065 || (p[1] == '.'
10066 && (p[2] == NUL
10067 || vim_ispathsep(p[2]))))))
10068 {
10069 /* Prepend "./". */
10070 cpy = concat_str((char_u *)"./", p);
10071 if (cpy != NULL)
10072 {
10073 vim_free(p);
10074 p = cpy;
10075 }
10076 }
10077 else if (!is_relative_to_current)
10078 {
10079 /* Strip leading "./". */
10080 q = p;
10081 while (q[0] == '.' && vim_ispathsep(q[1]))
10082 q += 2;
10083 if (q > p)
10084 STRMOVE(p, p + 2);
10085 }
10086 }
10087
10088 /* Ensure that the result will have no trailing path separator
10089 * if the argument had none. But keep "/" or "//". */
10090 if (!has_trailing_pathsep)
10091 {
10092 q = p + STRLEN(p);
10093 if (after_pathsep(p, q))
10094 *gettail_sep(p) = NUL;
10095 }
10096
10097 rettv->vval.v_string = p;
10098 }
10099# else
10100 rettv->vval.v_string = vim_strsave(p);
10101# endif
10102#endif
10103
10104 simplify_filename(rettv->vval.v_string);
10105
10106#ifdef HAVE_READLINK
10107fail:
10108 vim_free(buf);
10109#endif
10110 rettv->v_type = VAR_STRING;
10111}
10112
10113/*
10114 * "reverse({list})" function
10115 */
10116 static void
10117f_reverse(typval_T *argvars, typval_T *rettv)
10118{
10119 list_T *l;
10120 listitem_T *li, *ni;
10121
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010122 if (argvars[0].v_type == VAR_BLOB)
10123 {
10124 blob_T *b = argvars[0].vval.v_blob;
10125 int i, len = blob_len(b);
10126
10127 for (i = 0; i < len / 2; i++)
10128 {
10129 int tmp = blob_get(b, i);
10130
10131 blob_set(b, i, blob_get(b, len - i - 1));
10132 blob_set(b, len - i - 1, tmp);
10133 }
10134 rettv_blob_set(rettv, b);
10135 return;
10136 }
10137
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010138 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010139 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010140 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010141 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010142 (char_u *)N_("reverse() argument"), TRUE))
10143 {
10144 li = l->lv_last;
10145 l->lv_first = l->lv_last = NULL;
10146 l->lv_len = 0;
10147 while (li != NULL)
10148 {
10149 ni = li->li_prev;
10150 list_append(l, li);
10151 li = ni;
10152 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010153 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010154 l->lv_idx = l->lv_len - l->lv_idx - 1;
10155 }
10156}
10157
10158#define SP_NOMOVE 0x01 /* don't move cursor */
10159#define SP_REPEAT 0x02 /* repeat to find outer pair */
10160#define SP_RETCOUNT 0x04 /* return matchcount */
10161#define SP_SETPCMARK 0x08 /* set previous context mark */
10162#define SP_START 0x10 /* accept match at start position */
10163#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10164#define SP_END 0x40 /* leave cursor at end of match */
10165#define SP_COLUMN 0x80 /* start at cursor column */
10166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010167/*
10168 * Get flags for a search function.
10169 * Possibly sets "p_ws".
10170 * Returns BACKWARD, FORWARD or zero (for an error).
10171 */
10172 static int
10173get_search_arg(typval_T *varp, int *flagsp)
10174{
10175 int dir = FORWARD;
10176 char_u *flags;
10177 char_u nbuf[NUMBUFLEN];
10178 int mask;
10179
10180 if (varp->v_type != VAR_UNKNOWN)
10181 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010182 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010183 if (flags == NULL)
10184 return 0; /* type error; errmsg already given */
10185 while (*flags != NUL)
10186 {
10187 switch (*flags)
10188 {
10189 case 'b': dir = BACKWARD; break;
10190 case 'w': p_ws = TRUE; break;
10191 case 'W': p_ws = FALSE; break;
10192 default: mask = 0;
10193 if (flagsp != NULL)
10194 switch (*flags)
10195 {
10196 case 'c': mask = SP_START; break;
10197 case 'e': mask = SP_END; break;
10198 case 'm': mask = SP_RETCOUNT; break;
10199 case 'n': mask = SP_NOMOVE; break;
10200 case 'p': mask = SP_SUBPAT; break;
10201 case 'r': mask = SP_REPEAT; break;
10202 case 's': mask = SP_SETPCMARK; break;
10203 case 'z': mask = SP_COLUMN; break;
10204 }
10205 if (mask == 0)
10206 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010207 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010208 dir = 0;
10209 }
10210 else
10211 *flagsp |= mask;
10212 }
10213 if (dir == 0)
10214 break;
10215 ++flags;
10216 }
10217 }
10218 return dir;
10219}
10220
10221/*
10222 * Shared by search() and searchpos() functions.
10223 */
10224 static int
10225search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10226{
10227 int flags;
10228 char_u *pat;
10229 pos_T pos;
10230 pos_T save_cursor;
10231 int save_p_ws = p_ws;
10232 int dir;
10233 int retval = 0; /* default: FAIL */
10234 long lnum_stop = 0;
10235 proftime_T tm;
10236#ifdef FEAT_RELTIME
10237 long time_limit = 0;
10238#endif
10239 int options = SEARCH_KEEP;
10240 int subpatnum;
10241
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010242 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010243 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10244 if (dir == 0)
10245 goto theend;
10246 flags = *flagsp;
10247 if (flags & SP_START)
10248 options |= SEARCH_START;
10249 if (flags & SP_END)
10250 options |= SEARCH_END;
10251 if (flags & SP_COLUMN)
10252 options |= SEARCH_COL;
10253
10254 /* Optional arguments: line number to stop searching and timeout. */
10255 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10256 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010257 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010258 if (lnum_stop < 0)
10259 goto theend;
10260#ifdef FEAT_RELTIME
10261 if (argvars[3].v_type != VAR_UNKNOWN)
10262 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010263 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010264 if (time_limit < 0)
10265 goto theend;
10266 }
10267#endif
10268 }
10269
10270#ifdef FEAT_RELTIME
10271 /* Set the time limit, if there is one. */
10272 profile_setlimit(time_limit, &tm);
10273#endif
10274
10275 /*
10276 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10277 * Check to make sure only those flags are set.
10278 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10279 * flags cannot be set. Check for that condition also.
10280 */
10281 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10282 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010284 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010285 goto theend;
10286 }
10287
10288 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010289 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010290 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010291 if (subpatnum != FAIL)
10292 {
10293 if (flags & SP_SUBPAT)
10294 retval = subpatnum;
10295 else
10296 retval = pos.lnum;
10297 if (flags & SP_SETPCMARK)
10298 setpcmark();
10299 curwin->w_cursor = pos;
10300 if (match_pos != NULL)
10301 {
10302 /* Store the match cursor position */
10303 match_pos->lnum = pos.lnum;
10304 match_pos->col = pos.col + 1;
10305 }
10306 /* "/$" will put the cursor after the end of the line, may need to
10307 * correct that here */
10308 check_cursor();
10309 }
10310
10311 /* If 'n' flag is used: restore cursor position. */
10312 if (flags & SP_NOMOVE)
10313 curwin->w_cursor = save_cursor;
10314 else
10315 curwin->w_set_curswant = TRUE;
10316theend:
10317 p_ws = save_p_ws;
10318
10319 return retval;
10320}
10321
10322#ifdef FEAT_FLOAT
10323
10324/*
10325 * round() is not in C90, use ceil() or floor() instead.
10326 */
10327 float_T
10328vim_round(float_T f)
10329{
10330 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10331}
10332
10333/*
10334 * "round({float})" function
10335 */
10336 static void
10337f_round(typval_T *argvars, typval_T *rettv)
10338{
10339 float_T f = 0.0;
10340
10341 rettv->v_type = VAR_FLOAT;
10342 if (get_float_arg(argvars, &f) == OK)
10343 rettv->vval.v_float = vim_round(f);
10344 else
10345 rettv->vval.v_float = 0.0;
10346}
10347#endif
10348
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010349#ifdef FEAT_RUBY
10350/*
10351 * "rubyeval()" function
10352 */
10353 static void
10354f_rubyeval(typval_T *argvars, typval_T *rettv)
10355{
10356 char_u *str;
10357 char_u buf[NUMBUFLEN];
10358
10359 str = tv_get_string_buf(&argvars[0], buf);
10360 do_rubyeval(str, rettv);
10361}
10362#endif
10363
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010364/*
10365 * "screenattr()" function
10366 */
10367 static void
10368f_screenattr(typval_T *argvars, typval_T *rettv)
10369{
10370 int row;
10371 int col;
10372 int c;
10373
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010374 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10375 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010376 if (row < 0 || row >= screen_Rows
10377 || col < 0 || col >= screen_Columns)
10378 c = -1;
10379 else
10380 c = ScreenAttrs[LineOffset[row] + col];
10381 rettv->vval.v_number = c;
10382}
10383
10384/*
10385 * "screenchar()" function
10386 */
10387 static void
10388f_screenchar(typval_T *argvars, typval_T *rettv)
10389{
10390 int row;
10391 int col;
10392 int off;
10393 int c;
10394
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010395 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10396 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010397 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010398 c = -1;
10399 else
10400 {
10401 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010402 if (enc_utf8 && ScreenLinesUC[off] != 0)
10403 c = ScreenLinesUC[off];
10404 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010405 c = ScreenLines[off];
10406 }
10407 rettv->vval.v_number = c;
10408}
10409
10410/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010411 * "screenchars()" function
10412 */
10413 static void
10414f_screenchars(typval_T *argvars, typval_T *rettv)
10415{
10416 int row;
10417 int col;
10418 int off;
10419 int c;
10420 int i;
10421
10422 if (rettv_list_alloc(rettv) == FAIL)
10423 return;
10424 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10425 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10426 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10427 return;
10428
10429 off = LineOffset[row] + col;
10430 if (enc_utf8 && ScreenLinesUC[off] != 0)
10431 c = ScreenLinesUC[off];
10432 else
10433 c = ScreenLines[off];
10434 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10435
10436 if (enc_utf8)
10437
10438 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10439 list_append_number(rettv->vval.v_list,
10440 (varnumber_T)ScreenLinesC[i][off]);
10441}
10442
10443/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010444 * "screencol()" function
10445 *
10446 * First column is 1 to be consistent with virtcol().
10447 */
10448 static void
10449f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10450{
10451 rettv->vval.v_number = screen_screencol() + 1;
10452}
10453
10454/*
10455 * "screenrow()" function
10456 */
10457 static void
10458f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10459{
10460 rettv->vval.v_number = screen_screenrow() + 1;
10461}
10462
10463/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010464 * "screenstring()" function
10465 */
10466 static void
10467f_screenstring(typval_T *argvars, typval_T *rettv)
10468{
10469 int row;
10470 int col;
10471 int off;
10472 int c;
10473 int i;
10474 char_u buf[MB_MAXBYTES + 1];
10475 int buflen = 0;
10476
10477 rettv->vval.v_string = NULL;
10478 rettv->v_type = VAR_STRING;
10479
10480 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10481 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10482 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10483 return;
10484
10485 off = LineOffset[row] + col;
10486 if (enc_utf8 && ScreenLinesUC[off] != 0)
10487 c = ScreenLinesUC[off];
10488 else
10489 c = ScreenLines[off];
10490 buflen += mb_char2bytes(c, buf);
10491
10492 if (enc_utf8)
10493 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10494 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10495
10496 buf[buflen] = NUL;
10497 rettv->vval.v_string = vim_strsave(buf);
10498}
10499
10500/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010501 * "search()" function
10502 */
10503 static void
10504f_search(typval_T *argvars, typval_T *rettv)
10505{
10506 int flags = 0;
10507
10508 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10509}
10510
10511/*
10512 * "searchdecl()" function
10513 */
10514 static void
10515f_searchdecl(typval_T *argvars, typval_T *rettv)
10516{
10517 int locally = 1;
10518 int thisblock = 0;
10519 int error = FALSE;
10520 char_u *name;
10521
10522 rettv->vval.v_number = 1; /* default: FAIL */
10523
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010524 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010525 if (argvars[1].v_type != VAR_UNKNOWN)
10526 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010527 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010528 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010529 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010530 }
10531 if (!error && name != NULL)
10532 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10533 locally, thisblock, SEARCH_KEEP) == FAIL;
10534}
10535
10536/*
10537 * Used by searchpair() and searchpairpos()
10538 */
10539 static int
10540searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10541{
10542 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010543 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010544 int save_p_ws = p_ws;
10545 int dir;
10546 int flags = 0;
10547 char_u nbuf1[NUMBUFLEN];
10548 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010549 int retval = 0; /* default: FAIL */
10550 long lnum_stop = 0;
10551 long time_limit = 0;
10552
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010553 /* Get the three pattern arguments: start, middle, end. Will result in an
10554 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010555 spat = tv_get_string_chk(&argvars[0]);
10556 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10557 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010558 if (spat == NULL || mpat == NULL || epat == NULL)
10559 goto theend; /* type error */
10560
10561 /* Handle the optional fourth argument: flags */
10562 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10563 if (dir == 0)
10564 goto theend;
10565
10566 /* Don't accept SP_END or SP_SUBPAT.
10567 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10568 */
10569 if ((flags & (SP_END | SP_SUBPAT)) != 0
10570 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010572 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010573 goto theend;
10574 }
10575
10576 /* Using 'r' implies 'W', otherwise it doesn't work. */
10577 if (flags & SP_REPEAT)
10578 p_ws = FALSE;
10579
10580 /* Optional fifth argument: skip expression */
10581 if (argvars[3].v_type == VAR_UNKNOWN
10582 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010583 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010584 else
10585 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010586 skip = &argvars[4];
10587 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10588 && skip->v_type != VAR_STRING)
10589 {
10590 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010591 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010592 goto theend;
10593 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010594 if (argvars[5].v_type != VAR_UNKNOWN)
10595 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010596 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010597 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010599 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010600 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010601 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010602#ifdef FEAT_RELTIME
10603 if (argvars[6].v_type != VAR_UNKNOWN)
10604 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010605 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010606 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010608 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010609 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010610 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010611 }
10612#endif
10613 }
10614 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010615
10616 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10617 match_pos, lnum_stop, time_limit);
10618
10619theend:
10620 p_ws = save_p_ws;
10621
10622 return retval;
10623}
10624
10625/*
10626 * "searchpair()" function
10627 */
10628 static void
10629f_searchpair(typval_T *argvars, typval_T *rettv)
10630{
10631 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10632}
10633
10634/*
10635 * "searchpairpos()" function
10636 */
10637 static void
10638f_searchpairpos(typval_T *argvars, typval_T *rettv)
10639{
10640 pos_T match_pos;
10641 int lnum = 0;
10642 int col = 0;
10643
10644 if (rettv_list_alloc(rettv) == FAIL)
10645 return;
10646
10647 if (searchpair_cmn(argvars, &match_pos) > 0)
10648 {
10649 lnum = match_pos.lnum;
10650 col = match_pos.col;
10651 }
10652
10653 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10654 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10655}
10656
10657/*
10658 * Search for a start/middle/end thing.
10659 * Used by searchpair(), see its documentation for the details.
10660 * Returns 0 or -1 for no match,
10661 */
10662 long
10663do_searchpair(
10664 char_u *spat, /* start pattern */
10665 char_u *mpat, /* middle pattern */
10666 char_u *epat, /* end pattern */
10667 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010668 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010669 int flags, /* SP_SETPCMARK and other SP_ values */
10670 pos_T *match_pos,
10671 linenr_T lnum_stop, /* stop at this line if not zero */
10672 long time_limit UNUSED) /* stop after this many msec */
10673{
10674 char_u *save_cpo;
10675 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10676 long retval = 0;
10677 pos_T pos;
10678 pos_T firstpos;
10679 pos_T foundpos;
10680 pos_T save_cursor;
10681 pos_T save_pos;
10682 int n;
10683 int r;
10684 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010685 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010686 int err;
10687 int options = SEARCH_KEEP;
10688 proftime_T tm;
10689
10690 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10691 save_cpo = p_cpo;
10692 p_cpo = empty_option;
10693
10694#ifdef FEAT_RELTIME
10695 /* Set the time limit, if there is one. */
10696 profile_setlimit(time_limit, &tm);
10697#endif
10698
10699 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10700 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010701 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
10702 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010703 if (pat2 == NULL || pat3 == NULL)
10704 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010705 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010706 if (*mpat == NUL)
10707 STRCPY(pat3, pat2);
10708 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010709 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010710 spat, epat, mpat);
10711 if (flags & SP_START)
10712 options |= SEARCH_START;
10713
Bram Moolenaar48570482017-10-30 21:48:41 +010010714 if (skip != NULL)
10715 {
10716 /* Empty string means to not use the skip expression. */
10717 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10718 use_skip = skip->vval.v_string != NULL
10719 && *skip->vval.v_string != NUL;
10720 }
10721
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010722 save_cursor = curwin->w_cursor;
10723 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010724 CLEAR_POS(&firstpos);
10725 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010726 pat = pat3;
10727 for (;;)
10728 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010729 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010730 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010731 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010732 /* didn't find it or found the first match again: FAIL */
10733 break;
10734
10735 if (firstpos.lnum == 0)
10736 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010737 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010738 {
10739 /* Found the same position again. Can happen with a pattern that
10740 * has "\zs" at the end and searching backwards. Advance one
10741 * character and try again. */
10742 if (dir == BACKWARD)
10743 decl(&pos);
10744 else
10745 incl(&pos);
10746 }
10747 foundpos = pos;
10748
10749 /* clear the start flag to avoid getting stuck here */
10750 options &= ~SEARCH_START;
10751
10752 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010753 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010754 {
10755 save_pos = curwin->w_cursor;
10756 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010757 err = FALSE;
10758 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010759 curwin->w_cursor = save_pos;
10760 if (err)
10761 {
10762 /* Evaluating {skip} caused an error, break here. */
10763 curwin->w_cursor = save_cursor;
10764 retval = -1;
10765 break;
10766 }
10767 if (r)
10768 continue;
10769 }
10770
10771 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10772 {
10773 /* Found end when searching backwards or start when searching
10774 * forward: nested pair. */
10775 ++nest;
10776 pat = pat2; /* nested, don't search for middle */
10777 }
10778 else
10779 {
10780 /* Found end when searching forward or start when searching
10781 * backward: end of (nested) pair; or found middle in outer pair. */
10782 if (--nest == 1)
10783 pat = pat3; /* outer level, search for middle */
10784 }
10785
10786 if (nest == 0)
10787 {
10788 /* Found the match: return matchcount or line number. */
10789 if (flags & SP_RETCOUNT)
10790 ++retval;
10791 else
10792 retval = pos.lnum;
10793 if (flags & SP_SETPCMARK)
10794 setpcmark();
10795 curwin->w_cursor = pos;
10796 if (!(flags & SP_REPEAT))
10797 break;
10798 nest = 1; /* search for next unmatched */
10799 }
10800 }
10801
10802 if (match_pos != NULL)
10803 {
10804 /* Store the match cursor position */
10805 match_pos->lnum = curwin->w_cursor.lnum;
10806 match_pos->col = curwin->w_cursor.col + 1;
10807 }
10808
10809 /* If 'n' flag is used or search failed: restore cursor position. */
10810 if ((flags & SP_NOMOVE) || retval == 0)
10811 curwin->w_cursor = save_cursor;
10812
10813theend:
10814 vim_free(pat2);
10815 vim_free(pat3);
10816 if (p_cpo == empty_option)
10817 p_cpo = save_cpo;
10818 else
10819 /* Darn, evaluating the {skip} expression changed the value. */
10820 free_string_option(save_cpo);
10821
10822 return retval;
10823}
10824
10825/*
10826 * "searchpos()" function
10827 */
10828 static void
10829f_searchpos(typval_T *argvars, typval_T *rettv)
10830{
10831 pos_T match_pos;
10832 int lnum = 0;
10833 int col = 0;
10834 int n;
10835 int flags = 0;
10836
10837 if (rettv_list_alloc(rettv) == FAIL)
10838 return;
10839
10840 n = search_cmn(argvars, &match_pos, &flags);
10841 if (n > 0)
10842 {
10843 lnum = match_pos.lnum;
10844 col = match_pos.col;
10845 }
10846
10847 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10848 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10849 if (flags & SP_SUBPAT)
10850 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10851}
10852
10853 static void
10854f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10855{
10856#ifdef FEAT_CLIENTSERVER
10857 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010858 char_u *server = tv_get_string_chk(&argvars[0]);
10859 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010860
10861 rettv->vval.v_number = -1;
10862 if (server == NULL || reply == NULL)
10863 return;
10864 if (check_restricted() || check_secure())
10865 return;
10866# ifdef FEAT_X11
10867 if (check_connection() == FAIL)
10868 return;
10869# endif
10870
10871 if (serverSendReply(server, reply) < 0)
10872 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010873 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010874 return;
10875 }
10876 rettv->vval.v_number = 0;
10877#else
10878 rettv->vval.v_number = -1;
10879#endif
10880}
10881
10882 static void
10883f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10884{
10885 char_u *r = NULL;
10886
10887#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010888# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010889 r = serverGetVimNames();
10890# else
10891 make_connection();
10892 if (X_DISPLAY != NULL)
10893 r = serverGetVimNames(X_DISPLAY);
10894# endif
10895#endif
10896 rettv->v_type = VAR_STRING;
10897 rettv->vval.v_string = r;
10898}
10899
10900/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010901 * "setbufline()" function
10902 */
10903 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010904f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010905{
10906 linenr_T lnum;
10907 buf_T *buf;
10908
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010909 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010910 if (buf == NULL)
10911 rettv->vval.v_number = 1; /* FAIL */
10912 else
10913 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010914 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010915 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010916 }
10917}
10918
10919/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010920 * "setbufvar()" function
10921 */
10922 static void
10923f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10924{
10925 buf_T *buf;
10926 char_u *varname, *bufvarname;
10927 typval_T *varp;
10928 char_u nbuf[NUMBUFLEN];
10929
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010930 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010931 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010932 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10933 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010934 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010935 varp = &argvars[2];
10936
10937 if (buf != NULL && varname != NULL && varp != NULL)
10938 {
10939 if (*varname == '&')
10940 {
10941 long numval;
10942 char_u *strval;
10943 int error = FALSE;
10944 aco_save_T aco;
10945
10946 /* set curbuf to be our buf, temporarily */
10947 aucmd_prepbuf(&aco, buf);
10948
10949 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010950 numval = (long)tv_get_number_chk(varp, &error);
10951 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010952 if (!error && strval != NULL)
10953 set_option_value(varname, numval, strval, OPT_LOCAL);
10954
10955 /* reset notion of buffer */
10956 aucmd_restbuf(&aco);
10957 }
10958 else
10959 {
10960 buf_T *save_curbuf = curbuf;
10961
Bram Moolenaar964b3742019-05-24 18:54:09 +020010962 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010963 if (bufvarname != NULL)
10964 {
10965 curbuf = buf;
10966 STRCPY(bufvarname, "b:");
10967 STRCPY(bufvarname + 2, varname);
10968 set_var(bufvarname, varp, TRUE);
10969 vim_free(bufvarname);
10970 curbuf = save_curbuf;
10971 }
10972 }
10973 }
10974}
10975
10976 static void
10977f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10978{
10979 dict_T *d;
10980 dictitem_T *di;
10981 char_u *csearch;
10982
10983 if (argvars[0].v_type != VAR_DICT)
10984 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010985 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010986 return;
10987 }
10988
10989 if ((d = argvars[0].vval.v_dict) != NULL)
10990 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010991 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010992 if (csearch != NULL)
10993 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010994 if (enc_utf8)
10995 {
10996 int pcc[MAX_MCO];
10997 int c = utfc_ptr2char(csearch, pcc);
10998
10999 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11000 }
11001 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011002 set_last_csearch(PTR2CHAR(csearch),
11003 csearch, MB_PTR2LEN(csearch));
11004 }
11005
11006 di = dict_find(d, (char_u *)"forward", -1);
11007 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011008 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011009 ? FORWARD : BACKWARD);
11010
11011 di = dict_find(d, (char_u *)"until", -1);
11012 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011013 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011014 }
11015}
11016
11017/*
11018 * "setcmdpos()" function
11019 */
11020 static void
11021f_setcmdpos(typval_T *argvars, typval_T *rettv)
11022{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011023 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011024
11025 if (pos >= 0)
11026 rettv->vval.v_number = set_cmdline_pos(pos);
11027}
11028
11029/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011030 * "setenv()" function
11031 */
11032 static void
11033f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11034{
11035 char_u namebuf[NUMBUFLEN];
11036 char_u valbuf[NUMBUFLEN];
11037 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11038
11039 if (argvars[1].v_type == VAR_SPECIAL
11040 && argvars[1].vval.v_number == VVAL_NULL)
11041 vim_unsetenv(name);
11042 else
11043 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11044}
11045
11046/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011047 * "setfperm({fname}, {mode})" function
11048 */
11049 static void
11050f_setfperm(typval_T *argvars, typval_T *rettv)
11051{
11052 char_u *fname;
11053 char_u modebuf[NUMBUFLEN];
11054 char_u *mode_str;
11055 int i;
11056 int mask;
11057 int mode = 0;
11058
11059 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011060 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011061 if (fname == NULL)
11062 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011063 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011064 if (mode_str == NULL)
11065 return;
11066 if (STRLEN(mode_str) != 9)
11067 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011068 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011069 return;
11070 }
11071
11072 mask = 1;
11073 for (i = 8; i >= 0; --i)
11074 {
11075 if (mode_str[i] != '-')
11076 mode |= mask;
11077 mask = mask << 1;
11078 }
11079 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11080}
11081
11082/*
11083 * "setline()" function
11084 */
11085 static void
11086f_setline(typval_T *argvars, typval_T *rettv)
11087{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011088 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011089
Bram Moolenaarca851592018-06-06 21:04:07 +020011090 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011091}
11092
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011093/*
11094 * Used by "setqflist()" and "setloclist()" functions
11095 */
11096 static void
11097set_qf_ll_list(
11098 win_T *wp UNUSED,
11099 typval_T *list_arg UNUSED,
11100 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011101 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011102 typval_T *rettv)
11103{
11104#ifdef FEAT_QUICKFIX
11105 static char *e_invact = N_("E927: Invalid action: '%s'");
11106 char_u *act;
11107 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011108 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011109#endif
11110
11111 rettv->vval.v_number = -1;
11112
11113#ifdef FEAT_QUICKFIX
11114 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011115 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011116 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011117 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011118 else
11119 {
11120 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011121 dict_T *d = NULL;
11122 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011123
11124 if (action_arg->v_type == VAR_STRING)
11125 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011126 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011127 if (act == NULL)
11128 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011129 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11130 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011131 action = *act;
11132 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011133 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011134 }
11135 else if (action_arg->v_type == VAR_UNKNOWN)
11136 action = ' ';
11137 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011138 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011139
Bram Moolenaard823fa92016-08-12 16:29:27 +020011140 if (action_arg->v_type != VAR_UNKNOWN
11141 && what_arg->v_type != VAR_UNKNOWN)
11142 {
11143 if (what_arg->v_type == VAR_DICT)
11144 d = what_arg->vval.v_dict;
11145 else
11146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011147 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011148 valid_dict = FALSE;
11149 }
11150 }
11151
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011152 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011153 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011154 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11155 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011156 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011157 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011158 }
11159#endif
11160}
11161
11162/*
11163 * "setloclist()" function
11164 */
11165 static void
11166f_setloclist(typval_T *argvars, typval_T *rettv)
11167{
11168 win_T *win;
11169
11170 rettv->vval.v_number = -1;
11171
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011172 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011173 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011174 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011175}
11176
11177/*
11178 * "setmatches()" function
11179 */
11180 static void
11181f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11182{
11183#ifdef FEAT_SEARCH_EXTRA
11184 list_T *l;
11185 listitem_T *li;
11186 dict_T *d;
11187 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011188 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011189
11190 rettv->vval.v_number = -1;
11191 if (argvars[0].v_type != VAR_LIST)
11192 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011193 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011194 return;
11195 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011196 if (win == NULL)
11197 return;
11198
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011199 if ((l = argvars[0].vval.v_list) != NULL)
11200 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011201 /* To some extent make sure that we are dealing with a list from
11202 * "getmatches()". */
11203 li = l->lv_first;
11204 while (li != NULL)
11205 {
11206 if (li->li_tv.v_type != VAR_DICT
11207 || (d = li->li_tv.vval.v_dict) == NULL)
11208 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011209 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011210 return;
11211 }
11212 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11213 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11214 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11215 && dict_find(d, (char_u *)"priority", -1) != NULL
11216 && dict_find(d, (char_u *)"id", -1) != NULL))
11217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011218 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011219 return;
11220 }
11221 li = li->li_next;
11222 }
11223
Bram Moolenaaraff74912019-03-30 18:11:49 +010011224 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011225 li = l->lv_first;
11226 while (li != NULL)
11227 {
11228 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011229 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011230 dictitem_T *di;
11231 char_u *group;
11232 int priority;
11233 int id;
11234 char_u *conceal;
11235
11236 d = li->li_tv.vval.v_dict;
11237 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11238 {
11239 if (s == NULL)
11240 {
11241 s = list_alloc();
11242 if (s == NULL)
11243 return;
11244 }
11245
11246 /* match from matchaddpos() */
11247 for (i = 1; i < 9; i++)
11248 {
11249 sprintf((char *)buf, (char *)"pos%d", i);
11250 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11251 {
11252 if (di->di_tv.v_type != VAR_LIST)
11253 return;
11254
11255 list_append_tv(s, &di->di_tv);
11256 s->lv_refcount++;
11257 }
11258 else
11259 break;
11260 }
11261 }
11262
Bram Moolenaar8f667172018-12-14 15:38:31 +010011263 group = dict_get_string(d, (char_u *)"group", TRUE);
11264 priority = (int)dict_get_number(d, (char_u *)"priority");
11265 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011266 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011267 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011268 : NULL;
11269 if (i == 0)
11270 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011271 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011272 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011273 priority, id, NULL, conceal);
11274 }
11275 else
11276 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011277 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011278 list_unref(s);
11279 s = NULL;
11280 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011281 vim_free(group);
11282 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011283
11284 li = li->li_next;
11285 }
11286 rettv->vval.v_number = 0;
11287 }
11288#endif
11289}
11290
11291/*
11292 * "setpos()" function
11293 */
11294 static void
11295f_setpos(typval_T *argvars, typval_T *rettv)
11296{
11297 pos_T pos;
11298 int fnum;
11299 char_u *name;
11300 colnr_T curswant = -1;
11301
11302 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011303 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011304 if (name != NULL)
11305 {
11306 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11307 {
11308 if (--pos.col < 0)
11309 pos.col = 0;
11310 if (name[0] == '.' && name[1] == NUL)
11311 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011312 /* set cursor; "fnum" is ignored */
11313 curwin->w_cursor = pos;
11314 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011315 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011316 curwin->w_curswant = curswant - 1;
11317 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011318 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011319 check_cursor();
11320 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011321 }
11322 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11323 {
11324 /* set mark */
11325 if (setmark_pos(name[1], &pos, fnum) == OK)
11326 rettv->vval.v_number = 0;
11327 }
11328 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011329 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011330 }
11331 }
11332}
11333
11334/*
11335 * "setqflist()" function
11336 */
11337 static void
11338f_setqflist(typval_T *argvars, typval_T *rettv)
11339{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011340 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011341}
11342
11343/*
11344 * "setreg()" function
11345 */
11346 static void
11347f_setreg(typval_T *argvars, typval_T *rettv)
11348{
11349 int regname;
11350 char_u *strregname;
11351 char_u *stropt;
11352 char_u *strval;
11353 int append;
11354 char_u yank_type;
11355 long block_len;
11356
11357 block_len = -1;
11358 yank_type = MAUTO;
11359 append = FALSE;
11360
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011361 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011362 rettv->vval.v_number = 1; /* FAIL is default */
11363
11364 if (strregname == NULL)
11365 return; /* type error; errmsg already given */
11366 regname = *strregname;
11367 if (regname == 0 || regname == '@')
11368 regname = '"';
11369
11370 if (argvars[2].v_type != VAR_UNKNOWN)
11371 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011372 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011373 if (stropt == NULL)
11374 return; /* type error */
11375 for (; *stropt != NUL; ++stropt)
11376 switch (*stropt)
11377 {
11378 case 'a': case 'A': /* append */
11379 append = TRUE;
11380 break;
11381 case 'v': case 'c': /* character-wise selection */
11382 yank_type = MCHAR;
11383 break;
11384 case 'V': case 'l': /* line-wise selection */
11385 yank_type = MLINE;
11386 break;
11387 case 'b': case Ctrl_V: /* block-wise selection */
11388 yank_type = MBLOCK;
11389 if (VIM_ISDIGIT(stropt[1]))
11390 {
11391 ++stropt;
11392 block_len = getdigits(&stropt) - 1;
11393 --stropt;
11394 }
11395 break;
11396 }
11397 }
11398
11399 if (argvars[1].v_type == VAR_LIST)
11400 {
11401 char_u **lstval;
11402 char_u **allocval;
11403 char_u buf[NUMBUFLEN];
11404 char_u **curval;
11405 char_u **curallocval;
11406 list_T *ll = argvars[1].vval.v_list;
11407 listitem_T *li;
11408 int len;
11409
11410 /* If the list is NULL handle like an empty list. */
11411 len = ll == NULL ? 0 : ll->lv_len;
11412
11413 /* First half: use for pointers to result lines; second half: use for
11414 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011415 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011416 if (lstval == NULL)
11417 return;
11418 curval = lstval;
11419 allocval = lstval + len + 2;
11420 curallocval = allocval;
11421
11422 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11423 li = li->li_next)
11424 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011425 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011426 if (strval == NULL)
11427 goto free_lstval;
11428 if (strval == buf)
11429 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011430 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011431 * overwrite the string. */
11432 strval = vim_strsave(buf);
11433 if (strval == NULL)
11434 goto free_lstval;
11435 *curallocval++ = strval;
11436 }
11437 *curval++ = strval;
11438 }
11439 *curval++ = NULL;
11440
11441 write_reg_contents_lst(regname, lstval, -1,
11442 append, yank_type, block_len);
11443free_lstval:
11444 while (curallocval > allocval)
11445 vim_free(*--curallocval);
11446 vim_free(lstval);
11447 }
11448 else
11449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011450 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011451 if (strval == NULL)
11452 return;
11453 write_reg_contents_ex(regname, strval, -1,
11454 append, yank_type, block_len);
11455 }
11456 rettv->vval.v_number = 0;
11457}
11458
11459/*
11460 * "settabvar()" function
11461 */
11462 static void
11463f_settabvar(typval_T *argvars, typval_T *rettv)
11464{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011465 tabpage_T *save_curtab;
11466 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011467 char_u *varname, *tabvarname;
11468 typval_T *varp;
11469
11470 rettv->vval.v_number = 0;
11471
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011472 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011473 return;
11474
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011475 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11476 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011477 varp = &argvars[2];
11478
Bram Moolenaar4033c552017-09-16 20:54:51 +020011479 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011480 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011481 save_curtab = curtab;
11482 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011483
Bram Moolenaar964b3742019-05-24 18:54:09 +020011484 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011485 if (tabvarname != NULL)
11486 {
11487 STRCPY(tabvarname, "t:");
11488 STRCPY(tabvarname + 2, varname);
11489 set_var(tabvarname, varp, TRUE);
11490 vim_free(tabvarname);
11491 }
11492
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011493 /* Restore current tabpage */
11494 if (valid_tabpage(save_curtab))
11495 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011496 }
11497}
11498
11499/*
11500 * "settabwinvar()" function
11501 */
11502 static void
11503f_settabwinvar(typval_T *argvars, typval_T *rettv)
11504{
11505 setwinvar(argvars, rettv, 1);
11506}
11507
11508/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011509 * "settagstack()" function
11510 */
11511 static void
11512f_settagstack(typval_T *argvars, typval_T *rettv)
11513{
11514 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11515 win_T *wp;
11516 dict_T *d;
11517 int action = 'r';
11518
11519 rettv->vval.v_number = -1;
11520
11521 // first argument: window number or id
11522 wp = find_win_by_nr_or_id(&argvars[0]);
11523 if (wp == NULL)
11524 return;
11525
11526 // second argument: dict with items to set in the tag stack
11527 if (argvars[1].v_type != VAR_DICT)
11528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011529 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011530 return;
11531 }
11532 d = argvars[1].vval.v_dict;
11533 if (d == NULL)
11534 return;
11535
11536 // third argument: action - 'a' for append and 'r' for replace.
11537 // default is to replace the stack.
11538 if (argvars[2].v_type == VAR_UNKNOWN)
11539 action = 'r';
11540 else if (argvars[2].v_type == VAR_STRING)
11541 {
11542 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011543 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011544 if (actstr == NULL)
11545 return;
11546 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11547 action = *actstr;
11548 else
11549 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011550 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011551 return;
11552 }
11553 }
11554 else
11555 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011556 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011557 return;
11558 }
11559
11560 if (set_tagstack(wp, d, action) == OK)
11561 rettv->vval.v_number = 0;
11562}
11563
11564/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011565 * "setwinvar()" function
11566 */
11567 static void
11568f_setwinvar(typval_T *argvars, typval_T *rettv)
11569{
11570 setwinvar(argvars, rettv, 0);
11571}
11572
11573#ifdef FEAT_CRYPT
11574/*
11575 * "sha256({string})" function
11576 */
11577 static void
11578f_sha256(typval_T *argvars, typval_T *rettv)
11579{
11580 char_u *p;
11581
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011582 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011583 rettv->vval.v_string = vim_strsave(
11584 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11585 rettv->v_type = VAR_STRING;
11586}
11587#endif /* FEAT_CRYPT */
11588
11589/*
11590 * "shellescape({string})" function
11591 */
11592 static void
11593f_shellescape(typval_T *argvars, typval_T *rettv)
11594{
Bram Moolenaar20615522017-06-05 18:46:26 +020011595 int do_special = non_zero_arg(&argvars[1]);
11596
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011597 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011598 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011599 rettv->v_type = VAR_STRING;
11600}
11601
11602/*
11603 * shiftwidth() function
11604 */
11605 static void
11606f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11607{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011608 rettv->vval.v_number = 0;
11609
11610 if (argvars[0].v_type != VAR_UNKNOWN)
11611 {
11612 long col;
11613
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011614 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011615 if (col < 0)
11616 return; // type error; errmsg already given
11617#ifdef FEAT_VARTABS
11618 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11619 return;
11620#endif
11621 }
11622
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011623 rettv->vval.v_number = get_sw_value(curbuf);
11624}
11625
11626/*
11627 * "simplify()" function
11628 */
11629 static void
11630f_simplify(typval_T *argvars, typval_T *rettv)
11631{
11632 char_u *p;
11633
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011634 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011635 rettv->vval.v_string = vim_strsave(p);
11636 simplify_filename(rettv->vval.v_string); /* simplify in place */
11637 rettv->v_type = VAR_STRING;
11638}
11639
11640#ifdef FEAT_FLOAT
11641/*
11642 * "sin()" function
11643 */
11644 static void
11645f_sin(typval_T *argvars, typval_T *rettv)
11646{
11647 float_T f = 0.0;
11648
11649 rettv->v_type = VAR_FLOAT;
11650 if (get_float_arg(argvars, &f) == OK)
11651 rettv->vval.v_float = sin(f);
11652 else
11653 rettv->vval.v_float = 0.0;
11654}
11655
11656/*
11657 * "sinh()" function
11658 */
11659 static void
11660f_sinh(typval_T *argvars, typval_T *rettv)
11661{
11662 float_T f = 0.0;
11663
11664 rettv->v_type = VAR_FLOAT;
11665 if (get_float_arg(argvars, &f) == OK)
11666 rettv->vval.v_float = sinh(f);
11667 else
11668 rettv->vval.v_float = 0.0;
11669}
11670#endif
11671
Bram Moolenaareae1b912019-05-09 15:12:55 +020011672static int item_compare(const void *s1, const void *s2);
11673static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011674
11675/* struct used in the array that's given to qsort() */
11676typedef struct
11677{
11678 listitem_T *item;
11679 int idx;
11680} sortItem_T;
11681
11682/* struct storing information about current sort */
11683typedef struct
11684{
11685 int item_compare_ic;
11686 int item_compare_numeric;
11687 int item_compare_numbers;
11688#ifdef FEAT_FLOAT
11689 int item_compare_float;
11690#endif
11691 char_u *item_compare_func;
11692 partial_T *item_compare_partial;
11693 dict_T *item_compare_selfdict;
11694 int item_compare_func_err;
11695 int item_compare_keep_zero;
11696} sortinfo_T;
11697static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011698#define ITEM_COMPARE_FAIL 999
11699
11700/*
11701 * Compare functions for f_sort() and f_uniq() below.
11702 */
11703 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011704item_compare(const void *s1, const void *s2)
11705{
11706 sortItem_T *si1, *si2;
11707 typval_T *tv1, *tv2;
11708 char_u *p1, *p2;
11709 char_u *tofree1 = NULL, *tofree2 = NULL;
11710 int res;
11711 char_u numbuf1[NUMBUFLEN];
11712 char_u numbuf2[NUMBUFLEN];
11713
11714 si1 = (sortItem_T *)s1;
11715 si2 = (sortItem_T *)s2;
11716 tv1 = &si1->item->li_tv;
11717 tv2 = &si2->item->li_tv;
11718
11719 if (sortinfo->item_compare_numbers)
11720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011721 varnumber_T v1 = tv_get_number(tv1);
11722 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011723
11724 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11725 }
11726
11727#ifdef FEAT_FLOAT
11728 if (sortinfo->item_compare_float)
11729 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011730 float_T v1 = tv_get_float(tv1);
11731 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011732
11733 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11734 }
11735#endif
11736
11737 /* tv2string() puts quotes around a string and allocates memory. Don't do
11738 * that for string variables. Use a single quote when comparing with a
11739 * non-string to do what the docs promise. */
11740 if (tv1->v_type == VAR_STRING)
11741 {
11742 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11743 p1 = (char_u *)"'";
11744 else
11745 p1 = tv1->vval.v_string;
11746 }
11747 else
11748 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11749 if (tv2->v_type == VAR_STRING)
11750 {
11751 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11752 p2 = (char_u *)"'";
11753 else
11754 p2 = tv2->vval.v_string;
11755 }
11756 else
11757 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11758 if (p1 == NULL)
11759 p1 = (char_u *)"";
11760 if (p2 == NULL)
11761 p2 = (char_u *)"";
11762 if (!sortinfo->item_compare_numeric)
11763 {
11764 if (sortinfo->item_compare_ic)
11765 res = STRICMP(p1, p2);
11766 else
11767 res = STRCMP(p1, p2);
11768 }
11769 else
11770 {
11771 double n1, n2;
11772 n1 = strtod((char *)p1, (char **)&p1);
11773 n2 = strtod((char *)p2, (char **)&p2);
11774 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11775 }
11776
11777 /* When the result would be zero, compare the item indexes. Makes the
11778 * sort stable. */
11779 if (res == 0 && !sortinfo->item_compare_keep_zero)
11780 res = si1->idx > si2->idx ? 1 : -1;
11781
11782 vim_free(tofree1);
11783 vim_free(tofree2);
11784 return res;
11785}
11786
11787 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011788item_compare2(const void *s1, const void *s2)
11789{
11790 sortItem_T *si1, *si2;
11791 int res;
11792 typval_T rettv;
11793 typval_T argv[3];
11794 int dummy;
11795 char_u *func_name;
11796 partial_T *partial = sortinfo->item_compare_partial;
11797
11798 /* shortcut after failure in previous call; compare all items equal */
11799 if (sortinfo->item_compare_func_err)
11800 return 0;
11801
11802 si1 = (sortItem_T *)s1;
11803 si2 = (sortItem_T *)s2;
11804
11805 if (partial == NULL)
11806 func_name = sortinfo->item_compare_func;
11807 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011808 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011809
11810 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11811 * in the copy without changing the original list items. */
11812 copy_tv(&si1->item->li_tv, &argv[0]);
11813 copy_tv(&si2->item->li_tv, &argv[1]);
11814
11815 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020011816 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011817 partial, sortinfo->item_compare_selfdict);
11818 clear_tv(&argv[0]);
11819 clear_tv(&argv[1]);
11820
11821 if (res == FAIL)
11822 res = ITEM_COMPARE_FAIL;
11823 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011824 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011825 if (sortinfo->item_compare_func_err)
11826 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11827 clear_tv(&rettv);
11828
11829 /* When the result would be zero, compare the pointers themselves. Makes
11830 * the sort stable. */
11831 if (res == 0 && !sortinfo->item_compare_keep_zero)
11832 res = si1->idx > si2->idx ? 1 : -1;
11833
11834 return res;
11835}
11836
11837/*
11838 * "sort({list})" function
11839 */
11840 static void
11841do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11842{
11843 list_T *l;
11844 listitem_T *li;
11845 sortItem_T *ptrs;
11846 sortinfo_T *old_sortinfo;
11847 sortinfo_T info;
11848 long len;
11849 long i;
11850
11851 /* Pointer to current info struct used in compare function. Save and
11852 * restore the current one for nested calls. */
11853 old_sortinfo = sortinfo;
11854 sortinfo = &info;
11855
11856 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011857 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011858 else
11859 {
11860 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011861 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011862 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11863 TRUE))
11864 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011865 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011866
11867 len = list_len(l);
11868 if (len <= 1)
11869 goto theend; /* short list sorts pretty quickly */
11870
11871 info.item_compare_ic = FALSE;
11872 info.item_compare_numeric = FALSE;
11873 info.item_compare_numbers = FALSE;
11874#ifdef FEAT_FLOAT
11875 info.item_compare_float = FALSE;
11876#endif
11877 info.item_compare_func = NULL;
11878 info.item_compare_partial = NULL;
11879 info.item_compare_selfdict = NULL;
11880 if (argvars[1].v_type != VAR_UNKNOWN)
11881 {
11882 /* optional second argument: {func} */
11883 if (argvars[1].v_type == VAR_FUNC)
11884 info.item_compare_func = argvars[1].vval.v_string;
11885 else if (argvars[1].v_type == VAR_PARTIAL)
11886 info.item_compare_partial = argvars[1].vval.v_partial;
11887 else
11888 {
11889 int error = FALSE;
11890
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011891 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011892 if (error)
11893 goto theend; /* type error; errmsg already given */
11894 if (i == 1)
11895 info.item_compare_ic = TRUE;
11896 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011897 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011898 else if (i != 0)
11899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011900 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011901 goto theend;
11902 }
11903 if (info.item_compare_func != NULL)
11904 {
11905 if (*info.item_compare_func == NUL)
11906 {
11907 /* empty string means default sort */
11908 info.item_compare_func = NULL;
11909 }
11910 else if (STRCMP(info.item_compare_func, "n") == 0)
11911 {
11912 info.item_compare_func = NULL;
11913 info.item_compare_numeric = TRUE;
11914 }
11915 else if (STRCMP(info.item_compare_func, "N") == 0)
11916 {
11917 info.item_compare_func = NULL;
11918 info.item_compare_numbers = TRUE;
11919 }
11920#ifdef FEAT_FLOAT
11921 else if (STRCMP(info.item_compare_func, "f") == 0)
11922 {
11923 info.item_compare_func = NULL;
11924 info.item_compare_float = TRUE;
11925 }
11926#endif
11927 else if (STRCMP(info.item_compare_func, "i") == 0)
11928 {
11929 info.item_compare_func = NULL;
11930 info.item_compare_ic = TRUE;
11931 }
11932 }
11933 }
11934
11935 if (argvars[2].v_type != VAR_UNKNOWN)
11936 {
11937 /* optional third argument: {dict} */
11938 if (argvars[2].v_type != VAR_DICT)
11939 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011940 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011941 goto theend;
11942 }
11943 info.item_compare_selfdict = argvars[2].vval.v_dict;
11944 }
11945 }
11946
11947 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011948 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011949 if (ptrs == NULL)
11950 goto theend;
11951
11952 i = 0;
11953 if (sort)
11954 {
11955 /* sort(): ptrs will be the list to sort */
11956 for (li = l->lv_first; li != NULL; li = li->li_next)
11957 {
11958 ptrs[i].item = li;
11959 ptrs[i].idx = i;
11960 ++i;
11961 }
11962
11963 info.item_compare_func_err = FALSE;
11964 info.item_compare_keep_zero = FALSE;
11965 /* test the compare function */
11966 if ((info.item_compare_func != NULL
11967 || info.item_compare_partial != NULL)
11968 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11969 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011970 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011971 else
11972 {
11973 /* Sort the array with item pointers. */
11974 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11975 info.item_compare_func == NULL
11976 && info.item_compare_partial == NULL
11977 ? item_compare : item_compare2);
11978
11979 if (!info.item_compare_func_err)
11980 {
11981 /* Clear the List and append the items in sorted order. */
11982 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11983 l->lv_len = 0;
11984 for (i = 0; i < len; ++i)
11985 list_append(l, ptrs[i].item);
11986 }
11987 }
11988 }
11989 else
11990 {
11991 int (*item_compare_func_ptr)(const void *, const void *);
11992
11993 /* f_uniq(): ptrs will be a stack of items to remove */
11994 info.item_compare_func_err = FALSE;
11995 info.item_compare_keep_zero = TRUE;
11996 item_compare_func_ptr = info.item_compare_func != NULL
11997 || info.item_compare_partial != NULL
11998 ? item_compare2 : item_compare;
11999
12000 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12001 li = li->li_next)
12002 {
12003 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12004 == 0)
12005 ptrs[i++].item = li;
12006 if (info.item_compare_func_err)
12007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012008 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012009 break;
12010 }
12011 }
12012
12013 if (!info.item_compare_func_err)
12014 {
12015 while (--i >= 0)
12016 {
12017 li = ptrs[i].item->li_next;
12018 ptrs[i].item->li_next = li->li_next;
12019 if (li->li_next != NULL)
12020 li->li_next->li_prev = ptrs[i].item;
12021 else
12022 l->lv_last = ptrs[i].item;
12023 list_fix_watch(l, li);
12024 listitem_free(li);
12025 l->lv_len--;
12026 }
12027 }
12028 }
12029
12030 vim_free(ptrs);
12031 }
12032theend:
12033 sortinfo = old_sortinfo;
12034}
12035
12036/*
12037 * "sort({list})" function
12038 */
12039 static void
12040f_sort(typval_T *argvars, typval_T *rettv)
12041{
12042 do_sort_uniq(argvars, rettv, TRUE);
12043}
12044
12045/*
12046 * "uniq({list})" function
12047 */
12048 static void
12049f_uniq(typval_T *argvars, typval_T *rettv)
12050{
12051 do_sort_uniq(argvars, rettv, FALSE);
12052}
12053
12054/*
12055 * "soundfold({word})" function
12056 */
12057 static void
12058f_soundfold(typval_T *argvars, typval_T *rettv)
12059{
12060 char_u *s;
12061
12062 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012063 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012064#ifdef FEAT_SPELL
12065 rettv->vval.v_string = eval_soundfold(s);
12066#else
12067 rettv->vval.v_string = vim_strsave(s);
12068#endif
12069}
12070
12071/*
12072 * "spellbadword()" function
12073 */
12074 static void
12075f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12076{
12077 char_u *word = (char_u *)"";
12078 hlf_T attr = HLF_COUNT;
12079 int len = 0;
12080
12081 if (rettv_list_alloc(rettv) == FAIL)
12082 return;
12083
12084#ifdef FEAT_SPELL
12085 if (argvars[0].v_type == VAR_UNKNOWN)
12086 {
12087 /* Find the start and length of the badly spelled word. */
12088 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12089 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012090 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012091 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012092 curwin->w_set_curswant = TRUE;
12093 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012094 }
12095 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12096 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012097 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012098 int capcol = -1;
12099
12100 if (str != NULL)
12101 {
12102 /* Check the argument for spelling. */
12103 while (*str != NUL)
12104 {
12105 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12106 if (attr != HLF_COUNT)
12107 {
12108 word = str;
12109 break;
12110 }
12111 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012112 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012113 }
12114 }
12115 }
12116#endif
12117
12118 list_append_string(rettv->vval.v_list, word, len);
12119 list_append_string(rettv->vval.v_list, (char_u *)(
12120 attr == HLF_SPB ? "bad" :
12121 attr == HLF_SPR ? "rare" :
12122 attr == HLF_SPL ? "local" :
12123 attr == HLF_SPC ? "caps" :
12124 ""), -1);
12125}
12126
12127/*
12128 * "spellsuggest()" function
12129 */
12130 static void
12131f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12132{
12133#ifdef FEAT_SPELL
12134 char_u *str;
12135 int typeerr = FALSE;
12136 int maxcount;
12137 garray_T ga;
12138 int i;
12139 listitem_T *li;
12140 int need_capital = FALSE;
12141#endif
12142
12143 if (rettv_list_alloc(rettv) == FAIL)
12144 return;
12145
12146#ifdef FEAT_SPELL
12147 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12148 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012149 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012150 if (argvars[1].v_type != VAR_UNKNOWN)
12151 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012152 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012153 if (maxcount <= 0)
12154 return;
12155 if (argvars[2].v_type != VAR_UNKNOWN)
12156 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012157 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012158 if (typeerr)
12159 return;
12160 }
12161 }
12162 else
12163 maxcount = 25;
12164
12165 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12166
12167 for (i = 0; i < ga.ga_len; ++i)
12168 {
12169 str = ((char_u **)ga.ga_data)[i];
12170
12171 li = listitem_alloc();
12172 if (li == NULL)
12173 vim_free(str);
12174 else
12175 {
12176 li->li_tv.v_type = VAR_STRING;
12177 li->li_tv.v_lock = 0;
12178 li->li_tv.vval.v_string = str;
12179 list_append(rettv->vval.v_list, li);
12180 }
12181 }
12182 ga_clear(&ga);
12183 }
12184#endif
12185}
12186
12187 static void
12188f_split(typval_T *argvars, typval_T *rettv)
12189{
12190 char_u *str;
12191 char_u *end;
12192 char_u *pat = NULL;
12193 regmatch_T regmatch;
12194 char_u patbuf[NUMBUFLEN];
12195 char_u *save_cpo;
12196 int match;
12197 colnr_T col = 0;
12198 int keepempty = FALSE;
12199 int typeerr = FALSE;
12200
12201 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12202 save_cpo = p_cpo;
12203 p_cpo = (char_u *)"";
12204
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012205 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012206 if (argvars[1].v_type != VAR_UNKNOWN)
12207 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012208 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012209 if (pat == NULL)
12210 typeerr = TRUE;
12211 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012212 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012213 }
12214 if (pat == NULL || *pat == NUL)
12215 pat = (char_u *)"[\\x01- ]\\+";
12216
12217 if (rettv_list_alloc(rettv) == FAIL)
12218 return;
12219 if (typeerr)
12220 return;
12221
12222 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12223 if (regmatch.regprog != NULL)
12224 {
12225 regmatch.rm_ic = FALSE;
12226 while (*str != NUL || keepempty)
12227 {
12228 if (*str == NUL)
12229 match = FALSE; /* empty item at the end */
12230 else
12231 match = vim_regexec_nl(&regmatch, str, col);
12232 if (match)
12233 end = regmatch.startp[0];
12234 else
12235 end = str + STRLEN(str);
12236 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12237 && *str != NUL && match && end < regmatch.endp[0]))
12238 {
12239 if (list_append_string(rettv->vval.v_list, str,
12240 (int)(end - str)) == FAIL)
12241 break;
12242 }
12243 if (!match)
12244 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012245 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012246 if (regmatch.endp[0] > str)
12247 col = 0;
12248 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012249 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012250 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012251 str = regmatch.endp[0];
12252 }
12253
12254 vim_regfree(regmatch.regprog);
12255 }
12256
12257 p_cpo = save_cpo;
12258}
12259
12260#ifdef FEAT_FLOAT
12261/*
12262 * "sqrt()" function
12263 */
12264 static void
12265f_sqrt(typval_T *argvars, typval_T *rettv)
12266{
12267 float_T f = 0.0;
12268
12269 rettv->v_type = VAR_FLOAT;
12270 if (get_float_arg(argvars, &f) == OK)
12271 rettv->vval.v_float = sqrt(f);
12272 else
12273 rettv->vval.v_float = 0.0;
12274}
12275
12276/*
12277 * "str2float()" function
12278 */
12279 static void
12280f_str2float(typval_T *argvars, typval_T *rettv)
12281{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012282 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012283 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012284
Bram Moolenaar08243d22017-01-10 16:12:29 +010012285 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012286 p = skipwhite(p + 1);
12287 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012288 if (isneg)
12289 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012290 rettv->v_type = VAR_FLOAT;
12291}
12292#endif
12293
12294/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012295 * "str2list()" function
12296 */
12297 static void
12298f_str2list(typval_T *argvars, typval_T *rettv)
12299{
12300 char_u *p;
12301 int utf8 = FALSE;
12302
12303 if (rettv_list_alloc(rettv) == FAIL)
12304 return;
12305
12306 if (argvars[1].v_type != VAR_UNKNOWN)
12307 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12308
12309 p = tv_get_string(&argvars[0]);
12310
12311 if (has_mbyte || utf8)
12312 {
12313 int (*ptr2len)(char_u *);
12314 int (*ptr2char)(char_u *);
12315
12316 if (utf8 || enc_utf8)
12317 {
12318 ptr2len = utf_ptr2len;
12319 ptr2char = utf_ptr2char;
12320 }
12321 else
12322 {
12323 ptr2len = mb_ptr2len;
12324 ptr2char = mb_ptr2char;
12325 }
12326
12327 for ( ; *p != NUL; p += (*ptr2len)(p))
12328 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
12329 }
12330 else
12331 for ( ; *p != NUL; ++p)
12332 list_append_number(rettv->vval.v_list, *p);
12333}
12334
12335/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012336 * "str2nr()" function
12337 */
12338 static void
12339f_str2nr(typval_T *argvars, typval_T *rettv)
12340{
12341 int base = 10;
12342 char_u *p;
12343 varnumber_T n;
12344 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012345 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012346
12347 if (argvars[1].v_type != VAR_UNKNOWN)
12348 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012349 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012350 if (base != 2 && base != 8 && base != 10 && base != 16)
12351 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012352 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012353 return;
12354 }
12355 }
12356
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012357 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012358 isneg = (*p == '-');
12359 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012360 p = skipwhite(p + 1);
12361 switch (base)
12362 {
12363 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12364 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12365 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12366 default: what = 0;
12367 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020012368 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
12369 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010012370 if (isneg)
12371 rettv->vval.v_number = -n;
12372 else
12373 rettv->vval.v_number = n;
12374
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012375}
12376
12377#ifdef HAVE_STRFTIME
12378/*
12379 * "strftime({format}[, {time}])" function
12380 */
12381 static void
12382f_strftime(typval_T *argvars, typval_T *rettv)
12383{
12384 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020012385 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012386 struct tm *curtime;
12387 time_t seconds;
12388 char_u *p;
12389
12390 rettv->v_type = VAR_STRING;
12391
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012392 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012393 if (argvars[1].v_type == VAR_UNKNOWN)
12394 seconds = time(NULL);
12395 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012396 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020012397 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012398 /* MSVC returns NULL for an invalid value of seconds. */
12399 if (curtime == NULL)
12400 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12401 else
12402 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012403 vimconv_T conv;
12404 char_u *enc;
12405
12406 conv.vc_type = CONV_NONE;
12407 enc = enc_locale();
12408 convert_setup(&conv, p_enc, enc);
12409 if (conv.vc_type != CONV_NONE)
12410 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012411 if (p != NULL)
12412 (void)strftime((char *)result_buf, sizeof(result_buf),
12413 (char *)p, curtime);
12414 else
12415 result_buf[0] = NUL;
12416
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012417 if (conv.vc_type != CONV_NONE)
12418 vim_free(p);
12419 convert_setup(&conv, enc, p_enc);
12420 if (conv.vc_type != CONV_NONE)
12421 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12422 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012423 rettv->vval.v_string = vim_strsave(result_buf);
12424
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012425 /* Release conversion descriptors */
12426 convert_setup(&conv, NULL, NULL);
12427 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012428 }
12429}
12430#endif
12431
12432/*
12433 * "strgetchar()" function
12434 */
12435 static void
12436f_strgetchar(typval_T *argvars, typval_T *rettv)
12437{
12438 char_u *str;
12439 int len;
12440 int error = FALSE;
12441 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012442 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012443
12444 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012445 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012446 if (str == NULL)
12447 return;
12448 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012449 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450 if (error)
12451 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012452
Bram Moolenaar13505972019-01-24 15:04:48 +010012453 while (charidx >= 0 && byteidx < len)
12454 {
12455 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012456 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012457 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12458 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012459 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012460 --charidx;
12461 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012462 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012463}
12464
12465/*
12466 * "stridx()" function
12467 */
12468 static void
12469f_stridx(typval_T *argvars, typval_T *rettv)
12470{
12471 char_u buf[NUMBUFLEN];
12472 char_u *needle;
12473 char_u *haystack;
12474 char_u *save_haystack;
12475 char_u *pos;
12476 int start_idx;
12477
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012478 needle = tv_get_string_chk(&argvars[1]);
12479 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012480 rettv->vval.v_number = -1;
12481 if (needle == NULL || haystack == NULL)
12482 return; /* type error; errmsg already given */
12483
12484 if (argvars[2].v_type != VAR_UNKNOWN)
12485 {
12486 int error = FALSE;
12487
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012488 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012489 if (error || start_idx >= (int)STRLEN(haystack))
12490 return;
12491 if (start_idx >= 0)
12492 haystack += start_idx;
12493 }
12494
12495 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12496 if (pos != NULL)
12497 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12498}
12499
12500/*
12501 * "string()" function
12502 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012503 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012504f_string(typval_T *argvars, typval_T *rettv)
12505{
12506 char_u *tofree;
12507 char_u numbuf[NUMBUFLEN];
12508
12509 rettv->v_type = VAR_STRING;
12510 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12511 get_copyID());
12512 /* Make a copy if we have a value but it's not in allocated memory. */
12513 if (rettv->vval.v_string != NULL && tofree == NULL)
12514 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12515}
12516
12517/*
12518 * "strlen()" function
12519 */
12520 static void
12521f_strlen(typval_T *argvars, typval_T *rettv)
12522{
12523 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012524 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012525}
12526
12527/*
12528 * "strchars()" function
12529 */
12530 static void
12531f_strchars(typval_T *argvars, typval_T *rettv)
12532{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012533 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012534 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012535 varnumber_T len = 0;
12536 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012537
12538 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012539 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012540 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012541 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012542 else
12543 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012544 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12545 while (*s != NUL)
12546 {
12547 func_mb_ptr2char_adv(&s);
12548 ++len;
12549 }
12550 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012551 }
12552}
12553
12554/*
12555 * "strdisplaywidth()" function
12556 */
12557 static void
12558f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12559{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012560 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012561 int col = 0;
12562
12563 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012564 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012565
12566 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12567}
12568
12569/*
12570 * "strwidth()" function
12571 */
12572 static void
12573f_strwidth(typval_T *argvars, typval_T *rettv)
12574{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012575 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012576
Bram Moolenaar13505972019-01-24 15:04:48 +010012577 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012578}
12579
12580/*
12581 * "strcharpart()" function
12582 */
12583 static void
12584f_strcharpart(typval_T *argvars, typval_T *rettv)
12585{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012586 char_u *p;
12587 int nchar;
12588 int nbyte = 0;
12589 int charlen;
12590 int len = 0;
12591 int slen;
12592 int error = FALSE;
12593
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012594 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012595 slen = (int)STRLEN(p);
12596
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012597 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012598 if (!error)
12599 {
12600 if (nchar > 0)
12601 while (nchar > 0 && nbyte < slen)
12602 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012603 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012604 --nchar;
12605 }
12606 else
12607 nbyte = nchar;
12608 if (argvars[2].v_type != VAR_UNKNOWN)
12609 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012610 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012611 while (charlen > 0 && nbyte + len < slen)
12612 {
12613 int off = nbyte + len;
12614
12615 if (off < 0)
12616 len += 1;
12617 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012618 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012619 --charlen;
12620 }
12621 }
12622 else
12623 len = slen - nbyte; /* default: all bytes that are available. */
12624 }
12625
12626 /*
12627 * Only return the overlap between the specified part and the actual
12628 * string.
12629 */
12630 if (nbyte < 0)
12631 {
12632 len += nbyte;
12633 nbyte = 0;
12634 }
12635 else if (nbyte > slen)
12636 nbyte = slen;
12637 if (len < 0)
12638 len = 0;
12639 else if (nbyte + len > slen)
12640 len = slen - nbyte;
12641
12642 rettv->v_type = VAR_STRING;
12643 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012644}
12645
12646/*
12647 * "strpart()" function
12648 */
12649 static void
12650f_strpart(typval_T *argvars, typval_T *rettv)
12651{
12652 char_u *p;
12653 int n;
12654 int len;
12655 int slen;
12656 int error = FALSE;
12657
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012658 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012659 slen = (int)STRLEN(p);
12660
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012661 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012662 if (error)
12663 len = 0;
12664 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012665 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012666 else
12667 len = slen - n; /* default len: all bytes that are available. */
12668
12669 /*
12670 * Only return the overlap between the specified part and the actual
12671 * string.
12672 */
12673 if (n < 0)
12674 {
12675 len += n;
12676 n = 0;
12677 }
12678 else if (n > slen)
12679 n = slen;
12680 if (len < 0)
12681 len = 0;
12682 else if (n + len > slen)
12683 len = slen - n;
12684
12685 rettv->v_type = VAR_STRING;
12686 rettv->vval.v_string = vim_strnsave(p + n, len);
12687}
12688
12689/*
12690 * "strridx()" function
12691 */
12692 static void
12693f_strridx(typval_T *argvars, typval_T *rettv)
12694{
12695 char_u buf[NUMBUFLEN];
12696 char_u *needle;
12697 char_u *haystack;
12698 char_u *rest;
12699 char_u *lastmatch = NULL;
12700 int haystack_len, end_idx;
12701
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012702 needle = tv_get_string_chk(&argvars[1]);
12703 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012704
12705 rettv->vval.v_number = -1;
12706 if (needle == NULL || haystack == NULL)
12707 return; /* type error; errmsg already given */
12708
12709 haystack_len = (int)STRLEN(haystack);
12710 if (argvars[2].v_type != VAR_UNKNOWN)
12711 {
12712 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012713 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012714 if (end_idx < 0)
12715 return; /* can never find a match */
12716 }
12717 else
12718 end_idx = haystack_len;
12719
12720 if (*needle == NUL)
12721 {
12722 /* Empty string matches past the end. */
12723 lastmatch = haystack + end_idx;
12724 }
12725 else
12726 {
12727 for (rest = haystack; *rest != '\0'; ++rest)
12728 {
12729 rest = (char_u *)strstr((char *)rest, (char *)needle);
12730 if (rest == NULL || rest > haystack + end_idx)
12731 break;
12732 lastmatch = rest;
12733 }
12734 }
12735
12736 if (lastmatch == NULL)
12737 rettv->vval.v_number = -1;
12738 else
12739 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12740}
12741
12742/*
12743 * "strtrans()" function
12744 */
12745 static void
12746f_strtrans(typval_T *argvars, typval_T *rettv)
12747{
12748 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012749 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012750}
12751
12752/*
12753 * "submatch()" function
12754 */
12755 static void
12756f_submatch(typval_T *argvars, typval_T *rettv)
12757{
12758 int error = FALSE;
12759 int no;
12760 int retList = 0;
12761
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012762 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012763 if (error)
12764 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012765 if (no < 0 || no >= NSUBEXP)
12766 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012767 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012768 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012769 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012770 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012771 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012772 if (error)
12773 return;
12774
12775 if (retList == 0)
12776 {
12777 rettv->v_type = VAR_STRING;
12778 rettv->vval.v_string = reg_submatch(no);
12779 }
12780 else
12781 {
12782 rettv->v_type = VAR_LIST;
12783 rettv->vval.v_list = reg_submatch_list(no);
12784 }
12785}
12786
12787/*
12788 * "substitute()" function
12789 */
12790 static void
12791f_substitute(typval_T *argvars, typval_T *rettv)
12792{
12793 char_u patbuf[NUMBUFLEN];
12794 char_u subbuf[NUMBUFLEN];
12795 char_u flagsbuf[NUMBUFLEN];
12796
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012797 char_u *str = tv_get_string_chk(&argvars[0]);
12798 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012799 char_u *sub = NULL;
12800 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012801 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012802
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012803 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12804 expr = &argvars[2];
12805 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012806 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012807
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012808 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012809 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12810 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012811 rettv->vval.v_string = NULL;
12812 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012813 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012814}
12815
12816/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012817 * "swapinfo(swap_filename)" function
12818 */
12819 static void
12820f_swapinfo(typval_T *argvars, typval_T *rettv)
12821{
12822 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012823 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012824}
12825
12826/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012827 * "swapname(expr)" function
12828 */
12829 static void
12830f_swapname(typval_T *argvars, typval_T *rettv)
12831{
12832 buf_T *buf;
12833
12834 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010012835 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020012836 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12837 || buf->b_ml.ml_mfp->mf_fname == NULL)
12838 rettv->vval.v_string = NULL;
12839 else
12840 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12841}
12842
12843/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012844 * "synID(lnum, col, trans)" function
12845 */
12846 static void
12847f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12848{
12849 int id = 0;
12850#ifdef FEAT_SYN_HL
12851 linenr_T lnum;
12852 colnr_T col;
12853 int trans;
12854 int transerr = FALSE;
12855
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012856 lnum = tv_get_lnum(argvars); /* -1 on type error */
12857 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12858 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012859
12860 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12861 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12862 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12863#endif
12864
12865 rettv->vval.v_number = id;
12866}
12867
12868/*
12869 * "synIDattr(id, what [, mode])" function
12870 */
12871 static void
12872f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12873{
12874 char_u *p = NULL;
12875#ifdef FEAT_SYN_HL
12876 int id;
12877 char_u *what;
12878 char_u *mode;
12879 char_u modebuf[NUMBUFLEN];
12880 int modec;
12881
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012882 id = (int)tv_get_number(&argvars[0]);
12883 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012884 if (argvars[2].v_type != VAR_UNKNOWN)
12885 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012886 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012887 modec = TOLOWER_ASC(mode[0]);
12888 if (modec != 't' && modec != 'c' && modec != 'g')
12889 modec = 0; /* replace invalid with current */
12890 }
12891 else
12892 {
12893#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12894 if (USE_24BIT)
12895 modec = 'g';
12896 else
12897#endif
12898 if (t_colors > 1)
12899 modec = 'c';
12900 else
12901 modec = 't';
12902 }
12903
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012904 switch (TOLOWER_ASC(what[0]))
12905 {
12906 case 'b':
12907 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12908 p = highlight_color(id, what, modec);
12909 else /* bold */
12910 p = highlight_has_attr(id, HL_BOLD, modec);
12911 break;
12912
12913 case 'f': /* fg[#] or font */
12914 p = highlight_color(id, what, modec);
12915 break;
12916
12917 case 'i':
12918 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12919 p = highlight_has_attr(id, HL_INVERSE, modec);
12920 else /* italic */
12921 p = highlight_has_attr(id, HL_ITALIC, modec);
12922 break;
12923
12924 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012925 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012926 break;
12927
12928 case 'r': /* reverse */
12929 p = highlight_has_attr(id, HL_INVERSE, modec);
12930 break;
12931
12932 case 's':
12933 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12934 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012935 /* strikeout */
12936 else if (TOLOWER_ASC(what[1]) == 't' &&
12937 TOLOWER_ASC(what[2]) == 'r')
12938 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012939 else /* standout */
12940 p = highlight_has_attr(id, HL_STANDOUT, modec);
12941 break;
12942
12943 case 'u':
12944 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12945 /* underline */
12946 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12947 else
12948 /* undercurl */
12949 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12950 break;
12951 }
12952
12953 if (p != NULL)
12954 p = vim_strsave(p);
12955#endif
12956 rettv->v_type = VAR_STRING;
12957 rettv->vval.v_string = p;
12958}
12959
12960/*
12961 * "synIDtrans(id)" function
12962 */
12963 static void
12964f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12965{
12966 int id;
12967
12968#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012969 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012970
12971 if (id > 0)
12972 id = syn_get_final_id(id);
12973 else
12974#endif
12975 id = 0;
12976
12977 rettv->vval.v_number = id;
12978}
12979
12980/*
12981 * "synconcealed(lnum, col)" function
12982 */
12983 static void
12984f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12985{
12986#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12987 linenr_T lnum;
12988 colnr_T col;
12989 int syntax_flags = 0;
12990 int cchar;
12991 int matchid = 0;
12992 char_u str[NUMBUFLEN];
12993#endif
12994
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012995 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012996
12997#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012998 lnum = tv_get_lnum(argvars); /* -1 on type error */
12999 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013000
13001 vim_memset(str, NUL, sizeof(str));
13002
13003 if (rettv_list_alloc(rettv) != FAIL)
13004 {
13005 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13006 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13007 && curwin->w_p_cole > 0)
13008 {
13009 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13010 syntax_flags = get_syntax_info(&matchid);
13011
13012 /* get the conceal character */
13013 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13014 {
13015 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013016 if (cchar == NUL && curwin->w_p_cole == 1)
13017 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013018 if (cchar != NUL)
13019 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013020 if (has_mbyte)
13021 (*mb_char2bytes)(cchar, str);
13022 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013023 str[0] = cchar;
13024 }
13025 }
13026 }
13027
13028 list_append_number(rettv->vval.v_list,
13029 (syntax_flags & HL_CONCEAL) != 0);
13030 /* -1 to auto-determine strlen */
13031 list_append_string(rettv->vval.v_list, str, -1);
13032 list_append_number(rettv->vval.v_list, matchid);
13033 }
13034#endif
13035}
13036
13037/*
13038 * "synstack(lnum, col)" function
13039 */
13040 static void
13041f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13042{
13043#ifdef FEAT_SYN_HL
13044 linenr_T lnum;
13045 colnr_T col;
13046 int i;
13047 int id;
13048#endif
13049
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013050 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013051
13052#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013053 lnum = tv_get_lnum(argvars); /* -1 on type error */
13054 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013055
13056 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13057 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13058 && rettv_list_alloc(rettv) != FAIL)
13059 {
13060 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13061 for (i = 0; ; ++i)
13062 {
13063 id = syn_get_stack_item(i);
13064 if (id < 0)
13065 break;
13066 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13067 break;
13068 }
13069 }
13070#endif
13071}
13072
13073 static void
13074get_cmd_output_as_rettv(
13075 typval_T *argvars,
13076 typval_T *rettv,
13077 int retlist)
13078{
13079 char_u *res = NULL;
13080 char_u *p;
13081 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013082 int err = FALSE;
13083 FILE *fd;
13084 list_T *list = NULL;
13085 int flags = SHELL_SILENT;
13086
13087 rettv->v_type = VAR_STRING;
13088 rettv->vval.v_string = NULL;
13089 if (check_restricted() || check_secure())
13090 goto errret;
13091
13092 if (argvars[1].v_type != VAR_UNKNOWN)
13093 {
13094 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013095 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013096 * command.
13097 */
13098 if ((infile = vim_tempname('i', TRUE)) == NULL)
13099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013100 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013101 goto errret;
13102 }
13103
13104 fd = mch_fopen((char *)infile, WRITEBIN);
13105 if (fd == NULL)
13106 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013107 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013108 goto errret;
13109 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013110 if (argvars[1].v_type == VAR_NUMBER)
13111 {
13112 linenr_T lnum;
13113 buf_T *buf;
13114
13115 buf = buflist_findnr(argvars[1].vval.v_number);
13116 if (buf == NULL)
13117 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013118 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013119 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013120 goto errret;
13121 }
13122
13123 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13124 {
13125 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13126 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13127 {
13128 err = TRUE;
13129 break;
13130 }
13131 if (putc(NL, fd) == EOF)
13132 {
13133 err = TRUE;
13134 break;
13135 }
13136 }
13137 }
13138 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013139 {
13140 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13141 err = TRUE;
13142 }
13143 else
13144 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013145 size_t len;
13146 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013147
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013148 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013149 if (p == NULL)
13150 {
13151 fclose(fd);
13152 goto errret; /* type error; errmsg already given */
13153 }
13154 len = STRLEN(p);
13155 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13156 err = TRUE;
13157 }
13158 if (fclose(fd) != 0)
13159 err = TRUE;
13160 if (err)
13161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013162 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013163 goto errret;
13164 }
13165 }
13166
13167 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13168 * echoes typeahead, that messes up the display. */
13169 if (!msg_silent)
13170 flags += SHELL_COOKED;
13171
13172 if (retlist)
13173 {
13174 int len;
13175 listitem_T *li;
13176 char_u *s = NULL;
13177 char_u *start;
13178 char_u *end;
13179 int i;
13180
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013181 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013182 if (res == NULL)
13183 goto errret;
13184
13185 list = list_alloc();
13186 if (list == NULL)
13187 goto errret;
13188
13189 for (i = 0; i < len; ++i)
13190 {
13191 start = res + i;
13192 while (i < len && res[i] != NL)
13193 ++i;
13194 end = res + i;
13195
Bram Moolenaar964b3742019-05-24 18:54:09 +020013196 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013197 if (s == NULL)
13198 goto errret;
13199
13200 for (p = s; start < end; ++p, ++start)
13201 *p = *start == NUL ? NL : *start;
13202 *p = NUL;
13203
13204 li = listitem_alloc();
13205 if (li == NULL)
13206 {
13207 vim_free(s);
13208 goto errret;
13209 }
13210 li->li_tv.v_type = VAR_STRING;
13211 li->li_tv.v_lock = 0;
13212 li->li_tv.vval.v_string = s;
13213 list_append(list, li);
13214 }
13215
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013216 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013217 list = NULL;
13218 }
13219 else
13220 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013221 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013222#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013223 /* translate <CR><NL> into <NL> */
13224 if (res != NULL)
13225 {
13226 char_u *s, *d;
13227
13228 d = res;
13229 for (s = res; *s; ++s)
13230 {
13231 if (s[0] == CAR && s[1] == NL)
13232 ++s;
13233 *d++ = *s;
13234 }
13235 *d = NUL;
13236 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013237#endif
13238 rettv->vval.v_string = res;
13239 res = NULL;
13240 }
13241
13242errret:
13243 if (infile != NULL)
13244 {
13245 mch_remove(infile);
13246 vim_free(infile);
13247 }
13248 if (res != NULL)
13249 vim_free(res);
13250 if (list != NULL)
13251 list_free(list);
13252}
13253
13254/*
13255 * "system()" function
13256 */
13257 static void
13258f_system(typval_T *argvars, typval_T *rettv)
13259{
13260 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13261}
13262
13263/*
13264 * "systemlist()" function
13265 */
13266 static void
13267f_systemlist(typval_T *argvars, typval_T *rettv)
13268{
13269 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13270}
13271
13272/*
13273 * "tabpagebuflist()" function
13274 */
13275 static void
13276f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13277{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013278 tabpage_T *tp;
13279 win_T *wp = NULL;
13280
13281 if (argvars[0].v_type == VAR_UNKNOWN)
13282 wp = firstwin;
13283 else
13284 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013285 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013286 if (tp != NULL)
13287 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13288 }
13289 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13290 {
13291 for (; wp != NULL; wp = wp->w_next)
13292 if (list_append_number(rettv->vval.v_list,
13293 wp->w_buffer->b_fnum) == FAIL)
13294 break;
13295 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013296}
13297
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013298/*
13299 * "tabpagenr()" function
13300 */
13301 static void
13302f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13303{
13304 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013305 char_u *arg;
13306
13307 if (argvars[0].v_type != VAR_UNKNOWN)
13308 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013309 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013310 nr = 0;
13311 if (arg != NULL)
13312 {
13313 if (STRCMP(arg, "$") == 0)
13314 nr = tabpage_index(NULL) - 1;
13315 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013316 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013317 }
13318 }
13319 else
13320 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013321 rettv->vval.v_number = nr;
13322}
13323
13324
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013325/*
13326 * Common code for tabpagewinnr() and winnr().
13327 */
13328 static int
13329get_winnr(tabpage_T *tp, typval_T *argvar)
13330{
13331 win_T *twin;
13332 int nr = 1;
13333 win_T *wp;
13334 char_u *arg;
13335
13336 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13337 if (argvar->v_type != VAR_UNKNOWN)
13338 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013339 int invalid_arg = FALSE;
13340
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013341 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013342 if (arg == NULL)
13343 nr = 0; /* type error; errmsg already given */
13344 else if (STRCMP(arg, "$") == 0)
13345 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13346 else if (STRCMP(arg, "#") == 0)
13347 {
13348 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13349 if (twin == NULL)
13350 nr = 0;
13351 }
13352 else
13353 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013354 long count;
13355 char_u *endp;
13356
13357 // Extract the window count (if specified). e.g. winnr('3j')
13358 count = strtol((char *)arg, (char **)&endp, 10);
13359 if (count <= 0)
13360 count = 1; // if count is not specified, default to 1
13361 if (endp != NULL && *endp != '\0')
13362 {
13363 if (STRCMP(endp, "j") == 0)
13364 twin = win_vert_neighbor(tp, twin, FALSE, count);
13365 else if (STRCMP(endp, "k") == 0)
13366 twin = win_vert_neighbor(tp, twin, TRUE, count);
13367 else if (STRCMP(endp, "h") == 0)
13368 twin = win_horz_neighbor(tp, twin, TRUE, count);
13369 else if (STRCMP(endp, "l") == 0)
13370 twin = win_horz_neighbor(tp, twin, FALSE, count);
13371 else
13372 invalid_arg = TRUE;
13373 }
13374 else
13375 invalid_arg = TRUE;
13376 }
13377
13378 if (invalid_arg)
13379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013380 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013381 nr = 0;
13382 }
13383 }
13384
13385 if (nr > 0)
13386 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13387 wp != twin; wp = wp->w_next)
13388 {
13389 if (wp == NULL)
13390 {
13391 /* didn't find it in this tabpage */
13392 nr = 0;
13393 break;
13394 }
13395 ++nr;
13396 }
13397 return nr;
13398}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013399
13400/*
13401 * "tabpagewinnr()" function
13402 */
13403 static void
13404f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13405{
13406 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013407 tabpage_T *tp;
13408
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013409 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013410 if (tp == NULL)
13411 nr = 0;
13412 else
13413 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013414 rettv->vval.v_number = nr;
13415}
13416
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013417/*
13418 * "tagfiles()" function
13419 */
13420 static void
13421f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13422{
13423 char_u *fname;
13424 tagname_T tn;
13425 int first;
13426
13427 if (rettv_list_alloc(rettv) == FAIL)
13428 return;
13429 fname = alloc(MAXPATHL);
13430 if (fname == NULL)
13431 return;
13432
13433 for (first = TRUE; ; first = FALSE)
13434 if (get_tagfname(&tn, first, fname) == FAIL
13435 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13436 break;
13437 tagname_free(&tn);
13438 vim_free(fname);
13439}
13440
13441/*
13442 * "taglist()" function
13443 */
13444 static void
13445f_taglist(typval_T *argvars, typval_T *rettv)
13446{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013447 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013448 char_u *tag_pattern;
13449
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013450 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013451
13452 rettv->vval.v_number = FALSE;
13453 if (*tag_pattern == NUL)
13454 return;
13455
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013456 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013457 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013458 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013459 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013460}
13461
13462/*
13463 * "tempname()" function
13464 */
13465 static void
13466f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13467{
13468 static int x = 'A';
13469
13470 rettv->v_type = VAR_STRING;
13471 rettv->vval.v_string = vim_tempname(x, FALSE);
13472
13473 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13474 * names. Skip 'I' and 'O', they are used for shell redirection. */
13475 do
13476 {
13477 if (x == 'Z')
13478 x = '0';
13479 else if (x == '9')
13480 x = 'A';
13481 else
13482 {
13483#ifdef EBCDIC
13484 if (x == 'I')
13485 x = 'J';
13486 else if (x == 'R')
13487 x = 'S';
13488 else
13489#endif
13490 ++x;
13491 }
13492 } while (x == 'I' || x == 'O');
13493}
13494
13495#ifdef FEAT_FLOAT
13496/*
13497 * "tan()" function
13498 */
13499 static void
13500f_tan(typval_T *argvars, typval_T *rettv)
13501{
13502 float_T f = 0.0;
13503
13504 rettv->v_type = VAR_FLOAT;
13505 if (get_float_arg(argvars, &f) == OK)
13506 rettv->vval.v_float = tan(f);
13507 else
13508 rettv->vval.v_float = 0.0;
13509}
13510
13511/*
13512 * "tanh()" function
13513 */
13514 static void
13515f_tanh(typval_T *argvars, typval_T *rettv)
13516{
13517 float_T f = 0.0;
13518
13519 rettv->v_type = VAR_FLOAT;
13520 if (get_float_arg(argvars, &f) == OK)
13521 rettv->vval.v_float = tanh(f);
13522 else
13523 rettv->vval.v_float = 0.0;
13524}
13525#endif
13526
13527/*
13528 * "test_alloc_fail(id, countdown, repeat)" function
13529 */
13530 static void
13531f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13532{
13533 if (argvars[0].v_type != VAR_NUMBER
13534 || argvars[0].vval.v_number <= 0
13535 || argvars[1].v_type != VAR_NUMBER
13536 || argvars[1].vval.v_number < 0
13537 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013538 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013539 else
13540 {
13541 alloc_fail_id = argvars[0].vval.v_number;
13542 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013543 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013544 alloc_fail_countdown = argvars[1].vval.v_number;
13545 alloc_fail_repeat = argvars[2].vval.v_number;
13546 did_outofmem_msg = FALSE;
13547 }
13548}
13549
13550/*
13551 * "test_autochdir()"
13552 */
13553 static void
13554f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13555{
13556#if defined(FEAT_AUTOCHDIR)
13557 test_autochdir = TRUE;
13558#endif
13559}
13560
13561/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013562 * "test_feedinput()"
13563 */
13564 static void
13565f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13566{
13567#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013568 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013569
13570 if (val != NULL)
13571 {
13572 trash_input_buf();
13573 add_to_input_buf_csi(val, (int)STRLEN(val));
13574 }
13575#endif
13576}
13577
13578/*
Bram Moolenaareda65222019-05-16 20:29:44 +020013579 * "test_getvalue({name})" function
13580 */
13581 static void
13582f_test_getvalue(typval_T *argvars, typval_T *rettv)
13583{
13584 if (argvars[0].v_type != VAR_STRING)
13585 emsg(_(e_invarg));
13586 else
13587 {
13588 char_u *name = tv_get_string(&argvars[0]);
13589
13590 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
13591 rettv->vval.v_number = need_fileinfo;
13592 else
13593 semsg(_(e_invarg2), name);
13594 }
13595}
13596
13597/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013598 * "test_option_not_set({name})" function
13599 */
13600 static void
13601f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13602{
13603 char_u *name = (char_u *)"";
13604
13605 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013606 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013607 else
13608 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013609 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013610 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013611 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013612 }
13613}
13614
13615/*
13616 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013617 */
13618 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013619f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013620{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013621 char_u *name = (char_u *)"";
13622 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013623 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013624
13625 if (argvars[0].v_type != VAR_STRING
13626 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013627 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013628 else
13629 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013630 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013631 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013632
13633 if (STRCMP(name, (char_u *)"redraw") == 0)
13634 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013635 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13636 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013637 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13638 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013639 else if (STRCMP(name, (char_u *)"starting") == 0)
13640 {
13641 if (val)
13642 {
13643 if (save_starting < 0)
13644 save_starting = starting;
13645 starting = 0;
13646 }
13647 else
13648 {
13649 starting = save_starting;
13650 save_starting = -1;
13651 }
13652 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013653 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13654 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013655 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
13656 no_query_mouse_for_testing = val;
Bram Moolenaaradc67142019-06-22 01:40:42 +020013657 else if (STRCMP(name, (char_u *)"no_wait_return") == 0)
13658 no_wait_return = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013659 else if (STRCMP(name, (char_u *)"ALL") == 0)
13660 {
13661 disable_char_avail_for_testing = FALSE;
13662 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013663 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013664 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013665 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013666 if (save_starting >= 0)
13667 {
13668 starting = save_starting;
13669 save_starting = -1;
13670 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013671 }
13672 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013673 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013674 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013675}
13676
13677/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010013678 * "test_refcount({expr})" function
13679 */
13680 static void
13681f_test_refcount(typval_T *argvars, typval_T *rettv)
13682{
13683 int retval = -1;
13684
13685 switch (argvars[0].v_type)
13686 {
13687 case VAR_UNKNOWN:
13688 case VAR_NUMBER:
13689 case VAR_FLOAT:
13690 case VAR_SPECIAL:
13691 case VAR_STRING:
13692 break;
13693 case VAR_JOB:
13694#ifdef FEAT_JOB_CHANNEL
13695 if (argvars[0].vval.v_job != NULL)
13696 retval = argvars[0].vval.v_job->jv_refcount - 1;
13697#endif
13698 break;
13699 case VAR_CHANNEL:
13700#ifdef FEAT_JOB_CHANNEL
13701 if (argvars[0].vval.v_channel != NULL)
13702 retval = argvars[0].vval.v_channel->ch_refcount - 1;
13703#endif
13704 break;
13705 case VAR_FUNC:
13706 if (argvars[0].vval.v_string != NULL)
13707 {
13708 ufunc_T *fp;
13709
13710 fp = find_func(argvars[0].vval.v_string);
13711 if (fp != NULL)
13712 retval = fp->uf_refcount;
13713 }
13714 break;
13715 case VAR_PARTIAL:
13716 if (argvars[0].vval.v_partial != NULL)
13717 retval = argvars[0].vval.v_partial->pt_refcount - 1;
13718 break;
13719 case VAR_BLOB:
13720 if (argvars[0].vval.v_blob != NULL)
13721 retval = argvars[0].vval.v_blob->bv_refcount - 1;
13722 break;
13723 case VAR_LIST:
13724 if (argvars[0].vval.v_list != NULL)
13725 retval = argvars[0].vval.v_list->lv_refcount - 1;
13726 break;
13727 case VAR_DICT:
13728 if (argvars[0].vval.v_dict != NULL)
13729 retval = argvars[0].vval.v_dict->dv_refcount - 1;
13730 break;
13731 }
13732
13733 rettv->v_type = VAR_NUMBER;
13734 rettv->vval.v_number = retval;
13735
13736}
13737
13738/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013739 * "test_garbagecollect_now()" function
13740 */
13741 static void
13742f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13743{
13744 /* This is dangerous, any Lists and Dicts used internally may be freed
13745 * while still in use. */
13746 garbage_collect(TRUE);
13747}
13748
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013749/*
Bram Moolenaaradc67142019-06-22 01:40:42 +020013750 * "test_garbagecollect_soon()" function
13751 */
13752 static void
13753f_test_garbagecollect_soon(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13754{
13755 may_garbage_collect = TRUE;
13756}
13757
13758/*
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013759 * "test_ignore_error()" function
13760 */
13761 static void
13762f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13763{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013764 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013765}
13766
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013767 static void
13768f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13769{
13770 rettv->v_type = VAR_BLOB;
13771 rettv->vval.v_blob = NULL;
13772}
13773
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013774#ifdef FEAT_JOB_CHANNEL
13775 static void
13776f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13777{
13778 rettv->v_type = VAR_CHANNEL;
13779 rettv->vval.v_channel = NULL;
13780}
13781#endif
13782
13783 static void
13784f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13785{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013786 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013787}
13788
13789#ifdef FEAT_JOB_CHANNEL
13790 static void
13791f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13792{
13793 rettv->v_type = VAR_JOB;
13794 rettv->vval.v_job = NULL;
13795}
13796#endif
13797
13798 static void
13799f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13800{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013801 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013802}
13803
13804 static void
13805f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13806{
13807 rettv->v_type = VAR_PARTIAL;
13808 rettv->vval.v_partial = NULL;
13809}
13810
13811 static void
13812f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13813{
13814 rettv->v_type = VAR_STRING;
13815 rettv->vval.v_string = NULL;
13816}
13817
Bram Moolenaarab186732018-09-14 21:27:06 +020013818#ifdef FEAT_GUI
13819 static void
13820f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13821{
13822 char_u *which;
13823 long value;
13824 int dragging;
13825 scrollbar_T *sb = NULL;
13826
13827 if (argvars[0].v_type != VAR_STRING
13828 || (argvars[1].v_type) != VAR_NUMBER
13829 || (argvars[2].v_type) != VAR_NUMBER)
13830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013831 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013832 return;
13833 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013834 which = tv_get_string(&argvars[0]);
13835 value = tv_get_number(&argvars[1]);
13836 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013837
13838 if (STRCMP(which, "left") == 0)
13839 sb = &curwin->w_scrollbars[SBAR_LEFT];
13840 else if (STRCMP(which, "right") == 0)
13841 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13842 else if (STRCMP(which, "hor") == 0)
13843 sb = &gui.bottom_sbar;
13844 if (sb == NULL)
13845 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013846 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020013847 return;
13848 }
13849 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013850# ifndef USE_ON_FLY_SCROLL
13851 // need to loop through normal_cmd() to handle the scroll events
13852 exec_normal(FALSE, TRUE, FALSE);
13853# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013854}
13855#endif
13856
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013857#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013858 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013859f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
13860{
13861 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
13862 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
13863}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013864#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013865
13866 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013867f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13868{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013869 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013870}
13871
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013872/*
13873 * Get a callback from "arg". It can be a Funcref or a function name.
13874 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013875 * "cb_name" is not allocated.
13876 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013877 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013878 callback_T
13879get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013880{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013881 callback_T res;
13882
13883 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013884 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13885 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013886 res.cb_partial = arg->vval.v_partial;
13887 ++res.cb_partial->pt_refcount;
13888 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013889 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013890 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013891 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013892 res.cb_partial = NULL;
13893 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
13894 {
13895 // Note that we don't make a copy of the string.
13896 res.cb_name = arg->vval.v_string;
13897 func_ref(res.cb_name);
13898 }
13899 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13900 {
13901 res.cb_name = (char_u *)"";
13902 }
13903 else
13904 {
13905 emsg(_("E921: Invalid callback argument"));
13906 res.cb_name = NULL;
13907 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013908 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013909 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013910}
13911
13912/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013913 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013914 */
13915 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013916put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013917{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013918 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013919 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013920 tv->v_type = VAR_PARTIAL;
13921 tv->vval.v_partial = cb->cb_partial;
13922 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013923 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013924 else
13925 {
13926 tv->v_type = VAR_FUNC;
13927 tv->vval.v_string = vim_strsave(cb->cb_name);
13928 func_ref(cb->cb_name);
13929 }
13930}
13931
13932/*
13933 * Make a copy of "src" into "dest", allocating the function name if needed,
13934 * without incrementing the refcount.
13935 */
13936 void
13937set_callback(callback_T *dest, callback_T *src)
13938{
13939 if (src->cb_partial == NULL)
13940 {
13941 // just a function name, make a copy
13942 dest->cb_name = vim_strsave(src->cb_name);
13943 dest->cb_free_name = TRUE;
13944 }
13945 else
13946 {
13947 // cb_name is a pointer into cb_partial
13948 dest->cb_name = src->cb_name;
13949 dest->cb_free_name = FALSE;
13950 }
13951 dest->cb_partial = src->cb_partial;
13952}
13953
13954/*
13955 * Unref/free "callback" returned by get_callback() or set_callback().
13956 */
13957 void
13958free_callback(callback_T *callback)
13959{
13960 if (callback->cb_partial != NULL)
13961 {
13962 partial_unref(callback->cb_partial);
13963 callback->cb_partial = NULL;
13964 }
13965 else if (callback->cb_name != NULL)
13966 func_unref(callback->cb_name);
13967 if (callback->cb_free_name)
13968 {
13969 vim_free(callback->cb_name);
13970 callback->cb_free_name = FALSE;
13971 }
13972 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013973}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013974
13975#ifdef FEAT_TIMERS
13976/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013977 * "timer_info([timer])" function
13978 */
13979 static void
13980f_timer_info(typval_T *argvars, typval_T *rettv)
13981{
13982 timer_T *timer = NULL;
13983
13984 if (rettv_list_alloc(rettv) != OK)
13985 return;
13986 if (argvars[0].v_type != VAR_UNKNOWN)
13987 {
13988 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013989 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013990 else
13991 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013992 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013993 if (timer != NULL)
13994 add_timer_info(rettv, timer);
13995 }
13996 }
13997 else
13998 add_timer_info_all(rettv);
13999}
14000
14001/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014002 * "timer_pause(timer, paused)" function
14003 */
14004 static void
14005f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14006{
14007 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014008 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014009
14010 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014011 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014012 else
14013 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014014 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014015 if (timer != NULL)
14016 timer->tr_paused = paused;
14017 }
14018}
14019
14020/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014021 * "timer_start(time, callback [, options])" function
14022 */
14023 static void
14024f_timer_start(typval_T *argvars, typval_T *rettv)
14025{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014026 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014027 timer_T *timer;
14028 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014029 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014030 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014031
Bram Moolenaar75537a92016-09-05 22:45:28 +020014032 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014033 if (check_secure())
14034 return;
14035 if (argvars[2].v_type != VAR_UNKNOWN)
14036 {
14037 if (argvars[2].v_type != VAR_DICT
14038 || (dict = argvars[2].vval.v_dict) == NULL)
14039 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014040 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014041 return;
14042 }
14043 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014044 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014045 }
14046
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014047 callback = get_callback(&argvars[1]);
14048 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014049 return;
14050
14051 timer = create_timer(msec, repeat);
14052 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014053 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014054 else
14055 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014056 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014057 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014058 }
14059}
14060
14061/*
14062 * "timer_stop(timer)" function
14063 */
14064 static void
14065f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14066{
14067 timer_T *timer;
14068
14069 if (argvars[0].v_type != VAR_NUMBER)
14070 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014071 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014072 return;
14073 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014074 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014075 if (timer != NULL)
14076 stop_timer(timer);
14077}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014078
14079/*
14080 * "timer_stopall()" function
14081 */
14082 static void
14083f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14084{
14085 stop_all_timers();
14086}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014087#endif
14088
14089/*
14090 * "tolower(string)" function
14091 */
14092 static void
14093f_tolower(typval_T *argvars, typval_T *rettv)
14094{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014095 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014096 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014097}
14098
14099/*
14100 * "toupper(string)" function
14101 */
14102 static void
14103f_toupper(typval_T *argvars, typval_T *rettv)
14104{
14105 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014106 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014107}
14108
14109/*
14110 * "tr(string, fromstr, tostr)" function
14111 */
14112 static void
14113f_tr(typval_T *argvars, typval_T *rettv)
14114{
14115 char_u *in_str;
14116 char_u *fromstr;
14117 char_u *tostr;
14118 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014119 int inlen;
14120 int fromlen;
14121 int tolen;
14122 int idx;
14123 char_u *cpstr;
14124 int cplen;
14125 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014126 char_u buf[NUMBUFLEN];
14127 char_u buf2[NUMBUFLEN];
14128 garray_T ga;
14129
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014130 in_str = tv_get_string(&argvars[0]);
14131 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14132 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014133
14134 /* Default return value: empty string. */
14135 rettv->v_type = VAR_STRING;
14136 rettv->vval.v_string = NULL;
14137 if (fromstr == NULL || tostr == NULL)
14138 return; /* type error; errmsg already given */
14139 ga_init2(&ga, (int)sizeof(char), 80);
14140
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014141 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014142 /* not multi-byte: fromstr and tostr must be the same length */
14143 if (STRLEN(fromstr) != STRLEN(tostr))
14144 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014145error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014146 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014147 ga_clear(&ga);
14148 return;
14149 }
14150
14151 /* fromstr and tostr have to contain the same number of chars */
14152 while (*in_str != NUL)
14153 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014154 if (has_mbyte)
14155 {
14156 inlen = (*mb_ptr2len)(in_str);
14157 cpstr = in_str;
14158 cplen = inlen;
14159 idx = 0;
14160 for (p = fromstr; *p != NUL; p += fromlen)
14161 {
14162 fromlen = (*mb_ptr2len)(p);
14163 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14164 {
14165 for (p = tostr; *p != NUL; p += tolen)
14166 {
14167 tolen = (*mb_ptr2len)(p);
14168 if (idx-- == 0)
14169 {
14170 cplen = tolen;
14171 cpstr = p;
14172 break;
14173 }
14174 }
14175 if (*p == NUL) /* tostr is shorter than fromstr */
14176 goto error;
14177 break;
14178 }
14179 ++idx;
14180 }
14181
14182 if (first && cpstr == in_str)
14183 {
14184 /* Check that fromstr and tostr have the same number of
14185 * (multi-byte) characters. Done only once when a character
14186 * of in_str doesn't appear in fromstr. */
14187 first = FALSE;
14188 for (p = tostr; *p != NUL; p += tolen)
14189 {
14190 tolen = (*mb_ptr2len)(p);
14191 --idx;
14192 }
14193 if (idx != 0)
14194 goto error;
14195 }
14196
14197 (void)ga_grow(&ga, cplen);
14198 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14199 ga.ga_len += cplen;
14200
14201 in_str += inlen;
14202 }
14203 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014204 {
14205 /* When not using multi-byte chars we can do it faster. */
14206 p = vim_strchr(fromstr, *in_str);
14207 if (p != NULL)
14208 ga_append(&ga, tostr[p - fromstr]);
14209 else
14210 ga_append(&ga, *in_str);
14211 ++in_str;
14212 }
14213 }
14214
14215 /* add a terminating NUL */
14216 (void)ga_grow(&ga, 1);
14217 ga_append(&ga, NUL);
14218
14219 rettv->vval.v_string = ga.ga_data;
14220}
14221
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014222/*
14223 * "trim({expr})" function
14224 */
14225 static void
14226f_trim(typval_T *argvars, typval_T *rettv)
14227{
14228 char_u buf1[NUMBUFLEN];
14229 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014230 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014231 char_u *mask = NULL;
14232 char_u *tail;
14233 char_u *prev;
14234 char_u *p;
14235 int c1;
14236
14237 rettv->v_type = VAR_STRING;
14238 if (head == NULL)
14239 {
14240 rettv->vval.v_string = NULL;
14241 return;
14242 }
14243
14244 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014245 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014246
14247 while (*head != NUL)
14248 {
14249 c1 = PTR2CHAR(head);
14250 if (mask == NULL)
14251 {
14252 if (c1 > ' ' && c1 != 0xa0)
14253 break;
14254 }
14255 else
14256 {
14257 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14258 if (c1 == PTR2CHAR(p))
14259 break;
14260 if (*p == NUL)
14261 break;
14262 }
14263 MB_PTR_ADV(head);
14264 }
14265
14266 for (tail = head + STRLEN(head); tail > head; tail = prev)
14267 {
14268 prev = tail;
14269 MB_PTR_BACK(head, prev);
14270 c1 = PTR2CHAR(prev);
14271 if (mask == NULL)
14272 {
14273 if (c1 > ' ' && c1 != 0xa0)
14274 break;
14275 }
14276 else
14277 {
14278 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14279 if (c1 == PTR2CHAR(p))
14280 break;
14281 if (*p == NUL)
14282 break;
14283 }
14284 }
14285 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14286}
14287
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014288#ifdef FEAT_FLOAT
14289/*
14290 * "trunc({float})" function
14291 */
14292 static void
14293f_trunc(typval_T *argvars, typval_T *rettv)
14294{
14295 float_T f = 0.0;
14296
14297 rettv->v_type = VAR_FLOAT;
14298 if (get_float_arg(argvars, &f) == OK)
14299 /* trunc() is not in C90, use floor() or ceil() instead. */
14300 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14301 else
14302 rettv->vval.v_float = 0.0;
14303}
14304#endif
14305
14306/*
14307 * "type(expr)" function
14308 */
14309 static void
14310f_type(typval_T *argvars, typval_T *rettv)
14311{
14312 int n = -1;
14313
14314 switch (argvars[0].v_type)
14315 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014316 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14317 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014318 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014319 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14320 case VAR_LIST: n = VAR_TYPE_LIST; break;
14321 case VAR_DICT: n = VAR_TYPE_DICT; break;
14322 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014323 case VAR_SPECIAL:
14324 if (argvars[0].vval.v_number == VVAL_FALSE
14325 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014326 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014327 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014328 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014329 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014330 case VAR_JOB: n = VAR_TYPE_JOB; break;
14331 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014332 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014333 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014334 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014335 n = -1;
14336 break;
14337 }
14338 rettv->vval.v_number = n;
14339}
14340
14341/*
14342 * "undofile(name)" function
14343 */
14344 static void
14345f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14346{
14347 rettv->v_type = VAR_STRING;
14348#ifdef FEAT_PERSISTENT_UNDO
14349 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014350 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014351
14352 if (*fname == NUL)
14353 {
14354 /* If there is no file name there will be no undo file. */
14355 rettv->vval.v_string = NULL;
14356 }
14357 else
14358 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020014359 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014360
14361 if (ffname != NULL)
14362 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14363 vim_free(ffname);
14364 }
14365 }
14366#else
14367 rettv->vval.v_string = NULL;
14368#endif
14369}
14370
14371/*
14372 * "undotree()" function
14373 */
14374 static void
14375f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14376{
14377 if (rettv_dict_alloc(rettv) == OK)
14378 {
14379 dict_T *dict = rettv->vval.v_dict;
14380 list_T *list;
14381
Bram Moolenaare0be1672018-07-08 16:50:37 +020014382 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14383 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14384 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14385 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14386 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14387 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014388
14389 list = list_alloc();
14390 if (list != NULL)
14391 {
14392 u_eval_tree(curbuf->b_u_oldhead, list);
14393 dict_add_list(dict, "entries", list);
14394 }
14395 }
14396}
14397
14398/*
14399 * "values(dict)" function
14400 */
14401 static void
14402f_values(typval_T *argvars, typval_T *rettv)
14403{
14404 dict_list(argvars, rettv, 1);
14405}
14406
14407/*
14408 * "virtcol(string)" function
14409 */
14410 static void
14411f_virtcol(typval_T *argvars, typval_T *rettv)
14412{
14413 colnr_T vcol = 0;
14414 pos_T *fp;
14415 int fnum = curbuf->b_fnum;
14416
14417 fp = var2fpos(&argvars[0], FALSE, &fnum);
14418 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14419 && fnum == curbuf->b_fnum)
14420 {
14421 getvvcol(curwin, fp, NULL, NULL, &vcol);
14422 ++vcol;
14423 }
14424
14425 rettv->vval.v_number = vcol;
14426}
14427
14428/*
14429 * "visualmode()" function
14430 */
14431 static void
14432f_visualmode(typval_T *argvars, typval_T *rettv)
14433{
14434 char_u str[2];
14435
14436 rettv->v_type = VAR_STRING;
14437 str[0] = curbuf->b_visual_mode_eval;
14438 str[1] = NUL;
14439 rettv->vval.v_string = vim_strsave(str);
14440
14441 /* A non-zero number or non-empty string argument: reset mode. */
14442 if (non_zero_arg(&argvars[0]))
14443 curbuf->b_visual_mode_eval = NUL;
14444}
14445
14446/*
14447 * "wildmenumode()" function
14448 */
14449 static void
14450f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14451{
14452#ifdef FEAT_WILDMENU
14453 if (wild_menu_showing)
14454 rettv->vval.v_number = 1;
14455#endif
14456}
14457
14458/*
14459 * "winbufnr(nr)" function
14460 */
14461 static void
14462f_winbufnr(typval_T *argvars, typval_T *rettv)
14463{
14464 win_T *wp;
14465
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014466 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014467 if (wp == NULL)
14468 rettv->vval.v_number = -1;
14469 else
14470 rettv->vval.v_number = wp->w_buffer->b_fnum;
14471}
14472
14473/*
14474 * "wincol()" function
14475 */
14476 static void
14477f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14478{
14479 validate_cursor();
14480 rettv->vval.v_number = curwin->w_wcol + 1;
14481}
14482
14483/*
14484 * "winheight(nr)" function
14485 */
14486 static void
14487f_winheight(typval_T *argvars, typval_T *rettv)
14488{
14489 win_T *wp;
14490
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014491 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014492 if (wp == NULL)
14493 rettv->vval.v_number = -1;
14494 else
14495 rettv->vval.v_number = wp->w_height;
14496}
14497
14498/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014499 * "winlayout()" function
14500 */
14501 static void
14502f_winlayout(typval_T *argvars, typval_T *rettv)
14503{
14504 tabpage_T *tp;
14505
14506 if (rettv_list_alloc(rettv) != OK)
14507 return;
14508
14509 if (argvars[0].v_type == VAR_UNKNOWN)
14510 tp = curtab;
14511 else
14512 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014513 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014514 if (tp == NULL)
14515 return;
14516 }
14517
14518 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14519}
14520
14521/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014522 * "winline()" function
14523 */
14524 static void
14525f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14526{
14527 validate_cursor();
14528 rettv->vval.v_number = curwin->w_wrow + 1;
14529}
14530
14531/*
14532 * "winnr()" function
14533 */
14534 static void
14535f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14536{
14537 int nr = 1;
14538
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014539 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014540 rettv->vval.v_number = nr;
14541}
14542
14543/*
14544 * "winrestcmd()" function
14545 */
14546 static void
14547f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14548{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014549 win_T *wp;
14550 int winnr = 1;
14551 garray_T ga;
14552 char_u buf[50];
14553
14554 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014555 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014556 {
14557 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14558 ga_concat(&ga, buf);
14559 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14560 ga_concat(&ga, buf);
14561 ++winnr;
14562 }
14563 ga_append(&ga, NUL);
14564
14565 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014566 rettv->v_type = VAR_STRING;
14567}
14568
14569/*
14570 * "winrestview()" function
14571 */
14572 static void
14573f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14574{
14575 dict_T *dict;
14576
14577 if (argvars[0].v_type != VAR_DICT
14578 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014579 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014580 else
14581 {
14582 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014583 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014584 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014585 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014586 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014587 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014588 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14589 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014590 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014591 curwin->w_set_curswant = FALSE;
14592 }
14593
14594 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014595 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014596#ifdef FEAT_DIFF
14597 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014598 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014599#endif
14600 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014601 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014602 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014603 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014604
14605 check_cursor();
14606 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014607 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014608 changed_window_setting();
14609
14610 if (curwin->w_topline <= 0)
14611 curwin->w_topline = 1;
14612 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14613 curwin->w_topline = curbuf->b_ml.ml_line_count;
14614#ifdef FEAT_DIFF
14615 check_topfill(curwin, TRUE);
14616#endif
14617 }
14618}
14619
14620/*
14621 * "winsaveview()" function
14622 */
14623 static void
14624f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14625{
14626 dict_T *dict;
14627
14628 if (rettv_dict_alloc(rettv) == FAIL)
14629 return;
14630 dict = rettv->vval.v_dict;
14631
Bram Moolenaare0be1672018-07-08 16:50:37 +020014632 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14633 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020014634 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014635 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014636 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014637
Bram Moolenaare0be1672018-07-08 16:50:37 +020014638 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014639#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014640 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014641#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014642 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14643 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014644}
14645
14646/*
14647 * "winwidth(nr)" function
14648 */
14649 static void
14650f_winwidth(typval_T *argvars, typval_T *rettv)
14651{
14652 win_T *wp;
14653
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014654 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014655 if (wp == NULL)
14656 rettv->vval.v_number = -1;
14657 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014658 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014659}
14660
14661/*
14662 * "wordcount()" function
14663 */
14664 static void
14665f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14666{
14667 if (rettv_dict_alloc(rettv) == FAIL)
14668 return;
14669 cursor_pos_info(rettv->vval.v_dict);
14670}
14671
14672/*
14673 * "writefile()" function
14674 */
14675 static void
14676f_writefile(typval_T *argvars, typval_T *rettv)
14677{
14678 int binary = FALSE;
14679 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014680#ifdef HAVE_FSYNC
14681 int do_fsync = p_fs;
14682#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014683 char_u *fname;
14684 FILE *fd;
14685 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014686 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014687 list_T *list = NULL;
14688 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014689
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014690 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010014691 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014692 return;
14693
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014694 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014695 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014696 list = argvars[0].vval.v_list;
14697 if (list == NULL)
14698 return;
14699 for (li = list->lv_first; li != NULL; li = li->li_next)
14700 if (tv_get_string_chk(&li->li_tv) == NULL)
14701 return;
14702 }
14703 else if (argvars[0].v_type == VAR_BLOB)
14704 {
14705 blob = argvars[0].vval.v_blob;
14706 if (blob == NULL)
14707 return;
14708 }
14709 else
14710 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014711 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014712 return;
14713 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014714
14715 if (argvars[2].v_type != VAR_UNKNOWN)
14716 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014717 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014718
14719 if (arg2 == NULL)
14720 return;
14721 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014722 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014723 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014724 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014725#ifdef HAVE_FSYNC
14726 if (vim_strchr(arg2, 's') != NULL)
14727 do_fsync = TRUE;
14728 else if (vim_strchr(arg2, 'S') != NULL)
14729 do_fsync = FALSE;
14730#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014731 }
14732
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014733 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014734 if (fname == NULL)
14735 return;
14736
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014737 /* Always open the file in binary mode, library functions have a mind of
14738 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014739 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14740 append ? APPENDBIN : WRITEBIN)) == NULL)
14741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014742 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014743 ret = -1;
14744 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014745 else if (blob)
14746 {
14747 if (write_blob(fd, blob) == FAIL)
14748 ret = -1;
14749#ifdef HAVE_FSYNC
14750 else if (do_fsync)
14751 // Ignore the error, the user wouldn't know what to do about it.
14752 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010014753 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014754#endif
14755 fclose(fd);
14756 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014757 else
14758 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014759 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014760 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014761#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014762 else if (do_fsync)
14763 /* Ignore the error, the user wouldn't know what to do about it.
14764 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010014765 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014766#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014767 fclose(fd);
14768 }
14769
14770 rettv->vval.v_number = ret;
14771}
14772
14773/*
14774 * "xor(expr, expr)" function
14775 */
14776 static void
14777f_xor(typval_T *argvars, typval_T *rettv)
14778{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014779 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14780 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014781}
14782
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014783#endif /* FEAT_EVAL */