blob: 954ea9fe9ad5d8dd35e1ff84430ef28635f5ee22 [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{
1932 rettv->vval.v_number = buflist_add(tv_get_string(&argvars[0]), 0);
1933}
1934
1935/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001936 * "bufexists(expr)" function
1937 */
1938 static void
1939f_bufexists(typval_T *argvars, typval_T *rettv)
1940{
1941 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1942}
1943
1944/*
1945 * "buflisted(expr)" function
1946 */
1947 static void
1948f_buflisted(typval_T *argvars, typval_T *rettv)
1949{
1950 buf_T *buf;
1951
1952 buf = find_buffer(&argvars[0]);
1953 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1954}
1955
1956/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001957 * "bufload(expr)" function
1958 */
1959 static void
1960f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1961{
1962 buf_T *buf = get_buf_arg(&argvars[0]);
1963
1964 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
1965 {
1966 aco_save_T aco;
1967
1968 aucmd_prepbuf(&aco, buf);
1969 swap_exists_action = SEA_NONE;
1970 open_buffer(FALSE, NULL, 0);
1971 aucmd_restbuf(&aco);
1972 }
1973}
1974
1975/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001976 * "bufloaded(expr)" function
1977 */
1978 static void
1979f_bufloaded(typval_T *argvars, typval_T *rettv)
1980{
1981 buf_T *buf;
1982
1983 buf = find_buffer(&argvars[0]);
1984 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1985}
1986
1987 buf_T *
1988buflist_find_by_name(char_u *name, int curtab_only)
1989{
1990 int save_magic;
1991 char_u *save_cpo;
1992 buf_T *buf;
1993
1994 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1995 save_magic = p_magic;
1996 p_magic = TRUE;
1997 save_cpo = p_cpo;
1998 p_cpo = (char_u *)"";
1999
2000 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
2001 TRUE, FALSE, curtab_only));
2002
2003 p_magic = save_magic;
2004 p_cpo = save_cpo;
2005 return buf;
2006}
2007
2008/*
2009 * Get buffer by number or pattern.
2010 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002011 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002012tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002013{
2014 char_u *name = tv->vval.v_string;
2015 buf_T *buf;
2016
2017 if (tv->v_type == VAR_NUMBER)
2018 return buflist_findnr((int)tv->vval.v_number);
2019 if (tv->v_type != VAR_STRING)
2020 return NULL;
2021 if (name == NULL || *name == NUL)
2022 return curbuf;
2023 if (name[0] == '$' && name[1] == NUL)
2024 return lastbuf;
2025
2026 buf = buflist_find_by_name(name, curtab_only);
2027
2028 /* If not found, try expanding the name, like done for bufexists(). */
2029 if (buf == NULL)
2030 buf = find_buffer(tv);
2031
2032 return buf;
2033}
2034
2035/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002036 * Get the buffer from "arg" and give an error and return NULL if it is not
2037 * valid.
2038 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002039 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002040get_buf_arg(typval_T *arg)
2041{
2042 buf_T *buf;
2043
2044 ++emsg_off;
2045 buf = tv_get_buf(arg, FALSE);
2046 --emsg_off;
2047 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002048 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002049 return buf;
2050}
2051
2052/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002053 * "bufname(expr)" function
2054 */
2055 static void
2056f_bufname(typval_T *argvars, typval_T *rettv)
2057{
2058 buf_T *buf;
2059
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002060 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002062 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002063 rettv->v_type = VAR_STRING;
2064 if (buf != NULL && buf->b_fname != NULL)
2065 rettv->vval.v_string = vim_strsave(buf->b_fname);
2066 else
2067 rettv->vval.v_string = NULL;
2068 --emsg_off;
2069}
2070
2071/*
2072 * "bufnr(expr)" function
2073 */
2074 static void
2075f_bufnr(typval_T *argvars, typval_T *rettv)
2076{
2077 buf_T *buf;
2078 int error = FALSE;
2079 char_u *name;
2080
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002081 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002082 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002083 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002084 --emsg_off;
2085
2086 /* If the buffer isn't found and the second argument is not zero create a
2087 * new buffer. */
2088 if (buf == NULL
2089 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002090 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002091 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002092 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002093 && !error)
2094 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2095
2096 if (buf != NULL)
2097 rettv->vval.v_number = buf->b_fnum;
2098 else
2099 rettv->vval.v_number = -1;
2100}
2101
2102 static void
2103buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2104{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002105 win_T *wp;
2106 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002107 buf_T *buf;
2108
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002109 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002110 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002111 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002112 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002113 {
2114 ++winnr;
2115 if (wp->w_buffer == buf)
2116 break;
2117 }
2118 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002119 --emsg_off;
2120}
2121
2122/*
2123 * "bufwinid(nr)" function
2124 */
2125 static void
2126f_bufwinid(typval_T *argvars, typval_T *rettv)
2127{
2128 buf_win_common(argvars, rettv, FALSE);
2129}
2130
2131/*
2132 * "bufwinnr(nr)" function
2133 */
2134 static void
2135f_bufwinnr(typval_T *argvars, typval_T *rettv)
2136{
2137 buf_win_common(argvars, rettv, TRUE);
2138}
2139
2140/*
2141 * "byte2line(byte)" function
2142 */
2143 static void
2144f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2145{
2146#ifndef FEAT_BYTEOFF
2147 rettv->vval.v_number = -1;
2148#else
2149 long boff = 0;
2150
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002151 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002152 if (boff < 0)
2153 rettv->vval.v_number = -1;
2154 else
2155 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2156 (linenr_T)0, &boff);
2157#endif
2158}
2159
2160 static void
2161byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2162{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002163 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164 char_u *str;
2165 varnumber_T idx;
2166
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002167 str = tv_get_string_chk(&argvars[0]);
2168 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002169 rettv->vval.v_number = -1;
2170 if (str == NULL || idx < 0)
2171 return;
2172
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002173 t = str;
2174 for ( ; idx > 0; idx--)
2175 {
2176 if (*t == NUL) /* EOL reached */
2177 return;
2178 if (enc_utf8 && comp)
2179 t += utf_ptr2len(t);
2180 else
2181 t += (*mb_ptr2len)(t);
2182 }
2183 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002184}
2185
2186/*
2187 * "byteidx()" function
2188 */
2189 static void
2190f_byteidx(typval_T *argvars, typval_T *rettv)
2191{
2192 byteidx(argvars, rettv, FALSE);
2193}
2194
2195/*
2196 * "byteidxcomp()" function
2197 */
2198 static void
2199f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2200{
2201 byteidx(argvars, rettv, TRUE);
2202}
2203
2204/*
2205 * "call(func, arglist [, dict])" function
2206 */
2207 static void
2208f_call(typval_T *argvars, typval_T *rettv)
2209{
2210 char_u *func;
2211 partial_T *partial = NULL;
2212 dict_T *selfdict = NULL;
2213
2214 if (argvars[1].v_type != VAR_LIST)
2215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002216 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002217 return;
2218 }
2219 if (argvars[1].vval.v_list == NULL)
2220 return;
2221
2222 if (argvars[0].v_type == VAR_FUNC)
2223 func = argvars[0].vval.v_string;
2224 else if (argvars[0].v_type == VAR_PARTIAL)
2225 {
2226 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002227 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002228 }
2229 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002230 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002231 if (*func == NUL)
2232 return; /* type error or empty name */
2233
2234 if (argvars[2].v_type != VAR_UNKNOWN)
2235 {
2236 if (argvars[2].v_type != VAR_DICT)
2237 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002238 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002239 return;
2240 }
2241 selfdict = argvars[2].vval.v_dict;
2242 }
2243
2244 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2245}
2246
2247#ifdef FEAT_FLOAT
2248/*
2249 * "ceil({float})" function
2250 */
2251 static void
2252f_ceil(typval_T *argvars, typval_T *rettv)
2253{
2254 float_T f = 0.0;
2255
2256 rettv->v_type = VAR_FLOAT;
2257 if (get_float_arg(argvars, &f) == OK)
2258 rettv->vval.v_float = ceil(f);
2259 else
2260 rettv->vval.v_float = 0.0;
2261}
2262#endif
2263
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002264/*
2265 * "changenr()" function
2266 */
2267 static void
2268f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2269{
2270 rettv->vval.v_number = curbuf->b_u_seq_cur;
2271}
2272
2273/*
2274 * "char2nr(string)" function
2275 */
2276 static void
2277f_char2nr(typval_T *argvars, typval_T *rettv)
2278{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002279 if (has_mbyte)
2280 {
2281 int utf8 = 0;
2282
2283 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002284 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002285
2286 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002287 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002288 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002289 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002290 }
2291 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002292 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002293}
2294
2295/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002296 * "chdir(dir)" function
2297 */
2298 static void
2299f_chdir(typval_T *argvars, typval_T *rettv)
2300{
2301 char_u *cwd;
2302 cdscope_T scope = CDSCOPE_GLOBAL;
2303
2304 rettv->v_type = VAR_STRING;
2305 rettv->vval.v_string = NULL;
2306
2307 if (argvars[0].v_type != VAR_STRING)
2308 return;
2309
2310 // Return the current directory
2311 cwd = alloc(MAXPATHL);
2312 if (cwd != NULL)
2313 {
2314 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2315 {
2316#ifdef BACKSLASH_IN_FILENAME
2317 slash_adjust(cwd);
2318#endif
2319 rettv->vval.v_string = vim_strsave(cwd);
2320 }
2321 vim_free(cwd);
2322 }
2323
2324 if (curwin->w_localdir != NULL)
2325 scope = CDSCOPE_WINDOW;
2326 else if (curtab->tp_localdir != NULL)
2327 scope = CDSCOPE_TABPAGE;
2328
2329 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2330 // Directory change failed
2331 VIM_CLEAR(rettv->vval.v_string);
2332}
2333
2334/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002335 * "cindent(lnum)" function
2336 */
2337 static void
2338f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2339{
2340#ifdef FEAT_CINDENT
2341 pos_T pos;
2342 linenr_T lnum;
2343
2344 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002345 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002346 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2347 {
2348 curwin->w_cursor.lnum = lnum;
2349 rettv->vval.v_number = get_c_indent();
2350 curwin->w_cursor = pos;
2351 }
2352 else
2353#endif
2354 rettv->vval.v_number = -1;
2355}
2356
Bram Moolenaaraff74912019-03-30 18:11:49 +01002357 static win_T *
2358get_optional_window(typval_T *argvars, int idx)
2359{
2360 win_T *win = curwin;
2361
2362 if (argvars[idx].v_type != VAR_UNKNOWN)
2363 {
2364 win = find_win_by_nr_or_id(&argvars[idx]);
2365 if (win == NULL)
2366 {
2367 emsg(_(e_invalwindow));
2368 return NULL;
2369 }
2370 }
2371 return win;
2372}
2373
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002374/*
2375 * "clearmatches()" function
2376 */
2377 static void
2378f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2379{
2380#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002381 win_T *win = get_optional_window(argvars, 0);
2382
2383 if (win != NULL)
2384 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002385#endif
2386}
2387
2388/*
2389 * "col(string)" function
2390 */
2391 static void
2392f_col(typval_T *argvars, typval_T *rettv)
2393{
2394 colnr_T col = 0;
2395 pos_T *fp;
2396 int fnum = curbuf->b_fnum;
2397
2398 fp = var2fpos(&argvars[0], FALSE, &fnum);
2399 if (fp != NULL && fnum == curbuf->b_fnum)
2400 {
2401 if (fp->col == MAXCOL)
2402 {
2403 /* '> can be MAXCOL, get the length of the line then */
2404 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2405 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2406 else
2407 col = MAXCOL;
2408 }
2409 else
2410 {
2411 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412 /* col(".") when the cursor is on the NUL at the end of the line
2413 * because of "coladd" can be seen as an extra column. */
2414 if (virtual_active() && fp == &curwin->w_cursor)
2415 {
2416 char_u *p = ml_get_cursor();
2417
2418 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2419 curwin->w_virtcol - curwin->w_cursor.coladd))
2420 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002421 int l;
2422
2423 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2424 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002425 }
2426 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002427 }
2428 }
2429 rettv->vval.v_number = col;
2430}
2431
2432#if defined(FEAT_INS_EXPAND)
2433/*
2434 * "complete()" function
2435 */
2436 static void
2437f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2438{
2439 int startcol;
2440
2441 if ((State & INSERT) == 0)
2442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002443 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002444 return;
2445 }
2446
2447 /* Check for undo allowed here, because if something was already inserted
2448 * the line was already saved for undo and this check isn't done. */
2449 if (!undo_allowed())
2450 return;
2451
2452 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2453 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002454 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002455 return;
2456 }
2457
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002458 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002459 if (startcol <= 0)
2460 return;
2461
2462 set_completion(startcol - 1, argvars[1].vval.v_list);
2463}
2464
2465/*
2466 * "complete_add()" function
2467 */
2468 static void
2469f_complete_add(typval_T *argvars, typval_T *rettv)
2470{
2471 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2472}
2473
2474/*
2475 * "complete_check()" function
2476 */
2477 static void
2478f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2479{
2480 int saved = RedrawingDisabled;
2481
2482 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002483 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002484 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002485 RedrawingDisabled = saved;
2486}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002487
2488/*
2489 * "complete_info()" function
2490 */
2491 static void
2492f_complete_info(typval_T *argvars, typval_T *rettv)
2493{
2494 list_T *what_list = NULL;
2495
2496 if (rettv_dict_alloc(rettv) != OK)
2497 return;
2498
2499 if (argvars[0].v_type != VAR_UNKNOWN)
2500 {
2501 if (argvars[0].v_type != VAR_LIST)
2502 {
2503 emsg(_(e_listreq));
2504 return;
2505 }
2506 what_list = argvars[0].vval.v_list;
2507 }
2508 get_complete_info(what_list, rettv->vval.v_dict);
2509}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002510#endif
2511
2512/*
2513 * "confirm(message, buttons[, default [, type]])" function
2514 */
2515 static void
2516f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2517{
2518#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2519 char_u *message;
2520 char_u *buttons = NULL;
2521 char_u buf[NUMBUFLEN];
2522 char_u buf2[NUMBUFLEN];
2523 int def = 1;
2524 int type = VIM_GENERIC;
2525 char_u *typestr;
2526 int error = FALSE;
2527
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002528 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002529 if (message == NULL)
2530 error = TRUE;
2531 if (argvars[1].v_type != VAR_UNKNOWN)
2532 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002533 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002534 if (buttons == NULL)
2535 error = TRUE;
2536 if (argvars[2].v_type != VAR_UNKNOWN)
2537 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002538 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002539 if (argvars[3].v_type != VAR_UNKNOWN)
2540 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002541 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002542 if (typestr == NULL)
2543 error = TRUE;
2544 else
2545 {
2546 switch (TOUPPER_ASC(*typestr))
2547 {
2548 case 'E': type = VIM_ERROR; break;
2549 case 'Q': type = VIM_QUESTION; break;
2550 case 'I': type = VIM_INFO; break;
2551 case 'W': type = VIM_WARNING; break;
2552 case 'G': type = VIM_GENERIC; break;
2553 }
2554 }
2555 }
2556 }
2557 }
2558
2559 if (buttons == NULL || *buttons == NUL)
2560 buttons = (char_u *)_("&Ok");
2561
2562 if (!error)
2563 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2564 def, NULL, FALSE);
2565#endif
2566}
2567
2568/*
2569 * "copy()" function
2570 */
2571 static void
2572f_copy(typval_T *argvars, typval_T *rettv)
2573{
2574 item_copy(&argvars[0], rettv, FALSE, 0);
2575}
2576
2577#ifdef FEAT_FLOAT
2578/*
2579 * "cos()" function
2580 */
2581 static void
2582f_cos(typval_T *argvars, typval_T *rettv)
2583{
2584 float_T f = 0.0;
2585
2586 rettv->v_type = VAR_FLOAT;
2587 if (get_float_arg(argvars, &f) == OK)
2588 rettv->vval.v_float = cos(f);
2589 else
2590 rettv->vval.v_float = 0.0;
2591}
2592
2593/*
2594 * "cosh()" function
2595 */
2596 static void
2597f_cosh(typval_T *argvars, typval_T *rettv)
2598{
2599 float_T f = 0.0;
2600
2601 rettv->v_type = VAR_FLOAT;
2602 if (get_float_arg(argvars, &f) == OK)
2603 rettv->vval.v_float = cosh(f);
2604 else
2605 rettv->vval.v_float = 0.0;
2606}
2607#endif
2608
2609/*
2610 * "count()" function
2611 */
2612 static void
2613f_count(typval_T *argvars, typval_T *rettv)
2614{
2615 long n = 0;
2616 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002617 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002618
Bram Moolenaar9966b212017-07-28 16:46:57 +02002619 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002620 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002621
2622 if (argvars[0].v_type == VAR_STRING)
2623 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002624 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002625 char_u *p = argvars[0].vval.v_string;
2626 char_u *next;
2627
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002628 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002629 {
2630 if (ic)
2631 {
2632 size_t len = STRLEN(expr);
2633
2634 while (*p != NUL)
2635 {
2636 if (MB_STRNICMP(p, expr, len) == 0)
2637 {
2638 ++n;
2639 p += len;
2640 }
2641 else
2642 MB_PTR_ADV(p);
2643 }
2644 }
2645 else
2646 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2647 != NULL)
2648 {
2649 ++n;
2650 p = next + STRLEN(expr);
2651 }
2652 }
2653
2654 }
2655 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002656 {
2657 listitem_T *li;
2658 list_T *l;
2659 long idx;
2660
2661 if ((l = argvars[0].vval.v_list) != NULL)
2662 {
2663 li = l->lv_first;
2664 if (argvars[2].v_type != VAR_UNKNOWN)
2665 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002666 if (argvars[3].v_type != VAR_UNKNOWN)
2667 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002668 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002669 if (!error)
2670 {
2671 li = list_find(l, idx);
2672 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002673 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002674 }
2675 }
2676 if (error)
2677 li = NULL;
2678 }
2679
2680 for ( ; li != NULL; li = li->li_next)
2681 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2682 ++n;
2683 }
2684 }
2685 else if (argvars[0].v_type == VAR_DICT)
2686 {
2687 int todo;
2688 dict_T *d;
2689 hashitem_T *hi;
2690
2691 if ((d = argvars[0].vval.v_dict) != NULL)
2692 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002693 if (argvars[2].v_type != VAR_UNKNOWN)
2694 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002695 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002696 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002697 }
2698
2699 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2700 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2701 {
2702 if (!HASHITEM_EMPTY(hi))
2703 {
2704 --todo;
2705 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2706 ++n;
2707 }
2708 }
2709 }
2710 }
2711 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002712 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002713 rettv->vval.v_number = n;
2714}
2715
2716/*
2717 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2718 *
2719 * Checks the existence of a cscope connection.
2720 */
2721 static void
2722f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2723{
2724#ifdef FEAT_CSCOPE
2725 int num = 0;
2726 char_u *dbpath = NULL;
2727 char_u *prepend = NULL;
2728 char_u buf[NUMBUFLEN];
2729
2730 if (argvars[0].v_type != VAR_UNKNOWN
2731 && argvars[1].v_type != VAR_UNKNOWN)
2732 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002733 num = (int)tv_get_number(&argvars[0]);
2734 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002735 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002736 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002737 }
2738
2739 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2740#endif
2741}
2742
2743/*
2744 * "cursor(lnum, col)" function, or
2745 * "cursor(list)"
2746 *
2747 * Moves the cursor to the specified line and column.
2748 * Returns 0 when the position could be set, -1 otherwise.
2749 */
2750 static void
2751f_cursor(typval_T *argvars, typval_T *rettv)
2752{
2753 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002754 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002755 int set_curswant = TRUE;
2756
2757 rettv->vval.v_number = -1;
2758 if (argvars[1].v_type == VAR_UNKNOWN)
2759 {
2760 pos_T pos;
2761 colnr_T curswant = -1;
2762
2763 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2764 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002765 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766 return;
2767 }
2768 line = pos.lnum;
2769 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002770 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002771 if (curswant >= 0)
2772 {
2773 curwin->w_curswant = curswant - 1;
2774 set_curswant = FALSE;
2775 }
2776 }
2777 else
2778 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002779 line = tv_get_lnum(argvars);
2780 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002781 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002782 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002783 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002784 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785 return; /* type error; errmsg already given */
2786 if (line > 0)
2787 curwin->w_cursor.lnum = line;
2788 if (col > 0)
2789 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002791
2792 /* Make sure the cursor is in a valid position. */
2793 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002794 /* Correct cursor for multi-byte character. */
2795 if (has_mbyte)
2796 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797
2798 curwin->w_set_curswant = set_curswant;
2799 rettv->vval.v_number = 0;
2800}
2801
Bram Moolenaar4f974752019-02-17 17:44:42 +01002802#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002803/*
2804 * "debugbreak()" function
2805 */
2806 static void
2807f_debugbreak(typval_T *argvars, typval_T *rettv)
2808{
2809 int pid;
2810
2811 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002812 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002813 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002814 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002815 else
2816 {
2817 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2818
2819 if (hProcess != NULL)
2820 {
2821 DebugBreakProcess(hProcess);
2822 CloseHandle(hProcess);
2823 rettv->vval.v_number = OK;
2824 }
2825 }
2826}
2827#endif
2828
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002829/*
2830 * "deepcopy()" function
2831 */
2832 static void
2833f_deepcopy(typval_T *argvars, typval_T *rettv)
2834{
2835 int noref = 0;
2836 int copyID;
2837
2838 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002839 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002840 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002841 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002842 else
2843 {
2844 copyID = get_copyID();
2845 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2846 }
2847}
2848
2849/*
2850 * "delete()" function
2851 */
2852 static void
2853f_delete(typval_T *argvars, typval_T *rettv)
2854{
2855 char_u nbuf[NUMBUFLEN];
2856 char_u *name;
2857 char_u *flags;
2858
2859 rettv->vval.v_number = -1;
2860 if (check_restricted() || check_secure())
2861 return;
2862
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002863 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002864 if (name == NULL || *name == NUL)
2865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002866 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002867 return;
2868 }
2869
2870 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002871 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002872 else
2873 flags = (char_u *)"";
2874
2875 if (*flags == NUL)
2876 /* delete a file */
2877 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2878 else if (STRCMP(flags, "d") == 0)
2879 /* delete an empty directory */
2880 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2881 else if (STRCMP(flags, "rf") == 0)
2882 /* delete a directory recursively */
2883 rettv->vval.v_number = delete_recursive(name);
2884 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002885 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002886}
2887
2888/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002889 * "deletebufline()" function
2890 */
2891 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002892f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002893{
2894 buf_T *buf;
2895 linenr_T first, last;
2896 linenr_T lnum;
2897 long count;
2898 int is_curbuf;
2899 buf_T *curbuf_save = NULL;
2900 win_T *curwin_save = NULL;
2901 tabpage_T *tp;
2902 win_T *wp;
2903
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002904 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002905 if (buf == NULL)
2906 {
2907 rettv->vval.v_number = 1; /* FAIL */
2908 return;
2909 }
2910 is_curbuf = buf == curbuf;
2911
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002912 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002913 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002914 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002915 else
2916 last = first;
2917
2918 if (buf->b_ml.ml_mfp == NULL || first < 1
2919 || first > buf->b_ml.ml_line_count || last < first)
2920 {
2921 rettv->vval.v_number = 1; /* FAIL */
2922 return;
2923 }
2924
2925 if (!is_curbuf)
2926 {
2927 curbuf_save = curbuf;
2928 curwin_save = curwin;
2929 curbuf = buf;
2930 find_win_for_curbuf();
2931 }
2932 if (last > curbuf->b_ml.ml_line_count)
2933 last = curbuf->b_ml.ml_line_count;
2934 count = last - first + 1;
2935
2936 // When coming here from Insert mode, sync undo, so that this can be
2937 // undone separately from what was previously inserted.
2938 if (u_sync_once == 2)
2939 {
2940 u_sync_once = 1; // notify that u_sync() was called
2941 u_sync(TRUE);
2942 }
2943
2944 if (u_save(first - 1, last + 1) == FAIL)
2945 {
2946 rettv->vval.v_number = 1; /* FAIL */
2947 return;
2948 }
2949
2950 for (lnum = first; lnum <= last; ++lnum)
2951 ml_delete(first, TRUE);
2952
2953 FOR_ALL_TAB_WINDOWS(tp, wp)
2954 if (wp->w_buffer == buf)
2955 {
2956 if (wp->w_cursor.lnum > last)
2957 wp->w_cursor.lnum -= count;
2958 else if (wp->w_cursor.lnum> first)
2959 wp->w_cursor.lnum = first;
2960 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2961 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2962 }
2963 check_cursor_col();
2964 deleted_lines_mark(first, count);
2965
2966 if (!is_curbuf)
2967 {
2968 curbuf = curbuf_save;
2969 curwin = curwin_save;
2970 }
2971}
2972
2973/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002974 * "did_filetype()" function
2975 */
2976 static void
2977f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2978{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002979 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002980}
2981
2982/*
2983 * "diff_filler()" function
2984 */
2985 static void
2986f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2987{
2988#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002989 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002990#endif
2991}
2992
2993/*
2994 * "diff_hlID()" function
2995 */
2996 static void
2997f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2998{
2999#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003000 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003001 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003002 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003003 static int fnum = 0;
3004 static int change_start = 0;
3005 static int change_end = 0;
3006 static hlf_T hlID = (hlf_T)0;
3007 int filler_lines;
3008 int col;
3009
3010 if (lnum < 0) /* ignore type error in {lnum} arg */
3011 lnum = 0;
3012 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003013 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003014 || fnum != curbuf->b_fnum)
3015 {
3016 /* New line, buffer, change: need to get the values. */
3017 filler_lines = diff_check(curwin, lnum);
3018 if (filler_lines < 0)
3019 {
3020 if (filler_lines == -1)
3021 {
3022 change_start = MAXCOL;
3023 change_end = -1;
3024 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3025 hlID = HLF_ADD; /* added line */
3026 else
3027 hlID = HLF_CHD; /* changed line */
3028 }
3029 else
3030 hlID = HLF_ADD; /* added line */
3031 }
3032 else
3033 hlID = (hlf_T)0;
3034 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003035 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003036 fnum = curbuf->b_fnum;
3037 }
3038
3039 if (hlID == HLF_CHD || hlID == HLF_TXD)
3040 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003041 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003042 if (col >= change_start && col <= change_end)
3043 hlID = HLF_TXD; /* changed text */
3044 else
3045 hlID = HLF_CHD; /* changed line */
3046 }
3047 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3048#endif
3049}
3050
3051/*
3052 * "empty({expr})" function
3053 */
3054 static void
3055f_empty(typval_T *argvars, typval_T *rettv)
3056{
3057 int n = FALSE;
3058
3059 switch (argvars[0].v_type)
3060 {
3061 case VAR_STRING:
3062 case VAR_FUNC:
3063 n = argvars[0].vval.v_string == NULL
3064 || *argvars[0].vval.v_string == NUL;
3065 break;
3066 case VAR_PARTIAL:
3067 n = FALSE;
3068 break;
3069 case VAR_NUMBER:
3070 n = argvars[0].vval.v_number == 0;
3071 break;
3072 case VAR_FLOAT:
3073#ifdef FEAT_FLOAT
3074 n = argvars[0].vval.v_float == 0.0;
3075 break;
3076#endif
3077 case VAR_LIST:
3078 n = argvars[0].vval.v_list == NULL
3079 || argvars[0].vval.v_list->lv_first == NULL;
3080 break;
3081 case VAR_DICT:
3082 n = argvars[0].vval.v_dict == NULL
3083 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3084 break;
3085 case VAR_SPECIAL:
3086 n = argvars[0].vval.v_number != VVAL_TRUE;
3087 break;
3088
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003089 case VAR_BLOB:
3090 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003091 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3092 break;
3093
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003094 case VAR_JOB:
3095#ifdef FEAT_JOB_CHANNEL
3096 n = argvars[0].vval.v_job == NULL
3097 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3098 break;
3099#endif
3100 case VAR_CHANNEL:
3101#ifdef FEAT_JOB_CHANNEL
3102 n = argvars[0].vval.v_channel == NULL
3103 || !channel_is_open(argvars[0].vval.v_channel);
3104 break;
3105#endif
3106 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003107 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003108 n = TRUE;
3109 break;
3110 }
3111
3112 rettv->vval.v_number = n;
3113}
3114
3115/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003116 * "environ()" function
3117 */
3118 static void
3119f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3120{
3121#if !defined(AMIGA)
3122 int i = 0;
3123 char_u *entry, *value;
3124# ifdef MSWIN
3125 extern wchar_t **_wenviron;
3126# else
3127 extern char **environ;
3128# endif
3129
3130 if (rettv_dict_alloc(rettv) != OK)
3131 return;
3132
3133# ifdef MSWIN
3134 if (*_wenviron == NULL)
3135 return;
3136# else
3137 if (*environ == NULL)
3138 return;
3139# endif
3140
3141 for (i = 0; ; ++i)
3142 {
3143# ifdef MSWIN
3144 short_u *p;
3145
3146 if ((p = (short_u *)_wenviron[i]) == NULL)
3147 return;
3148 entry = utf16_to_enc(p, NULL);
3149# else
3150 if ((entry = (char_u *)environ[i]) == NULL)
3151 return;
3152 entry = vim_strsave(entry);
3153# endif
3154 if (entry == NULL) // out of memory
3155 return;
3156 if ((value = vim_strchr(entry, '=')) == NULL)
3157 {
3158 vim_free(entry);
3159 continue;
3160 }
3161 *value++ = NUL;
3162 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3163 vim_free(entry);
3164 }
3165#endif
3166}
3167
3168/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003169 * "escape({string}, {chars})" function
3170 */
3171 static void
3172f_escape(typval_T *argvars, typval_T *rettv)
3173{
3174 char_u buf[NUMBUFLEN];
3175
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003176 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3177 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003178 rettv->v_type = VAR_STRING;
3179}
3180
3181/*
3182 * "eval()" function
3183 */
3184 static void
3185f_eval(typval_T *argvars, typval_T *rettv)
3186{
3187 char_u *s, *p;
3188
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003189 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003190 if (s != NULL)
3191 s = skipwhite(s);
3192
3193 p = s;
3194 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3195 {
3196 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003197 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003198 need_clr_eos = FALSE;
3199 rettv->v_type = VAR_NUMBER;
3200 rettv->vval.v_number = 0;
3201 }
3202 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003203 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003204}
3205
3206/*
3207 * "eventhandler()" function
3208 */
3209 static void
3210f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3211{
3212 rettv->vval.v_number = vgetc_busy;
3213}
3214
3215/*
3216 * "executable()" function
3217 */
3218 static void
3219f_executable(typval_T *argvars, typval_T *rettv)
3220{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003221 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003222
3223 /* Check in $PATH and also check directly if there is a directory name. */
3224 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3225 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3226}
3227
3228static garray_T redir_execute_ga;
3229
3230/*
3231 * Append "value[value_len]" to the execute() output.
3232 */
3233 void
3234execute_redir_str(char_u *value, int value_len)
3235{
3236 int len;
3237
3238 if (value_len == -1)
3239 len = (int)STRLEN(value); /* Append the entire string */
3240 else
3241 len = value_len; /* Append only "value_len" characters */
3242 if (ga_grow(&redir_execute_ga, len) == OK)
3243 {
3244 mch_memmove((char *)redir_execute_ga.ga_data
3245 + redir_execute_ga.ga_len, value, len);
3246 redir_execute_ga.ga_len += len;
3247 }
3248}
3249
3250/*
3251 * Get next line from a list.
3252 * Called by do_cmdline() to get the next line.
3253 * Returns allocated string, or NULL for end of function.
3254 */
3255
3256 static char_u *
3257get_list_line(
3258 int c UNUSED,
3259 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003260 int indent UNUSED,
3261 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003262{
3263 listitem_T **p = (listitem_T **)cookie;
3264 listitem_T *item = *p;
3265 char_u buf[NUMBUFLEN];
3266 char_u *s;
3267
3268 if (item == NULL)
3269 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003270 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003271 *p = item->li_next;
3272 return s == NULL ? NULL : vim_strsave(s);
3273}
3274
3275/*
3276 * "execute()" function
3277 */
3278 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003279execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003280{
3281 char_u *cmd = NULL;
3282 list_T *list = NULL;
3283 int save_msg_silent = msg_silent;
3284 int save_emsg_silent = emsg_silent;
3285 int save_emsg_noredir = emsg_noredir;
3286 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003287 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003288 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003289 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003290 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003291
3292 rettv->vval.v_string = NULL;
3293 rettv->v_type = VAR_STRING;
3294
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003295 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003296 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003297 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003298 if (list == NULL || list->lv_first == NULL)
3299 /* empty list, no commands, empty output */
3300 return;
3301 ++list->lv_refcount;
3302 }
3303 else
3304 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003305 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003306 if (cmd == NULL)
3307 return;
3308 }
3309
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003310 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003311 {
3312 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003313 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003314
3315 if (s == NULL)
3316 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003317 if (*s == NUL)
3318 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003319 if (STRNCMP(s, "silent", 6) == 0)
3320 ++msg_silent;
3321 if (STRCMP(s, "silent!") == 0)
3322 {
3323 emsg_silent = TRUE;
3324 emsg_noredir = TRUE;
3325 }
3326 }
3327 else
3328 ++msg_silent;
3329
3330 if (redir_execute)
3331 save_ga = redir_execute_ga;
3332 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3333 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003334 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003335 if (!echo_output)
3336 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003337
3338 if (cmd != NULL)
3339 do_cmdline_cmd(cmd);
3340 else
3341 {
3342 listitem_T *item = list->lv_first;
3343
3344 do_cmdline(NULL, get_list_line, (void *)&item,
3345 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3346 --list->lv_refcount;
3347 }
3348
Bram Moolenaard297f352017-01-29 20:31:21 +01003349 /* Need to append a NUL to the result. */
3350 if (ga_grow(&redir_execute_ga, 1) == OK)
3351 {
3352 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3353 rettv->vval.v_string = redir_execute_ga.ga_data;
3354 }
3355 else
3356 {
3357 ga_clear(&redir_execute_ga);
3358 rettv->vval.v_string = NULL;
3359 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003360 msg_silent = save_msg_silent;
3361 emsg_silent = save_emsg_silent;
3362 emsg_noredir = save_emsg_noredir;
3363
3364 redir_execute = save_redir_execute;
3365 if (redir_execute)
3366 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003367 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003368
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003369 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003370 if (echo_output)
3371 // When not working silently: put it in column zero. A following
3372 // "echon" will overwrite the message, unavoidably.
3373 msg_col = 0;
3374 else
3375 // When working silently: Put it back where it was, since nothing
3376 // should have been written.
3377 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003378}
3379
3380/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003381 * "execute()" function
3382 */
3383 static void
3384f_execute(typval_T *argvars, typval_T *rettv)
3385{
3386 execute_common(argvars, rettv, 0);
3387}
3388
3389/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003390 * "exepath()" function
3391 */
3392 static void
3393f_exepath(typval_T *argvars, typval_T *rettv)
3394{
3395 char_u *p = NULL;
3396
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003397 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003398 rettv->v_type = VAR_STRING;
3399 rettv->vval.v_string = p;
3400}
3401
3402/*
3403 * "exists()" function
3404 */
3405 static void
3406f_exists(typval_T *argvars, typval_T *rettv)
3407{
3408 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003409 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003410
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003411 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003412 if (*p == '$') /* environment variable */
3413 {
3414 /* first try "normal" environment variables (fast) */
3415 if (mch_getenv(p + 1) != NULL)
3416 n = TRUE;
3417 else
3418 {
3419 /* try expanding things like $VIM and ${HOME} */
3420 p = expand_env_save(p);
3421 if (p != NULL && *p != '$')
3422 n = TRUE;
3423 vim_free(p);
3424 }
3425 }
3426 else if (*p == '&' || *p == '+') /* option */
3427 {
3428 n = (get_option_tv(&p, NULL, TRUE) == OK);
3429 if (*skipwhite(p) != NUL)
3430 n = FALSE; /* trailing garbage */
3431 }
3432 else if (*p == '*') /* internal or user defined function */
3433 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003434 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003435 }
3436 else if (*p == ':')
3437 {
3438 n = cmd_exists(p + 1);
3439 }
3440 else if (*p == '#')
3441 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003442 if (p[1] == '#')
3443 n = autocmd_supported(p + 2);
3444 else
3445 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003446 }
3447 else /* internal variable */
3448 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003449 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003450 }
3451
3452 rettv->vval.v_number = n;
3453}
3454
3455#ifdef FEAT_FLOAT
3456/*
3457 * "exp()" function
3458 */
3459 static void
3460f_exp(typval_T *argvars, typval_T *rettv)
3461{
3462 float_T f = 0.0;
3463
3464 rettv->v_type = VAR_FLOAT;
3465 if (get_float_arg(argvars, &f) == OK)
3466 rettv->vval.v_float = exp(f);
3467 else
3468 rettv->vval.v_float = 0.0;
3469}
3470#endif
3471
3472/*
3473 * "expand()" function
3474 */
3475 static void
3476f_expand(typval_T *argvars, typval_T *rettv)
3477{
3478 char_u *s;
3479 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003480 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003481 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3482 expand_T xpc;
3483 int error = FALSE;
3484 char_u *result;
3485
3486 rettv->v_type = VAR_STRING;
3487 if (argvars[1].v_type != VAR_UNKNOWN
3488 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003489 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003490 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003491 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003492
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003493 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003494 if (*s == '%' || *s == '#' || *s == '<')
3495 {
3496 ++emsg_off;
3497 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3498 --emsg_off;
3499 if (rettv->v_type == VAR_LIST)
3500 {
3501 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3502 list_append_string(rettv->vval.v_list, result, -1);
3503 else
3504 vim_free(result);
3505 }
3506 else
3507 rettv->vval.v_string = result;
3508 }
3509 else
3510 {
3511 /* When the optional second argument is non-zero, don't remove matches
3512 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3513 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003514 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003515 options |= WILD_KEEP_ALL;
3516 if (!error)
3517 {
3518 ExpandInit(&xpc);
3519 xpc.xp_context = EXPAND_FILES;
3520 if (p_wic)
3521 options += WILD_ICASE;
3522 if (rettv->v_type == VAR_STRING)
3523 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3524 options, WILD_ALL);
3525 else if (rettv_list_alloc(rettv) != FAIL)
3526 {
3527 int i;
3528
3529 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3530 for (i = 0; i < xpc.xp_numfiles; i++)
3531 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3532 ExpandCleanup(&xpc);
3533 }
3534 }
3535 else
3536 rettv->vval.v_string = NULL;
3537 }
3538}
3539
3540/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003541 * "expandcmd()" function
3542 * Expand all the special characters in a command string.
3543 */
3544 static void
3545f_expandcmd(typval_T *argvars, typval_T *rettv)
3546{
3547 exarg_T eap;
3548 char_u *cmdstr;
3549 char *errormsg = NULL;
3550
3551 rettv->v_type = VAR_STRING;
3552 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3553
3554 memset(&eap, 0, sizeof(eap));
3555 eap.cmd = cmdstr;
3556 eap.arg = cmdstr;
3557 eap.argt |= NOSPC;
3558 eap.usefilter = FALSE;
3559 eap.nextcmd = NULL;
3560 eap.cmdidx = CMD_USER;
3561
3562 expand_filename(&eap, &cmdstr, &errormsg);
3563 if (errormsg != NULL && *errormsg != NUL)
3564 emsg(errormsg);
3565
3566 rettv->vval.v_string = cmdstr;
3567}
3568
3569/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003570 * "extend(list, list [, idx])" function
3571 * "extend(dict, dict [, action])" function
3572 */
3573 static void
3574f_extend(typval_T *argvars, typval_T *rettv)
3575{
3576 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3577
3578 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3579 {
3580 list_T *l1, *l2;
3581 listitem_T *item;
3582 long before;
3583 int error = FALSE;
3584
3585 l1 = argvars[0].vval.v_list;
3586 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003587 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003588 && l2 != NULL)
3589 {
3590 if (argvars[2].v_type != VAR_UNKNOWN)
3591 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003592 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003593 if (error)
3594 return; /* type error; errmsg already given */
3595
3596 if (before == l1->lv_len)
3597 item = NULL;
3598 else
3599 {
3600 item = list_find(l1, before);
3601 if (item == NULL)
3602 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003603 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003604 return;
3605 }
3606 }
3607 }
3608 else
3609 item = NULL;
3610 list_extend(l1, l2, item);
3611
3612 copy_tv(&argvars[0], rettv);
3613 }
3614 }
3615 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3616 {
3617 dict_T *d1, *d2;
3618 char_u *action;
3619 int i;
3620
3621 d1 = argvars[0].vval.v_dict;
3622 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003623 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003624 && d2 != NULL)
3625 {
3626 /* Check the third argument. */
3627 if (argvars[2].v_type != VAR_UNKNOWN)
3628 {
3629 static char *(av[]) = {"keep", "force", "error"};
3630
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003631 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003632 if (action == NULL)
3633 return; /* type error; errmsg already given */
3634 for (i = 0; i < 3; ++i)
3635 if (STRCMP(action, av[i]) == 0)
3636 break;
3637 if (i == 3)
3638 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003639 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003640 return;
3641 }
3642 }
3643 else
3644 action = (char_u *)"force";
3645
3646 dict_extend(d1, d2, action);
3647
3648 copy_tv(&argvars[0], rettv);
3649 }
3650 }
3651 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003652 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003653}
3654
3655/*
3656 * "feedkeys()" function
3657 */
3658 static void
3659f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3660{
3661 int remap = TRUE;
3662 int insert = FALSE;
3663 char_u *keys, *flags;
3664 char_u nbuf[NUMBUFLEN];
3665 int typed = FALSE;
3666 int execute = FALSE;
3667 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003668 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003669 char_u *keys_esc;
3670
3671 /* This is not allowed in the sandbox. If the commands would still be
3672 * executed in the sandbox it would be OK, but it probably happens later,
3673 * when "sandbox" is no longer set. */
3674 if (check_secure())
3675 return;
3676
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003677 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003678
3679 if (argvars[1].v_type != VAR_UNKNOWN)
3680 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003681 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003682 for ( ; *flags != NUL; ++flags)
3683 {
3684 switch (*flags)
3685 {
3686 case 'n': remap = FALSE; break;
3687 case 'm': remap = TRUE; break;
3688 case 't': typed = TRUE; break;
3689 case 'i': insert = TRUE; break;
3690 case 'x': execute = TRUE; break;
3691 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003692 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003693 }
3694 }
3695 }
3696
3697 if (*keys != NUL || execute)
3698 {
3699 /* Need to escape K_SPECIAL and CSI before putting the string in the
3700 * typeahead buffer. */
3701 keys_esc = vim_strsave_escape_csi(keys);
3702 if (keys_esc != NULL)
3703 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003704 if (lowlevel)
3705 {
3706#ifdef USE_INPUT_BUF
3707 add_to_input_buf(keys, (int)STRLEN(keys));
3708#else
3709 emsg(_("E980: lowlevel input not supported"));
3710#endif
3711 }
3712 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003713 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003714 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003715 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003716 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003717#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003718 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003719#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003720 )
3721 typebuf_was_filled = TRUE;
3722 }
3723 vim_free(keys_esc);
3724
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003725 if (execute)
3726 {
3727 int save_msg_scroll = msg_scroll;
3728
3729 /* Avoid a 1 second delay when the keys start Insert mode. */
3730 msg_scroll = FALSE;
3731
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003732 if (!dangerous)
3733 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003734 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003735 if (!dangerous)
3736 --ex_normal_busy;
3737
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003738 msg_scroll |= save_msg_scroll;
3739 }
3740 }
3741 }
3742}
3743
3744/*
3745 * "filereadable()" function
3746 */
3747 static void
3748f_filereadable(typval_T *argvars, typval_T *rettv)
3749{
3750 int fd;
3751 char_u *p;
3752 int n;
3753
3754#ifndef O_NONBLOCK
3755# define O_NONBLOCK 0
3756#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003757 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003758 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3759 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3760 {
3761 n = TRUE;
3762 close(fd);
3763 }
3764 else
3765 n = FALSE;
3766
3767 rettv->vval.v_number = n;
3768}
3769
3770/*
3771 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3772 * rights to write into.
3773 */
3774 static void
3775f_filewritable(typval_T *argvars, typval_T *rettv)
3776{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003777 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003778}
3779
3780 static void
3781findfilendir(
3782 typval_T *argvars UNUSED,
3783 typval_T *rettv,
3784 int find_what UNUSED)
3785{
3786#ifdef FEAT_SEARCHPATH
3787 char_u *fname;
3788 char_u *fresult = NULL;
3789 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3790 char_u *p;
3791 char_u pathbuf[NUMBUFLEN];
3792 int count = 1;
3793 int first = TRUE;
3794 int error = FALSE;
3795#endif
3796
3797 rettv->vval.v_string = NULL;
3798 rettv->v_type = VAR_STRING;
3799
3800#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003801 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003802
3803 if (argvars[1].v_type != VAR_UNKNOWN)
3804 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003805 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003806 if (p == NULL)
3807 error = TRUE;
3808 else
3809 {
3810 if (*p != NUL)
3811 path = p;
3812
3813 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003814 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003815 }
3816 }
3817
3818 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3819 error = TRUE;
3820
3821 if (*fname != NUL && !error)
3822 {
3823 do
3824 {
3825 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3826 vim_free(fresult);
3827 fresult = find_file_in_path_option(first ? fname : NULL,
3828 first ? (int)STRLEN(fname) : 0,
3829 0, first, path,
3830 find_what,
3831 curbuf->b_ffname,
3832 find_what == FINDFILE_DIR
3833 ? (char_u *)"" : curbuf->b_p_sua);
3834 first = FALSE;
3835
3836 if (fresult != NULL && rettv->v_type == VAR_LIST)
3837 list_append_string(rettv->vval.v_list, fresult, -1);
3838
3839 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3840 }
3841
3842 if (rettv->v_type == VAR_STRING)
3843 rettv->vval.v_string = fresult;
3844#endif
3845}
3846
3847/*
3848 * "filter()" function
3849 */
3850 static void
3851f_filter(typval_T *argvars, typval_T *rettv)
3852{
3853 filter_map(argvars, rettv, FALSE);
3854}
3855
3856/*
3857 * "finddir({fname}[, {path}[, {count}]])" function
3858 */
3859 static void
3860f_finddir(typval_T *argvars, typval_T *rettv)
3861{
3862 findfilendir(argvars, rettv, FINDFILE_DIR);
3863}
3864
3865/*
3866 * "findfile({fname}[, {path}[, {count}]])" function
3867 */
3868 static void
3869f_findfile(typval_T *argvars, typval_T *rettv)
3870{
3871 findfilendir(argvars, rettv, FINDFILE_FILE);
3872}
3873
3874#ifdef FEAT_FLOAT
3875/*
3876 * "float2nr({float})" function
3877 */
3878 static void
3879f_float2nr(typval_T *argvars, typval_T *rettv)
3880{
3881 float_T f = 0.0;
3882
3883 if (get_float_arg(argvars, &f) == OK)
3884 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003885 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003886 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003887 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003888 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003889 else
3890 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003891 }
3892}
3893
3894/*
3895 * "floor({float})" function
3896 */
3897 static void
3898f_floor(typval_T *argvars, typval_T *rettv)
3899{
3900 float_T f = 0.0;
3901
3902 rettv->v_type = VAR_FLOAT;
3903 if (get_float_arg(argvars, &f) == OK)
3904 rettv->vval.v_float = floor(f);
3905 else
3906 rettv->vval.v_float = 0.0;
3907}
3908
3909/*
3910 * "fmod()" function
3911 */
3912 static void
3913f_fmod(typval_T *argvars, typval_T *rettv)
3914{
3915 float_T fx = 0.0, fy = 0.0;
3916
3917 rettv->v_type = VAR_FLOAT;
3918 if (get_float_arg(argvars, &fx) == OK
3919 && get_float_arg(&argvars[1], &fy) == OK)
3920 rettv->vval.v_float = fmod(fx, fy);
3921 else
3922 rettv->vval.v_float = 0.0;
3923}
3924#endif
3925
3926/*
3927 * "fnameescape({string})" function
3928 */
3929 static void
3930f_fnameescape(typval_T *argvars, typval_T *rettv)
3931{
3932 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003933 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003934 rettv->v_type = VAR_STRING;
3935}
3936
3937/*
3938 * "fnamemodify({fname}, {mods})" function
3939 */
3940 static void
3941f_fnamemodify(typval_T *argvars, typval_T *rettv)
3942{
3943 char_u *fname;
3944 char_u *mods;
3945 int usedlen = 0;
3946 int len;
3947 char_u *fbuf = NULL;
3948 char_u buf[NUMBUFLEN];
3949
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003950 fname = tv_get_string_chk(&argvars[0]);
3951 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003952 if (fname == NULL || mods == NULL)
3953 fname = NULL;
3954 else
3955 {
3956 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003957 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003958 }
3959
3960 rettv->v_type = VAR_STRING;
3961 if (fname == NULL)
3962 rettv->vval.v_string = NULL;
3963 else
3964 rettv->vval.v_string = vim_strnsave(fname, len);
3965 vim_free(fbuf);
3966}
3967
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003968/*
3969 * "foldclosed()" function
3970 */
3971 static void
3972foldclosed_both(
3973 typval_T *argvars UNUSED,
3974 typval_T *rettv,
3975 int end UNUSED)
3976{
3977#ifdef FEAT_FOLDING
3978 linenr_T lnum;
3979 linenr_T first, last;
3980
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003981 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003982 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3983 {
3984 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3985 {
3986 if (end)
3987 rettv->vval.v_number = (varnumber_T)last;
3988 else
3989 rettv->vval.v_number = (varnumber_T)first;
3990 return;
3991 }
3992 }
3993#endif
3994 rettv->vval.v_number = -1;
3995}
3996
3997/*
3998 * "foldclosed()" function
3999 */
4000 static void
4001f_foldclosed(typval_T *argvars, typval_T *rettv)
4002{
4003 foldclosed_both(argvars, rettv, FALSE);
4004}
4005
4006/*
4007 * "foldclosedend()" function
4008 */
4009 static void
4010f_foldclosedend(typval_T *argvars, typval_T *rettv)
4011{
4012 foldclosed_both(argvars, rettv, TRUE);
4013}
4014
4015/*
4016 * "foldlevel()" function
4017 */
4018 static void
4019f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4020{
4021#ifdef FEAT_FOLDING
4022 linenr_T lnum;
4023
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004024 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004025 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4026 rettv->vval.v_number = foldLevel(lnum);
4027#endif
4028}
4029
4030/*
4031 * "foldtext()" function
4032 */
4033 static void
4034f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4035{
4036#ifdef FEAT_FOLDING
4037 linenr_T foldstart;
4038 linenr_T foldend;
4039 char_u *dashes;
4040 linenr_T lnum;
4041 char_u *s;
4042 char_u *r;
4043 int len;
4044 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004045 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004046#endif
4047
4048 rettv->v_type = VAR_STRING;
4049 rettv->vval.v_string = NULL;
4050#ifdef FEAT_FOLDING
4051 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4052 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4053 dashes = get_vim_var_str(VV_FOLDDASHES);
4054 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4055 && dashes != NULL)
4056 {
4057 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004058 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004059 if (!linewhite(lnum))
4060 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004061
4062 /* Find interesting text in this line. */
4063 s = skipwhite(ml_get(lnum));
4064 /* skip C comment-start */
4065 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4066 {
4067 s = skipwhite(s + 2);
4068 if (*skipwhite(s) == NUL
4069 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4070 {
4071 s = skipwhite(ml_get(lnum + 1));
4072 if (*s == '*')
4073 s = skipwhite(s + 1);
4074 }
4075 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004076 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004077 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004078 r = alloc(STRLEN(txt)
4079 + STRLEN(dashes) // for %s
4080 + 20 // for %3ld
4081 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004082 if (r != NULL)
4083 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004084 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004085 len = (int)STRLEN(r);
4086 STRCAT(r, s);
4087 /* remove 'foldmarker' and 'commentstring' */
4088 foldtext_cleanup(r + len);
4089 rettv->vval.v_string = r;
4090 }
4091 }
4092#endif
4093}
4094
4095/*
4096 * "foldtextresult(lnum)" function
4097 */
4098 static void
4099f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4100{
4101#ifdef FEAT_FOLDING
4102 linenr_T lnum;
4103 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004104 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004105 foldinfo_T foldinfo;
4106 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004107 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004108#endif
4109
4110 rettv->v_type = VAR_STRING;
4111 rettv->vval.v_string = NULL;
4112#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004113 if (entered)
4114 return; /* reject recursive use */
4115 entered = TRUE;
4116
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004117 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004118 /* treat illegal types and illegal string values for {lnum} the same */
4119 if (lnum < 0)
4120 lnum = 0;
4121 fold_count = foldedCount(curwin, lnum, &foldinfo);
4122 if (fold_count > 0)
4123 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004124 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4125 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004126 if (text == buf)
4127 text = vim_strsave(text);
4128 rettv->vval.v_string = text;
4129 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004130
4131 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004132#endif
4133}
4134
4135/*
4136 * "foreground()" function
4137 */
4138 static void
4139f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4140{
4141#ifdef FEAT_GUI
4142 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004143 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004144 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004145 return;
4146 }
4147#endif
4148#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004149 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004150#endif
4151}
4152
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004153 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004154common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004155{
4156 char_u *s;
4157 char_u *name;
4158 int use_string = FALSE;
4159 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004160 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004161
4162 if (argvars[0].v_type == VAR_FUNC)
4163 {
4164 /* function(MyFunc, [arg], dict) */
4165 s = argvars[0].vval.v_string;
4166 }
4167 else if (argvars[0].v_type == VAR_PARTIAL
4168 && argvars[0].vval.v_partial != NULL)
4169 {
4170 /* function(dict.MyFunc, [arg]) */
4171 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004172 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004173 }
4174 else
4175 {
4176 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004177 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004178 use_string = TRUE;
4179 }
4180
Bram Moolenaar843b8842016-08-21 14:36:15 +02004181 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004182 {
4183 name = s;
4184 trans_name = trans_function_name(&name, FALSE,
4185 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4186 if (*name != NUL)
4187 s = NULL;
4188 }
4189
Bram Moolenaar843b8842016-08-21 14:36:15 +02004190 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4191 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004192 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004193 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004194 else if (trans_name != NULL && (is_funcref
4195 ? find_func(trans_name) == NULL
4196 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004197 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004198 else
4199 {
4200 int dict_idx = 0;
4201 int arg_idx = 0;
4202 list_T *list = NULL;
4203
4204 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4205 {
4206 char sid_buf[25];
4207 int off = *s == 's' ? 2 : 5;
4208
4209 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4210 * also be called from another script. Using trans_function_name()
4211 * would also work, but some plugins depend on the name being
4212 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004213 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004214 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004215 if (name != NULL)
4216 {
4217 STRCPY(name, sid_buf);
4218 STRCAT(name, s + off);
4219 }
4220 }
4221 else
4222 name = vim_strsave(s);
4223
4224 if (argvars[1].v_type != VAR_UNKNOWN)
4225 {
4226 if (argvars[2].v_type != VAR_UNKNOWN)
4227 {
4228 /* function(name, [args], dict) */
4229 arg_idx = 1;
4230 dict_idx = 2;
4231 }
4232 else if (argvars[1].v_type == VAR_DICT)
4233 /* function(name, dict) */
4234 dict_idx = 1;
4235 else
4236 /* function(name, [args]) */
4237 arg_idx = 1;
4238 if (dict_idx > 0)
4239 {
4240 if (argvars[dict_idx].v_type != VAR_DICT)
4241 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004242 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004243 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004244 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004245 }
4246 if (argvars[dict_idx].vval.v_dict == NULL)
4247 dict_idx = 0;
4248 }
4249 if (arg_idx > 0)
4250 {
4251 if (argvars[arg_idx].v_type != VAR_LIST)
4252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004253 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004254 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004255 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004256 }
4257 list = argvars[arg_idx].vval.v_list;
4258 if (list == NULL || list->lv_len == 0)
4259 arg_idx = 0;
4260 }
4261 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004262 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004263 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004264 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004265
4266 /* result is a VAR_PARTIAL */
4267 if (pt == NULL)
4268 vim_free(name);
4269 else
4270 {
4271 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4272 {
4273 listitem_T *li;
4274 int i = 0;
4275 int arg_len = 0;
4276 int lv_len = 0;
4277
4278 if (arg_pt != NULL)
4279 arg_len = arg_pt->pt_argc;
4280 if (list != NULL)
4281 lv_len = list->lv_len;
4282 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004283 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004284 if (pt->pt_argv == NULL)
4285 {
4286 vim_free(pt);
4287 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004288 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004289 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004290 for (i = 0; i < arg_len; i++)
4291 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4292 if (lv_len > 0)
4293 for (li = list->lv_first; li != NULL;
4294 li = li->li_next)
4295 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004296 }
4297
4298 /* For "function(dict.func, [], dict)" and "func" is a partial
4299 * use "dict". That is backwards compatible. */
4300 if (dict_idx > 0)
4301 {
4302 /* The dict is bound explicitly, pt_auto is FALSE. */
4303 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4304 ++pt->pt_dict->dv_refcount;
4305 }
4306 else if (arg_pt != NULL)
4307 {
4308 /* If the dict was bound automatically the result is also
4309 * bound automatically. */
4310 pt->pt_dict = arg_pt->pt_dict;
4311 pt->pt_auto = arg_pt->pt_auto;
4312 if (pt->pt_dict != NULL)
4313 ++pt->pt_dict->dv_refcount;
4314 }
4315
4316 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004317 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4318 {
4319 pt->pt_func = arg_pt->pt_func;
4320 func_ptr_ref(pt->pt_func);
4321 vim_free(name);
4322 }
4323 else if (is_funcref)
4324 {
4325 pt->pt_func = find_func(trans_name);
4326 func_ptr_ref(pt->pt_func);
4327 vim_free(name);
4328 }
4329 else
4330 {
4331 pt->pt_name = name;
4332 func_ref(name);
4333 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004334 }
4335 rettv->v_type = VAR_PARTIAL;
4336 rettv->vval.v_partial = pt;
4337 }
4338 else
4339 {
4340 /* result is a VAR_FUNC */
4341 rettv->v_type = VAR_FUNC;
4342 rettv->vval.v_string = name;
4343 func_ref(name);
4344 }
4345 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004346theend:
4347 vim_free(trans_name);
4348}
4349
4350/*
4351 * "funcref()" function
4352 */
4353 static void
4354f_funcref(typval_T *argvars, typval_T *rettv)
4355{
4356 common_function(argvars, rettv, TRUE);
4357}
4358
4359/*
4360 * "function()" function
4361 */
4362 static void
4363f_function(typval_T *argvars, typval_T *rettv)
4364{
4365 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004366}
4367
4368/*
4369 * "garbagecollect()" function
4370 */
4371 static void
4372f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4373{
4374 /* This is postponed until we are back at the toplevel, because we may be
4375 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4376 want_garbage_collect = TRUE;
4377
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004378 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004379 garbage_collect_at_exit = TRUE;
4380}
4381
4382/*
4383 * "get()" function
4384 */
4385 static void
4386f_get(typval_T *argvars, typval_T *rettv)
4387{
4388 listitem_T *li;
4389 list_T *l;
4390 dictitem_T *di;
4391 dict_T *d;
4392 typval_T *tv = NULL;
4393
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004394 if (argvars[0].v_type == VAR_BLOB)
4395 {
4396 int error = FALSE;
4397 int idx = tv_get_number_chk(&argvars[1], &error);
4398
4399 if (!error)
4400 {
4401 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004402 if (idx < 0)
4403 idx = blob_len(argvars[0].vval.v_blob) + idx;
4404 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4405 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004406 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004407 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004408 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004409 tv = rettv;
4410 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004411 }
4412 }
4413 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004414 {
4415 if ((l = argvars[0].vval.v_list) != NULL)
4416 {
4417 int error = FALSE;
4418
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004419 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004420 if (!error && li != NULL)
4421 tv = &li->li_tv;
4422 }
4423 }
4424 else if (argvars[0].v_type == VAR_DICT)
4425 {
4426 if ((d = argvars[0].vval.v_dict) != NULL)
4427 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004428 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004429 if (di != NULL)
4430 tv = &di->di_tv;
4431 }
4432 }
4433 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4434 {
4435 partial_T *pt;
4436 partial_T fref_pt;
4437
4438 if (argvars[0].v_type == VAR_PARTIAL)
4439 pt = argvars[0].vval.v_partial;
4440 else
4441 {
4442 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4443 fref_pt.pt_name = argvars[0].vval.v_string;
4444 pt = &fref_pt;
4445 }
4446
4447 if (pt != NULL)
4448 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004449 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004450 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004451
4452 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4453 {
4454 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004455 n = partial_name(pt);
4456 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004457 rettv->vval.v_string = NULL;
4458 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004459 {
4460 rettv->vval.v_string = vim_strsave(n);
4461 if (rettv->v_type == VAR_FUNC)
4462 func_ref(rettv->vval.v_string);
4463 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004464 }
4465 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004466 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004467 else if (STRCMP(what, "args") == 0)
4468 {
4469 rettv->v_type = VAR_LIST;
4470 if (rettv_list_alloc(rettv) == OK)
4471 {
4472 int i;
4473
4474 for (i = 0; i < pt->pt_argc; ++i)
4475 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4476 }
4477 }
4478 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004479 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004480 return;
4481 }
4482 }
4483 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004484 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004485
4486 if (tv == NULL)
4487 {
4488 if (argvars[2].v_type != VAR_UNKNOWN)
4489 copy_tv(&argvars[2], rettv);
4490 }
4491 else
4492 copy_tv(tv, rettv);
4493}
4494
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004495/*
4496 * Returns buffer options, variables and other attributes in a dictionary.
4497 */
4498 static dict_T *
4499get_buffer_info(buf_T *buf)
4500{
4501 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004502 tabpage_T *tp;
4503 win_T *wp;
4504 list_T *windows;
4505
4506 dict = dict_alloc();
4507 if (dict == NULL)
4508 return NULL;
4509
Bram Moolenaare0be1672018-07-08 16:50:37 +02004510 dict_add_number(dict, "bufnr", buf->b_fnum);
4511 dict_add_string(dict, "name", buf->b_ffname);
4512 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4513 : buflist_findlnum(buf));
4514 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4515 dict_add_number(dict, "listed", buf->b_p_bl);
4516 dict_add_number(dict, "changed", bufIsChanged(buf));
4517 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4518 dict_add_number(dict, "hidden",
4519 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004520
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004521 /* Get a reference to buffer variables */
4522 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004523
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004524 /* List of windows displaying this buffer */
4525 windows = list_alloc();
4526 if (windows != NULL)
4527 {
4528 FOR_ALL_TAB_WINDOWS(tp, wp)
4529 if (wp->w_buffer == buf)
4530 list_append_number(windows, (varnumber_T)wp->w_id);
4531 dict_add_list(dict, "windows", windows);
4532 }
4533
4534#ifdef FEAT_SIGNS
4535 if (buf->b_signlist != NULL)
4536 {
4537 /* List of signs placed in this buffer */
4538 list_T *signs = list_alloc();
4539 if (signs != NULL)
4540 {
4541 get_buffer_signs(buf, signs);
4542 dict_add_list(dict, "signs", signs);
4543 }
4544 }
4545#endif
4546
4547 return dict;
4548}
4549
4550/*
4551 * "getbufinfo()" function
4552 */
4553 static void
4554f_getbufinfo(typval_T *argvars, typval_T *rettv)
4555{
4556 buf_T *buf = NULL;
4557 buf_T *argbuf = NULL;
4558 dict_T *d;
4559 int filtered = FALSE;
4560 int sel_buflisted = FALSE;
4561 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004562 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004563
4564 if (rettv_list_alloc(rettv) != OK)
4565 return;
4566
4567 /* List of all the buffers or selected buffers */
4568 if (argvars[0].v_type == VAR_DICT)
4569 {
4570 dict_T *sel_d = argvars[0].vval.v_dict;
4571
4572 if (sel_d != NULL)
4573 {
4574 dictitem_T *di;
4575
4576 filtered = TRUE;
4577
4578 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004579 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004580 sel_buflisted = TRUE;
4581
4582 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004583 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004584 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004585
4586 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004587 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004588 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004589 }
4590 }
4591 else if (argvars[0].v_type != VAR_UNKNOWN)
4592 {
4593 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004594 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004595 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004596 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004597 --emsg_off;
4598 if (argbuf == NULL)
4599 return;
4600 }
4601
4602 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004603 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004604 {
4605 if (argbuf != NULL && argbuf != buf)
4606 continue;
4607 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004608 || (sel_buflisted && !buf->b_p_bl)
4609 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004610 continue;
4611
4612 d = get_buffer_info(buf);
4613 if (d != NULL)
4614 list_append_dict(rettv->vval.v_list, d);
4615 if (argbuf != NULL)
4616 return;
4617 }
4618}
4619
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004620/*
4621 * Get line or list of lines from buffer "buf" into "rettv".
4622 * Return a range (from start to end) of lines in rettv from the specified
4623 * buffer.
4624 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4625 */
4626 static void
4627get_buffer_lines(
4628 buf_T *buf,
4629 linenr_T start,
4630 linenr_T end,
4631 int retlist,
4632 typval_T *rettv)
4633{
4634 char_u *p;
4635
4636 rettv->v_type = VAR_STRING;
4637 rettv->vval.v_string = NULL;
4638 if (retlist && rettv_list_alloc(rettv) == FAIL)
4639 return;
4640
4641 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4642 return;
4643
4644 if (!retlist)
4645 {
4646 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4647 p = ml_get_buf(buf, start, FALSE);
4648 else
4649 p = (char_u *)"";
4650 rettv->vval.v_string = vim_strsave(p);
4651 }
4652 else
4653 {
4654 if (end < start)
4655 return;
4656
4657 if (start < 1)
4658 start = 1;
4659 if (end > buf->b_ml.ml_line_count)
4660 end = buf->b_ml.ml_line_count;
4661 while (start <= end)
4662 if (list_append_string(rettv->vval.v_list,
4663 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4664 break;
4665 }
4666}
4667
4668/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004669 * "getbufline()" function
4670 */
4671 static void
4672f_getbufline(typval_T *argvars, typval_T *rettv)
4673{
4674 linenr_T lnum;
4675 linenr_T end;
4676 buf_T *buf;
4677
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004678 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004679 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004680 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004681 --emsg_off;
4682
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004683 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004684 if (argvars[2].v_type == VAR_UNKNOWN)
4685 end = lnum;
4686 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004687 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004688
4689 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4690}
4691
4692/*
4693 * "getbufvar()" function
4694 */
4695 static void
4696f_getbufvar(typval_T *argvars, typval_T *rettv)
4697{
4698 buf_T *buf;
4699 buf_T *save_curbuf;
4700 char_u *varname;
4701 dictitem_T *v;
4702 int done = FALSE;
4703
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004704 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4705 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004706 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004707 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004708
4709 rettv->v_type = VAR_STRING;
4710 rettv->vval.v_string = NULL;
4711
4712 if (buf != NULL && varname != NULL)
4713 {
4714 /* set curbuf to be our buf, temporarily */
4715 save_curbuf = curbuf;
4716 curbuf = buf;
4717
Bram Moolenaar30567352016-08-27 21:25:44 +02004718 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004719 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004720 if (varname[1] == NUL)
4721 {
4722 /* get all buffer-local options in a dict */
4723 dict_T *opts = get_winbuf_options(TRUE);
4724
4725 if (opts != NULL)
4726 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004727 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004728 done = TRUE;
4729 }
4730 }
4731 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4732 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004733 done = TRUE;
4734 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004735 else
4736 {
4737 /* Look up the variable. */
4738 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4739 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4740 'b', varname, FALSE);
4741 if (v != NULL)
4742 {
4743 copy_tv(&v->di_tv, rettv);
4744 done = TRUE;
4745 }
4746 }
4747
4748 /* restore previous notion of curbuf */
4749 curbuf = save_curbuf;
4750 }
4751
4752 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4753 /* use the default value */
4754 copy_tv(&argvars[2], rettv);
4755
4756 --emsg_off;
4757}
4758
4759/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004760 * "getchangelist()" function
4761 */
4762 static void
4763f_getchangelist(typval_T *argvars, typval_T *rettv)
4764{
4765#ifdef FEAT_JUMPLIST
4766 buf_T *buf;
4767 int i;
4768 list_T *l;
4769 dict_T *d;
4770#endif
4771
4772 if (rettv_list_alloc(rettv) != OK)
4773 return;
4774
4775#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004776 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004777 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004778 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004779 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004780 if (buf == NULL)
4781 return;
4782
4783 l = list_alloc();
4784 if (l == NULL)
4785 return;
4786
4787 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4788 return;
4789 /*
4790 * The current window change list index tracks only the position in the
4791 * current buffer change list. For other buffers, use the change list
4792 * length as the current index.
4793 */
4794 list_append_number(rettv->vval.v_list,
4795 (varnumber_T)((buf == curwin->w_buffer)
4796 ? curwin->w_changelistidx : buf->b_changelistlen));
4797
4798 for (i = 0; i < buf->b_changelistlen; ++i)
4799 {
4800 if (buf->b_changelist[i].lnum == 0)
4801 continue;
4802 if ((d = dict_alloc()) == NULL)
4803 return;
4804 if (list_append_dict(l, d) == FAIL)
4805 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004806 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4807 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004808 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004809 }
4810#endif
4811}
4812/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004813 * "getchar()" function
4814 */
4815 static void
4816f_getchar(typval_T *argvars, typval_T *rettv)
4817{
4818 varnumber_T n;
4819 int error = FALSE;
4820
Bram Moolenaar84d93902018-09-11 20:10:20 +02004821#ifdef MESSAGE_QUEUE
4822 // vpeekc() used to check for messages, but that caused problems, invoking
4823 // a callback where it was not expected. Some plugins use getchar(1) in a
4824 // loop to await a message, therefore make sure we check for messages here.
4825 parse_queued_messages();
4826#endif
4827
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004828 /* Position the cursor. Needed after a message that ends in a space. */
4829 windgoto(msg_row, msg_col);
4830
4831 ++no_mapping;
4832 ++allow_keys;
4833 for (;;)
4834 {
4835 if (argvars[0].v_type == VAR_UNKNOWN)
4836 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004837 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004838 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004839 /* getchar(1): only check if char avail */
4840 n = vpeekc_any();
4841 else if (error || vpeekc_any() == NUL)
4842 /* illegal argument or getchar(0) and no char avail: return zero */
4843 n = 0;
4844 else
4845 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004846 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004847
4848 if (n == K_IGNORE)
4849 continue;
4850 break;
4851 }
4852 --no_mapping;
4853 --allow_keys;
4854
4855 set_vim_var_nr(VV_MOUSE_WIN, 0);
4856 set_vim_var_nr(VV_MOUSE_WINID, 0);
4857 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4858 set_vim_var_nr(VV_MOUSE_COL, 0);
4859
4860 rettv->vval.v_number = n;
4861 if (IS_SPECIAL(n) || mod_mask != 0)
4862 {
4863 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4864 int i = 0;
4865
4866 /* Turn a special key into three bytes, plus modifier. */
4867 if (mod_mask != 0)
4868 {
4869 temp[i++] = K_SPECIAL;
4870 temp[i++] = KS_MODIFIER;
4871 temp[i++] = mod_mask;
4872 }
4873 if (IS_SPECIAL(n))
4874 {
4875 temp[i++] = K_SPECIAL;
4876 temp[i++] = K_SECOND(n);
4877 temp[i++] = K_THIRD(n);
4878 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004879 else if (has_mbyte)
4880 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004881 else
4882 temp[i++] = n;
4883 temp[i++] = NUL;
4884 rettv->v_type = VAR_STRING;
4885 rettv->vval.v_string = vim_strsave(temp);
4886
4887#ifdef FEAT_MOUSE
4888 if (is_mouse_key(n))
4889 {
4890 int row = mouse_row;
4891 int col = mouse_col;
4892 win_T *win;
4893 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004894 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004895 int winnr = 1;
4896
4897 if (row >= 0 && col >= 0)
4898 {
4899 /* Find the window at the mouse coordinates and compute the
4900 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004901 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004902 if (win == NULL)
4903 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004904 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004905# ifdef FEAT_TEXT_PROP
4906 if (bt_popup(win->w_buffer))
4907 winnr = 0;
4908 else
4909# endif
4910 for (wp = firstwin; wp != win && wp != NULL;
4911 wp = wp->w_next)
4912 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004913 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4914 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4915 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4916 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4917 }
4918 }
4919#endif
4920 }
4921}
4922
4923/*
4924 * "getcharmod()" function
4925 */
4926 static void
4927f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4928{
4929 rettv->vval.v_number = mod_mask;
4930}
4931
4932/*
4933 * "getcharsearch()" function
4934 */
4935 static void
4936f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4937{
4938 if (rettv_dict_alloc(rettv) != FAIL)
4939 {
4940 dict_T *dict = rettv->vval.v_dict;
4941
Bram Moolenaare0be1672018-07-08 16:50:37 +02004942 dict_add_string(dict, "char", last_csearch());
4943 dict_add_number(dict, "forward", last_csearch_forward());
4944 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004945 }
4946}
4947
4948/*
4949 * "getcmdline()" function
4950 */
4951 static void
4952f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4953{
4954 rettv->v_type = VAR_STRING;
4955 rettv->vval.v_string = get_cmdline_str();
4956}
4957
4958/*
4959 * "getcmdpos()" function
4960 */
4961 static void
4962f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4963{
4964 rettv->vval.v_number = get_cmdline_pos() + 1;
4965}
4966
4967/*
4968 * "getcmdtype()" function
4969 */
4970 static void
4971f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4972{
4973 rettv->v_type = VAR_STRING;
4974 rettv->vval.v_string = alloc(2);
4975 if (rettv->vval.v_string != NULL)
4976 {
4977 rettv->vval.v_string[0] = get_cmdline_type();
4978 rettv->vval.v_string[1] = NUL;
4979 }
4980}
4981
4982/*
4983 * "getcmdwintype()" function
4984 */
4985 static void
4986f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4987{
4988 rettv->v_type = VAR_STRING;
4989 rettv->vval.v_string = NULL;
4990#ifdef FEAT_CMDWIN
4991 rettv->vval.v_string = alloc(2);
4992 if (rettv->vval.v_string != NULL)
4993 {
4994 rettv->vval.v_string[0] = cmdwin_type;
4995 rettv->vval.v_string[1] = NUL;
4996 }
4997#endif
4998}
4999
5000#if defined(FEAT_CMDL_COMPL)
5001/*
5002 * "getcompletion()" function
5003 */
5004 static void
5005f_getcompletion(typval_T *argvars, typval_T *rettv)
5006{
5007 char_u *pat;
5008 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005009 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005010 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5011 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005012
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005013 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005014 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005015
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005016 if (p_wic)
5017 options |= WILD_ICASE;
5018
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005019 /* For filtered results, 'wildignore' is used */
5020 if (!filtered)
5021 options |= WILD_KEEP_ALL;
5022
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005023 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005024 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005025 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005026 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005027 if (xpc.xp_context == EXPAND_NOTHING)
5028 {
5029 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005030 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005031 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005032 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005033 return;
5034 }
5035
5036# if defined(FEAT_MENU)
5037 if (xpc.xp_context == EXPAND_MENUS)
5038 {
5039 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5040 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5041 }
5042# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005043#ifdef FEAT_CSCOPE
5044 if (xpc.xp_context == EXPAND_CSCOPE)
5045 {
5046 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5047 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5048 }
5049#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005050#ifdef FEAT_SIGNS
5051 if (xpc.xp_context == EXPAND_SIGN)
5052 {
5053 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5054 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5055 }
5056#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005057
5058 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5059 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5060 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005061 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005062
5063 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5064
5065 for (i = 0; i < xpc.xp_numfiles; i++)
5066 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5067 }
5068 vim_free(pat);
5069 ExpandCleanup(&xpc);
5070}
5071#endif
5072
5073/*
5074 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005075 *
5076 * Return the current working directory of a window in a tab page.
5077 * First optional argument 'winnr' is the window number or -1 and the second
5078 * optional argument 'tabnr' is the tab page number.
5079 *
5080 * If no arguments are supplied, then return the directory of the current
5081 * window.
5082 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5083 * the specified window.
5084 * If 'winnr' is 0 then return the directory of the current window.
5085 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5086 * directory of the specified tab page. Otherwise return the directory of the
5087 * specified window in the specified tab page.
5088 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005089 */
5090 static void
5091f_getcwd(typval_T *argvars, typval_T *rettv)
5092{
5093 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005094 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005095 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005096 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005097
5098 rettv->v_type = VAR_STRING;
5099 rettv->vval.v_string = NULL;
5100
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005101 if (argvars[0].v_type == VAR_NUMBER
5102 && argvars[0].vval.v_number == -1
5103 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005104 global = TRUE;
5105 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005106 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005107
5108 if (wp != NULL && wp->w_localdir != NULL)
5109 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005110 else if (tp != NULL && tp->tp_localdir != NULL)
5111 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5112 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005113 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005114 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005115 rettv->vval.v_string = vim_strsave(globaldir);
5116 else
5117 {
5118 cwd = alloc(MAXPATHL);
5119 if (cwd != NULL)
5120 {
5121 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5122 rettv->vval.v_string = vim_strsave(cwd);
5123 vim_free(cwd);
5124 }
5125 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005126 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005127#ifdef BACKSLASH_IN_FILENAME
5128 if (rettv->vval.v_string != NULL)
5129 slash_adjust(rettv->vval.v_string);
5130#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005131}
5132
5133/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005134 * "getenv()" function
5135 */
5136 static void
5137f_getenv(typval_T *argvars, typval_T *rettv)
5138{
5139 int mustfree = FALSE;
5140 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5141
5142 if (p == NULL)
5143 {
5144 rettv->v_type = VAR_SPECIAL;
5145 rettv->vval.v_number = VVAL_NULL;
5146 return;
5147 }
5148 if (!mustfree)
5149 p = vim_strsave(p);
5150 rettv->vval.v_string = p;
5151 rettv->v_type = VAR_STRING;
5152}
5153
5154/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005155 * "getfontname()" function
5156 */
5157 static void
5158f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5159{
5160 rettv->v_type = VAR_STRING;
5161 rettv->vval.v_string = NULL;
5162#ifdef FEAT_GUI
5163 if (gui.in_use)
5164 {
5165 GuiFont font;
5166 char_u *name = NULL;
5167
5168 if (argvars[0].v_type == VAR_UNKNOWN)
5169 {
5170 /* Get the "Normal" font. Either the name saved by
5171 * hl_set_font_name() or from the font ID. */
5172 font = gui.norm_font;
5173 name = hl_get_font_name();
5174 }
5175 else
5176 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005177 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005178 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5179 return;
5180 font = gui_mch_get_font(name, FALSE);
5181 if (font == NOFONT)
5182 return; /* Invalid font name, return empty string. */
5183 }
5184 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5185 if (argvars[0].v_type != VAR_UNKNOWN)
5186 gui_mch_free_font(font);
5187 }
5188#endif
5189}
5190
5191/*
5192 * "getfperm({fname})" function
5193 */
5194 static void
5195f_getfperm(typval_T *argvars, typval_T *rettv)
5196{
5197 char_u *fname;
5198 stat_T st;
5199 char_u *perm = NULL;
5200 char_u flags[] = "rwx";
5201 int i;
5202
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005203 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005204
5205 rettv->v_type = VAR_STRING;
5206 if (mch_stat((char *)fname, &st) >= 0)
5207 {
5208 perm = vim_strsave((char_u *)"---------");
5209 if (perm != NULL)
5210 {
5211 for (i = 0; i < 9; i++)
5212 {
5213 if (st.st_mode & (1 << (8 - i)))
5214 perm[i] = flags[i % 3];
5215 }
5216 }
5217 }
5218 rettv->vval.v_string = perm;
5219}
5220
5221/*
5222 * "getfsize({fname})" function
5223 */
5224 static void
5225f_getfsize(typval_T *argvars, typval_T *rettv)
5226{
5227 char_u *fname;
5228 stat_T st;
5229
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005230 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005231
5232 rettv->v_type = VAR_NUMBER;
5233
5234 if (mch_stat((char *)fname, &st) >= 0)
5235 {
5236 if (mch_isdir(fname))
5237 rettv->vval.v_number = 0;
5238 else
5239 {
5240 rettv->vval.v_number = (varnumber_T)st.st_size;
5241
5242 /* non-perfect check for overflow */
5243 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5244 rettv->vval.v_number = -2;
5245 }
5246 }
5247 else
5248 rettv->vval.v_number = -1;
5249}
5250
5251/*
5252 * "getftime({fname})" function
5253 */
5254 static void
5255f_getftime(typval_T *argvars, typval_T *rettv)
5256{
5257 char_u *fname;
5258 stat_T st;
5259
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005260 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005261
5262 if (mch_stat((char *)fname, &st) >= 0)
5263 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5264 else
5265 rettv->vval.v_number = -1;
5266}
5267
5268/*
5269 * "getftype({fname})" function
5270 */
5271 static void
5272f_getftype(typval_T *argvars, typval_T *rettv)
5273{
5274 char_u *fname;
5275 stat_T st;
5276 char_u *type = NULL;
5277 char *t;
5278
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005279 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005280
5281 rettv->v_type = VAR_STRING;
5282 if (mch_lstat((char *)fname, &st) >= 0)
5283 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005284 if (S_ISREG(st.st_mode))
5285 t = "file";
5286 else if (S_ISDIR(st.st_mode))
5287 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005288 else if (S_ISLNK(st.st_mode))
5289 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005290 else if (S_ISBLK(st.st_mode))
5291 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005292 else if (S_ISCHR(st.st_mode))
5293 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005294 else if (S_ISFIFO(st.st_mode))
5295 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005296 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005297 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005298 else
5299 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005300 type = vim_strsave((char_u *)t);
5301 }
5302 rettv->vval.v_string = type;
5303}
5304
5305/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005306 * "getjumplist()" function
5307 */
5308 static void
5309f_getjumplist(typval_T *argvars, typval_T *rettv)
5310{
5311#ifdef FEAT_JUMPLIST
5312 win_T *wp;
5313 int i;
5314 list_T *l;
5315 dict_T *d;
5316#endif
5317
5318 if (rettv_list_alloc(rettv) != OK)
5319 return;
5320
5321#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005322 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005323 if (wp == NULL)
5324 return;
5325
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005326 cleanup_jumplist(wp, TRUE);
5327
Bram Moolenaar4f505882018-02-10 21:06:32 +01005328 l = list_alloc();
5329 if (l == NULL)
5330 return;
5331
5332 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5333 return;
5334 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5335
5336 for (i = 0; i < wp->w_jumplistlen; ++i)
5337 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005338 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5339 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005340 if ((d = dict_alloc()) == NULL)
5341 return;
5342 if (list_append_dict(l, d) == FAIL)
5343 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005344 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5345 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005346 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005347 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005348 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005349 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005350 }
5351#endif
5352}
5353
5354/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005355 * "getline(lnum, [end])" function
5356 */
5357 static void
5358f_getline(typval_T *argvars, typval_T *rettv)
5359{
5360 linenr_T lnum;
5361 linenr_T end;
5362 int retlist;
5363
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005364 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005365 if (argvars[1].v_type == VAR_UNKNOWN)
5366 {
5367 end = 0;
5368 retlist = FALSE;
5369 }
5370 else
5371 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005372 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005373 retlist = TRUE;
5374 }
5375
5376 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5377}
5378
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005379#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005380 static void
5381get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5382{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005383 if (what_arg->v_type == VAR_UNKNOWN)
5384 {
5385 if (rettv_list_alloc(rettv) == OK)
5386 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005387 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005388 }
5389 else
5390 {
5391 if (rettv_dict_alloc(rettv) == OK)
5392 if (is_qf || (wp != NULL))
5393 {
5394 if (what_arg->v_type == VAR_DICT)
5395 {
5396 dict_T *d = what_arg->vval.v_dict;
5397
5398 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005399 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005400 }
5401 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005402 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005403 }
5404 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005405}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005406#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005407
5408/*
5409 * "getloclist()" function
5410 */
5411 static void
5412f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5413{
5414#ifdef FEAT_QUICKFIX
5415 win_T *wp;
5416
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005417 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005418 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5419#endif
5420}
5421
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005422/*
5423 * "getmatches()" function
5424 */
5425 static void
5426f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5427{
5428#ifdef FEAT_SEARCH_EXTRA
5429 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005430 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005431 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005432 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005433
Bram Moolenaaraff74912019-03-30 18:11:49 +01005434 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5435 return;
5436
5437 cur = win->w_match_head;
5438 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005439 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005440 dict = dict_alloc();
5441 if (dict == NULL)
5442 return;
5443 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005444 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005445 /* match added with matchaddpos() */
5446 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005447 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005448 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005449 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005450 list_T *l;
5451
5452 llpos = &cur->pos.pos[i];
5453 if (llpos->lnum == 0)
5454 break;
5455 l = list_alloc();
5456 if (l == NULL)
5457 break;
5458 list_append_number(l, (varnumber_T)llpos->lnum);
5459 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005460 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005461 list_append_number(l, (varnumber_T)llpos->col);
5462 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005463 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005464 sprintf(buf, "pos%d", i + 1);
5465 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005466 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005467 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005468 else
5469 {
5470 dict_add_string(dict, "pattern", cur->pattern);
5471 }
5472 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5473 dict_add_number(dict, "priority", (long)cur->priority);
5474 dict_add_number(dict, "id", (long)cur->id);
5475# if defined(FEAT_CONCEAL)
5476 if (cur->conceal_char)
5477 {
5478 char_u buf[MB_MAXBYTES + 1];
5479
5480 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5481 dict_add_string(dict, "conceal", (char_u *)&buf);
5482 }
5483# endif
5484 list_append_dict(rettv->vval.v_list, dict);
5485 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005486 }
5487#endif
5488}
5489
5490/*
5491 * "getpid()" function
5492 */
5493 static void
5494f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5495{
5496 rettv->vval.v_number = mch_get_pid();
5497}
5498
5499 static void
5500getpos_both(
5501 typval_T *argvars,
5502 typval_T *rettv,
5503 int getcurpos)
5504{
5505 pos_T *fp;
5506 list_T *l;
5507 int fnum = -1;
5508
5509 if (rettv_list_alloc(rettv) == OK)
5510 {
5511 l = rettv->vval.v_list;
5512 if (getcurpos)
5513 fp = &curwin->w_cursor;
5514 else
5515 fp = var2fpos(&argvars[0], TRUE, &fnum);
5516 if (fnum != -1)
5517 list_append_number(l, (varnumber_T)fnum);
5518 else
5519 list_append_number(l, (varnumber_T)0);
5520 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5521 : (varnumber_T)0);
5522 list_append_number(l, (fp != NULL)
5523 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5524 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005525 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005526 (varnumber_T)0);
5527 if (getcurpos)
5528 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005529 int save_set_curswant = curwin->w_set_curswant;
5530 colnr_T save_curswant = curwin->w_curswant;
5531 colnr_T save_virtcol = curwin->w_virtcol;
5532
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005533 update_curswant();
5534 list_append_number(l, curwin->w_curswant == MAXCOL ?
5535 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005536
5537 // Do not change "curswant", as it is unexpected that a get
5538 // function has a side effect.
5539 if (save_set_curswant)
5540 {
5541 curwin->w_set_curswant = save_set_curswant;
5542 curwin->w_curswant = save_curswant;
5543 curwin->w_virtcol = save_virtcol;
5544 curwin->w_valid &= ~VALID_VIRTCOL;
5545 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546 }
5547 }
5548 else
5549 rettv->vval.v_number = FALSE;
5550}
5551
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005552/*
5553 * "getcurpos()" function
5554 */
5555 static void
5556f_getcurpos(typval_T *argvars, typval_T *rettv)
5557{
5558 getpos_both(argvars, rettv, TRUE);
5559}
5560
5561/*
5562 * "getpos(string)" function
5563 */
5564 static void
5565f_getpos(typval_T *argvars, typval_T *rettv)
5566{
5567 getpos_both(argvars, rettv, FALSE);
5568}
5569
5570/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005571 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005572 */
5573 static void
5574f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5575{
5576#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005577 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005578#endif
5579}
5580
5581/*
5582 * "getreg()" function
5583 */
5584 static void
5585f_getreg(typval_T *argvars, typval_T *rettv)
5586{
5587 char_u *strregname;
5588 int regname;
5589 int arg2 = FALSE;
5590 int return_list = FALSE;
5591 int error = FALSE;
5592
5593 if (argvars[0].v_type != VAR_UNKNOWN)
5594 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005595 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005596 error = strregname == NULL;
5597 if (argvars[1].v_type != VAR_UNKNOWN)
5598 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005599 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005600 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005601 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005602 }
5603 }
5604 else
5605 strregname = get_vim_var_str(VV_REG);
5606
5607 if (error)
5608 return;
5609
5610 regname = (strregname == NULL ? '"' : *strregname);
5611 if (regname == 0)
5612 regname = '"';
5613
5614 if (return_list)
5615 {
5616 rettv->v_type = VAR_LIST;
5617 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5618 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5619 if (rettv->vval.v_list == NULL)
5620 (void)rettv_list_alloc(rettv);
5621 else
5622 ++rettv->vval.v_list->lv_refcount;
5623 }
5624 else
5625 {
5626 rettv->v_type = VAR_STRING;
5627 rettv->vval.v_string = get_reg_contents(regname,
5628 arg2 ? GREG_EXPR_SRC : 0);
5629 }
5630}
5631
5632/*
5633 * "getregtype()" function
5634 */
5635 static void
5636f_getregtype(typval_T *argvars, typval_T *rettv)
5637{
5638 char_u *strregname;
5639 int regname;
5640 char_u buf[NUMBUFLEN + 2];
5641 long reglen = 0;
5642
5643 if (argvars[0].v_type != VAR_UNKNOWN)
5644 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005645 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005646 if (strregname == NULL) /* type error; errmsg already given */
5647 {
5648 rettv->v_type = VAR_STRING;
5649 rettv->vval.v_string = NULL;
5650 return;
5651 }
5652 }
5653 else
5654 /* Default to v:register */
5655 strregname = get_vim_var_str(VV_REG);
5656
5657 regname = (strregname == NULL ? '"' : *strregname);
5658 if (regname == 0)
5659 regname = '"';
5660
5661 buf[0] = NUL;
5662 buf[1] = NUL;
5663 switch (get_reg_type(regname, &reglen))
5664 {
5665 case MLINE: buf[0] = 'V'; break;
5666 case MCHAR: buf[0] = 'v'; break;
5667 case MBLOCK:
5668 buf[0] = Ctrl_V;
5669 sprintf((char *)buf + 1, "%ld", reglen + 1);
5670 break;
5671 }
5672 rettv->v_type = VAR_STRING;
5673 rettv->vval.v_string = vim_strsave(buf);
5674}
5675
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005676/*
5677 * Returns information (variables, options, etc.) about a tab page
5678 * as a dictionary.
5679 */
5680 static dict_T *
5681get_tabpage_info(tabpage_T *tp, int tp_idx)
5682{
5683 win_T *wp;
5684 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005685 list_T *l;
5686
5687 dict = dict_alloc();
5688 if (dict == NULL)
5689 return NULL;
5690
Bram Moolenaare0be1672018-07-08 16:50:37 +02005691 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005692
5693 l = list_alloc();
5694 if (l != NULL)
5695 {
5696 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5697 wp; wp = wp->w_next)
5698 list_append_number(l, (varnumber_T)wp->w_id);
5699 dict_add_list(dict, "windows", l);
5700 }
5701
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005702 /* Make a reference to tabpage variables */
5703 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005704
5705 return dict;
5706}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005707
5708/*
5709 * "gettabinfo()" function
5710 */
5711 static void
5712f_gettabinfo(typval_T *argvars, typval_T *rettv)
5713{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005714 tabpage_T *tp, *tparg = NULL;
5715 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005716 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005717
5718 if (rettv_list_alloc(rettv) != OK)
5719 return;
5720
5721 if (argvars[0].v_type != VAR_UNKNOWN)
5722 {
5723 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005724 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005725 if (tparg == NULL)
5726 return;
5727 }
5728
5729 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005730 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005731 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005732 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005733 if (tparg != NULL && tp != tparg)
5734 continue;
5735 d = get_tabpage_info(tp, tpnr);
5736 if (d != NULL)
5737 list_append_dict(rettv->vval.v_list, d);
5738 if (tparg != NULL)
5739 return;
5740 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005741}
5742
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005743/*
5744 * "gettabvar()" function
5745 */
5746 static void
5747f_gettabvar(typval_T *argvars, typval_T *rettv)
5748{
5749 win_T *oldcurwin;
5750 tabpage_T *tp, *oldtabpage;
5751 dictitem_T *v;
5752 char_u *varname;
5753 int done = FALSE;
5754
5755 rettv->v_type = VAR_STRING;
5756 rettv->vval.v_string = NULL;
5757
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005758 varname = tv_get_string_chk(&argvars[1]);
5759 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005760 if (tp != NULL && varname != NULL)
5761 {
5762 /* Set tp to be our tabpage, temporarily. Also set the window to the
5763 * first window in the tabpage, otherwise the window is not valid. */
5764 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005765 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5766 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005767 {
5768 /* look up the variable */
5769 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5770 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5771 if (v != NULL)
5772 {
5773 copy_tv(&v->di_tv, rettv);
5774 done = TRUE;
5775 }
5776 }
5777
5778 /* restore previous notion of curwin */
5779 restore_win(oldcurwin, oldtabpage, TRUE);
5780 }
5781
5782 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5783 copy_tv(&argvars[2], rettv);
5784}
5785
5786/*
5787 * "gettabwinvar()" function
5788 */
5789 static void
5790f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5791{
5792 getwinvar(argvars, rettv, 1);
5793}
5794
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005795/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005796 * "gettagstack()" function
5797 */
5798 static void
5799f_gettagstack(typval_T *argvars, typval_T *rettv)
5800{
5801 win_T *wp = curwin; // default is current window
5802
5803 if (rettv_dict_alloc(rettv) != OK)
5804 return;
5805
5806 if (argvars[0].v_type != VAR_UNKNOWN)
5807 {
5808 wp = find_win_by_nr_or_id(&argvars[0]);
5809 if (wp == NULL)
5810 return;
5811 }
5812
5813 get_tagstack(wp, rettv->vval.v_dict);
5814}
5815
5816/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005817 * Returns information about a window as a dictionary.
5818 */
5819 static dict_T *
5820get_win_info(win_T *wp, short tpnr, short winnr)
5821{
5822 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005823
5824 dict = dict_alloc();
5825 if (dict == NULL)
5826 return NULL;
5827
Bram Moolenaare0be1672018-07-08 16:50:37 +02005828 dict_add_number(dict, "tabnr", tpnr);
5829 dict_add_number(dict, "winnr", winnr);
5830 dict_add_number(dict, "winid", wp->w_id);
5831 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005832 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005833 dict_add_number(dict, "topline", wp->w_topline);
5834 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005835#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005836 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005837#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005838 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005839 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005840 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005841
Bram Moolenaar69905d12017-08-13 18:14:47 +02005842#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005843 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005844#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005845#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005846 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5847 dict_add_number(dict, "loclist",
5848 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005849#endif
5850
Bram Moolenaar30567352016-08-27 21:25:44 +02005851 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005852 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005853
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005854 return dict;
5855}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005856
5857/*
5858 * "getwininfo()" function
5859 */
5860 static void
5861f_getwininfo(typval_T *argvars, typval_T *rettv)
5862{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005863 tabpage_T *tp;
5864 win_T *wp = NULL, *wparg = NULL;
5865 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005866 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005867
5868 if (rettv_list_alloc(rettv) != OK)
5869 return;
5870
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005871 if (argvars[0].v_type != VAR_UNKNOWN)
5872 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005873 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005874 if (wparg == NULL)
5875 return;
5876 }
5877
5878 /* Collect information about either all the windows across all the tab
5879 * pages or one particular window.
5880 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005881 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005882 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005883 tabnr++;
5884 winnr = 0;
5885 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005886 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005887 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005888 if (wparg != NULL && wp != wparg)
5889 continue;
5890 d = get_win_info(wp, tabnr, winnr);
5891 if (d != NULL)
5892 list_append_dict(rettv->vval.v_list, d);
5893 if (wparg != NULL)
5894 /* found information about a specific window */
5895 return;
5896 }
5897 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005898}
5899
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005900/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005901 * "win_execute()" function
5902 */
5903 static void
5904f_win_execute(typval_T *argvars, typval_T *rettv)
5905{
5906 int id = (int)tv_get_number(argvars);
5907 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005908 win_T *save_curwin;
5909 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005910
5911 if (wp != NULL)
5912 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005913 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5914 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005915 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005916 check_cursor();
5917 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005918 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005919 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005920 }
5921}
5922
5923/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005924 * "win_findbuf()" function
5925 */
5926 static void
5927f_win_findbuf(typval_T *argvars, typval_T *rettv)
5928{
5929 if (rettv_list_alloc(rettv) != FAIL)
5930 win_findbuf(argvars, rettv->vval.v_list);
5931}
5932
5933/*
5934 * "win_getid()" function
5935 */
5936 static void
5937f_win_getid(typval_T *argvars, typval_T *rettv)
5938{
5939 rettv->vval.v_number = win_getid(argvars);
5940}
5941
5942/*
5943 * "win_gotoid()" function
5944 */
5945 static void
5946f_win_gotoid(typval_T *argvars, typval_T *rettv)
5947{
5948 rettv->vval.v_number = win_gotoid(argvars);
5949}
5950
5951/*
5952 * "win_id2tabwin()" function
5953 */
5954 static void
5955f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5956{
5957 if (rettv_list_alloc(rettv) != FAIL)
5958 win_id2tabwin(argvars, rettv->vval.v_list);
5959}
5960
5961/*
5962 * "win_id2win()" function
5963 */
5964 static void
5965f_win_id2win(typval_T *argvars, typval_T *rettv)
5966{
5967 rettv->vval.v_number = win_id2win(argvars);
5968}
5969
5970/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005971 * "win_screenpos()" function
5972 */
5973 static void
5974f_win_screenpos(typval_T *argvars, typval_T *rettv)
5975{
5976 win_T *wp;
5977
5978 if (rettv_list_alloc(rettv) == FAIL)
5979 return;
5980
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005981 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005982 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5983 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5984}
5985
5986/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005987 * "getwinpos({timeout})" function
5988 */
5989 static void
5990f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5991{
5992 int x = -1;
5993 int y = -1;
5994
5995 if (rettv_list_alloc(rettv) == FAIL)
5996 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005997#if defined(FEAT_GUI) \
5998 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5999 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006000 {
6001 varnumber_T timeout = 100;
6002
6003 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006004 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006005
6006 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006007 }
6008#endif
6009 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6010 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6011}
6012
6013
6014/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006015 * "getwinposx()" function
6016 */
6017 static void
6018f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6019{
6020 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006021#if defined(FEAT_GUI) \
6022 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6023 || defined(MSWIN)
6024
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006025 {
6026 int x, y;
6027
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006028 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006029 rettv->vval.v_number = x;
6030 }
6031#endif
6032}
6033
6034/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006035 * "getwinposy()" function
6036 */
6037 static void
6038f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6039{
6040 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006041#if defined(FEAT_GUI) \
6042 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6043 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006044 {
6045 int x, y;
6046
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006047 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006048 rettv->vval.v_number = y;
6049 }
6050#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006051}
6052
6053/*
6054 * "getwinvar()" function
6055 */
6056 static void
6057f_getwinvar(typval_T *argvars, typval_T *rettv)
6058{
6059 getwinvar(argvars, rettv, 0);
6060}
6061
6062/*
6063 * "glob()" function
6064 */
6065 static void
6066f_glob(typval_T *argvars, typval_T *rettv)
6067{
6068 int options = WILD_SILENT|WILD_USE_NL;
6069 expand_T xpc;
6070 int error = FALSE;
6071
6072 /* When the optional second argument is non-zero, don't remove matches
6073 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6074 rettv->v_type = VAR_STRING;
6075 if (argvars[1].v_type != VAR_UNKNOWN)
6076 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006077 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006078 options |= WILD_KEEP_ALL;
6079 if (argvars[2].v_type != VAR_UNKNOWN)
6080 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006081 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006082 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006083 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006084 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006085 options |= WILD_ALLLINKS;
6086 }
6087 }
6088 if (!error)
6089 {
6090 ExpandInit(&xpc);
6091 xpc.xp_context = EXPAND_FILES;
6092 if (p_wic)
6093 options += WILD_ICASE;
6094 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006095 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006096 NULL, options, WILD_ALL);
6097 else if (rettv_list_alloc(rettv) != FAIL)
6098 {
6099 int i;
6100
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006101 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006102 NULL, options, WILD_ALL_KEEP);
6103 for (i = 0; i < xpc.xp_numfiles; i++)
6104 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6105
6106 ExpandCleanup(&xpc);
6107 }
6108 }
6109 else
6110 rettv->vval.v_string = NULL;
6111}
6112
6113/*
6114 * "globpath()" function
6115 */
6116 static void
6117f_globpath(typval_T *argvars, typval_T *rettv)
6118{
6119 int flags = 0;
6120 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006121 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006122 int error = FALSE;
6123 garray_T ga;
6124 int i;
6125
6126 /* When the optional second argument is non-zero, don't remove matches
6127 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6128 rettv->v_type = VAR_STRING;
6129 if (argvars[2].v_type != VAR_UNKNOWN)
6130 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006131 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006132 flags |= WILD_KEEP_ALL;
6133 if (argvars[3].v_type != VAR_UNKNOWN)
6134 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006135 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006136 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006137 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006138 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006139 flags |= WILD_ALLLINKS;
6140 }
6141 }
6142 if (file != NULL && !error)
6143 {
6144 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006145 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006146 if (rettv->v_type == VAR_STRING)
6147 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6148 else if (rettv_list_alloc(rettv) != FAIL)
6149 for (i = 0; i < ga.ga_len; ++i)
6150 list_append_string(rettv->vval.v_list,
6151 ((char_u **)(ga.ga_data))[i], -1);
6152 ga_clear_strings(&ga);
6153 }
6154 else
6155 rettv->vval.v_string = NULL;
6156}
6157
6158/*
6159 * "glob2regpat()" function
6160 */
6161 static void
6162f_glob2regpat(typval_T *argvars, typval_T *rettv)
6163{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006164 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006165
6166 rettv->v_type = VAR_STRING;
6167 rettv->vval.v_string = (pat == NULL)
6168 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6169}
6170
6171/* for VIM_VERSION_ defines */
6172#include "version.h"
6173
6174/*
6175 * "has()" function
6176 */
6177 static void
6178f_has(typval_T *argvars, typval_T *rettv)
6179{
6180 int i;
6181 char_u *name;
6182 int n = FALSE;
6183 static char *(has_list[]) =
6184 {
6185#ifdef AMIGA
6186 "amiga",
6187# ifdef FEAT_ARP
6188 "arp",
6189# endif
6190#endif
6191#ifdef __BEOS__
6192 "beos",
6193#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006194#if defined(BSD) && !defined(MACOS_X)
6195 "bsd",
6196#endif
6197#ifdef hpux
6198 "hpux",
6199#endif
6200#ifdef __linux__
6201 "linux",
6202#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006203#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006204 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6205 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006206# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006207 "macunix", /* Mac OS X, with the darwin feature */
6208 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006209# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006210#endif
6211#ifdef __QNX__
6212 "qnx",
6213#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006214#ifdef SUN_SYSTEM
6215 "sun",
6216#else
6217 "moon",
6218#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006219#ifdef UNIX
6220 "unix",
6221#endif
6222#ifdef VMS
6223 "vms",
6224#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006225#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006226 "win32",
6227#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006228#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006229 "win32unix",
6230#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006231#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006232 "win64",
6233#endif
6234#ifdef EBCDIC
6235 "ebcdic",
6236#endif
6237#ifndef CASE_INSENSITIVE_FILENAME
6238 "fname_case",
6239#endif
6240#ifdef HAVE_ACL
6241 "acl",
6242#endif
6243#ifdef FEAT_ARABIC
6244 "arabic",
6245#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006246 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006247#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006248 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006249#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006250#ifdef FEAT_AUTOSERVERNAME
6251 "autoservername",
6252#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006253#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006255# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006256 "balloon_multiline",
6257# endif
6258#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006259#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006260 "balloon_eval_term",
6261#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006262#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6263 "builtin_terms",
6264# ifdef ALL_BUILTIN_TCAPS
6265 "all_builtin_terms",
6266# endif
6267#endif
6268#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006269 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006270 || defined(FEAT_GUI_MOTIF))
6271 "browsefilter",
6272#endif
6273#ifdef FEAT_BYTEOFF
6274 "byte_offset",
6275#endif
6276#ifdef FEAT_JOB_CHANNEL
6277 "channel",
6278#endif
6279#ifdef FEAT_CINDENT
6280 "cindent",
6281#endif
6282#ifdef FEAT_CLIENTSERVER
6283 "clientserver",
6284#endif
6285#ifdef FEAT_CLIPBOARD
6286 "clipboard",
6287#endif
6288#ifdef FEAT_CMDL_COMPL
6289 "cmdline_compl",
6290#endif
6291#ifdef FEAT_CMDHIST
6292 "cmdline_hist",
6293#endif
6294#ifdef FEAT_COMMENTS
6295 "comments",
6296#endif
6297#ifdef FEAT_CONCEAL
6298 "conceal",
6299#endif
6300#ifdef FEAT_CRYPT
6301 "cryptv",
6302 "crypt-blowfish",
6303 "crypt-blowfish2",
6304#endif
6305#ifdef FEAT_CSCOPE
6306 "cscope",
6307#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006309#ifdef CURSOR_SHAPE
6310 "cursorshape",
6311#endif
6312#ifdef DEBUG
6313 "debug",
6314#endif
6315#ifdef FEAT_CON_DIALOG
6316 "dialog_con",
6317#endif
6318#ifdef FEAT_GUI_DIALOG
6319 "dialog_gui",
6320#endif
6321#ifdef FEAT_DIFF
6322 "diff",
6323#endif
6324#ifdef FEAT_DIGRAPHS
6325 "digraphs",
6326#endif
6327#ifdef FEAT_DIRECTX
6328 "directx",
6329#endif
6330#ifdef FEAT_DND
6331 "dnd",
6332#endif
6333#ifdef FEAT_EMACS_TAGS
6334 "emacs_tags",
6335#endif
6336 "eval", /* always present, of course! */
6337 "ex_extra", /* graduated feature */
6338#ifdef FEAT_SEARCH_EXTRA
6339 "extra_search",
6340#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006341#ifdef FEAT_SEARCHPATH
6342 "file_in_path",
6343#endif
6344#ifdef FEAT_FILTERPIPE
6345 "filterpipe",
6346#endif
6347#ifdef FEAT_FIND_ID
6348 "find_in_path",
6349#endif
6350#ifdef FEAT_FLOAT
6351 "float",
6352#endif
6353#ifdef FEAT_FOLDING
6354 "folding",
6355#endif
6356#ifdef FEAT_FOOTER
6357 "footer",
6358#endif
6359#if !defined(USE_SYSTEM) && defined(UNIX)
6360 "fork",
6361#endif
6362#ifdef FEAT_GETTEXT
6363 "gettext",
6364#endif
6365#ifdef FEAT_GUI
6366 "gui",
6367#endif
6368#ifdef FEAT_GUI_ATHENA
6369# ifdef FEAT_GUI_NEXTAW
6370 "gui_neXtaw",
6371# else
6372 "gui_athena",
6373# endif
6374#endif
6375#ifdef FEAT_GUI_GTK
6376 "gui_gtk",
6377# ifdef USE_GTK3
6378 "gui_gtk3",
6379# else
6380 "gui_gtk2",
6381# endif
6382#endif
6383#ifdef FEAT_GUI_GNOME
6384 "gui_gnome",
6385#endif
6386#ifdef FEAT_GUI_MAC
6387 "gui_mac",
6388#endif
6389#ifdef FEAT_GUI_MOTIF
6390 "gui_motif",
6391#endif
6392#ifdef FEAT_GUI_PHOTON
6393 "gui_photon",
6394#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006395#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006396 "gui_win32",
6397#endif
6398#ifdef FEAT_HANGULIN
6399 "hangul_input",
6400#endif
6401#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6402 "iconv",
6403#endif
6404#ifdef FEAT_INS_EXPAND
6405 "insert_expand",
6406#endif
6407#ifdef FEAT_JOB_CHANNEL
6408 "job",
6409#endif
6410#ifdef FEAT_JUMPLIST
6411 "jumplist",
6412#endif
6413#ifdef FEAT_KEYMAP
6414 "keymap",
6415#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006416 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006417#ifdef FEAT_LANGMAP
6418 "langmap",
6419#endif
6420#ifdef FEAT_LIBCALL
6421 "libcall",
6422#endif
6423#ifdef FEAT_LINEBREAK
6424 "linebreak",
6425#endif
6426#ifdef FEAT_LISP
6427 "lispindent",
6428#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006429 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006430#ifdef FEAT_LOCALMAP
6431 "localmap",
6432#endif
6433#ifdef FEAT_LUA
6434# ifndef DYNAMIC_LUA
6435 "lua",
6436# endif
6437#endif
6438#ifdef FEAT_MENU
6439 "menu",
6440#endif
6441#ifdef FEAT_SESSION
6442 "mksession",
6443#endif
6444#ifdef FEAT_MODIFY_FNAME
6445 "modify_fname",
6446#endif
6447#ifdef FEAT_MOUSE
6448 "mouse",
6449#endif
6450#ifdef FEAT_MOUSESHAPE
6451 "mouseshape",
6452#endif
6453#if defined(UNIX) || defined(VMS)
6454# ifdef FEAT_MOUSE_DEC
6455 "mouse_dec",
6456# endif
6457# ifdef FEAT_MOUSE_GPM
6458 "mouse_gpm",
6459# endif
6460# ifdef FEAT_MOUSE_JSB
6461 "mouse_jsbterm",
6462# endif
6463# ifdef FEAT_MOUSE_NET
6464 "mouse_netterm",
6465# endif
6466# ifdef FEAT_MOUSE_PTERM
6467 "mouse_pterm",
6468# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006469# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006470 "mouse_sgr",
6471# endif
6472# ifdef FEAT_SYSMOUSE
6473 "mouse_sysmouse",
6474# endif
6475# ifdef FEAT_MOUSE_URXVT
6476 "mouse_urxvt",
6477# endif
6478# ifdef FEAT_MOUSE_XTERM
6479 "mouse_xterm",
6480# endif
6481#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006482 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006483#ifdef FEAT_MBYTE_IME
6484 "multi_byte_ime",
6485#endif
6486#ifdef FEAT_MULTI_LANG
6487 "multi_lang",
6488#endif
6489#ifdef FEAT_MZSCHEME
6490#ifndef DYNAMIC_MZSCHEME
6491 "mzscheme",
6492#endif
6493#endif
6494#ifdef FEAT_NUM64
6495 "num64",
6496#endif
6497#ifdef FEAT_OLE
6498 "ole",
6499#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006500#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006501 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006502#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006503#ifdef FEAT_PATH_EXTRA
6504 "path_extra",
6505#endif
6506#ifdef FEAT_PERL
6507#ifndef DYNAMIC_PERL
6508 "perl",
6509#endif
6510#endif
6511#ifdef FEAT_PERSISTENT_UNDO
6512 "persistent_undo",
6513#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006514#if defined(FEAT_PYTHON)
6515 "python_compiled",
6516# if defined(DYNAMIC_PYTHON)
6517 "python_dynamic",
6518# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006519 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006520 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006521# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006522#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006523#if defined(FEAT_PYTHON3)
6524 "python3_compiled",
6525# if defined(DYNAMIC_PYTHON3)
6526 "python3_dynamic",
6527# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006528 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006529 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006530# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006531#endif
6532#ifdef FEAT_POSTSCRIPT
6533 "postscript",
6534#endif
6535#ifdef FEAT_PRINTER
6536 "printer",
6537#endif
6538#ifdef FEAT_PROFILE
6539 "profile",
6540#endif
6541#ifdef FEAT_RELTIME
6542 "reltime",
6543#endif
6544#ifdef FEAT_QUICKFIX
6545 "quickfix",
6546#endif
6547#ifdef FEAT_RIGHTLEFT
6548 "rightleft",
6549#endif
6550#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6551 "ruby",
6552#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006553 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006554#ifdef FEAT_CMDL_INFO
6555 "showcmd",
6556 "cmdline_info",
6557#endif
6558#ifdef FEAT_SIGNS
6559 "signs",
6560#endif
6561#ifdef FEAT_SMARTINDENT
6562 "smartindent",
6563#endif
6564#ifdef STARTUPTIME
6565 "startuptime",
6566#endif
6567#ifdef FEAT_STL_OPT
6568 "statusline",
6569#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006570#ifdef FEAT_NETBEANS_INTG
6571 "netbeans_intg",
6572#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006573#ifdef FEAT_SOUND
6574 "sound",
6575#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006576#ifdef FEAT_SPELL
6577 "spell",
6578#endif
6579#ifdef FEAT_SYN_HL
6580 "syntax",
6581#endif
6582#if defined(USE_SYSTEM) || !defined(UNIX)
6583 "system",
6584#endif
6585#ifdef FEAT_TAG_BINS
6586 "tag_binary",
6587#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006588#ifdef FEAT_TCL
6589# ifndef DYNAMIC_TCL
6590 "tcl",
6591# endif
6592#endif
6593#ifdef FEAT_TERMGUICOLORS
6594 "termguicolors",
6595#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006596#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006597 "terminal",
6598#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006599#ifdef TERMINFO
6600 "terminfo",
6601#endif
6602#ifdef FEAT_TERMRESPONSE
6603 "termresponse",
6604#endif
6605#ifdef FEAT_TEXTOBJ
6606 "textobjects",
6607#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006608#ifdef FEAT_TEXT_PROP
6609 "textprop",
6610#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006611#ifdef HAVE_TGETENT
6612 "tgetent",
6613#endif
6614#ifdef FEAT_TIMERS
6615 "timers",
6616#endif
6617#ifdef FEAT_TITLE
6618 "title",
6619#endif
6620#ifdef FEAT_TOOLBAR
6621 "toolbar",
6622#endif
6623#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6624 "unnamedplus",
6625#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006626 "user-commands", /* was accidentally included in 5.4 */
6627 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006628#ifdef FEAT_VARTABS
6629 "vartabs",
6630#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006631 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006632#ifdef FEAT_VIMINFO
6633 "viminfo",
6634#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006635 "vimscript-1",
6636 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006637 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006638 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006640 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006642#ifdef FEAT_VTP
6643 "vtp",
6644#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006645#ifdef FEAT_WILDIGN
6646 "wildignore",
6647#endif
6648#ifdef FEAT_WILDMENU
6649 "wildmenu",
6650#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006651 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006652#ifdef FEAT_WAK
6653 "winaltkeys",
6654#endif
6655#ifdef FEAT_WRITEBACKUP
6656 "writebackup",
6657#endif
6658#ifdef FEAT_XIM
6659 "xim",
6660#endif
6661#ifdef FEAT_XFONTSET
6662 "xfontset",
6663#endif
6664#ifdef FEAT_XPM_W32
6665 "xpm",
6666 "xpm_w32", /* for backward compatibility */
6667#else
6668# if defined(HAVE_XPM)
6669 "xpm",
6670# endif
6671#endif
6672#ifdef USE_XSMP
6673 "xsmp",
6674#endif
6675#ifdef USE_XSMP_INTERACT
6676 "xsmp_interact",
6677#endif
6678#ifdef FEAT_XCLIPBOARD
6679 "xterm_clipboard",
6680#endif
6681#ifdef FEAT_XTERM_SAVE
6682 "xterm_save",
6683#endif
6684#if defined(UNIX) && defined(FEAT_X11)
6685 "X11",
6686#endif
6687 NULL
6688 };
6689
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006690 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006691 for (i = 0; has_list[i] != NULL; ++i)
6692 if (STRICMP(name, has_list[i]) == 0)
6693 {
6694 n = TRUE;
6695 break;
6696 }
6697
6698 if (n == FALSE)
6699 {
6700 if (STRNICMP(name, "patch", 5) == 0)
6701 {
6702 if (name[5] == '-'
6703 && STRLEN(name) >= 11
6704 && vim_isdigit(name[6])
6705 && vim_isdigit(name[8])
6706 && vim_isdigit(name[10]))
6707 {
6708 int major = atoi((char *)name + 6);
6709 int minor = atoi((char *)name + 8);
6710
6711 /* Expect "patch-9.9.01234". */
6712 n = (major < VIM_VERSION_MAJOR
6713 || (major == VIM_VERSION_MAJOR
6714 && (minor < VIM_VERSION_MINOR
6715 || (minor == VIM_VERSION_MINOR
6716 && has_patch(atoi((char *)name + 10))))));
6717 }
6718 else
6719 n = has_patch(atoi((char *)name + 5));
6720 }
6721 else if (STRICMP(name, "vim_starting") == 0)
6722 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006723 else if (STRICMP(name, "ttyin") == 0)
6724 n = mch_input_isatty();
6725 else if (STRICMP(name, "ttyout") == 0)
6726 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006727 else if (STRICMP(name, "multi_byte_encoding") == 0)
6728 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006729#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006730 else if (STRICMP(name, "balloon_multiline") == 0)
6731 n = multiline_balloon_available();
6732#endif
6733#ifdef DYNAMIC_TCL
6734 else if (STRICMP(name, "tcl") == 0)
6735 n = tcl_enabled(FALSE);
6736#endif
6737#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6738 else if (STRICMP(name, "iconv") == 0)
6739 n = iconv_enabled(FALSE);
6740#endif
6741#ifdef DYNAMIC_LUA
6742 else if (STRICMP(name, "lua") == 0)
6743 n = lua_enabled(FALSE);
6744#endif
6745#ifdef DYNAMIC_MZSCHEME
6746 else if (STRICMP(name, "mzscheme") == 0)
6747 n = mzscheme_enabled(FALSE);
6748#endif
6749#ifdef DYNAMIC_RUBY
6750 else if (STRICMP(name, "ruby") == 0)
6751 n = ruby_enabled(FALSE);
6752#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006753#ifdef DYNAMIC_PYTHON
6754 else if (STRICMP(name, "python") == 0)
6755 n = python_enabled(FALSE);
6756#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006757#ifdef DYNAMIC_PYTHON3
6758 else if (STRICMP(name, "python3") == 0)
6759 n = python3_enabled(FALSE);
6760#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006761#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6762 else if (STRICMP(name, "pythonx") == 0)
6763 {
6764# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6765 if (p_pyx == 0)
6766 n = python3_enabled(FALSE) || python_enabled(FALSE);
6767 else if (p_pyx == 3)
6768 n = python3_enabled(FALSE);
6769 else if (p_pyx == 2)
6770 n = python_enabled(FALSE);
6771# elif defined(DYNAMIC_PYTHON)
6772 n = python_enabled(FALSE);
6773# elif defined(DYNAMIC_PYTHON3)
6774 n = python3_enabled(FALSE);
6775# endif
6776 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006777#endif
6778#ifdef DYNAMIC_PERL
6779 else if (STRICMP(name, "perl") == 0)
6780 n = perl_enabled(FALSE);
6781#endif
6782#ifdef FEAT_GUI
6783 else if (STRICMP(name, "gui_running") == 0)
6784 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006785# ifdef FEAT_BROWSE
6786 else if (STRICMP(name, "browse") == 0)
6787 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6788# endif
6789#endif
6790#ifdef FEAT_SYN_HL
6791 else if (STRICMP(name, "syntax_items") == 0)
6792 n = syntax_present(curwin);
6793#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006794#ifdef FEAT_VTP
6795 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006796 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006797#endif
6798#ifdef FEAT_NETBEANS_INTG
6799 else if (STRICMP(name, "netbeans_enabled") == 0)
6800 n = netbeans_active();
6801#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006802#ifdef FEAT_MOUSE_GPM
6803 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6804 n = gpm_enabled();
6805#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006806#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006807 else if (STRICMP(name, "terminal") == 0)
6808 n = terminal_enabled();
6809#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006810#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006811 else if (STRICMP(name, "conpty") == 0)
6812 n = use_conpty();
6813#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006814 }
6815
6816 rettv->vval.v_number = n;
6817}
6818
6819/*
6820 * "has_key()" function
6821 */
6822 static void
6823f_has_key(typval_T *argvars, typval_T *rettv)
6824{
6825 if (argvars[0].v_type != VAR_DICT)
6826 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006827 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006828 return;
6829 }
6830 if (argvars[0].vval.v_dict == NULL)
6831 return;
6832
6833 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006834 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006835}
6836
6837/*
6838 * "haslocaldir()" function
6839 */
6840 static void
6841f_haslocaldir(typval_T *argvars, typval_T *rettv)
6842{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006843 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006844 win_T *wp = NULL;
6845
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006846 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6847
6848 // Check for window-local and tab-local directories
6849 if (wp != NULL && wp->w_localdir != NULL)
6850 rettv->vval.v_number = 1;
6851 else if (tp != NULL && tp->tp_localdir != NULL)
6852 rettv->vval.v_number = 2;
6853 else
6854 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006855}
6856
6857/*
6858 * "hasmapto()" function
6859 */
6860 static void
6861f_hasmapto(typval_T *argvars, typval_T *rettv)
6862{
6863 char_u *name;
6864 char_u *mode;
6865 char_u buf[NUMBUFLEN];
6866 int abbr = FALSE;
6867
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006868 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006869 if (argvars[1].v_type == VAR_UNKNOWN)
6870 mode = (char_u *)"nvo";
6871 else
6872 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006873 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006874 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006875 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006876 }
6877
6878 if (map_to_exists(name, mode, abbr))
6879 rettv->vval.v_number = TRUE;
6880 else
6881 rettv->vval.v_number = FALSE;
6882}
6883
6884/*
6885 * "histadd()" function
6886 */
6887 static void
6888f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6889{
6890#ifdef FEAT_CMDHIST
6891 int histype;
6892 char_u *str;
6893 char_u buf[NUMBUFLEN];
6894#endif
6895
6896 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006897 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006898 return;
6899#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006900 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 histype = str != NULL ? get_histtype(str) : -1;
6902 if (histype >= 0)
6903 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006904 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006905 if (*str != NUL)
6906 {
6907 init_history();
6908 add_to_history(histype, str, FALSE, NUL);
6909 rettv->vval.v_number = TRUE;
6910 return;
6911 }
6912 }
6913#endif
6914}
6915
6916/*
6917 * "histdel()" function
6918 */
6919 static void
6920f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6921{
6922#ifdef FEAT_CMDHIST
6923 int n;
6924 char_u buf[NUMBUFLEN];
6925 char_u *str;
6926
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006927 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006928 if (str == NULL)
6929 n = 0;
6930 else if (argvars[1].v_type == VAR_UNKNOWN)
6931 /* only one argument: clear entire history */
6932 n = clr_history(get_histtype(str));
6933 else if (argvars[1].v_type == VAR_NUMBER)
6934 /* index given: remove that entry */
6935 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006936 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006937 else
6938 /* string given: remove all matching entries */
6939 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006940 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006941 rettv->vval.v_number = n;
6942#endif
6943}
6944
6945/*
6946 * "histget()" function
6947 */
6948 static void
6949f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6950{
6951#ifdef FEAT_CMDHIST
6952 int type;
6953 int idx;
6954 char_u *str;
6955
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006956 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006957 if (str == NULL)
6958 rettv->vval.v_string = NULL;
6959 else
6960 {
6961 type = get_histtype(str);
6962 if (argvars[1].v_type == VAR_UNKNOWN)
6963 idx = get_history_idx(type);
6964 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006965 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006966 /* -1 on type error */
6967 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6968 }
6969#else
6970 rettv->vval.v_string = NULL;
6971#endif
6972 rettv->v_type = VAR_STRING;
6973}
6974
6975/*
6976 * "histnr()" function
6977 */
6978 static void
6979f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6980{
6981 int i;
6982
6983#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006984 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006985
6986 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6987 if (i >= HIST_CMD && i < HIST_COUNT)
6988 i = get_history_idx(i);
6989 else
6990#endif
6991 i = -1;
6992 rettv->vval.v_number = i;
6993}
6994
6995/*
6996 * "highlightID(name)" function
6997 */
6998 static void
6999f_hlID(typval_T *argvars, typval_T *rettv)
7000{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007001 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007002}
7003
7004/*
7005 * "highlight_exists()" function
7006 */
7007 static void
7008f_hlexists(typval_T *argvars, typval_T *rettv)
7009{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007010 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007011}
7012
7013/*
7014 * "hostname()" function
7015 */
7016 static void
7017f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7018{
7019 char_u hostname[256];
7020
7021 mch_get_host_name(hostname, 256);
7022 rettv->v_type = VAR_STRING;
7023 rettv->vval.v_string = vim_strsave(hostname);
7024}
7025
7026/*
7027 * iconv() function
7028 */
7029 static void
7030f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7031{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007032 char_u buf1[NUMBUFLEN];
7033 char_u buf2[NUMBUFLEN];
7034 char_u *from, *to, *str;
7035 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036
7037 rettv->v_type = VAR_STRING;
7038 rettv->vval.v_string = NULL;
7039
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007040 str = tv_get_string(&argvars[0]);
7041 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7042 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007043 vimconv.vc_type = CONV_NONE;
7044 convert_setup(&vimconv, from, to);
7045
7046 /* If the encodings are equal, no conversion needed. */
7047 if (vimconv.vc_type == CONV_NONE)
7048 rettv->vval.v_string = vim_strsave(str);
7049 else
7050 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7051
7052 convert_setup(&vimconv, NULL, NULL);
7053 vim_free(from);
7054 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007055}
7056
7057/*
7058 * "indent()" function
7059 */
7060 static void
7061f_indent(typval_T *argvars, typval_T *rettv)
7062{
7063 linenr_T lnum;
7064
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007065 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007066 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7067 rettv->vval.v_number = get_indent_lnum(lnum);
7068 else
7069 rettv->vval.v_number = -1;
7070}
7071
7072/*
7073 * "index()" function
7074 */
7075 static void
7076f_index(typval_T *argvars, typval_T *rettv)
7077{
7078 list_T *l;
7079 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007080 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007081 long idx = 0;
7082 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007083 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007084
7085 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007086 if (argvars[0].v_type == VAR_BLOB)
7087 {
7088 typval_T tv;
7089 int start = 0;
7090
7091 if (argvars[2].v_type != VAR_UNKNOWN)
7092 {
7093 start = tv_get_number_chk(&argvars[2], &error);
7094 if (error)
7095 return;
7096 }
7097 b = argvars[0].vval.v_blob;
7098 if (b == NULL)
7099 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007100 if (start < 0)
7101 {
7102 start = blob_len(b) + start;
7103 if (start < 0)
7104 start = 0;
7105 }
7106
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007107 for (idx = start; idx < blob_len(b); ++idx)
7108 {
7109 tv.v_type = VAR_NUMBER;
7110 tv.vval.v_number = blob_get(b, idx);
7111 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7112 {
7113 rettv->vval.v_number = idx;
7114 return;
7115 }
7116 }
7117 return;
7118 }
7119 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007120 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007121 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007122 return;
7123 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007124
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007125 l = argvars[0].vval.v_list;
7126 if (l != NULL)
7127 {
7128 item = l->lv_first;
7129 if (argvars[2].v_type != VAR_UNKNOWN)
7130 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007131 /* Start at specified item. Use the cached index that list_find()
7132 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007133 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007134 idx = l->lv_idx;
7135 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007136 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007137 if (error)
7138 item = NULL;
7139 }
7140
7141 for ( ; item != NULL; item = item->li_next, ++idx)
7142 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7143 {
7144 rettv->vval.v_number = idx;
7145 break;
7146 }
7147 }
7148}
7149
7150static int inputsecret_flag = 0;
7151
7152/*
7153 * "input()" function
7154 * Also handles inputsecret() when inputsecret is set.
7155 */
7156 static void
7157f_input(typval_T *argvars, typval_T *rettv)
7158{
7159 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7160}
7161
7162/*
7163 * "inputdialog()" function
7164 */
7165 static void
7166f_inputdialog(typval_T *argvars, typval_T *rettv)
7167{
7168#if defined(FEAT_GUI_TEXTDIALOG)
7169 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7170 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7171 {
7172 char_u *message;
7173 char_u buf[NUMBUFLEN];
7174 char_u *defstr = (char_u *)"";
7175
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007176 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007177 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007178 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007179 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7180 else
7181 IObuff[0] = NUL;
7182 if (message != NULL && defstr != NULL
7183 && do_dialog(VIM_QUESTION, NULL, message,
7184 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7185 rettv->vval.v_string = vim_strsave(IObuff);
7186 else
7187 {
7188 if (message != NULL && defstr != NULL
7189 && argvars[1].v_type != VAR_UNKNOWN
7190 && argvars[2].v_type != VAR_UNKNOWN)
7191 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007192 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007193 else
7194 rettv->vval.v_string = NULL;
7195 }
7196 rettv->v_type = VAR_STRING;
7197 }
7198 else
7199#endif
7200 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7201}
7202
7203/*
7204 * "inputlist()" function
7205 */
7206 static void
7207f_inputlist(typval_T *argvars, typval_T *rettv)
7208{
7209 listitem_T *li;
7210 int selected;
7211 int mouse_used;
7212
7213#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007214 /* While starting up, there is no place to enter text. When running tests
7215 * with --not-a-term we assume feedkeys() will be used. */
7216 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007217 return;
7218#endif
7219 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7220 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007221 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007222 return;
7223 }
7224
7225 msg_start();
7226 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7227 lines_left = Rows; /* avoid more prompt */
7228 msg_scroll = TRUE;
7229 msg_clr_eos();
7230
7231 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7232 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007233 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007234 msg_putchar('\n');
7235 }
7236
7237 /* Ask for choice. */
7238 selected = prompt_for_number(&mouse_used);
7239 if (mouse_used)
7240 selected -= lines_left;
7241
7242 rettv->vval.v_number = selected;
7243}
7244
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7246
7247/*
7248 * "inputrestore()" function
7249 */
7250 static void
7251f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7252{
7253 if (ga_userinput.ga_len > 0)
7254 {
7255 --ga_userinput.ga_len;
7256 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7257 + ga_userinput.ga_len);
7258 /* default return is zero == OK */
7259 }
7260 else if (p_verbose > 1)
7261 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007262 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007263 rettv->vval.v_number = 1; /* Failed */
7264 }
7265}
7266
7267/*
7268 * "inputsave()" function
7269 */
7270 static void
7271f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7272{
7273 /* Add an entry to the stack of typeahead storage. */
7274 if (ga_grow(&ga_userinput, 1) == OK)
7275 {
7276 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7277 + ga_userinput.ga_len);
7278 ++ga_userinput.ga_len;
7279 /* default return is zero == OK */
7280 }
7281 else
7282 rettv->vval.v_number = 1; /* Failed */
7283}
7284
7285/*
7286 * "inputsecret()" function
7287 */
7288 static void
7289f_inputsecret(typval_T *argvars, typval_T *rettv)
7290{
7291 ++cmdline_star;
7292 ++inputsecret_flag;
7293 f_input(argvars, rettv);
7294 --cmdline_star;
7295 --inputsecret_flag;
7296}
7297
7298/*
7299 * "insert()" function
7300 */
7301 static void
7302f_insert(typval_T *argvars, typval_T *rettv)
7303{
7304 long before = 0;
7305 listitem_T *item;
7306 list_T *l;
7307 int error = FALSE;
7308
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007309 if (argvars[0].v_type == VAR_BLOB)
7310 {
7311 int val, len;
7312 char_u *p;
7313
7314 len = blob_len(argvars[0].vval.v_blob);
7315 if (argvars[2].v_type != VAR_UNKNOWN)
7316 {
7317 before = (long)tv_get_number_chk(&argvars[2], &error);
7318 if (error)
7319 return; // type error; errmsg already given
7320 if (before < 0 || before > len)
7321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007322 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007323 return;
7324 }
7325 }
7326 val = tv_get_number_chk(&argvars[1], &error);
7327 if (error)
7328 return;
7329 if (val < 0 || val > 255)
7330 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007331 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007332 return;
7333 }
7334
7335 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7336 return;
7337 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7338 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7339 *(p + before) = val;
7340 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7341
7342 copy_tv(&argvars[0], rettv);
7343 }
7344 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007345 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007346 else if ((l = argvars[0].vval.v_list) != NULL
7347 && !var_check_lock(l->lv_lock,
7348 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007349 {
7350 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007351 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007352 if (error)
7353 return; /* type error; errmsg already given */
7354
7355 if (before == l->lv_len)
7356 item = NULL;
7357 else
7358 {
7359 item = list_find(l, before);
7360 if (item == NULL)
7361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007362 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007363 l = NULL;
7364 }
7365 }
7366 if (l != NULL)
7367 {
7368 list_insert_tv(l, &argvars[1], item);
7369 copy_tv(&argvars[0], rettv);
7370 }
7371 }
7372}
7373
7374/*
7375 * "invert(expr)" function
7376 */
7377 static void
7378f_invert(typval_T *argvars, typval_T *rettv)
7379{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007380 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007381}
7382
7383/*
7384 * "isdirectory()" function
7385 */
7386 static void
7387f_isdirectory(typval_T *argvars, typval_T *rettv)
7388{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007389 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007390}
7391
7392/*
7393 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7394 * or it refers to a List or Dictionary that is locked.
7395 */
7396 static int
7397tv_islocked(typval_T *tv)
7398{
7399 return (tv->v_lock & VAR_LOCKED)
7400 || (tv->v_type == VAR_LIST
7401 && tv->vval.v_list != NULL
7402 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7403 || (tv->v_type == VAR_DICT
7404 && tv->vval.v_dict != NULL
7405 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7406}
7407
7408/*
7409 * "islocked()" function
7410 */
7411 static void
7412f_islocked(typval_T *argvars, typval_T *rettv)
7413{
7414 lval_T lv;
7415 char_u *end;
7416 dictitem_T *di;
7417
7418 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007419 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007420 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007421 if (end != NULL && lv.ll_name != NULL)
7422 {
7423 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007424 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007425 else
7426 {
7427 if (lv.ll_tv == NULL)
7428 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007429 di = find_var(lv.ll_name, NULL, TRUE);
7430 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007431 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007432 /* Consider a variable locked when:
7433 * 1. the variable itself is locked
7434 * 2. the value of the variable is locked.
7435 * 3. the List or Dict value is locked.
7436 */
7437 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7438 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007439 }
7440 }
7441 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007442 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007443 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007444 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007445 else if (lv.ll_list != NULL)
7446 /* List item. */
7447 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7448 else
7449 /* Dictionary item. */
7450 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7451 }
7452 }
7453
7454 clear_lval(&lv);
7455}
7456
7457#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7458/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007459 * "isinf()" function
7460 */
7461 static void
7462f_isinf(typval_T *argvars, typval_T *rettv)
7463{
7464 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7465 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7466}
7467
7468/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007469 * "isnan()" function
7470 */
7471 static void
7472f_isnan(typval_T *argvars, typval_T *rettv)
7473{
7474 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7475 && isnan(argvars[0].vval.v_float);
7476}
7477#endif
7478
7479/*
7480 * "items(dict)" function
7481 */
7482 static void
7483f_items(typval_T *argvars, typval_T *rettv)
7484{
7485 dict_list(argvars, rettv, 2);
7486}
7487
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007488/*
7489 * "join()" function
7490 */
7491 static void
7492f_join(typval_T *argvars, typval_T *rettv)
7493{
7494 garray_T ga;
7495 char_u *sep;
7496
7497 if (argvars[0].v_type != VAR_LIST)
7498 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007499 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007500 return;
7501 }
7502 if (argvars[0].vval.v_list == NULL)
7503 return;
7504 if (argvars[1].v_type == VAR_UNKNOWN)
7505 sep = (char_u *)" ";
7506 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007507 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007508
7509 rettv->v_type = VAR_STRING;
7510
7511 if (sep != NULL)
7512 {
7513 ga_init2(&ga, (int)sizeof(char), 80);
7514 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7515 ga_append(&ga, NUL);
7516 rettv->vval.v_string = (char_u *)ga.ga_data;
7517 }
7518 else
7519 rettv->vval.v_string = NULL;
7520}
7521
7522/*
7523 * "js_decode()" function
7524 */
7525 static void
7526f_js_decode(typval_T *argvars, typval_T *rettv)
7527{
7528 js_read_T reader;
7529
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007530 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007531 reader.js_fill = NULL;
7532 reader.js_used = 0;
7533 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007534 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007535}
7536
7537/*
7538 * "js_encode()" function
7539 */
7540 static void
7541f_js_encode(typval_T *argvars, typval_T *rettv)
7542{
7543 rettv->v_type = VAR_STRING;
7544 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7545}
7546
7547/*
7548 * "json_decode()" function
7549 */
7550 static void
7551f_json_decode(typval_T *argvars, typval_T *rettv)
7552{
7553 js_read_T reader;
7554
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007555 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007556 reader.js_fill = NULL;
7557 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007558 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007559}
7560
7561/*
7562 * "json_encode()" function
7563 */
7564 static void
7565f_json_encode(typval_T *argvars, typval_T *rettv)
7566{
7567 rettv->v_type = VAR_STRING;
7568 rettv->vval.v_string = json_encode(&argvars[0], 0);
7569}
7570
7571/*
7572 * "keys()" function
7573 */
7574 static void
7575f_keys(typval_T *argvars, typval_T *rettv)
7576{
7577 dict_list(argvars, rettv, 0);
7578}
7579
7580/*
7581 * "last_buffer_nr()" function.
7582 */
7583 static void
7584f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7585{
7586 int n = 0;
7587 buf_T *buf;
7588
Bram Moolenaar29323592016-07-24 22:04:11 +02007589 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007590 if (n < buf->b_fnum)
7591 n = buf->b_fnum;
7592
7593 rettv->vval.v_number = n;
7594}
7595
7596/*
7597 * "len()" function
7598 */
7599 static void
7600f_len(typval_T *argvars, typval_T *rettv)
7601{
7602 switch (argvars[0].v_type)
7603 {
7604 case VAR_STRING:
7605 case VAR_NUMBER:
7606 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007607 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007608 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007609 case VAR_BLOB:
7610 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7611 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007612 case VAR_LIST:
7613 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7614 break;
7615 case VAR_DICT:
7616 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7617 break;
7618 case VAR_UNKNOWN:
7619 case VAR_SPECIAL:
7620 case VAR_FLOAT:
7621 case VAR_FUNC:
7622 case VAR_PARTIAL:
7623 case VAR_JOB:
7624 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007625 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007626 break;
7627 }
7628}
7629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007630 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007631libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007632{
7633#ifdef FEAT_LIBCALL
7634 char_u *string_in;
7635 char_u **string_result;
7636 int nr_result;
7637#endif
7638
7639 rettv->v_type = type;
7640 if (type != VAR_NUMBER)
7641 rettv->vval.v_string = NULL;
7642
7643 if (check_restricted() || check_secure())
7644 return;
7645
7646#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007647 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007648 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7649 {
7650 string_in = NULL;
7651 if (argvars[2].v_type == VAR_STRING)
7652 string_in = argvars[2].vval.v_string;
7653 if (type == VAR_NUMBER)
7654 string_result = NULL;
7655 else
7656 string_result = &rettv->vval.v_string;
7657 if (mch_libcall(argvars[0].vval.v_string,
7658 argvars[1].vval.v_string,
7659 string_in,
7660 argvars[2].vval.v_number,
7661 string_result,
7662 &nr_result) == OK
7663 && type == VAR_NUMBER)
7664 rettv->vval.v_number = nr_result;
7665 }
7666#endif
7667}
7668
7669/*
7670 * "libcall()" function
7671 */
7672 static void
7673f_libcall(typval_T *argvars, typval_T *rettv)
7674{
7675 libcall_common(argvars, rettv, VAR_STRING);
7676}
7677
7678/*
7679 * "libcallnr()" function
7680 */
7681 static void
7682f_libcallnr(typval_T *argvars, typval_T *rettv)
7683{
7684 libcall_common(argvars, rettv, VAR_NUMBER);
7685}
7686
7687/*
7688 * "line(string)" function
7689 */
7690 static void
7691f_line(typval_T *argvars, typval_T *rettv)
7692{
7693 linenr_T lnum = 0;
7694 pos_T *fp;
7695 int fnum;
7696
7697 fp = var2fpos(&argvars[0], TRUE, &fnum);
7698 if (fp != NULL)
7699 lnum = fp->lnum;
7700 rettv->vval.v_number = lnum;
7701}
7702
7703/*
7704 * "line2byte(lnum)" function
7705 */
7706 static void
7707f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7708{
7709#ifndef FEAT_BYTEOFF
7710 rettv->vval.v_number = -1;
7711#else
7712 linenr_T lnum;
7713
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007714 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007715 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7716 rettv->vval.v_number = -1;
7717 else
7718 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7719 if (rettv->vval.v_number >= 0)
7720 ++rettv->vval.v_number;
7721#endif
7722}
7723
7724/*
7725 * "lispindent(lnum)" function
7726 */
7727 static void
7728f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7729{
7730#ifdef FEAT_LISP
7731 pos_T pos;
7732 linenr_T lnum;
7733
7734 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007735 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007736 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7737 {
7738 curwin->w_cursor.lnum = lnum;
7739 rettv->vval.v_number = get_lisp_indent();
7740 curwin->w_cursor = pos;
7741 }
7742 else
7743#endif
7744 rettv->vval.v_number = -1;
7745}
7746
7747/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007748 * "list2str()" function
7749 */
7750 static void
7751f_list2str(typval_T *argvars, typval_T *rettv)
7752{
7753 list_T *l;
7754 listitem_T *li;
7755 garray_T ga;
7756 int utf8 = FALSE;
7757
7758 rettv->v_type = VAR_STRING;
7759 rettv->vval.v_string = NULL;
7760 if (argvars[0].v_type != VAR_LIST)
7761 {
7762 emsg(_(e_invarg));
7763 return;
7764 }
7765
7766 l = argvars[0].vval.v_list;
7767 if (l == NULL)
7768 return; // empty list results in empty string
7769
7770 if (argvars[1].v_type != VAR_UNKNOWN)
7771 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7772
7773 ga_init2(&ga, 1, 80);
7774 if (has_mbyte || utf8)
7775 {
7776 char_u buf[MB_MAXBYTES + 1];
7777 int (*char2bytes)(int, char_u *);
7778
7779 if (utf8 || enc_utf8)
7780 char2bytes = utf_char2bytes;
7781 else
7782 char2bytes = mb_char2bytes;
7783
7784 for (li = l->lv_first; li != NULL; li = li->li_next)
7785 {
7786 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7787 ga_concat(&ga, buf);
7788 }
7789 ga_append(&ga, NUL);
7790 }
7791 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7792 {
7793 for (li = l->lv_first; li != NULL; li = li->li_next)
7794 ga_append(&ga, tv_get_number(&li->li_tv));
7795 ga_append(&ga, NUL);
7796 }
7797
7798 rettv->v_type = VAR_STRING;
7799 rettv->vval.v_string = ga.ga_data;
7800}
7801
7802/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007803 * "localtime()" function
7804 */
7805 static void
7806f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7807{
7808 rettv->vval.v_number = (varnumber_T)time(NULL);
7809}
7810
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007811 static void
7812get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7813{
7814 char_u *keys;
7815 char_u *which;
7816 char_u buf[NUMBUFLEN];
7817 char_u *keys_buf = NULL;
7818 char_u *rhs;
7819 int mode;
7820 int abbr = FALSE;
7821 int get_dict = FALSE;
7822 mapblock_T *mp;
7823 int buffer_local;
7824
7825 /* return empty string for failure */
7826 rettv->v_type = VAR_STRING;
7827 rettv->vval.v_string = NULL;
7828
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007829 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007830 if (*keys == NUL)
7831 return;
7832
7833 if (argvars[1].v_type != VAR_UNKNOWN)
7834 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007835 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007836 if (argvars[2].v_type != VAR_UNKNOWN)
7837 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007838 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007839 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007840 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007841 }
7842 }
7843 else
7844 which = (char_u *)"";
7845 if (which == NULL)
7846 return;
7847
7848 mode = get_map_mode(&which, 0);
7849
7850 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7851 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7852 vim_free(keys_buf);
7853
7854 if (!get_dict)
7855 {
7856 /* Return a string. */
7857 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007858 {
7859 if (*rhs == NUL)
7860 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7861 else
7862 rettv->vval.v_string = str2special_save(rhs, FALSE);
7863 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007864
7865 }
7866 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7867 {
7868 /* Return a dictionary. */
7869 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7870 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7871 dict_T *dict = rettv->vval.v_dict;
7872
Bram Moolenaare0be1672018-07-08 16:50:37 +02007873 dict_add_string(dict, "lhs", lhs);
7874 dict_add_string(dict, "rhs", mp->m_orig_str);
7875 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7876 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7877 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007878 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7879 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007880 dict_add_number(dict, "buffer", (long)buffer_local);
7881 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7882 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007883
7884 vim_free(lhs);
7885 vim_free(mapmode);
7886 }
7887}
7888
7889#ifdef FEAT_FLOAT
7890/*
7891 * "log()" function
7892 */
7893 static void
7894f_log(typval_T *argvars, typval_T *rettv)
7895{
7896 float_T f = 0.0;
7897
7898 rettv->v_type = VAR_FLOAT;
7899 if (get_float_arg(argvars, &f) == OK)
7900 rettv->vval.v_float = log(f);
7901 else
7902 rettv->vval.v_float = 0.0;
7903}
7904
7905/*
7906 * "log10()" function
7907 */
7908 static void
7909f_log10(typval_T *argvars, typval_T *rettv)
7910{
7911 float_T f = 0.0;
7912
7913 rettv->v_type = VAR_FLOAT;
7914 if (get_float_arg(argvars, &f) == OK)
7915 rettv->vval.v_float = log10(f);
7916 else
7917 rettv->vval.v_float = 0.0;
7918}
7919#endif
7920
7921#ifdef FEAT_LUA
7922/*
7923 * "luaeval()" function
7924 */
7925 static void
7926f_luaeval(typval_T *argvars, typval_T *rettv)
7927{
7928 char_u *str;
7929 char_u buf[NUMBUFLEN];
7930
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007931 if (check_restricted() || check_secure())
7932 return;
7933
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007934 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007935 do_luaeval(str, argvars + 1, rettv);
7936}
7937#endif
7938
7939/*
7940 * "map()" function
7941 */
7942 static void
7943f_map(typval_T *argvars, typval_T *rettv)
7944{
7945 filter_map(argvars, rettv, TRUE);
7946}
7947
7948/*
7949 * "maparg()" function
7950 */
7951 static void
7952f_maparg(typval_T *argvars, typval_T *rettv)
7953{
7954 get_maparg(argvars, rettv, TRUE);
7955}
7956
7957/*
7958 * "mapcheck()" function
7959 */
7960 static void
7961f_mapcheck(typval_T *argvars, typval_T *rettv)
7962{
7963 get_maparg(argvars, rettv, FALSE);
7964}
7965
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007966typedef enum
7967{
7968 MATCH_END, /* matchend() */
7969 MATCH_MATCH, /* match() */
7970 MATCH_STR, /* matchstr() */
7971 MATCH_LIST, /* matchlist() */
7972 MATCH_POS /* matchstrpos() */
7973} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007974
7975 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007976find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007977{
7978 char_u *str = NULL;
7979 long len = 0;
7980 char_u *expr = NULL;
7981 char_u *pat;
7982 regmatch_T regmatch;
7983 char_u patbuf[NUMBUFLEN];
7984 char_u strbuf[NUMBUFLEN];
7985 char_u *save_cpo;
7986 long start = 0;
7987 long nth = 1;
7988 colnr_T startcol = 0;
7989 int match = 0;
7990 list_T *l = NULL;
7991 listitem_T *li = NULL;
7992 long idx = 0;
7993 char_u *tofree = NULL;
7994
7995 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7996 save_cpo = p_cpo;
7997 p_cpo = (char_u *)"";
7998
7999 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008000 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008002 /* type MATCH_LIST: return empty list when there are no matches.
8003 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008004 if (rettv_list_alloc(rettv) == FAIL)
8005 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008006 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008007 && (list_append_string(rettv->vval.v_list,
8008 (char_u *)"", 0) == FAIL
8009 || list_append_number(rettv->vval.v_list,
8010 (varnumber_T)-1) == FAIL
8011 || list_append_number(rettv->vval.v_list,
8012 (varnumber_T)-1) == FAIL
8013 || list_append_number(rettv->vval.v_list,
8014 (varnumber_T)-1) == FAIL))
8015 {
8016 list_free(rettv->vval.v_list);
8017 rettv->vval.v_list = NULL;
8018 goto theend;
8019 }
8020 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008021 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022 {
8023 rettv->v_type = VAR_STRING;
8024 rettv->vval.v_string = NULL;
8025 }
8026
8027 if (argvars[0].v_type == VAR_LIST)
8028 {
8029 if ((l = argvars[0].vval.v_list) == NULL)
8030 goto theend;
8031 li = l->lv_first;
8032 }
8033 else
8034 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008035 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 len = (long)STRLEN(str);
8037 }
8038
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008039 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008040 if (pat == NULL)
8041 goto theend;
8042
8043 if (argvars[2].v_type != VAR_UNKNOWN)
8044 {
8045 int error = FALSE;
8046
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008047 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008048 if (error)
8049 goto theend;
8050 if (l != NULL)
8051 {
8052 li = list_find(l, start);
8053 if (li == NULL)
8054 goto theend;
8055 idx = l->lv_idx; /* use the cached index */
8056 }
8057 else
8058 {
8059 if (start < 0)
8060 start = 0;
8061 if (start > len)
8062 goto theend;
8063 /* When "count" argument is there ignore matches before "start",
8064 * otherwise skip part of the string. Differs when pattern is "^"
8065 * or "\<". */
8066 if (argvars[3].v_type != VAR_UNKNOWN)
8067 startcol = start;
8068 else
8069 {
8070 str += start;
8071 len -= start;
8072 }
8073 }
8074
8075 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008076 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008077 if (error)
8078 goto theend;
8079 }
8080
8081 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8082 if (regmatch.regprog != NULL)
8083 {
8084 regmatch.rm_ic = p_ic;
8085
8086 for (;;)
8087 {
8088 if (l != NULL)
8089 {
8090 if (li == NULL)
8091 {
8092 match = FALSE;
8093 break;
8094 }
8095 vim_free(tofree);
8096 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8097 if (str == NULL)
8098 break;
8099 }
8100
8101 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8102
8103 if (match && --nth <= 0)
8104 break;
8105 if (l == NULL && !match)
8106 break;
8107
8108 /* Advance to just after the match. */
8109 if (l != NULL)
8110 {
8111 li = li->li_next;
8112 ++idx;
8113 }
8114 else
8115 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008116 startcol = (colnr_T)(regmatch.startp[0]
8117 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 if (startcol > (colnr_T)len
8119 || str + startcol <= regmatch.startp[0])
8120 {
8121 match = FALSE;
8122 break;
8123 }
8124 }
8125 }
8126
8127 if (match)
8128 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008129 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130 {
8131 listitem_T *li1 = rettv->vval.v_list->lv_first;
8132 listitem_T *li2 = li1->li_next;
8133 listitem_T *li3 = li2->li_next;
8134 listitem_T *li4 = li3->li_next;
8135
8136 vim_free(li1->li_tv.vval.v_string);
8137 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8138 (int)(regmatch.endp[0] - regmatch.startp[0]));
8139 li3->li_tv.vval.v_number =
8140 (varnumber_T)(regmatch.startp[0] - expr);
8141 li4->li_tv.vval.v_number =
8142 (varnumber_T)(regmatch.endp[0] - expr);
8143 if (l != NULL)
8144 li2->li_tv.vval.v_number = (varnumber_T)idx;
8145 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008146 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008147 {
8148 int i;
8149
8150 /* return list with matched string and submatches */
8151 for (i = 0; i < NSUBEXP; ++i)
8152 {
8153 if (regmatch.endp[i] == NULL)
8154 {
8155 if (list_append_string(rettv->vval.v_list,
8156 (char_u *)"", 0) == FAIL)
8157 break;
8158 }
8159 else if (list_append_string(rettv->vval.v_list,
8160 regmatch.startp[i],
8161 (int)(regmatch.endp[i] - regmatch.startp[i]))
8162 == FAIL)
8163 break;
8164 }
8165 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008166 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167 {
8168 /* return matched string */
8169 if (l != NULL)
8170 copy_tv(&li->li_tv, rettv);
8171 else
8172 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8173 (int)(regmatch.endp[0] - regmatch.startp[0]));
8174 }
8175 else if (l != NULL)
8176 rettv->vval.v_number = idx;
8177 else
8178 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008179 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008180 rettv->vval.v_number =
8181 (varnumber_T)(regmatch.startp[0] - str);
8182 else
8183 rettv->vval.v_number =
8184 (varnumber_T)(regmatch.endp[0] - str);
8185 rettv->vval.v_number += (varnumber_T)(str - expr);
8186 }
8187 }
8188 vim_regfree(regmatch.regprog);
8189 }
8190
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008191theend:
8192 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008193 /* matchstrpos() without a list: drop the second item. */
8194 listitem_remove(rettv->vval.v_list,
8195 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008196 vim_free(tofree);
8197 p_cpo = save_cpo;
8198}
8199
8200/*
8201 * "match()" function
8202 */
8203 static void
8204f_match(typval_T *argvars, typval_T *rettv)
8205{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008206 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207}
8208
Bram Moolenaar95e51472018-07-28 16:55:56 +02008209#ifdef FEAT_SEARCH_EXTRA
8210 static int
8211matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8212{
8213 dictitem_T *di;
8214
8215 if (tv->v_type != VAR_DICT)
8216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008217 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008218 return FAIL;
8219 }
8220
8221 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008222 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008223 (char_u *)"conceal", FALSE);
8224
8225 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8226 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008227 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008228 if (*win == NULL)
8229 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008230 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008231 return FAIL;
8232 }
8233 }
8234
8235 return OK;
8236}
8237#endif
8238
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008239/*
8240 * "matchadd()" function
8241 */
8242 static void
8243f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8244{
8245#ifdef FEAT_SEARCH_EXTRA
8246 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008247 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8248 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008249 int prio = 10; /* default priority */
8250 int id = -1;
8251 int error = FALSE;
8252 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008253 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008254
8255 rettv->vval.v_number = -1;
8256
8257 if (grp == NULL || pat == NULL)
8258 return;
8259 if (argvars[2].v_type != VAR_UNKNOWN)
8260 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008261 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008262 if (argvars[3].v_type != VAR_UNKNOWN)
8263 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008264 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008265 if (argvars[4].v_type != VAR_UNKNOWN
8266 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8267 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008268 }
8269 }
8270 if (error == TRUE)
8271 return;
8272 if (id >= 1 && id <= 3)
8273 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008274 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008275 return;
8276 }
8277
Bram Moolenaar95e51472018-07-28 16:55:56 +02008278 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008279 conceal_char);
8280#endif
8281}
8282
8283/*
8284 * "matchaddpos()" function
8285 */
8286 static void
8287f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8288{
8289#ifdef FEAT_SEARCH_EXTRA
8290 char_u buf[NUMBUFLEN];
8291 char_u *group;
8292 int prio = 10;
8293 int id = -1;
8294 int error = FALSE;
8295 list_T *l;
8296 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008297 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008298
8299 rettv->vval.v_number = -1;
8300
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008301 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008302 if (group == NULL)
8303 return;
8304
8305 if (argvars[1].v_type != VAR_LIST)
8306 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008307 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308 return;
8309 }
8310 l = argvars[1].vval.v_list;
8311 if (l == NULL)
8312 return;
8313
8314 if (argvars[2].v_type != VAR_UNKNOWN)
8315 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008316 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008317 if (argvars[3].v_type != VAR_UNKNOWN)
8318 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008319 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008320
8321 if (argvars[4].v_type != VAR_UNKNOWN
8322 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8323 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008324 }
8325 }
8326 if (error == TRUE)
8327 return;
8328
8329 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8330 if (id == 1 || id == 2)
8331 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008332 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008333 return;
8334 }
8335
Bram Moolenaar95e51472018-07-28 16:55:56 +02008336 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337 conceal_char);
8338#endif
8339}
8340
8341/*
8342 * "matcharg()" function
8343 */
8344 static void
8345f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8346{
8347 if (rettv_list_alloc(rettv) == OK)
8348 {
8349#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008350 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008351 matchitem_T *m;
8352
8353 if (id >= 1 && id <= 3)
8354 {
8355 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8356 {
8357 list_append_string(rettv->vval.v_list,
8358 syn_id2name(m->hlg_id), -1);
8359 list_append_string(rettv->vval.v_list, m->pattern, -1);
8360 }
8361 else
8362 {
8363 list_append_string(rettv->vval.v_list, NULL, -1);
8364 list_append_string(rettv->vval.v_list, NULL, -1);
8365 }
8366 }
8367#endif
8368 }
8369}
8370
8371/*
8372 * "matchdelete()" function
8373 */
8374 static void
8375f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8376{
8377#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008378 win_T *win = get_optional_window(argvars, 1);
8379
8380 if (win == NULL)
8381 rettv->vval.v_number = -1;
8382 else
8383 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008384 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008385#endif
8386}
8387
8388/*
8389 * "matchend()" function
8390 */
8391 static void
8392f_matchend(typval_T *argvars, typval_T *rettv)
8393{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008394 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008395}
8396
8397/*
8398 * "matchlist()" function
8399 */
8400 static void
8401f_matchlist(typval_T *argvars, typval_T *rettv)
8402{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008403 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404}
8405
8406/*
8407 * "matchstr()" function
8408 */
8409 static void
8410f_matchstr(typval_T *argvars, typval_T *rettv)
8411{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008412 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413}
8414
8415/*
8416 * "matchstrpos()" function
8417 */
8418 static void
8419f_matchstrpos(typval_T *argvars, typval_T *rettv)
8420{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008421 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008422}
8423
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008424 static void
8425max_min(typval_T *argvars, typval_T *rettv, int domax)
8426{
8427 varnumber_T n = 0;
8428 varnumber_T i;
8429 int error = FALSE;
8430
8431 if (argvars[0].v_type == VAR_LIST)
8432 {
8433 list_T *l;
8434 listitem_T *li;
8435
8436 l = argvars[0].vval.v_list;
8437 if (l != NULL)
8438 {
8439 li = l->lv_first;
8440 if (li != NULL)
8441 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008442 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008443 for (;;)
8444 {
8445 li = li->li_next;
8446 if (li == NULL)
8447 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008448 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008449 if (domax ? i > n : i < n)
8450 n = i;
8451 }
8452 }
8453 }
8454 }
8455 else if (argvars[0].v_type == VAR_DICT)
8456 {
8457 dict_T *d;
8458 int first = TRUE;
8459 hashitem_T *hi;
8460 int todo;
8461
8462 d = argvars[0].vval.v_dict;
8463 if (d != NULL)
8464 {
8465 todo = (int)d->dv_hashtab.ht_used;
8466 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8467 {
8468 if (!HASHITEM_EMPTY(hi))
8469 {
8470 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008471 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008472 if (first)
8473 {
8474 n = i;
8475 first = FALSE;
8476 }
8477 else if (domax ? i > n : i < n)
8478 n = i;
8479 }
8480 }
8481 }
8482 }
8483 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008484 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008485 rettv->vval.v_number = error ? 0 : n;
8486}
8487
8488/*
8489 * "max()" function
8490 */
8491 static void
8492f_max(typval_T *argvars, typval_T *rettv)
8493{
8494 max_min(argvars, rettv, TRUE);
8495}
8496
8497/*
8498 * "min()" function
8499 */
8500 static void
8501f_min(typval_T *argvars, typval_T *rettv)
8502{
8503 max_min(argvars, rettv, FALSE);
8504}
8505
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008506/*
8507 * Create the directory in which "dir" is located, and higher levels when
8508 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008509 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008510 */
8511 static int
8512mkdir_recurse(char_u *dir, int prot)
8513{
8514 char_u *p;
8515 char_u *updir;
8516 int r = FAIL;
8517
8518 /* Get end of directory name in "dir".
8519 * We're done when it's "/" or "c:/". */
8520 p = gettail_sep(dir);
8521 if (p <= get_past_head(dir))
8522 return OK;
8523
8524 /* If the directory exists we're done. Otherwise: create it.*/
8525 updir = vim_strnsave(dir, (int)(p - dir));
8526 if (updir == NULL)
8527 return FAIL;
8528 if (mch_isdir(updir))
8529 r = OK;
8530 else if (mkdir_recurse(updir, prot) == OK)
8531 r = vim_mkdir_emsg(updir, prot);
8532 vim_free(updir);
8533 return r;
8534}
8535
8536#ifdef vim_mkdir
8537/*
8538 * "mkdir()" function
8539 */
8540 static void
8541f_mkdir(typval_T *argvars, typval_T *rettv)
8542{
8543 char_u *dir;
8544 char_u buf[NUMBUFLEN];
8545 int prot = 0755;
8546
8547 rettv->vval.v_number = FAIL;
8548 if (check_restricted() || check_secure())
8549 return;
8550
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008551 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008552 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008553 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008554
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008555 if (*gettail(dir) == NUL)
8556 /* remove trailing slashes */
8557 *gettail_sep(dir) = NUL;
8558
8559 if (argvars[1].v_type != VAR_UNKNOWN)
8560 {
8561 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008563 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008564 if (prot == -1)
8565 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008566 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008567 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008568 {
8569 if (mch_isdir(dir))
8570 {
8571 /* With the "p" flag it's OK if the dir already exists. */
8572 rettv->vval.v_number = OK;
8573 return;
8574 }
8575 mkdir_recurse(dir, prot);
8576 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008578 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008579}
8580#endif
8581
8582/*
8583 * "mode()" function
8584 */
8585 static void
8586f_mode(typval_T *argvars, typval_T *rettv)
8587{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008588 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008589
Bram Moolenaar612cc382018-07-29 15:34:26 +02008590 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008591
8592 if (time_for_testing == 93784)
8593 {
8594 /* Testing the two-character code. */
8595 buf[0] = 'x';
8596 buf[1] = '!';
8597 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008598#ifdef FEAT_TERMINAL
8599 else if (term_use_loop())
8600 buf[0] = 't';
8601#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602 else if (VIsual_active)
8603 {
8604 if (VIsual_select)
8605 buf[0] = VIsual_mode + 's' - 'v';
8606 else
8607 buf[0] = VIsual_mode;
8608 }
8609 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8610 || State == CONFIRM)
8611 {
8612 buf[0] = 'r';
8613 if (State == ASKMORE)
8614 buf[1] = 'm';
8615 else if (State == CONFIRM)
8616 buf[1] = '?';
8617 }
8618 else if (State == EXTERNCMD)
8619 buf[0] = '!';
8620 else if (State & INSERT)
8621 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622 if (State & VREPLACE_FLAG)
8623 {
8624 buf[0] = 'R';
8625 buf[1] = 'v';
8626 }
8627 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008628 {
8629 if (State & REPLACE_FLAG)
8630 buf[0] = 'R';
8631 else
8632 buf[0] = 'i';
8633#ifdef FEAT_INS_EXPAND
8634 if (ins_compl_active())
8635 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008636 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008637 buf[1] = 'x';
8638#endif
8639 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008640 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008641 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008642 {
8643 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008644 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008646 else if (exmode_active == EXMODE_NORMAL)
8647 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008648 }
8649 else
8650 {
8651 buf[0] = 'n';
8652 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008653 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008654 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008655 // to be able to detect force-linewise/blockwise/characterwise operations
8656 buf[2] = motion_force;
8657 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008658 else if (restart_edit == 'I' || restart_edit == 'R'
8659 || restart_edit == 'V')
8660 {
8661 buf[1] = 'i';
8662 buf[2] = restart_edit;
8663 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008664 }
8665
8666 /* Clear out the minor mode when the argument is not a non-zero number or
8667 * non-empty string. */
8668 if (!non_zero_arg(&argvars[0]))
8669 buf[1] = NUL;
8670
8671 rettv->vval.v_string = vim_strsave(buf);
8672 rettv->v_type = VAR_STRING;
8673}
8674
8675#if defined(FEAT_MZSCHEME) || defined(PROTO)
8676/*
8677 * "mzeval()" function
8678 */
8679 static void
8680f_mzeval(typval_T *argvars, typval_T *rettv)
8681{
8682 char_u *str;
8683 char_u buf[NUMBUFLEN];
8684
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008685 if (check_restricted() || check_secure())
8686 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008687 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008688 do_mzeval(str, rettv);
8689}
8690
8691 void
8692mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8693{
8694 typval_T argvars[3];
8695
8696 argvars[0].v_type = VAR_STRING;
8697 argvars[0].vval.v_string = name;
8698 copy_tv(args, &argvars[1]);
8699 argvars[2].v_type = VAR_UNKNOWN;
8700 f_call(argvars, rettv);
8701 clear_tv(&argvars[1]);
8702}
8703#endif
8704
8705/*
8706 * "nextnonblank()" function
8707 */
8708 static void
8709f_nextnonblank(typval_T *argvars, typval_T *rettv)
8710{
8711 linenr_T lnum;
8712
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008713 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008714 {
8715 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8716 {
8717 lnum = 0;
8718 break;
8719 }
8720 if (*skipwhite(ml_get(lnum)) != NUL)
8721 break;
8722 }
8723 rettv->vval.v_number = lnum;
8724}
8725
8726/*
8727 * "nr2char()" function
8728 */
8729 static void
8730f_nr2char(typval_T *argvars, typval_T *rettv)
8731{
8732 char_u buf[NUMBUFLEN];
8733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734 if (has_mbyte)
8735 {
8736 int utf8 = 0;
8737
8738 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008739 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008740 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008741 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008743 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008744 }
8745 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008746 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008747 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008748 buf[1] = NUL;
8749 }
8750 rettv->v_type = VAR_STRING;
8751 rettv->vval.v_string = vim_strsave(buf);
8752}
8753
8754/*
8755 * "or(expr, expr)" function
8756 */
8757 static void
8758f_or(typval_T *argvars, typval_T *rettv)
8759{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008760 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8761 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008762}
8763
8764/*
8765 * "pathshorten()" function
8766 */
8767 static void
8768f_pathshorten(typval_T *argvars, typval_T *rettv)
8769{
8770 char_u *p;
8771
8772 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008773 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008774 if (p == NULL)
8775 rettv->vval.v_string = NULL;
8776 else
8777 {
8778 p = vim_strsave(p);
8779 rettv->vval.v_string = p;
8780 if (p != NULL)
8781 shorten_dir(p);
8782 }
8783}
8784
8785#ifdef FEAT_PERL
8786/*
8787 * "perleval()" function
8788 */
8789 static void
8790f_perleval(typval_T *argvars, typval_T *rettv)
8791{
8792 char_u *str;
8793 char_u buf[NUMBUFLEN];
8794
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008795 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 do_perleval(str, rettv);
8797}
8798#endif
8799
8800#ifdef FEAT_FLOAT
8801/*
8802 * "pow()" function
8803 */
8804 static void
8805f_pow(typval_T *argvars, typval_T *rettv)
8806{
8807 float_T fx = 0.0, fy = 0.0;
8808
8809 rettv->v_type = VAR_FLOAT;
8810 if (get_float_arg(argvars, &fx) == OK
8811 && get_float_arg(&argvars[1], &fy) == OK)
8812 rettv->vval.v_float = pow(fx, fy);
8813 else
8814 rettv->vval.v_float = 0.0;
8815}
8816#endif
8817
8818/*
8819 * "prevnonblank()" function
8820 */
8821 static void
8822f_prevnonblank(typval_T *argvars, typval_T *rettv)
8823{
8824 linenr_T lnum;
8825
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008826 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8828 lnum = 0;
8829 else
8830 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8831 --lnum;
8832 rettv->vval.v_number = lnum;
8833}
8834
8835/* This dummy va_list is here because:
8836 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8837 * - locally in the function results in a "used before set" warning
8838 * - using va_start() to initialize it gives "function with fixed args" error */
8839static va_list ap;
8840
8841/*
8842 * "printf()" function
8843 */
8844 static void
8845f_printf(typval_T *argvars, typval_T *rettv)
8846{
8847 char_u buf[NUMBUFLEN];
8848 int len;
8849 char_u *s;
8850 int saved_did_emsg = did_emsg;
8851 char *fmt;
8852
8853 rettv->v_type = VAR_STRING;
8854 rettv->vval.v_string = NULL;
8855
8856 /* Get the required length, allocate the buffer and do it for real. */
8857 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008858 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008859 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008860 if (!did_emsg)
8861 {
8862 s = alloc(len + 1);
8863 if (s != NULL)
8864 {
8865 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008866 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8867 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008868 }
8869 }
8870 did_emsg |= saved_did_emsg;
8871}
8872
8873/*
8874 * "pumvisible()" function
8875 */
8876 static void
8877f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8878{
8879#ifdef FEAT_INS_EXPAND
8880 if (pum_visible())
8881 rettv->vval.v_number = 1;
8882#endif
8883}
8884
8885#ifdef FEAT_PYTHON3
8886/*
8887 * "py3eval()" function
8888 */
8889 static void
8890f_py3eval(typval_T *argvars, typval_T *rettv)
8891{
8892 char_u *str;
8893 char_u buf[NUMBUFLEN];
8894
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008895 if (check_restricted() || check_secure())
8896 return;
8897
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008898 if (p_pyx == 0)
8899 p_pyx = 3;
8900
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008901 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008902 do_py3eval(str, rettv);
8903}
8904#endif
8905
8906#ifdef FEAT_PYTHON
8907/*
8908 * "pyeval()" function
8909 */
8910 static void
8911f_pyeval(typval_T *argvars, typval_T *rettv)
8912{
8913 char_u *str;
8914 char_u buf[NUMBUFLEN];
8915
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008916 if (check_restricted() || check_secure())
8917 return;
8918
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008919 if (p_pyx == 0)
8920 p_pyx = 2;
8921
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008922 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008923 do_pyeval(str, rettv);
8924}
8925#endif
8926
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008927#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8928/*
8929 * "pyxeval()" function
8930 */
8931 static void
8932f_pyxeval(typval_T *argvars, typval_T *rettv)
8933{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008934 if (check_restricted() || check_secure())
8935 return;
8936
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008937# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8938 init_pyxversion();
8939 if (p_pyx == 2)
8940 f_pyeval(argvars, rettv);
8941 else
8942 f_py3eval(argvars, rettv);
8943# elif defined(FEAT_PYTHON)
8944 f_pyeval(argvars, rettv);
8945# elif defined(FEAT_PYTHON3)
8946 f_py3eval(argvars, rettv);
8947# endif
8948}
8949#endif
8950
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008951/*
8952 * "range()" function
8953 */
8954 static void
8955f_range(typval_T *argvars, typval_T *rettv)
8956{
8957 varnumber_T start;
8958 varnumber_T end;
8959 varnumber_T stride = 1;
8960 varnumber_T i;
8961 int error = FALSE;
8962
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008963 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964 if (argvars[1].v_type == VAR_UNKNOWN)
8965 {
8966 end = start - 1;
8967 start = 0;
8968 }
8969 else
8970 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008971 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008972 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008973 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008974 }
8975
8976 if (error)
8977 return; /* type error; errmsg already given */
8978 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008979 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008980 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008981 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008982 else
8983 {
8984 if (rettv_list_alloc(rettv) == OK)
8985 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8986 if (list_append_number(rettv->vval.v_list,
8987 (varnumber_T)i) == FAIL)
8988 break;
8989 }
8990}
8991
8992/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008993 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008994 */
8995 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008996readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008997{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008998 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008999 typval_T save_val;
9000 typval_T rettv;
9001 typval_T argv[2];
9002 int retval = 0;
9003 int error = FALSE;
9004
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009005 if (expr->v_type == VAR_UNKNOWN)
9006 return 1;
9007
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009008 prepare_vimvar(VV_VAL, &save_val);
9009 set_vim_var_string(VV_VAL, name, -1);
9010 argv[0].v_type = VAR_STRING;
9011 argv[0].vval.v_string = name;
9012
9013 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9014 goto theend;
9015
9016 retval = tv_get_number_chk(&rettv, &error);
9017 if (error)
9018 retval = -1;
9019 clear_tv(&rettv);
9020
9021theend:
9022 set_vim_var_string(VV_VAL, NULL, 0);
9023 restore_vimvar(VV_VAL, &save_val);
9024 return retval;
9025}
9026
9027/*
9028 * "readdir()" function
9029 */
9030 static void
9031f_readdir(typval_T *argvars, typval_T *rettv)
9032{
9033 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009034 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009035 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009036 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009037 garray_T ga;
9038 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009039
9040 if (rettv_list_alloc(rettv) == FAIL)
9041 return;
9042 path = tv_get_string(&argvars[0]);
9043 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009044
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009045 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9046 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009047 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009048 for (i = 0; i < ga.ga_len; i++)
9049 {
9050 p = ((char_u **)ga.ga_data)[i];
9051 list_append_string(rettv->vval.v_list, p, -1);
9052 }
9053 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009054 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009055}
9056
9057/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009058 * "readfile()" function
9059 */
9060 static void
9061f_readfile(typval_T *argvars, typval_T *rettv)
9062{
9063 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009064 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009065 int failed = FALSE;
9066 char_u *fname;
9067 FILE *fd;
9068 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9069 int io_size = sizeof(buf);
9070 int readlen; /* size of last fread() */
9071 char_u *prev = NULL; /* previously read bytes, if any */
9072 long prevlen = 0; /* length of data in prev */
9073 long prevsize = 0; /* size of prev buffer */
9074 long maxline = MAXLNUM;
9075 long cnt = 0;
9076 char_u *p; /* position in buf */
9077 char_u *start; /* start of current line */
9078
9079 if (argvars[1].v_type != VAR_UNKNOWN)
9080 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009081 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009082 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009083 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9084 blob = TRUE;
9085
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009086 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009087 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009088 }
9089
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009090 if (blob)
9091 {
9092 if (rettv_blob_alloc(rettv) == FAIL)
9093 return;
9094 }
9095 else
9096 {
9097 if (rettv_list_alloc(rettv) == FAIL)
9098 return;
9099 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009100
9101 /* Always open the file in binary mode, library functions have a mind of
9102 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009103 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009104 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9105 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009106 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009107 return;
9108 }
9109
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009110 if (blob)
9111 {
9112 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009114 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009115 blob_free(rettv->vval.v_blob);
9116 }
9117 fclose(fd);
9118 return;
9119 }
9120
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009121 while (cnt < maxline || maxline < 0)
9122 {
9123 readlen = (int)fread(buf, 1, io_size, fd);
9124
9125 /* This for loop processes what was read, but is also entered at end
9126 * of file so that either:
9127 * - an incomplete line gets written
9128 * - a "binary" file gets an empty line at the end if it ends in a
9129 * newline. */
9130 for (p = buf, start = buf;
9131 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9132 ++p)
9133 {
9134 if (*p == '\n' || readlen <= 0)
9135 {
9136 listitem_T *li;
9137 char_u *s = NULL;
9138 long_u len = p - start;
9139
9140 /* Finished a line. Remove CRs before NL. */
9141 if (readlen > 0 && !binary)
9142 {
9143 while (len > 0 && start[len - 1] == '\r')
9144 --len;
9145 /* removal may cross back to the "prev" string */
9146 if (len == 0)
9147 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9148 --prevlen;
9149 }
9150 if (prevlen == 0)
9151 s = vim_strnsave(start, (int)len);
9152 else
9153 {
9154 /* Change "prev" buffer to be the right size. This way
9155 * the bytes are only copied once, and very long lines are
9156 * allocated only once. */
9157 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9158 {
9159 mch_memmove(s + prevlen, start, len);
9160 s[prevlen + len] = NUL;
9161 prev = NULL; /* the list will own the string */
9162 prevlen = prevsize = 0;
9163 }
9164 }
9165 if (s == NULL)
9166 {
9167 do_outofmem_msg((long_u) prevlen + len + 1);
9168 failed = TRUE;
9169 break;
9170 }
9171
9172 if ((li = listitem_alloc()) == NULL)
9173 {
9174 vim_free(s);
9175 failed = TRUE;
9176 break;
9177 }
9178 li->li_tv.v_type = VAR_STRING;
9179 li->li_tv.v_lock = 0;
9180 li->li_tv.vval.v_string = s;
9181 list_append(rettv->vval.v_list, li);
9182
9183 start = p + 1; /* step over newline */
9184 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9185 break;
9186 }
9187 else if (*p == NUL)
9188 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009189 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9190 * when finding the BF and check the previous two bytes. */
9191 else if (*p == 0xbf && enc_utf8 && !binary)
9192 {
9193 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9194 * + 1, these may be in the "prev" string. */
9195 char_u back1 = p >= buf + 1 ? p[-1]
9196 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9197 char_u back2 = p >= buf + 2 ? p[-2]
9198 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9199 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9200
9201 if (back2 == 0xef && back1 == 0xbb)
9202 {
9203 char_u *dest = p - 2;
9204
9205 /* Usually a BOM is at the beginning of a file, and so at
9206 * the beginning of a line; then we can just step over it.
9207 */
9208 if (start == dest)
9209 start = p + 1;
9210 else
9211 {
9212 /* have to shuffle buf to close gap */
9213 int adjust_prevlen = 0;
9214
9215 if (dest < buf)
9216 {
9217 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9218 dest = buf;
9219 }
9220 if (readlen > p - buf + 1)
9221 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9222 readlen -= 3 - adjust_prevlen;
9223 prevlen -= adjust_prevlen;
9224 p = dest - 1;
9225 }
9226 }
9227 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009228 } /* for */
9229
9230 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9231 break;
9232 if (start < p)
9233 {
9234 /* There's part of a line in buf, store it in "prev". */
9235 if (p - start + prevlen >= prevsize)
9236 {
9237 /* need bigger "prev" buffer */
9238 char_u *newprev;
9239
9240 /* A common use case is ordinary text files and "prev" gets a
9241 * fragment of a line, so the first allocation is made
9242 * small, to avoid repeatedly 'allocing' large and
9243 * 'reallocing' small. */
9244 if (prevsize == 0)
9245 prevsize = (long)(p - start);
9246 else
9247 {
9248 long grow50pc = (prevsize * 3) / 2;
9249 long growmin = (long)((p - start) * 2 + prevlen);
9250 prevsize = grow50pc > growmin ? grow50pc : growmin;
9251 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009252 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009253 if (newprev == NULL)
9254 {
9255 do_outofmem_msg((long_u)prevsize);
9256 failed = TRUE;
9257 break;
9258 }
9259 prev = newprev;
9260 }
9261 /* Add the line part to end of "prev". */
9262 mch_memmove(prev + prevlen, start, p - start);
9263 prevlen += (long)(p - start);
9264 }
9265 } /* while */
9266
9267 /*
9268 * For a negative line count use only the lines at the end of the file,
9269 * free the rest.
9270 */
9271 if (!failed && maxline < 0)
9272 while (cnt > -maxline)
9273 {
9274 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9275 --cnt;
9276 }
9277
9278 if (failed)
9279 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009280 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009281 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009282 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283 }
9284
9285 vim_free(prev);
9286 fclose(fd);
9287}
9288
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009289 static void
9290return_register(int regname, typval_T *rettv)
9291{
9292 char_u buf[2] = {0, 0};
9293
9294 buf[0] = (char_u)regname;
9295 rettv->v_type = VAR_STRING;
9296 rettv->vval.v_string = vim_strsave(buf);
9297}
9298
9299/*
9300 * "reg_executing()" function
9301 */
9302 static void
9303f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9304{
9305 return_register(reg_executing, rettv);
9306}
9307
9308/*
9309 * "reg_recording()" function
9310 */
9311 static void
9312f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9313{
9314 return_register(reg_recording, rettv);
9315}
9316
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009317#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009318/*
9319 * Convert a List to proftime_T.
9320 * Return FAIL when there is something wrong.
9321 */
9322 static int
9323list2proftime(typval_T *arg, proftime_T *tm)
9324{
9325 long n1, n2;
9326 int error = FALSE;
9327
9328 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9329 || arg->vval.v_list->lv_len != 2)
9330 return FAIL;
9331 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9332 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009333# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009334 tm->HighPart = n1;
9335 tm->LowPart = n2;
9336# else
9337 tm->tv_sec = n1;
9338 tm->tv_usec = n2;
9339# endif
9340 return error ? FAIL : OK;
9341}
9342#endif /* FEAT_RELTIME */
9343
9344/*
9345 * "reltime()" function
9346 */
9347 static void
9348f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9349{
9350#ifdef FEAT_RELTIME
9351 proftime_T res;
9352 proftime_T start;
9353
9354 if (argvars[0].v_type == VAR_UNKNOWN)
9355 {
9356 /* No arguments: get current time. */
9357 profile_start(&res);
9358 }
9359 else if (argvars[1].v_type == VAR_UNKNOWN)
9360 {
9361 if (list2proftime(&argvars[0], &res) == FAIL)
9362 return;
9363 profile_end(&res);
9364 }
9365 else
9366 {
9367 /* Two arguments: compute the difference. */
9368 if (list2proftime(&argvars[0], &start) == FAIL
9369 || list2proftime(&argvars[1], &res) == FAIL)
9370 return;
9371 profile_sub(&res, &start);
9372 }
9373
9374 if (rettv_list_alloc(rettv) == OK)
9375 {
9376 long n1, n2;
9377
Bram Moolenaar4f974752019-02-17 17:44:42 +01009378# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009379 n1 = res.HighPart;
9380 n2 = res.LowPart;
9381# else
9382 n1 = res.tv_sec;
9383 n2 = res.tv_usec;
9384# endif
9385 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9386 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9387 }
9388#endif
9389}
9390
9391#ifdef FEAT_FLOAT
9392/*
9393 * "reltimefloat()" function
9394 */
9395 static void
9396f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9397{
9398# ifdef FEAT_RELTIME
9399 proftime_T tm;
9400# endif
9401
9402 rettv->v_type = VAR_FLOAT;
9403 rettv->vval.v_float = 0;
9404# ifdef FEAT_RELTIME
9405 if (list2proftime(&argvars[0], &tm) == OK)
9406 rettv->vval.v_float = profile_float(&tm);
9407# endif
9408}
9409#endif
9410
9411/*
9412 * "reltimestr()" function
9413 */
9414 static void
9415f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9416{
9417#ifdef FEAT_RELTIME
9418 proftime_T tm;
9419#endif
9420
9421 rettv->v_type = VAR_STRING;
9422 rettv->vval.v_string = NULL;
9423#ifdef FEAT_RELTIME
9424 if (list2proftime(&argvars[0], &tm) == OK)
9425 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9426#endif
9427}
9428
9429#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009430 static void
9431make_connection(void)
9432{
9433 if (X_DISPLAY == NULL
9434# ifdef FEAT_GUI
9435 && !gui.in_use
9436# endif
9437 )
9438 {
9439 x_force_connect = TRUE;
9440 setup_term_clip();
9441 x_force_connect = FALSE;
9442 }
9443}
9444
9445 static int
9446check_connection(void)
9447{
9448 make_connection();
9449 if (X_DISPLAY == NULL)
9450 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009451 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009452 return FAIL;
9453 }
9454 return OK;
9455}
9456#endif
9457
9458#ifdef FEAT_CLIENTSERVER
9459 static void
9460remote_common(typval_T *argvars, typval_T *rettv, int expr)
9461{
9462 char_u *server_name;
9463 char_u *keys;
9464 char_u *r = NULL;
9465 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009466 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009467# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009468 HWND w;
9469# else
9470 Window w;
9471# endif
9472
9473 if (check_restricted() || check_secure())
9474 return;
9475
9476# ifdef FEAT_X11
9477 if (check_connection() == FAIL)
9478 return;
9479# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009480 if (argvars[2].v_type != VAR_UNKNOWN
9481 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009482 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009483
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009484 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009485 if (server_name == NULL)
9486 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009487 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009488# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009489 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009490# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009491 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9492 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009493# endif
9494 {
9495 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009497 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009498 vim_free(r);
9499 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009500 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009501 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009502 return;
9503 }
9504
9505 rettv->vval.v_string = r;
9506
9507 if (argvars[2].v_type != VAR_UNKNOWN)
9508 {
9509 dictitem_T v;
9510 char_u str[30];
9511 char_u *idvar;
9512
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009513 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009514 if (idvar != NULL && *idvar != NUL)
9515 {
9516 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9517 v.di_tv.v_type = VAR_STRING;
9518 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009519 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009520 vim_free(v.di_tv.vval.v_string);
9521 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009522 }
9523}
9524#endif
9525
9526/*
9527 * "remote_expr()" function
9528 */
9529 static void
9530f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9531{
9532 rettv->v_type = VAR_STRING;
9533 rettv->vval.v_string = NULL;
9534#ifdef FEAT_CLIENTSERVER
9535 remote_common(argvars, rettv, TRUE);
9536#endif
9537}
9538
9539/*
9540 * "remote_foreground()" function
9541 */
9542 static void
9543f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9544{
9545#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009546# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009547 /* On Win32 it's done in this application. */
9548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009549 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009550
9551 if (server_name != NULL)
9552 serverForeground(server_name);
9553 }
9554# else
9555 /* Send a foreground() expression to the server. */
9556 argvars[1].v_type = VAR_STRING;
9557 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9558 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009559 rettv->v_type = VAR_STRING;
9560 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009561 remote_common(argvars, rettv, TRUE);
9562 vim_free(argvars[1].vval.v_string);
9563# endif
9564#endif
9565}
9566
9567 static void
9568f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9569{
9570#ifdef FEAT_CLIENTSERVER
9571 dictitem_T v;
9572 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009573# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009574 long_u n = 0;
9575# endif
9576 char_u *serverid;
9577
9578 if (check_restricted() || check_secure())
9579 {
9580 rettv->vval.v_number = -1;
9581 return;
9582 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009583 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009584 if (serverid == NULL)
9585 {
9586 rettv->vval.v_number = -1;
9587 return; /* type error; errmsg already given */
9588 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009589# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009590 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9591 if (n == 0)
9592 rettv->vval.v_number = -1;
9593 else
9594 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009595 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009596 rettv->vval.v_number = (s != NULL);
9597 }
9598# else
9599 if (check_connection() == FAIL)
9600 return;
9601
9602 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9603 serverStrToWin(serverid), &s);
9604# endif
9605
9606 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9607 {
9608 char_u *retvar;
9609
9610 v.di_tv.v_type = VAR_STRING;
9611 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009612 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009613 if (retvar != NULL)
9614 set_var(retvar, &v.di_tv, FALSE);
9615 vim_free(v.di_tv.vval.v_string);
9616 }
9617#else
9618 rettv->vval.v_number = -1;
9619#endif
9620}
9621
9622 static void
9623f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9624{
9625 char_u *r = NULL;
9626
9627#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009628 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009629
9630 if (serverid != NULL && !check_restricted() && !check_secure())
9631 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009632 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009633# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009634 /* The server's HWND is encoded in the 'id' parameter */
9635 long_u n = 0;
9636# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009637
9638 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009639 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009640
Bram Moolenaar4f974752019-02-17 17:44:42 +01009641# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009642 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9643 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009644 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009645 if (r == NULL)
9646# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009647 if (check_connection() == FAIL
9648 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9649 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009650# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009651 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009652 }
9653#endif
9654 rettv->v_type = VAR_STRING;
9655 rettv->vval.v_string = r;
9656}
9657
9658/*
9659 * "remote_send()" function
9660 */
9661 static void
9662f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9663{
9664 rettv->v_type = VAR_STRING;
9665 rettv->vval.v_string = NULL;
9666#ifdef FEAT_CLIENTSERVER
9667 remote_common(argvars, rettv, FALSE);
9668#endif
9669}
9670
9671/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009672 * "remote_startserver()" function
9673 */
9674 static void
9675f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9676{
9677#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009678 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009679
9680 if (server == NULL)
9681 return; /* type error; errmsg already given */
9682 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009683 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009684 else
9685 {
9686# ifdef FEAT_X11
9687 if (check_connection() == OK)
9688 serverRegisterName(X_DISPLAY, server);
9689# else
9690 serverSetName(server);
9691# endif
9692 }
9693#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009694 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009695#endif
9696}
9697
9698/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009699 * "remove()" function
9700 */
9701 static void
9702f_remove(typval_T *argvars, typval_T *rettv)
9703{
9704 list_T *l;
9705 listitem_T *item, *item2;
9706 listitem_T *li;
9707 long idx;
9708 long end;
9709 char_u *key;
9710 dict_T *d;
9711 dictitem_T *di;
9712 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009713 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009714
9715 if (argvars[0].v_type == VAR_DICT)
9716 {
9717 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009718 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009719 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009720 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009721 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009722 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009723 if (key != NULL)
9724 {
9725 di = dict_find(d, key, -1);
9726 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009727 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009728 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9729 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9730 {
9731 *rettv = di->di_tv;
9732 init_tv(&di->di_tv);
9733 dictitem_remove(d, di);
9734 }
9735 }
9736 }
9737 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009738 else if (argvars[0].v_type == VAR_BLOB)
9739 {
9740 idx = (long)tv_get_number_chk(&argvars[1], &error);
9741 if (!error)
9742 {
9743 blob_T *b = argvars[0].vval.v_blob;
9744 int len = blob_len(b);
9745 char_u *p;
9746
9747 if (idx < 0)
9748 // count from the end
9749 idx = len + idx;
9750 if (idx < 0 || idx >= len)
9751 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009752 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009753 return;
9754 }
9755 if (argvars[2].v_type == VAR_UNKNOWN)
9756 {
9757 // Remove one item, return its value.
9758 p = (char_u *)b->bv_ga.ga_data;
9759 rettv->vval.v_number = (varnumber_T) *(p + idx);
9760 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9761 --b->bv_ga.ga_len;
9762 }
9763 else
9764 {
9765 blob_T *blob;
9766
9767 // Remove range of items, return list with values.
9768 end = (long)tv_get_number_chk(&argvars[2], &error);
9769 if (error)
9770 return;
9771 if (end < 0)
9772 // count from the end
9773 end = len + end;
9774 if (end >= len || idx > end)
9775 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009776 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009777 return;
9778 }
9779 blob = blob_alloc();
9780 if (blob == NULL)
9781 return;
9782 blob->bv_ga.ga_len = end - idx + 1;
9783 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9784 {
9785 vim_free(blob);
9786 return;
9787 }
9788 p = (char_u *)b->bv_ga.ga_data;
9789 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9790 (size_t)(end - idx + 1));
9791 ++blob->bv_refcount;
9792 rettv->v_type = VAR_BLOB;
9793 rettv->vval.v_blob = blob;
9794
9795 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9796 b->bv_ga.ga_len -= end - idx + 1;
9797 }
9798 }
9799 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009800 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009801 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009802 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009803 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009804 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009805 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009806 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009807 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009808 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009809 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009810 else
9811 {
9812 if (argvars[2].v_type == VAR_UNKNOWN)
9813 {
9814 /* Remove one item, return its value. */
9815 vimlist_remove(l, item, item);
9816 *rettv = item->li_tv;
9817 vim_free(item);
9818 }
9819 else
9820 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009821 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009822 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009823 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009824 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009825 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009826 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009827 else
9828 {
9829 int cnt = 0;
9830
9831 for (li = item; li != NULL; li = li->li_next)
9832 {
9833 ++cnt;
9834 if (li == item2)
9835 break;
9836 }
9837 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009838 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009839 else
9840 {
9841 vimlist_remove(l, item, item2);
9842 if (rettv_list_alloc(rettv) == OK)
9843 {
9844 l = rettv->vval.v_list;
9845 l->lv_first = item;
9846 l->lv_last = item2;
9847 item->li_prev = NULL;
9848 item2->li_next = NULL;
9849 l->lv_len = cnt;
9850 }
9851 }
9852 }
9853 }
9854 }
9855 }
9856}
9857
9858/*
9859 * "rename({from}, {to})" function
9860 */
9861 static void
9862f_rename(typval_T *argvars, typval_T *rettv)
9863{
9864 char_u buf[NUMBUFLEN];
9865
9866 if (check_restricted() || check_secure())
9867 rettv->vval.v_number = -1;
9868 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009869 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9870 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009871}
9872
9873/*
9874 * "repeat()" function
9875 */
9876 static void
9877f_repeat(typval_T *argvars, typval_T *rettv)
9878{
9879 char_u *p;
9880 int n;
9881 int slen;
9882 int len;
9883 char_u *r;
9884 int i;
9885
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009886 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009887 if (argvars[0].v_type == VAR_LIST)
9888 {
9889 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9890 while (n-- > 0)
9891 if (list_extend(rettv->vval.v_list,
9892 argvars[0].vval.v_list, NULL) == FAIL)
9893 break;
9894 }
9895 else
9896 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009897 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009898 rettv->v_type = VAR_STRING;
9899 rettv->vval.v_string = NULL;
9900
9901 slen = (int)STRLEN(p);
9902 len = slen * n;
9903 if (len <= 0)
9904 return;
9905
9906 r = alloc(len + 1);
9907 if (r != NULL)
9908 {
9909 for (i = 0; i < n; i++)
9910 mch_memmove(r + i * slen, p, (size_t)slen);
9911 r[len] = NUL;
9912 }
9913
9914 rettv->vval.v_string = r;
9915 }
9916}
9917
9918/*
9919 * "resolve()" function
9920 */
9921 static void
9922f_resolve(typval_T *argvars, typval_T *rettv)
9923{
9924 char_u *p;
9925#ifdef HAVE_READLINK
9926 char_u *buf = NULL;
9927#endif
9928
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009929 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009930#ifdef FEAT_SHORTCUT
9931 {
9932 char_u *v = NULL;
9933
Bram Moolenaardce1e892019-02-10 23:18:53 +01009934 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009935 if (v != NULL)
9936 rettv->vval.v_string = v;
9937 else
9938 rettv->vval.v_string = vim_strsave(p);
9939 }
9940#else
9941# ifdef HAVE_READLINK
9942 {
9943 char_u *cpy;
9944 int len;
9945 char_u *remain = NULL;
9946 char_u *q;
9947 int is_relative_to_current = FALSE;
9948 int has_trailing_pathsep = FALSE;
9949 int limit = 100;
9950
9951 p = vim_strsave(p);
9952
9953 if (p[0] == '.' && (vim_ispathsep(p[1])
9954 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9955 is_relative_to_current = TRUE;
9956
9957 len = STRLEN(p);
9958 if (len > 0 && after_pathsep(p, p + len))
9959 {
9960 has_trailing_pathsep = TRUE;
9961 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9962 }
9963
9964 q = getnextcomp(p);
9965 if (*q != NUL)
9966 {
9967 /* Separate the first path component in "p", and keep the
9968 * remainder (beginning with the path separator). */
9969 remain = vim_strsave(q - 1);
9970 q[-1] = NUL;
9971 }
9972
9973 buf = alloc(MAXPATHL + 1);
9974 if (buf == NULL)
9975 goto fail;
9976
9977 for (;;)
9978 {
9979 for (;;)
9980 {
9981 len = readlink((char *)p, (char *)buf, MAXPATHL);
9982 if (len <= 0)
9983 break;
9984 buf[len] = NUL;
9985
9986 if (limit-- == 0)
9987 {
9988 vim_free(p);
9989 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009990 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009991 rettv->vval.v_string = NULL;
9992 goto fail;
9993 }
9994
9995 /* Ensure that the result will have a trailing path separator
9996 * if the argument has one. */
9997 if (remain == NULL && has_trailing_pathsep)
9998 add_pathsep(buf);
9999
10000 /* Separate the first path component in the link value and
10001 * concatenate the remainders. */
10002 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10003 if (*q != NUL)
10004 {
10005 if (remain == NULL)
10006 remain = vim_strsave(q - 1);
10007 else
10008 {
10009 cpy = concat_str(q - 1, remain);
10010 if (cpy != NULL)
10011 {
10012 vim_free(remain);
10013 remain = cpy;
10014 }
10015 }
10016 q[-1] = NUL;
10017 }
10018
10019 q = gettail(p);
10020 if (q > p && *q == NUL)
10021 {
10022 /* Ignore trailing path separator. */
10023 q[-1] = NUL;
10024 q = gettail(p);
10025 }
10026 if (q > p && !mch_isFullName(buf))
10027 {
10028 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010029 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010030 if (cpy != NULL)
10031 {
10032 STRCPY(cpy, p);
10033 STRCPY(gettail(cpy), buf);
10034 vim_free(p);
10035 p = cpy;
10036 }
10037 }
10038 else
10039 {
10040 vim_free(p);
10041 p = vim_strsave(buf);
10042 }
10043 }
10044
10045 if (remain == NULL)
10046 break;
10047
10048 /* Append the first path component of "remain" to "p". */
10049 q = getnextcomp(remain + 1);
10050 len = q - remain - (*q != NUL);
10051 cpy = vim_strnsave(p, STRLEN(p) + len);
10052 if (cpy != NULL)
10053 {
10054 STRNCAT(cpy, remain, len);
10055 vim_free(p);
10056 p = cpy;
10057 }
10058 /* Shorten "remain". */
10059 if (*q != NUL)
10060 STRMOVE(remain, q - 1);
10061 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010062 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010063 }
10064
10065 /* If the result is a relative path name, make it explicitly relative to
10066 * the current directory if and only if the argument had this form. */
10067 if (!vim_ispathsep(*p))
10068 {
10069 if (is_relative_to_current
10070 && *p != NUL
10071 && !(p[0] == '.'
10072 && (p[1] == NUL
10073 || vim_ispathsep(p[1])
10074 || (p[1] == '.'
10075 && (p[2] == NUL
10076 || vim_ispathsep(p[2]))))))
10077 {
10078 /* Prepend "./". */
10079 cpy = concat_str((char_u *)"./", p);
10080 if (cpy != NULL)
10081 {
10082 vim_free(p);
10083 p = cpy;
10084 }
10085 }
10086 else if (!is_relative_to_current)
10087 {
10088 /* Strip leading "./". */
10089 q = p;
10090 while (q[0] == '.' && vim_ispathsep(q[1]))
10091 q += 2;
10092 if (q > p)
10093 STRMOVE(p, p + 2);
10094 }
10095 }
10096
10097 /* Ensure that the result will have no trailing path separator
10098 * if the argument had none. But keep "/" or "//". */
10099 if (!has_trailing_pathsep)
10100 {
10101 q = p + STRLEN(p);
10102 if (after_pathsep(p, q))
10103 *gettail_sep(p) = NUL;
10104 }
10105
10106 rettv->vval.v_string = p;
10107 }
10108# else
10109 rettv->vval.v_string = vim_strsave(p);
10110# endif
10111#endif
10112
10113 simplify_filename(rettv->vval.v_string);
10114
10115#ifdef HAVE_READLINK
10116fail:
10117 vim_free(buf);
10118#endif
10119 rettv->v_type = VAR_STRING;
10120}
10121
10122/*
10123 * "reverse({list})" function
10124 */
10125 static void
10126f_reverse(typval_T *argvars, typval_T *rettv)
10127{
10128 list_T *l;
10129 listitem_T *li, *ni;
10130
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010131 if (argvars[0].v_type == VAR_BLOB)
10132 {
10133 blob_T *b = argvars[0].vval.v_blob;
10134 int i, len = blob_len(b);
10135
10136 for (i = 0; i < len / 2; i++)
10137 {
10138 int tmp = blob_get(b, i);
10139
10140 blob_set(b, i, blob_get(b, len - i - 1));
10141 blob_set(b, len - i - 1, tmp);
10142 }
10143 rettv_blob_set(rettv, b);
10144 return;
10145 }
10146
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010147 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010148 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010149 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010150 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010151 (char_u *)N_("reverse() argument"), TRUE))
10152 {
10153 li = l->lv_last;
10154 l->lv_first = l->lv_last = NULL;
10155 l->lv_len = 0;
10156 while (li != NULL)
10157 {
10158 ni = li->li_prev;
10159 list_append(l, li);
10160 li = ni;
10161 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010162 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010163 l->lv_idx = l->lv_len - l->lv_idx - 1;
10164 }
10165}
10166
10167#define SP_NOMOVE 0x01 /* don't move cursor */
10168#define SP_REPEAT 0x02 /* repeat to find outer pair */
10169#define SP_RETCOUNT 0x04 /* return matchcount */
10170#define SP_SETPCMARK 0x08 /* set previous context mark */
10171#define SP_START 0x10 /* accept match at start position */
10172#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10173#define SP_END 0x40 /* leave cursor at end of match */
10174#define SP_COLUMN 0x80 /* start at cursor column */
10175
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010176/*
10177 * Get flags for a search function.
10178 * Possibly sets "p_ws".
10179 * Returns BACKWARD, FORWARD or zero (for an error).
10180 */
10181 static int
10182get_search_arg(typval_T *varp, int *flagsp)
10183{
10184 int dir = FORWARD;
10185 char_u *flags;
10186 char_u nbuf[NUMBUFLEN];
10187 int mask;
10188
10189 if (varp->v_type != VAR_UNKNOWN)
10190 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010191 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010192 if (flags == NULL)
10193 return 0; /* type error; errmsg already given */
10194 while (*flags != NUL)
10195 {
10196 switch (*flags)
10197 {
10198 case 'b': dir = BACKWARD; break;
10199 case 'w': p_ws = TRUE; break;
10200 case 'W': p_ws = FALSE; break;
10201 default: mask = 0;
10202 if (flagsp != NULL)
10203 switch (*flags)
10204 {
10205 case 'c': mask = SP_START; break;
10206 case 'e': mask = SP_END; break;
10207 case 'm': mask = SP_RETCOUNT; break;
10208 case 'n': mask = SP_NOMOVE; break;
10209 case 'p': mask = SP_SUBPAT; break;
10210 case 'r': mask = SP_REPEAT; break;
10211 case 's': mask = SP_SETPCMARK; break;
10212 case 'z': mask = SP_COLUMN; break;
10213 }
10214 if (mask == 0)
10215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010216 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010217 dir = 0;
10218 }
10219 else
10220 *flagsp |= mask;
10221 }
10222 if (dir == 0)
10223 break;
10224 ++flags;
10225 }
10226 }
10227 return dir;
10228}
10229
10230/*
10231 * Shared by search() and searchpos() functions.
10232 */
10233 static int
10234search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10235{
10236 int flags;
10237 char_u *pat;
10238 pos_T pos;
10239 pos_T save_cursor;
10240 int save_p_ws = p_ws;
10241 int dir;
10242 int retval = 0; /* default: FAIL */
10243 long lnum_stop = 0;
10244 proftime_T tm;
10245#ifdef FEAT_RELTIME
10246 long time_limit = 0;
10247#endif
10248 int options = SEARCH_KEEP;
10249 int subpatnum;
10250
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010251 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010252 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10253 if (dir == 0)
10254 goto theend;
10255 flags = *flagsp;
10256 if (flags & SP_START)
10257 options |= SEARCH_START;
10258 if (flags & SP_END)
10259 options |= SEARCH_END;
10260 if (flags & SP_COLUMN)
10261 options |= SEARCH_COL;
10262
10263 /* Optional arguments: line number to stop searching and timeout. */
10264 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10265 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010266 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010267 if (lnum_stop < 0)
10268 goto theend;
10269#ifdef FEAT_RELTIME
10270 if (argvars[3].v_type != VAR_UNKNOWN)
10271 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010272 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010273 if (time_limit < 0)
10274 goto theend;
10275 }
10276#endif
10277 }
10278
10279#ifdef FEAT_RELTIME
10280 /* Set the time limit, if there is one. */
10281 profile_setlimit(time_limit, &tm);
10282#endif
10283
10284 /*
10285 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10286 * Check to make sure only those flags are set.
10287 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10288 * flags cannot be set. Check for that condition also.
10289 */
10290 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10291 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10292 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010293 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010294 goto theend;
10295 }
10296
10297 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010298 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010299 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010300 if (subpatnum != FAIL)
10301 {
10302 if (flags & SP_SUBPAT)
10303 retval = subpatnum;
10304 else
10305 retval = pos.lnum;
10306 if (flags & SP_SETPCMARK)
10307 setpcmark();
10308 curwin->w_cursor = pos;
10309 if (match_pos != NULL)
10310 {
10311 /* Store the match cursor position */
10312 match_pos->lnum = pos.lnum;
10313 match_pos->col = pos.col + 1;
10314 }
10315 /* "/$" will put the cursor after the end of the line, may need to
10316 * correct that here */
10317 check_cursor();
10318 }
10319
10320 /* If 'n' flag is used: restore cursor position. */
10321 if (flags & SP_NOMOVE)
10322 curwin->w_cursor = save_cursor;
10323 else
10324 curwin->w_set_curswant = TRUE;
10325theend:
10326 p_ws = save_p_ws;
10327
10328 return retval;
10329}
10330
10331#ifdef FEAT_FLOAT
10332
10333/*
10334 * round() is not in C90, use ceil() or floor() instead.
10335 */
10336 float_T
10337vim_round(float_T f)
10338{
10339 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10340}
10341
10342/*
10343 * "round({float})" function
10344 */
10345 static void
10346f_round(typval_T *argvars, typval_T *rettv)
10347{
10348 float_T f = 0.0;
10349
10350 rettv->v_type = VAR_FLOAT;
10351 if (get_float_arg(argvars, &f) == OK)
10352 rettv->vval.v_float = vim_round(f);
10353 else
10354 rettv->vval.v_float = 0.0;
10355}
10356#endif
10357
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010358#ifdef FEAT_RUBY
10359/*
10360 * "rubyeval()" function
10361 */
10362 static void
10363f_rubyeval(typval_T *argvars, typval_T *rettv)
10364{
10365 char_u *str;
10366 char_u buf[NUMBUFLEN];
10367
10368 str = tv_get_string_buf(&argvars[0], buf);
10369 do_rubyeval(str, rettv);
10370}
10371#endif
10372
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010373/*
10374 * "screenattr()" function
10375 */
10376 static void
10377f_screenattr(typval_T *argvars, typval_T *rettv)
10378{
10379 int row;
10380 int col;
10381 int c;
10382
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010383 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10384 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010385 if (row < 0 || row >= screen_Rows
10386 || col < 0 || col >= screen_Columns)
10387 c = -1;
10388 else
10389 c = ScreenAttrs[LineOffset[row] + col];
10390 rettv->vval.v_number = c;
10391}
10392
10393/*
10394 * "screenchar()" function
10395 */
10396 static void
10397f_screenchar(typval_T *argvars, typval_T *rettv)
10398{
10399 int row;
10400 int col;
10401 int off;
10402 int c;
10403
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010404 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10405 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010406 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010407 c = -1;
10408 else
10409 {
10410 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010411 if (enc_utf8 && ScreenLinesUC[off] != 0)
10412 c = ScreenLinesUC[off];
10413 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010414 c = ScreenLines[off];
10415 }
10416 rettv->vval.v_number = c;
10417}
10418
10419/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010420 * "screenchars()" function
10421 */
10422 static void
10423f_screenchars(typval_T *argvars, typval_T *rettv)
10424{
10425 int row;
10426 int col;
10427 int off;
10428 int c;
10429 int i;
10430
10431 if (rettv_list_alloc(rettv) == FAIL)
10432 return;
10433 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10434 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10435 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10436 return;
10437
10438 off = LineOffset[row] + col;
10439 if (enc_utf8 && ScreenLinesUC[off] != 0)
10440 c = ScreenLinesUC[off];
10441 else
10442 c = ScreenLines[off];
10443 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10444
10445 if (enc_utf8)
10446
10447 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10448 list_append_number(rettv->vval.v_list,
10449 (varnumber_T)ScreenLinesC[i][off]);
10450}
10451
10452/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010453 * "screencol()" function
10454 *
10455 * First column is 1 to be consistent with virtcol().
10456 */
10457 static void
10458f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10459{
10460 rettv->vval.v_number = screen_screencol() + 1;
10461}
10462
10463/*
10464 * "screenrow()" function
10465 */
10466 static void
10467f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10468{
10469 rettv->vval.v_number = screen_screenrow() + 1;
10470}
10471
10472/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010473 * "screenstring()" function
10474 */
10475 static void
10476f_screenstring(typval_T *argvars, typval_T *rettv)
10477{
10478 int row;
10479 int col;
10480 int off;
10481 int c;
10482 int i;
10483 char_u buf[MB_MAXBYTES + 1];
10484 int buflen = 0;
10485
10486 rettv->vval.v_string = NULL;
10487 rettv->v_type = VAR_STRING;
10488
10489 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10490 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10491 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10492 return;
10493
10494 off = LineOffset[row] + col;
10495 if (enc_utf8 && ScreenLinesUC[off] != 0)
10496 c = ScreenLinesUC[off];
10497 else
10498 c = ScreenLines[off];
10499 buflen += mb_char2bytes(c, buf);
10500
10501 if (enc_utf8)
10502 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10503 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10504
10505 buf[buflen] = NUL;
10506 rettv->vval.v_string = vim_strsave(buf);
10507}
10508
10509/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010510 * "search()" function
10511 */
10512 static void
10513f_search(typval_T *argvars, typval_T *rettv)
10514{
10515 int flags = 0;
10516
10517 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10518}
10519
10520/*
10521 * "searchdecl()" function
10522 */
10523 static void
10524f_searchdecl(typval_T *argvars, typval_T *rettv)
10525{
10526 int locally = 1;
10527 int thisblock = 0;
10528 int error = FALSE;
10529 char_u *name;
10530
10531 rettv->vval.v_number = 1; /* default: FAIL */
10532
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010533 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010534 if (argvars[1].v_type != VAR_UNKNOWN)
10535 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010536 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010537 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010538 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010539 }
10540 if (!error && name != NULL)
10541 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10542 locally, thisblock, SEARCH_KEEP) == FAIL;
10543}
10544
10545/*
10546 * Used by searchpair() and searchpairpos()
10547 */
10548 static int
10549searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10550{
10551 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010552 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010553 int save_p_ws = p_ws;
10554 int dir;
10555 int flags = 0;
10556 char_u nbuf1[NUMBUFLEN];
10557 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010558 int retval = 0; /* default: FAIL */
10559 long lnum_stop = 0;
10560 long time_limit = 0;
10561
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010562 /* Get the three pattern arguments: start, middle, end. Will result in an
10563 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010564 spat = tv_get_string_chk(&argvars[0]);
10565 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10566 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010567 if (spat == NULL || mpat == NULL || epat == NULL)
10568 goto theend; /* type error */
10569
10570 /* Handle the optional fourth argument: flags */
10571 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10572 if (dir == 0)
10573 goto theend;
10574
10575 /* Don't accept SP_END or SP_SUBPAT.
10576 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10577 */
10578 if ((flags & (SP_END | SP_SUBPAT)) != 0
10579 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010581 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010582 goto theend;
10583 }
10584
10585 /* Using 'r' implies 'W', otherwise it doesn't work. */
10586 if (flags & SP_REPEAT)
10587 p_ws = FALSE;
10588
10589 /* Optional fifth argument: skip expression */
10590 if (argvars[3].v_type == VAR_UNKNOWN
10591 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010592 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010593 else
10594 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010595 skip = &argvars[4];
10596 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10597 && skip->v_type != VAR_STRING)
10598 {
10599 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010600 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010601 goto theend;
10602 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010603 if (argvars[5].v_type != VAR_UNKNOWN)
10604 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010605 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010606 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010608 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010609 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010610 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010611#ifdef FEAT_RELTIME
10612 if (argvars[6].v_type != VAR_UNKNOWN)
10613 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010614 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010615 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010617 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010618 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010619 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010620 }
10621#endif
10622 }
10623 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010624
10625 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10626 match_pos, lnum_stop, time_limit);
10627
10628theend:
10629 p_ws = save_p_ws;
10630
10631 return retval;
10632}
10633
10634/*
10635 * "searchpair()" function
10636 */
10637 static void
10638f_searchpair(typval_T *argvars, typval_T *rettv)
10639{
10640 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10641}
10642
10643/*
10644 * "searchpairpos()" function
10645 */
10646 static void
10647f_searchpairpos(typval_T *argvars, typval_T *rettv)
10648{
10649 pos_T match_pos;
10650 int lnum = 0;
10651 int col = 0;
10652
10653 if (rettv_list_alloc(rettv) == FAIL)
10654 return;
10655
10656 if (searchpair_cmn(argvars, &match_pos) > 0)
10657 {
10658 lnum = match_pos.lnum;
10659 col = match_pos.col;
10660 }
10661
10662 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10663 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10664}
10665
10666/*
10667 * Search for a start/middle/end thing.
10668 * Used by searchpair(), see its documentation for the details.
10669 * Returns 0 or -1 for no match,
10670 */
10671 long
10672do_searchpair(
10673 char_u *spat, /* start pattern */
10674 char_u *mpat, /* middle pattern */
10675 char_u *epat, /* end pattern */
10676 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010677 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010678 int flags, /* SP_SETPCMARK and other SP_ values */
10679 pos_T *match_pos,
10680 linenr_T lnum_stop, /* stop at this line if not zero */
10681 long time_limit UNUSED) /* stop after this many msec */
10682{
10683 char_u *save_cpo;
10684 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10685 long retval = 0;
10686 pos_T pos;
10687 pos_T firstpos;
10688 pos_T foundpos;
10689 pos_T save_cursor;
10690 pos_T save_pos;
10691 int n;
10692 int r;
10693 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010694 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010695 int err;
10696 int options = SEARCH_KEEP;
10697 proftime_T tm;
10698
10699 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10700 save_cpo = p_cpo;
10701 p_cpo = empty_option;
10702
10703#ifdef FEAT_RELTIME
10704 /* Set the time limit, if there is one. */
10705 profile_setlimit(time_limit, &tm);
10706#endif
10707
10708 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10709 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010710 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
10711 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010712 if (pat2 == NULL || pat3 == NULL)
10713 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010714 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010715 if (*mpat == NUL)
10716 STRCPY(pat3, pat2);
10717 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010718 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010719 spat, epat, mpat);
10720 if (flags & SP_START)
10721 options |= SEARCH_START;
10722
Bram Moolenaar48570482017-10-30 21:48:41 +010010723 if (skip != NULL)
10724 {
10725 /* Empty string means to not use the skip expression. */
10726 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10727 use_skip = skip->vval.v_string != NULL
10728 && *skip->vval.v_string != NUL;
10729 }
10730
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010731 save_cursor = curwin->w_cursor;
10732 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010733 CLEAR_POS(&firstpos);
10734 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010735 pat = pat3;
10736 for (;;)
10737 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010738 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010739 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010740 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010741 /* didn't find it or found the first match again: FAIL */
10742 break;
10743
10744 if (firstpos.lnum == 0)
10745 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010746 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010747 {
10748 /* Found the same position again. Can happen with a pattern that
10749 * has "\zs" at the end and searching backwards. Advance one
10750 * character and try again. */
10751 if (dir == BACKWARD)
10752 decl(&pos);
10753 else
10754 incl(&pos);
10755 }
10756 foundpos = pos;
10757
10758 /* clear the start flag to avoid getting stuck here */
10759 options &= ~SEARCH_START;
10760
10761 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010762 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010763 {
10764 save_pos = curwin->w_cursor;
10765 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010766 err = FALSE;
10767 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010768 curwin->w_cursor = save_pos;
10769 if (err)
10770 {
10771 /* Evaluating {skip} caused an error, break here. */
10772 curwin->w_cursor = save_cursor;
10773 retval = -1;
10774 break;
10775 }
10776 if (r)
10777 continue;
10778 }
10779
10780 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10781 {
10782 /* Found end when searching backwards or start when searching
10783 * forward: nested pair. */
10784 ++nest;
10785 pat = pat2; /* nested, don't search for middle */
10786 }
10787 else
10788 {
10789 /* Found end when searching forward or start when searching
10790 * backward: end of (nested) pair; or found middle in outer pair. */
10791 if (--nest == 1)
10792 pat = pat3; /* outer level, search for middle */
10793 }
10794
10795 if (nest == 0)
10796 {
10797 /* Found the match: return matchcount or line number. */
10798 if (flags & SP_RETCOUNT)
10799 ++retval;
10800 else
10801 retval = pos.lnum;
10802 if (flags & SP_SETPCMARK)
10803 setpcmark();
10804 curwin->w_cursor = pos;
10805 if (!(flags & SP_REPEAT))
10806 break;
10807 nest = 1; /* search for next unmatched */
10808 }
10809 }
10810
10811 if (match_pos != NULL)
10812 {
10813 /* Store the match cursor position */
10814 match_pos->lnum = curwin->w_cursor.lnum;
10815 match_pos->col = curwin->w_cursor.col + 1;
10816 }
10817
10818 /* If 'n' flag is used or search failed: restore cursor position. */
10819 if ((flags & SP_NOMOVE) || retval == 0)
10820 curwin->w_cursor = save_cursor;
10821
10822theend:
10823 vim_free(pat2);
10824 vim_free(pat3);
10825 if (p_cpo == empty_option)
10826 p_cpo = save_cpo;
10827 else
10828 /* Darn, evaluating the {skip} expression changed the value. */
10829 free_string_option(save_cpo);
10830
10831 return retval;
10832}
10833
10834/*
10835 * "searchpos()" function
10836 */
10837 static void
10838f_searchpos(typval_T *argvars, typval_T *rettv)
10839{
10840 pos_T match_pos;
10841 int lnum = 0;
10842 int col = 0;
10843 int n;
10844 int flags = 0;
10845
10846 if (rettv_list_alloc(rettv) == FAIL)
10847 return;
10848
10849 n = search_cmn(argvars, &match_pos, &flags);
10850 if (n > 0)
10851 {
10852 lnum = match_pos.lnum;
10853 col = match_pos.col;
10854 }
10855
10856 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10857 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10858 if (flags & SP_SUBPAT)
10859 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10860}
10861
10862 static void
10863f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10864{
10865#ifdef FEAT_CLIENTSERVER
10866 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010867 char_u *server = tv_get_string_chk(&argvars[0]);
10868 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010869
10870 rettv->vval.v_number = -1;
10871 if (server == NULL || reply == NULL)
10872 return;
10873 if (check_restricted() || check_secure())
10874 return;
10875# ifdef FEAT_X11
10876 if (check_connection() == FAIL)
10877 return;
10878# endif
10879
10880 if (serverSendReply(server, reply) < 0)
10881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010882 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010883 return;
10884 }
10885 rettv->vval.v_number = 0;
10886#else
10887 rettv->vval.v_number = -1;
10888#endif
10889}
10890
10891 static void
10892f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10893{
10894 char_u *r = NULL;
10895
10896#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010897# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010898 r = serverGetVimNames();
10899# else
10900 make_connection();
10901 if (X_DISPLAY != NULL)
10902 r = serverGetVimNames(X_DISPLAY);
10903# endif
10904#endif
10905 rettv->v_type = VAR_STRING;
10906 rettv->vval.v_string = r;
10907}
10908
10909/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010910 * "setbufline()" function
10911 */
10912 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010913f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010914{
10915 linenr_T lnum;
10916 buf_T *buf;
10917
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010918 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010919 if (buf == NULL)
10920 rettv->vval.v_number = 1; /* FAIL */
10921 else
10922 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010923 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010924 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010925 }
10926}
10927
10928/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010929 * "setbufvar()" function
10930 */
10931 static void
10932f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10933{
10934 buf_T *buf;
10935 char_u *varname, *bufvarname;
10936 typval_T *varp;
10937 char_u nbuf[NUMBUFLEN];
10938
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010939 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010940 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010941 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10942 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010943 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010944 varp = &argvars[2];
10945
10946 if (buf != NULL && varname != NULL && varp != NULL)
10947 {
10948 if (*varname == '&')
10949 {
10950 long numval;
10951 char_u *strval;
10952 int error = FALSE;
10953 aco_save_T aco;
10954
10955 /* set curbuf to be our buf, temporarily */
10956 aucmd_prepbuf(&aco, buf);
10957
10958 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010959 numval = (long)tv_get_number_chk(varp, &error);
10960 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010961 if (!error && strval != NULL)
10962 set_option_value(varname, numval, strval, OPT_LOCAL);
10963
10964 /* reset notion of buffer */
10965 aucmd_restbuf(&aco);
10966 }
10967 else
10968 {
10969 buf_T *save_curbuf = curbuf;
10970
Bram Moolenaar964b3742019-05-24 18:54:09 +020010971 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010972 if (bufvarname != NULL)
10973 {
10974 curbuf = buf;
10975 STRCPY(bufvarname, "b:");
10976 STRCPY(bufvarname + 2, varname);
10977 set_var(bufvarname, varp, TRUE);
10978 vim_free(bufvarname);
10979 curbuf = save_curbuf;
10980 }
10981 }
10982 }
10983}
10984
10985 static void
10986f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10987{
10988 dict_T *d;
10989 dictitem_T *di;
10990 char_u *csearch;
10991
10992 if (argvars[0].v_type != VAR_DICT)
10993 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010994 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010995 return;
10996 }
10997
10998 if ((d = argvars[0].vval.v_dict) != NULL)
10999 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011000 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011001 if (csearch != NULL)
11002 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011003 if (enc_utf8)
11004 {
11005 int pcc[MAX_MCO];
11006 int c = utfc_ptr2char(csearch, pcc);
11007
11008 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11009 }
11010 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011011 set_last_csearch(PTR2CHAR(csearch),
11012 csearch, MB_PTR2LEN(csearch));
11013 }
11014
11015 di = dict_find(d, (char_u *)"forward", -1);
11016 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011017 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011018 ? FORWARD : BACKWARD);
11019
11020 di = dict_find(d, (char_u *)"until", -1);
11021 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011022 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011023 }
11024}
11025
11026/*
11027 * "setcmdpos()" function
11028 */
11029 static void
11030f_setcmdpos(typval_T *argvars, typval_T *rettv)
11031{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011032 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011033
11034 if (pos >= 0)
11035 rettv->vval.v_number = set_cmdline_pos(pos);
11036}
11037
11038/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011039 * "setenv()" function
11040 */
11041 static void
11042f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11043{
11044 char_u namebuf[NUMBUFLEN];
11045 char_u valbuf[NUMBUFLEN];
11046 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11047
11048 if (argvars[1].v_type == VAR_SPECIAL
11049 && argvars[1].vval.v_number == VVAL_NULL)
11050 vim_unsetenv(name);
11051 else
11052 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11053}
11054
11055/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011056 * "setfperm({fname}, {mode})" function
11057 */
11058 static void
11059f_setfperm(typval_T *argvars, typval_T *rettv)
11060{
11061 char_u *fname;
11062 char_u modebuf[NUMBUFLEN];
11063 char_u *mode_str;
11064 int i;
11065 int mask;
11066 int mode = 0;
11067
11068 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011069 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011070 if (fname == NULL)
11071 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011072 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011073 if (mode_str == NULL)
11074 return;
11075 if (STRLEN(mode_str) != 9)
11076 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011077 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011078 return;
11079 }
11080
11081 mask = 1;
11082 for (i = 8; i >= 0; --i)
11083 {
11084 if (mode_str[i] != '-')
11085 mode |= mask;
11086 mask = mask << 1;
11087 }
11088 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11089}
11090
11091/*
11092 * "setline()" function
11093 */
11094 static void
11095f_setline(typval_T *argvars, typval_T *rettv)
11096{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011097 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011098
Bram Moolenaarca851592018-06-06 21:04:07 +020011099 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011100}
11101
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011102/*
11103 * Used by "setqflist()" and "setloclist()" functions
11104 */
11105 static void
11106set_qf_ll_list(
11107 win_T *wp UNUSED,
11108 typval_T *list_arg UNUSED,
11109 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011110 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011111 typval_T *rettv)
11112{
11113#ifdef FEAT_QUICKFIX
11114 static char *e_invact = N_("E927: Invalid action: '%s'");
11115 char_u *act;
11116 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011117 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011118#endif
11119
11120 rettv->vval.v_number = -1;
11121
11122#ifdef FEAT_QUICKFIX
11123 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011124 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011125 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011126 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011127 else
11128 {
11129 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011130 dict_T *d = NULL;
11131 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011132
11133 if (action_arg->v_type == VAR_STRING)
11134 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011135 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011136 if (act == NULL)
11137 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011138 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11139 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011140 action = *act;
11141 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011142 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011143 }
11144 else if (action_arg->v_type == VAR_UNKNOWN)
11145 action = ' ';
11146 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011147 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011148
Bram Moolenaard823fa92016-08-12 16:29:27 +020011149 if (action_arg->v_type != VAR_UNKNOWN
11150 && what_arg->v_type != VAR_UNKNOWN)
11151 {
11152 if (what_arg->v_type == VAR_DICT)
11153 d = what_arg->vval.v_dict;
11154 else
11155 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011156 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011157 valid_dict = FALSE;
11158 }
11159 }
11160
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011161 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011162 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011163 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11164 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011165 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011166 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011167 }
11168#endif
11169}
11170
11171/*
11172 * "setloclist()" function
11173 */
11174 static void
11175f_setloclist(typval_T *argvars, typval_T *rettv)
11176{
11177 win_T *win;
11178
11179 rettv->vval.v_number = -1;
11180
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011181 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011182 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011183 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011184}
11185
11186/*
11187 * "setmatches()" function
11188 */
11189 static void
11190f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11191{
11192#ifdef FEAT_SEARCH_EXTRA
11193 list_T *l;
11194 listitem_T *li;
11195 dict_T *d;
11196 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011197 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011198
11199 rettv->vval.v_number = -1;
11200 if (argvars[0].v_type != VAR_LIST)
11201 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011202 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011203 return;
11204 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011205 if (win == NULL)
11206 return;
11207
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011208 if ((l = argvars[0].vval.v_list) != NULL)
11209 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011210 /* To some extent make sure that we are dealing with a list from
11211 * "getmatches()". */
11212 li = l->lv_first;
11213 while (li != NULL)
11214 {
11215 if (li->li_tv.v_type != VAR_DICT
11216 || (d = li->li_tv.vval.v_dict) == NULL)
11217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011218 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011219 return;
11220 }
11221 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11222 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11223 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11224 && dict_find(d, (char_u *)"priority", -1) != NULL
11225 && dict_find(d, (char_u *)"id", -1) != NULL))
11226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011227 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011228 return;
11229 }
11230 li = li->li_next;
11231 }
11232
Bram Moolenaaraff74912019-03-30 18:11:49 +010011233 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011234 li = l->lv_first;
11235 while (li != NULL)
11236 {
11237 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011238 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011239 dictitem_T *di;
11240 char_u *group;
11241 int priority;
11242 int id;
11243 char_u *conceal;
11244
11245 d = li->li_tv.vval.v_dict;
11246 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11247 {
11248 if (s == NULL)
11249 {
11250 s = list_alloc();
11251 if (s == NULL)
11252 return;
11253 }
11254
11255 /* match from matchaddpos() */
11256 for (i = 1; i < 9; i++)
11257 {
11258 sprintf((char *)buf, (char *)"pos%d", i);
11259 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11260 {
11261 if (di->di_tv.v_type != VAR_LIST)
11262 return;
11263
11264 list_append_tv(s, &di->di_tv);
11265 s->lv_refcount++;
11266 }
11267 else
11268 break;
11269 }
11270 }
11271
Bram Moolenaar8f667172018-12-14 15:38:31 +010011272 group = dict_get_string(d, (char_u *)"group", TRUE);
11273 priority = (int)dict_get_number(d, (char_u *)"priority");
11274 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011275 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011276 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011277 : NULL;
11278 if (i == 0)
11279 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011280 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011281 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011282 priority, id, NULL, conceal);
11283 }
11284 else
11285 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011286 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011287 list_unref(s);
11288 s = NULL;
11289 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011290 vim_free(group);
11291 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011292
11293 li = li->li_next;
11294 }
11295 rettv->vval.v_number = 0;
11296 }
11297#endif
11298}
11299
11300/*
11301 * "setpos()" function
11302 */
11303 static void
11304f_setpos(typval_T *argvars, typval_T *rettv)
11305{
11306 pos_T pos;
11307 int fnum;
11308 char_u *name;
11309 colnr_T curswant = -1;
11310
11311 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011312 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011313 if (name != NULL)
11314 {
11315 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11316 {
11317 if (--pos.col < 0)
11318 pos.col = 0;
11319 if (name[0] == '.' && name[1] == NUL)
11320 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011321 /* set cursor; "fnum" is ignored */
11322 curwin->w_cursor = pos;
11323 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011324 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011325 curwin->w_curswant = curswant - 1;
11326 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011327 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011328 check_cursor();
11329 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011330 }
11331 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11332 {
11333 /* set mark */
11334 if (setmark_pos(name[1], &pos, fnum) == OK)
11335 rettv->vval.v_number = 0;
11336 }
11337 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011338 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011339 }
11340 }
11341}
11342
11343/*
11344 * "setqflist()" function
11345 */
11346 static void
11347f_setqflist(typval_T *argvars, typval_T *rettv)
11348{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011349 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011350}
11351
11352/*
11353 * "setreg()" function
11354 */
11355 static void
11356f_setreg(typval_T *argvars, typval_T *rettv)
11357{
11358 int regname;
11359 char_u *strregname;
11360 char_u *stropt;
11361 char_u *strval;
11362 int append;
11363 char_u yank_type;
11364 long block_len;
11365
11366 block_len = -1;
11367 yank_type = MAUTO;
11368 append = FALSE;
11369
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011370 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011371 rettv->vval.v_number = 1; /* FAIL is default */
11372
11373 if (strregname == NULL)
11374 return; /* type error; errmsg already given */
11375 regname = *strregname;
11376 if (regname == 0 || regname == '@')
11377 regname = '"';
11378
11379 if (argvars[2].v_type != VAR_UNKNOWN)
11380 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011381 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011382 if (stropt == NULL)
11383 return; /* type error */
11384 for (; *stropt != NUL; ++stropt)
11385 switch (*stropt)
11386 {
11387 case 'a': case 'A': /* append */
11388 append = TRUE;
11389 break;
11390 case 'v': case 'c': /* character-wise selection */
11391 yank_type = MCHAR;
11392 break;
11393 case 'V': case 'l': /* line-wise selection */
11394 yank_type = MLINE;
11395 break;
11396 case 'b': case Ctrl_V: /* block-wise selection */
11397 yank_type = MBLOCK;
11398 if (VIM_ISDIGIT(stropt[1]))
11399 {
11400 ++stropt;
11401 block_len = getdigits(&stropt) - 1;
11402 --stropt;
11403 }
11404 break;
11405 }
11406 }
11407
11408 if (argvars[1].v_type == VAR_LIST)
11409 {
11410 char_u **lstval;
11411 char_u **allocval;
11412 char_u buf[NUMBUFLEN];
11413 char_u **curval;
11414 char_u **curallocval;
11415 list_T *ll = argvars[1].vval.v_list;
11416 listitem_T *li;
11417 int len;
11418
11419 /* If the list is NULL handle like an empty list. */
11420 len = ll == NULL ? 0 : ll->lv_len;
11421
11422 /* First half: use for pointers to result lines; second half: use for
11423 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011424 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011425 if (lstval == NULL)
11426 return;
11427 curval = lstval;
11428 allocval = lstval + len + 2;
11429 curallocval = allocval;
11430
11431 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11432 li = li->li_next)
11433 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011434 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011435 if (strval == NULL)
11436 goto free_lstval;
11437 if (strval == buf)
11438 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011439 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011440 * overwrite the string. */
11441 strval = vim_strsave(buf);
11442 if (strval == NULL)
11443 goto free_lstval;
11444 *curallocval++ = strval;
11445 }
11446 *curval++ = strval;
11447 }
11448 *curval++ = NULL;
11449
11450 write_reg_contents_lst(regname, lstval, -1,
11451 append, yank_type, block_len);
11452free_lstval:
11453 while (curallocval > allocval)
11454 vim_free(*--curallocval);
11455 vim_free(lstval);
11456 }
11457 else
11458 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011459 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011460 if (strval == NULL)
11461 return;
11462 write_reg_contents_ex(regname, strval, -1,
11463 append, yank_type, block_len);
11464 }
11465 rettv->vval.v_number = 0;
11466}
11467
11468/*
11469 * "settabvar()" function
11470 */
11471 static void
11472f_settabvar(typval_T *argvars, typval_T *rettv)
11473{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011474 tabpage_T *save_curtab;
11475 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011476 char_u *varname, *tabvarname;
11477 typval_T *varp;
11478
11479 rettv->vval.v_number = 0;
11480
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011481 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011482 return;
11483
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011484 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11485 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011486 varp = &argvars[2];
11487
Bram Moolenaar4033c552017-09-16 20:54:51 +020011488 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011489 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011490 save_curtab = curtab;
11491 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011492
Bram Moolenaar964b3742019-05-24 18:54:09 +020011493 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011494 if (tabvarname != NULL)
11495 {
11496 STRCPY(tabvarname, "t:");
11497 STRCPY(tabvarname + 2, varname);
11498 set_var(tabvarname, varp, TRUE);
11499 vim_free(tabvarname);
11500 }
11501
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011502 /* Restore current tabpage */
11503 if (valid_tabpage(save_curtab))
11504 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011505 }
11506}
11507
11508/*
11509 * "settabwinvar()" function
11510 */
11511 static void
11512f_settabwinvar(typval_T *argvars, typval_T *rettv)
11513{
11514 setwinvar(argvars, rettv, 1);
11515}
11516
11517/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011518 * "settagstack()" function
11519 */
11520 static void
11521f_settagstack(typval_T *argvars, typval_T *rettv)
11522{
11523 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11524 win_T *wp;
11525 dict_T *d;
11526 int action = 'r';
11527
11528 rettv->vval.v_number = -1;
11529
11530 // first argument: window number or id
11531 wp = find_win_by_nr_or_id(&argvars[0]);
11532 if (wp == NULL)
11533 return;
11534
11535 // second argument: dict with items to set in the tag stack
11536 if (argvars[1].v_type != VAR_DICT)
11537 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011538 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011539 return;
11540 }
11541 d = argvars[1].vval.v_dict;
11542 if (d == NULL)
11543 return;
11544
11545 // third argument: action - 'a' for append and 'r' for replace.
11546 // default is to replace the stack.
11547 if (argvars[2].v_type == VAR_UNKNOWN)
11548 action = 'r';
11549 else if (argvars[2].v_type == VAR_STRING)
11550 {
11551 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011552 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011553 if (actstr == NULL)
11554 return;
11555 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11556 action = *actstr;
11557 else
11558 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011559 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011560 return;
11561 }
11562 }
11563 else
11564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011565 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011566 return;
11567 }
11568
11569 if (set_tagstack(wp, d, action) == OK)
11570 rettv->vval.v_number = 0;
11571}
11572
11573/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011574 * "setwinvar()" function
11575 */
11576 static void
11577f_setwinvar(typval_T *argvars, typval_T *rettv)
11578{
11579 setwinvar(argvars, rettv, 0);
11580}
11581
11582#ifdef FEAT_CRYPT
11583/*
11584 * "sha256({string})" function
11585 */
11586 static void
11587f_sha256(typval_T *argvars, typval_T *rettv)
11588{
11589 char_u *p;
11590
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011591 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011592 rettv->vval.v_string = vim_strsave(
11593 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11594 rettv->v_type = VAR_STRING;
11595}
11596#endif /* FEAT_CRYPT */
11597
11598/*
11599 * "shellescape({string})" function
11600 */
11601 static void
11602f_shellescape(typval_T *argvars, typval_T *rettv)
11603{
Bram Moolenaar20615522017-06-05 18:46:26 +020011604 int do_special = non_zero_arg(&argvars[1]);
11605
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011606 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011607 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011608 rettv->v_type = VAR_STRING;
11609}
11610
11611/*
11612 * shiftwidth() function
11613 */
11614 static void
11615f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11616{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011617 rettv->vval.v_number = 0;
11618
11619 if (argvars[0].v_type != VAR_UNKNOWN)
11620 {
11621 long col;
11622
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011623 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011624 if (col < 0)
11625 return; // type error; errmsg already given
11626#ifdef FEAT_VARTABS
11627 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11628 return;
11629#endif
11630 }
11631
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011632 rettv->vval.v_number = get_sw_value(curbuf);
11633}
11634
11635/*
11636 * "simplify()" function
11637 */
11638 static void
11639f_simplify(typval_T *argvars, typval_T *rettv)
11640{
11641 char_u *p;
11642
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011643 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011644 rettv->vval.v_string = vim_strsave(p);
11645 simplify_filename(rettv->vval.v_string); /* simplify in place */
11646 rettv->v_type = VAR_STRING;
11647}
11648
11649#ifdef FEAT_FLOAT
11650/*
11651 * "sin()" function
11652 */
11653 static void
11654f_sin(typval_T *argvars, typval_T *rettv)
11655{
11656 float_T f = 0.0;
11657
11658 rettv->v_type = VAR_FLOAT;
11659 if (get_float_arg(argvars, &f) == OK)
11660 rettv->vval.v_float = sin(f);
11661 else
11662 rettv->vval.v_float = 0.0;
11663}
11664
11665/*
11666 * "sinh()" function
11667 */
11668 static void
11669f_sinh(typval_T *argvars, typval_T *rettv)
11670{
11671 float_T f = 0.0;
11672
11673 rettv->v_type = VAR_FLOAT;
11674 if (get_float_arg(argvars, &f) == OK)
11675 rettv->vval.v_float = sinh(f);
11676 else
11677 rettv->vval.v_float = 0.0;
11678}
11679#endif
11680
Bram Moolenaareae1b912019-05-09 15:12:55 +020011681static int item_compare(const void *s1, const void *s2);
11682static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011683
11684/* struct used in the array that's given to qsort() */
11685typedef struct
11686{
11687 listitem_T *item;
11688 int idx;
11689} sortItem_T;
11690
11691/* struct storing information about current sort */
11692typedef struct
11693{
11694 int item_compare_ic;
11695 int item_compare_numeric;
11696 int item_compare_numbers;
11697#ifdef FEAT_FLOAT
11698 int item_compare_float;
11699#endif
11700 char_u *item_compare_func;
11701 partial_T *item_compare_partial;
11702 dict_T *item_compare_selfdict;
11703 int item_compare_func_err;
11704 int item_compare_keep_zero;
11705} sortinfo_T;
11706static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011707#define ITEM_COMPARE_FAIL 999
11708
11709/*
11710 * Compare functions for f_sort() and f_uniq() below.
11711 */
11712 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011713item_compare(const void *s1, const void *s2)
11714{
11715 sortItem_T *si1, *si2;
11716 typval_T *tv1, *tv2;
11717 char_u *p1, *p2;
11718 char_u *tofree1 = NULL, *tofree2 = NULL;
11719 int res;
11720 char_u numbuf1[NUMBUFLEN];
11721 char_u numbuf2[NUMBUFLEN];
11722
11723 si1 = (sortItem_T *)s1;
11724 si2 = (sortItem_T *)s2;
11725 tv1 = &si1->item->li_tv;
11726 tv2 = &si2->item->li_tv;
11727
11728 if (sortinfo->item_compare_numbers)
11729 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011730 varnumber_T v1 = tv_get_number(tv1);
11731 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011732
11733 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11734 }
11735
11736#ifdef FEAT_FLOAT
11737 if (sortinfo->item_compare_float)
11738 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011739 float_T v1 = tv_get_float(tv1);
11740 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011741
11742 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11743 }
11744#endif
11745
11746 /* tv2string() puts quotes around a string and allocates memory. Don't do
11747 * that for string variables. Use a single quote when comparing with a
11748 * non-string to do what the docs promise. */
11749 if (tv1->v_type == VAR_STRING)
11750 {
11751 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11752 p1 = (char_u *)"'";
11753 else
11754 p1 = tv1->vval.v_string;
11755 }
11756 else
11757 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11758 if (tv2->v_type == VAR_STRING)
11759 {
11760 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11761 p2 = (char_u *)"'";
11762 else
11763 p2 = tv2->vval.v_string;
11764 }
11765 else
11766 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11767 if (p1 == NULL)
11768 p1 = (char_u *)"";
11769 if (p2 == NULL)
11770 p2 = (char_u *)"";
11771 if (!sortinfo->item_compare_numeric)
11772 {
11773 if (sortinfo->item_compare_ic)
11774 res = STRICMP(p1, p2);
11775 else
11776 res = STRCMP(p1, p2);
11777 }
11778 else
11779 {
11780 double n1, n2;
11781 n1 = strtod((char *)p1, (char **)&p1);
11782 n2 = strtod((char *)p2, (char **)&p2);
11783 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11784 }
11785
11786 /* When the result would be zero, compare the item indexes. Makes the
11787 * sort stable. */
11788 if (res == 0 && !sortinfo->item_compare_keep_zero)
11789 res = si1->idx > si2->idx ? 1 : -1;
11790
11791 vim_free(tofree1);
11792 vim_free(tofree2);
11793 return res;
11794}
11795
11796 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011797item_compare2(const void *s1, const void *s2)
11798{
11799 sortItem_T *si1, *si2;
11800 int res;
11801 typval_T rettv;
11802 typval_T argv[3];
11803 int dummy;
11804 char_u *func_name;
11805 partial_T *partial = sortinfo->item_compare_partial;
11806
11807 /* shortcut after failure in previous call; compare all items equal */
11808 if (sortinfo->item_compare_func_err)
11809 return 0;
11810
11811 si1 = (sortItem_T *)s1;
11812 si2 = (sortItem_T *)s2;
11813
11814 if (partial == NULL)
11815 func_name = sortinfo->item_compare_func;
11816 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011817 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011818
11819 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11820 * in the copy without changing the original list items. */
11821 copy_tv(&si1->item->li_tv, &argv[0]);
11822 copy_tv(&si2->item->li_tv, &argv[1]);
11823
11824 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020011825 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011826 partial, sortinfo->item_compare_selfdict);
11827 clear_tv(&argv[0]);
11828 clear_tv(&argv[1]);
11829
11830 if (res == FAIL)
11831 res = ITEM_COMPARE_FAIL;
11832 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011833 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011834 if (sortinfo->item_compare_func_err)
11835 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11836 clear_tv(&rettv);
11837
11838 /* When the result would be zero, compare the pointers themselves. Makes
11839 * the sort stable. */
11840 if (res == 0 && !sortinfo->item_compare_keep_zero)
11841 res = si1->idx > si2->idx ? 1 : -1;
11842
11843 return res;
11844}
11845
11846/*
11847 * "sort({list})" function
11848 */
11849 static void
11850do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11851{
11852 list_T *l;
11853 listitem_T *li;
11854 sortItem_T *ptrs;
11855 sortinfo_T *old_sortinfo;
11856 sortinfo_T info;
11857 long len;
11858 long i;
11859
11860 /* Pointer to current info struct used in compare function. Save and
11861 * restore the current one for nested calls. */
11862 old_sortinfo = sortinfo;
11863 sortinfo = &info;
11864
11865 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011866 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011867 else
11868 {
11869 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011870 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011871 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11872 TRUE))
11873 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011874 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011875
11876 len = list_len(l);
11877 if (len <= 1)
11878 goto theend; /* short list sorts pretty quickly */
11879
11880 info.item_compare_ic = FALSE;
11881 info.item_compare_numeric = FALSE;
11882 info.item_compare_numbers = FALSE;
11883#ifdef FEAT_FLOAT
11884 info.item_compare_float = FALSE;
11885#endif
11886 info.item_compare_func = NULL;
11887 info.item_compare_partial = NULL;
11888 info.item_compare_selfdict = NULL;
11889 if (argvars[1].v_type != VAR_UNKNOWN)
11890 {
11891 /* optional second argument: {func} */
11892 if (argvars[1].v_type == VAR_FUNC)
11893 info.item_compare_func = argvars[1].vval.v_string;
11894 else if (argvars[1].v_type == VAR_PARTIAL)
11895 info.item_compare_partial = argvars[1].vval.v_partial;
11896 else
11897 {
11898 int error = FALSE;
11899
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011900 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011901 if (error)
11902 goto theend; /* type error; errmsg already given */
11903 if (i == 1)
11904 info.item_compare_ic = TRUE;
11905 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011906 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011907 else if (i != 0)
11908 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011909 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011910 goto theend;
11911 }
11912 if (info.item_compare_func != NULL)
11913 {
11914 if (*info.item_compare_func == NUL)
11915 {
11916 /* empty string means default sort */
11917 info.item_compare_func = NULL;
11918 }
11919 else if (STRCMP(info.item_compare_func, "n") == 0)
11920 {
11921 info.item_compare_func = NULL;
11922 info.item_compare_numeric = TRUE;
11923 }
11924 else if (STRCMP(info.item_compare_func, "N") == 0)
11925 {
11926 info.item_compare_func = NULL;
11927 info.item_compare_numbers = TRUE;
11928 }
11929#ifdef FEAT_FLOAT
11930 else if (STRCMP(info.item_compare_func, "f") == 0)
11931 {
11932 info.item_compare_func = NULL;
11933 info.item_compare_float = TRUE;
11934 }
11935#endif
11936 else if (STRCMP(info.item_compare_func, "i") == 0)
11937 {
11938 info.item_compare_func = NULL;
11939 info.item_compare_ic = TRUE;
11940 }
11941 }
11942 }
11943
11944 if (argvars[2].v_type != VAR_UNKNOWN)
11945 {
11946 /* optional third argument: {dict} */
11947 if (argvars[2].v_type != VAR_DICT)
11948 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011949 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011950 goto theend;
11951 }
11952 info.item_compare_selfdict = argvars[2].vval.v_dict;
11953 }
11954 }
11955
11956 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011957 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011958 if (ptrs == NULL)
11959 goto theend;
11960
11961 i = 0;
11962 if (sort)
11963 {
11964 /* sort(): ptrs will be the list to sort */
11965 for (li = l->lv_first; li != NULL; li = li->li_next)
11966 {
11967 ptrs[i].item = li;
11968 ptrs[i].idx = i;
11969 ++i;
11970 }
11971
11972 info.item_compare_func_err = FALSE;
11973 info.item_compare_keep_zero = FALSE;
11974 /* test the compare function */
11975 if ((info.item_compare_func != NULL
11976 || info.item_compare_partial != NULL)
11977 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11978 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011979 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011980 else
11981 {
11982 /* Sort the array with item pointers. */
11983 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11984 info.item_compare_func == NULL
11985 && info.item_compare_partial == NULL
11986 ? item_compare : item_compare2);
11987
11988 if (!info.item_compare_func_err)
11989 {
11990 /* Clear the List and append the items in sorted order. */
11991 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11992 l->lv_len = 0;
11993 for (i = 0; i < len; ++i)
11994 list_append(l, ptrs[i].item);
11995 }
11996 }
11997 }
11998 else
11999 {
12000 int (*item_compare_func_ptr)(const void *, const void *);
12001
12002 /* f_uniq(): ptrs will be a stack of items to remove */
12003 info.item_compare_func_err = FALSE;
12004 info.item_compare_keep_zero = TRUE;
12005 item_compare_func_ptr = info.item_compare_func != NULL
12006 || info.item_compare_partial != NULL
12007 ? item_compare2 : item_compare;
12008
12009 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12010 li = li->li_next)
12011 {
12012 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12013 == 0)
12014 ptrs[i++].item = li;
12015 if (info.item_compare_func_err)
12016 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012017 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012018 break;
12019 }
12020 }
12021
12022 if (!info.item_compare_func_err)
12023 {
12024 while (--i >= 0)
12025 {
12026 li = ptrs[i].item->li_next;
12027 ptrs[i].item->li_next = li->li_next;
12028 if (li->li_next != NULL)
12029 li->li_next->li_prev = ptrs[i].item;
12030 else
12031 l->lv_last = ptrs[i].item;
12032 list_fix_watch(l, li);
12033 listitem_free(li);
12034 l->lv_len--;
12035 }
12036 }
12037 }
12038
12039 vim_free(ptrs);
12040 }
12041theend:
12042 sortinfo = old_sortinfo;
12043}
12044
12045/*
12046 * "sort({list})" function
12047 */
12048 static void
12049f_sort(typval_T *argvars, typval_T *rettv)
12050{
12051 do_sort_uniq(argvars, rettv, TRUE);
12052}
12053
12054/*
12055 * "uniq({list})" function
12056 */
12057 static void
12058f_uniq(typval_T *argvars, typval_T *rettv)
12059{
12060 do_sort_uniq(argvars, rettv, FALSE);
12061}
12062
12063/*
12064 * "soundfold({word})" function
12065 */
12066 static void
12067f_soundfold(typval_T *argvars, typval_T *rettv)
12068{
12069 char_u *s;
12070
12071 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012072 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012073#ifdef FEAT_SPELL
12074 rettv->vval.v_string = eval_soundfold(s);
12075#else
12076 rettv->vval.v_string = vim_strsave(s);
12077#endif
12078}
12079
12080/*
12081 * "spellbadword()" function
12082 */
12083 static void
12084f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12085{
12086 char_u *word = (char_u *)"";
12087 hlf_T attr = HLF_COUNT;
12088 int len = 0;
12089
12090 if (rettv_list_alloc(rettv) == FAIL)
12091 return;
12092
12093#ifdef FEAT_SPELL
12094 if (argvars[0].v_type == VAR_UNKNOWN)
12095 {
12096 /* Find the start and length of the badly spelled word. */
12097 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12098 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012099 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012100 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012101 curwin->w_set_curswant = TRUE;
12102 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012103 }
12104 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12105 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012106 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012107 int capcol = -1;
12108
12109 if (str != NULL)
12110 {
12111 /* Check the argument for spelling. */
12112 while (*str != NUL)
12113 {
12114 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12115 if (attr != HLF_COUNT)
12116 {
12117 word = str;
12118 break;
12119 }
12120 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012121 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012122 }
12123 }
12124 }
12125#endif
12126
12127 list_append_string(rettv->vval.v_list, word, len);
12128 list_append_string(rettv->vval.v_list, (char_u *)(
12129 attr == HLF_SPB ? "bad" :
12130 attr == HLF_SPR ? "rare" :
12131 attr == HLF_SPL ? "local" :
12132 attr == HLF_SPC ? "caps" :
12133 ""), -1);
12134}
12135
12136/*
12137 * "spellsuggest()" function
12138 */
12139 static void
12140f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12141{
12142#ifdef FEAT_SPELL
12143 char_u *str;
12144 int typeerr = FALSE;
12145 int maxcount;
12146 garray_T ga;
12147 int i;
12148 listitem_T *li;
12149 int need_capital = FALSE;
12150#endif
12151
12152 if (rettv_list_alloc(rettv) == FAIL)
12153 return;
12154
12155#ifdef FEAT_SPELL
12156 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12157 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012158 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012159 if (argvars[1].v_type != VAR_UNKNOWN)
12160 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012161 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012162 if (maxcount <= 0)
12163 return;
12164 if (argvars[2].v_type != VAR_UNKNOWN)
12165 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012166 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012167 if (typeerr)
12168 return;
12169 }
12170 }
12171 else
12172 maxcount = 25;
12173
12174 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12175
12176 for (i = 0; i < ga.ga_len; ++i)
12177 {
12178 str = ((char_u **)ga.ga_data)[i];
12179
12180 li = listitem_alloc();
12181 if (li == NULL)
12182 vim_free(str);
12183 else
12184 {
12185 li->li_tv.v_type = VAR_STRING;
12186 li->li_tv.v_lock = 0;
12187 li->li_tv.vval.v_string = str;
12188 list_append(rettv->vval.v_list, li);
12189 }
12190 }
12191 ga_clear(&ga);
12192 }
12193#endif
12194}
12195
12196 static void
12197f_split(typval_T *argvars, typval_T *rettv)
12198{
12199 char_u *str;
12200 char_u *end;
12201 char_u *pat = NULL;
12202 regmatch_T regmatch;
12203 char_u patbuf[NUMBUFLEN];
12204 char_u *save_cpo;
12205 int match;
12206 colnr_T col = 0;
12207 int keepempty = FALSE;
12208 int typeerr = FALSE;
12209
12210 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12211 save_cpo = p_cpo;
12212 p_cpo = (char_u *)"";
12213
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012214 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012215 if (argvars[1].v_type != VAR_UNKNOWN)
12216 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012217 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012218 if (pat == NULL)
12219 typeerr = TRUE;
12220 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012221 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012222 }
12223 if (pat == NULL || *pat == NUL)
12224 pat = (char_u *)"[\\x01- ]\\+";
12225
12226 if (rettv_list_alloc(rettv) == FAIL)
12227 return;
12228 if (typeerr)
12229 return;
12230
12231 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12232 if (regmatch.regprog != NULL)
12233 {
12234 regmatch.rm_ic = FALSE;
12235 while (*str != NUL || keepempty)
12236 {
12237 if (*str == NUL)
12238 match = FALSE; /* empty item at the end */
12239 else
12240 match = vim_regexec_nl(&regmatch, str, col);
12241 if (match)
12242 end = regmatch.startp[0];
12243 else
12244 end = str + STRLEN(str);
12245 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12246 && *str != NUL && match && end < regmatch.endp[0]))
12247 {
12248 if (list_append_string(rettv->vval.v_list, str,
12249 (int)(end - str)) == FAIL)
12250 break;
12251 }
12252 if (!match)
12253 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012254 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012255 if (regmatch.endp[0] > str)
12256 col = 0;
12257 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012258 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012259 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012260 str = regmatch.endp[0];
12261 }
12262
12263 vim_regfree(regmatch.regprog);
12264 }
12265
12266 p_cpo = save_cpo;
12267}
12268
12269#ifdef FEAT_FLOAT
12270/*
12271 * "sqrt()" function
12272 */
12273 static void
12274f_sqrt(typval_T *argvars, typval_T *rettv)
12275{
12276 float_T f = 0.0;
12277
12278 rettv->v_type = VAR_FLOAT;
12279 if (get_float_arg(argvars, &f) == OK)
12280 rettv->vval.v_float = sqrt(f);
12281 else
12282 rettv->vval.v_float = 0.0;
12283}
12284
12285/*
12286 * "str2float()" function
12287 */
12288 static void
12289f_str2float(typval_T *argvars, typval_T *rettv)
12290{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012291 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012292 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012293
Bram Moolenaar08243d22017-01-10 16:12:29 +010012294 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012295 p = skipwhite(p + 1);
12296 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012297 if (isneg)
12298 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012299 rettv->v_type = VAR_FLOAT;
12300}
12301#endif
12302
12303/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012304 * "str2list()" function
12305 */
12306 static void
12307f_str2list(typval_T *argvars, typval_T *rettv)
12308{
12309 char_u *p;
12310 int utf8 = FALSE;
12311
12312 if (rettv_list_alloc(rettv) == FAIL)
12313 return;
12314
12315 if (argvars[1].v_type != VAR_UNKNOWN)
12316 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12317
12318 p = tv_get_string(&argvars[0]);
12319
12320 if (has_mbyte || utf8)
12321 {
12322 int (*ptr2len)(char_u *);
12323 int (*ptr2char)(char_u *);
12324
12325 if (utf8 || enc_utf8)
12326 {
12327 ptr2len = utf_ptr2len;
12328 ptr2char = utf_ptr2char;
12329 }
12330 else
12331 {
12332 ptr2len = mb_ptr2len;
12333 ptr2char = mb_ptr2char;
12334 }
12335
12336 for ( ; *p != NUL; p += (*ptr2len)(p))
12337 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
12338 }
12339 else
12340 for ( ; *p != NUL; ++p)
12341 list_append_number(rettv->vval.v_list, *p);
12342}
12343
12344/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012345 * "str2nr()" function
12346 */
12347 static void
12348f_str2nr(typval_T *argvars, typval_T *rettv)
12349{
12350 int base = 10;
12351 char_u *p;
12352 varnumber_T n;
12353 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012354 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012355
12356 if (argvars[1].v_type != VAR_UNKNOWN)
12357 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012358 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012359 if (base != 2 && base != 8 && base != 10 && base != 16)
12360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012361 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012362 return;
12363 }
12364 }
12365
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012366 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012367 isneg = (*p == '-');
12368 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012369 p = skipwhite(p + 1);
12370 switch (base)
12371 {
12372 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12373 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12374 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12375 default: what = 0;
12376 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020012377 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
12378 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010012379 if (isneg)
12380 rettv->vval.v_number = -n;
12381 else
12382 rettv->vval.v_number = n;
12383
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012384}
12385
12386#ifdef HAVE_STRFTIME
12387/*
12388 * "strftime({format}[, {time}])" function
12389 */
12390 static void
12391f_strftime(typval_T *argvars, typval_T *rettv)
12392{
12393 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020012394 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012395 struct tm *curtime;
12396 time_t seconds;
12397 char_u *p;
12398
12399 rettv->v_type = VAR_STRING;
12400
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012401 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012402 if (argvars[1].v_type == VAR_UNKNOWN)
12403 seconds = time(NULL);
12404 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012405 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020012406 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012407 /* MSVC returns NULL for an invalid value of seconds. */
12408 if (curtime == NULL)
12409 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12410 else
12411 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012412 vimconv_T conv;
12413 char_u *enc;
12414
12415 conv.vc_type = CONV_NONE;
12416 enc = enc_locale();
12417 convert_setup(&conv, p_enc, enc);
12418 if (conv.vc_type != CONV_NONE)
12419 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012420 if (p != NULL)
12421 (void)strftime((char *)result_buf, sizeof(result_buf),
12422 (char *)p, curtime);
12423 else
12424 result_buf[0] = NUL;
12425
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012426 if (conv.vc_type != CONV_NONE)
12427 vim_free(p);
12428 convert_setup(&conv, enc, p_enc);
12429 if (conv.vc_type != CONV_NONE)
12430 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12431 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012432 rettv->vval.v_string = vim_strsave(result_buf);
12433
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012434 /* Release conversion descriptors */
12435 convert_setup(&conv, NULL, NULL);
12436 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012437 }
12438}
12439#endif
12440
12441/*
12442 * "strgetchar()" function
12443 */
12444 static void
12445f_strgetchar(typval_T *argvars, typval_T *rettv)
12446{
12447 char_u *str;
12448 int len;
12449 int error = FALSE;
12450 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012451 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012452
12453 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012454 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012455 if (str == NULL)
12456 return;
12457 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012458 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012459 if (error)
12460 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012461
Bram Moolenaar13505972019-01-24 15:04:48 +010012462 while (charidx >= 0 && byteidx < len)
12463 {
12464 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012465 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012466 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12467 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012468 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012469 --charidx;
12470 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012471 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012472}
12473
12474/*
12475 * "stridx()" function
12476 */
12477 static void
12478f_stridx(typval_T *argvars, typval_T *rettv)
12479{
12480 char_u buf[NUMBUFLEN];
12481 char_u *needle;
12482 char_u *haystack;
12483 char_u *save_haystack;
12484 char_u *pos;
12485 int start_idx;
12486
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012487 needle = tv_get_string_chk(&argvars[1]);
12488 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012489 rettv->vval.v_number = -1;
12490 if (needle == NULL || haystack == NULL)
12491 return; /* type error; errmsg already given */
12492
12493 if (argvars[2].v_type != VAR_UNKNOWN)
12494 {
12495 int error = FALSE;
12496
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012497 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012498 if (error || start_idx >= (int)STRLEN(haystack))
12499 return;
12500 if (start_idx >= 0)
12501 haystack += start_idx;
12502 }
12503
12504 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12505 if (pos != NULL)
12506 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12507}
12508
12509/*
12510 * "string()" function
12511 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012512 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012513f_string(typval_T *argvars, typval_T *rettv)
12514{
12515 char_u *tofree;
12516 char_u numbuf[NUMBUFLEN];
12517
12518 rettv->v_type = VAR_STRING;
12519 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12520 get_copyID());
12521 /* Make a copy if we have a value but it's not in allocated memory. */
12522 if (rettv->vval.v_string != NULL && tofree == NULL)
12523 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12524}
12525
12526/*
12527 * "strlen()" function
12528 */
12529 static void
12530f_strlen(typval_T *argvars, typval_T *rettv)
12531{
12532 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012533 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012534}
12535
12536/*
12537 * "strchars()" function
12538 */
12539 static void
12540f_strchars(typval_T *argvars, typval_T *rettv)
12541{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012542 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012543 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012544 varnumber_T len = 0;
12545 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012546
12547 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012548 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012549 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012550 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012551 else
12552 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012553 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12554 while (*s != NUL)
12555 {
12556 func_mb_ptr2char_adv(&s);
12557 ++len;
12558 }
12559 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012560 }
12561}
12562
12563/*
12564 * "strdisplaywidth()" function
12565 */
12566 static void
12567f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12568{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012569 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012570 int col = 0;
12571
12572 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012573 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012574
12575 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12576}
12577
12578/*
12579 * "strwidth()" function
12580 */
12581 static void
12582f_strwidth(typval_T *argvars, typval_T *rettv)
12583{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012584 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012585
Bram Moolenaar13505972019-01-24 15:04:48 +010012586 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012587}
12588
12589/*
12590 * "strcharpart()" function
12591 */
12592 static void
12593f_strcharpart(typval_T *argvars, typval_T *rettv)
12594{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012595 char_u *p;
12596 int nchar;
12597 int nbyte = 0;
12598 int charlen;
12599 int len = 0;
12600 int slen;
12601 int error = FALSE;
12602
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012603 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012604 slen = (int)STRLEN(p);
12605
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012606 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012607 if (!error)
12608 {
12609 if (nchar > 0)
12610 while (nchar > 0 && nbyte < slen)
12611 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012612 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012613 --nchar;
12614 }
12615 else
12616 nbyte = nchar;
12617 if (argvars[2].v_type != VAR_UNKNOWN)
12618 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012619 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012620 while (charlen > 0 && nbyte + len < slen)
12621 {
12622 int off = nbyte + len;
12623
12624 if (off < 0)
12625 len += 1;
12626 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012627 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012628 --charlen;
12629 }
12630 }
12631 else
12632 len = slen - nbyte; /* default: all bytes that are available. */
12633 }
12634
12635 /*
12636 * Only return the overlap between the specified part and the actual
12637 * string.
12638 */
12639 if (nbyte < 0)
12640 {
12641 len += nbyte;
12642 nbyte = 0;
12643 }
12644 else if (nbyte > slen)
12645 nbyte = slen;
12646 if (len < 0)
12647 len = 0;
12648 else if (nbyte + len > slen)
12649 len = slen - nbyte;
12650
12651 rettv->v_type = VAR_STRING;
12652 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012653}
12654
12655/*
12656 * "strpart()" function
12657 */
12658 static void
12659f_strpart(typval_T *argvars, typval_T *rettv)
12660{
12661 char_u *p;
12662 int n;
12663 int len;
12664 int slen;
12665 int error = FALSE;
12666
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012667 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012668 slen = (int)STRLEN(p);
12669
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012670 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012671 if (error)
12672 len = 0;
12673 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012674 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012675 else
12676 len = slen - n; /* default len: all bytes that are available. */
12677
12678 /*
12679 * Only return the overlap between the specified part and the actual
12680 * string.
12681 */
12682 if (n < 0)
12683 {
12684 len += n;
12685 n = 0;
12686 }
12687 else if (n > slen)
12688 n = slen;
12689 if (len < 0)
12690 len = 0;
12691 else if (n + len > slen)
12692 len = slen - n;
12693
12694 rettv->v_type = VAR_STRING;
12695 rettv->vval.v_string = vim_strnsave(p + n, len);
12696}
12697
12698/*
12699 * "strridx()" function
12700 */
12701 static void
12702f_strridx(typval_T *argvars, typval_T *rettv)
12703{
12704 char_u buf[NUMBUFLEN];
12705 char_u *needle;
12706 char_u *haystack;
12707 char_u *rest;
12708 char_u *lastmatch = NULL;
12709 int haystack_len, end_idx;
12710
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012711 needle = tv_get_string_chk(&argvars[1]);
12712 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012713
12714 rettv->vval.v_number = -1;
12715 if (needle == NULL || haystack == NULL)
12716 return; /* type error; errmsg already given */
12717
12718 haystack_len = (int)STRLEN(haystack);
12719 if (argvars[2].v_type != VAR_UNKNOWN)
12720 {
12721 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012722 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012723 if (end_idx < 0)
12724 return; /* can never find a match */
12725 }
12726 else
12727 end_idx = haystack_len;
12728
12729 if (*needle == NUL)
12730 {
12731 /* Empty string matches past the end. */
12732 lastmatch = haystack + end_idx;
12733 }
12734 else
12735 {
12736 for (rest = haystack; *rest != '\0'; ++rest)
12737 {
12738 rest = (char_u *)strstr((char *)rest, (char *)needle);
12739 if (rest == NULL || rest > haystack + end_idx)
12740 break;
12741 lastmatch = rest;
12742 }
12743 }
12744
12745 if (lastmatch == NULL)
12746 rettv->vval.v_number = -1;
12747 else
12748 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12749}
12750
12751/*
12752 * "strtrans()" function
12753 */
12754 static void
12755f_strtrans(typval_T *argvars, typval_T *rettv)
12756{
12757 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012758 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012759}
12760
12761/*
12762 * "submatch()" function
12763 */
12764 static void
12765f_submatch(typval_T *argvars, typval_T *rettv)
12766{
12767 int error = FALSE;
12768 int no;
12769 int retList = 0;
12770
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012771 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012772 if (error)
12773 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012774 if (no < 0 || no >= NSUBEXP)
12775 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012776 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012777 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012778 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012779 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012780 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012781 if (error)
12782 return;
12783
12784 if (retList == 0)
12785 {
12786 rettv->v_type = VAR_STRING;
12787 rettv->vval.v_string = reg_submatch(no);
12788 }
12789 else
12790 {
12791 rettv->v_type = VAR_LIST;
12792 rettv->vval.v_list = reg_submatch_list(no);
12793 }
12794}
12795
12796/*
12797 * "substitute()" function
12798 */
12799 static void
12800f_substitute(typval_T *argvars, typval_T *rettv)
12801{
12802 char_u patbuf[NUMBUFLEN];
12803 char_u subbuf[NUMBUFLEN];
12804 char_u flagsbuf[NUMBUFLEN];
12805
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012806 char_u *str = tv_get_string_chk(&argvars[0]);
12807 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012808 char_u *sub = NULL;
12809 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012810 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012811
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012812 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12813 expr = &argvars[2];
12814 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012815 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012816
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012817 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012818 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12819 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012820 rettv->vval.v_string = NULL;
12821 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012822 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012823}
12824
12825/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012826 * "swapinfo(swap_filename)" function
12827 */
12828 static void
12829f_swapinfo(typval_T *argvars, typval_T *rettv)
12830{
12831 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012832 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012833}
12834
12835/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012836 * "swapname(expr)" function
12837 */
12838 static void
12839f_swapname(typval_T *argvars, typval_T *rettv)
12840{
12841 buf_T *buf;
12842
12843 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010012844 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020012845 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12846 || buf->b_ml.ml_mfp->mf_fname == NULL)
12847 rettv->vval.v_string = NULL;
12848 else
12849 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12850}
12851
12852/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012853 * "synID(lnum, col, trans)" function
12854 */
12855 static void
12856f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12857{
12858 int id = 0;
12859#ifdef FEAT_SYN_HL
12860 linenr_T lnum;
12861 colnr_T col;
12862 int trans;
12863 int transerr = FALSE;
12864
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012865 lnum = tv_get_lnum(argvars); /* -1 on type error */
12866 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12867 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012868
12869 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12870 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12871 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12872#endif
12873
12874 rettv->vval.v_number = id;
12875}
12876
12877/*
12878 * "synIDattr(id, what [, mode])" function
12879 */
12880 static void
12881f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12882{
12883 char_u *p = NULL;
12884#ifdef FEAT_SYN_HL
12885 int id;
12886 char_u *what;
12887 char_u *mode;
12888 char_u modebuf[NUMBUFLEN];
12889 int modec;
12890
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012891 id = (int)tv_get_number(&argvars[0]);
12892 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012893 if (argvars[2].v_type != VAR_UNKNOWN)
12894 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012895 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012896 modec = TOLOWER_ASC(mode[0]);
12897 if (modec != 't' && modec != 'c' && modec != 'g')
12898 modec = 0; /* replace invalid with current */
12899 }
12900 else
12901 {
12902#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12903 if (USE_24BIT)
12904 modec = 'g';
12905 else
12906#endif
12907 if (t_colors > 1)
12908 modec = 'c';
12909 else
12910 modec = 't';
12911 }
12912
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012913 switch (TOLOWER_ASC(what[0]))
12914 {
12915 case 'b':
12916 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12917 p = highlight_color(id, what, modec);
12918 else /* bold */
12919 p = highlight_has_attr(id, HL_BOLD, modec);
12920 break;
12921
12922 case 'f': /* fg[#] or font */
12923 p = highlight_color(id, what, modec);
12924 break;
12925
12926 case 'i':
12927 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12928 p = highlight_has_attr(id, HL_INVERSE, modec);
12929 else /* italic */
12930 p = highlight_has_attr(id, HL_ITALIC, modec);
12931 break;
12932
12933 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012934 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012935 break;
12936
12937 case 'r': /* reverse */
12938 p = highlight_has_attr(id, HL_INVERSE, modec);
12939 break;
12940
12941 case 's':
12942 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12943 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012944 /* strikeout */
12945 else if (TOLOWER_ASC(what[1]) == 't' &&
12946 TOLOWER_ASC(what[2]) == 'r')
12947 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012948 else /* standout */
12949 p = highlight_has_attr(id, HL_STANDOUT, modec);
12950 break;
12951
12952 case 'u':
12953 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12954 /* underline */
12955 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12956 else
12957 /* undercurl */
12958 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12959 break;
12960 }
12961
12962 if (p != NULL)
12963 p = vim_strsave(p);
12964#endif
12965 rettv->v_type = VAR_STRING;
12966 rettv->vval.v_string = p;
12967}
12968
12969/*
12970 * "synIDtrans(id)" function
12971 */
12972 static void
12973f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12974{
12975 int id;
12976
12977#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012978 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012979
12980 if (id > 0)
12981 id = syn_get_final_id(id);
12982 else
12983#endif
12984 id = 0;
12985
12986 rettv->vval.v_number = id;
12987}
12988
12989/*
12990 * "synconcealed(lnum, col)" function
12991 */
12992 static void
12993f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12994{
12995#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12996 linenr_T lnum;
12997 colnr_T col;
12998 int syntax_flags = 0;
12999 int cchar;
13000 int matchid = 0;
13001 char_u str[NUMBUFLEN];
13002#endif
13003
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013004 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013005
13006#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013007 lnum = tv_get_lnum(argvars); /* -1 on type error */
13008 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013009
13010 vim_memset(str, NUL, sizeof(str));
13011
13012 if (rettv_list_alloc(rettv) != FAIL)
13013 {
13014 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13015 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13016 && curwin->w_p_cole > 0)
13017 {
13018 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13019 syntax_flags = get_syntax_info(&matchid);
13020
13021 /* get the conceal character */
13022 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13023 {
13024 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013025 if (cchar == NUL && curwin->w_p_cole == 1)
13026 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013027 if (cchar != NUL)
13028 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013029 if (has_mbyte)
13030 (*mb_char2bytes)(cchar, str);
13031 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013032 str[0] = cchar;
13033 }
13034 }
13035 }
13036
13037 list_append_number(rettv->vval.v_list,
13038 (syntax_flags & HL_CONCEAL) != 0);
13039 /* -1 to auto-determine strlen */
13040 list_append_string(rettv->vval.v_list, str, -1);
13041 list_append_number(rettv->vval.v_list, matchid);
13042 }
13043#endif
13044}
13045
13046/*
13047 * "synstack(lnum, col)" function
13048 */
13049 static void
13050f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13051{
13052#ifdef FEAT_SYN_HL
13053 linenr_T lnum;
13054 colnr_T col;
13055 int i;
13056 int id;
13057#endif
13058
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013059 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013060
13061#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013062 lnum = tv_get_lnum(argvars); /* -1 on type error */
13063 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013064
13065 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13066 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13067 && rettv_list_alloc(rettv) != FAIL)
13068 {
13069 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13070 for (i = 0; ; ++i)
13071 {
13072 id = syn_get_stack_item(i);
13073 if (id < 0)
13074 break;
13075 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13076 break;
13077 }
13078 }
13079#endif
13080}
13081
13082 static void
13083get_cmd_output_as_rettv(
13084 typval_T *argvars,
13085 typval_T *rettv,
13086 int retlist)
13087{
13088 char_u *res = NULL;
13089 char_u *p;
13090 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013091 int err = FALSE;
13092 FILE *fd;
13093 list_T *list = NULL;
13094 int flags = SHELL_SILENT;
13095
13096 rettv->v_type = VAR_STRING;
13097 rettv->vval.v_string = NULL;
13098 if (check_restricted() || check_secure())
13099 goto errret;
13100
13101 if (argvars[1].v_type != VAR_UNKNOWN)
13102 {
13103 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013104 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013105 * command.
13106 */
13107 if ((infile = vim_tempname('i', TRUE)) == NULL)
13108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013109 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013110 goto errret;
13111 }
13112
13113 fd = mch_fopen((char *)infile, WRITEBIN);
13114 if (fd == NULL)
13115 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013116 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013117 goto errret;
13118 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013119 if (argvars[1].v_type == VAR_NUMBER)
13120 {
13121 linenr_T lnum;
13122 buf_T *buf;
13123
13124 buf = buflist_findnr(argvars[1].vval.v_number);
13125 if (buf == NULL)
13126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013127 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013128 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013129 goto errret;
13130 }
13131
13132 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13133 {
13134 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13135 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13136 {
13137 err = TRUE;
13138 break;
13139 }
13140 if (putc(NL, fd) == EOF)
13141 {
13142 err = TRUE;
13143 break;
13144 }
13145 }
13146 }
13147 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013148 {
13149 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13150 err = TRUE;
13151 }
13152 else
13153 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013154 size_t len;
13155 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013156
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013157 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013158 if (p == NULL)
13159 {
13160 fclose(fd);
13161 goto errret; /* type error; errmsg already given */
13162 }
13163 len = STRLEN(p);
13164 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13165 err = TRUE;
13166 }
13167 if (fclose(fd) != 0)
13168 err = TRUE;
13169 if (err)
13170 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013171 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013172 goto errret;
13173 }
13174 }
13175
13176 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13177 * echoes typeahead, that messes up the display. */
13178 if (!msg_silent)
13179 flags += SHELL_COOKED;
13180
13181 if (retlist)
13182 {
13183 int len;
13184 listitem_T *li;
13185 char_u *s = NULL;
13186 char_u *start;
13187 char_u *end;
13188 int i;
13189
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013190 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013191 if (res == NULL)
13192 goto errret;
13193
13194 list = list_alloc();
13195 if (list == NULL)
13196 goto errret;
13197
13198 for (i = 0; i < len; ++i)
13199 {
13200 start = res + i;
13201 while (i < len && res[i] != NL)
13202 ++i;
13203 end = res + i;
13204
Bram Moolenaar964b3742019-05-24 18:54:09 +020013205 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013206 if (s == NULL)
13207 goto errret;
13208
13209 for (p = s; start < end; ++p, ++start)
13210 *p = *start == NUL ? NL : *start;
13211 *p = NUL;
13212
13213 li = listitem_alloc();
13214 if (li == NULL)
13215 {
13216 vim_free(s);
13217 goto errret;
13218 }
13219 li->li_tv.v_type = VAR_STRING;
13220 li->li_tv.v_lock = 0;
13221 li->li_tv.vval.v_string = s;
13222 list_append(list, li);
13223 }
13224
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013225 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013226 list = NULL;
13227 }
13228 else
13229 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013230 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013231#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013232 /* translate <CR><NL> into <NL> */
13233 if (res != NULL)
13234 {
13235 char_u *s, *d;
13236
13237 d = res;
13238 for (s = res; *s; ++s)
13239 {
13240 if (s[0] == CAR && s[1] == NL)
13241 ++s;
13242 *d++ = *s;
13243 }
13244 *d = NUL;
13245 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013246#endif
13247 rettv->vval.v_string = res;
13248 res = NULL;
13249 }
13250
13251errret:
13252 if (infile != NULL)
13253 {
13254 mch_remove(infile);
13255 vim_free(infile);
13256 }
13257 if (res != NULL)
13258 vim_free(res);
13259 if (list != NULL)
13260 list_free(list);
13261}
13262
13263/*
13264 * "system()" function
13265 */
13266 static void
13267f_system(typval_T *argvars, typval_T *rettv)
13268{
13269 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13270}
13271
13272/*
13273 * "systemlist()" function
13274 */
13275 static void
13276f_systemlist(typval_T *argvars, typval_T *rettv)
13277{
13278 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13279}
13280
13281/*
13282 * "tabpagebuflist()" function
13283 */
13284 static void
13285f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13286{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013287 tabpage_T *tp;
13288 win_T *wp = NULL;
13289
13290 if (argvars[0].v_type == VAR_UNKNOWN)
13291 wp = firstwin;
13292 else
13293 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013294 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013295 if (tp != NULL)
13296 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13297 }
13298 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13299 {
13300 for (; wp != NULL; wp = wp->w_next)
13301 if (list_append_number(rettv->vval.v_list,
13302 wp->w_buffer->b_fnum) == FAIL)
13303 break;
13304 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013305}
13306
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013307/*
13308 * "tabpagenr()" function
13309 */
13310 static void
13311f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13312{
13313 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013314 char_u *arg;
13315
13316 if (argvars[0].v_type != VAR_UNKNOWN)
13317 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013318 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013319 nr = 0;
13320 if (arg != NULL)
13321 {
13322 if (STRCMP(arg, "$") == 0)
13323 nr = tabpage_index(NULL) - 1;
13324 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013325 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013326 }
13327 }
13328 else
13329 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013330 rettv->vval.v_number = nr;
13331}
13332
13333
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013334/*
13335 * Common code for tabpagewinnr() and winnr().
13336 */
13337 static int
13338get_winnr(tabpage_T *tp, typval_T *argvar)
13339{
13340 win_T *twin;
13341 int nr = 1;
13342 win_T *wp;
13343 char_u *arg;
13344
13345 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13346 if (argvar->v_type != VAR_UNKNOWN)
13347 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013348 int invalid_arg = FALSE;
13349
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013350 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013351 if (arg == NULL)
13352 nr = 0; /* type error; errmsg already given */
13353 else if (STRCMP(arg, "$") == 0)
13354 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13355 else if (STRCMP(arg, "#") == 0)
13356 {
13357 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13358 if (twin == NULL)
13359 nr = 0;
13360 }
13361 else
13362 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013363 long count;
13364 char_u *endp;
13365
13366 // Extract the window count (if specified). e.g. winnr('3j')
13367 count = strtol((char *)arg, (char **)&endp, 10);
13368 if (count <= 0)
13369 count = 1; // if count is not specified, default to 1
13370 if (endp != NULL && *endp != '\0')
13371 {
13372 if (STRCMP(endp, "j") == 0)
13373 twin = win_vert_neighbor(tp, twin, FALSE, count);
13374 else if (STRCMP(endp, "k") == 0)
13375 twin = win_vert_neighbor(tp, twin, TRUE, count);
13376 else if (STRCMP(endp, "h") == 0)
13377 twin = win_horz_neighbor(tp, twin, TRUE, count);
13378 else if (STRCMP(endp, "l") == 0)
13379 twin = win_horz_neighbor(tp, twin, FALSE, count);
13380 else
13381 invalid_arg = TRUE;
13382 }
13383 else
13384 invalid_arg = TRUE;
13385 }
13386
13387 if (invalid_arg)
13388 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013389 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013390 nr = 0;
13391 }
13392 }
13393
13394 if (nr > 0)
13395 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13396 wp != twin; wp = wp->w_next)
13397 {
13398 if (wp == NULL)
13399 {
13400 /* didn't find it in this tabpage */
13401 nr = 0;
13402 break;
13403 }
13404 ++nr;
13405 }
13406 return nr;
13407}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013408
13409/*
13410 * "tabpagewinnr()" function
13411 */
13412 static void
13413f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13414{
13415 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013416 tabpage_T *tp;
13417
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013418 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013419 if (tp == NULL)
13420 nr = 0;
13421 else
13422 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013423 rettv->vval.v_number = nr;
13424}
13425
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013426/*
13427 * "tagfiles()" function
13428 */
13429 static void
13430f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13431{
13432 char_u *fname;
13433 tagname_T tn;
13434 int first;
13435
13436 if (rettv_list_alloc(rettv) == FAIL)
13437 return;
13438 fname = alloc(MAXPATHL);
13439 if (fname == NULL)
13440 return;
13441
13442 for (first = TRUE; ; first = FALSE)
13443 if (get_tagfname(&tn, first, fname) == FAIL
13444 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13445 break;
13446 tagname_free(&tn);
13447 vim_free(fname);
13448}
13449
13450/*
13451 * "taglist()" function
13452 */
13453 static void
13454f_taglist(typval_T *argvars, typval_T *rettv)
13455{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013456 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013457 char_u *tag_pattern;
13458
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013459 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013460
13461 rettv->vval.v_number = FALSE;
13462 if (*tag_pattern == NUL)
13463 return;
13464
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013465 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013466 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013467 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013468 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013469}
13470
13471/*
13472 * "tempname()" function
13473 */
13474 static void
13475f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13476{
13477 static int x = 'A';
13478
13479 rettv->v_type = VAR_STRING;
13480 rettv->vval.v_string = vim_tempname(x, FALSE);
13481
13482 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13483 * names. Skip 'I' and 'O', they are used for shell redirection. */
13484 do
13485 {
13486 if (x == 'Z')
13487 x = '0';
13488 else if (x == '9')
13489 x = 'A';
13490 else
13491 {
13492#ifdef EBCDIC
13493 if (x == 'I')
13494 x = 'J';
13495 else if (x == 'R')
13496 x = 'S';
13497 else
13498#endif
13499 ++x;
13500 }
13501 } while (x == 'I' || x == 'O');
13502}
13503
13504#ifdef FEAT_FLOAT
13505/*
13506 * "tan()" function
13507 */
13508 static void
13509f_tan(typval_T *argvars, typval_T *rettv)
13510{
13511 float_T f = 0.0;
13512
13513 rettv->v_type = VAR_FLOAT;
13514 if (get_float_arg(argvars, &f) == OK)
13515 rettv->vval.v_float = tan(f);
13516 else
13517 rettv->vval.v_float = 0.0;
13518}
13519
13520/*
13521 * "tanh()" function
13522 */
13523 static void
13524f_tanh(typval_T *argvars, typval_T *rettv)
13525{
13526 float_T f = 0.0;
13527
13528 rettv->v_type = VAR_FLOAT;
13529 if (get_float_arg(argvars, &f) == OK)
13530 rettv->vval.v_float = tanh(f);
13531 else
13532 rettv->vval.v_float = 0.0;
13533}
13534#endif
13535
13536/*
13537 * "test_alloc_fail(id, countdown, repeat)" function
13538 */
13539 static void
13540f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13541{
13542 if (argvars[0].v_type != VAR_NUMBER
13543 || argvars[0].vval.v_number <= 0
13544 || argvars[1].v_type != VAR_NUMBER
13545 || argvars[1].vval.v_number < 0
13546 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013547 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013548 else
13549 {
13550 alloc_fail_id = argvars[0].vval.v_number;
13551 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013552 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013553 alloc_fail_countdown = argvars[1].vval.v_number;
13554 alloc_fail_repeat = argvars[2].vval.v_number;
13555 did_outofmem_msg = FALSE;
13556 }
13557}
13558
13559/*
13560 * "test_autochdir()"
13561 */
13562 static void
13563f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13564{
13565#if defined(FEAT_AUTOCHDIR)
13566 test_autochdir = TRUE;
13567#endif
13568}
13569
13570/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013571 * "test_feedinput()"
13572 */
13573 static void
13574f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13575{
13576#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013577 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013578
13579 if (val != NULL)
13580 {
13581 trash_input_buf();
13582 add_to_input_buf_csi(val, (int)STRLEN(val));
13583 }
13584#endif
13585}
13586
13587/*
Bram Moolenaareda65222019-05-16 20:29:44 +020013588 * "test_getvalue({name})" function
13589 */
13590 static void
13591f_test_getvalue(typval_T *argvars, typval_T *rettv)
13592{
13593 if (argvars[0].v_type != VAR_STRING)
13594 emsg(_(e_invarg));
13595 else
13596 {
13597 char_u *name = tv_get_string(&argvars[0]);
13598
13599 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
13600 rettv->vval.v_number = need_fileinfo;
13601 else
13602 semsg(_(e_invarg2), name);
13603 }
13604}
13605
13606/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013607 * "test_option_not_set({name})" function
13608 */
13609 static void
13610f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13611{
13612 char_u *name = (char_u *)"";
13613
13614 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013615 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013616 else
13617 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013618 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013619 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013620 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013621 }
13622}
13623
13624/*
13625 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013626 */
13627 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013628f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013629{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013630 char_u *name = (char_u *)"";
13631 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013632 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013633
13634 if (argvars[0].v_type != VAR_STRING
13635 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013636 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013637 else
13638 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013639 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013640 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013641
13642 if (STRCMP(name, (char_u *)"redraw") == 0)
13643 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013644 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13645 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013646 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13647 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013648 else if (STRCMP(name, (char_u *)"starting") == 0)
13649 {
13650 if (val)
13651 {
13652 if (save_starting < 0)
13653 save_starting = starting;
13654 starting = 0;
13655 }
13656 else
13657 {
13658 starting = save_starting;
13659 save_starting = -1;
13660 }
13661 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013662 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13663 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013664 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
13665 no_query_mouse_for_testing = val;
Bram Moolenaaradc67142019-06-22 01:40:42 +020013666 else if (STRCMP(name, (char_u *)"no_wait_return") == 0)
13667 no_wait_return = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013668 else if (STRCMP(name, (char_u *)"ALL") == 0)
13669 {
13670 disable_char_avail_for_testing = FALSE;
13671 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013672 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013673 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013674 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013675 if (save_starting >= 0)
13676 {
13677 starting = save_starting;
13678 save_starting = -1;
13679 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013680 }
13681 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013682 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013683 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013684}
13685
13686/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010013687 * "test_refcount({expr})" function
13688 */
13689 static void
13690f_test_refcount(typval_T *argvars, typval_T *rettv)
13691{
13692 int retval = -1;
13693
13694 switch (argvars[0].v_type)
13695 {
13696 case VAR_UNKNOWN:
13697 case VAR_NUMBER:
13698 case VAR_FLOAT:
13699 case VAR_SPECIAL:
13700 case VAR_STRING:
13701 break;
13702 case VAR_JOB:
13703#ifdef FEAT_JOB_CHANNEL
13704 if (argvars[0].vval.v_job != NULL)
13705 retval = argvars[0].vval.v_job->jv_refcount - 1;
13706#endif
13707 break;
13708 case VAR_CHANNEL:
13709#ifdef FEAT_JOB_CHANNEL
13710 if (argvars[0].vval.v_channel != NULL)
13711 retval = argvars[0].vval.v_channel->ch_refcount - 1;
13712#endif
13713 break;
13714 case VAR_FUNC:
13715 if (argvars[0].vval.v_string != NULL)
13716 {
13717 ufunc_T *fp;
13718
13719 fp = find_func(argvars[0].vval.v_string);
13720 if (fp != NULL)
13721 retval = fp->uf_refcount;
13722 }
13723 break;
13724 case VAR_PARTIAL:
13725 if (argvars[0].vval.v_partial != NULL)
13726 retval = argvars[0].vval.v_partial->pt_refcount - 1;
13727 break;
13728 case VAR_BLOB:
13729 if (argvars[0].vval.v_blob != NULL)
13730 retval = argvars[0].vval.v_blob->bv_refcount - 1;
13731 break;
13732 case VAR_LIST:
13733 if (argvars[0].vval.v_list != NULL)
13734 retval = argvars[0].vval.v_list->lv_refcount - 1;
13735 break;
13736 case VAR_DICT:
13737 if (argvars[0].vval.v_dict != NULL)
13738 retval = argvars[0].vval.v_dict->dv_refcount - 1;
13739 break;
13740 }
13741
13742 rettv->v_type = VAR_NUMBER;
13743 rettv->vval.v_number = retval;
13744
13745}
13746
13747/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013748 * "test_garbagecollect_now()" function
13749 */
13750 static void
13751f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13752{
13753 /* This is dangerous, any Lists and Dicts used internally may be freed
13754 * while still in use. */
13755 garbage_collect(TRUE);
13756}
13757
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013758/*
Bram Moolenaaradc67142019-06-22 01:40:42 +020013759 * "test_garbagecollect_soon()" function
13760 */
13761 static void
13762f_test_garbagecollect_soon(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13763{
13764 may_garbage_collect = TRUE;
13765}
13766
13767/*
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013768 * "test_ignore_error()" function
13769 */
13770 static void
13771f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13772{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013773 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013774}
13775
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013776 static void
13777f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13778{
13779 rettv->v_type = VAR_BLOB;
13780 rettv->vval.v_blob = NULL;
13781}
13782
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013783#ifdef FEAT_JOB_CHANNEL
13784 static void
13785f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13786{
13787 rettv->v_type = VAR_CHANNEL;
13788 rettv->vval.v_channel = NULL;
13789}
13790#endif
13791
13792 static void
13793f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13794{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013795 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013796}
13797
13798#ifdef FEAT_JOB_CHANNEL
13799 static void
13800f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13801{
13802 rettv->v_type = VAR_JOB;
13803 rettv->vval.v_job = NULL;
13804}
13805#endif
13806
13807 static void
13808f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13809{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013810 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013811}
13812
13813 static void
13814f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13815{
13816 rettv->v_type = VAR_PARTIAL;
13817 rettv->vval.v_partial = NULL;
13818}
13819
13820 static void
13821f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13822{
13823 rettv->v_type = VAR_STRING;
13824 rettv->vval.v_string = NULL;
13825}
13826
Bram Moolenaarab186732018-09-14 21:27:06 +020013827#ifdef FEAT_GUI
13828 static void
13829f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13830{
13831 char_u *which;
13832 long value;
13833 int dragging;
13834 scrollbar_T *sb = NULL;
13835
13836 if (argvars[0].v_type != VAR_STRING
13837 || (argvars[1].v_type) != VAR_NUMBER
13838 || (argvars[2].v_type) != VAR_NUMBER)
13839 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013840 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013841 return;
13842 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013843 which = tv_get_string(&argvars[0]);
13844 value = tv_get_number(&argvars[1]);
13845 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013846
13847 if (STRCMP(which, "left") == 0)
13848 sb = &curwin->w_scrollbars[SBAR_LEFT];
13849 else if (STRCMP(which, "right") == 0)
13850 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13851 else if (STRCMP(which, "hor") == 0)
13852 sb = &gui.bottom_sbar;
13853 if (sb == NULL)
13854 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013855 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020013856 return;
13857 }
13858 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013859# ifndef USE_ON_FLY_SCROLL
13860 // need to loop through normal_cmd() to handle the scroll events
13861 exec_normal(FALSE, TRUE, FALSE);
13862# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013863}
13864#endif
13865
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013866#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013867 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013868f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
13869{
13870 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
13871 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
13872}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013873#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013874
13875 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013876f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13877{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013878 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013879}
13880
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013881/*
13882 * Get a callback from "arg". It can be a Funcref or a function name.
13883 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013884 * "cb_name" is not allocated.
13885 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013886 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013887 callback_T
13888get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013889{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013890 callback_T res;
13891
13892 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013893 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13894 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013895 res.cb_partial = arg->vval.v_partial;
13896 ++res.cb_partial->pt_refcount;
13897 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013898 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013899 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013900 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013901 res.cb_partial = NULL;
13902 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
13903 {
13904 // Note that we don't make a copy of the string.
13905 res.cb_name = arg->vval.v_string;
13906 func_ref(res.cb_name);
13907 }
13908 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13909 {
13910 res.cb_name = (char_u *)"";
13911 }
13912 else
13913 {
13914 emsg(_("E921: Invalid callback argument"));
13915 res.cb_name = NULL;
13916 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013917 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013918 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013919}
13920
13921/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013922 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013923 */
13924 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013925put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013926{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013927 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013928 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013929 tv->v_type = VAR_PARTIAL;
13930 tv->vval.v_partial = cb->cb_partial;
13931 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013932 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013933 else
13934 {
13935 tv->v_type = VAR_FUNC;
13936 tv->vval.v_string = vim_strsave(cb->cb_name);
13937 func_ref(cb->cb_name);
13938 }
13939}
13940
13941/*
13942 * Make a copy of "src" into "dest", allocating the function name if needed,
13943 * without incrementing the refcount.
13944 */
13945 void
13946set_callback(callback_T *dest, callback_T *src)
13947{
13948 if (src->cb_partial == NULL)
13949 {
13950 // just a function name, make a copy
13951 dest->cb_name = vim_strsave(src->cb_name);
13952 dest->cb_free_name = TRUE;
13953 }
13954 else
13955 {
13956 // cb_name is a pointer into cb_partial
13957 dest->cb_name = src->cb_name;
13958 dest->cb_free_name = FALSE;
13959 }
13960 dest->cb_partial = src->cb_partial;
13961}
13962
13963/*
13964 * Unref/free "callback" returned by get_callback() or set_callback().
13965 */
13966 void
13967free_callback(callback_T *callback)
13968{
13969 if (callback->cb_partial != NULL)
13970 {
13971 partial_unref(callback->cb_partial);
13972 callback->cb_partial = NULL;
13973 }
13974 else if (callback->cb_name != NULL)
13975 func_unref(callback->cb_name);
13976 if (callback->cb_free_name)
13977 {
13978 vim_free(callback->cb_name);
13979 callback->cb_free_name = FALSE;
13980 }
13981 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013982}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013983
13984#ifdef FEAT_TIMERS
13985/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013986 * "timer_info([timer])" function
13987 */
13988 static void
13989f_timer_info(typval_T *argvars, typval_T *rettv)
13990{
13991 timer_T *timer = NULL;
13992
13993 if (rettv_list_alloc(rettv) != OK)
13994 return;
13995 if (argvars[0].v_type != VAR_UNKNOWN)
13996 {
13997 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013998 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013999 else
14000 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014001 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014002 if (timer != NULL)
14003 add_timer_info(rettv, timer);
14004 }
14005 }
14006 else
14007 add_timer_info_all(rettv);
14008}
14009
14010/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014011 * "timer_pause(timer, paused)" function
14012 */
14013 static void
14014f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14015{
14016 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014017 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014018
14019 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014020 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014021 else
14022 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014023 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014024 if (timer != NULL)
14025 timer->tr_paused = paused;
14026 }
14027}
14028
14029/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014030 * "timer_start(time, callback [, options])" function
14031 */
14032 static void
14033f_timer_start(typval_T *argvars, typval_T *rettv)
14034{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014035 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014036 timer_T *timer;
14037 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014038 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014039 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014040
Bram Moolenaar75537a92016-09-05 22:45:28 +020014041 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014042 if (check_secure())
14043 return;
14044 if (argvars[2].v_type != VAR_UNKNOWN)
14045 {
14046 if (argvars[2].v_type != VAR_DICT
14047 || (dict = argvars[2].vval.v_dict) == NULL)
14048 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014049 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014050 return;
14051 }
14052 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014053 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014054 }
14055
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014056 callback = get_callback(&argvars[1]);
14057 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014058 return;
14059
14060 timer = create_timer(msec, repeat);
14061 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014062 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014063 else
14064 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014065 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014066 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014067 }
14068}
14069
14070/*
14071 * "timer_stop(timer)" function
14072 */
14073 static void
14074f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14075{
14076 timer_T *timer;
14077
14078 if (argvars[0].v_type != VAR_NUMBER)
14079 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014080 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014081 return;
14082 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014083 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014084 if (timer != NULL)
14085 stop_timer(timer);
14086}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014087
14088/*
14089 * "timer_stopall()" function
14090 */
14091 static void
14092f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14093{
14094 stop_all_timers();
14095}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014096#endif
14097
14098/*
14099 * "tolower(string)" function
14100 */
14101 static void
14102f_tolower(typval_T *argvars, typval_T *rettv)
14103{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014104 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014105 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014106}
14107
14108/*
14109 * "toupper(string)" function
14110 */
14111 static void
14112f_toupper(typval_T *argvars, typval_T *rettv)
14113{
14114 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014115 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014116}
14117
14118/*
14119 * "tr(string, fromstr, tostr)" function
14120 */
14121 static void
14122f_tr(typval_T *argvars, typval_T *rettv)
14123{
14124 char_u *in_str;
14125 char_u *fromstr;
14126 char_u *tostr;
14127 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014128 int inlen;
14129 int fromlen;
14130 int tolen;
14131 int idx;
14132 char_u *cpstr;
14133 int cplen;
14134 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014135 char_u buf[NUMBUFLEN];
14136 char_u buf2[NUMBUFLEN];
14137 garray_T ga;
14138
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014139 in_str = tv_get_string(&argvars[0]);
14140 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14141 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014142
14143 /* Default return value: empty string. */
14144 rettv->v_type = VAR_STRING;
14145 rettv->vval.v_string = NULL;
14146 if (fromstr == NULL || tostr == NULL)
14147 return; /* type error; errmsg already given */
14148 ga_init2(&ga, (int)sizeof(char), 80);
14149
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014150 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014151 /* not multi-byte: fromstr and tostr must be the same length */
14152 if (STRLEN(fromstr) != STRLEN(tostr))
14153 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014154error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014155 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014156 ga_clear(&ga);
14157 return;
14158 }
14159
14160 /* fromstr and tostr have to contain the same number of chars */
14161 while (*in_str != NUL)
14162 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014163 if (has_mbyte)
14164 {
14165 inlen = (*mb_ptr2len)(in_str);
14166 cpstr = in_str;
14167 cplen = inlen;
14168 idx = 0;
14169 for (p = fromstr; *p != NUL; p += fromlen)
14170 {
14171 fromlen = (*mb_ptr2len)(p);
14172 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14173 {
14174 for (p = tostr; *p != NUL; p += tolen)
14175 {
14176 tolen = (*mb_ptr2len)(p);
14177 if (idx-- == 0)
14178 {
14179 cplen = tolen;
14180 cpstr = p;
14181 break;
14182 }
14183 }
14184 if (*p == NUL) /* tostr is shorter than fromstr */
14185 goto error;
14186 break;
14187 }
14188 ++idx;
14189 }
14190
14191 if (first && cpstr == in_str)
14192 {
14193 /* Check that fromstr and tostr have the same number of
14194 * (multi-byte) characters. Done only once when a character
14195 * of in_str doesn't appear in fromstr. */
14196 first = FALSE;
14197 for (p = tostr; *p != NUL; p += tolen)
14198 {
14199 tolen = (*mb_ptr2len)(p);
14200 --idx;
14201 }
14202 if (idx != 0)
14203 goto error;
14204 }
14205
14206 (void)ga_grow(&ga, cplen);
14207 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14208 ga.ga_len += cplen;
14209
14210 in_str += inlen;
14211 }
14212 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014213 {
14214 /* When not using multi-byte chars we can do it faster. */
14215 p = vim_strchr(fromstr, *in_str);
14216 if (p != NULL)
14217 ga_append(&ga, tostr[p - fromstr]);
14218 else
14219 ga_append(&ga, *in_str);
14220 ++in_str;
14221 }
14222 }
14223
14224 /* add a terminating NUL */
14225 (void)ga_grow(&ga, 1);
14226 ga_append(&ga, NUL);
14227
14228 rettv->vval.v_string = ga.ga_data;
14229}
14230
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014231/*
14232 * "trim({expr})" function
14233 */
14234 static void
14235f_trim(typval_T *argvars, typval_T *rettv)
14236{
14237 char_u buf1[NUMBUFLEN];
14238 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014239 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014240 char_u *mask = NULL;
14241 char_u *tail;
14242 char_u *prev;
14243 char_u *p;
14244 int c1;
14245
14246 rettv->v_type = VAR_STRING;
14247 if (head == NULL)
14248 {
14249 rettv->vval.v_string = NULL;
14250 return;
14251 }
14252
14253 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014254 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014255
14256 while (*head != NUL)
14257 {
14258 c1 = PTR2CHAR(head);
14259 if (mask == NULL)
14260 {
14261 if (c1 > ' ' && c1 != 0xa0)
14262 break;
14263 }
14264 else
14265 {
14266 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14267 if (c1 == PTR2CHAR(p))
14268 break;
14269 if (*p == NUL)
14270 break;
14271 }
14272 MB_PTR_ADV(head);
14273 }
14274
14275 for (tail = head + STRLEN(head); tail > head; tail = prev)
14276 {
14277 prev = tail;
14278 MB_PTR_BACK(head, prev);
14279 c1 = PTR2CHAR(prev);
14280 if (mask == NULL)
14281 {
14282 if (c1 > ' ' && c1 != 0xa0)
14283 break;
14284 }
14285 else
14286 {
14287 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14288 if (c1 == PTR2CHAR(p))
14289 break;
14290 if (*p == NUL)
14291 break;
14292 }
14293 }
14294 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14295}
14296
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014297#ifdef FEAT_FLOAT
14298/*
14299 * "trunc({float})" function
14300 */
14301 static void
14302f_trunc(typval_T *argvars, typval_T *rettv)
14303{
14304 float_T f = 0.0;
14305
14306 rettv->v_type = VAR_FLOAT;
14307 if (get_float_arg(argvars, &f) == OK)
14308 /* trunc() is not in C90, use floor() or ceil() instead. */
14309 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14310 else
14311 rettv->vval.v_float = 0.0;
14312}
14313#endif
14314
14315/*
14316 * "type(expr)" function
14317 */
14318 static void
14319f_type(typval_T *argvars, typval_T *rettv)
14320{
14321 int n = -1;
14322
14323 switch (argvars[0].v_type)
14324 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014325 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14326 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014327 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014328 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14329 case VAR_LIST: n = VAR_TYPE_LIST; break;
14330 case VAR_DICT: n = VAR_TYPE_DICT; break;
14331 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014332 case VAR_SPECIAL:
14333 if (argvars[0].vval.v_number == VVAL_FALSE
14334 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014335 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014336 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014337 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014338 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014339 case VAR_JOB: n = VAR_TYPE_JOB; break;
14340 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014341 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014342 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014343 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014344 n = -1;
14345 break;
14346 }
14347 rettv->vval.v_number = n;
14348}
14349
14350/*
14351 * "undofile(name)" function
14352 */
14353 static void
14354f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14355{
14356 rettv->v_type = VAR_STRING;
14357#ifdef FEAT_PERSISTENT_UNDO
14358 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014359 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014360
14361 if (*fname == NUL)
14362 {
14363 /* If there is no file name there will be no undo file. */
14364 rettv->vval.v_string = NULL;
14365 }
14366 else
14367 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020014368 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014369
14370 if (ffname != NULL)
14371 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14372 vim_free(ffname);
14373 }
14374 }
14375#else
14376 rettv->vval.v_string = NULL;
14377#endif
14378}
14379
14380/*
14381 * "undotree()" function
14382 */
14383 static void
14384f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14385{
14386 if (rettv_dict_alloc(rettv) == OK)
14387 {
14388 dict_T *dict = rettv->vval.v_dict;
14389 list_T *list;
14390
Bram Moolenaare0be1672018-07-08 16:50:37 +020014391 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14392 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14393 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14394 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14395 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14396 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014397
14398 list = list_alloc();
14399 if (list != NULL)
14400 {
14401 u_eval_tree(curbuf->b_u_oldhead, list);
14402 dict_add_list(dict, "entries", list);
14403 }
14404 }
14405}
14406
14407/*
14408 * "values(dict)" function
14409 */
14410 static void
14411f_values(typval_T *argvars, typval_T *rettv)
14412{
14413 dict_list(argvars, rettv, 1);
14414}
14415
14416/*
14417 * "virtcol(string)" function
14418 */
14419 static void
14420f_virtcol(typval_T *argvars, typval_T *rettv)
14421{
14422 colnr_T vcol = 0;
14423 pos_T *fp;
14424 int fnum = curbuf->b_fnum;
14425
14426 fp = var2fpos(&argvars[0], FALSE, &fnum);
14427 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14428 && fnum == curbuf->b_fnum)
14429 {
14430 getvvcol(curwin, fp, NULL, NULL, &vcol);
14431 ++vcol;
14432 }
14433
14434 rettv->vval.v_number = vcol;
14435}
14436
14437/*
14438 * "visualmode()" function
14439 */
14440 static void
14441f_visualmode(typval_T *argvars, typval_T *rettv)
14442{
14443 char_u str[2];
14444
14445 rettv->v_type = VAR_STRING;
14446 str[0] = curbuf->b_visual_mode_eval;
14447 str[1] = NUL;
14448 rettv->vval.v_string = vim_strsave(str);
14449
14450 /* A non-zero number or non-empty string argument: reset mode. */
14451 if (non_zero_arg(&argvars[0]))
14452 curbuf->b_visual_mode_eval = NUL;
14453}
14454
14455/*
14456 * "wildmenumode()" function
14457 */
14458 static void
14459f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14460{
14461#ifdef FEAT_WILDMENU
14462 if (wild_menu_showing)
14463 rettv->vval.v_number = 1;
14464#endif
14465}
14466
14467/*
14468 * "winbufnr(nr)" function
14469 */
14470 static void
14471f_winbufnr(typval_T *argvars, typval_T *rettv)
14472{
14473 win_T *wp;
14474
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014475 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014476 if (wp == NULL)
14477 rettv->vval.v_number = -1;
14478 else
14479 rettv->vval.v_number = wp->w_buffer->b_fnum;
14480}
14481
14482/*
14483 * "wincol()" function
14484 */
14485 static void
14486f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14487{
14488 validate_cursor();
14489 rettv->vval.v_number = curwin->w_wcol + 1;
14490}
14491
14492/*
14493 * "winheight(nr)" function
14494 */
14495 static void
14496f_winheight(typval_T *argvars, typval_T *rettv)
14497{
14498 win_T *wp;
14499
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014500 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014501 if (wp == NULL)
14502 rettv->vval.v_number = -1;
14503 else
14504 rettv->vval.v_number = wp->w_height;
14505}
14506
14507/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014508 * "winlayout()" function
14509 */
14510 static void
14511f_winlayout(typval_T *argvars, typval_T *rettv)
14512{
14513 tabpage_T *tp;
14514
14515 if (rettv_list_alloc(rettv) != OK)
14516 return;
14517
14518 if (argvars[0].v_type == VAR_UNKNOWN)
14519 tp = curtab;
14520 else
14521 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014522 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014523 if (tp == NULL)
14524 return;
14525 }
14526
14527 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14528}
14529
14530/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014531 * "winline()" function
14532 */
14533 static void
14534f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14535{
14536 validate_cursor();
14537 rettv->vval.v_number = curwin->w_wrow + 1;
14538}
14539
14540/*
14541 * "winnr()" function
14542 */
14543 static void
14544f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14545{
14546 int nr = 1;
14547
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014548 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014549 rettv->vval.v_number = nr;
14550}
14551
14552/*
14553 * "winrestcmd()" function
14554 */
14555 static void
14556f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14557{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014558 win_T *wp;
14559 int winnr = 1;
14560 garray_T ga;
14561 char_u buf[50];
14562
14563 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014564 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014565 {
14566 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14567 ga_concat(&ga, buf);
14568 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14569 ga_concat(&ga, buf);
14570 ++winnr;
14571 }
14572 ga_append(&ga, NUL);
14573
14574 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014575 rettv->v_type = VAR_STRING;
14576}
14577
14578/*
14579 * "winrestview()" function
14580 */
14581 static void
14582f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14583{
14584 dict_T *dict;
14585
14586 if (argvars[0].v_type != VAR_DICT
14587 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014588 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014589 else
14590 {
14591 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014592 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014593 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014594 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014595 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014596 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014597 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14598 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014599 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014600 curwin->w_set_curswant = FALSE;
14601 }
14602
14603 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014604 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014605#ifdef FEAT_DIFF
14606 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014607 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014608#endif
14609 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014610 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014611 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014612 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014613
14614 check_cursor();
14615 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014616 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014617 changed_window_setting();
14618
14619 if (curwin->w_topline <= 0)
14620 curwin->w_topline = 1;
14621 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14622 curwin->w_topline = curbuf->b_ml.ml_line_count;
14623#ifdef FEAT_DIFF
14624 check_topfill(curwin, TRUE);
14625#endif
14626 }
14627}
14628
14629/*
14630 * "winsaveview()" function
14631 */
14632 static void
14633f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14634{
14635 dict_T *dict;
14636
14637 if (rettv_dict_alloc(rettv) == FAIL)
14638 return;
14639 dict = rettv->vval.v_dict;
14640
Bram Moolenaare0be1672018-07-08 16:50:37 +020014641 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14642 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020014643 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014644 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014645 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014646
Bram Moolenaare0be1672018-07-08 16:50:37 +020014647 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014648#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014649 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014650#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014651 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14652 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014653}
14654
14655/*
14656 * "winwidth(nr)" function
14657 */
14658 static void
14659f_winwidth(typval_T *argvars, typval_T *rettv)
14660{
14661 win_T *wp;
14662
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014663 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014664 if (wp == NULL)
14665 rettv->vval.v_number = -1;
14666 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014667 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014668}
14669
14670/*
14671 * "wordcount()" function
14672 */
14673 static void
14674f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14675{
14676 if (rettv_dict_alloc(rettv) == FAIL)
14677 return;
14678 cursor_pos_info(rettv->vval.v_dict);
14679}
14680
14681/*
14682 * "writefile()" function
14683 */
14684 static void
14685f_writefile(typval_T *argvars, typval_T *rettv)
14686{
14687 int binary = FALSE;
14688 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014689#ifdef HAVE_FSYNC
14690 int do_fsync = p_fs;
14691#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014692 char_u *fname;
14693 FILE *fd;
14694 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014695 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014696 list_T *list = NULL;
14697 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014698
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014699 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010014700 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014701 return;
14702
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014703 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014704 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014705 list = argvars[0].vval.v_list;
14706 if (list == NULL)
14707 return;
14708 for (li = list->lv_first; li != NULL; li = li->li_next)
14709 if (tv_get_string_chk(&li->li_tv) == NULL)
14710 return;
14711 }
14712 else if (argvars[0].v_type == VAR_BLOB)
14713 {
14714 blob = argvars[0].vval.v_blob;
14715 if (blob == NULL)
14716 return;
14717 }
14718 else
14719 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014720 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014721 return;
14722 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014723
14724 if (argvars[2].v_type != VAR_UNKNOWN)
14725 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014726 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014727
14728 if (arg2 == NULL)
14729 return;
14730 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014731 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014732 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014733 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014734#ifdef HAVE_FSYNC
14735 if (vim_strchr(arg2, 's') != NULL)
14736 do_fsync = TRUE;
14737 else if (vim_strchr(arg2, 'S') != NULL)
14738 do_fsync = FALSE;
14739#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014740 }
14741
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014742 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014743 if (fname == NULL)
14744 return;
14745
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014746 /* Always open the file in binary mode, library functions have a mind of
14747 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014748 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14749 append ? APPENDBIN : WRITEBIN)) == NULL)
14750 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014751 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014752 ret = -1;
14753 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014754 else if (blob)
14755 {
14756 if (write_blob(fd, blob) == FAIL)
14757 ret = -1;
14758#ifdef HAVE_FSYNC
14759 else if (do_fsync)
14760 // Ignore the error, the user wouldn't know what to do about it.
14761 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010014762 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014763#endif
14764 fclose(fd);
14765 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014766 else
14767 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014768 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014769 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014770#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014771 else if (do_fsync)
14772 /* Ignore the error, the user wouldn't know what to do about it.
14773 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010014774 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014775#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014776 fclose(fd);
14777 }
14778
14779 rettv->vval.v_number = ret;
14780}
14781
14782/*
14783 * "xor(expr, expr)" function
14784 */
14785 static void
14786f_xor(typval_T *argvars, typval_T *rettv)
14787{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014788 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14789 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014790}
14791
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014792#endif /* FEAT_EVAL */