blob: f87fd1f99a079aa03436967ec3b06341b95ca0ac [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010032static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033static char *e_stringreq = N_("E928: String required");
Bram Moolenaaraff74912019-03-30 18:11:49 +010034static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020035
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020066static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010067static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010069static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010070# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010071#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_browse(typval_T *argvars, typval_T *rettv);
73static void f_browsedir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020074static void f_bufadd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020075static void f_bufexists(typval_T *argvars, typval_T *rettv);
76static void f_buflisted(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020077static void f_bufload(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020078static void f_bufloaded(typval_T *argvars, typval_T *rettv);
79static void f_bufname(typval_T *argvars, typval_T *rettv);
80static void f_bufnr(typval_T *argvars, typval_T *rettv);
81static void f_bufwinid(typval_T *argvars, typval_T *rettv);
82static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
83static void f_byte2line(typval_T *argvars, typval_T *rettv);
84static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
85static void f_byteidx(typval_T *argvars, typval_T *rettv);
86static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
87static void f_call(typval_T *argvars, typval_T *rettv);
88#ifdef FEAT_FLOAT
89static void f_ceil(typval_T *argvars, typval_T *rettv);
90#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_changenr(typval_T *argvars, typval_T *rettv);
92static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020093static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020094static void f_cindent(typval_T *argvars, typval_T *rettv);
95static void f_clearmatches(typval_T *argvars, typval_T *rettv);
96static void f_col(typval_T *argvars, typval_T *rettv);
97#if defined(FEAT_INS_EXPAND)
98static void f_complete(typval_T *argvars, typval_T *rettv);
99static void f_complete_add(typval_T *argvars, typval_T *rettv);
100static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +0100101static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102#endif
103static void f_confirm(typval_T *argvars, typval_T *rettv);
104static void f_copy(typval_T *argvars, typval_T *rettv);
105#ifdef FEAT_FLOAT
106static void f_cos(typval_T *argvars, typval_T *rettv);
107static void f_cosh(typval_T *argvars, typval_T *rettv);
108#endif
109static void f_count(typval_T *argvars, typval_T *rettv);
110static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
111static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100112#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200113static void f_debugbreak(typval_T *argvars, typval_T *rettv);
114#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200115static void f_deepcopy(typval_T *argvars, typval_T *rettv);
116static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200117static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200118static void f_did_filetype(typval_T *argvars, typval_T *rettv);
119static void f_diff_filler(typval_T *argvars, typval_T *rettv);
120static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
121static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200122static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200123static void f_escape(typval_T *argvars, typval_T *rettv);
124static void f_eval(typval_T *argvars, typval_T *rettv);
125static void f_eventhandler(typval_T *argvars, typval_T *rettv);
126static void f_executable(typval_T *argvars, typval_T *rettv);
127static void f_execute(typval_T *argvars, typval_T *rettv);
128static void f_exepath(typval_T *argvars, typval_T *rettv);
129static void f_exists(typval_T *argvars, typval_T *rettv);
130#ifdef FEAT_FLOAT
131static void f_exp(typval_T *argvars, typval_T *rettv);
132#endif
133static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200134static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200135static void f_extend(typval_T *argvars, typval_T *rettv);
136static void f_feedkeys(typval_T *argvars, typval_T *rettv);
137static void f_filereadable(typval_T *argvars, typval_T *rettv);
138static void f_filewritable(typval_T *argvars, typval_T *rettv);
139static void f_filter(typval_T *argvars, typval_T *rettv);
140static void f_finddir(typval_T *argvars, typval_T *rettv);
141static void f_findfile(typval_T *argvars, typval_T *rettv);
142#ifdef FEAT_FLOAT
143static void f_float2nr(typval_T *argvars, typval_T *rettv);
144static void f_floor(typval_T *argvars, typval_T *rettv);
145static void f_fmod(typval_T *argvars, typval_T *rettv);
146#endif
147static void f_fnameescape(typval_T *argvars, typval_T *rettv);
148static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
149static void f_foldclosed(typval_T *argvars, typval_T *rettv);
150static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
151static void f_foldlevel(typval_T *argvars, typval_T *rettv);
152static void f_foldtext(typval_T *argvars, typval_T *rettv);
153static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
154static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200155static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200156static void f_function(typval_T *argvars, typval_T *rettv);
157static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
158static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200159static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200160static void f_getbufline(typval_T *argvars, typval_T *rettv);
161static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100162static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200163static void f_getchar(typval_T *argvars, typval_T *rettv);
164static void f_getcharmod(typval_T *argvars, typval_T *rettv);
165static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
166static void f_getcmdline(typval_T *argvars, typval_T *rettv);
167#if defined(FEAT_CMDL_COMPL)
168static void f_getcompletion(typval_T *argvars, typval_T *rettv);
169#endif
170static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
171static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
172static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
173static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200174static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200175static void f_getfontname(typval_T *argvars, typval_T *rettv);
176static void f_getfperm(typval_T *argvars, typval_T *rettv);
177static void f_getfsize(typval_T *argvars, typval_T *rettv);
178static void f_getftime(typval_T *argvars, typval_T *rettv);
179static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100180static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200181static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200182static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200183static void f_getmatches(typval_T *argvars, typval_T *rettv);
184static void f_getpid(typval_T *argvars, typval_T *rettv);
185static void f_getcurpos(typval_T *argvars, typval_T *rettv);
186static void f_getpos(typval_T *argvars, typval_T *rettv);
187static void f_getqflist(typval_T *argvars, typval_T *rettv);
188static void f_getreg(typval_T *argvars, typval_T *rettv);
189static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200190static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_gettabvar(typval_T *argvars, typval_T *rettv);
192static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100193static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200194static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100195static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200196static void f_getwinposx(typval_T *argvars, typval_T *rettv);
197static void f_getwinposy(typval_T *argvars, typval_T *rettv);
198static void f_getwinvar(typval_T *argvars, typval_T *rettv);
199static void f_glob(typval_T *argvars, typval_T *rettv);
200static void f_globpath(typval_T *argvars, typval_T *rettv);
201static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
202static void f_has(typval_T *argvars, typval_T *rettv);
203static void f_has_key(typval_T *argvars, typval_T *rettv);
204static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
205static void f_hasmapto(typval_T *argvars, typval_T *rettv);
206static void f_histadd(typval_T *argvars, typval_T *rettv);
207static void f_histdel(typval_T *argvars, typval_T *rettv);
208static void f_histget(typval_T *argvars, typval_T *rettv);
209static void f_histnr(typval_T *argvars, typval_T *rettv);
210static void f_hlID(typval_T *argvars, typval_T *rettv);
211static void f_hlexists(typval_T *argvars, typval_T *rettv);
212static void f_hostname(typval_T *argvars, typval_T *rettv);
213static void f_iconv(typval_T *argvars, typval_T *rettv);
214static void f_indent(typval_T *argvars, typval_T *rettv);
215static void f_index(typval_T *argvars, typval_T *rettv);
216static void f_input(typval_T *argvars, typval_T *rettv);
217static void f_inputdialog(typval_T *argvars, typval_T *rettv);
218static void f_inputlist(typval_T *argvars, typval_T *rettv);
219static void f_inputrestore(typval_T *argvars, typval_T *rettv);
220static void f_inputsave(typval_T *argvars, typval_T *rettv);
221static void f_inputsecret(typval_T *argvars, typval_T *rettv);
222static void f_insert(typval_T *argvars, typval_T *rettv);
223static void f_invert(typval_T *argvars, typval_T *rettv);
224static void f_isdirectory(typval_T *argvars, typval_T *rettv);
225static void f_islocked(typval_T *argvars, typval_T *rettv);
226#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200227static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200228static void f_isnan(typval_T *argvars, typval_T *rettv);
229#endif
230static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200231static void f_join(typval_T *argvars, typval_T *rettv);
232static void f_js_decode(typval_T *argvars, typval_T *rettv);
233static void f_js_encode(typval_T *argvars, typval_T *rettv);
234static void f_json_decode(typval_T *argvars, typval_T *rettv);
235static void f_json_encode(typval_T *argvars, typval_T *rettv);
236static void f_keys(typval_T *argvars, typval_T *rettv);
237static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
238static void f_len(typval_T *argvars, typval_T *rettv);
239static void f_libcall(typval_T *argvars, typval_T *rettv);
240static void f_libcallnr(typval_T *argvars, typval_T *rettv);
241static void f_line(typval_T *argvars, typval_T *rettv);
242static void f_line2byte(typval_T *argvars, typval_T *rettv);
243static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200244static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_localtime(typval_T *argvars, typval_T *rettv);
246#ifdef FEAT_FLOAT
247static void f_log(typval_T *argvars, typval_T *rettv);
248static void f_log10(typval_T *argvars, typval_T *rettv);
249#endif
250#ifdef FEAT_LUA
251static void f_luaeval(typval_T *argvars, typval_T *rettv);
252#endif
253static void f_map(typval_T *argvars, typval_T *rettv);
254static void f_maparg(typval_T *argvars, typval_T *rettv);
255static void f_mapcheck(typval_T *argvars, typval_T *rettv);
256static void f_match(typval_T *argvars, typval_T *rettv);
257static void f_matchadd(typval_T *argvars, typval_T *rettv);
258static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
259static void f_matcharg(typval_T *argvars, typval_T *rettv);
260static void f_matchdelete(typval_T *argvars, typval_T *rettv);
261static void f_matchend(typval_T *argvars, typval_T *rettv);
262static void f_matchlist(typval_T *argvars, typval_T *rettv);
263static void f_matchstr(typval_T *argvars, typval_T *rettv);
264static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
265static void f_max(typval_T *argvars, typval_T *rettv);
266static void f_min(typval_T *argvars, typval_T *rettv);
267#ifdef vim_mkdir
268static void f_mkdir(typval_T *argvars, typval_T *rettv);
269#endif
270static void f_mode(typval_T *argvars, typval_T *rettv);
271#ifdef FEAT_MZSCHEME
272static void f_mzeval(typval_T *argvars, typval_T *rettv);
273#endif
274static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
275static void f_nr2char(typval_T *argvars, typval_T *rettv);
276static void f_or(typval_T *argvars, typval_T *rettv);
277static void f_pathshorten(typval_T *argvars, typval_T *rettv);
278#ifdef FEAT_PERL
279static void f_perleval(typval_T *argvars, typval_T *rettv);
280#endif
281#ifdef FEAT_FLOAT
282static void f_pow(typval_T *argvars, typval_T *rettv);
283#endif
284static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
285static void f_printf(typval_T *argvars, typval_T *rettv);
286static void f_pumvisible(typval_T *argvars, typval_T *rettv);
287#ifdef FEAT_PYTHON3
288static void f_py3eval(typval_T *argvars, typval_T *rettv);
289#endif
290#ifdef FEAT_PYTHON
291static void f_pyeval(typval_T *argvars, typval_T *rettv);
292#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100293#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
294static void f_pyxeval(typval_T *argvars, typval_T *rettv);
295#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200296static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200297static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200298static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200299static void f_reg_executing(typval_T *argvars, typval_T *rettv);
300static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200301static void f_reltime(typval_T *argvars, typval_T *rettv);
302#ifdef FEAT_FLOAT
303static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
304#endif
305static void f_reltimestr(typval_T *argvars, typval_T *rettv);
306static void f_remote_expr(typval_T *argvars, typval_T *rettv);
307static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
308static void f_remote_peek(typval_T *argvars, typval_T *rettv);
309static void f_remote_read(typval_T *argvars, typval_T *rettv);
310static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100311static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200312static void f_remove(typval_T *argvars, typval_T *rettv);
313static void f_rename(typval_T *argvars, typval_T *rettv);
314static void f_repeat(typval_T *argvars, typval_T *rettv);
315static void f_resolve(typval_T *argvars, typval_T *rettv);
316static void f_reverse(typval_T *argvars, typval_T *rettv);
317#ifdef FEAT_FLOAT
318static void f_round(typval_T *argvars, typval_T *rettv);
319#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100320#ifdef FEAT_RUBY
321static void f_rubyeval(typval_T *argvars, typval_T *rettv);
322#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200323static void f_screenattr(typval_T *argvars, typval_T *rettv);
324static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100325static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200326static void f_screencol(typval_T *argvars, typval_T *rettv);
327static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100328static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200329static void f_search(typval_T *argvars, typval_T *rettv);
330static void f_searchdecl(typval_T *argvars, typval_T *rettv);
331static void f_searchpair(typval_T *argvars, typval_T *rettv);
332static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
333static void f_searchpos(typval_T *argvars, typval_T *rettv);
334static void f_server2client(typval_T *argvars, typval_T *rettv);
335static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200336static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200337static void f_setbufvar(typval_T *argvars, typval_T *rettv);
338static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
339static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200340static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200341static void f_setfperm(typval_T *argvars, typval_T *rettv);
342static void f_setline(typval_T *argvars, typval_T *rettv);
343static void f_setloclist(typval_T *argvars, typval_T *rettv);
344static void f_setmatches(typval_T *argvars, typval_T *rettv);
345static void f_setpos(typval_T *argvars, typval_T *rettv);
346static void f_setqflist(typval_T *argvars, typval_T *rettv);
347static void f_setreg(typval_T *argvars, typval_T *rettv);
348static void f_settabvar(typval_T *argvars, typval_T *rettv);
349static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100350static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200351static void f_setwinvar(typval_T *argvars, typval_T *rettv);
352#ifdef FEAT_CRYPT
353static void f_sha256(typval_T *argvars, typval_T *rettv);
354#endif /* FEAT_CRYPT */
355static void f_shellescape(typval_T *argvars, typval_T *rettv);
356static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
357static void f_simplify(typval_T *argvars, typval_T *rettv);
358#ifdef FEAT_FLOAT
359static void f_sin(typval_T *argvars, typval_T *rettv);
360static void f_sinh(typval_T *argvars, typval_T *rettv);
361#endif
362static void f_sort(typval_T *argvars, typval_T *rettv);
363static void f_soundfold(typval_T *argvars, typval_T *rettv);
364static void f_spellbadword(typval_T *argvars, typval_T *rettv);
365static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
366static void f_split(typval_T *argvars, typval_T *rettv);
367#ifdef FEAT_FLOAT
368static void f_sqrt(typval_T *argvars, typval_T *rettv);
369static void f_str2float(typval_T *argvars, typval_T *rettv);
370#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200371static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200372static void f_str2nr(typval_T *argvars, typval_T *rettv);
373static void f_strchars(typval_T *argvars, typval_T *rettv);
374#ifdef HAVE_STRFTIME
375static void f_strftime(typval_T *argvars, typval_T *rettv);
376#endif
377static void f_strgetchar(typval_T *argvars, typval_T *rettv);
378static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200379static void f_strlen(typval_T *argvars, typval_T *rettv);
380static void f_strcharpart(typval_T *argvars, typval_T *rettv);
381static void f_strpart(typval_T *argvars, typval_T *rettv);
382static void f_strridx(typval_T *argvars, typval_T *rettv);
383static void f_strtrans(typval_T *argvars, typval_T *rettv);
384static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
385static void f_strwidth(typval_T *argvars, typval_T *rettv);
386static void f_submatch(typval_T *argvars, typval_T *rettv);
387static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200388static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200389static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200390static void f_synID(typval_T *argvars, typval_T *rettv);
391static void f_synIDattr(typval_T *argvars, typval_T *rettv);
392static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
393static void f_synstack(typval_T *argvars, typval_T *rettv);
394static void f_synconcealed(typval_T *argvars, typval_T *rettv);
395static void f_system(typval_T *argvars, typval_T *rettv);
396static void f_systemlist(typval_T *argvars, typval_T *rettv);
397static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
398static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
399static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
400static void f_taglist(typval_T *argvars, typval_T *rettv);
401static void f_tagfiles(typval_T *argvars, typval_T *rettv);
402static void f_tempname(typval_T *argvars, typval_T *rettv);
403static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
404static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200405static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareda65222019-05-16 20:29:44 +0200406static void f_test_getvalue(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200407static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100408static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100409static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200410static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaaradc67142019-06-22 01:40:42 +0200411static void f_test_garbagecollect_soon(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100412static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100413static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200414#ifdef FEAT_JOB_CHANNEL
415static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
416#endif
417static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
418#ifdef FEAT_JOB_CHANNEL
419static void f_test_null_job(typval_T *argvars, typval_T *rettv);
420#endif
421static void f_test_null_list(typval_T *argvars, typval_T *rettv);
422static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
423static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200424#ifdef FEAT_GUI
425static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
426#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200427#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200428static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200429#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200430static void f_test_settime(typval_T *argvars, typval_T *rettv);
431#ifdef FEAT_FLOAT
432static void f_tan(typval_T *argvars, typval_T *rettv);
433static void f_tanh(typval_T *argvars, typval_T *rettv);
434#endif
435#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200436static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200437static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200438static void f_timer_start(typval_T *argvars, typval_T *rettv);
439static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200440static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200441#endif
442static void f_tolower(typval_T *argvars, typval_T *rettv);
443static void f_toupper(typval_T *argvars, typval_T *rettv);
444static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100445static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446#ifdef FEAT_FLOAT
447static void f_trunc(typval_T *argvars, typval_T *rettv);
448#endif
449static void f_type(typval_T *argvars, typval_T *rettv);
450static void f_undofile(typval_T *argvars, typval_T *rettv);
451static void f_undotree(typval_T *argvars, typval_T *rettv);
452static void f_uniq(typval_T *argvars, typval_T *rettv);
453static void f_values(typval_T *argvars, typval_T *rettv);
454static void f_virtcol(typval_T *argvars, typval_T *rettv);
455static void f_visualmode(typval_T *argvars, typval_T *rettv);
456static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200457static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200458static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
459static void f_win_getid(typval_T *argvars, typval_T *rettv);
460static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
461static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
462static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100463static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200464static void f_winbufnr(typval_T *argvars, typval_T *rettv);
465static void f_wincol(typval_T *argvars, typval_T *rettv);
466static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200467static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200468static void f_winline(typval_T *argvars, typval_T *rettv);
469static void f_winnr(typval_T *argvars, typval_T *rettv);
470static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
471static void f_winrestview(typval_T *argvars, typval_T *rettv);
472static void f_winsaveview(typval_T *argvars, typval_T *rettv);
473static void f_winwidth(typval_T *argvars, typval_T *rettv);
474static void f_writefile(typval_T *argvars, typval_T *rettv);
475static void f_wordcount(typval_T *argvars, typval_T *rettv);
476static void f_xor(typval_T *argvars, typval_T *rettv);
477
478/*
479 * Array with names and number of arguments of all internal functions
480 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
481 */
482static struct fst
483{
484 char *f_name; /* function name */
485 char f_min_argc; /* minimal number of arguments */
486 char f_max_argc; /* maximal number of arguments */
487 void (*f_func)(typval_T *args, typval_T *rvar);
488 /* implementation of function */
489} functions[] =
490{
491#ifdef FEAT_FLOAT
492 {"abs", 1, 1, f_abs},
493 {"acos", 1, 1, f_acos}, /* WJMc */
494#endif
495 {"add", 2, 2, f_add},
496 {"and", 2, 2, f_and},
497 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200498 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200499 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200500 {"argidx", 0, 0, f_argidx},
501 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200502 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200503#ifdef FEAT_FLOAT
504 {"asin", 1, 1, f_asin}, /* WJMc */
505#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100506 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100508 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200509 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200510 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200511 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100512 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200513 {"assert_match", 2, 3, f_assert_match},
514 {"assert_notequal", 2, 3, f_assert_notequal},
515 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100516 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200517 {"assert_true", 1, 2, f_assert_true},
518#ifdef FEAT_FLOAT
519 {"atan", 1, 1, f_atan},
520 {"atan2", 2, 2, f_atan2},
521#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100522#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200523 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100524 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100525# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100526 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100527# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100528#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529 {"browse", 4, 4, f_browse},
530 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200531 {"bufadd", 1, 1, f_bufadd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200532 {"bufexists", 1, 1, f_bufexists},
533 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
534 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
535 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
536 {"buflisted", 1, 1, f_buflisted},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200537 {"bufload", 1, 1, f_bufload},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200538 {"bufloaded", 1, 1, f_bufloaded},
539 {"bufname", 1, 1, f_bufname},
540 {"bufnr", 1, 2, f_bufnr},
541 {"bufwinid", 1, 1, f_bufwinid},
542 {"bufwinnr", 1, 1, f_bufwinnr},
543 {"byte2line", 1, 1, f_byte2line},
544 {"byteidx", 2, 2, f_byteidx},
545 {"byteidxcomp", 2, 2, f_byteidxcomp},
546 {"call", 2, 3, f_call},
547#ifdef FEAT_FLOAT
548 {"ceil", 1, 1, f_ceil},
549#endif
550#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100551 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200552 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200553 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200554 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
555 {"ch_evalraw", 2, 3, f_ch_evalraw},
556 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
557 {"ch_getjob", 1, 1, f_ch_getjob},
558 {"ch_info", 1, 1, f_ch_info},
559 {"ch_log", 1, 2, f_ch_log},
560 {"ch_logfile", 1, 2, f_ch_logfile},
561 {"ch_open", 1, 2, f_ch_open},
562 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100563 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200564 {"ch_readraw", 1, 2, f_ch_readraw},
565 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
566 {"ch_sendraw", 2, 3, f_ch_sendraw},
567 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200568 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200569#endif
570 {"changenr", 0, 0, f_changenr},
571 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200572 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200573 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100574 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200575 {"col", 1, 1, f_col},
576#if defined(FEAT_INS_EXPAND)
577 {"complete", 2, 2, f_complete},
578 {"complete_add", 1, 1, f_complete_add},
579 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100580 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200581#endif
582 {"confirm", 1, 4, f_confirm},
583 {"copy", 1, 1, f_copy},
584#ifdef FEAT_FLOAT
585 {"cos", 1, 1, f_cos},
586 {"cosh", 1, 1, f_cosh},
587#endif
588 {"count", 2, 4, f_count},
589 {"cscope_connection",0,3, f_cscope_connection},
590 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100591#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200592 {"debugbreak", 1, 1, f_debugbreak},
593#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200594 {"deepcopy", 1, 2, f_deepcopy},
595 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200596 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200597 {"did_filetype", 0, 0, f_did_filetype},
598 {"diff_filler", 1, 1, f_diff_filler},
599 {"diff_hlID", 2, 2, f_diff_hlID},
600 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200601 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200602 {"escape", 2, 2, f_escape},
603 {"eval", 1, 1, f_eval},
604 {"eventhandler", 0, 0, f_eventhandler},
605 {"executable", 1, 1, f_executable},
606 {"execute", 1, 2, f_execute},
607 {"exepath", 1, 1, f_exepath},
608 {"exists", 1, 1, f_exists},
609#ifdef FEAT_FLOAT
610 {"exp", 1, 1, f_exp},
611#endif
612 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200613 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200614 {"extend", 2, 3, f_extend},
615 {"feedkeys", 1, 2, f_feedkeys},
616 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
617 {"filereadable", 1, 1, f_filereadable},
618 {"filewritable", 1, 1, f_filewritable},
619 {"filter", 2, 2, f_filter},
620 {"finddir", 1, 3, f_finddir},
621 {"findfile", 1, 3, f_findfile},
622#ifdef FEAT_FLOAT
623 {"float2nr", 1, 1, f_float2nr},
624 {"floor", 1, 1, f_floor},
625 {"fmod", 2, 2, f_fmod},
626#endif
627 {"fnameescape", 1, 1, f_fnameescape},
628 {"fnamemodify", 2, 2, f_fnamemodify},
629 {"foldclosed", 1, 1, f_foldclosed},
630 {"foldclosedend", 1, 1, f_foldclosedend},
631 {"foldlevel", 1, 1, f_foldlevel},
632 {"foldtext", 0, 0, f_foldtext},
633 {"foldtextresult", 1, 1, f_foldtextresult},
634 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200635 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636 {"function", 1, 3, f_function},
637 {"garbagecollect", 0, 1, f_garbagecollect},
638 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200639 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200640 {"getbufline", 2, 3, f_getbufline},
641 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100642 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200643 {"getchar", 0, 1, f_getchar},
644 {"getcharmod", 0, 0, f_getcharmod},
645 {"getcharsearch", 0, 0, f_getcharsearch},
646 {"getcmdline", 0, 0, f_getcmdline},
647 {"getcmdpos", 0, 0, f_getcmdpos},
648 {"getcmdtype", 0, 0, f_getcmdtype},
649 {"getcmdwintype", 0, 0, f_getcmdwintype},
650#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200651 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200652#endif
653 {"getcurpos", 0, 0, f_getcurpos},
654 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200655 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200656 {"getfontname", 0, 1, f_getfontname},
657 {"getfperm", 1, 1, f_getfperm},
658 {"getfsize", 1, 1, f_getfsize},
659 {"getftime", 1, 1, f_getftime},
660 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100661 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200662 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200663 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100664 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200665 {"getpid", 0, 0, f_getpid},
666 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200667 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200668 {"getreg", 0, 3, f_getreg},
669 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200670 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200671 {"gettabvar", 2, 3, f_gettabvar},
672 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100673 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200674 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100675 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200676 {"getwinposx", 0, 0, f_getwinposx},
677 {"getwinposy", 0, 0, f_getwinposy},
678 {"getwinvar", 2, 3, f_getwinvar},
679 {"glob", 1, 4, f_glob},
680 {"glob2regpat", 1, 1, f_glob2regpat},
681 {"globpath", 2, 5, f_globpath},
682 {"has", 1, 1, f_has},
683 {"has_key", 2, 2, f_has_key},
684 {"haslocaldir", 0, 2, f_haslocaldir},
685 {"hasmapto", 1, 3, f_hasmapto},
686 {"highlightID", 1, 1, f_hlID}, /* obsolete */
687 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
688 {"histadd", 2, 2, f_histadd},
689 {"histdel", 1, 2, f_histdel},
690 {"histget", 1, 2, f_histget},
691 {"histnr", 1, 1, f_histnr},
692 {"hlID", 1, 1, f_hlID},
693 {"hlexists", 1, 1, f_hlexists},
694 {"hostname", 0, 0, f_hostname},
695 {"iconv", 3, 3, f_iconv},
696 {"indent", 1, 1, f_indent},
697 {"index", 2, 4, f_index},
698 {"input", 1, 3, f_input},
699 {"inputdialog", 1, 3, f_inputdialog},
700 {"inputlist", 1, 1, f_inputlist},
701 {"inputrestore", 0, 0, f_inputrestore},
702 {"inputsave", 0, 0, f_inputsave},
703 {"inputsecret", 1, 2, f_inputsecret},
704 {"insert", 2, 3, f_insert},
705 {"invert", 1, 1, f_invert},
706 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200707#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
708 {"isinf", 1, 1, f_isinf},
709#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200710 {"islocked", 1, 1, f_islocked},
711#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
712 {"isnan", 1, 1, f_isnan},
713#endif
714 {"items", 1, 1, f_items},
715#ifdef FEAT_JOB_CHANNEL
716 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200717 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200718 {"job_setoptions", 2, 2, f_job_setoptions},
719 {"job_start", 1, 2, f_job_start},
720 {"job_status", 1, 1, f_job_status},
721 {"job_stop", 1, 2, f_job_stop},
722#endif
723 {"join", 1, 2, f_join},
724 {"js_decode", 1, 1, f_js_decode},
725 {"js_encode", 1, 1, f_js_encode},
726 {"json_decode", 1, 1, f_json_decode},
727 {"json_encode", 1, 1, f_json_encode},
728 {"keys", 1, 1, f_keys},
729 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
730 {"len", 1, 1, f_len},
731 {"libcall", 3, 3, f_libcall},
732 {"libcallnr", 3, 3, f_libcallnr},
733 {"line", 1, 1, f_line},
734 {"line2byte", 1, 1, f_line2byte},
735 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200736 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200737 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200738 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200739 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200740 {"localtime", 0, 0, f_localtime},
741#ifdef FEAT_FLOAT
742 {"log", 1, 1, f_log},
743 {"log10", 1, 1, f_log10},
744#endif
745#ifdef FEAT_LUA
746 {"luaeval", 1, 2, f_luaeval},
747#endif
748 {"map", 2, 2, f_map},
749 {"maparg", 1, 4, f_maparg},
750 {"mapcheck", 1, 3, f_mapcheck},
751 {"match", 2, 4, f_match},
752 {"matchadd", 2, 5, f_matchadd},
753 {"matchaddpos", 2, 5, f_matchaddpos},
754 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100755 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200756 {"matchend", 2, 4, f_matchend},
757 {"matchlist", 2, 4, f_matchlist},
758 {"matchstr", 2, 4, f_matchstr},
759 {"matchstrpos", 2, 4, f_matchstrpos},
760 {"max", 1, 1, f_max},
761 {"min", 1, 1, f_min},
762#ifdef vim_mkdir
763 {"mkdir", 1, 3, f_mkdir},
764#endif
765 {"mode", 0, 1, f_mode},
766#ifdef FEAT_MZSCHEME
767 {"mzeval", 1, 1, f_mzeval},
768#endif
769 {"nextnonblank", 1, 1, f_nextnonblank},
770 {"nr2char", 1, 2, f_nr2char},
771 {"or", 2, 2, f_or},
772 {"pathshorten", 1, 1, f_pathshorten},
773#ifdef FEAT_PERL
774 {"perleval", 1, 1, f_perleval},
775#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200776#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200777 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200778 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200779 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200780 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200781 {"popup_dialog", 2, 2, f_popup_dialog},
Bram Moolenaara730e552019-06-16 19:05:31 +0200782 {"popup_filter_menu", 2, 2, f_popup_filter_menu},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200783 {"popup_filter_yesno", 2, 2, f_popup_filter_yesno},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200784 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200785 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200786 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaara730e552019-06-16 19:05:31 +0200787 {"popup_menu", 2, 2, f_popup_menu},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200788 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200789 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaarae943152019-06-16 22:54:14 +0200790 {"popup_setoptions", 2, 2, f_popup_setoptions},
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200791 {"popup_settext", 2, 2, f_popup_settext},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200792 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200793#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200794#ifdef FEAT_FLOAT
795 {"pow", 2, 2, f_pow},
796#endif
797 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100798 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200799#ifdef FEAT_JOB_CHANNEL
800 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200801 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200802 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
803#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100804#ifdef FEAT_TEXT_PROP
805 {"prop_add", 3, 3, f_prop_add},
806 {"prop_clear", 1, 3, f_prop_clear},
807 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100808 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100809 {"prop_type_add", 2, 2, f_prop_type_add},
810 {"prop_type_change", 2, 2, f_prop_type_change},
811 {"prop_type_delete", 1, 2, f_prop_type_delete},
812 {"prop_type_get", 1, 2, f_prop_type_get},
813 {"prop_type_list", 0, 1, f_prop_type_list},
814#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200815 {"pumvisible", 0, 0, f_pumvisible},
816#ifdef FEAT_PYTHON3
817 {"py3eval", 1, 1, f_py3eval},
818#endif
819#ifdef FEAT_PYTHON
820 {"pyeval", 1, 1, f_pyeval},
821#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100822#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
823 {"pyxeval", 1, 1, f_pyxeval},
824#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200825 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200826 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200827 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200828 {"reg_executing", 0, 0, f_reg_executing},
829 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200830 {"reltime", 0, 2, f_reltime},
831#ifdef FEAT_FLOAT
832 {"reltimefloat", 1, 1, f_reltimefloat},
833#endif
834 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100835 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200836 {"remote_foreground", 1, 1, f_remote_foreground},
837 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100838 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200839 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100840 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200841 {"remove", 2, 3, f_remove},
842 {"rename", 2, 2, f_rename},
843 {"repeat", 2, 2, f_repeat},
844 {"resolve", 1, 1, f_resolve},
845 {"reverse", 1, 1, f_reverse},
846#ifdef FEAT_FLOAT
847 {"round", 1, 1, f_round},
848#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100849#ifdef FEAT_RUBY
850 {"rubyeval", 1, 1, f_rubyeval},
851#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"screenattr", 2, 2, f_screenattr},
853 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100854 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855 {"screencol", 0, 0, f_screencol},
856 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100857 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200858 {"search", 1, 4, f_search},
859 {"searchdecl", 1, 3, f_searchdecl},
860 {"searchpair", 3, 7, f_searchpair},
861 {"searchpairpos", 3, 7, f_searchpairpos},
862 {"searchpos", 1, 4, f_searchpos},
863 {"server2client", 2, 2, f_server2client},
864 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200865 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200866 {"setbufvar", 3, 3, f_setbufvar},
867 {"setcharsearch", 1, 1, f_setcharsearch},
868 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200869 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200870 {"setfperm", 2, 2, f_setfperm},
871 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200872 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100873 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200874 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200875 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200876 {"setreg", 2, 3, f_setreg},
877 {"settabvar", 3, 3, f_settabvar},
878 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100879 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200880 {"setwinvar", 3, 3, f_setwinvar},
881#ifdef FEAT_CRYPT
882 {"sha256", 1, 1, f_sha256},
883#endif
884 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100885 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100886#ifdef FEAT_SIGNS
887 {"sign_define", 1, 2, f_sign_define},
888 {"sign_getdefined", 0, 1, f_sign_getdefined},
889 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100890 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100891 {"sign_place", 4, 5, f_sign_place},
892 {"sign_undefine", 0, 1, f_sign_undefine},
893 {"sign_unplace", 1, 2, f_sign_unplace},
894#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200895 {"simplify", 1, 1, f_simplify},
896#ifdef FEAT_FLOAT
897 {"sin", 1, 1, f_sin},
898 {"sinh", 1, 1, f_sinh},
899#endif
900 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200901#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200902 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200903 {"sound_playevent", 1, 2, f_sound_playevent},
904 {"sound_playfile", 1, 2, f_sound_playfile},
905 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200906#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200907 {"soundfold", 1, 1, f_soundfold},
908 {"spellbadword", 0, 1, f_spellbadword},
909 {"spellsuggest", 1, 3, f_spellsuggest},
910 {"split", 1, 3, f_split},
911#ifdef FEAT_FLOAT
912 {"sqrt", 1, 1, f_sqrt},
913 {"str2float", 1, 1, f_str2float},
914#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200915 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200916 {"str2nr", 1, 2, f_str2nr},
917 {"strcharpart", 2, 3, f_strcharpart},
918 {"strchars", 1, 2, f_strchars},
919 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
920#ifdef HAVE_STRFTIME
921 {"strftime", 1, 2, f_strftime},
922#endif
923 {"strgetchar", 2, 2, f_strgetchar},
924 {"stridx", 2, 3, f_stridx},
925 {"string", 1, 1, f_string},
926 {"strlen", 1, 1, f_strlen},
927 {"strpart", 2, 3, f_strpart},
928 {"strridx", 2, 3, f_strridx},
929 {"strtrans", 1, 1, f_strtrans},
930 {"strwidth", 1, 1, f_strwidth},
931 {"submatch", 1, 2, f_submatch},
932 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200933 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200934 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200935 {"synID", 3, 3, f_synID},
936 {"synIDattr", 2, 3, f_synIDattr},
937 {"synIDtrans", 1, 1, f_synIDtrans},
938 {"synconcealed", 2, 2, f_synconcealed},
939 {"synstack", 2, 2, f_synstack},
940 {"system", 1, 2, f_system},
941 {"systemlist", 1, 2, f_systemlist},
942 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
943 {"tabpagenr", 0, 1, f_tabpagenr},
944 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
945 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100946 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200947#ifdef FEAT_FLOAT
948 {"tan", 1, 1, f_tan},
949 {"tanh", 1, 1, f_tanh},
950#endif
951 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200952#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100953 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
954 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100955 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200956 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200957# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
958 {"term_getansicolors", 1, 1, f_term_getansicolors},
959# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200960 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200961 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200962 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200963 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200964 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200965 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200966 {"term_getstatus", 1, 1, f_term_getstatus},
967 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200968 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200969 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200970 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200971 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200972# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
973 {"term_setansicolors", 2, 2, f_term_setansicolors},
974# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100975 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100976 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200977 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200978 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200979 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200980#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200981 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
982 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200983 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200984 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaaradc67142019-06-22 01:40:42 +0200985 {"test_garbagecollect_soon", 0, 0, f_test_garbagecollect_soon},
Bram Moolenaareda65222019-05-16 20:29:44 +0200986 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100987 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100988 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200989#ifdef FEAT_JOB_CHANNEL
990 {"test_null_channel", 0, 0, f_test_null_channel},
991#endif
992 {"test_null_dict", 0, 0, f_test_null_dict},
993#ifdef FEAT_JOB_CHANNEL
994 {"test_null_job", 0, 0, f_test_null_job},
995#endif
996 {"test_null_list", 0, 0, f_test_null_list},
997 {"test_null_partial", 0, 0, f_test_null_partial},
998 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200999 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001000 {"test_override", 2, 2, f_test_override},
1001 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001002#ifdef FEAT_GUI
1003 {"test_scrollbar", 3, 3, f_test_scrollbar},
1004#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001005#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001006 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001007#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001008 {"test_settime", 1, 1, f_test_settime},
1009#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001010 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001011 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001012 {"timer_start", 2, 3, f_timer_start},
1013 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001014 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001015#endif
1016 {"tolower", 1, 1, f_tolower},
1017 {"toupper", 1, 1, f_toupper},
1018 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001019 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001020#ifdef FEAT_FLOAT
1021 {"trunc", 1, 1, f_trunc},
1022#endif
1023 {"type", 1, 1, f_type},
1024 {"undofile", 1, 1, f_undofile},
1025 {"undotree", 0, 0, f_undotree},
1026 {"uniq", 1, 3, f_uniq},
1027 {"values", 1, 1, f_values},
1028 {"virtcol", 1, 1, f_virtcol},
1029 {"visualmode", 0, 1, f_visualmode},
1030 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001031 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001032 {"win_findbuf", 1, 1, f_win_findbuf},
1033 {"win_getid", 0, 2, f_win_getid},
1034 {"win_gotoid", 1, 1, f_win_gotoid},
1035 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1036 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001037 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001038 {"winbufnr", 1, 1, f_winbufnr},
1039 {"wincol", 0, 0, f_wincol},
1040 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001041 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001042 {"winline", 0, 0, f_winline},
1043 {"winnr", 0, 1, f_winnr},
1044 {"winrestcmd", 0, 0, f_winrestcmd},
1045 {"winrestview", 1, 1, f_winrestview},
1046 {"winsaveview", 0, 0, f_winsaveview},
1047 {"winwidth", 1, 1, f_winwidth},
1048 {"wordcount", 0, 0, f_wordcount},
1049 {"writefile", 2, 3, f_writefile},
1050 {"xor", 2, 2, f_xor},
1051};
1052
1053#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1054
1055/*
1056 * Function given to ExpandGeneric() to obtain the list of internal
1057 * or user defined function names.
1058 */
1059 char_u *
1060get_function_name(expand_T *xp, int idx)
1061{
1062 static int intidx = -1;
1063 char_u *name;
1064
1065 if (idx == 0)
1066 intidx = -1;
1067 if (intidx < 0)
1068 {
1069 name = get_user_func_name(xp, idx);
1070 if (name != NULL)
1071 return name;
1072 }
1073 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1074 {
1075 STRCPY(IObuff, functions[intidx].f_name);
1076 STRCAT(IObuff, "(");
1077 if (functions[intidx].f_max_argc == 0)
1078 STRCAT(IObuff, ")");
1079 return IObuff;
1080 }
1081
1082 return NULL;
1083}
1084
1085/*
1086 * Function given to ExpandGeneric() to obtain the list of internal or
1087 * user defined variable or function names.
1088 */
1089 char_u *
1090get_expr_name(expand_T *xp, int idx)
1091{
1092 static int intidx = -1;
1093 char_u *name;
1094
1095 if (idx == 0)
1096 intidx = -1;
1097 if (intidx < 0)
1098 {
1099 name = get_function_name(xp, idx);
1100 if (name != NULL)
1101 return name;
1102 }
1103 return get_user_var_name(xp, ++intidx);
1104}
1105
1106#endif /* FEAT_CMDL_COMPL */
1107
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001108/*
1109 * Find internal function in table above.
1110 * Return index, or -1 if not found
1111 */
1112 int
1113find_internal_func(
1114 char_u *name) /* name of the function */
1115{
1116 int first = 0;
1117 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1118 int cmp;
1119 int x;
1120
1121 /*
1122 * Find the function name in the table. Binary search.
1123 */
1124 while (first <= last)
1125 {
1126 x = first + ((unsigned)(last - first) >> 1);
1127 cmp = STRCMP(name, functions[x].f_name);
1128 if (cmp < 0)
1129 last = x - 1;
1130 else if (cmp > 0)
1131 first = x + 1;
1132 else
1133 return x;
1134 }
1135 return -1;
1136}
1137
1138 int
1139call_internal_func(
1140 char_u *name,
1141 int argcount,
1142 typval_T *argvars,
1143 typval_T *rettv)
1144{
1145 int i;
1146
1147 i = find_internal_func(name);
1148 if (i < 0)
1149 return ERROR_UNKNOWN;
1150 if (argcount < functions[i].f_min_argc)
1151 return ERROR_TOOFEW;
1152 if (argcount > functions[i].f_max_argc)
1153 return ERROR_TOOMANY;
1154 argvars[argcount].v_type = VAR_UNKNOWN;
1155 functions[i].f_func(argvars, rettv);
1156 return ERROR_NONE;
1157}
1158
1159/*
1160 * Return TRUE for a non-zero Number and a non-empty String.
1161 */
1162 static int
1163non_zero_arg(typval_T *argvars)
1164{
1165 return ((argvars[0].v_type == VAR_NUMBER
1166 && argvars[0].vval.v_number != 0)
1167 || (argvars[0].v_type == VAR_SPECIAL
1168 && argvars[0].vval.v_number == VVAL_TRUE)
1169 || (argvars[0].v_type == VAR_STRING
1170 && argvars[0].vval.v_string != NULL
1171 && *argvars[0].vval.v_string != NUL));
1172}
1173
1174/*
1175 * Get the lnum from the first argument.
1176 * Also accepts ".", "$", etc., but that only works for the current buffer.
1177 * Returns -1 on error.
1178 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001179 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001180tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001181{
1182 typval_T rettv;
1183 linenr_T lnum;
1184
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001185 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001186 if (lnum == 0) /* no valid number, try using line() */
1187 {
1188 rettv.v_type = VAR_NUMBER;
1189 f_line(argvars, &rettv);
1190 lnum = (linenr_T)rettv.vval.v_number;
1191 clear_tv(&rettv);
1192 }
1193 return lnum;
1194}
1195
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001196/*
1197 * Get the lnum from the first argument.
1198 * Also accepts "$", then "buf" is used.
1199 * Returns 0 on error.
1200 */
1201 static linenr_T
1202tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1203{
1204 if (argvars[0].v_type == VAR_STRING
1205 && argvars[0].vval.v_string != NULL
1206 && argvars[0].vval.v_string[0] == '$'
1207 && buf != NULL)
1208 return buf->b_ml.ml_line_count;
1209 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1210}
1211
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001212#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001213/*
1214 * Get the float value of "argvars[0]" into "f".
1215 * Returns FAIL when the argument is not a Number or Float.
1216 */
1217 static int
1218get_float_arg(typval_T *argvars, float_T *f)
1219{
1220 if (argvars[0].v_type == VAR_FLOAT)
1221 {
1222 *f = argvars[0].vval.v_float;
1223 return OK;
1224 }
1225 if (argvars[0].v_type == VAR_NUMBER)
1226 {
1227 *f = (float_T)argvars[0].vval.v_number;
1228 return OK;
1229 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001230 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001231 return FAIL;
1232}
1233
1234/*
1235 * "abs(expr)" function
1236 */
1237 static void
1238f_abs(typval_T *argvars, typval_T *rettv)
1239{
1240 if (argvars[0].v_type == VAR_FLOAT)
1241 {
1242 rettv->v_type = VAR_FLOAT;
1243 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1244 }
1245 else
1246 {
1247 varnumber_T n;
1248 int error = FALSE;
1249
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001250 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001251 if (error)
1252 rettv->vval.v_number = -1;
1253 else if (n > 0)
1254 rettv->vval.v_number = n;
1255 else
1256 rettv->vval.v_number = -n;
1257 }
1258}
1259
1260/*
1261 * "acos()" function
1262 */
1263 static void
1264f_acos(typval_T *argvars, typval_T *rettv)
1265{
1266 float_T f = 0.0;
1267
1268 rettv->v_type = VAR_FLOAT;
1269 if (get_float_arg(argvars, &f) == OK)
1270 rettv->vval.v_float = acos(f);
1271 else
1272 rettv->vval.v_float = 0.0;
1273}
1274#endif
1275
1276/*
1277 * "add(list, item)" function
1278 */
1279 static void
1280f_add(typval_T *argvars, typval_T *rettv)
1281{
1282 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001283 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001284
1285 rettv->vval.v_number = 1; /* Default: Failed */
1286 if (argvars[0].v_type == VAR_LIST)
1287 {
1288 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001289 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001290 (char_u *)N_("add() argument"), TRUE)
1291 && list_append_tv(l, &argvars[1]) == OK)
1292 copy_tv(&argvars[0], rettv);
1293 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294 else if (argvars[0].v_type == VAR_BLOB)
1295 {
1296 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001297 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001298 (char_u *)N_("add() argument"), TRUE))
1299 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001300 int error = FALSE;
1301 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1302
1303 if (!error)
1304 {
1305 ga_append(&b->bv_ga, (int)n);
1306 copy_tv(&argvars[0], rettv);
1307 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001308 }
1309 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001310 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001311 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001312}
1313
1314/*
1315 * "and(expr, expr)" function
1316 */
1317 static void
1318f_and(typval_T *argvars, typval_T *rettv)
1319{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001320 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1321 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001322}
1323
1324/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001325 * If there is a window for "curbuf", make it the current window.
1326 */
1327 static void
1328find_win_for_curbuf(void)
1329{
1330 wininfo_T *wip;
1331
1332 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1333 {
1334 if (wip->wi_win != NULL)
1335 {
1336 curwin = wip->wi_win;
1337 break;
1338 }
1339 }
1340}
1341
1342/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001343 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001344 */
1345 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001346set_buffer_lines(
1347 buf_T *buf,
1348 linenr_T lnum_arg,
1349 int append,
1350 typval_T *lines,
1351 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001352{
Bram Moolenaarca851592018-06-06 21:04:07 +02001353 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1354 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001355 list_T *l = NULL;
1356 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001357 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001358 linenr_T append_lnum;
1359 buf_T *curbuf_save = NULL;
1360 win_T *curwin_save = NULL;
1361 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001362
Bram Moolenaarca851592018-06-06 21:04:07 +02001363 /* When using the current buffer ml_mfp will be set if needed. Useful when
1364 * setline() is used on startup. For other buffers the buffer must be
1365 * loaded. */
1366 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001367 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001368 rettv->vval.v_number = 1; /* FAIL */
1369 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001370 }
1371
Bram Moolenaarca851592018-06-06 21:04:07 +02001372 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001373 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001374 curbuf_save = curbuf;
1375 curwin_save = curwin;
1376 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001377 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001378 }
1379
1380 if (append)
1381 // appendbufline() uses the line number below which we insert
1382 append_lnum = lnum - 1;
1383 else
1384 // setbufline() uses the line number above which we insert, we only
1385 // append if it's below the last line
1386 append_lnum = curbuf->b_ml.ml_line_count;
1387
1388 if (lines->v_type == VAR_LIST)
1389 {
1390 l = lines->vval.v_list;
1391 li = l->lv_first;
1392 }
1393 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001394 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001395
1396 /* default result is zero == OK */
1397 for (;;)
1398 {
1399 if (l != NULL)
1400 {
1401 /* list argument, get next string */
1402 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001403 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001404 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001405 li = li->li_next;
1406 }
1407
Bram Moolenaarca851592018-06-06 21:04:07 +02001408 rettv->vval.v_number = 1; /* FAIL */
1409 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1410 break;
1411
1412 /* When coming here from Insert mode, sync undo, so that this can be
1413 * undone separately from what was previously inserted. */
1414 if (u_sync_once == 2)
1415 {
1416 u_sync_once = 1; /* notify that u_sync() was called */
1417 u_sync(TRUE);
1418 }
1419
1420 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1421 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001422 // Existing line, replace it.
1423 // Removes any existing text properties.
1424 if (u_savesub(lnum) == OK && ml_replace_len(
1425 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001426 {
1427 changed_bytes(lnum, 0);
1428 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1429 check_cursor_col();
1430 rettv->vval.v_number = 0; /* OK */
1431 }
1432 }
1433 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1434 {
1435 /* append the line */
1436 ++added;
1437 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1438 rettv->vval.v_number = 0; /* OK */
1439 }
1440
1441 if (l == NULL) /* only one string argument */
1442 break;
1443 ++lnum;
1444 }
1445
1446 if (added > 0)
1447 {
1448 win_T *wp;
1449 tabpage_T *tp;
1450
1451 appended_lines_mark(append_lnum, added);
1452 FOR_ALL_TAB_WINDOWS(tp, wp)
1453 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1454 wp->w_cursor.lnum += added;
1455 check_cursor_col();
1456
Bram Moolenaarf2732452018-06-03 14:47:35 +02001457#ifdef FEAT_JOB_CHANNEL
1458 if (bt_prompt(curbuf) && (State & INSERT))
1459 // show the line with the prompt
1460 update_topline();
1461#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001462 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001463
1464 if (!is_curbuf)
1465 {
1466 curbuf = curbuf_save;
1467 curwin = curwin_save;
1468 }
1469}
1470
1471/*
1472 * "append(lnum, string/list)" function
1473 */
1474 static void
1475f_append(typval_T *argvars, typval_T *rettv)
1476{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001477 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001478
1479 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1480}
1481
1482/*
1483 * "appendbufline(buf, lnum, string/list)" function
1484 */
1485 static void
1486f_appendbufline(typval_T *argvars, typval_T *rettv)
1487{
1488 linenr_T lnum;
1489 buf_T *buf;
1490
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001491 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001492 if (buf == NULL)
1493 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001494 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001495 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001496 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001497 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1498 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001499}
1500
1501/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001502 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001503 */
1504 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001505f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001506{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001507 win_T *wp;
1508
1509 if (argvars[0].v_type == VAR_UNKNOWN)
1510 // use the current window
1511 rettv->vval.v_number = ARGCOUNT;
1512 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001513 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001514 // use the global argument list
1515 rettv->vval.v_number = GARGCOUNT;
1516 else
1517 {
1518 // use the argument list of the specified window
1519 wp = find_win_by_nr_or_id(&argvars[0]);
1520 if (wp != NULL)
1521 rettv->vval.v_number = WARGCOUNT(wp);
1522 else
1523 rettv->vval.v_number = -1;
1524 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001525}
1526
1527/*
1528 * "argidx()" function
1529 */
1530 static void
1531f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1532{
1533 rettv->vval.v_number = curwin->w_arg_idx;
1534}
1535
1536/*
1537 * "arglistid()" function
1538 */
1539 static void
1540f_arglistid(typval_T *argvars, typval_T *rettv)
1541{
1542 win_T *wp;
1543
1544 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001545 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001546 if (wp != NULL)
1547 rettv->vval.v_number = wp->w_alist->id;
1548}
1549
1550/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001551 * Get the argument list for a given window
1552 */
1553 static void
1554get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1555{
1556 int idx;
1557
1558 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1559 for (idx = 0; idx < argcount; ++idx)
1560 list_append_string(rettv->vval.v_list,
1561 alist_name(&arglist[idx]), -1);
1562}
1563
1564/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001565 * "argv(nr)" function
1566 */
1567 static void
1568f_argv(typval_T *argvars, typval_T *rettv)
1569{
1570 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001571 aentry_T *arglist = NULL;
1572 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001573
1574 if (argvars[0].v_type != VAR_UNKNOWN)
1575 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001576 if (argvars[1].v_type == VAR_UNKNOWN)
1577 {
1578 arglist = ARGLIST;
1579 argcount = ARGCOUNT;
1580 }
1581 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001582 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001583 {
1584 arglist = GARGLIST;
1585 argcount = GARGCOUNT;
1586 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001587 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001588 {
1589 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1590
1591 if (wp != NULL)
1592 {
1593 /* Use the argument list of the specified window */
1594 arglist = WARGLIST(wp);
1595 argcount = WARGCOUNT(wp);
1596 }
1597 }
1598
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001599 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001600 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001601 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001602 if (arglist != NULL && idx >= 0 && idx < argcount)
1603 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1604 else if (idx == -1)
1605 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001606 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001607 else
1608 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001609}
1610
1611/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001612 * "assert_beeps(cmd [, error])" function
1613 */
1614 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001615f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001616{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001617 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001618}
1619
1620/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001621 * "assert_equal(expected, actual[, msg])" function
1622 */
1623 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001624f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001625{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001626 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001627}
1628
1629/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001630 * "assert_equalfile(fname-one, fname-two)" function
1631 */
1632 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001633f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001634{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001635 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001636}
1637
1638/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001639 * "assert_notequal(expected, actual[, msg])" function
1640 */
1641 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001642f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001643{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001644 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001645}
1646
1647/*
1648 * "assert_exception(string[, msg])" function
1649 */
1650 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001651f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001652{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001653 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001654}
1655
1656/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001657 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001658 */
1659 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001660f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001661{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001662 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001663}
1664
1665/*
1666 * "assert_false(actual[, msg])" function
1667 */
1668 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001669f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001670{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001671 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001672}
1673
1674/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001675 * "assert_inrange(lower, upper[, msg])" function
1676 */
1677 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001678f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001679{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001680 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001681}
1682
1683/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001684 * "assert_match(pattern, actual[, msg])" function
1685 */
1686 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001687f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001688{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001689 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001690}
1691
1692/*
1693 * "assert_notmatch(pattern, actual[, msg])" function
1694 */
1695 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001696f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001697{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001698 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001699}
1700
1701/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001702 * "assert_report(msg)" function
1703 */
1704 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001705f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001706{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001707 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001708}
1709
1710/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001711 * "assert_true(actual[, msg])" function
1712 */
1713 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001714f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001715{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001716 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001717}
1718
1719#ifdef FEAT_FLOAT
1720/*
1721 * "asin()" function
1722 */
1723 static void
1724f_asin(typval_T *argvars, typval_T *rettv)
1725{
1726 float_T f = 0.0;
1727
1728 rettv->v_type = VAR_FLOAT;
1729 if (get_float_arg(argvars, &f) == OK)
1730 rettv->vval.v_float = asin(f);
1731 else
1732 rettv->vval.v_float = 0.0;
1733}
1734
1735/*
1736 * "atan()" function
1737 */
1738 static void
1739f_atan(typval_T *argvars, typval_T *rettv)
1740{
1741 float_T f = 0.0;
1742
1743 rettv->v_type = VAR_FLOAT;
1744 if (get_float_arg(argvars, &f) == OK)
1745 rettv->vval.v_float = atan(f);
1746 else
1747 rettv->vval.v_float = 0.0;
1748}
1749
1750/*
1751 * "atan2()" function
1752 */
1753 static void
1754f_atan2(typval_T *argvars, typval_T *rettv)
1755{
1756 float_T fx = 0.0, fy = 0.0;
1757
1758 rettv->v_type = VAR_FLOAT;
1759 if (get_float_arg(argvars, &fx) == OK
1760 && get_float_arg(&argvars[1], &fy) == OK)
1761 rettv->vval.v_float = atan2(fx, fy);
1762 else
1763 rettv->vval.v_float = 0.0;
1764}
1765#endif
1766
1767/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001768 * "balloon_show()" function
1769 */
1770#ifdef FEAT_BEVAL
1771 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001772f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1773{
1774 rettv->v_type = VAR_STRING;
1775 if (balloonEval != NULL)
1776 {
1777 if (balloonEval->msg == NULL)
1778 rettv->vval.v_string = NULL;
1779 else
1780 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1781 }
1782}
1783
1784 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001785f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1786{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001787 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001788 {
1789 if (argvars[0].v_type == VAR_LIST
1790# ifdef FEAT_GUI
1791 && !gui.in_use
1792# endif
1793 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001794 {
1795 list_T *l = argvars[0].vval.v_list;
1796
1797 // empty list removes the balloon
1798 post_balloon(balloonEval, NULL,
1799 l == NULL || l->lv_len == 0 ? NULL : l);
1800 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001801 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001802 {
1803 char_u *mesg = tv_get_string_chk(&argvars[0]);
1804
1805 if (mesg != NULL)
1806 // empty string removes the balloon
1807 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1808 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001809 }
1810}
1811
Bram Moolenaar669a8282017-11-19 20:13:05 +01001812# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001813 static void
1814f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1815{
1816 if (rettv_list_alloc(rettv) == OK)
1817 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001818 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001819
1820 if (msg != NULL)
1821 {
1822 pumitem_T *array;
1823 int size = split_message(msg, &array);
1824 int i;
1825
1826 /* Skip the first and last item, they are always empty. */
1827 for (i = 1; i < size - 1; ++i)
1828 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001829 while (size > 0)
1830 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001831 vim_free(array);
1832 }
1833 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001834}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001835# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001836#endif
1837
1838/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001839 * "browse(save, title, initdir, default)" function
1840 */
1841 static void
1842f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1843{
1844#ifdef FEAT_BROWSE
1845 int save;
1846 char_u *title;
1847 char_u *initdir;
1848 char_u *defname;
1849 char_u buf[NUMBUFLEN];
1850 char_u buf2[NUMBUFLEN];
1851 int error = FALSE;
1852
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001853 save = (int)tv_get_number_chk(&argvars[0], &error);
1854 title = tv_get_string_chk(&argvars[1]);
1855 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1856 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001857
1858 if (error || title == NULL || initdir == NULL || defname == NULL)
1859 rettv->vval.v_string = NULL;
1860 else
1861 rettv->vval.v_string =
1862 do_browse(save ? BROWSE_SAVE : 0,
1863 title, defname, NULL, initdir, NULL, curbuf);
1864#else
1865 rettv->vval.v_string = NULL;
1866#endif
1867 rettv->v_type = VAR_STRING;
1868}
1869
1870/*
1871 * "browsedir(title, initdir)" function
1872 */
1873 static void
1874f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1875{
1876#ifdef FEAT_BROWSE
1877 char_u *title;
1878 char_u *initdir;
1879 char_u buf[NUMBUFLEN];
1880
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001881 title = tv_get_string_chk(&argvars[0]);
1882 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001883
1884 if (title == NULL || initdir == NULL)
1885 rettv->vval.v_string = NULL;
1886 else
1887 rettv->vval.v_string = do_browse(BROWSE_DIR,
1888 title, NULL, NULL, initdir, NULL, curbuf);
1889#else
1890 rettv->vval.v_string = NULL;
1891#endif
1892 rettv->v_type = VAR_STRING;
1893}
1894
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001895/*
1896 * Find a buffer by number or exact name.
1897 */
1898 static buf_T *
1899find_buffer(typval_T *avar)
1900{
1901 buf_T *buf = NULL;
1902
1903 if (avar->v_type == VAR_NUMBER)
1904 buf = buflist_findnr((int)avar->vval.v_number);
1905 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1906 {
1907 buf = buflist_findname_exp(avar->vval.v_string);
1908 if (buf == NULL)
1909 {
1910 /* No full path name match, try a match with a URL or a "nofile"
1911 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001912 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913 if (buf->b_fname != NULL
1914 && (path_with_url(buf->b_fname)
1915#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001916 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001917#endif
1918 )
1919 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1920 break;
1921 }
1922 }
1923 return buf;
1924}
1925
1926/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001927 * "bufadd(expr)" function
1928 */
1929 static void
1930f_bufadd(typval_T *argvars, typval_T *rettv)
1931{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001932 char_u *name = tv_get_string(&argvars[0]);
1933
1934 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001935}
1936
1937/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001938 * "bufexists(expr)" function
1939 */
1940 static void
1941f_bufexists(typval_T *argvars, typval_T *rettv)
1942{
1943 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1944}
1945
1946/*
1947 * "buflisted(expr)" function
1948 */
1949 static void
1950f_buflisted(typval_T *argvars, typval_T *rettv)
1951{
1952 buf_T *buf;
1953
1954 buf = find_buffer(&argvars[0]);
1955 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1956}
1957
1958/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001959 * "bufload(expr)" function
1960 */
1961 static void
1962f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1963{
1964 buf_T *buf = get_buf_arg(&argvars[0]);
1965
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001966 if (buf != NULL)
1967 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001968}
1969
1970/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001971 * "bufloaded(expr)" function
1972 */
1973 static void
1974f_bufloaded(typval_T *argvars, typval_T *rettv)
1975{
1976 buf_T *buf;
1977
1978 buf = find_buffer(&argvars[0]);
1979 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1980}
1981
1982 buf_T *
1983buflist_find_by_name(char_u *name, int curtab_only)
1984{
1985 int save_magic;
1986 char_u *save_cpo;
1987 buf_T *buf;
1988
1989 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1990 save_magic = p_magic;
1991 p_magic = TRUE;
1992 save_cpo = p_cpo;
1993 p_cpo = (char_u *)"";
1994
1995 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1996 TRUE, FALSE, curtab_only));
1997
1998 p_magic = save_magic;
1999 p_cpo = save_cpo;
2000 return buf;
2001}
2002
2003/*
2004 * Get buffer by number or pattern.
2005 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002006 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002007tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002008{
2009 char_u *name = tv->vval.v_string;
2010 buf_T *buf;
2011
2012 if (tv->v_type == VAR_NUMBER)
2013 return buflist_findnr((int)tv->vval.v_number);
2014 if (tv->v_type != VAR_STRING)
2015 return NULL;
2016 if (name == NULL || *name == NUL)
2017 return curbuf;
2018 if (name[0] == '$' && name[1] == NUL)
2019 return lastbuf;
2020
2021 buf = buflist_find_by_name(name, curtab_only);
2022
2023 /* If not found, try expanding the name, like done for bufexists(). */
2024 if (buf == NULL)
2025 buf = find_buffer(tv);
2026
2027 return buf;
2028}
2029
2030/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002031 * Get the buffer from "arg" and give an error and return NULL if it is not
2032 * valid.
2033 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002034 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002035get_buf_arg(typval_T *arg)
2036{
2037 buf_T *buf;
2038
2039 ++emsg_off;
2040 buf = tv_get_buf(arg, FALSE);
2041 --emsg_off;
2042 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002043 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002044 return buf;
2045}
2046
2047/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002048 * "bufname(expr)" function
2049 */
2050 static void
2051f_bufname(typval_T *argvars, typval_T *rettv)
2052{
2053 buf_T *buf;
2054
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002055 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002056 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002057 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002058 rettv->v_type = VAR_STRING;
2059 if (buf != NULL && buf->b_fname != NULL)
2060 rettv->vval.v_string = vim_strsave(buf->b_fname);
2061 else
2062 rettv->vval.v_string = NULL;
2063 --emsg_off;
2064}
2065
2066/*
2067 * "bufnr(expr)" function
2068 */
2069 static void
2070f_bufnr(typval_T *argvars, typval_T *rettv)
2071{
2072 buf_T *buf;
2073 int error = FALSE;
2074 char_u *name;
2075
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002076 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002077 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002078 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002079 --emsg_off;
2080
2081 /* If the buffer isn't found and the second argument is not zero create a
2082 * new buffer. */
2083 if (buf == NULL
2084 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002085 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002086 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002087 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002088 && !error)
2089 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2090
2091 if (buf != NULL)
2092 rettv->vval.v_number = buf->b_fnum;
2093 else
2094 rettv->vval.v_number = -1;
2095}
2096
2097 static void
2098buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2099{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002100 win_T *wp;
2101 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002102 buf_T *buf;
2103
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002104 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002105 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002106 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002107 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002108 {
2109 ++winnr;
2110 if (wp->w_buffer == buf)
2111 break;
2112 }
2113 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002114 --emsg_off;
2115}
2116
2117/*
2118 * "bufwinid(nr)" function
2119 */
2120 static void
2121f_bufwinid(typval_T *argvars, typval_T *rettv)
2122{
2123 buf_win_common(argvars, rettv, FALSE);
2124}
2125
2126/*
2127 * "bufwinnr(nr)" function
2128 */
2129 static void
2130f_bufwinnr(typval_T *argvars, typval_T *rettv)
2131{
2132 buf_win_common(argvars, rettv, TRUE);
2133}
2134
2135/*
2136 * "byte2line(byte)" function
2137 */
2138 static void
2139f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2140{
2141#ifndef FEAT_BYTEOFF
2142 rettv->vval.v_number = -1;
2143#else
2144 long boff = 0;
2145
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002146 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002147 if (boff < 0)
2148 rettv->vval.v_number = -1;
2149 else
2150 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2151 (linenr_T)0, &boff);
2152#endif
2153}
2154
2155 static void
2156byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2157{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002158 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002159 char_u *str;
2160 varnumber_T idx;
2161
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002162 str = tv_get_string_chk(&argvars[0]);
2163 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164 rettv->vval.v_number = -1;
2165 if (str == NULL || idx < 0)
2166 return;
2167
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002168 t = str;
2169 for ( ; idx > 0; idx--)
2170 {
2171 if (*t == NUL) /* EOL reached */
2172 return;
2173 if (enc_utf8 && comp)
2174 t += utf_ptr2len(t);
2175 else
2176 t += (*mb_ptr2len)(t);
2177 }
2178 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002179}
2180
2181/*
2182 * "byteidx()" function
2183 */
2184 static void
2185f_byteidx(typval_T *argvars, typval_T *rettv)
2186{
2187 byteidx(argvars, rettv, FALSE);
2188}
2189
2190/*
2191 * "byteidxcomp()" function
2192 */
2193 static void
2194f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2195{
2196 byteidx(argvars, rettv, TRUE);
2197}
2198
2199/*
2200 * "call(func, arglist [, dict])" function
2201 */
2202 static void
2203f_call(typval_T *argvars, typval_T *rettv)
2204{
2205 char_u *func;
2206 partial_T *partial = NULL;
2207 dict_T *selfdict = NULL;
2208
2209 if (argvars[1].v_type != VAR_LIST)
2210 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002211 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002212 return;
2213 }
2214 if (argvars[1].vval.v_list == NULL)
2215 return;
2216
2217 if (argvars[0].v_type == VAR_FUNC)
2218 func = argvars[0].vval.v_string;
2219 else if (argvars[0].v_type == VAR_PARTIAL)
2220 {
2221 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002222 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002223 }
2224 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002225 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002226 if (*func == NUL)
2227 return; /* type error or empty name */
2228
2229 if (argvars[2].v_type != VAR_UNKNOWN)
2230 {
2231 if (argvars[2].v_type != VAR_DICT)
2232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002233 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002234 return;
2235 }
2236 selfdict = argvars[2].vval.v_dict;
2237 }
2238
2239 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2240}
2241
2242#ifdef FEAT_FLOAT
2243/*
2244 * "ceil({float})" function
2245 */
2246 static void
2247f_ceil(typval_T *argvars, typval_T *rettv)
2248{
2249 float_T f = 0.0;
2250
2251 rettv->v_type = VAR_FLOAT;
2252 if (get_float_arg(argvars, &f) == OK)
2253 rettv->vval.v_float = ceil(f);
2254 else
2255 rettv->vval.v_float = 0.0;
2256}
2257#endif
2258
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002259/*
2260 * "changenr()" function
2261 */
2262 static void
2263f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2264{
2265 rettv->vval.v_number = curbuf->b_u_seq_cur;
2266}
2267
2268/*
2269 * "char2nr(string)" function
2270 */
2271 static void
2272f_char2nr(typval_T *argvars, typval_T *rettv)
2273{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002274 if (has_mbyte)
2275 {
2276 int utf8 = 0;
2277
2278 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002279 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002280
2281 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002282 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002283 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002284 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002285 }
2286 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002287 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002288}
2289
2290/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002291 * "chdir(dir)" function
2292 */
2293 static void
2294f_chdir(typval_T *argvars, typval_T *rettv)
2295{
2296 char_u *cwd;
2297 cdscope_T scope = CDSCOPE_GLOBAL;
2298
2299 rettv->v_type = VAR_STRING;
2300 rettv->vval.v_string = NULL;
2301
2302 if (argvars[0].v_type != VAR_STRING)
2303 return;
2304
2305 // Return the current directory
2306 cwd = alloc(MAXPATHL);
2307 if (cwd != NULL)
2308 {
2309 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2310 {
2311#ifdef BACKSLASH_IN_FILENAME
2312 slash_adjust(cwd);
2313#endif
2314 rettv->vval.v_string = vim_strsave(cwd);
2315 }
2316 vim_free(cwd);
2317 }
2318
2319 if (curwin->w_localdir != NULL)
2320 scope = CDSCOPE_WINDOW;
2321 else if (curtab->tp_localdir != NULL)
2322 scope = CDSCOPE_TABPAGE;
2323
2324 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2325 // Directory change failed
2326 VIM_CLEAR(rettv->vval.v_string);
2327}
2328
2329/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002330 * "cindent(lnum)" function
2331 */
2332 static void
2333f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2334{
2335#ifdef FEAT_CINDENT
2336 pos_T pos;
2337 linenr_T lnum;
2338
2339 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002340 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002341 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2342 {
2343 curwin->w_cursor.lnum = lnum;
2344 rettv->vval.v_number = get_c_indent();
2345 curwin->w_cursor = pos;
2346 }
2347 else
2348#endif
2349 rettv->vval.v_number = -1;
2350}
2351
Bram Moolenaaraff74912019-03-30 18:11:49 +01002352 static win_T *
2353get_optional_window(typval_T *argvars, int idx)
2354{
2355 win_T *win = curwin;
2356
2357 if (argvars[idx].v_type != VAR_UNKNOWN)
2358 {
2359 win = find_win_by_nr_or_id(&argvars[idx]);
2360 if (win == NULL)
2361 {
2362 emsg(_(e_invalwindow));
2363 return NULL;
2364 }
2365 }
2366 return win;
2367}
2368
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002369/*
2370 * "clearmatches()" function
2371 */
2372 static void
2373f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2374{
2375#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002376 win_T *win = get_optional_window(argvars, 0);
2377
2378 if (win != NULL)
2379 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002380#endif
2381}
2382
2383/*
2384 * "col(string)" function
2385 */
2386 static void
2387f_col(typval_T *argvars, typval_T *rettv)
2388{
2389 colnr_T col = 0;
2390 pos_T *fp;
2391 int fnum = curbuf->b_fnum;
2392
2393 fp = var2fpos(&argvars[0], FALSE, &fnum);
2394 if (fp != NULL && fnum == curbuf->b_fnum)
2395 {
2396 if (fp->col == MAXCOL)
2397 {
2398 /* '> can be MAXCOL, get the length of the line then */
2399 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2400 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2401 else
2402 col = MAXCOL;
2403 }
2404 else
2405 {
2406 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002407 /* col(".") when the cursor is on the NUL at the end of the line
2408 * because of "coladd" can be seen as an extra column. */
2409 if (virtual_active() && fp == &curwin->w_cursor)
2410 {
2411 char_u *p = ml_get_cursor();
2412
2413 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2414 curwin->w_virtcol - curwin->w_cursor.coladd))
2415 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002416 int l;
2417
2418 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2419 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002420 }
2421 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002422 }
2423 }
2424 rettv->vval.v_number = col;
2425}
2426
2427#if defined(FEAT_INS_EXPAND)
2428/*
2429 * "complete()" function
2430 */
2431 static void
2432f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2433{
2434 int startcol;
2435
2436 if ((State & INSERT) == 0)
2437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002438 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002439 return;
2440 }
2441
2442 /* Check for undo allowed here, because if something was already inserted
2443 * the line was already saved for undo and this check isn't done. */
2444 if (!undo_allowed())
2445 return;
2446
2447 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2448 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002449 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002450 return;
2451 }
2452
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002453 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002454 if (startcol <= 0)
2455 return;
2456
2457 set_completion(startcol - 1, argvars[1].vval.v_list);
2458}
2459
2460/*
2461 * "complete_add()" function
2462 */
2463 static void
2464f_complete_add(typval_T *argvars, typval_T *rettv)
2465{
2466 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2467}
2468
2469/*
2470 * "complete_check()" function
2471 */
2472 static void
2473f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2474{
2475 int saved = RedrawingDisabled;
2476
2477 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002478 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002479 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002480 RedrawingDisabled = saved;
2481}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002482
2483/*
2484 * "complete_info()" function
2485 */
2486 static void
2487f_complete_info(typval_T *argvars, typval_T *rettv)
2488{
2489 list_T *what_list = NULL;
2490
2491 if (rettv_dict_alloc(rettv) != OK)
2492 return;
2493
2494 if (argvars[0].v_type != VAR_UNKNOWN)
2495 {
2496 if (argvars[0].v_type != VAR_LIST)
2497 {
2498 emsg(_(e_listreq));
2499 return;
2500 }
2501 what_list = argvars[0].vval.v_list;
2502 }
2503 get_complete_info(what_list, rettv->vval.v_dict);
2504}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002505#endif
2506
2507/*
2508 * "confirm(message, buttons[, default [, type]])" function
2509 */
2510 static void
2511f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2512{
2513#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2514 char_u *message;
2515 char_u *buttons = NULL;
2516 char_u buf[NUMBUFLEN];
2517 char_u buf2[NUMBUFLEN];
2518 int def = 1;
2519 int type = VIM_GENERIC;
2520 char_u *typestr;
2521 int error = FALSE;
2522
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002523 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002524 if (message == NULL)
2525 error = TRUE;
2526 if (argvars[1].v_type != VAR_UNKNOWN)
2527 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002528 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002529 if (buttons == NULL)
2530 error = TRUE;
2531 if (argvars[2].v_type != VAR_UNKNOWN)
2532 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002533 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002534 if (argvars[3].v_type != VAR_UNKNOWN)
2535 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002536 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002537 if (typestr == NULL)
2538 error = TRUE;
2539 else
2540 {
2541 switch (TOUPPER_ASC(*typestr))
2542 {
2543 case 'E': type = VIM_ERROR; break;
2544 case 'Q': type = VIM_QUESTION; break;
2545 case 'I': type = VIM_INFO; break;
2546 case 'W': type = VIM_WARNING; break;
2547 case 'G': type = VIM_GENERIC; break;
2548 }
2549 }
2550 }
2551 }
2552 }
2553
2554 if (buttons == NULL || *buttons == NUL)
2555 buttons = (char_u *)_("&Ok");
2556
2557 if (!error)
2558 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2559 def, NULL, FALSE);
2560#endif
2561}
2562
2563/*
2564 * "copy()" function
2565 */
2566 static void
2567f_copy(typval_T *argvars, typval_T *rettv)
2568{
2569 item_copy(&argvars[0], rettv, FALSE, 0);
2570}
2571
2572#ifdef FEAT_FLOAT
2573/*
2574 * "cos()" function
2575 */
2576 static void
2577f_cos(typval_T *argvars, typval_T *rettv)
2578{
2579 float_T f = 0.0;
2580
2581 rettv->v_type = VAR_FLOAT;
2582 if (get_float_arg(argvars, &f) == OK)
2583 rettv->vval.v_float = cos(f);
2584 else
2585 rettv->vval.v_float = 0.0;
2586}
2587
2588/*
2589 * "cosh()" function
2590 */
2591 static void
2592f_cosh(typval_T *argvars, typval_T *rettv)
2593{
2594 float_T f = 0.0;
2595
2596 rettv->v_type = VAR_FLOAT;
2597 if (get_float_arg(argvars, &f) == OK)
2598 rettv->vval.v_float = cosh(f);
2599 else
2600 rettv->vval.v_float = 0.0;
2601}
2602#endif
2603
2604/*
2605 * "count()" function
2606 */
2607 static void
2608f_count(typval_T *argvars, typval_T *rettv)
2609{
2610 long n = 0;
2611 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002612 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002613
Bram Moolenaar9966b212017-07-28 16:46:57 +02002614 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002615 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002616
2617 if (argvars[0].v_type == VAR_STRING)
2618 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002619 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002620 char_u *p = argvars[0].vval.v_string;
2621 char_u *next;
2622
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002623 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002624 {
2625 if (ic)
2626 {
2627 size_t len = STRLEN(expr);
2628
2629 while (*p != NUL)
2630 {
2631 if (MB_STRNICMP(p, expr, len) == 0)
2632 {
2633 ++n;
2634 p += len;
2635 }
2636 else
2637 MB_PTR_ADV(p);
2638 }
2639 }
2640 else
2641 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2642 != NULL)
2643 {
2644 ++n;
2645 p = next + STRLEN(expr);
2646 }
2647 }
2648
2649 }
2650 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002651 {
2652 listitem_T *li;
2653 list_T *l;
2654 long idx;
2655
2656 if ((l = argvars[0].vval.v_list) != NULL)
2657 {
2658 li = l->lv_first;
2659 if (argvars[2].v_type != VAR_UNKNOWN)
2660 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002661 if (argvars[3].v_type != VAR_UNKNOWN)
2662 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002663 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002664 if (!error)
2665 {
2666 li = list_find(l, idx);
2667 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002668 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002669 }
2670 }
2671 if (error)
2672 li = NULL;
2673 }
2674
2675 for ( ; li != NULL; li = li->li_next)
2676 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2677 ++n;
2678 }
2679 }
2680 else if (argvars[0].v_type == VAR_DICT)
2681 {
2682 int todo;
2683 dict_T *d;
2684 hashitem_T *hi;
2685
2686 if ((d = argvars[0].vval.v_dict) != NULL)
2687 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 if (argvars[2].v_type != VAR_UNKNOWN)
2689 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002690 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002691 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002692 }
2693
2694 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2695 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2696 {
2697 if (!HASHITEM_EMPTY(hi))
2698 {
2699 --todo;
2700 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2701 ++n;
2702 }
2703 }
2704 }
2705 }
2706 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002707 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002708 rettv->vval.v_number = n;
2709}
2710
2711/*
2712 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2713 *
2714 * Checks the existence of a cscope connection.
2715 */
2716 static void
2717f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2718{
2719#ifdef FEAT_CSCOPE
2720 int num = 0;
2721 char_u *dbpath = NULL;
2722 char_u *prepend = NULL;
2723 char_u buf[NUMBUFLEN];
2724
2725 if (argvars[0].v_type != VAR_UNKNOWN
2726 && argvars[1].v_type != VAR_UNKNOWN)
2727 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002728 num = (int)tv_get_number(&argvars[0]);
2729 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002730 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002731 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002732 }
2733
2734 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2735#endif
2736}
2737
2738/*
2739 * "cursor(lnum, col)" function, or
2740 * "cursor(list)"
2741 *
2742 * Moves the cursor to the specified line and column.
2743 * Returns 0 when the position could be set, -1 otherwise.
2744 */
2745 static void
2746f_cursor(typval_T *argvars, typval_T *rettv)
2747{
2748 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002749 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002750 int set_curswant = TRUE;
2751
2752 rettv->vval.v_number = -1;
2753 if (argvars[1].v_type == VAR_UNKNOWN)
2754 {
2755 pos_T pos;
2756 colnr_T curswant = -1;
2757
2758 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2759 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002760 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002761 return;
2762 }
2763 line = pos.lnum;
2764 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002765 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766 if (curswant >= 0)
2767 {
2768 curwin->w_curswant = curswant - 1;
2769 set_curswant = FALSE;
2770 }
2771 }
2772 else
2773 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002774 line = tv_get_lnum(argvars);
2775 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002776 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002777 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002779 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002780 return; /* type error; errmsg already given */
2781 if (line > 0)
2782 curwin->w_cursor.lnum = line;
2783 if (col > 0)
2784 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002786
2787 /* Make sure the cursor is in a valid position. */
2788 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002789 /* Correct cursor for multi-byte character. */
2790 if (has_mbyte)
2791 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002792
2793 curwin->w_set_curswant = set_curswant;
2794 rettv->vval.v_number = 0;
2795}
2796
Bram Moolenaar4f974752019-02-17 17:44:42 +01002797#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002798/*
2799 * "debugbreak()" function
2800 */
2801 static void
2802f_debugbreak(typval_T *argvars, typval_T *rettv)
2803{
2804 int pid;
2805
2806 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002807 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002808 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002809 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002810 else
2811 {
2812 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2813
2814 if (hProcess != NULL)
2815 {
2816 DebugBreakProcess(hProcess);
2817 CloseHandle(hProcess);
2818 rettv->vval.v_number = OK;
2819 }
2820 }
2821}
2822#endif
2823
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002824/*
2825 * "deepcopy()" function
2826 */
2827 static void
2828f_deepcopy(typval_T *argvars, typval_T *rettv)
2829{
2830 int noref = 0;
2831 int copyID;
2832
2833 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002834 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002835 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002836 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002837 else
2838 {
2839 copyID = get_copyID();
2840 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2841 }
2842}
2843
2844/*
2845 * "delete()" function
2846 */
2847 static void
2848f_delete(typval_T *argvars, typval_T *rettv)
2849{
2850 char_u nbuf[NUMBUFLEN];
2851 char_u *name;
2852 char_u *flags;
2853
2854 rettv->vval.v_number = -1;
2855 if (check_restricted() || check_secure())
2856 return;
2857
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002858 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002859 if (name == NULL || *name == NUL)
2860 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002861 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002862 return;
2863 }
2864
2865 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002866 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002867 else
2868 flags = (char_u *)"";
2869
2870 if (*flags == NUL)
2871 /* delete a file */
2872 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2873 else if (STRCMP(flags, "d") == 0)
2874 /* delete an empty directory */
2875 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2876 else if (STRCMP(flags, "rf") == 0)
2877 /* delete a directory recursively */
2878 rettv->vval.v_number = delete_recursive(name);
2879 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002880 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002881}
2882
2883/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002884 * "deletebufline()" function
2885 */
2886 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002887f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002888{
2889 buf_T *buf;
2890 linenr_T first, last;
2891 linenr_T lnum;
2892 long count;
2893 int is_curbuf;
2894 buf_T *curbuf_save = NULL;
2895 win_T *curwin_save = NULL;
2896 tabpage_T *tp;
2897 win_T *wp;
2898
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002899 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002900 if (buf == NULL)
2901 {
2902 rettv->vval.v_number = 1; /* FAIL */
2903 return;
2904 }
2905 is_curbuf = buf == curbuf;
2906
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002907 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002908 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002909 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002910 else
2911 last = first;
2912
2913 if (buf->b_ml.ml_mfp == NULL || first < 1
2914 || first > buf->b_ml.ml_line_count || last < first)
2915 {
2916 rettv->vval.v_number = 1; /* FAIL */
2917 return;
2918 }
2919
2920 if (!is_curbuf)
2921 {
2922 curbuf_save = curbuf;
2923 curwin_save = curwin;
2924 curbuf = buf;
2925 find_win_for_curbuf();
2926 }
2927 if (last > curbuf->b_ml.ml_line_count)
2928 last = curbuf->b_ml.ml_line_count;
2929 count = last - first + 1;
2930
2931 // When coming here from Insert mode, sync undo, so that this can be
2932 // undone separately from what was previously inserted.
2933 if (u_sync_once == 2)
2934 {
2935 u_sync_once = 1; // notify that u_sync() was called
2936 u_sync(TRUE);
2937 }
2938
2939 if (u_save(first - 1, last + 1) == FAIL)
2940 {
2941 rettv->vval.v_number = 1; /* FAIL */
2942 return;
2943 }
2944
2945 for (lnum = first; lnum <= last; ++lnum)
2946 ml_delete(first, TRUE);
2947
2948 FOR_ALL_TAB_WINDOWS(tp, wp)
2949 if (wp->w_buffer == buf)
2950 {
2951 if (wp->w_cursor.lnum > last)
2952 wp->w_cursor.lnum -= count;
2953 else if (wp->w_cursor.lnum> first)
2954 wp->w_cursor.lnum = first;
2955 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2956 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2957 }
2958 check_cursor_col();
2959 deleted_lines_mark(first, count);
2960
2961 if (!is_curbuf)
2962 {
2963 curbuf = curbuf_save;
2964 curwin = curwin_save;
2965 }
2966}
2967
2968/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002969 * "did_filetype()" function
2970 */
2971 static void
2972f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2973{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002974 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002975}
2976
2977/*
2978 * "diff_filler()" function
2979 */
2980 static void
2981f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2982{
2983#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002984 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002985#endif
2986}
2987
2988/*
2989 * "diff_hlID()" function
2990 */
2991 static void
2992f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2993{
2994#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002995 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002996 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002997 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002998 static int fnum = 0;
2999 static int change_start = 0;
3000 static int change_end = 0;
3001 static hlf_T hlID = (hlf_T)0;
3002 int filler_lines;
3003 int col;
3004
3005 if (lnum < 0) /* ignore type error in {lnum} arg */
3006 lnum = 0;
3007 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003008 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009 || fnum != curbuf->b_fnum)
3010 {
3011 /* New line, buffer, change: need to get the values. */
3012 filler_lines = diff_check(curwin, lnum);
3013 if (filler_lines < 0)
3014 {
3015 if (filler_lines == -1)
3016 {
3017 change_start = MAXCOL;
3018 change_end = -1;
3019 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3020 hlID = HLF_ADD; /* added line */
3021 else
3022 hlID = HLF_CHD; /* changed line */
3023 }
3024 else
3025 hlID = HLF_ADD; /* added line */
3026 }
3027 else
3028 hlID = (hlf_T)0;
3029 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003030 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031 fnum = curbuf->b_fnum;
3032 }
3033
3034 if (hlID == HLF_CHD || hlID == HLF_TXD)
3035 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003036 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003037 if (col >= change_start && col <= change_end)
3038 hlID = HLF_TXD; /* changed text */
3039 else
3040 hlID = HLF_CHD; /* changed line */
3041 }
3042 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3043#endif
3044}
3045
3046/*
3047 * "empty({expr})" function
3048 */
3049 static void
3050f_empty(typval_T *argvars, typval_T *rettv)
3051{
3052 int n = FALSE;
3053
3054 switch (argvars[0].v_type)
3055 {
3056 case VAR_STRING:
3057 case VAR_FUNC:
3058 n = argvars[0].vval.v_string == NULL
3059 || *argvars[0].vval.v_string == NUL;
3060 break;
3061 case VAR_PARTIAL:
3062 n = FALSE;
3063 break;
3064 case VAR_NUMBER:
3065 n = argvars[0].vval.v_number == 0;
3066 break;
3067 case VAR_FLOAT:
3068#ifdef FEAT_FLOAT
3069 n = argvars[0].vval.v_float == 0.0;
3070 break;
3071#endif
3072 case VAR_LIST:
3073 n = argvars[0].vval.v_list == NULL
3074 || argvars[0].vval.v_list->lv_first == NULL;
3075 break;
3076 case VAR_DICT:
3077 n = argvars[0].vval.v_dict == NULL
3078 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3079 break;
3080 case VAR_SPECIAL:
3081 n = argvars[0].vval.v_number != VVAL_TRUE;
3082 break;
3083
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003084 case VAR_BLOB:
3085 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003086 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3087 break;
3088
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003089 case VAR_JOB:
3090#ifdef FEAT_JOB_CHANNEL
3091 n = argvars[0].vval.v_job == NULL
3092 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3093 break;
3094#endif
3095 case VAR_CHANNEL:
3096#ifdef FEAT_JOB_CHANNEL
3097 n = argvars[0].vval.v_channel == NULL
3098 || !channel_is_open(argvars[0].vval.v_channel);
3099 break;
3100#endif
3101 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003102 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003103 n = TRUE;
3104 break;
3105 }
3106
3107 rettv->vval.v_number = n;
3108}
3109
3110/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003111 * "environ()" function
3112 */
3113 static void
3114f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3115{
3116#if !defined(AMIGA)
3117 int i = 0;
3118 char_u *entry, *value;
3119# ifdef MSWIN
3120 extern wchar_t **_wenviron;
3121# else
3122 extern char **environ;
3123# endif
3124
3125 if (rettv_dict_alloc(rettv) != OK)
3126 return;
3127
3128# ifdef MSWIN
3129 if (*_wenviron == NULL)
3130 return;
3131# else
3132 if (*environ == NULL)
3133 return;
3134# endif
3135
3136 for (i = 0; ; ++i)
3137 {
3138# ifdef MSWIN
3139 short_u *p;
3140
3141 if ((p = (short_u *)_wenviron[i]) == NULL)
3142 return;
3143 entry = utf16_to_enc(p, NULL);
3144# else
3145 if ((entry = (char_u *)environ[i]) == NULL)
3146 return;
3147 entry = vim_strsave(entry);
3148# endif
3149 if (entry == NULL) // out of memory
3150 return;
3151 if ((value = vim_strchr(entry, '=')) == NULL)
3152 {
3153 vim_free(entry);
3154 continue;
3155 }
3156 *value++ = NUL;
3157 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3158 vim_free(entry);
3159 }
3160#endif
3161}
3162
3163/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003164 * "escape({string}, {chars})" function
3165 */
3166 static void
3167f_escape(typval_T *argvars, typval_T *rettv)
3168{
3169 char_u buf[NUMBUFLEN];
3170
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003171 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3172 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003173 rettv->v_type = VAR_STRING;
3174}
3175
3176/*
3177 * "eval()" function
3178 */
3179 static void
3180f_eval(typval_T *argvars, typval_T *rettv)
3181{
3182 char_u *s, *p;
3183
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003184 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003185 if (s != NULL)
3186 s = skipwhite(s);
3187
3188 p = s;
3189 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3190 {
3191 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003192 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003193 need_clr_eos = FALSE;
3194 rettv->v_type = VAR_NUMBER;
3195 rettv->vval.v_number = 0;
3196 }
3197 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003198 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003199}
3200
3201/*
3202 * "eventhandler()" function
3203 */
3204 static void
3205f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3206{
3207 rettv->vval.v_number = vgetc_busy;
3208}
3209
3210/*
3211 * "executable()" function
3212 */
3213 static void
3214f_executable(typval_T *argvars, typval_T *rettv)
3215{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003216 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003217
3218 /* Check in $PATH and also check directly if there is a directory name. */
3219 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3220 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3221}
3222
3223static garray_T redir_execute_ga;
3224
3225/*
3226 * Append "value[value_len]" to the execute() output.
3227 */
3228 void
3229execute_redir_str(char_u *value, int value_len)
3230{
3231 int len;
3232
3233 if (value_len == -1)
3234 len = (int)STRLEN(value); /* Append the entire string */
3235 else
3236 len = value_len; /* Append only "value_len" characters */
3237 if (ga_grow(&redir_execute_ga, len) == OK)
3238 {
3239 mch_memmove((char *)redir_execute_ga.ga_data
3240 + redir_execute_ga.ga_len, value, len);
3241 redir_execute_ga.ga_len += len;
3242 }
3243}
3244
3245/*
3246 * Get next line from a list.
3247 * Called by do_cmdline() to get the next line.
3248 * Returns allocated string, or NULL for end of function.
3249 */
3250
3251 static char_u *
3252get_list_line(
3253 int c UNUSED,
3254 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003255 int indent UNUSED,
3256 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003257{
3258 listitem_T **p = (listitem_T **)cookie;
3259 listitem_T *item = *p;
3260 char_u buf[NUMBUFLEN];
3261 char_u *s;
3262
3263 if (item == NULL)
3264 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003265 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003266 *p = item->li_next;
3267 return s == NULL ? NULL : vim_strsave(s);
3268}
3269
3270/*
3271 * "execute()" function
3272 */
3273 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003274execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003275{
3276 char_u *cmd = NULL;
3277 list_T *list = NULL;
3278 int save_msg_silent = msg_silent;
3279 int save_emsg_silent = emsg_silent;
3280 int save_emsg_noredir = emsg_noredir;
3281 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003282 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003283 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003284 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003285 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003286
3287 rettv->vval.v_string = NULL;
3288 rettv->v_type = VAR_STRING;
3289
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003290 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003291 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003292 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003293 if (list == NULL || list->lv_first == NULL)
3294 /* empty list, no commands, empty output */
3295 return;
3296 ++list->lv_refcount;
3297 }
3298 else
3299 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003300 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003301 if (cmd == NULL)
3302 return;
3303 }
3304
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003305 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003306 {
3307 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003308 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003309
3310 if (s == NULL)
3311 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003312 if (*s == NUL)
3313 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003314 if (STRNCMP(s, "silent", 6) == 0)
3315 ++msg_silent;
3316 if (STRCMP(s, "silent!") == 0)
3317 {
3318 emsg_silent = TRUE;
3319 emsg_noredir = TRUE;
3320 }
3321 }
3322 else
3323 ++msg_silent;
3324
3325 if (redir_execute)
3326 save_ga = redir_execute_ga;
3327 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3328 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003329 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003330 if (!echo_output)
3331 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003332
3333 if (cmd != NULL)
3334 do_cmdline_cmd(cmd);
3335 else
3336 {
3337 listitem_T *item = list->lv_first;
3338
3339 do_cmdline(NULL, get_list_line, (void *)&item,
3340 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3341 --list->lv_refcount;
3342 }
3343
Bram Moolenaard297f352017-01-29 20:31:21 +01003344 /* Need to append a NUL to the result. */
3345 if (ga_grow(&redir_execute_ga, 1) == OK)
3346 {
3347 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3348 rettv->vval.v_string = redir_execute_ga.ga_data;
3349 }
3350 else
3351 {
3352 ga_clear(&redir_execute_ga);
3353 rettv->vval.v_string = NULL;
3354 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003355 msg_silent = save_msg_silent;
3356 emsg_silent = save_emsg_silent;
3357 emsg_noredir = save_emsg_noredir;
3358
3359 redir_execute = save_redir_execute;
3360 if (redir_execute)
3361 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003362 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003363
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003364 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003365 if (echo_output)
3366 // When not working silently: put it in column zero. A following
3367 // "echon" will overwrite the message, unavoidably.
3368 msg_col = 0;
3369 else
3370 // When working silently: Put it back where it was, since nothing
3371 // should have been written.
3372 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003373}
3374
3375/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003376 * "execute()" function
3377 */
3378 static void
3379f_execute(typval_T *argvars, typval_T *rettv)
3380{
3381 execute_common(argvars, rettv, 0);
3382}
3383
3384/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003385 * "exepath()" function
3386 */
3387 static void
3388f_exepath(typval_T *argvars, typval_T *rettv)
3389{
3390 char_u *p = NULL;
3391
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003392 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003393 rettv->v_type = VAR_STRING;
3394 rettv->vval.v_string = p;
3395}
3396
3397/*
3398 * "exists()" function
3399 */
3400 static void
3401f_exists(typval_T *argvars, typval_T *rettv)
3402{
3403 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003404 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003406 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003407 if (*p == '$') /* environment variable */
3408 {
3409 /* first try "normal" environment variables (fast) */
3410 if (mch_getenv(p + 1) != NULL)
3411 n = TRUE;
3412 else
3413 {
3414 /* try expanding things like $VIM and ${HOME} */
3415 p = expand_env_save(p);
3416 if (p != NULL && *p != '$')
3417 n = TRUE;
3418 vim_free(p);
3419 }
3420 }
3421 else if (*p == '&' || *p == '+') /* option */
3422 {
3423 n = (get_option_tv(&p, NULL, TRUE) == OK);
3424 if (*skipwhite(p) != NUL)
3425 n = FALSE; /* trailing garbage */
3426 }
3427 else if (*p == '*') /* internal or user defined function */
3428 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003429 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003430 }
3431 else if (*p == ':')
3432 {
3433 n = cmd_exists(p + 1);
3434 }
3435 else if (*p == '#')
3436 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003437 if (p[1] == '#')
3438 n = autocmd_supported(p + 2);
3439 else
3440 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441 }
3442 else /* internal variable */
3443 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003444 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003445 }
3446
3447 rettv->vval.v_number = n;
3448}
3449
3450#ifdef FEAT_FLOAT
3451/*
3452 * "exp()" function
3453 */
3454 static void
3455f_exp(typval_T *argvars, typval_T *rettv)
3456{
3457 float_T f = 0.0;
3458
3459 rettv->v_type = VAR_FLOAT;
3460 if (get_float_arg(argvars, &f) == OK)
3461 rettv->vval.v_float = exp(f);
3462 else
3463 rettv->vval.v_float = 0.0;
3464}
3465#endif
3466
3467/*
3468 * "expand()" function
3469 */
3470 static void
3471f_expand(typval_T *argvars, typval_T *rettv)
3472{
3473 char_u *s;
3474 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003475 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003476 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3477 expand_T xpc;
3478 int error = FALSE;
3479 char_u *result;
3480
3481 rettv->v_type = VAR_STRING;
3482 if (argvars[1].v_type != VAR_UNKNOWN
3483 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003484 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003485 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003486 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003487
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003488 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003489 if (*s == '%' || *s == '#' || *s == '<')
3490 {
3491 ++emsg_off;
3492 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3493 --emsg_off;
3494 if (rettv->v_type == VAR_LIST)
3495 {
3496 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3497 list_append_string(rettv->vval.v_list, result, -1);
3498 else
3499 vim_free(result);
3500 }
3501 else
3502 rettv->vval.v_string = result;
3503 }
3504 else
3505 {
3506 /* When the optional second argument is non-zero, don't remove matches
3507 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3508 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003509 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003510 options |= WILD_KEEP_ALL;
3511 if (!error)
3512 {
3513 ExpandInit(&xpc);
3514 xpc.xp_context = EXPAND_FILES;
3515 if (p_wic)
3516 options += WILD_ICASE;
3517 if (rettv->v_type == VAR_STRING)
3518 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3519 options, WILD_ALL);
3520 else if (rettv_list_alloc(rettv) != FAIL)
3521 {
3522 int i;
3523
3524 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3525 for (i = 0; i < xpc.xp_numfiles; i++)
3526 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3527 ExpandCleanup(&xpc);
3528 }
3529 }
3530 else
3531 rettv->vval.v_string = NULL;
3532 }
3533}
3534
3535/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003536 * "expandcmd()" function
3537 * Expand all the special characters in a command string.
3538 */
3539 static void
3540f_expandcmd(typval_T *argvars, typval_T *rettv)
3541{
3542 exarg_T eap;
3543 char_u *cmdstr;
3544 char *errormsg = NULL;
3545
3546 rettv->v_type = VAR_STRING;
3547 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3548
3549 memset(&eap, 0, sizeof(eap));
3550 eap.cmd = cmdstr;
3551 eap.arg = cmdstr;
3552 eap.argt |= NOSPC;
3553 eap.usefilter = FALSE;
3554 eap.nextcmd = NULL;
3555 eap.cmdidx = CMD_USER;
3556
3557 expand_filename(&eap, &cmdstr, &errormsg);
3558 if (errormsg != NULL && *errormsg != NUL)
3559 emsg(errormsg);
3560
3561 rettv->vval.v_string = cmdstr;
3562}
3563
3564/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003565 * "extend(list, list [, idx])" function
3566 * "extend(dict, dict [, action])" function
3567 */
3568 static void
3569f_extend(typval_T *argvars, typval_T *rettv)
3570{
3571 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3572
3573 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3574 {
3575 list_T *l1, *l2;
3576 listitem_T *item;
3577 long before;
3578 int error = FALSE;
3579
3580 l1 = argvars[0].vval.v_list;
3581 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003582 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003583 && l2 != NULL)
3584 {
3585 if (argvars[2].v_type != VAR_UNKNOWN)
3586 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003587 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003588 if (error)
3589 return; /* type error; errmsg already given */
3590
3591 if (before == l1->lv_len)
3592 item = NULL;
3593 else
3594 {
3595 item = list_find(l1, before);
3596 if (item == NULL)
3597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003598 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003599 return;
3600 }
3601 }
3602 }
3603 else
3604 item = NULL;
3605 list_extend(l1, l2, item);
3606
3607 copy_tv(&argvars[0], rettv);
3608 }
3609 }
3610 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3611 {
3612 dict_T *d1, *d2;
3613 char_u *action;
3614 int i;
3615
3616 d1 = argvars[0].vval.v_dict;
3617 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003618 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003619 && d2 != NULL)
3620 {
3621 /* Check the third argument. */
3622 if (argvars[2].v_type != VAR_UNKNOWN)
3623 {
3624 static char *(av[]) = {"keep", "force", "error"};
3625
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003626 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003627 if (action == NULL)
3628 return; /* type error; errmsg already given */
3629 for (i = 0; i < 3; ++i)
3630 if (STRCMP(action, av[i]) == 0)
3631 break;
3632 if (i == 3)
3633 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003634 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003635 return;
3636 }
3637 }
3638 else
3639 action = (char_u *)"force";
3640
3641 dict_extend(d1, d2, action);
3642
3643 copy_tv(&argvars[0], rettv);
3644 }
3645 }
3646 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003647 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003648}
3649
3650/*
3651 * "feedkeys()" function
3652 */
3653 static void
3654f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3655{
3656 int remap = TRUE;
3657 int insert = FALSE;
3658 char_u *keys, *flags;
3659 char_u nbuf[NUMBUFLEN];
3660 int typed = FALSE;
3661 int execute = FALSE;
3662 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003663 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003664 char_u *keys_esc;
3665
3666 /* This is not allowed in the sandbox. If the commands would still be
3667 * executed in the sandbox it would be OK, but it probably happens later,
3668 * when "sandbox" is no longer set. */
3669 if (check_secure())
3670 return;
3671
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003672 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003673
3674 if (argvars[1].v_type != VAR_UNKNOWN)
3675 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003676 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003677 for ( ; *flags != NUL; ++flags)
3678 {
3679 switch (*flags)
3680 {
3681 case 'n': remap = FALSE; break;
3682 case 'm': remap = TRUE; break;
3683 case 't': typed = TRUE; break;
3684 case 'i': insert = TRUE; break;
3685 case 'x': execute = TRUE; break;
3686 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003687 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688 }
3689 }
3690 }
3691
3692 if (*keys != NUL || execute)
3693 {
3694 /* Need to escape K_SPECIAL and CSI before putting the string in the
3695 * typeahead buffer. */
3696 keys_esc = vim_strsave_escape_csi(keys);
3697 if (keys_esc != NULL)
3698 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003699 if (lowlevel)
3700 {
3701#ifdef USE_INPUT_BUF
3702 add_to_input_buf(keys, (int)STRLEN(keys));
3703#else
3704 emsg(_("E980: lowlevel input not supported"));
3705#endif
3706 }
3707 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003708 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003709 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003710 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003711 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003712#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003713 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003714#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003715 )
3716 typebuf_was_filled = TRUE;
3717 }
3718 vim_free(keys_esc);
3719
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003720 if (execute)
3721 {
3722 int save_msg_scroll = msg_scroll;
3723
3724 /* Avoid a 1 second delay when the keys start Insert mode. */
3725 msg_scroll = FALSE;
3726
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003727 if (!dangerous)
3728 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003729 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003730 if (!dangerous)
3731 --ex_normal_busy;
3732
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003733 msg_scroll |= save_msg_scroll;
3734 }
3735 }
3736 }
3737}
3738
3739/*
3740 * "filereadable()" function
3741 */
3742 static void
3743f_filereadable(typval_T *argvars, typval_T *rettv)
3744{
3745 int fd;
3746 char_u *p;
3747 int n;
3748
3749#ifndef O_NONBLOCK
3750# define O_NONBLOCK 0
3751#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003752 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003753 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3754 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3755 {
3756 n = TRUE;
3757 close(fd);
3758 }
3759 else
3760 n = FALSE;
3761
3762 rettv->vval.v_number = n;
3763}
3764
3765/*
3766 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3767 * rights to write into.
3768 */
3769 static void
3770f_filewritable(typval_T *argvars, typval_T *rettv)
3771{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003772 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003773}
3774
3775 static void
3776findfilendir(
3777 typval_T *argvars UNUSED,
3778 typval_T *rettv,
3779 int find_what UNUSED)
3780{
3781#ifdef FEAT_SEARCHPATH
3782 char_u *fname;
3783 char_u *fresult = NULL;
3784 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3785 char_u *p;
3786 char_u pathbuf[NUMBUFLEN];
3787 int count = 1;
3788 int first = TRUE;
3789 int error = FALSE;
3790#endif
3791
3792 rettv->vval.v_string = NULL;
3793 rettv->v_type = VAR_STRING;
3794
3795#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003796 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003797
3798 if (argvars[1].v_type != VAR_UNKNOWN)
3799 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003800 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003801 if (p == NULL)
3802 error = TRUE;
3803 else
3804 {
3805 if (*p != NUL)
3806 path = p;
3807
3808 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003809 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003810 }
3811 }
3812
3813 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3814 error = TRUE;
3815
3816 if (*fname != NUL && !error)
3817 {
3818 do
3819 {
3820 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3821 vim_free(fresult);
3822 fresult = find_file_in_path_option(first ? fname : NULL,
3823 first ? (int)STRLEN(fname) : 0,
3824 0, first, path,
3825 find_what,
3826 curbuf->b_ffname,
3827 find_what == FINDFILE_DIR
3828 ? (char_u *)"" : curbuf->b_p_sua);
3829 first = FALSE;
3830
3831 if (fresult != NULL && rettv->v_type == VAR_LIST)
3832 list_append_string(rettv->vval.v_list, fresult, -1);
3833
3834 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3835 }
3836
3837 if (rettv->v_type == VAR_STRING)
3838 rettv->vval.v_string = fresult;
3839#endif
3840}
3841
3842/*
3843 * "filter()" function
3844 */
3845 static void
3846f_filter(typval_T *argvars, typval_T *rettv)
3847{
3848 filter_map(argvars, rettv, FALSE);
3849}
3850
3851/*
3852 * "finddir({fname}[, {path}[, {count}]])" function
3853 */
3854 static void
3855f_finddir(typval_T *argvars, typval_T *rettv)
3856{
3857 findfilendir(argvars, rettv, FINDFILE_DIR);
3858}
3859
3860/*
3861 * "findfile({fname}[, {path}[, {count}]])" function
3862 */
3863 static void
3864f_findfile(typval_T *argvars, typval_T *rettv)
3865{
3866 findfilendir(argvars, rettv, FINDFILE_FILE);
3867}
3868
3869#ifdef FEAT_FLOAT
3870/*
3871 * "float2nr({float})" function
3872 */
3873 static void
3874f_float2nr(typval_T *argvars, typval_T *rettv)
3875{
3876 float_T f = 0.0;
3877
3878 if (get_float_arg(argvars, &f) == OK)
3879 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003880 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003881 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003882 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003883 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003884 else
3885 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003886 }
3887}
3888
3889/*
3890 * "floor({float})" function
3891 */
3892 static void
3893f_floor(typval_T *argvars, typval_T *rettv)
3894{
3895 float_T f = 0.0;
3896
3897 rettv->v_type = VAR_FLOAT;
3898 if (get_float_arg(argvars, &f) == OK)
3899 rettv->vval.v_float = floor(f);
3900 else
3901 rettv->vval.v_float = 0.0;
3902}
3903
3904/*
3905 * "fmod()" function
3906 */
3907 static void
3908f_fmod(typval_T *argvars, typval_T *rettv)
3909{
3910 float_T fx = 0.0, fy = 0.0;
3911
3912 rettv->v_type = VAR_FLOAT;
3913 if (get_float_arg(argvars, &fx) == OK
3914 && get_float_arg(&argvars[1], &fy) == OK)
3915 rettv->vval.v_float = fmod(fx, fy);
3916 else
3917 rettv->vval.v_float = 0.0;
3918}
3919#endif
3920
3921/*
3922 * "fnameescape({string})" function
3923 */
3924 static void
3925f_fnameescape(typval_T *argvars, typval_T *rettv)
3926{
3927 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003928 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003929 rettv->v_type = VAR_STRING;
3930}
3931
3932/*
3933 * "fnamemodify({fname}, {mods})" function
3934 */
3935 static void
3936f_fnamemodify(typval_T *argvars, typval_T *rettv)
3937{
3938 char_u *fname;
3939 char_u *mods;
3940 int usedlen = 0;
3941 int len;
3942 char_u *fbuf = NULL;
3943 char_u buf[NUMBUFLEN];
3944
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003945 fname = tv_get_string_chk(&argvars[0]);
3946 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003947 if (fname == NULL || mods == NULL)
3948 fname = NULL;
3949 else
3950 {
3951 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003952 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003953 }
3954
3955 rettv->v_type = VAR_STRING;
3956 if (fname == NULL)
3957 rettv->vval.v_string = NULL;
3958 else
3959 rettv->vval.v_string = vim_strnsave(fname, len);
3960 vim_free(fbuf);
3961}
3962
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003963/*
3964 * "foldclosed()" function
3965 */
3966 static void
3967foldclosed_both(
3968 typval_T *argvars UNUSED,
3969 typval_T *rettv,
3970 int end UNUSED)
3971{
3972#ifdef FEAT_FOLDING
3973 linenr_T lnum;
3974 linenr_T first, last;
3975
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003976 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003977 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3978 {
3979 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3980 {
3981 if (end)
3982 rettv->vval.v_number = (varnumber_T)last;
3983 else
3984 rettv->vval.v_number = (varnumber_T)first;
3985 return;
3986 }
3987 }
3988#endif
3989 rettv->vval.v_number = -1;
3990}
3991
3992/*
3993 * "foldclosed()" function
3994 */
3995 static void
3996f_foldclosed(typval_T *argvars, typval_T *rettv)
3997{
3998 foldclosed_both(argvars, rettv, FALSE);
3999}
4000
4001/*
4002 * "foldclosedend()" function
4003 */
4004 static void
4005f_foldclosedend(typval_T *argvars, typval_T *rettv)
4006{
4007 foldclosed_both(argvars, rettv, TRUE);
4008}
4009
4010/*
4011 * "foldlevel()" function
4012 */
4013 static void
4014f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4015{
4016#ifdef FEAT_FOLDING
4017 linenr_T lnum;
4018
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004019 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004020 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4021 rettv->vval.v_number = foldLevel(lnum);
4022#endif
4023}
4024
4025/*
4026 * "foldtext()" function
4027 */
4028 static void
4029f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4030{
4031#ifdef FEAT_FOLDING
4032 linenr_T foldstart;
4033 linenr_T foldend;
4034 char_u *dashes;
4035 linenr_T lnum;
4036 char_u *s;
4037 char_u *r;
4038 int len;
4039 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004040 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004041#endif
4042
4043 rettv->v_type = VAR_STRING;
4044 rettv->vval.v_string = NULL;
4045#ifdef FEAT_FOLDING
4046 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4047 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4048 dashes = get_vim_var_str(VV_FOLDDASHES);
4049 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4050 && dashes != NULL)
4051 {
4052 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004053 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004054 if (!linewhite(lnum))
4055 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004056
4057 /* Find interesting text in this line. */
4058 s = skipwhite(ml_get(lnum));
4059 /* skip C comment-start */
4060 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4061 {
4062 s = skipwhite(s + 2);
4063 if (*skipwhite(s) == NUL
4064 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4065 {
4066 s = skipwhite(ml_get(lnum + 1));
4067 if (*s == '*')
4068 s = skipwhite(s + 1);
4069 }
4070 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004071 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004072 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004073 r = alloc(STRLEN(txt)
4074 + STRLEN(dashes) // for %s
4075 + 20 // for %3ld
4076 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004077 if (r != NULL)
4078 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004079 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004080 len = (int)STRLEN(r);
4081 STRCAT(r, s);
4082 /* remove 'foldmarker' and 'commentstring' */
4083 foldtext_cleanup(r + len);
4084 rettv->vval.v_string = r;
4085 }
4086 }
4087#endif
4088}
4089
4090/*
4091 * "foldtextresult(lnum)" function
4092 */
4093 static void
4094f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4095{
4096#ifdef FEAT_FOLDING
4097 linenr_T lnum;
4098 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004099 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004100 foldinfo_T foldinfo;
4101 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004102 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004103#endif
4104
4105 rettv->v_type = VAR_STRING;
4106 rettv->vval.v_string = NULL;
4107#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004108 if (entered)
4109 return; /* reject recursive use */
4110 entered = TRUE;
4111
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004112 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004113 /* treat illegal types and illegal string values for {lnum} the same */
4114 if (lnum < 0)
4115 lnum = 0;
4116 fold_count = foldedCount(curwin, lnum, &foldinfo);
4117 if (fold_count > 0)
4118 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004119 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4120 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004121 if (text == buf)
4122 text = vim_strsave(text);
4123 rettv->vval.v_string = text;
4124 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004125
4126 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004127#endif
4128}
4129
4130/*
4131 * "foreground()" function
4132 */
4133 static void
4134f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4135{
4136#ifdef FEAT_GUI
4137 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004138 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004139 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004140 return;
4141 }
4142#endif
4143#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004144 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004145#endif
4146}
4147
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004148 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004149common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004150{
4151 char_u *s;
4152 char_u *name;
4153 int use_string = FALSE;
4154 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004155 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004156
4157 if (argvars[0].v_type == VAR_FUNC)
4158 {
4159 /* function(MyFunc, [arg], dict) */
4160 s = argvars[0].vval.v_string;
4161 }
4162 else if (argvars[0].v_type == VAR_PARTIAL
4163 && argvars[0].vval.v_partial != NULL)
4164 {
4165 /* function(dict.MyFunc, [arg]) */
4166 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004167 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004168 }
4169 else
4170 {
4171 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004172 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004173 use_string = TRUE;
4174 }
4175
Bram Moolenaar843b8842016-08-21 14:36:15 +02004176 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004177 {
4178 name = s;
4179 trans_name = trans_function_name(&name, FALSE,
4180 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4181 if (*name != NUL)
4182 s = NULL;
4183 }
4184
Bram Moolenaar843b8842016-08-21 14:36:15 +02004185 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4186 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004187 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004188 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004189 else if (trans_name != NULL && (is_funcref
4190 ? find_func(trans_name) == NULL
4191 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004192 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004193 else
4194 {
4195 int dict_idx = 0;
4196 int arg_idx = 0;
4197 list_T *list = NULL;
4198
4199 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4200 {
4201 char sid_buf[25];
4202 int off = *s == 's' ? 2 : 5;
4203
4204 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4205 * also be called from another script. Using trans_function_name()
4206 * would also work, but some plugins depend on the name being
4207 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004208 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004209 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004210 if (name != NULL)
4211 {
4212 STRCPY(name, sid_buf);
4213 STRCAT(name, s + off);
4214 }
4215 }
4216 else
4217 name = vim_strsave(s);
4218
4219 if (argvars[1].v_type != VAR_UNKNOWN)
4220 {
4221 if (argvars[2].v_type != VAR_UNKNOWN)
4222 {
4223 /* function(name, [args], dict) */
4224 arg_idx = 1;
4225 dict_idx = 2;
4226 }
4227 else if (argvars[1].v_type == VAR_DICT)
4228 /* function(name, dict) */
4229 dict_idx = 1;
4230 else
4231 /* function(name, [args]) */
4232 arg_idx = 1;
4233 if (dict_idx > 0)
4234 {
4235 if (argvars[dict_idx].v_type != VAR_DICT)
4236 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004237 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004238 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004239 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004240 }
4241 if (argvars[dict_idx].vval.v_dict == NULL)
4242 dict_idx = 0;
4243 }
4244 if (arg_idx > 0)
4245 {
4246 if (argvars[arg_idx].v_type != VAR_LIST)
4247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004248 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004249 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004250 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004251 }
4252 list = argvars[arg_idx].vval.v_list;
4253 if (list == NULL || list->lv_len == 0)
4254 arg_idx = 0;
4255 }
4256 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004257 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004258 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004259 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004260
4261 /* result is a VAR_PARTIAL */
4262 if (pt == NULL)
4263 vim_free(name);
4264 else
4265 {
4266 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4267 {
4268 listitem_T *li;
4269 int i = 0;
4270 int arg_len = 0;
4271 int lv_len = 0;
4272
4273 if (arg_pt != NULL)
4274 arg_len = arg_pt->pt_argc;
4275 if (list != NULL)
4276 lv_len = list->lv_len;
4277 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004278 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004279 if (pt->pt_argv == NULL)
4280 {
4281 vim_free(pt);
4282 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004283 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004284 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004285 for (i = 0; i < arg_len; i++)
4286 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4287 if (lv_len > 0)
4288 for (li = list->lv_first; li != NULL;
4289 li = li->li_next)
4290 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004291 }
4292
4293 /* For "function(dict.func, [], dict)" and "func" is a partial
4294 * use "dict". That is backwards compatible. */
4295 if (dict_idx > 0)
4296 {
4297 /* The dict is bound explicitly, pt_auto is FALSE. */
4298 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4299 ++pt->pt_dict->dv_refcount;
4300 }
4301 else if (arg_pt != NULL)
4302 {
4303 /* If the dict was bound automatically the result is also
4304 * bound automatically. */
4305 pt->pt_dict = arg_pt->pt_dict;
4306 pt->pt_auto = arg_pt->pt_auto;
4307 if (pt->pt_dict != NULL)
4308 ++pt->pt_dict->dv_refcount;
4309 }
4310
4311 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004312 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4313 {
4314 pt->pt_func = arg_pt->pt_func;
4315 func_ptr_ref(pt->pt_func);
4316 vim_free(name);
4317 }
4318 else if (is_funcref)
4319 {
4320 pt->pt_func = find_func(trans_name);
4321 func_ptr_ref(pt->pt_func);
4322 vim_free(name);
4323 }
4324 else
4325 {
4326 pt->pt_name = name;
4327 func_ref(name);
4328 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004329 }
4330 rettv->v_type = VAR_PARTIAL;
4331 rettv->vval.v_partial = pt;
4332 }
4333 else
4334 {
4335 /* result is a VAR_FUNC */
4336 rettv->v_type = VAR_FUNC;
4337 rettv->vval.v_string = name;
4338 func_ref(name);
4339 }
4340 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004341theend:
4342 vim_free(trans_name);
4343}
4344
4345/*
4346 * "funcref()" function
4347 */
4348 static void
4349f_funcref(typval_T *argvars, typval_T *rettv)
4350{
4351 common_function(argvars, rettv, TRUE);
4352}
4353
4354/*
4355 * "function()" function
4356 */
4357 static void
4358f_function(typval_T *argvars, typval_T *rettv)
4359{
4360 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004361}
4362
4363/*
4364 * "garbagecollect()" function
4365 */
4366 static void
4367f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4368{
4369 /* This is postponed until we are back at the toplevel, because we may be
4370 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4371 want_garbage_collect = TRUE;
4372
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004373 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004374 garbage_collect_at_exit = TRUE;
4375}
4376
4377/*
4378 * "get()" function
4379 */
4380 static void
4381f_get(typval_T *argvars, typval_T *rettv)
4382{
4383 listitem_T *li;
4384 list_T *l;
4385 dictitem_T *di;
4386 dict_T *d;
4387 typval_T *tv = NULL;
4388
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004389 if (argvars[0].v_type == VAR_BLOB)
4390 {
4391 int error = FALSE;
4392 int idx = tv_get_number_chk(&argvars[1], &error);
4393
4394 if (!error)
4395 {
4396 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004397 if (idx < 0)
4398 idx = blob_len(argvars[0].vval.v_blob) + idx;
4399 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4400 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004401 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004402 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004403 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004404 tv = rettv;
4405 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004406 }
4407 }
4408 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004409 {
4410 if ((l = argvars[0].vval.v_list) != NULL)
4411 {
4412 int error = FALSE;
4413
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004414 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004415 if (!error && li != NULL)
4416 tv = &li->li_tv;
4417 }
4418 }
4419 else if (argvars[0].v_type == VAR_DICT)
4420 {
4421 if ((d = argvars[0].vval.v_dict) != NULL)
4422 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004423 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004424 if (di != NULL)
4425 tv = &di->di_tv;
4426 }
4427 }
4428 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4429 {
4430 partial_T *pt;
4431 partial_T fref_pt;
4432
4433 if (argvars[0].v_type == VAR_PARTIAL)
4434 pt = argvars[0].vval.v_partial;
4435 else
4436 {
4437 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4438 fref_pt.pt_name = argvars[0].vval.v_string;
4439 pt = &fref_pt;
4440 }
4441
4442 if (pt != NULL)
4443 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004444 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004445 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004446
4447 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4448 {
4449 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004450 n = partial_name(pt);
4451 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004452 rettv->vval.v_string = NULL;
4453 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004454 {
4455 rettv->vval.v_string = vim_strsave(n);
4456 if (rettv->v_type == VAR_FUNC)
4457 func_ref(rettv->vval.v_string);
4458 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004459 }
4460 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004461 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004462 else if (STRCMP(what, "args") == 0)
4463 {
4464 rettv->v_type = VAR_LIST;
4465 if (rettv_list_alloc(rettv) == OK)
4466 {
4467 int i;
4468
4469 for (i = 0; i < pt->pt_argc; ++i)
4470 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4471 }
4472 }
4473 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004474 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004475 return;
4476 }
4477 }
4478 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004479 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004480
4481 if (tv == NULL)
4482 {
4483 if (argvars[2].v_type != VAR_UNKNOWN)
4484 copy_tv(&argvars[2], rettv);
4485 }
4486 else
4487 copy_tv(tv, rettv);
4488}
4489
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004490/*
4491 * Returns buffer options, variables and other attributes in a dictionary.
4492 */
4493 static dict_T *
4494get_buffer_info(buf_T *buf)
4495{
4496 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004497 tabpage_T *tp;
4498 win_T *wp;
4499 list_T *windows;
4500
4501 dict = dict_alloc();
4502 if (dict == NULL)
4503 return NULL;
4504
Bram Moolenaare0be1672018-07-08 16:50:37 +02004505 dict_add_number(dict, "bufnr", buf->b_fnum);
4506 dict_add_string(dict, "name", buf->b_ffname);
4507 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4508 : buflist_findlnum(buf));
4509 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4510 dict_add_number(dict, "listed", buf->b_p_bl);
4511 dict_add_number(dict, "changed", bufIsChanged(buf));
4512 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4513 dict_add_number(dict, "hidden",
4514 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004515
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004516 /* Get a reference to buffer variables */
4517 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004518
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004519 /* List of windows displaying this buffer */
4520 windows = list_alloc();
4521 if (windows != NULL)
4522 {
4523 FOR_ALL_TAB_WINDOWS(tp, wp)
4524 if (wp->w_buffer == buf)
4525 list_append_number(windows, (varnumber_T)wp->w_id);
4526 dict_add_list(dict, "windows", windows);
4527 }
4528
4529#ifdef FEAT_SIGNS
4530 if (buf->b_signlist != NULL)
4531 {
4532 /* List of signs placed in this buffer */
4533 list_T *signs = list_alloc();
4534 if (signs != NULL)
4535 {
4536 get_buffer_signs(buf, signs);
4537 dict_add_list(dict, "signs", signs);
4538 }
4539 }
4540#endif
4541
4542 return dict;
4543}
4544
4545/*
4546 * "getbufinfo()" function
4547 */
4548 static void
4549f_getbufinfo(typval_T *argvars, typval_T *rettv)
4550{
4551 buf_T *buf = NULL;
4552 buf_T *argbuf = NULL;
4553 dict_T *d;
4554 int filtered = FALSE;
4555 int sel_buflisted = FALSE;
4556 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004557 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004558
4559 if (rettv_list_alloc(rettv) != OK)
4560 return;
4561
4562 /* List of all the buffers or selected buffers */
4563 if (argvars[0].v_type == VAR_DICT)
4564 {
4565 dict_T *sel_d = argvars[0].vval.v_dict;
4566
4567 if (sel_d != NULL)
4568 {
4569 dictitem_T *di;
4570
4571 filtered = TRUE;
4572
4573 di = dict_find(sel_d, (char_u *)"buflisted", -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_buflisted = TRUE;
4576
4577 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004578 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004579 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004580
4581 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004582 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004583 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004584 }
4585 }
4586 else if (argvars[0].v_type != VAR_UNKNOWN)
4587 {
4588 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004589 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004590 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004591 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004592 --emsg_off;
4593 if (argbuf == NULL)
4594 return;
4595 }
4596
4597 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004598 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004599 {
4600 if (argbuf != NULL && argbuf != buf)
4601 continue;
4602 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004603 || (sel_buflisted && !buf->b_p_bl)
4604 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004605 continue;
4606
4607 d = get_buffer_info(buf);
4608 if (d != NULL)
4609 list_append_dict(rettv->vval.v_list, d);
4610 if (argbuf != NULL)
4611 return;
4612 }
4613}
4614
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004615/*
4616 * Get line or list of lines from buffer "buf" into "rettv".
4617 * Return a range (from start to end) of lines in rettv from the specified
4618 * buffer.
4619 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4620 */
4621 static void
4622get_buffer_lines(
4623 buf_T *buf,
4624 linenr_T start,
4625 linenr_T end,
4626 int retlist,
4627 typval_T *rettv)
4628{
4629 char_u *p;
4630
4631 rettv->v_type = VAR_STRING;
4632 rettv->vval.v_string = NULL;
4633 if (retlist && rettv_list_alloc(rettv) == FAIL)
4634 return;
4635
4636 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4637 return;
4638
4639 if (!retlist)
4640 {
4641 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4642 p = ml_get_buf(buf, start, FALSE);
4643 else
4644 p = (char_u *)"";
4645 rettv->vval.v_string = vim_strsave(p);
4646 }
4647 else
4648 {
4649 if (end < start)
4650 return;
4651
4652 if (start < 1)
4653 start = 1;
4654 if (end > buf->b_ml.ml_line_count)
4655 end = buf->b_ml.ml_line_count;
4656 while (start <= end)
4657 if (list_append_string(rettv->vval.v_list,
4658 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4659 break;
4660 }
4661}
4662
4663/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004664 * "getbufline()" function
4665 */
4666 static void
4667f_getbufline(typval_T *argvars, typval_T *rettv)
4668{
4669 linenr_T lnum;
4670 linenr_T end;
4671 buf_T *buf;
4672
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004673 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004674 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004675 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004676 --emsg_off;
4677
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004678 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004679 if (argvars[2].v_type == VAR_UNKNOWN)
4680 end = lnum;
4681 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004682 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004683
4684 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4685}
4686
4687/*
4688 * "getbufvar()" function
4689 */
4690 static void
4691f_getbufvar(typval_T *argvars, typval_T *rettv)
4692{
4693 buf_T *buf;
4694 buf_T *save_curbuf;
4695 char_u *varname;
4696 dictitem_T *v;
4697 int done = FALSE;
4698
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004699 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4700 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004701 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004702 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004703
4704 rettv->v_type = VAR_STRING;
4705 rettv->vval.v_string = NULL;
4706
4707 if (buf != NULL && varname != NULL)
4708 {
4709 /* set curbuf to be our buf, temporarily */
4710 save_curbuf = curbuf;
4711 curbuf = buf;
4712
Bram Moolenaar30567352016-08-27 21:25:44 +02004713 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004714 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004715 if (varname[1] == NUL)
4716 {
4717 /* get all buffer-local options in a dict */
4718 dict_T *opts = get_winbuf_options(TRUE);
4719
4720 if (opts != NULL)
4721 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004722 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004723 done = TRUE;
4724 }
4725 }
4726 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4727 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004728 done = TRUE;
4729 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004730 else
4731 {
4732 /* Look up the variable. */
4733 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4734 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4735 'b', varname, FALSE);
4736 if (v != NULL)
4737 {
4738 copy_tv(&v->di_tv, rettv);
4739 done = TRUE;
4740 }
4741 }
4742
4743 /* restore previous notion of curbuf */
4744 curbuf = save_curbuf;
4745 }
4746
4747 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4748 /* use the default value */
4749 copy_tv(&argvars[2], rettv);
4750
4751 --emsg_off;
4752}
4753
4754/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004755 * "getchangelist()" function
4756 */
4757 static void
4758f_getchangelist(typval_T *argvars, typval_T *rettv)
4759{
4760#ifdef FEAT_JUMPLIST
4761 buf_T *buf;
4762 int i;
4763 list_T *l;
4764 dict_T *d;
4765#endif
4766
4767 if (rettv_list_alloc(rettv) != OK)
4768 return;
4769
4770#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004771 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004772 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004773 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004774 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004775 if (buf == NULL)
4776 return;
4777
4778 l = list_alloc();
4779 if (l == NULL)
4780 return;
4781
4782 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4783 return;
4784 /*
4785 * The current window change list index tracks only the position in the
4786 * current buffer change list. For other buffers, use the change list
4787 * length as the current index.
4788 */
4789 list_append_number(rettv->vval.v_list,
4790 (varnumber_T)((buf == curwin->w_buffer)
4791 ? curwin->w_changelistidx : buf->b_changelistlen));
4792
4793 for (i = 0; i < buf->b_changelistlen; ++i)
4794 {
4795 if (buf->b_changelist[i].lnum == 0)
4796 continue;
4797 if ((d = dict_alloc()) == NULL)
4798 return;
4799 if (list_append_dict(l, d) == FAIL)
4800 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004801 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4802 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004803 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004804 }
4805#endif
4806}
4807/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004808 * "getchar()" function
4809 */
4810 static void
4811f_getchar(typval_T *argvars, typval_T *rettv)
4812{
4813 varnumber_T n;
4814 int error = FALSE;
4815
Bram Moolenaar84d93902018-09-11 20:10:20 +02004816#ifdef MESSAGE_QUEUE
4817 // vpeekc() used to check for messages, but that caused problems, invoking
4818 // a callback where it was not expected. Some plugins use getchar(1) in a
4819 // loop to await a message, therefore make sure we check for messages here.
4820 parse_queued_messages();
4821#endif
4822
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004823 /* Position the cursor. Needed after a message that ends in a space. */
4824 windgoto(msg_row, msg_col);
4825
4826 ++no_mapping;
4827 ++allow_keys;
4828 for (;;)
4829 {
4830 if (argvars[0].v_type == VAR_UNKNOWN)
4831 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004832 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004833 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004834 /* getchar(1): only check if char avail */
4835 n = vpeekc_any();
4836 else if (error || vpeekc_any() == NUL)
4837 /* illegal argument or getchar(0) and no char avail: return zero */
4838 n = 0;
4839 else
4840 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004841 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004842
4843 if (n == K_IGNORE)
4844 continue;
4845 break;
4846 }
4847 --no_mapping;
4848 --allow_keys;
4849
4850 set_vim_var_nr(VV_MOUSE_WIN, 0);
4851 set_vim_var_nr(VV_MOUSE_WINID, 0);
4852 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4853 set_vim_var_nr(VV_MOUSE_COL, 0);
4854
4855 rettv->vval.v_number = n;
4856 if (IS_SPECIAL(n) || mod_mask != 0)
4857 {
4858 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4859 int i = 0;
4860
4861 /* Turn a special key into three bytes, plus modifier. */
4862 if (mod_mask != 0)
4863 {
4864 temp[i++] = K_SPECIAL;
4865 temp[i++] = KS_MODIFIER;
4866 temp[i++] = mod_mask;
4867 }
4868 if (IS_SPECIAL(n))
4869 {
4870 temp[i++] = K_SPECIAL;
4871 temp[i++] = K_SECOND(n);
4872 temp[i++] = K_THIRD(n);
4873 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004874 else if (has_mbyte)
4875 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004876 else
4877 temp[i++] = n;
4878 temp[i++] = NUL;
4879 rettv->v_type = VAR_STRING;
4880 rettv->vval.v_string = vim_strsave(temp);
4881
4882#ifdef FEAT_MOUSE
4883 if (is_mouse_key(n))
4884 {
4885 int row = mouse_row;
4886 int col = mouse_col;
4887 win_T *win;
4888 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004889 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004890 int winnr = 1;
4891
4892 if (row >= 0 && col >= 0)
4893 {
4894 /* Find the window at the mouse coordinates and compute the
4895 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004896 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004897 if (win == NULL)
4898 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004899 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004900# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004901 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004902 winnr = 0;
4903 else
4904# endif
4905 for (wp = firstwin; wp != win && wp != NULL;
4906 wp = wp->w_next)
4907 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004908 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4909 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4910 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4911 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4912 }
4913 }
4914#endif
4915 }
4916}
4917
4918/*
4919 * "getcharmod()" function
4920 */
4921 static void
4922f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4923{
4924 rettv->vval.v_number = mod_mask;
4925}
4926
4927/*
4928 * "getcharsearch()" function
4929 */
4930 static void
4931f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4932{
4933 if (rettv_dict_alloc(rettv) != FAIL)
4934 {
4935 dict_T *dict = rettv->vval.v_dict;
4936
Bram Moolenaare0be1672018-07-08 16:50:37 +02004937 dict_add_string(dict, "char", last_csearch());
4938 dict_add_number(dict, "forward", last_csearch_forward());
4939 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004940 }
4941}
4942
4943/*
4944 * "getcmdline()" function
4945 */
4946 static void
4947f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4948{
4949 rettv->v_type = VAR_STRING;
4950 rettv->vval.v_string = get_cmdline_str();
4951}
4952
4953/*
4954 * "getcmdpos()" function
4955 */
4956 static void
4957f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4958{
4959 rettv->vval.v_number = get_cmdline_pos() + 1;
4960}
4961
4962/*
4963 * "getcmdtype()" function
4964 */
4965 static void
4966f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4967{
4968 rettv->v_type = VAR_STRING;
4969 rettv->vval.v_string = alloc(2);
4970 if (rettv->vval.v_string != NULL)
4971 {
4972 rettv->vval.v_string[0] = get_cmdline_type();
4973 rettv->vval.v_string[1] = NUL;
4974 }
4975}
4976
4977/*
4978 * "getcmdwintype()" function
4979 */
4980 static void
4981f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4982{
4983 rettv->v_type = VAR_STRING;
4984 rettv->vval.v_string = NULL;
4985#ifdef FEAT_CMDWIN
4986 rettv->vval.v_string = alloc(2);
4987 if (rettv->vval.v_string != NULL)
4988 {
4989 rettv->vval.v_string[0] = cmdwin_type;
4990 rettv->vval.v_string[1] = NUL;
4991 }
4992#endif
4993}
4994
4995#if defined(FEAT_CMDL_COMPL)
4996/*
4997 * "getcompletion()" function
4998 */
4999 static void
5000f_getcompletion(typval_T *argvars, typval_T *rettv)
5001{
5002 char_u *pat;
5003 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005004 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005005 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5006 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005007
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005008 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005009 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005010
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005011 if (p_wic)
5012 options |= WILD_ICASE;
5013
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005014 /* For filtered results, 'wildignore' is used */
5015 if (!filtered)
5016 options |= WILD_KEEP_ALL;
5017
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005018 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005019 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005020 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005021 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005022 if (xpc.xp_context == EXPAND_NOTHING)
5023 {
5024 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005025 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005026 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005027 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005028 return;
5029 }
5030
5031# if defined(FEAT_MENU)
5032 if (xpc.xp_context == EXPAND_MENUS)
5033 {
5034 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5035 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5036 }
5037# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005038#ifdef FEAT_CSCOPE
5039 if (xpc.xp_context == EXPAND_CSCOPE)
5040 {
5041 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5042 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5043 }
5044#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005045#ifdef FEAT_SIGNS
5046 if (xpc.xp_context == EXPAND_SIGN)
5047 {
5048 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5049 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5050 }
5051#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005052
5053 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5054 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5055 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005056 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005057
5058 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5059
5060 for (i = 0; i < xpc.xp_numfiles; i++)
5061 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5062 }
5063 vim_free(pat);
5064 ExpandCleanup(&xpc);
5065}
5066#endif
5067
5068/*
5069 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005070 *
5071 * Return the current working directory of a window in a tab page.
5072 * First optional argument 'winnr' is the window number or -1 and the second
5073 * optional argument 'tabnr' is the tab page number.
5074 *
5075 * If no arguments are supplied, then return the directory of the current
5076 * window.
5077 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5078 * the specified window.
5079 * If 'winnr' is 0 then return the directory of the current window.
5080 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5081 * directory of the specified tab page. Otherwise return the directory of the
5082 * specified window in the specified tab page.
5083 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005084 */
5085 static void
5086f_getcwd(typval_T *argvars, typval_T *rettv)
5087{
5088 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005089 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005090 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005091 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005092
5093 rettv->v_type = VAR_STRING;
5094 rettv->vval.v_string = NULL;
5095
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005096 if (argvars[0].v_type == VAR_NUMBER
5097 && argvars[0].vval.v_number == -1
5098 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005099 global = TRUE;
5100 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005101 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005102
5103 if (wp != NULL && wp->w_localdir != NULL)
5104 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005105 else if (tp != NULL && tp->tp_localdir != NULL)
5106 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5107 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005108 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005109 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110 rettv->vval.v_string = vim_strsave(globaldir);
5111 else
5112 {
5113 cwd = alloc(MAXPATHL);
5114 if (cwd != NULL)
5115 {
5116 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5117 rettv->vval.v_string = vim_strsave(cwd);
5118 vim_free(cwd);
5119 }
5120 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005121 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005122#ifdef BACKSLASH_IN_FILENAME
5123 if (rettv->vval.v_string != NULL)
5124 slash_adjust(rettv->vval.v_string);
5125#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005126}
5127
5128/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005129 * "getenv()" function
5130 */
5131 static void
5132f_getenv(typval_T *argvars, typval_T *rettv)
5133{
5134 int mustfree = FALSE;
5135 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5136
5137 if (p == NULL)
5138 {
5139 rettv->v_type = VAR_SPECIAL;
5140 rettv->vval.v_number = VVAL_NULL;
5141 return;
5142 }
5143 if (!mustfree)
5144 p = vim_strsave(p);
5145 rettv->vval.v_string = p;
5146 rettv->v_type = VAR_STRING;
5147}
5148
5149/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005150 * "getfontname()" function
5151 */
5152 static void
5153f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5154{
5155 rettv->v_type = VAR_STRING;
5156 rettv->vval.v_string = NULL;
5157#ifdef FEAT_GUI
5158 if (gui.in_use)
5159 {
5160 GuiFont font;
5161 char_u *name = NULL;
5162
5163 if (argvars[0].v_type == VAR_UNKNOWN)
5164 {
5165 /* Get the "Normal" font. Either the name saved by
5166 * hl_set_font_name() or from the font ID. */
5167 font = gui.norm_font;
5168 name = hl_get_font_name();
5169 }
5170 else
5171 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005172 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005173 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5174 return;
5175 font = gui_mch_get_font(name, FALSE);
5176 if (font == NOFONT)
5177 return; /* Invalid font name, return empty string. */
5178 }
5179 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5180 if (argvars[0].v_type != VAR_UNKNOWN)
5181 gui_mch_free_font(font);
5182 }
5183#endif
5184}
5185
5186/*
5187 * "getfperm({fname})" function
5188 */
5189 static void
5190f_getfperm(typval_T *argvars, typval_T *rettv)
5191{
5192 char_u *fname;
5193 stat_T st;
5194 char_u *perm = NULL;
5195 char_u flags[] = "rwx";
5196 int i;
5197
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005198 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005199
5200 rettv->v_type = VAR_STRING;
5201 if (mch_stat((char *)fname, &st) >= 0)
5202 {
5203 perm = vim_strsave((char_u *)"---------");
5204 if (perm != NULL)
5205 {
5206 for (i = 0; i < 9; i++)
5207 {
5208 if (st.st_mode & (1 << (8 - i)))
5209 perm[i] = flags[i % 3];
5210 }
5211 }
5212 }
5213 rettv->vval.v_string = perm;
5214}
5215
5216/*
5217 * "getfsize({fname})" function
5218 */
5219 static void
5220f_getfsize(typval_T *argvars, typval_T *rettv)
5221{
5222 char_u *fname;
5223 stat_T st;
5224
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005225 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005226
5227 rettv->v_type = VAR_NUMBER;
5228
5229 if (mch_stat((char *)fname, &st) >= 0)
5230 {
5231 if (mch_isdir(fname))
5232 rettv->vval.v_number = 0;
5233 else
5234 {
5235 rettv->vval.v_number = (varnumber_T)st.st_size;
5236
5237 /* non-perfect check for overflow */
5238 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5239 rettv->vval.v_number = -2;
5240 }
5241 }
5242 else
5243 rettv->vval.v_number = -1;
5244}
5245
5246/*
5247 * "getftime({fname})" function
5248 */
5249 static void
5250f_getftime(typval_T *argvars, typval_T *rettv)
5251{
5252 char_u *fname;
5253 stat_T st;
5254
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005255 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005256
5257 if (mch_stat((char *)fname, &st) >= 0)
5258 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5259 else
5260 rettv->vval.v_number = -1;
5261}
5262
5263/*
5264 * "getftype({fname})" function
5265 */
5266 static void
5267f_getftype(typval_T *argvars, typval_T *rettv)
5268{
5269 char_u *fname;
5270 stat_T st;
5271 char_u *type = NULL;
5272 char *t;
5273
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005274 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005275
5276 rettv->v_type = VAR_STRING;
5277 if (mch_lstat((char *)fname, &st) >= 0)
5278 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005279 if (S_ISREG(st.st_mode))
5280 t = "file";
5281 else if (S_ISDIR(st.st_mode))
5282 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005283 else if (S_ISLNK(st.st_mode))
5284 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005285 else if (S_ISBLK(st.st_mode))
5286 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005287 else if (S_ISCHR(st.st_mode))
5288 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005289 else if (S_ISFIFO(st.st_mode))
5290 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005291 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005292 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005293 else
5294 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005295 type = vim_strsave((char_u *)t);
5296 }
5297 rettv->vval.v_string = type;
5298}
5299
5300/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005301 * "getjumplist()" function
5302 */
5303 static void
5304f_getjumplist(typval_T *argvars, typval_T *rettv)
5305{
5306#ifdef FEAT_JUMPLIST
5307 win_T *wp;
5308 int i;
5309 list_T *l;
5310 dict_T *d;
5311#endif
5312
5313 if (rettv_list_alloc(rettv) != OK)
5314 return;
5315
5316#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005317 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005318 if (wp == NULL)
5319 return;
5320
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005321 cleanup_jumplist(wp, TRUE);
5322
Bram Moolenaar4f505882018-02-10 21:06:32 +01005323 l = list_alloc();
5324 if (l == NULL)
5325 return;
5326
5327 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5328 return;
5329 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5330
5331 for (i = 0; i < wp->w_jumplistlen; ++i)
5332 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005333 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5334 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005335 if ((d = dict_alloc()) == NULL)
5336 return;
5337 if (list_append_dict(l, d) == FAIL)
5338 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005339 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5340 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005341 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005342 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005343 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005344 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005345 }
5346#endif
5347}
5348
5349/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005350 * "getline(lnum, [end])" function
5351 */
5352 static void
5353f_getline(typval_T *argvars, typval_T *rettv)
5354{
5355 linenr_T lnum;
5356 linenr_T end;
5357 int retlist;
5358
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005359 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005360 if (argvars[1].v_type == VAR_UNKNOWN)
5361 {
5362 end = 0;
5363 retlist = FALSE;
5364 }
5365 else
5366 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005367 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005368 retlist = TRUE;
5369 }
5370
5371 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5372}
5373
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005374#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005375 static void
5376get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5377{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005378 if (what_arg->v_type == VAR_UNKNOWN)
5379 {
5380 if (rettv_list_alloc(rettv) == OK)
5381 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005382 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005383 }
5384 else
5385 {
5386 if (rettv_dict_alloc(rettv) == OK)
5387 if (is_qf || (wp != NULL))
5388 {
5389 if (what_arg->v_type == VAR_DICT)
5390 {
5391 dict_T *d = what_arg->vval.v_dict;
5392
5393 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005394 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005395 }
5396 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005397 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005398 }
5399 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005400}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005401#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005402
5403/*
5404 * "getloclist()" function
5405 */
5406 static void
5407f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5408{
5409#ifdef FEAT_QUICKFIX
5410 win_T *wp;
5411
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005412 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005413 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5414#endif
5415}
5416
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005417/*
5418 * "getmatches()" function
5419 */
5420 static void
5421f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5422{
5423#ifdef FEAT_SEARCH_EXTRA
5424 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005425 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005426 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005427 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005428
Bram Moolenaaraff74912019-03-30 18:11:49 +01005429 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5430 return;
5431
5432 cur = win->w_match_head;
5433 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005434 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005435 dict = dict_alloc();
5436 if (dict == NULL)
5437 return;
5438 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005439 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005440 /* match added with matchaddpos() */
5441 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005442 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005443 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005444 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005445 list_T *l;
5446
5447 llpos = &cur->pos.pos[i];
5448 if (llpos->lnum == 0)
5449 break;
5450 l = list_alloc();
5451 if (l == NULL)
5452 break;
5453 list_append_number(l, (varnumber_T)llpos->lnum);
5454 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005455 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005456 list_append_number(l, (varnumber_T)llpos->col);
5457 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005458 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005459 sprintf(buf, "pos%d", i + 1);
5460 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005461 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005462 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005463 else
5464 {
5465 dict_add_string(dict, "pattern", cur->pattern);
5466 }
5467 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5468 dict_add_number(dict, "priority", (long)cur->priority);
5469 dict_add_number(dict, "id", (long)cur->id);
5470# if defined(FEAT_CONCEAL)
5471 if (cur->conceal_char)
5472 {
5473 char_u buf[MB_MAXBYTES + 1];
5474
5475 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5476 dict_add_string(dict, "conceal", (char_u *)&buf);
5477 }
5478# endif
5479 list_append_dict(rettv->vval.v_list, dict);
5480 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005481 }
5482#endif
5483}
5484
5485/*
5486 * "getpid()" function
5487 */
5488 static void
5489f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5490{
5491 rettv->vval.v_number = mch_get_pid();
5492}
5493
5494 static void
5495getpos_both(
5496 typval_T *argvars,
5497 typval_T *rettv,
5498 int getcurpos)
5499{
5500 pos_T *fp;
5501 list_T *l;
5502 int fnum = -1;
5503
5504 if (rettv_list_alloc(rettv) == OK)
5505 {
5506 l = rettv->vval.v_list;
5507 if (getcurpos)
5508 fp = &curwin->w_cursor;
5509 else
5510 fp = var2fpos(&argvars[0], TRUE, &fnum);
5511 if (fnum != -1)
5512 list_append_number(l, (varnumber_T)fnum);
5513 else
5514 list_append_number(l, (varnumber_T)0);
5515 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5516 : (varnumber_T)0);
5517 list_append_number(l, (fp != NULL)
5518 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5519 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005520 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005521 (varnumber_T)0);
5522 if (getcurpos)
5523 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005524 int save_set_curswant = curwin->w_set_curswant;
5525 colnr_T save_curswant = curwin->w_curswant;
5526 colnr_T save_virtcol = curwin->w_virtcol;
5527
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005528 update_curswant();
5529 list_append_number(l, curwin->w_curswant == MAXCOL ?
5530 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005531
5532 // Do not change "curswant", as it is unexpected that a get
5533 // function has a side effect.
5534 if (save_set_curswant)
5535 {
5536 curwin->w_set_curswant = save_set_curswant;
5537 curwin->w_curswant = save_curswant;
5538 curwin->w_virtcol = save_virtcol;
5539 curwin->w_valid &= ~VALID_VIRTCOL;
5540 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005541 }
5542 }
5543 else
5544 rettv->vval.v_number = FALSE;
5545}
5546
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005547/*
5548 * "getcurpos()" function
5549 */
5550 static void
5551f_getcurpos(typval_T *argvars, typval_T *rettv)
5552{
5553 getpos_both(argvars, rettv, TRUE);
5554}
5555
5556/*
5557 * "getpos(string)" function
5558 */
5559 static void
5560f_getpos(typval_T *argvars, typval_T *rettv)
5561{
5562 getpos_both(argvars, rettv, FALSE);
5563}
5564
5565/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005566 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005567 */
5568 static void
5569f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5570{
5571#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005572 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005573#endif
5574}
5575
5576/*
5577 * "getreg()" function
5578 */
5579 static void
5580f_getreg(typval_T *argvars, typval_T *rettv)
5581{
5582 char_u *strregname;
5583 int regname;
5584 int arg2 = FALSE;
5585 int return_list = FALSE;
5586 int error = FALSE;
5587
5588 if (argvars[0].v_type != VAR_UNKNOWN)
5589 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005590 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005591 error = strregname == NULL;
5592 if (argvars[1].v_type != VAR_UNKNOWN)
5593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005594 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005595 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005596 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005597 }
5598 }
5599 else
5600 strregname = get_vim_var_str(VV_REG);
5601
5602 if (error)
5603 return;
5604
5605 regname = (strregname == NULL ? '"' : *strregname);
5606 if (regname == 0)
5607 regname = '"';
5608
5609 if (return_list)
5610 {
5611 rettv->v_type = VAR_LIST;
5612 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5613 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5614 if (rettv->vval.v_list == NULL)
5615 (void)rettv_list_alloc(rettv);
5616 else
5617 ++rettv->vval.v_list->lv_refcount;
5618 }
5619 else
5620 {
5621 rettv->v_type = VAR_STRING;
5622 rettv->vval.v_string = get_reg_contents(regname,
5623 arg2 ? GREG_EXPR_SRC : 0);
5624 }
5625}
5626
5627/*
5628 * "getregtype()" function
5629 */
5630 static void
5631f_getregtype(typval_T *argvars, typval_T *rettv)
5632{
5633 char_u *strregname;
5634 int regname;
5635 char_u buf[NUMBUFLEN + 2];
5636 long reglen = 0;
5637
5638 if (argvars[0].v_type != VAR_UNKNOWN)
5639 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005640 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005641 if (strregname == NULL) /* type error; errmsg already given */
5642 {
5643 rettv->v_type = VAR_STRING;
5644 rettv->vval.v_string = NULL;
5645 return;
5646 }
5647 }
5648 else
5649 /* Default to v:register */
5650 strregname = get_vim_var_str(VV_REG);
5651
5652 regname = (strregname == NULL ? '"' : *strregname);
5653 if (regname == 0)
5654 regname = '"';
5655
5656 buf[0] = NUL;
5657 buf[1] = NUL;
5658 switch (get_reg_type(regname, &reglen))
5659 {
5660 case MLINE: buf[0] = 'V'; break;
5661 case MCHAR: buf[0] = 'v'; break;
5662 case MBLOCK:
5663 buf[0] = Ctrl_V;
5664 sprintf((char *)buf + 1, "%ld", reglen + 1);
5665 break;
5666 }
5667 rettv->v_type = VAR_STRING;
5668 rettv->vval.v_string = vim_strsave(buf);
5669}
5670
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005671/*
5672 * Returns information (variables, options, etc.) about a tab page
5673 * as a dictionary.
5674 */
5675 static dict_T *
5676get_tabpage_info(tabpage_T *tp, int tp_idx)
5677{
5678 win_T *wp;
5679 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005680 list_T *l;
5681
5682 dict = dict_alloc();
5683 if (dict == NULL)
5684 return NULL;
5685
Bram Moolenaare0be1672018-07-08 16:50:37 +02005686 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005687
5688 l = list_alloc();
5689 if (l != NULL)
5690 {
5691 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5692 wp; wp = wp->w_next)
5693 list_append_number(l, (varnumber_T)wp->w_id);
5694 dict_add_list(dict, "windows", l);
5695 }
5696
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005697 /* Make a reference to tabpage variables */
5698 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005699
5700 return dict;
5701}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005702
5703/*
5704 * "gettabinfo()" function
5705 */
5706 static void
5707f_gettabinfo(typval_T *argvars, typval_T *rettv)
5708{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005709 tabpage_T *tp, *tparg = NULL;
5710 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005711 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005712
5713 if (rettv_list_alloc(rettv) != OK)
5714 return;
5715
5716 if (argvars[0].v_type != VAR_UNKNOWN)
5717 {
5718 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005719 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005720 if (tparg == NULL)
5721 return;
5722 }
5723
5724 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005725 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005726 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005727 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005728 if (tparg != NULL && tp != tparg)
5729 continue;
5730 d = get_tabpage_info(tp, tpnr);
5731 if (d != NULL)
5732 list_append_dict(rettv->vval.v_list, d);
5733 if (tparg != NULL)
5734 return;
5735 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005736}
5737
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005738/*
5739 * "gettabvar()" function
5740 */
5741 static void
5742f_gettabvar(typval_T *argvars, typval_T *rettv)
5743{
5744 win_T *oldcurwin;
5745 tabpage_T *tp, *oldtabpage;
5746 dictitem_T *v;
5747 char_u *varname;
5748 int done = FALSE;
5749
5750 rettv->v_type = VAR_STRING;
5751 rettv->vval.v_string = NULL;
5752
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005753 varname = tv_get_string_chk(&argvars[1]);
5754 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005755 if (tp != NULL && varname != NULL)
5756 {
5757 /* Set tp to be our tabpage, temporarily. Also set the window to the
5758 * first window in the tabpage, otherwise the window is not valid. */
5759 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005760 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5761 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005762 {
5763 /* look up the variable */
5764 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5765 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5766 if (v != NULL)
5767 {
5768 copy_tv(&v->di_tv, rettv);
5769 done = TRUE;
5770 }
5771 }
5772
5773 /* restore previous notion of curwin */
5774 restore_win(oldcurwin, oldtabpage, TRUE);
5775 }
5776
5777 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5778 copy_tv(&argvars[2], rettv);
5779}
5780
5781/*
5782 * "gettabwinvar()" function
5783 */
5784 static void
5785f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5786{
5787 getwinvar(argvars, rettv, 1);
5788}
5789
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005790/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005791 * "gettagstack()" function
5792 */
5793 static void
5794f_gettagstack(typval_T *argvars, typval_T *rettv)
5795{
5796 win_T *wp = curwin; // default is current window
5797
5798 if (rettv_dict_alloc(rettv) != OK)
5799 return;
5800
5801 if (argvars[0].v_type != VAR_UNKNOWN)
5802 {
5803 wp = find_win_by_nr_or_id(&argvars[0]);
5804 if (wp == NULL)
5805 return;
5806 }
5807
5808 get_tagstack(wp, rettv->vval.v_dict);
5809}
5810
5811/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005812 * Returns information about a window as a dictionary.
5813 */
5814 static dict_T *
5815get_win_info(win_T *wp, short tpnr, short winnr)
5816{
5817 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005818
5819 dict = dict_alloc();
5820 if (dict == NULL)
5821 return NULL;
5822
Bram Moolenaare0be1672018-07-08 16:50:37 +02005823 dict_add_number(dict, "tabnr", tpnr);
5824 dict_add_number(dict, "winnr", winnr);
5825 dict_add_number(dict, "winid", wp->w_id);
5826 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005827 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005828 dict_add_number(dict, "topline", wp->w_topline);
5829 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005830#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005831 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005832#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005833 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005834 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005835 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005836
Bram Moolenaar69905d12017-08-13 18:14:47 +02005837#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005838 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005839#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005840#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005841 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5842 dict_add_number(dict, "loclist",
5843 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005844#endif
5845
Bram Moolenaar30567352016-08-27 21:25:44 +02005846 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005847 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005848
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005849 return dict;
5850}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005851
5852/*
5853 * "getwininfo()" function
5854 */
5855 static void
5856f_getwininfo(typval_T *argvars, typval_T *rettv)
5857{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005858 tabpage_T *tp;
5859 win_T *wp = NULL, *wparg = NULL;
5860 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005861 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005862
5863 if (rettv_list_alloc(rettv) != OK)
5864 return;
5865
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005866 if (argvars[0].v_type != VAR_UNKNOWN)
5867 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005868 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005869 if (wparg == NULL)
5870 return;
5871 }
5872
5873 /* Collect information about either all the windows across all the tab
5874 * pages or one particular window.
5875 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005876 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005877 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005878 tabnr++;
5879 winnr = 0;
5880 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005881 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005882 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005883 if (wparg != NULL && wp != wparg)
5884 continue;
5885 d = get_win_info(wp, tabnr, winnr);
5886 if (d != NULL)
5887 list_append_dict(rettv->vval.v_list, d);
5888 if (wparg != NULL)
5889 /* found information about a specific window */
5890 return;
5891 }
5892 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005893}
5894
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005895/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005896 * "win_execute()" function
5897 */
5898 static void
5899f_win_execute(typval_T *argvars, typval_T *rettv)
5900{
5901 int id = (int)tv_get_number(argvars);
5902 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005903 win_T *save_curwin;
5904 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005905
5906 if (wp != NULL)
5907 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005908 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5909 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005910 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005911 check_cursor();
5912 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005913 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005914 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005915 }
5916}
5917
5918/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005919 * "win_findbuf()" function
5920 */
5921 static void
5922f_win_findbuf(typval_T *argvars, typval_T *rettv)
5923{
5924 if (rettv_list_alloc(rettv) != FAIL)
5925 win_findbuf(argvars, rettv->vval.v_list);
5926}
5927
5928/*
5929 * "win_getid()" function
5930 */
5931 static void
5932f_win_getid(typval_T *argvars, typval_T *rettv)
5933{
5934 rettv->vval.v_number = win_getid(argvars);
5935}
5936
5937/*
5938 * "win_gotoid()" function
5939 */
5940 static void
5941f_win_gotoid(typval_T *argvars, typval_T *rettv)
5942{
5943 rettv->vval.v_number = win_gotoid(argvars);
5944}
5945
5946/*
5947 * "win_id2tabwin()" function
5948 */
5949 static void
5950f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5951{
5952 if (rettv_list_alloc(rettv) != FAIL)
5953 win_id2tabwin(argvars, rettv->vval.v_list);
5954}
5955
5956/*
5957 * "win_id2win()" function
5958 */
5959 static void
5960f_win_id2win(typval_T *argvars, typval_T *rettv)
5961{
5962 rettv->vval.v_number = win_id2win(argvars);
5963}
5964
5965/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005966 * "win_screenpos()" function
5967 */
5968 static void
5969f_win_screenpos(typval_T *argvars, typval_T *rettv)
5970{
5971 win_T *wp;
5972
5973 if (rettv_list_alloc(rettv) == FAIL)
5974 return;
5975
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005976 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005977 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5978 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5979}
5980
5981/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005982 * "getwinpos({timeout})" function
5983 */
5984 static void
5985f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5986{
5987 int x = -1;
5988 int y = -1;
5989
5990 if (rettv_list_alloc(rettv) == FAIL)
5991 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005992#if defined(FEAT_GUI) \
5993 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5994 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005995 {
5996 varnumber_T timeout = 100;
5997
5998 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005999 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006000
6001 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006002 }
6003#endif
6004 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6005 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6006}
6007
6008
6009/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006010 * "getwinposx()" function
6011 */
6012 static void
6013f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6014{
6015 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006016#if defined(FEAT_GUI) \
6017 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6018 || defined(MSWIN)
6019
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006020 {
6021 int x, y;
6022
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006023 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006024 rettv->vval.v_number = x;
6025 }
6026#endif
6027}
6028
6029/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006030 * "getwinposy()" function
6031 */
6032 static void
6033f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6034{
6035 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006036#if defined(FEAT_GUI) \
6037 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6038 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006039 {
6040 int x, y;
6041
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006042 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006043 rettv->vval.v_number = y;
6044 }
6045#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006046}
6047
6048/*
6049 * "getwinvar()" function
6050 */
6051 static void
6052f_getwinvar(typval_T *argvars, typval_T *rettv)
6053{
6054 getwinvar(argvars, rettv, 0);
6055}
6056
6057/*
6058 * "glob()" function
6059 */
6060 static void
6061f_glob(typval_T *argvars, typval_T *rettv)
6062{
6063 int options = WILD_SILENT|WILD_USE_NL;
6064 expand_T xpc;
6065 int error = FALSE;
6066
6067 /* When the optional second argument is non-zero, don't remove matches
6068 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6069 rettv->v_type = VAR_STRING;
6070 if (argvars[1].v_type != VAR_UNKNOWN)
6071 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006072 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006073 options |= WILD_KEEP_ALL;
6074 if (argvars[2].v_type != VAR_UNKNOWN)
6075 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006076 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006077 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006078 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006079 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006080 options |= WILD_ALLLINKS;
6081 }
6082 }
6083 if (!error)
6084 {
6085 ExpandInit(&xpc);
6086 xpc.xp_context = EXPAND_FILES;
6087 if (p_wic)
6088 options += WILD_ICASE;
6089 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006090 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006091 NULL, options, WILD_ALL);
6092 else if (rettv_list_alloc(rettv) != FAIL)
6093 {
6094 int i;
6095
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006096 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006097 NULL, options, WILD_ALL_KEEP);
6098 for (i = 0; i < xpc.xp_numfiles; i++)
6099 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6100
6101 ExpandCleanup(&xpc);
6102 }
6103 }
6104 else
6105 rettv->vval.v_string = NULL;
6106}
6107
6108/*
6109 * "globpath()" function
6110 */
6111 static void
6112f_globpath(typval_T *argvars, typval_T *rettv)
6113{
6114 int flags = 0;
6115 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006116 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006117 int error = FALSE;
6118 garray_T ga;
6119 int i;
6120
6121 /* When the optional second argument is non-zero, don't remove matches
6122 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6123 rettv->v_type = VAR_STRING;
6124 if (argvars[2].v_type != VAR_UNKNOWN)
6125 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006126 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006127 flags |= WILD_KEEP_ALL;
6128 if (argvars[3].v_type != VAR_UNKNOWN)
6129 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006130 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006131 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006132 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006133 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006134 flags |= WILD_ALLLINKS;
6135 }
6136 }
6137 if (file != NULL && !error)
6138 {
6139 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006140 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006141 if (rettv->v_type == VAR_STRING)
6142 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6143 else if (rettv_list_alloc(rettv) != FAIL)
6144 for (i = 0; i < ga.ga_len; ++i)
6145 list_append_string(rettv->vval.v_list,
6146 ((char_u **)(ga.ga_data))[i], -1);
6147 ga_clear_strings(&ga);
6148 }
6149 else
6150 rettv->vval.v_string = NULL;
6151}
6152
6153/*
6154 * "glob2regpat()" function
6155 */
6156 static void
6157f_glob2regpat(typval_T *argvars, typval_T *rettv)
6158{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006159 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006160
6161 rettv->v_type = VAR_STRING;
6162 rettv->vval.v_string = (pat == NULL)
6163 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6164}
6165
6166/* for VIM_VERSION_ defines */
6167#include "version.h"
6168
6169/*
6170 * "has()" function
6171 */
6172 static void
6173f_has(typval_T *argvars, typval_T *rettv)
6174{
6175 int i;
6176 char_u *name;
6177 int n = FALSE;
6178 static char *(has_list[]) =
6179 {
6180#ifdef AMIGA
6181 "amiga",
6182# ifdef FEAT_ARP
6183 "arp",
6184# endif
6185#endif
6186#ifdef __BEOS__
6187 "beos",
6188#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006189#if defined(BSD) && !defined(MACOS_X)
6190 "bsd",
6191#endif
6192#ifdef hpux
6193 "hpux",
6194#endif
6195#ifdef __linux__
6196 "linux",
6197#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006198#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006199 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6200 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006201# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006202 "macunix", /* Mac OS X, with the darwin feature */
6203 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006204# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006205#endif
6206#ifdef __QNX__
6207 "qnx",
6208#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006209#ifdef SUN_SYSTEM
6210 "sun",
6211#else
6212 "moon",
6213#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006214#ifdef UNIX
6215 "unix",
6216#endif
6217#ifdef VMS
6218 "vms",
6219#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006220#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006221 "win32",
6222#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006223#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006224 "win32unix",
6225#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006226#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006227 "win64",
6228#endif
6229#ifdef EBCDIC
6230 "ebcdic",
6231#endif
6232#ifndef CASE_INSENSITIVE_FILENAME
6233 "fname_case",
6234#endif
6235#ifdef HAVE_ACL
6236 "acl",
6237#endif
6238#ifdef FEAT_ARABIC
6239 "arabic",
6240#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006241 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006242#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006243 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006244#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006245#ifdef FEAT_AUTOSERVERNAME
6246 "autoservername",
6247#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006248#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006249 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006250# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006251 "balloon_multiline",
6252# endif
6253#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006254#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006255 "balloon_eval_term",
6256#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006257#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6258 "builtin_terms",
6259# ifdef ALL_BUILTIN_TCAPS
6260 "all_builtin_terms",
6261# endif
6262#endif
6263#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006264 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006265 || defined(FEAT_GUI_MOTIF))
6266 "browsefilter",
6267#endif
6268#ifdef FEAT_BYTEOFF
6269 "byte_offset",
6270#endif
6271#ifdef FEAT_JOB_CHANNEL
6272 "channel",
6273#endif
6274#ifdef FEAT_CINDENT
6275 "cindent",
6276#endif
6277#ifdef FEAT_CLIENTSERVER
6278 "clientserver",
6279#endif
6280#ifdef FEAT_CLIPBOARD
6281 "clipboard",
6282#endif
6283#ifdef FEAT_CMDL_COMPL
6284 "cmdline_compl",
6285#endif
6286#ifdef FEAT_CMDHIST
6287 "cmdline_hist",
6288#endif
6289#ifdef FEAT_COMMENTS
6290 "comments",
6291#endif
6292#ifdef FEAT_CONCEAL
6293 "conceal",
6294#endif
6295#ifdef FEAT_CRYPT
6296 "cryptv",
6297 "crypt-blowfish",
6298 "crypt-blowfish2",
6299#endif
6300#ifdef FEAT_CSCOPE
6301 "cscope",
6302#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006303 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006304#ifdef CURSOR_SHAPE
6305 "cursorshape",
6306#endif
6307#ifdef DEBUG
6308 "debug",
6309#endif
6310#ifdef FEAT_CON_DIALOG
6311 "dialog_con",
6312#endif
6313#ifdef FEAT_GUI_DIALOG
6314 "dialog_gui",
6315#endif
6316#ifdef FEAT_DIFF
6317 "diff",
6318#endif
6319#ifdef FEAT_DIGRAPHS
6320 "digraphs",
6321#endif
6322#ifdef FEAT_DIRECTX
6323 "directx",
6324#endif
6325#ifdef FEAT_DND
6326 "dnd",
6327#endif
6328#ifdef FEAT_EMACS_TAGS
6329 "emacs_tags",
6330#endif
6331 "eval", /* always present, of course! */
6332 "ex_extra", /* graduated feature */
6333#ifdef FEAT_SEARCH_EXTRA
6334 "extra_search",
6335#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006336#ifdef FEAT_SEARCHPATH
6337 "file_in_path",
6338#endif
6339#ifdef FEAT_FILTERPIPE
6340 "filterpipe",
6341#endif
6342#ifdef FEAT_FIND_ID
6343 "find_in_path",
6344#endif
6345#ifdef FEAT_FLOAT
6346 "float",
6347#endif
6348#ifdef FEAT_FOLDING
6349 "folding",
6350#endif
6351#ifdef FEAT_FOOTER
6352 "footer",
6353#endif
6354#if !defined(USE_SYSTEM) && defined(UNIX)
6355 "fork",
6356#endif
6357#ifdef FEAT_GETTEXT
6358 "gettext",
6359#endif
6360#ifdef FEAT_GUI
6361 "gui",
6362#endif
6363#ifdef FEAT_GUI_ATHENA
6364# ifdef FEAT_GUI_NEXTAW
6365 "gui_neXtaw",
6366# else
6367 "gui_athena",
6368# endif
6369#endif
6370#ifdef FEAT_GUI_GTK
6371 "gui_gtk",
6372# ifdef USE_GTK3
6373 "gui_gtk3",
6374# else
6375 "gui_gtk2",
6376# endif
6377#endif
6378#ifdef FEAT_GUI_GNOME
6379 "gui_gnome",
6380#endif
6381#ifdef FEAT_GUI_MAC
6382 "gui_mac",
6383#endif
6384#ifdef FEAT_GUI_MOTIF
6385 "gui_motif",
6386#endif
6387#ifdef FEAT_GUI_PHOTON
6388 "gui_photon",
6389#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006390#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006391 "gui_win32",
6392#endif
6393#ifdef FEAT_HANGULIN
6394 "hangul_input",
6395#endif
6396#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6397 "iconv",
6398#endif
6399#ifdef FEAT_INS_EXPAND
6400 "insert_expand",
6401#endif
6402#ifdef FEAT_JOB_CHANNEL
6403 "job",
6404#endif
6405#ifdef FEAT_JUMPLIST
6406 "jumplist",
6407#endif
6408#ifdef FEAT_KEYMAP
6409 "keymap",
6410#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006411 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006412#ifdef FEAT_LANGMAP
6413 "langmap",
6414#endif
6415#ifdef FEAT_LIBCALL
6416 "libcall",
6417#endif
6418#ifdef FEAT_LINEBREAK
6419 "linebreak",
6420#endif
6421#ifdef FEAT_LISP
6422 "lispindent",
6423#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006424 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006425#ifdef FEAT_LOCALMAP
6426 "localmap",
6427#endif
6428#ifdef FEAT_LUA
6429# ifndef DYNAMIC_LUA
6430 "lua",
6431# endif
6432#endif
6433#ifdef FEAT_MENU
6434 "menu",
6435#endif
6436#ifdef FEAT_SESSION
6437 "mksession",
6438#endif
6439#ifdef FEAT_MODIFY_FNAME
6440 "modify_fname",
6441#endif
6442#ifdef FEAT_MOUSE
6443 "mouse",
6444#endif
6445#ifdef FEAT_MOUSESHAPE
6446 "mouseshape",
6447#endif
6448#if defined(UNIX) || defined(VMS)
6449# ifdef FEAT_MOUSE_DEC
6450 "mouse_dec",
6451# endif
6452# ifdef FEAT_MOUSE_GPM
6453 "mouse_gpm",
6454# endif
6455# ifdef FEAT_MOUSE_JSB
6456 "mouse_jsbterm",
6457# endif
6458# ifdef FEAT_MOUSE_NET
6459 "mouse_netterm",
6460# endif
6461# ifdef FEAT_MOUSE_PTERM
6462 "mouse_pterm",
6463# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006464# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006465 "mouse_sgr",
6466# endif
6467# ifdef FEAT_SYSMOUSE
6468 "mouse_sysmouse",
6469# endif
6470# ifdef FEAT_MOUSE_URXVT
6471 "mouse_urxvt",
6472# endif
6473# ifdef FEAT_MOUSE_XTERM
6474 "mouse_xterm",
6475# endif
6476#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006477 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006478#ifdef FEAT_MBYTE_IME
6479 "multi_byte_ime",
6480#endif
6481#ifdef FEAT_MULTI_LANG
6482 "multi_lang",
6483#endif
6484#ifdef FEAT_MZSCHEME
6485#ifndef DYNAMIC_MZSCHEME
6486 "mzscheme",
6487#endif
6488#endif
6489#ifdef FEAT_NUM64
6490 "num64",
6491#endif
6492#ifdef FEAT_OLE
6493 "ole",
6494#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006495#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006496 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006497#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006498#ifdef FEAT_PATH_EXTRA
6499 "path_extra",
6500#endif
6501#ifdef FEAT_PERL
6502#ifndef DYNAMIC_PERL
6503 "perl",
6504#endif
6505#endif
6506#ifdef FEAT_PERSISTENT_UNDO
6507 "persistent_undo",
6508#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006509#if defined(FEAT_PYTHON)
6510 "python_compiled",
6511# if defined(DYNAMIC_PYTHON)
6512 "python_dynamic",
6513# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006514 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006515 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006516# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006517#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006518#if defined(FEAT_PYTHON3)
6519 "python3_compiled",
6520# if defined(DYNAMIC_PYTHON3)
6521 "python3_dynamic",
6522# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006523 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006524 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006525# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006526#endif
6527#ifdef FEAT_POSTSCRIPT
6528 "postscript",
6529#endif
6530#ifdef FEAT_PRINTER
6531 "printer",
6532#endif
6533#ifdef FEAT_PROFILE
6534 "profile",
6535#endif
6536#ifdef FEAT_RELTIME
6537 "reltime",
6538#endif
6539#ifdef FEAT_QUICKFIX
6540 "quickfix",
6541#endif
6542#ifdef FEAT_RIGHTLEFT
6543 "rightleft",
6544#endif
6545#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6546 "ruby",
6547#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006548 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006549#ifdef FEAT_CMDL_INFO
6550 "showcmd",
6551 "cmdline_info",
6552#endif
6553#ifdef FEAT_SIGNS
6554 "signs",
6555#endif
6556#ifdef FEAT_SMARTINDENT
6557 "smartindent",
6558#endif
6559#ifdef STARTUPTIME
6560 "startuptime",
6561#endif
6562#ifdef FEAT_STL_OPT
6563 "statusline",
6564#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565#ifdef FEAT_NETBEANS_INTG
6566 "netbeans_intg",
6567#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006568#ifdef FEAT_SOUND
6569 "sound",
6570#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571#ifdef FEAT_SPELL
6572 "spell",
6573#endif
6574#ifdef FEAT_SYN_HL
6575 "syntax",
6576#endif
6577#if defined(USE_SYSTEM) || !defined(UNIX)
6578 "system",
6579#endif
6580#ifdef FEAT_TAG_BINS
6581 "tag_binary",
6582#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006583#ifdef FEAT_TCL
6584# ifndef DYNAMIC_TCL
6585 "tcl",
6586# endif
6587#endif
6588#ifdef FEAT_TERMGUICOLORS
6589 "termguicolors",
6590#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006591#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006592 "terminal",
6593#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006594#ifdef TERMINFO
6595 "terminfo",
6596#endif
6597#ifdef FEAT_TERMRESPONSE
6598 "termresponse",
6599#endif
6600#ifdef FEAT_TEXTOBJ
6601 "textobjects",
6602#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006603#ifdef FEAT_TEXT_PROP
6604 "textprop",
6605#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006606#ifdef HAVE_TGETENT
6607 "tgetent",
6608#endif
6609#ifdef FEAT_TIMERS
6610 "timers",
6611#endif
6612#ifdef FEAT_TITLE
6613 "title",
6614#endif
6615#ifdef FEAT_TOOLBAR
6616 "toolbar",
6617#endif
6618#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6619 "unnamedplus",
6620#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006621 "user-commands", /* was accidentally included in 5.4 */
6622 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006623#ifdef FEAT_VARTABS
6624 "vartabs",
6625#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006626 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006627#ifdef FEAT_VIMINFO
6628 "viminfo",
6629#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006630 "vimscript-1",
6631 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006632 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006633 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006634 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006635 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006636 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006637#ifdef FEAT_VTP
6638 "vtp",
6639#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006640#ifdef FEAT_WILDIGN
6641 "wildignore",
6642#endif
6643#ifdef FEAT_WILDMENU
6644 "wildmenu",
6645#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006646 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006647#ifdef FEAT_WAK
6648 "winaltkeys",
6649#endif
6650#ifdef FEAT_WRITEBACKUP
6651 "writebackup",
6652#endif
6653#ifdef FEAT_XIM
6654 "xim",
6655#endif
6656#ifdef FEAT_XFONTSET
6657 "xfontset",
6658#endif
6659#ifdef FEAT_XPM_W32
6660 "xpm",
6661 "xpm_w32", /* for backward compatibility */
6662#else
6663# if defined(HAVE_XPM)
6664 "xpm",
6665# endif
6666#endif
6667#ifdef USE_XSMP
6668 "xsmp",
6669#endif
6670#ifdef USE_XSMP_INTERACT
6671 "xsmp_interact",
6672#endif
6673#ifdef FEAT_XCLIPBOARD
6674 "xterm_clipboard",
6675#endif
6676#ifdef FEAT_XTERM_SAVE
6677 "xterm_save",
6678#endif
6679#if defined(UNIX) && defined(FEAT_X11)
6680 "X11",
6681#endif
6682 NULL
6683 };
6684
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006685 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006686 for (i = 0; has_list[i] != NULL; ++i)
6687 if (STRICMP(name, has_list[i]) == 0)
6688 {
6689 n = TRUE;
6690 break;
6691 }
6692
6693 if (n == FALSE)
6694 {
6695 if (STRNICMP(name, "patch", 5) == 0)
6696 {
6697 if (name[5] == '-'
6698 && STRLEN(name) >= 11
6699 && vim_isdigit(name[6])
6700 && vim_isdigit(name[8])
6701 && vim_isdigit(name[10]))
6702 {
6703 int major = atoi((char *)name + 6);
6704 int minor = atoi((char *)name + 8);
6705
6706 /* Expect "patch-9.9.01234". */
6707 n = (major < VIM_VERSION_MAJOR
6708 || (major == VIM_VERSION_MAJOR
6709 && (minor < VIM_VERSION_MINOR
6710 || (minor == VIM_VERSION_MINOR
6711 && has_patch(atoi((char *)name + 10))))));
6712 }
6713 else
6714 n = has_patch(atoi((char *)name + 5));
6715 }
6716 else if (STRICMP(name, "vim_starting") == 0)
6717 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006718 else if (STRICMP(name, "ttyin") == 0)
6719 n = mch_input_isatty();
6720 else if (STRICMP(name, "ttyout") == 0)
6721 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006722 else if (STRICMP(name, "multi_byte_encoding") == 0)
6723 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006724#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006725 else if (STRICMP(name, "balloon_multiline") == 0)
6726 n = multiline_balloon_available();
6727#endif
6728#ifdef DYNAMIC_TCL
6729 else if (STRICMP(name, "tcl") == 0)
6730 n = tcl_enabled(FALSE);
6731#endif
6732#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6733 else if (STRICMP(name, "iconv") == 0)
6734 n = iconv_enabled(FALSE);
6735#endif
6736#ifdef DYNAMIC_LUA
6737 else if (STRICMP(name, "lua") == 0)
6738 n = lua_enabled(FALSE);
6739#endif
6740#ifdef DYNAMIC_MZSCHEME
6741 else if (STRICMP(name, "mzscheme") == 0)
6742 n = mzscheme_enabled(FALSE);
6743#endif
6744#ifdef DYNAMIC_RUBY
6745 else if (STRICMP(name, "ruby") == 0)
6746 n = ruby_enabled(FALSE);
6747#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006748#ifdef DYNAMIC_PYTHON
6749 else if (STRICMP(name, "python") == 0)
6750 n = python_enabled(FALSE);
6751#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006752#ifdef DYNAMIC_PYTHON3
6753 else if (STRICMP(name, "python3") == 0)
6754 n = python3_enabled(FALSE);
6755#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006756#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6757 else if (STRICMP(name, "pythonx") == 0)
6758 {
6759# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6760 if (p_pyx == 0)
6761 n = python3_enabled(FALSE) || python_enabled(FALSE);
6762 else if (p_pyx == 3)
6763 n = python3_enabled(FALSE);
6764 else if (p_pyx == 2)
6765 n = python_enabled(FALSE);
6766# elif defined(DYNAMIC_PYTHON)
6767 n = python_enabled(FALSE);
6768# elif defined(DYNAMIC_PYTHON3)
6769 n = python3_enabled(FALSE);
6770# endif
6771 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006772#endif
6773#ifdef DYNAMIC_PERL
6774 else if (STRICMP(name, "perl") == 0)
6775 n = perl_enabled(FALSE);
6776#endif
6777#ifdef FEAT_GUI
6778 else if (STRICMP(name, "gui_running") == 0)
6779 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006780# ifdef FEAT_BROWSE
6781 else if (STRICMP(name, "browse") == 0)
6782 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6783# endif
6784#endif
6785#ifdef FEAT_SYN_HL
6786 else if (STRICMP(name, "syntax_items") == 0)
6787 n = syntax_present(curwin);
6788#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006789#ifdef FEAT_VTP
6790 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006791 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006792#endif
6793#ifdef FEAT_NETBEANS_INTG
6794 else if (STRICMP(name, "netbeans_enabled") == 0)
6795 n = netbeans_active();
6796#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006797#ifdef FEAT_MOUSE_GPM
6798 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6799 n = gpm_enabled();
6800#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006801#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006802 else if (STRICMP(name, "terminal") == 0)
6803 n = terminal_enabled();
6804#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006805#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006806 else if (STRICMP(name, "conpty") == 0)
6807 n = use_conpty();
6808#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006809 }
6810
6811 rettv->vval.v_number = n;
6812}
6813
6814/*
6815 * "has_key()" function
6816 */
6817 static void
6818f_has_key(typval_T *argvars, typval_T *rettv)
6819{
6820 if (argvars[0].v_type != VAR_DICT)
6821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006822 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006823 return;
6824 }
6825 if (argvars[0].vval.v_dict == NULL)
6826 return;
6827
6828 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006829 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006830}
6831
6832/*
6833 * "haslocaldir()" function
6834 */
6835 static void
6836f_haslocaldir(typval_T *argvars, typval_T *rettv)
6837{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006838 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006839 win_T *wp = NULL;
6840
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006841 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6842
6843 // Check for window-local and tab-local directories
6844 if (wp != NULL && wp->w_localdir != NULL)
6845 rettv->vval.v_number = 1;
6846 else if (tp != NULL && tp->tp_localdir != NULL)
6847 rettv->vval.v_number = 2;
6848 else
6849 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006850}
6851
6852/*
6853 * "hasmapto()" function
6854 */
6855 static void
6856f_hasmapto(typval_T *argvars, typval_T *rettv)
6857{
6858 char_u *name;
6859 char_u *mode;
6860 char_u buf[NUMBUFLEN];
6861 int abbr = FALSE;
6862
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006863 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006864 if (argvars[1].v_type == VAR_UNKNOWN)
6865 mode = (char_u *)"nvo";
6866 else
6867 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006868 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006869 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006870 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006871 }
6872
6873 if (map_to_exists(name, mode, abbr))
6874 rettv->vval.v_number = TRUE;
6875 else
6876 rettv->vval.v_number = FALSE;
6877}
6878
6879/*
6880 * "histadd()" function
6881 */
6882 static void
6883f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6884{
6885#ifdef FEAT_CMDHIST
6886 int histype;
6887 char_u *str;
6888 char_u buf[NUMBUFLEN];
6889#endif
6890
6891 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006892 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006893 return;
6894#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006895 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006896 histype = str != NULL ? get_histtype(str) : -1;
6897 if (histype >= 0)
6898 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006899 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 if (*str != NUL)
6901 {
6902 init_history();
6903 add_to_history(histype, str, FALSE, NUL);
6904 rettv->vval.v_number = TRUE;
6905 return;
6906 }
6907 }
6908#endif
6909}
6910
6911/*
6912 * "histdel()" function
6913 */
6914 static void
6915f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6916{
6917#ifdef FEAT_CMDHIST
6918 int n;
6919 char_u buf[NUMBUFLEN];
6920 char_u *str;
6921
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006922 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923 if (str == NULL)
6924 n = 0;
6925 else if (argvars[1].v_type == VAR_UNKNOWN)
6926 /* only one argument: clear entire history */
6927 n = clr_history(get_histtype(str));
6928 else if (argvars[1].v_type == VAR_NUMBER)
6929 /* index given: remove that entry */
6930 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006931 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006932 else
6933 /* string given: remove all matching entries */
6934 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006935 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006936 rettv->vval.v_number = n;
6937#endif
6938}
6939
6940/*
6941 * "histget()" function
6942 */
6943 static void
6944f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6945{
6946#ifdef FEAT_CMDHIST
6947 int type;
6948 int idx;
6949 char_u *str;
6950
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006951 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006952 if (str == NULL)
6953 rettv->vval.v_string = NULL;
6954 else
6955 {
6956 type = get_histtype(str);
6957 if (argvars[1].v_type == VAR_UNKNOWN)
6958 idx = get_history_idx(type);
6959 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006960 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006961 /* -1 on type error */
6962 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6963 }
6964#else
6965 rettv->vval.v_string = NULL;
6966#endif
6967 rettv->v_type = VAR_STRING;
6968}
6969
6970/*
6971 * "histnr()" function
6972 */
6973 static void
6974f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6975{
6976 int i;
6977
6978#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006979 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006980
6981 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6982 if (i >= HIST_CMD && i < HIST_COUNT)
6983 i = get_history_idx(i);
6984 else
6985#endif
6986 i = -1;
6987 rettv->vval.v_number = i;
6988}
6989
6990/*
6991 * "highlightID(name)" function
6992 */
6993 static void
6994f_hlID(typval_T *argvars, typval_T *rettv)
6995{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006996 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006997}
6998
6999/*
7000 * "highlight_exists()" function
7001 */
7002 static void
7003f_hlexists(typval_T *argvars, typval_T *rettv)
7004{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007005 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007006}
7007
7008/*
7009 * "hostname()" function
7010 */
7011 static void
7012f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7013{
7014 char_u hostname[256];
7015
7016 mch_get_host_name(hostname, 256);
7017 rettv->v_type = VAR_STRING;
7018 rettv->vval.v_string = vim_strsave(hostname);
7019}
7020
7021/*
7022 * iconv() function
7023 */
7024 static void
7025f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7026{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027 char_u buf1[NUMBUFLEN];
7028 char_u buf2[NUMBUFLEN];
7029 char_u *from, *to, *str;
7030 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007031
7032 rettv->v_type = VAR_STRING;
7033 rettv->vval.v_string = NULL;
7034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007035 str = tv_get_string(&argvars[0]);
7036 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7037 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007038 vimconv.vc_type = CONV_NONE;
7039 convert_setup(&vimconv, from, to);
7040
7041 /* If the encodings are equal, no conversion needed. */
7042 if (vimconv.vc_type == CONV_NONE)
7043 rettv->vval.v_string = vim_strsave(str);
7044 else
7045 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7046
7047 convert_setup(&vimconv, NULL, NULL);
7048 vim_free(from);
7049 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007050}
7051
7052/*
7053 * "indent()" function
7054 */
7055 static void
7056f_indent(typval_T *argvars, typval_T *rettv)
7057{
7058 linenr_T lnum;
7059
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007060 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007061 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7062 rettv->vval.v_number = get_indent_lnum(lnum);
7063 else
7064 rettv->vval.v_number = -1;
7065}
7066
7067/*
7068 * "index()" function
7069 */
7070 static void
7071f_index(typval_T *argvars, typval_T *rettv)
7072{
7073 list_T *l;
7074 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007075 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007076 long idx = 0;
7077 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007078 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007079
7080 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007081 if (argvars[0].v_type == VAR_BLOB)
7082 {
7083 typval_T tv;
7084 int start = 0;
7085
7086 if (argvars[2].v_type != VAR_UNKNOWN)
7087 {
7088 start = tv_get_number_chk(&argvars[2], &error);
7089 if (error)
7090 return;
7091 }
7092 b = argvars[0].vval.v_blob;
7093 if (b == NULL)
7094 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007095 if (start < 0)
7096 {
7097 start = blob_len(b) + start;
7098 if (start < 0)
7099 start = 0;
7100 }
7101
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007102 for (idx = start; idx < blob_len(b); ++idx)
7103 {
7104 tv.v_type = VAR_NUMBER;
7105 tv.vval.v_number = blob_get(b, idx);
7106 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7107 {
7108 rettv->vval.v_number = idx;
7109 return;
7110 }
7111 }
7112 return;
7113 }
7114 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007115 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007116 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007117 return;
7118 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007119
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007120 l = argvars[0].vval.v_list;
7121 if (l != NULL)
7122 {
7123 item = l->lv_first;
7124 if (argvars[2].v_type != VAR_UNKNOWN)
7125 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007126 /* Start at specified item. Use the cached index that list_find()
7127 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007128 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007129 idx = l->lv_idx;
7130 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007131 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007132 if (error)
7133 item = NULL;
7134 }
7135
7136 for ( ; item != NULL; item = item->li_next, ++idx)
7137 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7138 {
7139 rettv->vval.v_number = idx;
7140 break;
7141 }
7142 }
7143}
7144
7145static int inputsecret_flag = 0;
7146
7147/*
7148 * "input()" function
7149 * Also handles inputsecret() when inputsecret is set.
7150 */
7151 static void
7152f_input(typval_T *argvars, typval_T *rettv)
7153{
7154 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7155}
7156
7157/*
7158 * "inputdialog()" function
7159 */
7160 static void
7161f_inputdialog(typval_T *argvars, typval_T *rettv)
7162{
7163#if defined(FEAT_GUI_TEXTDIALOG)
7164 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7165 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7166 {
7167 char_u *message;
7168 char_u buf[NUMBUFLEN];
7169 char_u *defstr = (char_u *)"";
7170
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007171 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007172 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007173 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007174 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7175 else
7176 IObuff[0] = NUL;
7177 if (message != NULL && defstr != NULL
7178 && do_dialog(VIM_QUESTION, NULL, message,
7179 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7180 rettv->vval.v_string = vim_strsave(IObuff);
7181 else
7182 {
7183 if (message != NULL && defstr != NULL
7184 && argvars[1].v_type != VAR_UNKNOWN
7185 && argvars[2].v_type != VAR_UNKNOWN)
7186 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007187 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007188 else
7189 rettv->vval.v_string = NULL;
7190 }
7191 rettv->v_type = VAR_STRING;
7192 }
7193 else
7194#endif
7195 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7196}
7197
7198/*
7199 * "inputlist()" function
7200 */
7201 static void
7202f_inputlist(typval_T *argvars, typval_T *rettv)
7203{
7204 listitem_T *li;
7205 int selected;
7206 int mouse_used;
7207
7208#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007209 /* While starting up, there is no place to enter text. When running tests
7210 * with --not-a-term we assume feedkeys() will be used. */
7211 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007212 return;
7213#endif
7214 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007216 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007217 return;
7218 }
7219
7220 msg_start();
7221 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7222 lines_left = Rows; /* avoid more prompt */
7223 msg_scroll = TRUE;
7224 msg_clr_eos();
7225
7226 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7227 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007228 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007229 msg_putchar('\n');
7230 }
7231
7232 /* Ask for choice. */
7233 selected = prompt_for_number(&mouse_used);
7234 if (mouse_used)
7235 selected -= lines_left;
7236
7237 rettv->vval.v_number = selected;
7238}
7239
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007240static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7241
7242/*
7243 * "inputrestore()" function
7244 */
7245 static void
7246f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7247{
7248 if (ga_userinput.ga_len > 0)
7249 {
7250 --ga_userinput.ga_len;
7251 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7252 + ga_userinput.ga_len);
7253 /* default return is zero == OK */
7254 }
7255 else if (p_verbose > 1)
7256 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007257 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007258 rettv->vval.v_number = 1; /* Failed */
7259 }
7260}
7261
7262/*
7263 * "inputsave()" function
7264 */
7265 static void
7266f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7267{
7268 /* Add an entry to the stack of typeahead storage. */
7269 if (ga_grow(&ga_userinput, 1) == OK)
7270 {
7271 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7272 + ga_userinput.ga_len);
7273 ++ga_userinput.ga_len;
7274 /* default return is zero == OK */
7275 }
7276 else
7277 rettv->vval.v_number = 1; /* Failed */
7278}
7279
7280/*
7281 * "inputsecret()" function
7282 */
7283 static void
7284f_inputsecret(typval_T *argvars, typval_T *rettv)
7285{
7286 ++cmdline_star;
7287 ++inputsecret_flag;
7288 f_input(argvars, rettv);
7289 --cmdline_star;
7290 --inputsecret_flag;
7291}
7292
7293/*
7294 * "insert()" function
7295 */
7296 static void
7297f_insert(typval_T *argvars, typval_T *rettv)
7298{
7299 long before = 0;
7300 listitem_T *item;
7301 list_T *l;
7302 int error = FALSE;
7303
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007304 if (argvars[0].v_type == VAR_BLOB)
7305 {
7306 int val, len;
7307 char_u *p;
7308
7309 len = blob_len(argvars[0].vval.v_blob);
7310 if (argvars[2].v_type != VAR_UNKNOWN)
7311 {
7312 before = (long)tv_get_number_chk(&argvars[2], &error);
7313 if (error)
7314 return; // type error; errmsg already given
7315 if (before < 0 || before > len)
7316 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007317 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007318 return;
7319 }
7320 }
7321 val = tv_get_number_chk(&argvars[1], &error);
7322 if (error)
7323 return;
7324 if (val < 0 || val > 255)
7325 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007326 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007327 return;
7328 }
7329
7330 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7331 return;
7332 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7333 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7334 *(p + before) = val;
7335 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7336
7337 copy_tv(&argvars[0], rettv);
7338 }
7339 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007340 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007341 else if ((l = argvars[0].vval.v_list) != NULL
7342 && !var_check_lock(l->lv_lock,
7343 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007344 {
7345 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007346 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007347 if (error)
7348 return; /* type error; errmsg already given */
7349
7350 if (before == l->lv_len)
7351 item = NULL;
7352 else
7353 {
7354 item = list_find(l, before);
7355 if (item == NULL)
7356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007357 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007358 l = NULL;
7359 }
7360 }
7361 if (l != NULL)
7362 {
7363 list_insert_tv(l, &argvars[1], item);
7364 copy_tv(&argvars[0], rettv);
7365 }
7366 }
7367}
7368
7369/*
7370 * "invert(expr)" function
7371 */
7372 static void
7373f_invert(typval_T *argvars, typval_T *rettv)
7374{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007375 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007376}
7377
7378/*
7379 * "isdirectory()" function
7380 */
7381 static void
7382f_isdirectory(typval_T *argvars, typval_T *rettv)
7383{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007384 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007385}
7386
7387/*
7388 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7389 * or it refers to a List or Dictionary that is locked.
7390 */
7391 static int
7392tv_islocked(typval_T *tv)
7393{
7394 return (tv->v_lock & VAR_LOCKED)
7395 || (tv->v_type == VAR_LIST
7396 && tv->vval.v_list != NULL
7397 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7398 || (tv->v_type == VAR_DICT
7399 && tv->vval.v_dict != NULL
7400 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7401}
7402
7403/*
7404 * "islocked()" function
7405 */
7406 static void
7407f_islocked(typval_T *argvars, typval_T *rettv)
7408{
7409 lval_T lv;
7410 char_u *end;
7411 dictitem_T *di;
7412
7413 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007414 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007415 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007416 if (end != NULL && lv.ll_name != NULL)
7417 {
7418 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007419 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007420 else
7421 {
7422 if (lv.ll_tv == NULL)
7423 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007424 di = find_var(lv.ll_name, NULL, TRUE);
7425 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007426 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007427 /* Consider a variable locked when:
7428 * 1. the variable itself is locked
7429 * 2. the value of the variable is locked.
7430 * 3. the List or Dict value is locked.
7431 */
7432 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7433 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007434 }
7435 }
7436 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007437 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007438 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007439 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007440 else if (lv.ll_list != NULL)
7441 /* List item. */
7442 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7443 else
7444 /* Dictionary item. */
7445 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7446 }
7447 }
7448
7449 clear_lval(&lv);
7450}
7451
7452#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7453/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007454 * "isinf()" function
7455 */
7456 static void
7457f_isinf(typval_T *argvars, typval_T *rettv)
7458{
7459 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7460 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7461}
7462
7463/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007464 * "isnan()" function
7465 */
7466 static void
7467f_isnan(typval_T *argvars, typval_T *rettv)
7468{
7469 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7470 && isnan(argvars[0].vval.v_float);
7471}
7472#endif
7473
7474/*
7475 * "items(dict)" function
7476 */
7477 static void
7478f_items(typval_T *argvars, typval_T *rettv)
7479{
7480 dict_list(argvars, rettv, 2);
7481}
7482
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007483/*
7484 * "join()" function
7485 */
7486 static void
7487f_join(typval_T *argvars, typval_T *rettv)
7488{
7489 garray_T ga;
7490 char_u *sep;
7491
7492 if (argvars[0].v_type != VAR_LIST)
7493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007494 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007495 return;
7496 }
7497 if (argvars[0].vval.v_list == NULL)
7498 return;
7499 if (argvars[1].v_type == VAR_UNKNOWN)
7500 sep = (char_u *)" ";
7501 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007502 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007503
7504 rettv->v_type = VAR_STRING;
7505
7506 if (sep != NULL)
7507 {
7508 ga_init2(&ga, (int)sizeof(char), 80);
7509 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7510 ga_append(&ga, NUL);
7511 rettv->vval.v_string = (char_u *)ga.ga_data;
7512 }
7513 else
7514 rettv->vval.v_string = NULL;
7515}
7516
7517/*
7518 * "js_decode()" function
7519 */
7520 static void
7521f_js_decode(typval_T *argvars, typval_T *rettv)
7522{
7523 js_read_T reader;
7524
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007525 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007526 reader.js_fill = NULL;
7527 reader.js_used = 0;
7528 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007529 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007530}
7531
7532/*
7533 * "js_encode()" function
7534 */
7535 static void
7536f_js_encode(typval_T *argvars, typval_T *rettv)
7537{
7538 rettv->v_type = VAR_STRING;
7539 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7540}
7541
7542/*
7543 * "json_decode()" function
7544 */
7545 static void
7546f_json_decode(typval_T *argvars, typval_T *rettv)
7547{
7548 js_read_T reader;
7549
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007550 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007551 reader.js_fill = NULL;
7552 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007553 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007554}
7555
7556/*
7557 * "json_encode()" function
7558 */
7559 static void
7560f_json_encode(typval_T *argvars, typval_T *rettv)
7561{
7562 rettv->v_type = VAR_STRING;
7563 rettv->vval.v_string = json_encode(&argvars[0], 0);
7564}
7565
7566/*
7567 * "keys()" function
7568 */
7569 static void
7570f_keys(typval_T *argvars, typval_T *rettv)
7571{
7572 dict_list(argvars, rettv, 0);
7573}
7574
7575/*
7576 * "last_buffer_nr()" function.
7577 */
7578 static void
7579f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7580{
7581 int n = 0;
7582 buf_T *buf;
7583
Bram Moolenaar29323592016-07-24 22:04:11 +02007584 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007585 if (n < buf->b_fnum)
7586 n = buf->b_fnum;
7587
7588 rettv->vval.v_number = n;
7589}
7590
7591/*
7592 * "len()" function
7593 */
7594 static void
7595f_len(typval_T *argvars, typval_T *rettv)
7596{
7597 switch (argvars[0].v_type)
7598 {
7599 case VAR_STRING:
7600 case VAR_NUMBER:
7601 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007602 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007604 case VAR_BLOB:
7605 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7606 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007607 case VAR_LIST:
7608 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7609 break;
7610 case VAR_DICT:
7611 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7612 break;
7613 case VAR_UNKNOWN:
7614 case VAR_SPECIAL:
7615 case VAR_FLOAT:
7616 case VAR_FUNC:
7617 case VAR_PARTIAL:
7618 case VAR_JOB:
7619 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007620 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007621 break;
7622 }
7623}
7624
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007625 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007626libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007627{
7628#ifdef FEAT_LIBCALL
7629 char_u *string_in;
7630 char_u **string_result;
7631 int nr_result;
7632#endif
7633
7634 rettv->v_type = type;
7635 if (type != VAR_NUMBER)
7636 rettv->vval.v_string = NULL;
7637
7638 if (check_restricted() || check_secure())
7639 return;
7640
7641#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007642 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007643 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7644 {
7645 string_in = NULL;
7646 if (argvars[2].v_type == VAR_STRING)
7647 string_in = argvars[2].vval.v_string;
7648 if (type == VAR_NUMBER)
7649 string_result = NULL;
7650 else
7651 string_result = &rettv->vval.v_string;
7652 if (mch_libcall(argvars[0].vval.v_string,
7653 argvars[1].vval.v_string,
7654 string_in,
7655 argvars[2].vval.v_number,
7656 string_result,
7657 &nr_result) == OK
7658 && type == VAR_NUMBER)
7659 rettv->vval.v_number = nr_result;
7660 }
7661#endif
7662}
7663
7664/*
7665 * "libcall()" function
7666 */
7667 static void
7668f_libcall(typval_T *argvars, typval_T *rettv)
7669{
7670 libcall_common(argvars, rettv, VAR_STRING);
7671}
7672
7673/*
7674 * "libcallnr()" function
7675 */
7676 static void
7677f_libcallnr(typval_T *argvars, typval_T *rettv)
7678{
7679 libcall_common(argvars, rettv, VAR_NUMBER);
7680}
7681
7682/*
7683 * "line(string)" function
7684 */
7685 static void
7686f_line(typval_T *argvars, typval_T *rettv)
7687{
7688 linenr_T lnum = 0;
7689 pos_T *fp;
7690 int fnum;
7691
7692 fp = var2fpos(&argvars[0], TRUE, &fnum);
7693 if (fp != NULL)
7694 lnum = fp->lnum;
7695 rettv->vval.v_number = lnum;
7696}
7697
7698/*
7699 * "line2byte(lnum)" function
7700 */
7701 static void
7702f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7703{
7704#ifndef FEAT_BYTEOFF
7705 rettv->vval.v_number = -1;
7706#else
7707 linenr_T lnum;
7708
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007709 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007710 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7711 rettv->vval.v_number = -1;
7712 else
7713 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7714 if (rettv->vval.v_number >= 0)
7715 ++rettv->vval.v_number;
7716#endif
7717}
7718
7719/*
7720 * "lispindent(lnum)" function
7721 */
7722 static void
7723f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7724{
7725#ifdef FEAT_LISP
7726 pos_T pos;
7727 linenr_T lnum;
7728
7729 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007730 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007731 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7732 {
7733 curwin->w_cursor.lnum = lnum;
7734 rettv->vval.v_number = get_lisp_indent();
7735 curwin->w_cursor = pos;
7736 }
7737 else
7738#endif
7739 rettv->vval.v_number = -1;
7740}
7741
7742/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007743 * "list2str()" function
7744 */
7745 static void
7746f_list2str(typval_T *argvars, typval_T *rettv)
7747{
7748 list_T *l;
7749 listitem_T *li;
7750 garray_T ga;
7751 int utf8 = FALSE;
7752
7753 rettv->v_type = VAR_STRING;
7754 rettv->vval.v_string = NULL;
7755 if (argvars[0].v_type != VAR_LIST)
7756 {
7757 emsg(_(e_invarg));
7758 return;
7759 }
7760
7761 l = argvars[0].vval.v_list;
7762 if (l == NULL)
7763 return; // empty list results in empty string
7764
7765 if (argvars[1].v_type != VAR_UNKNOWN)
7766 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7767
7768 ga_init2(&ga, 1, 80);
7769 if (has_mbyte || utf8)
7770 {
7771 char_u buf[MB_MAXBYTES + 1];
7772 int (*char2bytes)(int, char_u *);
7773
7774 if (utf8 || enc_utf8)
7775 char2bytes = utf_char2bytes;
7776 else
7777 char2bytes = mb_char2bytes;
7778
7779 for (li = l->lv_first; li != NULL; li = li->li_next)
7780 {
7781 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7782 ga_concat(&ga, buf);
7783 }
7784 ga_append(&ga, NUL);
7785 }
7786 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7787 {
7788 for (li = l->lv_first; li != NULL; li = li->li_next)
7789 ga_append(&ga, tv_get_number(&li->li_tv));
7790 ga_append(&ga, NUL);
7791 }
7792
7793 rettv->v_type = VAR_STRING;
7794 rettv->vval.v_string = ga.ga_data;
7795}
7796
7797/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007798 * "localtime()" function
7799 */
7800 static void
7801f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7802{
7803 rettv->vval.v_number = (varnumber_T)time(NULL);
7804}
7805
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007806 static void
7807get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7808{
7809 char_u *keys;
7810 char_u *which;
7811 char_u buf[NUMBUFLEN];
7812 char_u *keys_buf = NULL;
7813 char_u *rhs;
7814 int mode;
7815 int abbr = FALSE;
7816 int get_dict = FALSE;
7817 mapblock_T *mp;
7818 int buffer_local;
7819
7820 /* return empty string for failure */
7821 rettv->v_type = VAR_STRING;
7822 rettv->vval.v_string = NULL;
7823
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007824 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007825 if (*keys == NUL)
7826 return;
7827
7828 if (argvars[1].v_type != VAR_UNKNOWN)
7829 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007830 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007831 if (argvars[2].v_type != VAR_UNKNOWN)
7832 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007833 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007834 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007835 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007836 }
7837 }
7838 else
7839 which = (char_u *)"";
7840 if (which == NULL)
7841 return;
7842
7843 mode = get_map_mode(&which, 0);
7844
7845 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7846 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7847 vim_free(keys_buf);
7848
7849 if (!get_dict)
7850 {
7851 /* Return a string. */
7852 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007853 {
7854 if (*rhs == NUL)
7855 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7856 else
7857 rettv->vval.v_string = str2special_save(rhs, FALSE);
7858 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007859
7860 }
7861 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7862 {
7863 /* Return a dictionary. */
7864 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7865 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7866 dict_T *dict = rettv->vval.v_dict;
7867
Bram Moolenaare0be1672018-07-08 16:50:37 +02007868 dict_add_string(dict, "lhs", lhs);
7869 dict_add_string(dict, "rhs", mp->m_orig_str);
7870 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7871 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7872 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007873 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7874 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007875 dict_add_number(dict, "buffer", (long)buffer_local);
7876 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7877 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007878
7879 vim_free(lhs);
7880 vim_free(mapmode);
7881 }
7882}
7883
7884#ifdef FEAT_FLOAT
7885/*
7886 * "log()" function
7887 */
7888 static void
7889f_log(typval_T *argvars, typval_T *rettv)
7890{
7891 float_T f = 0.0;
7892
7893 rettv->v_type = VAR_FLOAT;
7894 if (get_float_arg(argvars, &f) == OK)
7895 rettv->vval.v_float = log(f);
7896 else
7897 rettv->vval.v_float = 0.0;
7898}
7899
7900/*
7901 * "log10()" function
7902 */
7903 static void
7904f_log10(typval_T *argvars, typval_T *rettv)
7905{
7906 float_T f = 0.0;
7907
7908 rettv->v_type = VAR_FLOAT;
7909 if (get_float_arg(argvars, &f) == OK)
7910 rettv->vval.v_float = log10(f);
7911 else
7912 rettv->vval.v_float = 0.0;
7913}
7914#endif
7915
7916#ifdef FEAT_LUA
7917/*
7918 * "luaeval()" function
7919 */
7920 static void
7921f_luaeval(typval_T *argvars, typval_T *rettv)
7922{
7923 char_u *str;
7924 char_u buf[NUMBUFLEN];
7925
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007926 if (check_restricted() || check_secure())
7927 return;
7928
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007929 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007930 do_luaeval(str, argvars + 1, rettv);
7931}
7932#endif
7933
7934/*
7935 * "map()" function
7936 */
7937 static void
7938f_map(typval_T *argvars, typval_T *rettv)
7939{
7940 filter_map(argvars, rettv, TRUE);
7941}
7942
7943/*
7944 * "maparg()" function
7945 */
7946 static void
7947f_maparg(typval_T *argvars, typval_T *rettv)
7948{
7949 get_maparg(argvars, rettv, TRUE);
7950}
7951
7952/*
7953 * "mapcheck()" function
7954 */
7955 static void
7956f_mapcheck(typval_T *argvars, typval_T *rettv)
7957{
7958 get_maparg(argvars, rettv, FALSE);
7959}
7960
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007961typedef enum
7962{
7963 MATCH_END, /* matchend() */
7964 MATCH_MATCH, /* match() */
7965 MATCH_STR, /* matchstr() */
7966 MATCH_LIST, /* matchlist() */
7967 MATCH_POS /* matchstrpos() */
7968} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007969
7970 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007971find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007972{
7973 char_u *str = NULL;
7974 long len = 0;
7975 char_u *expr = NULL;
7976 char_u *pat;
7977 regmatch_T regmatch;
7978 char_u patbuf[NUMBUFLEN];
7979 char_u strbuf[NUMBUFLEN];
7980 char_u *save_cpo;
7981 long start = 0;
7982 long nth = 1;
7983 colnr_T startcol = 0;
7984 int match = 0;
7985 list_T *l = NULL;
7986 listitem_T *li = NULL;
7987 long idx = 0;
7988 char_u *tofree = NULL;
7989
7990 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7991 save_cpo = p_cpo;
7992 p_cpo = (char_u *)"";
7993
7994 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007995 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007996 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007997 /* type MATCH_LIST: return empty list when there are no matches.
7998 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007999 if (rettv_list_alloc(rettv) == FAIL)
8000 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008001 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008002 && (list_append_string(rettv->vval.v_list,
8003 (char_u *)"", 0) == FAIL
8004 || list_append_number(rettv->vval.v_list,
8005 (varnumber_T)-1) == FAIL
8006 || list_append_number(rettv->vval.v_list,
8007 (varnumber_T)-1) == FAIL
8008 || list_append_number(rettv->vval.v_list,
8009 (varnumber_T)-1) == FAIL))
8010 {
8011 list_free(rettv->vval.v_list);
8012 rettv->vval.v_list = NULL;
8013 goto theend;
8014 }
8015 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008016 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008017 {
8018 rettv->v_type = VAR_STRING;
8019 rettv->vval.v_string = NULL;
8020 }
8021
8022 if (argvars[0].v_type == VAR_LIST)
8023 {
8024 if ((l = argvars[0].vval.v_list) == NULL)
8025 goto theend;
8026 li = l->lv_first;
8027 }
8028 else
8029 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008030 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008031 len = (long)STRLEN(str);
8032 }
8033
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008034 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008035 if (pat == NULL)
8036 goto theend;
8037
8038 if (argvars[2].v_type != VAR_UNKNOWN)
8039 {
8040 int error = FALSE;
8041
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008042 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008043 if (error)
8044 goto theend;
8045 if (l != NULL)
8046 {
8047 li = list_find(l, start);
8048 if (li == NULL)
8049 goto theend;
8050 idx = l->lv_idx; /* use the cached index */
8051 }
8052 else
8053 {
8054 if (start < 0)
8055 start = 0;
8056 if (start > len)
8057 goto theend;
8058 /* When "count" argument is there ignore matches before "start",
8059 * otherwise skip part of the string. Differs when pattern is "^"
8060 * or "\<". */
8061 if (argvars[3].v_type != VAR_UNKNOWN)
8062 startcol = start;
8063 else
8064 {
8065 str += start;
8066 len -= start;
8067 }
8068 }
8069
8070 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008071 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008072 if (error)
8073 goto theend;
8074 }
8075
8076 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8077 if (regmatch.regprog != NULL)
8078 {
8079 regmatch.rm_ic = p_ic;
8080
8081 for (;;)
8082 {
8083 if (l != NULL)
8084 {
8085 if (li == NULL)
8086 {
8087 match = FALSE;
8088 break;
8089 }
8090 vim_free(tofree);
8091 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8092 if (str == NULL)
8093 break;
8094 }
8095
8096 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8097
8098 if (match && --nth <= 0)
8099 break;
8100 if (l == NULL && !match)
8101 break;
8102
8103 /* Advance to just after the match. */
8104 if (l != NULL)
8105 {
8106 li = li->li_next;
8107 ++idx;
8108 }
8109 else
8110 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008111 startcol = (colnr_T)(regmatch.startp[0]
8112 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008113 if (startcol > (colnr_T)len
8114 || str + startcol <= regmatch.startp[0])
8115 {
8116 match = FALSE;
8117 break;
8118 }
8119 }
8120 }
8121
8122 if (match)
8123 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008124 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125 {
8126 listitem_T *li1 = rettv->vval.v_list->lv_first;
8127 listitem_T *li2 = li1->li_next;
8128 listitem_T *li3 = li2->li_next;
8129 listitem_T *li4 = li3->li_next;
8130
8131 vim_free(li1->li_tv.vval.v_string);
8132 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8133 (int)(regmatch.endp[0] - regmatch.startp[0]));
8134 li3->li_tv.vval.v_number =
8135 (varnumber_T)(regmatch.startp[0] - expr);
8136 li4->li_tv.vval.v_number =
8137 (varnumber_T)(regmatch.endp[0] - expr);
8138 if (l != NULL)
8139 li2->li_tv.vval.v_number = (varnumber_T)idx;
8140 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008141 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008142 {
8143 int i;
8144
8145 /* return list with matched string and submatches */
8146 for (i = 0; i < NSUBEXP; ++i)
8147 {
8148 if (regmatch.endp[i] == NULL)
8149 {
8150 if (list_append_string(rettv->vval.v_list,
8151 (char_u *)"", 0) == FAIL)
8152 break;
8153 }
8154 else if (list_append_string(rettv->vval.v_list,
8155 regmatch.startp[i],
8156 (int)(regmatch.endp[i] - regmatch.startp[i]))
8157 == FAIL)
8158 break;
8159 }
8160 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008161 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008162 {
8163 /* return matched string */
8164 if (l != NULL)
8165 copy_tv(&li->li_tv, rettv);
8166 else
8167 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8168 (int)(regmatch.endp[0] - regmatch.startp[0]));
8169 }
8170 else if (l != NULL)
8171 rettv->vval.v_number = idx;
8172 else
8173 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008174 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008175 rettv->vval.v_number =
8176 (varnumber_T)(regmatch.startp[0] - str);
8177 else
8178 rettv->vval.v_number =
8179 (varnumber_T)(regmatch.endp[0] - str);
8180 rettv->vval.v_number += (varnumber_T)(str - expr);
8181 }
8182 }
8183 vim_regfree(regmatch.regprog);
8184 }
8185
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008186theend:
8187 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008188 /* matchstrpos() without a list: drop the second item. */
8189 listitem_remove(rettv->vval.v_list,
8190 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008191 vim_free(tofree);
8192 p_cpo = save_cpo;
8193}
8194
8195/*
8196 * "match()" function
8197 */
8198 static void
8199f_match(typval_T *argvars, typval_T *rettv)
8200{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008201 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008202}
8203
Bram Moolenaar95e51472018-07-28 16:55:56 +02008204#ifdef FEAT_SEARCH_EXTRA
8205 static int
8206matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8207{
8208 dictitem_T *di;
8209
8210 if (tv->v_type != VAR_DICT)
8211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008212 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008213 return FAIL;
8214 }
8215
8216 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008217 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008218 (char_u *)"conceal", FALSE);
8219
8220 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8221 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008222 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008223 if (*win == NULL)
8224 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008225 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008226 return FAIL;
8227 }
8228 }
8229
8230 return OK;
8231}
8232#endif
8233
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008234/*
8235 * "matchadd()" function
8236 */
8237 static void
8238f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8239{
8240#ifdef FEAT_SEARCH_EXTRA
8241 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008242 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8243 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008244 int prio = 10; /* default priority */
8245 int id = -1;
8246 int error = FALSE;
8247 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008248 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008249
8250 rettv->vval.v_number = -1;
8251
8252 if (grp == NULL || pat == NULL)
8253 return;
8254 if (argvars[2].v_type != VAR_UNKNOWN)
8255 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008256 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008257 if (argvars[3].v_type != VAR_UNKNOWN)
8258 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008259 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008260 if (argvars[4].v_type != VAR_UNKNOWN
8261 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8262 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008263 }
8264 }
8265 if (error == TRUE)
8266 return;
8267 if (id >= 1 && id <= 3)
8268 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008269 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008270 return;
8271 }
8272
Bram Moolenaar95e51472018-07-28 16:55:56 +02008273 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008274 conceal_char);
8275#endif
8276}
8277
8278/*
8279 * "matchaddpos()" function
8280 */
8281 static void
8282f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8283{
8284#ifdef FEAT_SEARCH_EXTRA
8285 char_u buf[NUMBUFLEN];
8286 char_u *group;
8287 int prio = 10;
8288 int id = -1;
8289 int error = FALSE;
8290 list_T *l;
8291 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008292 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293
8294 rettv->vval.v_number = -1;
8295
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008296 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008297 if (group == NULL)
8298 return;
8299
8300 if (argvars[1].v_type != VAR_LIST)
8301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008302 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008303 return;
8304 }
8305 l = argvars[1].vval.v_list;
8306 if (l == NULL)
8307 return;
8308
8309 if (argvars[2].v_type != VAR_UNKNOWN)
8310 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008311 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008312 if (argvars[3].v_type != VAR_UNKNOWN)
8313 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008314 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008315
8316 if (argvars[4].v_type != VAR_UNKNOWN
8317 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8318 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008319 }
8320 }
8321 if (error == TRUE)
8322 return;
8323
8324 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8325 if (id == 1 || id == 2)
8326 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008327 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 return;
8329 }
8330
Bram Moolenaar95e51472018-07-28 16:55:56 +02008331 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008332 conceal_char);
8333#endif
8334}
8335
8336/*
8337 * "matcharg()" function
8338 */
8339 static void
8340f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8341{
8342 if (rettv_list_alloc(rettv) == OK)
8343 {
8344#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008345 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008346 matchitem_T *m;
8347
8348 if (id >= 1 && id <= 3)
8349 {
8350 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8351 {
8352 list_append_string(rettv->vval.v_list,
8353 syn_id2name(m->hlg_id), -1);
8354 list_append_string(rettv->vval.v_list, m->pattern, -1);
8355 }
8356 else
8357 {
8358 list_append_string(rettv->vval.v_list, NULL, -1);
8359 list_append_string(rettv->vval.v_list, NULL, -1);
8360 }
8361 }
8362#endif
8363 }
8364}
8365
8366/*
8367 * "matchdelete()" function
8368 */
8369 static void
8370f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8371{
8372#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008373 win_T *win = get_optional_window(argvars, 1);
8374
8375 if (win == NULL)
8376 rettv->vval.v_number = -1;
8377 else
8378 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008379 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008380#endif
8381}
8382
8383/*
8384 * "matchend()" function
8385 */
8386 static void
8387f_matchend(typval_T *argvars, typval_T *rettv)
8388{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008389 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008390}
8391
8392/*
8393 * "matchlist()" function
8394 */
8395 static void
8396f_matchlist(typval_T *argvars, typval_T *rettv)
8397{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008398 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008399}
8400
8401/*
8402 * "matchstr()" function
8403 */
8404 static void
8405f_matchstr(typval_T *argvars, typval_T *rettv)
8406{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008407 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408}
8409
8410/*
8411 * "matchstrpos()" function
8412 */
8413 static void
8414f_matchstrpos(typval_T *argvars, typval_T *rettv)
8415{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008416 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008417}
8418
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008419 static void
8420max_min(typval_T *argvars, typval_T *rettv, int domax)
8421{
8422 varnumber_T n = 0;
8423 varnumber_T i;
8424 int error = FALSE;
8425
8426 if (argvars[0].v_type == VAR_LIST)
8427 {
8428 list_T *l;
8429 listitem_T *li;
8430
8431 l = argvars[0].vval.v_list;
8432 if (l != NULL)
8433 {
8434 li = l->lv_first;
8435 if (li != NULL)
8436 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008437 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438 for (;;)
8439 {
8440 li = li->li_next;
8441 if (li == NULL)
8442 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008443 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008444 if (domax ? i > n : i < n)
8445 n = i;
8446 }
8447 }
8448 }
8449 }
8450 else if (argvars[0].v_type == VAR_DICT)
8451 {
8452 dict_T *d;
8453 int first = TRUE;
8454 hashitem_T *hi;
8455 int todo;
8456
8457 d = argvars[0].vval.v_dict;
8458 if (d != NULL)
8459 {
8460 todo = (int)d->dv_hashtab.ht_used;
8461 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8462 {
8463 if (!HASHITEM_EMPTY(hi))
8464 {
8465 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008466 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008467 if (first)
8468 {
8469 n = i;
8470 first = FALSE;
8471 }
8472 else if (domax ? i > n : i < n)
8473 n = i;
8474 }
8475 }
8476 }
8477 }
8478 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008479 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008480 rettv->vval.v_number = error ? 0 : n;
8481}
8482
8483/*
8484 * "max()" function
8485 */
8486 static void
8487f_max(typval_T *argvars, typval_T *rettv)
8488{
8489 max_min(argvars, rettv, TRUE);
8490}
8491
8492/*
8493 * "min()" function
8494 */
8495 static void
8496f_min(typval_T *argvars, typval_T *rettv)
8497{
8498 max_min(argvars, rettv, FALSE);
8499}
8500
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008501/*
8502 * Create the directory in which "dir" is located, and higher levels when
8503 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008504 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008505 */
8506 static int
8507mkdir_recurse(char_u *dir, int prot)
8508{
8509 char_u *p;
8510 char_u *updir;
8511 int r = FAIL;
8512
8513 /* Get end of directory name in "dir".
8514 * We're done when it's "/" or "c:/". */
8515 p = gettail_sep(dir);
8516 if (p <= get_past_head(dir))
8517 return OK;
8518
8519 /* If the directory exists we're done. Otherwise: create it.*/
8520 updir = vim_strnsave(dir, (int)(p - dir));
8521 if (updir == NULL)
8522 return FAIL;
8523 if (mch_isdir(updir))
8524 r = OK;
8525 else if (mkdir_recurse(updir, prot) == OK)
8526 r = vim_mkdir_emsg(updir, prot);
8527 vim_free(updir);
8528 return r;
8529}
8530
8531#ifdef vim_mkdir
8532/*
8533 * "mkdir()" function
8534 */
8535 static void
8536f_mkdir(typval_T *argvars, typval_T *rettv)
8537{
8538 char_u *dir;
8539 char_u buf[NUMBUFLEN];
8540 int prot = 0755;
8541
8542 rettv->vval.v_number = FAIL;
8543 if (check_restricted() || check_secure())
8544 return;
8545
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008546 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008548 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008549
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008550 if (*gettail(dir) == NUL)
8551 /* remove trailing slashes */
8552 *gettail_sep(dir) = NUL;
8553
8554 if (argvars[1].v_type != VAR_UNKNOWN)
8555 {
8556 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008557 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008558 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008559 if (prot == -1)
8560 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008562 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008563 {
8564 if (mch_isdir(dir))
8565 {
8566 /* With the "p" flag it's OK if the dir already exists. */
8567 rettv->vval.v_number = OK;
8568 return;
8569 }
8570 mkdir_recurse(dir, prot);
8571 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008572 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008573 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574}
8575#endif
8576
8577/*
8578 * "mode()" function
8579 */
8580 static void
8581f_mode(typval_T *argvars, typval_T *rettv)
8582{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008583 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008584
Bram Moolenaar612cc382018-07-29 15:34:26 +02008585 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008586
8587 if (time_for_testing == 93784)
8588 {
8589 /* Testing the two-character code. */
8590 buf[0] = 'x';
8591 buf[1] = '!';
8592 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008593#ifdef FEAT_TERMINAL
8594 else if (term_use_loop())
8595 buf[0] = 't';
8596#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008597 else if (VIsual_active)
8598 {
8599 if (VIsual_select)
8600 buf[0] = VIsual_mode + 's' - 'v';
8601 else
8602 buf[0] = VIsual_mode;
8603 }
8604 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8605 || State == CONFIRM)
8606 {
8607 buf[0] = 'r';
8608 if (State == ASKMORE)
8609 buf[1] = 'm';
8610 else if (State == CONFIRM)
8611 buf[1] = '?';
8612 }
8613 else if (State == EXTERNCMD)
8614 buf[0] = '!';
8615 else if (State & INSERT)
8616 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008617 if (State & VREPLACE_FLAG)
8618 {
8619 buf[0] = 'R';
8620 buf[1] = 'v';
8621 }
8622 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008623 {
8624 if (State & REPLACE_FLAG)
8625 buf[0] = 'R';
8626 else
8627 buf[0] = 'i';
8628#ifdef FEAT_INS_EXPAND
8629 if (ins_compl_active())
8630 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008631 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008632 buf[1] = 'x';
8633#endif
8634 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008635 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008636 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008637 {
8638 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008639 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008640 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008641 else if (exmode_active == EXMODE_NORMAL)
8642 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008643 }
8644 else
8645 {
8646 buf[0] = 'n';
8647 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008648 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008649 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008650 // to be able to detect force-linewise/blockwise/characterwise operations
8651 buf[2] = motion_force;
8652 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008653 else if (restart_edit == 'I' || restart_edit == 'R'
8654 || restart_edit == 'V')
8655 {
8656 buf[1] = 'i';
8657 buf[2] = restart_edit;
8658 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659 }
8660
8661 /* Clear out the minor mode when the argument is not a non-zero number or
8662 * non-empty string. */
8663 if (!non_zero_arg(&argvars[0]))
8664 buf[1] = NUL;
8665
8666 rettv->vval.v_string = vim_strsave(buf);
8667 rettv->v_type = VAR_STRING;
8668}
8669
8670#if defined(FEAT_MZSCHEME) || defined(PROTO)
8671/*
8672 * "mzeval()" function
8673 */
8674 static void
8675f_mzeval(typval_T *argvars, typval_T *rettv)
8676{
8677 char_u *str;
8678 char_u buf[NUMBUFLEN];
8679
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008680 if (check_restricted() || check_secure())
8681 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008682 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008683 do_mzeval(str, rettv);
8684}
8685
8686 void
8687mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8688{
8689 typval_T argvars[3];
8690
8691 argvars[0].v_type = VAR_STRING;
8692 argvars[0].vval.v_string = name;
8693 copy_tv(args, &argvars[1]);
8694 argvars[2].v_type = VAR_UNKNOWN;
8695 f_call(argvars, rettv);
8696 clear_tv(&argvars[1]);
8697}
8698#endif
8699
8700/*
8701 * "nextnonblank()" function
8702 */
8703 static void
8704f_nextnonblank(typval_T *argvars, typval_T *rettv)
8705{
8706 linenr_T lnum;
8707
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008708 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709 {
8710 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8711 {
8712 lnum = 0;
8713 break;
8714 }
8715 if (*skipwhite(ml_get(lnum)) != NUL)
8716 break;
8717 }
8718 rettv->vval.v_number = lnum;
8719}
8720
8721/*
8722 * "nr2char()" function
8723 */
8724 static void
8725f_nr2char(typval_T *argvars, typval_T *rettv)
8726{
8727 char_u buf[NUMBUFLEN];
8728
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008729 if (has_mbyte)
8730 {
8731 int utf8 = 0;
8732
8733 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008734 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008736 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008737 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008738 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008739 }
8740 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008741 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008742 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008743 buf[1] = NUL;
8744 }
8745 rettv->v_type = VAR_STRING;
8746 rettv->vval.v_string = vim_strsave(buf);
8747}
8748
8749/*
8750 * "or(expr, expr)" function
8751 */
8752 static void
8753f_or(typval_T *argvars, typval_T *rettv)
8754{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008755 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8756 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757}
8758
8759/*
8760 * "pathshorten()" function
8761 */
8762 static void
8763f_pathshorten(typval_T *argvars, typval_T *rettv)
8764{
8765 char_u *p;
8766
8767 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008768 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008769 if (p == NULL)
8770 rettv->vval.v_string = NULL;
8771 else
8772 {
8773 p = vim_strsave(p);
8774 rettv->vval.v_string = p;
8775 if (p != NULL)
8776 shorten_dir(p);
8777 }
8778}
8779
8780#ifdef FEAT_PERL
8781/*
8782 * "perleval()" function
8783 */
8784 static void
8785f_perleval(typval_T *argvars, typval_T *rettv)
8786{
8787 char_u *str;
8788 char_u buf[NUMBUFLEN];
8789
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008790 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008791 do_perleval(str, rettv);
8792}
8793#endif
8794
8795#ifdef FEAT_FLOAT
8796/*
8797 * "pow()" function
8798 */
8799 static void
8800f_pow(typval_T *argvars, typval_T *rettv)
8801{
8802 float_T fx = 0.0, fy = 0.0;
8803
8804 rettv->v_type = VAR_FLOAT;
8805 if (get_float_arg(argvars, &fx) == OK
8806 && get_float_arg(&argvars[1], &fy) == OK)
8807 rettv->vval.v_float = pow(fx, fy);
8808 else
8809 rettv->vval.v_float = 0.0;
8810}
8811#endif
8812
8813/*
8814 * "prevnonblank()" function
8815 */
8816 static void
8817f_prevnonblank(typval_T *argvars, typval_T *rettv)
8818{
8819 linenr_T lnum;
8820
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008821 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008822 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8823 lnum = 0;
8824 else
8825 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8826 --lnum;
8827 rettv->vval.v_number = lnum;
8828}
8829
8830/* This dummy va_list is here because:
8831 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8832 * - locally in the function results in a "used before set" warning
8833 * - using va_start() to initialize it gives "function with fixed args" error */
8834static va_list ap;
8835
8836/*
8837 * "printf()" function
8838 */
8839 static void
8840f_printf(typval_T *argvars, typval_T *rettv)
8841{
8842 char_u buf[NUMBUFLEN];
8843 int len;
8844 char_u *s;
8845 int saved_did_emsg = did_emsg;
8846 char *fmt;
8847
8848 rettv->v_type = VAR_STRING;
8849 rettv->vval.v_string = NULL;
8850
8851 /* Get the required length, allocate the buffer and do it for real. */
8852 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008853 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008854 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008855 if (!did_emsg)
8856 {
8857 s = alloc(len + 1);
8858 if (s != NULL)
8859 {
8860 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008861 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8862 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008863 }
8864 }
8865 did_emsg |= saved_did_emsg;
8866}
8867
8868/*
8869 * "pumvisible()" function
8870 */
8871 static void
8872f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8873{
8874#ifdef FEAT_INS_EXPAND
8875 if (pum_visible())
8876 rettv->vval.v_number = 1;
8877#endif
8878}
8879
8880#ifdef FEAT_PYTHON3
8881/*
8882 * "py3eval()" function
8883 */
8884 static void
8885f_py3eval(typval_T *argvars, typval_T *rettv)
8886{
8887 char_u *str;
8888 char_u buf[NUMBUFLEN];
8889
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008890 if (check_restricted() || check_secure())
8891 return;
8892
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008893 if (p_pyx == 0)
8894 p_pyx = 3;
8895
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008896 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008897 do_py3eval(str, rettv);
8898}
8899#endif
8900
8901#ifdef FEAT_PYTHON
8902/*
8903 * "pyeval()" function
8904 */
8905 static void
8906f_pyeval(typval_T *argvars, typval_T *rettv)
8907{
8908 char_u *str;
8909 char_u buf[NUMBUFLEN];
8910
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008911 if (check_restricted() || check_secure())
8912 return;
8913
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008914 if (p_pyx == 0)
8915 p_pyx = 2;
8916
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008917 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008918 do_pyeval(str, rettv);
8919}
8920#endif
8921
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008922#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8923/*
8924 * "pyxeval()" function
8925 */
8926 static void
8927f_pyxeval(typval_T *argvars, typval_T *rettv)
8928{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008929 if (check_restricted() || check_secure())
8930 return;
8931
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008932# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8933 init_pyxversion();
8934 if (p_pyx == 2)
8935 f_pyeval(argvars, rettv);
8936 else
8937 f_py3eval(argvars, rettv);
8938# elif defined(FEAT_PYTHON)
8939 f_pyeval(argvars, rettv);
8940# elif defined(FEAT_PYTHON3)
8941 f_py3eval(argvars, rettv);
8942# endif
8943}
8944#endif
8945
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008946/*
8947 * "range()" function
8948 */
8949 static void
8950f_range(typval_T *argvars, typval_T *rettv)
8951{
8952 varnumber_T start;
8953 varnumber_T end;
8954 varnumber_T stride = 1;
8955 varnumber_T i;
8956 int error = FALSE;
8957
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008958 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008959 if (argvars[1].v_type == VAR_UNKNOWN)
8960 {
8961 end = start - 1;
8962 start = 0;
8963 }
8964 else
8965 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008966 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008967 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008968 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008969 }
8970
8971 if (error)
8972 return; /* type error; errmsg already given */
8973 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008974 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008975 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008976 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977 else
8978 {
8979 if (rettv_list_alloc(rettv) == OK)
8980 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8981 if (list_append_number(rettv->vval.v_list,
8982 (varnumber_T)i) == FAIL)
8983 break;
8984 }
8985}
8986
8987/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008988 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008989 */
8990 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008991readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008992{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008993 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008994 typval_T save_val;
8995 typval_T rettv;
8996 typval_T argv[2];
8997 int retval = 0;
8998 int error = FALSE;
8999
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009000 if (expr->v_type == VAR_UNKNOWN)
9001 return 1;
9002
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009003 prepare_vimvar(VV_VAL, &save_val);
9004 set_vim_var_string(VV_VAL, name, -1);
9005 argv[0].v_type = VAR_STRING;
9006 argv[0].vval.v_string = name;
9007
9008 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9009 goto theend;
9010
9011 retval = tv_get_number_chk(&rettv, &error);
9012 if (error)
9013 retval = -1;
9014 clear_tv(&rettv);
9015
9016theend:
9017 set_vim_var_string(VV_VAL, NULL, 0);
9018 restore_vimvar(VV_VAL, &save_val);
9019 return retval;
9020}
9021
9022/*
9023 * "readdir()" function
9024 */
9025 static void
9026f_readdir(typval_T *argvars, typval_T *rettv)
9027{
9028 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009029 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009030 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009031 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009032 garray_T ga;
9033 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009034
9035 if (rettv_list_alloc(rettv) == FAIL)
9036 return;
9037 path = tv_get_string(&argvars[0]);
9038 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009039
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009040 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9041 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009042 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009043 for (i = 0; i < ga.ga_len; i++)
9044 {
9045 p = ((char_u **)ga.ga_data)[i];
9046 list_append_string(rettv->vval.v_list, p, -1);
9047 }
9048 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009049 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009050}
9051
9052/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009053 * "readfile()" function
9054 */
9055 static void
9056f_readfile(typval_T *argvars, typval_T *rettv)
9057{
9058 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009059 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009060 int failed = FALSE;
9061 char_u *fname;
9062 FILE *fd;
9063 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9064 int io_size = sizeof(buf);
9065 int readlen; /* size of last fread() */
9066 char_u *prev = NULL; /* previously read bytes, if any */
9067 long prevlen = 0; /* length of data in prev */
9068 long prevsize = 0; /* size of prev buffer */
9069 long maxline = MAXLNUM;
9070 long cnt = 0;
9071 char_u *p; /* position in buf */
9072 char_u *start; /* start of current line */
9073
9074 if (argvars[1].v_type != VAR_UNKNOWN)
9075 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009076 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009077 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009078 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9079 blob = TRUE;
9080
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009081 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009082 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009083 }
9084
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009085 if (blob)
9086 {
9087 if (rettv_blob_alloc(rettv) == FAIL)
9088 return;
9089 }
9090 else
9091 {
9092 if (rettv_list_alloc(rettv) == FAIL)
9093 return;
9094 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009095
9096 /* Always open the file in binary mode, library functions have a mind of
9097 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009098 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009099 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9100 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009101 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009102 return;
9103 }
9104
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009105 if (blob)
9106 {
9107 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009109 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009110 blob_free(rettv->vval.v_blob);
9111 }
9112 fclose(fd);
9113 return;
9114 }
9115
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009116 while (cnt < maxline || maxline < 0)
9117 {
9118 readlen = (int)fread(buf, 1, io_size, fd);
9119
9120 /* This for loop processes what was read, but is also entered at end
9121 * of file so that either:
9122 * - an incomplete line gets written
9123 * - a "binary" file gets an empty line at the end if it ends in a
9124 * newline. */
9125 for (p = buf, start = buf;
9126 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9127 ++p)
9128 {
9129 if (*p == '\n' || readlen <= 0)
9130 {
9131 listitem_T *li;
9132 char_u *s = NULL;
9133 long_u len = p - start;
9134
9135 /* Finished a line. Remove CRs before NL. */
9136 if (readlen > 0 && !binary)
9137 {
9138 while (len > 0 && start[len - 1] == '\r')
9139 --len;
9140 /* removal may cross back to the "prev" string */
9141 if (len == 0)
9142 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9143 --prevlen;
9144 }
9145 if (prevlen == 0)
9146 s = vim_strnsave(start, (int)len);
9147 else
9148 {
9149 /* Change "prev" buffer to be the right size. This way
9150 * the bytes are only copied once, and very long lines are
9151 * allocated only once. */
9152 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9153 {
9154 mch_memmove(s + prevlen, start, len);
9155 s[prevlen + len] = NUL;
9156 prev = NULL; /* the list will own the string */
9157 prevlen = prevsize = 0;
9158 }
9159 }
9160 if (s == NULL)
9161 {
9162 do_outofmem_msg((long_u) prevlen + len + 1);
9163 failed = TRUE;
9164 break;
9165 }
9166
9167 if ((li = listitem_alloc()) == NULL)
9168 {
9169 vim_free(s);
9170 failed = TRUE;
9171 break;
9172 }
9173 li->li_tv.v_type = VAR_STRING;
9174 li->li_tv.v_lock = 0;
9175 li->li_tv.vval.v_string = s;
9176 list_append(rettv->vval.v_list, li);
9177
9178 start = p + 1; /* step over newline */
9179 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9180 break;
9181 }
9182 else if (*p == NUL)
9183 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009184 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9185 * when finding the BF and check the previous two bytes. */
9186 else if (*p == 0xbf && enc_utf8 && !binary)
9187 {
9188 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9189 * + 1, these may be in the "prev" string. */
9190 char_u back1 = p >= buf + 1 ? p[-1]
9191 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9192 char_u back2 = p >= buf + 2 ? p[-2]
9193 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9194 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9195
9196 if (back2 == 0xef && back1 == 0xbb)
9197 {
9198 char_u *dest = p - 2;
9199
9200 /* Usually a BOM is at the beginning of a file, and so at
9201 * the beginning of a line; then we can just step over it.
9202 */
9203 if (start == dest)
9204 start = p + 1;
9205 else
9206 {
9207 /* have to shuffle buf to close gap */
9208 int adjust_prevlen = 0;
9209
9210 if (dest < buf)
9211 {
9212 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9213 dest = buf;
9214 }
9215 if (readlen > p - buf + 1)
9216 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9217 readlen -= 3 - adjust_prevlen;
9218 prevlen -= adjust_prevlen;
9219 p = dest - 1;
9220 }
9221 }
9222 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009223 } /* for */
9224
9225 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9226 break;
9227 if (start < p)
9228 {
9229 /* There's part of a line in buf, store it in "prev". */
9230 if (p - start + prevlen >= prevsize)
9231 {
9232 /* need bigger "prev" buffer */
9233 char_u *newprev;
9234
9235 /* A common use case is ordinary text files and "prev" gets a
9236 * fragment of a line, so the first allocation is made
9237 * small, to avoid repeatedly 'allocing' large and
9238 * 'reallocing' small. */
9239 if (prevsize == 0)
9240 prevsize = (long)(p - start);
9241 else
9242 {
9243 long grow50pc = (prevsize * 3) / 2;
9244 long growmin = (long)((p - start) * 2 + prevlen);
9245 prevsize = grow50pc > growmin ? grow50pc : growmin;
9246 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009247 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009248 if (newprev == NULL)
9249 {
9250 do_outofmem_msg((long_u)prevsize);
9251 failed = TRUE;
9252 break;
9253 }
9254 prev = newprev;
9255 }
9256 /* Add the line part to end of "prev". */
9257 mch_memmove(prev + prevlen, start, p - start);
9258 prevlen += (long)(p - start);
9259 }
9260 } /* while */
9261
9262 /*
9263 * For a negative line count use only the lines at the end of the file,
9264 * free the rest.
9265 */
9266 if (!failed && maxline < 0)
9267 while (cnt > -maxline)
9268 {
9269 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9270 --cnt;
9271 }
9272
9273 if (failed)
9274 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009275 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009276 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009277 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009278 }
9279
9280 vim_free(prev);
9281 fclose(fd);
9282}
9283
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009284 static void
9285return_register(int regname, typval_T *rettv)
9286{
9287 char_u buf[2] = {0, 0};
9288
9289 buf[0] = (char_u)regname;
9290 rettv->v_type = VAR_STRING;
9291 rettv->vval.v_string = vim_strsave(buf);
9292}
9293
9294/*
9295 * "reg_executing()" function
9296 */
9297 static void
9298f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9299{
9300 return_register(reg_executing, rettv);
9301}
9302
9303/*
9304 * "reg_recording()" function
9305 */
9306 static void
9307f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9308{
9309 return_register(reg_recording, rettv);
9310}
9311
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009312#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009313/*
9314 * Convert a List to proftime_T.
9315 * Return FAIL when there is something wrong.
9316 */
9317 static int
9318list2proftime(typval_T *arg, proftime_T *tm)
9319{
9320 long n1, n2;
9321 int error = FALSE;
9322
9323 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9324 || arg->vval.v_list->lv_len != 2)
9325 return FAIL;
9326 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9327 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009328# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009329 tm->HighPart = n1;
9330 tm->LowPart = n2;
9331# else
9332 tm->tv_sec = n1;
9333 tm->tv_usec = n2;
9334# endif
9335 return error ? FAIL : OK;
9336}
9337#endif /* FEAT_RELTIME */
9338
9339/*
9340 * "reltime()" function
9341 */
9342 static void
9343f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9344{
9345#ifdef FEAT_RELTIME
9346 proftime_T res;
9347 proftime_T start;
9348
9349 if (argvars[0].v_type == VAR_UNKNOWN)
9350 {
9351 /* No arguments: get current time. */
9352 profile_start(&res);
9353 }
9354 else if (argvars[1].v_type == VAR_UNKNOWN)
9355 {
9356 if (list2proftime(&argvars[0], &res) == FAIL)
9357 return;
9358 profile_end(&res);
9359 }
9360 else
9361 {
9362 /* Two arguments: compute the difference. */
9363 if (list2proftime(&argvars[0], &start) == FAIL
9364 || list2proftime(&argvars[1], &res) == FAIL)
9365 return;
9366 profile_sub(&res, &start);
9367 }
9368
9369 if (rettv_list_alloc(rettv) == OK)
9370 {
9371 long n1, n2;
9372
Bram Moolenaar4f974752019-02-17 17:44:42 +01009373# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009374 n1 = res.HighPart;
9375 n2 = res.LowPart;
9376# else
9377 n1 = res.tv_sec;
9378 n2 = res.tv_usec;
9379# endif
9380 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9381 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9382 }
9383#endif
9384}
9385
9386#ifdef FEAT_FLOAT
9387/*
9388 * "reltimefloat()" function
9389 */
9390 static void
9391f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9392{
9393# ifdef FEAT_RELTIME
9394 proftime_T tm;
9395# endif
9396
9397 rettv->v_type = VAR_FLOAT;
9398 rettv->vval.v_float = 0;
9399# ifdef FEAT_RELTIME
9400 if (list2proftime(&argvars[0], &tm) == OK)
9401 rettv->vval.v_float = profile_float(&tm);
9402# endif
9403}
9404#endif
9405
9406/*
9407 * "reltimestr()" function
9408 */
9409 static void
9410f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9411{
9412#ifdef FEAT_RELTIME
9413 proftime_T tm;
9414#endif
9415
9416 rettv->v_type = VAR_STRING;
9417 rettv->vval.v_string = NULL;
9418#ifdef FEAT_RELTIME
9419 if (list2proftime(&argvars[0], &tm) == OK)
9420 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9421#endif
9422}
9423
9424#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009425 static void
9426make_connection(void)
9427{
9428 if (X_DISPLAY == NULL
9429# ifdef FEAT_GUI
9430 && !gui.in_use
9431# endif
9432 )
9433 {
9434 x_force_connect = TRUE;
9435 setup_term_clip();
9436 x_force_connect = FALSE;
9437 }
9438}
9439
9440 static int
9441check_connection(void)
9442{
9443 make_connection();
9444 if (X_DISPLAY == NULL)
9445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009446 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009447 return FAIL;
9448 }
9449 return OK;
9450}
9451#endif
9452
9453#ifdef FEAT_CLIENTSERVER
9454 static void
9455remote_common(typval_T *argvars, typval_T *rettv, int expr)
9456{
9457 char_u *server_name;
9458 char_u *keys;
9459 char_u *r = NULL;
9460 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009461 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009462# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009463 HWND w;
9464# else
9465 Window w;
9466# endif
9467
9468 if (check_restricted() || check_secure())
9469 return;
9470
9471# ifdef FEAT_X11
9472 if (check_connection() == FAIL)
9473 return;
9474# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009475 if (argvars[2].v_type != VAR_UNKNOWN
9476 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009477 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009478
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009479 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009480 if (server_name == NULL)
9481 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009482 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009483# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009484 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009485# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009486 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9487 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009488# endif
9489 {
9490 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009492 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009493 vim_free(r);
9494 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009495 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009496 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009497 return;
9498 }
9499
9500 rettv->vval.v_string = r;
9501
9502 if (argvars[2].v_type != VAR_UNKNOWN)
9503 {
9504 dictitem_T v;
9505 char_u str[30];
9506 char_u *idvar;
9507
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009508 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009509 if (idvar != NULL && *idvar != NUL)
9510 {
9511 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9512 v.di_tv.v_type = VAR_STRING;
9513 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009514 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009515 vim_free(v.di_tv.vval.v_string);
9516 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009517 }
9518}
9519#endif
9520
9521/*
9522 * "remote_expr()" function
9523 */
9524 static void
9525f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9526{
9527 rettv->v_type = VAR_STRING;
9528 rettv->vval.v_string = NULL;
9529#ifdef FEAT_CLIENTSERVER
9530 remote_common(argvars, rettv, TRUE);
9531#endif
9532}
9533
9534/*
9535 * "remote_foreground()" function
9536 */
9537 static void
9538f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9539{
9540#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009541# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009542 /* On Win32 it's done in this application. */
9543 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009544 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009545
9546 if (server_name != NULL)
9547 serverForeground(server_name);
9548 }
9549# else
9550 /* Send a foreground() expression to the server. */
9551 argvars[1].v_type = VAR_STRING;
9552 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9553 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009554 rettv->v_type = VAR_STRING;
9555 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009556 remote_common(argvars, rettv, TRUE);
9557 vim_free(argvars[1].vval.v_string);
9558# endif
9559#endif
9560}
9561
9562 static void
9563f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9564{
9565#ifdef FEAT_CLIENTSERVER
9566 dictitem_T v;
9567 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009568# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009569 long_u n = 0;
9570# endif
9571 char_u *serverid;
9572
9573 if (check_restricted() || check_secure())
9574 {
9575 rettv->vval.v_number = -1;
9576 return;
9577 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009578 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009579 if (serverid == NULL)
9580 {
9581 rettv->vval.v_number = -1;
9582 return; /* type error; errmsg already given */
9583 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009584# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009585 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9586 if (n == 0)
9587 rettv->vval.v_number = -1;
9588 else
9589 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009590 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009591 rettv->vval.v_number = (s != NULL);
9592 }
9593# else
9594 if (check_connection() == FAIL)
9595 return;
9596
9597 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9598 serverStrToWin(serverid), &s);
9599# endif
9600
9601 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9602 {
9603 char_u *retvar;
9604
9605 v.di_tv.v_type = VAR_STRING;
9606 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009607 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009608 if (retvar != NULL)
9609 set_var(retvar, &v.di_tv, FALSE);
9610 vim_free(v.di_tv.vval.v_string);
9611 }
9612#else
9613 rettv->vval.v_number = -1;
9614#endif
9615}
9616
9617 static void
9618f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9619{
9620 char_u *r = NULL;
9621
9622#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009623 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009624
9625 if (serverid != NULL && !check_restricted() && !check_secure())
9626 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009627 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009628# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009629 /* The server's HWND is encoded in the 'id' parameter */
9630 long_u n = 0;
9631# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009632
9633 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009634 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009635
Bram Moolenaar4f974752019-02-17 17:44:42 +01009636# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009637 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9638 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009639 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009640 if (r == NULL)
9641# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009642 if (check_connection() == FAIL
9643 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9644 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009645# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009646 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009647 }
9648#endif
9649 rettv->v_type = VAR_STRING;
9650 rettv->vval.v_string = r;
9651}
9652
9653/*
9654 * "remote_send()" function
9655 */
9656 static void
9657f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9658{
9659 rettv->v_type = VAR_STRING;
9660 rettv->vval.v_string = NULL;
9661#ifdef FEAT_CLIENTSERVER
9662 remote_common(argvars, rettv, FALSE);
9663#endif
9664}
9665
9666/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009667 * "remote_startserver()" function
9668 */
9669 static void
9670f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9671{
9672#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009673 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009674
9675 if (server == NULL)
9676 return; /* type error; errmsg already given */
9677 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009678 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009679 else
9680 {
9681# ifdef FEAT_X11
9682 if (check_connection() == OK)
9683 serverRegisterName(X_DISPLAY, server);
9684# else
9685 serverSetName(server);
9686# endif
9687 }
9688#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009689 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009690#endif
9691}
9692
9693/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009694 * "remove()" function
9695 */
9696 static void
9697f_remove(typval_T *argvars, typval_T *rettv)
9698{
9699 list_T *l;
9700 listitem_T *item, *item2;
9701 listitem_T *li;
9702 long idx;
9703 long end;
9704 char_u *key;
9705 dict_T *d;
9706 dictitem_T *di;
9707 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009708 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009709
9710 if (argvars[0].v_type == VAR_DICT)
9711 {
9712 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009713 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009714 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009715 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009716 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009717 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009718 if (key != NULL)
9719 {
9720 di = dict_find(d, key, -1);
9721 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009722 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009723 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9724 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9725 {
9726 *rettv = di->di_tv;
9727 init_tv(&di->di_tv);
9728 dictitem_remove(d, di);
9729 }
9730 }
9731 }
9732 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009733 else if (argvars[0].v_type == VAR_BLOB)
9734 {
9735 idx = (long)tv_get_number_chk(&argvars[1], &error);
9736 if (!error)
9737 {
9738 blob_T *b = argvars[0].vval.v_blob;
9739 int len = blob_len(b);
9740 char_u *p;
9741
9742 if (idx < 0)
9743 // count from the end
9744 idx = len + idx;
9745 if (idx < 0 || idx >= len)
9746 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009747 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009748 return;
9749 }
9750 if (argvars[2].v_type == VAR_UNKNOWN)
9751 {
9752 // Remove one item, return its value.
9753 p = (char_u *)b->bv_ga.ga_data;
9754 rettv->vval.v_number = (varnumber_T) *(p + idx);
9755 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9756 --b->bv_ga.ga_len;
9757 }
9758 else
9759 {
9760 blob_T *blob;
9761
9762 // Remove range of items, return list with values.
9763 end = (long)tv_get_number_chk(&argvars[2], &error);
9764 if (error)
9765 return;
9766 if (end < 0)
9767 // count from the end
9768 end = len + end;
9769 if (end >= len || idx > end)
9770 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009771 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009772 return;
9773 }
9774 blob = blob_alloc();
9775 if (blob == NULL)
9776 return;
9777 blob->bv_ga.ga_len = end - idx + 1;
9778 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9779 {
9780 vim_free(blob);
9781 return;
9782 }
9783 p = (char_u *)b->bv_ga.ga_data;
9784 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9785 (size_t)(end - idx + 1));
9786 ++blob->bv_refcount;
9787 rettv->v_type = VAR_BLOB;
9788 rettv->vval.v_blob = blob;
9789
9790 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9791 b->bv_ga.ga_len -= end - idx + 1;
9792 }
9793 }
9794 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009795 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009796 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009797 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009798 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009800 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009801 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009802 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009803 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009804 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009805 else
9806 {
9807 if (argvars[2].v_type == VAR_UNKNOWN)
9808 {
9809 /* Remove one item, return its value. */
9810 vimlist_remove(l, item, item);
9811 *rettv = item->li_tv;
9812 vim_free(item);
9813 }
9814 else
9815 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009816 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009817 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009819 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009820 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009821 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009822 else
9823 {
9824 int cnt = 0;
9825
9826 for (li = item; li != NULL; li = li->li_next)
9827 {
9828 ++cnt;
9829 if (li == item2)
9830 break;
9831 }
9832 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009833 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009834 else
9835 {
9836 vimlist_remove(l, item, item2);
9837 if (rettv_list_alloc(rettv) == OK)
9838 {
9839 l = rettv->vval.v_list;
9840 l->lv_first = item;
9841 l->lv_last = item2;
9842 item->li_prev = NULL;
9843 item2->li_next = NULL;
9844 l->lv_len = cnt;
9845 }
9846 }
9847 }
9848 }
9849 }
9850 }
9851}
9852
9853/*
9854 * "rename({from}, {to})" function
9855 */
9856 static void
9857f_rename(typval_T *argvars, typval_T *rettv)
9858{
9859 char_u buf[NUMBUFLEN];
9860
9861 if (check_restricted() || check_secure())
9862 rettv->vval.v_number = -1;
9863 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009864 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9865 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009866}
9867
9868/*
9869 * "repeat()" function
9870 */
9871 static void
9872f_repeat(typval_T *argvars, typval_T *rettv)
9873{
9874 char_u *p;
9875 int n;
9876 int slen;
9877 int len;
9878 char_u *r;
9879 int i;
9880
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009881 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009882 if (argvars[0].v_type == VAR_LIST)
9883 {
9884 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9885 while (n-- > 0)
9886 if (list_extend(rettv->vval.v_list,
9887 argvars[0].vval.v_list, NULL) == FAIL)
9888 break;
9889 }
9890 else
9891 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009892 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009893 rettv->v_type = VAR_STRING;
9894 rettv->vval.v_string = NULL;
9895
9896 slen = (int)STRLEN(p);
9897 len = slen * n;
9898 if (len <= 0)
9899 return;
9900
9901 r = alloc(len + 1);
9902 if (r != NULL)
9903 {
9904 for (i = 0; i < n; i++)
9905 mch_memmove(r + i * slen, p, (size_t)slen);
9906 r[len] = NUL;
9907 }
9908
9909 rettv->vval.v_string = r;
9910 }
9911}
9912
9913/*
9914 * "resolve()" function
9915 */
9916 static void
9917f_resolve(typval_T *argvars, typval_T *rettv)
9918{
9919 char_u *p;
9920#ifdef HAVE_READLINK
9921 char_u *buf = NULL;
9922#endif
9923
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009924 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009925#ifdef FEAT_SHORTCUT
9926 {
9927 char_u *v = NULL;
9928
Bram Moolenaardce1e892019-02-10 23:18:53 +01009929 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009930 if (v != NULL)
9931 rettv->vval.v_string = v;
9932 else
9933 rettv->vval.v_string = vim_strsave(p);
9934 }
9935#else
9936# ifdef HAVE_READLINK
9937 {
9938 char_u *cpy;
9939 int len;
9940 char_u *remain = NULL;
9941 char_u *q;
9942 int is_relative_to_current = FALSE;
9943 int has_trailing_pathsep = FALSE;
9944 int limit = 100;
9945
9946 p = vim_strsave(p);
9947
9948 if (p[0] == '.' && (vim_ispathsep(p[1])
9949 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9950 is_relative_to_current = TRUE;
9951
9952 len = STRLEN(p);
9953 if (len > 0 && after_pathsep(p, p + len))
9954 {
9955 has_trailing_pathsep = TRUE;
9956 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9957 }
9958
9959 q = getnextcomp(p);
9960 if (*q != NUL)
9961 {
9962 /* Separate the first path component in "p", and keep the
9963 * remainder (beginning with the path separator). */
9964 remain = vim_strsave(q - 1);
9965 q[-1] = NUL;
9966 }
9967
9968 buf = alloc(MAXPATHL + 1);
9969 if (buf == NULL)
9970 goto fail;
9971
9972 for (;;)
9973 {
9974 for (;;)
9975 {
9976 len = readlink((char *)p, (char *)buf, MAXPATHL);
9977 if (len <= 0)
9978 break;
9979 buf[len] = NUL;
9980
9981 if (limit-- == 0)
9982 {
9983 vim_free(p);
9984 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009985 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009986 rettv->vval.v_string = NULL;
9987 goto fail;
9988 }
9989
9990 /* Ensure that the result will have a trailing path separator
9991 * if the argument has one. */
9992 if (remain == NULL && has_trailing_pathsep)
9993 add_pathsep(buf);
9994
9995 /* Separate the first path component in the link value and
9996 * concatenate the remainders. */
9997 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9998 if (*q != NUL)
9999 {
10000 if (remain == NULL)
10001 remain = vim_strsave(q - 1);
10002 else
10003 {
10004 cpy = concat_str(q - 1, remain);
10005 if (cpy != NULL)
10006 {
10007 vim_free(remain);
10008 remain = cpy;
10009 }
10010 }
10011 q[-1] = NUL;
10012 }
10013
10014 q = gettail(p);
10015 if (q > p && *q == NUL)
10016 {
10017 /* Ignore trailing path separator. */
10018 q[-1] = NUL;
10019 q = gettail(p);
10020 }
10021 if (q > p && !mch_isFullName(buf))
10022 {
10023 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010024 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010025 if (cpy != NULL)
10026 {
10027 STRCPY(cpy, p);
10028 STRCPY(gettail(cpy), buf);
10029 vim_free(p);
10030 p = cpy;
10031 }
10032 }
10033 else
10034 {
10035 vim_free(p);
10036 p = vim_strsave(buf);
10037 }
10038 }
10039
10040 if (remain == NULL)
10041 break;
10042
10043 /* Append the first path component of "remain" to "p". */
10044 q = getnextcomp(remain + 1);
10045 len = q - remain - (*q != NUL);
10046 cpy = vim_strnsave(p, STRLEN(p) + len);
10047 if (cpy != NULL)
10048 {
10049 STRNCAT(cpy, remain, len);
10050 vim_free(p);
10051 p = cpy;
10052 }
10053 /* Shorten "remain". */
10054 if (*q != NUL)
10055 STRMOVE(remain, q - 1);
10056 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010057 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010058 }
10059
10060 /* If the result is a relative path name, make it explicitly relative to
10061 * the current directory if and only if the argument had this form. */
10062 if (!vim_ispathsep(*p))
10063 {
10064 if (is_relative_to_current
10065 && *p != NUL
10066 && !(p[0] == '.'
10067 && (p[1] == NUL
10068 || vim_ispathsep(p[1])
10069 || (p[1] == '.'
10070 && (p[2] == NUL
10071 || vim_ispathsep(p[2]))))))
10072 {
10073 /* Prepend "./". */
10074 cpy = concat_str((char_u *)"./", p);
10075 if (cpy != NULL)
10076 {
10077 vim_free(p);
10078 p = cpy;
10079 }
10080 }
10081 else if (!is_relative_to_current)
10082 {
10083 /* Strip leading "./". */
10084 q = p;
10085 while (q[0] == '.' && vim_ispathsep(q[1]))
10086 q += 2;
10087 if (q > p)
10088 STRMOVE(p, p + 2);
10089 }
10090 }
10091
10092 /* Ensure that the result will have no trailing path separator
10093 * if the argument had none. But keep "/" or "//". */
10094 if (!has_trailing_pathsep)
10095 {
10096 q = p + STRLEN(p);
10097 if (after_pathsep(p, q))
10098 *gettail_sep(p) = NUL;
10099 }
10100
10101 rettv->vval.v_string = p;
10102 }
10103# else
10104 rettv->vval.v_string = vim_strsave(p);
10105# endif
10106#endif
10107
10108 simplify_filename(rettv->vval.v_string);
10109
10110#ifdef HAVE_READLINK
10111fail:
10112 vim_free(buf);
10113#endif
10114 rettv->v_type = VAR_STRING;
10115}
10116
10117/*
10118 * "reverse({list})" function
10119 */
10120 static void
10121f_reverse(typval_T *argvars, typval_T *rettv)
10122{
10123 list_T *l;
10124 listitem_T *li, *ni;
10125
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010126 if (argvars[0].v_type == VAR_BLOB)
10127 {
10128 blob_T *b = argvars[0].vval.v_blob;
10129 int i, len = blob_len(b);
10130
10131 for (i = 0; i < len / 2; i++)
10132 {
10133 int tmp = blob_get(b, i);
10134
10135 blob_set(b, i, blob_get(b, len - i - 1));
10136 blob_set(b, len - i - 1, tmp);
10137 }
10138 rettv_blob_set(rettv, b);
10139 return;
10140 }
10141
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010142 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010143 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010144 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010145 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010146 (char_u *)N_("reverse() argument"), TRUE))
10147 {
10148 li = l->lv_last;
10149 l->lv_first = l->lv_last = NULL;
10150 l->lv_len = 0;
10151 while (li != NULL)
10152 {
10153 ni = li->li_prev;
10154 list_append(l, li);
10155 li = ni;
10156 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010157 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010158 l->lv_idx = l->lv_len - l->lv_idx - 1;
10159 }
10160}
10161
10162#define SP_NOMOVE 0x01 /* don't move cursor */
10163#define SP_REPEAT 0x02 /* repeat to find outer pair */
10164#define SP_RETCOUNT 0x04 /* return matchcount */
10165#define SP_SETPCMARK 0x08 /* set previous context mark */
10166#define SP_START 0x10 /* accept match at start position */
10167#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10168#define SP_END 0x40 /* leave cursor at end of match */
10169#define SP_COLUMN 0x80 /* start at cursor column */
10170
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010171/*
10172 * Get flags for a search function.
10173 * Possibly sets "p_ws".
10174 * Returns BACKWARD, FORWARD or zero (for an error).
10175 */
10176 static int
10177get_search_arg(typval_T *varp, int *flagsp)
10178{
10179 int dir = FORWARD;
10180 char_u *flags;
10181 char_u nbuf[NUMBUFLEN];
10182 int mask;
10183
10184 if (varp->v_type != VAR_UNKNOWN)
10185 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010186 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010187 if (flags == NULL)
10188 return 0; /* type error; errmsg already given */
10189 while (*flags != NUL)
10190 {
10191 switch (*flags)
10192 {
10193 case 'b': dir = BACKWARD; break;
10194 case 'w': p_ws = TRUE; break;
10195 case 'W': p_ws = FALSE; break;
10196 default: mask = 0;
10197 if (flagsp != NULL)
10198 switch (*flags)
10199 {
10200 case 'c': mask = SP_START; break;
10201 case 'e': mask = SP_END; break;
10202 case 'm': mask = SP_RETCOUNT; break;
10203 case 'n': mask = SP_NOMOVE; break;
10204 case 'p': mask = SP_SUBPAT; break;
10205 case 'r': mask = SP_REPEAT; break;
10206 case 's': mask = SP_SETPCMARK; break;
10207 case 'z': mask = SP_COLUMN; break;
10208 }
10209 if (mask == 0)
10210 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010211 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010212 dir = 0;
10213 }
10214 else
10215 *flagsp |= mask;
10216 }
10217 if (dir == 0)
10218 break;
10219 ++flags;
10220 }
10221 }
10222 return dir;
10223}
10224
10225/*
10226 * Shared by search() and searchpos() functions.
10227 */
10228 static int
10229search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10230{
10231 int flags;
10232 char_u *pat;
10233 pos_T pos;
10234 pos_T save_cursor;
10235 int save_p_ws = p_ws;
10236 int dir;
10237 int retval = 0; /* default: FAIL */
10238 long lnum_stop = 0;
10239 proftime_T tm;
10240#ifdef FEAT_RELTIME
10241 long time_limit = 0;
10242#endif
10243 int options = SEARCH_KEEP;
10244 int subpatnum;
10245
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010246 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010247 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10248 if (dir == 0)
10249 goto theend;
10250 flags = *flagsp;
10251 if (flags & SP_START)
10252 options |= SEARCH_START;
10253 if (flags & SP_END)
10254 options |= SEARCH_END;
10255 if (flags & SP_COLUMN)
10256 options |= SEARCH_COL;
10257
10258 /* Optional arguments: line number to stop searching and timeout. */
10259 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10260 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010261 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010262 if (lnum_stop < 0)
10263 goto theend;
10264#ifdef FEAT_RELTIME
10265 if (argvars[3].v_type != VAR_UNKNOWN)
10266 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010267 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010268 if (time_limit < 0)
10269 goto theend;
10270 }
10271#endif
10272 }
10273
10274#ifdef FEAT_RELTIME
10275 /* Set the time limit, if there is one. */
10276 profile_setlimit(time_limit, &tm);
10277#endif
10278
10279 /*
10280 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10281 * Check to make sure only those flags are set.
10282 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10283 * flags cannot be set. Check for that condition also.
10284 */
10285 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10286 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10287 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010288 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010289 goto theend;
10290 }
10291
10292 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010293 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010294 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010295 if (subpatnum != FAIL)
10296 {
10297 if (flags & SP_SUBPAT)
10298 retval = subpatnum;
10299 else
10300 retval = pos.lnum;
10301 if (flags & SP_SETPCMARK)
10302 setpcmark();
10303 curwin->w_cursor = pos;
10304 if (match_pos != NULL)
10305 {
10306 /* Store the match cursor position */
10307 match_pos->lnum = pos.lnum;
10308 match_pos->col = pos.col + 1;
10309 }
10310 /* "/$" will put the cursor after the end of the line, may need to
10311 * correct that here */
10312 check_cursor();
10313 }
10314
10315 /* If 'n' flag is used: restore cursor position. */
10316 if (flags & SP_NOMOVE)
10317 curwin->w_cursor = save_cursor;
10318 else
10319 curwin->w_set_curswant = TRUE;
10320theend:
10321 p_ws = save_p_ws;
10322
10323 return retval;
10324}
10325
10326#ifdef FEAT_FLOAT
10327
10328/*
10329 * round() is not in C90, use ceil() or floor() instead.
10330 */
10331 float_T
10332vim_round(float_T f)
10333{
10334 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10335}
10336
10337/*
10338 * "round({float})" function
10339 */
10340 static void
10341f_round(typval_T *argvars, typval_T *rettv)
10342{
10343 float_T f = 0.0;
10344
10345 rettv->v_type = VAR_FLOAT;
10346 if (get_float_arg(argvars, &f) == OK)
10347 rettv->vval.v_float = vim_round(f);
10348 else
10349 rettv->vval.v_float = 0.0;
10350}
10351#endif
10352
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010353#ifdef FEAT_RUBY
10354/*
10355 * "rubyeval()" function
10356 */
10357 static void
10358f_rubyeval(typval_T *argvars, typval_T *rettv)
10359{
10360 char_u *str;
10361 char_u buf[NUMBUFLEN];
10362
10363 str = tv_get_string_buf(&argvars[0], buf);
10364 do_rubyeval(str, rettv);
10365}
10366#endif
10367
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010368/*
10369 * "screenattr()" function
10370 */
10371 static void
10372f_screenattr(typval_T *argvars, typval_T *rettv)
10373{
10374 int row;
10375 int col;
10376 int c;
10377
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010378 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10379 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010380 if (row < 0 || row >= screen_Rows
10381 || col < 0 || col >= screen_Columns)
10382 c = -1;
10383 else
10384 c = ScreenAttrs[LineOffset[row] + col];
10385 rettv->vval.v_number = c;
10386}
10387
10388/*
10389 * "screenchar()" function
10390 */
10391 static void
10392f_screenchar(typval_T *argvars, typval_T *rettv)
10393{
10394 int row;
10395 int col;
10396 int off;
10397 int c;
10398
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010399 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10400 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010401 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010402 c = -1;
10403 else
10404 {
10405 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010406 if (enc_utf8 && ScreenLinesUC[off] != 0)
10407 c = ScreenLinesUC[off];
10408 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010409 c = ScreenLines[off];
10410 }
10411 rettv->vval.v_number = c;
10412}
10413
10414/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010415 * "screenchars()" function
10416 */
10417 static void
10418f_screenchars(typval_T *argvars, typval_T *rettv)
10419{
10420 int row;
10421 int col;
10422 int off;
10423 int c;
10424 int i;
10425
10426 if (rettv_list_alloc(rettv) == FAIL)
10427 return;
10428 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10429 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10430 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10431 return;
10432
10433 off = LineOffset[row] + col;
10434 if (enc_utf8 && ScreenLinesUC[off] != 0)
10435 c = ScreenLinesUC[off];
10436 else
10437 c = ScreenLines[off];
10438 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10439
10440 if (enc_utf8)
10441
10442 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10443 list_append_number(rettv->vval.v_list,
10444 (varnumber_T)ScreenLinesC[i][off]);
10445}
10446
10447/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010448 * "screencol()" function
10449 *
10450 * First column is 1 to be consistent with virtcol().
10451 */
10452 static void
10453f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10454{
10455 rettv->vval.v_number = screen_screencol() + 1;
10456}
10457
10458/*
10459 * "screenrow()" function
10460 */
10461 static void
10462f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10463{
10464 rettv->vval.v_number = screen_screenrow() + 1;
10465}
10466
10467/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010468 * "screenstring()" function
10469 */
10470 static void
10471f_screenstring(typval_T *argvars, typval_T *rettv)
10472{
10473 int row;
10474 int col;
10475 int off;
10476 int c;
10477 int i;
10478 char_u buf[MB_MAXBYTES + 1];
10479 int buflen = 0;
10480
10481 rettv->vval.v_string = NULL;
10482 rettv->v_type = VAR_STRING;
10483
10484 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10485 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10486 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10487 return;
10488
10489 off = LineOffset[row] + col;
10490 if (enc_utf8 && ScreenLinesUC[off] != 0)
10491 c = ScreenLinesUC[off];
10492 else
10493 c = ScreenLines[off];
10494 buflen += mb_char2bytes(c, buf);
10495
10496 if (enc_utf8)
10497 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10498 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10499
10500 buf[buflen] = NUL;
10501 rettv->vval.v_string = vim_strsave(buf);
10502}
10503
10504/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010505 * "search()" function
10506 */
10507 static void
10508f_search(typval_T *argvars, typval_T *rettv)
10509{
10510 int flags = 0;
10511
10512 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10513}
10514
10515/*
10516 * "searchdecl()" function
10517 */
10518 static void
10519f_searchdecl(typval_T *argvars, typval_T *rettv)
10520{
10521 int locally = 1;
10522 int thisblock = 0;
10523 int error = FALSE;
10524 char_u *name;
10525
10526 rettv->vval.v_number = 1; /* default: FAIL */
10527
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010528 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010529 if (argvars[1].v_type != VAR_UNKNOWN)
10530 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010531 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010532 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010533 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010534 }
10535 if (!error && name != NULL)
10536 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10537 locally, thisblock, SEARCH_KEEP) == FAIL;
10538}
10539
10540/*
10541 * Used by searchpair() and searchpairpos()
10542 */
10543 static int
10544searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10545{
10546 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010547 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010548 int save_p_ws = p_ws;
10549 int dir;
10550 int flags = 0;
10551 char_u nbuf1[NUMBUFLEN];
10552 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010553 int retval = 0; /* default: FAIL */
10554 long lnum_stop = 0;
10555 long time_limit = 0;
10556
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010557 /* Get the three pattern arguments: start, middle, end. Will result in an
10558 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010559 spat = tv_get_string_chk(&argvars[0]);
10560 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10561 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010562 if (spat == NULL || mpat == NULL || epat == NULL)
10563 goto theend; /* type error */
10564
10565 /* Handle the optional fourth argument: flags */
10566 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10567 if (dir == 0)
10568 goto theend;
10569
10570 /* Don't accept SP_END or SP_SUBPAT.
10571 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10572 */
10573 if ((flags & (SP_END | SP_SUBPAT)) != 0
10574 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010576 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010577 goto theend;
10578 }
10579
10580 /* Using 'r' implies 'W', otherwise it doesn't work. */
10581 if (flags & SP_REPEAT)
10582 p_ws = FALSE;
10583
10584 /* Optional fifth argument: skip expression */
10585 if (argvars[3].v_type == VAR_UNKNOWN
10586 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010587 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010588 else
10589 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010590 skip = &argvars[4];
10591 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10592 && skip->v_type != VAR_STRING)
10593 {
10594 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010595 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010596 goto theend;
10597 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010598 if (argvars[5].v_type != VAR_UNKNOWN)
10599 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010600 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010601 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010602 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010603 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010604 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010605 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010606#ifdef FEAT_RELTIME
10607 if (argvars[6].v_type != VAR_UNKNOWN)
10608 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010609 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010610 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010612 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010613 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010614 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010615 }
10616#endif
10617 }
10618 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619
10620 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10621 match_pos, lnum_stop, time_limit);
10622
10623theend:
10624 p_ws = save_p_ws;
10625
10626 return retval;
10627}
10628
10629/*
10630 * "searchpair()" function
10631 */
10632 static void
10633f_searchpair(typval_T *argvars, typval_T *rettv)
10634{
10635 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10636}
10637
10638/*
10639 * "searchpairpos()" function
10640 */
10641 static void
10642f_searchpairpos(typval_T *argvars, typval_T *rettv)
10643{
10644 pos_T match_pos;
10645 int lnum = 0;
10646 int col = 0;
10647
10648 if (rettv_list_alloc(rettv) == FAIL)
10649 return;
10650
10651 if (searchpair_cmn(argvars, &match_pos) > 0)
10652 {
10653 lnum = match_pos.lnum;
10654 col = match_pos.col;
10655 }
10656
10657 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10658 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10659}
10660
10661/*
10662 * Search for a start/middle/end thing.
10663 * Used by searchpair(), see its documentation for the details.
10664 * Returns 0 or -1 for no match,
10665 */
10666 long
10667do_searchpair(
10668 char_u *spat, /* start pattern */
10669 char_u *mpat, /* middle pattern */
10670 char_u *epat, /* end pattern */
10671 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010672 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010673 int flags, /* SP_SETPCMARK and other SP_ values */
10674 pos_T *match_pos,
10675 linenr_T lnum_stop, /* stop at this line if not zero */
10676 long time_limit UNUSED) /* stop after this many msec */
10677{
10678 char_u *save_cpo;
10679 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10680 long retval = 0;
10681 pos_T pos;
10682 pos_T firstpos;
10683 pos_T foundpos;
10684 pos_T save_cursor;
10685 pos_T save_pos;
10686 int n;
10687 int r;
10688 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010689 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010690 int err;
10691 int options = SEARCH_KEEP;
10692 proftime_T tm;
10693
10694 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10695 save_cpo = p_cpo;
10696 p_cpo = empty_option;
10697
10698#ifdef FEAT_RELTIME
10699 /* Set the time limit, if there is one. */
10700 profile_setlimit(time_limit, &tm);
10701#endif
10702
10703 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10704 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010705 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
10706 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010707 if (pat2 == NULL || pat3 == NULL)
10708 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010709 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010710 if (*mpat == NUL)
10711 STRCPY(pat3, pat2);
10712 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010713 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010714 spat, epat, mpat);
10715 if (flags & SP_START)
10716 options |= SEARCH_START;
10717
Bram Moolenaar48570482017-10-30 21:48:41 +010010718 if (skip != NULL)
10719 {
10720 /* Empty string means to not use the skip expression. */
10721 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10722 use_skip = skip->vval.v_string != NULL
10723 && *skip->vval.v_string != NUL;
10724 }
10725
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010726 save_cursor = curwin->w_cursor;
10727 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010728 CLEAR_POS(&firstpos);
10729 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010730 pat = pat3;
10731 for (;;)
10732 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010733 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010734 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010735 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010736 /* didn't find it or found the first match again: FAIL */
10737 break;
10738
10739 if (firstpos.lnum == 0)
10740 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010741 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010742 {
10743 /* Found the same position again. Can happen with a pattern that
10744 * has "\zs" at the end and searching backwards. Advance one
10745 * character and try again. */
10746 if (dir == BACKWARD)
10747 decl(&pos);
10748 else
10749 incl(&pos);
10750 }
10751 foundpos = pos;
10752
10753 /* clear the start flag to avoid getting stuck here */
10754 options &= ~SEARCH_START;
10755
10756 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010757 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010758 {
10759 save_pos = curwin->w_cursor;
10760 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010761 err = FALSE;
10762 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010763 curwin->w_cursor = save_pos;
10764 if (err)
10765 {
10766 /* Evaluating {skip} caused an error, break here. */
10767 curwin->w_cursor = save_cursor;
10768 retval = -1;
10769 break;
10770 }
10771 if (r)
10772 continue;
10773 }
10774
10775 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10776 {
10777 /* Found end when searching backwards or start when searching
10778 * forward: nested pair. */
10779 ++nest;
10780 pat = pat2; /* nested, don't search for middle */
10781 }
10782 else
10783 {
10784 /* Found end when searching forward or start when searching
10785 * backward: end of (nested) pair; or found middle in outer pair. */
10786 if (--nest == 1)
10787 pat = pat3; /* outer level, search for middle */
10788 }
10789
10790 if (nest == 0)
10791 {
10792 /* Found the match: return matchcount or line number. */
10793 if (flags & SP_RETCOUNT)
10794 ++retval;
10795 else
10796 retval = pos.lnum;
10797 if (flags & SP_SETPCMARK)
10798 setpcmark();
10799 curwin->w_cursor = pos;
10800 if (!(flags & SP_REPEAT))
10801 break;
10802 nest = 1; /* search for next unmatched */
10803 }
10804 }
10805
10806 if (match_pos != NULL)
10807 {
10808 /* Store the match cursor position */
10809 match_pos->lnum = curwin->w_cursor.lnum;
10810 match_pos->col = curwin->w_cursor.col + 1;
10811 }
10812
10813 /* If 'n' flag is used or search failed: restore cursor position. */
10814 if ((flags & SP_NOMOVE) || retval == 0)
10815 curwin->w_cursor = save_cursor;
10816
10817theend:
10818 vim_free(pat2);
10819 vim_free(pat3);
10820 if (p_cpo == empty_option)
10821 p_cpo = save_cpo;
10822 else
10823 /* Darn, evaluating the {skip} expression changed the value. */
10824 free_string_option(save_cpo);
10825
10826 return retval;
10827}
10828
10829/*
10830 * "searchpos()" function
10831 */
10832 static void
10833f_searchpos(typval_T *argvars, typval_T *rettv)
10834{
10835 pos_T match_pos;
10836 int lnum = 0;
10837 int col = 0;
10838 int n;
10839 int flags = 0;
10840
10841 if (rettv_list_alloc(rettv) == FAIL)
10842 return;
10843
10844 n = search_cmn(argvars, &match_pos, &flags);
10845 if (n > 0)
10846 {
10847 lnum = match_pos.lnum;
10848 col = match_pos.col;
10849 }
10850
10851 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10852 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10853 if (flags & SP_SUBPAT)
10854 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10855}
10856
10857 static void
10858f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10859{
10860#ifdef FEAT_CLIENTSERVER
10861 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010862 char_u *server = tv_get_string_chk(&argvars[0]);
10863 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010864
10865 rettv->vval.v_number = -1;
10866 if (server == NULL || reply == NULL)
10867 return;
10868 if (check_restricted() || check_secure())
10869 return;
10870# ifdef FEAT_X11
10871 if (check_connection() == FAIL)
10872 return;
10873# endif
10874
10875 if (serverSendReply(server, reply) < 0)
10876 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010877 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010878 return;
10879 }
10880 rettv->vval.v_number = 0;
10881#else
10882 rettv->vval.v_number = -1;
10883#endif
10884}
10885
10886 static void
10887f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10888{
10889 char_u *r = NULL;
10890
10891#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010892# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010893 r = serverGetVimNames();
10894# else
10895 make_connection();
10896 if (X_DISPLAY != NULL)
10897 r = serverGetVimNames(X_DISPLAY);
10898# endif
10899#endif
10900 rettv->v_type = VAR_STRING;
10901 rettv->vval.v_string = r;
10902}
10903
10904/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010905 * "setbufline()" function
10906 */
10907 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010908f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010909{
10910 linenr_T lnum;
10911 buf_T *buf;
10912
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010913 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010914 if (buf == NULL)
10915 rettv->vval.v_number = 1; /* FAIL */
10916 else
10917 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010918 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010919 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010920 }
10921}
10922
10923/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010924 * "setbufvar()" function
10925 */
10926 static void
10927f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10928{
10929 buf_T *buf;
10930 char_u *varname, *bufvarname;
10931 typval_T *varp;
10932 char_u nbuf[NUMBUFLEN];
10933
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010934 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010935 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010936 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10937 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010938 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010939 varp = &argvars[2];
10940
10941 if (buf != NULL && varname != NULL && varp != NULL)
10942 {
10943 if (*varname == '&')
10944 {
10945 long numval;
10946 char_u *strval;
10947 int error = FALSE;
10948 aco_save_T aco;
10949
10950 /* set curbuf to be our buf, temporarily */
10951 aucmd_prepbuf(&aco, buf);
10952
10953 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010954 numval = (long)tv_get_number_chk(varp, &error);
10955 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010956 if (!error && strval != NULL)
10957 set_option_value(varname, numval, strval, OPT_LOCAL);
10958
10959 /* reset notion of buffer */
10960 aucmd_restbuf(&aco);
10961 }
10962 else
10963 {
10964 buf_T *save_curbuf = curbuf;
10965
Bram Moolenaar964b3742019-05-24 18:54:09 +020010966 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010967 if (bufvarname != NULL)
10968 {
10969 curbuf = buf;
10970 STRCPY(bufvarname, "b:");
10971 STRCPY(bufvarname + 2, varname);
10972 set_var(bufvarname, varp, TRUE);
10973 vim_free(bufvarname);
10974 curbuf = save_curbuf;
10975 }
10976 }
10977 }
10978}
10979
10980 static void
10981f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10982{
10983 dict_T *d;
10984 dictitem_T *di;
10985 char_u *csearch;
10986
10987 if (argvars[0].v_type != VAR_DICT)
10988 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010989 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010990 return;
10991 }
10992
10993 if ((d = argvars[0].vval.v_dict) != NULL)
10994 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010995 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010996 if (csearch != NULL)
10997 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010998 if (enc_utf8)
10999 {
11000 int pcc[MAX_MCO];
11001 int c = utfc_ptr2char(csearch, pcc);
11002
11003 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11004 }
11005 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011006 set_last_csearch(PTR2CHAR(csearch),
11007 csearch, MB_PTR2LEN(csearch));
11008 }
11009
11010 di = dict_find(d, (char_u *)"forward", -1);
11011 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011012 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011013 ? FORWARD : BACKWARD);
11014
11015 di = dict_find(d, (char_u *)"until", -1);
11016 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011017 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011018 }
11019}
11020
11021/*
11022 * "setcmdpos()" function
11023 */
11024 static void
11025f_setcmdpos(typval_T *argvars, typval_T *rettv)
11026{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011027 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011028
11029 if (pos >= 0)
11030 rettv->vval.v_number = set_cmdline_pos(pos);
11031}
11032
11033/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011034 * "setenv()" function
11035 */
11036 static void
11037f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11038{
11039 char_u namebuf[NUMBUFLEN];
11040 char_u valbuf[NUMBUFLEN];
11041 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11042
11043 if (argvars[1].v_type == VAR_SPECIAL
11044 && argvars[1].vval.v_number == VVAL_NULL)
11045 vim_unsetenv(name);
11046 else
11047 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11048}
11049
11050/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011051 * "setfperm({fname}, {mode})" function
11052 */
11053 static void
11054f_setfperm(typval_T *argvars, typval_T *rettv)
11055{
11056 char_u *fname;
11057 char_u modebuf[NUMBUFLEN];
11058 char_u *mode_str;
11059 int i;
11060 int mask;
11061 int mode = 0;
11062
11063 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011064 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011065 if (fname == NULL)
11066 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011067 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011068 if (mode_str == NULL)
11069 return;
11070 if (STRLEN(mode_str) != 9)
11071 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011072 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011073 return;
11074 }
11075
11076 mask = 1;
11077 for (i = 8; i >= 0; --i)
11078 {
11079 if (mode_str[i] != '-')
11080 mode |= mask;
11081 mask = mask << 1;
11082 }
11083 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11084}
11085
11086/*
11087 * "setline()" function
11088 */
11089 static void
11090f_setline(typval_T *argvars, typval_T *rettv)
11091{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011092 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011093
Bram Moolenaarca851592018-06-06 21:04:07 +020011094 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011095}
11096
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011097/*
11098 * Used by "setqflist()" and "setloclist()" functions
11099 */
11100 static void
11101set_qf_ll_list(
11102 win_T *wp UNUSED,
11103 typval_T *list_arg UNUSED,
11104 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011105 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011106 typval_T *rettv)
11107{
11108#ifdef FEAT_QUICKFIX
11109 static char *e_invact = N_("E927: Invalid action: '%s'");
11110 char_u *act;
11111 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011112 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011113#endif
11114
11115 rettv->vval.v_number = -1;
11116
11117#ifdef FEAT_QUICKFIX
11118 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011119 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011120 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011121 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011122 else
11123 {
11124 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011125 dict_T *d = NULL;
11126 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011127
11128 if (action_arg->v_type == VAR_STRING)
11129 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011130 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011131 if (act == NULL)
11132 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011133 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11134 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011135 action = *act;
11136 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011137 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011138 }
11139 else if (action_arg->v_type == VAR_UNKNOWN)
11140 action = ' ';
11141 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011142 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011143
Bram Moolenaard823fa92016-08-12 16:29:27 +020011144 if (action_arg->v_type != VAR_UNKNOWN
11145 && what_arg->v_type != VAR_UNKNOWN)
11146 {
11147 if (what_arg->v_type == VAR_DICT)
11148 d = what_arg->vval.v_dict;
11149 else
11150 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011151 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011152 valid_dict = FALSE;
11153 }
11154 }
11155
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011156 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011157 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011158 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11159 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011160 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011161 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011162 }
11163#endif
11164}
11165
11166/*
11167 * "setloclist()" function
11168 */
11169 static void
11170f_setloclist(typval_T *argvars, typval_T *rettv)
11171{
11172 win_T *win;
11173
11174 rettv->vval.v_number = -1;
11175
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011176 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011177 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011178 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011179}
11180
11181/*
11182 * "setmatches()" function
11183 */
11184 static void
11185f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11186{
11187#ifdef FEAT_SEARCH_EXTRA
11188 list_T *l;
11189 listitem_T *li;
11190 dict_T *d;
11191 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011192 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011193
11194 rettv->vval.v_number = -1;
11195 if (argvars[0].v_type != VAR_LIST)
11196 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011197 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011198 return;
11199 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011200 if (win == NULL)
11201 return;
11202
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011203 if ((l = argvars[0].vval.v_list) != NULL)
11204 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011205 /* To some extent make sure that we are dealing with a list from
11206 * "getmatches()". */
11207 li = l->lv_first;
11208 while (li != NULL)
11209 {
11210 if (li->li_tv.v_type != VAR_DICT
11211 || (d = li->li_tv.vval.v_dict) == NULL)
11212 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011213 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011214 return;
11215 }
11216 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11217 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11218 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11219 && dict_find(d, (char_u *)"priority", -1) != NULL
11220 && dict_find(d, (char_u *)"id", -1) != NULL))
11221 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011222 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011223 return;
11224 }
11225 li = li->li_next;
11226 }
11227
Bram Moolenaaraff74912019-03-30 18:11:49 +010011228 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011229 li = l->lv_first;
11230 while (li != NULL)
11231 {
11232 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011233 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011234 dictitem_T *di;
11235 char_u *group;
11236 int priority;
11237 int id;
11238 char_u *conceal;
11239
11240 d = li->li_tv.vval.v_dict;
11241 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11242 {
11243 if (s == NULL)
11244 {
11245 s = list_alloc();
11246 if (s == NULL)
11247 return;
11248 }
11249
11250 /* match from matchaddpos() */
11251 for (i = 1; i < 9; i++)
11252 {
11253 sprintf((char *)buf, (char *)"pos%d", i);
11254 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11255 {
11256 if (di->di_tv.v_type != VAR_LIST)
11257 return;
11258
11259 list_append_tv(s, &di->di_tv);
11260 s->lv_refcount++;
11261 }
11262 else
11263 break;
11264 }
11265 }
11266
Bram Moolenaar8f667172018-12-14 15:38:31 +010011267 group = dict_get_string(d, (char_u *)"group", TRUE);
11268 priority = (int)dict_get_number(d, (char_u *)"priority");
11269 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011270 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011271 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011272 : NULL;
11273 if (i == 0)
11274 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011275 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011276 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011277 priority, id, NULL, conceal);
11278 }
11279 else
11280 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011281 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011282 list_unref(s);
11283 s = NULL;
11284 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011285 vim_free(group);
11286 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011287
11288 li = li->li_next;
11289 }
11290 rettv->vval.v_number = 0;
11291 }
11292#endif
11293}
11294
11295/*
11296 * "setpos()" function
11297 */
11298 static void
11299f_setpos(typval_T *argvars, typval_T *rettv)
11300{
11301 pos_T pos;
11302 int fnum;
11303 char_u *name;
11304 colnr_T curswant = -1;
11305
11306 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011307 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011308 if (name != NULL)
11309 {
11310 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11311 {
11312 if (--pos.col < 0)
11313 pos.col = 0;
11314 if (name[0] == '.' && name[1] == NUL)
11315 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011316 /* set cursor; "fnum" is ignored */
11317 curwin->w_cursor = pos;
11318 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011319 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011320 curwin->w_curswant = curswant - 1;
11321 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011322 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011323 check_cursor();
11324 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011325 }
11326 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11327 {
11328 /* set mark */
11329 if (setmark_pos(name[1], &pos, fnum) == OK)
11330 rettv->vval.v_number = 0;
11331 }
11332 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011333 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011334 }
11335 }
11336}
11337
11338/*
11339 * "setqflist()" function
11340 */
11341 static void
11342f_setqflist(typval_T *argvars, typval_T *rettv)
11343{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011344 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011345}
11346
11347/*
11348 * "setreg()" function
11349 */
11350 static void
11351f_setreg(typval_T *argvars, typval_T *rettv)
11352{
11353 int regname;
11354 char_u *strregname;
11355 char_u *stropt;
11356 char_u *strval;
11357 int append;
11358 char_u yank_type;
11359 long block_len;
11360
11361 block_len = -1;
11362 yank_type = MAUTO;
11363 append = FALSE;
11364
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011365 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011366 rettv->vval.v_number = 1; /* FAIL is default */
11367
11368 if (strregname == NULL)
11369 return; /* type error; errmsg already given */
11370 regname = *strregname;
11371 if (regname == 0 || regname == '@')
11372 regname = '"';
11373
11374 if (argvars[2].v_type != VAR_UNKNOWN)
11375 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011376 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011377 if (stropt == NULL)
11378 return; /* type error */
11379 for (; *stropt != NUL; ++stropt)
11380 switch (*stropt)
11381 {
11382 case 'a': case 'A': /* append */
11383 append = TRUE;
11384 break;
11385 case 'v': case 'c': /* character-wise selection */
11386 yank_type = MCHAR;
11387 break;
11388 case 'V': case 'l': /* line-wise selection */
11389 yank_type = MLINE;
11390 break;
11391 case 'b': case Ctrl_V: /* block-wise selection */
11392 yank_type = MBLOCK;
11393 if (VIM_ISDIGIT(stropt[1]))
11394 {
11395 ++stropt;
11396 block_len = getdigits(&stropt) - 1;
11397 --stropt;
11398 }
11399 break;
11400 }
11401 }
11402
11403 if (argvars[1].v_type == VAR_LIST)
11404 {
11405 char_u **lstval;
11406 char_u **allocval;
11407 char_u buf[NUMBUFLEN];
11408 char_u **curval;
11409 char_u **curallocval;
11410 list_T *ll = argvars[1].vval.v_list;
11411 listitem_T *li;
11412 int len;
11413
11414 /* If the list is NULL handle like an empty list. */
11415 len = ll == NULL ? 0 : ll->lv_len;
11416
11417 /* First half: use for pointers to result lines; second half: use for
11418 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011419 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011420 if (lstval == NULL)
11421 return;
11422 curval = lstval;
11423 allocval = lstval + len + 2;
11424 curallocval = allocval;
11425
11426 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11427 li = li->li_next)
11428 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011429 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011430 if (strval == NULL)
11431 goto free_lstval;
11432 if (strval == buf)
11433 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011434 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011435 * overwrite the string. */
11436 strval = vim_strsave(buf);
11437 if (strval == NULL)
11438 goto free_lstval;
11439 *curallocval++ = strval;
11440 }
11441 *curval++ = strval;
11442 }
11443 *curval++ = NULL;
11444
11445 write_reg_contents_lst(regname, lstval, -1,
11446 append, yank_type, block_len);
11447free_lstval:
11448 while (curallocval > allocval)
11449 vim_free(*--curallocval);
11450 vim_free(lstval);
11451 }
11452 else
11453 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011454 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011455 if (strval == NULL)
11456 return;
11457 write_reg_contents_ex(regname, strval, -1,
11458 append, yank_type, block_len);
11459 }
11460 rettv->vval.v_number = 0;
11461}
11462
11463/*
11464 * "settabvar()" function
11465 */
11466 static void
11467f_settabvar(typval_T *argvars, typval_T *rettv)
11468{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011469 tabpage_T *save_curtab;
11470 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011471 char_u *varname, *tabvarname;
11472 typval_T *varp;
11473
11474 rettv->vval.v_number = 0;
11475
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011476 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011477 return;
11478
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011479 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11480 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011481 varp = &argvars[2];
11482
Bram Moolenaar4033c552017-09-16 20:54:51 +020011483 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011484 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011485 save_curtab = curtab;
11486 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011487
Bram Moolenaar964b3742019-05-24 18:54:09 +020011488 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011489 if (tabvarname != NULL)
11490 {
11491 STRCPY(tabvarname, "t:");
11492 STRCPY(tabvarname + 2, varname);
11493 set_var(tabvarname, varp, TRUE);
11494 vim_free(tabvarname);
11495 }
11496
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011497 /* Restore current tabpage */
11498 if (valid_tabpage(save_curtab))
11499 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011500 }
11501}
11502
11503/*
11504 * "settabwinvar()" function
11505 */
11506 static void
11507f_settabwinvar(typval_T *argvars, typval_T *rettv)
11508{
11509 setwinvar(argvars, rettv, 1);
11510}
11511
11512/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011513 * "settagstack()" function
11514 */
11515 static void
11516f_settagstack(typval_T *argvars, typval_T *rettv)
11517{
11518 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11519 win_T *wp;
11520 dict_T *d;
11521 int action = 'r';
11522
11523 rettv->vval.v_number = -1;
11524
11525 // first argument: window number or id
11526 wp = find_win_by_nr_or_id(&argvars[0]);
11527 if (wp == NULL)
11528 return;
11529
11530 // second argument: dict with items to set in the tag stack
11531 if (argvars[1].v_type != VAR_DICT)
11532 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011533 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011534 return;
11535 }
11536 d = argvars[1].vval.v_dict;
11537 if (d == NULL)
11538 return;
11539
11540 // third argument: action - 'a' for append and 'r' for replace.
11541 // default is to replace the stack.
11542 if (argvars[2].v_type == VAR_UNKNOWN)
11543 action = 'r';
11544 else if (argvars[2].v_type == VAR_STRING)
11545 {
11546 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011547 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011548 if (actstr == NULL)
11549 return;
11550 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11551 action = *actstr;
11552 else
11553 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011554 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011555 return;
11556 }
11557 }
11558 else
11559 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011560 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011561 return;
11562 }
11563
11564 if (set_tagstack(wp, d, action) == OK)
11565 rettv->vval.v_number = 0;
11566}
11567
11568/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011569 * "setwinvar()" function
11570 */
11571 static void
11572f_setwinvar(typval_T *argvars, typval_T *rettv)
11573{
11574 setwinvar(argvars, rettv, 0);
11575}
11576
11577#ifdef FEAT_CRYPT
11578/*
11579 * "sha256({string})" function
11580 */
11581 static void
11582f_sha256(typval_T *argvars, typval_T *rettv)
11583{
11584 char_u *p;
11585
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011586 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011587 rettv->vval.v_string = vim_strsave(
11588 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11589 rettv->v_type = VAR_STRING;
11590}
11591#endif /* FEAT_CRYPT */
11592
11593/*
11594 * "shellescape({string})" function
11595 */
11596 static void
11597f_shellescape(typval_T *argvars, typval_T *rettv)
11598{
Bram Moolenaar20615522017-06-05 18:46:26 +020011599 int do_special = non_zero_arg(&argvars[1]);
11600
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011601 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011602 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011603 rettv->v_type = VAR_STRING;
11604}
11605
11606/*
11607 * shiftwidth() function
11608 */
11609 static void
11610f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11611{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011612 rettv->vval.v_number = 0;
11613
11614 if (argvars[0].v_type != VAR_UNKNOWN)
11615 {
11616 long col;
11617
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011618 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011619 if (col < 0)
11620 return; // type error; errmsg already given
11621#ifdef FEAT_VARTABS
11622 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11623 return;
11624#endif
11625 }
11626
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011627 rettv->vval.v_number = get_sw_value(curbuf);
11628}
11629
11630/*
11631 * "simplify()" function
11632 */
11633 static void
11634f_simplify(typval_T *argvars, typval_T *rettv)
11635{
11636 char_u *p;
11637
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011638 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011639 rettv->vval.v_string = vim_strsave(p);
11640 simplify_filename(rettv->vval.v_string); /* simplify in place */
11641 rettv->v_type = VAR_STRING;
11642}
11643
11644#ifdef FEAT_FLOAT
11645/*
11646 * "sin()" function
11647 */
11648 static void
11649f_sin(typval_T *argvars, typval_T *rettv)
11650{
11651 float_T f = 0.0;
11652
11653 rettv->v_type = VAR_FLOAT;
11654 if (get_float_arg(argvars, &f) == OK)
11655 rettv->vval.v_float = sin(f);
11656 else
11657 rettv->vval.v_float = 0.0;
11658}
11659
11660/*
11661 * "sinh()" function
11662 */
11663 static void
11664f_sinh(typval_T *argvars, typval_T *rettv)
11665{
11666 float_T f = 0.0;
11667
11668 rettv->v_type = VAR_FLOAT;
11669 if (get_float_arg(argvars, &f) == OK)
11670 rettv->vval.v_float = sinh(f);
11671 else
11672 rettv->vval.v_float = 0.0;
11673}
11674#endif
11675
Bram Moolenaareae1b912019-05-09 15:12:55 +020011676static int item_compare(const void *s1, const void *s2);
11677static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011678
11679/* struct used in the array that's given to qsort() */
11680typedef struct
11681{
11682 listitem_T *item;
11683 int idx;
11684} sortItem_T;
11685
11686/* struct storing information about current sort */
11687typedef struct
11688{
11689 int item_compare_ic;
11690 int item_compare_numeric;
11691 int item_compare_numbers;
11692#ifdef FEAT_FLOAT
11693 int item_compare_float;
11694#endif
11695 char_u *item_compare_func;
11696 partial_T *item_compare_partial;
11697 dict_T *item_compare_selfdict;
11698 int item_compare_func_err;
11699 int item_compare_keep_zero;
11700} sortinfo_T;
11701static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011702#define ITEM_COMPARE_FAIL 999
11703
11704/*
11705 * Compare functions for f_sort() and f_uniq() below.
11706 */
11707 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011708item_compare(const void *s1, const void *s2)
11709{
11710 sortItem_T *si1, *si2;
11711 typval_T *tv1, *tv2;
11712 char_u *p1, *p2;
11713 char_u *tofree1 = NULL, *tofree2 = NULL;
11714 int res;
11715 char_u numbuf1[NUMBUFLEN];
11716 char_u numbuf2[NUMBUFLEN];
11717
11718 si1 = (sortItem_T *)s1;
11719 si2 = (sortItem_T *)s2;
11720 tv1 = &si1->item->li_tv;
11721 tv2 = &si2->item->li_tv;
11722
11723 if (sortinfo->item_compare_numbers)
11724 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011725 varnumber_T v1 = tv_get_number(tv1);
11726 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011727
11728 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11729 }
11730
11731#ifdef FEAT_FLOAT
11732 if (sortinfo->item_compare_float)
11733 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011734 float_T v1 = tv_get_float(tv1);
11735 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011736
11737 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11738 }
11739#endif
11740
11741 /* tv2string() puts quotes around a string and allocates memory. Don't do
11742 * that for string variables. Use a single quote when comparing with a
11743 * non-string to do what the docs promise. */
11744 if (tv1->v_type == VAR_STRING)
11745 {
11746 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11747 p1 = (char_u *)"'";
11748 else
11749 p1 = tv1->vval.v_string;
11750 }
11751 else
11752 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11753 if (tv2->v_type == VAR_STRING)
11754 {
11755 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11756 p2 = (char_u *)"'";
11757 else
11758 p2 = tv2->vval.v_string;
11759 }
11760 else
11761 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11762 if (p1 == NULL)
11763 p1 = (char_u *)"";
11764 if (p2 == NULL)
11765 p2 = (char_u *)"";
11766 if (!sortinfo->item_compare_numeric)
11767 {
11768 if (sortinfo->item_compare_ic)
11769 res = STRICMP(p1, p2);
11770 else
11771 res = STRCMP(p1, p2);
11772 }
11773 else
11774 {
11775 double n1, n2;
11776 n1 = strtod((char *)p1, (char **)&p1);
11777 n2 = strtod((char *)p2, (char **)&p2);
11778 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11779 }
11780
11781 /* When the result would be zero, compare the item indexes. Makes the
11782 * sort stable. */
11783 if (res == 0 && !sortinfo->item_compare_keep_zero)
11784 res = si1->idx > si2->idx ? 1 : -1;
11785
11786 vim_free(tofree1);
11787 vim_free(tofree2);
11788 return res;
11789}
11790
11791 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011792item_compare2(const void *s1, const void *s2)
11793{
11794 sortItem_T *si1, *si2;
11795 int res;
11796 typval_T rettv;
11797 typval_T argv[3];
11798 int dummy;
11799 char_u *func_name;
11800 partial_T *partial = sortinfo->item_compare_partial;
11801
11802 /* shortcut after failure in previous call; compare all items equal */
11803 if (sortinfo->item_compare_func_err)
11804 return 0;
11805
11806 si1 = (sortItem_T *)s1;
11807 si2 = (sortItem_T *)s2;
11808
11809 if (partial == NULL)
11810 func_name = sortinfo->item_compare_func;
11811 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011812 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011813
11814 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11815 * in the copy without changing the original list items. */
11816 copy_tv(&si1->item->li_tv, &argv[0]);
11817 copy_tv(&si2->item->li_tv, &argv[1]);
11818
11819 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020011820 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011821 partial, sortinfo->item_compare_selfdict);
11822 clear_tv(&argv[0]);
11823 clear_tv(&argv[1]);
11824
11825 if (res == FAIL)
11826 res = ITEM_COMPARE_FAIL;
11827 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011828 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011829 if (sortinfo->item_compare_func_err)
11830 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11831 clear_tv(&rettv);
11832
11833 /* When the result would be zero, compare the pointers themselves. Makes
11834 * the sort stable. */
11835 if (res == 0 && !sortinfo->item_compare_keep_zero)
11836 res = si1->idx > si2->idx ? 1 : -1;
11837
11838 return res;
11839}
11840
11841/*
11842 * "sort({list})" function
11843 */
11844 static void
11845do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11846{
11847 list_T *l;
11848 listitem_T *li;
11849 sortItem_T *ptrs;
11850 sortinfo_T *old_sortinfo;
11851 sortinfo_T info;
11852 long len;
11853 long i;
11854
11855 /* Pointer to current info struct used in compare function. Save and
11856 * restore the current one for nested calls. */
11857 old_sortinfo = sortinfo;
11858 sortinfo = &info;
11859
11860 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011861 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011862 else
11863 {
11864 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011865 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011866 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11867 TRUE))
11868 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011869 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011870
11871 len = list_len(l);
11872 if (len <= 1)
11873 goto theend; /* short list sorts pretty quickly */
11874
11875 info.item_compare_ic = FALSE;
11876 info.item_compare_numeric = FALSE;
11877 info.item_compare_numbers = FALSE;
11878#ifdef FEAT_FLOAT
11879 info.item_compare_float = FALSE;
11880#endif
11881 info.item_compare_func = NULL;
11882 info.item_compare_partial = NULL;
11883 info.item_compare_selfdict = NULL;
11884 if (argvars[1].v_type != VAR_UNKNOWN)
11885 {
11886 /* optional second argument: {func} */
11887 if (argvars[1].v_type == VAR_FUNC)
11888 info.item_compare_func = argvars[1].vval.v_string;
11889 else if (argvars[1].v_type == VAR_PARTIAL)
11890 info.item_compare_partial = argvars[1].vval.v_partial;
11891 else
11892 {
11893 int error = FALSE;
11894
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011895 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011896 if (error)
11897 goto theend; /* type error; errmsg already given */
11898 if (i == 1)
11899 info.item_compare_ic = TRUE;
11900 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011901 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011902 else if (i != 0)
11903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011904 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011905 goto theend;
11906 }
11907 if (info.item_compare_func != NULL)
11908 {
11909 if (*info.item_compare_func == NUL)
11910 {
11911 /* empty string means default sort */
11912 info.item_compare_func = NULL;
11913 }
11914 else if (STRCMP(info.item_compare_func, "n") == 0)
11915 {
11916 info.item_compare_func = NULL;
11917 info.item_compare_numeric = TRUE;
11918 }
11919 else if (STRCMP(info.item_compare_func, "N") == 0)
11920 {
11921 info.item_compare_func = NULL;
11922 info.item_compare_numbers = TRUE;
11923 }
11924#ifdef FEAT_FLOAT
11925 else if (STRCMP(info.item_compare_func, "f") == 0)
11926 {
11927 info.item_compare_func = NULL;
11928 info.item_compare_float = TRUE;
11929 }
11930#endif
11931 else if (STRCMP(info.item_compare_func, "i") == 0)
11932 {
11933 info.item_compare_func = NULL;
11934 info.item_compare_ic = TRUE;
11935 }
11936 }
11937 }
11938
11939 if (argvars[2].v_type != VAR_UNKNOWN)
11940 {
11941 /* optional third argument: {dict} */
11942 if (argvars[2].v_type != VAR_DICT)
11943 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011944 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011945 goto theend;
11946 }
11947 info.item_compare_selfdict = argvars[2].vval.v_dict;
11948 }
11949 }
11950
11951 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011952 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011953 if (ptrs == NULL)
11954 goto theend;
11955
11956 i = 0;
11957 if (sort)
11958 {
11959 /* sort(): ptrs will be the list to sort */
11960 for (li = l->lv_first; li != NULL; li = li->li_next)
11961 {
11962 ptrs[i].item = li;
11963 ptrs[i].idx = i;
11964 ++i;
11965 }
11966
11967 info.item_compare_func_err = FALSE;
11968 info.item_compare_keep_zero = FALSE;
11969 /* test the compare function */
11970 if ((info.item_compare_func != NULL
11971 || info.item_compare_partial != NULL)
11972 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11973 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011974 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011975 else
11976 {
11977 /* Sort the array with item pointers. */
11978 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11979 info.item_compare_func == NULL
11980 && info.item_compare_partial == NULL
11981 ? item_compare : item_compare2);
11982
11983 if (!info.item_compare_func_err)
11984 {
11985 /* Clear the List and append the items in sorted order. */
11986 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11987 l->lv_len = 0;
11988 for (i = 0; i < len; ++i)
11989 list_append(l, ptrs[i].item);
11990 }
11991 }
11992 }
11993 else
11994 {
11995 int (*item_compare_func_ptr)(const void *, const void *);
11996
11997 /* f_uniq(): ptrs will be a stack of items to remove */
11998 info.item_compare_func_err = FALSE;
11999 info.item_compare_keep_zero = TRUE;
12000 item_compare_func_ptr = info.item_compare_func != NULL
12001 || info.item_compare_partial != NULL
12002 ? item_compare2 : item_compare;
12003
12004 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12005 li = li->li_next)
12006 {
12007 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12008 == 0)
12009 ptrs[i++].item = li;
12010 if (info.item_compare_func_err)
12011 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012012 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012013 break;
12014 }
12015 }
12016
12017 if (!info.item_compare_func_err)
12018 {
12019 while (--i >= 0)
12020 {
12021 li = ptrs[i].item->li_next;
12022 ptrs[i].item->li_next = li->li_next;
12023 if (li->li_next != NULL)
12024 li->li_next->li_prev = ptrs[i].item;
12025 else
12026 l->lv_last = ptrs[i].item;
12027 list_fix_watch(l, li);
12028 listitem_free(li);
12029 l->lv_len--;
12030 }
12031 }
12032 }
12033
12034 vim_free(ptrs);
12035 }
12036theend:
12037 sortinfo = old_sortinfo;
12038}
12039
12040/*
12041 * "sort({list})" function
12042 */
12043 static void
12044f_sort(typval_T *argvars, typval_T *rettv)
12045{
12046 do_sort_uniq(argvars, rettv, TRUE);
12047}
12048
12049/*
12050 * "uniq({list})" function
12051 */
12052 static void
12053f_uniq(typval_T *argvars, typval_T *rettv)
12054{
12055 do_sort_uniq(argvars, rettv, FALSE);
12056}
12057
12058/*
12059 * "soundfold({word})" function
12060 */
12061 static void
12062f_soundfold(typval_T *argvars, typval_T *rettv)
12063{
12064 char_u *s;
12065
12066 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012067 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012068#ifdef FEAT_SPELL
12069 rettv->vval.v_string = eval_soundfold(s);
12070#else
12071 rettv->vval.v_string = vim_strsave(s);
12072#endif
12073}
12074
12075/*
12076 * "spellbadword()" function
12077 */
12078 static void
12079f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12080{
12081 char_u *word = (char_u *)"";
12082 hlf_T attr = HLF_COUNT;
12083 int len = 0;
12084
12085 if (rettv_list_alloc(rettv) == FAIL)
12086 return;
12087
12088#ifdef FEAT_SPELL
12089 if (argvars[0].v_type == VAR_UNKNOWN)
12090 {
12091 /* Find the start and length of the badly spelled word. */
12092 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12093 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012094 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012095 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012096 curwin->w_set_curswant = TRUE;
12097 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012098 }
12099 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12100 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012101 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012102 int capcol = -1;
12103
12104 if (str != NULL)
12105 {
12106 /* Check the argument for spelling. */
12107 while (*str != NUL)
12108 {
12109 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12110 if (attr != HLF_COUNT)
12111 {
12112 word = str;
12113 break;
12114 }
12115 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012116 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012117 }
12118 }
12119 }
12120#endif
12121
12122 list_append_string(rettv->vval.v_list, word, len);
12123 list_append_string(rettv->vval.v_list, (char_u *)(
12124 attr == HLF_SPB ? "bad" :
12125 attr == HLF_SPR ? "rare" :
12126 attr == HLF_SPL ? "local" :
12127 attr == HLF_SPC ? "caps" :
12128 ""), -1);
12129}
12130
12131/*
12132 * "spellsuggest()" function
12133 */
12134 static void
12135f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12136{
12137#ifdef FEAT_SPELL
12138 char_u *str;
12139 int typeerr = FALSE;
12140 int maxcount;
12141 garray_T ga;
12142 int i;
12143 listitem_T *li;
12144 int need_capital = FALSE;
12145#endif
12146
12147 if (rettv_list_alloc(rettv) == FAIL)
12148 return;
12149
12150#ifdef FEAT_SPELL
12151 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12152 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012153 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012154 if (argvars[1].v_type != VAR_UNKNOWN)
12155 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012156 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012157 if (maxcount <= 0)
12158 return;
12159 if (argvars[2].v_type != VAR_UNKNOWN)
12160 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012161 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012162 if (typeerr)
12163 return;
12164 }
12165 }
12166 else
12167 maxcount = 25;
12168
12169 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12170
12171 for (i = 0; i < ga.ga_len; ++i)
12172 {
12173 str = ((char_u **)ga.ga_data)[i];
12174
12175 li = listitem_alloc();
12176 if (li == NULL)
12177 vim_free(str);
12178 else
12179 {
12180 li->li_tv.v_type = VAR_STRING;
12181 li->li_tv.v_lock = 0;
12182 li->li_tv.vval.v_string = str;
12183 list_append(rettv->vval.v_list, li);
12184 }
12185 }
12186 ga_clear(&ga);
12187 }
12188#endif
12189}
12190
12191 static void
12192f_split(typval_T *argvars, typval_T *rettv)
12193{
12194 char_u *str;
12195 char_u *end;
12196 char_u *pat = NULL;
12197 regmatch_T regmatch;
12198 char_u patbuf[NUMBUFLEN];
12199 char_u *save_cpo;
12200 int match;
12201 colnr_T col = 0;
12202 int keepempty = FALSE;
12203 int typeerr = FALSE;
12204
12205 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12206 save_cpo = p_cpo;
12207 p_cpo = (char_u *)"";
12208
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012209 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012210 if (argvars[1].v_type != VAR_UNKNOWN)
12211 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012212 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012213 if (pat == NULL)
12214 typeerr = TRUE;
12215 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012216 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012217 }
12218 if (pat == NULL || *pat == NUL)
12219 pat = (char_u *)"[\\x01- ]\\+";
12220
12221 if (rettv_list_alloc(rettv) == FAIL)
12222 return;
12223 if (typeerr)
12224 return;
12225
12226 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12227 if (regmatch.regprog != NULL)
12228 {
12229 regmatch.rm_ic = FALSE;
12230 while (*str != NUL || keepempty)
12231 {
12232 if (*str == NUL)
12233 match = FALSE; /* empty item at the end */
12234 else
12235 match = vim_regexec_nl(&regmatch, str, col);
12236 if (match)
12237 end = regmatch.startp[0];
12238 else
12239 end = str + STRLEN(str);
12240 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12241 && *str != NUL && match && end < regmatch.endp[0]))
12242 {
12243 if (list_append_string(rettv->vval.v_list, str,
12244 (int)(end - str)) == FAIL)
12245 break;
12246 }
12247 if (!match)
12248 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012249 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012250 if (regmatch.endp[0] > str)
12251 col = 0;
12252 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012253 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012254 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012255 str = regmatch.endp[0];
12256 }
12257
12258 vim_regfree(regmatch.regprog);
12259 }
12260
12261 p_cpo = save_cpo;
12262}
12263
12264#ifdef FEAT_FLOAT
12265/*
12266 * "sqrt()" function
12267 */
12268 static void
12269f_sqrt(typval_T *argvars, typval_T *rettv)
12270{
12271 float_T f = 0.0;
12272
12273 rettv->v_type = VAR_FLOAT;
12274 if (get_float_arg(argvars, &f) == OK)
12275 rettv->vval.v_float = sqrt(f);
12276 else
12277 rettv->vval.v_float = 0.0;
12278}
12279
12280/*
12281 * "str2float()" function
12282 */
12283 static void
12284f_str2float(typval_T *argvars, typval_T *rettv)
12285{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012286 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012287 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012288
Bram Moolenaar08243d22017-01-10 16:12:29 +010012289 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012290 p = skipwhite(p + 1);
12291 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012292 if (isneg)
12293 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012294 rettv->v_type = VAR_FLOAT;
12295}
12296#endif
12297
12298/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012299 * "str2list()" function
12300 */
12301 static void
12302f_str2list(typval_T *argvars, typval_T *rettv)
12303{
12304 char_u *p;
12305 int utf8 = FALSE;
12306
12307 if (rettv_list_alloc(rettv) == FAIL)
12308 return;
12309
12310 if (argvars[1].v_type != VAR_UNKNOWN)
12311 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12312
12313 p = tv_get_string(&argvars[0]);
12314
12315 if (has_mbyte || utf8)
12316 {
12317 int (*ptr2len)(char_u *);
12318 int (*ptr2char)(char_u *);
12319
12320 if (utf8 || enc_utf8)
12321 {
12322 ptr2len = utf_ptr2len;
12323 ptr2char = utf_ptr2char;
12324 }
12325 else
12326 {
12327 ptr2len = mb_ptr2len;
12328 ptr2char = mb_ptr2char;
12329 }
12330
12331 for ( ; *p != NUL; p += (*ptr2len)(p))
12332 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
12333 }
12334 else
12335 for ( ; *p != NUL; ++p)
12336 list_append_number(rettv->vval.v_list, *p);
12337}
12338
12339/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012340 * "str2nr()" function
12341 */
12342 static void
12343f_str2nr(typval_T *argvars, typval_T *rettv)
12344{
12345 int base = 10;
12346 char_u *p;
12347 varnumber_T n;
12348 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012349 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012350
12351 if (argvars[1].v_type != VAR_UNKNOWN)
12352 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012353 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012354 if (base != 2 && base != 8 && base != 10 && base != 16)
12355 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012356 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012357 return;
12358 }
12359 }
12360
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012361 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012362 isneg = (*p == '-');
12363 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012364 p = skipwhite(p + 1);
12365 switch (base)
12366 {
12367 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12368 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12369 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12370 default: what = 0;
12371 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020012372 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
12373 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010012374 if (isneg)
12375 rettv->vval.v_number = -n;
12376 else
12377 rettv->vval.v_number = n;
12378
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012379}
12380
12381#ifdef HAVE_STRFTIME
12382/*
12383 * "strftime({format}[, {time}])" function
12384 */
12385 static void
12386f_strftime(typval_T *argvars, typval_T *rettv)
12387{
12388 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020012389 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012390 struct tm *curtime;
12391 time_t seconds;
12392 char_u *p;
12393
12394 rettv->v_type = VAR_STRING;
12395
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012396 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012397 if (argvars[1].v_type == VAR_UNKNOWN)
12398 seconds = time(NULL);
12399 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012400 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020012401 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012402 /* MSVC returns NULL for an invalid value of seconds. */
12403 if (curtime == NULL)
12404 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12405 else
12406 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012407 vimconv_T conv;
12408 char_u *enc;
12409
12410 conv.vc_type = CONV_NONE;
12411 enc = enc_locale();
12412 convert_setup(&conv, p_enc, enc);
12413 if (conv.vc_type != CONV_NONE)
12414 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012415 if (p != NULL)
12416 (void)strftime((char *)result_buf, sizeof(result_buf),
12417 (char *)p, curtime);
12418 else
12419 result_buf[0] = NUL;
12420
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012421 if (conv.vc_type != CONV_NONE)
12422 vim_free(p);
12423 convert_setup(&conv, enc, p_enc);
12424 if (conv.vc_type != CONV_NONE)
12425 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12426 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012427 rettv->vval.v_string = vim_strsave(result_buf);
12428
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012429 /* Release conversion descriptors */
12430 convert_setup(&conv, NULL, NULL);
12431 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012432 }
12433}
12434#endif
12435
12436/*
12437 * "strgetchar()" function
12438 */
12439 static void
12440f_strgetchar(typval_T *argvars, typval_T *rettv)
12441{
12442 char_u *str;
12443 int len;
12444 int error = FALSE;
12445 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012446 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012447
12448 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012449 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450 if (str == NULL)
12451 return;
12452 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012453 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012454 if (error)
12455 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012456
Bram Moolenaar13505972019-01-24 15:04:48 +010012457 while (charidx >= 0 && byteidx < len)
12458 {
12459 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012460 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012461 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12462 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012463 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012464 --charidx;
12465 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012466 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012467}
12468
12469/*
12470 * "stridx()" function
12471 */
12472 static void
12473f_stridx(typval_T *argvars, typval_T *rettv)
12474{
12475 char_u buf[NUMBUFLEN];
12476 char_u *needle;
12477 char_u *haystack;
12478 char_u *save_haystack;
12479 char_u *pos;
12480 int start_idx;
12481
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012482 needle = tv_get_string_chk(&argvars[1]);
12483 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012484 rettv->vval.v_number = -1;
12485 if (needle == NULL || haystack == NULL)
12486 return; /* type error; errmsg already given */
12487
12488 if (argvars[2].v_type != VAR_UNKNOWN)
12489 {
12490 int error = FALSE;
12491
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012492 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012493 if (error || start_idx >= (int)STRLEN(haystack))
12494 return;
12495 if (start_idx >= 0)
12496 haystack += start_idx;
12497 }
12498
12499 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12500 if (pos != NULL)
12501 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12502}
12503
12504/*
12505 * "string()" function
12506 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012507 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012508f_string(typval_T *argvars, typval_T *rettv)
12509{
12510 char_u *tofree;
12511 char_u numbuf[NUMBUFLEN];
12512
12513 rettv->v_type = VAR_STRING;
12514 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12515 get_copyID());
12516 /* Make a copy if we have a value but it's not in allocated memory. */
12517 if (rettv->vval.v_string != NULL && tofree == NULL)
12518 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12519}
12520
12521/*
12522 * "strlen()" function
12523 */
12524 static void
12525f_strlen(typval_T *argvars, typval_T *rettv)
12526{
12527 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012528 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012529}
12530
12531/*
12532 * "strchars()" function
12533 */
12534 static void
12535f_strchars(typval_T *argvars, typval_T *rettv)
12536{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012537 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012538 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012539 varnumber_T len = 0;
12540 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012541
12542 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012543 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012544 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012545 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012546 else
12547 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012548 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12549 while (*s != NUL)
12550 {
12551 func_mb_ptr2char_adv(&s);
12552 ++len;
12553 }
12554 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012555 }
12556}
12557
12558/*
12559 * "strdisplaywidth()" function
12560 */
12561 static void
12562f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12563{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012564 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012565 int col = 0;
12566
12567 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012568 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012569
12570 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12571}
12572
12573/*
12574 * "strwidth()" function
12575 */
12576 static void
12577f_strwidth(typval_T *argvars, typval_T *rettv)
12578{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012579 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012580
Bram Moolenaar13505972019-01-24 15:04:48 +010012581 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012582}
12583
12584/*
12585 * "strcharpart()" function
12586 */
12587 static void
12588f_strcharpart(typval_T *argvars, typval_T *rettv)
12589{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012590 char_u *p;
12591 int nchar;
12592 int nbyte = 0;
12593 int charlen;
12594 int len = 0;
12595 int slen;
12596 int error = FALSE;
12597
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012598 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012599 slen = (int)STRLEN(p);
12600
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012601 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012602 if (!error)
12603 {
12604 if (nchar > 0)
12605 while (nchar > 0 && nbyte < slen)
12606 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012607 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012608 --nchar;
12609 }
12610 else
12611 nbyte = nchar;
12612 if (argvars[2].v_type != VAR_UNKNOWN)
12613 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012614 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012615 while (charlen > 0 && nbyte + len < slen)
12616 {
12617 int off = nbyte + len;
12618
12619 if (off < 0)
12620 len += 1;
12621 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012622 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012623 --charlen;
12624 }
12625 }
12626 else
12627 len = slen - nbyte; /* default: all bytes that are available. */
12628 }
12629
12630 /*
12631 * Only return the overlap between the specified part and the actual
12632 * string.
12633 */
12634 if (nbyte < 0)
12635 {
12636 len += nbyte;
12637 nbyte = 0;
12638 }
12639 else if (nbyte > slen)
12640 nbyte = slen;
12641 if (len < 0)
12642 len = 0;
12643 else if (nbyte + len > slen)
12644 len = slen - nbyte;
12645
12646 rettv->v_type = VAR_STRING;
12647 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012648}
12649
12650/*
12651 * "strpart()" function
12652 */
12653 static void
12654f_strpart(typval_T *argvars, typval_T *rettv)
12655{
12656 char_u *p;
12657 int n;
12658 int len;
12659 int slen;
12660 int error = FALSE;
12661
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012662 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012663 slen = (int)STRLEN(p);
12664
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012665 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012666 if (error)
12667 len = 0;
12668 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012669 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012670 else
12671 len = slen - n; /* default len: all bytes that are available. */
12672
12673 /*
12674 * Only return the overlap between the specified part and the actual
12675 * string.
12676 */
12677 if (n < 0)
12678 {
12679 len += n;
12680 n = 0;
12681 }
12682 else if (n > slen)
12683 n = slen;
12684 if (len < 0)
12685 len = 0;
12686 else if (n + len > slen)
12687 len = slen - n;
12688
12689 rettv->v_type = VAR_STRING;
12690 rettv->vval.v_string = vim_strnsave(p + n, len);
12691}
12692
12693/*
12694 * "strridx()" function
12695 */
12696 static void
12697f_strridx(typval_T *argvars, typval_T *rettv)
12698{
12699 char_u buf[NUMBUFLEN];
12700 char_u *needle;
12701 char_u *haystack;
12702 char_u *rest;
12703 char_u *lastmatch = NULL;
12704 int haystack_len, end_idx;
12705
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012706 needle = tv_get_string_chk(&argvars[1]);
12707 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012708
12709 rettv->vval.v_number = -1;
12710 if (needle == NULL || haystack == NULL)
12711 return; /* type error; errmsg already given */
12712
12713 haystack_len = (int)STRLEN(haystack);
12714 if (argvars[2].v_type != VAR_UNKNOWN)
12715 {
12716 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012717 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012718 if (end_idx < 0)
12719 return; /* can never find a match */
12720 }
12721 else
12722 end_idx = haystack_len;
12723
12724 if (*needle == NUL)
12725 {
12726 /* Empty string matches past the end. */
12727 lastmatch = haystack + end_idx;
12728 }
12729 else
12730 {
12731 for (rest = haystack; *rest != '\0'; ++rest)
12732 {
12733 rest = (char_u *)strstr((char *)rest, (char *)needle);
12734 if (rest == NULL || rest > haystack + end_idx)
12735 break;
12736 lastmatch = rest;
12737 }
12738 }
12739
12740 if (lastmatch == NULL)
12741 rettv->vval.v_number = -1;
12742 else
12743 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12744}
12745
12746/*
12747 * "strtrans()" function
12748 */
12749 static void
12750f_strtrans(typval_T *argvars, typval_T *rettv)
12751{
12752 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012753 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012754}
12755
12756/*
12757 * "submatch()" function
12758 */
12759 static void
12760f_submatch(typval_T *argvars, typval_T *rettv)
12761{
12762 int error = FALSE;
12763 int no;
12764 int retList = 0;
12765
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012766 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012767 if (error)
12768 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012769 if (no < 0 || no >= NSUBEXP)
12770 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012771 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012772 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012773 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012774 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012775 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012776 if (error)
12777 return;
12778
12779 if (retList == 0)
12780 {
12781 rettv->v_type = VAR_STRING;
12782 rettv->vval.v_string = reg_submatch(no);
12783 }
12784 else
12785 {
12786 rettv->v_type = VAR_LIST;
12787 rettv->vval.v_list = reg_submatch_list(no);
12788 }
12789}
12790
12791/*
12792 * "substitute()" function
12793 */
12794 static void
12795f_substitute(typval_T *argvars, typval_T *rettv)
12796{
12797 char_u patbuf[NUMBUFLEN];
12798 char_u subbuf[NUMBUFLEN];
12799 char_u flagsbuf[NUMBUFLEN];
12800
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012801 char_u *str = tv_get_string_chk(&argvars[0]);
12802 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012803 char_u *sub = NULL;
12804 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012805 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012806
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012807 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12808 expr = &argvars[2];
12809 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012810 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012811
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012812 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012813 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12814 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012815 rettv->vval.v_string = NULL;
12816 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012817 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012818}
12819
12820/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012821 * "swapinfo(swap_filename)" function
12822 */
12823 static void
12824f_swapinfo(typval_T *argvars, typval_T *rettv)
12825{
12826 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012827 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012828}
12829
12830/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012831 * "swapname(expr)" function
12832 */
12833 static void
12834f_swapname(typval_T *argvars, typval_T *rettv)
12835{
12836 buf_T *buf;
12837
12838 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010012839 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020012840 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12841 || buf->b_ml.ml_mfp->mf_fname == NULL)
12842 rettv->vval.v_string = NULL;
12843 else
12844 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12845}
12846
12847/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012848 * "synID(lnum, col, trans)" function
12849 */
12850 static void
12851f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12852{
12853 int id = 0;
12854#ifdef FEAT_SYN_HL
12855 linenr_T lnum;
12856 colnr_T col;
12857 int trans;
12858 int transerr = FALSE;
12859
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012860 lnum = tv_get_lnum(argvars); /* -1 on type error */
12861 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12862 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012863
12864 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12865 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12866 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12867#endif
12868
12869 rettv->vval.v_number = id;
12870}
12871
12872/*
12873 * "synIDattr(id, what [, mode])" function
12874 */
12875 static void
12876f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12877{
12878 char_u *p = NULL;
12879#ifdef FEAT_SYN_HL
12880 int id;
12881 char_u *what;
12882 char_u *mode;
12883 char_u modebuf[NUMBUFLEN];
12884 int modec;
12885
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012886 id = (int)tv_get_number(&argvars[0]);
12887 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012888 if (argvars[2].v_type != VAR_UNKNOWN)
12889 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012890 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012891 modec = TOLOWER_ASC(mode[0]);
12892 if (modec != 't' && modec != 'c' && modec != 'g')
12893 modec = 0; /* replace invalid with current */
12894 }
12895 else
12896 {
12897#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12898 if (USE_24BIT)
12899 modec = 'g';
12900 else
12901#endif
12902 if (t_colors > 1)
12903 modec = 'c';
12904 else
12905 modec = 't';
12906 }
12907
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012908 switch (TOLOWER_ASC(what[0]))
12909 {
12910 case 'b':
12911 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12912 p = highlight_color(id, what, modec);
12913 else /* bold */
12914 p = highlight_has_attr(id, HL_BOLD, modec);
12915 break;
12916
12917 case 'f': /* fg[#] or font */
12918 p = highlight_color(id, what, modec);
12919 break;
12920
12921 case 'i':
12922 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12923 p = highlight_has_attr(id, HL_INVERSE, modec);
12924 else /* italic */
12925 p = highlight_has_attr(id, HL_ITALIC, modec);
12926 break;
12927
12928 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012929 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012930 break;
12931
12932 case 'r': /* reverse */
12933 p = highlight_has_attr(id, HL_INVERSE, modec);
12934 break;
12935
12936 case 's':
12937 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12938 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012939 /* strikeout */
12940 else if (TOLOWER_ASC(what[1]) == 't' &&
12941 TOLOWER_ASC(what[2]) == 'r')
12942 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012943 else /* standout */
12944 p = highlight_has_attr(id, HL_STANDOUT, modec);
12945 break;
12946
12947 case 'u':
12948 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12949 /* underline */
12950 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12951 else
12952 /* undercurl */
12953 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12954 break;
12955 }
12956
12957 if (p != NULL)
12958 p = vim_strsave(p);
12959#endif
12960 rettv->v_type = VAR_STRING;
12961 rettv->vval.v_string = p;
12962}
12963
12964/*
12965 * "synIDtrans(id)" function
12966 */
12967 static void
12968f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12969{
12970 int id;
12971
12972#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012973 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012974
12975 if (id > 0)
12976 id = syn_get_final_id(id);
12977 else
12978#endif
12979 id = 0;
12980
12981 rettv->vval.v_number = id;
12982}
12983
12984/*
12985 * "synconcealed(lnum, col)" function
12986 */
12987 static void
12988f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12989{
12990#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12991 linenr_T lnum;
12992 colnr_T col;
12993 int syntax_flags = 0;
12994 int cchar;
12995 int matchid = 0;
12996 char_u str[NUMBUFLEN];
12997#endif
12998
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012999 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013000
13001#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013002 lnum = tv_get_lnum(argvars); /* -1 on type error */
13003 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013004
13005 vim_memset(str, NUL, sizeof(str));
13006
13007 if (rettv_list_alloc(rettv) != FAIL)
13008 {
13009 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13010 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13011 && curwin->w_p_cole > 0)
13012 {
13013 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13014 syntax_flags = get_syntax_info(&matchid);
13015
13016 /* get the conceal character */
13017 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13018 {
13019 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013020 if (cchar == NUL && curwin->w_p_cole == 1)
13021 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013022 if (cchar != NUL)
13023 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013024 if (has_mbyte)
13025 (*mb_char2bytes)(cchar, str);
13026 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013027 str[0] = cchar;
13028 }
13029 }
13030 }
13031
13032 list_append_number(rettv->vval.v_list,
13033 (syntax_flags & HL_CONCEAL) != 0);
13034 /* -1 to auto-determine strlen */
13035 list_append_string(rettv->vval.v_list, str, -1);
13036 list_append_number(rettv->vval.v_list, matchid);
13037 }
13038#endif
13039}
13040
13041/*
13042 * "synstack(lnum, col)" function
13043 */
13044 static void
13045f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13046{
13047#ifdef FEAT_SYN_HL
13048 linenr_T lnum;
13049 colnr_T col;
13050 int i;
13051 int id;
13052#endif
13053
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013054 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013055
13056#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013057 lnum = tv_get_lnum(argvars); /* -1 on type error */
13058 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013059
13060 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13061 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13062 && rettv_list_alloc(rettv) != FAIL)
13063 {
13064 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13065 for (i = 0; ; ++i)
13066 {
13067 id = syn_get_stack_item(i);
13068 if (id < 0)
13069 break;
13070 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13071 break;
13072 }
13073 }
13074#endif
13075}
13076
13077 static void
13078get_cmd_output_as_rettv(
13079 typval_T *argvars,
13080 typval_T *rettv,
13081 int retlist)
13082{
13083 char_u *res = NULL;
13084 char_u *p;
13085 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013086 int err = FALSE;
13087 FILE *fd;
13088 list_T *list = NULL;
13089 int flags = SHELL_SILENT;
13090
13091 rettv->v_type = VAR_STRING;
13092 rettv->vval.v_string = NULL;
13093 if (check_restricted() || check_secure())
13094 goto errret;
13095
13096 if (argvars[1].v_type != VAR_UNKNOWN)
13097 {
13098 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013099 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013100 * command.
13101 */
13102 if ((infile = vim_tempname('i', TRUE)) == NULL)
13103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013104 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013105 goto errret;
13106 }
13107
13108 fd = mch_fopen((char *)infile, WRITEBIN);
13109 if (fd == NULL)
13110 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013111 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013112 goto errret;
13113 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013114 if (argvars[1].v_type == VAR_NUMBER)
13115 {
13116 linenr_T lnum;
13117 buf_T *buf;
13118
13119 buf = buflist_findnr(argvars[1].vval.v_number);
13120 if (buf == NULL)
13121 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013122 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013123 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013124 goto errret;
13125 }
13126
13127 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13128 {
13129 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13130 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13131 {
13132 err = TRUE;
13133 break;
13134 }
13135 if (putc(NL, fd) == EOF)
13136 {
13137 err = TRUE;
13138 break;
13139 }
13140 }
13141 }
13142 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013143 {
13144 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13145 err = TRUE;
13146 }
13147 else
13148 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013149 size_t len;
13150 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013151
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013152 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013153 if (p == NULL)
13154 {
13155 fclose(fd);
13156 goto errret; /* type error; errmsg already given */
13157 }
13158 len = STRLEN(p);
13159 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13160 err = TRUE;
13161 }
13162 if (fclose(fd) != 0)
13163 err = TRUE;
13164 if (err)
13165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013166 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013167 goto errret;
13168 }
13169 }
13170
13171 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13172 * echoes typeahead, that messes up the display. */
13173 if (!msg_silent)
13174 flags += SHELL_COOKED;
13175
13176 if (retlist)
13177 {
13178 int len;
13179 listitem_T *li;
13180 char_u *s = NULL;
13181 char_u *start;
13182 char_u *end;
13183 int i;
13184
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013185 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013186 if (res == NULL)
13187 goto errret;
13188
13189 list = list_alloc();
13190 if (list == NULL)
13191 goto errret;
13192
13193 for (i = 0; i < len; ++i)
13194 {
13195 start = res + i;
13196 while (i < len && res[i] != NL)
13197 ++i;
13198 end = res + i;
13199
Bram Moolenaar964b3742019-05-24 18:54:09 +020013200 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013201 if (s == NULL)
13202 goto errret;
13203
13204 for (p = s; start < end; ++p, ++start)
13205 *p = *start == NUL ? NL : *start;
13206 *p = NUL;
13207
13208 li = listitem_alloc();
13209 if (li == NULL)
13210 {
13211 vim_free(s);
13212 goto errret;
13213 }
13214 li->li_tv.v_type = VAR_STRING;
13215 li->li_tv.v_lock = 0;
13216 li->li_tv.vval.v_string = s;
13217 list_append(list, li);
13218 }
13219
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013220 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013221 list = NULL;
13222 }
13223 else
13224 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013225 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013226#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013227 /* translate <CR><NL> into <NL> */
13228 if (res != NULL)
13229 {
13230 char_u *s, *d;
13231
13232 d = res;
13233 for (s = res; *s; ++s)
13234 {
13235 if (s[0] == CAR && s[1] == NL)
13236 ++s;
13237 *d++ = *s;
13238 }
13239 *d = NUL;
13240 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013241#endif
13242 rettv->vval.v_string = res;
13243 res = NULL;
13244 }
13245
13246errret:
13247 if (infile != NULL)
13248 {
13249 mch_remove(infile);
13250 vim_free(infile);
13251 }
13252 if (res != NULL)
13253 vim_free(res);
13254 if (list != NULL)
13255 list_free(list);
13256}
13257
13258/*
13259 * "system()" function
13260 */
13261 static void
13262f_system(typval_T *argvars, typval_T *rettv)
13263{
13264 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13265}
13266
13267/*
13268 * "systemlist()" function
13269 */
13270 static void
13271f_systemlist(typval_T *argvars, typval_T *rettv)
13272{
13273 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13274}
13275
13276/*
13277 * "tabpagebuflist()" function
13278 */
13279 static void
13280f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13281{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013282 tabpage_T *tp;
13283 win_T *wp = NULL;
13284
13285 if (argvars[0].v_type == VAR_UNKNOWN)
13286 wp = firstwin;
13287 else
13288 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013289 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013290 if (tp != NULL)
13291 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13292 }
13293 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13294 {
13295 for (; wp != NULL; wp = wp->w_next)
13296 if (list_append_number(rettv->vval.v_list,
13297 wp->w_buffer->b_fnum) == FAIL)
13298 break;
13299 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013300}
13301
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013302/*
13303 * "tabpagenr()" function
13304 */
13305 static void
13306f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13307{
13308 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013309 char_u *arg;
13310
13311 if (argvars[0].v_type != VAR_UNKNOWN)
13312 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013313 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013314 nr = 0;
13315 if (arg != NULL)
13316 {
13317 if (STRCMP(arg, "$") == 0)
13318 nr = tabpage_index(NULL) - 1;
13319 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013320 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013321 }
13322 }
13323 else
13324 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013325 rettv->vval.v_number = nr;
13326}
13327
13328
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013329/*
13330 * Common code for tabpagewinnr() and winnr().
13331 */
13332 static int
13333get_winnr(tabpage_T *tp, typval_T *argvar)
13334{
13335 win_T *twin;
13336 int nr = 1;
13337 win_T *wp;
13338 char_u *arg;
13339
13340 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13341 if (argvar->v_type != VAR_UNKNOWN)
13342 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013343 int invalid_arg = FALSE;
13344
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013345 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013346 if (arg == NULL)
13347 nr = 0; /* type error; errmsg already given */
13348 else if (STRCMP(arg, "$") == 0)
13349 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13350 else if (STRCMP(arg, "#") == 0)
13351 {
13352 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13353 if (twin == NULL)
13354 nr = 0;
13355 }
13356 else
13357 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013358 long count;
13359 char_u *endp;
13360
13361 // Extract the window count (if specified). e.g. winnr('3j')
13362 count = strtol((char *)arg, (char **)&endp, 10);
13363 if (count <= 0)
13364 count = 1; // if count is not specified, default to 1
13365 if (endp != NULL && *endp != '\0')
13366 {
13367 if (STRCMP(endp, "j") == 0)
13368 twin = win_vert_neighbor(tp, twin, FALSE, count);
13369 else if (STRCMP(endp, "k") == 0)
13370 twin = win_vert_neighbor(tp, twin, TRUE, count);
13371 else if (STRCMP(endp, "h") == 0)
13372 twin = win_horz_neighbor(tp, twin, TRUE, count);
13373 else if (STRCMP(endp, "l") == 0)
13374 twin = win_horz_neighbor(tp, twin, FALSE, count);
13375 else
13376 invalid_arg = TRUE;
13377 }
13378 else
13379 invalid_arg = TRUE;
13380 }
13381
13382 if (invalid_arg)
13383 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013384 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013385 nr = 0;
13386 }
13387 }
13388
13389 if (nr > 0)
13390 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13391 wp != twin; wp = wp->w_next)
13392 {
13393 if (wp == NULL)
13394 {
13395 /* didn't find it in this tabpage */
13396 nr = 0;
13397 break;
13398 }
13399 ++nr;
13400 }
13401 return nr;
13402}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013403
13404/*
13405 * "tabpagewinnr()" function
13406 */
13407 static void
13408f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13409{
13410 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013411 tabpage_T *tp;
13412
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013413 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013414 if (tp == NULL)
13415 nr = 0;
13416 else
13417 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013418 rettv->vval.v_number = nr;
13419}
13420
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013421/*
13422 * "tagfiles()" function
13423 */
13424 static void
13425f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13426{
13427 char_u *fname;
13428 tagname_T tn;
13429 int first;
13430
13431 if (rettv_list_alloc(rettv) == FAIL)
13432 return;
13433 fname = alloc(MAXPATHL);
13434 if (fname == NULL)
13435 return;
13436
13437 for (first = TRUE; ; first = FALSE)
13438 if (get_tagfname(&tn, first, fname) == FAIL
13439 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13440 break;
13441 tagname_free(&tn);
13442 vim_free(fname);
13443}
13444
13445/*
13446 * "taglist()" function
13447 */
13448 static void
13449f_taglist(typval_T *argvars, typval_T *rettv)
13450{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013451 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013452 char_u *tag_pattern;
13453
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013454 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013455
13456 rettv->vval.v_number = FALSE;
13457 if (*tag_pattern == NUL)
13458 return;
13459
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013460 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013461 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013462 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013463 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013464}
13465
13466/*
13467 * "tempname()" function
13468 */
13469 static void
13470f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13471{
13472 static int x = 'A';
13473
13474 rettv->v_type = VAR_STRING;
13475 rettv->vval.v_string = vim_tempname(x, FALSE);
13476
13477 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13478 * names. Skip 'I' and 'O', they are used for shell redirection. */
13479 do
13480 {
13481 if (x == 'Z')
13482 x = '0';
13483 else if (x == '9')
13484 x = 'A';
13485 else
13486 {
13487#ifdef EBCDIC
13488 if (x == 'I')
13489 x = 'J';
13490 else if (x == 'R')
13491 x = 'S';
13492 else
13493#endif
13494 ++x;
13495 }
13496 } while (x == 'I' || x == 'O');
13497}
13498
13499#ifdef FEAT_FLOAT
13500/*
13501 * "tan()" function
13502 */
13503 static void
13504f_tan(typval_T *argvars, typval_T *rettv)
13505{
13506 float_T f = 0.0;
13507
13508 rettv->v_type = VAR_FLOAT;
13509 if (get_float_arg(argvars, &f) == OK)
13510 rettv->vval.v_float = tan(f);
13511 else
13512 rettv->vval.v_float = 0.0;
13513}
13514
13515/*
13516 * "tanh()" function
13517 */
13518 static void
13519f_tanh(typval_T *argvars, typval_T *rettv)
13520{
13521 float_T f = 0.0;
13522
13523 rettv->v_type = VAR_FLOAT;
13524 if (get_float_arg(argvars, &f) == OK)
13525 rettv->vval.v_float = tanh(f);
13526 else
13527 rettv->vval.v_float = 0.0;
13528}
13529#endif
13530
13531/*
13532 * "test_alloc_fail(id, countdown, repeat)" function
13533 */
13534 static void
13535f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13536{
13537 if (argvars[0].v_type != VAR_NUMBER
13538 || argvars[0].vval.v_number <= 0
13539 || argvars[1].v_type != VAR_NUMBER
13540 || argvars[1].vval.v_number < 0
13541 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013542 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013543 else
13544 {
13545 alloc_fail_id = argvars[0].vval.v_number;
13546 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013547 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013548 alloc_fail_countdown = argvars[1].vval.v_number;
13549 alloc_fail_repeat = argvars[2].vval.v_number;
13550 did_outofmem_msg = FALSE;
13551 }
13552}
13553
13554/*
13555 * "test_autochdir()"
13556 */
13557 static void
13558f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13559{
13560#if defined(FEAT_AUTOCHDIR)
13561 test_autochdir = TRUE;
13562#endif
13563}
13564
13565/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013566 * "test_feedinput()"
13567 */
13568 static void
13569f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13570{
13571#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013572 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013573
13574 if (val != NULL)
13575 {
13576 trash_input_buf();
13577 add_to_input_buf_csi(val, (int)STRLEN(val));
13578 }
13579#endif
13580}
13581
13582/*
Bram Moolenaareda65222019-05-16 20:29:44 +020013583 * "test_getvalue({name})" function
13584 */
13585 static void
13586f_test_getvalue(typval_T *argvars, typval_T *rettv)
13587{
13588 if (argvars[0].v_type != VAR_STRING)
13589 emsg(_(e_invarg));
13590 else
13591 {
13592 char_u *name = tv_get_string(&argvars[0]);
13593
13594 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
13595 rettv->vval.v_number = need_fileinfo;
13596 else
13597 semsg(_(e_invarg2), name);
13598 }
13599}
13600
13601/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013602 * "test_option_not_set({name})" function
13603 */
13604 static void
13605f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13606{
13607 char_u *name = (char_u *)"";
13608
13609 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013610 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013611 else
13612 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013613 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013614 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013615 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013616 }
13617}
13618
13619/*
13620 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013621 */
13622 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013623f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013624{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013625 char_u *name = (char_u *)"";
13626 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013627 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013628
13629 if (argvars[0].v_type != VAR_STRING
13630 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013631 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013632 else
13633 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013634 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013635 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013636
13637 if (STRCMP(name, (char_u *)"redraw") == 0)
13638 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013639 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13640 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013641 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13642 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013643 else if (STRCMP(name, (char_u *)"starting") == 0)
13644 {
13645 if (val)
13646 {
13647 if (save_starting < 0)
13648 save_starting = starting;
13649 starting = 0;
13650 }
13651 else
13652 {
13653 starting = save_starting;
13654 save_starting = -1;
13655 }
13656 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013657 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13658 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013659 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
13660 no_query_mouse_for_testing = val;
Bram Moolenaaradc67142019-06-22 01:40:42 +020013661 else if (STRCMP(name, (char_u *)"no_wait_return") == 0)
13662 no_wait_return = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013663 else if (STRCMP(name, (char_u *)"ALL") == 0)
13664 {
13665 disable_char_avail_for_testing = FALSE;
13666 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013667 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013668 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013669 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013670 if (save_starting >= 0)
13671 {
13672 starting = save_starting;
13673 save_starting = -1;
13674 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013675 }
13676 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013677 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013678 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013679}
13680
13681/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010013682 * "test_refcount({expr})" function
13683 */
13684 static void
13685f_test_refcount(typval_T *argvars, typval_T *rettv)
13686{
13687 int retval = -1;
13688
13689 switch (argvars[0].v_type)
13690 {
13691 case VAR_UNKNOWN:
13692 case VAR_NUMBER:
13693 case VAR_FLOAT:
13694 case VAR_SPECIAL:
13695 case VAR_STRING:
13696 break;
13697 case VAR_JOB:
13698#ifdef FEAT_JOB_CHANNEL
13699 if (argvars[0].vval.v_job != NULL)
13700 retval = argvars[0].vval.v_job->jv_refcount - 1;
13701#endif
13702 break;
13703 case VAR_CHANNEL:
13704#ifdef FEAT_JOB_CHANNEL
13705 if (argvars[0].vval.v_channel != NULL)
13706 retval = argvars[0].vval.v_channel->ch_refcount - 1;
13707#endif
13708 break;
13709 case VAR_FUNC:
13710 if (argvars[0].vval.v_string != NULL)
13711 {
13712 ufunc_T *fp;
13713
13714 fp = find_func(argvars[0].vval.v_string);
13715 if (fp != NULL)
13716 retval = fp->uf_refcount;
13717 }
13718 break;
13719 case VAR_PARTIAL:
13720 if (argvars[0].vval.v_partial != NULL)
13721 retval = argvars[0].vval.v_partial->pt_refcount - 1;
13722 break;
13723 case VAR_BLOB:
13724 if (argvars[0].vval.v_blob != NULL)
13725 retval = argvars[0].vval.v_blob->bv_refcount - 1;
13726 break;
13727 case VAR_LIST:
13728 if (argvars[0].vval.v_list != NULL)
13729 retval = argvars[0].vval.v_list->lv_refcount - 1;
13730 break;
13731 case VAR_DICT:
13732 if (argvars[0].vval.v_dict != NULL)
13733 retval = argvars[0].vval.v_dict->dv_refcount - 1;
13734 break;
13735 }
13736
13737 rettv->v_type = VAR_NUMBER;
13738 rettv->vval.v_number = retval;
13739
13740}
13741
13742/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013743 * "test_garbagecollect_now()" function
13744 */
13745 static void
13746f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13747{
13748 /* This is dangerous, any Lists and Dicts used internally may be freed
13749 * while still in use. */
13750 garbage_collect(TRUE);
13751}
13752
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013753/*
Bram Moolenaaradc67142019-06-22 01:40:42 +020013754 * "test_garbagecollect_soon()" function
13755 */
13756 static void
13757f_test_garbagecollect_soon(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13758{
13759 may_garbage_collect = TRUE;
13760}
13761
13762/*
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013763 * "test_ignore_error()" function
13764 */
13765 static void
13766f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13767{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013768 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013769}
13770
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013771 static void
13772f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13773{
13774 rettv->v_type = VAR_BLOB;
13775 rettv->vval.v_blob = NULL;
13776}
13777
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013778#ifdef FEAT_JOB_CHANNEL
13779 static void
13780f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13781{
13782 rettv->v_type = VAR_CHANNEL;
13783 rettv->vval.v_channel = NULL;
13784}
13785#endif
13786
13787 static void
13788f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13789{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013790 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013791}
13792
13793#ifdef FEAT_JOB_CHANNEL
13794 static void
13795f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13796{
13797 rettv->v_type = VAR_JOB;
13798 rettv->vval.v_job = NULL;
13799}
13800#endif
13801
13802 static void
13803f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13804{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013805 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013806}
13807
13808 static void
13809f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13810{
13811 rettv->v_type = VAR_PARTIAL;
13812 rettv->vval.v_partial = NULL;
13813}
13814
13815 static void
13816f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13817{
13818 rettv->v_type = VAR_STRING;
13819 rettv->vval.v_string = NULL;
13820}
13821
Bram Moolenaarab186732018-09-14 21:27:06 +020013822#ifdef FEAT_GUI
13823 static void
13824f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13825{
13826 char_u *which;
13827 long value;
13828 int dragging;
13829 scrollbar_T *sb = NULL;
13830
13831 if (argvars[0].v_type != VAR_STRING
13832 || (argvars[1].v_type) != VAR_NUMBER
13833 || (argvars[2].v_type) != VAR_NUMBER)
13834 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013835 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013836 return;
13837 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013838 which = tv_get_string(&argvars[0]);
13839 value = tv_get_number(&argvars[1]);
13840 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013841
13842 if (STRCMP(which, "left") == 0)
13843 sb = &curwin->w_scrollbars[SBAR_LEFT];
13844 else if (STRCMP(which, "right") == 0)
13845 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13846 else if (STRCMP(which, "hor") == 0)
13847 sb = &gui.bottom_sbar;
13848 if (sb == NULL)
13849 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013850 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020013851 return;
13852 }
13853 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013854# ifndef USE_ON_FLY_SCROLL
13855 // need to loop through normal_cmd() to handle the scroll events
13856 exec_normal(FALSE, TRUE, FALSE);
13857# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013858}
13859#endif
13860
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013861#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013862 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013863f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
13864{
13865 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
13866 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
13867}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013868#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013869
13870 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013871f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13872{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013873 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013874}
13875
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013876/*
13877 * Get a callback from "arg". It can be a Funcref or a function name.
13878 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013879 * "cb_name" is not allocated.
13880 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013881 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013882 callback_T
13883get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013884{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013885 callback_T res;
13886
13887 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013888 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13889 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013890 res.cb_partial = arg->vval.v_partial;
13891 ++res.cb_partial->pt_refcount;
13892 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013893 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013894 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013895 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013896 res.cb_partial = NULL;
13897 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
13898 {
13899 // Note that we don't make a copy of the string.
13900 res.cb_name = arg->vval.v_string;
13901 func_ref(res.cb_name);
13902 }
13903 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13904 {
13905 res.cb_name = (char_u *)"";
13906 }
13907 else
13908 {
13909 emsg(_("E921: Invalid callback argument"));
13910 res.cb_name = NULL;
13911 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013912 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013913 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013914}
13915
13916/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013917 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013918 */
13919 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013920put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013921{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013922 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013923 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013924 tv->v_type = VAR_PARTIAL;
13925 tv->vval.v_partial = cb->cb_partial;
13926 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013927 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013928 else
13929 {
13930 tv->v_type = VAR_FUNC;
13931 tv->vval.v_string = vim_strsave(cb->cb_name);
13932 func_ref(cb->cb_name);
13933 }
13934}
13935
13936/*
13937 * Make a copy of "src" into "dest", allocating the function name if needed,
13938 * without incrementing the refcount.
13939 */
13940 void
13941set_callback(callback_T *dest, callback_T *src)
13942{
13943 if (src->cb_partial == NULL)
13944 {
13945 // just a function name, make a copy
13946 dest->cb_name = vim_strsave(src->cb_name);
13947 dest->cb_free_name = TRUE;
13948 }
13949 else
13950 {
13951 // cb_name is a pointer into cb_partial
13952 dest->cb_name = src->cb_name;
13953 dest->cb_free_name = FALSE;
13954 }
13955 dest->cb_partial = src->cb_partial;
13956}
13957
13958/*
13959 * Unref/free "callback" returned by get_callback() or set_callback().
13960 */
13961 void
13962free_callback(callback_T *callback)
13963{
13964 if (callback->cb_partial != NULL)
13965 {
13966 partial_unref(callback->cb_partial);
13967 callback->cb_partial = NULL;
13968 }
13969 else if (callback->cb_name != NULL)
13970 func_unref(callback->cb_name);
13971 if (callback->cb_free_name)
13972 {
13973 vim_free(callback->cb_name);
13974 callback->cb_free_name = FALSE;
13975 }
13976 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013977}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013978
13979#ifdef FEAT_TIMERS
13980/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013981 * "timer_info([timer])" function
13982 */
13983 static void
13984f_timer_info(typval_T *argvars, typval_T *rettv)
13985{
13986 timer_T *timer = NULL;
13987
13988 if (rettv_list_alloc(rettv) != OK)
13989 return;
13990 if (argvars[0].v_type != VAR_UNKNOWN)
13991 {
13992 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013993 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013994 else
13995 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013996 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013997 if (timer != NULL)
13998 add_timer_info(rettv, timer);
13999 }
14000 }
14001 else
14002 add_timer_info_all(rettv);
14003}
14004
14005/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014006 * "timer_pause(timer, paused)" function
14007 */
14008 static void
14009f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14010{
14011 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014012 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014013
14014 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014015 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014016 else
14017 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014018 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014019 if (timer != NULL)
14020 timer->tr_paused = paused;
14021 }
14022}
14023
14024/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014025 * "timer_start(time, callback [, options])" function
14026 */
14027 static void
14028f_timer_start(typval_T *argvars, typval_T *rettv)
14029{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014030 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014031 timer_T *timer;
14032 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014033 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014034 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014035
Bram Moolenaar75537a92016-09-05 22:45:28 +020014036 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014037 if (check_secure())
14038 return;
14039 if (argvars[2].v_type != VAR_UNKNOWN)
14040 {
14041 if (argvars[2].v_type != VAR_DICT
14042 || (dict = argvars[2].vval.v_dict) == NULL)
14043 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014044 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014045 return;
14046 }
14047 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014048 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014049 }
14050
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014051 callback = get_callback(&argvars[1]);
14052 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014053 return;
14054
14055 timer = create_timer(msec, repeat);
14056 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014057 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014058 else
14059 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014060 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014061 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014062 }
14063}
14064
14065/*
14066 * "timer_stop(timer)" function
14067 */
14068 static void
14069f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14070{
14071 timer_T *timer;
14072
14073 if (argvars[0].v_type != VAR_NUMBER)
14074 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014075 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014076 return;
14077 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014078 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014079 if (timer != NULL)
14080 stop_timer(timer);
14081}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014082
14083/*
14084 * "timer_stopall()" function
14085 */
14086 static void
14087f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14088{
14089 stop_all_timers();
14090}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014091#endif
14092
14093/*
14094 * "tolower(string)" function
14095 */
14096 static void
14097f_tolower(typval_T *argvars, typval_T *rettv)
14098{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014099 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014100 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014101}
14102
14103/*
14104 * "toupper(string)" function
14105 */
14106 static void
14107f_toupper(typval_T *argvars, typval_T *rettv)
14108{
14109 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014110 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014111}
14112
14113/*
14114 * "tr(string, fromstr, tostr)" function
14115 */
14116 static void
14117f_tr(typval_T *argvars, typval_T *rettv)
14118{
14119 char_u *in_str;
14120 char_u *fromstr;
14121 char_u *tostr;
14122 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014123 int inlen;
14124 int fromlen;
14125 int tolen;
14126 int idx;
14127 char_u *cpstr;
14128 int cplen;
14129 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014130 char_u buf[NUMBUFLEN];
14131 char_u buf2[NUMBUFLEN];
14132 garray_T ga;
14133
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014134 in_str = tv_get_string(&argvars[0]);
14135 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14136 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014137
14138 /* Default return value: empty string. */
14139 rettv->v_type = VAR_STRING;
14140 rettv->vval.v_string = NULL;
14141 if (fromstr == NULL || tostr == NULL)
14142 return; /* type error; errmsg already given */
14143 ga_init2(&ga, (int)sizeof(char), 80);
14144
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014145 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014146 /* not multi-byte: fromstr and tostr must be the same length */
14147 if (STRLEN(fromstr) != STRLEN(tostr))
14148 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014149error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014150 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014151 ga_clear(&ga);
14152 return;
14153 }
14154
14155 /* fromstr and tostr have to contain the same number of chars */
14156 while (*in_str != NUL)
14157 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014158 if (has_mbyte)
14159 {
14160 inlen = (*mb_ptr2len)(in_str);
14161 cpstr = in_str;
14162 cplen = inlen;
14163 idx = 0;
14164 for (p = fromstr; *p != NUL; p += fromlen)
14165 {
14166 fromlen = (*mb_ptr2len)(p);
14167 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14168 {
14169 for (p = tostr; *p != NUL; p += tolen)
14170 {
14171 tolen = (*mb_ptr2len)(p);
14172 if (idx-- == 0)
14173 {
14174 cplen = tolen;
14175 cpstr = p;
14176 break;
14177 }
14178 }
14179 if (*p == NUL) /* tostr is shorter than fromstr */
14180 goto error;
14181 break;
14182 }
14183 ++idx;
14184 }
14185
14186 if (first && cpstr == in_str)
14187 {
14188 /* Check that fromstr and tostr have the same number of
14189 * (multi-byte) characters. Done only once when a character
14190 * of in_str doesn't appear in fromstr. */
14191 first = FALSE;
14192 for (p = tostr; *p != NUL; p += tolen)
14193 {
14194 tolen = (*mb_ptr2len)(p);
14195 --idx;
14196 }
14197 if (idx != 0)
14198 goto error;
14199 }
14200
14201 (void)ga_grow(&ga, cplen);
14202 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14203 ga.ga_len += cplen;
14204
14205 in_str += inlen;
14206 }
14207 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014208 {
14209 /* When not using multi-byte chars we can do it faster. */
14210 p = vim_strchr(fromstr, *in_str);
14211 if (p != NULL)
14212 ga_append(&ga, tostr[p - fromstr]);
14213 else
14214 ga_append(&ga, *in_str);
14215 ++in_str;
14216 }
14217 }
14218
14219 /* add a terminating NUL */
14220 (void)ga_grow(&ga, 1);
14221 ga_append(&ga, NUL);
14222
14223 rettv->vval.v_string = ga.ga_data;
14224}
14225
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014226/*
14227 * "trim({expr})" function
14228 */
14229 static void
14230f_trim(typval_T *argvars, typval_T *rettv)
14231{
14232 char_u buf1[NUMBUFLEN];
14233 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014234 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014235 char_u *mask = NULL;
14236 char_u *tail;
14237 char_u *prev;
14238 char_u *p;
14239 int c1;
14240
14241 rettv->v_type = VAR_STRING;
14242 if (head == NULL)
14243 {
14244 rettv->vval.v_string = NULL;
14245 return;
14246 }
14247
14248 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014249 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014250
14251 while (*head != NUL)
14252 {
14253 c1 = PTR2CHAR(head);
14254 if (mask == NULL)
14255 {
14256 if (c1 > ' ' && c1 != 0xa0)
14257 break;
14258 }
14259 else
14260 {
14261 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14262 if (c1 == PTR2CHAR(p))
14263 break;
14264 if (*p == NUL)
14265 break;
14266 }
14267 MB_PTR_ADV(head);
14268 }
14269
14270 for (tail = head + STRLEN(head); tail > head; tail = prev)
14271 {
14272 prev = tail;
14273 MB_PTR_BACK(head, prev);
14274 c1 = PTR2CHAR(prev);
14275 if (mask == NULL)
14276 {
14277 if (c1 > ' ' && c1 != 0xa0)
14278 break;
14279 }
14280 else
14281 {
14282 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14283 if (c1 == PTR2CHAR(p))
14284 break;
14285 if (*p == NUL)
14286 break;
14287 }
14288 }
14289 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14290}
14291
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014292#ifdef FEAT_FLOAT
14293/*
14294 * "trunc({float})" function
14295 */
14296 static void
14297f_trunc(typval_T *argvars, typval_T *rettv)
14298{
14299 float_T f = 0.0;
14300
14301 rettv->v_type = VAR_FLOAT;
14302 if (get_float_arg(argvars, &f) == OK)
14303 /* trunc() is not in C90, use floor() or ceil() instead. */
14304 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14305 else
14306 rettv->vval.v_float = 0.0;
14307}
14308#endif
14309
14310/*
14311 * "type(expr)" function
14312 */
14313 static void
14314f_type(typval_T *argvars, typval_T *rettv)
14315{
14316 int n = -1;
14317
14318 switch (argvars[0].v_type)
14319 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014320 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14321 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014322 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014323 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14324 case VAR_LIST: n = VAR_TYPE_LIST; break;
14325 case VAR_DICT: n = VAR_TYPE_DICT; break;
14326 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014327 case VAR_SPECIAL:
14328 if (argvars[0].vval.v_number == VVAL_FALSE
14329 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014330 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014331 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014332 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014333 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014334 case VAR_JOB: n = VAR_TYPE_JOB; break;
14335 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014336 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014337 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014338 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014339 n = -1;
14340 break;
14341 }
14342 rettv->vval.v_number = n;
14343}
14344
14345/*
14346 * "undofile(name)" function
14347 */
14348 static void
14349f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14350{
14351 rettv->v_type = VAR_STRING;
14352#ifdef FEAT_PERSISTENT_UNDO
14353 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014354 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014355
14356 if (*fname == NUL)
14357 {
14358 /* If there is no file name there will be no undo file. */
14359 rettv->vval.v_string = NULL;
14360 }
14361 else
14362 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020014363 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014364
14365 if (ffname != NULL)
14366 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14367 vim_free(ffname);
14368 }
14369 }
14370#else
14371 rettv->vval.v_string = NULL;
14372#endif
14373}
14374
14375/*
14376 * "undotree()" function
14377 */
14378 static void
14379f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14380{
14381 if (rettv_dict_alloc(rettv) == OK)
14382 {
14383 dict_T *dict = rettv->vval.v_dict;
14384 list_T *list;
14385
Bram Moolenaare0be1672018-07-08 16:50:37 +020014386 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14387 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14388 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14389 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14390 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14391 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014392
14393 list = list_alloc();
14394 if (list != NULL)
14395 {
14396 u_eval_tree(curbuf->b_u_oldhead, list);
14397 dict_add_list(dict, "entries", list);
14398 }
14399 }
14400}
14401
14402/*
14403 * "values(dict)" function
14404 */
14405 static void
14406f_values(typval_T *argvars, typval_T *rettv)
14407{
14408 dict_list(argvars, rettv, 1);
14409}
14410
14411/*
14412 * "virtcol(string)" function
14413 */
14414 static void
14415f_virtcol(typval_T *argvars, typval_T *rettv)
14416{
14417 colnr_T vcol = 0;
14418 pos_T *fp;
14419 int fnum = curbuf->b_fnum;
14420
14421 fp = var2fpos(&argvars[0], FALSE, &fnum);
14422 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14423 && fnum == curbuf->b_fnum)
14424 {
14425 getvvcol(curwin, fp, NULL, NULL, &vcol);
14426 ++vcol;
14427 }
14428
14429 rettv->vval.v_number = vcol;
14430}
14431
14432/*
14433 * "visualmode()" function
14434 */
14435 static void
14436f_visualmode(typval_T *argvars, typval_T *rettv)
14437{
14438 char_u str[2];
14439
14440 rettv->v_type = VAR_STRING;
14441 str[0] = curbuf->b_visual_mode_eval;
14442 str[1] = NUL;
14443 rettv->vval.v_string = vim_strsave(str);
14444
14445 /* A non-zero number or non-empty string argument: reset mode. */
14446 if (non_zero_arg(&argvars[0]))
14447 curbuf->b_visual_mode_eval = NUL;
14448}
14449
14450/*
14451 * "wildmenumode()" function
14452 */
14453 static void
14454f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14455{
14456#ifdef FEAT_WILDMENU
14457 if (wild_menu_showing)
14458 rettv->vval.v_number = 1;
14459#endif
14460}
14461
14462/*
14463 * "winbufnr(nr)" function
14464 */
14465 static void
14466f_winbufnr(typval_T *argvars, typval_T *rettv)
14467{
14468 win_T *wp;
14469
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014470 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014471 if (wp == NULL)
14472 rettv->vval.v_number = -1;
14473 else
14474 rettv->vval.v_number = wp->w_buffer->b_fnum;
14475}
14476
14477/*
14478 * "wincol()" function
14479 */
14480 static void
14481f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14482{
14483 validate_cursor();
14484 rettv->vval.v_number = curwin->w_wcol + 1;
14485}
14486
14487/*
14488 * "winheight(nr)" function
14489 */
14490 static void
14491f_winheight(typval_T *argvars, typval_T *rettv)
14492{
14493 win_T *wp;
14494
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014495 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014496 if (wp == NULL)
14497 rettv->vval.v_number = -1;
14498 else
14499 rettv->vval.v_number = wp->w_height;
14500}
14501
14502/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014503 * "winlayout()" function
14504 */
14505 static void
14506f_winlayout(typval_T *argvars, typval_T *rettv)
14507{
14508 tabpage_T *tp;
14509
14510 if (rettv_list_alloc(rettv) != OK)
14511 return;
14512
14513 if (argvars[0].v_type == VAR_UNKNOWN)
14514 tp = curtab;
14515 else
14516 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014517 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014518 if (tp == NULL)
14519 return;
14520 }
14521
14522 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14523}
14524
14525/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014526 * "winline()" function
14527 */
14528 static void
14529f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14530{
14531 validate_cursor();
14532 rettv->vval.v_number = curwin->w_wrow + 1;
14533}
14534
14535/*
14536 * "winnr()" function
14537 */
14538 static void
14539f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14540{
14541 int nr = 1;
14542
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014543 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014544 rettv->vval.v_number = nr;
14545}
14546
14547/*
14548 * "winrestcmd()" function
14549 */
14550 static void
14551f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14552{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014553 win_T *wp;
14554 int winnr = 1;
14555 garray_T ga;
14556 char_u buf[50];
14557
14558 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014559 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014560 {
14561 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14562 ga_concat(&ga, buf);
14563 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14564 ga_concat(&ga, buf);
14565 ++winnr;
14566 }
14567 ga_append(&ga, NUL);
14568
14569 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014570 rettv->v_type = VAR_STRING;
14571}
14572
14573/*
14574 * "winrestview()" function
14575 */
14576 static void
14577f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14578{
14579 dict_T *dict;
14580
14581 if (argvars[0].v_type != VAR_DICT
14582 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014583 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014584 else
14585 {
14586 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014587 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014588 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014589 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014590 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014591 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014592 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14593 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014594 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014595 curwin->w_set_curswant = FALSE;
14596 }
14597
14598 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014599 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014600#ifdef FEAT_DIFF
14601 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014602 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014603#endif
14604 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014605 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014606 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014607 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014608
14609 check_cursor();
14610 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014611 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014612 changed_window_setting();
14613
14614 if (curwin->w_topline <= 0)
14615 curwin->w_topline = 1;
14616 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14617 curwin->w_topline = curbuf->b_ml.ml_line_count;
14618#ifdef FEAT_DIFF
14619 check_topfill(curwin, TRUE);
14620#endif
14621 }
14622}
14623
14624/*
14625 * "winsaveview()" function
14626 */
14627 static void
14628f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14629{
14630 dict_T *dict;
14631
14632 if (rettv_dict_alloc(rettv) == FAIL)
14633 return;
14634 dict = rettv->vval.v_dict;
14635
Bram Moolenaare0be1672018-07-08 16:50:37 +020014636 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14637 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020014638 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014639 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014640 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014641
Bram Moolenaare0be1672018-07-08 16:50:37 +020014642 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014643#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014644 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014645#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014646 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14647 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014648}
14649
14650/*
14651 * "winwidth(nr)" function
14652 */
14653 static void
14654f_winwidth(typval_T *argvars, typval_T *rettv)
14655{
14656 win_T *wp;
14657
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014658 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014659 if (wp == NULL)
14660 rettv->vval.v_number = -1;
14661 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014662 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014663}
14664
14665/*
14666 * "wordcount()" function
14667 */
14668 static void
14669f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14670{
14671 if (rettv_dict_alloc(rettv) == FAIL)
14672 return;
14673 cursor_pos_info(rettv->vval.v_dict);
14674}
14675
14676/*
14677 * "writefile()" function
14678 */
14679 static void
14680f_writefile(typval_T *argvars, typval_T *rettv)
14681{
14682 int binary = FALSE;
14683 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014684#ifdef HAVE_FSYNC
14685 int do_fsync = p_fs;
14686#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014687 char_u *fname;
14688 FILE *fd;
14689 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014690 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014691 list_T *list = NULL;
14692 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014693
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014694 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010014695 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014696 return;
14697
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014698 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014699 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014700 list = argvars[0].vval.v_list;
14701 if (list == NULL)
14702 return;
14703 for (li = list->lv_first; li != NULL; li = li->li_next)
14704 if (tv_get_string_chk(&li->li_tv) == NULL)
14705 return;
14706 }
14707 else if (argvars[0].v_type == VAR_BLOB)
14708 {
14709 blob = argvars[0].vval.v_blob;
14710 if (blob == NULL)
14711 return;
14712 }
14713 else
14714 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014715 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014716 return;
14717 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014718
14719 if (argvars[2].v_type != VAR_UNKNOWN)
14720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014721 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014722
14723 if (arg2 == NULL)
14724 return;
14725 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014726 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014727 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014728 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014729#ifdef HAVE_FSYNC
14730 if (vim_strchr(arg2, 's') != NULL)
14731 do_fsync = TRUE;
14732 else if (vim_strchr(arg2, 'S') != NULL)
14733 do_fsync = FALSE;
14734#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014735 }
14736
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014737 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014738 if (fname == NULL)
14739 return;
14740
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014741 /* Always open the file in binary mode, library functions have a mind of
14742 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014743 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14744 append ? APPENDBIN : WRITEBIN)) == NULL)
14745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014746 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014747 ret = -1;
14748 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014749 else if (blob)
14750 {
14751 if (write_blob(fd, blob) == FAIL)
14752 ret = -1;
14753#ifdef HAVE_FSYNC
14754 else if (do_fsync)
14755 // Ignore the error, the user wouldn't know what to do about it.
14756 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010014757 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014758#endif
14759 fclose(fd);
14760 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014761 else
14762 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014763 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014764 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014765#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014766 else if (do_fsync)
14767 /* Ignore the error, the user wouldn't know what to do about it.
14768 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010014769 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014770#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014771 fclose(fd);
14772 }
14773
14774 rettv->vval.v_number = ret;
14775}
14776
14777/*
14778 * "xor(expr, expr)" function
14779 */
14780 static void
14781f_xor(typval_T *argvars, typval_T *rettv)
14782{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014783 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14784 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014785}
14786
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014787#endif /* FEAT_EVAL */