blob: 43c8028b6ed8ffb87c2755a85d950644e1ab78f3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaard0573012017-10-28 21:11:06 +020023#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020024# include <time.h> /* for time_t */
25#endif
26
27static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010028static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020029static char *e_stringreq = N_("E928: String required");
Bram Moolenaaraff74912019-03-30 18:11:49 +010030static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020031
32#ifdef FEAT_FLOAT
33static void f_abs(typval_T *argvars, typval_T *rettv);
34static void f_acos(typval_T *argvars, typval_T *rettv);
35#endif
36static void f_add(typval_T *argvars, typval_T *rettv);
37static void f_and(typval_T *argvars, typval_T *rettv);
38static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020039static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_argc(typval_T *argvars, typval_T *rettv);
41static void f_argidx(typval_T *argvars, typval_T *rettv);
42static void f_arglistid(typval_T *argvars, typval_T *rettv);
43static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010044static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020045static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010046static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020047static void f_assert_exception(typval_T *argvars, typval_T *rettv);
48static void f_assert_fails(typval_T *argvars, typval_T *rettv);
49static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020050static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_match(typval_T *argvars, typval_T *rettv);
52static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
53static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010054static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_true(typval_T *argvars, typval_T *rettv);
56#ifdef FEAT_FLOAT
57static void f_asin(typval_T *argvars, typval_T *rettv);
58static void f_atan(typval_T *argvars, typval_T *rettv);
59static void f_atan2(typval_T *argvars, typval_T *rettv);
60#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010061#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020062static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010063static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010064# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010065static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010067#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_browse(typval_T *argvars, typval_T *rettv);
69static void f_browsedir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020070static void f_bufadd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_bufexists(typval_T *argvars, typval_T *rettv);
72static void f_buflisted(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020073static void f_bufload(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020074static void f_bufloaded(typval_T *argvars, typval_T *rettv);
75static void f_bufname(typval_T *argvars, typval_T *rettv);
76static void f_bufnr(typval_T *argvars, typval_T *rettv);
77static void f_bufwinid(typval_T *argvars, typval_T *rettv);
78static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
79static void f_byte2line(typval_T *argvars, typval_T *rettv);
80static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
81static void f_byteidx(typval_T *argvars, typval_T *rettv);
82static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
83static void f_call(typval_T *argvars, typval_T *rettv);
84#ifdef FEAT_FLOAT
85static void f_ceil(typval_T *argvars, typval_T *rettv);
86#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_changenr(typval_T *argvars, typval_T *rettv);
88static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020089static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_cindent(typval_T *argvars, typval_T *rettv);
91static void f_clearmatches(typval_T *argvars, typval_T *rettv);
92static void f_col(typval_T *argvars, typval_T *rettv);
93#if defined(FEAT_INS_EXPAND)
94static void f_complete(typval_T *argvars, typval_T *rettv);
95static void f_complete_add(typval_T *argvars, typval_T *rettv);
96static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +010097static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020098#endif
99static void f_confirm(typval_T *argvars, typval_T *rettv);
100static void f_copy(typval_T *argvars, typval_T *rettv);
101#ifdef FEAT_FLOAT
102static void f_cos(typval_T *argvars, typval_T *rettv);
103static void f_cosh(typval_T *argvars, typval_T *rettv);
104#endif
105static void f_count(typval_T *argvars, typval_T *rettv);
106static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
107static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100108#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200109static void f_debugbreak(typval_T *argvars, typval_T *rettv);
110#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200111static void f_deepcopy(typval_T *argvars, typval_T *rettv);
112static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200113static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200114static void f_did_filetype(typval_T *argvars, typval_T *rettv);
115static void f_diff_filler(typval_T *argvars, typval_T *rettv);
116static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
117static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200118static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119static void f_escape(typval_T *argvars, typval_T *rettv);
120static void f_eval(typval_T *argvars, typval_T *rettv);
121static void f_eventhandler(typval_T *argvars, typval_T *rettv);
122static void f_executable(typval_T *argvars, typval_T *rettv);
123static void f_execute(typval_T *argvars, typval_T *rettv);
124static void f_exepath(typval_T *argvars, typval_T *rettv);
125static void f_exists(typval_T *argvars, typval_T *rettv);
126#ifdef FEAT_FLOAT
127static void f_exp(typval_T *argvars, typval_T *rettv);
128#endif
129static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200130static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131static void f_extend(typval_T *argvars, typval_T *rettv);
132static void f_feedkeys(typval_T *argvars, typval_T *rettv);
133static void f_filereadable(typval_T *argvars, typval_T *rettv);
134static void f_filewritable(typval_T *argvars, typval_T *rettv);
135static void f_filter(typval_T *argvars, typval_T *rettv);
136static void f_finddir(typval_T *argvars, typval_T *rettv);
137static void f_findfile(typval_T *argvars, typval_T *rettv);
138#ifdef FEAT_FLOAT
139static void f_float2nr(typval_T *argvars, typval_T *rettv);
140static void f_floor(typval_T *argvars, typval_T *rettv);
141static void f_fmod(typval_T *argvars, typval_T *rettv);
142#endif
143static void f_fnameescape(typval_T *argvars, typval_T *rettv);
144static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
145static void f_foldclosed(typval_T *argvars, typval_T *rettv);
146static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
147static void f_foldlevel(typval_T *argvars, typval_T *rettv);
148static void f_foldtext(typval_T *argvars, typval_T *rettv);
149static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
150static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200151static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200152static void f_function(typval_T *argvars, typval_T *rettv);
153static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
154static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200155static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200156static void f_getbufline(typval_T *argvars, typval_T *rettv);
157static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100158static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200159static void f_getchar(typval_T *argvars, typval_T *rettv);
160static void f_getcharmod(typval_T *argvars, typval_T *rettv);
161static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
162static void f_getcmdline(typval_T *argvars, typval_T *rettv);
163#if defined(FEAT_CMDL_COMPL)
164static void f_getcompletion(typval_T *argvars, typval_T *rettv);
165#endif
166static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
167static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
168static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
169static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200170static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200171static void f_getfontname(typval_T *argvars, typval_T *rettv);
172static void f_getfperm(typval_T *argvars, typval_T *rettv);
173static void f_getfsize(typval_T *argvars, typval_T *rettv);
174static void f_getftime(typval_T *argvars, typval_T *rettv);
175static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100176static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200178static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179static void f_getmatches(typval_T *argvars, typval_T *rettv);
180static void f_getpid(typval_T *argvars, typval_T *rettv);
181static void f_getcurpos(typval_T *argvars, typval_T *rettv);
182static void f_getpos(typval_T *argvars, typval_T *rettv);
183static void f_getqflist(typval_T *argvars, typval_T *rettv);
184static void f_getreg(typval_T *argvars, typval_T *rettv);
185static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200186static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200187static void f_gettabvar(typval_T *argvars, typval_T *rettv);
188static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100189static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200190static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100191static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getwinposx(typval_T *argvars, typval_T *rettv);
193static void f_getwinposy(typval_T *argvars, typval_T *rettv);
194static void f_getwinvar(typval_T *argvars, typval_T *rettv);
195static void f_glob(typval_T *argvars, typval_T *rettv);
196static void f_globpath(typval_T *argvars, typval_T *rettv);
197static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
198static void f_has(typval_T *argvars, typval_T *rettv);
199static void f_has_key(typval_T *argvars, typval_T *rettv);
200static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
201static void f_hasmapto(typval_T *argvars, typval_T *rettv);
202static void f_histadd(typval_T *argvars, typval_T *rettv);
203static void f_histdel(typval_T *argvars, typval_T *rettv);
204static void f_histget(typval_T *argvars, typval_T *rettv);
205static void f_histnr(typval_T *argvars, typval_T *rettv);
206static void f_hlID(typval_T *argvars, typval_T *rettv);
207static void f_hlexists(typval_T *argvars, typval_T *rettv);
208static void f_hostname(typval_T *argvars, typval_T *rettv);
209static void f_iconv(typval_T *argvars, typval_T *rettv);
210static void f_indent(typval_T *argvars, typval_T *rettv);
211static void f_index(typval_T *argvars, typval_T *rettv);
212static void f_input(typval_T *argvars, typval_T *rettv);
213static void f_inputdialog(typval_T *argvars, typval_T *rettv);
214static void f_inputlist(typval_T *argvars, typval_T *rettv);
215static void f_inputrestore(typval_T *argvars, typval_T *rettv);
216static void f_inputsave(typval_T *argvars, typval_T *rettv);
217static void f_inputsecret(typval_T *argvars, typval_T *rettv);
218static void f_insert(typval_T *argvars, typval_T *rettv);
219static void f_invert(typval_T *argvars, typval_T *rettv);
220static void f_isdirectory(typval_T *argvars, typval_T *rettv);
221static void f_islocked(typval_T *argvars, typval_T *rettv);
222#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200223static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200224static void f_isnan(typval_T *argvars, typval_T *rettv);
225#endif
226static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200227static void f_join(typval_T *argvars, typval_T *rettv);
228static void f_js_decode(typval_T *argvars, typval_T *rettv);
229static void f_js_encode(typval_T *argvars, typval_T *rettv);
230static void f_json_decode(typval_T *argvars, typval_T *rettv);
231static void f_json_encode(typval_T *argvars, typval_T *rettv);
232static void f_keys(typval_T *argvars, typval_T *rettv);
233static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
234static void f_len(typval_T *argvars, typval_T *rettv);
235static void f_libcall(typval_T *argvars, typval_T *rettv);
236static void f_libcallnr(typval_T *argvars, typval_T *rettv);
237static void f_line(typval_T *argvars, typval_T *rettv);
238static void f_line2byte(typval_T *argvars, typval_T *rettv);
239static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200240static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200241static void f_localtime(typval_T *argvars, typval_T *rettv);
242#ifdef FEAT_FLOAT
243static void f_log(typval_T *argvars, typval_T *rettv);
244static void f_log10(typval_T *argvars, typval_T *rettv);
245#endif
246#ifdef FEAT_LUA
247static void f_luaeval(typval_T *argvars, typval_T *rettv);
248#endif
249static void f_map(typval_T *argvars, typval_T *rettv);
250static void f_maparg(typval_T *argvars, typval_T *rettv);
251static void f_mapcheck(typval_T *argvars, typval_T *rettv);
252static void f_match(typval_T *argvars, typval_T *rettv);
253static void f_matchadd(typval_T *argvars, typval_T *rettv);
254static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
255static void f_matcharg(typval_T *argvars, typval_T *rettv);
256static void f_matchdelete(typval_T *argvars, typval_T *rettv);
257static void f_matchend(typval_T *argvars, typval_T *rettv);
258static void f_matchlist(typval_T *argvars, typval_T *rettv);
259static void f_matchstr(typval_T *argvars, typval_T *rettv);
260static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
261static void f_max(typval_T *argvars, typval_T *rettv);
262static void f_min(typval_T *argvars, typval_T *rettv);
263#ifdef vim_mkdir
264static void f_mkdir(typval_T *argvars, typval_T *rettv);
265#endif
266static void f_mode(typval_T *argvars, typval_T *rettv);
267#ifdef FEAT_MZSCHEME
268static void f_mzeval(typval_T *argvars, typval_T *rettv);
269#endif
270static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
271static void f_nr2char(typval_T *argvars, typval_T *rettv);
272static void f_or(typval_T *argvars, typval_T *rettv);
273static void f_pathshorten(typval_T *argvars, typval_T *rettv);
274#ifdef FEAT_PERL
275static void f_perleval(typval_T *argvars, typval_T *rettv);
276#endif
277#ifdef FEAT_FLOAT
278static void f_pow(typval_T *argvars, typval_T *rettv);
279#endif
280static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
281static void f_printf(typval_T *argvars, typval_T *rettv);
282static void f_pumvisible(typval_T *argvars, typval_T *rettv);
283#ifdef FEAT_PYTHON3
284static void f_py3eval(typval_T *argvars, typval_T *rettv);
285#endif
286#ifdef FEAT_PYTHON
287static void f_pyeval(typval_T *argvars, typval_T *rettv);
288#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100289#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
290static void f_pyxeval(typval_T *argvars, typval_T *rettv);
291#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200292static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200293static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200294static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200295static void f_reg_executing(typval_T *argvars, typval_T *rettv);
296static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200297static void f_reltime(typval_T *argvars, typval_T *rettv);
298#ifdef FEAT_FLOAT
299static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
300#endif
301static void f_reltimestr(typval_T *argvars, typval_T *rettv);
302static void f_remote_expr(typval_T *argvars, typval_T *rettv);
303static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
304static void f_remote_peek(typval_T *argvars, typval_T *rettv);
305static void f_remote_read(typval_T *argvars, typval_T *rettv);
306static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100307static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200308static void f_remove(typval_T *argvars, typval_T *rettv);
309static void f_rename(typval_T *argvars, typval_T *rettv);
310static void f_repeat(typval_T *argvars, typval_T *rettv);
311static void f_resolve(typval_T *argvars, typval_T *rettv);
312static void f_reverse(typval_T *argvars, typval_T *rettv);
313#ifdef FEAT_FLOAT
314static void f_round(typval_T *argvars, typval_T *rettv);
315#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100316#ifdef FEAT_RUBY
317static void f_rubyeval(typval_T *argvars, typval_T *rettv);
318#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200319static void f_screenattr(typval_T *argvars, typval_T *rettv);
320static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100321static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200322static void f_screencol(typval_T *argvars, typval_T *rettv);
323static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100324static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200325static void f_search(typval_T *argvars, typval_T *rettv);
326static void f_searchdecl(typval_T *argvars, typval_T *rettv);
327static void f_searchpair(typval_T *argvars, typval_T *rettv);
328static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
329static void f_searchpos(typval_T *argvars, typval_T *rettv);
330static void f_server2client(typval_T *argvars, typval_T *rettv);
331static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200332static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200333static void f_setbufvar(typval_T *argvars, typval_T *rettv);
334static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
335static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200336static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200337static void f_setfperm(typval_T *argvars, typval_T *rettv);
338static void f_setline(typval_T *argvars, typval_T *rettv);
339static void f_setloclist(typval_T *argvars, typval_T *rettv);
340static void f_setmatches(typval_T *argvars, typval_T *rettv);
341static void f_setpos(typval_T *argvars, typval_T *rettv);
342static void f_setqflist(typval_T *argvars, typval_T *rettv);
343static void f_setreg(typval_T *argvars, typval_T *rettv);
344static void f_settabvar(typval_T *argvars, typval_T *rettv);
345static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100346static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200347static void f_setwinvar(typval_T *argvars, typval_T *rettv);
348#ifdef FEAT_CRYPT
349static void f_sha256(typval_T *argvars, typval_T *rettv);
350#endif /* FEAT_CRYPT */
351static void f_shellescape(typval_T *argvars, typval_T *rettv);
352static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
353static void f_simplify(typval_T *argvars, typval_T *rettv);
354#ifdef FEAT_FLOAT
355static void f_sin(typval_T *argvars, typval_T *rettv);
356static void f_sinh(typval_T *argvars, typval_T *rettv);
357#endif
358static void f_sort(typval_T *argvars, typval_T *rettv);
359static void f_soundfold(typval_T *argvars, typval_T *rettv);
360static void f_spellbadword(typval_T *argvars, typval_T *rettv);
361static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
362static void f_split(typval_T *argvars, typval_T *rettv);
363#ifdef FEAT_FLOAT
364static void f_sqrt(typval_T *argvars, typval_T *rettv);
365static void f_str2float(typval_T *argvars, typval_T *rettv);
366#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200367static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200368static void f_str2nr(typval_T *argvars, typval_T *rettv);
369static void f_strchars(typval_T *argvars, typval_T *rettv);
370#ifdef HAVE_STRFTIME
371static void f_strftime(typval_T *argvars, typval_T *rettv);
372#endif
373static void f_strgetchar(typval_T *argvars, typval_T *rettv);
374static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200375static void f_strlen(typval_T *argvars, typval_T *rettv);
376static void f_strcharpart(typval_T *argvars, typval_T *rettv);
377static void f_strpart(typval_T *argvars, typval_T *rettv);
378static void f_strridx(typval_T *argvars, typval_T *rettv);
379static void f_strtrans(typval_T *argvars, typval_T *rettv);
380static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
381static void f_strwidth(typval_T *argvars, typval_T *rettv);
382static void f_submatch(typval_T *argvars, typval_T *rettv);
383static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200384static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200385static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200386static void f_synID(typval_T *argvars, typval_T *rettv);
387static void f_synIDattr(typval_T *argvars, typval_T *rettv);
388static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
389static void f_synstack(typval_T *argvars, typval_T *rettv);
390static void f_synconcealed(typval_T *argvars, typval_T *rettv);
391static void f_system(typval_T *argvars, typval_T *rettv);
392static void f_systemlist(typval_T *argvars, typval_T *rettv);
393static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
394static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
395static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
396static void f_taglist(typval_T *argvars, typval_T *rettv);
397static void f_tagfiles(typval_T *argvars, typval_T *rettv);
398static void f_tempname(typval_T *argvars, typval_T *rettv);
399static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
400static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200401static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareda65222019-05-16 20:29:44 +0200402static void f_test_getvalue(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200403static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100404static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100405static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200406static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaaradc67142019-06-22 01:40:42 +0200407static void f_test_garbagecollect_soon(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100408static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100409static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200410#ifdef FEAT_JOB_CHANNEL
411static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
412#endif
413static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
414#ifdef FEAT_JOB_CHANNEL
415static void f_test_null_job(typval_T *argvars, typval_T *rettv);
416#endif
417static void f_test_null_list(typval_T *argvars, typval_T *rettv);
418static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
419static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200420#ifdef FEAT_GUI
421static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
422#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200423#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200424static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200425#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200426static void f_test_settime(typval_T *argvars, typval_T *rettv);
427#ifdef FEAT_FLOAT
428static void f_tan(typval_T *argvars, typval_T *rettv);
429static void f_tanh(typval_T *argvars, typval_T *rettv);
430#endif
431#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200432static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200433static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200434static void f_timer_start(typval_T *argvars, typval_T *rettv);
435static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200436static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200437#endif
438static void f_tolower(typval_T *argvars, typval_T *rettv);
439static void f_toupper(typval_T *argvars, typval_T *rettv);
440static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100441static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200442#ifdef FEAT_FLOAT
443static void f_trunc(typval_T *argvars, typval_T *rettv);
444#endif
445static void f_type(typval_T *argvars, typval_T *rettv);
446static void f_undofile(typval_T *argvars, typval_T *rettv);
447static void f_undotree(typval_T *argvars, typval_T *rettv);
448static void f_uniq(typval_T *argvars, typval_T *rettv);
449static void f_values(typval_T *argvars, typval_T *rettv);
450static void f_virtcol(typval_T *argvars, typval_T *rettv);
451static void f_visualmode(typval_T *argvars, typval_T *rettv);
452static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200453static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200454static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
455static void f_win_getid(typval_T *argvars, typval_T *rettv);
456static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
457static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
458static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100459static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200460static void f_winbufnr(typval_T *argvars, typval_T *rettv);
461static void f_wincol(typval_T *argvars, typval_T *rettv);
462static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200463static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200464static void f_winline(typval_T *argvars, typval_T *rettv);
465static void f_winnr(typval_T *argvars, typval_T *rettv);
466static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
467static void f_winrestview(typval_T *argvars, typval_T *rettv);
468static void f_winsaveview(typval_T *argvars, typval_T *rettv);
469static void f_winwidth(typval_T *argvars, typval_T *rettv);
470static void f_writefile(typval_T *argvars, typval_T *rettv);
471static void f_wordcount(typval_T *argvars, typval_T *rettv);
472static void f_xor(typval_T *argvars, typval_T *rettv);
473
474/*
475 * Array with names and number of arguments of all internal functions
476 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
477 */
478static struct fst
479{
480 char *f_name; /* function name */
481 char f_min_argc; /* minimal number of arguments */
482 char f_max_argc; /* maximal number of arguments */
483 void (*f_func)(typval_T *args, typval_T *rvar);
484 /* implementation of function */
485} functions[] =
486{
487#ifdef FEAT_FLOAT
488 {"abs", 1, 1, f_abs},
489 {"acos", 1, 1, f_acos}, /* WJMc */
490#endif
491 {"add", 2, 2, f_add},
492 {"and", 2, 2, f_and},
493 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200494 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200495 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200496 {"argidx", 0, 0, f_argidx},
497 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200498 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200499#ifdef FEAT_FLOAT
500 {"asin", 1, 1, f_asin}, /* WJMc */
501#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100502 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200503 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100504 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200506 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100508 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200509 {"assert_match", 2, 3, f_assert_match},
510 {"assert_notequal", 2, 3, f_assert_notequal},
511 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100512 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200513 {"assert_true", 1, 2, f_assert_true},
514#ifdef FEAT_FLOAT
515 {"atan", 1, 1, f_atan},
516 {"atan2", 2, 2, f_atan2},
517#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100518#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200519 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100520 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100521# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100522 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100523# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100524#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200525 {"browse", 4, 4, f_browse},
526 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200527 {"bufadd", 1, 1, f_bufadd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200528 {"bufexists", 1, 1, f_bufexists},
529 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
530 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
531 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
532 {"buflisted", 1, 1, f_buflisted},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200533 {"bufload", 1, 1, f_bufload},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200534 {"bufloaded", 1, 1, f_bufloaded},
535 {"bufname", 1, 1, f_bufname},
536 {"bufnr", 1, 2, f_bufnr},
537 {"bufwinid", 1, 1, f_bufwinid},
538 {"bufwinnr", 1, 1, f_bufwinnr},
539 {"byte2line", 1, 1, f_byte2line},
540 {"byteidx", 2, 2, f_byteidx},
541 {"byteidxcomp", 2, 2, f_byteidxcomp},
542 {"call", 2, 3, f_call},
543#ifdef FEAT_FLOAT
544 {"ceil", 1, 1, f_ceil},
545#endif
546#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100547 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200549 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
551 {"ch_evalraw", 2, 3, f_ch_evalraw},
552 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
553 {"ch_getjob", 1, 1, f_ch_getjob},
554 {"ch_info", 1, 1, f_ch_info},
555 {"ch_log", 1, 2, f_ch_log},
556 {"ch_logfile", 1, 2, f_ch_logfile},
557 {"ch_open", 1, 2, f_ch_open},
558 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100559 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200560 {"ch_readraw", 1, 2, f_ch_readraw},
561 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
562 {"ch_sendraw", 2, 3, f_ch_sendraw},
563 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200564 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200565#endif
566 {"changenr", 0, 0, f_changenr},
567 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200568 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200569 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100570 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200571 {"col", 1, 1, f_col},
572#if defined(FEAT_INS_EXPAND)
573 {"complete", 2, 2, f_complete},
574 {"complete_add", 1, 1, f_complete_add},
575 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100576 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200577#endif
578 {"confirm", 1, 4, f_confirm},
579 {"copy", 1, 1, f_copy},
580#ifdef FEAT_FLOAT
581 {"cos", 1, 1, f_cos},
582 {"cosh", 1, 1, f_cosh},
583#endif
584 {"count", 2, 4, f_count},
585 {"cscope_connection",0,3, f_cscope_connection},
586 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100587#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200588 {"debugbreak", 1, 1, f_debugbreak},
589#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200590 {"deepcopy", 1, 2, f_deepcopy},
591 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200592 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200593 {"did_filetype", 0, 0, f_did_filetype},
594 {"diff_filler", 1, 1, f_diff_filler},
595 {"diff_hlID", 2, 2, f_diff_hlID},
596 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200597 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200598 {"escape", 2, 2, f_escape},
599 {"eval", 1, 1, f_eval},
600 {"eventhandler", 0, 0, f_eventhandler},
601 {"executable", 1, 1, f_executable},
602 {"execute", 1, 2, f_execute},
603 {"exepath", 1, 1, f_exepath},
604 {"exists", 1, 1, f_exists},
605#ifdef FEAT_FLOAT
606 {"exp", 1, 1, f_exp},
607#endif
608 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200609 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610 {"extend", 2, 3, f_extend},
611 {"feedkeys", 1, 2, f_feedkeys},
612 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
613 {"filereadable", 1, 1, f_filereadable},
614 {"filewritable", 1, 1, f_filewritable},
615 {"filter", 2, 2, f_filter},
616 {"finddir", 1, 3, f_finddir},
617 {"findfile", 1, 3, f_findfile},
618#ifdef FEAT_FLOAT
619 {"float2nr", 1, 1, f_float2nr},
620 {"floor", 1, 1, f_floor},
621 {"fmod", 2, 2, f_fmod},
622#endif
623 {"fnameescape", 1, 1, f_fnameescape},
624 {"fnamemodify", 2, 2, f_fnamemodify},
625 {"foldclosed", 1, 1, f_foldclosed},
626 {"foldclosedend", 1, 1, f_foldclosedend},
627 {"foldlevel", 1, 1, f_foldlevel},
628 {"foldtext", 0, 0, f_foldtext},
629 {"foldtextresult", 1, 1, f_foldtextresult},
630 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200631 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"function", 1, 3, f_function},
633 {"garbagecollect", 0, 1, f_garbagecollect},
634 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200635 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636 {"getbufline", 2, 3, f_getbufline},
637 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100638 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"getchar", 0, 1, f_getchar},
640 {"getcharmod", 0, 0, f_getcharmod},
641 {"getcharsearch", 0, 0, f_getcharsearch},
642 {"getcmdline", 0, 0, f_getcmdline},
643 {"getcmdpos", 0, 0, f_getcmdpos},
644 {"getcmdtype", 0, 0, f_getcmdtype},
645 {"getcmdwintype", 0, 0, f_getcmdwintype},
646#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200647 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200648#endif
649 {"getcurpos", 0, 0, f_getcurpos},
650 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200651 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200652 {"getfontname", 0, 1, f_getfontname},
653 {"getfperm", 1, 1, f_getfperm},
654 {"getfsize", 1, 1, f_getfsize},
655 {"getftime", 1, 1, f_getftime},
656 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100657 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200658 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200659 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100660 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200661 {"getpid", 0, 0, f_getpid},
662 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200663 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200664 {"getreg", 0, 3, f_getreg},
665 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200666 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200667 {"gettabvar", 2, 3, f_gettabvar},
668 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100669 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200670 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100671 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200672 {"getwinposx", 0, 0, f_getwinposx},
673 {"getwinposy", 0, 0, f_getwinposy},
674 {"getwinvar", 2, 3, f_getwinvar},
675 {"glob", 1, 4, f_glob},
676 {"glob2regpat", 1, 1, f_glob2regpat},
677 {"globpath", 2, 5, f_globpath},
678 {"has", 1, 1, f_has},
679 {"has_key", 2, 2, f_has_key},
680 {"haslocaldir", 0, 2, f_haslocaldir},
681 {"hasmapto", 1, 3, f_hasmapto},
682 {"highlightID", 1, 1, f_hlID}, /* obsolete */
683 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
684 {"histadd", 2, 2, f_histadd},
685 {"histdel", 1, 2, f_histdel},
686 {"histget", 1, 2, f_histget},
687 {"histnr", 1, 1, f_histnr},
688 {"hlID", 1, 1, f_hlID},
689 {"hlexists", 1, 1, f_hlexists},
690 {"hostname", 0, 0, f_hostname},
691 {"iconv", 3, 3, f_iconv},
692 {"indent", 1, 1, f_indent},
693 {"index", 2, 4, f_index},
694 {"input", 1, 3, f_input},
695 {"inputdialog", 1, 3, f_inputdialog},
696 {"inputlist", 1, 1, f_inputlist},
697 {"inputrestore", 0, 0, f_inputrestore},
698 {"inputsave", 0, 0, f_inputsave},
699 {"inputsecret", 1, 2, f_inputsecret},
700 {"insert", 2, 3, f_insert},
701 {"invert", 1, 1, f_invert},
702 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200703#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
704 {"isinf", 1, 1, f_isinf},
705#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200706 {"islocked", 1, 1, f_islocked},
707#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
708 {"isnan", 1, 1, f_isnan},
709#endif
710 {"items", 1, 1, f_items},
711#ifdef FEAT_JOB_CHANNEL
712 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200713 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200714 {"job_setoptions", 2, 2, f_job_setoptions},
715 {"job_start", 1, 2, f_job_start},
716 {"job_status", 1, 1, f_job_status},
717 {"job_stop", 1, 2, f_job_stop},
718#endif
719 {"join", 1, 2, f_join},
720 {"js_decode", 1, 1, f_js_decode},
721 {"js_encode", 1, 1, f_js_encode},
722 {"json_decode", 1, 1, f_json_decode},
723 {"json_encode", 1, 1, f_json_encode},
724 {"keys", 1, 1, f_keys},
725 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
726 {"len", 1, 1, f_len},
727 {"libcall", 3, 3, f_libcall},
728 {"libcallnr", 3, 3, f_libcallnr},
729 {"line", 1, 1, f_line},
730 {"line2byte", 1, 1, f_line2byte},
731 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200732 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200733 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200734 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200735 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200736 {"localtime", 0, 0, f_localtime},
737#ifdef FEAT_FLOAT
738 {"log", 1, 1, f_log},
739 {"log10", 1, 1, f_log10},
740#endif
741#ifdef FEAT_LUA
742 {"luaeval", 1, 2, f_luaeval},
743#endif
744 {"map", 2, 2, f_map},
745 {"maparg", 1, 4, f_maparg},
746 {"mapcheck", 1, 3, f_mapcheck},
747 {"match", 2, 4, f_match},
748 {"matchadd", 2, 5, f_matchadd},
749 {"matchaddpos", 2, 5, f_matchaddpos},
750 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100751 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200752 {"matchend", 2, 4, f_matchend},
753 {"matchlist", 2, 4, f_matchlist},
754 {"matchstr", 2, 4, f_matchstr},
755 {"matchstrpos", 2, 4, f_matchstrpos},
756 {"max", 1, 1, f_max},
757 {"min", 1, 1, f_min},
758#ifdef vim_mkdir
759 {"mkdir", 1, 3, f_mkdir},
760#endif
761 {"mode", 0, 1, f_mode},
762#ifdef FEAT_MZSCHEME
763 {"mzeval", 1, 1, f_mzeval},
764#endif
765 {"nextnonblank", 1, 1, f_nextnonblank},
766 {"nr2char", 1, 2, f_nr2char},
767 {"or", 2, 2, f_or},
768 {"pathshorten", 1, 1, f_pathshorten},
769#ifdef FEAT_PERL
770 {"perleval", 1, 1, f_perleval},
771#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200772#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200773 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200774 {"popup_beval", 2, 2, f_popup_beval},
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200775 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200776 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200777 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200778 {"popup_dialog", 2, 2, f_popup_dialog},
Bram Moolenaara730e552019-06-16 19:05:31 +0200779 {"popup_filter_menu", 2, 2, f_popup_filter_menu},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200780 {"popup_filter_yesno", 2, 2, f_popup_filter_yesno},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200781 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200782 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200783 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200784 {"popup_locate", 2, 2, f_popup_locate},
Bram Moolenaara730e552019-06-16 19:05:31 +0200785 {"popup_menu", 2, 2, f_popup_menu},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200786 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200787 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaarae943152019-06-16 22:54:14 +0200788 {"popup_setoptions", 2, 2, f_popup_setoptions},
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200789 {"popup_settext", 2, 2, f_popup_settext},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200790 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200791#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200792#ifdef FEAT_FLOAT
793 {"pow", 2, 2, f_pow},
794#endif
795 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100796 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200797#ifdef FEAT_JOB_CHANNEL
798 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200799 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200800 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
801#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100802#ifdef FEAT_TEXT_PROP
803 {"prop_add", 3, 3, f_prop_add},
804 {"prop_clear", 1, 3, f_prop_clear},
805 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100806 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100807 {"prop_type_add", 2, 2, f_prop_type_add},
808 {"prop_type_change", 2, 2, f_prop_type_change},
809 {"prop_type_delete", 1, 2, f_prop_type_delete},
810 {"prop_type_get", 1, 2, f_prop_type_get},
811 {"prop_type_list", 0, 1, f_prop_type_list},
812#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200813 {"pumvisible", 0, 0, f_pumvisible},
814#ifdef FEAT_PYTHON3
815 {"py3eval", 1, 1, f_py3eval},
816#endif
817#ifdef FEAT_PYTHON
818 {"pyeval", 1, 1, f_pyeval},
819#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100820#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
821 {"pyxeval", 1, 1, f_pyxeval},
822#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200823 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200824 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200825 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200826 {"reg_executing", 0, 0, f_reg_executing},
827 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200828 {"reltime", 0, 2, f_reltime},
829#ifdef FEAT_FLOAT
830 {"reltimefloat", 1, 1, f_reltimefloat},
831#endif
832 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100833 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200834 {"remote_foreground", 1, 1, f_remote_foreground},
835 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100836 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200837 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100838 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200839 {"remove", 2, 3, f_remove},
840 {"rename", 2, 2, f_rename},
841 {"repeat", 2, 2, f_repeat},
842 {"resolve", 1, 1, f_resolve},
843 {"reverse", 1, 1, f_reverse},
844#ifdef FEAT_FLOAT
845 {"round", 1, 1, f_round},
846#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100847#ifdef FEAT_RUBY
848 {"rubyeval", 1, 1, f_rubyeval},
849#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200850 {"screenattr", 2, 2, f_screenattr},
851 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100852 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200853 {"screencol", 0, 0, f_screencol},
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200854 {"screenpos", 3, 3, f_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100856 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200857 {"search", 1, 4, f_search},
858 {"searchdecl", 1, 3, f_searchdecl},
859 {"searchpair", 3, 7, f_searchpair},
860 {"searchpairpos", 3, 7, f_searchpairpos},
861 {"searchpos", 1, 4, f_searchpos},
862 {"server2client", 2, 2, f_server2client},
863 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200864 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865 {"setbufvar", 3, 3, f_setbufvar},
866 {"setcharsearch", 1, 1, f_setcharsearch},
867 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200868 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200869 {"setfperm", 2, 2, f_setfperm},
870 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200871 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100872 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200873 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200874 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200875 {"setreg", 2, 3, f_setreg},
876 {"settabvar", 3, 3, f_settabvar},
877 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100878 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200879 {"setwinvar", 3, 3, f_setwinvar},
880#ifdef FEAT_CRYPT
881 {"sha256", 1, 1, f_sha256},
882#endif
883 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100884 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100885#ifdef FEAT_SIGNS
886 {"sign_define", 1, 2, f_sign_define},
887 {"sign_getdefined", 0, 1, f_sign_getdefined},
888 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100889 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100890 {"sign_place", 4, 5, f_sign_place},
891 {"sign_undefine", 0, 1, f_sign_undefine},
892 {"sign_unplace", 1, 2, f_sign_unplace},
893#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200894 {"simplify", 1, 1, f_simplify},
895#ifdef FEAT_FLOAT
896 {"sin", 1, 1, f_sin},
897 {"sinh", 1, 1, f_sinh},
898#endif
899 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200900#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200901 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200902 {"sound_playevent", 1, 2, f_sound_playevent},
903 {"sound_playfile", 1, 2, f_sound_playfile},
904 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200905#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200906 {"soundfold", 1, 1, f_soundfold},
907 {"spellbadword", 0, 1, f_spellbadword},
908 {"spellsuggest", 1, 3, f_spellsuggest},
909 {"split", 1, 3, f_split},
910#ifdef FEAT_FLOAT
911 {"sqrt", 1, 1, f_sqrt},
912 {"str2float", 1, 1, f_str2float},
913#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200914 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200915 {"str2nr", 1, 2, f_str2nr},
916 {"strcharpart", 2, 3, f_strcharpart},
917 {"strchars", 1, 2, f_strchars},
918 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
919#ifdef HAVE_STRFTIME
920 {"strftime", 1, 2, f_strftime},
921#endif
922 {"strgetchar", 2, 2, f_strgetchar},
923 {"stridx", 2, 3, f_stridx},
924 {"string", 1, 1, f_string},
925 {"strlen", 1, 1, f_strlen},
926 {"strpart", 2, 3, f_strpart},
927 {"strridx", 2, 3, f_strridx},
928 {"strtrans", 1, 1, f_strtrans},
929 {"strwidth", 1, 1, f_strwidth},
930 {"submatch", 1, 2, f_submatch},
931 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200932 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200933 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200934 {"synID", 3, 3, f_synID},
935 {"synIDattr", 2, 3, f_synIDattr},
936 {"synIDtrans", 1, 1, f_synIDtrans},
937 {"synconcealed", 2, 2, f_synconcealed},
938 {"synstack", 2, 2, f_synstack},
939 {"system", 1, 2, f_system},
940 {"systemlist", 1, 2, f_systemlist},
941 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
942 {"tabpagenr", 0, 1, f_tabpagenr},
943 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
944 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100945 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200946#ifdef FEAT_FLOAT
947 {"tan", 1, 1, f_tan},
948 {"tanh", 1, 1, f_tanh},
949#endif
950 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200951#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100952 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
953 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100954 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200955 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200956# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
957 {"term_getansicolors", 1, 1, f_term_getansicolors},
958# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200959 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200960 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200961 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200962 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200963 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200964 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200965 {"term_getstatus", 1, 1, f_term_getstatus},
966 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200967 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200968 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200969 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200970 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200971# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
972 {"term_setansicolors", 2, 2, f_term_setansicolors},
973# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100974 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100975 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200976 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200977 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200978 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200979#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200980 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
981 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200982 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200983 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaaradc67142019-06-22 01:40:42 +0200984 {"test_garbagecollect_soon", 0, 0, f_test_garbagecollect_soon},
Bram Moolenaareda65222019-05-16 20:29:44 +0200985 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100986 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100987 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200988#ifdef FEAT_JOB_CHANNEL
989 {"test_null_channel", 0, 0, f_test_null_channel},
990#endif
991 {"test_null_dict", 0, 0, f_test_null_dict},
992#ifdef FEAT_JOB_CHANNEL
993 {"test_null_job", 0, 0, f_test_null_job},
994#endif
995 {"test_null_list", 0, 0, f_test_null_list},
996 {"test_null_partial", 0, 0, f_test_null_partial},
997 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200998 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100999 {"test_override", 2, 2, f_test_override},
1000 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001001#ifdef FEAT_GUI
1002 {"test_scrollbar", 3, 3, f_test_scrollbar},
1003#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001004#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001005 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001006#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001007 {"test_settime", 1, 1, f_test_settime},
1008#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001009 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001010 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001011 {"timer_start", 2, 3, f_timer_start},
1012 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001013 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001014#endif
1015 {"tolower", 1, 1, f_tolower},
1016 {"toupper", 1, 1, f_toupper},
1017 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001018 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001019#ifdef FEAT_FLOAT
1020 {"trunc", 1, 1, f_trunc},
1021#endif
1022 {"type", 1, 1, f_type},
1023 {"undofile", 1, 1, f_undofile},
1024 {"undotree", 0, 0, f_undotree},
1025 {"uniq", 1, 3, f_uniq},
1026 {"values", 1, 1, f_values},
1027 {"virtcol", 1, 1, f_virtcol},
1028 {"visualmode", 0, 1, f_visualmode},
1029 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001030 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001031 {"win_findbuf", 1, 1, f_win_findbuf},
1032 {"win_getid", 0, 2, f_win_getid},
1033 {"win_gotoid", 1, 1, f_win_gotoid},
1034 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1035 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001036 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001037 {"winbufnr", 1, 1, f_winbufnr},
1038 {"wincol", 0, 0, f_wincol},
1039 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001040 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001041 {"winline", 0, 0, f_winline},
1042 {"winnr", 0, 1, f_winnr},
1043 {"winrestcmd", 0, 0, f_winrestcmd},
1044 {"winrestview", 1, 1, f_winrestview},
1045 {"winsaveview", 0, 0, f_winsaveview},
1046 {"winwidth", 1, 1, f_winwidth},
1047 {"wordcount", 0, 0, f_wordcount},
1048 {"writefile", 2, 3, f_writefile},
1049 {"xor", 2, 2, f_xor},
1050};
1051
1052#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1053
1054/*
1055 * Function given to ExpandGeneric() to obtain the list of internal
1056 * or user defined function names.
1057 */
1058 char_u *
1059get_function_name(expand_T *xp, int idx)
1060{
1061 static int intidx = -1;
1062 char_u *name;
1063
1064 if (idx == 0)
1065 intidx = -1;
1066 if (intidx < 0)
1067 {
1068 name = get_user_func_name(xp, idx);
1069 if (name != NULL)
1070 return name;
1071 }
1072 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1073 {
1074 STRCPY(IObuff, functions[intidx].f_name);
1075 STRCAT(IObuff, "(");
1076 if (functions[intidx].f_max_argc == 0)
1077 STRCAT(IObuff, ")");
1078 return IObuff;
1079 }
1080
1081 return NULL;
1082}
1083
1084/*
1085 * Function given to ExpandGeneric() to obtain the list of internal or
1086 * user defined variable or function names.
1087 */
1088 char_u *
1089get_expr_name(expand_T *xp, int idx)
1090{
1091 static int intidx = -1;
1092 char_u *name;
1093
1094 if (idx == 0)
1095 intidx = -1;
1096 if (intidx < 0)
1097 {
1098 name = get_function_name(xp, idx);
1099 if (name != NULL)
1100 return name;
1101 }
1102 return get_user_var_name(xp, ++intidx);
1103}
1104
1105#endif /* FEAT_CMDL_COMPL */
1106
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001107/*
1108 * Find internal function in table above.
1109 * Return index, or -1 if not found
1110 */
1111 int
1112find_internal_func(
1113 char_u *name) /* name of the function */
1114{
1115 int first = 0;
1116 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1117 int cmp;
1118 int x;
1119
1120 /*
1121 * Find the function name in the table. Binary search.
1122 */
1123 while (first <= last)
1124 {
1125 x = first + ((unsigned)(last - first) >> 1);
1126 cmp = STRCMP(name, functions[x].f_name);
1127 if (cmp < 0)
1128 last = x - 1;
1129 else if (cmp > 0)
1130 first = x + 1;
1131 else
1132 return x;
1133 }
1134 return -1;
1135}
1136
1137 int
1138call_internal_func(
1139 char_u *name,
1140 int argcount,
1141 typval_T *argvars,
1142 typval_T *rettv)
1143{
1144 int i;
1145
1146 i = find_internal_func(name);
1147 if (i < 0)
1148 return ERROR_UNKNOWN;
1149 if (argcount < functions[i].f_min_argc)
1150 return ERROR_TOOFEW;
1151 if (argcount > functions[i].f_max_argc)
1152 return ERROR_TOOMANY;
1153 argvars[argcount].v_type = VAR_UNKNOWN;
1154 functions[i].f_func(argvars, rettv);
1155 return ERROR_NONE;
1156}
1157
1158/*
1159 * Return TRUE for a non-zero Number and a non-empty String.
1160 */
1161 static int
1162non_zero_arg(typval_T *argvars)
1163{
1164 return ((argvars[0].v_type == VAR_NUMBER
1165 && argvars[0].vval.v_number != 0)
1166 || (argvars[0].v_type == VAR_SPECIAL
1167 && argvars[0].vval.v_number == VVAL_TRUE)
1168 || (argvars[0].v_type == VAR_STRING
1169 && argvars[0].vval.v_string != NULL
1170 && *argvars[0].vval.v_string != NUL));
1171}
1172
1173/*
1174 * Get the lnum from the first argument.
1175 * Also accepts ".", "$", etc., but that only works for the current buffer.
1176 * Returns -1 on error.
1177 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001178 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001179tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001180{
1181 typval_T rettv;
1182 linenr_T lnum;
1183
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001184 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001185 if (lnum == 0) /* no valid number, try using line() */
1186 {
1187 rettv.v_type = VAR_NUMBER;
1188 f_line(argvars, &rettv);
1189 lnum = (linenr_T)rettv.vval.v_number;
1190 clear_tv(&rettv);
1191 }
1192 return lnum;
1193}
1194
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001195/*
1196 * Get the lnum from the first argument.
1197 * Also accepts "$", then "buf" is used.
1198 * Returns 0 on error.
1199 */
1200 static linenr_T
1201tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1202{
1203 if (argvars[0].v_type == VAR_STRING
1204 && argvars[0].vval.v_string != NULL
1205 && argvars[0].vval.v_string[0] == '$'
1206 && buf != NULL)
1207 return buf->b_ml.ml_line_count;
1208 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1209}
1210
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001211#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001212/*
1213 * Get the float value of "argvars[0]" into "f".
1214 * Returns FAIL when the argument is not a Number or Float.
1215 */
1216 static int
1217get_float_arg(typval_T *argvars, float_T *f)
1218{
1219 if (argvars[0].v_type == VAR_FLOAT)
1220 {
1221 *f = argvars[0].vval.v_float;
1222 return OK;
1223 }
1224 if (argvars[0].v_type == VAR_NUMBER)
1225 {
1226 *f = (float_T)argvars[0].vval.v_number;
1227 return OK;
1228 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001229 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001230 return FAIL;
1231}
1232
1233/*
1234 * "abs(expr)" function
1235 */
1236 static void
1237f_abs(typval_T *argvars, typval_T *rettv)
1238{
1239 if (argvars[0].v_type == VAR_FLOAT)
1240 {
1241 rettv->v_type = VAR_FLOAT;
1242 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1243 }
1244 else
1245 {
1246 varnumber_T n;
1247 int error = FALSE;
1248
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001249 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001250 if (error)
1251 rettv->vval.v_number = -1;
1252 else if (n > 0)
1253 rettv->vval.v_number = n;
1254 else
1255 rettv->vval.v_number = -n;
1256 }
1257}
1258
1259/*
1260 * "acos()" function
1261 */
1262 static void
1263f_acos(typval_T *argvars, typval_T *rettv)
1264{
1265 float_T f = 0.0;
1266
1267 rettv->v_type = VAR_FLOAT;
1268 if (get_float_arg(argvars, &f) == OK)
1269 rettv->vval.v_float = acos(f);
1270 else
1271 rettv->vval.v_float = 0.0;
1272}
1273#endif
1274
1275/*
1276 * "add(list, item)" function
1277 */
1278 static void
1279f_add(typval_T *argvars, typval_T *rettv)
1280{
1281 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001282 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001283
1284 rettv->vval.v_number = 1; /* Default: Failed */
1285 if (argvars[0].v_type == VAR_LIST)
1286 {
1287 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001288 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001289 (char_u *)N_("add() argument"), TRUE)
1290 && list_append_tv(l, &argvars[1]) == OK)
1291 copy_tv(&argvars[0], rettv);
1292 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001293 else if (argvars[0].v_type == VAR_BLOB)
1294 {
1295 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001296 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001297 (char_u *)N_("add() argument"), TRUE))
1298 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001299 int error = FALSE;
1300 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1301
1302 if (!error)
1303 {
1304 ga_append(&b->bv_ga, (int)n);
1305 copy_tv(&argvars[0], rettv);
1306 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001307 }
1308 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001309 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001310 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001311}
1312
1313/*
1314 * "and(expr, expr)" function
1315 */
1316 static void
1317f_and(typval_T *argvars, typval_T *rettv)
1318{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001319 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1320 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001321}
1322
1323/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001324 * If there is a window for "curbuf", make it the current window.
1325 */
1326 static void
1327find_win_for_curbuf(void)
1328{
1329 wininfo_T *wip;
1330
1331 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1332 {
1333 if (wip->wi_win != NULL)
1334 {
1335 curwin = wip->wi_win;
1336 break;
1337 }
1338 }
1339}
1340
1341/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001342 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001343 */
1344 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001345set_buffer_lines(
1346 buf_T *buf,
1347 linenr_T lnum_arg,
1348 int append,
1349 typval_T *lines,
1350 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001351{
Bram Moolenaarca851592018-06-06 21:04:07 +02001352 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1353 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001354 list_T *l = NULL;
1355 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001356 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001357 linenr_T append_lnum;
1358 buf_T *curbuf_save = NULL;
1359 win_T *curwin_save = NULL;
1360 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001361
Bram Moolenaarca851592018-06-06 21:04:07 +02001362 /* When using the current buffer ml_mfp will be set if needed. Useful when
1363 * setline() is used on startup. For other buffers the buffer must be
1364 * loaded. */
1365 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001366 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001367 rettv->vval.v_number = 1; /* FAIL */
1368 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001369 }
1370
Bram Moolenaarca851592018-06-06 21:04:07 +02001371 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001373 curbuf_save = curbuf;
1374 curwin_save = curwin;
1375 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001376 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001377 }
1378
1379 if (append)
1380 // appendbufline() uses the line number below which we insert
1381 append_lnum = lnum - 1;
1382 else
1383 // setbufline() uses the line number above which we insert, we only
1384 // append if it's below the last line
1385 append_lnum = curbuf->b_ml.ml_line_count;
1386
1387 if (lines->v_type == VAR_LIST)
1388 {
1389 l = lines->vval.v_list;
1390 li = l->lv_first;
1391 }
1392 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001393 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001394
1395 /* default result is zero == OK */
1396 for (;;)
1397 {
1398 if (l != NULL)
1399 {
1400 /* list argument, get next string */
1401 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001402 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001403 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001404 li = li->li_next;
1405 }
1406
Bram Moolenaarca851592018-06-06 21:04:07 +02001407 rettv->vval.v_number = 1; /* FAIL */
1408 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1409 break;
1410
1411 /* When coming here from Insert mode, sync undo, so that this can be
1412 * undone separately from what was previously inserted. */
1413 if (u_sync_once == 2)
1414 {
1415 u_sync_once = 1; /* notify that u_sync() was called */
1416 u_sync(TRUE);
1417 }
1418
1419 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1420 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001421 // Existing line, replace it.
1422 // Removes any existing text properties.
1423 if (u_savesub(lnum) == OK && ml_replace_len(
1424 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001425 {
1426 changed_bytes(lnum, 0);
1427 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1428 check_cursor_col();
1429 rettv->vval.v_number = 0; /* OK */
1430 }
1431 }
1432 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1433 {
1434 /* append the line */
1435 ++added;
1436 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1437 rettv->vval.v_number = 0; /* OK */
1438 }
1439
1440 if (l == NULL) /* only one string argument */
1441 break;
1442 ++lnum;
1443 }
1444
1445 if (added > 0)
1446 {
1447 win_T *wp;
1448 tabpage_T *tp;
1449
1450 appended_lines_mark(append_lnum, added);
1451 FOR_ALL_TAB_WINDOWS(tp, wp)
1452 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1453 wp->w_cursor.lnum += added;
1454 check_cursor_col();
1455
Bram Moolenaarf2732452018-06-03 14:47:35 +02001456#ifdef FEAT_JOB_CHANNEL
1457 if (bt_prompt(curbuf) && (State & INSERT))
1458 // show the line with the prompt
1459 update_topline();
1460#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001461 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001462
1463 if (!is_curbuf)
1464 {
1465 curbuf = curbuf_save;
1466 curwin = curwin_save;
1467 }
1468}
1469
1470/*
1471 * "append(lnum, string/list)" function
1472 */
1473 static void
1474f_append(typval_T *argvars, typval_T *rettv)
1475{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001476 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001477
1478 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1479}
1480
1481/*
1482 * "appendbufline(buf, lnum, string/list)" function
1483 */
1484 static void
1485f_appendbufline(typval_T *argvars, typval_T *rettv)
1486{
1487 linenr_T lnum;
1488 buf_T *buf;
1489
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001490 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001491 if (buf == NULL)
1492 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001493 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001494 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001495 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001496 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1497 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001498}
1499
1500/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001501 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001502 */
1503 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001504f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001505{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001506 win_T *wp;
1507
1508 if (argvars[0].v_type == VAR_UNKNOWN)
1509 // use the current window
1510 rettv->vval.v_number = ARGCOUNT;
1511 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001512 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001513 // use the global argument list
1514 rettv->vval.v_number = GARGCOUNT;
1515 else
1516 {
1517 // use the argument list of the specified window
1518 wp = find_win_by_nr_or_id(&argvars[0]);
1519 if (wp != NULL)
1520 rettv->vval.v_number = WARGCOUNT(wp);
1521 else
1522 rettv->vval.v_number = -1;
1523 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001524}
1525
1526/*
1527 * "argidx()" function
1528 */
1529 static void
1530f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1531{
1532 rettv->vval.v_number = curwin->w_arg_idx;
1533}
1534
1535/*
1536 * "arglistid()" function
1537 */
1538 static void
1539f_arglistid(typval_T *argvars, typval_T *rettv)
1540{
1541 win_T *wp;
1542
1543 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001544 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001545 if (wp != NULL)
1546 rettv->vval.v_number = wp->w_alist->id;
1547}
1548
1549/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001550 * Get the argument list for a given window
1551 */
1552 static void
1553get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1554{
1555 int idx;
1556
1557 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1558 for (idx = 0; idx < argcount; ++idx)
1559 list_append_string(rettv->vval.v_list,
1560 alist_name(&arglist[idx]), -1);
1561}
1562
1563/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001564 * "argv(nr)" function
1565 */
1566 static void
1567f_argv(typval_T *argvars, typval_T *rettv)
1568{
1569 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001570 aentry_T *arglist = NULL;
1571 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001572
1573 if (argvars[0].v_type != VAR_UNKNOWN)
1574 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001575 if (argvars[1].v_type == VAR_UNKNOWN)
1576 {
1577 arglist = ARGLIST;
1578 argcount = ARGCOUNT;
1579 }
1580 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001581 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001582 {
1583 arglist = GARGLIST;
1584 argcount = GARGCOUNT;
1585 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001586 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001587 {
1588 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1589
1590 if (wp != NULL)
1591 {
1592 /* Use the argument list of the specified window */
1593 arglist = WARGLIST(wp);
1594 argcount = WARGCOUNT(wp);
1595 }
1596 }
1597
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001598 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001599 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001600 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001601 if (arglist != NULL && idx >= 0 && idx < argcount)
1602 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1603 else if (idx == -1)
1604 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001606 else
1607 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001608}
1609
1610/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001611 * "assert_beeps(cmd [, error])" function
1612 */
1613 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001614f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001615{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001616 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001617}
1618
1619/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001620 * "assert_equal(expected, actual[, msg])" function
1621 */
1622 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001623f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001624{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001625 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001626}
1627
1628/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001629 * "assert_equalfile(fname-one, fname-two)" function
1630 */
1631 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001632f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001633{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001634 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001635}
1636
1637/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001638 * "assert_notequal(expected, actual[, msg])" function
1639 */
1640 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001641f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001642{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001643 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001644}
1645
1646/*
1647 * "assert_exception(string[, msg])" function
1648 */
1649 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001650f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001651{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001652 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001653}
1654
1655/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001656 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657 */
1658 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001659f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001660{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001661 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001662}
1663
1664/*
1665 * "assert_false(actual[, msg])" function
1666 */
1667 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001668f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001669{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001670 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001671}
1672
1673/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001674 * "assert_inrange(lower, upper[, msg])" function
1675 */
1676 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001677f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001678{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001679 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001680}
1681
1682/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001683 * "assert_match(pattern, actual[, msg])" function
1684 */
1685 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001686f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001687{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001688 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001689}
1690
1691/*
1692 * "assert_notmatch(pattern, actual[, msg])" function
1693 */
1694 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001695f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001696{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001697 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001698}
1699
1700/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001701 * "assert_report(msg)" function
1702 */
1703 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001704f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001705{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001706 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001707}
1708
1709/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001710 * "assert_true(actual[, msg])" function
1711 */
1712 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001713f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001714{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001715 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001716}
1717
1718#ifdef FEAT_FLOAT
1719/*
1720 * "asin()" function
1721 */
1722 static void
1723f_asin(typval_T *argvars, typval_T *rettv)
1724{
1725 float_T f = 0.0;
1726
1727 rettv->v_type = VAR_FLOAT;
1728 if (get_float_arg(argvars, &f) == OK)
1729 rettv->vval.v_float = asin(f);
1730 else
1731 rettv->vval.v_float = 0.0;
1732}
1733
1734/*
1735 * "atan()" function
1736 */
1737 static void
1738f_atan(typval_T *argvars, typval_T *rettv)
1739{
1740 float_T f = 0.0;
1741
1742 rettv->v_type = VAR_FLOAT;
1743 if (get_float_arg(argvars, &f) == OK)
1744 rettv->vval.v_float = atan(f);
1745 else
1746 rettv->vval.v_float = 0.0;
1747}
1748
1749/*
1750 * "atan2()" function
1751 */
1752 static void
1753f_atan2(typval_T *argvars, typval_T *rettv)
1754{
1755 float_T fx = 0.0, fy = 0.0;
1756
1757 rettv->v_type = VAR_FLOAT;
1758 if (get_float_arg(argvars, &fx) == OK
1759 && get_float_arg(&argvars[1], &fy) == OK)
1760 rettv->vval.v_float = atan2(fx, fy);
1761 else
1762 rettv->vval.v_float = 0.0;
1763}
1764#endif
1765
1766/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001767 * "balloon_show()" function
1768 */
1769#ifdef FEAT_BEVAL
1770 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001771f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1772{
1773 rettv->v_type = VAR_STRING;
1774 if (balloonEval != NULL)
1775 {
1776 if (balloonEval->msg == NULL)
1777 rettv->vval.v_string = NULL;
1778 else
1779 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1780 }
1781}
1782
1783 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001784f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1785{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001786 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001787 {
1788 if (argvars[0].v_type == VAR_LIST
1789# ifdef FEAT_GUI
1790 && !gui.in_use
1791# endif
1792 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001793 {
1794 list_T *l = argvars[0].vval.v_list;
1795
1796 // empty list removes the balloon
1797 post_balloon(balloonEval, NULL,
1798 l == NULL || l->lv_len == 0 ? NULL : l);
1799 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001800 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001801 {
1802 char_u *mesg = tv_get_string_chk(&argvars[0]);
1803
1804 if (mesg != NULL)
1805 // empty string removes the balloon
1806 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1807 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001808 }
1809}
1810
Bram Moolenaar669a8282017-11-19 20:13:05 +01001811# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001812 static void
1813f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1814{
1815 if (rettv_list_alloc(rettv) == OK)
1816 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001817 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001818
1819 if (msg != NULL)
1820 {
1821 pumitem_T *array;
1822 int size = split_message(msg, &array);
1823 int i;
1824
1825 /* Skip the first and last item, they are always empty. */
1826 for (i = 1; i < size - 1; ++i)
1827 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001828 while (size > 0)
1829 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001830 vim_free(array);
1831 }
1832 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001833}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001834# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001835#endif
1836
1837/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001838 * "browse(save, title, initdir, default)" function
1839 */
1840 static void
1841f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1842{
1843#ifdef FEAT_BROWSE
1844 int save;
1845 char_u *title;
1846 char_u *initdir;
1847 char_u *defname;
1848 char_u buf[NUMBUFLEN];
1849 char_u buf2[NUMBUFLEN];
1850 int error = FALSE;
1851
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001852 save = (int)tv_get_number_chk(&argvars[0], &error);
1853 title = tv_get_string_chk(&argvars[1]);
1854 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1855 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001856
1857 if (error || title == NULL || initdir == NULL || defname == NULL)
1858 rettv->vval.v_string = NULL;
1859 else
1860 rettv->vval.v_string =
1861 do_browse(save ? BROWSE_SAVE : 0,
1862 title, defname, NULL, initdir, NULL, curbuf);
1863#else
1864 rettv->vval.v_string = NULL;
1865#endif
1866 rettv->v_type = VAR_STRING;
1867}
1868
1869/*
1870 * "browsedir(title, initdir)" function
1871 */
1872 static void
1873f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1874{
1875#ifdef FEAT_BROWSE
1876 char_u *title;
1877 char_u *initdir;
1878 char_u buf[NUMBUFLEN];
1879
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001880 title = tv_get_string_chk(&argvars[0]);
1881 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001882
1883 if (title == NULL || initdir == NULL)
1884 rettv->vval.v_string = NULL;
1885 else
1886 rettv->vval.v_string = do_browse(BROWSE_DIR,
1887 title, NULL, NULL, initdir, NULL, curbuf);
1888#else
1889 rettv->vval.v_string = NULL;
1890#endif
1891 rettv->v_type = VAR_STRING;
1892}
1893
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001894/*
1895 * Find a buffer by number or exact name.
1896 */
1897 static buf_T *
1898find_buffer(typval_T *avar)
1899{
1900 buf_T *buf = NULL;
1901
1902 if (avar->v_type == VAR_NUMBER)
1903 buf = buflist_findnr((int)avar->vval.v_number);
1904 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1905 {
1906 buf = buflist_findname_exp(avar->vval.v_string);
1907 if (buf == NULL)
1908 {
1909 /* No full path name match, try a match with a URL or a "nofile"
1910 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001911 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001912 if (buf->b_fname != NULL
1913 && (path_with_url(buf->b_fname)
1914#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001915 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001916#endif
1917 )
1918 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1919 break;
1920 }
1921 }
1922 return buf;
1923}
1924
1925/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001926 * "bufadd(expr)" function
1927 */
1928 static void
1929f_bufadd(typval_T *argvars, typval_T *rettv)
1930{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001931 char_u *name = tv_get_string(&argvars[0]);
1932
1933 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001934}
1935
1936/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001937 * "bufexists(expr)" function
1938 */
1939 static void
1940f_bufexists(typval_T *argvars, typval_T *rettv)
1941{
1942 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1943}
1944
1945/*
1946 * "buflisted(expr)" function
1947 */
1948 static void
1949f_buflisted(typval_T *argvars, typval_T *rettv)
1950{
1951 buf_T *buf;
1952
1953 buf = find_buffer(&argvars[0]);
1954 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1955}
1956
1957/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001958 * "bufload(expr)" function
1959 */
1960 static void
1961f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1962{
1963 buf_T *buf = get_buf_arg(&argvars[0]);
1964
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001965 if (buf != NULL)
1966 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001967}
1968
1969/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001970 * "bufloaded(expr)" function
1971 */
1972 static void
1973f_bufloaded(typval_T *argvars, typval_T *rettv)
1974{
1975 buf_T *buf;
1976
1977 buf = find_buffer(&argvars[0]);
1978 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1979}
1980
1981 buf_T *
1982buflist_find_by_name(char_u *name, int curtab_only)
1983{
1984 int save_magic;
1985 char_u *save_cpo;
1986 buf_T *buf;
1987
1988 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1989 save_magic = p_magic;
1990 p_magic = TRUE;
1991 save_cpo = p_cpo;
1992 p_cpo = (char_u *)"";
1993
1994 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1995 TRUE, FALSE, curtab_only));
1996
1997 p_magic = save_magic;
1998 p_cpo = save_cpo;
1999 return buf;
2000}
2001
2002/*
2003 * Get buffer by number or pattern.
2004 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002005 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002006tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002007{
2008 char_u *name = tv->vval.v_string;
2009 buf_T *buf;
2010
2011 if (tv->v_type == VAR_NUMBER)
2012 return buflist_findnr((int)tv->vval.v_number);
2013 if (tv->v_type != VAR_STRING)
2014 return NULL;
2015 if (name == NULL || *name == NUL)
2016 return curbuf;
2017 if (name[0] == '$' && name[1] == NUL)
2018 return lastbuf;
2019
2020 buf = buflist_find_by_name(name, curtab_only);
2021
2022 /* If not found, try expanding the name, like done for bufexists(). */
2023 if (buf == NULL)
2024 buf = find_buffer(tv);
2025
2026 return buf;
2027}
2028
2029/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002030 * Get the buffer from "arg" and give an error and return NULL if it is not
2031 * valid.
2032 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002033 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002034get_buf_arg(typval_T *arg)
2035{
2036 buf_T *buf;
2037
2038 ++emsg_off;
2039 buf = tv_get_buf(arg, FALSE);
2040 --emsg_off;
2041 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002042 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002043 return buf;
2044}
2045
2046/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002047 * "bufname(expr)" function
2048 */
2049 static void
2050f_bufname(typval_T *argvars, typval_T *rettv)
2051{
2052 buf_T *buf;
2053
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002054 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002055 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002056 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002057 rettv->v_type = VAR_STRING;
2058 if (buf != NULL && buf->b_fname != NULL)
2059 rettv->vval.v_string = vim_strsave(buf->b_fname);
2060 else
2061 rettv->vval.v_string = NULL;
2062 --emsg_off;
2063}
2064
2065/*
2066 * "bufnr(expr)" function
2067 */
2068 static void
2069f_bufnr(typval_T *argvars, typval_T *rettv)
2070{
2071 buf_T *buf;
2072 int error = FALSE;
2073 char_u *name;
2074
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002075 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002076 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002077 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002078 --emsg_off;
2079
2080 /* If the buffer isn't found and the second argument is not zero create a
2081 * new buffer. */
2082 if (buf == NULL
2083 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002084 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002085 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002086 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002087 && !error)
2088 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2089
2090 if (buf != NULL)
2091 rettv->vval.v_number = buf->b_fnum;
2092 else
2093 rettv->vval.v_number = -1;
2094}
2095
2096 static void
2097buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2098{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002099 win_T *wp;
2100 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002101 buf_T *buf;
2102
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002103 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002104 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002105 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002106 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002107 {
2108 ++winnr;
2109 if (wp->w_buffer == buf)
2110 break;
2111 }
2112 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002113 --emsg_off;
2114}
2115
2116/*
2117 * "bufwinid(nr)" function
2118 */
2119 static void
2120f_bufwinid(typval_T *argvars, typval_T *rettv)
2121{
2122 buf_win_common(argvars, rettv, FALSE);
2123}
2124
2125/*
2126 * "bufwinnr(nr)" function
2127 */
2128 static void
2129f_bufwinnr(typval_T *argvars, typval_T *rettv)
2130{
2131 buf_win_common(argvars, rettv, TRUE);
2132}
2133
2134/*
2135 * "byte2line(byte)" function
2136 */
2137 static void
2138f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2139{
2140#ifndef FEAT_BYTEOFF
2141 rettv->vval.v_number = -1;
2142#else
2143 long boff = 0;
2144
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002145 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002146 if (boff < 0)
2147 rettv->vval.v_number = -1;
2148 else
2149 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2150 (linenr_T)0, &boff);
2151#endif
2152}
2153
2154 static void
2155byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2156{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002158 char_u *str;
2159 varnumber_T idx;
2160
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002161 str = tv_get_string_chk(&argvars[0]);
2162 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002163 rettv->vval.v_number = -1;
2164 if (str == NULL || idx < 0)
2165 return;
2166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002167 t = str;
2168 for ( ; idx > 0; idx--)
2169 {
2170 if (*t == NUL) /* EOL reached */
2171 return;
2172 if (enc_utf8 && comp)
2173 t += utf_ptr2len(t);
2174 else
2175 t += (*mb_ptr2len)(t);
2176 }
2177 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002178}
2179
2180/*
2181 * "byteidx()" function
2182 */
2183 static void
2184f_byteidx(typval_T *argvars, typval_T *rettv)
2185{
2186 byteidx(argvars, rettv, FALSE);
2187}
2188
2189/*
2190 * "byteidxcomp()" function
2191 */
2192 static void
2193f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2194{
2195 byteidx(argvars, rettv, TRUE);
2196}
2197
2198/*
2199 * "call(func, arglist [, dict])" function
2200 */
2201 static void
2202f_call(typval_T *argvars, typval_T *rettv)
2203{
2204 char_u *func;
2205 partial_T *partial = NULL;
2206 dict_T *selfdict = NULL;
2207
2208 if (argvars[1].v_type != VAR_LIST)
2209 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002210 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002211 return;
2212 }
2213 if (argvars[1].vval.v_list == NULL)
2214 return;
2215
2216 if (argvars[0].v_type == VAR_FUNC)
2217 func = argvars[0].vval.v_string;
2218 else if (argvars[0].v_type == VAR_PARTIAL)
2219 {
2220 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002221 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002222 }
2223 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002224 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002225 if (*func == NUL)
2226 return; /* type error or empty name */
2227
2228 if (argvars[2].v_type != VAR_UNKNOWN)
2229 {
2230 if (argvars[2].v_type != VAR_DICT)
2231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002232 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002233 return;
2234 }
2235 selfdict = argvars[2].vval.v_dict;
2236 }
2237
2238 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2239}
2240
2241#ifdef FEAT_FLOAT
2242/*
2243 * "ceil({float})" function
2244 */
2245 static void
2246f_ceil(typval_T *argvars, typval_T *rettv)
2247{
2248 float_T f = 0.0;
2249
2250 rettv->v_type = VAR_FLOAT;
2251 if (get_float_arg(argvars, &f) == OK)
2252 rettv->vval.v_float = ceil(f);
2253 else
2254 rettv->vval.v_float = 0.0;
2255}
2256#endif
2257
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002258/*
2259 * "changenr()" function
2260 */
2261 static void
2262f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2263{
2264 rettv->vval.v_number = curbuf->b_u_seq_cur;
2265}
2266
2267/*
2268 * "char2nr(string)" function
2269 */
2270 static void
2271f_char2nr(typval_T *argvars, typval_T *rettv)
2272{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002273 if (has_mbyte)
2274 {
2275 int utf8 = 0;
2276
2277 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002278 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002279
2280 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002281 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002282 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002283 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002284 }
2285 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002286 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002287}
2288
2289/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002290 * "chdir(dir)" function
2291 */
2292 static void
2293f_chdir(typval_T *argvars, typval_T *rettv)
2294{
2295 char_u *cwd;
2296 cdscope_T scope = CDSCOPE_GLOBAL;
2297
2298 rettv->v_type = VAR_STRING;
2299 rettv->vval.v_string = NULL;
2300
2301 if (argvars[0].v_type != VAR_STRING)
2302 return;
2303
2304 // Return the current directory
2305 cwd = alloc(MAXPATHL);
2306 if (cwd != NULL)
2307 {
2308 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2309 {
2310#ifdef BACKSLASH_IN_FILENAME
2311 slash_adjust(cwd);
2312#endif
2313 rettv->vval.v_string = vim_strsave(cwd);
2314 }
2315 vim_free(cwd);
2316 }
2317
2318 if (curwin->w_localdir != NULL)
2319 scope = CDSCOPE_WINDOW;
2320 else if (curtab->tp_localdir != NULL)
2321 scope = CDSCOPE_TABPAGE;
2322
2323 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2324 // Directory change failed
2325 VIM_CLEAR(rettv->vval.v_string);
2326}
2327
2328/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002329 * "cindent(lnum)" function
2330 */
2331 static void
2332f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2333{
2334#ifdef FEAT_CINDENT
2335 pos_T pos;
2336 linenr_T lnum;
2337
2338 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002339 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002340 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2341 {
2342 curwin->w_cursor.lnum = lnum;
2343 rettv->vval.v_number = get_c_indent();
2344 curwin->w_cursor = pos;
2345 }
2346 else
2347#endif
2348 rettv->vval.v_number = -1;
2349}
2350
Bram Moolenaaraff74912019-03-30 18:11:49 +01002351 static win_T *
2352get_optional_window(typval_T *argvars, int idx)
2353{
2354 win_T *win = curwin;
2355
2356 if (argvars[idx].v_type != VAR_UNKNOWN)
2357 {
2358 win = find_win_by_nr_or_id(&argvars[idx]);
2359 if (win == NULL)
2360 {
2361 emsg(_(e_invalwindow));
2362 return NULL;
2363 }
2364 }
2365 return win;
2366}
2367
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002368/*
2369 * "clearmatches()" function
2370 */
2371 static void
2372f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2373{
2374#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002375 win_T *win = get_optional_window(argvars, 0);
2376
2377 if (win != NULL)
2378 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379#endif
2380}
2381
2382/*
2383 * "col(string)" function
2384 */
2385 static void
2386f_col(typval_T *argvars, typval_T *rettv)
2387{
2388 colnr_T col = 0;
2389 pos_T *fp;
2390 int fnum = curbuf->b_fnum;
2391
2392 fp = var2fpos(&argvars[0], FALSE, &fnum);
2393 if (fp != NULL && fnum == curbuf->b_fnum)
2394 {
2395 if (fp->col == MAXCOL)
2396 {
2397 /* '> can be MAXCOL, get the length of the line then */
2398 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2399 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2400 else
2401 col = MAXCOL;
2402 }
2403 else
2404 {
2405 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002406 /* col(".") when the cursor is on the NUL at the end of the line
2407 * because of "coladd" can be seen as an extra column. */
2408 if (virtual_active() && fp == &curwin->w_cursor)
2409 {
2410 char_u *p = ml_get_cursor();
2411
2412 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2413 curwin->w_virtcol - curwin->w_cursor.coladd))
2414 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002415 int l;
2416
2417 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2418 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002419 }
2420 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002421 }
2422 }
2423 rettv->vval.v_number = col;
2424}
2425
2426#if defined(FEAT_INS_EXPAND)
2427/*
2428 * "complete()" function
2429 */
2430 static void
2431f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2432{
2433 int startcol;
2434
2435 if ((State & INSERT) == 0)
2436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002437 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002438 return;
2439 }
2440
2441 /* Check for undo allowed here, because if something was already inserted
2442 * the line was already saved for undo and this check isn't done. */
2443 if (!undo_allowed())
2444 return;
2445
2446 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2447 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002448 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002449 return;
2450 }
2451
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002452 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002453 if (startcol <= 0)
2454 return;
2455
2456 set_completion(startcol - 1, argvars[1].vval.v_list);
2457}
2458
2459/*
2460 * "complete_add()" function
2461 */
2462 static void
2463f_complete_add(typval_T *argvars, typval_T *rettv)
2464{
2465 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2466}
2467
2468/*
2469 * "complete_check()" function
2470 */
2471 static void
2472f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2473{
2474 int saved = RedrawingDisabled;
2475
2476 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002477 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002478 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002479 RedrawingDisabled = saved;
2480}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002481
2482/*
2483 * "complete_info()" function
2484 */
2485 static void
2486f_complete_info(typval_T *argvars, typval_T *rettv)
2487{
2488 list_T *what_list = NULL;
2489
2490 if (rettv_dict_alloc(rettv) != OK)
2491 return;
2492
2493 if (argvars[0].v_type != VAR_UNKNOWN)
2494 {
2495 if (argvars[0].v_type != VAR_LIST)
2496 {
2497 emsg(_(e_listreq));
2498 return;
2499 }
2500 what_list = argvars[0].vval.v_list;
2501 }
2502 get_complete_info(what_list, rettv->vval.v_dict);
2503}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002504#endif
2505
2506/*
2507 * "confirm(message, buttons[, default [, type]])" function
2508 */
2509 static void
2510f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2511{
2512#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2513 char_u *message;
2514 char_u *buttons = NULL;
2515 char_u buf[NUMBUFLEN];
2516 char_u buf2[NUMBUFLEN];
2517 int def = 1;
2518 int type = VIM_GENERIC;
2519 char_u *typestr;
2520 int error = FALSE;
2521
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002522 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002523 if (message == NULL)
2524 error = TRUE;
2525 if (argvars[1].v_type != VAR_UNKNOWN)
2526 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002527 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002528 if (buttons == NULL)
2529 error = TRUE;
2530 if (argvars[2].v_type != VAR_UNKNOWN)
2531 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002532 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002533 if (argvars[3].v_type != VAR_UNKNOWN)
2534 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002535 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002536 if (typestr == NULL)
2537 error = TRUE;
2538 else
2539 {
2540 switch (TOUPPER_ASC(*typestr))
2541 {
2542 case 'E': type = VIM_ERROR; break;
2543 case 'Q': type = VIM_QUESTION; break;
2544 case 'I': type = VIM_INFO; break;
2545 case 'W': type = VIM_WARNING; break;
2546 case 'G': type = VIM_GENERIC; break;
2547 }
2548 }
2549 }
2550 }
2551 }
2552
2553 if (buttons == NULL || *buttons == NUL)
2554 buttons = (char_u *)_("&Ok");
2555
2556 if (!error)
2557 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2558 def, NULL, FALSE);
2559#endif
2560}
2561
2562/*
2563 * "copy()" function
2564 */
2565 static void
2566f_copy(typval_T *argvars, typval_T *rettv)
2567{
2568 item_copy(&argvars[0], rettv, FALSE, 0);
2569}
2570
2571#ifdef FEAT_FLOAT
2572/*
2573 * "cos()" function
2574 */
2575 static void
2576f_cos(typval_T *argvars, typval_T *rettv)
2577{
2578 float_T f = 0.0;
2579
2580 rettv->v_type = VAR_FLOAT;
2581 if (get_float_arg(argvars, &f) == OK)
2582 rettv->vval.v_float = cos(f);
2583 else
2584 rettv->vval.v_float = 0.0;
2585}
2586
2587/*
2588 * "cosh()" function
2589 */
2590 static void
2591f_cosh(typval_T *argvars, typval_T *rettv)
2592{
2593 float_T f = 0.0;
2594
2595 rettv->v_type = VAR_FLOAT;
2596 if (get_float_arg(argvars, &f) == OK)
2597 rettv->vval.v_float = cosh(f);
2598 else
2599 rettv->vval.v_float = 0.0;
2600}
2601#endif
2602
2603/*
2604 * "count()" function
2605 */
2606 static void
2607f_count(typval_T *argvars, typval_T *rettv)
2608{
2609 long n = 0;
2610 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002611 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002612
Bram Moolenaar9966b212017-07-28 16:46:57 +02002613 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002614 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002615
2616 if (argvars[0].v_type == VAR_STRING)
2617 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002618 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002619 char_u *p = argvars[0].vval.v_string;
2620 char_u *next;
2621
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002622 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002623 {
2624 if (ic)
2625 {
2626 size_t len = STRLEN(expr);
2627
2628 while (*p != NUL)
2629 {
2630 if (MB_STRNICMP(p, expr, len) == 0)
2631 {
2632 ++n;
2633 p += len;
2634 }
2635 else
2636 MB_PTR_ADV(p);
2637 }
2638 }
2639 else
2640 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2641 != NULL)
2642 {
2643 ++n;
2644 p = next + STRLEN(expr);
2645 }
2646 }
2647
2648 }
2649 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002650 {
2651 listitem_T *li;
2652 list_T *l;
2653 long idx;
2654
2655 if ((l = argvars[0].vval.v_list) != NULL)
2656 {
2657 li = l->lv_first;
2658 if (argvars[2].v_type != VAR_UNKNOWN)
2659 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660 if (argvars[3].v_type != VAR_UNKNOWN)
2661 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002662 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002663 if (!error)
2664 {
2665 li = list_find(l, idx);
2666 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002667 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002668 }
2669 }
2670 if (error)
2671 li = NULL;
2672 }
2673
2674 for ( ; li != NULL; li = li->li_next)
2675 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2676 ++n;
2677 }
2678 }
2679 else if (argvars[0].v_type == VAR_DICT)
2680 {
2681 int todo;
2682 dict_T *d;
2683 hashitem_T *hi;
2684
2685 if ((d = argvars[0].vval.v_dict) != NULL)
2686 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687 if (argvars[2].v_type != VAR_UNKNOWN)
2688 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002689 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002690 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002691 }
2692
2693 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2694 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2695 {
2696 if (!HASHITEM_EMPTY(hi))
2697 {
2698 --todo;
2699 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2700 ++n;
2701 }
2702 }
2703 }
2704 }
2705 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002706 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002707 rettv->vval.v_number = n;
2708}
2709
2710/*
2711 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2712 *
2713 * Checks the existence of a cscope connection.
2714 */
2715 static void
2716f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2717{
2718#ifdef FEAT_CSCOPE
2719 int num = 0;
2720 char_u *dbpath = NULL;
2721 char_u *prepend = NULL;
2722 char_u buf[NUMBUFLEN];
2723
2724 if (argvars[0].v_type != VAR_UNKNOWN
2725 && argvars[1].v_type != VAR_UNKNOWN)
2726 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002727 num = (int)tv_get_number(&argvars[0]);
2728 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002729 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002730 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002731 }
2732
2733 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2734#endif
2735}
2736
2737/*
2738 * "cursor(lnum, col)" function, or
2739 * "cursor(list)"
2740 *
2741 * Moves the cursor to the specified line and column.
2742 * Returns 0 when the position could be set, -1 otherwise.
2743 */
2744 static void
2745f_cursor(typval_T *argvars, typval_T *rettv)
2746{
2747 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002748 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002749 int set_curswant = TRUE;
2750
2751 rettv->vval.v_number = -1;
2752 if (argvars[1].v_type == VAR_UNKNOWN)
2753 {
2754 pos_T pos;
2755 colnr_T curswant = -1;
2756
2757 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2758 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002759 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002760 return;
2761 }
2762 line = pos.lnum;
2763 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002764 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002765 if (curswant >= 0)
2766 {
2767 curwin->w_curswant = curswant - 1;
2768 set_curswant = FALSE;
2769 }
2770 }
2771 else
2772 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002773 line = tv_get_lnum(argvars);
2774 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002775 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002776 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002777 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002778 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002779 return; /* type error; errmsg already given */
2780 if (line > 0)
2781 curwin->w_cursor.lnum = line;
2782 if (col > 0)
2783 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785
2786 /* Make sure the cursor is in a valid position. */
2787 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002788 /* Correct cursor for multi-byte character. */
2789 if (has_mbyte)
2790 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002791
2792 curwin->w_set_curswant = set_curswant;
2793 rettv->vval.v_number = 0;
2794}
2795
Bram Moolenaar4f974752019-02-17 17:44:42 +01002796#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002797/*
2798 * "debugbreak()" function
2799 */
2800 static void
2801f_debugbreak(typval_T *argvars, typval_T *rettv)
2802{
2803 int pid;
2804
2805 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002806 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002807 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002808 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002809 else
2810 {
2811 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2812
2813 if (hProcess != NULL)
2814 {
2815 DebugBreakProcess(hProcess);
2816 CloseHandle(hProcess);
2817 rettv->vval.v_number = OK;
2818 }
2819 }
2820}
2821#endif
2822
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002823/*
2824 * "deepcopy()" function
2825 */
2826 static void
2827f_deepcopy(typval_T *argvars, typval_T *rettv)
2828{
2829 int noref = 0;
2830 int copyID;
2831
2832 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002833 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002834 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002835 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002836 else
2837 {
2838 copyID = get_copyID();
2839 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2840 }
2841}
2842
2843/*
2844 * "delete()" function
2845 */
2846 static void
2847f_delete(typval_T *argvars, typval_T *rettv)
2848{
2849 char_u nbuf[NUMBUFLEN];
2850 char_u *name;
2851 char_u *flags;
2852
2853 rettv->vval.v_number = -1;
2854 if (check_restricted() || check_secure())
2855 return;
2856
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002857 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002858 if (name == NULL || *name == NUL)
2859 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002860 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002861 return;
2862 }
2863
2864 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002865 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866 else
2867 flags = (char_u *)"";
2868
2869 if (*flags == NUL)
2870 /* delete a file */
2871 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2872 else if (STRCMP(flags, "d") == 0)
2873 /* delete an empty directory */
2874 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2875 else if (STRCMP(flags, "rf") == 0)
2876 /* delete a directory recursively */
2877 rettv->vval.v_number = delete_recursive(name);
2878 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002879 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002880}
2881
2882/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002883 * "deletebufline()" function
2884 */
2885 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002886f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002887{
2888 buf_T *buf;
2889 linenr_T first, last;
2890 linenr_T lnum;
2891 long count;
2892 int is_curbuf;
2893 buf_T *curbuf_save = NULL;
2894 win_T *curwin_save = NULL;
2895 tabpage_T *tp;
2896 win_T *wp;
2897
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002898 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002899 if (buf == NULL)
2900 {
2901 rettv->vval.v_number = 1; /* FAIL */
2902 return;
2903 }
2904 is_curbuf = buf == curbuf;
2905
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002906 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002907 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002908 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002909 else
2910 last = first;
2911
2912 if (buf->b_ml.ml_mfp == NULL || first < 1
2913 || first > buf->b_ml.ml_line_count || last < first)
2914 {
2915 rettv->vval.v_number = 1; /* FAIL */
2916 return;
2917 }
2918
2919 if (!is_curbuf)
2920 {
2921 curbuf_save = curbuf;
2922 curwin_save = curwin;
2923 curbuf = buf;
2924 find_win_for_curbuf();
2925 }
2926 if (last > curbuf->b_ml.ml_line_count)
2927 last = curbuf->b_ml.ml_line_count;
2928 count = last - first + 1;
2929
2930 // When coming here from Insert mode, sync undo, so that this can be
2931 // undone separately from what was previously inserted.
2932 if (u_sync_once == 2)
2933 {
2934 u_sync_once = 1; // notify that u_sync() was called
2935 u_sync(TRUE);
2936 }
2937
2938 if (u_save(first - 1, last + 1) == FAIL)
2939 {
2940 rettv->vval.v_number = 1; /* FAIL */
2941 return;
2942 }
2943
2944 for (lnum = first; lnum <= last; ++lnum)
2945 ml_delete(first, TRUE);
2946
2947 FOR_ALL_TAB_WINDOWS(tp, wp)
2948 if (wp->w_buffer == buf)
2949 {
2950 if (wp->w_cursor.lnum > last)
2951 wp->w_cursor.lnum -= count;
2952 else if (wp->w_cursor.lnum> first)
2953 wp->w_cursor.lnum = first;
2954 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2955 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2956 }
2957 check_cursor_col();
2958 deleted_lines_mark(first, count);
2959
2960 if (!is_curbuf)
2961 {
2962 curbuf = curbuf_save;
2963 curwin = curwin_save;
2964 }
2965}
2966
2967/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002968 * "did_filetype()" function
2969 */
2970 static void
2971f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2972{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002973 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002974}
2975
2976/*
2977 * "diff_filler()" function
2978 */
2979 static void
2980f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2981{
2982#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002983 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002984#endif
2985}
2986
2987/*
2988 * "diff_hlID()" function
2989 */
2990 static void
2991f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2992{
2993#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002994 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002996 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002997 static int fnum = 0;
2998 static int change_start = 0;
2999 static int change_end = 0;
3000 static hlf_T hlID = (hlf_T)0;
3001 int filler_lines;
3002 int col;
3003
3004 if (lnum < 0) /* ignore type error in {lnum} arg */
3005 lnum = 0;
3006 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003007 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003008 || fnum != curbuf->b_fnum)
3009 {
3010 /* New line, buffer, change: need to get the values. */
3011 filler_lines = diff_check(curwin, lnum);
3012 if (filler_lines < 0)
3013 {
3014 if (filler_lines == -1)
3015 {
3016 change_start = MAXCOL;
3017 change_end = -1;
3018 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3019 hlID = HLF_ADD; /* added line */
3020 else
3021 hlID = HLF_CHD; /* changed line */
3022 }
3023 else
3024 hlID = HLF_ADD; /* added line */
3025 }
3026 else
3027 hlID = (hlf_T)0;
3028 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003029 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003030 fnum = curbuf->b_fnum;
3031 }
3032
3033 if (hlID == HLF_CHD || hlID == HLF_TXD)
3034 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003035 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003036 if (col >= change_start && col <= change_end)
3037 hlID = HLF_TXD; /* changed text */
3038 else
3039 hlID = HLF_CHD; /* changed line */
3040 }
3041 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3042#endif
3043}
3044
3045/*
3046 * "empty({expr})" function
3047 */
3048 static void
3049f_empty(typval_T *argvars, typval_T *rettv)
3050{
3051 int n = FALSE;
3052
3053 switch (argvars[0].v_type)
3054 {
3055 case VAR_STRING:
3056 case VAR_FUNC:
3057 n = argvars[0].vval.v_string == NULL
3058 || *argvars[0].vval.v_string == NUL;
3059 break;
3060 case VAR_PARTIAL:
3061 n = FALSE;
3062 break;
3063 case VAR_NUMBER:
3064 n = argvars[0].vval.v_number == 0;
3065 break;
3066 case VAR_FLOAT:
3067#ifdef FEAT_FLOAT
3068 n = argvars[0].vval.v_float == 0.0;
3069 break;
3070#endif
3071 case VAR_LIST:
3072 n = argvars[0].vval.v_list == NULL
3073 || argvars[0].vval.v_list->lv_first == NULL;
3074 break;
3075 case VAR_DICT:
3076 n = argvars[0].vval.v_dict == NULL
3077 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3078 break;
3079 case VAR_SPECIAL:
3080 n = argvars[0].vval.v_number != VVAL_TRUE;
3081 break;
3082
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003083 case VAR_BLOB:
3084 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003085 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3086 break;
3087
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003088 case VAR_JOB:
3089#ifdef FEAT_JOB_CHANNEL
3090 n = argvars[0].vval.v_job == NULL
3091 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3092 break;
3093#endif
3094 case VAR_CHANNEL:
3095#ifdef FEAT_JOB_CHANNEL
3096 n = argvars[0].vval.v_channel == NULL
3097 || !channel_is_open(argvars[0].vval.v_channel);
3098 break;
3099#endif
3100 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003101 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003102 n = TRUE;
3103 break;
3104 }
3105
3106 rettv->vval.v_number = n;
3107}
3108
3109/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003110 * "environ()" function
3111 */
3112 static void
3113f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3114{
3115#if !defined(AMIGA)
3116 int i = 0;
3117 char_u *entry, *value;
3118# ifdef MSWIN
3119 extern wchar_t **_wenviron;
3120# else
3121 extern char **environ;
3122# endif
3123
3124 if (rettv_dict_alloc(rettv) != OK)
3125 return;
3126
3127# ifdef MSWIN
3128 if (*_wenviron == NULL)
3129 return;
3130# else
3131 if (*environ == NULL)
3132 return;
3133# endif
3134
3135 for (i = 0; ; ++i)
3136 {
3137# ifdef MSWIN
3138 short_u *p;
3139
3140 if ((p = (short_u *)_wenviron[i]) == NULL)
3141 return;
3142 entry = utf16_to_enc(p, NULL);
3143# else
3144 if ((entry = (char_u *)environ[i]) == NULL)
3145 return;
3146 entry = vim_strsave(entry);
3147# endif
3148 if (entry == NULL) // out of memory
3149 return;
3150 if ((value = vim_strchr(entry, '=')) == NULL)
3151 {
3152 vim_free(entry);
3153 continue;
3154 }
3155 *value++ = NUL;
3156 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3157 vim_free(entry);
3158 }
3159#endif
3160}
3161
3162/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003163 * "escape({string}, {chars})" function
3164 */
3165 static void
3166f_escape(typval_T *argvars, typval_T *rettv)
3167{
3168 char_u buf[NUMBUFLEN];
3169
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003170 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3171 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003172 rettv->v_type = VAR_STRING;
3173}
3174
3175/*
3176 * "eval()" function
3177 */
3178 static void
3179f_eval(typval_T *argvars, typval_T *rettv)
3180{
3181 char_u *s, *p;
3182
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003183 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003184 if (s != NULL)
3185 s = skipwhite(s);
3186
3187 p = s;
3188 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3189 {
3190 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003191 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003192 need_clr_eos = FALSE;
3193 rettv->v_type = VAR_NUMBER;
3194 rettv->vval.v_number = 0;
3195 }
3196 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003197 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003198}
3199
3200/*
3201 * "eventhandler()" function
3202 */
3203 static void
3204f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3205{
3206 rettv->vval.v_number = vgetc_busy;
3207}
3208
3209/*
3210 * "executable()" function
3211 */
3212 static void
3213f_executable(typval_T *argvars, typval_T *rettv)
3214{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003215 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003216
3217 /* Check in $PATH and also check directly if there is a directory name. */
3218 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3219 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3220}
3221
3222static garray_T redir_execute_ga;
3223
3224/*
3225 * Append "value[value_len]" to the execute() output.
3226 */
3227 void
3228execute_redir_str(char_u *value, int value_len)
3229{
3230 int len;
3231
3232 if (value_len == -1)
3233 len = (int)STRLEN(value); /* Append the entire string */
3234 else
3235 len = value_len; /* Append only "value_len" characters */
3236 if (ga_grow(&redir_execute_ga, len) == OK)
3237 {
3238 mch_memmove((char *)redir_execute_ga.ga_data
3239 + redir_execute_ga.ga_len, value, len);
3240 redir_execute_ga.ga_len += len;
3241 }
3242}
3243
3244/*
3245 * Get next line from a list.
3246 * Called by do_cmdline() to get the next line.
3247 * Returns allocated string, or NULL for end of function.
3248 */
3249
3250 static char_u *
3251get_list_line(
3252 int c UNUSED,
3253 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003254 int indent UNUSED,
3255 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003256{
3257 listitem_T **p = (listitem_T **)cookie;
3258 listitem_T *item = *p;
3259 char_u buf[NUMBUFLEN];
3260 char_u *s;
3261
3262 if (item == NULL)
3263 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003264 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003265 *p = item->li_next;
3266 return s == NULL ? NULL : vim_strsave(s);
3267}
3268
3269/*
3270 * "execute()" function
3271 */
3272 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003273execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003274{
3275 char_u *cmd = NULL;
3276 list_T *list = NULL;
3277 int save_msg_silent = msg_silent;
3278 int save_emsg_silent = emsg_silent;
3279 int save_emsg_noredir = emsg_noredir;
3280 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003281 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003282 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003283 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003284 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003285
3286 rettv->vval.v_string = NULL;
3287 rettv->v_type = VAR_STRING;
3288
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003289 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003291 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003292 if (list == NULL || list->lv_first == NULL)
3293 /* empty list, no commands, empty output */
3294 return;
3295 ++list->lv_refcount;
3296 }
3297 else
3298 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003299 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003300 if (cmd == NULL)
3301 return;
3302 }
3303
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003304 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003305 {
3306 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003307 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003308
3309 if (s == NULL)
3310 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003311 if (*s == NUL)
3312 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003313 if (STRNCMP(s, "silent", 6) == 0)
3314 ++msg_silent;
3315 if (STRCMP(s, "silent!") == 0)
3316 {
3317 emsg_silent = TRUE;
3318 emsg_noredir = TRUE;
3319 }
3320 }
3321 else
3322 ++msg_silent;
3323
3324 if (redir_execute)
3325 save_ga = redir_execute_ga;
3326 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3327 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003328 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003329 if (!echo_output)
3330 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003331
3332 if (cmd != NULL)
3333 do_cmdline_cmd(cmd);
3334 else
3335 {
3336 listitem_T *item = list->lv_first;
3337
3338 do_cmdline(NULL, get_list_line, (void *)&item,
3339 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3340 --list->lv_refcount;
3341 }
3342
Bram Moolenaard297f352017-01-29 20:31:21 +01003343 /* Need to append a NUL to the result. */
3344 if (ga_grow(&redir_execute_ga, 1) == OK)
3345 {
3346 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3347 rettv->vval.v_string = redir_execute_ga.ga_data;
3348 }
3349 else
3350 {
3351 ga_clear(&redir_execute_ga);
3352 rettv->vval.v_string = NULL;
3353 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003354 msg_silent = save_msg_silent;
3355 emsg_silent = save_emsg_silent;
3356 emsg_noredir = save_emsg_noredir;
3357
3358 redir_execute = save_redir_execute;
3359 if (redir_execute)
3360 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003361 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003362
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003363 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003364 if (echo_output)
3365 // When not working silently: put it in column zero. A following
3366 // "echon" will overwrite the message, unavoidably.
3367 msg_col = 0;
3368 else
3369 // When working silently: Put it back where it was, since nothing
3370 // should have been written.
3371 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003372}
3373
3374/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003375 * "execute()" function
3376 */
3377 static void
3378f_execute(typval_T *argvars, typval_T *rettv)
3379{
3380 execute_common(argvars, rettv, 0);
3381}
3382
3383/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003384 * "exepath()" function
3385 */
3386 static void
3387f_exepath(typval_T *argvars, typval_T *rettv)
3388{
3389 char_u *p = NULL;
3390
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003391 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003392 rettv->v_type = VAR_STRING;
3393 rettv->vval.v_string = p;
3394}
3395
3396/*
3397 * "exists()" function
3398 */
3399 static void
3400f_exists(typval_T *argvars, typval_T *rettv)
3401{
3402 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003403 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003404
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003405 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003406 if (*p == '$') /* environment variable */
3407 {
3408 /* first try "normal" environment variables (fast) */
3409 if (mch_getenv(p + 1) != NULL)
3410 n = TRUE;
3411 else
3412 {
3413 /* try expanding things like $VIM and ${HOME} */
3414 p = expand_env_save(p);
3415 if (p != NULL && *p != '$')
3416 n = TRUE;
3417 vim_free(p);
3418 }
3419 }
3420 else if (*p == '&' || *p == '+') /* option */
3421 {
3422 n = (get_option_tv(&p, NULL, TRUE) == OK);
3423 if (*skipwhite(p) != NUL)
3424 n = FALSE; /* trailing garbage */
3425 }
3426 else if (*p == '*') /* internal or user defined function */
3427 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003428 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003429 }
3430 else if (*p == ':')
3431 {
3432 n = cmd_exists(p + 1);
3433 }
3434 else if (*p == '#')
3435 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003436 if (p[1] == '#')
3437 n = autocmd_supported(p + 2);
3438 else
3439 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003440 }
3441 else /* internal variable */
3442 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003443 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003444 }
3445
3446 rettv->vval.v_number = n;
3447}
3448
3449#ifdef FEAT_FLOAT
3450/*
3451 * "exp()" function
3452 */
3453 static void
3454f_exp(typval_T *argvars, typval_T *rettv)
3455{
3456 float_T f = 0.0;
3457
3458 rettv->v_type = VAR_FLOAT;
3459 if (get_float_arg(argvars, &f) == OK)
3460 rettv->vval.v_float = exp(f);
3461 else
3462 rettv->vval.v_float = 0.0;
3463}
3464#endif
3465
3466/*
3467 * "expand()" function
3468 */
3469 static void
3470f_expand(typval_T *argvars, typval_T *rettv)
3471{
3472 char_u *s;
3473 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003474 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003475 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3476 expand_T xpc;
3477 int error = FALSE;
3478 char_u *result;
3479
3480 rettv->v_type = VAR_STRING;
3481 if (argvars[1].v_type != VAR_UNKNOWN
3482 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003483 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003484 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003485 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003486
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003487 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003488 if (*s == '%' || *s == '#' || *s == '<')
3489 {
3490 ++emsg_off;
3491 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3492 --emsg_off;
3493 if (rettv->v_type == VAR_LIST)
3494 {
3495 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3496 list_append_string(rettv->vval.v_list, result, -1);
3497 else
3498 vim_free(result);
3499 }
3500 else
3501 rettv->vval.v_string = result;
3502 }
3503 else
3504 {
3505 /* When the optional second argument is non-zero, don't remove matches
3506 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3507 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003508 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003509 options |= WILD_KEEP_ALL;
3510 if (!error)
3511 {
3512 ExpandInit(&xpc);
3513 xpc.xp_context = EXPAND_FILES;
3514 if (p_wic)
3515 options += WILD_ICASE;
3516 if (rettv->v_type == VAR_STRING)
3517 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3518 options, WILD_ALL);
3519 else if (rettv_list_alloc(rettv) != FAIL)
3520 {
3521 int i;
3522
3523 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3524 for (i = 0; i < xpc.xp_numfiles; i++)
3525 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3526 ExpandCleanup(&xpc);
3527 }
3528 }
3529 else
3530 rettv->vval.v_string = NULL;
3531 }
3532}
3533
3534/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003535 * "expandcmd()" function
3536 * Expand all the special characters in a command string.
3537 */
3538 static void
3539f_expandcmd(typval_T *argvars, typval_T *rettv)
3540{
3541 exarg_T eap;
3542 char_u *cmdstr;
3543 char *errormsg = NULL;
3544
3545 rettv->v_type = VAR_STRING;
3546 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3547
3548 memset(&eap, 0, sizeof(eap));
3549 eap.cmd = cmdstr;
3550 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003551 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003552 eap.usefilter = FALSE;
3553 eap.nextcmd = NULL;
3554 eap.cmdidx = CMD_USER;
3555
3556 expand_filename(&eap, &cmdstr, &errormsg);
3557 if (errormsg != NULL && *errormsg != NUL)
3558 emsg(errormsg);
3559
3560 rettv->vval.v_string = cmdstr;
3561}
3562
3563/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003564 * "extend(list, list [, idx])" function
3565 * "extend(dict, dict [, action])" function
3566 */
3567 static void
3568f_extend(typval_T *argvars, typval_T *rettv)
3569{
3570 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3571
3572 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3573 {
3574 list_T *l1, *l2;
3575 listitem_T *item;
3576 long before;
3577 int error = FALSE;
3578
3579 l1 = argvars[0].vval.v_list;
3580 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003581 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003582 && l2 != NULL)
3583 {
3584 if (argvars[2].v_type != VAR_UNKNOWN)
3585 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003586 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003587 if (error)
3588 return; /* type error; errmsg already given */
3589
3590 if (before == l1->lv_len)
3591 item = NULL;
3592 else
3593 {
3594 item = list_find(l1, before);
3595 if (item == NULL)
3596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003597 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003598 return;
3599 }
3600 }
3601 }
3602 else
3603 item = NULL;
3604 list_extend(l1, l2, item);
3605
3606 copy_tv(&argvars[0], rettv);
3607 }
3608 }
3609 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3610 {
3611 dict_T *d1, *d2;
3612 char_u *action;
3613 int i;
3614
3615 d1 = argvars[0].vval.v_dict;
3616 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003617 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003618 && d2 != NULL)
3619 {
3620 /* Check the third argument. */
3621 if (argvars[2].v_type != VAR_UNKNOWN)
3622 {
3623 static char *(av[]) = {"keep", "force", "error"};
3624
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003625 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003626 if (action == NULL)
3627 return; /* type error; errmsg already given */
3628 for (i = 0; i < 3; ++i)
3629 if (STRCMP(action, av[i]) == 0)
3630 break;
3631 if (i == 3)
3632 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003633 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003634 return;
3635 }
3636 }
3637 else
3638 action = (char_u *)"force";
3639
3640 dict_extend(d1, d2, action);
3641
3642 copy_tv(&argvars[0], rettv);
3643 }
3644 }
3645 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003646 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003647}
3648
3649/*
3650 * "feedkeys()" function
3651 */
3652 static void
3653f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3654{
3655 int remap = TRUE;
3656 int insert = FALSE;
3657 char_u *keys, *flags;
3658 char_u nbuf[NUMBUFLEN];
3659 int typed = FALSE;
3660 int execute = FALSE;
3661 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003662 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003663 char_u *keys_esc;
3664
3665 /* This is not allowed in the sandbox. If the commands would still be
3666 * executed in the sandbox it would be OK, but it probably happens later,
3667 * when "sandbox" is no longer set. */
3668 if (check_secure())
3669 return;
3670
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003671 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003672
3673 if (argvars[1].v_type != VAR_UNKNOWN)
3674 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003675 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003676 for ( ; *flags != NUL; ++flags)
3677 {
3678 switch (*flags)
3679 {
3680 case 'n': remap = FALSE; break;
3681 case 'm': remap = TRUE; break;
3682 case 't': typed = TRUE; break;
3683 case 'i': insert = TRUE; break;
3684 case 'x': execute = TRUE; break;
3685 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003686 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003687 }
3688 }
3689 }
3690
3691 if (*keys != NUL || execute)
3692 {
3693 /* Need to escape K_SPECIAL and CSI before putting the string in the
3694 * typeahead buffer. */
3695 keys_esc = vim_strsave_escape_csi(keys);
3696 if (keys_esc != NULL)
3697 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003698 if (lowlevel)
3699 {
3700#ifdef USE_INPUT_BUF
3701 add_to_input_buf(keys, (int)STRLEN(keys));
3702#else
3703 emsg(_("E980: lowlevel input not supported"));
3704#endif
3705 }
3706 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003707 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003708 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003709 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003710 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003711#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003712 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003713#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003714 )
3715 typebuf_was_filled = TRUE;
3716 }
3717 vim_free(keys_esc);
3718
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003719 if (execute)
3720 {
3721 int save_msg_scroll = msg_scroll;
3722
3723 /* Avoid a 1 second delay when the keys start Insert mode. */
3724 msg_scroll = FALSE;
3725
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003726 if (!dangerous)
3727 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003728 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003729 if (!dangerous)
3730 --ex_normal_busy;
3731
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003732 msg_scroll |= save_msg_scroll;
3733 }
3734 }
3735 }
3736}
3737
3738/*
3739 * "filereadable()" function
3740 */
3741 static void
3742f_filereadable(typval_T *argvars, typval_T *rettv)
3743{
3744 int fd;
3745 char_u *p;
3746 int n;
3747
3748#ifndef O_NONBLOCK
3749# define O_NONBLOCK 0
3750#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003751 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003752 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3753 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3754 {
3755 n = TRUE;
3756 close(fd);
3757 }
3758 else
3759 n = FALSE;
3760
3761 rettv->vval.v_number = n;
3762}
3763
3764/*
3765 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3766 * rights to write into.
3767 */
3768 static void
3769f_filewritable(typval_T *argvars, typval_T *rettv)
3770{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003771 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003772}
3773
3774 static void
3775findfilendir(
3776 typval_T *argvars UNUSED,
3777 typval_T *rettv,
3778 int find_what UNUSED)
3779{
3780#ifdef FEAT_SEARCHPATH
3781 char_u *fname;
3782 char_u *fresult = NULL;
3783 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3784 char_u *p;
3785 char_u pathbuf[NUMBUFLEN];
3786 int count = 1;
3787 int first = TRUE;
3788 int error = FALSE;
3789#endif
3790
3791 rettv->vval.v_string = NULL;
3792 rettv->v_type = VAR_STRING;
3793
3794#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003795 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003796
3797 if (argvars[1].v_type != VAR_UNKNOWN)
3798 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003799 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003800 if (p == NULL)
3801 error = TRUE;
3802 else
3803 {
3804 if (*p != NUL)
3805 path = p;
3806
3807 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003808 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003809 }
3810 }
3811
3812 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3813 error = TRUE;
3814
3815 if (*fname != NUL && !error)
3816 {
3817 do
3818 {
3819 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3820 vim_free(fresult);
3821 fresult = find_file_in_path_option(first ? fname : NULL,
3822 first ? (int)STRLEN(fname) : 0,
3823 0, first, path,
3824 find_what,
3825 curbuf->b_ffname,
3826 find_what == FINDFILE_DIR
3827 ? (char_u *)"" : curbuf->b_p_sua);
3828 first = FALSE;
3829
3830 if (fresult != NULL && rettv->v_type == VAR_LIST)
3831 list_append_string(rettv->vval.v_list, fresult, -1);
3832
3833 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3834 }
3835
3836 if (rettv->v_type == VAR_STRING)
3837 rettv->vval.v_string = fresult;
3838#endif
3839}
3840
3841/*
3842 * "filter()" function
3843 */
3844 static void
3845f_filter(typval_T *argvars, typval_T *rettv)
3846{
3847 filter_map(argvars, rettv, FALSE);
3848}
3849
3850/*
3851 * "finddir({fname}[, {path}[, {count}]])" function
3852 */
3853 static void
3854f_finddir(typval_T *argvars, typval_T *rettv)
3855{
3856 findfilendir(argvars, rettv, FINDFILE_DIR);
3857}
3858
3859/*
3860 * "findfile({fname}[, {path}[, {count}]])" function
3861 */
3862 static void
3863f_findfile(typval_T *argvars, typval_T *rettv)
3864{
3865 findfilendir(argvars, rettv, FINDFILE_FILE);
3866}
3867
3868#ifdef FEAT_FLOAT
3869/*
3870 * "float2nr({float})" function
3871 */
3872 static void
3873f_float2nr(typval_T *argvars, typval_T *rettv)
3874{
3875 float_T f = 0.0;
3876
3877 if (get_float_arg(argvars, &f) == OK)
3878 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003879 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003880 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003881 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003882 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003883 else
3884 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003885 }
3886}
3887
3888/*
3889 * "floor({float})" function
3890 */
3891 static void
3892f_floor(typval_T *argvars, typval_T *rettv)
3893{
3894 float_T f = 0.0;
3895
3896 rettv->v_type = VAR_FLOAT;
3897 if (get_float_arg(argvars, &f) == OK)
3898 rettv->vval.v_float = floor(f);
3899 else
3900 rettv->vval.v_float = 0.0;
3901}
3902
3903/*
3904 * "fmod()" function
3905 */
3906 static void
3907f_fmod(typval_T *argvars, typval_T *rettv)
3908{
3909 float_T fx = 0.0, fy = 0.0;
3910
3911 rettv->v_type = VAR_FLOAT;
3912 if (get_float_arg(argvars, &fx) == OK
3913 && get_float_arg(&argvars[1], &fy) == OK)
3914 rettv->vval.v_float = fmod(fx, fy);
3915 else
3916 rettv->vval.v_float = 0.0;
3917}
3918#endif
3919
3920/*
3921 * "fnameescape({string})" function
3922 */
3923 static void
3924f_fnameescape(typval_T *argvars, typval_T *rettv)
3925{
3926 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003927 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003928 rettv->v_type = VAR_STRING;
3929}
3930
3931/*
3932 * "fnamemodify({fname}, {mods})" function
3933 */
3934 static void
3935f_fnamemodify(typval_T *argvars, typval_T *rettv)
3936{
3937 char_u *fname;
3938 char_u *mods;
3939 int usedlen = 0;
3940 int len;
3941 char_u *fbuf = NULL;
3942 char_u buf[NUMBUFLEN];
3943
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003944 fname = tv_get_string_chk(&argvars[0]);
3945 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003946 if (fname == NULL || mods == NULL)
3947 fname = NULL;
3948 else
3949 {
3950 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003951 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003952 }
3953
3954 rettv->v_type = VAR_STRING;
3955 if (fname == NULL)
3956 rettv->vval.v_string = NULL;
3957 else
3958 rettv->vval.v_string = vim_strnsave(fname, len);
3959 vim_free(fbuf);
3960}
3961
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003962/*
3963 * "foldclosed()" function
3964 */
3965 static void
3966foldclosed_both(
3967 typval_T *argvars UNUSED,
3968 typval_T *rettv,
3969 int end UNUSED)
3970{
3971#ifdef FEAT_FOLDING
3972 linenr_T lnum;
3973 linenr_T first, last;
3974
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003975 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003976 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3977 {
3978 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3979 {
3980 if (end)
3981 rettv->vval.v_number = (varnumber_T)last;
3982 else
3983 rettv->vval.v_number = (varnumber_T)first;
3984 return;
3985 }
3986 }
3987#endif
3988 rettv->vval.v_number = -1;
3989}
3990
3991/*
3992 * "foldclosed()" function
3993 */
3994 static void
3995f_foldclosed(typval_T *argvars, typval_T *rettv)
3996{
3997 foldclosed_both(argvars, rettv, FALSE);
3998}
3999
4000/*
4001 * "foldclosedend()" function
4002 */
4003 static void
4004f_foldclosedend(typval_T *argvars, typval_T *rettv)
4005{
4006 foldclosed_both(argvars, rettv, TRUE);
4007}
4008
4009/*
4010 * "foldlevel()" function
4011 */
4012 static void
4013f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4014{
4015#ifdef FEAT_FOLDING
4016 linenr_T lnum;
4017
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004018 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004019 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4020 rettv->vval.v_number = foldLevel(lnum);
4021#endif
4022}
4023
4024/*
4025 * "foldtext()" function
4026 */
4027 static void
4028f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4029{
4030#ifdef FEAT_FOLDING
4031 linenr_T foldstart;
4032 linenr_T foldend;
4033 char_u *dashes;
4034 linenr_T lnum;
4035 char_u *s;
4036 char_u *r;
4037 int len;
4038 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004039 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004040#endif
4041
4042 rettv->v_type = VAR_STRING;
4043 rettv->vval.v_string = NULL;
4044#ifdef FEAT_FOLDING
4045 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4046 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4047 dashes = get_vim_var_str(VV_FOLDDASHES);
4048 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4049 && dashes != NULL)
4050 {
4051 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004052 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004053 if (!linewhite(lnum))
4054 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004055
4056 /* Find interesting text in this line. */
4057 s = skipwhite(ml_get(lnum));
4058 /* skip C comment-start */
4059 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4060 {
4061 s = skipwhite(s + 2);
4062 if (*skipwhite(s) == NUL
4063 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4064 {
4065 s = skipwhite(ml_get(lnum + 1));
4066 if (*s == '*')
4067 s = skipwhite(s + 1);
4068 }
4069 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004070 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004071 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004072 r = alloc(STRLEN(txt)
4073 + STRLEN(dashes) // for %s
4074 + 20 // for %3ld
4075 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004076 if (r != NULL)
4077 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004078 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004079 len = (int)STRLEN(r);
4080 STRCAT(r, s);
4081 /* remove 'foldmarker' and 'commentstring' */
4082 foldtext_cleanup(r + len);
4083 rettv->vval.v_string = r;
4084 }
4085 }
4086#endif
4087}
4088
4089/*
4090 * "foldtextresult(lnum)" function
4091 */
4092 static void
4093f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4094{
4095#ifdef FEAT_FOLDING
4096 linenr_T lnum;
4097 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004098 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004099 foldinfo_T foldinfo;
4100 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004101 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004102#endif
4103
4104 rettv->v_type = VAR_STRING;
4105 rettv->vval.v_string = NULL;
4106#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004107 if (entered)
4108 return; /* reject recursive use */
4109 entered = TRUE;
4110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004111 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004112 /* treat illegal types and illegal string values for {lnum} the same */
4113 if (lnum < 0)
4114 lnum = 0;
4115 fold_count = foldedCount(curwin, lnum, &foldinfo);
4116 if (fold_count > 0)
4117 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004118 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4119 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004120 if (text == buf)
4121 text = vim_strsave(text);
4122 rettv->vval.v_string = text;
4123 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004124
4125 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004126#endif
4127}
4128
4129/*
4130 * "foreground()" function
4131 */
4132 static void
4133f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4134{
4135#ifdef FEAT_GUI
4136 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004137 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004138 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004139 return;
4140 }
4141#endif
4142#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004143 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004144#endif
4145}
4146
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004147 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004148common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004149{
4150 char_u *s;
4151 char_u *name;
4152 int use_string = FALSE;
4153 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004154 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004155
4156 if (argvars[0].v_type == VAR_FUNC)
4157 {
4158 /* function(MyFunc, [arg], dict) */
4159 s = argvars[0].vval.v_string;
4160 }
4161 else if (argvars[0].v_type == VAR_PARTIAL
4162 && argvars[0].vval.v_partial != NULL)
4163 {
4164 /* function(dict.MyFunc, [arg]) */
4165 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004166 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004167 }
4168 else
4169 {
4170 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004171 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004172 use_string = TRUE;
4173 }
4174
Bram Moolenaar843b8842016-08-21 14:36:15 +02004175 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004176 {
4177 name = s;
4178 trans_name = trans_function_name(&name, FALSE,
4179 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4180 if (*name != NUL)
4181 s = NULL;
4182 }
4183
Bram Moolenaar843b8842016-08-21 14:36:15 +02004184 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4185 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004186 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004187 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004188 else if (trans_name != NULL && (is_funcref
4189 ? find_func(trans_name) == NULL
4190 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004191 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004192 else
4193 {
4194 int dict_idx = 0;
4195 int arg_idx = 0;
4196 list_T *list = NULL;
4197
4198 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4199 {
4200 char sid_buf[25];
4201 int off = *s == 's' ? 2 : 5;
4202
4203 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4204 * also be called from another script. Using trans_function_name()
4205 * would also work, but some plugins depend on the name being
4206 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004207 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004208 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004209 if (name != NULL)
4210 {
4211 STRCPY(name, sid_buf);
4212 STRCAT(name, s + off);
4213 }
4214 }
4215 else
4216 name = vim_strsave(s);
4217
4218 if (argvars[1].v_type != VAR_UNKNOWN)
4219 {
4220 if (argvars[2].v_type != VAR_UNKNOWN)
4221 {
4222 /* function(name, [args], dict) */
4223 arg_idx = 1;
4224 dict_idx = 2;
4225 }
4226 else if (argvars[1].v_type == VAR_DICT)
4227 /* function(name, dict) */
4228 dict_idx = 1;
4229 else
4230 /* function(name, [args]) */
4231 arg_idx = 1;
4232 if (dict_idx > 0)
4233 {
4234 if (argvars[dict_idx].v_type != VAR_DICT)
4235 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004236 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004237 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004238 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004239 }
4240 if (argvars[dict_idx].vval.v_dict == NULL)
4241 dict_idx = 0;
4242 }
4243 if (arg_idx > 0)
4244 {
4245 if (argvars[arg_idx].v_type != VAR_LIST)
4246 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004247 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004248 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004249 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004250 }
4251 list = argvars[arg_idx].vval.v_list;
4252 if (list == NULL || list->lv_len == 0)
4253 arg_idx = 0;
4254 }
4255 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004256 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004257 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004258 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004259
4260 /* result is a VAR_PARTIAL */
4261 if (pt == NULL)
4262 vim_free(name);
4263 else
4264 {
4265 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4266 {
4267 listitem_T *li;
4268 int i = 0;
4269 int arg_len = 0;
4270 int lv_len = 0;
4271
4272 if (arg_pt != NULL)
4273 arg_len = arg_pt->pt_argc;
4274 if (list != NULL)
4275 lv_len = list->lv_len;
4276 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004277 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004278 if (pt->pt_argv == NULL)
4279 {
4280 vim_free(pt);
4281 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004282 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004283 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004284 for (i = 0; i < arg_len; i++)
4285 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4286 if (lv_len > 0)
4287 for (li = list->lv_first; li != NULL;
4288 li = li->li_next)
4289 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004290 }
4291
4292 /* For "function(dict.func, [], dict)" and "func" is a partial
4293 * use "dict". That is backwards compatible. */
4294 if (dict_idx > 0)
4295 {
4296 /* The dict is bound explicitly, pt_auto is FALSE. */
4297 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4298 ++pt->pt_dict->dv_refcount;
4299 }
4300 else if (arg_pt != NULL)
4301 {
4302 /* If the dict was bound automatically the result is also
4303 * bound automatically. */
4304 pt->pt_dict = arg_pt->pt_dict;
4305 pt->pt_auto = arg_pt->pt_auto;
4306 if (pt->pt_dict != NULL)
4307 ++pt->pt_dict->dv_refcount;
4308 }
4309
4310 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004311 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4312 {
4313 pt->pt_func = arg_pt->pt_func;
4314 func_ptr_ref(pt->pt_func);
4315 vim_free(name);
4316 }
4317 else if (is_funcref)
4318 {
4319 pt->pt_func = find_func(trans_name);
4320 func_ptr_ref(pt->pt_func);
4321 vim_free(name);
4322 }
4323 else
4324 {
4325 pt->pt_name = name;
4326 func_ref(name);
4327 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004328 }
4329 rettv->v_type = VAR_PARTIAL;
4330 rettv->vval.v_partial = pt;
4331 }
4332 else
4333 {
4334 /* result is a VAR_FUNC */
4335 rettv->v_type = VAR_FUNC;
4336 rettv->vval.v_string = name;
4337 func_ref(name);
4338 }
4339 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004340theend:
4341 vim_free(trans_name);
4342}
4343
4344/*
4345 * "funcref()" function
4346 */
4347 static void
4348f_funcref(typval_T *argvars, typval_T *rettv)
4349{
4350 common_function(argvars, rettv, TRUE);
4351}
4352
4353/*
4354 * "function()" function
4355 */
4356 static void
4357f_function(typval_T *argvars, typval_T *rettv)
4358{
4359 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004360}
4361
4362/*
4363 * "garbagecollect()" function
4364 */
4365 static void
4366f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4367{
4368 /* This is postponed until we are back at the toplevel, because we may be
4369 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4370 want_garbage_collect = TRUE;
4371
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004372 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004373 garbage_collect_at_exit = TRUE;
4374}
4375
4376/*
4377 * "get()" function
4378 */
4379 static void
4380f_get(typval_T *argvars, typval_T *rettv)
4381{
4382 listitem_T *li;
4383 list_T *l;
4384 dictitem_T *di;
4385 dict_T *d;
4386 typval_T *tv = NULL;
4387
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004388 if (argvars[0].v_type == VAR_BLOB)
4389 {
4390 int error = FALSE;
4391 int idx = tv_get_number_chk(&argvars[1], &error);
4392
4393 if (!error)
4394 {
4395 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004396 if (idx < 0)
4397 idx = blob_len(argvars[0].vval.v_blob) + idx;
4398 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4399 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004400 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004401 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004402 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004403 tv = rettv;
4404 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004405 }
4406 }
4407 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004408 {
4409 if ((l = argvars[0].vval.v_list) != NULL)
4410 {
4411 int error = FALSE;
4412
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004413 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004414 if (!error && li != NULL)
4415 tv = &li->li_tv;
4416 }
4417 }
4418 else if (argvars[0].v_type == VAR_DICT)
4419 {
4420 if ((d = argvars[0].vval.v_dict) != NULL)
4421 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004422 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004423 if (di != NULL)
4424 tv = &di->di_tv;
4425 }
4426 }
4427 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4428 {
4429 partial_T *pt;
4430 partial_T fref_pt;
4431
4432 if (argvars[0].v_type == VAR_PARTIAL)
4433 pt = argvars[0].vval.v_partial;
4434 else
4435 {
4436 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4437 fref_pt.pt_name = argvars[0].vval.v_string;
4438 pt = &fref_pt;
4439 }
4440
4441 if (pt != NULL)
4442 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004443 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004444 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004445
4446 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4447 {
4448 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004449 n = partial_name(pt);
4450 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004451 rettv->vval.v_string = NULL;
4452 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004453 {
4454 rettv->vval.v_string = vim_strsave(n);
4455 if (rettv->v_type == VAR_FUNC)
4456 func_ref(rettv->vval.v_string);
4457 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004458 }
4459 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004460 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004461 else if (STRCMP(what, "args") == 0)
4462 {
4463 rettv->v_type = VAR_LIST;
4464 if (rettv_list_alloc(rettv) == OK)
4465 {
4466 int i;
4467
4468 for (i = 0; i < pt->pt_argc; ++i)
4469 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4470 }
4471 }
4472 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004473 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004474 return;
4475 }
4476 }
4477 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004478 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004479
4480 if (tv == NULL)
4481 {
4482 if (argvars[2].v_type != VAR_UNKNOWN)
4483 copy_tv(&argvars[2], rettv);
4484 }
4485 else
4486 copy_tv(tv, rettv);
4487}
4488
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004489/*
4490 * Returns buffer options, variables and other attributes in a dictionary.
4491 */
4492 static dict_T *
4493get_buffer_info(buf_T *buf)
4494{
4495 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004496 tabpage_T *tp;
4497 win_T *wp;
4498 list_T *windows;
4499
4500 dict = dict_alloc();
4501 if (dict == NULL)
4502 return NULL;
4503
Bram Moolenaare0be1672018-07-08 16:50:37 +02004504 dict_add_number(dict, "bufnr", buf->b_fnum);
4505 dict_add_string(dict, "name", buf->b_ffname);
4506 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4507 : buflist_findlnum(buf));
4508 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4509 dict_add_number(dict, "listed", buf->b_p_bl);
4510 dict_add_number(dict, "changed", bufIsChanged(buf));
4511 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4512 dict_add_number(dict, "hidden",
4513 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004514
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004515 // Get a reference to buffer variables
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004516 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004517
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004518 // List of windows displaying this buffer
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004519 windows = list_alloc();
4520 if (windows != NULL)
4521 {
4522 FOR_ALL_TAB_WINDOWS(tp, wp)
4523 if (wp->w_buffer == buf)
4524 list_append_number(windows, (varnumber_T)wp->w_id);
4525 dict_add_list(dict, "windows", windows);
4526 }
4527
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004528#ifdef FEAT_TEXT_PROP
4529 // List of popup windows displaying this buffer
4530 windows = list_alloc();
4531 if (windows != NULL)
4532 {
4533 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4534 if (wp->w_buffer == buf)
4535 list_append_number(windows, (varnumber_T)wp->w_id);
4536 FOR_ALL_TABPAGES(tp)
4537 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
4538 if (wp->w_buffer == buf)
4539 list_append_number(windows, (varnumber_T)wp->w_id);
4540
4541 dict_add_list(dict, "popups", windows);
4542 }
4543#endif
4544
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004545#ifdef FEAT_SIGNS
4546 if (buf->b_signlist != NULL)
4547 {
4548 /* List of signs placed in this buffer */
4549 list_T *signs = list_alloc();
4550 if (signs != NULL)
4551 {
4552 get_buffer_signs(buf, signs);
4553 dict_add_list(dict, "signs", signs);
4554 }
4555 }
4556#endif
4557
4558 return dict;
4559}
4560
4561/*
4562 * "getbufinfo()" function
4563 */
4564 static void
4565f_getbufinfo(typval_T *argvars, typval_T *rettv)
4566{
4567 buf_T *buf = NULL;
4568 buf_T *argbuf = NULL;
4569 dict_T *d;
4570 int filtered = FALSE;
4571 int sel_buflisted = FALSE;
4572 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004573 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004574
4575 if (rettv_list_alloc(rettv) != OK)
4576 return;
4577
4578 /* List of all the buffers or selected buffers */
4579 if (argvars[0].v_type == VAR_DICT)
4580 {
4581 dict_T *sel_d = argvars[0].vval.v_dict;
4582
4583 if (sel_d != NULL)
4584 {
4585 dictitem_T *di;
4586
4587 filtered = TRUE;
4588
4589 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004590 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004591 sel_buflisted = TRUE;
4592
4593 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004594 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004595 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004596
4597 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004598 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004599 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004600 }
4601 }
4602 else if (argvars[0].v_type != VAR_UNKNOWN)
4603 {
4604 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004605 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004606 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004607 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004608 --emsg_off;
4609 if (argbuf == NULL)
4610 return;
4611 }
4612
4613 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004614 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004615 {
4616 if (argbuf != NULL && argbuf != buf)
4617 continue;
4618 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004619 || (sel_buflisted && !buf->b_p_bl)
4620 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004621 continue;
4622
4623 d = get_buffer_info(buf);
4624 if (d != NULL)
4625 list_append_dict(rettv->vval.v_list, d);
4626 if (argbuf != NULL)
4627 return;
4628 }
4629}
4630
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004631/*
4632 * Get line or list of lines from buffer "buf" into "rettv".
4633 * Return a range (from start to end) of lines in rettv from the specified
4634 * buffer.
4635 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4636 */
4637 static void
4638get_buffer_lines(
4639 buf_T *buf,
4640 linenr_T start,
4641 linenr_T end,
4642 int retlist,
4643 typval_T *rettv)
4644{
4645 char_u *p;
4646
4647 rettv->v_type = VAR_STRING;
4648 rettv->vval.v_string = NULL;
4649 if (retlist && rettv_list_alloc(rettv) == FAIL)
4650 return;
4651
4652 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4653 return;
4654
4655 if (!retlist)
4656 {
4657 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4658 p = ml_get_buf(buf, start, FALSE);
4659 else
4660 p = (char_u *)"";
4661 rettv->vval.v_string = vim_strsave(p);
4662 }
4663 else
4664 {
4665 if (end < start)
4666 return;
4667
4668 if (start < 1)
4669 start = 1;
4670 if (end > buf->b_ml.ml_line_count)
4671 end = buf->b_ml.ml_line_count;
4672 while (start <= end)
4673 if (list_append_string(rettv->vval.v_list,
4674 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4675 break;
4676 }
4677}
4678
4679/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004680 * "getbufline()" function
4681 */
4682 static void
4683f_getbufline(typval_T *argvars, typval_T *rettv)
4684{
4685 linenr_T lnum;
4686 linenr_T end;
4687 buf_T *buf;
4688
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004689 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004690 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004691 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004692 --emsg_off;
4693
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004694 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004695 if (argvars[2].v_type == VAR_UNKNOWN)
4696 end = lnum;
4697 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004698 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004699
4700 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4701}
4702
4703/*
4704 * "getbufvar()" function
4705 */
4706 static void
4707f_getbufvar(typval_T *argvars, typval_T *rettv)
4708{
4709 buf_T *buf;
4710 buf_T *save_curbuf;
4711 char_u *varname;
4712 dictitem_T *v;
4713 int done = FALSE;
4714
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004715 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4716 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004717 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004718 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004719
4720 rettv->v_type = VAR_STRING;
4721 rettv->vval.v_string = NULL;
4722
4723 if (buf != NULL && varname != NULL)
4724 {
4725 /* set curbuf to be our buf, temporarily */
4726 save_curbuf = curbuf;
4727 curbuf = buf;
4728
Bram Moolenaar30567352016-08-27 21:25:44 +02004729 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004730 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004731 if (varname[1] == NUL)
4732 {
4733 /* get all buffer-local options in a dict */
4734 dict_T *opts = get_winbuf_options(TRUE);
4735
4736 if (opts != NULL)
4737 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004738 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004739 done = TRUE;
4740 }
4741 }
4742 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4743 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004744 done = TRUE;
4745 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004746 else
4747 {
4748 /* Look up the variable. */
4749 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4750 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4751 'b', varname, FALSE);
4752 if (v != NULL)
4753 {
4754 copy_tv(&v->di_tv, rettv);
4755 done = TRUE;
4756 }
4757 }
4758
4759 /* restore previous notion of curbuf */
4760 curbuf = save_curbuf;
4761 }
4762
4763 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4764 /* use the default value */
4765 copy_tv(&argvars[2], rettv);
4766
4767 --emsg_off;
4768}
4769
4770/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004771 * "getchangelist()" function
4772 */
4773 static void
4774f_getchangelist(typval_T *argvars, typval_T *rettv)
4775{
4776#ifdef FEAT_JUMPLIST
4777 buf_T *buf;
4778 int i;
4779 list_T *l;
4780 dict_T *d;
4781#endif
4782
4783 if (rettv_list_alloc(rettv) != OK)
4784 return;
4785
4786#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004787 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004788 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004789 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004790 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004791 if (buf == NULL)
4792 return;
4793
4794 l = list_alloc();
4795 if (l == NULL)
4796 return;
4797
4798 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4799 return;
4800 /*
4801 * The current window change list index tracks only the position in the
4802 * current buffer change list. For other buffers, use the change list
4803 * length as the current index.
4804 */
4805 list_append_number(rettv->vval.v_list,
4806 (varnumber_T)((buf == curwin->w_buffer)
4807 ? curwin->w_changelistidx : buf->b_changelistlen));
4808
4809 for (i = 0; i < buf->b_changelistlen; ++i)
4810 {
4811 if (buf->b_changelist[i].lnum == 0)
4812 continue;
4813 if ((d = dict_alloc()) == NULL)
4814 return;
4815 if (list_append_dict(l, d) == FAIL)
4816 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004817 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4818 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004819 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004820 }
4821#endif
4822}
4823/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004824 * "getchar()" function
4825 */
4826 static void
4827f_getchar(typval_T *argvars, typval_T *rettv)
4828{
4829 varnumber_T n;
4830 int error = FALSE;
4831
Bram Moolenaar84d93902018-09-11 20:10:20 +02004832#ifdef MESSAGE_QUEUE
4833 // vpeekc() used to check for messages, but that caused problems, invoking
4834 // a callback where it was not expected. Some plugins use getchar(1) in a
4835 // loop to await a message, therefore make sure we check for messages here.
4836 parse_queued_messages();
4837#endif
4838
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004839 /* Position the cursor. Needed after a message that ends in a space. */
4840 windgoto(msg_row, msg_col);
4841
4842 ++no_mapping;
4843 ++allow_keys;
4844 for (;;)
4845 {
4846 if (argvars[0].v_type == VAR_UNKNOWN)
4847 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004848 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004849 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004850 /* getchar(1): only check if char avail */
4851 n = vpeekc_any();
4852 else if (error || vpeekc_any() == NUL)
4853 /* illegal argument or getchar(0) and no char avail: return zero */
4854 n = 0;
4855 else
4856 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004857 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004858
4859 if (n == K_IGNORE)
4860 continue;
4861 break;
4862 }
4863 --no_mapping;
4864 --allow_keys;
4865
4866 set_vim_var_nr(VV_MOUSE_WIN, 0);
4867 set_vim_var_nr(VV_MOUSE_WINID, 0);
4868 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4869 set_vim_var_nr(VV_MOUSE_COL, 0);
4870
4871 rettv->vval.v_number = n;
4872 if (IS_SPECIAL(n) || mod_mask != 0)
4873 {
4874 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4875 int i = 0;
4876
4877 /* Turn a special key into three bytes, plus modifier. */
4878 if (mod_mask != 0)
4879 {
4880 temp[i++] = K_SPECIAL;
4881 temp[i++] = KS_MODIFIER;
4882 temp[i++] = mod_mask;
4883 }
4884 if (IS_SPECIAL(n))
4885 {
4886 temp[i++] = K_SPECIAL;
4887 temp[i++] = K_SECOND(n);
4888 temp[i++] = K_THIRD(n);
4889 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004890 else if (has_mbyte)
4891 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004892 else
4893 temp[i++] = n;
4894 temp[i++] = NUL;
4895 rettv->v_type = VAR_STRING;
4896 rettv->vval.v_string = vim_strsave(temp);
4897
4898#ifdef FEAT_MOUSE
4899 if (is_mouse_key(n))
4900 {
4901 int row = mouse_row;
4902 int col = mouse_col;
4903 win_T *win;
4904 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004905 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004906 int winnr = 1;
4907
4908 if (row >= 0 && col >= 0)
4909 {
4910 /* Find the window at the mouse coordinates and compute the
4911 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004912 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004913 if (win == NULL)
4914 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004915 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004916# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004917 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004918 winnr = 0;
4919 else
4920# endif
4921 for (wp = firstwin; wp != win && wp != NULL;
4922 wp = wp->w_next)
4923 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004924 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4925 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4926 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4927 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4928 }
4929 }
4930#endif
4931 }
4932}
4933
4934/*
4935 * "getcharmod()" function
4936 */
4937 static void
4938f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4939{
4940 rettv->vval.v_number = mod_mask;
4941}
4942
4943/*
4944 * "getcharsearch()" function
4945 */
4946 static void
4947f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4948{
4949 if (rettv_dict_alloc(rettv) != FAIL)
4950 {
4951 dict_T *dict = rettv->vval.v_dict;
4952
Bram Moolenaare0be1672018-07-08 16:50:37 +02004953 dict_add_string(dict, "char", last_csearch());
4954 dict_add_number(dict, "forward", last_csearch_forward());
4955 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004956 }
4957}
4958
4959/*
4960 * "getcmdline()" function
4961 */
4962 static void
4963f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4964{
4965 rettv->v_type = VAR_STRING;
4966 rettv->vval.v_string = get_cmdline_str();
4967}
4968
4969/*
4970 * "getcmdpos()" function
4971 */
4972 static void
4973f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4974{
4975 rettv->vval.v_number = get_cmdline_pos() + 1;
4976}
4977
4978/*
4979 * "getcmdtype()" function
4980 */
4981 static void
4982f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4983{
4984 rettv->v_type = VAR_STRING;
4985 rettv->vval.v_string = alloc(2);
4986 if (rettv->vval.v_string != NULL)
4987 {
4988 rettv->vval.v_string[0] = get_cmdline_type();
4989 rettv->vval.v_string[1] = NUL;
4990 }
4991}
4992
4993/*
4994 * "getcmdwintype()" function
4995 */
4996 static void
4997f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4998{
4999 rettv->v_type = VAR_STRING;
5000 rettv->vval.v_string = NULL;
5001#ifdef FEAT_CMDWIN
5002 rettv->vval.v_string = alloc(2);
5003 if (rettv->vval.v_string != NULL)
5004 {
5005 rettv->vval.v_string[0] = cmdwin_type;
5006 rettv->vval.v_string[1] = NUL;
5007 }
5008#endif
5009}
5010
5011#if defined(FEAT_CMDL_COMPL)
5012/*
5013 * "getcompletion()" function
5014 */
5015 static void
5016f_getcompletion(typval_T *argvars, typval_T *rettv)
5017{
5018 char_u *pat;
5019 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005020 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005021 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5022 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005023
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005024 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005025 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005026
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005027 if (p_wic)
5028 options |= WILD_ICASE;
5029
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005030 /* For filtered results, 'wildignore' is used */
5031 if (!filtered)
5032 options |= WILD_KEEP_ALL;
5033
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005034 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005035 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005036 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005037 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005038 if (xpc.xp_context == EXPAND_NOTHING)
5039 {
5040 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005041 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005042 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005043 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005044 return;
5045 }
5046
5047# if defined(FEAT_MENU)
5048 if (xpc.xp_context == EXPAND_MENUS)
5049 {
5050 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5051 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5052 }
5053# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005054#ifdef FEAT_CSCOPE
5055 if (xpc.xp_context == EXPAND_CSCOPE)
5056 {
5057 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5058 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5059 }
5060#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005061#ifdef FEAT_SIGNS
5062 if (xpc.xp_context == EXPAND_SIGN)
5063 {
5064 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5065 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5066 }
5067#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005068
5069 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5070 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5071 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005072 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005073
5074 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5075
5076 for (i = 0; i < xpc.xp_numfiles; i++)
5077 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5078 }
5079 vim_free(pat);
5080 ExpandCleanup(&xpc);
5081}
5082#endif
5083
5084/*
5085 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005086 *
5087 * Return the current working directory of a window in a tab page.
5088 * First optional argument 'winnr' is the window number or -1 and the second
5089 * optional argument 'tabnr' is the tab page number.
5090 *
5091 * If no arguments are supplied, then return the directory of the current
5092 * window.
5093 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5094 * the specified window.
5095 * If 'winnr' is 0 then return the directory of the current window.
5096 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5097 * directory of the specified tab page. Otherwise return the directory of the
5098 * specified window in the specified tab page.
5099 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005100 */
5101 static void
5102f_getcwd(typval_T *argvars, typval_T *rettv)
5103{
5104 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005105 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005106 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005107 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005108
5109 rettv->v_type = VAR_STRING;
5110 rettv->vval.v_string = NULL;
5111
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005112 if (argvars[0].v_type == VAR_NUMBER
5113 && argvars[0].vval.v_number == -1
5114 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005115 global = TRUE;
5116 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005117 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005118
5119 if (wp != NULL && wp->w_localdir != NULL)
5120 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005121 else if (tp != NULL && tp->tp_localdir != NULL)
5122 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5123 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005124 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005125 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005126 rettv->vval.v_string = vim_strsave(globaldir);
5127 else
5128 {
5129 cwd = alloc(MAXPATHL);
5130 if (cwd != NULL)
5131 {
5132 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5133 rettv->vval.v_string = vim_strsave(cwd);
5134 vim_free(cwd);
5135 }
5136 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005137 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005138#ifdef BACKSLASH_IN_FILENAME
5139 if (rettv->vval.v_string != NULL)
5140 slash_adjust(rettv->vval.v_string);
5141#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005142}
5143
5144/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005145 * "getenv()" function
5146 */
5147 static void
5148f_getenv(typval_T *argvars, typval_T *rettv)
5149{
5150 int mustfree = FALSE;
5151 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5152
5153 if (p == NULL)
5154 {
5155 rettv->v_type = VAR_SPECIAL;
5156 rettv->vval.v_number = VVAL_NULL;
5157 return;
5158 }
5159 if (!mustfree)
5160 p = vim_strsave(p);
5161 rettv->vval.v_string = p;
5162 rettv->v_type = VAR_STRING;
5163}
5164
5165/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005166 * "getfontname()" function
5167 */
5168 static void
5169f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5170{
5171 rettv->v_type = VAR_STRING;
5172 rettv->vval.v_string = NULL;
5173#ifdef FEAT_GUI
5174 if (gui.in_use)
5175 {
5176 GuiFont font;
5177 char_u *name = NULL;
5178
5179 if (argvars[0].v_type == VAR_UNKNOWN)
5180 {
5181 /* Get the "Normal" font. Either the name saved by
5182 * hl_set_font_name() or from the font ID. */
5183 font = gui.norm_font;
5184 name = hl_get_font_name();
5185 }
5186 else
5187 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005188 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005189 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5190 return;
5191 font = gui_mch_get_font(name, FALSE);
5192 if (font == NOFONT)
5193 return; /* Invalid font name, return empty string. */
5194 }
5195 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5196 if (argvars[0].v_type != VAR_UNKNOWN)
5197 gui_mch_free_font(font);
5198 }
5199#endif
5200}
5201
5202/*
5203 * "getfperm({fname})" function
5204 */
5205 static void
5206f_getfperm(typval_T *argvars, typval_T *rettv)
5207{
5208 char_u *fname;
5209 stat_T st;
5210 char_u *perm = NULL;
5211 char_u flags[] = "rwx";
5212 int i;
5213
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005214 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005215
5216 rettv->v_type = VAR_STRING;
5217 if (mch_stat((char *)fname, &st) >= 0)
5218 {
5219 perm = vim_strsave((char_u *)"---------");
5220 if (perm != NULL)
5221 {
5222 for (i = 0; i < 9; i++)
5223 {
5224 if (st.st_mode & (1 << (8 - i)))
5225 perm[i] = flags[i % 3];
5226 }
5227 }
5228 }
5229 rettv->vval.v_string = perm;
5230}
5231
5232/*
5233 * "getfsize({fname})" function
5234 */
5235 static void
5236f_getfsize(typval_T *argvars, typval_T *rettv)
5237{
5238 char_u *fname;
5239 stat_T st;
5240
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005241 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005242
5243 rettv->v_type = VAR_NUMBER;
5244
5245 if (mch_stat((char *)fname, &st) >= 0)
5246 {
5247 if (mch_isdir(fname))
5248 rettv->vval.v_number = 0;
5249 else
5250 {
5251 rettv->vval.v_number = (varnumber_T)st.st_size;
5252
5253 /* non-perfect check for overflow */
5254 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5255 rettv->vval.v_number = -2;
5256 }
5257 }
5258 else
5259 rettv->vval.v_number = -1;
5260}
5261
5262/*
5263 * "getftime({fname})" function
5264 */
5265 static void
5266f_getftime(typval_T *argvars, typval_T *rettv)
5267{
5268 char_u *fname;
5269 stat_T st;
5270
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005271 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005272
5273 if (mch_stat((char *)fname, &st) >= 0)
5274 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5275 else
5276 rettv->vval.v_number = -1;
5277}
5278
5279/*
5280 * "getftype({fname})" function
5281 */
5282 static void
5283f_getftype(typval_T *argvars, typval_T *rettv)
5284{
5285 char_u *fname;
5286 stat_T st;
5287 char_u *type = NULL;
5288 char *t;
5289
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005290 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005291
5292 rettv->v_type = VAR_STRING;
5293 if (mch_lstat((char *)fname, &st) >= 0)
5294 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005295 if (S_ISREG(st.st_mode))
5296 t = "file";
5297 else if (S_ISDIR(st.st_mode))
5298 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005299 else if (S_ISLNK(st.st_mode))
5300 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005301 else if (S_ISBLK(st.st_mode))
5302 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005303 else if (S_ISCHR(st.st_mode))
5304 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005305 else if (S_ISFIFO(st.st_mode))
5306 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005307 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005308 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005309 else
5310 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005311 type = vim_strsave((char_u *)t);
5312 }
5313 rettv->vval.v_string = type;
5314}
5315
5316/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005317 * "getjumplist()" function
5318 */
5319 static void
5320f_getjumplist(typval_T *argvars, typval_T *rettv)
5321{
5322#ifdef FEAT_JUMPLIST
5323 win_T *wp;
5324 int i;
5325 list_T *l;
5326 dict_T *d;
5327#endif
5328
5329 if (rettv_list_alloc(rettv) != OK)
5330 return;
5331
5332#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005333 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005334 if (wp == NULL)
5335 return;
5336
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005337 cleanup_jumplist(wp, TRUE);
5338
Bram Moolenaar4f505882018-02-10 21:06:32 +01005339 l = list_alloc();
5340 if (l == NULL)
5341 return;
5342
5343 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5344 return;
5345 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5346
5347 for (i = 0; i < wp->w_jumplistlen; ++i)
5348 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005349 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5350 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005351 if ((d = dict_alloc()) == NULL)
5352 return;
5353 if (list_append_dict(l, d) == FAIL)
5354 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005355 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5356 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005357 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005358 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005359 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005360 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005361 }
5362#endif
5363}
5364
5365/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005366 * "getline(lnum, [end])" function
5367 */
5368 static void
5369f_getline(typval_T *argvars, typval_T *rettv)
5370{
5371 linenr_T lnum;
5372 linenr_T end;
5373 int retlist;
5374
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005375 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005376 if (argvars[1].v_type == VAR_UNKNOWN)
5377 {
5378 end = 0;
5379 retlist = FALSE;
5380 }
5381 else
5382 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005383 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005384 retlist = TRUE;
5385 }
5386
5387 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5388}
5389
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005390#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005391 static void
5392get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5393{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005394 if (what_arg->v_type == VAR_UNKNOWN)
5395 {
5396 if (rettv_list_alloc(rettv) == OK)
5397 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005398 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005399 }
5400 else
5401 {
5402 if (rettv_dict_alloc(rettv) == OK)
5403 if (is_qf || (wp != NULL))
5404 {
5405 if (what_arg->v_type == VAR_DICT)
5406 {
5407 dict_T *d = what_arg->vval.v_dict;
5408
5409 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005410 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005411 }
5412 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005413 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005414 }
5415 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005416}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005417#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005418
5419/*
5420 * "getloclist()" function
5421 */
5422 static void
5423f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5424{
5425#ifdef FEAT_QUICKFIX
5426 win_T *wp;
5427
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005428 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005429 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5430#endif
5431}
5432
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005433/*
5434 * "getmatches()" function
5435 */
5436 static void
5437f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5438{
5439#ifdef FEAT_SEARCH_EXTRA
5440 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005441 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005442 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005443 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005444
Bram Moolenaaraff74912019-03-30 18:11:49 +01005445 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5446 return;
5447
5448 cur = win->w_match_head;
5449 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005450 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005451 dict = dict_alloc();
5452 if (dict == NULL)
5453 return;
5454 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005455 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005456 /* match added with matchaddpos() */
5457 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005458 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005459 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005460 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005461 list_T *l;
5462
5463 llpos = &cur->pos.pos[i];
5464 if (llpos->lnum == 0)
5465 break;
5466 l = list_alloc();
5467 if (l == NULL)
5468 break;
5469 list_append_number(l, (varnumber_T)llpos->lnum);
5470 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005471 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005472 list_append_number(l, (varnumber_T)llpos->col);
5473 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005474 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005475 sprintf(buf, "pos%d", i + 1);
5476 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005477 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005478 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005479 else
5480 {
5481 dict_add_string(dict, "pattern", cur->pattern);
5482 }
5483 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5484 dict_add_number(dict, "priority", (long)cur->priority);
5485 dict_add_number(dict, "id", (long)cur->id);
5486# if defined(FEAT_CONCEAL)
5487 if (cur->conceal_char)
5488 {
5489 char_u buf[MB_MAXBYTES + 1];
5490
5491 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5492 dict_add_string(dict, "conceal", (char_u *)&buf);
5493 }
5494# endif
5495 list_append_dict(rettv->vval.v_list, dict);
5496 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005497 }
5498#endif
5499}
5500
5501/*
5502 * "getpid()" function
5503 */
5504 static void
5505f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5506{
5507 rettv->vval.v_number = mch_get_pid();
5508}
5509
5510 static void
5511getpos_both(
5512 typval_T *argvars,
5513 typval_T *rettv,
5514 int getcurpos)
5515{
5516 pos_T *fp;
5517 list_T *l;
5518 int fnum = -1;
5519
5520 if (rettv_list_alloc(rettv) == OK)
5521 {
5522 l = rettv->vval.v_list;
5523 if (getcurpos)
5524 fp = &curwin->w_cursor;
5525 else
5526 fp = var2fpos(&argvars[0], TRUE, &fnum);
5527 if (fnum != -1)
5528 list_append_number(l, (varnumber_T)fnum);
5529 else
5530 list_append_number(l, (varnumber_T)0);
5531 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5532 : (varnumber_T)0);
5533 list_append_number(l, (fp != NULL)
5534 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5535 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005536 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005537 (varnumber_T)0);
5538 if (getcurpos)
5539 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005540 int save_set_curswant = curwin->w_set_curswant;
5541 colnr_T save_curswant = curwin->w_curswant;
5542 colnr_T save_virtcol = curwin->w_virtcol;
5543
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005544 update_curswant();
5545 list_append_number(l, curwin->w_curswant == MAXCOL ?
5546 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005547
5548 // Do not change "curswant", as it is unexpected that a get
5549 // function has a side effect.
5550 if (save_set_curswant)
5551 {
5552 curwin->w_set_curswant = save_set_curswant;
5553 curwin->w_curswant = save_curswant;
5554 curwin->w_virtcol = save_virtcol;
5555 curwin->w_valid &= ~VALID_VIRTCOL;
5556 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557 }
5558 }
5559 else
5560 rettv->vval.v_number = FALSE;
5561}
5562
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005563/*
5564 * "getcurpos()" function
5565 */
5566 static void
5567f_getcurpos(typval_T *argvars, typval_T *rettv)
5568{
5569 getpos_both(argvars, rettv, TRUE);
5570}
5571
5572/*
5573 * "getpos(string)" function
5574 */
5575 static void
5576f_getpos(typval_T *argvars, typval_T *rettv)
5577{
5578 getpos_both(argvars, rettv, FALSE);
5579}
5580
5581/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005582 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005583 */
5584 static void
5585f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5586{
5587#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005588 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005589#endif
5590}
5591
5592/*
5593 * "getreg()" function
5594 */
5595 static void
5596f_getreg(typval_T *argvars, typval_T *rettv)
5597{
5598 char_u *strregname;
5599 int regname;
5600 int arg2 = FALSE;
5601 int return_list = FALSE;
5602 int error = FALSE;
5603
5604 if (argvars[0].v_type != VAR_UNKNOWN)
5605 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005606 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005607 error = strregname == NULL;
5608 if (argvars[1].v_type != VAR_UNKNOWN)
5609 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005610 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005611 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005612 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005613 }
5614 }
5615 else
5616 strregname = get_vim_var_str(VV_REG);
5617
5618 if (error)
5619 return;
5620
5621 regname = (strregname == NULL ? '"' : *strregname);
5622 if (regname == 0)
5623 regname = '"';
5624
5625 if (return_list)
5626 {
5627 rettv->v_type = VAR_LIST;
5628 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5629 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5630 if (rettv->vval.v_list == NULL)
5631 (void)rettv_list_alloc(rettv);
5632 else
5633 ++rettv->vval.v_list->lv_refcount;
5634 }
5635 else
5636 {
5637 rettv->v_type = VAR_STRING;
5638 rettv->vval.v_string = get_reg_contents(regname,
5639 arg2 ? GREG_EXPR_SRC : 0);
5640 }
5641}
5642
5643/*
5644 * "getregtype()" function
5645 */
5646 static void
5647f_getregtype(typval_T *argvars, typval_T *rettv)
5648{
5649 char_u *strregname;
5650 int regname;
5651 char_u buf[NUMBUFLEN + 2];
5652 long reglen = 0;
5653
5654 if (argvars[0].v_type != VAR_UNKNOWN)
5655 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005656 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005657 if (strregname == NULL) /* type error; errmsg already given */
5658 {
5659 rettv->v_type = VAR_STRING;
5660 rettv->vval.v_string = NULL;
5661 return;
5662 }
5663 }
5664 else
5665 /* Default to v:register */
5666 strregname = get_vim_var_str(VV_REG);
5667
5668 regname = (strregname == NULL ? '"' : *strregname);
5669 if (regname == 0)
5670 regname = '"';
5671
5672 buf[0] = NUL;
5673 buf[1] = NUL;
5674 switch (get_reg_type(regname, &reglen))
5675 {
5676 case MLINE: buf[0] = 'V'; break;
5677 case MCHAR: buf[0] = 'v'; break;
5678 case MBLOCK:
5679 buf[0] = Ctrl_V;
5680 sprintf((char *)buf + 1, "%ld", reglen + 1);
5681 break;
5682 }
5683 rettv->v_type = VAR_STRING;
5684 rettv->vval.v_string = vim_strsave(buf);
5685}
5686
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005687/*
5688 * Returns information (variables, options, etc.) about a tab page
5689 * as a dictionary.
5690 */
5691 static dict_T *
5692get_tabpage_info(tabpage_T *tp, int tp_idx)
5693{
5694 win_T *wp;
5695 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005696 list_T *l;
5697
5698 dict = dict_alloc();
5699 if (dict == NULL)
5700 return NULL;
5701
Bram Moolenaare0be1672018-07-08 16:50:37 +02005702 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005703
5704 l = list_alloc();
5705 if (l != NULL)
5706 {
5707 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02005708 wp != NULL; wp = wp->w_next)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005709 list_append_number(l, (varnumber_T)wp->w_id);
5710 dict_add_list(dict, "windows", l);
5711 }
5712
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005713 /* Make a reference to tabpage variables */
5714 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005715
5716 return dict;
5717}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005718
5719/*
5720 * "gettabinfo()" function
5721 */
5722 static void
5723f_gettabinfo(typval_T *argvars, typval_T *rettv)
5724{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005725 tabpage_T *tp, *tparg = NULL;
5726 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005727 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005728
5729 if (rettv_list_alloc(rettv) != OK)
5730 return;
5731
5732 if (argvars[0].v_type != VAR_UNKNOWN)
5733 {
5734 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005735 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005736 if (tparg == NULL)
5737 return;
5738 }
5739
5740 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005741 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005742 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005743 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005744 if (tparg != NULL && tp != tparg)
5745 continue;
5746 d = get_tabpage_info(tp, tpnr);
5747 if (d != NULL)
5748 list_append_dict(rettv->vval.v_list, d);
5749 if (tparg != NULL)
5750 return;
5751 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005752}
5753
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005754/*
5755 * "gettabvar()" function
5756 */
5757 static void
5758f_gettabvar(typval_T *argvars, typval_T *rettv)
5759{
5760 win_T *oldcurwin;
5761 tabpage_T *tp, *oldtabpage;
5762 dictitem_T *v;
5763 char_u *varname;
5764 int done = FALSE;
5765
5766 rettv->v_type = VAR_STRING;
5767 rettv->vval.v_string = NULL;
5768
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005769 varname = tv_get_string_chk(&argvars[1]);
5770 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005771 if (tp != NULL && varname != NULL)
5772 {
5773 /* Set tp to be our tabpage, temporarily. Also set the window to the
5774 * first window in the tabpage, otherwise the window is not valid. */
5775 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005776 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5777 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005778 {
5779 /* look up the variable */
5780 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5781 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5782 if (v != NULL)
5783 {
5784 copy_tv(&v->di_tv, rettv);
5785 done = TRUE;
5786 }
5787 }
5788
5789 /* restore previous notion of curwin */
5790 restore_win(oldcurwin, oldtabpage, TRUE);
5791 }
5792
5793 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5794 copy_tv(&argvars[2], rettv);
5795}
5796
5797/*
5798 * "gettabwinvar()" function
5799 */
5800 static void
5801f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5802{
5803 getwinvar(argvars, rettv, 1);
5804}
5805
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005806/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005807 * "gettagstack()" function
5808 */
5809 static void
5810f_gettagstack(typval_T *argvars, typval_T *rettv)
5811{
5812 win_T *wp = curwin; // default is current window
5813
5814 if (rettv_dict_alloc(rettv) != OK)
5815 return;
5816
5817 if (argvars[0].v_type != VAR_UNKNOWN)
5818 {
5819 wp = find_win_by_nr_or_id(&argvars[0]);
5820 if (wp == NULL)
5821 return;
5822 }
5823
5824 get_tagstack(wp, rettv->vval.v_dict);
5825}
5826
5827/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005828 * Returns information about a window as a dictionary.
5829 */
5830 static dict_T *
5831get_win_info(win_T *wp, short tpnr, short winnr)
5832{
5833 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005834
5835 dict = dict_alloc();
5836 if (dict == NULL)
5837 return NULL;
5838
Bram Moolenaare0be1672018-07-08 16:50:37 +02005839 dict_add_number(dict, "tabnr", tpnr);
5840 dict_add_number(dict, "winnr", winnr);
5841 dict_add_number(dict, "winid", wp->w_id);
5842 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005843 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005844 dict_add_number(dict, "topline", wp->w_topline);
5845 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005846#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005847 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005848#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005849 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005850 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005851 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005852
Bram Moolenaar69905d12017-08-13 18:14:47 +02005853#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005854 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005855#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005856#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005857 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5858 dict_add_number(dict, "loclist",
5859 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005860#endif
5861
Bram Moolenaar30567352016-08-27 21:25:44 +02005862 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005863 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005864
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005865 return dict;
5866}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005867
5868/*
5869 * "getwininfo()" function
5870 */
5871 static void
5872f_getwininfo(typval_T *argvars, typval_T *rettv)
5873{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005874 tabpage_T *tp;
5875 win_T *wp = NULL, *wparg = NULL;
5876 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005877 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005878
5879 if (rettv_list_alloc(rettv) != OK)
5880 return;
5881
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005882 if (argvars[0].v_type != VAR_UNKNOWN)
5883 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005884 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005885 if (wparg == NULL)
5886 return;
5887 }
5888
5889 /* Collect information about either all the windows across all the tab
5890 * pages or one particular window.
5891 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005892 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005893 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005894 tabnr++;
5895 winnr = 0;
5896 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005897 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005898 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005899 if (wparg != NULL && wp != wparg)
5900 continue;
5901 d = get_win_info(wp, tabnr, winnr);
5902 if (d != NULL)
5903 list_append_dict(rettv->vval.v_list, d);
5904 if (wparg != NULL)
5905 /* found information about a specific window */
5906 return;
5907 }
5908 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005909}
5910
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005911/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005912 * "win_execute()" function
5913 */
5914 static void
5915f_win_execute(typval_T *argvars, typval_T *rettv)
5916{
5917 int id = (int)tv_get_number(argvars);
5918 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005919 win_T *save_curwin;
5920 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005921
5922 if (wp != NULL)
5923 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005924 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5925 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005926 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005927 check_cursor();
5928 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005929 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005930 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005931 }
5932}
5933
5934/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005935 * "win_findbuf()" function
5936 */
5937 static void
5938f_win_findbuf(typval_T *argvars, typval_T *rettv)
5939{
5940 if (rettv_list_alloc(rettv) != FAIL)
5941 win_findbuf(argvars, rettv->vval.v_list);
5942}
5943
5944/*
5945 * "win_getid()" function
5946 */
5947 static void
5948f_win_getid(typval_T *argvars, typval_T *rettv)
5949{
5950 rettv->vval.v_number = win_getid(argvars);
5951}
5952
5953/*
5954 * "win_gotoid()" function
5955 */
5956 static void
5957f_win_gotoid(typval_T *argvars, typval_T *rettv)
5958{
5959 rettv->vval.v_number = win_gotoid(argvars);
5960}
5961
5962/*
5963 * "win_id2tabwin()" function
5964 */
5965 static void
5966f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5967{
5968 if (rettv_list_alloc(rettv) != FAIL)
5969 win_id2tabwin(argvars, rettv->vval.v_list);
5970}
5971
5972/*
5973 * "win_id2win()" function
5974 */
5975 static void
5976f_win_id2win(typval_T *argvars, typval_T *rettv)
5977{
5978 rettv->vval.v_number = win_id2win(argvars);
5979}
5980
5981/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005982 * "win_screenpos()" function
5983 */
5984 static void
5985f_win_screenpos(typval_T *argvars, typval_T *rettv)
5986{
5987 win_T *wp;
5988
5989 if (rettv_list_alloc(rettv) == FAIL)
5990 return;
5991
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005992 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005993 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5994 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5995}
5996
5997/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005998 * "getwinpos({timeout})" function
5999 */
6000 static void
6001f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6002{
6003 int x = -1;
6004 int y = -1;
6005
6006 if (rettv_list_alloc(rettv) == FAIL)
6007 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006008#if defined(FEAT_GUI) \
6009 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6010 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006011 {
6012 varnumber_T timeout = 100;
6013
6014 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006015 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006016
6017 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006018 }
6019#endif
6020 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6021 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6022}
6023
6024
6025/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006026 * "getwinposx()" function
6027 */
6028 static void
6029f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6030{
6031 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006032#if defined(FEAT_GUI) \
6033 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6034 || defined(MSWIN)
6035
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006036 {
6037 int x, y;
6038
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006039 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006040 rettv->vval.v_number = x;
6041 }
6042#endif
6043}
6044
6045/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006046 * "getwinposy()" function
6047 */
6048 static void
6049f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6050{
6051 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006052#if defined(FEAT_GUI) \
6053 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6054 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006055 {
6056 int x, y;
6057
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006058 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006059 rettv->vval.v_number = y;
6060 }
6061#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006062}
6063
6064/*
6065 * "getwinvar()" function
6066 */
6067 static void
6068f_getwinvar(typval_T *argvars, typval_T *rettv)
6069{
6070 getwinvar(argvars, rettv, 0);
6071}
6072
6073/*
6074 * "glob()" function
6075 */
6076 static void
6077f_glob(typval_T *argvars, typval_T *rettv)
6078{
6079 int options = WILD_SILENT|WILD_USE_NL;
6080 expand_T xpc;
6081 int error = FALSE;
6082
6083 /* When the optional second argument is non-zero, don't remove matches
6084 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6085 rettv->v_type = VAR_STRING;
6086 if (argvars[1].v_type != VAR_UNKNOWN)
6087 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006088 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006089 options |= WILD_KEEP_ALL;
6090 if (argvars[2].v_type != VAR_UNKNOWN)
6091 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006092 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006093 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006094 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006095 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006096 options |= WILD_ALLLINKS;
6097 }
6098 }
6099 if (!error)
6100 {
6101 ExpandInit(&xpc);
6102 xpc.xp_context = EXPAND_FILES;
6103 if (p_wic)
6104 options += WILD_ICASE;
6105 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006106 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006107 NULL, options, WILD_ALL);
6108 else if (rettv_list_alloc(rettv) != FAIL)
6109 {
6110 int i;
6111
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006112 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006113 NULL, options, WILD_ALL_KEEP);
6114 for (i = 0; i < xpc.xp_numfiles; i++)
6115 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6116
6117 ExpandCleanup(&xpc);
6118 }
6119 }
6120 else
6121 rettv->vval.v_string = NULL;
6122}
6123
6124/*
6125 * "globpath()" function
6126 */
6127 static void
6128f_globpath(typval_T *argvars, typval_T *rettv)
6129{
6130 int flags = 0;
6131 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006132 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006133 int error = FALSE;
6134 garray_T ga;
6135 int i;
6136
6137 /* When the optional second argument is non-zero, don't remove matches
6138 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6139 rettv->v_type = VAR_STRING;
6140 if (argvars[2].v_type != VAR_UNKNOWN)
6141 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006142 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006143 flags |= WILD_KEEP_ALL;
6144 if (argvars[3].v_type != VAR_UNKNOWN)
6145 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006146 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006147 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006148 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006149 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006150 flags |= WILD_ALLLINKS;
6151 }
6152 }
6153 if (file != NULL && !error)
6154 {
6155 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006156 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006157 if (rettv->v_type == VAR_STRING)
6158 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6159 else if (rettv_list_alloc(rettv) != FAIL)
6160 for (i = 0; i < ga.ga_len; ++i)
6161 list_append_string(rettv->vval.v_list,
6162 ((char_u **)(ga.ga_data))[i], -1);
6163 ga_clear_strings(&ga);
6164 }
6165 else
6166 rettv->vval.v_string = NULL;
6167}
6168
6169/*
6170 * "glob2regpat()" function
6171 */
6172 static void
6173f_glob2regpat(typval_T *argvars, typval_T *rettv)
6174{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006175 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006176
6177 rettv->v_type = VAR_STRING;
6178 rettv->vval.v_string = (pat == NULL)
6179 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6180}
6181
6182/* for VIM_VERSION_ defines */
6183#include "version.h"
6184
6185/*
6186 * "has()" function
6187 */
6188 static void
6189f_has(typval_T *argvars, typval_T *rettv)
6190{
6191 int i;
6192 char_u *name;
6193 int n = FALSE;
6194 static char *(has_list[]) =
6195 {
6196#ifdef AMIGA
6197 "amiga",
6198# ifdef FEAT_ARP
6199 "arp",
6200# endif
6201#endif
6202#ifdef __BEOS__
6203 "beos",
6204#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006205#if defined(BSD) && !defined(MACOS_X)
6206 "bsd",
6207#endif
6208#ifdef hpux
6209 "hpux",
6210#endif
6211#ifdef __linux__
6212 "linux",
6213#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006214#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006215 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6216 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006217# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006218 "macunix", /* Mac OS X, with the darwin feature */
6219 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006220# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006221#endif
6222#ifdef __QNX__
6223 "qnx",
6224#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006225#ifdef SUN_SYSTEM
6226 "sun",
6227#else
6228 "moon",
6229#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006230#ifdef UNIX
6231 "unix",
6232#endif
6233#ifdef VMS
6234 "vms",
6235#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006236#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006237 "win32",
6238#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006239#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006240 "win32unix",
6241#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006242#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006243 "win64",
6244#endif
6245#ifdef EBCDIC
6246 "ebcdic",
6247#endif
6248#ifndef CASE_INSENSITIVE_FILENAME
6249 "fname_case",
6250#endif
6251#ifdef HAVE_ACL
6252 "acl",
6253#endif
6254#ifdef FEAT_ARABIC
6255 "arabic",
6256#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006257 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006258#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006259 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006260#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006261#ifdef FEAT_AUTOSERVERNAME
6262 "autoservername",
6263#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006264#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006265 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006266# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006267 "balloon_multiline",
6268# endif
6269#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006270#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006271 "balloon_eval_term",
6272#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006273#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6274 "builtin_terms",
6275# ifdef ALL_BUILTIN_TCAPS
6276 "all_builtin_terms",
6277# endif
6278#endif
6279#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006280 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006281 || defined(FEAT_GUI_MOTIF))
6282 "browsefilter",
6283#endif
6284#ifdef FEAT_BYTEOFF
6285 "byte_offset",
6286#endif
6287#ifdef FEAT_JOB_CHANNEL
6288 "channel",
6289#endif
6290#ifdef FEAT_CINDENT
6291 "cindent",
6292#endif
6293#ifdef FEAT_CLIENTSERVER
6294 "clientserver",
6295#endif
6296#ifdef FEAT_CLIPBOARD
6297 "clipboard",
6298#endif
6299#ifdef FEAT_CMDL_COMPL
6300 "cmdline_compl",
6301#endif
6302#ifdef FEAT_CMDHIST
6303 "cmdline_hist",
6304#endif
6305#ifdef FEAT_COMMENTS
6306 "comments",
6307#endif
6308#ifdef FEAT_CONCEAL
6309 "conceal",
6310#endif
6311#ifdef FEAT_CRYPT
6312 "cryptv",
6313 "crypt-blowfish",
6314 "crypt-blowfish2",
6315#endif
6316#ifdef FEAT_CSCOPE
6317 "cscope",
6318#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006319 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006320#ifdef CURSOR_SHAPE
6321 "cursorshape",
6322#endif
6323#ifdef DEBUG
6324 "debug",
6325#endif
6326#ifdef FEAT_CON_DIALOG
6327 "dialog_con",
6328#endif
6329#ifdef FEAT_GUI_DIALOG
6330 "dialog_gui",
6331#endif
6332#ifdef FEAT_DIFF
6333 "diff",
6334#endif
6335#ifdef FEAT_DIGRAPHS
6336 "digraphs",
6337#endif
6338#ifdef FEAT_DIRECTX
6339 "directx",
6340#endif
6341#ifdef FEAT_DND
6342 "dnd",
6343#endif
6344#ifdef FEAT_EMACS_TAGS
6345 "emacs_tags",
6346#endif
6347 "eval", /* always present, of course! */
6348 "ex_extra", /* graduated feature */
6349#ifdef FEAT_SEARCH_EXTRA
6350 "extra_search",
6351#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006352#ifdef FEAT_SEARCHPATH
6353 "file_in_path",
6354#endif
6355#ifdef FEAT_FILTERPIPE
6356 "filterpipe",
6357#endif
6358#ifdef FEAT_FIND_ID
6359 "find_in_path",
6360#endif
6361#ifdef FEAT_FLOAT
6362 "float",
6363#endif
6364#ifdef FEAT_FOLDING
6365 "folding",
6366#endif
6367#ifdef FEAT_FOOTER
6368 "footer",
6369#endif
6370#if !defined(USE_SYSTEM) && defined(UNIX)
6371 "fork",
6372#endif
6373#ifdef FEAT_GETTEXT
6374 "gettext",
6375#endif
6376#ifdef FEAT_GUI
6377 "gui",
6378#endif
6379#ifdef FEAT_GUI_ATHENA
6380# ifdef FEAT_GUI_NEXTAW
6381 "gui_neXtaw",
6382# else
6383 "gui_athena",
6384# endif
6385#endif
6386#ifdef FEAT_GUI_GTK
6387 "gui_gtk",
6388# ifdef USE_GTK3
6389 "gui_gtk3",
6390# else
6391 "gui_gtk2",
6392# endif
6393#endif
6394#ifdef FEAT_GUI_GNOME
6395 "gui_gnome",
6396#endif
6397#ifdef FEAT_GUI_MAC
6398 "gui_mac",
6399#endif
6400#ifdef FEAT_GUI_MOTIF
6401 "gui_motif",
6402#endif
6403#ifdef FEAT_GUI_PHOTON
6404 "gui_photon",
6405#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006406#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006407 "gui_win32",
6408#endif
6409#ifdef FEAT_HANGULIN
6410 "hangul_input",
6411#endif
6412#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6413 "iconv",
6414#endif
6415#ifdef FEAT_INS_EXPAND
6416 "insert_expand",
6417#endif
6418#ifdef FEAT_JOB_CHANNEL
6419 "job",
6420#endif
6421#ifdef FEAT_JUMPLIST
6422 "jumplist",
6423#endif
6424#ifdef FEAT_KEYMAP
6425 "keymap",
6426#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006427 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006428#ifdef FEAT_LANGMAP
6429 "langmap",
6430#endif
6431#ifdef FEAT_LIBCALL
6432 "libcall",
6433#endif
6434#ifdef FEAT_LINEBREAK
6435 "linebreak",
6436#endif
6437#ifdef FEAT_LISP
6438 "lispindent",
6439#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006440 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006441#ifdef FEAT_LOCALMAP
6442 "localmap",
6443#endif
6444#ifdef FEAT_LUA
6445# ifndef DYNAMIC_LUA
6446 "lua",
6447# endif
6448#endif
6449#ifdef FEAT_MENU
6450 "menu",
6451#endif
6452#ifdef FEAT_SESSION
6453 "mksession",
6454#endif
6455#ifdef FEAT_MODIFY_FNAME
6456 "modify_fname",
6457#endif
6458#ifdef FEAT_MOUSE
6459 "mouse",
6460#endif
6461#ifdef FEAT_MOUSESHAPE
6462 "mouseshape",
6463#endif
6464#if defined(UNIX) || defined(VMS)
6465# ifdef FEAT_MOUSE_DEC
6466 "mouse_dec",
6467# endif
6468# ifdef FEAT_MOUSE_GPM
6469 "mouse_gpm",
6470# endif
6471# ifdef FEAT_MOUSE_JSB
6472 "mouse_jsbterm",
6473# endif
6474# ifdef FEAT_MOUSE_NET
6475 "mouse_netterm",
6476# endif
6477# ifdef FEAT_MOUSE_PTERM
6478 "mouse_pterm",
6479# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006480# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006481 "mouse_sgr",
6482# endif
6483# ifdef FEAT_SYSMOUSE
6484 "mouse_sysmouse",
6485# endif
6486# ifdef FEAT_MOUSE_URXVT
6487 "mouse_urxvt",
6488# endif
6489# ifdef FEAT_MOUSE_XTERM
6490 "mouse_xterm",
6491# endif
6492#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006493 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006494#ifdef FEAT_MBYTE_IME
6495 "multi_byte_ime",
6496#endif
6497#ifdef FEAT_MULTI_LANG
6498 "multi_lang",
6499#endif
6500#ifdef FEAT_MZSCHEME
6501#ifndef DYNAMIC_MZSCHEME
6502 "mzscheme",
6503#endif
6504#endif
6505#ifdef FEAT_NUM64
6506 "num64",
6507#endif
6508#ifdef FEAT_OLE
6509 "ole",
6510#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006511#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006512 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006513#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006514#ifdef FEAT_PATH_EXTRA
6515 "path_extra",
6516#endif
6517#ifdef FEAT_PERL
6518#ifndef DYNAMIC_PERL
6519 "perl",
6520#endif
6521#endif
6522#ifdef FEAT_PERSISTENT_UNDO
6523 "persistent_undo",
6524#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006525#if defined(FEAT_PYTHON)
6526 "python_compiled",
6527# if defined(DYNAMIC_PYTHON)
6528 "python_dynamic",
6529# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006530 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006531 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006532# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006533#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006534#if defined(FEAT_PYTHON3)
6535 "python3_compiled",
6536# if defined(DYNAMIC_PYTHON3)
6537 "python3_dynamic",
6538# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006539 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006540 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006541# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006542#endif
6543#ifdef FEAT_POSTSCRIPT
6544 "postscript",
6545#endif
6546#ifdef FEAT_PRINTER
6547 "printer",
6548#endif
6549#ifdef FEAT_PROFILE
6550 "profile",
6551#endif
6552#ifdef FEAT_RELTIME
6553 "reltime",
6554#endif
6555#ifdef FEAT_QUICKFIX
6556 "quickfix",
6557#endif
6558#ifdef FEAT_RIGHTLEFT
6559 "rightleft",
6560#endif
6561#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6562 "ruby",
6563#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006564 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565#ifdef FEAT_CMDL_INFO
6566 "showcmd",
6567 "cmdline_info",
6568#endif
6569#ifdef FEAT_SIGNS
6570 "signs",
6571#endif
6572#ifdef FEAT_SMARTINDENT
6573 "smartindent",
6574#endif
6575#ifdef STARTUPTIME
6576 "startuptime",
6577#endif
6578#ifdef FEAT_STL_OPT
6579 "statusline",
6580#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006581#ifdef FEAT_NETBEANS_INTG
6582 "netbeans_intg",
6583#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006584#ifdef FEAT_SOUND
6585 "sound",
6586#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006587#ifdef FEAT_SPELL
6588 "spell",
6589#endif
6590#ifdef FEAT_SYN_HL
6591 "syntax",
6592#endif
6593#if defined(USE_SYSTEM) || !defined(UNIX)
6594 "system",
6595#endif
6596#ifdef FEAT_TAG_BINS
6597 "tag_binary",
6598#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006599#ifdef FEAT_TCL
6600# ifndef DYNAMIC_TCL
6601 "tcl",
6602# endif
6603#endif
6604#ifdef FEAT_TERMGUICOLORS
6605 "termguicolors",
6606#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006607#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006608 "terminal",
6609#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006610#ifdef TERMINFO
6611 "terminfo",
6612#endif
6613#ifdef FEAT_TERMRESPONSE
6614 "termresponse",
6615#endif
6616#ifdef FEAT_TEXTOBJ
6617 "textobjects",
6618#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006619#ifdef FEAT_TEXT_PROP
6620 "textprop",
6621#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006622#ifdef HAVE_TGETENT
6623 "tgetent",
6624#endif
6625#ifdef FEAT_TIMERS
6626 "timers",
6627#endif
6628#ifdef FEAT_TITLE
6629 "title",
6630#endif
6631#ifdef FEAT_TOOLBAR
6632 "toolbar",
6633#endif
6634#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6635 "unnamedplus",
6636#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006637 "user-commands", /* was accidentally included in 5.4 */
6638 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006639#ifdef FEAT_VARTABS
6640 "vartabs",
6641#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006642 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006643#ifdef FEAT_VIMINFO
6644 "viminfo",
6645#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006646 "vimscript-1",
6647 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006648 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006649 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006651 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006652 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006653#ifdef FEAT_VTP
6654 "vtp",
6655#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006656#ifdef FEAT_WILDIGN
6657 "wildignore",
6658#endif
6659#ifdef FEAT_WILDMENU
6660 "wildmenu",
6661#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006662 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006663#ifdef FEAT_WAK
6664 "winaltkeys",
6665#endif
6666#ifdef FEAT_WRITEBACKUP
6667 "writebackup",
6668#endif
6669#ifdef FEAT_XIM
6670 "xim",
6671#endif
6672#ifdef FEAT_XFONTSET
6673 "xfontset",
6674#endif
6675#ifdef FEAT_XPM_W32
6676 "xpm",
6677 "xpm_w32", /* for backward compatibility */
6678#else
6679# if defined(HAVE_XPM)
6680 "xpm",
6681# endif
6682#endif
6683#ifdef USE_XSMP
6684 "xsmp",
6685#endif
6686#ifdef USE_XSMP_INTERACT
6687 "xsmp_interact",
6688#endif
6689#ifdef FEAT_XCLIPBOARD
6690 "xterm_clipboard",
6691#endif
6692#ifdef FEAT_XTERM_SAVE
6693 "xterm_save",
6694#endif
6695#if defined(UNIX) && defined(FEAT_X11)
6696 "X11",
6697#endif
6698 NULL
6699 };
6700
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006701 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006702 for (i = 0; has_list[i] != NULL; ++i)
6703 if (STRICMP(name, has_list[i]) == 0)
6704 {
6705 n = TRUE;
6706 break;
6707 }
6708
6709 if (n == FALSE)
6710 {
6711 if (STRNICMP(name, "patch", 5) == 0)
6712 {
6713 if (name[5] == '-'
6714 && STRLEN(name) >= 11
6715 && vim_isdigit(name[6])
6716 && vim_isdigit(name[8])
6717 && vim_isdigit(name[10]))
6718 {
6719 int major = atoi((char *)name + 6);
6720 int minor = atoi((char *)name + 8);
6721
6722 /* Expect "patch-9.9.01234". */
6723 n = (major < VIM_VERSION_MAJOR
6724 || (major == VIM_VERSION_MAJOR
6725 && (minor < VIM_VERSION_MINOR
6726 || (minor == VIM_VERSION_MINOR
6727 && has_patch(atoi((char *)name + 10))))));
6728 }
6729 else
6730 n = has_patch(atoi((char *)name + 5));
6731 }
6732 else if (STRICMP(name, "vim_starting") == 0)
6733 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006734 else if (STRICMP(name, "ttyin") == 0)
6735 n = mch_input_isatty();
6736 else if (STRICMP(name, "ttyout") == 0)
6737 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006738 else if (STRICMP(name, "multi_byte_encoding") == 0)
6739 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006740#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006741 else if (STRICMP(name, "balloon_multiline") == 0)
6742 n = multiline_balloon_available();
6743#endif
6744#ifdef DYNAMIC_TCL
6745 else if (STRICMP(name, "tcl") == 0)
6746 n = tcl_enabled(FALSE);
6747#endif
6748#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6749 else if (STRICMP(name, "iconv") == 0)
6750 n = iconv_enabled(FALSE);
6751#endif
6752#ifdef DYNAMIC_LUA
6753 else if (STRICMP(name, "lua") == 0)
6754 n = lua_enabled(FALSE);
6755#endif
6756#ifdef DYNAMIC_MZSCHEME
6757 else if (STRICMP(name, "mzscheme") == 0)
6758 n = mzscheme_enabled(FALSE);
6759#endif
6760#ifdef DYNAMIC_RUBY
6761 else if (STRICMP(name, "ruby") == 0)
6762 n = ruby_enabled(FALSE);
6763#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006764#ifdef DYNAMIC_PYTHON
6765 else if (STRICMP(name, "python") == 0)
6766 n = python_enabled(FALSE);
6767#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006768#ifdef DYNAMIC_PYTHON3
6769 else if (STRICMP(name, "python3") == 0)
6770 n = python3_enabled(FALSE);
6771#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006772#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6773 else if (STRICMP(name, "pythonx") == 0)
6774 {
6775# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6776 if (p_pyx == 0)
6777 n = python3_enabled(FALSE) || python_enabled(FALSE);
6778 else if (p_pyx == 3)
6779 n = python3_enabled(FALSE);
6780 else if (p_pyx == 2)
6781 n = python_enabled(FALSE);
6782# elif defined(DYNAMIC_PYTHON)
6783 n = python_enabled(FALSE);
6784# elif defined(DYNAMIC_PYTHON3)
6785 n = python3_enabled(FALSE);
6786# endif
6787 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006788#endif
6789#ifdef DYNAMIC_PERL
6790 else if (STRICMP(name, "perl") == 0)
6791 n = perl_enabled(FALSE);
6792#endif
6793#ifdef FEAT_GUI
6794 else if (STRICMP(name, "gui_running") == 0)
6795 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006796# ifdef FEAT_BROWSE
6797 else if (STRICMP(name, "browse") == 0)
6798 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6799# endif
6800#endif
6801#ifdef FEAT_SYN_HL
6802 else if (STRICMP(name, "syntax_items") == 0)
6803 n = syntax_present(curwin);
6804#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006805#ifdef FEAT_VTP
6806 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006807 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006808#endif
6809#ifdef FEAT_NETBEANS_INTG
6810 else if (STRICMP(name, "netbeans_enabled") == 0)
6811 n = netbeans_active();
6812#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006813#ifdef FEAT_MOUSE_GPM
6814 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6815 n = gpm_enabled();
6816#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006817#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006818 else if (STRICMP(name, "terminal") == 0)
6819 n = terminal_enabled();
6820#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006821#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006822 else if (STRICMP(name, "conpty") == 0)
6823 n = use_conpty();
6824#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006825 }
6826
6827 rettv->vval.v_number = n;
6828}
6829
6830/*
6831 * "has_key()" function
6832 */
6833 static void
6834f_has_key(typval_T *argvars, typval_T *rettv)
6835{
6836 if (argvars[0].v_type != VAR_DICT)
6837 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006838 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006839 return;
6840 }
6841 if (argvars[0].vval.v_dict == NULL)
6842 return;
6843
6844 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006845 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006846}
6847
6848/*
6849 * "haslocaldir()" function
6850 */
6851 static void
6852f_haslocaldir(typval_T *argvars, typval_T *rettv)
6853{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006854 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006855 win_T *wp = NULL;
6856
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006857 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6858
6859 // Check for window-local and tab-local directories
6860 if (wp != NULL && wp->w_localdir != NULL)
6861 rettv->vval.v_number = 1;
6862 else if (tp != NULL && tp->tp_localdir != NULL)
6863 rettv->vval.v_number = 2;
6864 else
6865 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006866}
6867
6868/*
6869 * "hasmapto()" function
6870 */
6871 static void
6872f_hasmapto(typval_T *argvars, typval_T *rettv)
6873{
6874 char_u *name;
6875 char_u *mode;
6876 char_u buf[NUMBUFLEN];
6877 int abbr = FALSE;
6878
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006879 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006880 if (argvars[1].v_type == VAR_UNKNOWN)
6881 mode = (char_u *)"nvo";
6882 else
6883 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006884 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006885 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006886 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006887 }
6888
6889 if (map_to_exists(name, mode, abbr))
6890 rettv->vval.v_number = TRUE;
6891 else
6892 rettv->vval.v_number = FALSE;
6893}
6894
6895/*
6896 * "histadd()" function
6897 */
6898 static void
6899f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6900{
6901#ifdef FEAT_CMDHIST
6902 int histype;
6903 char_u *str;
6904 char_u buf[NUMBUFLEN];
6905#endif
6906
6907 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006908 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006909 return;
6910#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006911 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912 histype = str != NULL ? get_histtype(str) : -1;
6913 if (histype >= 0)
6914 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006915 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006916 if (*str != NUL)
6917 {
6918 init_history();
6919 add_to_history(histype, str, FALSE, NUL);
6920 rettv->vval.v_number = TRUE;
6921 return;
6922 }
6923 }
6924#endif
6925}
6926
6927/*
6928 * "histdel()" function
6929 */
6930 static void
6931f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6932{
6933#ifdef FEAT_CMDHIST
6934 int n;
6935 char_u buf[NUMBUFLEN];
6936 char_u *str;
6937
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006938 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006939 if (str == NULL)
6940 n = 0;
6941 else if (argvars[1].v_type == VAR_UNKNOWN)
6942 /* only one argument: clear entire history */
6943 n = clr_history(get_histtype(str));
6944 else if (argvars[1].v_type == VAR_NUMBER)
6945 /* index given: remove that entry */
6946 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006947 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006948 else
6949 /* string given: remove all matching entries */
6950 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006951 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006952 rettv->vval.v_number = n;
6953#endif
6954}
6955
6956/*
6957 * "histget()" function
6958 */
6959 static void
6960f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6961{
6962#ifdef FEAT_CMDHIST
6963 int type;
6964 int idx;
6965 char_u *str;
6966
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006967 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006968 if (str == NULL)
6969 rettv->vval.v_string = NULL;
6970 else
6971 {
6972 type = get_histtype(str);
6973 if (argvars[1].v_type == VAR_UNKNOWN)
6974 idx = get_history_idx(type);
6975 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006976 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006977 /* -1 on type error */
6978 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6979 }
6980#else
6981 rettv->vval.v_string = NULL;
6982#endif
6983 rettv->v_type = VAR_STRING;
6984}
6985
6986/*
6987 * "histnr()" function
6988 */
6989 static void
6990f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6991{
6992 int i;
6993
6994#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006995 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006996
6997 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6998 if (i >= HIST_CMD && i < HIST_COUNT)
6999 i = get_history_idx(i);
7000 else
7001#endif
7002 i = -1;
7003 rettv->vval.v_number = i;
7004}
7005
7006/*
7007 * "highlightID(name)" function
7008 */
7009 static void
7010f_hlID(typval_T *argvars, typval_T *rettv)
7011{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007012 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007013}
7014
7015/*
7016 * "highlight_exists()" function
7017 */
7018 static void
7019f_hlexists(typval_T *argvars, typval_T *rettv)
7020{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007021 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007022}
7023
7024/*
7025 * "hostname()" function
7026 */
7027 static void
7028f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7029{
7030 char_u hostname[256];
7031
7032 mch_get_host_name(hostname, 256);
7033 rettv->v_type = VAR_STRING;
7034 rettv->vval.v_string = vim_strsave(hostname);
7035}
7036
7037/*
7038 * iconv() function
7039 */
7040 static void
7041f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7042{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007043 char_u buf1[NUMBUFLEN];
7044 char_u buf2[NUMBUFLEN];
7045 char_u *from, *to, *str;
7046 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007047
7048 rettv->v_type = VAR_STRING;
7049 rettv->vval.v_string = NULL;
7050
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007051 str = tv_get_string(&argvars[0]);
7052 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7053 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007054 vimconv.vc_type = CONV_NONE;
7055 convert_setup(&vimconv, from, to);
7056
7057 /* If the encodings are equal, no conversion needed. */
7058 if (vimconv.vc_type == CONV_NONE)
7059 rettv->vval.v_string = vim_strsave(str);
7060 else
7061 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7062
7063 convert_setup(&vimconv, NULL, NULL);
7064 vim_free(from);
7065 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007066}
7067
7068/*
7069 * "indent()" function
7070 */
7071 static void
7072f_indent(typval_T *argvars, typval_T *rettv)
7073{
7074 linenr_T lnum;
7075
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007076 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007077 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7078 rettv->vval.v_number = get_indent_lnum(lnum);
7079 else
7080 rettv->vval.v_number = -1;
7081}
7082
7083/*
7084 * "index()" function
7085 */
7086 static void
7087f_index(typval_T *argvars, typval_T *rettv)
7088{
7089 list_T *l;
7090 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007091 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007092 long idx = 0;
7093 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007094 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007095
7096 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007097 if (argvars[0].v_type == VAR_BLOB)
7098 {
7099 typval_T tv;
7100 int start = 0;
7101
7102 if (argvars[2].v_type != VAR_UNKNOWN)
7103 {
7104 start = tv_get_number_chk(&argvars[2], &error);
7105 if (error)
7106 return;
7107 }
7108 b = argvars[0].vval.v_blob;
7109 if (b == NULL)
7110 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007111 if (start < 0)
7112 {
7113 start = blob_len(b) + start;
7114 if (start < 0)
7115 start = 0;
7116 }
7117
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007118 for (idx = start; idx < blob_len(b); ++idx)
7119 {
7120 tv.v_type = VAR_NUMBER;
7121 tv.vval.v_number = blob_get(b, idx);
7122 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7123 {
7124 rettv->vval.v_number = idx;
7125 return;
7126 }
7127 }
7128 return;
7129 }
7130 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007131 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007132 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007133 return;
7134 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007135
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007136 l = argvars[0].vval.v_list;
7137 if (l != NULL)
7138 {
7139 item = l->lv_first;
7140 if (argvars[2].v_type != VAR_UNKNOWN)
7141 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007142 /* Start at specified item. Use the cached index that list_find()
7143 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007144 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007145 idx = l->lv_idx;
7146 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007147 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007148 if (error)
7149 item = NULL;
7150 }
7151
7152 for ( ; item != NULL; item = item->li_next, ++idx)
7153 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7154 {
7155 rettv->vval.v_number = idx;
7156 break;
7157 }
7158 }
7159}
7160
7161static int inputsecret_flag = 0;
7162
7163/*
7164 * "input()" function
7165 * Also handles inputsecret() when inputsecret is set.
7166 */
7167 static void
7168f_input(typval_T *argvars, typval_T *rettv)
7169{
7170 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7171}
7172
7173/*
7174 * "inputdialog()" function
7175 */
7176 static void
7177f_inputdialog(typval_T *argvars, typval_T *rettv)
7178{
7179#if defined(FEAT_GUI_TEXTDIALOG)
7180 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7181 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7182 {
7183 char_u *message;
7184 char_u buf[NUMBUFLEN];
7185 char_u *defstr = (char_u *)"";
7186
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007187 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007188 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007189 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007190 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7191 else
7192 IObuff[0] = NUL;
7193 if (message != NULL && defstr != NULL
7194 && do_dialog(VIM_QUESTION, NULL, message,
7195 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7196 rettv->vval.v_string = vim_strsave(IObuff);
7197 else
7198 {
7199 if (message != NULL && defstr != NULL
7200 && argvars[1].v_type != VAR_UNKNOWN
7201 && argvars[2].v_type != VAR_UNKNOWN)
7202 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007203 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007204 else
7205 rettv->vval.v_string = NULL;
7206 }
7207 rettv->v_type = VAR_STRING;
7208 }
7209 else
7210#endif
7211 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7212}
7213
7214/*
7215 * "inputlist()" function
7216 */
7217 static void
7218f_inputlist(typval_T *argvars, typval_T *rettv)
7219{
7220 listitem_T *li;
7221 int selected;
7222 int mouse_used;
7223
7224#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007225 /* While starting up, there is no place to enter text. When running tests
7226 * with --not-a-term we assume feedkeys() will be used. */
7227 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007228 return;
7229#endif
7230 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007232 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007233 return;
7234 }
7235
7236 msg_start();
7237 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7238 lines_left = Rows; /* avoid more prompt */
7239 msg_scroll = TRUE;
7240 msg_clr_eos();
7241
7242 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7243 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007244 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245 msg_putchar('\n');
7246 }
7247
7248 /* Ask for choice. */
7249 selected = prompt_for_number(&mouse_used);
7250 if (mouse_used)
7251 selected -= lines_left;
7252
7253 rettv->vval.v_number = selected;
7254}
7255
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007256static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7257
7258/*
7259 * "inputrestore()" function
7260 */
7261 static void
7262f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7263{
7264 if (ga_userinput.ga_len > 0)
7265 {
7266 --ga_userinput.ga_len;
7267 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7268 + ga_userinput.ga_len);
7269 /* default return is zero == OK */
7270 }
7271 else if (p_verbose > 1)
7272 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007273 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007274 rettv->vval.v_number = 1; /* Failed */
7275 }
7276}
7277
7278/*
7279 * "inputsave()" function
7280 */
7281 static void
7282f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7283{
7284 /* Add an entry to the stack of typeahead storage. */
7285 if (ga_grow(&ga_userinput, 1) == OK)
7286 {
7287 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7288 + ga_userinput.ga_len);
7289 ++ga_userinput.ga_len;
7290 /* default return is zero == OK */
7291 }
7292 else
7293 rettv->vval.v_number = 1; /* Failed */
7294}
7295
7296/*
7297 * "inputsecret()" function
7298 */
7299 static void
7300f_inputsecret(typval_T *argvars, typval_T *rettv)
7301{
7302 ++cmdline_star;
7303 ++inputsecret_flag;
7304 f_input(argvars, rettv);
7305 --cmdline_star;
7306 --inputsecret_flag;
7307}
7308
7309/*
7310 * "insert()" function
7311 */
7312 static void
7313f_insert(typval_T *argvars, typval_T *rettv)
7314{
7315 long before = 0;
7316 listitem_T *item;
7317 list_T *l;
7318 int error = FALSE;
7319
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007320 if (argvars[0].v_type == VAR_BLOB)
7321 {
7322 int val, len;
7323 char_u *p;
7324
7325 len = blob_len(argvars[0].vval.v_blob);
7326 if (argvars[2].v_type != VAR_UNKNOWN)
7327 {
7328 before = (long)tv_get_number_chk(&argvars[2], &error);
7329 if (error)
7330 return; // type error; errmsg already given
7331 if (before < 0 || before > len)
7332 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007333 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007334 return;
7335 }
7336 }
7337 val = tv_get_number_chk(&argvars[1], &error);
7338 if (error)
7339 return;
7340 if (val < 0 || val > 255)
7341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007342 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007343 return;
7344 }
7345
7346 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7347 return;
7348 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7349 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7350 *(p + before) = val;
7351 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7352
7353 copy_tv(&argvars[0], rettv);
7354 }
7355 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007356 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007357 else if ((l = argvars[0].vval.v_list) != NULL
7358 && !var_check_lock(l->lv_lock,
7359 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007360 {
7361 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007362 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007363 if (error)
7364 return; /* type error; errmsg already given */
7365
7366 if (before == l->lv_len)
7367 item = NULL;
7368 else
7369 {
7370 item = list_find(l, before);
7371 if (item == NULL)
7372 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007373 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007374 l = NULL;
7375 }
7376 }
7377 if (l != NULL)
7378 {
7379 list_insert_tv(l, &argvars[1], item);
7380 copy_tv(&argvars[0], rettv);
7381 }
7382 }
7383}
7384
7385/*
7386 * "invert(expr)" function
7387 */
7388 static void
7389f_invert(typval_T *argvars, typval_T *rettv)
7390{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007391 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007392}
7393
7394/*
7395 * "isdirectory()" function
7396 */
7397 static void
7398f_isdirectory(typval_T *argvars, typval_T *rettv)
7399{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007400 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007401}
7402
7403/*
7404 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7405 * or it refers to a List or Dictionary that is locked.
7406 */
7407 static int
7408tv_islocked(typval_T *tv)
7409{
7410 return (tv->v_lock & VAR_LOCKED)
7411 || (tv->v_type == VAR_LIST
7412 && tv->vval.v_list != NULL
7413 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7414 || (tv->v_type == VAR_DICT
7415 && tv->vval.v_dict != NULL
7416 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7417}
7418
7419/*
7420 * "islocked()" function
7421 */
7422 static void
7423f_islocked(typval_T *argvars, typval_T *rettv)
7424{
7425 lval_T lv;
7426 char_u *end;
7427 dictitem_T *di;
7428
7429 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007430 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007431 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007432 if (end != NULL && lv.ll_name != NULL)
7433 {
7434 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007435 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436 else
7437 {
7438 if (lv.ll_tv == NULL)
7439 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007440 di = find_var(lv.ll_name, NULL, TRUE);
7441 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007442 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007443 /* Consider a variable locked when:
7444 * 1. the variable itself is locked
7445 * 2. the value of the variable is locked.
7446 * 3. the List or Dict value is locked.
7447 */
7448 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7449 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007450 }
7451 }
7452 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007453 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007454 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007455 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007456 else if (lv.ll_list != NULL)
7457 /* List item. */
7458 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7459 else
7460 /* Dictionary item. */
7461 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7462 }
7463 }
7464
7465 clear_lval(&lv);
7466}
7467
7468#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7469/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007470 * "isinf()" function
7471 */
7472 static void
7473f_isinf(typval_T *argvars, typval_T *rettv)
7474{
7475 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7476 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7477}
7478
7479/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007480 * "isnan()" function
7481 */
7482 static void
7483f_isnan(typval_T *argvars, typval_T *rettv)
7484{
7485 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7486 && isnan(argvars[0].vval.v_float);
7487}
7488#endif
7489
7490/*
7491 * "items(dict)" function
7492 */
7493 static void
7494f_items(typval_T *argvars, typval_T *rettv)
7495{
7496 dict_list(argvars, rettv, 2);
7497}
7498
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007499/*
7500 * "join()" function
7501 */
7502 static void
7503f_join(typval_T *argvars, typval_T *rettv)
7504{
7505 garray_T ga;
7506 char_u *sep;
7507
7508 if (argvars[0].v_type != VAR_LIST)
7509 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007510 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007511 return;
7512 }
7513 if (argvars[0].vval.v_list == NULL)
7514 return;
7515 if (argvars[1].v_type == VAR_UNKNOWN)
7516 sep = (char_u *)" ";
7517 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007518 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007519
7520 rettv->v_type = VAR_STRING;
7521
7522 if (sep != NULL)
7523 {
7524 ga_init2(&ga, (int)sizeof(char), 80);
7525 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7526 ga_append(&ga, NUL);
7527 rettv->vval.v_string = (char_u *)ga.ga_data;
7528 }
7529 else
7530 rettv->vval.v_string = NULL;
7531}
7532
7533/*
7534 * "js_decode()" function
7535 */
7536 static void
7537f_js_decode(typval_T *argvars, typval_T *rettv)
7538{
7539 js_read_T reader;
7540
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007541 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007542 reader.js_fill = NULL;
7543 reader.js_used = 0;
7544 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007545 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007546}
7547
7548/*
7549 * "js_encode()" function
7550 */
7551 static void
7552f_js_encode(typval_T *argvars, typval_T *rettv)
7553{
7554 rettv->v_type = VAR_STRING;
7555 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7556}
7557
7558/*
7559 * "json_decode()" function
7560 */
7561 static void
7562f_json_decode(typval_T *argvars, typval_T *rettv)
7563{
7564 js_read_T reader;
7565
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007566 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007567 reader.js_fill = NULL;
7568 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007569 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007570}
7571
7572/*
7573 * "json_encode()" function
7574 */
7575 static void
7576f_json_encode(typval_T *argvars, typval_T *rettv)
7577{
7578 rettv->v_type = VAR_STRING;
7579 rettv->vval.v_string = json_encode(&argvars[0], 0);
7580}
7581
7582/*
7583 * "keys()" function
7584 */
7585 static void
7586f_keys(typval_T *argvars, typval_T *rettv)
7587{
7588 dict_list(argvars, rettv, 0);
7589}
7590
7591/*
7592 * "last_buffer_nr()" function.
7593 */
7594 static void
7595f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7596{
7597 int n = 0;
7598 buf_T *buf;
7599
Bram Moolenaar29323592016-07-24 22:04:11 +02007600 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007601 if (n < buf->b_fnum)
7602 n = buf->b_fnum;
7603
7604 rettv->vval.v_number = n;
7605}
7606
7607/*
7608 * "len()" function
7609 */
7610 static void
7611f_len(typval_T *argvars, typval_T *rettv)
7612{
7613 switch (argvars[0].v_type)
7614 {
7615 case VAR_STRING:
7616 case VAR_NUMBER:
7617 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007618 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007619 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007620 case VAR_BLOB:
7621 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7622 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007623 case VAR_LIST:
7624 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7625 break;
7626 case VAR_DICT:
7627 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7628 break;
7629 case VAR_UNKNOWN:
7630 case VAR_SPECIAL:
7631 case VAR_FLOAT:
7632 case VAR_FUNC:
7633 case VAR_PARTIAL:
7634 case VAR_JOB:
7635 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007636 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007637 break;
7638 }
7639}
7640
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007641 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007642libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007643{
7644#ifdef FEAT_LIBCALL
7645 char_u *string_in;
7646 char_u **string_result;
7647 int nr_result;
7648#endif
7649
7650 rettv->v_type = type;
7651 if (type != VAR_NUMBER)
7652 rettv->vval.v_string = NULL;
7653
7654 if (check_restricted() || check_secure())
7655 return;
7656
7657#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007658 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007659 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7660 {
7661 string_in = NULL;
7662 if (argvars[2].v_type == VAR_STRING)
7663 string_in = argvars[2].vval.v_string;
7664 if (type == VAR_NUMBER)
7665 string_result = NULL;
7666 else
7667 string_result = &rettv->vval.v_string;
7668 if (mch_libcall(argvars[0].vval.v_string,
7669 argvars[1].vval.v_string,
7670 string_in,
7671 argvars[2].vval.v_number,
7672 string_result,
7673 &nr_result) == OK
7674 && type == VAR_NUMBER)
7675 rettv->vval.v_number = nr_result;
7676 }
7677#endif
7678}
7679
7680/*
7681 * "libcall()" function
7682 */
7683 static void
7684f_libcall(typval_T *argvars, typval_T *rettv)
7685{
7686 libcall_common(argvars, rettv, VAR_STRING);
7687}
7688
7689/*
7690 * "libcallnr()" function
7691 */
7692 static void
7693f_libcallnr(typval_T *argvars, typval_T *rettv)
7694{
7695 libcall_common(argvars, rettv, VAR_NUMBER);
7696}
7697
7698/*
7699 * "line(string)" function
7700 */
7701 static void
7702f_line(typval_T *argvars, typval_T *rettv)
7703{
7704 linenr_T lnum = 0;
7705 pos_T *fp;
7706 int fnum;
7707
7708 fp = var2fpos(&argvars[0], TRUE, &fnum);
7709 if (fp != NULL)
7710 lnum = fp->lnum;
7711 rettv->vval.v_number = lnum;
7712}
7713
7714/*
7715 * "line2byte(lnum)" function
7716 */
7717 static void
7718f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7719{
7720#ifndef FEAT_BYTEOFF
7721 rettv->vval.v_number = -1;
7722#else
7723 linenr_T lnum;
7724
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007725 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007726 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7727 rettv->vval.v_number = -1;
7728 else
7729 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7730 if (rettv->vval.v_number >= 0)
7731 ++rettv->vval.v_number;
7732#endif
7733}
7734
7735/*
7736 * "lispindent(lnum)" function
7737 */
7738 static void
7739f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7740{
7741#ifdef FEAT_LISP
7742 pos_T pos;
7743 linenr_T lnum;
7744
7745 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007746 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007747 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7748 {
7749 curwin->w_cursor.lnum = lnum;
7750 rettv->vval.v_number = get_lisp_indent();
7751 curwin->w_cursor = pos;
7752 }
7753 else
7754#endif
7755 rettv->vval.v_number = -1;
7756}
7757
7758/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007759 * "list2str()" function
7760 */
7761 static void
7762f_list2str(typval_T *argvars, typval_T *rettv)
7763{
7764 list_T *l;
7765 listitem_T *li;
7766 garray_T ga;
7767 int utf8 = FALSE;
7768
7769 rettv->v_type = VAR_STRING;
7770 rettv->vval.v_string = NULL;
7771 if (argvars[0].v_type != VAR_LIST)
7772 {
7773 emsg(_(e_invarg));
7774 return;
7775 }
7776
7777 l = argvars[0].vval.v_list;
7778 if (l == NULL)
7779 return; // empty list results in empty string
7780
7781 if (argvars[1].v_type != VAR_UNKNOWN)
7782 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7783
7784 ga_init2(&ga, 1, 80);
7785 if (has_mbyte || utf8)
7786 {
7787 char_u buf[MB_MAXBYTES + 1];
7788 int (*char2bytes)(int, char_u *);
7789
7790 if (utf8 || enc_utf8)
7791 char2bytes = utf_char2bytes;
7792 else
7793 char2bytes = mb_char2bytes;
7794
7795 for (li = l->lv_first; li != NULL; li = li->li_next)
7796 {
7797 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7798 ga_concat(&ga, buf);
7799 }
7800 ga_append(&ga, NUL);
7801 }
7802 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7803 {
7804 for (li = l->lv_first; li != NULL; li = li->li_next)
7805 ga_append(&ga, tv_get_number(&li->li_tv));
7806 ga_append(&ga, NUL);
7807 }
7808
7809 rettv->v_type = VAR_STRING;
7810 rettv->vval.v_string = ga.ga_data;
7811}
7812
7813/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007814 * "localtime()" function
7815 */
7816 static void
7817f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7818{
7819 rettv->vval.v_number = (varnumber_T)time(NULL);
7820}
7821
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007822 static void
7823get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7824{
7825 char_u *keys;
7826 char_u *which;
7827 char_u buf[NUMBUFLEN];
7828 char_u *keys_buf = NULL;
7829 char_u *rhs;
7830 int mode;
7831 int abbr = FALSE;
7832 int get_dict = FALSE;
7833 mapblock_T *mp;
7834 int buffer_local;
7835
7836 /* return empty string for failure */
7837 rettv->v_type = VAR_STRING;
7838 rettv->vval.v_string = NULL;
7839
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007840 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007841 if (*keys == NUL)
7842 return;
7843
7844 if (argvars[1].v_type != VAR_UNKNOWN)
7845 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007846 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007847 if (argvars[2].v_type != VAR_UNKNOWN)
7848 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007849 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007850 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007851 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007852 }
7853 }
7854 else
7855 which = (char_u *)"";
7856 if (which == NULL)
7857 return;
7858
7859 mode = get_map_mode(&which, 0);
7860
7861 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7862 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7863 vim_free(keys_buf);
7864
7865 if (!get_dict)
7866 {
7867 /* Return a string. */
7868 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007869 {
7870 if (*rhs == NUL)
7871 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7872 else
7873 rettv->vval.v_string = str2special_save(rhs, FALSE);
7874 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007875
7876 }
7877 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7878 {
7879 /* Return a dictionary. */
7880 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7881 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7882 dict_T *dict = rettv->vval.v_dict;
7883
Bram Moolenaare0be1672018-07-08 16:50:37 +02007884 dict_add_string(dict, "lhs", lhs);
7885 dict_add_string(dict, "rhs", mp->m_orig_str);
7886 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7887 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7888 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007889 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7890 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007891 dict_add_number(dict, "buffer", (long)buffer_local);
7892 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7893 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894
7895 vim_free(lhs);
7896 vim_free(mapmode);
7897 }
7898}
7899
7900#ifdef FEAT_FLOAT
7901/*
7902 * "log()" function
7903 */
7904 static void
7905f_log(typval_T *argvars, typval_T *rettv)
7906{
7907 float_T f = 0.0;
7908
7909 rettv->v_type = VAR_FLOAT;
7910 if (get_float_arg(argvars, &f) == OK)
7911 rettv->vval.v_float = log(f);
7912 else
7913 rettv->vval.v_float = 0.0;
7914}
7915
7916/*
7917 * "log10()" function
7918 */
7919 static void
7920f_log10(typval_T *argvars, typval_T *rettv)
7921{
7922 float_T f = 0.0;
7923
7924 rettv->v_type = VAR_FLOAT;
7925 if (get_float_arg(argvars, &f) == OK)
7926 rettv->vval.v_float = log10(f);
7927 else
7928 rettv->vval.v_float = 0.0;
7929}
7930#endif
7931
7932#ifdef FEAT_LUA
7933/*
7934 * "luaeval()" function
7935 */
7936 static void
7937f_luaeval(typval_T *argvars, typval_T *rettv)
7938{
7939 char_u *str;
7940 char_u buf[NUMBUFLEN];
7941
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007942 if (check_restricted() || check_secure())
7943 return;
7944
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007945 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007946 do_luaeval(str, argvars + 1, rettv);
7947}
7948#endif
7949
7950/*
7951 * "map()" function
7952 */
7953 static void
7954f_map(typval_T *argvars, typval_T *rettv)
7955{
7956 filter_map(argvars, rettv, TRUE);
7957}
7958
7959/*
7960 * "maparg()" function
7961 */
7962 static void
7963f_maparg(typval_T *argvars, typval_T *rettv)
7964{
7965 get_maparg(argvars, rettv, TRUE);
7966}
7967
7968/*
7969 * "mapcheck()" function
7970 */
7971 static void
7972f_mapcheck(typval_T *argvars, typval_T *rettv)
7973{
7974 get_maparg(argvars, rettv, FALSE);
7975}
7976
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007977typedef enum
7978{
7979 MATCH_END, /* matchend() */
7980 MATCH_MATCH, /* match() */
7981 MATCH_STR, /* matchstr() */
7982 MATCH_LIST, /* matchlist() */
7983 MATCH_POS /* matchstrpos() */
7984} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007985
7986 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007987find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007988{
7989 char_u *str = NULL;
7990 long len = 0;
7991 char_u *expr = NULL;
7992 char_u *pat;
7993 regmatch_T regmatch;
7994 char_u patbuf[NUMBUFLEN];
7995 char_u strbuf[NUMBUFLEN];
7996 char_u *save_cpo;
7997 long start = 0;
7998 long nth = 1;
7999 colnr_T startcol = 0;
8000 int match = 0;
8001 list_T *l = NULL;
8002 listitem_T *li = NULL;
8003 long idx = 0;
8004 char_u *tofree = NULL;
8005
8006 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8007 save_cpo = p_cpo;
8008 p_cpo = (char_u *)"";
8009
8010 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008011 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008012 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008013 /* type MATCH_LIST: return empty list when there are no matches.
8014 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008015 if (rettv_list_alloc(rettv) == FAIL)
8016 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008017 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008018 && (list_append_string(rettv->vval.v_list,
8019 (char_u *)"", 0) == FAIL
8020 || list_append_number(rettv->vval.v_list,
8021 (varnumber_T)-1) == FAIL
8022 || list_append_number(rettv->vval.v_list,
8023 (varnumber_T)-1) == FAIL
8024 || list_append_number(rettv->vval.v_list,
8025 (varnumber_T)-1) == FAIL))
8026 {
8027 list_free(rettv->vval.v_list);
8028 rettv->vval.v_list = NULL;
8029 goto theend;
8030 }
8031 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008032 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008033 {
8034 rettv->v_type = VAR_STRING;
8035 rettv->vval.v_string = NULL;
8036 }
8037
8038 if (argvars[0].v_type == VAR_LIST)
8039 {
8040 if ((l = argvars[0].vval.v_list) == NULL)
8041 goto theend;
8042 li = l->lv_first;
8043 }
8044 else
8045 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008046 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008047 len = (long)STRLEN(str);
8048 }
8049
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008050 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008051 if (pat == NULL)
8052 goto theend;
8053
8054 if (argvars[2].v_type != VAR_UNKNOWN)
8055 {
8056 int error = FALSE;
8057
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008058 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008059 if (error)
8060 goto theend;
8061 if (l != NULL)
8062 {
8063 li = list_find(l, start);
8064 if (li == NULL)
8065 goto theend;
8066 idx = l->lv_idx; /* use the cached index */
8067 }
8068 else
8069 {
8070 if (start < 0)
8071 start = 0;
8072 if (start > len)
8073 goto theend;
8074 /* When "count" argument is there ignore matches before "start",
8075 * otherwise skip part of the string. Differs when pattern is "^"
8076 * or "\<". */
8077 if (argvars[3].v_type != VAR_UNKNOWN)
8078 startcol = start;
8079 else
8080 {
8081 str += start;
8082 len -= start;
8083 }
8084 }
8085
8086 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008087 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088 if (error)
8089 goto theend;
8090 }
8091
8092 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8093 if (regmatch.regprog != NULL)
8094 {
8095 regmatch.rm_ic = p_ic;
8096
8097 for (;;)
8098 {
8099 if (l != NULL)
8100 {
8101 if (li == NULL)
8102 {
8103 match = FALSE;
8104 break;
8105 }
8106 vim_free(tofree);
8107 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8108 if (str == NULL)
8109 break;
8110 }
8111
8112 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8113
8114 if (match && --nth <= 0)
8115 break;
8116 if (l == NULL && !match)
8117 break;
8118
8119 /* Advance to just after the match. */
8120 if (l != NULL)
8121 {
8122 li = li->li_next;
8123 ++idx;
8124 }
8125 else
8126 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008127 startcol = (colnr_T)(regmatch.startp[0]
8128 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008129 if (startcol > (colnr_T)len
8130 || str + startcol <= regmatch.startp[0])
8131 {
8132 match = FALSE;
8133 break;
8134 }
8135 }
8136 }
8137
8138 if (match)
8139 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008140 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008141 {
8142 listitem_T *li1 = rettv->vval.v_list->lv_first;
8143 listitem_T *li2 = li1->li_next;
8144 listitem_T *li3 = li2->li_next;
8145 listitem_T *li4 = li3->li_next;
8146
8147 vim_free(li1->li_tv.vval.v_string);
8148 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8149 (int)(regmatch.endp[0] - regmatch.startp[0]));
8150 li3->li_tv.vval.v_number =
8151 (varnumber_T)(regmatch.startp[0] - expr);
8152 li4->li_tv.vval.v_number =
8153 (varnumber_T)(regmatch.endp[0] - expr);
8154 if (l != NULL)
8155 li2->li_tv.vval.v_number = (varnumber_T)idx;
8156 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008157 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158 {
8159 int i;
8160
8161 /* return list with matched string and submatches */
8162 for (i = 0; i < NSUBEXP; ++i)
8163 {
8164 if (regmatch.endp[i] == NULL)
8165 {
8166 if (list_append_string(rettv->vval.v_list,
8167 (char_u *)"", 0) == FAIL)
8168 break;
8169 }
8170 else if (list_append_string(rettv->vval.v_list,
8171 regmatch.startp[i],
8172 (int)(regmatch.endp[i] - regmatch.startp[i]))
8173 == FAIL)
8174 break;
8175 }
8176 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008177 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008178 {
8179 /* return matched string */
8180 if (l != NULL)
8181 copy_tv(&li->li_tv, rettv);
8182 else
8183 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8184 (int)(regmatch.endp[0] - regmatch.startp[0]));
8185 }
8186 else if (l != NULL)
8187 rettv->vval.v_number = idx;
8188 else
8189 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008190 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008191 rettv->vval.v_number =
8192 (varnumber_T)(regmatch.startp[0] - str);
8193 else
8194 rettv->vval.v_number =
8195 (varnumber_T)(regmatch.endp[0] - str);
8196 rettv->vval.v_number += (varnumber_T)(str - expr);
8197 }
8198 }
8199 vim_regfree(regmatch.regprog);
8200 }
8201
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008202theend:
8203 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008204 /* matchstrpos() without a list: drop the second item. */
8205 listitem_remove(rettv->vval.v_list,
8206 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207 vim_free(tofree);
8208 p_cpo = save_cpo;
8209}
8210
8211/*
8212 * "match()" function
8213 */
8214 static void
8215f_match(typval_T *argvars, typval_T *rettv)
8216{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008217 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008218}
8219
Bram Moolenaar95e51472018-07-28 16:55:56 +02008220#ifdef FEAT_SEARCH_EXTRA
8221 static int
8222matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8223{
8224 dictitem_T *di;
8225
8226 if (tv->v_type != VAR_DICT)
8227 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008228 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008229 return FAIL;
8230 }
8231
8232 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008233 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008234 (char_u *)"conceal", FALSE);
8235
8236 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8237 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008238 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008239 if (*win == NULL)
8240 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008241 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008242 return FAIL;
8243 }
8244 }
8245
8246 return OK;
8247}
8248#endif
8249
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008250/*
8251 * "matchadd()" function
8252 */
8253 static void
8254f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8255{
8256#ifdef FEAT_SEARCH_EXTRA
8257 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008258 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8259 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008260 int prio = 10; /* default priority */
8261 int id = -1;
8262 int error = FALSE;
8263 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008264 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008265
8266 rettv->vval.v_number = -1;
8267
8268 if (grp == NULL || pat == NULL)
8269 return;
8270 if (argvars[2].v_type != VAR_UNKNOWN)
8271 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008272 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008273 if (argvars[3].v_type != VAR_UNKNOWN)
8274 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008275 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008276 if (argvars[4].v_type != VAR_UNKNOWN
8277 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8278 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008279 }
8280 }
8281 if (error == TRUE)
8282 return;
8283 if (id >= 1 && id <= 3)
8284 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008285 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008286 return;
8287 }
8288
Bram Moolenaar95e51472018-07-28 16:55:56 +02008289 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008290 conceal_char);
8291#endif
8292}
8293
8294/*
8295 * "matchaddpos()" function
8296 */
8297 static void
8298f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8299{
8300#ifdef FEAT_SEARCH_EXTRA
8301 char_u buf[NUMBUFLEN];
8302 char_u *group;
8303 int prio = 10;
8304 int id = -1;
8305 int error = FALSE;
8306 list_T *l;
8307 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008308 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309
8310 rettv->vval.v_number = -1;
8311
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008312 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008313 if (group == NULL)
8314 return;
8315
8316 if (argvars[1].v_type != VAR_LIST)
8317 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008318 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008319 return;
8320 }
8321 l = argvars[1].vval.v_list;
8322 if (l == NULL)
8323 return;
8324
8325 if (argvars[2].v_type != VAR_UNKNOWN)
8326 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008327 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 if (argvars[3].v_type != VAR_UNKNOWN)
8329 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008330 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008331
8332 if (argvars[4].v_type != VAR_UNKNOWN
8333 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8334 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008335 }
8336 }
8337 if (error == TRUE)
8338 return;
8339
8340 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8341 if (id == 1 || id == 2)
8342 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008343 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008344 return;
8345 }
8346
Bram Moolenaar95e51472018-07-28 16:55:56 +02008347 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008348 conceal_char);
8349#endif
8350}
8351
8352/*
8353 * "matcharg()" function
8354 */
8355 static void
8356f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8357{
8358 if (rettv_list_alloc(rettv) == OK)
8359 {
8360#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008361 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 matchitem_T *m;
8363
8364 if (id >= 1 && id <= 3)
8365 {
8366 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8367 {
8368 list_append_string(rettv->vval.v_list,
8369 syn_id2name(m->hlg_id), -1);
8370 list_append_string(rettv->vval.v_list, m->pattern, -1);
8371 }
8372 else
8373 {
8374 list_append_string(rettv->vval.v_list, NULL, -1);
8375 list_append_string(rettv->vval.v_list, NULL, -1);
8376 }
8377 }
8378#endif
8379 }
8380}
8381
8382/*
8383 * "matchdelete()" function
8384 */
8385 static void
8386f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8387{
8388#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008389 win_T *win = get_optional_window(argvars, 1);
8390
8391 if (win == NULL)
8392 rettv->vval.v_number = -1;
8393 else
8394 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008395 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008396#endif
8397}
8398
8399/*
8400 * "matchend()" function
8401 */
8402 static void
8403f_matchend(typval_T *argvars, typval_T *rettv)
8404{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008405 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406}
8407
8408/*
8409 * "matchlist()" function
8410 */
8411 static void
8412f_matchlist(typval_T *argvars, typval_T *rettv)
8413{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008414 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008415}
8416
8417/*
8418 * "matchstr()" function
8419 */
8420 static void
8421f_matchstr(typval_T *argvars, typval_T *rettv)
8422{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008423 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008424}
8425
8426/*
8427 * "matchstrpos()" function
8428 */
8429 static void
8430f_matchstrpos(typval_T *argvars, typval_T *rettv)
8431{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008432 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008433}
8434
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008435 static void
8436max_min(typval_T *argvars, typval_T *rettv, int domax)
8437{
8438 varnumber_T n = 0;
8439 varnumber_T i;
8440 int error = FALSE;
8441
8442 if (argvars[0].v_type == VAR_LIST)
8443 {
8444 list_T *l;
8445 listitem_T *li;
8446
8447 l = argvars[0].vval.v_list;
8448 if (l != NULL)
8449 {
8450 li = l->lv_first;
8451 if (li != NULL)
8452 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008453 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008454 for (;;)
8455 {
8456 li = li->li_next;
8457 if (li == NULL)
8458 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008459 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008460 if (domax ? i > n : i < n)
8461 n = i;
8462 }
8463 }
8464 }
8465 }
8466 else if (argvars[0].v_type == VAR_DICT)
8467 {
8468 dict_T *d;
8469 int first = TRUE;
8470 hashitem_T *hi;
8471 int todo;
8472
8473 d = argvars[0].vval.v_dict;
8474 if (d != NULL)
8475 {
8476 todo = (int)d->dv_hashtab.ht_used;
8477 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8478 {
8479 if (!HASHITEM_EMPTY(hi))
8480 {
8481 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008482 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008483 if (first)
8484 {
8485 n = i;
8486 first = FALSE;
8487 }
8488 else if (domax ? i > n : i < n)
8489 n = i;
8490 }
8491 }
8492 }
8493 }
8494 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008495 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008496 rettv->vval.v_number = error ? 0 : n;
8497}
8498
8499/*
8500 * "max()" function
8501 */
8502 static void
8503f_max(typval_T *argvars, typval_T *rettv)
8504{
8505 max_min(argvars, rettv, TRUE);
8506}
8507
8508/*
8509 * "min()" function
8510 */
8511 static void
8512f_min(typval_T *argvars, typval_T *rettv)
8513{
8514 max_min(argvars, rettv, FALSE);
8515}
8516
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008517/*
8518 * Create the directory in which "dir" is located, and higher levels when
8519 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008520 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008521 */
8522 static int
8523mkdir_recurse(char_u *dir, int prot)
8524{
8525 char_u *p;
8526 char_u *updir;
8527 int r = FAIL;
8528
8529 /* Get end of directory name in "dir".
8530 * We're done when it's "/" or "c:/". */
8531 p = gettail_sep(dir);
8532 if (p <= get_past_head(dir))
8533 return OK;
8534
8535 /* If the directory exists we're done. Otherwise: create it.*/
8536 updir = vim_strnsave(dir, (int)(p - dir));
8537 if (updir == NULL)
8538 return FAIL;
8539 if (mch_isdir(updir))
8540 r = OK;
8541 else if (mkdir_recurse(updir, prot) == OK)
8542 r = vim_mkdir_emsg(updir, prot);
8543 vim_free(updir);
8544 return r;
8545}
8546
8547#ifdef vim_mkdir
8548/*
8549 * "mkdir()" function
8550 */
8551 static void
8552f_mkdir(typval_T *argvars, typval_T *rettv)
8553{
8554 char_u *dir;
8555 char_u buf[NUMBUFLEN];
8556 int prot = 0755;
8557
8558 rettv->vval.v_number = FAIL;
8559 if (check_restricted() || check_secure())
8560 return;
8561
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008562 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008564 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008565
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008566 if (*gettail(dir) == NUL)
8567 /* remove trailing slashes */
8568 *gettail_sep(dir) = NUL;
8569
8570 if (argvars[1].v_type != VAR_UNKNOWN)
8571 {
8572 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008573 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008574 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008575 if (prot == -1)
8576 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008578 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008579 {
8580 if (mch_isdir(dir))
8581 {
8582 /* With the "p" flag it's OK if the dir already exists. */
8583 rettv->vval.v_number = OK;
8584 return;
8585 }
8586 mkdir_recurse(dir, prot);
8587 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008588 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008589 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008590}
8591#endif
8592
8593/*
8594 * "mode()" function
8595 */
8596 static void
8597f_mode(typval_T *argvars, typval_T *rettv)
8598{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008599 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008600
Bram Moolenaar612cc382018-07-29 15:34:26 +02008601 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602
8603 if (time_for_testing == 93784)
8604 {
8605 /* Testing the two-character code. */
8606 buf[0] = 'x';
8607 buf[1] = '!';
8608 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008609#ifdef FEAT_TERMINAL
8610 else if (term_use_loop())
8611 buf[0] = 't';
8612#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008613 else if (VIsual_active)
8614 {
8615 if (VIsual_select)
8616 buf[0] = VIsual_mode + 's' - 'v';
8617 else
8618 buf[0] = VIsual_mode;
8619 }
8620 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8621 || State == CONFIRM)
8622 {
8623 buf[0] = 'r';
8624 if (State == ASKMORE)
8625 buf[1] = 'm';
8626 else if (State == CONFIRM)
8627 buf[1] = '?';
8628 }
8629 else if (State == EXTERNCMD)
8630 buf[0] = '!';
8631 else if (State & INSERT)
8632 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008633 if (State & VREPLACE_FLAG)
8634 {
8635 buf[0] = 'R';
8636 buf[1] = 'v';
8637 }
8638 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008639 {
8640 if (State & REPLACE_FLAG)
8641 buf[0] = 'R';
8642 else
8643 buf[0] = 'i';
8644#ifdef FEAT_INS_EXPAND
8645 if (ins_compl_active())
8646 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008647 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008648 buf[1] = 'x';
8649#endif
8650 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008652 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008653 {
8654 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008655 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008657 else if (exmode_active == EXMODE_NORMAL)
8658 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659 }
8660 else
8661 {
8662 buf[0] = 'n';
8663 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008664 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008665 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008666 // to be able to detect force-linewise/blockwise/characterwise operations
8667 buf[2] = motion_force;
8668 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008669 else if (restart_edit == 'I' || restart_edit == 'R'
8670 || restart_edit == 'V')
8671 {
8672 buf[1] = 'i';
8673 buf[2] = restart_edit;
8674 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008675 }
8676
8677 /* Clear out the minor mode when the argument is not a non-zero number or
8678 * non-empty string. */
8679 if (!non_zero_arg(&argvars[0]))
8680 buf[1] = NUL;
8681
8682 rettv->vval.v_string = vim_strsave(buf);
8683 rettv->v_type = VAR_STRING;
8684}
8685
8686#if defined(FEAT_MZSCHEME) || defined(PROTO)
8687/*
8688 * "mzeval()" function
8689 */
8690 static void
8691f_mzeval(typval_T *argvars, typval_T *rettv)
8692{
8693 char_u *str;
8694 char_u buf[NUMBUFLEN];
8695
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008696 if (check_restricted() || check_secure())
8697 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008698 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699 do_mzeval(str, rettv);
8700}
8701
8702 void
8703mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8704{
8705 typval_T argvars[3];
8706
8707 argvars[0].v_type = VAR_STRING;
8708 argvars[0].vval.v_string = name;
8709 copy_tv(args, &argvars[1]);
8710 argvars[2].v_type = VAR_UNKNOWN;
8711 f_call(argvars, rettv);
8712 clear_tv(&argvars[1]);
8713}
8714#endif
8715
8716/*
8717 * "nextnonblank()" function
8718 */
8719 static void
8720f_nextnonblank(typval_T *argvars, typval_T *rettv)
8721{
8722 linenr_T lnum;
8723
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008724 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725 {
8726 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8727 {
8728 lnum = 0;
8729 break;
8730 }
8731 if (*skipwhite(ml_get(lnum)) != NUL)
8732 break;
8733 }
8734 rettv->vval.v_number = lnum;
8735}
8736
8737/*
8738 * "nr2char()" function
8739 */
8740 static void
8741f_nr2char(typval_T *argvars, typval_T *rettv)
8742{
8743 char_u buf[NUMBUFLEN];
8744
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745 if (has_mbyte)
8746 {
8747 int utf8 = 0;
8748
8749 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008750 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008752 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008754 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755 }
8756 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008758 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008759 buf[1] = NUL;
8760 }
8761 rettv->v_type = VAR_STRING;
8762 rettv->vval.v_string = vim_strsave(buf);
8763}
8764
8765/*
8766 * "or(expr, expr)" function
8767 */
8768 static void
8769f_or(typval_T *argvars, typval_T *rettv)
8770{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008771 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8772 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773}
8774
8775/*
8776 * "pathshorten()" function
8777 */
8778 static void
8779f_pathshorten(typval_T *argvars, typval_T *rettv)
8780{
8781 char_u *p;
8782
8783 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008784 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008785 if (p == NULL)
8786 rettv->vval.v_string = NULL;
8787 else
8788 {
8789 p = vim_strsave(p);
8790 rettv->vval.v_string = p;
8791 if (p != NULL)
8792 shorten_dir(p);
8793 }
8794}
8795
8796#ifdef FEAT_PERL
8797/*
8798 * "perleval()" function
8799 */
8800 static void
8801f_perleval(typval_T *argvars, typval_T *rettv)
8802{
8803 char_u *str;
8804 char_u buf[NUMBUFLEN];
8805
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008806 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008807 do_perleval(str, rettv);
8808}
8809#endif
8810
8811#ifdef FEAT_FLOAT
8812/*
8813 * "pow()" function
8814 */
8815 static void
8816f_pow(typval_T *argvars, typval_T *rettv)
8817{
8818 float_T fx = 0.0, fy = 0.0;
8819
8820 rettv->v_type = VAR_FLOAT;
8821 if (get_float_arg(argvars, &fx) == OK
8822 && get_float_arg(&argvars[1], &fy) == OK)
8823 rettv->vval.v_float = pow(fx, fy);
8824 else
8825 rettv->vval.v_float = 0.0;
8826}
8827#endif
8828
8829/*
8830 * "prevnonblank()" function
8831 */
8832 static void
8833f_prevnonblank(typval_T *argvars, typval_T *rettv)
8834{
8835 linenr_T lnum;
8836
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008837 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008838 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8839 lnum = 0;
8840 else
8841 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8842 --lnum;
8843 rettv->vval.v_number = lnum;
8844}
8845
8846/* This dummy va_list is here because:
8847 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8848 * - locally in the function results in a "used before set" warning
8849 * - using va_start() to initialize it gives "function with fixed args" error */
8850static va_list ap;
8851
8852/*
8853 * "printf()" function
8854 */
8855 static void
8856f_printf(typval_T *argvars, typval_T *rettv)
8857{
8858 char_u buf[NUMBUFLEN];
8859 int len;
8860 char_u *s;
8861 int saved_did_emsg = did_emsg;
8862 char *fmt;
8863
8864 rettv->v_type = VAR_STRING;
8865 rettv->vval.v_string = NULL;
8866
8867 /* Get the required length, allocate the buffer and do it for real. */
8868 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008869 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008870 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008871 if (!did_emsg)
8872 {
8873 s = alloc(len + 1);
8874 if (s != NULL)
8875 {
8876 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008877 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8878 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008879 }
8880 }
8881 did_emsg |= saved_did_emsg;
8882}
8883
8884/*
8885 * "pumvisible()" function
8886 */
8887 static void
8888f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8889{
8890#ifdef FEAT_INS_EXPAND
8891 if (pum_visible())
8892 rettv->vval.v_number = 1;
8893#endif
8894}
8895
8896#ifdef FEAT_PYTHON3
8897/*
8898 * "py3eval()" function
8899 */
8900 static void
8901f_py3eval(typval_T *argvars, typval_T *rettv)
8902{
8903 char_u *str;
8904 char_u buf[NUMBUFLEN];
8905
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008906 if (check_restricted() || check_secure())
8907 return;
8908
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008909 if (p_pyx == 0)
8910 p_pyx = 3;
8911
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008912 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008913 do_py3eval(str, rettv);
8914}
8915#endif
8916
8917#ifdef FEAT_PYTHON
8918/*
8919 * "pyeval()" function
8920 */
8921 static void
8922f_pyeval(typval_T *argvars, typval_T *rettv)
8923{
8924 char_u *str;
8925 char_u buf[NUMBUFLEN];
8926
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008927 if (check_restricted() || check_secure())
8928 return;
8929
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008930 if (p_pyx == 0)
8931 p_pyx = 2;
8932
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008933 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008934 do_pyeval(str, rettv);
8935}
8936#endif
8937
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008938#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8939/*
8940 * "pyxeval()" function
8941 */
8942 static void
8943f_pyxeval(typval_T *argvars, typval_T *rettv)
8944{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008945 if (check_restricted() || check_secure())
8946 return;
8947
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008948# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8949 init_pyxversion();
8950 if (p_pyx == 2)
8951 f_pyeval(argvars, rettv);
8952 else
8953 f_py3eval(argvars, rettv);
8954# elif defined(FEAT_PYTHON)
8955 f_pyeval(argvars, rettv);
8956# elif defined(FEAT_PYTHON3)
8957 f_py3eval(argvars, rettv);
8958# endif
8959}
8960#endif
8961
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008962/*
8963 * "range()" function
8964 */
8965 static void
8966f_range(typval_T *argvars, typval_T *rettv)
8967{
8968 varnumber_T start;
8969 varnumber_T end;
8970 varnumber_T stride = 1;
8971 varnumber_T i;
8972 int error = FALSE;
8973
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008974 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008975 if (argvars[1].v_type == VAR_UNKNOWN)
8976 {
8977 end = start - 1;
8978 start = 0;
8979 }
8980 else
8981 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008982 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008983 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008984 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008985 }
8986
8987 if (error)
8988 return; /* type error; errmsg already given */
8989 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008990 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008991 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008992 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008993 else
8994 {
8995 if (rettv_list_alloc(rettv) == OK)
8996 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8997 if (list_append_number(rettv->vval.v_list,
8998 (varnumber_T)i) == FAIL)
8999 break;
9000 }
9001}
9002
9003/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009004 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009005 */
9006 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009007readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009008{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009009 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009010 typval_T save_val;
9011 typval_T rettv;
9012 typval_T argv[2];
9013 int retval = 0;
9014 int error = FALSE;
9015
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009016 if (expr->v_type == VAR_UNKNOWN)
9017 return 1;
9018
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009019 prepare_vimvar(VV_VAL, &save_val);
9020 set_vim_var_string(VV_VAL, name, -1);
9021 argv[0].v_type = VAR_STRING;
9022 argv[0].vval.v_string = name;
9023
9024 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9025 goto theend;
9026
9027 retval = tv_get_number_chk(&rettv, &error);
9028 if (error)
9029 retval = -1;
9030 clear_tv(&rettv);
9031
9032theend:
9033 set_vim_var_string(VV_VAL, NULL, 0);
9034 restore_vimvar(VV_VAL, &save_val);
9035 return retval;
9036}
9037
9038/*
9039 * "readdir()" function
9040 */
9041 static void
9042f_readdir(typval_T *argvars, typval_T *rettv)
9043{
9044 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009045 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009046 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009047 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009048 garray_T ga;
9049 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009050
9051 if (rettv_list_alloc(rettv) == FAIL)
9052 return;
9053 path = tv_get_string(&argvars[0]);
9054 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009055
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009056 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9057 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009058 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009059 for (i = 0; i < ga.ga_len; i++)
9060 {
9061 p = ((char_u **)ga.ga_data)[i];
9062 list_append_string(rettv->vval.v_list, p, -1);
9063 }
9064 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009065 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009066}
9067
9068/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009069 * "readfile()" function
9070 */
9071 static void
9072f_readfile(typval_T *argvars, typval_T *rettv)
9073{
9074 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009075 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009076 int failed = FALSE;
9077 char_u *fname;
9078 FILE *fd;
9079 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9080 int io_size = sizeof(buf);
9081 int readlen; /* size of last fread() */
9082 char_u *prev = NULL; /* previously read bytes, if any */
9083 long prevlen = 0; /* length of data in prev */
9084 long prevsize = 0; /* size of prev buffer */
9085 long maxline = MAXLNUM;
9086 long cnt = 0;
9087 char_u *p; /* position in buf */
9088 char_u *start; /* start of current line */
9089
9090 if (argvars[1].v_type != VAR_UNKNOWN)
9091 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009092 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009093 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009094 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9095 blob = TRUE;
9096
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009097 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009098 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009099 }
9100
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009101 if (blob)
9102 {
9103 if (rettv_blob_alloc(rettv) == FAIL)
9104 return;
9105 }
9106 else
9107 {
9108 if (rettv_list_alloc(rettv) == FAIL)
9109 return;
9110 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009111
9112 /* Always open the file in binary mode, library functions have a mind of
9113 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009114 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009115 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9116 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009117 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009118 return;
9119 }
9120
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009121 if (blob)
9122 {
9123 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9124 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009125 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009126 blob_free(rettv->vval.v_blob);
9127 }
9128 fclose(fd);
9129 return;
9130 }
9131
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009132 while (cnt < maxline || maxline < 0)
9133 {
9134 readlen = (int)fread(buf, 1, io_size, fd);
9135
9136 /* This for loop processes what was read, but is also entered at end
9137 * of file so that either:
9138 * - an incomplete line gets written
9139 * - a "binary" file gets an empty line at the end if it ends in a
9140 * newline. */
9141 for (p = buf, start = buf;
9142 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9143 ++p)
9144 {
9145 if (*p == '\n' || readlen <= 0)
9146 {
9147 listitem_T *li;
9148 char_u *s = NULL;
9149 long_u len = p - start;
9150
9151 /* Finished a line. Remove CRs before NL. */
9152 if (readlen > 0 && !binary)
9153 {
9154 while (len > 0 && start[len - 1] == '\r')
9155 --len;
9156 /* removal may cross back to the "prev" string */
9157 if (len == 0)
9158 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9159 --prevlen;
9160 }
9161 if (prevlen == 0)
9162 s = vim_strnsave(start, (int)len);
9163 else
9164 {
9165 /* Change "prev" buffer to be the right size. This way
9166 * the bytes are only copied once, and very long lines are
9167 * allocated only once. */
9168 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9169 {
9170 mch_memmove(s + prevlen, start, len);
9171 s[prevlen + len] = NUL;
9172 prev = NULL; /* the list will own the string */
9173 prevlen = prevsize = 0;
9174 }
9175 }
9176 if (s == NULL)
9177 {
9178 do_outofmem_msg((long_u) prevlen + len + 1);
9179 failed = TRUE;
9180 break;
9181 }
9182
9183 if ((li = listitem_alloc()) == NULL)
9184 {
9185 vim_free(s);
9186 failed = TRUE;
9187 break;
9188 }
9189 li->li_tv.v_type = VAR_STRING;
9190 li->li_tv.v_lock = 0;
9191 li->li_tv.vval.v_string = s;
9192 list_append(rettv->vval.v_list, li);
9193
9194 start = p + 1; /* step over newline */
9195 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9196 break;
9197 }
9198 else if (*p == NUL)
9199 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009200 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9201 * when finding the BF and check the previous two bytes. */
9202 else if (*p == 0xbf && enc_utf8 && !binary)
9203 {
9204 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9205 * + 1, these may be in the "prev" string. */
9206 char_u back1 = p >= buf + 1 ? p[-1]
9207 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9208 char_u back2 = p >= buf + 2 ? p[-2]
9209 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9210 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9211
9212 if (back2 == 0xef && back1 == 0xbb)
9213 {
9214 char_u *dest = p - 2;
9215
9216 /* Usually a BOM is at the beginning of a file, and so at
9217 * the beginning of a line; then we can just step over it.
9218 */
9219 if (start == dest)
9220 start = p + 1;
9221 else
9222 {
9223 /* have to shuffle buf to close gap */
9224 int adjust_prevlen = 0;
9225
9226 if (dest < buf)
9227 {
9228 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9229 dest = buf;
9230 }
9231 if (readlen > p - buf + 1)
9232 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9233 readlen -= 3 - adjust_prevlen;
9234 prevlen -= adjust_prevlen;
9235 p = dest - 1;
9236 }
9237 }
9238 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009239 } /* for */
9240
9241 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9242 break;
9243 if (start < p)
9244 {
9245 /* There's part of a line in buf, store it in "prev". */
9246 if (p - start + prevlen >= prevsize)
9247 {
9248 /* need bigger "prev" buffer */
9249 char_u *newprev;
9250
9251 /* A common use case is ordinary text files and "prev" gets a
9252 * fragment of a line, so the first allocation is made
9253 * small, to avoid repeatedly 'allocing' large and
9254 * 'reallocing' small. */
9255 if (prevsize == 0)
9256 prevsize = (long)(p - start);
9257 else
9258 {
9259 long grow50pc = (prevsize * 3) / 2;
9260 long growmin = (long)((p - start) * 2 + prevlen);
9261 prevsize = grow50pc > growmin ? grow50pc : growmin;
9262 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009263 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009264 if (newprev == NULL)
9265 {
9266 do_outofmem_msg((long_u)prevsize);
9267 failed = TRUE;
9268 break;
9269 }
9270 prev = newprev;
9271 }
9272 /* Add the line part to end of "prev". */
9273 mch_memmove(prev + prevlen, start, p - start);
9274 prevlen += (long)(p - start);
9275 }
9276 } /* while */
9277
9278 /*
9279 * For a negative line count use only the lines at the end of the file,
9280 * free the rest.
9281 */
9282 if (!failed && maxline < 0)
9283 while (cnt > -maxline)
9284 {
9285 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9286 --cnt;
9287 }
9288
9289 if (failed)
9290 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009291 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009292 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009293 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009294 }
9295
9296 vim_free(prev);
9297 fclose(fd);
9298}
9299
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009300 static void
9301return_register(int regname, typval_T *rettv)
9302{
9303 char_u buf[2] = {0, 0};
9304
9305 buf[0] = (char_u)regname;
9306 rettv->v_type = VAR_STRING;
9307 rettv->vval.v_string = vim_strsave(buf);
9308}
9309
9310/*
9311 * "reg_executing()" function
9312 */
9313 static void
9314f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9315{
9316 return_register(reg_executing, rettv);
9317}
9318
9319/*
9320 * "reg_recording()" function
9321 */
9322 static void
9323f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9324{
9325 return_register(reg_recording, rettv);
9326}
9327
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009328#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009329/*
9330 * Convert a List to proftime_T.
9331 * Return FAIL when there is something wrong.
9332 */
9333 static int
9334list2proftime(typval_T *arg, proftime_T *tm)
9335{
9336 long n1, n2;
9337 int error = FALSE;
9338
9339 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9340 || arg->vval.v_list->lv_len != 2)
9341 return FAIL;
9342 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9343 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009344# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009345 tm->HighPart = n1;
9346 tm->LowPart = n2;
9347# else
9348 tm->tv_sec = n1;
9349 tm->tv_usec = n2;
9350# endif
9351 return error ? FAIL : OK;
9352}
9353#endif /* FEAT_RELTIME */
9354
9355/*
9356 * "reltime()" function
9357 */
9358 static void
9359f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9360{
9361#ifdef FEAT_RELTIME
9362 proftime_T res;
9363 proftime_T start;
9364
9365 if (argvars[0].v_type == VAR_UNKNOWN)
9366 {
9367 /* No arguments: get current time. */
9368 profile_start(&res);
9369 }
9370 else if (argvars[1].v_type == VAR_UNKNOWN)
9371 {
9372 if (list2proftime(&argvars[0], &res) == FAIL)
9373 return;
9374 profile_end(&res);
9375 }
9376 else
9377 {
9378 /* Two arguments: compute the difference. */
9379 if (list2proftime(&argvars[0], &start) == FAIL
9380 || list2proftime(&argvars[1], &res) == FAIL)
9381 return;
9382 profile_sub(&res, &start);
9383 }
9384
9385 if (rettv_list_alloc(rettv) == OK)
9386 {
9387 long n1, n2;
9388
Bram Moolenaar4f974752019-02-17 17:44:42 +01009389# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009390 n1 = res.HighPart;
9391 n2 = res.LowPart;
9392# else
9393 n1 = res.tv_sec;
9394 n2 = res.tv_usec;
9395# endif
9396 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9397 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9398 }
9399#endif
9400}
9401
9402#ifdef FEAT_FLOAT
9403/*
9404 * "reltimefloat()" function
9405 */
9406 static void
9407f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9408{
9409# ifdef FEAT_RELTIME
9410 proftime_T tm;
9411# endif
9412
9413 rettv->v_type = VAR_FLOAT;
9414 rettv->vval.v_float = 0;
9415# ifdef FEAT_RELTIME
9416 if (list2proftime(&argvars[0], &tm) == OK)
9417 rettv->vval.v_float = profile_float(&tm);
9418# endif
9419}
9420#endif
9421
9422/*
9423 * "reltimestr()" function
9424 */
9425 static void
9426f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9427{
9428#ifdef FEAT_RELTIME
9429 proftime_T tm;
9430#endif
9431
9432 rettv->v_type = VAR_STRING;
9433 rettv->vval.v_string = NULL;
9434#ifdef FEAT_RELTIME
9435 if (list2proftime(&argvars[0], &tm) == OK)
9436 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9437#endif
9438}
9439
9440#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009441 static void
9442make_connection(void)
9443{
9444 if (X_DISPLAY == NULL
9445# ifdef FEAT_GUI
9446 && !gui.in_use
9447# endif
9448 )
9449 {
9450 x_force_connect = TRUE;
9451 setup_term_clip();
9452 x_force_connect = FALSE;
9453 }
9454}
9455
9456 static int
9457check_connection(void)
9458{
9459 make_connection();
9460 if (X_DISPLAY == NULL)
9461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009462 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009463 return FAIL;
9464 }
9465 return OK;
9466}
9467#endif
9468
9469#ifdef FEAT_CLIENTSERVER
9470 static void
9471remote_common(typval_T *argvars, typval_T *rettv, int expr)
9472{
9473 char_u *server_name;
9474 char_u *keys;
9475 char_u *r = NULL;
9476 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009477 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009478# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009479 HWND w;
9480# else
9481 Window w;
9482# endif
9483
9484 if (check_restricted() || check_secure())
9485 return;
9486
9487# ifdef FEAT_X11
9488 if (check_connection() == FAIL)
9489 return;
9490# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009491 if (argvars[2].v_type != VAR_UNKNOWN
9492 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009493 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009494
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009495 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496 if (server_name == NULL)
9497 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009498 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009499# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009500 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009502 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9503 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009504# endif
9505 {
9506 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009508 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009509 vim_free(r);
9510 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009511 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009512 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009513 return;
9514 }
9515
9516 rettv->vval.v_string = r;
9517
9518 if (argvars[2].v_type != VAR_UNKNOWN)
9519 {
9520 dictitem_T v;
9521 char_u str[30];
9522 char_u *idvar;
9523
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009524 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009525 if (idvar != NULL && *idvar != NUL)
9526 {
9527 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9528 v.di_tv.v_type = VAR_STRING;
9529 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009530 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009531 vim_free(v.di_tv.vval.v_string);
9532 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009533 }
9534}
9535#endif
9536
9537/*
9538 * "remote_expr()" function
9539 */
9540 static void
9541f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9542{
9543 rettv->v_type = VAR_STRING;
9544 rettv->vval.v_string = NULL;
9545#ifdef FEAT_CLIENTSERVER
9546 remote_common(argvars, rettv, TRUE);
9547#endif
9548}
9549
9550/*
9551 * "remote_foreground()" function
9552 */
9553 static void
9554f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9555{
9556#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009557# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009558 /* On Win32 it's done in this application. */
9559 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009560 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009561
9562 if (server_name != NULL)
9563 serverForeground(server_name);
9564 }
9565# else
9566 /* Send a foreground() expression to the server. */
9567 argvars[1].v_type = VAR_STRING;
9568 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9569 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009570 rettv->v_type = VAR_STRING;
9571 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009572 remote_common(argvars, rettv, TRUE);
9573 vim_free(argvars[1].vval.v_string);
9574# endif
9575#endif
9576}
9577
9578 static void
9579f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9580{
9581#ifdef FEAT_CLIENTSERVER
9582 dictitem_T v;
9583 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009584# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009585 long_u n = 0;
9586# endif
9587 char_u *serverid;
9588
9589 if (check_restricted() || check_secure())
9590 {
9591 rettv->vval.v_number = -1;
9592 return;
9593 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009594 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009595 if (serverid == NULL)
9596 {
9597 rettv->vval.v_number = -1;
9598 return; /* type error; errmsg already given */
9599 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009600# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009601 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9602 if (n == 0)
9603 rettv->vval.v_number = -1;
9604 else
9605 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009606 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009607 rettv->vval.v_number = (s != NULL);
9608 }
9609# else
9610 if (check_connection() == FAIL)
9611 return;
9612
9613 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9614 serverStrToWin(serverid), &s);
9615# endif
9616
9617 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9618 {
9619 char_u *retvar;
9620
9621 v.di_tv.v_type = VAR_STRING;
9622 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009623 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009624 if (retvar != NULL)
9625 set_var(retvar, &v.di_tv, FALSE);
9626 vim_free(v.di_tv.vval.v_string);
9627 }
9628#else
9629 rettv->vval.v_number = -1;
9630#endif
9631}
9632
9633 static void
9634f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9635{
9636 char_u *r = NULL;
9637
9638#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009639 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009640
9641 if (serverid != NULL && !check_restricted() && !check_secure())
9642 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009643 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009644# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009645 /* The server's HWND is encoded in the 'id' parameter */
9646 long_u n = 0;
9647# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009648
9649 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009650 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009651
Bram Moolenaar4f974752019-02-17 17:44:42 +01009652# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009653 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9654 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009655 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009656 if (r == NULL)
9657# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009658 if (check_connection() == FAIL
9659 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9660 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009661# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009662 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009663 }
9664#endif
9665 rettv->v_type = VAR_STRING;
9666 rettv->vval.v_string = r;
9667}
9668
9669/*
9670 * "remote_send()" function
9671 */
9672 static void
9673f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9674{
9675 rettv->v_type = VAR_STRING;
9676 rettv->vval.v_string = NULL;
9677#ifdef FEAT_CLIENTSERVER
9678 remote_common(argvars, rettv, FALSE);
9679#endif
9680}
9681
9682/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009683 * "remote_startserver()" function
9684 */
9685 static void
9686f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9687{
9688#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009689 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009690
9691 if (server == NULL)
9692 return; /* type error; errmsg already given */
9693 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009694 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009695 else
9696 {
9697# ifdef FEAT_X11
9698 if (check_connection() == OK)
9699 serverRegisterName(X_DISPLAY, server);
9700# else
9701 serverSetName(server);
9702# endif
9703 }
9704#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009705 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009706#endif
9707}
9708
9709/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009710 * "remove()" function
9711 */
9712 static void
9713f_remove(typval_T *argvars, typval_T *rettv)
9714{
9715 list_T *l;
9716 listitem_T *item, *item2;
9717 listitem_T *li;
9718 long idx;
9719 long end;
9720 char_u *key;
9721 dict_T *d;
9722 dictitem_T *di;
9723 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009724 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009725
9726 if (argvars[0].v_type == VAR_DICT)
9727 {
9728 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009729 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009730 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009731 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009732 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009733 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009734 if (key != NULL)
9735 {
9736 di = dict_find(d, key, -1);
9737 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009738 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009739 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9740 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9741 {
9742 *rettv = di->di_tv;
9743 init_tv(&di->di_tv);
9744 dictitem_remove(d, di);
9745 }
9746 }
9747 }
9748 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009749 else if (argvars[0].v_type == VAR_BLOB)
9750 {
9751 idx = (long)tv_get_number_chk(&argvars[1], &error);
9752 if (!error)
9753 {
9754 blob_T *b = argvars[0].vval.v_blob;
9755 int len = blob_len(b);
9756 char_u *p;
9757
9758 if (idx < 0)
9759 // count from the end
9760 idx = len + idx;
9761 if (idx < 0 || idx >= len)
9762 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009763 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009764 return;
9765 }
9766 if (argvars[2].v_type == VAR_UNKNOWN)
9767 {
9768 // Remove one item, return its value.
9769 p = (char_u *)b->bv_ga.ga_data;
9770 rettv->vval.v_number = (varnumber_T) *(p + idx);
9771 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9772 --b->bv_ga.ga_len;
9773 }
9774 else
9775 {
9776 blob_T *blob;
9777
9778 // Remove range of items, return list with values.
9779 end = (long)tv_get_number_chk(&argvars[2], &error);
9780 if (error)
9781 return;
9782 if (end < 0)
9783 // count from the end
9784 end = len + end;
9785 if (end >= len || idx > end)
9786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009787 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009788 return;
9789 }
9790 blob = blob_alloc();
9791 if (blob == NULL)
9792 return;
9793 blob->bv_ga.ga_len = end - idx + 1;
9794 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9795 {
9796 vim_free(blob);
9797 return;
9798 }
9799 p = (char_u *)b->bv_ga.ga_data;
9800 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9801 (size_t)(end - idx + 1));
9802 ++blob->bv_refcount;
9803 rettv->v_type = VAR_BLOB;
9804 rettv->vval.v_blob = blob;
9805
9806 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9807 b->bv_ga.ga_len -= end - idx + 1;
9808 }
9809 }
9810 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009811 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009812 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009813 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009814 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009815 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009816 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009817 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009818 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009819 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009820 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009821 else
9822 {
9823 if (argvars[2].v_type == VAR_UNKNOWN)
9824 {
9825 /* Remove one item, return its value. */
9826 vimlist_remove(l, item, item);
9827 *rettv = item->li_tv;
9828 vim_free(item);
9829 }
9830 else
9831 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009832 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009833 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009834 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009835 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009836 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009837 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009838 else
9839 {
9840 int cnt = 0;
9841
9842 for (li = item; li != NULL; li = li->li_next)
9843 {
9844 ++cnt;
9845 if (li == item2)
9846 break;
9847 }
9848 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009849 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009850 else
9851 {
9852 vimlist_remove(l, item, item2);
9853 if (rettv_list_alloc(rettv) == OK)
9854 {
9855 l = rettv->vval.v_list;
9856 l->lv_first = item;
9857 l->lv_last = item2;
9858 item->li_prev = NULL;
9859 item2->li_next = NULL;
9860 l->lv_len = cnt;
9861 }
9862 }
9863 }
9864 }
9865 }
9866 }
9867}
9868
9869/*
9870 * "rename({from}, {to})" function
9871 */
9872 static void
9873f_rename(typval_T *argvars, typval_T *rettv)
9874{
9875 char_u buf[NUMBUFLEN];
9876
9877 if (check_restricted() || check_secure())
9878 rettv->vval.v_number = -1;
9879 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009880 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9881 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009882}
9883
9884/*
9885 * "repeat()" function
9886 */
9887 static void
9888f_repeat(typval_T *argvars, typval_T *rettv)
9889{
9890 char_u *p;
9891 int n;
9892 int slen;
9893 int len;
9894 char_u *r;
9895 int i;
9896
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009897 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009898 if (argvars[0].v_type == VAR_LIST)
9899 {
9900 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9901 while (n-- > 0)
9902 if (list_extend(rettv->vval.v_list,
9903 argvars[0].vval.v_list, NULL) == FAIL)
9904 break;
9905 }
9906 else
9907 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009908 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009909 rettv->v_type = VAR_STRING;
9910 rettv->vval.v_string = NULL;
9911
9912 slen = (int)STRLEN(p);
9913 len = slen * n;
9914 if (len <= 0)
9915 return;
9916
9917 r = alloc(len + 1);
9918 if (r != NULL)
9919 {
9920 for (i = 0; i < n; i++)
9921 mch_memmove(r + i * slen, p, (size_t)slen);
9922 r[len] = NUL;
9923 }
9924
9925 rettv->vval.v_string = r;
9926 }
9927}
9928
9929/*
9930 * "resolve()" function
9931 */
9932 static void
9933f_resolve(typval_T *argvars, typval_T *rettv)
9934{
9935 char_u *p;
9936#ifdef HAVE_READLINK
9937 char_u *buf = NULL;
9938#endif
9939
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009940 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009941#ifdef FEAT_SHORTCUT
9942 {
9943 char_u *v = NULL;
9944
Bram Moolenaardce1e892019-02-10 23:18:53 +01009945 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009946 if (v != NULL)
9947 rettv->vval.v_string = v;
9948 else
9949 rettv->vval.v_string = vim_strsave(p);
9950 }
9951#else
9952# ifdef HAVE_READLINK
9953 {
9954 char_u *cpy;
9955 int len;
9956 char_u *remain = NULL;
9957 char_u *q;
9958 int is_relative_to_current = FALSE;
9959 int has_trailing_pathsep = FALSE;
9960 int limit = 100;
9961
9962 p = vim_strsave(p);
9963
9964 if (p[0] == '.' && (vim_ispathsep(p[1])
9965 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9966 is_relative_to_current = TRUE;
9967
9968 len = STRLEN(p);
9969 if (len > 0 && after_pathsep(p, p + len))
9970 {
9971 has_trailing_pathsep = TRUE;
9972 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9973 }
9974
9975 q = getnextcomp(p);
9976 if (*q != NUL)
9977 {
9978 /* Separate the first path component in "p", and keep the
9979 * remainder (beginning with the path separator). */
9980 remain = vim_strsave(q - 1);
9981 q[-1] = NUL;
9982 }
9983
9984 buf = alloc(MAXPATHL + 1);
9985 if (buf == NULL)
9986 goto fail;
9987
9988 for (;;)
9989 {
9990 for (;;)
9991 {
9992 len = readlink((char *)p, (char *)buf, MAXPATHL);
9993 if (len <= 0)
9994 break;
9995 buf[len] = NUL;
9996
9997 if (limit-- == 0)
9998 {
9999 vim_free(p);
10000 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010001 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010002 rettv->vval.v_string = NULL;
10003 goto fail;
10004 }
10005
10006 /* Ensure that the result will have a trailing path separator
10007 * if the argument has one. */
10008 if (remain == NULL && has_trailing_pathsep)
10009 add_pathsep(buf);
10010
10011 /* Separate the first path component in the link value and
10012 * concatenate the remainders. */
10013 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10014 if (*q != NUL)
10015 {
10016 if (remain == NULL)
10017 remain = vim_strsave(q - 1);
10018 else
10019 {
10020 cpy = concat_str(q - 1, remain);
10021 if (cpy != NULL)
10022 {
10023 vim_free(remain);
10024 remain = cpy;
10025 }
10026 }
10027 q[-1] = NUL;
10028 }
10029
10030 q = gettail(p);
10031 if (q > p && *q == NUL)
10032 {
10033 /* Ignore trailing path separator. */
10034 q[-1] = NUL;
10035 q = gettail(p);
10036 }
10037 if (q > p && !mch_isFullName(buf))
10038 {
10039 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010040 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010041 if (cpy != NULL)
10042 {
10043 STRCPY(cpy, p);
10044 STRCPY(gettail(cpy), buf);
10045 vim_free(p);
10046 p = cpy;
10047 }
10048 }
10049 else
10050 {
10051 vim_free(p);
10052 p = vim_strsave(buf);
10053 }
10054 }
10055
10056 if (remain == NULL)
10057 break;
10058
10059 /* Append the first path component of "remain" to "p". */
10060 q = getnextcomp(remain + 1);
10061 len = q - remain - (*q != NUL);
10062 cpy = vim_strnsave(p, STRLEN(p) + len);
10063 if (cpy != NULL)
10064 {
10065 STRNCAT(cpy, remain, len);
10066 vim_free(p);
10067 p = cpy;
10068 }
10069 /* Shorten "remain". */
10070 if (*q != NUL)
10071 STRMOVE(remain, q - 1);
10072 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010073 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010074 }
10075
10076 /* If the result is a relative path name, make it explicitly relative to
10077 * the current directory if and only if the argument had this form. */
10078 if (!vim_ispathsep(*p))
10079 {
10080 if (is_relative_to_current
10081 && *p != NUL
10082 && !(p[0] == '.'
10083 && (p[1] == NUL
10084 || vim_ispathsep(p[1])
10085 || (p[1] == '.'
10086 && (p[2] == NUL
10087 || vim_ispathsep(p[2]))))))
10088 {
10089 /* Prepend "./". */
10090 cpy = concat_str((char_u *)"./", p);
10091 if (cpy != NULL)
10092 {
10093 vim_free(p);
10094 p = cpy;
10095 }
10096 }
10097 else if (!is_relative_to_current)
10098 {
10099 /* Strip leading "./". */
10100 q = p;
10101 while (q[0] == '.' && vim_ispathsep(q[1]))
10102 q += 2;
10103 if (q > p)
10104 STRMOVE(p, p + 2);
10105 }
10106 }
10107
10108 /* Ensure that the result will have no trailing path separator
10109 * if the argument had none. But keep "/" or "//". */
10110 if (!has_trailing_pathsep)
10111 {
10112 q = p + STRLEN(p);
10113 if (after_pathsep(p, q))
10114 *gettail_sep(p) = NUL;
10115 }
10116
10117 rettv->vval.v_string = p;
10118 }
10119# else
10120 rettv->vval.v_string = vim_strsave(p);
10121# endif
10122#endif
10123
10124 simplify_filename(rettv->vval.v_string);
10125
10126#ifdef HAVE_READLINK
10127fail:
10128 vim_free(buf);
10129#endif
10130 rettv->v_type = VAR_STRING;
10131}
10132
10133/*
10134 * "reverse({list})" function
10135 */
10136 static void
10137f_reverse(typval_T *argvars, typval_T *rettv)
10138{
10139 list_T *l;
10140 listitem_T *li, *ni;
10141
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010142 if (argvars[0].v_type == VAR_BLOB)
10143 {
10144 blob_T *b = argvars[0].vval.v_blob;
10145 int i, len = blob_len(b);
10146
10147 for (i = 0; i < len / 2; i++)
10148 {
10149 int tmp = blob_get(b, i);
10150
10151 blob_set(b, i, blob_get(b, len - i - 1));
10152 blob_set(b, len - i - 1, tmp);
10153 }
10154 rettv_blob_set(rettv, b);
10155 return;
10156 }
10157
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010158 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010159 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010160 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010161 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010162 (char_u *)N_("reverse() argument"), TRUE))
10163 {
10164 li = l->lv_last;
10165 l->lv_first = l->lv_last = NULL;
10166 l->lv_len = 0;
10167 while (li != NULL)
10168 {
10169 ni = li->li_prev;
10170 list_append(l, li);
10171 li = ni;
10172 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010173 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010174 l->lv_idx = l->lv_len - l->lv_idx - 1;
10175 }
10176}
10177
10178#define SP_NOMOVE 0x01 /* don't move cursor */
10179#define SP_REPEAT 0x02 /* repeat to find outer pair */
10180#define SP_RETCOUNT 0x04 /* return matchcount */
10181#define SP_SETPCMARK 0x08 /* set previous context mark */
10182#define SP_START 0x10 /* accept match at start position */
10183#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10184#define SP_END 0x40 /* leave cursor at end of match */
10185#define SP_COLUMN 0x80 /* start at cursor column */
10186
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010187/*
10188 * Get flags for a search function.
10189 * Possibly sets "p_ws".
10190 * Returns BACKWARD, FORWARD or zero (for an error).
10191 */
10192 static int
10193get_search_arg(typval_T *varp, int *flagsp)
10194{
10195 int dir = FORWARD;
10196 char_u *flags;
10197 char_u nbuf[NUMBUFLEN];
10198 int mask;
10199
10200 if (varp->v_type != VAR_UNKNOWN)
10201 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010202 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010203 if (flags == NULL)
10204 return 0; /* type error; errmsg already given */
10205 while (*flags != NUL)
10206 {
10207 switch (*flags)
10208 {
10209 case 'b': dir = BACKWARD; break;
10210 case 'w': p_ws = TRUE; break;
10211 case 'W': p_ws = FALSE; break;
10212 default: mask = 0;
10213 if (flagsp != NULL)
10214 switch (*flags)
10215 {
10216 case 'c': mask = SP_START; break;
10217 case 'e': mask = SP_END; break;
10218 case 'm': mask = SP_RETCOUNT; break;
10219 case 'n': mask = SP_NOMOVE; break;
10220 case 'p': mask = SP_SUBPAT; break;
10221 case 'r': mask = SP_REPEAT; break;
10222 case 's': mask = SP_SETPCMARK; break;
10223 case 'z': mask = SP_COLUMN; break;
10224 }
10225 if (mask == 0)
10226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010227 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010228 dir = 0;
10229 }
10230 else
10231 *flagsp |= mask;
10232 }
10233 if (dir == 0)
10234 break;
10235 ++flags;
10236 }
10237 }
10238 return dir;
10239}
10240
10241/*
10242 * Shared by search() and searchpos() functions.
10243 */
10244 static int
10245search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10246{
10247 int flags;
10248 char_u *pat;
10249 pos_T pos;
10250 pos_T save_cursor;
10251 int save_p_ws = p_ws;
10252 int dir;
10253 int retval = 0; /* default: FAIL */
10254 long lnum_stop = 0;
10255 proftime_T tm;
10256#ifdef FEAT_RELTIME
10257 long time_limit = 0;
10258#endif
10259 int options = SEARCH_KEEP;
10260 int subpatnum;
10261
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010262 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010263 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10264 if (dir == 0)
10265 goto theend;
10266 flags = *flagsp;
10267 if (flags & SP_START)
10268 options |= SEARCH_START;
10269 if (flags & SP_END)
10270 options |= SEARCH_END;
10271 if (flags & SP_COLUMN)
10272 options |= SEARCH_COL;
10273
10274 /* Optional arguments: line number to stop searching and timeout. */
10275 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10276 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010277 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010278 if (lnum_stop < 0)
10279 goto theend;
10280#ifdef FEAT_RELTIME
10281 if (argvars[3].v_type != VAR_UNKNOWN)
10282 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010283 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010284 if (time_limit < 0)
10285 goto theend;
10286 }
10287#endif
10288 }
10289
10290#ifdef FEAT_RELTIME
10291 /* Set the time limit, if there is one. */
10292 profile_setlimit(time_limit, &tm);
10293#endif
10294
10295 /*
10296 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10297 * Check to make sure only those flags are set.
10298 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10299 * flags cannot be set. Check for that condition also.
10300 */
10301 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10302 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10303 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010304 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010305 goto theend;
10306 }
10307
10308 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010309 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010310 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010311 if (subpatnum != FAIL)
10312 {
10313 if (flags & SP_SUBPAT)
10314 retval = subpatnum;
10315 else
10316 retval = pos.lnum;
10317 if (flags & SP_SETPCMARK)
10318 setpcmark();
10319 curwin->w_cursor = pos;
10320 if (match_pos != NULL)
10321 {
10322 /* Store the match cursor position */
10323 match_pos->lnum = pos.lnum;
10324 match_pos->col = pos.col + 1;
10325 }
10326 /* "/$" will put the cursor after the end of the line, may need to
10327 * correct that here */
10328 check_cursor();
10329 }
10330
10331 /* If 'n' flag is used: restore cursor position. */
10332 if (flags & SP_NOMOVE)
10333 curwin->w_cursor = save_cursor;
10334 else
10335 curwin->w_set_curswant = TRUE;
10336theend:
10337 p_ws = save_p_ws;
10338
10339 return retval;
10340}
10341
10342#ifdef FEAT_FLOAT
10343
10344/*
10345 * round() is not in C90, use ceil() or floor() instead.
10346 */
10347 float_T
10348vim_round(float_T f)
10349{
10350 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10351}
10352
10353/*
10354 * "round({float})" function
10355 */
10356 static void
10357f_round(typval_T *argvars, typval_T *rettv)
10358{
10359 float_T f = 0.0;
10360
10361 rettv->v_type = VAR_FLOAT;
10362 if (get_float_arg(argvars, &f) == OK)
10363 rettv->vval.v_float = vim_round(f);
10364 else
10365 rettv->vval.v_float = 0.0;
10366}
10367#endif
10368
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010369#ifdef FEAT_RUBY
10370/*
10371 * "rubyeval()" function
10372 */
10373 static void
10374f_rubyeval(typval_T *argvars, typval_T *rettv)
10375{
10376 char_u *str;
10377 char_u buf[NUMBUFLEN];
10378
10379 str = tv_get_string_buf(&argvars[0], buf);
10380 do_rubyeval(str, rettv);
10381}
10382#endif
10383
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010384/*
10385 * "screenattr()" function
10386 */
10387 static void
10388f_screenattr(typval_T *argvars, typval_T *rettv)
10389{
10390 int row;
10391 int col;
10392 int c;
10393
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010394 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10395 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010396 if (row < 0 || row >= screen_Rows
10397 || col < 0 || col >= screen_Columns)
10398 c = -1;
10399 else
10400 c = ScreenAttrs[LineOffset[row] + col];
10401 rettv->vval.v_number = c;
10402}
10403
10404/*
10405 * "screenchar()" function
10406 */
10407 static void
10408f_screenchar(typval_T *argvars, typval_T *rettv)
10409{
10410 int row;
10411 int col;
10412 int off;
10413 int c;
10414
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010415 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10416 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010417 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010418 c = -1;
10419 else
10420 {
10421 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010422 if (enc_utf8 && ScreenLinesUC[off] != 0)
10423 c = ScreenLinesUC[off];
10424 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010425 c = ScreenLines[off];
10426 }
10427 rettv->vval.v_number = c;
10428}
10429
10430/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010431 * "screenchars()" function
10432 */
10433 static void
10434f_screenchars(typval_T *argvars, typval_T *rettv)
10435{
10436 int row;
10437 int col;
10438 int off;
10439 int c;
10440 int i;
10441
10442 if (rettv_list_alloc(rettv) == FAIL)
10443 return;
10444 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10445 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10446 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10447 return;
10448
10449 off = LineOffset[row] + col;
10450 if (enc_utf8 && ScreenLinesUC[off] != 0)
10451 c = ScreenLinesUC[off];
10452 else
10453 c = ScreenLines[off];
10454 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10455
10456 if (enc_utf8)
10457
10458 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10459 list_append_number(rettv->vval.v_list,
10460 (varnumber_T)ScreenLinesC[i][off]);
10461}
10462
10463/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010464 * "screencol()" function
10465 *
10466 * First column is 1 to be consistent with virtcol().
10467 */
10468 static void
10469f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10470{
10471 rettv->vval.v_number = screen_screencol() + 1;
10472}
10473
10474/*
10475 * "screenrow()" function
10476 */
10477 static void
10478f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10479{
10480 rettv->vval.v_number = screen_screenrow() + 1;
10481}
10482
10483/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010484 * "screenstring()" function
10485 */
10486 static void
10487f_screenstring(typval_T *argvars, typval_T *rettv)
10488{
10489 int row;
10490 int col;
10491 int off;
10492 int c;
10493 int i;
10494 char_u buf[MB_MAXBYTES + 1];
10495 int buflen = 0;
10496
10497 rettv->vval.v_string = NULL;
10498 rettv->v_type = VAR_STRING;
10499
10500 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10501 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10502 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10503 return;
10504
10505 off = LineOffset[row] + col;
10506 if (enc_utf8 && ScreenLinesUC[off] != 0)
10507 c = ScreenLinesUC[off];
10508 else
10509 c = ScreenLines[off];
10510 buflen += mb_char2bytes(c, buf);
10511
10512 if (enc_utf8)
10513 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10514 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10515
10516 buf[buflen] = NUL;
10517 rettv->vval.v_string = vim_strsave(buf);
10518}
10519
10520/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010521 * "search()" function
10522 */
10523 static void
10524f_search(typval_T *argvars, typval_T *rettv)
10525{
10526 int flags = 0;
10527
10528 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10529}
10530
10531/*
10532 * "searchdecl()" function
10533 */
10534 static void
10535f_searchdecl(typval_T *argvars, typval_T *rettv)
10536{
10537 int locally = 1;
10538 int thisblock = 0;
10539 int error = FALSE;
10540 char_u *name;
10541
10542 rettv->vval.v_number = 1; /* default: FAIL */
10543
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010544 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010545 if (argvars[1].v_type != VAR_UNKNOWN)
10546 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010547 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010548 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010549 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010550 }
10551 if (!error && name != NULL)
10552 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10553 locally, thisblock, SEARCH_KEEP) == FAIL;
10554}
10555
10556/*
10557 * Used by searchpair() and searchpairpos()
10558 */
10559 static int
10560searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10561{
10562 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010563 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010564 int save_p_ws = p_ws;
10565 int dir;
10566 int flags = 0;
10567 char_u nbuf1[NUMBUFLEN];
10568 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010569 int retval = 0; /* default: FAIL */
10570 long lnum_stop = 0;
10571 long time_limit = 0;
10572
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010573 /* Get the three pattern arguments: start, middle, end. Will result in an
10574 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010575 spat = tv_get_string_chk(&argvars[0]);
10576 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10577 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010578 if (spat == NULL || mpat == NULL || epat == NULL)
10579 goto theend; /* type error */
10580
10581 /* Handle the optional fourth argument: flags */
10582 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10583 if (dir == 0)
10584 goto theend;
10585
10586 /* Don't accept SP_END or SP_SUBPAT.
10587 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10588 */
10589 if ((flags & (SP_END | SP_SUBPAT)) != 0
10590 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010592 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010593 goto theend;
10594 }
10595
10596 /* Using 'r' implies 'W', otherwise it doesn't work. */
10597 if (flags & SP_REPEAT)
10598 p_ws = FALSE;
10599
10600 /* Optional fifth argument: skip expression */
10601 if (argvars[3].v_type == VAR_UNKNOWN
10602 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010603 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010604 else
10605 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010606 skip = &argvars[4];
10607 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10608 && skip->v_type != VAR_STRING)
10609 {
10610 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010611 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010612 goto theend;
10613 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010614 if (argvars[5].v_type != VAR_UNKNOWN)
10615 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010616 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010617 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010618 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010619 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010620 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010621 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010622#ifdef FEAT_RELTIME
10623 if (argvars[6].v_type != VAR_UNKNOWN)
10624 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010625 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010626 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010628 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010629 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010630 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010631 }
10632#endif
10633 }
10634 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010635
10636 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10637 match_pos, lnum_stop, time_limit);
10638
10639theend:
10640 p_ws = save_p_ws;
10641
10642 return retval;
10643}
10644
10645/*
10646 * "searchpair()" function
10647 */
10648 static void
10649f_searchpair(typval_T *argvars, typval_T *rettv)
10650{
10651 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10652}
10653
10654/*
10655 * "searchpairpos()" function
10656 */
10657 static void
10658f_searchpairpos(typval_T *argvars, typval_T *rettv)
10659{
10660 pos_T match_pos;
10661 int lnum = 0;
10662 int col = 0;
10663
10664 if (rettv_list_alloc(rettv) == FAIL)
10665 return;
10666
10667 if (searchpair_cmn(argvars, &match_pos) > 0)
10668 {
10669 lnum = match_pos.lnum;
10670 col = match_pos.col;
10671 }
10672
10673 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10674 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10675}
10676
10677/*
10678 * Search for a start/middle/end thing.
10679 * Used by searchpair(), see its documentation for the details.
10680 * Returns 0 or -1 for no match,
10681 */
10682 long
10683do_searchpair(
10684 char_u *spat, /* start pattern */
10685 char_u *mpat, /* middle pattern */
10686 char_u *epat, /* end pattern */
10687 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010688 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010689 int flags, /* SP_SETPCMARK and other SP_ values */
10690 pos_T *match_pos,
10691 linenr_T lnum_stop, /* stop at this line if not zero */
10692 long time_limit UNUSED) /* stop after this many msec */
10693{
10694 char_u *save_cpo;
10695 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10696 long retval = 0;
10697 pos_T pos;
10698 pos_T firstpos;
10699 pos_T foundpos;
10700 pos_T save_cursor;
10701 pos_T save_pos;
10702 int n;
10703 int r;
10704 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010705 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010706 int err;
10707 int options = SEARCH_KEEP;
10708 proftime_T tm;
10709
10710 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10711 save_cpo = p_cpo;
10712 p_cpo = empty_option;
10713
10714#ifdef FEAT_RELTIME
10715 /* Set the time limit, if there is one. */
10716 profile_setlimit(time_limit, &tm);
10717#endif
10718
10719 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10720 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010721 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
10722 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010723 if (pat2 == NULL || pat3 == NULL)
10724 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010725 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010726 if (*mpat == NUL)
10727 STRCPY(pat3, pat2);
10728 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010729 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010730 spat, epat, mpat);
10731 if (flags & SP_START)
10732 options |= SEARCH_START;
10733
Bram Moolenaar48570482017-10-30 21:48:41 +010010734 if (skip != NULL)
10735 {
10736 /* Empty string means to not use the skip expression. */
10737 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10738 use_skip = skip->vval.v_string != NULL
10739 && *skip->vval.v_string != NUL;
10740 }
10741
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010742 save_cursor = curwin->w_cursor;
10743 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010744 CLEAR_POS(&firstpos);
10745 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010746 pat = pat3;
10747 for (;;)
10748 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010749 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010750 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010751 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010752 /* didn't find it or found the first match again: FAIL */
10753 break;
10754
10755 if (firstpos.lnum == 0)
10756 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010757 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010758 {
10759 /* Found the same position again. Can happen with a pattern that
10760 * has "\zs" at the end and searching backwards. Advance one
10761 * character and try again. */
10762 if (dir == BACKWARD)
10763 decl(&pos);
10764 else
10765 incl(&pos);
10766 }
10767 foundpos = pos;
10768
10769 /* clear the start flag to avoid getting stuck here */
10770 options &= ~SEARCH_START;
10771
10772 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010773 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010774 {
10775 save_pos = curwin->w_cursor;
10776 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010777 err = FALSE;
10778 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010779 curwin->w_cursor = save_pos;
10780 if (err)
10781 {
10782 /* Evaluating {skip} caused an error, break here. */
10783 curwin->w_cursor = save_cursor;
10784 retval = -1;
10785 break;
10786 }
10787 if (r)
10788 continue;
10789 }
10790
10791 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10792 {
10793 /* Found end when searching backwards or start when searching
10794 * forward: nested pair. */
10795 ++nest;
10796 pat = pat2; /* nested, don't search for middle */
10797 }
10798 else
10799 {
10800 /* Found end when searching forward or start when searching
10801 * backward: end of (nested) pair; or found middle in outer pair. */
10802 if (--nest == 1)
10803 pat = pat3; /* outer level, search for middle */
10804 }
10805
10806 if (nest == 0)
10807 {
10808 /* Found the match: return matchcount or line number. */
10809 if (flags & SP_RETCOUNT)
10810 ++retval;
10811 else
10812 retval = pos.lnum;
10813 if (flags & SP_SETPCMARK)
10814 setpcmark();
10815 curwin->w_cursor = pos;
10816 if (!(flags & SP_REPEAT))
10817 break;
10818 nest = 1; /* search for next unmatched */
10819 }
10820 }
10821
10822 if (match_pos != NULL)
10823 {
10824 /* Store the match cursor position */
10825 match_pos->lnum = curwin->w_cursor.lnum;
10826 match_pos->col = curwin->w_cursor.col + 1;
10827 }
10828
10829 /* If 'n' flag is used or search failed: restore cursor position. */
10830 if ((flags & SP_NOMOVE) || retval == 0)
10831 curwin->w_cursor = save_cursor;
10832
10833theend:
10834 vim_free(pat2);
10835 vim_free(pat3);
10836 if (p_cpo == empty_option)
10837 p_cpo = save_cpo;
10838 else
10839 /* Darn, evaluating the {skip} expression changed the value. */
10840 free_string_option(save_cpo);
10841
10842 return retval;
10843}
10844
10845/*
10846 * "searchpos()" function
10847 */
10848 static void
10849f_searchpos(typval_T *argvars, typval_T *rettv)
10850{
10851 pos_T match_pos;
10852 int lnum = 0;
10853 int col = 0;
10854 int n;
10855 int flags = 0;
10856
10857 if (rettv_list_alloc(rettv) == FAIL)
10858 return;
10859
10860 n = search_cmn(argvars, &match_pos, &flags);
10861 if (n > 0)
10862 {
10863 lnum = match_pos.lnum;
10864 col = match_pos.col;
10865 }
10866
10867 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10868 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10869 if (flags & SP_SUBPAT)
10870 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10871}
10872
10873 static void
10874f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10875{
10876#ifdef FEAT_CLIENTSERVER
10877 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010878 char_u *server = tv_get_string_chk(&argvars[0]);
10879 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010880
10881 rettv->vval.v_number = -1;
10882 if (server == NULL || reply == NULL)
10883 return;
10884 if (check_restricted() || check_secure())
10885 return;
10886# ifdef FEAT_X11
10887 if (check_connection() == FAIL)
10888 return;
10889# endif
10890
10891 if (serverSendReply(server, reply) < 0)
10892 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010893 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010894 return;
10895 }
10896 rettv->vval.v_number = 0;
10897#else
10898 rettv->vval.v_number = -1;
10899#endif
10900}
10901
10902 static void
10903f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10904{
10905 char_u *r = NULL;
10906
10907#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010908# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010909 r = serverGetVimNames();
10910# else
10911 make_connection();
10912 if (X_DISPLAY != NULL)
10913 r = serverGetVimNames(X_DISPLAY);
10914# endif
10915#endif
10916 rettv->v_type = VAR_STRING;
10917 rettv->vval.v_string = r;
10918}
10919
10920/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010921 * "setbufline()" function
10922 */
10923 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010924f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010925{
10926 linenr_T lnum;
10927 buf_T *buf;
10928
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010929 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010930 if (buf == NULL)
10931 rettv->vval.v_number = 1; /* FAIL */
10932 else
10933 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010934 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010935 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010936 }
10937}
10938
10939/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010940 * "setbufvar()" function
10941 */
10942 static void
10943f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10944{
10945 buf_T *buf;
10946 char_u *varname, *bufvarname;
10947 typval_T *varp;
10948 char_u nbuf[NUMBUFLEN];
10949
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010950 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010951 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010952 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10953 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010954 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010955 varp = &argvars[2];
10956
10957 if (buf != NULL && varname != NULL && varp != NULL)
10958 {
10959 if (*varname == '&')
10960 {
10961 long numval;
10962 char_u *strval;
10963 int error = FALSE;
10964 aco_save_T aco;
10965
10966 /* set curbuf to be our buf, temporarily */
10967 aucmd_prepbuf(&aco, buf);
10968
10969 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010970 numval = (long)tv_get_number_chk(varp, &error);
10971 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010972 if (!error && strval != NULL)
10973 set_option_value(varname, numval, strval, OPT_LOCAL);
10974
10975 /* reset notion of buffer */
10976 aucmd_restbuf(&aco);
10977 }
10978 else
10979 {
10980 buf_T *save_curbuf = curbuf;
10981
Bram Moolenaar964b3742019-05-24 18:54:09 +020010982 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010983 if (bufvarname != NULL)
10984 {
10985 curbuf = buf;
10986 STRCPY(bufvarname, "b:");
10987 STRCPY(bufvarname + 2, varname);
10988 set_var(bufvarname, varp, TRUE);
10989 vim_free(bufvarname);
10990 curbuf = save_curbuf;
10991 }
10992 }
10993 }
10994}
10995
10996 static void
10997f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10998{
10999 dict_T *d;
11000 dictitem_T *di;
11001 char_u *csearch;
11002
11003 if (argvars[0].v_type != VAR_DICT)
11004 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011005 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011006 return;
11007 }
11008
11009 if ((d = argvars[0].vval.v_dict) != NULL)
11010 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011011 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011012 if (csearch != NULL)
11013 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011014 if (enc_utf8)
11015 {
11016 int pcc[MAX_MCO];
11017 int c = utfc_ptr2char(csearch, pcc);
11018
11019 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11020 }
11021 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011022 set_last_csearch(PTR2CHAR(csearch),
11023 csearch, MB_PTR2LEN(csearch));
11024 }
11025
11026 di = dict_find(d, (char_u *)"forward", -1);
11027 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011028 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011029 ? FORWARD : BACKWARD);
11030
11031 di = dict_find(d, (char_u *)"until", -1);
11032 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011033 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011034 }
11035}
11036
11037/*
11038 * "setcmdpos()" function
11039 */
11040 static void
11041f_setcmdpos(typval_T *argvars, typval_T *rettv)
11042{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011043 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011044
11045 if (pos >= 0)
11046 rettv->vval.v_number = set_cmdline_pos(pos);
11047}
11048
11049/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011050 * "setenv()" function
11051 */
11052 static void
11053f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11054{
11055 char_u namebuf[NUMBUFLEN];
11056 char_u valbuf[NUMBUFLEN];
11057 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11058
11059 if (argvars[1].v_type == VAR_SPECIAL
11060 && argvars[1].vval.v_number == VVAL_NULL)
11061 vim_unsetenv(name);
11062 else
11063 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11064}
11065
11066/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011067 * "setfperm({fname}, {mode})" function
11068 */
11069 static void
11070f_setfperm(typval_T *argvars, typval_T *rettv)
11071{
11072 char_u *fname;
11073 char_u modebuf[NUMBUFLEN];
11074 char_u *mode_str;
11075 int i;
11076 int mask;
11077 int mode = 0;
11078
11079 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011080 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011081 if (fname == NULL)
11082 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011083 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011084 if (mode_str == NULL)
11085 return;
11086 if (STRLEN(mode_str) != 9)
11087 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011088 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011089 return;
11090 }
11091
11092 mask = 1;
11093 for (i = 8; i >= 0; --i)
11094 {
11095 if (mode_str[i] != '-')
11096 mode |= mask;
11097 mask = mask << 1;
11098 }
11099 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11100}
11101
11102/*
11103 * "setline()" function
11104 */
11105 static void
11106f_setline(typval_T *argvars, typval_T *rettv)
11107{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011108 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011109
Bram Moolenaarca851592018-06-06 21:04:07 +020011110 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011111}
11112
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011113/*
11114 * Used by "setqflist()" and "setloclist()" functions
11115 */
11116 static void
11117set_qf_ll_list(
11118 win_T *wp UNUSED,
11119 typval_T *list_arg UNUSED,
11120 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011121 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011122 typval_T *rettv)
11123{
11124#ifdef FEAT_QUICKFIX
11125 static char *e_invact = N_("E927: Invalid action: '%s'");
11126 char_u *act;
11127 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011128 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011129#endif
11130
11131 rettv->vval.v_number = -1;
11132
11133#ifdef FEAT_QUICKFIX
11134 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011135 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011136 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011137 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011138 else
11139 {
11140 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011141 dict_T *d = NULL;
11142 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011143
11144 if (action_arg->v_type == VAR_STRING)
11145 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011146 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011147 if (act == NULL)
11148 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011149 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11150 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011151 action = *act;
11152 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011153 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011154 }
11155 else if (action_arg->v_type == VAR_UNKNOWN)
11156 action = ' ';
11157 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011158 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011159
Bram Moolenaard823fa92016-08-12 16:29:27 +020011160 if (action_arg->v_type != VAR_UNKNOWN
11161 && what_arg->v_type != VAR_UNKNOWN)
11162 {
11163 if (what_arg->v_type == VAR_DICT)
11164 d = what_arg->vval.v_dict;
11165 else
11166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011167 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011168 valid_dict = FALSE;
11169 }
11170 }
11171
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011172 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011173 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011174 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11175 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011176 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011177 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011178 }
11179#endif
11180}
11181
11182/*
11183 * "setloclist()" function
11184 */
11185 static void
11186f_setloclist(typval_T *argvars, typval_T *rettv)
11187{
11188 win_T *win;
11189
11190 rettv->vval.v_number = -1;
11191
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011192 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011193 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011194 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011195}
11196
11197/*
11198 * "setmatches()" function
11199 */
11200 static void
11201f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11202{
11203#ifdef FEAT_SEARCH_EXTRA
11204 list_T *l;
11205 listitem_T *li;
11206 dict_T *d;
11207 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011208 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011209
11210 rettv->vval.v_number = -1;
11211 if (argvars[0].v_type != VAR_LIST)
11212 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011213 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011214 return;
11215 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011216 if (win == NULL)
11217 return;
11218
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011219 if ((l = argvars[0].vval.v_list) != NULL)
11220 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011221 /* To some extent make sure that we are dealing with a list from
11222 * "getmatches()". */
11223 li = l->lv_first;
11224 while (li != NULL)
11225 {
11226 if (li->li_tv.v_type != VAR_DICT
11227 || (d = li->li_tv.vval.v_dict) == NULL)
11228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011229 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011230 return;
11231 }
11232 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11233 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11234 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11235 && dict_find(d, (char_u *)"priority", -1) != NULL
11236 && dict_find(d, (char_u *)"id", -1) != NULL))
11237 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011238 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011239 return;
11240 }
11241 li = li->li_next;
11242 }
11243
Bram Moolenaaraff74912019-03-30 18:11:49 +010011244 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011245 li = l->lv_first;
11246 while (li != NULL)
11247 {
11248 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011249 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011250 dictitem_T *di;
11251 char_u *group;
11252 int priority;
11253 int id;
11254 char_u *conceal;
11255
11256 d = li->li_tv.vval.v_dict;
11257 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11258 {
11259 if (s == NULL)
11260 {
11261 s = list_alloc();
11262 if (s == NULL)
11263 return;
11264 }
11265
11266 /* match from matchaddpos() */
11267 for (i = 1; i < 9; i++)
11268 {
11269 sprintf((char *)buf, (char *)"pos%d", i);
11270 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11271 {
11272 if (di->di_tv.v_type != VAR_LIST)
11273 return;
11274
11275 list_append_tv(s, &di->di_tv);
11276 s->lv_refcount++;
11277 }
11278 else
11279 break;
11280 }
11281 }
11282
Bram Moolenaar8f667172018-12-14 15:38:31 +010011283 group = dict_get_string(d, (char_u *)"group", TRUE);
11284 priority = (int)dict_get_number(d, (char_u *)"priority");
11285 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011286 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011287 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011288 : NULL;
11289 if (i == 0)
11290 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011291 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011292 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011293 priority, id, NULL, conceal);
11294 }
11295 else
11296 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011297 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011298 list_unref(s);
11299 s = NULL;
11300 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011301 vim_free(group);
11302 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011303
11304 li = li->li_next;
11305 }
11306 rettv->vval.v_number = 0;
11307 }
11308#endif
11309}
11310
11311/*
11312 * "setpos()" function
11313 */
11314 static void
11315f_setpos(typval_T *argvars, typval_T *rettv)
11316{
11317 pos_T pos;
11318 int fnum;
11319 char_u *name;
11320 colnr_T curswant = -1;
11321
11322 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011323 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011324 if (name != NULL)
11325 {
11326 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11327 {
11328 if (--pos.col < 0)
11329 pos.col = 0;
11330 if (name[0] == '.' && name[1] == NUL)
11331 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011332 /* set cursor; "fnum" is ignored */
11333 curwin->w_cursor = pos;
11334 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011335 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011336 curwin->w_curswant = curswant - 1;
11337 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011338 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011339 check_cursor();
11340 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011341 }
11342 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11343 {
11344 /* set mark */
11345 if (setmark_pos(name[1], &pos, fnum) == OK)
11346 rettv->vval.v_number = 0;
11347 }
11348 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011349 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011350 }
11351 }
11352}
11353
11354/*
11355 * "setqflist()" function
11356 */
11357 static void
11358f_setqflist(typval_T *argvars, typval_T *rettv)
11359{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011360 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011361}
11362
11363/*
11364 * "setreg()" function
11365 */
11366 static void
11367f_setreg(typval_T *argvars, typval_T *rettv)
11368{
11369 int regname;
11370 char_u *strregname;
11371 char_u *stropt;
11372 char_u *strval;
11373 int append;
11374 char_u yank_type;
11375 long block_len;
11376
11377 block_len = -1;
11378 yank_type = MAUTO;
11379 append = FALSE;
11380
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011381 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011382 rettv->vval.v_number = 1; /* FAIL is default */
11383
11384 if (strregname == NULL)
11385 return; /* type error; errmsg already given */
11386 regname = *strregname;
11387 if (regname == 0 || regname == '@')
11388 regname = '"';
11389
11390 if (argvars[2].v_type != VAR_UNKNOWN)
11391 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011392 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011393 if (stropt == NULL)
11394 return; /* type error */
11395 for (; *stropt != NUL; ++stropt)
11396 switch (*stropt)
11397 {
11398 case 'a': case 'A': /* append */
11399 append = TRUE;
11400 break;
11401 case 'v': case 'c': /* character-wise selection */
11402 yank_type = MCHAR;
11403 break;
11404 case 'V': case 'l': /* line-wise selection */
11405 yank_type = MLINE;
11406 break;
11407 case 'b': case Ctrl_V: /* block-wise selection */
11408 yank_type = MBLOCK;
11409 if (VIM_ISDIGIT(stropt[1]))
11410 {
11411 ++stropt;
11412 block_len = getdigits(&stropt) - 1;
11413 --stropt;
11414 }
11415 break;
11416 }
11417 }
11418
11419 if (argvars[1].v_type == VAR_LIST)
11420 {
11421 char_u **lstval;
11422 char_u **allocval;
11423 char_u buf[NUMBUFLEN];
11424 char_u **curval;
11425 char_u **curallocval;
11426 list_T *ll = argvars[1].vval.v_list;
11427 listitem_T *li;
11428 int len;
11429
11430 /* If the list is NULL handle like an empty list. */
11431 len = ll == NULL ? 0 : ll->lv_len;
11432
11433 /* First half: use for pointers to result lines; second half: use for
11434 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011435 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011436 if (lstval == NULL)
11437 return;
11438 curval = lstval;
11439 allocval = lstval + len + 2;
11440 curallocval = allocval;
11441
11442 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11443 li = li->li_next)
11444 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011445 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011446 if (strval == NULL)
11447 goto free_lstval;
11448 if (strval == buf)
11449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011450 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011451 * overwrite the string. */
11452 strval = vim_strsave(buf);
11453 if (strval == NULL)
11454 goto free_lstval;
11455 *curallocval++ = strval;
11456 }
11457 *curval++ = strval;
11458 }
11459 *curval++ = NULL;
11460
11461 write_reg_contents_lst(regname, lstval, -1,
11462 append, yank_type, block_len);
11463free_lstval:
11464 while (curallocval > allocval)
11465 vim_free(*--curallocval);
11466 vim_free(lstval);
11467 }
11468 else
11469 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011470 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011471 if (strval == NULL)
11472 return;
11473 write_reg_contents_ex(regname, strval, -1,
11474 append, yank_type, block_len);
11475 }
11476 rettv->vval.v_number = 0;
11477}
11478
11479/*
11480 * "settabvar()" function
11481 */
11482 static void
11483f_settabvar(typval_T *argvars, typval_T *rettv)
11484{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011485 tabpage_T *save_curtab;
11486 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011487 char_u *varname, *tabvarname;
11488 typval_T *varp;
11489
11490 rettv->vval.v_number = 0;
11491
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011492 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011493 return;
11494
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011495 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11496 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011497 varp = &argvars[2];
11498
Bram Moolenaar4033c552017-09-16 20:54:51 +020011499 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011500 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011501 save_curtab = curtab;
11502 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011503
Bram Moolenaar964b3742019-05-24 18:54:09 +020011504 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011505 if (tabvarname != NULL)
11506 {
11507 STRCPY(tabvarname, "t:");
11508 STRCPY(tabvarname + 2, varname);
11509 set_var(tabvarname, varp, TRUE);
11510 vim_free(tabvarname);
11511 }
11512
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011513 /* Restore current tabpage */
11514 if (valid_tabpage(save_curtab))
11515 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011516 }
11517}
11518
11519/*
11520 * "settabwinvar()" function
11521 */
11522 static void
11523f_settabwinvar(typval_T *argvars, typval_T *rettv)
11524{
11525 setwinvar(argvars, rettv, 1);
11526}
11527
11528/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011529 * "settagstack()" function
11530 */
11531 static void
11532f_settagstack(typval_T *argvars, typval_T *rettv)
11533{
11534 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11535 win_T *wp;
11536 dict_T *d;
11537 int action = 'r';
11538
11539 rettv->vval.v_number = -1;
11540
11541 // first argument: window number or id
11542 wp = find_win_by_nr_or_id(&argvars[0]);
11543 if (wp == NULL)
11544 return;
11545
11546 // second argument: dict with items to set in the tag stack
11547 if (argvars[1].v_type != VAR_DICT)
11548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011549 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011550 return;
11551 }
11552 d = argvars[1].vval.v_dict;
11553 if (d == NULL)
11554 return;
11555
11556 // third argument: action - 'a' for append and 'r' for replace.
11557 // default is to replace the stack.
11558 if (argvars[2].v_type == VAR_UNKNOWN)
11559 action = 'r';
11560 else if (argvars[2].v_type == VAR_STRING)
11561 {
11562 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011563 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011564 if (actstr == NULL)
11565 return;
11566 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11567 action = *actstr;
11568 else
11569 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011570 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011571 return;
11572 }
11573 }
11574 else
11575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011576 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011577 return;
11578 }
11579
11580 if (set_tagstack(wp, d, action) == OK)
11581 rettv->vval.v_number = 0;
11582}
11583
11584/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011585 * "setwinvar()" function
11586 */
11587 static void
11588f_setwinvar(typval_T *argvars, typval_T *rettv)
11589{
11590 setwinvar(argvars, rettv, 0);
11591}
11592
11593#ifdef FEAT_CRYPT
11594/*
11595 * "sha256({string})" function
11596 */
11597 static void
11598f_sha256(typval_T *argvars, typval_T *rettv)
11599{
11600 char_u *p;
11601
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011602 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011603 rettv->vval.v_string = vim_strsave(
11604 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11605 rettv->v_type = VAR_STRING;
11606}
11607#endif /* FEAT_CRYPT */
11608
11609/*
11610 * "shellescape({string})" function
11611 */
11612 static void
11613f_shellescape(typval_T *argvars, typval_T *rettv)
11614{
Bram Moolenaar20615522017-06-05 18:46:26 +020011615 int do_special = non_zero_arg(&argvars[1]);
11616
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011617 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011618 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011619 rettv->v_type = VAR_STRING;
11620}
11621
11622/*
11623 * shiftwidth() function
11624 */
11625 static void
11626f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11627{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011628 rettv->vval.v_number = 0;
11629
11630 if (argvars[0].v_type != VAR_UNKNOWN)
11631 {
11632 long col;
11633
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011634 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011635 if (col < 0)
11636 return; // type error; errmsg already given
11637#ifdef FEAT_VARTABS
11638 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11639 return;
11640#endif
11641 }
11642
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011643 rettv->vval.v_number = get_sw_value(curbuf);
11644}
11645
11646/*
11647 * "simplify()" function
11648 */
11649 static void
11650f_simplify(typval_T *argvars, typval_T *rettv)
11651{
11652 char_u *p;
11653
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011654 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011655 rettv->vval.v_string = vim_strsave(p);
11656 simplify_filename(rettv->vval.v_string); /* simplify in place */
11657 rettv->v_type = VAR_STRING;
11658}
11659
11660#ifdef FEAT_FLOAT
11661/*
11662 * "sin()" function
11663 */
11664 static void
11665f_sin(typval_T *argvars, typval_T *rettv)
11666{
11667 float_T f = 0.0;
11668
11669 rettv->v_type = VAR_FLOAT;
11670 if (get_float_arg(argvars, &f) == OK)
11671 rettv->vval.v_float = sin(f);
11672 else
11673 rettv->vval.v_float = 0.0;
11674}
11675
11676/*
11677 * "sinh()" function
11678 */
11679 static void
11680f_sinh(typval_T *argvars, typval_T *rettv)
11681{
11682 float_T f = 0.0;
11683
11684 rettv->v_type = VAR_FLOAT;
11685 if (get_float_arg(argvars, &f) == OK)
11686 rettv->vval.v_float = sinh(f);
11687 else
11688 rettv->vval.v_float = 0.0;
11689}
11690#endif
11691
Bram Moolenaareae1b912019-05-09 15:12:55 +020011692static int item_compare(const void *s1, const void *s2);
11693static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011694
11695/* struct used in the array that's given to qsort() */
11696typedef struct
11697{
11698 listitem_T *item;
11699 int idx;
11700} sortItem_T;
11701
11702/* struct storing information about current sort */
11703typedef struct
11704{
11705 int item_compare_ic;
11706 int item_compare_numeric;
11707 int item_compare_numbers;
11708#ifdef FEAT_FLOAT
11709 int item_compare_float;
11710#endif
11711 char_u *item_compare_func;
11712 partial_T *item_compare_partial;
11713 dict_T *item_compare_selfdict;
11714 int item_compare_func_err;
11715 int item_compare_keep_zero;
11716} sortinfo_T;
11717static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011718#define ITEM_COMPARE_FAIL 999
11719
11720/*
11721 * Compare functions for f_sort() and f_uniq() below.
11722 */
11723 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011724item_compare(const void *s1, const void *s2)
11725{
11726 sortItem_T *si1, *si2;
11727 typval_T *tv1, *tv2;
11728 char_u *p1, *p2;
11729 char_u *tofree1 = NULL, *tofree2 = NULL;
11730 int res;
11731 char_u numbuf1[NUMBUFLEN];
11732 char_u numbuf2[NUMBUFLEN];
11733
11734 si1 = (sortItem_T *)s1;
11735 si2 = (sortItem_T *)s2;
11736 tv1 = &si1->item->li_tv;
11737 tv2 = &si2->item->li_tv;
11738
11739 if (sortinfo->item_compare_numbers)
11740 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011741 varnumber_T v1 = tv_get_number(tv1);
11742 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011743
11744 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11745 }
11746
11747#ifdef FEAT_FLOAT
11748 if (sortinfo->item_compare_float)
11749 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011750 float_T v1 = tv_get_float(tv1);
11751 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011752
11753 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11754 }
11755#endif
11756
11757 /* tv2string() puts quotes around a string and allocates memory. Don't do
11758 * that for string variables. Use a single quote when comparing with a
11759 * non-string to do what the docs promise. */
11760 if (tv1->v_type == VAR_STRING)
11761 {
11762 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11763 p1 = (char_u *)"'";
11764 else
11765 p1 = tv1->vval.v_string;
11766 }
11767 else
11768 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11769 if (tv2->v_type == VAR_STRING)
11770 {
11771 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11772 p2 = (char_u *)"'";
11773 else
11774 p2 = tv2->vval.v_string;
11775 }
11776 else
11777 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11778 if (p1 == NULL)
11779 p1 = (char_u *)"";
11780 if (p2 == NULL)
11781 p2 = (char_u *)"";
11782 if (!sortinfo->item_compare_numeric)
11783 {
11784 if (sortinfo->item_compare_ic)
11785 res = STRICMP(p1, p2);
11786 else
11787 res = STRCMP(p1, p2);
11788 }
11789 else
11790 {
11791 double n1, n2;
11792 n1 = strtod((char *)p1, (char **)&p1);
11793 n2 = strtod((char *)p2, (char **)&p2);
11794 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11795 }
11796
11797 /* When the result would be zero, compare the item indexes. Makes the
11798 * sort stable. */
11799 if (res == 0 && !sortinfo->item_compare_keep_zero)
11800 res = si1->idx > si2->idx ? 1 : -1;
11801
11802 vim_free(tofree1);
11803 vim_free(tofree2);
11804 return res;
11805}
11806
11807 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011808item_compare2(const void *s1, const void *s2)
11809{
11810 sortItem_T *si1, *si2;
11811 int res;
11812 typval_T rettv;
11813 typval_T argv[3];
11814 int dummy;
11815 char_u *func_name;
11816 partial_T *partial = sortinfo->item_compare_partial;
11817
11818 /* shortcut after failure in previous call; compare all items equal */
11819 if (sortinfo->item_compare_func_err)
11820 return 0;
11821
11822 si1 = (sortItem_T *)s1;
11823 si2 = (sortItem_T *)s2;
11824
11825 if (partial == NULL)
11826 func_name = sortinfo->item_compare_func;
11827 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011828 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011829
11830 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11831 * in the copy without changing the original list items. */
11832 copy_tv(&si1->item->li_tv, &argv[0]);
11833 copy_tv(&si2->item->li_tv, &argv[1]);
11834
11835 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020011836 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011837 partial, sortinfo->item_compare_selfdict);
11838 clear_tv(&argv[0]);
11839 clear_tv(&argv[1]);
11840
11841 if (res == FAIL)
11842 res = ITEM_COMPARE_FAIL;
11843 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011844 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011845 if (sortinfo->item_compare_func_err)
11846 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11847 clear_tv(&rettv);
11848
11849 /* When the result would be zero, compare the pointers themselves. Makes
11850 * the sort stable. */
11851 if (res == 0 && !sortinfo->item_compare_keep_zero)
11852 res = si1->idx > si2->idx ? 1 : -1;
11853
11854 return res;
11855}
11856
11857/*
11858 * "sort({list})" function
11859 */
11860 static void
11861do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11862{
11863 list_T *l;
11864 listitem_T *li;
11865 sortItem_T *ptrs;
11866 sortinfo_T *old_sortinfo;
11867 sortinfo_T info;
11868 long len;
11869 long i;
11870
11871 /* Pointer to current info struct used in compare function. Save and
11872 * restore the current one for nested calls. */
11873 old_sortinfo = sortinfo;
11874 sortinfo = &info;
11875
11876 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011877 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011878 else
11879 {
11880 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011881 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011882 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11883 TRUE))
11884 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011885 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011886
11887 len = list_len(l);
11888 if (len <= 1)
11889 goto theend; /* short list sorts pretty quickly */
11890
11891 info.item_compare_ic = FALSE;
11892 info.item_compare_numeric = FALSE;
11893 info.item_compare_numbers = FALSE;
11894#ifdef FEAT_FLOAT
11895 info.item_compare_float = FALSE;
11896#endif
11897 info.item_compare_func = NULL;
11898 info.item_compare_partial = NULL;
11899 info.item_compare_selfdict = NULL;
11900 if (argvars[1].v_type != VAR_UNKNOWN)
11901 {
11902 /* optional second argument: {func} */
11903 if (argvars[1].v_type == VAR_FUNC)
11904 info.item_compare_func = argvars[1].vval.v_string;
11905 else if (argvars[1].v_type == VAR_PARTIAL)
11906 info.item_compare_partial = argvars[1].vval.v_partial;
11907 else
11908 {
11909 int error = FALSE;
11910
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011911 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011912 if (error)
11913 goto theend; /* type error; errmsg already given */
11914 if (i == 1)
11915 info.item_compare_ic = TRUE;
11916 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011917 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011918 else if (i != 0)
11919 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011920 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011921 goto theend;
11922 }
11923 if (info.item_compare_func != NULL)
11924 {
11925 if (*info.item_compare_func == NUL)
11926 {
11927 /* empty string means default sort */
11928 info.item_compare_func = NULL;
11929 }
11930 else if (STRCMP(info.item_compare_func, "n") == 0)
11931 {
11932 info.item_compare_func = NULL;
11933 info.item_compare_numeric = TRUE;
11934 }
11935 else if (STRCMP(info.item_compare_func, "N") == 0)
11936 {
11937 info.item_compare_func = NULL;
11938 info.item_compare_numbers = TRUE;
11939 }
11940#ifdef FEAT_FLOAT
11941 else if (STRCMP(info.item_compare_func, "f") == 0)
11942 {
11943 info.item_compare_func = NULL;
11944 info.item_compare_float = TRUE;
11945 }
11946#endif
11947 else if (STRCMP(info.item_compare_func, "i") == 0)
11948 {
11949 info.item_compare_func = NULL;
11950 info.item_compare_ic = TRUE;
11951 }
11952 }
11953 }
11954
11955 if (argvars[2].v_type != VAR_UNKNOWN)
11956 {
11957 /* optional third argument: {dict} */
11958 if (argvars[2].v_type != VAR_DICT)
11959 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011960 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011961 goto theend;
11962 }
11963 info.item_compare_selfdict = argvars[2].vval.v_dict;
11964 }
11965 }
11966
11967 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011968 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011969 if (ptrs == NULL)
11970 goto theend;
11971
11972 i = 0;
11973 if (sort)
11974 {
11975 /* sort(): ptrs will be the list to sort */
11976 for (li = l->lv_first; li != NULL; li = li->li_next)
11977 {
11978 ptrs[i].item = li;
11979 ptrs[i].idx = i;
11980 ++i;
11981 }
11982
11983 info.item_compare_func_err = FALSE;
11984 info.item_compare_keep_zero = FALSE;
11985 /* test the compare function */
11986 if ((info.item_compare_func != NULL
11987 || info.item_compare_partial != NULL)
11988 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11989 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011990 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011991 else
11992 {
11993 /* Sort the array with item pointers. */
11994 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11995 info.item_compare_func == NULL
11996 && info.item_compare_partial == NULL
11997 ? item_compare : item_compare2);
11998
11999 if (!info.item_compare_func_err)
12000 {
12001 /* Clear the List and append the items in sorted order. */
12002 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12003 l->lv_len = 0;
12004 for (i = 0; i < len; ++i)
12005 list_append(l, ptrs[i].item);
12006 }
12007 }
12008 }
12009 else
12010 {
12011 int (*item_compare_func_ptr)(const void *, const void *);
12012
12013 /* f_uniq(): ptrs will be a stack of items to remove */
12014 info.item_compare_func_err = FALSE;
12015 info.item_compare_keep_zero = TRUE;
12016 item_compare_func_ptr = info.item_compare_func != NULL
12017 || info.item_compare_partial != NULL
12018 ? item_compare2 : item_compare;
12019
12020 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12021 li = li->li_next)
12022 {
12023 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12024 == 0)
12025 ptrs[i++].item = li;
12026 if (info.item_compare_func_err)
12027 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012028 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012029 break;
12030 }
12031 }
12032
12033 if (!info.item_compare_func_err)
12034 {
12035 while (--i >= 0)
12036 {
12037 li = ptrs[i].item->li_next;
12038 ptrs[i].item->li_next = li->li_next;
12039 if (li->li_next != NULL)
12040 li->li_next->li_prev = ptrs[i].item;
12041 else
12042 l->lv_last = ptrs[i].item;
12043 list_fix_watch(l, li);
12044 listitem_free(li);
12045 l->lv_len--;
12046 }
12047 }
12048 }
12049
12050 vim_free(ptrs);
12051 }
12052theend:
12053 sortinfo = old_sortinfo;
12054}
12055
12056/*
12057 * "sort({list})" function
12058 */
12059 static void
12060f_sort(typval_T *argvars, typval_T *rettv)
12061{
12062 do_sort_uniq(argvars, rettv, TRUE);
12063}
12064
12065/*
12066 * "uniq({list})" function
12067 */
12068 static void
12069f_uniq(typval_T *argvars, typval_T *rettv)
12070{
12071 do_sort_uniq(argvars, rettv, FALSE);
12072}
12073
12074/*
12075 * "soundfold({word})" function
12076 */
12077 static void
12078f_soundfold(typval_T *argvars, typval_T *rettv)
12079{
12080 char_u *s;
12081
12082 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012083 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012084#ifdef FEAT_SPELL
12085 rettv->vval.v_string = eval_soundfold(s);
12086#else
12087 rettv->vval.v_string = vim_strsave(s);
12088#endif
12089}
12090
12091/*
12092 * "spellbadword()" function
12093 */
12094 static void
12095f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12096{
12097 char_u *word = (char_u *)"";
12098 hlf_T attr = HLF_COUNT;
12099 int len = 0;
12100
12101 if (rettv_list_alloc(rettv) == FAIL)
12102 return;
12103
12104#ifdef FEAT_SPELL
12105 if (argvars[0].v_type == VAR_UNKNOWN)
12106 {
12107 /* Find the start and length of the badly spelled word. */
12108 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12109 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012110 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012111 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012112 curwin->w_set_curswant = TRUE;
12113 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012114 }
12115 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12116 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012117 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012118 int capcol = -1;
12119
12120 if (str != NULL)
12121 {
12122 /* Check the argument for spelling. */
12123 while (*str != NUL)
12124 {
12125 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12126 if (attr != HLF_COUNT)
12127 {
12128 word = str;
12129 break;
12130 }
12131 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012132 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012133 }
12134 }
12135 }
12136#endif
12137
12138 list_append_string(rettv->vval.v_list, word, len);
12139 list_append_string(rettv->vval.v_list, (char_u *)(
12140 attr == HLF_SPB ? "bad" :
12141 attr == HLF_SPR ? "rare" :
12142 attr == HLF_SPL ? "local" :
12143 attr == HLF_SPC ? "caps" :
12144 ""), -1);
12145}
12146
12147/*
12148 * "spellsuggest()" function
12149 */
12150 static void
12151f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12152{
12153#ifdef FEAT_SPELL
12154 char_u *str;
12155 int typeerr = FALSE;
12156 int maxcount;
12157 garray_T ga;
12158 int i;
12159 listitem_T *li;
12160 int need_capital = FALSE;
12161#endif
12162
12163 if (rettv_list_alloc(rettv) == FAIL)
12164 return;
12165
12166#ifdef FEAT_SPELL
12167 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12168 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012169 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012170 if (argvars[1].v_type != VAR_UNKNOWN)
12171 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012172 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012173 if (maxcount <= 0)
12174 return;
12175 if (argvars[2].v_type != VAR_UNKNOWN)
12176 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012177 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012178 if (typeerr)
12179 return;
12180 }
12181 }
12182 else
12183 maxcount = 25;
12184
12185 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12186
12187 for (i = 0; i < ga.ga_len; ++i)
12188 {
12189 str = ((char_u **)ga.ga_data)[i];
12190
12191 li = listitem_alloc();
12192 if (li == NULL)
12193 vim_free(str);
12194 else
12195 {
12196 li->li_tv.v_type = VAR_STRING;
12197 li->li_tv.v_lock = 0;
12198 li->li_tv.vval.v_string = str;
12199 list_append(rettv->vval.v_list, li);
12200 }
12201 }
12202 ga_clear(&ga);
12203 }
12204#endif
12205}
12206
12207 static void
12208f_split(typval_T *argvars, typval_T *rettv)
12209{
12210 char_u *str;
12211 char_u *end;
12212 char_u *pat = NULL;
12213 regmatch_T regmatch;
12214 char_u patbuf[NUMBUFLEN];
12215 char_u *save_cpo;
12216 int match;
12217 colnr_T col = 0;
12218 int keepempty = FALSE;
12219 int typeerr = FALSE;
12220
12221 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12222 save_cpo = p_cpo;
12223 p_cpo = (char_u *)"";
12224
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012225 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012226 if (argvars[1].v_type != VAR_UNKNOWN)
12227 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012228 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012229 if (pat == NULL)
12230 typeerr = TRUE;
12231 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012232 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012233 }
12234 if (pat == NULL || *pat == NUL)
12235 pat = (char_u *)"[\\x01- ]\\+";
12236
12237 if (rettv_list_alloc(rettv) == FAIL)
12238 return;
12239 if (typeerr)
12240 return;
12241
12242 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12243 if (regmatch.regprog != NULL)
12244 {
12245 regmatch.rm_ic = FALSE;
12246 while (*str != NUL || keepempty)
12247 {
12248 if (*str == NUL)
12249 match = FALSE; /* empty item at the end */
12250 else
12251 match = vim_regexec_nl(&regmatch, str, col);
12252 if (match)
12253 end = regmatch.startp[0];
12254 else
12255 end = str + STRLEN(str);
12256 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12257 && *str != NUL && match && end < regmatch.endp[0]))
12258 {
12259 if (list_append_string(rettv->vval.v_list, str,
12260 (int)(end - str)) == FAIL)
12261 break;
12262 }
12263 if (!match)
12264 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012265 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012266 if (regmatch.endp[0] > str)
12267 col = 0;
12268 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012269 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012270 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012271 str = regmatch.endp[0];
12272 }
12273
12274 vim_regfree(regmatch.regprog);
12275 }
12276
12277 p_cpo = save_cpo;
12278}
12279
12280#ifdef FEAT_FLOAT
12281/*
12282 * "sqrt()" function
12283 */
12284 static void
12285f_sqrt(typval_T *argvars, typval_T *rettv)
12286{
12287 float_T f = 0.0;
12288
12289 rettv->v_type = VAR_FLOAT;
12290 if (get_float_arg(argvars, &f) == OK)
12291 rettv->vval.v_float = sqrt(f);
12292 else
12293 rettv->vval.v_float = 0.0;
12294}
12295
12296/*
12297 * "str2float()" function
12298 */
12299 static void
12300f_str2float(typval_T *argvars, typval_T *rettv)
12301{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012302 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012303 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012304
Bram Moolenaar08243d22017-01-10 16:12:29 +010012305 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012306 p = skipwhite(p + 1);
12307 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012308 if (isneg)
12309 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012310 rettv->v_type = VAR_FLOAT;
12311}
12312#endif
12313
12314/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012315 * "str2list()" function
12316 */
12317 static void
12318f_str2list(typval_T *argvars, typval_T *rettv)
12319{
12320 char_u *p;
12321 int utf8 = FALSE;
12322
12323 if (rettv_list_alloc(rettv) == FAIL)
12324 return;
12325
12326 if (argvars[1].v_type != VAR_UNKNOWN)
12327 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12328
12329 p = tv_get_string(&argvars[0]);
12330
12331 if (has_mbyte || utf8)
12332 {
12333 int (*ptr2len)(char_u *);
12334 int (*ptr2char)(char_u *);
12335
12336 if (utf8 || enc_utf8)
12337 {
12338 ptr2len = utf_ptr2len;
12339 ptr2char = utf_ptr2char;
12340 }
12341 else
12342 {
12343 ptr2len = mb_ptr2len;
12344 ptr2char = mb_ptr2char;
12345 }
12346
12347 for ( ; *p != NUL; p += (*ptr2len)(p))
12348 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
12349 }
12350 else
12351 for ( ; *p != NUL; ++p)
12352 list_append_number(rettv->vval.v_list, *p);
12353}
12354
12355/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012356 * "str2nr()" function
12357 */
12358 static void
12359f_str2nr(typval_T *argvars, typval_T *rettv)
12360{
12361 int base = 10;
12362 char_u *p;
12363 varnumber_T n;
12364 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012365 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012366
12367 if (argvars[1].v_type != VAR_UNKNOWN)
12368 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012369 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012370 if (base != 2 && base != 8 && base != 10 && base != 16)
12371 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012372 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012373 return;
12374 }
12375 }
12376
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012377 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012378 isneg = (*p == '-');
12379 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012380 p = skipwhite(p + 1);
12381 switch (base)
12382 {
12383 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12384 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12385 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12386 default: what = 0;
12387 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020012388 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
12389 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010012390 if (isneg)
12391 rettv->vval.v_number = -n;
12392 else
12393 rettv->vval.v_number = n;
12394
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012395}
12396
12397#ifdef HAVE_STRFTIME
12398/*
12399 * "strftime({format}[, {time}])" function
12400 */
12401 static void
12402f_strftime(typval_T *argvars, typval_T *rettv)
12403{
12404 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020012405 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012406 struct tm *curtime;
12407 time_t seconds;
12408 char_u *p;
12409
12410 rettv->v_type = VAR_STRING;
12411
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012412 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012413 if (argvars[1].v_type == VAR_UNKNOWN)
12414 seconds = time(NULL);
12415 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012416 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020012417 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012418 /* MSVC returns NULL for an invalid value of seconds. */
12419 if (curtime == NULL)
12420 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12421 else
12422 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012423 vimconv_T conv;
12424 char_u *enc;
12425
12426 conv.vc_type = CONV_NONE;
12427 enc = enc_locale();
12428 convert_setup(&conv, p_enc, enc);
12429 if (conv.vc_type != CONV_NONE)
12430 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012431 if (p != NULL)
12432 (void)strftime((char *)result_buf, sizeof(result_buf),
12433 (char *)p, curtime);
12434 else
12435 result_buf[0] = NUL;
12436
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012437 if (conv.vc_type != CONV_NONE)
12438 vim_free(p);
12439 convert_setup(&conv, enc, p_enc);
12440 if (conv.vc_type != CONV_NONE)
12441 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12442 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012443 rettv->vval.v_string = vim_strsave(result_buf);
12444
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012445 /* Release conversion descriptors */
12446 convert_setup(&conv, NULL, NULL);
12447 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012448 }
12449}
12450#endif
12451
12452/*
12453 * "strgetchar()" function
12454 */
12455 static void
12456f_strgetchar(typval_T *argvars, typval_T *rettv)
12457{
12458 char_u *str;
12459 int len;
12460 int error = FALSE;
12461 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012462 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012463
12464 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012465 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012466 if (str == NULL)
12467 return;
12468 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012469 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012470 if (error)
12471 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012472
Bram Moolenaar13505972019-01-24 15:04:48 +010012473 while (charidx >= 0 && byteidx < len)
12474 {
12475 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012476 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012477 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12478 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012479 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012480 --charidx;
12481 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012482 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012483}
12484
12485/*
12486 * "stridx()" function
12487 */
12488 static void
12489f_stridx(typval_T *argvars, typval_T *rettv)
12490{
12491 char_u buf[NUMBUFLEN];
12492 char_u *needle;
12493 char_u *haystack;
12494 char_u *save_haystack;
12495 char_u *pos;
12496 int start_idx;
12497
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012498 needle = tv_get_string_chk(&argvars[1]);
12499 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012500 rettv->vval.v_number = -1;
12501 if (needle == NULL || haystack == NULL)
12502 return; /* type error; errmsg already given */
12503
12504 if (argvars[2].v_type != VAR_UNKNOWN)
12505 {
12506 int error = FALSE;
12507
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012508 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012509 if (error || start_idx >= (int)STRLEN(haystack))
12510 return;
12511 if (start_idx >= 0)
12512 haystack += start_idx;
12513 }
12514
12515 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12516 if (pos != NULL)
12517 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12518}
12519
12520/*
12521 * "string()" function
12522 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012523 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012524f_string(typval_T *argvars, typval_T *rettv)
12525{
12526 char_u *tofree;
12527 char_u numbuf[NUMBUFLEN];
12528
12529 rettv->v_type = VAR_STRING;
12530 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12531 get_copyID());
12532 /* Make a copy if we have a value but it's not in allocated memory. */
12533 if (rettv->vval.v_string != NULL && tofree == NULL)
12534 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12535}
12536
12537/*
12538 * "strlen()" function
12539 */
12540 static void
12541f_strlen(typval_T *argvars, typval_T *rettv)
12542{
12543 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012544 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012545}
12546
12547/*
12548 * "strchars()" function
12549 */
12550 static void
12551f_strchars(typval_T *argvars, typval_T *rettv)
12552{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012553 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012554 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012555 varnumber_T len = 0;
12556 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012557
12558 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012559 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012560 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012561 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012562 else
12563 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012564 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12565 while (*s != NUL)
12566 {
12567 func_mb_ptr2char_adv(&s);
12568 ++len;
12569 }
12570 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012571 }
12572}
12573
12574/*
12575 * "strdisplaywidth()" function
12576 */
12577 static void
12578f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12579{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012580 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012581 int col = 0;
12582
12583 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012584 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012585
12586 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12587}
12588
12589/*
12590 * "strwidth()" function
12591 */
12592 static void
12593f_strwidth(typval_T *argvars, typval_T *rettv)
12594{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012595 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012596
Bram Moolenaar13505972019-01-24 15:04:48 +010012597 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012598}
12599
12600/*
12601 * "strcharpart()" function
12602 */
12603 static void
12604f_strcharpart(typval_T *argvars, typval_T *rettv)
12605{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012606 char_u *p;
12607 int nchar;
12608 int nbyte = 0;
12609 int charlen;
12610 int len = 0;
12611 int slen;
12612 int error = FALSE;
12613
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012614 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012615 slen = (int)STRLEN(p);
12616
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012617 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012618 if (!error)
12619 {
12620 if (nchar > 0)
12621 while (nchar > 0 && nbyte < slen)
12622 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012623 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012624 --nchar;
12625 }
12626 else
12627 nbyte = nchar;
12628 if (argvars[2].v_type != VAR_UNKNOWN)
12629 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012630 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012631 while (charlen > 0 && nbyte + len < slen)
12632 {
12633 int off = nbyte + len;
12634
12635 if (off < 0)
12636 len += 1;
12637 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012638 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012639 --charlen;
12640 }
12641 }
12642 else
12643 len = slen - nbyte; /* default: all bytes that are available. */
12644 }
12645
12646 /*
12647 * Only return the overlap between the specified part and the actual
12648 * string.
12649 */
12650 if (nbyte < 0)
12651 {
12652 len += nbyte;
12653 nbyte = 0;
12654 }
12655 else if (nbyte > slen)
12656 nbyte = slen;
12657 if (len < 0)
12658 len = 0;
12659 else if (nbyte + len > slen)
12660 len = slen - nbyte;
12661
12662 rettv->v_type = VAR_STRING;
12663 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012664}
12665
12666/*
12667 * "strpart()" function
12668 */
12669 static void
12670f_strpart(typval_T *argvars, typval_T *rettv)
12671{
12672 char_u *p;
12673 int n;
12674 int len;
12675 int slen;
12676 int error = FALSE;
12677
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012678 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012679 slen = (int)STRLEN(p);
12680
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012681 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012682 if (error)
12683 len = 0;
12684 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012685 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012686 else
12687 len = slen - n; /* default len: all bytes that are available. */
12688
12689 /*
12690 * Only return the overlap between the specified part and the actual
12691 * string.
12692 */
12693 if (n < 0)
12694 {
12695 len += n;
12696 n = 0;
12697 }
12698 else if (n > slen)
12699 n = slen;
12700 if (len < 0)
12701 len = 0;
12702 else if (n + len > slen)
12703 len = slen - n;
12704
12705 rettv->v_type = VAR_STRING;
12706 rettv->vval.v_string = vim_strnsave(p + n, len);
12707}
12708
12709/*
12710 * "strridx()" function
12711 */
12712 static void
12713f_strridx(typval_T *argvars, typval_T *rettv)
12714{
12715 char_u buf[NUMBUFLEN];
12716 char_u *needle;
12717 char_u *haystack;
12718 char_u *rest;
12719 char_u *lastmatch = NULL;
12720 int haystack_len, end_idx;
12721
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012722 needle = tv_get_string_chk(&argvars[1]);
12723 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012724
12725 rettv->vval.v_number = -1;
12726 if (needle == NULL || haystack == NULL)
12727 return; /* type error; errmsg already given */
12728
12729 haystack_len = (int)STRLEN(haystack);
12730 if (argvars[2].v_type != VAR_UNKNOWN)
12731 {
12732 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012733 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012734 if (end_idx < 0)
12735 return; /* can never find a match */
12736 }
12737 else
12738 end_idx = haystack_len;
12739
12740 if (*needle == NUL)
12741 {
12742 /* Empty string matches past the end. */
12743 lastmatch = haystack + end_idx;
12744 }
12745 else
12746 {
12747 for (rest = haystack; *rest != '\0'; ++rest)
12748 {
12749 rest = (char_u *)strstr((char *)rest, (char *)needle);
12750 if (rest == NULL || rest > haystack + end_idx)
12751 break;
12752 lastmatch = rest;
12753 }
12754 }
12755
12756 if (lastmatch == NULL)
12757 rettv->vval.v_number = -1;
12758 else
12759 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12760}
12761
12762/*
12763 * "strtrans()" function
12764 */
12765 static void
12766f_strtrans(typval_T *argvars, typval_T *rettv)
12767{
12768 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012769 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012770}
12771
12772/*
12773 * "submatch()" function
12774 */
12775 static void
12776f_submatch(typval_T *argvars, typval_T *rettv)
12777{
12778 int error = FALSE;
12779 int no;
12780 int retList = 0;
12781
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012782 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012783 if (error)
12784 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012785 if (no < 0 || no >= NSUBEXP)
12786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012787 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012788 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012789 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012790 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012791 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012792 if (error)
12793 return;
12794
12795 if (retList == 0)
12796 {
12797 rettv->v_type = VAR_STRING;
12798 rettv->vval.v_string = reg_submatch(no);
12799 }
12800 else
12801 {
12802 rettv->v_type = VAR_LIST;
12803 rettv->vval.v_list = reg_submatch_list(no);
12804 }
12805}
12806
12807/*
12808 * "substitute()" function
12809 */
12810 static void
12811f_substitute(typval_T *argvars, typval_T *rettv)
12812{
12813 char_u patbuf[NUMBUFLEN];
12814 char_u subbuf[NUMBUFLEN];
12815 char_u flagsbuf[NUMBUFLEN];
12816
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012817 char_u *str = tv_get_string_chk(&argvars[0]);
12818 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012819 char_u *sub = NULL;
12820 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012821 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012822
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012823 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12824 expr = &argvars[2];
12825 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012826 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012827
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012828 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012829 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12830 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012831 rettv->vval.v_string = NULL;
12832 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012833 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012834}
12835
12836/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012837 * "swapinfo(swap_filename)" function
12838 */
12839 static void
12840f_swapinfo(typval_T *argvars, typval_T *rettv)
12841{
12842 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012843 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012844}
12845
12846/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012847 * "swapname(expr)" function
12848 */
12849 static void
12850f_swapname(typval_T *argvars, typval_T *rettv)
12851{
12852 buf_T *buf;
12853
12854 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010012855 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020012856 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12857 || buf->b_ml.ml_mfp->mf_fname == NULL)
12858 rettv->vval.v_string = NULL;
12859 else
12860 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12861}
12862
12863/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012864 * "synID(lnum, col, trans)" function
12865 */
12866 static void
12867f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12868{
12869 int id = 0;
12870#ifdef FEAT_SYN_HL
12871 linenr_T lnum;
12872 colnr_T col;
12873 int trans;
12874 int transerr = FALSE;
12875
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012876 lnum = tv_get_lnum(argvars); /* -1 on type error */
12877 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12878 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012879
12880 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12881 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12882 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12883#endif
12884
12885 rettv->vval.v_number = id;
12886}
12887
12888/*
12889 * "synIDattr(id, what [, mode])" function
12890 */
12891 static void
12892f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12893{
12894 char_u *p = NULL;
12895#ifdef FEAT_SYN_HL
12896 int id;
12897 char_u *what;
12898 char_u *mode;
12899 char_u modebuf[NUMBUFLEN];
12900 int modec;
12901
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012902 id = (int)tv_get_number(&argvars[0]);
12903 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012904 if (argvars[2].v_type != VAR_UNKNOWN)
12905 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012906 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012907 modec = TOLOWER_ASC(mode[0]);
12908 if (modec != 't' && modec != 'c' && modec != 'g')
12909 modec = 0; /* replace invalid with current */
12910 }
12911 else
12912 {
12913#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12914 if (USE_24BIT)
12915 modec = 'g';
12916 else
12917#endif
12918 if (t_colors > 1)
12919 modec = 'c';
12920 else
12921 modec = 't';
12922 }
12923
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012924 switch (TOLOWER_ASC(what[0]))
12925 {
12926 case 'b':
12927 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12928 p = highlight_color(id, what, modec);
12929 else /* bold */
12930 p = highlight_has_attr(id, HL_BOLD, modec);
12931 break;
12932
12933 case 'f': /* fg[#] or font */
12934 p = highlight_color(id, what, modec);
12935 break;
12936
12937 case 'i':
12938 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12939 p = highlight_has_attr(id, HL_INVERSE, modec);
12940 else /* italic */
12941 p = highlight_has_attr(id, HL_ITALIC, modec);
12942 break;
12943
12944 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012945 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012946 break;
12947
12948 case 'r': /* reverse */
12949 p = highlight_has_attr(id, HL_INVERSE, modec);
12950 break;
12951
12952 case 's':
12953 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12954 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012955 /* strikeout */
12956 else if (TOLOWER_ASC(what[1]) == 't' &&
12957 TOLOWER_ASC(what[2]) == 'r')
12958 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012959 else /* standout */
12960 p = highlight_has_attr(id, HL_STANDOUT, modec);
12961 break;
12962
12963 case 'u':
12964 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12965 /* underline */
12966 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12967 else
12968 /* undercurl */
12969 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12970 break;
12971 }
12972
12973 if (p != NULL)
12974 p = vim_strsave(p);
12975#endif
12976 rettv->v_type = VAR_STRING;
12977 rettv->vval.v_string = p;
12978}
12979
12980/*
12981 * "synIDtrans(id)" function
12982 */
12983 static void
12984f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12985{
12986 int id;
12987
12988#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012989 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012990
12991 if (id > 0)
12992 id = syn_get_final_id(id);
12993 else
12994#endif
12995 id = 0;
12996
12997 rettv->vval.v_number = id;
12998}
12999
13000/*
13001 * "synconcealed(lnum, col)" function
13002 */
13003 static void
13004f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13005{
13006#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13007 linenr_T lnum;
13008 colnr_T col;
13009 int syntax_flags = 0;
13010 int cchar;
13011 int matchid = 0;
13012 char_u str[NUMBUFLEN];
13013#endif
13014
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013015 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013016
13017#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013018 lnum = tv_get_lnum(argvars); /* -1 on type error */
13019 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013020
13021 vim_memset(str, NUL, sizeof(str));
13022
13023 if (rettv_list_alloc(rettv) != FAIL)
13024 {
13025 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13026 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13027 && curwin->w_p_cole > 0)
13028 {
13029 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13030 syntax_flags = get_syntax_info(&matchid);
13031
13032 /* get the conceal character */
13033 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13034 {
13035 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013036 if (cchar == NUL && curwin->w_p_cole == 1)
13037 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013038 if (cchar != NUL)
13039 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013040 if (has_mbyte)
13041 (*mb_char2bytes)(cchar, str);
13042 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013043 str[0] = cchar;
13044 }
13045 }
13046 }
13047
13048 list_append_number(rettv->vval.v_list,
13049 (syntax_flags & HL_CONCEAL) != 0);
13050 /* -1 to auto-determine strlen */
13051 list_append_string(rettv->vval.v_list, str, -1);
13052 list_append_number(rettv->vval.v_list, matchid);
13053 }
13054#endif
13055}
13056
13057/*
13058 * "synstack(lnum, col)" function
13059 */
13060 static void
13061f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13062{
13063#ifdef FEAT_SYN_HL
13064 linenr_T lnum;
13065 colnr_T col;
13066 int i;
13067 int id;
13068#endif
13069
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013070 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013071
13072#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013073 lnum = tv_get_lnum(argvars); /* -1 on type error */
13074 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013075
13076 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13077 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13078 && rettv_list_alloc(rettv) != FAIL)
13079 {
13080 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13081 for (i = 0; ; ++i)
13082 {
13083 id = syn_get_stack_item(i);
13084 if (id < 0)
13085 break;
13086 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13087 break;
13088 }
13089 }
13090#endif
13091}
13092
13093 static void
13094get_cmd_output_as_rettv(
13095 typval_T *argvars,
13096 typval_T *rettv,
13097 int retlist)
13098{
13099 char_u *res = NULL;
13100 char_u *p;
13101 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013102 int err = FALSE;
13103 FILE *fd;
13104 list_T *list = NULL;
13105 int flags = SHELL_SILENT;
13106
13107 rettv->v_type = VAR_STRING;
13108 rettv->vval.v_string = NULL;
13109 if (check_restricted() || check_secure())
13110 goto errret;
13111
13112 if (argvars[1].v_type != VAR_UNKNOWN)
13113 {
13114 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013115 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013116 * command.
13117 */
13118 if ((infile = vim_tempname('i', TRUE)) == NULL)
13119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013120 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013121 goto errret;
13122 }
13123
13124 fd = mch_fopen((char *)infile, WRITEBIN);
13125 if (fd == NULL)
13126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013127 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013128 goto errret;
13129 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013130 if (argvars[1].v_type == VAR_NUMBER)
13131 {
13132 linenr_T lnum;
13133 buf_T *buf;
13134
13135 buf = buflist_findnr(argvars[1].vval.v_number);
13136 if (buf == NULL)
13137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013138 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013139 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013140 goto errret;
13141 }
13142
13143 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13144 {
13145 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13146 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13147 {
13148 err = TRUE;
13149 break;
13150 }
13151 if (putc(NL, fd) == EOF)
13152 {
13153 err = TRUE;
13154 break;
13155 }
13156 }
13157 }
13158 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013159 {
13160 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13161 err = TRUE;
13162 }
13163 else
13164 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013165 size_t len;
13166 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013167
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013168 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013169 if (p == NULL)
13170 {
13171 fclose(fd);
13172 goto errret; /* type error; errmsg already given */
13173 }
13174 len = STRLEN(p);
13175 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13176 err = TRUE;
13177 }
13178 if (fclose(fd) != 0)
13179 err = TRUE;
13180 if (err)
13181 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013182 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013183 goto errret;
13184 }
13185 }
13186
13187 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13188 * echoes typeahead, that messes up the display. */
13189 if (!msg_silent)
13190 flags += SHELL_COOKED;
13191
13192 if (retlist)
13193 {
13194 int len;
13195 listitem_T *li;
13196 char_u *s = NULL;
13197 char_u *start;
13198 char_u *end;
13199 int i;
13200
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013201 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013202 if (res == NULL)
13203 goto errret;
13204
13205 list = list_alloc();
13206 if (list == NULL)
13207 goto errret;
13208
13209 for (i = 0; i < len; ++i)
13210 {
13211 start = res + i;
13212 while (i < len && res[i] != NL)
13213 ++i;
13214 end = res + i;
13215
Bram Moolenaar964b3742019-05-24 18:54:09 +020013216 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013217 if (s == NULL)
13218 goto errret;
13219
13220 for (p = s; start < end; ++p, ++start)
13221 *p = *start == NUL ? NL : *start;
13222 *p = NUL;
13223
13224 li = listitem_alloc();
13225 if (li == NULL)
13226 {
13227 vim_free(s);
13228 goto errret;
13229 }
13230 li->li_tv.v_type = VAR_STRING;
13231 li->li_tv.v_lock = 0;
13232 li->li_tv.vval.v_string = s;
13233 list_append(list, li);
13234 }
13235
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013236 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013237 list = NULL;
13238 }
13239 else
13240 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013241 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013242#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013243 /* translate <CR><NL> into <NL> */
13244 if (res != NULL)
13245 {
13246 char_u *s, *d;
13247
13248 d = res;
13249 for (s = res; *s; ++s)
13250 {
13251 if (s[0] == CAR && s[1] == NL)
13252 ++s;
13253 *d++ = *s;
13254 }
13255 *d = NUL;
13256 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013257#endif
13258 rettv->vval.v_string = res;
13259 res = NULL;
13260 }
13261
13262errret:
13263 if (infile != NULL)
13264 {
13265 mch_remove(infile);
13266 vim_free(infile);
13267 }
13268 if (res != NULL)
13269 vim_free(res);
13270 if (list != NULL)
13271 list_free(list);
13272}
13273
13274/*
13275 * "system()" function
13276 */
13277 static void
13278f_system(typval_T *argvars, typval_T *rettv)
13279{
13280 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13281}
13282
13283/*
13284 * "systemlist()" function
13285 */
13286 static void
13287f_systemlist(typval_T *argvars, typval_T *rettv)
13288{
13289 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13290}
13291
13292/*
13293 * "tabpagebuflist()" function
13294 */
13295 static void
13296f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13297{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013298 tabpage_T *tp;
13299 win_T *wp = NULL;
13300
13301 if (argvars[0].v_type == VAR_UNKNOWN)
13302 wp = firstwin;
13303 else
13304 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013305 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013306 if (tp != NULL)
13307 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13308 }
13309 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13310 {
13311 for (; wp != NULL; wp = wp->w_next)
13312 if (list_append_number(rettv->vval.v_list,
13313 wp->w_buffer->b_fnum) == FAIL)
13314 break;
13315 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013316}
13317
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013318/*
13319 * "tabpagenr()" function
13320 */
13321 static void
13322f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13323{
13324 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013325 char_u *arg;
13326
13327 if (argvars[0].v_type != VAR_UNKNOWN)
13328 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013329 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013330 nr = 0;
13331 if (arg != NULL)
13332 {
13333 if (STRCMP(arg, "$") == 0)
13334 nr = tabpage_index(NULL) - 1;
13335 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013336 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013337 }
13338 }
13339 else
13340 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013341 rettv->vval.v_number = nr;
13342}
13343
13344
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013345/*
13346 * Common code for tabpagewinnr() and winnr().
13347 */
13348 static int
13349get_winnr(tabpage_T *tp, typval_T *argvar)
13350{
13351 win_T *twin;
13352 int nr = 1;
13353 win_T *wp;
13354 char_u *arg;
13355
13356 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13357 if (argvar->v_type != VAR_UNKNOWN)
13358 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013359 int invalid_arg = FALSE;
13360
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013361 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013362 if (arg == NULL)
13363 nr = 0; /* type error; errmsg already given */
13364 else if (STRCMP(arg, "$") == 0)
13365 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13366 else if (STRCMP(arg, "#") == 0)
13367 {
13368 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13369 if (twin == NULL)
13370 nr = 0;
13371 }
13372 else
13373 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013374 long count;
13375 char_u *endp;
13376
13377 // Extract the window count (if specified). e.g. winnr('3j')
13378 count = strtol((char *)arg, (char **)&endp, 10);
13379 if (count <= 0)
13380 count = 1; // if count is not specified, default to 1
13381 if (endp != NULL && *endp != '\0')
13382 {
13383 if (STRCMP(endp, "j") == 0)
13384 twin = win_vert_neighbor(tp, twin, FALSE, count);
13385 else if (STRCMP(endp, "k") == 0)
13386 twin = win_vert_neighbor(tp, twin, TRUE, count);
13387 else if (STRCMP(endp, "h") == 0)
13388 twin = win_horz_neighbor(tp, twin, TRUE, count);
13389 else if (STRCMP(endp, "l") == 0)
13390 twin = win_horz_neighbor(tp, twin, FALSE, count);
13391 else
13392 invalid_arg = TRUE;
13393 }
13394 else
13395 invalid_arg = TRUE;
13396 }
13397
13398 if (invalid_arg)
13399 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013400 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013401 nr = 0;
13402 }
13403 }
13404
13405 if (nr > 0)
13406 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13407 wp != twin; wp = wp->w_next)
13408 {
13409 if (wp == NULL)
13410 {
13411 /* didn't find it in this tabpage */
13412 nr = 0;
13413 break;
13414 }
13415 ++nr;
13416 }
13417 return nr;
13418}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013419
13420/*
13421 * "tabpagewinnr()" function
13422 */
13423 static void
13424f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13425{
13426 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013427 tabpage_T *tp;
13428
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013429 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013430 if (tp == NULL)
13431 nr = 0;
13432 else
13433 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013434 rettv->vval.v_number = nr;
13435}
13436
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013437/*
13438 * "tagfiles()" function
13439 */
13440 static void
13441f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13442{
13443 char_u *fname;
13444 tagname_T tn;
13445 int first;
13446
13447 if (rettv_list_alloc(rettv) == FAIL)
13448 return;
13449 fname = alloc(MAXPATHL);
13450 if (fname == NULL)
13451 return;
13452
13453 for (first = TRUE; ; first = FALSE)
13454 if (get_tagfname(&tn, first, fname) == FAIL
13455 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13456 break;
13457 tagname_free(&tn);
13458 vim_free(fname);
13459}
13460
13461/*
13462 * "taglist()" function
13463 */
13464 static void
13465f_taglist(typval_T *argvars, typval_T *rettv)
13466{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013467 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013468 char_u *tag_pattern;
13469
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013470 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013471
13472 rettv->vval.v_number = FALSE;
13473 if (*tag_pattern == NUL)
13474 return;
13475
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013476 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013477 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013478 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013479 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013480}
13481
13482/*
13483 * "tempname()" function
13484 */
13485 static void
13486f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13487{
13488 static int x = 'A';
13489
13490 rettv->v_type = VAR_STRING;
13491 rettv->vval.v_string = vim_tempname(x, FALSE);
13492
13493 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13494 * names. Skip 'I' and 'O', they are used for shell redirection. */
13495 do
13496 {
13497 if (x == 'Z')
13498 x = '0';
13499 else if (x == '9')
13500 x = 'A';
13501 else
13502 {
13503#ifdef EBCDIC
13504 if (x == 'I')
13505 x = 'J';
13506 else if (x == 'R')
13507 x = 'S';
13508 else
13509#endif
13510 ++x;
13511 }
13512 } while (x == 'I' || x == 'O');
13513}
13514
13515#ifdef FEAT_FLOAT
13516/*
13517 * "tan()" function
13518 */
13519 static void
13520f_tan(typval_T *argvars, typval_T *rettv)
13521{
13522 float_T f = 0.0;
13523
13524 rettv->v_type = VAR_FLOAT;
13525 if (get_float_arg(argvars, &f) == OK)
13526 rettv->vval.v_float = tan(f);
13527 else
13528 rettv->vval.v_float = 0.0;
13529}
13530
13531/*
13532 * "tanh()" function
13533 */
13534 static void
13535f_tanh(typval_T *argvars, typval_T *rettv)
13536{
13537 float_T f = 0.0;
13538
13539 rettv->v_type = VAR_FLOAT;
13540 if (get_float_arg(argvars, &f) == OK)
13541 rettv->vval.v_float = tanh(f);
13542 else
13543 rettv->vval.v_float = 0.0;
13544}
13545#endif
13546
13547/*
13548 * "test_alloc_fail(id, countdown, repeat)" function
13549 */
13550 static void
13551f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13552{
13553 if (argvars[0].v_type != VAR_NUMBER
13554 || argvars[0].vval.v_number <= 0
13555 || argvars[1].v_type != VAR_NUMBER
13556 || argvars[1].vval.v_number < 0
13557 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013558 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013559 else
13560 {
13561 alloc_fail_id = argvars[0].vval.v_number;
13562 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013563 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013564 alloc_fail_countdown = argvars[1].vval.v_number;
13565 alloc_fail_repeat = argvars[2].vval.v_number;
13566 did_outofmem_msg = FALSE;
13567 }
13568}
13569
13570/*
13571 * "test_autochdir()"
13572 */
13573 static void
13574f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13575{
13576#if defined(FEAT_AUTOCHDIR)
13577 test_autochdir = TRUE;
13578#endif
13579}
13580
13581/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013582 * "test_feedinput()"
13583 */
13584 static void
13585f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13586{
13587#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013588 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013589
13590 if (val != NULL)
13591 {
13592 trash_input_buf();
13593 add_to_input_buf_csi(val, (int)STRLEN(val));
13594 }
13595#endif
13596}
13597
13598/*
Bram Moolenaareda65222019-05-16 20:29:44 +020013599 * "test_getvalue({name})" function
13600 */
13601 static void
13602f_test_getvalue(typval_T *argvars, typval_T *rettv)
13603{
13604 if (argvars[0].v_type != VAR_STRING)
13605 emsg(_(e_invarg));
13606 else
13607 {
13608 char_u *name = tv_get_string(&argvars[0]);
13609
13610 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
13611 rettv->vval.v_number = need_fileinfo;
13612 else
13613 semsg(_(e_invarg2), name);
13614 }
13615}
13616
13617/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013618 * "test_option_not_set({name})" function
13619 */
13620 static void
13621f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13622{
13623 char_u *name = (char_u *)"";
13624
13625 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013626 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013627 else
13628 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013629 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013630 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013631 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013632 }
13633}
13634
13635/*
13636 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013637 */
13638 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013639f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013640{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013641 char_u *name = (char_u *)"";
13642 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013643 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013644
13645 if (argvars[0].v_type != VAR_STRING
13646 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013647 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013648 else
13649 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013650 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013651 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013652
13653 if (STRCMP(name, (char_u *)"redraw") == 0)
13654 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013655 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13656 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013657 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13658 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013659 else if (STRCMP(name, (char_u *)"starting") == 0)
13660 {
13661 if (val)
13662 {
13663 if (save_starting < 0)
13664 save_starting = starting;
13665 starting = 0;
13666 }
13667 else
13668 {
13669 starting = save_starting;
13670 save_starting = -1;
13671 }
13672 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013673 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13674 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013675 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
13676 no_query_mouse_for_testing = val;
Bram Moolenaaradc67142019-06-22 01:40:42 +020013677 else if (STRCMP(name, (char_u *)"no_wait_return") == 0)
13678 no_wait_return = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013679 else if (STRCMP(name, (char_u *)"ALL") == 0)
13680 {
13681 disable_char_avail_for_testing = FALSE;
13682 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013683 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013684 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013685 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013686 if (save_starting >= 0)
13687 {
13688 starting = save_starting;
13689 save_starting = -1;
13690 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013691 }
13692 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013693 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013694 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013695}
13696
13697/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010013698 * "test_refcount({expr})" function
13699 */
13700 static void
13701f_test_refcount(typval_T *argvars, typval_T *rettv)
13702{
13703 int retval = -1;
13704
13705 switch (argvars[0].v_type)
13706 {
13707 case VAR_UNKNOWN:
13708 case VAR_NUMBER:
13709 case VAR_FLOAT:
13710 case VAR_SPECIAL:
13711 case VAR_STRING:
13712 break;
13713 case VAR_JOB:
13714#ifdef FEAT_JOB_CHANNEL
13715 if (argvars[0].vval.v_job != NULL)
13716 retval = argvars[0].vval.v_job->jv_refcount - 1;
13717#endif
13718 break;
13719 case VAR_CHANNEL:
13720#ifdef FEAT_JOB_CHANNEL
13721 if (argvars[0].vval.v_channel != NULL)
13722 retval = argvars[0].vval.v_channel->ch_refcount - 1;
13723#endif
13724 break;
13725 case VAR_FUNC:
13726 if (argvars[0].vval.v_string != NULL)
13727 {
13728 ufunc_T *fp;
13729
13730 fp = find_func(argvars[0].vval.v_string);
13731 if (fp != NULL)
13732 retval = fp->uf_refcount;
13733 }
13734 break;
13735 case VAR_PARTIAL:
13736 if (argvars[0].vval.v_partial != NULL)
13737 retval = argvars[0].vval.v_partial->pt_refcount - 1;
13738 break;
13739 case VAR_BLOB:
13740 if (argvars[0].vval.v_blob != NULL)
13741 retval = argvars[0].vval.v_blob->bv_refcount - 1;
13742 break;
13743 case VAR_LIST:
13744 if (argvars[0].vval.v_list != NULL)
13745 retval = argvars[0].vval.v_list->lv_refcount - 1;
13746 break;
13747 case VAR_DICT:
13748 if (argvars[0].vval.v_dict != NULL)
13749 retval = argvars[0].vval.v_dict->dv_refcount - 1;
13750 break;
13751 }
13752
13753 rettv->v_type = VAR_NUMBER;
13754 rettv->vval.v_number = retval;
13755
13756}
13757
13758/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013759 * "test_garbagecollect_now()" function
13760 */
13761 static void
13762f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13763{
13764 /* This is dangerous, any Lists and Dicts used internally may be freed
13765 * while still in use. */
13766 garbage_collect(TRUE);
13767}
13768
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013769/*
Bram Moolenaaradc67142019-06-22 01:40:42 +020013770 * "test_garbagecollect_soon()" function
13771 */
13772 static void
13773f_test_garbagecollect_soon(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13774{
13775 may_garbage_collect = TRUE;
13776}
13777
13778/*
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013779 * "test_ignore_error()" function
13780 */
13781 static void
13782f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13783{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013784 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013785}
13786
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013787 static void
13788f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13789{
13790 rettv->v_type = VAR_BLOB;
13791 rettv->vval.v_blob = NULL;
13792}
13793
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013794#ifdef FEAT_JOB_CHANNEL
13795 static void
13796f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13797{
13798 rettv->v_type = VAR_CHANNEL;
13799 rettv->vval.v_channel = NULL;
13800}
13801#endif
13802
13803 static void
13804f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13805{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013806 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013807}
13808
13809#ifdef FEAT_JOB_CHANNEL
13810 static void
13811f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13812{
13813 rettv->v_type = VAR_JOB;
13814 rettv->vval.v_job = NULL;
13815}
13816#endif
13817
13818 static void
13819f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13820{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013821 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013822}
13823
13824 static void
13825f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13826{
13827 rettv->v_type = VAR_PARTIAL;
13828 rettv->vval.v_partial = NULL;
13829}
13830
13831 static void
13832f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13833{
13834 rettv->v_type = VAR_STRING;
13835 rettv->vval.v_string = NULL;
13836}
13837
Bram Moolenaarab186732018-09-14 21:27:06 +020013838#ifdef FEAT_GUI
13839 static void
13840f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13841{
13842 char_u *which;
13843 long value;
13844 int dragging;
13845 scrollbar_T *sb = NULL;
13846
13847 if (argvars[0].v_type != VAR_STRING
13848 || (argvars[1].v_type) != VAR_NUMBER
13849 || (argvars[2].v_type) != VAR_NUMBER)
13850 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013851 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013852 return;
13853 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013854 which = tv_get_string(&argvars[0]);
13855 value = tv_get_number(&argvars[1]);
13856 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013857
13858 if (STRCMP(which, "left") == 0)
13859 sb = &curwin->w_scrollbars[SBAR_LEFT];
13860 else if (STRCMP(which, "right") == 0)
13861 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13862 else if (STRCMP(which, "hor") == 0)
13863 sb = &gui.bottom_sbar;
13864 if (sb == NULL)
13865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013866 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020013867 return;
13868 }
13869 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013870# ifndef USE_ON_FLY_SCROLL
13871 // need to loop through normal_cmd() to handle the scroll events
13872 exec_normal(FALSE, TRUE, FALSE);
13873# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013874}
13875#endif
13876
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013877#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013878 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013879f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
13880{
13881 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
13882 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
13883}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013884#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013885
13886 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013887f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13888{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013889 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013890}
13891
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013892/*
13893 * Get a callback from "arg". It can be a Funcref or a function name.
13894 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013895 * "cb_name" is not allocated.
13896 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013897 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013898 callback_T
13899get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013900{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013901 callback_T res;
13902
13903 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013904 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13905 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013906 res.cb_partial = arg->vval.v_partial;
13907 ++res.cb_partial->pt_refcount;
13908 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013909 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013910 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013911 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013912 res.cb_partial = NULL;
13913 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
13914 {
13915 // Note that we don't make a copy of the string.
13916 res.cb_name = arg->vval.v_string;
13917 func_ref(res.cb_name);
13918 }
13919 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13920 {
13921 res.cb_name = (char_u *)"";
13922 }
13923 else
13924 {
13925 emsg(_("E921: Invalid callback argument"));
13926 res.cb_name = NULL;
13927 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013928 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013929 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013930}
13931
13932/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013933 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013934 */
13935 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013936put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013937{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013938 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013939 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013940 tv->v_type = VAR_PARTIAL;
13941 tv->vval.v_partial = cb->cb_partial;
13942 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013943 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013944 else
13945 {
13946 tv->v_type = VAR_FUNC;
13947 tv->vval.v_string = vim_strsave(cb->cb_name);
13948 func_ref(cb->cb_name);
13949 }
13950}
13951
13952/*
13953 * Make a copy of "src" into "dest", allocating the function name if needed,
13954 * without incrementing the refcount.
13955 */
13956 void
13957set_callback(callback_T *dest, callback_T *src)
13958{
13959 if (src->cb_partial == NULL)
13960 {
13961 // just a function name, make a copy
13962 dest->cb_name = vim_strsave(src->cb_name);
13963 dest->cb_free_name = TRUE;
13964 }
13965 else
13966 {
13967 // cb_name is a pointer into cb_partial
13968 dest->cb_name = src->cb_name;
13969 dest->cb_free_name = FALSE;
13970 }
13971 dest->cb_partial = src->cb_partial;
13972}
13973
13974/*
13975 * Unref/free "callback" returned by get_callback() or set_callback().
13976 */
13977 void
13978free_callback(callback_T *callback)
13979{
13980 if (callback->cb_partial != NULL)
13981 {
13982 partial_unref(callback->cb_partial);
13983 callback->cb_partial = NULL;
13984 }
13985 else if (callback->cb_name != NULL)
13986 func_unref(callback->cb_name);
13987 if (callback->cb_free_name)
13988 {
13989 vim_free(callback->cb_name);
13990 callback->cb_free_name = FALSE;
13991 }
13992 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013993}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013994
13995#ifdef FEAT_TIMERS
13996/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013997 * "timer_info([timer])" function
13998 */
13999 static void
14000f_timer_info(typval_T *argvars, typval_T *rettv)
14001{
14002 timer_T *timer = NULL;
14003
14004 if (rettv_list_alloc(rettv) != OK)
14005 return;
14006 if (argvars[0].v_type != VAR_UNKNOWN)
14007 {
14008 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014009 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014010 else
14011 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014012 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014013 if (timer != NULL)
14014 add_timer_info(rettv, timer);
14015 }
14016 }
14017 else
14018 add_timer_info_all(rettv);
14019}
14020
14021/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014022 * "timer_pause(timer, paused)" function
14023 */
14024 static void
14025f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14026{
14027 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014028 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014029
14030 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014031 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014032 else
14033 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014034 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014035 if (timer != NULL)
14036 timer->tr_paused = paused;
14037 }
14038}
14039
14040/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014041 * "timer_start(time, callback [, options])" function
14042 */
14043 static void
14044f_timer_start(typval_T *argvars, typval_T *rettv)
14045{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014046 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014047 timer_T *timer;
14048 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014049 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014050 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014051
Bram Moolenaar75537a92016-09-05 22:45:28 +020014052 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014053 if (check_secure())
14054 return;
14055 if (argvars[2].v_type != VAR_UNKNOWN)
14056 {
14057 if (argvars[2].v_type != VAR_DICT
14058 || (dict = argvars[2].vval.v_dict) == NULL)
14059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014060 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014061 return;
14062 }
14063 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014064 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014065 }
14066
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014067 callback = get_callback(&argvars[1]);
14068 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014069 return;
14070
14071 timer = create_timer(msec, repeat);
14072 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014073 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014074 else
14075 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014076 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014077 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014078 }
14079}
14080
14081/*
14082 * "timer_stop(timer)" function
14083 */
14084 static void
14085f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14086{
14087 timer_T *timer;
14088
14089 if (argvars[0].v_type != VAR_NUMBER)
14090 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014091 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014092 return;
14093 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014094 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014095 if (timer != NULL)
14096 stop_timer(timer);
14097}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014098
14099/*
14100 * "timer_stopall()" function
14101 */
14102 static void
14103f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14104{
14105 stop_all_timers();
14106}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014107#endif
14108
14109/*
14110 * "tolower(string)" function
14111 */
14112 static void
14113f_tolower(typval_T *argvars, typval_T *rettv)
14114{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014115 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014116 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014117}
14118
14119/*
14120 * "toupper(string)" function
14121 */
14122 static void
14123f_toupper(typval_T *argvars, typval_T *rettv)
14124{
14125 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014126 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014127}
14128
14129/*
14130 * "tr(string, fromstr, tostr)" function
14131 */
14132 static void
14133f_tr(typval_T *argvars, typval_T *rettv)
14134{
14135 char_u *in_str;
14136 char_u *fromstr;
14137 char_u *tostr;
14138 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014139 int inlen;
14140 int fromlen;
14141 int tolen;
14142 int idx;
14143 char_u *cpstr;
14144 int cplen;
14145 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014146 char_u buf[NUMBUFLEN];
14147 char_u buf2[NUMBUFLEN];
14148 garray_T ga;
14149
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014150 in_str = tv_get_string(&argvars[0]);
14151 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14152 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014153
14154 /* Default return value: empty string. */
14155 rettv->v_type = VAR_STRING;
14156 rettv->vval.v_string = NULL;
14157 if (fromstr == NULL || tostr == NULL)
14158 return; /* type error; errmsg already given */
14159 ga_init2(&ga, (int)sizeof(char), 80);
14160
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014161 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014162 /* not multi-byte: fromstr and tostr must be the same length */
14163 if (STRLEN(fromstr) != STRLEN(tostr))
14164 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014165error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014166 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014167 ga_clear(&ga);
14168 return;
14169 }
14170
14171 /* fromstr and tostr have to contain the same number of chars */
14172 while (*in_str != NUL)
14173 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014174 if (has_mbyte)
14175 {
14176 inlen = (*mb_ptr2len)(in_str);
14177 cpstr = in_str;
14178 cplen = inlen;
14179 idx = 0;
14180 for (p = fromstr; *p != NUL; p += fromlen)
14181 {
14182 fromlen = (*mb_ptr2len)(p);
14183 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14184 {
14185 for (p = tostr; *p != NUL; p += tolen)
14186 {
14187 tolen = (*mb_ptr2len)(p);
14188 if (idx-- == 0)
14189 {
14190 cplen = tolen;
14191 cpstr = p;
14192 break;
14193 }
14194 }
14195 if (*p == NUL) /* tostr is shorter than fromstr */
14196 goto error;
14197 break;
14198 }
14199 ++idx;
14200 }
14201
14202 if (first && cpstr == in_str)
14203 {
14204 /* Check that fromstr and tostr have the same number of
14205 * (multi-byte) characters. Done only once when a character
14206 * of in_str doesn't appear in fromstr. */
14207 first = FALSE;
14208 for (p = tostr; *p != NUL; p += tolen)
14209 {
14210 tolen = (*mb_ptr2len)(p);
14211 --idx;
14212 }
14213 if (idx != 0)
14214 goto error;
14215 }
14216
14217 (void)ga_grow(&ga, cplen);
14218 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14219 ga.ga_len += cplen;
14220
14221 in_str += inlen;
14222 }
14223 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014224 {
14225 /* When not using multi-byte chars we can do it faster. */
14226 p = vim_strchr(fromstr, *in_str);
14227 if (p != NULL)
14228 ga_append(&ga, tostr[p - fromstr]);
14229 else
14230 ga_append(&ga, *in_str);
14231 ++in_str;
14232 }
14233 }
14234
14235 /* add a terminating NUL */
14236 (void)ga_grow(&ga, 1);
14237 ga_append(&ga, NUL);
14238
14239 rettv->vval.v_string = ga.ga_data;
14240}
14241
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014242/*
14243 * "trim({expr})" function
14244 */
14245 static void
14246f_trim(typval_T *argvars, typval_T *rettv)
14247{
14248 char_u buf1[NUMBUFLEN];
14249 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014250 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014251 char_u *mask = NULL;
14252 char_u *tail;
14253 char_u *prev;
14254 char_u *p;
14255 int c1;
14256
14257 rettv->v_type = VAR_STRING;
14258 if (head == NULL)
14259 {
14260 rettv->vval.v_string = NULL;
14261 return;
14262 }
14263
14264 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014265 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014266
14267 while (*head != NUL)
14268 {
14269 c1 = PTR2CHAR(head);
14270 if (mask == NULL)
14271 {
14272 if (c1 > ' ' && c1 != 0xa0)
14273 break;
14274 }
14275 else
14276 {
14277 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14278 if (c1 == PTR2CHAR(p))
14279 break;
14280 if (*p == NUL)
14281 break;
14282 }
14283 MB_PTR_ADV(head);
14284 }
14285
14286 for (tail = head + STRLEN(head); tail > head; tail = prev)
14287 {
14288 prev = tail;
14289 MB_PTR_BACK(head, prev);
14290 c1 = PTR2CHAR(prev);
14291 if (mask == NULL)
14292 {
14293 if (c1 > ' ' && c1 != 0xa0)
14294 break;
14295 }
14296 else
14297 {
14298 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14299 if (c1 == PTR2CHAR(p))
14300 break;
14301 if (*p == NUL)
14302 break;
14303 }
14304 }
14305 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14306}
14307
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014308#ifdef FEAT_FLOAT
14309/*
14310 * "trunc({float})" function
14311 */
14312 static void
14313f_trunc(typval_T *argvars, typval_T *rettv)
14314{
14315 float_T f = 0.0;
14316
14317 rettv->v_type = VAR_FLOAT;
14318 if (get_float_arg(argvars, &f) == OK)
14319 /* trunc() is not in C90, use floor() or ceil() instead. */
14320 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14321 else
14322 rettv->vval.v_float = 0.0;
14323}
14324#endif
14325
14326/*
14327 * "type(expr)" function
14328 */
14329 static void
14330f_type(typval_T *argvars, typval_T *rettv)
14331{
14332 int n = -1;
14333
14334 switch (argvars[0].v_type)
14335 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014336 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14337 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014338 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014339 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14340 case VAR_LIST: n = VAR_TYPE_LIST; break;
14341 case VAR_DICT: n = VAR_TYPE_DICT; break;
14342 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014343 case VAR_SPECIAL:
14344 if (argvars[0].vval.v_number == VVAL_FALSE
14345 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014346 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014347 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014348 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014349 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014350 case VAR_JOB: n = VAR_TYPE_JOB; break;
14351 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014352 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014353 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014354 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014355 n = -1;
14356 break;
14357 }
14358 rettv->vval.v_number = n;
14359}
14360
14361/*
14362 * "undofile(name)" function
14363 */
14364 static void
14365f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14366{
14367 rettv->v_type = VAR_STRING;
14368#ifdef FEAT_PERSISTENT_UNDO
14369 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014370 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014371
14372 if (*fname == NUL)
14373 {
14374 /* If there is no file name there will be no undo file. */
14375 rettv->vval.v_string = NULL;
14376 }
14377 else
14378 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020014379 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014380
14381 if (ffname != NULL)
14382 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14383 vim_free(ffname);
14384 }
14385 }
14386#else
14387 rettv->vval.v_string = NULL;
14388#endif
14389}
14390
14391/*
14392 * "undotree()" function
14393 */
14394 static void
14395f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14396{
14397 if (rettv_dict_alloc(rettv) == OK)
14398 {
14399 dict_T *dict = rettv->vval.v_dict;
14400 list_T *list;
14401
Bram Moolenaare0be1672018-07-08 16:50:37 +020014402 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14403 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14404 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14405 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14406 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14407 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014408
14409 list = list_alloc();
14410 if (list != NULL)
14411 {
14412 u_eval_tree(curbuf->b_u_oldhead, list);
14413 dict_add_list(dict, "entries", list);
14414 }
14415 }
14416}
14417
14418/*
14419 * "values(dict)" function
14420 */
14421 static void
14422f_values(typval_T *argvars, typval_T *rettv)
14423{
14424 dict_list(argvars, rettv, 1);
14425}
14426
14427/*
14428 * "virtcol(string)" function
14429 */
14430 static void
14431f_virtcol(typval_T *argvars, typval_T *rettv)
14432{
14433 colnr_T vcol = 0;
14434 pos_T *fp;
14435 int fnum = curbuf->b_fnum;
14436
14437 fp = var2fpos(&argvars[0], FALSE, &fnum);
14438 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14439 && fnum == curbuf->b_fnum)
14440 {
14441 getvvcol(curwin, fp, NULL, NULL, &vcol);
14442 ++vcol;
14443 }
14444
14445 rettv->vval.v_number = vcol;
14446}
14447
14448/*
14449 * "visualmode()" function
14450 */
14451 static void
14452f_visualmode(typval_T *argvars, typval_T *rettv)
14453{
14454 char_u str[2];
14455
14456 rettv->v_type = VAR_STRING;
14457 str[0] = curbuf->b_visual_mode_eval;
14458 str[1] = NUL;
14459 rettv->vval.v_string = vim_strsave(str);
14460
14461 /* A non-zero number or non-empty string argument: reset mode. */
14462 if (non_zero_arg(&argvars[0]))
14463 curbuf->b_visual_mode_eval = NUL;
14464}
14465
14466/*
14467 * "wildmenumode()" function
14468 */
14469 static void
14470f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14471{
14472#ifdef FEAT_WILDMENU
14473 if (wild_menu_showing)
14474 rettv->vval.v_number = 1;
14475#endif
14476}
14477
14478/*
14479 * "winbufnr(nr)" function
14480 */
14481 static void
14482f_winbufnr(typval_T *argvars, typval_T *rettv)
14483{
14484 win_T *wp;
14485
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014486 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014487 if (wp == NULL)
14488 rettv->vval.v_number = -1;
14489 else
14490 rettv->vval.v_number = wp->w_buffer->b_fnum;
14491}
14492
14493/*
14494 * "wincol()" function
14495 */
14496 static void
14497f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14498{
14499 validate_cursor();
14500 rettv->vval.v_number = curwin->w_wcol + 1;
14501}
14502
14503/*
14504 * "winheight(nr)" function
14505 */
14506 static void
14507f_winheight(typval_T *argvars, typval_T *rettv)
14508{
14509 win_T *wp;
14510
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014511 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014512 if (wp == NULL)
14513 rettv->vval.v_number = -1;
14514 else
14515 rettv->vval.v_number = wp->w_height;
14516}
14517
14518/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014519 * "winlayout()" function
14520 */
14521 static void
14522f_winlayout(typval_T *argvars, typval_T *rettv)
14523{
14524 tabpage_T *tp;
14525
14526 if (rettv_list_alloc(rettv) != OK)
14527 return;
14528
14529 if (argvars[0].v_type == VAR_UNKNOWN)
14530 tp = curtab;
14531 else
14532 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014533 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014534 if (tp == NULL)
14535 return;
14536 }
14537
14538 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14539}
14540
14541/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014542 * "winline()" function
14543 */
14544 static void
14545f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14546{
14547 validate_cursor();
14548 rettv->vval.v_number = curwin->w_wrow + 1;
14549}
14550
14551/*
14552 * "winnr()" function
14553 */
14554 static void
14555f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14556{
14557 int nr = 1;
14558
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014559 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014560 rettv->vval.v_number = nr;
14561}
14562
14563/*
14564 * "winrestcmd()" function
14565 */
14566 static void
14567f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14568{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014569 win_T *wp;
14570 int winnr = 1;
14571 garray_T ga;
14572 char_u buf[50];
14573
14574 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014575 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014576 {
14577 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14578 ga_concat(&ga, buf);
14579 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14580 ga_concat(&ga, buf);
14581 ++winnr;
14582 }
14583 ga_append(&ga, NUL);
14584
14585 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014586 rettv->v_type = VAR_STRING;
14587}
14588
14589/*
14590 * "winrestview()" function
14591 */
14592 static void
14593f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14594{
14595 dict_T *dict;
14596
14597 if (argvars[0].v_type != VAR_DICT
14598 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014599 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014600 else
14601 {
14602 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014603 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014604 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014605 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014606 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014607 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014608 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14609 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014610 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014611 curwin->w_set_curswant = FALSE;
14612 }
14613
14614 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014615 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014616#ifdef FEAT_DIFF
14617 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014618 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014619#endif
14620 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014621 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014622 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014623 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014624
14625 check_cursor();
14626 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014627 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014628 changed_window_setting();
14629
14630 if (curwin->w_topline <= 0)
14631 curwin->w_topline = 1;
14632 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14633 curwin->w_topline = curbuf->b_ml.ml_line_count;
14634#ifdef FEAT_DIFF
14635 check_topfill(curwin, TRUE);
14636#endif
14637 }
14638}
14639
14640/*
14641 * "winsaveview()" function
14642 */
14643 static void
14644f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14645{
14646 dict_T *dict;
14647
14648 if (rettv_dict_alloc(rettv) == FAIL)
14649 return;
14650 dict = rettv->vval.v_dict;
14651
Bram Moolenaare0be1672018-07-08 16:50:37 +020014652 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14653 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020014654 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014655 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014656 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014657
Bram Moolenaare0be1672018-07-08 16:50:37 +020014658 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014659#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014660 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014661#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014662 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14663 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014664}
14665
14666/*
14667 * "winwidth(nr)" function
14668 */
14669 static void
14670f_winwidth(typval_T *argvars, typval_T *rettv)
14671{
14672 win_T *wp;
14673
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014674 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014675 if (wp == NULL)
14676 rettv->vval.v_number = -1;
14677 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014678 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014679}
14680
14681/*
14682 * "wordcount()" function
14683 */
14684 static void
14685f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14686{
14687 if (rettv_dict_alloc(rettv) == FAIL)
14688 return;
14689 cursor_pos_info(rettv->vval.v_dict);
14690}
14691
14692/*
14693 * "writefile()" function
14694 */
14695 static void
14696f_writefile(typval_T *argvars, typval_T *rettv)
14697{
14698 int binary = FALSE;
14699 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014700#ifdef HAVE_FSYNC
14701 int do_fsync = p_fs;
14702#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014703 char_u *fname;
14704 FILE *fd;
14705 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014706 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014707 list_T *list = NULL;
14708 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014709
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014710 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010014711 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014712 return;
14713
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014714 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014715 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014716 list = argvars[0].vval.v_list;
14717 if (list == NULL)
14718 return;
14719 for (li = list->lv_first; li != NULL; li = li->li_next)
14720 if (tv_get_string_chk(&li->li_tv) == NULL)
14721 return;
14722 }
14723 else if (argvars[0].v_type == VAR_BLOB)
14724 {
14725 blob = argvars[0].vval.v_blob;
14726 if (blob == NULL)
14727 return;
14728 }
14729 else
14730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014731 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014732 return;
14733 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014734
14735 if (argvars[2].v_type != VAR_UNKNOWN)
14736 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014737 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014738
14739 if (arg2 == NULL)
14740 return;
14741 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014742 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014743 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014744 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014745#ifdef HAVE_FSYNC
14746 if (vim_strchr(arg2, 's') != NULL)
14747 do_fsync = TRUE;
14748 else if (vim_strchr(arg2, 'S') != NULL)
14749 do_fsync = FALSE;
14750#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014751 }
14752
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014753 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014754 if (fname == NULL)
14755 return;
14756
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014757 /* Always open the file in binary mode, library functions have a mind of
14758 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014759 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14760 append ? APPENDBIN : WRITEBIN)) == NULL)
14761 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014762 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014763 ret = -1;
14764 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014765 else if (blob)
14766 {
14767 if (write_blob(fd, blob) == FAIL)
14768 ret = -1;
14769#ifdef HAVE_FSYNC
14770 else if (do_fsync)
14771 // Ignore the error, the user wouldn't know what to do about it.
14772 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010014773 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014774#endif
14775 fclose(fd);
14776 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014777 else
14778 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014779 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014780 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014781#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014782 else if (do_fsync)
14783 /* Ignore the error, the user wouldn't know what to do about it.
14784 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010014785 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014786#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014787 fclose(fd);
14788 }
14789
14790 rettv->vval.v_number = ret;
14791}
14792
14793/*
14794 * "xor(expr, expr)" function
14795 */
14796 static void
14797f_xor(typval_T *argvars, typval_T *rettv)
14798{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014799 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14800 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014801}
14802
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014803#endif /* FEAT_EVAL */