blob: 9ac67b8c9fa7b291e7266db40820aa7d42b9dffa [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},
Bram Moolenaar809ce4d2019-07-13 21:21:40 +0200891 {"sign_placelist", 1, 1, f_sign_placelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100892 {"sign_undefine", 0, 1, f_sign_undefine},
893 {"sign_unplace", 1, 2, f_sign_unplace},
Bram Moolenaar809ce4d2019-07-13 21:21:40 +0200894 {"sign_unplacelist", 1, 2, f_sign_unplacelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100895#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200896 {"simplify", 1, 1, f_simplify},
897#ifdef FEAT_FLOAT
898 {"sin", 1, 1, f_sin},
899 {"sinh", 1, 1, f_sinh},
900#endif
901 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200902#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200903 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200904 {"sound_playevent", 1, 2, f_sound_playevent},
905 {"sound_playfile", 1, 2, f_sound_playfile},
906 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200907#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200908 {"soundfold", 1, 1, f_soundfold},
909 {"spellbadword", 0, 1, f_spellbadword},
910 {"spellsuggest", 1, 3, f_spellsuggest},
911 {"split", 1, 3, f_split},
912#ifdef FEAT_FLOAT
913 {"sqrt", 1, 1, f_sqrt},
914 {"str2float", 1, 1, f_str2float},
915#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200916 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200917 {"str2nr", 1, 2, f_str2nr},
918 {"strcharpart", 2, 3, f_strcharpart},
919 {"strchars", 1, 2, f_strchars},
920 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
921#ifdef HAVE_STRFTIME
922 {"strftime", 1, 2, f_strftime},
923#endif
924 {"strgetchar", 2, 2, f_strgetchar},
925 {"stridx", 2, 3, f_stridx},
926 {"string", 1, 1, f_string},
927 {"strlen", 1, 1, f_strlen},
928 {"strpart", 2, 3, f_strpart},
929 {"strridx", 2, 3, f_strridx},
930 {"strtrans", 1, 1, f_strtrans},
931 {"strwidth", 1, 1, f_strwidth},
932 {"submatch", 1, 2, f_submatch},
933 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200934 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200935 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200936 {"synID", 3, 3, f_synID},
937 {"synIDattr", 2, 3, f_synIDattr},
938 {"synIDtrans", 1, 1, f_synIDtrans},
939 {"synconcealed", 2, 2, f_synconcealed},
940 {"synstack", 2, 2, f_synstack},
941 {"system", 1, 2, f_system},
942 {"systemlist", 1, 2, f_systemlist},
943 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
944 {"tabpagenr", 0, 1, f_tabpagenr},
945 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
946 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100947 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200948#ifdef FEAT_FLOAT
949 {"tan", 1, 1, f_tan},
950 {"tanh", 1, 1, f_tanh},
951#endif
952 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200953#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100954 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
955 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100956 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200957 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200958# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
959 {"term_getansicolors", 1, 1, f_term_getansicolors},
960# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200961 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200962 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200963 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200964 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200965 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200966 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200967 {"term_getstatus", 1, 1, f_term_getstatus},
968 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200969 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200970 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200971 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200972 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200973# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
974 {"term_setansicolors", 2, 2, f_term_setansicolors},
975# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100976 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100977 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200978 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200979 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200980 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200981#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200982 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
983 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200984 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200985 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaaradc67142019-06-22 01:40:42 +0200986 {"test_garbagecollect_soon", 0, 0, f_test_garbagecollect_soon},
Bram Moolenaareda65222019-05-16 20:29:44 +0200987 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100988 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100989 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200990#ifdef FEAT_JOB_CHANNEL
991 {"test_null_channel", 0, 0, f_test_null_channel},
992#endif
993 {"test_null_dict", 0, 0, f_test_null_dict},
994#ifdef FEAT_JOB_CHANNEL
995 {"test_null_job", 0, 0, f_test_null_job},
996#endif
997 {"test_null_list", 0, 0, f_test_null_list},
998 {"test_null_partial", 0, 0, f_test_null_partial},
999 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001000 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001001 {"test_override", 2, 2, f_test_override},
1002 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001003#ifdef FEAT_GUI
1004 {"test_scrollbar", 3, 3, f_test_scrollbar},
1005#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001006#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001007 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001008#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001009 {"test_settime", 1, 1, f_test_settime},
1010#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001011 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001012 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001013 {"timer_start", 2, 3, f_timer_start},
1014 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001015 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001016#endif
1017 {"tolower", 1, 1, f_tolower},
1018 {"toupper", 1, 1, f_toupper},
1019 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001020 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001021#ifdef FEAT_FLOAT
1022 {"trunc", 1, 1, f_trunc},
1023#endif
1024 {"type", 1, 1, f_type},
1025 {"undofile", 1, 1, f_undofile},
1026 {"undotree", 0, 0, f_undotree},
1027 {"uniq", 1, 3, f_uniq},
1028 {"values", 1, 1, f_values},
1029 {"virtcol", 1, 1, f_virtcol},
1030 {"visualmode", 0, 1, f_visualmode},
1031 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001032 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001033 {"win_findbuf", 1, 1, f_win_findbuf},
1034 {"win_getid", 0, 2, f_win_getid},
1035 {"win_gotoid", 1, 1, f_win_gotoid},
1036 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1037 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001038 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001039 {"winbufnr", 1, 1, f_winbufnr},
1040 {"wincol", 0, 0, f_wincol},
1041 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001042 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001043 {"winline", 0, 0, f_winline},
1044 {"winnr", 0, 1, f_winnr},
1045 {"winrestcmd", 0, 0, f_winrestcmd},
1046 {"winrestview", 1, 1, f_winrestview},
1047 {"winsaveview", 0, 0, f_winsaveview},
1048 {"winwidth", 1, 1, f_winwidth},
1049 {"wordcount", 0, 0, f_wordcount},
1050 {"writefile", 2, 3, f_writefile},
1051 {"xor", 2, 2, f_xor},
1052};
1053
1054#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1055
1056/*
1057 * Function given to ExpandGeneric() to obtain the list of internal
1058 * or user defined function names.
1059 */
1060 char_u *
1061get_function_name(expand_T *xp, int idx)
1062{
1063 static int intidx = -1;
1064 char_u *name;
1065
1066 if (idx == 0)
1067 intidx = -1;
1068 if (intidx < 0)
1069 {
1070 name = get_user_func_name(xp, idx);
1071 if (name != NULL)
1072 return name;
1073 }
1074 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1075 {
1076 STRCPY(IObuff, functions[intidx].f_name);
1077 STRCAT(IObuff, "(");
1078 if (functions[intidx].f_max_argc == 0)
1079 STRCAT(IObuff, ")");
1080 return IObuff;
1081 }
1082
1083 return NULL;
1084}
1085
1086/*
1087 * Function given to ExpandGeneric() to obtain the list of internal or
1088 * user defined variable or function names.
1089 */
1090 char_u *
1091get_expr_name(expand_T *xp, int idx)
1092{
1093 static int intidx = -1;
1094 char_u *name;
1095
1096 if (idx == 0)
1097 intidx = -1;
1098 if (intidx < 0)
1099 {
1100 name = get_function_name(xp, idx);
1101 if (name != NULL)
1102 return name;
1103 }
1104 return get_user_var_name(xp, ++intidx);
1105}
1106
1107#endif /* FEAT_CMDL_COMPL */
1108
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001109/*
1110 * Find internal function in table above.
1111 * Return index, or -1 if not found
1112 */
1113 int
1114find_internal_func(
1115 char_u *name) /* name of the function */
1116{
1117 int first = 0;
1118 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1119 int cmp;
1120 int x;
1121
1122 /*
1123 * Find the function name in the table. Binary search.
1124 */
1125 while (first <= last)
1126 {
1127 x = first + ((unsigned)(last - first) >> 1);
1128 cmp = STRCMP(name, functions[x].f_name);
1129 if (cmp < 0)
1130 last = x - 1;
1131 else if (cmp > 0)
1132 first = x + 1;
1133 else
1134 return x;
1135 }
1136 return -1;
1137}
1138
1139 int
1140call_internal_func(
1141 char_u *name,
1142 int argcount,
1143 typval_T *argvars,
1144 typval_T *rettv)
1145{
1146 int i;
1147
1148 i = find_internal_func(name);
1149 if (i < 0)
1150 return ERROR_UNKNOWN;
1151 if (argcount < functions[i].f_min_argc)
1152 return ERROR_TOOFEW;
1153 if (argcount > functions[i].f_max_argc)
1154 return ERROR_TOOMANY;
1155 argvars[argcount].v_type = VAR_UNKNOWN;
1156 functions[i].f_func(argvars, rettv);
1157 return ERROR_NONE;
1158}
1159
1160/*
1161 * Return TRUE for a non-zero Number and a non-empty String.
1162 */
1163 static int
1164non_zero_arg(typval_T *argvars)
1165{
1166 return ((argvars[0].v_type == VAR_NUMBER
1167 && argvars[0].vval.v_number != 0)
1168 || (argvars[0].v_type == VAR_SPECIAL
1169 && argvars[0].vval.v_number == VVAL_TRUE)
1170 || (argvars[0].v_type == VAR_STRING
1171 && argvars[0].vval.v_string != NULL
1172 && *argvars[0].vval.v_string != NUL));
1173}
1174
1175/*
1176 * Get the lnum from the first argument.
1177 * Also accepts ".", "$", etc., but that only works for the current buffer.
1178 * Returns -1 on error.
1179 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001180 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001181tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001182{
1183 typval_T rettv;
1184 linenr_T lnum;
1185
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001186 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001187 if (lnum == 0) /* no valid number, try using line() */
1188 {
1189 rettv.v_type = VAR_NUMBER;
1190 f_line(argvars, &rettv);
1191 lnum = (linenr_T)rettv.vval.v_number;
1192 clear_tv(&rettv);
1193 }
1194 return lnum;
1195}
1196
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001197/*
1198 * Get the lnum from the first argument.
1199 * Also accepts "$", then "buf" is used.
1200 * Returns 0 on error.
1201 */
1202 static linenr_T
1203tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1204{
1205 if (argvars[0].v_type == VAR_STRING
1206 && argvars[0].vval.v_string != NULL
1207 && argvars[0].vval.v_string[0] == '$'
1208 && buf != NULL)
1209 return buf->b_ml.ml_line_count;
1210 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1211}
1212
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001213#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001214/*
1215 * Get the float value of "argvars[0]" into "f".
1216 * Returns FAIL when the argument is not a Number or Float.
1217 */
1218 static int
1219get_float_arg(typval_T *argvars, float_T *f)
1220{
1221 if (argvars[0].v_type == VAR_FLOAT)
1222 {
1223 *f = argvars[0].vval.v_float;
1224 return OK;
1225 }
1226 if (argvars[0].v_type == VAR_NUMBER)
1227 {
1228 *f = (float_T)argvars[0].vval.v_number;
1229 return OK;
1230 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001231 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001232 return FAIL;
1233}
1234
1235/*
1236 * "abs(expr)" function
1237 */
1238 static void
1239f_abs(typval_T *argvars, typval_T *rettv)
1240{
1241 if (argvars[0].v_type == VAR_FLOAT)
1242 {
1243 rettv->v_type = VAR_FLOAT;
1244 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1245 }
1246 else
1247 {
1248 varnumber_T n;
1249 int error = FALSE;
1250
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001251 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001252 if (error)
1253 rettv->vval.v_number = -1;
1254 else if (n > 0)
1255 rettv->vval.v_number = n;
1256 else
1257 rettv->vval.v_number = -n;
1258 }
1259}
1260
1261/*
1262 * "acos()" function
1263 */
1264 static void
1265f_acos(typval_T *argvars, typval_T *rettv)
1266{
1267 float_T f = 0.0;
1268
1269 rettv->v_type = VAR_FLOAT;
1270 if (get_float_arg(argvars, &f) == OK)
1271 rettv->vval.v_float = acos(f);
1272 else
1273 rettv->vval.v_float = 0.0;
1274}
1275#endif
1276
1277/*
1278 * "add(list, item)" function
1279 */
1280 static void
1281f_add(typval_T *argvars, typval_T *rettv)
1282{
1283 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001284 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001285
1286 rettv->vval.v_number = 1; /* Default: Failed */
1287 if (argvars[0].v_type == VAR_LIST)
1288 {
1289 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001290 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001291 (char_u *)N_("add() argument"), TRUE)
1292 && list_append_tv(l, &argvars[1]) == OK)
1293 copy_tv(&argvars[0], rettv);
1294 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295 else if (argvars[0].v_type == VAR_BLOB)
1296 {
1297 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001298 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001299 (char_u *)N_("add() argument"), TRUE))
1300 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001301 int error = FALSE;
1302 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1303
1304 if (!error)
1305 {
1306 ga_append(&b->bv_ga, (int)n);
1307 copy_tv(&argvars[0], rettv);
1308 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001309 }
1310 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001311 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001312 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001313}
1314
1315/*
1316 * "and(expr, expr)" function
1317 */
1318 static void
1319f_and(typval_T *argvars, typval_T *rettv)
1320{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001321 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1322 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001323}
1324
1325/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001326 * If there is a window for "curbuf", make it the current window.
1327 */
1328 static void
1329find_win_for_curbuf(void)
1330{
1331 wininfo_T *wip;
1332
1333 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1334 {
1335 if (wip->wi_win != NULL)
1336 {
1337 curwin = wip->wi_win;
1338 break;
1339 }
1340 }
1341}
1342
1343/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001344 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001345 */
1346 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001347set_buffer_lines(
1348 buf_T *buf,
1349 linenr_T lnum_arg,
1350 int append,
1351 typval_T *lines,
1352 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353{
Bram Moolenaarca851592018-06-06 21:04:07 +02001354 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1355 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001356 list_T *l = NULL;
1357 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001359 linenr_T append_lnum;
1360 buf_T *curbuf_save = NULL;
1361 win_T *curwin_save = NULL;
1362 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001363
Bram Moolenaarca851592018-06-06 21:04:07 +02001364 /* When using the current buffer ml_mfp will be set if needed. Useful when
1365 * setline() is used on startup. For other buffers the buffer must be
1366 * loaded. */
1367 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001368 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001369 rettv->vval.v_number = 1; /* FAIL */
1370 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001371 }
1372
Bram Moolenaarca851592018-06-06 21:04:07 +02001373 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001374 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001375 curbuf_save = curbuf;
1376 curwin_save = curwin;
1377 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001378 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001379 }
1380
1381 if (append)
1382 // appendbufline() uses the line number below which we insert
1383 append_lnum = lnum - 1;
1384 else
1385 // setbufline() uses the line number above which we insert, we only
1386 // append if it's below the last line
1387 append_lnum = curbuf->b_ml.ml_line_count;
1388
1389 if (lines->v_type == VAR_LIST)
1390 {
1391 l = lines->vval.v_list;
1392 li = l->lv_first;
1393 }
1394 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001395 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001396
1397 /* default result is zero == OK */
1398 for (;;)
1399 {
1400 if (l != NULL)
1401 {
1402 /* list argument, get next string */
1403 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001404 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001405 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001406 li = li->li_next;
1407 }
1408
Bram Moolenaarca851592018-06-06 21:04:07 +02001409 rettv->vval.v_number = 1; /* FAIL */
1410 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1411 break;
1412
1413 /* When coming here from Insert mode, sync undo, so that this can be
1414 * undone separately from what was previously inserted. */
1415 if (u_sync_once == 2)
1416 {
1417 u_sync_once = 1; /* notify that u_sync() was called */
1418 u_sync(TRUE);
1419 }
1420
1421 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1422 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001423 // Existing line, replace it.
1424 // Removes any existing text properties.
1425 if (u_savesub(lnum) == OK && ml_replace_len(
1426 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001427 {
1428 changed_bytes(lnum, 0);
1429 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1430 check_cursor_col();
1431 rettv->vval.v_number = 0; /* OK */
1432 }
1433 }
1434 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1435 {
1436 /* append the line */
1437 ++added;
1438 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1439 rettv->vval.v_number = 0; /* OK */
1440 }
1441
1442 if (l == NULL) /* only one string argument */
1443 break;
1444 ++lnum;
1445 }
1446
1447 if (added > 0)
1448 {
1449 win_T *wp;
1450 tabpage_T *tp;
1451
1452 appended_lines_mark(append_lnum, added);
1453 FOR_ALL_TAB_WINDOWS(tp, wp)
1454 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1455 wp->w_cursor.lnum += added;
1456 check_cursor_col();
1457
Bram Moolenaarf2732452018-06-03 14:47:35 +02001458#ifdef FEAT_JOB_CHANNEL
1459 if (bt_prompt(curbuf) && (State & INSERT))
1460 // show the line with the prompt
1461 update_topline();
1462#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001463 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001464
1465 if (!is_curbuf)
1466 {
1467 curbuf = curbuf_save;
1468 curwin = curwin_save;
1469 }
1470}
1471
1472/*
1473 * "append(lnum, string/list)" function
1474 */
1475 static void
1476f_append(typval_T *argvars, typval_T *rettv)
1477{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001478 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001479
1480 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1481}
1482
1483/*
1484 * "appendbufline(buf, lnum, string/list)" function
1485 */
1486 static void
1487f_appendbufline(typval_T *argvars, typval_T *rettv)
1488{
1489 linenr_T lnum;
1490 buf_T *buf;
1491
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001492 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001493 if (buf == NULL)
1494 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001495 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001496 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001497 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001498 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1499 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001500}
1501
1502/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001503 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001504 */
1505 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001506f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001507{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001508 win_T *wp;
1509
1510 if (argvars[0].v_type == VAR_UNKNOWN)
1511 // use the current window
1512 rettv->vval.v_number = ARGCOUNT;
1513 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001514 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001515 // use the global argument list
1516 rettv->vval.v_number = GARGCOUNT;
1517 else
1518 {
1519 // use the argument list of the specified window
1520 wp = find_win_by_nr_or_id(&argvars[0]);
1521 if (wp != NULL)
1522 rettv->vval.v_number = WARGCOUNT(wp);
1523 else
1524 rettv->vval.v_number = -1;
1525 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001526}
1527
1528/*
1529 * "argidx()" function
1530 */
1531 static void
1532f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1533{
1534 rettv->vval.v_number = curwin->w_arg_idx;
1535}
1536
1537/*
1538 * "arglistid()" function
1539 */
1540 static void
1541f_arglistid(typval_T *argvars, typval_T *rettv)
1542{
1543 win_T *wp;
1544
1545 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001546 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001547 if (wp != NULL)
1548 rettv->vval.v_number = wp->w_alist->id;
1549}
1550
1551/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001552 * Get the argument list for a given window
1553 */
1554 static void
1555get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1556{
1557 int idx;
1558
1559 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1560 for (idx = 0; idx < argcount; ++idx)
1561 list_append_string(rettv->vval.v_list,
1562 alist_name(&arglist[idx]), -1);
1563}
1564
1565/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001566 * "argv(nr)" function
1567 */
1568 static void
1569f_argv(typval_T *argvars, typval_T *rettv)
1570{
1571 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001572 aentry_T *arglist = NULL;
1573 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001574
1575 if (argvars[0].v_type != VAR_UNKNOWN)
1576 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001577 if (argvars[1].v_type == VAR_UNKNOWN)
1578 {
1579 arglist = ARGLIST;
1580 argcount = ARGCOUNT;
1581 }
1582 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001583 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001584 {
1585 arglist = GARGLIST;
1586 argcount = GARGCOUNT;
1587 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001588 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001589 {
1590 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1591
1592 if (wp != NULL)
1593 {
1594 /* Use the argument list of the specified window */
1595 arglist = WARGLIST(wp);
1596 argcount = WARGCOUNT(wp);
1597 }
1598 }
1599
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001600 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001601 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001602 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001603 if (arglist != NULL && idx >= 0 && idx < argcount)
1604 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1605 else if (idx == -1)
1606 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001608 else
1609 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001610}
1611
1612/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001613 * "assert_beeps(cmd [, error])" function
1614 */
1615 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001616f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001617{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001618 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001619}
1620
1621/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001622 * "assert_equal(expected, actual[, msg])" function
1623 */
1624 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001625f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001626{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001627 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001628}
1629
1630/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001631 * "assert_equalfile(fname-one, fname-two)" function
1632 */
1633 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001634f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001635{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001636 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001637}
1638
1639/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001640 * "assert_notequal(expected, actual[, msg])" function
1641 */
1642 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001643f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001644{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001645 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001646}
1647
1648/*
1649 * "assert_exception(string[, msg])" function
1650 */
1651 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001652f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001653{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001654 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001655}
1656
1657/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001658 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659 */
1660 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001661f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001662{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001663 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001664}
1665
1666/*
1667 * "assert_false(actual[, msg])" function
1668 */
1669 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001670f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001671{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001672 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001673}
1674
1675/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001676 * "assert_inrange(lower, upper[, msg])" function
1677 */
1678 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001679f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001680{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001681 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001682}
1683
1684/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001685 * "assert_match(pattern, actual[, msg])" function
1686 */
1687 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001688f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001689{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001690 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001691}
1692
1693/*
1694 * "assert_notmatch(pattern, actual[, msg])" function
1695 */
1696 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001697f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001698{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001699 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001700}
1701
1702/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001703 * "assert_report(msg)" function
1704 */
1705 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001706f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001707{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001708 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001709}
1710
1711/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001712 * "assert_true(actual[, msg])" function
1713 */
1714 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001715f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001716{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001717 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001718}
1719
1720#ifdef FEAT_FLOAT
1721/*
1722 * "asin()" function
1723 */
1724 static void
1725f_asin(typval_T *argvars, typval_T *rettv)
1726{
1727 float_T f = 0.0;
1728
1729 rettv->v_type = VAR_FLOAT;
1730 if (get_float_arg(argvars, &f) == OK)
1731 rettv->vval.v_float = asin(f);
1732 else
1733 rettv->vval.v_float = 0.0;
1734}
1735
1736/*
1737 * "atan()" function
1738 */
1739 static void
1740f_atan(typval_T *argvars, typval_T *rettv)
1741{
1742 float_T f = 0.0;
1743
1744 rettv->v_type = VAR_FLOAT;
1745 if (get_float_arg(argvars, &f) == OK)
1746 rettv->vval.v_float = atan(f);
1747 else
1748 rettv->vval.v_float = 0.0;
1749}
1750
1751/*
1752 * "atan2()" function
1753 */
1754 static void
1755f_atan2(typval_T *argvars, typval_T *rettv)
1756{
1757 float_T fx = 0.0, fy = 0.0;
1758
1759 rettv->v_type = VAR_FLOAT;
1760 if (get_float_arg(argvars, &fx) == OK
1761 && get_float_arg(&argvars[1], &fy) == OK)
1762 rettv->vval.v_float = atan2(fx, fy);
1763 else
1764 rettv->vval.v_float = 0.0;
1765}
1766#endif
1767
1768/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001769 * "balloon_show()" function
1770 */
1771#ifdef FEAT_BEVAL
1772 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001773f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1774{
1775 rettv->v_type = VAR_STRING;
1776 if (balloonEval != NULL)
1777 {
1778 if (balloonEval->msg == NULL)
1779 rettv->vval.v_string = NULL;
1780 else
1781 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1782 }
1783}
1784
1785 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001786f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1787{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001788 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001789 {
1790 if (argvars[0].v_type == VAR_LIST
1791# ifdef FEAT_GUI
1792 && !gui.in_use
1793# endif
1794 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001795 {
1796 list_T *l = argvars[0].vval.v_list;
1797
1798 // empty list removes the balloon
1799 post_balloon(balloonEval, NULL,
1800 l == NULL || l->lv_len == 0 ? NULL : l);
1801 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001802 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001803 {
1804 char_u *mesg = tv_get_string_chk(&argvars[0]);
1805
1806 if (mesg != NULL)
1807 // empty string removes the balloon
1808 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1809 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001810 }
1811}
1812
Bram Moolenaar669a8282017-11-19 20:13:05 +01001813# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001814 static void
1815f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1816{
1817 if (rettv_list_alloc(rettv) == OK)
1818 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001819 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001820
1821 if (msg != NULL)
1822 {
1823 pumitem_T *array;
1824 int size = split_message(msg, &array);
1825 int i;
1826
1827 /* Skip the first and last item, they are always empty. */
1828 for (i = 1; i < size - 1; ++i)
1829 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001830 while (size > 0)
1831 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001832 vim_free(array);
1833 }
1834 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001835}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001836# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001837#endif
1838
1839/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001840 * "browse(save, title, initdir, default)" function
1841 */
1842 static void
1843f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1844{
1845#ifdef FEAT_BROWSE
1846 int save;
1847 char_u *title;
1848 char_u *initdir;
1849 char_u *defname;
1850 char_u buf[NUMBUFLEN];
1851 char_u buf2[NUMBUFLEN];
1852 int error = FALSE;
1853
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001854 save = (int)tv_get_number_chk(&argvars[0], &error);
1855 title = tv_get_string_chk(&argvars[1]);
1856 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1857 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001858
1859 if (error || title == NULL || initdir == NULL || defname == NULL)
1860 rettv->vval.v_string = NULL;
1861 else
1862 rettv->vval.v_string =
1863 do_browse(save ? BROWSE_SAVE : 0,
1864 title, defname, NULL, initdir, NULL, curbuf);
1865#else
1866 rettv->vval.v_string = NULL;
1867#endif
1868 rettv->v_type = VAR_STRING;
1869}
1870
1871/*
1872 * "browsedir(title, initdir)" function
1873 */
1874 static void
1875f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1876{
1877#ifdef FEAT_BROWSE
1878 char_u *title;
1879 char_u *initdir;
1880 char_u buf[NUMBUFLEN];
1881
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001882 title = tv_get_string_chk(&argvars[0]);
1883 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001884
1885 if (title == NULL || initdir == NULL)
1886 rettv->vval.v_string = NULL;
1887 else
1888 rettv->vval.v_string = do_browse(BROWSE_DIR,
1889 title, NULL, NULL, initdir, NULL, curbuf);
1890#else
1891 rettv->vval.v_string = NULL;
1892#endif
1893 rettv->v_type = VAR_STRING;
1894}
1895
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001896/*
1897 * Find a buffer by number or exact name.
1898 */
1899 static buf_T *
1900find_buffer(typval_T *avar)
1901{
1902 buf_T *buf = NULL;
1903
1904 if (avar->v_type == VAR_NUMBER)
1905 buf = buflist_findnr((int)avar->vval.v_number);
1906 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1907 {
1908 buf = buflist_findname_exp(avar->vval.v_string);
1909 if (buf == NULL)
1910 {
1911 /* No full path name match, try a match with a URL or a "nofile"
1912 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001913 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001914 if (buf->b_fname != NULL
1915 && (path_with_url(buf->b_fname)
1916#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001917 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001918#endif
1919 )
1920 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1921 break;
1922 }
1923 }
1924 return buf;
1925}
1926
1927/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001928 * "bufadd(expr)" function
1929 */
1930 static void
1931f_bufadd(typval_T *argvars, typval_T *rettv)
1932{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001933 char_u *name = tv_get_string(&argvars[0]);
1934
1935 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001936}
1937
1938/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001939 * "bufexists(expr)" function
1940 */
1941 static void
1942f_bufexists(typval_T *argvars, typval_T *rettv)
1943{
1944 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1945}
1946
1947/*
1948 * "buflisted(expr)" function
1949 */
1950 static void
1951f_buflisted(typval_T *argvars, typval_T *rettv)
1952{
1953 buf_T *buf;
1954
1955 buf = find_buffer(&argvars[0]);
1956 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1957}
1958
1959/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001960 * "bufload(expr)" function
1961 */
1962 static void
1963f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1964{
1965 buf_T *buf = get_buf_arg(&argvars[0]);
1966
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001967 if (buf != NULL)
1968 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001969}
1970
1971/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001972 * "bufloaded(expr)" function
1973 */
1974 static void
1975f_bufloaded(typval_T *argvars, typval_T *rettv)
1976{
1977 buf_T *buf;
1978
1979 buf = find_buffer(&argvars[0]);
1980 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1981}
1982
1983 buf_T *
1984buflist_find_by_name(char_u *name, int curtab_only)
1985{
1986 int save_magic;
1987 char_u *save_cpo;
1988 buf_T *buf;
1989
1990 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1991 save_magic = p_magic;
1992 p_magic = TRUE;
1993 save_cpo = p_cpo;
1994 p_cpo = (char_u *)"";
1995
1996 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1997 TRUE, FALSE, curtab_only));
1998
1999 p_magic = save_magic;
2000 p_cpo = save_cpo;
2001 return buf;
2002}
2003
2004/*
2005 * Get buffer by number or pattern.
2006 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002007 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002008tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002009{
2010 char_u *name = tv->vval.v_string;
2011 buf_T *buf;
2012
2013 if (tv->v_type == VAR_NUMBER)
2014 return buflist_findnr((int)tv->vval.v_number);
2015 if (tv->v_type != VAR_STRING)
2016 return NULL;
2017 if (name == NULL || *name == NUL)
2018 return curbuf;
2019 if (name[0] == '$' && name[1] == NUL)
2020 return lastbuf;
2021
2022 buf = buflist_find_by_name(name, curtab_only);
2023
2024 /* If not found, try expanding the name, like done for bufexists(). */
2025 if (buf == NULL)
2026 buf = find_buffer(tv);
2027
2028 return buf;
2029}
2030
2031/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002032 * Get the buffer from "arg" and give an error and return NULL if it is not
2033 * valid.
2034 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002035 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002036get_buf_arg(typval_T *arg)
2037{
2038 buf_T *buf;
2039
2040 ++emsg_off;
2041 buf = tv_get_buf(arg, FALSE);
2042 --emsg_off;
2043 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002044 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002045 return buf;
2046}
2047
2048/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002049 * "bufname(expr)" function
2050 */
2051 static void
2052f_bufname(typval_T *argvars, typval_T *rettv)
2053{
2054 buf_T *buf;
2055
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002056 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002057 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002058 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002059 rettv->v_type = VAR_STRING;
2060 if (buf != NULL && buf->b_fname != NULL)
2061 rettv->vval.v_string = vim_strsave(buf->b_fname);
2062 else
2063 rettv->vval.v_string = NULL;
2064 --emsg_off;
2065}
2066
2067/*
2068 * "bufnr(expr)" function
2069 */
2070 static void
2071f_bufnr(typval_T *argvars, typval_T *rettv)
2072{
2073 buf_T *buf;
2074 int error = FALSE;
2075 char_u *name;
2076
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002077 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002078 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002079 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002080 --emsg_off;
2081
2082 /* If the buffer isn't found and the second argument is not zero create a
2083 * new buffer. */
2084 if (buf == NULL
2085 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002086 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002087 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002088 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002089 && !error)
2090 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2091
2092 if (buf != NULL)
2093 rettv->vval.v_number = buf->b_fnum;
2094 else
2095 rettv->vval.v_number = -1;
2096}
2097
2098 static void
2099buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2100{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002101 win_T *wp;
2102 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002103 buf_T *buf;
2104
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002105 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002106 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002107 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002108 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002109 {
2110 ++winnr;
2111 if (wp->w_buffer == buf)
2112 break;
2113 }
2114 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002115 --emsg_off;
2116}
2117
2118/*
2119 * "bufwinid(nr)" function
2120 */
2121 static void
2122f_bufwinid(typval_T *argvars, typval_T *rettv)
2123{
2124 buf_win_common(argvars, rettv, FALSE);
2125}
2126
2127/*
2128 * "bufwinnr(nr)" function
2129 */
2130 static void
2131f_bufwinnr(typval_T *argvars, typval_T *rettv)
2132{
2133 buf_win_common(argvars, rettv, TRUE);
2134}
2135
2136/*
2137 * "byte2line(byte)" function
2138 */
2139 static void
2140f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2141{
2142#ifndef FEAT_BYTEOFF
2143 rettv->vval.v_number = -1;
2144#else
2145 long boff = 0;
2146
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002147 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002148 if (boff < 0)
2149 rettv->vval.v_number = -1;
2150 else
2151 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2152 (linenr_T)0, &boff);
2153#endif
2154}
2155
2156 static void
2157byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2158{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002159 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002160 char_u *str;
2161 varnumber_T idx;
2162
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002163 str = tv_get_string_chk(&argvars[0]);
2164 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002165 rettv->vval.v_number = -1;
2166 if (str == NULL || idx < 0)
2167 return;
2168
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002169 t = str;
2170 for ( ; idx > 0; idx--)
2171 {
2172 if (*t == NUL) /* EOL reached */
2173 return;
2174 if (enc_utf8 && comp)
2175 t += utf_ptr2len(t);
2176 else
2177 t += (*mb_ptr2len)(t);
2178 }
2179 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002180}
2181
2182/*
2183 * "byteidx()" function
2184 */
2185 static void
2186f_byteidx(typval_T *argvars, typval_T *rettv)
2187{
2188 byteidx(argvars, rettv, FALSE);
2189}
2190
2191/*
2192 * "byteidxcomp()" function
2193 */
2194 static void
2195f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2196{
2197 byteidx(argvars, rettv, TRUE);
2198}
2199
2200/*
2201 * "call(func, arglist [, dict])" function
2202 */
2203 static void
2204f_call(typval_T *argvars, typval_T *rettv)
2205{
2206 char_u *func;
2207 partial_T *partial = NULL;
2208 dict_T *selfdict = NULL;
2209
2210 if (argvars[1].v_type != VAR_LIST)
2211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002212 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002213 return;
2214 }
2215 if (argvars[1].vval.v_list == NULL)
2216 return;
2217
2218 if (argvars[0].v_type == VAR_FUNC)
2219 func = argvars[0].vval.v_string;
2220 else if (argvars[0].v_type == VAR_PARTIAL)
2221 {
2222 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002223 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002224 }
2225 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002226 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002227 if (*func == NUL)
2228 return; /* type error or empty name */
2229
2230 if (argvars[2].v_type != VAR_UNKNOWN)
2231 {
2232 if (argvars[2].v_type != VAR_DICT)
2233 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002234 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002235 return;
2236 }
2237 selfdict = argvars[2].vval.v_dict;
2238 }
2239
2240 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2241}
2242
2243#ifdef FEAT_FLOAT
2244/*
2245 * "ceil({float})" function
2246 */
2247 static void
2248f_ceil(typval_T *argvars, typval_T *rettv)
2249{
2250 float_T f = 0.0;
2251
2252 rettv->v_type = VAR_FLOAT;
2253 if (get_float_arg(argvars, &f) == OK)
2254 rettv->vval.v_float = ceil(f);
2255 else
2256 rettv->vval.v_float = 0.0;
2257}
2258#endif
2259
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002260/*
2261 * "changenr()" function
2262 */
2263 static void
2264f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2265{
2266 rettv->vval.v_number = curbuf->b_u_seq_cur;
2267}
2268
2269/*
2270 * "char2nr(string)" function
2271 */
2272 static void
2273f_char2nr(typval_T *argvars, typval_T *rettv)
2274{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002275 if (has_mbyte)
2276 {
2277 int utf8 = 0;
2278
2279 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002280 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002281
2282 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002283 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002284 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002285 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002286 }
2287 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002288 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002289}
2290
2291/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002292 * "chdir(dir)" function
2293 */
2294 static void
2295f_chdir(typval_T *argvars, typval_T *rettv)
2296{
2297 char_u *cwd;
2298 cdscope_T scope = CDSCOPE_GLOBAL;
2299
2300 rettv->v_type = VAR_STRING;
2301 rettv->vval.v_string = NULL;
2302
2303 if (argvars[0].v_type != VAR_STRING)
2304 return;
2305
2306 // Return the current directory
2307 cwd = alloc(MAXPATHL);
2308 if (cwd != NULL)
2309 {
2310 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2311 {
2312#ifdef BACKSLASH_IN_FILENAME
2313 slash_adjust(cwd);
2314#endif
2315 rettv->vval.v_string = vim_strsave(cwd);
2316 }
2317 vim_free(cwd);
2318 }
2319
2320 if (curwin->w_localdir != NULL)
2321 scope = CDSCOPE_WINDOW;
2322 else if (curtab->tp_localdir != NULL)
2323 scope = CDSCOPE_TABPAGE;
2324
2325 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2326 // Directory change failed
2327 VIM_CLEAR(rettv->vval.v_string);
2328}
2329
2330/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002331 * "cindent(lnum)" function
2332 */
2333 static void
2334f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2335{
2336#ifdef FEAT_CINDENT
2337 pos_T pos;
2338 linenr_T lnum;
2339
2340 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002341 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002342 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2343 {
2344 curwin->w_cursor.lnum = lnum;
2345 rettv->vval.v_number = get_c_indent();
2346 curwin->w_cursor = pos;
2347 }
2348 else
2349#endif
2350 rettv->vval.v_number = -1;
2351}
2352
Bram Moolenaaraff74912019-03-30 18:11:49 +01002353 static win_T *
2354get_optional_window(typval_T *argvars, int idx)
2355{
2356 win_T *win = curwin;
2357
2358 if (argvars[idx].v_type != VAR_UNKNOWN)
2359 {
2360 win = find_win_by_nr_or_id(&argvars[idx]);
2361 if (win == NULL)
2362 {
2363 emsg(_(e_invalwindow));
2364 return NULL;
2365 }
2366 }
2367 return win;
2368}
2369
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002370/*
2371 * "clearmatches()" function
2372 */
2373 static void
2374f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2375{
2376#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002377 win_T *win = get_optional_window(argvars, 0);
2378
2379 if (win != NULL)
2380 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002381#endif
2382}
2383
2384/*
2385 * "col(string)" function
2386 */
2387 static void
2388f_col(typval_T *argvars, typval_T *rettv)
2389{
2390 colnr_T col = 0;
2391 pos_T *fp;
2392 int fnum = curbuf->b_fnum;
2393
2394 fp = var2fpos(&argvars[0], FALSE, &fnum);
2395 if (fp != NULL && fnum == curbuf->b_fnum)
2396 {
2397 if (fp->col == MAXCOL)
2398 {
2399 /* '> can be MAXCOL, get the length of the line then */
2400 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2401 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2402 else
2403 col = MAXCOL;
2404 }
2405 else
2406 {
2407 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002408 /* col(".") when the cursor is on the NUL at the end of the line
2409 * because of "coladd" can be seen as an extra column. */
2410 if (virtual_active() && fp == &curwin->w_cursor)
2411 {
2412 char_u *p = ml_get_cursor();
2413
2414 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2415 curwin->w_virtcol - curwin->w_cursor.coladd))
2416 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002417 int l;
2418
2419 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2420 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002421 }
2422 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002423 }
2424 }
2425 rettv->vval.v_number = col;
2426}
2427
2428#if defined(FEAT_INS_EXPAND)
2429/*
2430 * "complete()" function
2431 */
2432 static void
2433f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2434{
2435 int startcol;
2436
2437 if ((State & INSERT) == 0)
2438 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002439 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002440 return;
2441 }
2442
2443 /* Check for undo allowed here, because if something was already inserted
2444 * the line was already saved for undo and this check isn't done. */
2445 if (!undo_allowed())
2446 return;
2447
2448 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002450 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002451 return;
2452 }
2453
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002454 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002455 if (startcol <= 0)
2456 return;
2457
2458 set_completion(startcol - 1, argvars[1].vval.v_list);
2459}
2460
2461/*
2462 * "complete_add()" function
2463 */
2464 static void
2465f_complete_add(typval_T *argvars, typval_T *rettv)
2466{
2467 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2468}
2469
2470/*
2471 * "complete_check()" function
2472 */
2473 static void
2474f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2475{
2476 int saved = RedrawingDisabled;
2477
2478 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002479 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002480 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002481 RedrawingDisabled = saved;
2482}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002483
2484/*
2485 * "complete_info()" function
2486 */
2487 static void
2488f_complete_info(typval_T *argvars, typval_T *rettv)
2489{
2490 list_T *what_list = NULL;
2491
2492 if (rettv_dict_alloc(rettv) != OK)
2493 return;
2494
2495 if (argvars[0].v_type != VAR_UNKNOWN)
2496 {
2497 if (argvars[0].v_type != VAR_LIST)
2498 {
2499 emsg(_(e_listreq));
2500 return;
2501 }
2502 what_list = argvars[0].vval.v_list;
2503 }
2504 get_complete_info(what_list, rettv->vval.v_dict);
2505}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002506#endif
2507
2508/*
2509 * "confirm(message, buttons[, default [, type]])" function
2510 */
2511 static void
2512f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2513{
2514#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2515 char_u *message;
2516 char_u *buttons = NULL;
2517 char_u buf[NUMBUFLEN];
2518 char_u buf2[NUMBUFLEN];
2519 int def = 1;
2520 int type = VIM_GENERIC;
2521 char_u *typestr;
2522 int error = FALSE;
2523
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002524 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002525 if (message == NULL)
2526 error = TRUE;
2527 if (argvars[1].v_type != VAR_UNKNOWN)
2528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002529 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002530 if (buttons == NULL)
2531 error = TRUE;
2532 if (argvars[2].v_type != VAR_UNKNOWN)
2533 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002534 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002535 if (argvars[3].v_type != VAR_UNKNOWN)
2536 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002537 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002538 if (typestr == NULL)
2539 error = TRUE;
2540 else
2541 {
2542 switch (TOUPPER_ASC(*typestr))
2543 {
2544 case 'E': type = VIM_ERROR; break;
2545 case 'Q': type = VIM_QUESTION; break;
2546 case 'I': type = VIM_INFO; break;
2547 case 'W': type = VIM_WARNING; break;
2548 case 'G': type = VIM_GENERIC; break;
2549 }
2550 }
2551 }
2552 }
2553 }
2554
2555 if (buttons == NULL || *buttons == NUL)
2556 buttons = (char_u *)_("&Ok");
2557
2558 if (!error)
2559 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2560 def, NULL, FALSE);
2561#endif
2562}
2563
2564/*
2565 * "copy()" function
2566 */
2567 static void
2568f_copy(typval_T *argvars, typval_T *rettv)
2569{
2570 item_copy(&argvars[0], rettv, FALSE, 0);
2571}
2572
2573#ifdef FEAT_FLOAT
2574/*
2575 * "cos()" function
2576 */
2577 static void
2578f_cos(typval_T *argvars, typval_T *rettv)
2579{
2580 float_T f = 0.0;
2581
2582 rettv->v_type = VAR_FLOAT;
2583 if (get_float_arg(argvars, &f) == OK)
2584 rettv->vval.v_float = cos(f);
2585 else
2586 rettv->vval.v_float = 0.0;
2587}
2588
2589/*
2590 * "cosh()" function
2591 */
2592 static void
2593f_cosh(typval_T *argvars, typval_T *rettv)
2594{
2595 float_T f = 0.0;
2596
2597 rettv->v_type = VAR_FLOAT;
2598 if (get_float_arg(argvars, &f) == OK)
2599 rettv->vval.v_float = cosh(f);
2600 else
2601 rettv->vval.v_float = 0.0;
2602}
2603#endif
2604
2605/*
2606 * "count()" function
2607 */
2608 static void
2609f_count(typval_T *argvars, typval_T *rettv)
2610{
2611 long n = 0;
2612 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002613 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002614
Bram Moolenaar9966b212017-07-28 16:46:57 +02002615 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002616 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002617
2618 if (argvars[0].v_type == VAR_STRING)
2619 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002620 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002621 char_u *p = argvars[0].vval.v_string;
2622 char_u *next;
2623
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002624 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002625 {
2626 if (ic)
2627 {
2628 size_t len = STRLEN(expr);
2629
2630 while (*p != NUL)
2631 {
2632 if (MB_STRNICMP(p, expr, len) == 0)
2633 {
2634 ++n;
2635 p += len;
2636 }
2637 else
2638 MB_PTR_ADV(p);
2639 }
2640 }
2641 else
2642 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2643 != NULL)
2644 {
2645 ++n;
2646 p = next + STRLEN(expr);
2647 }
2648 }
2649
2650 }
2651 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002652 {
2653 listitem_T *li;
2654 list_T *l;
2655 long idx;
2656
2657 if ((l = argvars[0].vval.v_list) != NULL)
2658 {
2659 li = l->lv_first;
2660 if (argvars[2].v_type != VAR_UNKNOWN)
2661 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002662 if (argvars[3].v_type != VAR_UNKNOWN)
2663 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002664 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002665 if (!error)
2666 {
2667 li = list_find(l, idx);
2668 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002669 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002670 }
2671 }
2672 if (error)
2673 li = NULL;
2674 }
2675
2676 for ( ; li != NULL; li = li->li_next)
2677 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2678 ++n;
2679 }
2680 }
2681 else if (argvars[0].v_type == VAR_DICT)
2682 {
2683 int todo;
2684 dict_T *d;
2685 hashitem_T *hi;
2686
2687 if ((d = argvars[0].vval.v_dict) != NULL)
2688 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002689 if (argvars[2].v_type != VAR_UNKNOWN)
2690 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002691 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002692 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002693 }
2694
2695 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2696 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2697 {
2698 if (!HASHITEM_EMPTY(hi))
2699 {
2700 --todo;
2701 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2702 ++n;
2703 }
2704 }
2705 }
2706 }
2707 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002708 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002709 rettv->vval.v_number = n;
2710}
2711
2712/*
2713 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2714 *
2715 * Checks the existence of a cscope connection.
2716 */
2717 static void
2718f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2719{
2720#ifdef FEAT_CSCOPE
2721 int num = 0;
2722 char_u *dbpath = NULL;
2723 char_u *prepend = NULL;
2724 char_u buf[NUMBUFLEN];
2725
2726 if (argvars[0].v_type != VAR_UNKNOWN
2727 && argvars[1].v_type != VAR_UNKNOWN)
2728 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002729 num = (int)tv_get_number(&argvars[0]);
2730 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002731 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002732 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002733 }
2734
2735 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2736#endif
2737}
2738
2739/*
2740 * "cursor(lnum, col)" function, or
2741 * "cursor(list)"
2742 *
2743 * Moves the cursor to the specified line and column.
2744 * Returns 0 when the position could be set, -1 otherwise.
2745 */
2746 static void
2747f_cursor(typval_T *argvars, typval_T *rettv)
2748{
2749 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002750 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002751 int set_curswant = TRUE;
2752
2753 rettv->vval.v_number = -1;
2754 if (argvars[1].v_type == VAR_UNKNOWN)
2755 {
2756 pos_T pos;
2757 colnr_T curswant = -1;
2758
2759 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2760 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002761 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002762 return;
2763 }
2764 line = pos.lnum;
2765 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002767 if (curswant >= 0)
2768 {
2769 curwin->w_curswant = curswant - 1;
2770 set_curswant = FALSE;
2771 }
2772 }
2773 else
2774 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002775 line = tv_get_lnum(argvars);
2776 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002777 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002778 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002779 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002780 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002781 return; /* type error; errmsg already given */
2782 if (line > 0)
2783 curwin->w_cursor.lnum = line;
2784 if (col > 0)
2785 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002786 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002787
2788 /* Make sure the cursor is in a valid position. */
2789 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 /* Correct cursor for multi-byte character. */
2791 if (has_mbyte)
2792 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002793
2794 curwin->w_set_curswant = set_curswant;
2795 rettv->vval.v_number = 0;
2796}
2797
Bram Moolenaar4f974752019-02-17 17:44:42 +01002798#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002799/*
2800 * "debugbreak()" function
2801 */
2802 static void
2803f_debugbreak(typval_T *argvars, typval_T *rettv)
2804{
2805 int pid;
2806
2807 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002808 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002809 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002810 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002811 else
2812 {
2813 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2814
2815 if (hProcess != NULL)
2816 {
2817 DebugBreakProcess(hProcess);
2818 CloseHandle(hProcess);
2819 rettv->vval.v_number = OK;
2820 }
2821 }
2822}
2823#endif
2824
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002825/*
2826 * "deepcopy()" function
2827 */
2828 static void
2829f_deepcopy(typval_T *argvars, typval_T *rettv)
2830{
2831 int noref = 0;
2832 int copyID;
2833
2834 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002835 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002836 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002837 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002838 else
2839 {
2840 copyID = get_copyID();
2841 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2842 }
2843}
2844
2845/*
2846 * "delete()" function
2847 */
2848 static void
2849f_delete(typval_T *argvars, typval_T *rettv)
2850{
2851 char_u nbuf[NUMBUFLEN];
2852 char_u *name;
2853 char_u *flags;
2854
2855 rettv->vval.v_number = -1;
2856 if (check_restricted() || check_secure())
2857 return;
2858
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002859 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002860 if (name == NULL || *name == NUL)
2861 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002862 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002863 return;
2864 }
2865
2866 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002867 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002868 else
2869 flags = (char_u *)"";
2870
2871 if (*flags == NUL)
2872 /* delete a file */
2873 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2874 else if (STRCMP(flags, "d") == 0)
2875 /* delete an empty directory */
2876 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2877 else if (STRCMP(flags, "rf") == 0)
2878 /* delete a directory recursively */
2879 rettv->vval.v_number = delete_recursive(name);
2880 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002881 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002882}
2883
2884/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002885 * "deletebufline()" function
2886 */
2887 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002888f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002889{
2890 buf_T *buf;
2891 linenr_T first, last;
2892 linenr_T lnum;
2893 long count;
2894 int is_curbuf;
2895 buf_T *curbuf_save = NULL;
2896 win_T *curwin_save = NULL;
2897 tabpage_T *tp;
2898 win_T *wp;
2899
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002900 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002901 if (buf == NULL)
2902 {
2903 rettv->vval.v_number = 1; /* FAIL */
2904 return;
2905 }
2906 is_curbuf = buf == curbuf;
2907
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002908 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002909 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002910 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002911 else
2912 last = first;
2913
2914 if (buf->b_ml.ml_mfp == NULL || first < 1
2915 || first > buf->b_ml.ml_line_count || last < first)
2916 {
2917 rettv->vval.v_number = 1; /* FAIL */
2918 return;
2919 }
2920
2921 if (!is_curbuf)
2922 {
2923 curbuf_save = curbuf;
2924 curwin_save = curwin;
2925 curbuf = buf;
2926 find_win_for_curbuf();
2927 }
2928 if (last > curbuf->b_ml.ml_line_count)
2929 last = curbuf->b_ml.ml_line_count;
2930 count = last - first + 1;
2931
2932 // When coming here from Insert mode, sync undo, so that this can be
2933 // undone separately from what was previously inserted.
2934 if (u_sync_once == 2)
2935 {
2936 u_sync_once = 1; // notify that u_sync() was called
2937 u_sync(TRUE);
2938 }
2939
2940 if (u_save(first - 1, last + 1) == FAIL)
2941 {
2942 rettv->vval.v_number = 1; /* FAIL */
2943 return;
2944 }
2945
2946 for (lnum = first; lnum <= last; ++lnum)
2947 ml_delete(first, TRUE);
2948
2949 FOR_ALL_TAB_WINDOWS(tp, wp)
2950 if (wp->w_buffer == buf)
2951 {
2952 if (wp->w_cursor.lnum > last)
2953 wp->w_cursor.lnum -= count;
2954 else if (wp->w_cursor.lnum> first)
2955 wp->w_cursor.lnum = first;
2956 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2957 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2958 }
2959 check_cursor_col();
2960 deleted_lines_mark(first, count);
2961
2962 if (!is_curbuf)
2963 {
2964 curbuf = curbuf_save;
2965 curwin = curwin_save;
2966 }
2967}
2968
2969/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002970 * "did_filetype()" function
2971 */
2972 static void
2973f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2974{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002975 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002976}
2977
2978/*
2979 * "diff_filler()" function
2980 */
2981 static void
2982f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2983{
2984#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002985 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002986#endif
2987}
2988
2989/*
2990 * "diff_hlID()" function
2991 */
2992 static void
2993f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2994{
2995#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002996 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002997 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002998 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002999 static int fnum = 0;
3000 static int change_start = 0;
3001 static int change_end = 0;
3002 static hlf_T hlID = (hlf_T)0;
3003 int filler_lines;
3004 int col;
3005
3006 if (lnum < 0) /* ignore type error in {lnum} arg */
3007 lnum = 0;
3008 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003009 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003010 || fnum != curbuf->b_fnum)
3011 {
3012 /* New line, buffer, change: need to get the values. */
3013 filler_lines = diff_check(curwin, lnum);
3014 if (filler_lines < 0)
3015 {
3016 if (filler_lines == -1)
3017 {
3018 change_start = MAXCOL;
3019 change_end = -1;
3020 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3021 hlID = HLF_ADD; /* added line */
3022 else
3023 hlID = HLF_CHD; /* changed line */
3024 }
3025 else
3026 hlID = HLF_ADD; /* added line */
3027 }
3028 else
3029 hlID = (hlf_T)0;
3030 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003031 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003032 fnum = curbuf->b_fnum;
3033 }
3034
3035 if (hlID == HLF_CHD || hlID == HLF_TXD)
3036 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003037 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038 if (col >= change_start && col <= change_end)
3039 hlID = HLF_TXD; /* changed text */
3040 else
3041 hlID = HLF_CHD; /* changed line */
3042 }
3043 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3044#endif
3045}
3046
3047/*
3048 * "empty({expr})" function
3049 */
3050 static void
3051f_empty(typval_T *argvars, typval_T *rettv)
3052{
3053 int n = FALSE;
3054
3055 switch (argvars[0].v_type)
3056 {
3057 case VAR_STRING:
3058 case VAR_FUNC:
3059 n = argvars[0].vval.v_string == NULL
3060 || *argvars[0].vval.v_string == NUL;
3061 break;
3062 case VAR_PARTIAL:
3063 n = FALSE;
3064 break;
3065 case VAR_NUMBER:
3066 n = argvars[0].vval.v_number == 0;
3067 break;
3068 case VAR_FLOAT:
3069#ifdef FEAT_FLOAT
3070 n = argvars[0].vval.v_float == 0.0;
3071 break;
3072#endif
3073 case VAR_LIST:
3074 n = argvars[0].vval.v_list == NULL
3075 || argvars[0].vval.v_list->lv_first == NULL;
3076 break;
3077 case VAR_DICT:
3078 n = argvars[0].vval.v_dict == NULL
3079 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3080 break;
3081 case VAR_SPECIAL:
3082 n = argvars[0].vval.v_number != VVAL_TRUE;
3083 break;
3084
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003085 case VAR_BLOB:
3086 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003087 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3088 break;
3089
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003090 case VAR_JOB:
3091#ifdef FEAT_JOB_CHANNEL
3092 n = argvars[0].vval.v_job == NULL
3093 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3094 break;
3095#endif
3096 case VAR_CHANNEL:
3097#ifdef FEAT_JOB_CHANNEL
3098 n = argvars[0].vval.v_channel == NULL
3099 || !channel_is_open(argvars[0].vval.v_channel);
3100 break;
3101#endif
3102 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003103 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003104 n = TRUE;
3105 break;
3106 }
3107
3108 rettv->vval.v_number = n;
3109}
3110
3111/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003112 * "environ()" function
3113 */
3114 static void
3115f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3116{
3117#if !defined(AMIGA)
3118 int i = 0;
3119 char_u *entry, *value;
3120# ifdef MSWIN
3121 extern wchar_t **_wenviron;
3122# else
3123 extern char **environ;
3124# endif
3125
3126 if (rettv_dict_alloc(rettv) != OK)
3127 return;
3128
3129# ifdef MSWIN
3130 if (*_wenviron == NULL)
3131 return;
3132# else
3133 if (*environ == NULL)
3134 return;
3135# endif
3136
3137 for (i = 0; ; ++i)
3138 {
3139# ifdef MSWIN
3140 short_u *p;
3141
3142 if ((p = (short_u *)_wenviron[i]) == NULL)
3143 return;
3144 entry = utf16_to_enc(p, NULL);
3145# else
3146 if ((entry = (char_u *)environ[i]) == NULL)
3147 return;
3148 entry = vim_strsave(entry);
3149# endif
3150 if (entry == NULL) // out of memory
3151 return;
3152 if ((value = vim_strchr(entry, '=')) == NULL)
3153 {
3154 vim_free(entry);
3155 continue;
3156 }
3157 *value++ = NUL;
3158 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3159 vim_free(entry);
3160 }
3161#endif
3162}
3163
3164/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003165 * "escape({string}, {chars})" function
3166 */
3167 static void
3168f_escape(typval_T *argvars, typval_T *rettv)
3169{
3170 char_u buf[NUMBUFLEN];
3171
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003172 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3173 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003174 rettv->v_type = VAR_STRING;
3175}
3176
3177/*
3178 * "eval()" function
3179 */
3180 static void
3181f_eval(typval_T *argvars, typval_T *rettv)
3182{
3183 char_u *s, *p;
3184
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003185 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003186 if (s != NULL)
3187 s = skipwhite(s);
3188
3189 p = s;
3190 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3191 {
3192 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003193 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003194 need_clr_eos = FALSE;
3195 rettv->v_type = VAR_NUMBER;
3196 rettv->vval.v_number = 0;
3197 }
3198 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003199 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003200}
3201
3202/*
3203 * "eventhandler()" function
3204 */
3205 static void
3206f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3207{
3208 rettv->vval.v_number = vgetc_busy;
3209}
3210
3211/*
3212 * "executable()" function
3213 */
3214 static void
3215f_executable(typval_T *argvars, typval_T *rettv)
3216{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003217 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003218
3219 /* Check in $PATH and also check directly if there is a directory name. */
3220 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3221 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3222}
3223
3224static garray_T redir_execute_ga;
3225
3226/*
3227 * Append "value[value_len]" to the execute() output.
3228 */
3229 void
3230execute_redir_str(char_u *value, int value_len)
3231{
3232 int len;
3233
3234 if (value_len == -1)
3235 len = (int)STRLEN(value); /* Append the entire string */
3236 else
3237 len = value_len; /* Append only "value_len" characters */
3238 if (ga_grow(&redir_execute_ga, len) == OK)
3239 {
3240 mch_memmove((char *)redir_execute_ga.ga_data
3241 + redir_execute_ga.ga_len, value, len);
3242 redir_execute_ga.ga_len += len;
3243 }
3244}
3245
3246/*
3247 * Get next line from a list.
3248 * Called by do_cmdline() to get the next line.
3249 * Returns allocated string, or NULL for end of function.
3250 */
3251
3252 static char_u *
3253get_list_line(
3254 int c UNUSED,
3255 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003256 int indent UNUSED,
3257 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258{
3259 listitem_T **p = (listitem_T **)cookie;
3260 listitem_T *item = *p;
3261 char_u buf[NUMBUFLEN];
3262 char_u *s;
3263
3264 if (item == NULL)
3265 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003266 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003267 *p = item->li_next;
3268 return s == NULL ? NULL : vim_strsave(s);
3269}
3270
3271/*
3272 * "execute()" function
3273 */
3274 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003275execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003276{
3277 char_u *cmd = NULL;
3278 list_T *list = NULL;
3279 int save_msg_silent = msg_silent;
3280 int save_emsg_silent = emsg_silent;
3281 int save_emsg_noredir = emsg_noredir;
3282 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003283 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003284 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003285 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003286 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003287
3288 rettv->vval.v_string = NULL;
3289 rettv->v_type = VAR_STRING;
3290
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003291 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003292 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003293 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003294 if (list == NULL || list->lv_first == NULL)
3295 /* empty list, no commands, empty output */
3296 return;
3297 ++list->lv_refcount;
3298 }
3299 else
3300 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003301 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003302 if (cmd == NULL)
3303 return;
3304 }
3305
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003306 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003307 {
3308 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003309 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003310
3311 if (s == NULL)
3312 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003313 if (*s == NUL)
3314 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003315 if (STRNCMP(s, "silent", 6) == 0)
3316 ++msg_silent;
3317 if (STRCMP(s, "silent!") == 0)
3318 {
3319 emsg_silent = TRUE;
3320 emsg_noredir = TRUE;
3321 }
3322 }
3323 else
3324 ++msg_silent;
3325
3326 if (redir_execute)
3327 save_ga = redir_execute_ga;
3328 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3329 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003330 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003331 if (!echo_output)
3332 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003333
3334 if (cmd != NULL)
3335 do_cmdline_cmd(cmd);
3336 else
3337 {
3338 listitem_T *item = list->lv_first;
3339
3340 do_cmdline(NULL, get_list_line, (void *)&item,
3341 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3342 --list->lv_refcount;
3343 }
3344
Bram Moolenaard297f352017-01-29 20:31:21 +01003345 /* Need to append a NUL to the result. */
3346 if (ga_grow(&redir_execute_ga, 1) == OK)
3347 {
3348 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3349 rettv->vval.v_string = redir_execute_ga.ga_data;
3350 }
3351 else
3352 {
3353 ga_clear(&redir_execute_ga);
3354 rettv->vval.v_string = NULL;
3355 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003356 msg_silent = save_msg_silent;
3357 emsg_silent = save_emsg_silent;
3358 emsg_noredir = save_emsg_noredir;
3359
3360 redir_execute = save_redir_execute;
3361 if (redir_execute)
3362 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003363 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003364
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003365 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003366 if (echo_output)
3367 // When not working silently: put it in column zero. A following
3368 // "echon" will overwrite the message, unavoidably.
3369 msg_col = 0;
3370 else
3371 // When working silently: Put it back where it was, since nothing
3372 // should have been written.
3373 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003374}
3375
3376/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003377 * "execute()" function
3378 */
3379 static void
3380f_execute(typval_T *argvars, typval_T *rettv)
3381{
3382 execute_common(argvars, rettv, 0);
3383}
3384
3385/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003386 * "exepath()" function
3387 */
3388 static void
3389f_exepath(typval_T *argvars, typval_T *rettv)
3390{
3391 char_u *p = NULL;
3392
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003393 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003394 rettv->v_type = VAR_STRING;
3395 rettv->vval.v_string = p;
3396}
3397
3398/*
3399 * "exists()" function
3400 */
3401 static void
3402f_exists(typval_T *argvars, typval_T *rettv)
3403{
3404 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003406
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003407 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003408 if (*p == '$') /* environment variable */
3409 {
3410 /* first try "normal" environment variables (fast) */
3411 if (mch_getenv(p + 1) != NULL)
3412 n = TRUE;
3413 else
3414 {
3415 /* try expanding things like $VIM and ${HOME} */
3416 p = expand_env_save(p);
3417 if (p != NULL && *p != '$')
3418 n = TRUE;
3419 vim_free(p);
3420 }
3421 }
3422 else if (*p == '&' || *p == '+') /* option */
3423 {
3424 n = (get_option_tv(&p, NULL, TRUE) == OK);
3425 if (*skipwhite(p) != NUL)
3426 n = FALSE; /* trailing garbage */
3427 }
3428 else if (*p == '*') /* internal or user defined function */
3429 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003430 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003431 }
3432 else if (*p == ':')
3433 {
3434 n = cmd_exists(p + 1);
3435 }
3436 else if (*p == '#')
3437 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003438 if (p[1] == '#')
3439 n = autocmd_supported(p + 2);
3440 else
3441 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003442 }
3443 else /* internal variable */
3444 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003445 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003446 }
3447
3448 rettv->vval.v_number = n;
3449}
3450
3451#ifdef FEAT_FLOAT
3452/*
3453 * "exp()" function
3454 */
3455 static void
3456f_exp(typval_T *argvars, typval_T *rettv)
3457{
3458 float_T f = 0.0;
3459
3460 rettv->v_type = VAR_FLOAT;
3461 if (get_float_arg(argvars, &f) == OK)
3462 rettv->vval.v_float = exp(f);
3463 else
3464 rettv->vval.v_float = 0.0;
3465}
3466#endif
3467
3468/*
3469 * "expand()" function
3470 */
3471 static void
3472f_expand(typval_T *argvars, typval_T *rettv)
3473{
3474 char_u *s;
3475 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003476 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003477 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3478 expand_T xpc;
3479 int error = FALSE;
3480 char_u *result;
3481
3482 rettv->v_type = VAR_STRING;
3483 if (argvars[1].v_type != VAR_UNKNOWN
3484 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003485 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003486 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003487 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003488
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003489 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003490 if (*s == '%' || *s == '#' || *s == '<')
3491 {
3492 ++emsg_off;
3493 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3494 --emsg_off;
3495 if (rettv->v_type == VAR_LIST)
3496 {
3497 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3498 list_append_string(rettv->vval.v_list, result, -1);
3499 else
3500 vim_free(result);
3501 }
3502 else
3503 rettv->vval.v_string = result;
3504 }
3505 else
3506 {
3507 /* When the optional second argument is non-zero, don't remove matches
3508 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3509 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003510 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003511 options |= WILD_KEEP_ALL;
3512 if (!error)
3513 {
3514 ExpandInit(&xpc);
3515 xpc.xp_context = EXPAND_FILES;
3516 if (p_wic)
3517 options += WILD_ICASE;
3518 if (rettv->v_type == VAR_STRING)
3519 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3520 options, WILD_ALL);
3521 else if (rettv_list_alloc(rettv) != FAIL)
3522 {
3523 int i;
3524
3525 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3526 for (i = 0; i < xpc.xp_numfiles; i++)
3527 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3528 ExpandCleanup(&xpc);
3529 }
3530 }
3531 else
3532 rettv->vval.v_string = NULL;
3533 }
3534}
3535
3536/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003537 * "expandcmd()" function
3538 * Expand all the special characters in a command string.
3539 */
3540 static void
3541f_expandcmd(typval_T *argvars, typval_T *rettv)
3542{
3543 exarg_T eap;
3544 char_u *cmdstr;
3545 char *errormsg = NULL;
3546
3547 rettv->v_type = VAR_STRING;
3548 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3549
3550 memset(&eap, 0, sizeof(eap));
3551 eap.cmd = cmdstr;
3552 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003553 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003554 eap.usefilter = FALSE;
3555 eap.nextcmd = NULL;
3556 eap.cmdidx = CMD_USER;
3557
3558 expand_filename(&eap, &cmdstr, &errormsg);
3559 if (errormsg != NULL && *errormsg != NUL)
3560 emsg(errormsg);
3561
3562 rettv->vval.v_string = cmdstr;
3563}
3564
3565/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003566 * "extend(list, list [, idx])" function
3567 * "extend(dict, dict [, action])" function
3568 */
3569 static void
3570f_extend(typval_T *argvars, typval_T *rettv)
3571{
3572 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3573
3574 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3575 {
3576 list_T *l1, *l2;
3577 listitem_T *item;
3578 long before;
3579 int error = FALSE;
3580
3581 l1 = argvars[0].vval.v_list;
3582 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003583 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003584 && l2 != NULL)
3585 {
3586 if (argvars[2].v_type != VAR_UNKNOWN)
3587 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003588 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003589 if (error)
3590 return; /* type error; errmsg already given */
3591
3592 if (before == l1->lv_len)
3593 item = NULL;
3594 else
3595 {
3596 item = list_find(l1, before);
3597 if (item == NULL)
3598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003599 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003600 return;
3601 }
3602 }
3603 }
3604 else
3605 item = NULL;
3606 list_extend(l1, l2, item);
3607
3608 copy_tv(&argvars[0], rettv);
3609 }
3610 }
3611 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3612 {
3613 dict_T *d1, *d2;
3614 char_u *action;
3615 int i;
3616
3617 d1 = argvars[0].vval.v_dict;
3618 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003619 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003620 && d2 != NULL)
3621 {
3622 /* Check the third argument. */
3623 if (argvars[2].v_type != VAR_UNKNOWN)
3624 {
3625 static char *(av[]) = {"keep", "force", "error"};
3626
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003627 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003628 if (action == NULL)
3629 return; /* type error; errmsg already given */
3630 for (i = 0; i < 3; ++i)
3631 if (STRCMP(action, av[i]) == 0)
3632 break;
3633 if (i == 3)
3634 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003635 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003636 return;
3637 }
3638 }
3639 else
3640 action = (char_u *)"force";
3641
3642 dict_extend(d1, d2, action);
3643
3644 copy_tv(&argvars[0], rettv);
3645 }
3646 }
3647 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003648 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003649}
3650
3651/*
3652 * "feedkeys()" function
3653 */
3654 static void
3655f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3656{
3657 int remap = TRUE;
3658 int insert = FALSE;
3659 char_u *keys, *flags;
3660 char_u nbuf[NUMBUFLEN];
3661 int typed = FALSE;
3662 int execute = FALSE;
3663 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003664 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003665 char_u *keys_esc;
3666
3667 /* This is not allowed in the sandbox. If the commands would still be
3668 * executed in the sandbox it would be OK, but it probably happens later,
3669 * when "sandbox" is no longer set. */
3670 if (check_secure())
3671 return;
3672
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003673 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003674
3675 if (argvars[1].v_type != VAR_UNKNOWN)
3676 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003677 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003678 for ( ; *flags != NUL; ++flags)
3679 {
3680 switch (*flags)
3681 {
3682 case 'n': remap = FALSE; break;
3683 case 'm': remap = TRUE; break;
3684 case 't': typed = TRUE; break;
3685 case 'i': insert = TRUE; break;
3686 case 'x': execute = TRUE; break;
3687 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003688 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003689 }
3690 }
3691 }
3692
3693 if (*keys != NUL || execute)
3694 {
3695 /* Need to escape K_SPECIAL and CSI before putting the string in the
3696 * typeahead buffer. */
3697 keys_esc = vim_strsave_escape_csi(keys);
3698 if (keys_esc != NULL)
3699 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003700 if (lowlevel)
3701 {
3702#ifdef USE_INPUT_BUF
3703 add_to_input_buf(keys, (int)STRLEN(keys));
3704#else
3705 emsg(_("E980: lowlevel input not supported"));
3706#endif
3707 }
3708 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003709 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003710 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003711 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003712 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003713#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003714 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003715#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003716 )
3717 typebuf_was_filled = TRUE;
3718 }
3719 vim_free(keys_esc);
3720
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003721 if (execute)
3722 {
3723 int save_msg_scroll = msg_scroll;
3724
3725 /* Avoid a 1 second delay when the keys start Insert mode. */
3726 msg_scroll = FALSE;
3727
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003728 if (!dangerous)
3729 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003730 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003731 if (!dangerous)
3732 --ex_normal_busy;
3733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003734 msg_scroll |= save_msg_scroll;
3735 }
3736 }
3737 }
3738}
3739
3740/*
3741 * "filereadable()" function
3742 */
3743 static void
3744f_filereadable(typval_T *argvars, typval_T *rettv)
3745{
3746 int fd;
3747 char_u *p;
3748 int n;
3749
3750#ifndef O_NONBLOCK
3751# define O_NONBLOCK 0
3752#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003753 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3755 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3756 {
3757 n = TRUE;
3758 close(fd);
3759 }
3760 else
3761 n = FALSE;
3762
3763 rettv->vval.v_number = n;
3764}
3765
3766/*
3767 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3768 * rights to write into.
3769 */
3770 static void
3771f_filewritable(typval_T *argvars, typval_T *rettv)
3772{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003773 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003774}
3775
3776 static void
3777findfilendir(
3778 typval_T *argvars UNUSED,
3779 typval_T *rettv,
3780 int find_what UNUSED)
3781{
3782#ifdef FEAT_SEARCHPATH
3783 char_u *fname;
3784 char_u *fresult = NULL;
3785 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3786 char_u *p;
3787 char_u pathbuf[NUMBUFLEN];
3788 int count = 1;
3789 int first = TRUE;
3790 int error = FALSE;
3791#endif
3792
3793 rettv->vval.v_string = NULL;
3794 rettv->v_type = VAR_STRING;
3795
3796#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003797 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003798
3799 if (argvars[1].v_type != VAR_UNKNOWN)
3800 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003801 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003802 if (p == NULL)
3803 error = TRUE;
3804 else
3805 {
3806 if (*p != NUL)
3807 path = p;
3808
3809 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003810 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003811 }
3812 }
3813
3814 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3815 error = TRUE;
3816
3817 if (*fname != NUL && !error)
3818 {
3819 do
3820 {
3821 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3822 vim_free(fresult);
3823 fresult = find_file_in_path_option(first ? fname : NULL,
3824 first ? (int)STRLEN(fname) : 0,
3825 0, first, path,
3826 find_what,
3827 curbuf->b_ffname,
3828 find_what == FINDFILE_DIR
3829 ? (char_u *)"" : curbuf->b_p_sua);
3830 first = FALSE;
3831
3832 if (fresult != NULL && rettv->v_type == VAR_LIST)
3833 list_append_string(rettv->vval.v_list, fresult, -1);
3834
3835 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3836 }
3837
3838 if (rettv->v_type == VAR_STRING)
3839 rettv->vval.v_string = fresult;
3840#endif
3841}
3842
3843/*
3844 * "filter()" function
3845 */
3846 static void
3847f_filter(typval_T *argvars, typval_T *rettv)
3848{
3849 filter_map(argvars, rettv, FALSE);
3850}
3851
3852/*
3853 * "finddir({fname}[, {path}[, {count}]])" function
3854 */
3855 static void
3856f_finddir(typval_T *argvars, typval_T *rettv)
3857{
3858 findfilendir(argvars, rettv, FINDFILE_DIR);
3859}
3860
3861/*
3862 * "findfile({fname}[, {path}[, {count}]])" function
3863 */
3864 static void
3865f_findfile(typval_T *argvars, typval_T *rettv)
3866{
3867 findfilendir(argvars, rettv, FINDFILE_FILE);
3868}
3869
3870#ifdef FEAT_FLOAT
3871/*
3872 * "float2nr({float})" function
3873 */
3874 static void
3875f_float2nr(typval_T *argvars, typval_T *rettv)
3876{
3877 float_T f = 0.0;
3878
3879 if (get_float_arg(argvars, &f) == OK)
3880 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003881 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003882 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003883 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003884 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003885 else
3886 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003887 }
3888}
3889
3890/*
3891 * "floor({float})" function
3892 */
3893 static void
3894f_floor(typval_T *argvars, typval_T *rettv)
3895{
3896 float_T f = 0.0;
3897
3898 rettv->v_type = VAR_FLOAT;
3899 if (get_float_arg(argvars, &f) == OK)
3900 rettv->vval.v_float = floor(f);
3901 else
3902 rettv->vval.v_float = 0.0;
3903}
3904
3905/*
3906 * "fmod()" function
3907 */
3908 static void
3909f_fmod(typval_T *argvars, typval_T *rettv)
3910{
3911 float_T fx = 0.0, fy = 0.0;
3912
3913 rettv->v_type = VAR_FLOAT;
3914 if (get_float_arg(argvars, &fx) == OK
3915 && get_float_arg(&argvars[1], &fy) == OK)
3916 rettv->vval.v_float = fmod(fx, fy);
3917 else
3918 rettv->vval.v_float = 0.0;
3919}
3920#endif
3921
3922/*
3923 * "fnameescape({string})" function
3924 */
3925 static void
3926f_fnameescape(typval_T *argvars, typval_T *rettv)
3927{
3928 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003929 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003930 rettv->v_type = VAR_STRING;
3931}
3932
3933/*
3934 * "fnamemodify({fname}, {mods})" function
3935 */
3936 static void
3937f_fnamemodify(typval_T *argvars, typval_T *rettv)
3938{
3939 char_u *fname;
3940 char_u *mods;
3941 int usedlen = 0;
3942 int len;
3943 char_u *fbuf = NULL;
3944 char_u buf[NUMBUFLEN];
3945
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003946 fname = tv_get_string_chk(&argvars[0]);
3947 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003948 if (fname == NULL || mods == NULL)
3949 fname = NULL;
3950 else
3951 {
3952 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003953 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003954 }
3955
3956 rettv->v_type = VAR_STRING;
3957 if (fname == NULL)
3958 rettv->vval.v_string = NULL;
3959 else
3960 rettv->vval.v_string = vim_strnsave(fname, len);
3961 vim_free(fbuf);
3962}
3963
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003964/*
3965 * "foldclosed()" function
3966 */
3967 static void
3968foldclosed_both(
3969 typval_T *argvars UNUSED,
3970 typval_T *rettv,
3971 int end UNUSED)
3972{
3973#ifdef FEAT_FOLDING
3974 linenr_T lnum;
3975 linenr_T first, last;
3976
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003977 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003978 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3979 {
3980 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3981 {
3982 if (end)
3983 rettv->vval.v_number = (varnumber_T)last;
3984 else
3985 rettv->vval.v_number = (varnumber_T)first;
3986 return;
3987 }
3988 }
3989#endif
3990 rettv->vval.v_number = -1;
3991}
3992
3993/*
3994 * "foldclosed()" function
3995 */
3996 static void
3997f_foldclosed(typval_T *argvars, typval_T *rettv)
3998{
3999 foldclosed_both(argvars, rettv, FALSE);
4000}
4001
4002/*
4003 * "foldclosedend()" function
4004 */
4005 static void
4006f_foldclosedend(typval_T *argvars, typval_T *rettv)
4007{
4008 foldclosed_both(argvars, rettv, TRUE);
4009}
4010
4011/*
4012 * "foldlevel()" function
4013 */
4014 static void
4015f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4016{
4017#ifdef FEAT_FOLDING
4018 linenr_T lnum;
4019
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004020 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004021 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4022 rettv->vval.v_number = foldLevel(lnum);
4023#endif
4024}
4025
4026/*
4027 * "foldtext()" function
4028 */
4029 static void
4030f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4031{
4032#ifdef FEAT_FOLDING
4033 linenr_T foldstart;
4034 linenr_T foldend;
4035 char_u *dashes;
4036 linenr_T lnum;
4037 char_u *s;
4038 char_u *r;
4039 int len;
4040 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004041 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004042#endif
4043
4044 rettv->v_type = VAR_STRING;
4045 rettv->vval.v_string = NULL;
4046#ifdef FEAT_FOLDING
4047 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4048 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4049 dashes = get_vim_var_str(VV_FOLDDASHES);
4050 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4051 && dashes != NULL)
4052 {
4053 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004054 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004055 if (!linewhite(lnum))
4056 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004057
4058 /* Find interesting text in this line. */
4059 s = skipwhite(ml_get(lnum));
4060 /* skip C comment-start */
4061 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4062 {
4063 s = skipwhite(s + 2);
4064 if (*skipwhite(s) == NUL
4065 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4066 {
4067 s = skipwhite(ml_get(lnum + 1));
4068 if (*s == '*')
4069 s = skipwhite(s + 1);
4070 }
4071 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004072 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004073 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004074 r = alloc(STRLEN(txt)
4075 + STRLEN(dashes) // for %s
4076 + 20 // for %3ld
4077 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004078 if (r != NULL)
4079 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004080 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004081 len = (int)STRLEN(r);
4082 STRCAT(r, s);
4083 /* remove 'foldmarker' and 'commentstring' */
4084 foldtext_cleanup(r + len);
4085 rettv->vval.v_string = r;
4086 }
4087 }
4088#endif
4089}
4090
4091/*
4092 * "foldtextresult(lnum)" function
4093 */
4094 static void
4095f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4096{
4097#ifdef FEAT_FOLDING
4098 linenr_T lnum;
4099 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004100 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004101 foldinfo_T foldinfo;
4102 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004103 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004104#endif
4105
4106 rettv->v_type = VAR_STRING;
4107 rettv->vval.v_string = NULL;
4108#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004109 if (entered)
4110 return; /* reject recursive use */
4111 entered = TRUE;
4112
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004113 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004114 /* treat illegal types and illegal string values for {lnum} the same */
4115 if (lnum < 0)
4116 lnum = 0;
4117 fold_count = foldedCount(curwin, lnum, &foldinfo);
4118 if (fold_count > 0)
4119 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004120 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4121 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004122 if (text == buf)
4123 text = vim_strsave(text);
4124 rettv->vval.v_string = text;
4125 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004126
4127 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004128#endif
4129}
4130
4131/*
4132 * "foreground()" function
4133 */
4134 static void
4135f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4136{
4137#ifdef FEAT_GUI
4138 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004139 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004140 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004141 return;
4142 }
4143#endif
4144#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004145 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004146#endif
4147}
4148
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004149 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004150common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004151{
4152 char_u *s;
4153 char_u *name;
4154 int use_string = FALSE;
4155 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004156 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004157
4158 if (argvars[0].v_type == VAR_FUNC)
4159 {
4160 /* function(MyFunc, [arg], dict) */
4161 s = argvars[0].vval.v_string;
4162 }
4163 else if (argvars[0].v_type == VAR_PARTIAL
4164 && argvars[0].vval.v_partial != NULL)
4165 {
4166 /* function(dict.MyFunc, [arg]) */
4167 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004168 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004169 }
4170 else
4171 {
4172 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004173 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004174 use_string = TRUE;
4175 }
4176
Bram Moolenaar843b8842016-08-21 14:36:15 +02004177 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004178 {
4179 name = s;
4180 trans_name = trans_function_name(&name, FALSE,
4181 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4182 if (*name != NUL)
4183 s = NULL;
4184 }
4185
Bram Moolenaar843b8842016-08-21 14:36:15 +02004186 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4187 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004188 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004189 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004190 else if (trans_name != NULL && (is_funcref
4191 ? find_func(trans_name) == NULL
4192 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004193 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004194 else
4195 {
4196 int dict_idx = 0;
4197 int arg_idx = 0;
4198 list_T *list = NULL;
4199
4200 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4201 {
4202 char sid_buf[25];
4203 int off = *s == 's' ? 2 : 5;
4204
4205 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4206 * also be called from another script. Using trans_function_name()
4207 * would also work, but some plugins depend on the name being
4208 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004209 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004210 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004211 if (name != NULL)
4212 {
4213 STRCPY(name, sid_buf);
4214 STRCAT(name, s + off);
4215 }
4216 }
4217 else
4218 name = vim_strsave(s);
4219
4220 if (argvars[1].v_type != VAR_UNKNOWN)
4221 {
4222 if (argvars[2].v_type != VAR_UNKNOWN)
4223 {
4224 /* function(name, [args], dict) */
4225 arg_idx = 1;
4226 dict_idx = 2;
4227 }
4228 else if (argvars[1].v_type == VAR_DICT)
4229 /* function(name, dict) */
4230 dict_idx = 1;
4231 else
4232 /* function(name, [args]) */
4233 arg_idx = 1;
4234 if (dict_idx > 0)
4235 {
4236 if (argvars[dict_idx].v_type != VAR_DICT)
4237 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004238 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004239 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004240 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004241 }
4242 if (argvars[dict_idx].vval.v_dict == NULL)
4243 dict_idx = 0;
4244 }
4245 if (arg_idx > 0)
4246 {
4247 if (argvars[arg_idx].v_type != VAR_LIST)
4248 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004249 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004250 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004251 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004252 }
4253 list = argvars[arg_idx].vval.v_list;
4254 if (list == NULL || list->lv_len == 0)
4255 arg_idx = 0;
4256 }
4257 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004258 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004259 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004260 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004261
4262 /* result is a VAR_PARTIAL */
4263 if (pt == NULL)
4264 vim_free(name);
4265 else
4266 {
4267 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4268 {
4269 listitem_T *li;
4270 int i = 0;
4271 int arg_len = 0;
4272 int lv_len = 0;
4273
4274 if (arg_pt != NULL)
4275 arg_len = arg_pt->pt_argc;
4276 if (list != NULL)
4277 lv_len = list->lv_len;
4278 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004279 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004280 if (pt->pt_argv == NULL)
4281 {
4282 vim_free(pt);
4283 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004284 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004285 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004286 for (i = 0; i < arg_len; i++)
4287 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4288 if (lv_len > 0)
4289 for (li = list->lv_first; li != NULL;
4290 li = li->li_next)
4291 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004292 }
4293
4294 /* For "function(dict.func, [], dict)" and "func" is a partial
4295 * use "dict". That is backwards compatible. */
4296 if (dict_idx > 0)
4297 {
4298 /* The dict is bound explicitly, pt_auto is FALSE. */
4299 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4300 ++pt->pt_dict->dv_refcount;
4301 }
4302 else if (arg_pt != NULL)
4303 {
4304 /* If the dict was bound automatically the result is also
4305 * bound automatically. */
4306 pt->pt_dict = arg_pt->pt_dict;
4307 pt->pt_auto = arg_pt->pt_auto;
4308 if (pt->pt_dict != NULL)
4309 ++pt->pt_dict->dv_refcount;
4310 }
4311
4312 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004313 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4314 {
4315 pt->pt_func = arg_pt->pt_func;
4316 func_ptr_ref(pt->pt_func);
4317 vim_free(name);
4318 }
4319 else if (is_funcref)
4320 {
4321 pt->pt_func = find_func(trans_name);
4322 func_ptr_ref(pt->pt_func);
4323 vim_free(name);
4324 }
4325 else
4326 {
4327 pt->pt_name = name;
4328 func_ref(name);
4329 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004330 }
4331 rettv->v_type = VAR_PARTIAL;
4332 rettv->vval.v_partial = pt;
4333 }
4334 else
4335 {
4336 /* result is a VAR_FUNC */
4337 rettv->v_type = VAR_FUNC;
4338 rettv->vval.v_string = name;
4339 func_ref(name);
4340 }
4341 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004342theend:
4343 vim_free(trans_name);
4344}
4345
4346/*
4347 * "funcref()" function
4348 */
4349 static void
4350f_funcref(typval_T *argvars, typval_T *rettv)
4351{
4352 common_function(argvars, rettv, TRUE);
4353}
4354
4355/*
4356 * "function()" function
4357 */
4358 static void
4359f_function(typval_T *argvars, typval_T *rettv)
4360{
4361 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004362}
4363
4364/*
4365 * "garbagecollect()" function
4366 */
4367 static void
4368f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4369{
4370 /* This is postponed until we are back at the toplevel, because we may be
4371 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4372 want_garbage_collect = TRUE;
4373
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004374 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004375 garbage_collect_at_exit = TRUE;
4376}
4377
4378/*
4379 * "get()" function
4380 */
4381 static void
4382f_get(typval_T *argvars, typval_T *rettv)
4383{
4384 listitem_T *li;
4385 list_T *l;
4386 dictitem_T *di;
4387 dict_T *d;
4388 typval_T *tv = NULL;
4389
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004390 if (argvars[0].v_type == VAR_BLOB)
4391 {
4392 int error = FALSE;
4393 int idx = tv_get_number_chk(&argvars[1], &error);
4394
4395 if (!error)
4396 {
4397 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004398 if (idx < 0)
4399 idx = blob_len(argvars[0].vval.v_blob) + idx;
4400 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4401 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004402 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004403 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004404 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004405 tv = rettv;
4406 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004407 }
4408 }
4409 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004410 {
4411 if ((l = argvars[0].vval.v_list) != NULL)
4412 {
4413 int error = FALSE;
4414
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004415 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004416 if (!error && li != NULL)
4417 tv = &li->li_tv;
4418 }
4419 }
4420 else if (argvars[0].v_type == VAR_DICT)
4421 {
4422 if ((d = argvars[0].vval.v_dict) != NULL)
4423 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004424 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004425 if (di != NULL)
4426 tv = &di->di_tv;
4427 }
4428 }
4429 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4430 {
4431 partial_T *pt;
4432 partial_T fref_pt;
4433
4434 if (argvars[0].v_type == VAR_PARTIAL)
4435 pt = argvars[0].vval.v_partial;
4436 else
4437 {
4438 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4439 fref_pt.pt_name = argvars[0].vval.v_string;
4440 pt = &fref_pt;
4441 }
4442
4443 if (pt != NULL)
4444 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004445 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004446 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004447
4448 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4449 {
4450 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004451 n = partial_name(pt);
4452 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004453 rettv->vval.v_string = NULL;
4454 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004455 {
4456 rettv->vval.v_string = vim_strsave(n);
4457 if (rettv->v_type == VAR_FUNC)
4458 func_ref(rettv->vval.v_string);
4459 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004460 }
4461 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004462 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004463 else if (STRCMP(what, "args") == 0)
4464 {
4465 rettv->v_type = VAR_LIST;
4466 if (rettv_list_alloc(rettv) == OK)
4467 {
4468 int i;
4469
4470 for (i = 0; i < pt->pt_argc; ++i)
4471 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4472 }
4473 }
4474 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004475 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004476 return;
4477 }
4478 }
4479 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004480 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004481
4482 if (tv == NULL)
4483 {
4484 if (argvars[2].v_type != VAR_UNKNOWN)
4485 copy_tv(&argvars[2], rettv);
4486 }
4487 else
4488 copy_tv(tv, rettv);
4489}
4490
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004491/*
4492 * Returns buffer options, variables and other attributes in a dictionary.
4493 */
4494 static dict_T *
4495get_buffer_info(buf_T *buf)
4496{
4497 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004498 tabpage_T *tp;
4499 win_T *wp;
4500 list_T *windows;
4501
4502 dict = dict_alloc();
4503 if (dict == NULL)
4504 return NULL;
4505
Bram Moolenaare0be1672018-07-08 16:50:37 +02004506 dict_add_number(dict, "bufnr", buf->b_fnum);
4507 dict_add_string(dict, "name", buf->b_ffname);
4508 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4509 : buflist_findlnum(buf));
4510 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4511 dict_add_number(dict, "listed", buf->b_p_bl);
4512 dict_add_number(dict, "changed", bufIsChanged(buf));
4513 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4514 dict_add_number(dict, "hidden",
4515 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004516
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004517 // Get a reference to buffer variables
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004518 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004519
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004520 // List of windows displaying this buffer
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004521 windows = list_alloc();
4522 if (windows != NULL)
4523 {
4524 FOR_ALL_TAB_WINDOWS(tp, wp)
4525 if (wp->w_buffer == buf)
4526 list_append_number(windows, (varnumber_T)wp->w_id);
4527 dict_add_list(dict, "windows", windows);
4528 }
4529
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004530#ifdef FEAT_TEXT_PROP
4531 // List of popup windows displaying this buffer
4532 windows = list_alloc();
4533 if (windows != NULL)
4534 {
4535 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4536 if (wp->w_buffer == buf)
4537 list_append_number(windows, (varnumber_T)wp->w_id);
4538 FOR_ALL_TABPAGES(tp)
4539 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
4540 if (wp->w_buffer == buf)
4541 list_append_number(windows, (varnumber_T)wp->w_id);
4542
4543 dict_add_list(dict, "popups", windows);
4544 }
4545#endif
4546
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004547#ifdef FEAT_SIGNS
4548 if (buf->b_signlist != NULL)
4549 {
4550 /* List of signs placed in this buffer */
4551 list_T *signs = list_alloc();
4552 if (signs != NULL)
4553 {
4554 get_buffer_signs(buf, signs);
4555 dict_add_list(dict, "signs", signs);
4556 }
4557 }
4558#endif
4559
4560 return dict;
4561}
4562
4563/*
4564 * "getbufinfo()" function
4565 */
4566 static void
4567f_getbufinfo(typval_T *argvars, typval_T *rettv)
4568{
4569 buf_T *buf = NULL;
4570 buf_T *argbuf = NULL;
4571 dict_T *d;
4572 int filtered = FALSE;
4573 int sel_buflisted = FALSE;
4574 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004575 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004576
4577 if (rettv_list_alloc(rettv) != OK)
4578 return;
4579
4580 /* List of all the buffers or selected buffers */
4581 if (argvars[0].v_type == VAR_DICT)
4582 {
4583 dict_T *sel_d = argvars[0].vval.v_dict;
4584
4585 if (sel_d != NULL)
4586 {
4587 dictitem_T *di;
4588
4589 filtered = TRUE;
4590
4591 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004592 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004593 sel_buflisted = TRUE;
4594
4595 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004596 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004597 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004598
4599 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004600 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004601 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004602 }
4603 }
4604 else if (argvars[0].v_type != VAR_UNKNOWN)
4605 {
4606 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004607 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004608 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004609 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004610 --emsg_off;
4611 if (argbuf == NULL)
4612 return;
4613 }
4614
4615 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004616 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004617 {
4618 if (argbuf != NULL && argbuf != buf)
4619 continue;
4620 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004621 || (sel_buflisted && !buf->b_p_bl)
4622 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004623 continue;
4624
4625 d = get_buffer_info(buf);
4626 if (d != NULL)
4627 list_append_dict(rettv->vval.v_list, d);
4628 if (argbuf != NULL)
4629 return;
4630 }
4631}
4632
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004633/*
4634 * Get line or list of lines from buffer "buf" into "rettv".
4635 * Return a range (from start to end) of lines in rettv from the specified
4636 * buffer.
4637 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4638 */
4639 static void
4640get_buffer_lines(
4641 buf_T *buf,
4642 linenr_T start,
4643 linenr_T end,
4644 int retlist,
4645 typval_T *rettv)
4646{
4647 char_u *p;
4648
4649 rettv->v_type = VAR_STRING;
4650 rettv->vval.v_string = NULL;
4651 if (retlist && rettv_list_alloc(rettv) == FAIL)
4652 return;
4653
4654 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4655 return;
4656
4657 if (!retlist)
4658 {
4659 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4660 p = ml_get_buf(buf, start, FALSE);
4661 else
4662 p = (char_u *)"";
4663 rettv->vval.v_string = vim_strsave(p);
4664 }
4665 else
4666 {
4667 if (end < start)
4668 return;
4669
4670 if (start < 1)
4671 start = 1;
4672 if (end > buf->b_ml.ml_line_count)
4673 end = buf->b_ml.ml_line_count;
4674 while (start <= end)
4675 if (list_append_string(rettv->vval.v_list,
4676 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4677 break;
4678 }
4679}
4680
4681/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004682 * "getbufline()" function
4683 */
4684 static void
4685f_getbufline(typval_T *argvars, typval_T *rettv)
4686{
4687 linenr_T lnum;
4688 linenr_T end;
4689 buf_T *buf;
4690
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004691 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004692 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004693 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004694 --emsg_off;
4695
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004696 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004697 if (argvars[2].v_type == VAR_UNKNOWN)
4698 end = lnum;
4699 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004700 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004701
4702 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4703}
4704
4705/*
4706 * "getbufvar()" function
4707 */
4708 static void
4709f_getbufvar(typval_T *argvars, typval_T *rettv)
4710{
4711 buf_T *buf;
4712 buf_T *save_curbuf;
4713 char_u *varname;
4714 dictitem_T *v;
4715 int done = FALSE;
4716
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004717 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4718 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004719 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004720 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004721
4722 rettv->v_type = VAR_STRING;
4723 rettv->vval.v_string = NULL;
4724
4725 if (buf != NULL && varname != NULL)
4726 {
4727 /* set curbuf to be our buf, temporarily */
4728 save_curbuf = curbuf;
4729 curbuf = buf;
4730
Bram Moolenaar30567352016-08-27 21:25:44 +02004731 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004732 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004733 if (varname[1] == NUL)
4734 {
4735 /* get all buffer-local options in a dict */
4736 dict_T *opts = get_winbuf_options(TRUE);
4737
4738 if (opts != NULL)
4739 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004740 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004741 done = TRUE;
4742 }
4743 }
4744 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4745 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004746 done = TRUE;
4747 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004748 else
4749 {
4750 /* Look up the variable. */
4751 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4752 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4753 'b', varname, FALSE);
4754 if (v != NULL)
4755 {
4756 copy_tv(&v->di_tv, rettv);
4757 done = TRUE;
4758 }
4759 }
4760
4761 /* restore previous notion of curbuf */
4762 curbuf = save_curbuf;
4763 }
4764
4765 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4766 /* use the default value */
4767 copy_tv(&argvars[2], rettv);
4768
4769 --emsg_off;
4770}
4771
4772/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004773 * "getchangelist()" function
4774 */
4775 static void
4776f_getchangelist(typval_T *argvars, typval_T *rettv)
4777{
4778#ifdef FEAT_JUMPLIST
4779 buf_T *buf;
4780 int i;
4781 list_T *l;
4782 dict_T *d;
4783#endif
4784
4785 if (rettv_list_alloc(rettv) != OK)
4786 return;
4787
4788#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004789 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004790 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004791 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004792 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004793 if (buf == NULL)
4794 return;
4795
4796 l = list_alloc();
4797 if (l == NULL)
4798 return;
4799
4800 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4801 return;
4802 /*
4803 * The current window change list index tracks only the position in the
4804 * current buffer change list. For other buffers, use the change list
4805 * length as the current index.
4806 */
4807 list_append_number(rettv->vval.v_list,
4808 (varnumber_T)((buf == curwin->w_buffer)
4809 ? curwin->w_changelistidx : buf->b_changelistlen));
4810
4811 for (i = 0; i < buf->b_changelistlen; ++i)
4812 {
4813 if (buf->b_changelist[i].lnum == 0)
4814 continue;
4815 if ((d = dict_alloc()) == NULL)
4816 return;
4817 if (list_append_dict(l, d) == FAIL)
4818 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004819 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4820 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004821 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004822 }
4823#endif
4824}
4825/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004826 * "getchar()" function
4827 */
4828 static void
4829f_getchar(typval_T *argvars, typval_T *rettv)
4830{
4831 varnumber_T n;
4832 int error = FALSE;
4833
Bram Moolenaar84d93902018-09-11 20:10:20 +02004834#ifdef MESSAGE_QUEUE
4835 // vpeekc() used to check for messages, but that caused problems, invoking
4836 // a callback where it was not expected. Some plugins use getchar(1) in a
4837 // loop to await a message, therefore make sure we check for messages here.
4838 parse_queued_messages();
4839#endif
4840
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004841 /* Position the cursor. Needed after a message that ends in a space. */
4842 windgoto(msg_row, msg_col);
4843
4844 ++no_mapping;
4845 ++allow_keys;
4846 for (;;)
4847 {
4848 if (argvars[0].v_type == VAR_UNKNOWN)
4849 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004850 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004851 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004852 /* getchar(1): only check if char avail */
4853 n = vpeekc_any();
4854 else if (error || vpeekc_any() == NUL)
4855 /* illegal argument or getchar(0) and no char avail: return zero */
4856 n = 0;
4857 else
4858 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004859 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004860
4861 if (n == K_IGNORE)
4862 continue;
4863 break;
4864 }
4865 --no_mapping;
4866 --allow_keys;
4867
4868 set_vim_var_nr(VV_MOUSE_WIN, 0);
4869 set_vim_var_nr(VV_MOUSE_WINID, 0);
4870 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4871 set_vim_var_nr(VV_MOUSE_COL, 0);
4872
4873 rettv->vval.v_number = n;
4874 if (IS_SPECIAL(n) || mod_mask != 0)
4875 {
4876 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4877 int i = 0;
4878
4879 /* Turn a special key into three bytes, plus modifier. */
4880 if (mod_mask != 0)
4881 {
4882 temp[i++] = K_SPECIAL;
4883 temp[i++] = KS_MODIFIER;
4884 temp[i++] = mod_mask;
4885 }
4886 if (IS_SPECIAL(n))
4887 {
4888 temp[i++] = K_SPECIAL;
4889 temp[i++] = K_SECOND(n);
4890 temp[i++] = K_THIRD(n);
4891 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004892 else if (has_mbyte)
4893 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004894 else
4895 temp[i++] = n;
4896 temp[i++] = NUL;
4897 rettv->v_type = VAR_STRING;
4898 rettv->vval.v_string = vim_strsave(temp);
4899
4900#ifdef FEAT_MOUSE
4901 if (is_mouse_key(n))
4902 {
4903 int row = mouse_row;
4904 int col = mouse_col;
4905 win_T *win;
4906 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004907 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004908 int winnr = 1;
4909
4910 if (row >= 0 && col >= 0)
4911 {
4912 /* Find the window at the mouse coordinates and compute the
4913 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004914 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004915 if (win == NULL)
4916 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004917 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004918# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004919 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004920 winnr = 0;
4921 else
4922# endif
4923 for (wp = firstwin; wp != win && wp != NULL;
4924 wp = wp->w_next)
4925 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004926 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4927 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4928 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4929 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4930 }
4931 }
4932#endif
4933 }
4934}
4935
4936/*
4937 * "getcharmod()" function
4938 */
4939 static void
4940f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4941{
4942 rettv->vval.v_number = mod_mask;
4943}
4944
4945/*
4946 * "getcharsearch()" function
4947 */
4948 static void
4949f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4950{
4951 if (rettv_dict_alloc(rettv) != FAIL)
4952 {
4953 dict_T *dict = rettv->vval.v_dict;
4954
Bram Moolenaare0be1672018-07-08 16:50:37 +02004955 dict_add_string(dict, "char", last_csearch());
4956 dict_add_number(dict, "forward", last_csearch_forward());
4957 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004958 }
4959}
4960
4961/*
4962 * "getcmdline()" function
4963 */
4964 static void
4965f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4966{
4967 rettv->v_type = VAR_STRING;
4968 rettv->vval.v_string = get_cmdline_str();
4969}
4970
4971/*
4972 * "getcmdpos()" function
4973 */
4974 static void
4975f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4976{
4977 rettv->vval.v_number = get_cmdline_pos() + 1;
4978}
4979
4980/*
4981 * "getcmdtype()" function
4982 */
4983 static void
4984f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4985{
4986 rettv->v_type = VAR_STRING;
4987 rettv->vval.v_string = alloc(2);
4988 if (rettv->vval.v_string != NULL)
4989 {
4990 rettv->vval.v_string[0] = get_cmdline_type();
4991 rettv->vval.v_string[1] = NUL;
4992 }
4993}
4994
4995/*
4996 * "getcmdwintype()" function
4997 */
4998 static void
4999f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5000{
5001 rettv->v_type = VAR_STRING;
5002 rettv->vval.v_string = NULL;
5003#ifdef FEAT_CMDWIN
5004 rettv->vval.v_string = alloc(2);
5005 if (rettv->vval.v_string != NULL)
5006 {
5007 rettv->vval.v_string[0] = cmdwin_type;
5008 rettv->vval.v_string[1] = NUL;
5009 }
5010#endif
5011}
5012
5013#if defined(FEAT_CMDL_COMPL)
5014/*
5015 * "getcompletion()" function
5016 */
5017 static void
5018f_getcompletion(typval_T *argvars, typval_T *rettv)
5019{
5020 char_u *pat;
5021 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005022 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005023 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5024 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005025
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005026 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005027 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005028
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005029 if (p_wic)
5030 options |= WILD_ICASE;
5031
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005032 /* For filtered results, 'wildignore' is used */
5033 if (!filtered)
5034 options |= WILD_KEEP_ALL;
5035
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005036 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005037 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005038 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005039 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005040 if (xpc.xp_context == EXPAND_NOTHING)
5041 {
5042 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005043 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005044 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005045 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005046 return;
5047 }
5048
5049# if defined(FEAT_MENU)
5050 if (xpc.xp_context == EXPAND_MENUS)
5051 {
5052 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5053 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5054 }
5055# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005056#ifdef FEAT_CSCOPE
5057 if (xpc.xp_context == EXPAND_CSCOPE)
5058 {
5059 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5060 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5061 }
5062#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005063#ifdef FEAT_SIGNS
5064 if (xpc.xp_context == EXPAND_SIGN)
5065 {
5066 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5067 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5068 }
5069#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005070
5071 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5072 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5073 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005074 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005075
5076 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5077
5078 for (i = 0; i < xpc.xp_numfiles; i++)
5079 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5080 }
5081 vim_free(pat);
5082 ExpandCleanup(&xpc);
5083}
5084#endif
5085
5086/*
5087 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005088 *
5089 * Return the current working directory of a window in a tab page.
5090 * First optional argument 'winnr' is the window number or -1 and the second
5091 * optional argument 'tabnr' is the tab page number.
5092 *
5093 * If no arguments are supplied, then return the directory of the current
5094 * window.
5095 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5096 * the specified window.
5097 * If 'winnr' is 0 then return the directory of the current window.
5098 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5099 * directory of the specified tab page. Otherwise return the directory of the
5100 * specified window in the specified tab page.
5101 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005102 */
5103 static void
5104f_getcwd(typval_T *argvars, typval_T *rettv)
5105{
5106 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005107 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005108 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005109 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110
5111 rettv->v_type = VAR_STRING;
5112 rettv->vval.v_string = NULL;
5113
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005114 if (argvars[0].v_type == VAR_NUMBER
5115 && argvars[0].vval.v_number == -1
5116 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005117 global = TRUE;
5118 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005119 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005120
5121 if (wp != NULL && wp->w_localdir != NULL)
5122 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005123 else if (tp != NULL && tp->tp_localdir != NULL)
5124 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5125 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005126 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005127 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005128 rettv->vval.v_string = vim_strsave(globaldir);
5129 else
5130 {
5131 cwd = alloc(MAXPATHL);
5132 if (cwd != NULL)
5133 {
5134 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5135 rettv->vval.v_string = vim_strsave(cwd);
5136 vim_free(cwd);
5137 }
5138 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005139 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005140#ifdef BACKSLASH_IN_FILENAME
5141 if (rettv->vval.v_string != NULL)
5142 slash_adjust(rettv->vval.v_string);
5143#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005144}
5145
5146/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005147 * "getenv()" function
5148 */
5149 static void
5150f_getenv(typval_T *argvars, typval_T *rettv)
5151{
5152 int mustfree = FALSE;
5153 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5154
5155 if (p == NULL)
5156 {
5157 rettv->v_type = VAR_SPECIAL;
5158 rettv->vval.v_number = VVAL_NULL;
5159 return;
5160 }
5161 if (!mustfree)
5162 p = vim_strsave(p);
5163 rettv->vval.v_string = p;
5164 rettv->v_type = VAR_STRING;
5165}
5166
5167/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005168 * "getfontname()" function
5169 */
5170 static void
5171f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5172{
5173 rettv->v_type = VAR_STRING;
5174 rettv->vval.v_string = NULL;
5175#ifdef FEAT_GUI
5176 if (gui.in_use)
5177 {
5178 GuiFont font;
5179 char_u *name = NULL;
5180
5181 if (argvars[0].v_type == VAR_UNKNOWN)
5182 {
5183 /* Get the "Normal" font. Either the name saved by
5184 * hl_set_font_name() or from the font ID. */
5185 font = gui.norm_font;
5186 name = hl_get_font_name();
5187 }
5188 else
5189 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005190 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005191 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5192 return;
5193 font = gui_mch_get_font(name, FALSE);
5194 if (font == NOFONT)
5195 return; /* Invalid font name, return empty string. */
5196 }
5197 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5198 if (argvars[0].v_type != VAR_UNKNOWN)
5199 gui_mch_free_font(font);
5200 }
5201#endif
5202}
5203
5204/*
5205 * "getfperm({fname})" function
5206 */
5207 static void
5208f_getfperm(typval_T *argvars, typval_T *rettv)
5209{
5210 char_u *fname;
5211 stat_T st;
5212 char_u *perm = NULL;
5213 char_u flags[] = "rwx";
5214 int i;
5215
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005216 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005217
5218 rettv->v_type = VAR_STRING;
5219 if (mch_stat((char *)fname, &st) >= 0)
5220 {
5221 perm = vim_strsave((char_u *)"---------");
5222 if (perm != NULL)
5223 {
5224 for (i = 0; i < 9; i++)
5225 {
5226 if (st.st_mode & (1 << (8 - i)))
5227 perm[i] = flags[i % 3];
5228 }
5229 }
5230 }
5231 rettv->vval.v_string = perm;
5232}
5233
5234/*
5235 * "getfsize({fname})" function
5236 */
5237 static void
5238f_getfsize(typval_T *argvars, typval_T *rettv)
5239{
5240 char_u *fname;
5241 stat_T st;
5242
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005243 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005244
5245 rettv->v_type = VAR_NUMBER;
5246
5247 if (mch_stat((char *)fname, &st) >= 0)
5248 {
5249 if (mch_isdir(fname))
5250 rettv->vval.v_number = 0;
5251 else
5252 {
5253 rettv->vval.v_number = (varnumber_T)st.st_size;
5254
5255 /* non-perfect check for overflow */
5256 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5257 rettv->vval.v_number = -2;
5258 }
5259 }
5260 else
5261 rettv->vval.v_number = -1;
5262}
5263
5264/*
5265 * "getftime({fname})" function
5266 */
5267 static void
5268f_getftime(typval_T *argvars, typval_T *rettv)
5269{
5270 char_u *fname;
5271 stat_T st;
5272
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005273 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005274
5275 if (mch_stat((char *)fname, &st) >= 0)
5276 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5277 else
5278 rettv->vval.v_number = -1;
5279}
5280
5281/*
5282 * "getftype({fname})" function
5283 */
5284 static void
5285f_getftype(typval_T *argvars, typval_T *rettv)
5286{
5287 char_u *fname;
5288 stat_T st;
5289 char_u *type = NULL;
5290 char *t;
5291
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005292 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005293
5294 rettv->v_type = VAR_STRING;
5295 if (mch_lstat((char *)fname, &st) >= 0)
5296 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005297 if (S_ISREG(st.st_mode))
5298 t = "file";
5299 else if (S_ISDIR(st.st_mode))
5300 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005301 else if (S_ISLNK(st.st_mode))
5302 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005303 else if (S_ISBLK(st.st_mode))
5304 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005305 else if (S_ISCHR(st.st_mode))
5306 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005307 else if (S_ISFIFO(st.st_mode))
5308 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005309 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005310 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005311 else
5312 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005313 type = vim_strsave((char_u *)t);
5314 }
5315 rettv->vval.v_string = type;
5316}
5317
5318/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005319 * "getjumplist()" function
5320 */
5321 static void
5322f_getjumplist(typval_T *argvars, typval_T *rettv)
5323{
5324#ifdef FEAT_JUMPLIST
5325 win_T *wp;
5326 int i;
5327 list_T *l;
5328 dict_T *d;
5329#endif
5330
5331 if (rettv_list_alloc(rettv) != OK)
5332 return;
5333
5334#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005335 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005336 if (wp == NULL)
5337 return;
5338
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005339 cleanup_jumplist(wp, TRUE);
5340
Bram Moolenaar4f505882018-02-10 21:06:32 +01005341 l = list_alloc();
5342 if (l == NULL)
5343 return;
5344
5345 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5346 return;
5347 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5348
5349 for (i = 0; i < wp->w_jumplistlen; ++i)
5350 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005351 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5352 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005353 if ((d = dict_alloc()) == NULL)
5354 return;
5355 if (list_append_dict(l, d) == FAIL)
5356 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005357 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5358 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005359 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005360 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005361 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005362 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005363 }
5364#endif
5365}
5366
5367/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005368 * "getline(lnum, [end])" function
5369 */
5370 static void
5371f_getline(typval_T *argvars, typval_T *rettv)
5372{
5373 linenr_T lnum;
5374 linenr_T end;
5375 int retlist;
5376
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005377 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005378 if (argvars[1].v_type == VAR_UNKNOWN)
5379 {
5380 end = 0;
5381 retlist = FALSE;
5382 }
5383 else
5384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005385 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005386 retlist = TRUE;
5387 }
5388
5389 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5390}
5391
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005392#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005393 static void
5394get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5395{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005396 if (what_arg->v_type == VAR_UNKNOWN)
5397 {
5398 if (rettv_list_alloc(rettv) == OK)
5399 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005400 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005401 }
5402 else
5403 {
5404 if (rettv_dict_alloc(rettv) == OK)
5405 if (is_qf || (wp != NULL))
5406 {
5407 if (what_arg->v_type == VAR_DICT)
5408 {
5409 dict_T *d = what_arg->vval.v_dict;
5410
5411 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005412 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005413 }
5414 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005415 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005416 }
5417 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005418}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005419#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005420
5421/*
5422 * "getloclist()" function
5423 */
5424 static void
5425f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5426{
5427#ifdef FEAT_QUICKFIX
5428 win_T *wp;
5429
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005430 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005431 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5432#endif
5433}
5434
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005435/*
5436 * "getmatches()" function
5437 */
5438 static void
5439f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5440{
5441#ifdef FEAT_SEARCH_EXTRA
5442 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005443 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005444 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005445 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005446
Bram Moolenaaraff74912019-03-30 18:11:49 +01005447 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5448 return;
5449
5450 cur = win->w_match_head;
5451 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005452 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005453 dict = dict_alloc();
5454 if (dict == NULL)
5455 return;
5456 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005457 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005458 /* match added with matchaddpos() */
5459 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005460 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005461 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005462 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005463 list_T *l;
5464
5465 llpos = &cur->pos.pos[i];
5466 if (llpos->lnum == 0)
5467 break;
5468 l = list_alloc();
5469 if (l == NULL)
5470 break;
5471 list_append_number(l, (varnumber_T)llpos->lnum);
5472 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005473 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005474 list_append_number(l, (varnumber_T)llpos->col);
5475 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005476 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005477 sprintf(buf, "pos%d", i + 1);
5478 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005479 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005480 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005481 else
5482 {
5483 dict_add_string(dict, "pattern", cur->pattern);
5484 }
5485 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5486 dict_add_number(dict, "priority", (long)cur->priority);
5487 dict_add_number(dict, "id", (long)cur->id);
5488# if defined(FEAT_CONCEAL)
5489 if (cur->conceal_char)
5490 {
5491 char_u buf[MB_MAXBYTES + 1];
5492
5493 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5494 dict_add_string(dict, "conceal", (char_u *)&buf);
5495 }
5496# endif
5497 list_append_dict(rettv->vval.v_list, dict);
5498 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005499 }
5500#endif
5501}
5502
5503/*
5504 * "getpid()" function
5505 */
5506 static void
5507f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5508{
5509 rettv->vval.v_number = mch_get_pid();
5510}
5511
5512 static void
5513getpos_both(
5514 typval_T *argvars,
5515 typval_T *rettv,
5516 int getcurpos)
5517{
5518 pos_T *fp;
5519 list_T *l;
5520 int fnum = -1;
5521
5522 if (rettv_list_alloc(rettv) == OK)
5523 {
5524 l = rettv->vval.v_list;
5525 if (getcurpos)
5526 fp = &curwin->w_cursor;
5527 else
5528 fp = var2fpos(&argvars[0], TRUE, &fnum);
5529 if (fnum != -1)
5530 list_append_number(l, (varnumber_T)fnum);
5531 else
5532 list_append_number(l, (varnumber_T)0);
5533 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5534 : (varnumber_T)0);
5535 list_append_number(l, (fp != NULL)
5536 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5537 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005538 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005539 (varnumber_T)0);
5540 if (getcurpos)
5541 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005542 int save_set_curswant = curwin->w_set_curswant;
5543 colnr_T save_curswant = curwin->w_curswant;
5544 colnr_T save_virtcol = curwin->w_virtcol;
5545
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546 update_curswant();
5547 list_append_number(l, curwin->w_curswant == MAXCOL ?
5548 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005549
5550 // Do not change "curswant", as it is unexpected that a get
5551 // function has a side effect.
5552 if (save_set_curswant)
5553 {
5554 curwin->w_set_curswant = save_set_curswant;
5555 curwin->w_curswant = save_curswant;
5556 curwin->w_virtcol = save_virtcol;
5557 curwin->w_valid &= ~VALID_VIRTCOL;
5558 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005559 }
5560 }
5561 else
5562 rettv->vval.v_number = FALSE;
5563}
5564
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005565/*
5566 * "getcurpos()" function
5567 */
5568 static void
5569f_getcurpos(typval_T *argvars, typval_T *rettv)
5570{
5571 getpos_both(argvars, rettv, TRUE);
5572}
5573
5574/*
5575 * "getpos(string)" function
5576 */
5577 static void
5578f_getpos(typval_T *argvars, typval_T *rettv)
5579{
5580 getpos_both(argvars, rettv, FALSE);
5581}
5582
5583/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005584 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005585 */
5586 static void
5587f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5588{
5589#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005590 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005591#endif
5592}
5593
5594/*
5595 * "getreg()" function
5596 */
5597 static void
5598f_getreg(typval_T *argvars, typval_T *rettv)
5599{
5600 char_u *strregname;
5601 int regname;
5602 int arg2 = FALSE;
5603 int return_list = FALSE;
5604 int error = FALSE;
5605
5606 if (argvars[0].v_type != VAR_UNKNOWN)
5607 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005608 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005609 error = strregname == NULL;
5610 if (argvars[1].v_type != VAR_UNKNOWN)
5611 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005612 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005613 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005614 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005615 }
5616 }
5617 else
5618 strregname = get_vim_var_str(VV_REG);
5619
5620 if (error)
5621 return;
5622
5623 regname = (strregname == NULL ? '"' : *strregname);
5624 if (regname == 0)
5625 regname = '"';
5626
5627 if (return_list)
5628 {
5629 rettv->v_type = VAR_LIST;
5630 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5631 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5632 if (rettv->vval.v_list == NULL)
5633 (void)rettv_list_alloc(rettv);
5634 else
5635 ++rettv->vval.v_list->lv_refcount;
5636 }
5637 else
5638 {
5639 rettv->v_type = VAR_STRING;
5640 rettv->vval.v_string = get_reg_contents(regname,
5641 arg2 ? GREG_EXPR_SRC : 0);
5642 }
5643}
5644
5645/*
5646 * "getregtype()" function
5647 */
5648 static void
5649f_getregtype(typval_T *argvars, typval_T *rettv)
5650{
5651 char_u *strregname;
5652 int regname;
5653 char_u buf[NUMBUFLEN + 2];
5654 long reglen = 0;
5655
5656 if (argvars[0].v_type != VAR_UNKNOWN)
5657 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005658 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005659 if (strregname == NULL) /* type error; errmsg already given */
5660 {
5661 rettv->v_type = VAR_STRING;
5662 rettv->vval.v_string = NULL;
5663 return;
5664 }
5665 }
5666 else
5667 /* Default to v:register */
5668 strregname = get_vim_var_str(VV_REG);
5669
5670 regname = (strregname == NULL ? '"' : *strregname);
5671 if (regname == 0)
5672 regname = '"';
5673
5674 buf[0] = NUL;
5675 buf[1] = NUL;
5676 switch (get_reg_type(regname, &reglen))
5677 {
5678 case MLINE: buf[0] = 'V'; break;
5679 case MCHAR: buf[0] = 'v'; break;
5680 case MBLOCK:
5681 buf[0] = Ctrl_V;
5682 sprintf((char *)buf + 1, "%ld", reglen + 1);
5683 break;
5684 }
5685 rettv->v_type = VAR_STRING;
5686 rettv->vval.v_string = vim_strsave(buf);
5687}
5688
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005689/*
5690 * Returns information (variables, options, etc.) about a tab page
5691 * as a dictionary.
5692 */
5693 static dict_T *
5694get_tabpage_info(tabpage_T *tp, int tp_idx)
5695{
5696 win_T *wp;
5697 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005698 list_T *l;
5699
5700 dict = dict_alloc();
5701 if (dict == NULL)
5702 return NULL;
5703
Bram Moolenaare0be1672018-07-08 16:50:37 +02005704 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005705
5706 l = list_alloc();
5707 if (l != NULL)
5708 {
5709 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02005710 wp != NULL; wp = wp->w_next)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005711 list_append_number(l, (varnumber_T)wp->w_id);
5712 dict_add_list(dict, "windows", l);
5713 }
5714
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005715 /* Make a reference to tabpage variables */
5716 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005717
5718 return dict;
5719}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005720
5721/*
5722 * "gettabinfo()" function
5723 */
5724 static void
5725f_gettabinfo(typval_T *argvars, typval_T *rettv)
5726{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005727 tabpage_T *tp, *tparg = NULL;
5728 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005729 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005730
5731 if (rettv_list_alloc(rettv) != OK)
5732 return;
5733
5734 if (argvars[0].v_type != VAR_UNKNOWN)
5735 {
5736 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005737 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005738 if (tparg == NULL)
5739 return;
5740 }
5741
5742 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005743 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005744 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005745 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005746 if (tparg != NULL && tp != tparg)
5747 continue;
5748 d = get_tabpage_info(tp, tpnr);
5749 if (d != NULL)
5750 list_append_dict(rettv->vval.v_list, d);
5751 if (tparg != NULL)
5752 return;
5753 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005754}
5755
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005756/*
5757 * "gettabvar()" function
5758 */
5759 static void
5760f_gettabvar(typval_T *argvars, typval_T *rettv)
5761{
5762 win_T *oldcurwin;
5763 tabpage_T *tp, *oldtabpage;
5764 dictitem_T *v;
5765 char_u *varname;
5766 int done = FALSE;
5767
5768 rettv->v_type = VAR_STRING;
5769 rettv->vval.v_string = NULL;
5770
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005771 varname = tv_get_string_chk(&argvars[1]);
5772 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005773 if (tp != NULL && varname != NULL)
5774 {
5775 /* Set tp to be our tabpage, temporarily. Also set the window to the
5776 * first window in the tabpage, otherwise the window is not valid. */
5777 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005778 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5779 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005780 {
5781 /* look up the variable */
5782 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5783 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5784 if (v != NULL)
5785 {
5786 copy_tv(&v->di_tv, rettv);
5787 done = TRUE;
5788 }
5789 }
5790
5791 /* restore previous notion of curwin */
5792 restore_win(oldcurwin, oldtabpage, TRUE);
5793 }
5794
5795 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5796 copy_tv(&argvars[2], rettv);
5797}
5798
5799/*
5800 * "gettabwinvar()" function
5801 */
5802 static void
5803f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5804{
5805 getwinvar(argvars, rettv, 1);
5806}
5807
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005808/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005809 * "gettagstack()" function
5810 */
5811 static void
5812f_gettagstack(typval_T *argvars, typval_T *rettv)
5813{
5814 win_T *wp = curwin; // default is current window
5815
5816 if (rettv_dict_alloc(rettv) != OK)
5817 return;
5818
5819 if (argvars[0].v_type != VAR_UNKNOWN)
5820 {
5821 wp = find_win_by_nr_or_id(&argvars[0]);
5822 if (wp == NULL)
5823 return;
5824 }
5825
5826 get_tagstack(wp, rettv->vval.v_dict);
5827}
5828
5829/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005830 * Returns information about a window as a dictionary.
5831 */
5832 static dict_T *
5833get_win_info(win_T *wp, short tpnr, short winnr)
5834{
5835 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005836
5837 dict = dict_alloc();
5838 if (dict == NULL)
5839 return NULL;
5840
Bram Moolenaare0be1672018-07-08 16:50:37 +02005841 dict_add_number(dict, "tabnr", tpnr);
5842 dict_add_number(dict, "winnr", winnr);
5843 dict_add_number(dict, "winid", wp->w_id);
5844 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005845 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005846 dict_add_number(dict, "topline", wp->w_topline);
5847 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005848#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005849 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005850#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005851 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005852 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005853 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005854
Bram Moolenaar69905d12017-08-13 18:14:47 +02005855#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005856 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005857#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005858#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005859 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5860 dict_add_number(dict, "loclist",
5861 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005862#endif
5863
Bram Moolenaar30567352016-08-27 21:25:44 +02005864 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005865 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005866
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005867 return dict;
5868}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005869
5870/*
5871 * "getwininfo()" function
5872 */
5873 static void
5874f_getwininfo(typval_T *argvars, typval_T *rettv)
5875{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005876 tabpage_T *tp;
5877 win_T *wp = NULL, *wparg = NULL;
5878 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005879 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005880
5881 if (rettv_list_alloc(rettv) != OK)
5882 return;
5883
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005884 if (argvars[0].v_type != VAR_UNKNOWN)
5885 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005886 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005887 if (wparg == NULL)
5888 return;
5889 }
5890
5891 /* Collect information about either all the windows across all the tab
5892 * pages or one particular window.
5893 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005894 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005895 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005896 tabnr++;
5897 winnr = 0;
5898 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005899 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005900 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005901 if (wparg != NULL && wp != wparg)
5902 continue;
5903 d = get_win_info(wp, tabnr, winnr);
5904 if (d != NULL)
5905 list_append_dict(rettv->vval.v_list, d);
5906 if (wparg != NULL)
5907 /* found information about a specific window */
5908 return;
5909 }
5910 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005911}
5912
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005913/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005914 * "win_execute()" function
5915 */
5916 static void
5917f_win_execute(typval_T *argvars, typval_T *rettv)
5918{
5919 int id = (int)tv_get_number(argvars);
5920 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005921 win_T *save_curwin;
5922 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005923
5924 if (wp != NULL)
5925 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005926 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5927 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005928 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005929 check_cursor();
5930 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005931 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005932 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005933 }
5934}
5935
5936/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005937 * "win_findbuf()" function
5938 */
5939 static void
5940f_win_findbuf(typval_T *argvars, typval_T *rettv)
5941{
5942 if (rettv_list_alloc(rettv) != FAIL)
5943 win_findbuf(argvars, rettv->vval.v_list);
5944}
5945
5946/*
5947 * "win_getid()" function
5948 */
5949 static void
5950f_win_getid(typval_T *argvars, typval_T *rettv)
5951{
5952 rettv->vval.v_number = win_getid(argvars);
5953}
5954
5955/*
5956 * "win_gotoid()" function
5957 */
5958 static void
5959f_win_gotoid(typval_T *argvars, typval_T *rettv)
5960{
5961 rettv->vval.v_number = win_gotoid(argvars);
5962}
5963
5964/*
5965 * "win_id2tabwin()" function
5966 */
5967 static void
5968f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5969{
5970 if (rettv_list_alloc(rettv) != FAIL)
5971 win_id2tabwin(argvars, rettv->vval.v_list);
5972}
5973
5974/*
5975 * "win_id2win()" function
5976 */
5977 static void
5978f_win_id2win(typval_T *argvars, typval_T *rettv)
5979{
5980 rettv->vval.v_number = win_id2win(argvars);
5981}
5982
5983/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005984 * "win_screenpos()" function
5985 */
5986 static void
5987f_win_screenpos(typval_T *argvars, typval_T *rettv)
5988{
5989 win_T *wp;
5990
5991 if (rettv_list_alloc(rettv) == FAIL)
5992 return;
5993
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005994 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005995 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5996 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5997}
5998
5999/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006000 * "getwinpos({timeout})" function
6001 */
6002 static void
6003f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6004{
6005 int x = -1;
6006 int y = -1;
6007
6008 if (rettv_list_alloc(rettv) == FAIL)
6009 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006010#if defined(FEAT_GUI) \
6011 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6012 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006013 {
6014 varnumber_T timeout = 100;
6015
6016 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006017 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006018
6019 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006020 }
6021#endif
6022 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6023 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6024}
6025
6026
6027/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006028 * "getwinposx()" function
6029 */
6030 static void
6031f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6032{
6033 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006034#if defined(FEAT_GUI) \
6035 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6036 || defined(MSWIN)
6037
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006038 {
6039 int x, y;
6040
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006041 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006042 rettv->vval.v_number = x;
6043 }
6044#endif
6045}
6046
6047/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006048 * "getwinposy()" function
6049 */
6050 static void
6051f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6052{
6053 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006054#if defined(FEAT_GUI) \
6055 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6056 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006057 {
6058 int x, y;
6059
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006060 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006061 rettv->vval.v_number = y;
6062 }
6063#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006064}
6065
6066/*
6067 * "getwinvar()" function
6068 */
6069 static void
6070f_getwinvar(typval_T *argvars, typval_T *rettv)
6071{
6072 getwinvar(argvars, rettv, 0);
6073}
6074
6075/*
6076 * "glob()" function
6077 */
6078 static void
6079f_glob(typval_T *argvars, typval_T *rettv)
6080{
6081 int options = WILD_SILENT|WILD_USE_NL;
6082 expand_T xpc;
6083 int error = FALSE;
6084
6085 /* When the optional second argument is non-zero, don't remove matches
6086 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6087 rettv->v_type = VAR_STRING;
6088 if (argvars[1].v_type != VAR_UNKNOWN)
6089 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006090 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006091 options |= WILD_KEEP_ALL;
6092 if (argvars[2].v_type != VAR_UNKNOWN)
6093 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006094 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006095 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006096 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006097 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006098 options |= WILD_ALLLINKS;
6099 }
6100 }
6101 if (!error)
6102 {
6103 ExpandInit(&xpc);
6104 xpc.xp_context = EXPAND_FILES;
6105 if (p_wic)
6106 options += WILD_ICASE;
6107 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006108 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006109 NULL, options, WILD_ALL);
6110 else if (rettv_list_alloc(rettv) != FAIL)
6111 {
6112 int i;
6113
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006114 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006115 NULL, options, WILD_ALL_KEEP);
6116 for (i = 0; i < xpc.xp_numfiles; i++)
6117 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6118
6119 ExpandCleanup(&xpc);
6120 }
6121 }
6122 else
6123 rettv->vval.v_string = NULL;
6124}
6125
6126/*
6127 * "globpath()" function
6128 */
6129 static void
6130f_globpath(typval_T *argvars, typval_T *rettv)
6131{
6132 int flags = 0;
6133 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006134 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006135 int error = FALSE;
6136 garray_T ga;
6137 int i;
6138
6139 /* When the optional second argument is non-zero, don't remove matches
6140 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6141 rettv->v_type = VAR_STRING;
6142 if (argvars[2].v_type != VAR_UNKNOWN)
6143 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006144 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006145 flags |= WILD_KEEP_ALL;
6146 if (argvars[3].v_type != VAR_UNKNOWN)
6147 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006148 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006149 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006150 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006151 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006152 flags |= WILD_ALLLINKS;
6153 }
6154 }
6155 if (file != NULL && !error)
6156 {
6157 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006158 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006159 if (rettv->v_type == VAR_STRING)
6160 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6161 else if (rettv_list_alloc(rettv) != FAIL)
6162 for (i = 0; i < ga.ga_len; ++i)
6163 list_append_string(rettv->vval.v_list,
6164 ((char_u **)(ga.ga_data))[i], -1);
6165 ga_clear_strings(&ga);
6166 }
6167 else
6168 rettv->vval.v_string = NULL;
6169}
6170
6171/*
6172 * "glob2regpat()" function
6173 */
6174 static void
6175f_glob2regpat(typval_T *argvars, typval_T *rettv)
6176{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006177 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006178
6179 rettv->v_type = VAR_STRING;
6180 rettv->vval.v_string = (pat == NULL)
6181 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6182}
6183
6184/* for VIM_VERSION_ defines */
6185#include "version.h"
6186
6187/*
6188 * "has()" function
6189 */
6190 static void
6191f_has(typval_T *argvars, typval_T *rettv)
6192{
6193 int i;
6194 char_u *name;
6195 int n = FALSE;
6196 static char *(has_list[]) =
6197 {
6198#ifdef AMIGA
6199 "amiga",
6200# ifdef FEAT_ARP
6201 "arp",
6202# endif
6203#endif
6204#ifdef __BEOS__
6205 "beos",
6206#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006207#if defined(BSD) && !defined(MACOS_X)
6208 "bsd",
6209#endif
6210#ifdef hpux
6211 "hpux",
6212#endif
6213#ifdef __linux__
6214 "linux",
6215#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006216#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006217 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6218 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006219# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006220 "macunix", /* Mac OS X, with the darwin feature */
6221 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006222# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006223#endif
6224#ifdef __QNX__
6225 "qnx",
6226#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006227#ifdef SUN_SYSTEM
6228 "sun",
6229#else
6230 "moon",
6231#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006232#ifdef UNIX
6233 "unix",
6234#endif
6235#ifdef VMS
6236 "vms",
6237#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006238#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006239 "win32",
6240#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006241#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006242 "win32unix",
6243#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006244#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006245 "win64",
6246#endif
6247#ifdef EBCDIC
6248 "ebcdic",
6249#endif
6250#ifndef CASE_INSENSITIVE_FILENAME
6251 "fname_case",
6252#endif
6253#ifdef HAVE_ACL
6254 "acl",
6255#endif
6256#ifdef FEAT_ARABIC
6257 "arabic",
6258#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006259 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006260#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006261 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006262#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006263#ifdef FEAT_AUTOSERVERNAME
6264 "autoservername",
6265#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006266#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006267 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006268# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006269 "balloon_multiline",
6270# endif
6271#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006272#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006273 "balloon_eval_term",
6274#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006275#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6276 "builtin_terms",
6277# ifdef ALL_BUILTIN_TCAPS
6278 "all_builtin_terms",
6279# endif
6280#endif
6281#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006282 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006283 || defined(FEAT_GUI_MOTIF))
6284 "browsefilter",
6285#endif
6286#ifdef FEAT_BYTEOFF
6287 "byte_offset",
6288#endif
6289#ifdef FEAT_JOB_CHANNEL
6290 "channel",
6291#endif
6292#ifdef FEAT_CINDENT
6293 "cindent",
6294#endif
6295#ifdef FEAT_CLIENTSERVER
6296 "clientserver",
6297#endif
6298#ifdef FEAT_CLIPBOARD
6299 "clipboard",
6300#endif
6301#ifdef FEAT_CMDL_COMPL
6302 "cmdline_compl",
6303#endif
6304#ifdef FEAT_CMDHIST
6305 "cmdline_hist",
6306#endif
6307#ifdef FEAT_COMMENTS
6308 "comments",
6309#endif
6310#ifdef FEAT_CONCEAL
6311 "conceal",
6312#endif
6313#ifdef FEAT_CRYPT
6314 "cryptv",
6315 "crypt-blowfish",
6316 "crypt-blowfish2",
6317#endif
6318#ifdef FEAT_CSCOPE
6319 "cscope",
6320#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006321 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006322#ifdef CURSOR_SHAPE
6323 "cursorshape",
6324#endif
6325#ifdef DEBUG
6326 "debug",
6327#endif
6328#ifdef FEAT_CON_DIALOG
6329 "dialog_con",
6330#endif
6331#ifdef FEAT_GUI_DIALOG
6332 "dialog_gui",
6333#endif
6334#ifdef FEAT_DIFF
6335 "diff",
6336#endif
6337#ifdef FEAT_DIGRAPHS
6338 "digraphs",
6339#endif
6340#ifdef FEAT_DIRECTX
6341 "directx",
6342#endif
6343#ifdef FEAT_DND
6344 "dnd",
6345#endif
6346#ifdef FEAT_EMACS_TAGS
6347 "emacs_tags",
6348#endif
6349 "eval", /* always present, of course! */
6350 "ex_extra", /* graduated feature */
6351#ifdef FEAT_SEARCH_EXTRA
6352 "extra_search",
6353#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006354#ifdef FEAT_SEARCHPATH
6355 "file_in_path",
6356#endif
6357#ifdef FEAT_FILTERPIPE
6358 "filterpipe",
6359#endif
6360#ifdef FEAT_FIND_ID
6361 "find_in_path",
6362#endif
6363#ifdef FEAT_FLOAT
6364 "float",
6365#endif
6366#ifdef FEAT_FOLDING
6367 "folding",
6368#endif
6369#ifdef FEAT_FOOTER
6370 "footer",
6371#endif
6372#if !defined(USE_SYSTEM) && defined(UNIX)
6373 "fork",
6374#endif
6375#ifdef FEAT_GETTEXT
6376 "gettext",
6377#endif
6378#ifdef FEAT_GUI
6379 "gui",
6380#endif
6381#ifdef FEAT_GUI_ATHENA
6382# ifdef FEAT_GUI_NEXTAW
6383 "gui_neXtaw",
6384# else
6385 "gui_athena",
6386# endif
6387#endif
6388#ifdef FEAT_GUI_GTK
6389 "gui_gtk",
6390# ifdef USE_GTK3
6391 "gui_gtk3",
6392# else
6393 "gui_gtk2",
6394# endif
6395#endif
6396#ifdef FEAT_GUI_GNOME
6397 "gui_gnome",
6398#endif
6399#ifdef FEAT_GUI_MAC
6400 "gui_mac",
6401#endif
6402#ifdef FEAT_GUI_MOTIF
6403 "gui_motif",
6404#endif
6405#ifdef FEAT_GUI_PHOTON
6406 "gui_photon",
6407#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006408#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006409 "gui_win32",
6410#endif
6411#ifdef FEAT_HANGULIN
6412 "hangul_input",
6413#endif
6414#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6415 "iconv",
6416#endif
6417#ifdef FEAT_INS_EXPAND
6418 "insert_expand",
6419#endif
6420#ifdef FEAT_JOB_CHANNEL
6421 "job",
6422#endif
6423#ifdef FEAT_JUMPLIST
6424 "jumplist",
6425#endif
6426#ifdef FEAT_KEYMAP
6427 "keymap",
6428#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006429 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006430#ifdef FEAT_LANGMAP
6431 "langmap",
6432#endif
6433#ifdef FEAT_LIBCALL
6434 "libcall",
6435#endif
6436#ifdef FEAT_LINEBREAK
6437 "linebreak",
6438#endif
6439#ifdef FEAT_LISP
6440 "lispindent",
6441#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006442 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006443#ifdef FEAT_LOCALMAP
6444 "localmap",
6445#endif
6446#ifdef FEAT_LUA
6447# ifndef DYNAMIC_LUA
6448 "lua",
6449# endif
6450#endif
6451#ifdef FEAT_MENU
6452 "menu",
6453#endif
6454#ifdef FEAT_SESSION
6455 "mksession",
6456#endif
6457#ifdef FEAT_MODIFY_FNAME
6458 "modify_fname",
6459#endif
6460#ifdef FEAT_MOUSE
6461 "mouse",
6462#endif
6463#ifdef FEAT_MOUSESHAPE
6464 "mouseshape",
6465#endif
6466#if defined(UNIX) || defined(VMS)
6467# ifdef FEAT_MOUSE_DEC
6468 "mouse_dec",
6469# endif
6470# ifdef FEAT_MOUSE_GPM
6471 "mouse_gpm",
6472# endif
6473# ifdef FEAT_MOUSE_JSB
6474 "mouse_jsbterm",
6475# endif
6476# ifdef FEAT_MOUSE_NET
6477 "mouse_netterm",
6478# endif
6479# ifdef FEAT_MOUSE_PTERM
6480 "mouse_pterm",
6481# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006482# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006483 "mouse_sgr",
6484# endif
6485# ifdef FEAT_SYSMOUSE
6486 "mouse_sysmouse",
6487# endif
6488# ifdef FEAT_MOUSE_URXVT
6489 "mouse_urxvt",
6490# endif
6491# ifdef FEAT_MOUSE_XTERM
6492 "mouse_xterm",
6493# endif
6494#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006495 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006496#ifdef FEAT_MBYTE_IME
6497 "multi_byte_ime",
6498#endif
6499#ifdef FEAT_MULTI_LANG
6500 "multi_lang",
6501#endif
6502#ifdef FEAT_MZSCHEME
6503#ifndef DYNAMIC_MZSCHEME
6504 "mzscheme",
6505#endif
6506#endif
6507#ifdef FEAT_NUM64
6508 "num64",
6509#endif
6510#ifdef FEAT_OLE
6511 "ole",
6512#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006513#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006514 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006515#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516#ifdef FEAT_PATH_EXTRA
6517 "path_extra",
6518#endif
6519#ifdef FEAT_PERL
6520#ifndef DYNAMIC_PERL
6521 "perl",
6522#endif
6523#endif
6524#ifdef FEAT_PERSISTENT_UNDO
6525 "persistent_undo",
6526#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006527#if defined(FEAT_PYTHON)
6528 "python_compiled",
6529# if defined(DYNAMIC_PYTHON)
6530 "python_dynamic",
6531# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006532 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006533 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006534# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006535#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006536#if defined(FEAT_PYTHON3)
6537 "python3_compiled",
6538# if defined(DYNAMIC_PYTHON3)
6539 "python3_dynamic",
6540# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006541 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006542 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006543# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006544#endif
6545#ifdef FEAT_POSTSCRIPT
6546 "postscript",
6547#endif
6548#ifdef FEAT_PRINTER
6549 "printer",
6550#endif
6551#ifdef FEAT_PROFILE
6552 "profile",
6553#endif
6554#ifdef FEAT_RELTIME
6555 "reltime",
6556#endif
6557#ifdef FEAT_QUICKFIX
6558 "quickfix",
6559#endif
6560#ifdef FEAT_RIGHTLEFT
6561 "rightleft",
6562#endif
6563#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6564 "ruby",
6565#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006566 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006567#ifdef FEAT_CMDL_INFO
6568 "showcmd",
6569 "cmdline_info",
6570#endif
6571#ifdef FEAT_SIGNS
6572 "signs",
6573#endif
6574#ifdef FEAT_SMARTINDENT
6575 "smartindent",
6576#endif
6577#ifdef STARTUPTIME
6578 "startuptime",
6579#endif
6580#ifdef FEAT_STL_OPT
6581 "statusline",
6582#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006583#ifdef FEAT_NETBEANS_INTG
6584 "netbeans_intg",
6585#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006586#ifdef FEAT_SOUND
6587 "sound",
6588#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006589#ifdef FEAT_SPELL
6590 "spell",
6591#endif
6592#ifdef FEAT_SYN_HL
6593 "syntax",
6594#endif
6595#if defined(USE_SYSTEM) || !defined(UNIX)
6596 "system",
6597#endif
6598#ifdef FEAT_TAG_BINS
6599 "tag_binary",
6600#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006601#ifdef FEAT_TCL
6602# ifndef DYNAMIC_TCL
6603 "tcl",
6604# endif
6605#endif
6606#ifdef FEAT_TERMGUICOLORS
6607 "termguicolors",
6608#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006609#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006610 "terminal",
6611#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006612#ifdef TERMINFO
6613 "terminfo",
6614#endif
6615#ifdef FEAT_TERMRESPONSE
6616 "termresponse",
6617#endif
6618#ifdef FEAT_TEXTOBJ
6619 "textobjects",
6620#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006621#ifdef FEAT_TEXT_PROP
6622 "textprop",
6623#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006624#ifdef HAVE_TGETENT
6625 "tgetent",
6626#endif
6627#ifdef FEAT_TIMERS
6628 "timers",
6629#endif
6630#ifdef FEAT_TITLE
6631 "title",
6632#endif
6633#ifdef FEAT_TOOLBAR
6634 "toolbar",
6635#endif
6636#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6637 "unnamedplus",
6638#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 "user-commands", /* was accidentally included in 5.4 */
6640 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006641#ifdef FEAT_VARTABS
6642 "vartabs",
6643#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006644 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006645#ifdef FEAT_VIMINFO
6646 "viminfo",
6647#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006648 "vimscript-1",
6649 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006650 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006651 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006652 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006653 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006654 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006655#ifdef FEAT_VTP
6656 "vtp",
6657#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006658#ifdef FEAT_WILDIGN
6659 "wildignore",
6660#endif
6661#ifdef FEAT_WILDMENU
6662 "wildmenu",
6663#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006664 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006665#ifdef FEAT_WAK
6666 "winaltkeys",
6667#endif
6668#ifdef FEAT_WRITEBACKUP
6669 "writebackup",
6670#endif
6671#ifdef FEAT_XIM
6672 "xim",
6673#endif
6674#ifdef FEAT_XFONTSET
6675 "xfontset",
6676#endif
6677#ifdef FEAT_XPM_W32
6678 "xpm",
6679 "xpm_w32", /* for backward compatibility */
6680#else
6681# if defined(HAVE_XPM)
6682 "xpm",
6683# endif
6684#endif
6685#ifdef USE_XSMP
6686 "xsmp",
6687#endif
6688#ifdef USE_XSMP_INTERACT
6689 "xsmp_interact",
6690#endif
6691#ifdef FEAT_XCLIPBOARD
6692 "xterm_clipboard",
6693#endif
6694#ifdef FEAT_XTERM_SAVE
6695 "xterm_save",
6696#endif
6697#if defined(UNIX) && defined(FEAT_X11)
6698 "X11",
6699#endif
6700 NULL
6701 };
6702
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006703 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006704 for (i = 0; has_list[i] != NULL; ++i)
6705 if (STRICMP(name, has_list[i]) == 0)
6706 {
6707 n = TRUE;
6708 break;
6709 }
6710
6711 if (n == FALSE)
6712 {
6713 if (STRNICMP(name, "patch", 5) == 0)
6714 {
6715 if (name[5] == '-'
6716 && STRLEN(name) >= 11
6717 && vim_isdigit(name[6])
6718 && vim_isdigit(name[8])
6719 && vim_isdigit(name[10]))
6720 {
6721 int major = atoi((char *)name + 6);
6722 int minor = atoi((char *)name + 8);
6723
6724 /* Expect "patch-9.9.01234". */
6725 n = (major < VIM_VERSION_MAJOR
6726 || (major == VIM_VERSION_MAJOR
6727 && (minor < VIM_VERSION_MINOR
6728 || (minor == VIM_VERSION_MINOR
6729 && has_patch(atoi((char *)name + 10))))));
6730 }
6731 else
6732 n = has_patch(atoi((char *)name + 5));
6733 }
6734 else if (STRICMP(name, "vim_starting") == 0)
6735 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006736 else if (STRICMP(name, "ttyin") == 0)
6737 n = mch_input_isatty();
6738 else if (STRICMP(name, "ttyout") == 0)
6739 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006740 else if (STRICMP(name, "multi_byte_encoding") == 0)
6741 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006742#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006743 else if (STRICMP(name, "balloon_multiline") == 0)
6744 n = multiline_balloon_available();
6745#endif
6746#ifdef DYNAMIC_TCL
6747 else if (STRICMP(name, "tcl") == 0)
6748 n = tcl_enabled(FALSE);
6749#endif
6750#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6751 else if (STRICMP(name, "iconv") == 0)
6752 n = iconv_enabled(FALSE);
6753#endif
6754#ifdef DYNAMIC_LUA
6755 else if (STRICMP(name, "lua") == 0)
6756 n = lua_enabled(FALSE);
6757#endif
6758#ifdef DYNAMIC_MZSCHEME
6759 else if (STRICMP(name, "mzscheme") == 0)
6760 n = mzscheme_enabled(FALSE);
6761#endif
6762#ifdef DYNAMIC_RUBY
6763 else if (STRICMP(name, "ruby") == 0)
6764 n = ruby_enabled(FALSE);
6765#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006766#ifdef DYNAMIC_PYTHON
6767 else if (STRICMP(name, "python") == 0)
6768 n = python_enabled(FALSE);
6769#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006770#ifdef DYNAMIC_PYTHON3
6771 else if (STRICMP(name, "python3") == 0)
6772 n = python3_enabled(FALSE);
6773#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006774#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6775 else if (STRICMP(name, "pythonx") == 0)
6776 {
6777# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6778 if (p_pyx == 0)
6779 n = python3_enabled(FALSE) || python_enabled(FALSE);
6780 else if (p_pyx == 3)
6781 n = python3_enabled(FALSE);
6782 else if (p_pyx == 2)
6783 n = python_enabled(FALSE);
6784# elif defined(DYNAMIC_PYTHON)
6785 n = python_enabled(FALSE);
6786# elif defined(DYNAMIC_PYTHON3)
6787 n = python3_enabled(FALSE);
6788# endif
6789 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006790#endif
6791#ifdef DYNAMIC_PERL
6792 else if (STRICMP(name, "perl") == 0)
6793 n = perl_enabled(FALSE);
6794#endif
6795#ifdef FEAT_GUI
6796 else if (STRICMP(name, "gui_running") == 0)
6797 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006798# ifdef FEAT_BROWSE
6799 else if (STRICMP(name, "browse") == 0)
6800 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6801# endif
6802#endif
6803#ifdef FEAT_SYN_HL
6804 else if (STRICMP(name, "syntax_items") == 0)
6805 n = syntax_present(curwin);
6806#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006807#ifdef FEAT_VTP
6808 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006809 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006810#endif
6811#ifdef FEAT_NETBEANS_INTG
6812 else if (STRICMP(name, "netbeans_enabled") == 0)
6813 n = netbeans_active();
6814#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006815#ifdef FEAT_MOUSE_GPM
6816 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6817 n = gpm_enabled();
6818#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006819#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006820 else if (STRICMP(name, "terminal") == 0)
6821 n = terminal_enabled();
6822#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006823#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006824 else if (STRICMP(name, "conpty") == 0)
6825 n = use_conpty();
6826#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006827 }
6828
6829 rettv->vval.v_number = n;
6830}
6831
6832/*
6833 * "has_key()" function
6834 */
6835 static void
6836f_has_key(typval_T *argvars, typval_T *rettv)
6837{
6838 if (argvars[0].v_type != VAR_DICT)
6839 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006840 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006841 return;
6842 }
6843 if (argvars[0].vval.v_dict == NULL)
6844 return;
6845
6846 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006847 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006848}
6849
6850/*
6851 * "haslocaldir()" function
6852 */
6853 static void
6854f_haslocaldir(typval_T *argvars, typval_T *rettv)
6855{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006856 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006857 win_T *wp = NULL;
6858
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006859 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6860
6861 // Check for window-local and tab-local directories
6862 if (wp != NULL && wp->w_localdir != NULL)
6863 rettv->vval.v_number = 1;
6864 else if (tp != NULL && tp->tp_localdir != NULL)
6865 rettv->vval.v_number = 2;
6866 else
6867 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006868}
6869
6870/*
6871 * "hasmapto()" function
6872 */
6873 static void
6874f_hasmapto(typval_T *argvars, typval_T *rettv)
6875{
6876 char_u *name;
6877 char_u *mode;
6878 char_u buf[NUMBUFLEN];
6879 int abbr = FALSE;
6880
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006881 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006882 if (argvars[1].v_type == VAR_UNKNOWN)
6883 mode = (char_u *)"nvo";
6884 else
6885 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006886 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006887 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006888 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006889 }
6890
6891 if (map_to_exists(name, mode, abbr))
6892 rettv->vval.v_number = TRUE;
6893 else
6894 rettv->vval.v_number = FALSE;
6895}
6896
6897/*
6898 * "histadd()" function
6899 */
6900 static void
6901f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6902{
6903#ifdef FEAT_CMDHIST
6904 int histype;
6905 char_u *str;
6906 char_u buf[NUMBUFLEN];
6907#endif
6908
6909 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006910 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911 return;
6912#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006913 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006914 histype = str != NULL ? get_histtype(str) : -1;
6915 if (histype >= 0)
6916 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006917 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006918 if (*str != NUL)
6919 {
6920 init_history();
6921 add_to_history(histype, str, FALSE, NUL);
6922 rettv->vval.v_number = TRUE;
6923 return;
6924 }
6925 }
6926#endif
6927}
6928
6929/*
6930 * "histdel()" function
6931 */
6932 static void
6933f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6934{
6935#ifdef FEAT_CMDHIST
6936 int n;
6937 char_u buf[NUMBUFLEN];
6938 char_u *str;
6939
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006940 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006941 if (str == NULL)
6942 n = 0;
6943 else if (argvars[1].v_type == VAR_UNKNOWN)
6944 /* only one argument: clear entire history */
6945 n = clr_history(get_histtype(str));
6946 else if (argvars[1].v_type == VAR_NUMBER)
6947 /* index given: remove that entry */
6948 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006949 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006950 else
6951 /* string given: remove all matching entries */
6952 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006953 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006954 rettv->vval.v_number = n;
6955#endif
6956}
6957
6958/*
6959 * "histget()" function
6960 */
6961 static void
6962f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6963{
6964#ifdef FEAT_CMDHIST
6965 int type;
6966 int idx;
6967 char_u *str;
6968
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006969 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006970 if (str == NULL)
6971 rettv->vval.v_string = NULL;
6972 else
6973 {
6974 type = get_histtype(str);
6975 if (argvars[1].v_type == VAR_UNKNOWN)
6976 idx = get_history_idx(type);
6977 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006978 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006979 /* -1 on type error */
6980 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6981 }
6982#else
6983 rettv->vval.v_string = NULL;
6984#endif
6985 rettv->v_type = VAR_STRING;
6986}
6987
6988/*
6989 * "histnr()" function
6990 */
6991 static void
6992f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6993{
6994 int i;
6995
6996#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006997 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006998
6999 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7000 if (i >= HIST_CMD && i < HIST_COUNT)
7001 i = get_history_idx(i);
7002 else
7003#endif
7004 i = -1;
7005 rettv->vval.v_number = i;
7006}
7007
7008/*
7009 * "highlightID(name)" function
7010 */
7011 static void
7012f_hlID(typval_T *argvars, typval_T *rettv)
7013{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007014 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007015}
7016
7017/*
7018 * "highlight_exists()" function
7019 */
7020 static void
7021f_hlexists(typval_T *argvars, typval_T *rettv)
7022{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007023 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007024}
7025
7026/*
7027 * "hostname()" function
7028 */
7029 static void
7030f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7031{
7032 char_u hostname[256];
7033
7034 mch_get_host_name(hostname, 256);
7035 rettv->v_type = VAR_STRING;
7036 rettv->vval.v_string = vim_strsave(hostname);
7037}
7038
7039/*
7040 * iconv() function
7041 */
7042 static void
7043f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7044{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007045 char_u buf1[NUMBUFLEN];
7046 char_u buf2[NUMBUFLEN];
7047 char_u *from, *to, *str;
7048 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007049
7050 rettv->v_type = VAR_STRING;
7051 rettv->vval.v_string = NULL;
7052
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007053 str = tv_get_string(&argvars[0]);
7054 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7055 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007056 vimconv.vc_type = CONV_NONE;
7057 convert_setup(&vimconv, from, to);
7058
7059 /* If the encodings are equal, no conversion needed. */
7060 if (vimconv.vc_type == CONV_NONE)
7061 rettv->vval.v_string = vim_strsave(str);
7062 else
7063 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7064
7065 convert_setup(&vimconv, NULL, NULL);
7066 vim_free(from);
7067 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007068}
7069
7070/*
7071 * "indent()" function
7072 */
7073 static void
7074f_indent(typval_T *argvars, typval_T *rettv)
7075{
7076 linenr_T lnum;
7077
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007078 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007079 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7080 rettv->vval.v_number = get_indent_lnum(lnum);
7081 else
7082 rettv->vval.v_number = -1;
7083}
7084
7085/*
7086 * "index()" function
7087 */
7088 static void
7089f_index(typval_T *argvars, typval_T *rettv)
7090{
7091 list_T *l;
7092 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007093 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007094 long idx = 0;
7095 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007096 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007097
7098 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007099 if (argvars[0].v_type == VAR_BLOB)
7100 {
7101 typval_T tv;
7102 int start = 0;
7103
7104 if (argvars[2].v_type != VAR_UNKNOWN)
7105 {
7106 start = tv_get_number_chk(&argvars[2], &error);
7107 if (error)
7108 return;
7109 }
7110 b = argvars[0].vval.v_blob;
7111 if (b == NULL)
7112 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007113 if (start < 0)
7114 {
7115 start = blob_len(b) + start;
7116 if (start < 0)
7117 start = 0;
7118 }
7119
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007120 for (idx = start; idx < blob_len(b); ++idx)
7121 {
7122 tv.v_type = VAR_NUMBER;
7123 tv.vval.v_number = blob_get(b, idx);
7124 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7125 {
7126 rettv->vval.v_number = idx;
7127 return;
7128 }
7129 }
7130 return;
7131 }
7132 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007133 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007134 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007135 return;
7136 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007137
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007138 l = argvars[0].vval.v_list;
7139 if (l != NULL)
7140 {
7141 item = l->lv_first;
7142 if (argvars[2].v_type != VAR_UNKNOWN)
7143 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007144 /* Start at specified item. Use the cached index that list_find()
7145 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007146 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007147 idx = l->lv_idx;
7148 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007149 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007150 if (error)
7151 item = NULL;
7152 }
7153
7154 for ( ; item != NULL; item = item->li_next, ++idx)
7155 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7156 {
7157 rettv->vval.v_number = idx;
7158 break;
7159 }
7160 }
7161}
7162
7163static int inputsecret_flag = 0;
7164
7165/*
7166 * "input()" function
7167 * Also handles inputsecret() when inputsecret is set.
7168 */
7169 static void
7170f_input(typval_T *argvars, typval_T *rettv)
7171{
7172 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7173}
7174
7175/*
7176 * "inputdialog()" function
7177 */
7178 static void
7179f_inputdialog(typval_T *argvars, typval_T *rettv)
7180{
7181#if defined(FEAT_GUI_TEXTDIALOG)
7182 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7183 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7184 {
7185 char_u *message;
7186 char_u buf[NUMBUFLEN];
7187 char_u *defstr = (char_u *)"";
7188
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007189 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007190 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007191 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007192 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7193 else
7194 IObuff[0] = NUL;
7195 if (message != NULL && defstr != NULL
7196 && do_dialog(VIM_QUESTION, NULL, message,
7197 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7198 rettv->vval.v_string = vim_strsave(IObuff);
7199 else
7200 {
7201 if (message != NULL && defstr != NULL
7202 && argvars[1].v_type != VAR_UNKNOWN
7203 && argvars[2].v_type != VAR_UNKNOWN)
7204 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007205 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007206 else
7207 rettv->vval.v_string = NULL;
7208 }
7209 rettv->v_type = VAR_STRING;
7210 }
7211 else
7212#endif
7213 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7214}
7215
7216/*
7217 * "inputlist()" function
7218 */
7219 static void
7220f_inputlist(typval_T *argvars, typval_T *rettv)
7221{
7222 listitem_T *li;
7223 int selected;
7224 int mouse_used;
7225
7226#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007227 /* While starting up, there is no place to enter text. When running tests
7228 * with --not-a-term we assume feedkeys() will be used. */
7229 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007230 return;
7231#endif
7232 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7233 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007234 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007235 return;
7236 }
7237
7238 msg_start();
7239 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7240 lines_left = Rows; /* avoid more prompt */
7241 msg_scroll = TRUE;
7242 msg_clr_eos();
7243
7244 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7245 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007246 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007247 msg_putchar('\n');
7248 }
7249
7250 /* Ask for choice. */
7251 selected = prompt_for_number(&mouse_used);
7252 if (mouse_used)
7253 selected -= lines_left;
7254
7255 rettv->vval.v_number = selected;
7256}
7257
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007258static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7259
7260/*
7261 * "inputrestore()" function
7262 */
7263 static void
7264f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7265{
7266 if (ga_userinput.ga_len > 0)
7267 {
7268 --ga_userinput.ga_len;
7269 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7270 + ga_userinput.ga_len);
7271 /* default return is zero == OK */
7272 }
7273 else if (p_verbose > 1)
7274 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007275 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007276 rettv->vval.v_number = 1; /* Failed */
7277 }
7278}
7279
7280/*
7281 * "inputsave()" function
7282 */
7283 static void
7284f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7285{
7286 /* Add an entry to the stack of typeahead storage. */
7287 if (ga_grow(&ga_userinput, 1) == OK)
7288 {
7289 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7290 + ga_userinput.ga_len);
7291 ++ga_userinput.ga_len;
7292 /* default return is zero == OK */
7293 }
7294 else
7295 rettv->vval.v_number = 1; /* Failed */
7296}
7297
7298/*
7299 * "inputsecret()" function
7300 */
7301 static void
7302f_inputsecret(typval_T *argvars, typval_T *rettv)
7303{
7304 ++cmdline_star;
7305 ++inputsecret_flag;
7306 f_input(argvars, rettv);
7307 --cmdline_star;
7308 --inputsecret_flag;
7309}
7310
7311/*
7312 * "insert()" function
7313 */
7314 static void
7315f_insert(typval_T *argvars, typval_T *rettv)
7316{
7317 long before = 0;
7318 listitem_T *item;
7319 list_T *l;
7320 int error = FALSE;
7321
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007322 if (argvars[0].v_type == VAR_BLOB)
7323 {
7324 int val, len;
7325 char_u *p;
7326
7327 len = blob_len(argvars[0].vval.v_blob);
7328 if (argvars[2].v_type != VAR_UNKNOWN)
7329 {
7330 before = (long)tv_get_number_chk(&argvars[2], &error);
7331 if (error)
7332 return; // type error; errmsg already given
7333 if (before < 0 || before > len)
7334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007335 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007336 return;
7337 }
7338 }
7339 val = tv_get_number_chk(&argvars[1], &error);
7340 if (error)
7341 return;
7342 if (val < 0 || val > 255)
7343 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007344 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007345 return;
7346 }
7347
7348 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7349 return;
7350 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7351 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7352 *(p + before) = val;
7353 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7354
7355 copy_tv(&argvars[0], rettv);
7356 }
7357 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007358 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007359 else if ((l = argvars[0].vval.v_list) != NULL
7360 && !var_check_lock(l->lv_lock,
7361 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007362 {
7363 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007364 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007365 if (error)
7366 return; /* type error; errmsg already given */
7367
7368 if (before == l->lv_len)
7369 item = NULL;
7370 else
7371 {
7372 item = list_find(l, before);
7373 if (item == NULL)
7374 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007375 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007376 l = NULL;
7377 }
7378 }
7379 if (l != NULL)
7380 {
7381 list_insert_tv(l, &argvars[1], item);
7382 copy_tv(&argvars[0], rettv);
7383 }
7384 }
7385}
7386
7387/*
7388 * "invert(expr)" function
7389 */
7390 static void
7391f_invert(typval_T *argvars, typval_T *rettv)
7392{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007393 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007394}
7395
7396/*
7397 * "isdirectory()" function
7398 */
7399 static void
7400f_isdirectory(typval_T *argvars, typval_T *rettv)
7401{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007402 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007403}
7404
7405/*
7406 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7407 * or it refers to a List or Dictionary that is locked.
7408 */
7409 static int
7410tv_islocked(typval_T *tv)
7411{
7412 return (tv->v_lock & VAR_LOCKED)
7413 || (tv->v_type == VAR_LIST
7414 && tv->vval.v_list != NULL
7415 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7416 || (tv->v_type == VAR_DICT
7417 && tv->vval.v_dict != NULL
7418 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7419}
7420
7421/*
7422 * "islocked()" function
7423 */
7424 static void
7425f_islocked(typval_T *argvars, typval_T *rettv)
7426{
7427 lval_T lv;
7428 char_u *end;
7429 dictitem_T *di;
7430
7431 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007432 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007433 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007434 if (end != NULL && lv.ll_name != NULL)
7435 {
7436 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007437 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007438 else
7439 {
7440 if (lv.ll_tv == NULL)
7441 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007442 di = find_var(lv.ll_name, NULL, TRUE);
7443 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007444 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007445 /* Consider a variable locked when:
7446 * 1. the variable itself is locked
7447 * 2. the value of the variable is locked.
7448 * 3. the List or Dict value is locked.
7449 */
7450 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7451 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007452 }
7453 }
7454 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007455 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007456 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007457 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007458 else if (lv.ll_list != NULL)
7459 /* List item. */
7460 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7461 else
7462 /* Dictionary item. */
7463 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7464 }
7465 }
7466
7467 clear_lval(&lv);
7468}
7469
7470#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7471/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007472 * "isinf()" function
7473 */
7474 static void
7475f_isinf(typval_T *argvars, typval_T *rettv)
7476{
7477 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7478 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7479}
7480
7481/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007482 * "isnan()" function
7483 */
7484 static void
7485f_isnan(typval_T *argvars, typval_T *rettv)
7486{
7487 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7488 && isnan(argvars[0].vval.v_float);
7489}
7490#endif
7491
7492/*
7493 * "items(dict)" function
7494 */
7495 static void
7496f_items(typval_T *argvars, typval_T *rettv)
7497{
7498 dict_list(argvars, rettv, 2);
7499}
7500
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007501/*
7502 * "join()" function
7503 */
7504 static void
7505f_join(typval_T *argvars, typval_T *rettv)
7506{
7507 garray_T ga;
7508 char_u *sep;
7509
7510 if (argvars[0].v_type != VAR_LIST)
7511 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007512 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007513 return;
7514 }
7515 if (argvars[0].vval.v_list == NULL)
7516 return;
7517 if (argvars[1].v_type == VAR_UNKNOWN)
7518 sep = (char_u *)" ";
7519 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007520 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007521
7522 rettv->v_type = VAR_STRING;
7523
7524 if (sep != NULL)
7525 {
7526 ga_init2(&ga, (int)sizeof(char), 80);
7527 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7528 ga_append(&ga, NUL);
7529 rettv->vval.v_string = (char_u *)ga.ga_data;
7530 }
7531 else
7532 rettv->vval.v_string = NULL;
7533}
7534
7535/*
7536 * "js_decode()" function
7537 */
7538 static void
7539f_js_decode(typval_T *argvars, typval_T *rettv)
7540{
7541 js_read_T reader;
7542
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007543 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007544 reader.js_fill = NULL;
7545 reader.js_used = 0;
7546 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007547 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007548}
7549
7550/*
7551 * "js_encode()" function
7552 */
7553 static void
7554f_js_encode(typval_T *argvars, typval_T *rettv)
7555{
7556 rettv->v_type = VAR_STRING;
7557 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7558}
7559
7560/*
7561 * "json_decode()" function
7562 */
7563 static void
7564f_json_decode(typval_T *argvars, typval_T *rettv)
7565{
7566 js_read_T reader;
7567
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007568 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007569 reader.js_fill = NULL;
7570 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007571 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007572}
7573
7574/*
7575 * "json_encode()" function
7576 */
7577 static void
7578f_json_encode(typval_T *argvars, typval_T *rettv)
7579{
7580 rettv->v_type = VAR_STRING;
7581 rettv->vval.v_string = json_encode(&argvars[0], 0);
7582}
7583
7584/*
7585 * "keys()" function
7586 */
7587 static void
7588f_keys(typval_T *argvars, typval_T *rettv)
7589{
7590 dict_list(argvars, rettv, 0);
7591}
7592
7593/*
7594 * "last_buffer_nr()" function.
7595 */
7596 static void
7597f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7598{
7599 int n = 0;
7600 buf_T *buf;
7601
Bram Moolenaar29323592016-07-24 22:04:11 +02007602 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603 if (n < buf->b_fnum)
7604 n = buf->b_fnum;
7605
7606 rettv->vval.v_number = n;
7607}
7608
7609/*
7610 * "len()" function
7611 */
7612 static void
7613f_len(typval_T *argvars, typval_T *rettv)
7614{
7615 switch (argvars[0].v_type)
7616 {
7617 case VAR_STRING:
7618 case VAR_NUMBER:
7619 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007620 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007621 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007622 case VAR_BLOB:
7623 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7624 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007625 case VAR_LIST:
7626 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7627 break;
7628 case VAR_DICT:
7629 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7630 break;
7631 case VAR_UNKNOWN:
7632 case VAR_SPECIAL:
7633 case VAR_FLOAT:
7634 case VAR_FUNC:
7635 case VAR_PARTIAL:
7636 case VAR_JOB:
7637 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007638 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007639 break;
7640 }
7641}
7642
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007643 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007644libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007645{
7646#ifdef FEAT_LIBCALL
7647 char_u *string_in;
7648 char_u **string_result;
7649 int nr_result;
7650#endif
7651
7652 rettv->v_type = type;
7653 if (type != VAR_NUMBER)
7654 rettv->vval.v_string = NULL;
7655
7656 if (check_restricted() || check_secure())
7657 return;
7658
7659#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007660 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007661 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7662 {
7663 string_in = NULL;
7664 if (argvars[2].v_type == VAR_STRING)
7665 string_in = argvars[2].vval.v_string;
7666 if (type == VAR_NUMBER)
7667 string_result = NULL;
7668 else
7669 string_result = &rettv->vval.v_string;
7670 if (mch_libcall(argvars[0].vval.v_string,
7671 argvars[1].vval.v_string,
7672 string_in,
7673 argvars[2].vval.v_number,
7674 string_result,
7675 &nr_result) == OK
7676 && type == VAR_NUMBER)
7677 rettv->vval.v_number = nr_result;
7678 }
7679#endif
7680}
7681
7682/*
7683 * "libcall()" function
7684 */
7685 static void
7686f_libcall(typval_T *argvars, typval_T *rettv)
7687{
7688 libcall_common(argvars, rettv, VAR_STRING);
7689}
7690
7691/*
7692 * "libcallnr()" function
7693 */
7694 static void
7695f_libcallnr(typval_T *argvars, typval_T *rettv)
7696{
7697 libcall_common(argvars, rettv, VAR_NUMBER);
7698}
7699
7700/*
7701 * "line(string)" function
7702 */
7703 static void
7704f_line(typval_T *argvars, typval_T *rettv)
7705{
7706 linenr_T lnum = 0;
7707 pos_T *fp;
7708 int fnum;
7709
7710 fp = var2fpos(&argvars[0], TRUE, &fnum);
7711 if (fp != NULL)
7712 lnum = fp->lnum;
7713 rettv->vval.v_number = lnum;
7714}
7715
7716/*
7717 * "line2byte(lnum)" function
7718 */
7719 static void
7720f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7721{
7722#ifndef FEAT_BYTEOFF
7723 rettv->vval.v_number = -1;
7724#else
7725 linenr_T lnum;
7726
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007727 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007728 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7729 rettv->vval.v_number = -1;
7730 else
7731 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7732 if (rettv->vval.v_number >= 0)
7733 ++rettv->vval.v_number;
7734#endif
7735}
7736
7737/*
7738 * "lispindent(lnum)" function
7739 */
7740 static void
7741f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7742{
7743#ifdef FEAT_LISP
7744 pos_T pos;
7745 linenr_T lnum;
7746
7747 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007748 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007749 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7750 {
7751 curwin->w_cursor.lnum = lnum;
7752 rettv->vval.v_number = get_lisp_indent();
7753 curwin->w_cursor = pos;
7754 }
7755 else
7756#endif
7757 rettv->vval.v_number = -1;
7758}
7759
7760/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007761 * "list2str()" function
7762 */
7763 static void
7764f_list2str(typval_T *argvars, typval_T *rettv)
7765{
7766 list_T *l;
7767 listitem_T *li;
7768 garray_T ga;
7769 int utf8 = FALSE;
7770
7771 rettv->v_type = VAR_STRING;
7772 rettv->vval.v_string = NULL;
7773 if (argvars[0].v_type != VAR_LIST)
7774 {
7775 emsg(_(e_invarg));
7776 return;
7777 }
7778
7779 l = argvars[0].vval.v_list;
7780 if (l == NULL)
7781 return; // empty list results in empty string
7782
7783 if (argvars[1].v_type != VAR_UNKNOWN)
7784 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7785
7786 ga_init2(&ga, 1, 80);
7787 if (has_mbyte || utf8)
7788 {
7789 char_u buf[MB_MAXBYTES + 1];
7790 int (*char2bytes)(int, char_u *);
7791
7792 if (utf8 || enc_utf8)
7793 char2bytes = utf_char2bytes;
7794 else
7795 char2bytes = mb_char2bytes;
7796
7797 for (li = l->lv_first; li != NULL; li = li->li_next)
7798 {
7799 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7800 ga_concat(&ga, buf);
7801 }
7802 ga_append(&ga, NUL);
7803 }
7804 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7805 {
7806 for (li = l->lv_first; li != NULL; li = li->li_next)
7807 ga_append(&ga, tv_get_number(&li->li_tv));
7808 ga_append(&ga, NUL);
7809 }
7810
7811 rettv->v_type = VAR_STRING;
7812 rettv->vval.v_string = ga.ga_data;
7813}
7814
7815/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007816 * "localtime()" function
7817 */
7818 static void
7819f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7820{
7821 rettv->vval.v_number = (varnumber_T)time(NULL);
7822}
7823
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007824 static void
7825get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7826{
7827 char_u *keys;
7828 char_u *which;
7829 char_u buf[NUMBUFLEN];
7830 char_u *keys_buf = NULL;
7831 char_u *rhs;
7832 int mode;
7833 int abbr = FALSE;
7834 int get_dict = FALSE;
7835 mapblock_T *mp;
7836 int buffer_local;
7837
7838 /* return empty string for failure */
7839 rettv->v_type = VAR_STRING;
7840 rettv->vval.v_string = NULL;
7841
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007842 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007843 if (*keys == NUL)
7844 return;
7845
7846 if (argvars[1].v_type != VAR_UNKNOWN)
7847 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007848 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007849 if (argvars[2].v_type != VAR_UNKNOWN)
7850 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007851 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007852 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007853 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007854 }
7855 }
7856 else
7857 which = (char_u *)"";
7858 if (which == NULL)
7859 return;
7860
7861 mode = get_map_mode(&which, 0);
7862
7863 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7864 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7865 vim_free(keys_buf);
7866
7867 if (!get_dict)
7868 {
7869 /* Return a string. */
7870 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007871 {
7872 if (*rhs == NUL)
7873 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7874 else
7875 rettv->vval.v_string = str2special_save(rhs, FALSE);
7876 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007877
7878 }
7879 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7880 {
7881 /* Return a dictionary. */
7882 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7883 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7884 dict_T *dict = rettv->vval.v_dict;
7885
Bram Moolenaare0be1672018-07-08 16:50:37 +02007886 dict_add_string(dict, "lhs", lhs);
7887 dict_add_string(dict, "rhs", mp->m_orig_str);
7888 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7889 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7890 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007891 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7892 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007893 dict_add_number(dict, "buffer", (long)buffer_local);
7894 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7895 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007896
7897 vim_free(lhs);
7898 vim_free(mapmode);
7899 }
7900}
7901
7902#ifdef FEAT_FLOAT
7903/*
7904 * "log()" function
7905 */
7906 static void
7907f_log(typval_T *argvars, typval_T *rettv)
7908{
7909 float_T f = 0.0;
7910
7911 rettv->v_type = VAR_FLOAT;
7912 if (get_float_arg(argvars, &f) == OK)
7913 rettv->vval.v_float = log(f);
7914 else
7915 rettv->vval.v_float = 0.0;
7916}
7917
7918/*
7919 * "log10()" function
7920 */
7921 static void
7922f_log10(typval_T *argvars, typval_T *rettv)
7923{
7924 float_T f = 0.0;
7925
7926 rettv->v_type = VAR_FLOAT;
7927 if (get_float_arg(argvars, &f) == OK)
7928 rettv->vval.v_float = log10(f);
7929 else
7930 rettv->vval.v_float = 0.0;
7931}
7932#endif
7933
7934#ifdef FEAT_LUA
7935/*
7936 * "luaeval()" function
7937 */
7938 static void
7939f_luaeval(typval_T *argvars, typval_T *rettv)
7940{
7941 char_u *str;
7942 char_u buf[NUMBUFLEN];
7943
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007944 if (check_restricted() || check_secure())
7945 return;
7946
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007947 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007948 do_luaeval(str, argvars + 1, rettv);
7949}
7950#endif
7951
7952/*
7953 * "map()" function
7954 */
7955 static void
7956f_map(typval_T *argvars, typval_T *rettv)
7957{
7958 filter_map(argvars, rettv, TRUE);
7959}
7960
7961/*
7962 * "maparg()" function
7963 */
7964 static void
7965f_maparg(typval_T *argvars, typval_T *rettv)
7966{
7967 get_maparg(argvars, rettv, TRUE);
7968}
7969
7970/*
7971 * "mapcheck()" function
7972 */
7973 static void
7974f_mapcheck(typval_T *argvars, typval_T *rettv)
7975{
7976 get_maparg(argvars, rettv, FALSE);
7977}
7978
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007979typedef enum
7980{
7981 MATCH_END, /* matchend() */
7982 MATCH_MATCH, /* match() */
7983 MATCH_STR, /* matchstr() */
7984 MATCH_LIST, /* matchlist() */
7985 MATCH_POS /* matchstrpos() */
7986} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007987
7988 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007989find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990{
7991 char_u *str = NULL;
7992 long len = 0;
7993 char_u *expr = NULL;
7994 char_u *pat;
7995 regmatch_T regmatch;
7996 char_u patbuf[NUMBUFLEN];
7997 char_u strbuf[NUMBUFLEN];
7998 char_u *save_cpo;
7999 long start = 0;
8000 long nth = 1;
8001 colnr_T startcol = 0;
8002 int match = 0;
8003 list_T *l = NULL;
8004 listitem_T *li = NULL;
8005 long idx = 0;
8006 char_u *tofree = NULL;
8007
8008 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8009 save_cpo = p_cpo;
8010 p_cpo = (char_u *)"";
8011
8012 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008013 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008014 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008015 /* type MATCH_LIST: return empty list when there are no matches.
8016 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008017 if (rettv_list_alloc(rettv) == FAIL)
8018 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008019 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008020 && (list_append_string(rettv->vval.v_list,
8021 (char_u *)"", 0) == 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 || list_append_number(rettv->vval.v_list,
8027 (varnumber_T)-1) == FAIL))
8028 {
8029 list_free(rettv->vval.v_list);
8030 rettv->vval.v_list = NULL;
8031 goto theend;
8032 }
8033 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008034 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008035 {
8036 rettv->v_type = VAR_STRING;
8037 rettv->vval.v_string = NULL;
8038 }
8039
8040 if (argvars[0].v_type == VAR_LIST)
8041 {
8042 if ((l = argvars[0].vval.v_list) == NULL)
8043 goto theend;
8044 li = l->lv_first;
8045 }
8046 else
8047 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008048 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008049 len = (long)STRLEN(str);
8050 }
8051
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008052 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008053 if (pat == NULL)
8054 goto theend;
8055
8056 if (argvars[2].v_type != VAR_UNKNOWN)
8057 {
8058 int error = FALSE;
8059
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008060 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061 if (error)
8062 goto theend;
8063 if (l != NULL)
8064 {
8065 li = list_find(l, start);
8066 if (li == NULL)
8067 goto theend;
8068 idx = l->lv_idx; /* use the cached index */
8069 }
8070 else
8071 {
8072 if (start < 0)
8073 start = 0;
8074 if (start > len)
8075 goto theend;
8076 /* When "count" argument is there ignore matches before "start",
8077 * otherwise skip part of the string. Differs when pattern is "^"
8078 * or "\<". */
8079 if (argvars[3].v_type != VAR_UNKNOWN)
8080 startcol = start;
8081 else
8082 {
8083 str += start;
8084 len -= start;
8085 }
8086 }
8087
8088 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008089 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090 if (error)
8091 goto theend;
8092 }
8093
8094 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8095 if (regmatch.regprog != NULL)
8096 {
8097 regmatch.rm_ic = p_ic;
8098
8099 for (;;)
8100 {
8101 if (l != NULL)
8102 {
8103 if (li == NULL)
8104 {
8105 match = FALSE;
8106 break;
8107 }
8108 vim_free(tofree);
8109 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8110 if (str == NULL)
8111 break;
8112 }
8113
8114 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8115
8116 if (match && --nth <= 0)
8117 break;
8118 if (l == NULL && !match)
8119 break;
8120
8121 /* Advance to just after the match. */
8122 if (l != NULL)
8123 {
8124 li = li->li_next;
8125 ++idx;
8126 }
8127 else
8128 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008129 startcol = (colnr_T)(regmatch.startp[0]
8130 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008131 if (startcol > (colnr_T)len
8132 || str + startcol <= regmatch.startp[0])
8133 {
8134 match = FALSE;
8135 break;
8136 }
8137 }
8138 }
8139
8140 if (match)
8141 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008142 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008143 {
8144 listitem_T *li1 = rettv->vval.v_list->lv_first;
8145 listitem_T *li2 = li1->li_next;
8146 listitem_T *li3 = li2->li_next;
8147 listitem_T *li4 = li3->li_next;
8148
8149 vim_free(li1->li_tv.vval.v_string);
8150 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8151 (int)(regmatch.endp[0] - regmatch.startp[0]));
8152 li3->li_tv.vval.v_number =
8153 (varnumber_T)(regmatch.startp[0] - expr);
8154 li4->li_tv.vval.v_number =
8155 (varnumber_T)(regmatch.endp[0] - expr);
8156 if (l != NULL)
8157 li2->li_tv.vval.v_number = (varnumber_T)idx;
8158 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008159 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008160 {
8161 int i;
8162
8163 /* return list with matched string and submatches */
8164 for (i = 0; i < NSUBEXP; ++i)
8165 {
8166 if (regmatch.endp[i] == NULL)
8167 {
8168 if (list_append_string(rettv->vval.v_list,
8169 (char_u *)"", 0) == FAIL)
8170 break;
8171 }
8172 else if (list_append_string(rettv->vval.v_list,
8173 regmatch.startp[i],
8174 (int)(regmatch.endp[i] - regmatch.startp[i]))
8175 == FAIL)
8176 break;
8177 }
8178 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008179 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008180 {
8181 /* return matched string */
8182 if (l != NULL)
8183 copy_tv(&li->li_tv, rettv);
8184 else
8185 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8186 (int)(regmatch.endp[0] - regmatch.startp[0]));
8187 }
8188 else if (l != NULL)
8189 rettv->vval.v_number = idx;
8190 else
8191 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008192 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008193 rettv->vval.v_number =
8194 (varnumber_T)(regmatch.startp[0] - str);
8195 else
8196 rettv->vval.v_number =
8197 (varnumber_T)(regmatch.endp[0] - str);
8198 rettv->vval.v_number += (varnumber_T)(str - expr);
8199 }
8200 }
8201 vim_regfree(regmatch.regprog);
8202 }
8203
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008204theend:
8205 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008206 /* matchstrpos() without a list: drop the second item. */
8207 listitem_remove(rettv->vval.v_list,
8208 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008209 vim_free(tofree);
8210 p_cpo = save_cpo;
8211}
8212
8213/*
8214 * "match()" function
8215 */
8216 static void
8217f_match(typval_T *argvars, typval_T *rettv)
8218{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008219 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008220}
8221
Bram Moolenaar95e51472018-07-28 16:55:56 +02008222#ifdef FEAT_SEARCH_EXTRA
8223 static int
8224matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8225{
8226 dictitem_T *di;
8227
8228 if (tv->v_type != VAR_DICT)
8229 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008230 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008231 return FAIL;
8232 }
8233
8234 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008235 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008236 (char_u *)"conceal", FALSE);
8237
8238 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8239 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008240 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008241 if (*win == NULL)
8242 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008243 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008244 return FAIL;
8245 }
8246 }
8247
8248 return OK;
8249}
8250#endif
8251
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008252/*
8253 * "matchadd()" function
8254 */
8255 static void
8256f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8257{
8258#ifdef FEAT_SEARCH_EXTRA
8259 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008260 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8261 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008262 int prio = 10; /* default priority */
8263 int id = -1;
8264 int error = FALSE;
8265 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008266 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008267
8268 rettv->vval.v_number = -1;
8269
8270 if (grp == NULL || pat == NULL)
8271 return;
8272 if (argvars[2].v_type != VAR_UNKNOWN)
8273 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008274 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008275 if (argvars[3].v_type != VAR_UNKNOWN)
8276 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008277 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008278 if (argvars[4].v_type != VAR_UNKNOWN
8279 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8280 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008281 }
8282 }
8283 if (error == TRUE)
8284 return;
8285 if (id >= 1 && id <= 3)
8286 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008287 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008288 return;
8289 }
8290
Bram Moolenaar95e51472018-07-28 16:55:56 +02008291 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008292 conceal_char);
8293#endif
8294}
8295
8296/*
8297 * "matchaddpos()" function
8298 */
8299 static void
8300f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8301{
8302#ifdef FEAT_SEARCH_EXTRA
8303 char_u buf[NUMBUFLEN];
8304 char_u *group;
8305 int prio = 10;
8306 int id = -1;
8307 int error = FALSE;
8308 list_T *l;
8309 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008310 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008311
8312 rettv->vval.v_number = -1;
8313
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008314 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008315 if (group == NULL)
8316 return;
8317
8318 if (argvars[1].v_type != VAR_LIST)
8319 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008320 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008321 return;
8322 }
8323 l = argvars[1].vval.v_list;
8324 if (l == NULL)
8325 return;
8326
8327 if (argvars[2].v_type != VAR_UNKNOWN)
8328 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008329 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008330 if (argvars[3].v_type != VAR_UNKNOWN)
8331 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008332 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008333
8334 if (argvars[4].v_type != VAR_UNKNOWN
8335 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8336 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337 }
8338 }
8339 if (error == TRUE)
8340 return;
8341
8342 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8343 if (id == 1 || id == 2)
8344 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008345 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008346 return;
8347 }
8348
Bram Moolenaar95e51472018-07-28 16:55:56 +02008349 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008350 conceal_char);
8351#endif
8352}
8353
8354/*
8355 * "matcharg()" function
8356 */
8357 static void
8358f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8359{
8360 if (rettv_list_alloc(rettv) == OK)
8361 {
8362#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008363 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008364 matchitem_T *m;
8365
8366 if (id >= 1 && id <= 3)
8367 {
8368 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8369 {
8370 list_append_string(rettv->vval.v_list,
8371 syn_id2name(m->hlg_id), -1);
8372 list_append_string(rettv->vval.v_list, m->pattern, -1);
8373 }
8374 else
8375 {
8376 list_append_string(rettv->vval.v_list, NULL, -1);
8377 list_append_string(rettv->vval.v_list, NULL, -1);
8378 }
8379 }
8380#endif
8381 }
8382}
8383
8384/*
8385 * "matchdelete()" function
8386 */
8387 static void
8388f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8389{
8390#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008391 win_T *win = get_optional_window(argvars, 1);
8392
8393 if (win == NULL)
8394 rettv->vval.v_number = -1;
8395 else
8396 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008397 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008398#endif
8399}
8400
8401/*
8402 * "matchend()" function
8403 */
8404 static void
8405f_matchend(typval_T *argvars, typval_T *rettv)
8406{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008407 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408}
8409
8410/*
8411 * "matchlist()" function
8412 */
8413 static void
8414f_matchlist(typval_T *argvars, typval_T *rettv)
8415{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008416 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008417}
8418
8419/*
8420 * "matchstr()" function
8421 */
8422 static void
8423f_matchstr(typval_T *argvars, typval_T *rettv)
8424{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008425 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426}
8427
8428/*
8429 * "matchstrpos()" function
8430 */
8431 static void
8432f_matchstrpos(typval_T *argvars, typval_T *rettv)
8433{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008434 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008435}
8436
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008437 static void
8438max_min(typval_T *argvars, typval_T *rettv, int domax)
8439{
8440 varnumber_T n = 0;
8441 varnumber_T i;
8442 int error = FALSE;
8443
8444 if (argvars[0].v_type == VAR_LIST)
8445 {
8446 list_T *l;
8447 listitem_T *li;
8448
8449 l = argvars[0].vval.v_list;
8450 if (l != NULL)
8451 {
8452 li = l->lv_first;
8453 if (li != NULL)
8454 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008455 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008456 for (;;)
8457 {
8458 li = li->li_next;
8459 if (li == NULL)
8460 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008461 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008462 if (domax ? i > n : i < n)
8463 n = i;
8464 }
8465 }
8466 }
8467 }
8468 else if (argvars[0].v_type == VAR_DICT)
8469 {
8470 dict_T *d;
8471 int first = TRUE;
8472 hashitem_T *hi;
8473 int todo;
8474
8475 d = argvars[0].vval.v_dict;
8476 if (d != NULL)
8477 {
8478 todo = (int)d->dv_hashtab.ht_used;
8479 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8480 {
8481 if (!HASHITEM_EMPTY(hi))
8482 {
8483 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008484 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008485 if (first)
8486 {
8487 n = i;
8488 first = FALSE;
8489 }
8490 else if (domax ? i > n : i < n)
8491 n = i;
8492 }
8493 }
8494 }
8495 }
8496 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008497 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008498 rettv->vval.v_number = error ? 0 : n;
8499}
8500
8501/*
8502 * "max()" function
8503 */
8504 static void
8505f_max(typval_T *argvars, typval_T *rettv)
8506{
8507 max_min(argvars, rettv, TRUE);
8508}
8509
8510/*
8511 * "min()" function
8512 */
8513 static void
8514f_min(typval_T *argvars, typval_T *rettv)
8515{
8516 max_min(argvars, rettv, FALSE);
8517}
8518
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519/*
8520 * Create the directory in which "dir" is located, and higher levels when
8521 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008522 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008523 */
8524 static int
8525mkdir_recurse(char_u *dir, int prot)
8526{
8527 char_u *p;
8528 char_u *updir;
8529 int r = FAIL;
8530
8531 /* Get end of directory name in "dir".
8532 * We're done when it's "/" or "c:/". */
8533 p = gettail_sep(dir);
8534 if (p <= get_past_head(dir))
8535 return OK;
8536
8537 /* If the directory exists we're done. Otherwise: create it.*/
8538 updir = vim_strnsave(dir, (int)(p - dir));
8539 if (updir == NULL)
8540 return FAIL;
8541 if (mch_isdir(updir))
8542 r = OK;
8543 else if (mkdir_recurse(updir, prot) == OK)
8544 r = vim_mkdir_emsg(updir, prot);
8545 vim_free(updir);
8546 return r;
8547}
8548
8549#ifdef vim_mkdir
8550/*
8551 * "mkdir()" function
8552 */
8553 static void
8554f_mkdir(typval_T *argvars, typval_T *rettv)
8555{
8556 char_u *dir;
8557 char_u buf[NUMBUFLEN];
8558 int prot = 0755;
8559
8560 rettv->vval.v_number = FAIL;
8561 if (check_restricted() || check_secure())
8562 return;
8563
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008564 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008565 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008566 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008567
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008568 if (*gettail(dir) == NUL)
8569 /* remove trailing slashes */
8570 *gettail_sep(dir) = NUL;
8571
8572 if (argvars[1].v_type != VAR_UNKNOWN)
8573 {
8574 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008575 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008576 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008577 if (prot == -1)
8578 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008579 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008580 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008581 {
8582 if (mch_isdir(dir))
8583 {
8584 /* With the "p" flag it's OK if the dir already exists. */
8585 rettv->vval.v_number = OK;
8586 return;
8587 }
8588 mkdir_recurse(dir, prot);
8589 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008590 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008591 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008592}
8593#endif
8594
8595/*
8596 * "mode()" function
8597 */
8598 static void
8599f_mode(typval_T *argvars, typval_T *rettv)
8600{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008601 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602
Bram Moolenaar612cc382018-07-29 15:34:26 +02008603 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008604
8605 if (time_for_testing == 93784)
8606 {
8607 /* Testing the two-character code. */
8608 buf[0] = 'x';
8609 buf[1] = '!';
8610 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008611#ifdef FEAT_TERMINAL
8612 else if (term_use_loop())
8613 buf[0] = 't';
8614#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008615 else if (VIsual_active)
8616 {
8617 if (VIsual_select)
8618 buf[0] = VIsual_mode + 's' - 'v';
8619 else
8620 buf[0] = VIsual_mode;
8621 }
8622 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8623 || State == CONFIRM)
8624 {
8625 buf[0] = 'r';
8626 if (State == ASKMORE)
8627 buf[1] = 'm';
8628 else if (State == CONFIRM)
8629 buf[1] = '?';
8630 }
8631 else if (State == EXTERNCMD)
8632 buf[0] = '!';
8633 else if (State & INSERT)
8634 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008635 if (State & VREPLACE_FLAG)
8636 {
8637 buf[0] = 'R';
8638 buf[1] = 'v';
8639 }
8640 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008641 {
8642 if (State & REPLACE_FLAG)
8643 buf[0] = 'R';
8644 else
8645 buf[0] = 'i';
8646#ifdef FEAT_INS_EXPAND
8647 if (ins_compl_active())
8648 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008649 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008650 buf[1] = 'x';
8651#endif
8652 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008653 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008654 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 {
8656 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008657 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008658 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008659 else if (exmode_active == EXMODE_NORMAL)
8660 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008661 }
8662 else
8663 {
8664 buf[0] = 'n';
8665 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008666 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008667 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008668 // to be able to detect force-linewise/blockwise/characterwise operations
8669 buf[2] = motion_force;
8670 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008671 else if (restart_edit == 'I' || restart_edit == 'R'
8672 || restart_edit == 'V')
8673 {
8674 buf[1] = 'i';
8675 buf[2] = restart_edit;
8676 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008677 }
8678
8679 /* Clear out the minor mode when the argument is not a non-zero number or
8680 * non-empty string. */
8681 if (!non_zero_arg(&argvars[0]))
8682 buf[1] = NUL;
8683
8684 rettv->vval.v_string = vim_strsave(buf);
8685 rettv->v_type = VAR_STRING;
8686}
8687
8688#if defined(FEAT_MZSCHEME) || defined(PROTO)
8689/*
8690 * "mzeval()" function
8691 */
8692 static void
8693f_mzeval(typval_T *argvars, typval_T *rettv)
8694{
8695 char_u *str;
8696 char_u buf[NUMBUFLEN];
8697
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008698 if (check_restricted() || check_secure())
8699 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008700 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701 do_mzeval(str, rettv);
8702}
8703
8704 void
8705mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8706{
8707 typval_T argvars[3];
8708
8709 argvars[0].v_type = VAR_STRING;
8710 argvars[0].vval.v_string = name;
8711 copy_tv(args, &argvars[1]);
8712 argvars[2].v_type = VAR_UNKNOWN;
8713 f_call(argvars, rettv);
8714 clear_tv(&argvars[1]);
8715}
8716#endif
8717
8718/*
8719 * "nextnonblank()" function
8720 */
8721 static void
8722f_nextnonblank(typval_T *argvars, typval_T *rettv)
8723{
8724 linenr_T lnum;
8725
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008726 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008727 {
8728 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8729 {
8730 lnum = 0;
8731 break;
8732 }
8733 if (*skipwhite(ml_get(lnum)) != NUL)
8734 break;
8735 }
8736 rettv->vval.v_number = lnum;
8737}
8738
8739/*
8740 * "nr2char()" function
8741 */
8742 static void
8743f_nr2char(typval_T *argvars, typval_T *rettv)
8744{
8745 char_u buf[NUMBUFLEN];
8746
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008747 if (has_mbyte)
8748 {
8749 int utf8 = 0;
8750
8751 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008752 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008754 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008756 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757 }
8758 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008759 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008760 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008761 buf[1] = NUL;
8762 }
8763 rettv->v_type = VAR_STRING;
8764 rettv->vval.v_string = vim_strsave(buf);
8765}
8766
8767/*
8768 * "or(expr, expr)" function
8769 */
8770 static void
8771f_or(typval_T *argvars, typval_T *rettv)
8772{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008773 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8774 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008775}
8776
8777/*
8778 * "pathshorten()" function
8779 */
8780 static void
8781f_pathshorten(typval_T *argvars, typval_T *rettv)
8782{
8783 char_u *p;
8784
8785 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008786 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008787 if (p == NULL)
8788 rettv->vval.v_string = NULL;
8789 else
8790 {
8791 p = vim_strsave(p);
8792 rettv->vval.v_string = p;
8793 if (p != NULL)
8794 shorten_dir(p);
8795 }
8796}
8797
8798#ifdef FEAT_PERL
8799/*
8800 * "perleval()" function
8801 */
8802 static void
8803f_perleval(typval_T *argvars, typval_T *rettv)
8804{
8805 char_u *str;
8806 char_u buf[NUMBUFLEN];
8807
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008808 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008809 do_perleval(str, rettv);
8810}
8811#endif
8812
8813#ifdef FEAT_FLOAT
8814/*
8815 * "pow()" function
8816 */
8817 static void
8818f_pow(typval_T *argvars, typval_T *rettv)
8819{
8820 float_T fx = 0.0, fy = 0.0;
8821
8822 rettv->v_type = VAR_FLOAT;
8823 if (get_float_arg(argvars, &fx) == OK
8824 && get_float_arg(&argvars[1], &fy) == OK)
8825 rettv->vval.v_float = pow(fx, fy);
8826 else
8827 rettv->vval.v_float = 0.0;
8828}
8829#endif
8830
8831/*
8832 * "prevnonblank()" function
8833 */
8834 static void
8835f_prevnonblank(typval_T *argvars, typval_T *rettv)
8836{
8837 linenr_T lnum;
8838
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008839 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8841 lnum = 0;
8842 else
8843 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8844 --lnum;
8845 rettv->vval.v_number = lnum;
8846}
8847
8848/* This dummy va_list is here because:
8849 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8850 * - locally in the function results in a "used before set" warning
8851 * - using va_start() to initialize it gives "function with fixed args" error */
8852static va_list ap;
8853
8854/*
8855 * "printf()" function
8856 */
8857 static void
8858f_printf(typval_T *argvars, typval_T *rettv)
8859{
8860 char_u buf[NUMBUFLEN];
8861 int len;
8862 char_u *s;
8863 int saved_did_emsg = did_emsg;
8864 char *fmt;
8865
8866 rettv->v_type = VAR_STRING;
8867 rettv->vval.v_string = NULL;
8868
8869 /* Get the required length, allocate the buffer and do it for real. */
8870 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008871 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008872 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008873 if (!did_emsg)
8874 {
8875 s = alloc(len + 1);
8876 if (s != NULL)
8877 {
8878 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008879 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8880 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008881 }
8882 }
8883 did_emsg |= saved_did_emsg;
8884}
8885
8886/*
8887 * "pumvisible()" function
8888 */
8889 static void
8890f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8891{
8892#ifdef FEAT_INS_EXPAND
8893 if (pum_visible())
8894 rettv->vval.v_number = 1;
8895#endif
8896}
8897
8898#ifdef FEAT_PYTHON3
8899/*
8900 * "py3eval()" function
8901 */
8902 static void
8903f_py3eval(typval_T *argvars, typval_T *rettv)
8904{
8905 char_u *str;
8906 char_u buf[NUMBUFLEN];
8907
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008908 if (check_restricted() || check_secure())
8909 return;
8910
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008911 if (p_pyx == 0)
8912 p_pyx = 3;
8913
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008914 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915 do_py3eval(str, rettv);
8916}
8917#endif
8918
8919#ifdef FEAT_PYTHON
8920/*
8921 * "pyeval()" function
8922 */
8923 static void
8924f_pyeval(typval_T *argvars, typval_T *rettv)
8925{
8926 char_u *str;
8927 char_u buf[NUMBUFLEN];
8928
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008929 if (check_restricted() || check_secure())
8930 return;
8931
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008932 if (p_pyx == 0)
8933 p_pyx = 2;
8934
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008935 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008936 do_pyeval(str, rettv);
8937}
8938#endif
8939
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008940#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8941/*
8942 * "pyxeval()" function
8943 */
8944 static void
8945f_pyxeval(typval_T *argvars, typval_T *rettv)
8946{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008947 if (check_restricted() || check_secure())
8948 return;
8949
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008950# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8951 init_pyxversion();
8952 if (p_pyx == 2)
8953 f_pyeval(argvars, rettv);
8954 else
8955 f_py3eval(argvars, rettv);
8956# elif defined(FEAT_PYTHON)
8957 f_pyeval(argvars, rettv);
8958# elif defined(FEAT_PYTHON3)
8959 f_py3eval(argvars, rettv);
8960# endif
8961}
8962#endif
8963
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964/*
8965 * "range()" function
8966 */
8967 static void
8968f_range(typval_T *argvars, typval_T *rettv)
8969{
8970 varnumber_T start;
8971 varnumber_T end;
8972 varnumber_T stride = 1;
8973 varnumber_T i;
8974 int error = FALSE;
8975
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008976 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977 if (argvars[1].v_type == VAR_UNKNOWN)
8978 {
8979 end = start - 1;
8980 start = 0;
8981 }
8982 else
8983 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008984 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008985 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008986 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008987 }
8988
8989 if (error)
8990 return; /* type error; errmsg already given */
8991 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008992 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008993 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008994 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008995 else
8996 {
8997 if (rettv_list_alloc(rettv) == OK)
8998 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8999 if (list_append_number(rettv->vval.v_list,
9000 (varnumber_T)i) == FAIL)
9001 break;
9002 }
9003}
9004
9005/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009006 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009007 */
9008 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009009readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009010{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009011 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009012 typval_T save_val;
9013 typval_T rettv;
9014 typval_T argv[2];
9015 int retval = 0;
9016 int error = FALSE;
9017
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009018 if (expr->v_type == VAR_UNKNOWN)
9019 return 1;
9020
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009021 prepare_vimvar(VV_VAL, &save_val);
9022 set_vim_var_string(VV_VAL, name, -1);
9023 argv[0].v_type = VAR_STRING;
9024 argv[0].vval.v_string = name;
9025
9026 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9027 goto theend;
9028
9029 retval = tv_get_number_chk(&rettv, &error);
9030 if (error)
9031 retval = -1;
9032 clear_tv(&rettv);
9033
9034theend:
9035 set_vim_var_string(VV_VAL, NULL, 0);
9036 restore_vimvar(VV_VAL, &save_val);
9037 return retval;
9038}
9039
9040/*
9041 * "readdir()" function
9042 */
9043 static void
9044f_readdir(typval_T *argvars, typval_T *rettv)
9045{
9046 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009047 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009048 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009049 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009050 garray_T ga;
9051 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009052
9053 if (rettv_list_alloc(rettv) == FAIL)
9054 return;
9055 path = tv_get_string(&argvars[0]);
9056 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009057
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009058 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9059 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009060 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009061 for (i = 0; i < ga.ga_len; i++)
9062 {
9063 p = ((char_u **)ga.ga_data)[i];
9064 list_append_string(rettv->vval.v_list, p, -1);
9065 }
9066 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009067 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009068}
9069
9070/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009071 * "readfile()" function
9072 */
9073 static void
9074f_readfile(typval_T *argvars, typval_T *rettv)
9075{
9076 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009077 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009078 int failed = FALSE;
9079 char_u *fname;
9080 FILE *fd;
9081 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9082 int io_size = sizeof(buf);
9083 int readlen; /* size of last fread() */
9084 char_u *prev = NULL; /* previously read bytes, if any */
9085 long prevlen = 0; /* length of data in prev */
9086 long prevsize = 0; /* size of prev buffer */
9087 long maxline = MAXLNUM;
9088 long cnt = 0;
9089 char_u *p; /* position in buf */
9090 char_u *start; /* start of current line */
9091
9092 if (argvars[1].v_type != VAR_UNKNOWN)
9093 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009094 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009095 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009096 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9097 blob = TRUE;
9098
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009099 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009100 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009101 }
9102
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009103 if (blob)
9104 {
9105 if (rettv_blob_alloc(rettv) == FAIL)
9106 return;
9107 }
9108 else
9109 {
9110 if (rettv_list_alloc(rettv) == FAIL)
9111 return;
9112 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009113
9114 /* Always open the file in binary mode, library functions have a mind of
9115 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009116 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009117 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009119 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009120 return;
9121 }
9122
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009123 if (blob)
9124 {
9125 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009127 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009128 blob_free(rettv->vval.v_blob);
9129 }
9130 fclose(fd);
9131 return;
9132 }
9133
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009134 while (cnt < maxline || maxline < 0)
9135 {
9136 readlen = (int)fread(buf, 1, io_size, fd);
9137
9138 /* This for loop processes what was read, but is also entered at end
9139 * of file so that either:
9140 * - an incomplete line gets written
9141 * - a "binary" file gets an empty line at the end if it ends in a
9142 * newline. */
9143 for (p = buf, start = buf;
9144 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9145 ++p)
9146 {
9147 if (*p == '\n' || readlen <= 0)
9148 {
9149 listitem_T *li;
9150 char_u *s = NULL;
9151 long_u len = p - start;
9152
9153 /* Finished a line. Remove CRs before NL. */
9154 if (readlen > 0 && !binary)
9155 {
9156 while (len > 0 && start[len - 1] == '\r')
9157 --len;
9158 /* removal may cross back to the "prev" string */
9159 if (len == 0)
9160 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9161 --prevlen;
9162 }
9163 if (prevlen == 0)
9164 s = vim_strnsave(start, (int)len);
9165 else
9166 {
9167 /* Change "prev" buffer to be the right size. This way
9168 * the bytes are only copied once, and very long lines are
9169 * allocated only once. */
9170 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9171 {
9172 mch_memmove(s + prevlen, start, len);
9173 s[prevlen + len] = NUL;
9174 prev = NULL; /* the list will own the string */
9175 prevlen = prevsize = 0;
9176 }
9177 }
9178 if (s == NULL)
9179 {
9180 do_outofmem_msg((long_u) prevlen + len + 1);
9181 failed = TRUE;
9182 break;
9183 }
9184
9185 if ((li = listitem_alloc()) == NULL)
9186 {
9187 vim_free(s);
9188 failed = TRUE;
9189 break;
9190 }
9191 li->li_tv.v_type = VAR_STRING;
9192 li->li_tv.v_lock = 0;
9193 li->li_tv.vval.v_string = s;
9194 list_append(rettv->vval.v_list, li);
9195
9196 start = p + 1; /* step over newline */
9197 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9198 break;
9199 }
9200 else if (*p == NUL)
9201 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009202 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9203 * when finding the BF and check the previous two bytes. */
9204 else if (*p == 0xbf && enc_utf8 && !binary)
9205 {
9206 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9207 * + 1, these may be in the "prev" string. */
9208 char_u back1 = p >= buf + 1 ? p[-1]
9209 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9210 char_u back2 = p >= buf + 2 ? p[-2]
9211 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9212 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9213
9214 if (back2 == 0xef && back1 == 0xbb)
9215 {
9216 char_u *dest = p - 2;
9217
9218 /* Usually a BOM is at the beginning of a file, and so at
9219 * the beginning of a line; then we can just step over it.
9220 */
9221 if (start == dest)
9222 start = p + 1;
9223 else
9224 {
9225 /* have to shuffle buf to close gap */
9226 int adjust_prevlen = 0;
9227
9228 if (dest < buf)
9229 {
9230 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9231 dest = buf;
9232 }
9233 if (readlen > p - buf + 1)
9234 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9235 readlen -= 3 - adjust_prevlen;
9236 prevlen -= adjust_prevlen;
9237 p = dest - 1;
9238 }
9239 }
9240 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009241 } /* for */
9242
9243 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9244 break;
9245 if (start < p)
9246 {
9247 /* There's part of a line in buf, store it in "prev". */
9248 if (p - start + prevlen >= prevsize)
9249 {
9250 /* need bigger "prev" buffer */
9251 char_u *newprev;
9252
9253 /* A common use case is ordinary text files and "prev" gets a
9254 * fragment of a line, so the first allocation is made
9255 * small, to avoid repeatedly 'allocing' large and
9256 * 'reallocing' small. */
9257 if (prevsize == 0)
9258 prevsize = (long)(p - start);
9259 else
9260 {
9261 long grow50pc = (prevsize * 3) / 2;
9262 long growmin = (long)((p - start) * 2 + prevlen);
9263 prevsize = grow50pc > growmin ? grow50pc : growmin;
9264 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009265 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009266 if (newprev == NULL)
9267 {
9268 do_outofmem_msg((long_u)prevsize);
9269 failed = TRUE;
9270 break;
9271 }
9272 prev = newprev;
9273 }
9274 /* Add the line part to end of "prev". */
9275 mch_memmove(prev + prevlen, start, p - start);
9276 prevlen += (long)(p - start);
9277 }
9278 } /* while */
9279
9280 /*
9281 * For a negative line count use only the lines at the end of the file,
9282 * free the rest.
9283 */
9284 if (!failed && maxline < 0)
9285 while (cnt > -maxline)
9286 {
9287 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9288 --cnt;
9289 }
9290
9291 if (failed)
9292 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009293 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009294 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009295 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009296 }
9297
9298 vim_free(prev);
9299 fclose(fd);
9300}
9301
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009302 static void
9303return_register(int regname, typval_T *rettv)
9304{
9305 char_u buf[2] = {0, 0};
9306
9307 buf[0] = (char_u)regname;
9308 rettv->v_type = VAR_STRING;
9309 rettv->vval.v_string = vim_strsave(buf);
9310}
9311
9312/*
9313 * "reg_executing()" function
9314 */
9315 static void
9316f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9317{
9318 return_register(reg_executing, rettv);
9319}
9320
9321/*
9322 * "reg_recording()" function
9323 */
9324 static void
9325f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9326{
9327 return_register(reg_recording, rettv);
9328}
9329
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009330#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009331/*
9332 * Convert a List to proftime_T.
9333 * Return FAIL when there is something wrong.
9334 */
9335 static int
9336list2proftime(typval_T *arg, proftime_T *tm)
9337{
9338 long n1, n2;
9339 int error = FALSE;
9340
9341 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9342 || arg->vval.v_list->lv_len != 2)
9343 return FAIL;
9344 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9345 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009346# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009347 tm->HighPart = n1;
9348 tm->LowPart = n2;
9349# else
9350 tm->tv_sec = n1;
9351 tm->tv_usec = n2;
9352# endif
9353 return error ? FAIL : OK;
9354}
9355#endif /* FEAT_RELTIME */
9356
9357/*
9358 * "reltime()" function
9359 */
9360 static void
9361f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9362{
9363#ifdef FEAT_RELTIME
9364 proftime_T res;
9365 proftime_T start;
9366
9367 if (argvars[0].v_type == VAR_UNKNOWN)
9368 {
9369 /* No arguments: get current time. */
9370 profile_start(&res);
9371 }
9372 else if (argvars[1].v_type == VAR_UNKNOWN)
9373 {
9374 if (list2proftime(&argvars[0], &res) == FAIL)
9375 return;
9376 profile_end(&res);
9377 }
9378 else
9379 {
9380 /* Two arguments: compute the difference. */
9381 if (list2proftime(&argvars[0], &start) == FAIL
9382 || list2proftime(&argvars[1], &res) == FAIL)
9383 return;
9384 profile_sub(&res, &start);
9385 }
9386
9387 if (rettv_list_alloc(rettv) == OK)
9388 {
9389 long n1, n2;
9390
Bram Moolenaar4f974752019-02-17 17:44:42 +01009391# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009392 n1 = res.HighPart;
9393 n2 = res.LowPart;
9394# else
9395 n1 = res.tv_sec;
9396 n2 = res.tv_usec;
9397# endif
9398 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9399 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9400 }
9401#endif
9402}
9403
9404#ifdef FEAT_FLOAT
9405/*
9406 * "reltimefloat()" function
9407 */
9408 static void
9409f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9410{
9411# ifdef FEAT_RELTIME
9412 proftime_T tm;
9413# endif
9414
9415 rettv->v_type = VAR_FLOAT;
9416 rettv->vval.v_float = 0;
9417# ifdef FEAT_RELTIME
9418 if (list2proftime(&argvars[0], &tm) == OK)
9419 rettv->vval.v_float = profile_float(&tm);
9420# endif
9421}
9422#endif
9423
9424/*
9425 * "reltimestr()" function
9426 */
9427 static void
9428f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9429{
9430#ifdef FEAT_RELTIME
9431 proftime_T tm;
9432#endif
9433
9434 rettv->v_type = VAR_STRING;
9435 rettv->vval.v_string = NULL;
9436#ifdef FEAT_RELTIME
9437 if (list2proftime(&argvars[0], &tm) == OK)
9438 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9439#endif
9440}
9441
9442#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009443 static void
9444make_connection(void)
9445{
9446 if (X_DISPLAY == NULL
9447# ifdef FEAT_GUI
9448 && !gui.in_use
9449# endif
9450 )
9451 {
9452 x_force_connect = TRUE;
9453 setup_term_clip();
9454 x_force_connect = FALSE;
9455 }
9456}
9457
9458 static int
9459check_connection(void)
9460{
9461 make_connection();
9462 if (X_DISPLAY == NULL)
9463 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009464 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009465 return FAIL;
9466 }
9467 return OK;
9468}
9469#endif
9470
9471#ifdef FEAT_CLIENTSERVER
9472 static void
9473remote_common(typval_T *argvars, typval_T *rettv, int expr)
9474{
9475 char_u *server_name;
9476 char_u *keys;
9477 char_u *r = NULL;
9478 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009479 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009480# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009481 HWND w;
9482# else
9483 Window w;
9484# endif
9485
9486 if (check_restricted() || check_secure())
9487 return;
9488
9489# ifdef FEAT_X11
9490 if (check_connection() == FAIL)
9491 return;
9492# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009493 if (argvars[2].v_type != VAR_UNKNOWN
9494 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009495 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009497 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009498 if (server_name == NULL)
9499 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009500 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009501# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009502 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009503# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009504 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9505 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009506# endif
9507 {
9508 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009509 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009510 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009511 vim_free(r);
9512 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009513 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009514 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009515 return;
9516 }
9517
9518 rettv->vval.v_string = r;
9519
9520 if (argvars[2].v_type != VAR_UNKNOWN)
9521 {
9522 dictitem_T v;
9523 char_u str[30];
9524 char_u *idvar;
9525
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009526 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009527 if (idvar != NULL && *idvar != NUL)
9528 {
9529 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9530 v.di_tv.v_type = VAR_STRING;
9531 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009532 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009533 vim_free(v.di_tv.vval.v_string);
9534 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009535 }
9536}
9537#endif
9538
9539/*
9540 * "remote_expr()" function
9541 */
9542 static void
9543f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9544{
9545 rettv->v_type = VAR_STRING;
9546 rettv->vval.v_string = NULL;
9547#ifdef FEAT_CLIENTSERVER
9548 remote_common(argvars, rettv, TRUE);
9549#endif
9550}
9551
9552/*
9553 * "remote_foreground()" function
9554 */
9555 static void
9556f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9557{
9558#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009559# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009560 /* On Win32 it's done in this application. */
9561 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009562 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009563
9564 if (server_name != NULL)
9565 serverForeground(server_name);
9566 }
9567# else
9568 /* Send a foreground() expression to the server. */
9569 argvars[1].v_type = VAR_STRING;
9570 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9571 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009572 rettv->v_type = VAR_STRING;
9573 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009574 remote_common(argvars, rettv, TRUE);
9575 vim_free(argvars[1].vval.v_string);
9576# endif
9577#endif
9578}
9579
9580 static void
9581f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9582{
9583#ifdef FEAT_CLIENTSERVER
9584 dictitem_T v;
9585 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009586# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009587 long_u n = 0;
9588# endif
9589 char_u *serverid;
9590
9591 if (check_restricted() || check_secure())
9592 {
9593 rettv->vval.v_number = -1;
9594 return;
9595 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009596 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009597 if (serverid == NULL)
9598 {
9599 rettv->vval.v_number = -1;
9600 return; /* type error; errmsg already given */
9601 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009602# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009603 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9604 if (n == 0)
9605 rettv->vval.v_number = -1;
9606 else
9607 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009608 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009609 rettv->vval.v_number = (s != NULL);
9610 }
9611# else
9612 if (check_connection() == FAIL)
9613 return;
9614
9615 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9616 serverStrToWin(serverid), &s);
9617# endif
9618
9619 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9620 {
9621 char_u *retvar;
9622
9623 v.di_tv.v_type = VAR_STRING;
9624 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009625 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009626 if (retvar != NULL)
9627 set_var(retvar, &v.di_tv, FALSE);
9628 vim_free(v.di_tv.vval.v_string);
9629 }
9630#else
9631 rettv->vval.v_number = -1;
9632#endif
9633}
9634
9635 static void
9636f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9637{
9638 char_u *r = NULL;
9639
9640#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009641 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009642
9643 if (serverid != NULL && !check_restricted() && !check_secure())
9644 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009645 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009646# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009647 /* The server's HWND is encoded in the 'id' parameter */
9648 long_u n = 0;
9649# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009650
9651 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009652 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009653
Bram Moolenaar4f974752019-02-17 17:44:42 +01009654# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009655 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9656 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009657 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009658 if (r == NULL)
9659# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009660 if (check_connection() == FAIL
9661 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9662 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009663# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009664 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009665 }
9666#endif
9667 rettv->v_type = VAR_STRING;
9668 rettv->vval.v_string = r;
9669}
9670
9671/*
9672 * "remote_send()" function
9673 */
9674 static void
9675f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9676{
9677 rettv->v_type = VAR_STRING;
9678 rettv->vval.v_string = NULL;
9679#ifdef FEAT_CLIENTSERVER
9680 remote_common(argvars, rettv, FALSE);
9681#endif
9682}
9683
9684/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009685 * "remote_startserver()" function
9686 */
9687 static void
9688f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9689{
9690#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009691 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009692
9693 if (server == NULL)
9694 return; /* type error; errmsg already given */
9695 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009696 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009697 else
9698 {
9699# ifdef FEAT_X11
9700 if (check_connection() == OK)
9701 serverRegisterName(X_DISPLAY, server);
9702# else
9703 serverSetName(server);
9704# endif
9705 }
9706#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009707 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009708#endif
9709}
9710
9711/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009712 * "remove()" function
9713 */
9714 static void
9715f_remove(typval_T *argvars, typval_T *rettv)
9716{
9717 list_T *l;
9718 listitem_T *item, *item2;
9719 listitem_T *li;
9720 long idx;
9721 long end;
9722 char_u *key;
9723 dict_T *d;
9724 dictitem_T *di;
9725 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009726 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009727
9728 if (argvars[0].v_type == VAR_DICT)
9729 {
9730 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009731 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009732 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009733 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009734 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009735 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009736 if (key != NULL)
9737 {
9738 di = dict_find(d, key, -1);
9739 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009740 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009741 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9742 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9743 {
9744 *rettv = di->di_tv;
9745 init_tv(&di->di_tv);
9746 dictitem_remove(d, di);
9747 }
9748 }
9749 }
9750 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009751 else if (argvars[0].v_type == VAR_BLOB)
9752 {
9753 idx = (long)tv_get_number_chk(&argvars[1], &error);
9754 if (!error)
9755 {
9756 blob_T *b = argvars[0].vval.v_blob;
9757 int len = blob_len(b);
9758 char_u *p;
9759
9760 if (idx < 0)
9761 // count from the end
9762 idx = len + idx;
9763 if (idx < 0 || idx >= len)
9764 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009765 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009766 return;
9767 }
9768 if (argvars[2].v_type == VAR_UNKNOWN)
9769 {
9770 // Remove one item, return its value.
9771 p = (char_u *)b->bv_ga.ga_data;
9772 rettv->vval.v_number = (varnumber_T) *(p + idx);
9773 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9774 --b->bv_ga.ga_len;
9775 }
9776 else
9777 {
9778 blob_T *blob;
9779
9780 // Remove range of items, return list with values.
9781 end = (long)tv_get_number_chk(&argvars[2], &error);
9782 if (error)
9783 return;
9784 if (end < 0)
9785 // count from the end
9786 end = len + end;
9787 if (end >= len || idx > end)
9788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009789 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009790 return;
9791 }
9792 blob = blob_alloc();
9793 if (blob == NULL)
9794 return;
9795 blob->bv_ga.ga_len = end - idx + 1;
9796 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9797 {
9798 vim_free(blob);
9799 return;
9800 }
9801 p = (char_u *)b->bv_ga.ga_data;
9802 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9803 (size_t)(end - idx + 1));
9804 ++blob->bv_refcount;
9805 rettv->v_type = VAR_BLOB;
9806 rettv->vval.v_blob = blob;
9807
9808 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9809 b->bv_ga.ga_len -= end - idx + 1;
9810 }
9811 }
9812 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009813 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009814 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009815 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009816 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009817 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009818 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009819 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009820 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009821 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009822 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009823 else
9824 {
9825 if (argvars[2].v_type == VAR_UNKNOWN)
9826 {
9827 /* Remove one item, return its value. */
9828 vimlist_remove(l, item, item);
9829 *rettv = item->li_tv;
9830 vim_free(item);
9831 }
9832 else
9833 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009834 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009835 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009836 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009837 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009838 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009839 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009840 else
9841 {
9842 int cnt = 0;
9843
9844 for (li = item; li != NULL; li = li->li_next)
9845 {
9846 ++cnt;
9847 if (li == item2)
9848 break;
9849 }
9850 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009851 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009852 else
9853 {
9854 vimlist_remove(l, item, item2);
9855 if (rettv_list_alloc(rettv) == OK)
9856 {
9857 l = rettv->vval.v_list;
9858 l->lv_first = item;
9859 l->lv_last = item2;
9860 item->li_prev = NULL;
9861 item2->li_next = NULL;
9862 l->lv_len = cnt;
9863 }
9864 }
9865 }
9866 }
9867 }
9868 }
9869}
9870
9871/*
9872 * "rename({from}, {to})" function
9873 */
9874 static void
9875f_rename(typval_T *argvars, typval_T *rettv)
9876{
9877 char_u buf[NUMBUFLEN];
9878
9879 if (check_restricted() || check_secure())
9880 rettv->vval.v_number = -1;
9881 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009882 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9883 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009884}
9885
9886/*
9887 * "repeat()" function
9888 */
9889 static void
9890f_repeat(typval_T *argvars, typval_T *rettv)
9891{
9892 char_u *p;
9893 int n;
9894 int slen;
9895 int len;
9896 char_u *r;
9897 int i;
9898
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009899 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009900 if (argvars[0].v_type == VAR_LIST)
9901 {
9902 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9903 while (n-- > 0)
9904 if (list_extend(rettv->vval.v_list,
9905 argvars[0].vval.v_list, NULL) == FAIL)
9906 break;
9907 }
9908 else
9909 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009910 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009911 rettv->v_type = VAR_STRING;
9912 rettv->vval.v_string = NULL;
9913
9914 slen = (int)STRLEN(p);
9915 len = slen * n;
9916 if (len <= 0)
9917 return;
9918
9919 r = alloc(len + 1);
9920 if (r != NULL)
9921 {
9922 for (i = 0; i < n; i++)
9923 mch_memmove(r + i * slen, p, (size_t)slen);
9924 r[len] = NUL;
9925 }
9926
9927 rettv->vval.v_string = r;
9928 }
9929}
9930
9931/*
9932 * "resolve()" function
9933 */
9934 static void
9935f_resolve(typval_T *argvars, typval_T *rettv)
9936{
9937 char_u *p;
9938#ifdef HAVE_READLINK
9939 char_u *buf = NULL;
9940#endif
9941
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009942 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943#ifdef FEAT_SHORTCUT
9944 {
9945 char_u *v = NULL;
9946
Bram Moolenaardce1e892019-02-10 23:18:53 +01009947 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009948 if (v != NULL)
9949 rettv->vval.v_string = v;
9950 else
9951 rettv->vval.v_string = vim_strsave(p);
9952 }
9953#else
9954# ifdef HAVE_READLINK
9955 {
9956 char_u *cpy;
9957 int len;
9958 char_u *remain = NULL;
9959 char_u *q;
9960 int is_relative_to_current = FALSE;
9961 int has_trailing_pathsep = FALSE;
9962 int limit = 100;
9963
9964 p = vim_strsave(p);
9965
9966 if (p[0] == '.' && (vim_ispathsep(p[1])
9967 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9968 is_relative_to_current = TRUE;
9969
9970 len = STRLEN(p);
9971 if (len > 0 && after_pathsep(p, p + len))
9972 {
9973 has_trailing_pathsep = TRUE;
9974 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9975 }
9976
9977 q = getnextcomp(p);
9978 if (*q != NUL)
9979 {
9980 /* Separate the first path component in "p", and keep the
9981 * remainder (beginning with the path separator). */
9982 remain = vim_strsave(q - 1);
9983 q[-1] = NUL;
9984 }
9985
9986 buf = alloc(MAXPATHL + 1);
9987 if (buf == NULL)
9988 goto fail;
9989
9990 for (;;)
9991 {
9992 for (;;)
9993 {
9994 len = readlink((char *)p, (char *)buf, MAXPATHL);
9995 if (len <= 0)
9996 break;
9997 buf[len] = NUL;
9998
9999 if (limit-- == 0)
10000 {
10001 vim_free(p);
10002 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010003 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010004 rettv->vval.v_string = NULL;
10005 goto fail;
10006 }
10007
10008 /* Ensure that the result will have a trailing path separator
10009 * if the argument has one. */
10010 if (remain == NULL && has_trailing_pathsep)
10011 add_pathsep(buf);
10012
10013 /* Separate the first path component in the link value and
10014 * concatenate the remainders. */
10015 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10016 if (*q != NUL)
10017 {
10018 if (remain == NULL)
10019 remain = vim_strsave(q - 1);
10020 else
10021 {
10022 cpy = concat_str(q - 1, remain);
10023 if (cpy != NULL)
10024 {
10025 vim_free(remain);
10026 remain = cpy;
10027 }
10028 }
10029 q[-1] = NUL;
10030 }
10031
10032 q = gettail(p);
10033 if (q > p && *q == NUL)
10034 {
10035 /* Ignore trailing path separator. */
10036 q[-1] = NUL;
10037 q = gettail(p);
10038 }
10039 if (q > p && !mch_isFullName(buf))
10040 {
10041 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010042 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010043 if (cpy != NULL)
10044 {
10045 STRCPY(cpy, p);
10046 STRCPY(gettail(cpy), buf);
10047 vim_free(p);
10048 p = cpy;
10049 }
10050 }
10051 else
10052 {
10053 vim_free(p);
10054 p = vim_strsave(buf);
10055 }
10056 }
10057
10058 if (remain == NULL)
10059 break;
10060
10061 /* Append the first path component of "remain" to "p". */
10062 q = getnextcomp(remain + 1);
10063 len = q - remain - (*q != NUL);
10064 cpy = vim_strnsave(p, STRLEN(p) + len);
10065 if (cpy != NULL)
10066 {
10067 STRNCAT(cpy, remain, len);
10068 vim_free(p);
10069 p = cpy;
10070 }
10071 /* Shorten "remain". */
10072 if (*q != NUL)
10073 STRMOVE(remain, q - 1);
10074 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010075 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010076 }
10077
10078 /* If the result is a relative path name, make it explicitly relative to
10079 * the current directory if and only if the argument had this form. */
10080 if (!vim_ispathsep(*p))
10081 {
10082 if (is_relative_to_current
10083 && *p != NUL
10084 && !(p[0] == '.'
10085 && (p[1] == NUL
10086 || vim_ispathsep(p[1])
10087 || (p[1] == '.'
10088 && (p[2] == NUL
10089 || vim_ispathsep(p[2]))))))
10090 {
10091 /* Prepend "./". */
10092 cpy = concat_str((char_u *)"./", p);
10093 if (cpy != NULL)
10094 {
10095 vim_free(p);
10096 p = cpy;
10097 }
10098 }
10099 else if (!is_relative_to_current)
10100 {
10101 /* Strip leading "./". */
10102 q = p;
10103 while (q[0] == '.' && vim_ispathsep(q[1]))
10104 q += 2;
10105 if (q > p)
10106 STRMOVE(p, p + 2);
10107 }
10108 }
10109
10110 /* Ensure that the result will have no trailing path separator
10111 * if the argument had none. But keep "/" or "//". */
10112 if (!has_trailing_pathsep)
10113 {
10114 q = p + STRLEN(p);
10115 if (after_pathsep(p, q))
10116 *gettail_sep(p) = NUL;
10117 }
10118
10119 rettv->vval.v_string = p;
10120 }
10121# else
10122 rettv->vval.v_string = vim_strsave(p);
10123# endif
10124#endif
10125
10126 simplify_filename(rettv->vval.v_string);
10127
10128#ifdef HAVE_READLINK
10129fail:
10130 vim_free(buf);
10131#endif
10132 rettv->v_type = VAR_STRING;
10133}
10134
10135/*
10136 * "reverse({list})" function
10137 */
10138 static void
10139f_reverse(typval_T *argvars, typval_T *rettv)
10140{
10141 list_T *l;
10142 listitem_T *li, *ni;
10143
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010144 if (argvars[0].v_type == VAR_BLOB)
10145 {
10146 blob_T *b = argvars[0].vval.v_blob;
10147 int i, len = blob_len(b);
10148
10149 for (i = 0; i < len / 2; i++)
10150 {
10151 int tmp = blob_get(b, i);
10152
10153 blob_set(b, i, blob_get(b, len - i - 1));
10154 blob_set(b, len - i - 1, tmp);
10155 }
10156 rettv_blob_set(rettv, b);
10157 return;
10158 }
10159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010160 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010161 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010162 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010163 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010164 (char_u *)N_("reverse() argument"), TRUE))
10165 {
10166 li = l->lv_last;
10167 l->lv_first = l->lv_last = NULL;
10168 l->lv_len = 0;
10169 while (li != NULL)
10170 {
10171 ni = li->li_prev;
10172 list_append(l, li);
10173 li = ni;
10174 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010175 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010176 l->lv_idx = l->lv_len - l->lv_idx - 1;
10177 }
10178}
10179
10180#define SP_NOMOVE 0x01 /* don't move cursor */
10181#define SP_REPEAT 0x02 /* repeat to find outer pair */
10182#define SP_RETCOUNT 0x04 /* return matchcount */
10183#define SP_SETPCMARK 0x08 /* set previous context mark */
10184#define SP_START 0x10 /* accept match at start position */
10185#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10186#define SP_END 0x40 /* leave cursor at end of match */
10187#define SP_COLUMN 0x80 /* start at cursor column */
10188
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010189/*
10190 * Get flags for a search function.
10191 * Possibly sets "p_ws".
10192 * Returns BACKWARD, FORWARD or zero (for an error).
10193 */
10194 static int
10195get_search_arg(typval_T *varp, int *flagsp)
10196{
10197 int dir = FORWARD;
10198 char_u *flags;
10199 char_u nbuf[NUMBUFLEN];
10200 int mask;
10201
10202 if (varp->v_type != VAR_UNKNOWN)
10203 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010204 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010205 if (flags == NULL)
10206 return 0; /* type error; errmsg already given */
10207 while (*flags != NUL)
10208 {
10209 switch (*flags)
10210 {
10211 case 'b': dir = BACKWARD; break;
10212 case 'w': p_ws = TRUE; break;
10213 case 'W': p_ws = FALSE; break;
10214 default: mask = 0;
10215 if (flagsp != NULL)
10216 switch (*flags)
10217 {
10218 case 'c': mask = SP_START; break;
10219 case 'e': mask = SP_END; break;
10220 case 'm': mask = SP_RETCOUNT; break;
10221 case 'n': mask = SP_NOMOVE; break;
10222 case 'p': mask = SP_SUBPAT; break;
10223 case 'r': mask = SP_REPEAT; break;
10224 case 's': mask = SP_SETPCMARK; break;
10225 case 'z': mask = SP_COLUMN; break;
10226 }
10227 if (mask == 0)
10228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010229 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010230 dir = 0;
10231 }
10232 else
10233 *flagsp |= mask;
10234 }
10235 if (dir == 0)
10236 break;
10237 ++flags;
10238 }
10239 }
10240 return dir;
10241}
10242
10243/*
10244 * Shared by search() and searchpos() functions.
10245 */
10246 static int
10247search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10248{
10249 int flags;
10250 char_u *pat;
10251 pos_T pos;
10252 pos_T save_cursor;
10253 int save_p_ws = p_ws;
10254 int dir;
10255 int retval = 0; /* default: FAIL */
10256 long lnum_stop = 0;
10257 proftime_T tm;
10258#ifdef FEAT_RELTIME
10259 long time_limit = 0;
10260#endif
10261 int options = SEARCH_KEEP;
10262 int subpatnum;
10263
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010264 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010265 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10266 if (dir == 0)
10267 goto theend;
10268 flags = *flagsp;
10269 if (flags & SP_START)
10270 options |= SEARCH_START;
10271 if (flags & SP_END)
10272 options |= SEARCH_END;
10273 if (flags & SP_COLUMN)
10274 options |= SEARCH_COL;
10275
10276 /* Optional arguments: line number to stop searching and timeout. */
10277 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10278 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010279 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010280 if (lnum_stop < 0)
10281 goto theend;
10282#ifdef FEAT_RELTIME
10283 if (argvars[3].v_type != VAR_UNKNOWN)
10284 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010285 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010286 if (time_limit < 0)
10287 goto theend;
10288 }
10289#endif
10290 }
10291
10292#ifdef FEAT_RELTIME
10293 /* Set the time limit, if there is one. */
10294 profile_setlimit(time_limit, &tm);
10295#endif
10296
10297 /*
10298 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10299 * Check to make sure only those flags are set.
10300 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10301 * flags cannot be set. Check for that condition also.
10302 */
10303 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10304 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010306 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010307 goto theend;
10308 }
10309
10310 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010311 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010312 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010313 if (subpatnum != FAIL)
10314 {
10315 if (flags & SP_SUBPAT)
10316 retval = subpatnum;
10317 else
10318 retval = pos.lnum;
10319 if (flags & SP_SETPCMARK)
10320 setpcmark();
10321 curwin->w_cursor = pos;
10322 if (match_pos != NULL)
10323 {
10324 /* Store the match cursor position */
10325 match_pos->lnum = pos.lnum;
10326 match_pos->col = pos.col + 1;
10327 }
10328 /* "/$" will put the cursor after the end of the line, may need to
10329 * correct that here */
10330 check_cursor();
10331 }
10332
10333 /* If 'n' flag is used: restore cursor position. */
10334 if (flags & SP_NOMOVE)
10335 curwin->w_cursor = save_cursor;
10336 else
10337 curwin->w_set_curswant = TRUE;
10338theend:
10339 p_ws = save_p_ws;
10340
10341 return retval;
10342}
10343
10344#ifdef FEAT_FLOAT
10345
10346/*
10347 * round() is not in C90, use ceil() or floor() instead.
10348 */
10349 float_T
10350vim_round(float_T f)
10351{
10352 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10353}
10354
10355/*
10356 * "round({float})" function
10357 */
10358 static void
10359f_round(typval_T *argvars, typval_T *rettv)
10360{
10361 float_T f = 0.0;
10362
10363 rettv->v_type = VAR_FLOAT;
10364 if (get_float_arg(argvars, &f) == OK)
10365 rettv->vval.v_float = vim_round(f);
10366 else
10367 rettv->vval.v_float = 0.0;
10368}
10369#endif
10370
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010371#ifdef FEAT_RUBY
10372/*
10373 * "rubyeval()" function
10374 */
10375 static void
10376f_rubyeval(typval_T *argvars, typval_T *rettv)
10377{
10378 char_u *str;
10379 char_u buf[NUMBUFLEN];
10380
10381 str = tv_get_string_buf(&argvars[0], buf);
10382 do_rubyeval(str, rettv);
10383}
10384#endif
10385
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010386/*
10387 * "screenattr()" function
10388 */
10389 static void
10390f_screenattr(typval_T *argvars, typval_T *rettv)
10391{
10392 int row;
10393 int col;
10394 int c;
10395
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010396 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10397 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010398 if (row < 0 || row >= screen_Rows
10399 || col < 0 || col >= screen_Columns)
10400 c = -1;
10401 else
10402 c = ScreenAttrs[LineOffset[row] + col];
10403 rettv->vval.v_number = c;
10404}
10405
10406/*
10407 * "screenchar()" function
10408 */
10409 static void
10410f_screenchar(typval_T *argvars, typval_T *rettv)
10411{
10412 int row;
10413 int col;
10414 int off;
10415 int c;
10416
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010417 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10418 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010419 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010420 c = -1;
10421 else
10422 {
10423 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010424 if (enc_utf8 && ScreenLinesUC[off] != 0)
10425 c = ScreenLinesUC[off];
10426 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010427 c = ScreenLines[off];
10428 }
10429 rettv->vval.v_number = c;
10430}
10431
10432/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010433 * "screenchars()" function
10434 */
10435 static void
10436f_screenchars(typval_T *argvars, typval_T *rettv)
10437{
10438 int row;
10439 int col;
10440 int off;
10441 int c;
10442 int i;
10443
10444 if (rettv_list_alloc(rettv) == FAIL)
10445 return;
10446 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10447 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10448 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10449 return;
10450
10451 off = LineOffset[row] + col;
10452 if (enc_utf8 && ScreenLinesUC[off] != 0)
10453 c = ScreenLinesUC[off];
10454 else
10455 c = ScreenLines[off];
10456 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10457
10458 if (enc_utf8)
10459
10460 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10461 list_append_number(rettv->vval.v_list,
10462 (varnumber_T)ScreenLinesC[i][off]);
10463}
10464
10465/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010466 * "screencol()" function
10467 *
10468 * First column is 1 to be consistent with virtcol().
10469 */
10470 static void
10471f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10472{
10473 rettv->vval.v_number = screen_screencol() + 1;
10474}
10475
10476/*
10477 * "screenrow()" function
10478 */
10479 static void
10480f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10481{
10482 rettv->vval.v_number = screen_screenrow() + 1;
10483}
10484
10485/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010486 * "screenstring()" function
10487 */
10488 static void
10489f_screenstring(typval_T *argvars, typval_T *rettv)
10490{
10491 int row;
10492 int col;
10493 int off;
10494 int c;
10495 int i;
10496 char_u buf[MB_MAXBYTES + 1];
10497 int buflen = 0;
10498
10499 rettv->vval.v_string = NULL;
10500 rettv->v_type = VAR_STRING;
10501
10502 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10503 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10504 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10505 return;
10506
10507 off = LineOffset[row] + col;
10508 if (enc_utf8 && ScreenLinesUC[off] != 0)
10509 c = ScreenLinesUC[off];
10510 else
10511 c = ScreenLines[off];
10512 buflen += mb_char2bytes(c, buf);
10513
10514 if (enc_utf8)
10515 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10516 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10517
10518 buf[buflen] = NUL;
10519 rettv->vval.v_string = vim_strsave(buf);
10520}
10521
10522/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010523 * "search()" function
10524 */
10525 static void
10526f_search(typval_T *argvars, typval_T *rettv)
10527{
10528 int flags = 0;
10529
10530 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10531}
10532
10533/*
10534 * "searchdecl()" function
10535 */
10536 static void
10537f_searchdecl(typval_T *argvars, typval_T *rettv)
10538{
10539 int locally = 1;
10540 int thisblock = 0;
10541 int error = FALSE;
10542 char_u *name;
10543
10544 rettv->vval.v_number = 1; /* default: FAIL */
10545
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010546 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010547 if (argvars[1].v_type != VAR_UNKNOWN)
10548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010549 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010550 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010551 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010552 }
10553 if (!error && name != NULL)
10554 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10555 locally, thisblock, SEARCH_KEEP) == FAIL;
10556}
10557
10558/*
10559 * Used by searchpair() and searchpairpos()
10560 */
10561 static int
10562searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10563{
10564 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010565 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010566 int save_p_ws = p_ws;
10567 int dir;
10568 int flags = 0;
10569 char_u nbuf1[NUMBUFLEN];
10570 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010571 int retval = 0; /* default: FAIL */
10572 long lnum_stop = 0;
10573 long time_limit = 0;
10574
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010575 /* Get the three pattern arguments: start, middle, end. Will result in an
10576 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010577 spat = tv_get_string_chk(&argvars[0]);
10578 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10579 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010580 if (spat == NULL || mpat == NULL || epat == NULL)
10581 goto theend; /* type error */
10582
10583 /* Handle the optional fourth argument: flags */
10584 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10585 if (dir == 0)
10586 goto theend;
10587
10588 /* Don't accept SP_END or SP_SUBPAT.
10589 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10590 */
10591 if ((flags & (SP_END | SP_SUBPAT)) != 0
10592 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010594 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010595 goto theend;
10596 }
10597
10598 /* Using 'r' implies 'W', otherwise it doesn't work. */
10599 if (flags & SP_REPEAT)
10600 p_ws = FALSE;
10601
10602 /* Optional fifth argument: skip expression */
10603 if (argvars[3].v_type == VAR_UNKNOWN
10604 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010605 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010606 else
10607 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010608 skip = &argvars[4];
10609 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10610 && skip->v_type != VAR_STRING)
10611 {
10612 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010613 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010614 goto theend;
10615 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010616 if (argvars[5].v_type != VAR_UNKNOWN)
10617 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010618 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010620 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010621 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010622 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010623 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010624#ifdef FEAT_RELTIME
10625 if (argvars[6].v_type != VAR_UNKNOWN)
10626 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010627 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010628 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010630 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010631 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010632 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010633 }
10634#endif
10635 }
10636 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010637
10638 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10639 match_pos, lnum_stop, time_limit);
10640
10641theend:
10642 p_ws = save_p_ws;
10643
10644 return retval;
10645}
10646
10647/*
10648 * "searchpair()" function
10649 */
10650 static void
10651f_searchpair(typval_T *argvars, typval_T *rettv)
10652{
10653 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10654}
10655
10656/*
10657 * "searchpairpos()" function
10658 */
10659 static void
10660f_searchpairpos(typval_T *argvars, typval_T *rettv)
10661{
10662 pos_T match_pos;
10663 int lnum = 0;
10664 int col = 0;
10665
10666 if (rettv_list_alloc(rettv) == FAIL)
10667 return;
10668
10669 if (searchpair_cmn(argvars, &match_pos) > 0)
10670 {
10671 lnum = match_pos.lnum;
10672 col = match_pos.col;
10673 }
10674
10675 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10676 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10677}
10678
10679/*
10680 * Search for a start/middle/end thing.
10681 * Used by searchpair(), see its documentation for the details.
10682 * Returns 0 or -1 for no match,
10683 */
10684 long
10685do_searchpair(
10686 char_u *spat, /* start pattern */
10687 char_u *mpat, /* middle pattern */
10688 char_u *epat, /* end pattern */
10689 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010690 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010691 int flags, /* SP_SETPCMARK and other SP_ values */
10692 pos_T *match_pos,
10693 linenr_T lnum_stop, /* stop at this line if not zero */
10694 long time_limit UNUSED) /* stop after this many msec */
10695{
10696 char_u *save_cpo;
10697 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10698 long retval = 0;
10699 pos_T pos;
10700 pos_T firstpos;
10701 pos_T foundpos;
10702 pos_T save_cursor;
10703 pos_T save_pos;
10704 int n;
10705 int r;
10706 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010707 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010708 int err;
10709 int options = SEARCH_KEEP;
10710 proftime_T tm;
10711
10712 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10713 save_cpo = p_cpo;
10714 p_cpo = empty_option;
10715
10716#ifdef FEAT_RELTIME
10717 /* Set the time limit, if there is one. */
10718 profile_setlimit(time_limit, &tm);
10719#endif
10720
10721 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10722 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010723 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
10724 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010725 if (pat2 == NULL || pat3 == NULL)
10726 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010727 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010728 if (*mpat == NUL)
10729 STRCPY(pat3, pat2);
10730 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010731 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010732 spat, epat, mpat);
10733 if (flags & SP_START)
10734 options |= SEARCH_START;
10735
Bram Moolenaar48570482017-10-30 21:48:41 +010010736 if (skip != NULL)
10737 {
10738 /* Empty string means to not use the skip expression. */
10739 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10740 use_skip = skip->vval.v_string != NULL
10741 && *skip->vval.v_string != NUL;
10742 }
10743
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010744 save_cursor = curwin->w_cursor;
10745 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010746 CLEAR_POS(&firstpos);
10747 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010748 pat = pat3;
10749 for (;;)
10750 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010751 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010752 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010753 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010754 /* didn't find it or found the first match again: FAIL */
10755 break;
10756
10757 if (firstpos.lnum == 0)
10758 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010759 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010760 {
10761 /* Found the same position again. Can happen with a pattern that
10762 * has "\zs" at the end and searching backwards. Advance one
10763 * character and try again. */
10764 if (dir == BACKWARD)
10765 decl(&pos);
10766 else
10767 incl(&pos);
10768 }
10769 foundpos = pos;
10770
10771 /* clear the start flag to avoid getting stuck here */
10772 options &= ~SEARCH_START;
10773
10774 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010775 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010776 {
10777 save_pos = curwin->w_cursor;
10778 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010779 err = FALSE;
10780 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010781 curwin->w_cursor = save_pos;
10782 if (err)
10783 {
10784 /* Evaluating {skip} caused an error, break here. */
10785 curwin->w_cursor = save_cursor;
10786 retval = -1;
10787 break;
10788 }
10789 if (r)
10790 continue;
10791 }
10792
10793 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10794 {
10795 /* Found end when searching backwards or start when searching
10796 * forward: nested pair. */
10797 ++nest;
10798 pat = pat2; /* nested, don't search for middle */
10799 }
10800 else
10801 {
10802 /* Found end when searching forward or start when searching
10803 * backward: end of (nested) pair; or found middle in outer pair. */
10804 if (--nest == 1)
10805 pat = pat3; /* outer level, search for middle */
10806 }
10807
10808 if (nest == 0)
10809 {
10810 /* Found the match: return matchcount or line number. */
10811 if (flags & SP_RETCOUNT)
10812 ++retval;
10813 else
10814 retval = pos.lnum;
10815 if (flags & SP_SETPCMARK)
10816 setpcmark();
10817 curwin->w_cursor = pos;
10818 if (!(flags & SP_REPEAT))
10819 break;
10820 nest = 1; /* search for next unmatched */
10821 }
10822 }
10823
10824 if (match_pos != NULL)
10825 {
10826 /* Store the match cursor position */
10827 match_pos->lnum = curwin->w_cursor.lnum;
10828 match_pos->col = curwin->w_cursor.col + 1;
10829 }
10830
10831 /* If 'n' flag is used or search failed: restore cursor position. */
10832 if ((flags & SP_NOMOVE) || retval == 0)
10833 curwin->w_cursor = save_cursor;
10834
10835theend:
10836 vim_free(pat2);
10837 vim_free(pat3);
10838 if (p_cpo == empty_option)
10839 p_cpo = save_cpo;
10840 else
10841 /* Darn, evaluating the {skip} expression changed the value. */
10842 free_string_option(save_cpo);
10843
10844 return retval;
10845}
10846
10847/*
10848 * "searchpos()" function
10849 */
10850 static void
10851f_searchpos(typval_T *argvars, typval_T *rettv)
10852{
10853 pos_T match_pos;
10854 int lnum = 0;
10855 int col = 0;
10856 int n;
10857 int flags = 0;
10858
10859 if (rettv_list_alloc(rettv) == FAIL)
10860 return;
10861
10862 n = search_cmn(argvars, &match_pos, &flags);
10863 if (n > 0)
10864 {
10865 lnum = match_pos.lnum;
10866 col = match_pos.col;
10867 }
10868
10869 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10870 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10871 if (flags & SP_SUBPAT)
10872 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10873}
10874
10875 static void
10876f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10877{
10878#ifdef FEAT_CLIENTSERVER
10879 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010880 char_u *server = tv_get_string_chk(&argvars[0]);
10881 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010882
10883 rettv->vval.v_number = -1;
10884 if (server == NULL || reply == NULL)
10885 return;
10886 if (check_restricted() || check_secure())
10887 return;
10888# ifdef FEAT_X11
10889 if (check_connection() == FAIL)
10890 return;
10891# endif
10892
10893 if (serverSendReply(server, reply) < 0)
10894 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010895 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010896 return;
10897 }
10898 rettv->vval.v_number = 0;
10899#else
10900 rettv->vval.v_number = -1;
10901#endif
10902}
10903
10904 static void
10905f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10906{
10907 char_u *r = NULL;
10908
10909#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010910# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010911 r = serverGetVimNames();
10912# else
10913 make_connection();
10914 if (X_DISPLAY != NULL)
10915 r = serverGetVimNames(X_DISPLAY);
10916# endif
10917#endif
10918 rettv->v_type = VAR_STRING;
10919 rettv->vval.v_string = r;
10920}
10921
10922/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010923 * "setbufline()" function
10924 */
10925 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010926f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010927{
10928 linenr_T lnum;
10929 buf_T *buf;
10930
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010931 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010932 if (buf == NULL)
10933 rettv->vval.v_number = 1; /* FAIL */
10934 else
10935 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010936 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010937 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010938 }
10939}
10940
10941/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010942 * "setbufvar()" function
10943 */
10944 static void
10945f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10946{
10947 buf_T *buf;
10948 char_u *varname, *bufvarname;
10949 typval_T *varp;
10950 char_u nbuf[NUMBUFLEN];
10951
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010952 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010953 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010954 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10955 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010956 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010957 varp = &argvars[2];
10958
10959 if (buf != NULL && varname != NULL && varp != NULL)
10960 {
10961 if (*varname == '&')
10962 {
10963 long numval;
10964 char_u *strval;
10965 int error = FALSE;
10966 aco_save_T aco;
10967
10968 /* set curbuf to be our buf, temporarily */
10969 aucmd_prepbuf(&aco, buf);
10970
10971 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010972 numval = (long)tv_get_number_chk(varp, &error);
10973 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010974 if (!error && strval != NULL)
10975 set_option_value(varname, numval, strval, OPT_LOCAL);
10976
10977 /* reset notion of buffer */
10978 aucmd_restbuf(&aco);
10979 }
10980 else
10981 {
10982 buf_T *save_curbuf = curbuf;
10983
Bram Moolenaar964b3742019-05-24 18:54:09 +020010984 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010985 if (bufvarname != NULL)
10986 {
10987 curbuf = buf;
10988 STRCPY(bufvarname, "b:");
10989 STRCPY(bufvarname + 2, varname);
10990 set_var(bufvarname, varp, TRUE);
10991 vim_free(bufvarname);
10992 curbuf = save_curbuf;
10993 }
10994 }
10995 }
10996}
10997
10998 static void
10999f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11000{
11001 dict_T *d;
11002 dictitem_T *di;
11003 char_u *csearch;
11004
11005 if (argvars[0].v_type != VAR_DICT)
11006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011007 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011008 return;
11009 }
11010
11011 if ((d = argvars[0].vval.v_dict) != NULL)
11012 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011013 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011014 if (csearch != NULL)
11015 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011016 if (enc_utf8)
11017 {
11018 int pcc[MAX_MCO];
11019 int c = utfc_ptr2char(csearch, pcc);
11020
11021 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11022 }
11023 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011024 set_last_csearch(PTR2CHAR(csearch),
11025 csearch, MB_PTR2LEN(csearch));
11026 }
11027
11028 di = dict_find(d, (char_u *)"forward", -1);
11029 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011030 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011031 ? FORWARD : BACKWARD);
11032
11033 di = dict_find(d, (char_u *)"until", -1);
11034 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011035 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011036 }
11037}
11038
11039/*
11040 * "setcmdpos()" function
11041 */
11042 static void
11043f_setcmdpos(typval_T *argvars, typval_T *rettv)
11044{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011045 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011046
11047 if (pos >= 0)
11048 rettv->vval.v_number = set_cmdline_pos(pos);
11049}
11050
11051/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011052 * "setenv()" function
11053 */
11054 static void
11055f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11056{
11057 char_u namebuf[NUMBUFLEN];
11058 char_u valbuf[NUMBUFLEN];
11059 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11060
11061 if (argvars[1].v_type == VAR_SPECIAL
11062 && argvars[1].vval.v_number == VVAL_NULL)
11063 vim_unsetenv(name);
11064 else
11065 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11066}
11067
11068/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011069 * "setfperm({fname}, {mode})" function
11070 */
11071 static void
11072f_setfperm(typval_T *argvars, typval_T *rettv)
11073{
11074 char_u *fname;
11075 char_u modebuf[NUMBUFLEN];
11076 char_u *mode_str;
11077 int i;
11078 int mask;
11079 int mode = 0;
11080
11081 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011082 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011083 if (fname == NULL)
11084 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011085 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011086 if (mode_str == NULL)
11087 return;
11088 if (STRLEN(mode_str) != 9)
11089 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011090 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011091 return;
11092 }
11093
11094 mask = 1;
11095 for (i = 8; i >= 0; --i)
11096 {
11097 if (mode_str[i] != '-')
11098 mode |= mask;
11099 mask = mask << 1;
11100 }
11101 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11102}
11103
11104/*
11105 * "setline()" function
11106 */
11107 static void
11108f_setline(typval_T *argvars, typval_T *rettv)
11109{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011110 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011111
Bram Moolenaarca851592018-06-06 21:04:07 +020011112 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011113}
11114
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011115/*
11116 * Used by "setqflist()" and "setloclist()" functions
11117 */
11118 static void
11119set_qf_ll_list(
11120 win_T *wp UNUSED,
11121 typval_T *list_arg UNUSED,
11122 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011123 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011124 typval_T *rettv)
11125{
11126#ifdef FEAT_QUICKFIX
11127 static char *e_invact = N_("E927: Invalid action: '%s'");
11128 char_u *act;
11129 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011130 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011131#endif
11132
11133 rettv->vval.v_number = -1;
11134
11135#ifdef FEAT_QUICKFIX
11136 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011137 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011138 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011139 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011140 else
11141 {
11142 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011143 dict_T *d = NULL;
11144 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011145
11146 if (action_arg->v_type == VAR_STRING)
11147 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011148 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011149 if (act == NULL)
11150 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011151 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11152 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011153 action = *act;
11154 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011155 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011156 }
11157 else if (action_arg->v_type == VAR_UNKNOWN)
11158 action = ' ';
11159 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011160 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011161
Bram Moolenaard823fa92016-08-12 16:29:27 +020011162 if (action_arg->v_type != VAR_UNKNOWN
11163 && what_arg->v_type != VAR_UNKNOWN)
11164 {
11165 if (what_arg->v_type == VAR_DICT)
11166 d = what_arg->vval.v_dict;
11167 else
11168 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011169 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011170 valid_dict = FALSE;
11171 }
11172 }
11173
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011174 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011175 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011176 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11177 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011178 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011179 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011180 }
11181#endif
11182}
11183
11184/*
11185 * "setloclist()" function
11186 */
11187 static void
11188f_setloclist(typval_T *argvars, typval_T *rettv)
11189{
11190 win_T *win;
11191
11192 rettv->vval.v_number = -1;
11193
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011194 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011195 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011196 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011197}
11198
11199/*
11200 * "setmatches()" function
11201 */
11202 static void
11203f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11204{
11205#ifdef FEAT_SEARCH_EXTRA
11206 list_T *l;
11207 listitem_T *li;
11208 dict_T *d;
11209 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011210 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011211
11212 rettv->vval.v_number = -1;
11213 if (argvars[0].v_type != VAR_LIST)
11214 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011215 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011216 return;
11217 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011218 if (win == NULL)
11219 return;
11220
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011221 if ((l = argvars[0].vval.v_list) != NULL)
11222 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011223 /* To some extent make sure that we are dealing with a list from
11224 * "getmatches()". */
11225 li = l->lv_first;
11226 while (li != NULL)
11227 {
11228 if (li->li_tv.v_type != VAR_DICT
11229 || (d = li->li_tv.vval.v_dict) == NULL)
11230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011231 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011232 return;
11233 }
11234 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11235 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11236 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11237 && dict_find(d, (char_u *)"priority", -1) != NULL
11238 && dict_find(d, (char_u *)"id", -1) != NULL))
11239 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011240 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011241 return;
11242 }
11243 li = li->li_next;
11244 }
11245
Bram Moolenaaraff74912019-03-30 18:11:49 +010011246 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011247 li = l->lv_first;
11248 while (li != NULL)
11249 {
11250 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011251 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011252 dictitem_T *di;
11253 char_u *group;
11254 int priority;
11255 int id;
11256 char_u *conceal;
11257
11258 d = li->li_tv.vval.v_dict;
11259 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11260 {
11261 if (s == NULL)
11262 {
11263 s = list_alloc();
11264 if (s == NULL)
11265 return;
11266 }
11267
11268 /* match from matchaddpos() */
11269 for (i = 1; i < 9; i++)
11270 {
11271 sprintf((char *)buf, (char *)"pos%d", i);
11272 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11273 {
11274 if (di->di_tv.v_type != VAR_LIST)
11275 return;
11276
11277 list_append_tv(s, &di->di_tv);
11278 s->lv_refcount++;
11279 }
11280 else
11281 break;
11282 }
11283 }
11284
Bram Moolenaar8f667172018-12-14 15:38:31 +010011285 group = dict_get_string(d, (char_u *)"group", TRUE);
11286 priority = (int)dict_get_number(d, (char_u *)"priority");
11287 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011288 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011289 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011290 : NULL;
11291 if (i == 0)
11292 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011293 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011294 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011295 priority, id, NULL, conceal);
11296 }
11297 else
11298 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011299 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011300 list_unref(s);
11301 s = NULL;
11302 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011303 vim_free(group);
11304 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011305
11306 li = li->li_next;
11307 }
11308 rettv->vval.v_number = 0;
11309 }
11310#endif
11311}
11312
11313/*
11314 * "setpos()" function
11315 */
11316 static void
11317f_setpos(typval_T *argvars, typval_T *rettv)
11318{
11319 pos_T pos;
11320 int fnum;
11321 char_u *name;
11322 colnr_T curswant = -1;
11323
11324 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011325 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011326 if (name != NULL)
11327 {
11328 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11329 {
11330 if (--pos.col < 0)
11331 pos.col = 0;
11332 if (name[0] == '.' && name[1] == NUL)
11333 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011334 /* set cursor; "fnum" is ignored */
11335 curwin->w_cursor = pos;
11336 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011337 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011338 curwin->w_curswant = curswant - 1;
11339 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011340 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011341 check_cursor();
11342 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011343 }
11344 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11345 {
11346 /* set mark */
11347 if (setmark_pos(name[1], &pos, fnum) == OK)
11348 rettv->vval.v_number = 0;
11349 }
11350 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011351 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011352 }
11353 }
11354}
11355
11356/*
11357 * "setqflist()" function
11358 */
11359 static void
11360f_setqflist(typval_T *argvars, typval_T *rettv)
11361{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011362 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011363}
11364
11365/*
11366 * "setreg()" function
11367 */
11368 static void
11369f_setreg(typval_T *argvars, typval_T *rettv)
11370{
11371 int regname;
11372 char_u *strregname;
11373 char_u *stropt;
11374 char_u *strval;
11375 int append;
11376 char_u yank_type;
11377 long block_len;
11378
11379 block_len = -1;
11380 yank_type = MAUTO;
11381 append = FALSE;
11382
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011383 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011384 rettv->vval.v_number = 1; /* FAIL is default */
11385
11386 if (strregname == NULL)
11387 return; /* type error; errmsg already given */
11388 regname = *strregname;
11389 if (regname == 0 || regname == '@')
11390 regname = '"';
11391
11392 if (argvars[2].v_type != VAR_UNKNOWN)
11393 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011394 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011395 if (stropt == NULL)
11396 return; /* type error */
11397 for (; *stropt != NUL; ++stropt)
11398 switch (*stropt)
11399 {
11400 case 'a': case 'A': /* append */
11401 append = TRUE;
11402 break;
11403 case 'v': case 'c': /* character-wise selection */
11404 yank_type = MCHAR;
11405 break;
11406 case 'V': case 'l': /* line-wise selection */
11407 yank_type = MLINE;
11408 break;
11409 case 'b': case Ctrl_V: /* block-wise selection */
11410 yank_type = MBLOCK;
11411 if (VIM_ISDIGIT(stropt[1]))
11412 {
11413 ++stropt;
11414 block_len = getdigits(&stropt) - 1;
11415 --stropt;
11416 }
11417 break;
11418 }
11419 }
11420
11421 if (argvars[1].v_type == VAR_LIST)
11422 {
11423 char_u **lstval;
11424 char_u **allocval;
11425 char_u buf[NUMBUFLEN];
11426 char_u **curval;
11427 char_u **curallocval;
11428 list_T *ll = argvars[1].vval.v_list;
11429 listitem_T *li;
11430 int len;
11431
11432 /* If the list is NULL handle like an empty list. */
11433 len = ll == NULL ? 0 : ll->lv_len;
11434
11435 /* First half: use for pointers to result lines; second half: use for
11436 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011437 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011438 if (lstval == NULL)
11439 return;
11440 curval = lstval;
11441 allocval = lstval + len + 2;
11442 curallocval = allocval;
11443
11444 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11445 li = li->li_next)
11446 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011447 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011448 if (strval == NULL)
11449 goto free_lstval;
11450 if (strval == buf)
11451 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011452 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011453 * overwrite the string. */
11454 strval = vim_strsave(buf);
11455 if (strval == NULL)
11456 goto free_lstval;
11457 *curallocval++ = strval;
11458 }
11459 *curval++ = strval;
11460 }
11461 *curval++ = NULL;
11462
11463 write_reg_contents_lst(regname, lstval, -1,
11464 append, yank_type, block_len);
11465free_lstval:
11466 while (curallocval > allocval)
11467 vim_free(*--curallocval);
11468 vim_free(lstval);
11469 }
11470 else
11471 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011472 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011473 if (strval == NULL)
11474 return;
11475 write_reg_contents_ex(regname, strval, -1,
11476 append, yank_type, block_len);
11477 }
11478 rettv->vval.v_number = 0;
11479}
11480
11481/*
11482 * "settabvar()" function
11483 */
11484 static void
11485f_settabvar(typval_T *argvars, typval_T *rettv)
11486{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011487 tabpage_T *save_curtab;
11488 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011489 char_u *varname, *tabvarname;
11490 typval_T *varp;
11491
11492 rettv->vval.v_number = 0;
11493
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011494 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011495 return;
11496
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011497 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11498 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011499 varp = &argvars[2];
11500
Bram Moolenaar4033c552017-09-16 20:54:51 +020011501 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011502 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011503 save_curtab = curtab;
11504 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011505
Bram Moolenaar964b3742019-05-24 18:54:09 +020011506 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011507 if (tabvarname != NULL)
11508 {
11509 STRCPY(tabvarname, "t:");
11510 STRCPY(tabvarname + 2, varname);
11511 set_var(tabvarname, varp, TRUE);
11512 vim_free(tabvarname);
11513 }
11514
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011515 /* Restore current tabpage */
11516 if (valid_tabpage(save_curtab))
11517 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011518 }
11519}
11520
11521/*
11522 * "settabwinvar()" function
11523 */
11524 static void
11525f_settabwinvar(typval_T *argvars, typval_T *rettv)
11526{
11527 setwinvar(argvars, rettv, 1);
11528}
11529
11530/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011531 * "settagstack()" function
11532 */
11533 static void
11534f_settagstack(typval_T *argvars, typval_T *rettv)
11535{
11536 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11537 win_T *wp;
11538 dict_T *d;
11539 int action = 'r';
11540
11541 rettv->vval.v_number = -1;
11542
11543 // first argument: window number or id
11544 wp = find_win_by_nr_or_id(&argvars[0]);
11545 if (wp == NULL)
11546 return;
11547
11548 // second argument: dict with items to set in the tag stack
11549 if (argvars[1].v_type != VAR_DICT)
11550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011551 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011552 return;
11553 }
11554 d = argvars[1].vval.v_dict;
11555 if (d == NULL)
11556 return;
11557
11558 // third argument: action - 'a' for append and 'r' for replace.
11559 // default is to replace the stack.
11560 if (argvars[2].v_type == VAR_UNKNOWN)
11561 action = 'r';
11562 else if (argvars[2].v_type == VAR_STRING)
11563 {
11564 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011565 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011566 if (actstr == NULL)
11567 return;
11568 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11569 action = *actstr;
11570 else
11571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011572 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011573 return;
11574 }
11575 }
11576 else
11577 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011578 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011579 return;
11580 }
11581
11582 if (set_tagstack(wp, d, action) == OK)
11583 rettv->vval.v_number = 0;
11584}
11585
11586/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011587 * "setwinvar()" function
11588 */
11589 static void
11590f_setwinvar(typval_T *argvars, typval_T *rettv)
11591{
11592 setwinvar(argvars, rettv, 0);
11593}
11594
11595#ifdef FEAT_CRYPT
11596/*
11597 * "sha256({string})" function
11598 */
11599 static void
11600f_sha256(typval_T *argvars, typval_T *rettv)
11601{
11602 char_u *p;
11603
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011604 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011605 rettv->vval.v_string = vim_strsave(
11606 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11607 rettv->v_type = VAR_STRING;
11608}
11609#endif /* FEAT_CRYPT */
11610
11611/*
11612 * "shellescape({string})" function
11613 */
11614 static void
11615f_shellescape(typval_T *argvars, typval_T *rettv)
11616{
Bram Moolenaar20615522017-06-05 18:46:26 +020011617 int do_special = non_zero_arg(&argvars[1]);
11618
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011619 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011620 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011621 rettv->v_type = VAR_STRING;
11622}
11623
11624/*
11625 * shiftwidth() function
11626 */
11627 static void
11628f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11629{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011630 rettv->vval.v_number = 0;
11631
11632 if (argvars[0].v_type != VAR_UNKNOWN)
11633 {
11634 long col;
11635
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011636 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011637 if (col < 0)
11638 return; // type error; errmsg already given
11639#ifdef FEAT_VARTABS
11640 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11641 return;
11642#endif
11643 }
11644
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011645 rettv->vval.v_number = get_sw_value(curbuf);
11646}
11647
11648/*
11649 * "simplify()" function
11650 */
11651 static void
11652f_simplify(typval_T *argvars, typval_T *rettv)
11653{
11654 char_u *p;
11655
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011656 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011657 rettv->vval.v_string = vim_strsave(p);
11658 simplify_filename(rettv->vval.v_string); /* simplify in place */
11659 rettv->v_type = VAR_STRING;
11660}
11661
11662#ifdef FEAT_FLOAT
11663/*
11664 * "sin()" function
11665 */
11666 static void
11667f_sin(typval_T *argvars, typval_T *rettv)
11668{
11669 float_T f = 0.0;
11670
11671 rettv->v_type = VAR_FLOAT;
11672 if (get_float_arg(argvars, &f) == OK)
11673 rettv->vval.v_float = sin(f);
11674 else
11675 rettv->vval.v_float = 0.0;
11676}
11677
11678/*
11679 * "sinh()" function
11680 */
11681 static void
11682f_sinh(typval_T *argvars, typval_T *rettv)
11683{
11684 float_T f = 0.0;
11685
11686 rettv->v_type = VAR_FLOAT;
11687 if (get_float_arg(argvars, &f) == OK)
11688 rettv->vval.v_float = sinh(f);
11689 else
11690 rettv->vval.v_float = 0.0;
11691}
11692#endif
11693
Bram Moolenaareae1b912019-05-09 15:12:55 +020011694static int item_compare(const void *s1, const void *s2);
11695static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011696
11697/* struct used in the array that's given to qsort() */
11698typedef struct
11699{
11700 listitem_T *item;
11701 int idx;
11702} sortItem_T;
11703
11704/* struct storing information about current sort */
11705typedef struct
11706{
11707 int item_compare_ic;
11708 int item_compare_numeric;
11709 int item_compare_numbers;
11710#ifdef FEAT_FLOAT
11711 int item_compare_float;
11712#endif
11713 char_u *item_compare_func;
11714 partial_T *item_compare_partial;
11715 dict_T *item_compare_selfdict;
11716 int item_compare_func_err;
11717 int item_compare_keep_zero;
11718} sortinfo_T;
11719static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011720#define ITEM_COMPARE_FAIL 999
11721
11722/*
11723 * Compare functions for f_sort() and f_uniq() below.
11724 */
11725 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011726item_compare(const void *s1, const void *s2)
11727{
11728 sortItem_T *si1, *si2;
11729 typval_T *tv1, *tv2;
11730 char_u *p1, *p2;
11731 char_u *tofree1 = NULL, *tofree2 = NULL;
11732 int res;
11733 char_u numbuf1[NUMBUFLEN];
11734 char_u numbuf2[NUMBUFLEN];
11735
11736 si1 = (sortItem_T *)s1;
11737 si2 = (sortItem_T *)s2;
11738 tv1 = &si1->item->li_tv;
11739 tv2 = &si2->item->li_tv;
11740
11741 if (sortinfo->item_compare_numbers)
11742 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011743 varnumber_T v1 = tv_get_number(tv1);
11744 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011745
11746 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11747 }
11748
11749#ifdef FEAT_FLOAT
11750 if (sortinfo->item_compare_float)
11751 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011752 float_T v1 = tv_get_float(tv1);
11753 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011754
11755 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11756 }
11757#endif
11758
11759 /* tv2string() puts quotes around a string and allocates memory. Don't do
11760 * that for string variables. Use a single quote when comparing with a
11761 * non-string to do what the docs promise. */
11762 if (tv1->v_type == VAR_STRING)
11763 {
11764 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11765 p1 = (char_u *)"'";
11766 else
11767 p1 = tv1->vval.v_string;
11768 }
11769 else
11770 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11771 if (tv2->v_type == VAR_STRING)
11772 {
11773 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11774 p2 = (char_u *)"'";
11775 else
11776 p2 = tv2->vval.v_string;
11777 }
11778 else
11779 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11780 if (p1 == NULL)
11781 p1 = (char_u *)"";
11782 if (p2 == NULL)
11783 p2 = (char_u *)"";
11784 if (!sortinfo->item_compare_numeric)
11785 {
11786 if (sortinfo->item_compare_ic)
11787 res = STRICMP(p1, p2);
11788 else
11789 res = STRCMP(p1, p2);
11790 }
11791 else
11792 {
11793 double n1, n2;
11794 n1 = strtod((char *)p1, (char **)&p1);
11795 n2 = strtod((char *)p2, (char **)&p2);
11796 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11797 }
11798
11799 /* When the result would be zero, compare the item indexes. Makes the
11800 * sort stable. */
11801 if (res == 0 && !sortinfo->item_compare_keep_zero)
11802 res = si1->idx > si2->idx ? 1 : -1;
11803
11804 vim_free(tofree1);
11805 vim_free(tofree2);
11806 return res;
11807}
11808
11809 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011810item_compare2(const void *s1, const void *s2)
11811{
11812 sortItem_T *si1, *si2;
11813 int res;
11814 typval_T rettv;
11815 typval_T argv[3];
11816 int dummy;
11817 char_u *func_name;
11818 partial_T *partial = sortinfo->item_compare_partial;
11819
11820 /* shortcut after failure in previous call; compare all items equal */
11821 if (sortinfo->item_compare_func_err)
11822 return 0;
11823
11824 si1 = (sortItem_T *)s1;
11825 si2 = (sortItem_T *)s2;
11826
11827 if (partial == NULL)
11828 func_name = sortinfo->item_compare_func;
11829 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011830 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011831
11832 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11833 * in the copy without changing the original list items. */
11834 copy_tv(&si1->item->li_tv, &argv[0]);
11835 copy_tv(&si2->item->li_tv, &argv[1]);
11836
11837 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020011838 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011839 partial, sortinfo->item_compare_selfdict);
11840 clear_tv(&argv[0]);
11841 clear_tv(&argv[1]);
11842
11843 if (res == FAIL)
11844 res = ITEM_COMPARE_FAIL;
11845 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011846 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011847 if (sortinfo->item_compare_func_err)
11848 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11849 clear_tv(&rettv);
11850
11851 /* When the result would be zero, compare the pointers themselves. Makes
11852 * the sort stable. */
11853 if (res == 0 && !sortinfo->item_compare_keep_zero)
11854 res = si1->idx > si2->idx ? 1 : -1;
11855
11856 return res;
11857}
11858
11859/*
11860 * "sort({list})" function
11861 */
11862 static void
11863do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11864{
11865 list_T *l;
11866 listitem_T *li;
11867 sortItem_T *ptrs;
11868 sortinfo_T *old_sortinfo;
11869 sortinfo_T info;
11870 long len;
11871 long i;
11872
11873 /* Pointer to current info struct used in compare function. Save and
11874 * restore the current one for nested calls. */
11875 old_sortinfo = sortinfo;
11876 sortinfo = &info;
11877
11878 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011879 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011880 else
11881 {
11882 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011883 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011884 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11885 TRUE))
11886 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011887 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011888
11889 len = list_len(l);
11890 if (len <= 1)
11891 goto theend; /* short list sorts pretty quickly */
11892
11893 info.item_compare_ic = FALSE;
11894 info.item_compare_numeric = FALSE;
11895 info.item_compare_numbers = FALSE;
11896#ifdef FEAT_FLOAT
11897 info.item_compare_float = FALSE;
11898#endif
11899 info.item_compare_func = NULL;
11900 info.item_compare_partial = NULL;
11901 info.item_compare_selfdict = NULL;
11902 if (argvars[1].v_type != VAR_UNKNOWN)
11903 {
11904 /* optional second argument: {func} */
11905 if (argvars[1].v_type == VAR_FUNC)
11906 info.item_compare_func = argvars[1].vval.v_string;
11907 else if (argvars[1].v_type == VAR_PARTIAL)
11908 info.item_compare_partial = argvars[1].vval.v_partial;
11909 else
11910 {
11911 int error = FALSE;
11912
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011913 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011914 if (error)
11915 goto theend; /* type error; errmsg already given */
11916 if (i == 1)
11917 info.item_compare_ic = TRUE;
11918 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011919 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011920 else if (i != 0)
11921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011922 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011923 goto theend;
11924 }
11925 if (info.item_compare_func != NULL)
11926 {
11927 if (*info.item_compare_func == NUL)
11928 {
11929 /* empty string means default sort */
11930 info.item_compare_func = NULL;
11931 }
11932 else if (STRCMP(info.item_compare_func, "n") == 0)
11933 {
11934 info.item_compare_func = NULL;
11935 info.item_compare_numeric = TRUE;
11936 }
11937 else if (STRCMP(info.item_compare_func, "N") == 0)
11938 {
11939 info.item_compare_func = NULL;
11940 info.item_compare_numbers = TRUE;
11941 }
11942#ifdef FEAT_FLOAT
11943 else if (STRCMP(info.item_compare_func, "f") == 0)
11944 {
11945 info.item_compare_func = NULL;
11946 info.item_compare_float = TRUE;
11947 }
11948#endif
11949 else if (STRCMP(info.item_compare_func, "i") == 0)
11950 {
11951 info.item_compare_func = NULL;
11952 info.item_compare_ic = TRUE;
11953 }
11954 }
11955 }
11956
11957 if (argvars[2].v_type != VAR_UNKNOWN)
11958 {
11959 /* optional third argument: {dict} */
11960 if (argvars[2].v_type != VAR_DICT)
11961 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011962 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011963 goto theend;
11964 }
11965 info.item_compare_selfdict = argvars[2].vval.v_dict;
11966 }
11967 }
11968
11969 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011970 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011971 if (ptrs == NULL)
11972 goto theend;
11973
11974 i = 0;
11975 if (sort)
11976 {
11977 /* sort(): ptrs will be the list to sort */
11978 for (li = l->lv_first; li != NULL; li = li->li_next)
11979 {
11980 ptrs[i].item = li;
11981 ptrs[i].idx = i;
11982 ++i;
11983 }
11984
11985 info.item_compare_func_err = FALSE;
11986 info.item_compare_keep_zero = FALSE;
11987 /* test the compare function */
11988 if ((info.item_compare_func != NULL
11989 || info.item_compare_partial != NULL)
11990 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11991 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011992 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011993 else
11994 {
11995 /* Sort the array with item pointers. */
11996 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11997 info.item_compare_func == NULL
11998 && info.item_compare_partial == NULL
11999 ? item_compare : item_compare2);
12000
12001 if (!info.item_compare_func_err)
12002 {
12003 /* Clear the List and append the items in sorted order. */
12004 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12005 l->lv_len = 0;
12006 for (i = 0; i < len; ++i)
12007 list_append(l, ptrs[i].item);
12008 }
12009 }
12010 }
12011 else
12012 {
12013 int (*item_compare_func_ptr)(const void *, const void *);
12014
12015 /* f_uniq(): ptrs will be a stack of items to remove */
12016 info.item_compare_func_err = FALSE;
12017 info.item_compare_keep_zero = TRUE;
12018 item_compare_func_ptr = info.item_compare_func != NULL
12019 || info.item_compare_partial != NULL
12020 ? item_compare2 : item_compare;
12021
12022 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12023 li = li->li_next)
12024 {
12025 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12026 == 0)
12027 ptrs[i++].item = li;
12028 if (info.item_compare_func_err)
12029 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012030 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012031 break;
12032 }
12033 }
12034
12035 if (!info.item_compare_func_err)
12036 {
12037 while (--i >= 0)
12038 {
12039 li = ptrs[i].item->li_next;
12040 ptrs[i].item->li_next = li->li_next;
12041 if (li->li_next != NULL)
12042 li->li_next->li_prev = ptrs[i].item;
12043 else
12044 l->lv_last = ptrs[i].item;
12045 list_fix_watch(l, li);
12046 listitem_free(li);
12047 l->lv_len--;
12048 }
12049 }
12050 }
12051
12052 vim_free(ptrs);
12053 }
12054theend:
12055 sortinfo = old_sortinfo;
12056}
12057
12058/*
12059 * "sort({list})" function
12060 */
12061 static void
12062f_sort(typval_T *argvars, typval_T *rettv)
12063{
12064 do_sort_uniq(argvars, rettv, TRUE);
12065}
12066
12067/*
12068 * "uniq({list})" function
12069 */
12070 static void
12071f_uniq(typval_T *argvars, typval_T *rettv)
12072{
12073 do_sort_uniq(argvars, rettv, FALSE);
12074}
12075
12076/*
12077 * "soundfold({word})" function
12078 */
12079 static void
12080f_soundfold(typval_T *argvars, typval_T *rettv)
12081{
12082 char_u *s;
12083
12084 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012085 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012086#ifdef FEAT_SPELL
12087 rettv->vval.v_string = eval_soundfold(s);
12088#else
12089 rettv->vval.v_string = vim_strsave(s);
12090#endif
12091}
12092
12093/*
12094 * "spellbadword()" function
12095 */
12096 static void
12097f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12098{
12099 char_u *word = (char_u *)"";
12100 hlf_T attr = HLF_COUNT;
12101 int len = 0;
12102
12103 if (rettv_list_alloc(rettv) == FAIL)
12104 return;
12105
12106#ifdef FEAT_SPELL
12107 if (argvars[0].v_type == VAR_UNKNOWN)
12108 {
12109 /* Find the start and length of the badly spelled word. */
12110 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12111 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012112 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012113 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012114 curwin->w_set_curswant = TRUE;
12115 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012116 }
12117 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12118 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012119 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012120 int capcol = -1;
12121
12122 if (str != NULL)
12123 {
12124 /* Check the argument for spelling. */
12125 while (*str != NUL)
12126 {
12127 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12128 if (attr != HLF_COUNT)
12129 {
12130 word = str;
12131 break;
12132 }
12133 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012134 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012135 }
12136 }
12137 }
12138#endif
12139
12140 list_append_string(rettv->vval.v_list, word, len);
12141 list_append_string(rettv->vval.v_list, (char_u *)(
12142 attr == HLF_SPB ? "bad" :
12143 attr == HLF_SPR ? "rare" :
12144 attr == HLF_SPL ? "local" :
12145 attr == HLF_SPC ? "caps" :
12146 ""), -1);
12147}
12148
12149/*
12150 * "spellsuggest()" function
12151 */
12152 static void
12153f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12154{
12155#ifdef FEAT_SPELL
12156 char_u *str;
12157 int typeerr = FALSE;
12158 int maxcount;
12159 garray_T ga;
12160 int i;
12161 listitem_T *li;
12162 int need_capital = FALSE;
12163#endif
12164
12165 if (rettv_list_alloc(rettv) == FAIL)
12166 return;
12167
12168#ifdef FEAT_SPELL
12169 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12170 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012171 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012172 if (argvars[1].v_type != VAR_UNKNOWN)
12173 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012174 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012175 if (maxcount <= 0)
12176 return;
12177 if (argvars[2].v_type != VAR_UNKNOWN)
12178 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012179 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012180 if (typeerr)
12181 return;
12182 }
12183 }
12184 else
12185 maxcount = 25;
12186
12187 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12188
12189 for (i = 0; i < ga.ga_len; ++i)
12190 {
12191 str = ((char_u **)ga.ga_data)[i];
12192
12193 li = listitem_alloc();
12194 if (li == NULL)
12195 vim_free(str);
12196 else
12197 {
12198 li->li_tv.v_type = VAR_STRING;
12199 li->li_tv.v_lock = 0;
12200 li->li_tv.vval.v_string = str;
12201 list_append(rettv->vval.v_list, li);
12202 }
12203 }
12204 ga_clear(&ga);
12205 }
12206#endif
12207}
12208
12209 static void
12210f_split(typval_T *argvars, typval_T *rettv)
12211{
12212 char_u *str;
12213 char_u *end;
12214 char_u *pat = NULL;
12215 regmatch_T regmatch;
12216 char_u patbuf[NUMBUFLEN];
12217 char_u *save_cpo;
12218 int match;
12219 colnr_T col = 0;
12220 int keepempty = FALSE;
12221 int typeerr = FALSE;
12222
12223 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12224 save_cpo = p_cpo;
12225 p_cpo = (char_u *)"";
12226
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012227 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012228 if (argvars[1].v_type != VAR_UNKNOWN)
12229 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012230 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012231 if (pat == NULL)
12232 typeerr = TRUE;
12233 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012234 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012235 }
12236 if (pat == NULL || *pat == NUL)
12237 pat = (char_u *)"[\\x01- ]\\+";
12238
12239 if (rettv_list_alloc(rettv) == FAIL)
12240 return;
12241 if (typeerr)
12242 return;
12243
12244 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12245 if (regmatch.regprog != NULL)
12246 {
12247 regmatch.rm_ic = FALSE;
12248 while (*str != NUL || keepempty)
12249 {
12250 if (*str == NUL)
12251 match = FALSE; /* empty item at the end */
12252 else
12253 match = vim_regexec_nl(&regmatch, str, col);
12254 if (match)
12255 end = regmatch.startp[0];
12256 else
12257 end = str + STRLEN(str);
12258 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12259 && *str != NUL && match && end < regmatch.endp[0]))
12260 {
12261 if (list_append_string(rettv->vval.v_list, str,
12262 (int)(end - str)) == FAIL)
12263 break;
12264 }
12265 if (!match)
12266 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012267 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012268 if (regmatch.endp[0] > str)
12269 col = 0;
12270 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012271 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012272 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012273 str = regmatch.endp[0];
12274 }
12275
12276 vim_regfree(regmatch.regprog);
12277 }
12278
12279 p_cpo = save_cpo;
12280}
12281
12282#ifdef FEAT_FLOAT
12283/*
12284 * "sqrt()" function
12285 */
12286 static void
12287f_sqrt(typval_T *argvars, typval_T *rettv)
12288{
12289 float_T f = 0.0;
12290
12291 rettv->v_type = VAR_FLOAT;
12292 if (get_float_arg(argvars, &f) == OK)
12293 rettv->vval.v_float = sqrt(f);
12294 else
12295 rettv->vval.v_float = 0.0;
12296}
12297
12298/*
12299 * "str2float()" function
12300 */
12301 static void
12302f_str2float(typval_T *argvars, typval_T *rettv)
12303{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012304 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012305 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012306
Bram Moolenaar08243d22017-01-10 16:12:29 +010012307 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012308 p = skipwhite(p + 1);
12309 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012310 if (isneg)
12311 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012312 rettv->v_type = VAR_FLOAT;
12313}
12314#endif
12315
12316/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012317 * "str2list()" function
12318 */
12319 static void
12320f_str2list(typval_T *argvars, typval_T *rettv)
12321{
12322 char_u *p;
12323 int utf8 = FALSE;
12324
12325 if (rettv_list_alloc(rettv) == FAIL)
12326 return;
12327
12328 if (argvars[1].v_type != VAR_UNKNOWN)
12329 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12330
12331 p = tv_get_string(&argvars[0]);
12332
12333 if (has_mbyte || utf8)
12334 {
12335 int (*ptr2len)(char_u *);
12336 int (*ptr2char)(char_u *);
12337
12338 if (utf8 || enc_utf8)
12339 {
12340 ptr2len = utf_ptr2len;
12341 ptr2char = utf_ptr2char;
12342 }
12343 else
12344 {
12345 ptr2len = mb_ptr2len;
12346 ptr2char = mb_ptr2char;
12347 }
12348
12349 for ( ; *p != NUL; p += (*ptr2len)(p))
12350 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
12351 }
12352 else
12353 for ( ; *p != NUL; ++p)
12354 list_append_number(rettv->vval.v_list, *p);
12355}
12356
12357/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012358 * "str2nr()" function
12359 */
12360 static void
12361f_str2nr(typval_T *argvars, typval_T *rettv)
12362{
12363 int base = 10;
12364 char_u *p;
12365 varnumber_T n;
12366 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012367 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012368
12369 if (argvars[1].v_type != VAR_UNKNOWN)
12370 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012371 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012372 if (base != 2 && base != 8 && base != 10 && base != 16)
12373 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012374 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012375 return;
12376 }
12377 }
12378
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012379 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012380 isneg = (*p == '-');
12381 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012382 p = skipwhite(p + 1);
12383 switch (base)
12384 {
12385 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12386 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12387 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12388 default: what = 0;
12389 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020012390 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
12391 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010012392 if (isneg)
12393 rettv->vval.v_number = -n;
12394 else
12395 rettv->vval.v_number = n;
12396
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012397}
12398
12399#ifdef HAVE_STRFTIME
12400/*
12401 * "strftime({format}[, {time}])" function
12402 */
12403 static void
12404f_strftime(typval_T *argvars, typval_T *rettv)
12405{
12406 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020012407 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012408 struct tm *curtime;
12409 time_t seconds;
12410 char_u *p;
12411
12412 rettv->v_type = VAR_STRING;
12413
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012414 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012415 if (argvars[1].v_type == VAR_UNKNOWN)
12416 seconds = time(NULL);
12417 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012418 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020012419 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012420 /* MSVC returns NULL for an invalid value of seconds. */
12421 if (curtime == NULL)
12422 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12423 else
12424 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012425 vimconv_T conv;
12426 char_u *enc;
12427
12428 conv.vc_type = CONV_NONE;
12429 enc = enc_locale();
12430 convert_setup(&conv, p_enc, enc);
12431 if (conv.vc_type != CONV_NONE)
12432 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012433 if (p != NULL)
12434 (void)strftime((char *)result_buf, sizeof(result_buf),
12435 (char *)p, curtime);
12436 else
12437 result_buf[0] = NUL;
12438
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012439 if (conv.vc_type != CONV_NONE)
12440 vim_free(p);
12441 convert_setup(&conv, enc, p_enc);
12442 if (conv.vc_type != CONV_NONE)
12443 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12444 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012445 rettv->vval.v_string = vim_strsave(result_buf);
12446
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012447 /* Release conversion descriptors */
12448 convert_setup(&conv, NULL, NULL);
12449 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450 }
12451}
12452#endif
12453
12454/*
12455 * "strgetchar()" function
12456 */
12457 static void
12458f_strgetchar(typval_T *argvars, typval_T *rettv)
12459{
12460 char_u *str;
12461 int len;
12462 int error = FALSE;
12463 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012464 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012465
12466 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012467 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012468 if (str == NULL)
12469 return;
12470 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012471 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012472 if (error)
12473 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012474
Bram Moolenaar13505972019-01-24 15:04:48 +010012475 while (charidx >= 0 && byteidx < len)
12476 {
12477 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012478 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012479 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12480 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012481 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012482 --charidx;
12483 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012484 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012485}
12486
12487/*
12488 * "stridx()" function
12489 */
12490 static void
12491f_stridx(typval_T *argvars, typval_T *rettv)
12492{
12493 char_u buf[NUMBUFLEN];
12494 char_u *needle;
12495 char_u *haystack;
12496 char_u *save_haystack;
12497 char_u *pos;
12498 int start_idx;
12499
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012500 needle = tv_get_string_chk(&argvars[1]);
12501 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012502 rettv->vval.v_number = -1;
12503 if (needle == NULL || haystack == NULL)
12504 return; /* type error; errmsg already given */
12505
12506 if (argvars[2].v_type != VAR_UNKNOWN)
12507 {
12508 int error = FALSE;
12509
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012510 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012511 if (error || start_idx >= (int)STRLEN(haystack))
12512 return;
12513 if (start_idx >= 0)
12514 haystack += start_idx;
12515 }
12516
12517 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12518 if (pos != NULL)
12519 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12520}
12521
12522/*
12523 * "string()" function
12524 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012525 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012526f_string(typval_T *argvars, typval_T *rettv)
12527{
12528 char_u *tofree;
12529 char_u numbuf[NUMBUFLEN];
12530
12531 rettv->v_type = VAR_STRING;
12532 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12533 get_copyID());
12534 /* Make a copy if we have a value but it's not in allocated memory. */
12535 if (rettv->vval.v_string != NULL && tofree == NULL)
12536 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12537}
12538
12539/*
12540 * "strlen()" function
12541 */
12542 static void
12543f_strlen(typval_T *argvars, typval_T *rettv)
12544{
12545 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012546 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012547}
12548
12549/*
12550 * "strchars()" function
12551 */
12552 static void
12553f_strchars(typval_T *argvars, typval_T *rettv)
12554{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012555 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012556 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012557 varnumber_T len = 0;
12558 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012559
12560 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012561 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012562 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012563 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012564 else
12565 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012566 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12567 while (*s != NUL)
12568 {
12569 func_mb_ptr2char_adv(&s);
12570 ++len;
12571 }
12572 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012573 }
12574}
12575
12576/*
12577 * "strdisplaywidth()" function
12578 */
12579 static void
12580f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12581{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012582 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012583 int col = 0;
12584
12585 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012586 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012587
12588 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12589}
12590
12591/*
12592 * "strwidth()" function
12593 */
12594 static void
12595f_strwidth(typval_T *argvars, typval_T *rettv)
12596{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012597 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012598
Bram Moolenaar13505972019-01-24 15:04:48 +010012599 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012600}
12601
12602/*
12603 * "strcharpart()" function
12604 */
12605 static void
12606f_strcharpart(typval_T *argvars, typval_T *rettv)
12607{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012608 char_u *p;
12609 int nchar;
12610 int nbyte = 0;
12611 int charlen;
12612 int len = 0;
12613 int slen;
12614 int error = FALSE;
12615
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012616 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012617 slen = (int)STRLEN(p);
12618
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012619 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012620 if (!error)
12621 {
12622 if (nchar > 0)
12623 while (nchar > 0 && nbyte < slen)
12624 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012625 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012626 --nchar;
12627 }
12628 else
12629 nbyte = nchar;
12630 if (argvars[2].v_type != VAR_UNKNOWN)
12631 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012632 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012633 while (charlen > 0 && nbyte + len < slen)
12634 {
12635 int off = nbyte + len;
12636
12637 if (off < 0)
12638 len += 1;
12639 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012640 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012641 --charlen;
12642 }
12643 }
12644 else
12645 len = slen - nbyte; /* default: all bytes that are available. */
12646 }
12647
12648 /*
12649 * Only return the overlap between the specified part and the actual
12650 * string.
12651 */
12652 if (nbyte < 0)
12653 {
12654 len += nbyte;
12655 nbyte = 0;
12656 }
12657 else if (nbyte > slen)
12658 nbyte = slen;
12659 if (len < 0)
12660 len = 0;
12661 else if (nbyte + len > slen)
12662 len = slen - nbyte;
12663
12664 rettv->v_type = VAR_STRING;
12665 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012666}
12667
12668/*
12669 * "strpart()" function
12670 */
12671 static void
12672f_strpart(typval_T *argvars, typval_T *rettv)
12673{
12674 char_u *p;
12675 int n;
12676 int len;
12677 int slen;
12678 int error = FALSE;
12679
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012680 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012681 slen = (int)STRLEN(p);
12682
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012683 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012684 if (error)
12685 len = 0;
12686 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012687 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012688 else
12689 len = slen - n; /* default len: all bytes that are available. */
12690
12691 /*
12692 * Only return the overlap between the specified part and the actual
12693 * string.
12694 */
12695 if (n < 0)
12696 {
12697 len += n;
12698 n = 0;
12699 }
12700 else if (n > slen)
12701 n = slen;
12702 if (len < 0)
12703 len = 0;
12704 else if (n + len > slen)
12705 len = slen - n;
12706
12707 rettv->v_type = VAR_STRING;
12708 rettv->vval.v_string = vim_strnsave(p + n, len);
12709}
12710
12711/*
12712 * "strridx()" function
12713 */
12714 static void
12715f_strridx(typval_T *argvars, typval_T *rettv)
12716{
12717 char_u buf[NUMBUFLEN];
12718 char_u *needle;
12719 char_u *haystack;
12720 char_u *rest;
12721 char_u *lastmatch = NULL;
12722 int haystack_len, end_idx;
12723
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012724 needle = tv_get_string_chk(&argvars[1]);
12725 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012726
12727 rettv->vval.v_number = -1;
12728 if (needle == NULL || haystack == NULL)
12729 return; /* type error; errmsg already given */
12730
12731 haystack_len = (int)STRLEN(haystack);
12732 if (argvars[2].v_type != VAR_UNKNOWN)
12733 {
12734 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012735 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012736 if (end_idx < 0)
12737 return; /* can never find a match */
12738 }
12739 else
12740 end_idx = haystack_len;
12741
12742 if (*needle == NUL)
12743 {
12744 /* Empty string matches past the end. */
12745 lastmatch = haystack + end_idx;
12746 }
12747 else
12748 {
12749 for (rest = haystack; *rest != '\0'; ++rest)
12750 {
12751 rest = (char_u *)strstr((char *)rest, (char *)needle);
12752 if (rest == NULL || rest > haystack + end_idx)
12753 break;
12754 lastmatch = rest;
12755 }
12756 }
12757
12758 if (lastmatch == NULL)
12759 rettv->vval.v_number = -1;
12760 else
12761 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12762}
12763
12764/*
12765 * "strtrans()" function
12766 */
12767 static void
12768f_strtrans(typval_T *argvars, typval_T *rettv)
12769{
12770 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012771 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012772}
12773
12774/*
12775 * "submatch()" function
12776 */
12777 static void
12778f_submatch(typval_T *argvars, typval_T *rettv)
12779{
12780 int error = FALSE;
12781 int no;
12782 int retList = 0;
12783
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012784 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012785 if (error)
12786 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012787 if (no < 0 || no >= NSUBEXP)
12788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012789 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012790 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012791 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012792 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012793 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012794 if (error)
12795 return;
12796
12797 if (retList == 0)
12798 {
12799 rettv->v_type = VAR_STRING;
12800 rettv->vval.v_string = reg_submatch(no);
12801 }
12802 else
12803 {
12804 rettv->v_type = VAR_LIST;
12805 rettv->vval.v_list = reg_submatch_list(no);
12806 }
12807}
12808
12809/*
12810 * "substitute()" function
12811 */
12812 static void
12813f_substitute(typval_T *argvars, typval_T *rettv)
12814{
12815 char_u patbuf[NUMBUFLEN];
12816 char_u subbuf[NUMBUFLEN];
12817 char_u flagsbuf[NUMBUFLEN];
12818
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012819 char_u *str = tv_get_string_chk(&argvars[0]);
12820 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012821 char_u *sub = NULL;
12822 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012823 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012824
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012825 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12826 expr = &argvars[2];
12827 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012828 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012829
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012830 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012831 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12832 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012833 rettv->vval.v_string = NULL;
12834 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012835 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012836}
12837
12838/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012839 * "swapinfo(swap_filename)" function
12840 */
12841 static void
12842f_swapinfo(typval_T *argvars, typval_T *rettv)
12843{
12844 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012845 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012846}
12847
12848/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012849 * "swapname(expr)" function
12850 */
12851 static void
12852f_swapname(typval_T *argvars, typval_T *rettv)
12853{
12854 buf_T *buf;
12855
12856 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010012857 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020012858 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12859 || buf->b_ml.ml_mfp->mf_fname == NULL)
12860 rettv->vval.v_string = NULL;
12861 else
12862 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12863}
12864
12865/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012866 * "synID(lnum, col, trans)" function
12867 */
12868 static void
12869f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12870{
12871 int id = 0;
12872#ifdef FEAT_SYN_HL
12873 linenr_T lnum;
12874 colnr_T col;
12875 int trans;
12876 int transerr = FALSE;
12877
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012878 lnum = tv_get_lnum(argvars); /* -1 on type error */
12879 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12880 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012881
12882 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12883 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12884 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12885#endif
12886
12887 rettv->vval.v_number = id;
12888}
12889
12890/*
12891 * "synIDattr(id, what [, mode])" function
12892 */
12893 static void
12894f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12895{
12896 char_u *p = NULL;
12897#ifdef FEAT_SYN_HL
12898 int id;
12899 char_u *what;
12900 char_u *mode;
12901 char_u modebuf[NUMBUFLEN];
12902 int modec;
12903
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012904 id = (int)tv_get_number(&argvars[0]);
12905 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012906 if (argvars[2].v_type != VAR_UNKNOWN)
12907 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012908 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012909 modec = TOLOWER_ASC(mode[0]);
12910 if (modec != 't' && modec != 'c' && modec != 'g')
12911 modec = 0; /* replace invalid with current */
12912 }
12913 else
12914 {
12915#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12916 if (USE_24BIT)
12917 modec = 'g';
12918 else
12919#endif
12920 if (t_colors > 1)
12921 modec = 'c';
12922 else
12923 modec = 't';
12924 }
12925
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012926 switch (TOLOWER_ASC(what[0]))
12927 {
12928 case 'b':
12929 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12930 p = highlight_color(id, what, modec);
12931 else /* bold */
12932 p = highlight_has_attr(id, HL_BOLD, modec);
12933 break;
12934
12935 case 'f': /* fg[#] or font */
12936 p = highlight_color(id, what, modec);
12937 break;
12938
12939 case 'i':
12940 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12941 p = highlight_has_attr(id, HL_INVERSE, modec);
12942 else /* italic */
12943 p = highlight_has_attr(id, HL_ITALIC, modec);
12944 break;
12945
12946 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012947 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012948 break;
12949
12950 case 'r': /* reverse */
12951 p = highlight_has_attr(id, HL_INVERSE, modec);
12952 break;
12953
12954 case 's':
12955 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12956 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012957 /* strikeout */
12958 else if (TOLOWER_ASC(what[1]) == 't' &&
12959 TOLOWER_ASC(what[2]) == 'r')
12960 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012961 else /* standout */
12962 p = highlight_has_attr(id, HL_STANDOUT, modec);
12963 break;
12964
12965 case 'u':
12966 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12967 /* underline */
12968 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12969 else
12970 /* undercurl */
12971 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12972 break;
12973 }
12974
12975 if (p != NULL)
12976 p = vim_strsave(p);
12977#endif
12978 rettv->v_type = VAR_STRING;
12979 rettv->vval.v_string = p;
12980}
12981
12982/*
12983 * "synIDtrans(id)" function
12984 */
12985 static void
12986f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12987{
12988 int id;
12989
12990#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012991 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012992
12993 if (id > 0)
12994 id = syn_get_final_id(id);
12995 else
12996#endif
12997 id = 0;
12998
12999 rettv->vval.v_number = id;
13000}
13001
13002/*
13003 * "synconcealed(lnum, col)" function
13004 */
13005 static void
13006f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13007{
13008#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13009 linenr_T lnum;
13010 colnr_T col;
13011 int syntax_flags = 0;
13012 int cchar;
13013 int matchid = 0;
13014 char_u str[NUMBUFLEN];
13015#endif
13016
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013017 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013018
13019#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013020 lnum = tv_get_lnum(argvars); /* -1 on type error */
13021 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013022
13023 vim_memset(str, NUL, sizeof(str));
13024
13025 if (rettv_list_alloc(rettv) != FAIL)
13026 {
13027 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13028 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13029 && curwin->w_p_cole > 0)
13030 {
13031 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13032 syntax_flags = get_syntax_info(&matchid);
13033
13034 /* get the conceal character */
13035 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13036 {
13037 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013038 if (cchar == NUL && curwin->w_p_cole == 1)
13039 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013040 if (cchar != NUL)
13041 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013042 if (has_mbyte)
13043 (*mb_char2bytes)(cchar, str);
13044 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013045 str[0] = cchar;
13046 }
13047 }
13048 }
13049
13050 list_append_number(rettv->vval.v_list,
13051 (syntax_flags & HL_CONCEAL) != 0);
13052 /* -1 to auto-determine strlen */
13053 list_append_string(rettv->vval.v_list, str, -1);
13054 list_append_number(rettv->vval.v_list, matchid);
13055 }
13056#endif
13057}
13058
13059/*
13060 * "synstack(lnum, col)" function
13061 */
13062 static void
13063f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13064{
13065#ifdef FEAT_SYN_HL
13066 linenr_T lnum;
13067 colnr_T col;
13068 int i;
13069 int id;
13070#endif
13071
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013072 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013073
13074#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013075 lnum = tv_get_lnum(argvars); /* -1 on type error */
13076 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013077
13078 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13079 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13080 && rettv_list_alloc(rettv) != FAIL)
13081 {
13082 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13083 for (i = 0; ; ++i)
13084 {
13085 id = syn_get_stack_item(i);
13086 if (id < 0)
13087 break;
13088 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13089 break;
13090 }
13091 }
13092#endif
13093}
13094
13095 static void
13096get_cmd_output_as_rettv(
13097 typval_T *argvars,
13098 typval_T *rettv,
13099 int retlist)
13100{
13101 char_u *res = NULL;
13102 char_u *p;
13103 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013104 int err = FALSE;
13105 FILE *fd;
13106 list_T *list = NULL;
13107 int flags = SHELL_SILENT;
13108
13109 rettv->v_type = VAR_STRING;
13110 rettv->vval.v_string = NULL;
13111 if (check_restricted() || check_secure())
13112 goto errret;
13113
13114 if (argvars[1].v_type != VAR_UNKNOWN)
13115 {
13116 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013117 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013118 * command.
13119 */
13120 if ((infile = vim_tempname('i', TRUE)) == NULL)
13121 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013122 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013123 goto errret;
13124 }
13125
13126 fd = mch_fopen((char *)infile, WRITEBIN);
13127 if (fd == NULL)
13128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013129 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013130 goto errret;
13131 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013132 if (argvars[1].v_type == VAR_NUMBER)
13133 {
13134 linenr_T lnum;
13135 buf_T *buf;
13136
13137 buf = buflist_findnr(argvars[1].vval.v_number);
13138 if (buf == NULL)
13139 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013140 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013141 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013142 goto errret;
13143 }
13144
13145 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13146 {
13147 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13148 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13149 {
13150 err = TRUE;
13151 break;
13152 }
13153 if (putc(NL, fd) == EOF)
13154 {
13155 err = TRUE;
13156 break;
13157 }
13158 }
13159 }
13160 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013161 {
13162 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13163 err = TRUE;
13164 }
13165 else
13166 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013167 size_t len;
13168 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013169
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013170 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013171 if (p == NULL)
13172 {
13173 fclose(fd);
13174 goto errret; /* type error; errmsg already given */
13175 }
13176 len = STRLEN(p);
13177 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13178 err = TRUE;
13179 }
13180 if (fclose(fd) != 0)
13181 err = TRUE;
13182 if (err)
13183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013184 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013185 goto errret;
13186 }
13187 }
13188
13189 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13190 * echoes typeahead, that messes up the display. */
13191 if (!msg_silent)
13192 flags += SHELL_COOKED;
13193
13194 if (retlist)
13195 {
13196 int len;
13197 listitem_T *li;
13198 char_u *s = NULL;
13199 char_u *start;
13200 char_u *end;
13201 int i;
13202
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013203 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013204 if (res == NULL)
13205 goto errret;
13206
13207 list = list_alloc();
13208 if (list == NULL)
13209 goto errret;
13210
13211 for (i = 0; i < len; ++i)
13212 {
13213 start = res + i;
13214 while (i < len && res[i] != NL)
13215 ++i;
13216 end = res + i;
13217
Bram Moolenaar964b3742019-05-24 18:54:09 +020013218 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013219 if (s == NULL)
13220 goto errret;
13221
13222 for (p = s; start < end; ++p, ++start)
13223 *p = *start == NUL ? NL : *start;
13224 *p = NUL;
13225
13226 li = listitem_alloc();
13227 if (li == NULL)
13228 {
13229 vim_free(s);
13230 goto errret;
13231 }
13232 li->li_tv.v_type = VAR_STRING;
13233 li->li_tv.v_lock = 0;
13234 li->li_tv.vval.v_string = s;
13235 list_append(list, li);
13236 }
13237
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013238 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013239 list = NULL;
13240 }
13241 else
13242 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013243 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013244#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013245 /* translate <CR><NL> into <NL> */
13246 if (res != NULL)
13247 {
13248 char_u *s, *d;
13249
13250 d = res;
13251 for (s = res; *s; ++s)
13252 {
13253 if (s[0] == CAR && s[1] == NL)
13254 ++s;
13255 *d++ = *s;
13256 }
13257 *d = NUL;
13258 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013259#endif
13260 rettv->vval.v_string = res;
13261 res = NULL;
13262 }
13263
13264errret:
13265 if (infile != NULL)
13266 {
13267 mch_remove(infile);
13268 vim_free(infile);
13269 }
13270 if (res != NULL)
13271 vim_free(res);
13272 if (list != NULL)
13273 list_free(list);
13274}
13275
13276/*
13277 * "system()" function
13278 */
13279 static void
13280f_system(typval_T *argvars, typval_T *rettv)
13281{
13282 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13283}
13284
13285/*
13286 * "systemlist()" function
13287 */
13288 static void
13289f_systemlist(typval_T *argvars, typval_T *rettv)
13290{
13291 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13292}
13293
13294/*
13295 * "tabpagebuflist()" function
13296 */
13297 static void
13298f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13299{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013300 tabpage_T *tp;
13301 win_T *wp = NULL;
13302
13303 if (argvars[0].v_type == VAR_UNKNOWN)
13304 wp = firstwin;
13305 else
13306 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013307 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013308 if (tp != NULL)
13309 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13310 }
13311 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13312 {
13313 for (; wp != NULL; wp = wp->w_next)
13314 if (list_append_number(rettv->vval.v_list,
13315 wp->w_buffer->b_fnum) == FAIL)
13316 break;
13317 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013318}
13319
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013320/*
13321 * "tabpagenr()" function
13322 */
13323 static void
13324f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13325{
13326 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013327 char_u *arg;
13328
13329 if (argvars[0].v_type != VAR_UNKNOWN)
13330 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013331 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013332 nr = 0;
13333 if (arg != NULL)
13334 {
13335 if (STRCMP(arg, "$") == 0)
13336 nr = tabpage_index(NULL) - 1;
13337 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013338 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013339 }
13340 }
13341 else
13342 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013343 rettv->vval.v_number = nr;
13344}
13345
13346
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013347/*
13348 * Common code for tabpagewinnr() and winnr().
13349 */
13350 static int
13351get_winnr(tabpage_T *tp, typval_T *argvar)
13352{
13353 win_T *twin;
13354 int nr = 1;
13355 win_T *wp;
13356 char_u *arg;
13357
13358 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13359 if (argvar->v_type != VAR_UNKNOWN)
13360 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013361 int invalid_arg = FALSE;
13362
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013363 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013364 if (arg == NULL)
13365 nr = 0; /* type error; errmsg already given */
13366 else if (STRCMP(arg, "$") == 0)
13367 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13368 else if (STRCMP(arg, "#") == 0)
13369 {
13370 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13371 if (twin == NULL)
13372 nr = 0;
13373 }
13374 else
13375 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013376 long count;
13377 char_u *endp;
13378
13379 // Extract the window count (if specified). e.g. winnr('3j')
13380 count = strtol((char *)arg, (char **)&endp, 10);
13381 if (count <= 0)
13382 count = 1; // if count is not specified, default to 1
13383 if (endp != NULL && *endp != '\0')
13384 {
13385 if (STRCMP(endp, "j") == 0)
13386 twin = win_vert_neighbor(tp, twin, FALSE, count);
13387 else if (STRCMP(endp, "k") == 0)
13388 twin = win_vert_neighbor(tp, twin, TRUE, count);
13389 else if (STRCMP(endp, "h") == 0)
13390 twin = win_horz_neighbor(tp, twin, TRUE, count);
13391 else if (STRCMP(endp, "l") == 0)
13392 twin = win_horz_neighbor(tp, twin, FALSE, count);
13393 else
13394 invalid_arg = TRUE;
13395 }
13396 else
13397 invalid_arg = TRUE;
13398 }
13399
13400 if (invalid_arg)
13401 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013402 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013403 nr = 0;
13404 }
13405 }
13406
13407 if (nr > 0)
13408 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13409 wp != twin; wp = wp->w_next)
13410 {
13411 if (wp == NULL)
13412 {
13413 /* didn't find it in this tabpage */
13414 nr = 0;
13415 break;
13416 }
13417 ++nr;
13418 }
13419 return nr;
13420}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013421
13422/*
13423 * "tabpagewinnr()" function
13424 */
13425 static void
13426f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13427{
13428 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013429 tabpage_T *tp;
13430
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013431 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013432 if (tp == NULL)
13433 nr = 0;
13434 else
13435 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013436 rettv->vval.v_number = nr;
13437}
13438
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013439/*
13440 * "tagfiles()" function
13441 */
13442 static void
13443f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13444{
13445 char_u *fname;
13446 tagname_T tn;
13447 int first;
13448
13449 if (rettv_list_alloc(rettv) == FAIL)
13450 return;
13451 fname = alloc(MAXPATHL);
13452 if (fname == NULL)
13453 return;
13454
13455 for (first = TRUE; ; first = FALSE)
13456 if (get_tagfname(&tn, first, fname) == FAIL
13457 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13458 break;
13459 tagname_free(&tn);
13460 vim_free(fname);
13461}
13462
13463/*
13464 * "taglist()" function
13465 */
13466 static void
13467f_taglist(typval_T *argvars, typval_T *rettv)
13468{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013469 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013470 char_u *tag_pattern;
13471
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013472 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013473
13474 rettv->vval.v_number = FALSE;
13475 if (*tag_pattern == NUL)
13476 return;
13477
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013478 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013479 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013480 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013481 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013482}
13483
13484/*
13485 * "tempname()" function
13486 */
13487 static void
13488f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13489{
13490 static int x = 'A';
13491
13492 rettv->v_type = VAR_STRING;
13493 rettv->vval.v_string = vim_tempname(x, FALSE);
13494
13495 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13496 * names. Skip 'I' and 'O', they are used for shell redirection. */
13497 do
13498 {
13499 if (x == 'Z')
13500 x = '0';
13501 else if (x == '9')
13502 x = 'A';
13503 else
13504 {
13505#ifdef EBCDIC
13506 if (x == 'I')
13507 x = 'J';
13508 else if (x == 'R')
13509 x = 'S';
13510 else
13511#endif
13512 ++x;
13513 }
13514 } while (x == 'I' || x == 'O');
13515}
13516
13517#ifdef FEAT_FLOAT
13518/*
13519 * "tan()" function
13520 */
13521 static void
13522f_tan(typval_T *argvars, typval_T *rettv)
13523{
13524 float_T f = 0.0;
13525
13526 rettv->v_type = VAR_FLOAT;
13527 if (get_float_arg(argvars, &f) == OK)
13528 rettv->vval.v_float = tan(f);
13529 else
13530 rettv->vval.v_float = 0.0;
13531}
13532
13533/*
13534 * "tanh()" function
13535 */
13536 static void
13537f_tanh(typval_T *argvars, typval_T *rettv)
13538{
13539 float_T f = 0.0;
13540
13541 rettv->v_type = VAR_FLOAT;
13542 if (get_float_arg(argvars, &f) == OK)
13543 rettv->vval.v_float = tanh(f);
13544 else
13545 rettv->vval.v_float = 0.0;
13546}
13547#endif
13548
13549/*
13550 * "test_alloc_fail(id, countdown, repeat)" function
13551 */
13552 static void
13553f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13554{
13555 if (argvars[0].v_type != VAR_NUMBER
13556 || argvars[0].vval.v_number <= 0
13557 || argvars[1].v_type != VAR_NUMBER
13558 || argvars[1].vval.v_number < 0
13559 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013560 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013561 else
13562 {
13563 alloc_fail_id = argvars[0].vval.v_number;
13564 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013565 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013566 alloc_fail_countdown = argvars[1].vval.v_number;
13567 alloc_fail_repeat = argvars[2].vval.v_number;
13568 did_outofmem_msg = FALSE;
13569 }
13570}
13571
13572/*
13573 * "test_autochdir()"
13574 */
13575 static void
13576f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13577{
13578#if defined(FEAT_AUTOCHDIR)
13579 test_autochdir = TRUE;
13580#endif
13581}
13582
13583/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013584 * "test_feedinput()"
13585 */
13586 static void
13587f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13588{
13589#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013590 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013591
13592 if (val != NULL)
13593 {
13594 trash_input_buf();
13595 add_to_input_buf_csi(val, (int)STRLEN(val));
13596 }
13597#endif
13598}
13599
13600/*
Bram Moolenaareda65222019-05-16 20:29:44 +020013601 * "test_getvalue({name})" function
13602 */
13603 static void
13604f_test_getvalue(typval_T *argvars, typval_T *rettv)
13605{
13606 if (argvars[0].v_type != VAR_STRING)
13607 emsg(_(e_invarg));
13608 else
13609 {
13610 char_u *name = tv_get_string(&argvars[0]);
13611
13612 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
13613 rettv->vval.v_number = need_fileinfo;
13614 else
13615 semsg(_(e_invarg2), name);
13616 }
13617}
13618
13619/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013620 * "test_option_not_set({name})" function
13621 */
13622 static void
13623f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13624{
13625 char_u *name = (char_u *)"";
13626
13627 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013628 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013629 else
13630 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013631 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013632 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013633 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013634 }
13635}
13636
13637/*
13638 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013639 */
13640 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013641f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013642{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013643 char_u *name = (char_u *)"";
13644 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013645 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013646
13647 if (argvars[0].v_type != VAR_STRING
13648 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013649 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013650 else
13651 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013652 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013653 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013654
13655 if (STRCMP(name, (char_u *)"redraw") == 0)
13656 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013657 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13658 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013659 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13660 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013661 else if (STRCMP(name, (char_u *)"starting") == 0)
13662 {
13663 if (val)
13664 {
13665 if (save_starting < 0)
13666 save_starting = starting;
13667 starting = 0;
13668 }
13669 else
13670 {
13671 starting = save_starting;
13672 save_starting = -1;
13673 }
13674 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013675 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13676 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013677 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
13678 no_query_mouse_for_testing = val;
Bram Moolenaaradc67142019-06-22 01:40:42 +020013679 else if (STRCMP(name, (char_u *)"no_wait_return") == 0)
13680 no_wait_return = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013681 else if (STRCMP(name, (char_u *)"ALL") == 0)
13682 {
13683 disable_char_avail_for_testing = FALSE;
13684 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013685 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013686 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013687 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013688 if (save_starting >= 0)
13689 {
13690 starting = save_starting;
13691 save_starting = -1;
13692 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013693 }
13694 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013695 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013696 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013697}
13698
13699/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010013700 * "test_refcount({expr})" function
13701 */
13702 static void
13703f_test_refcount(typval_T *argvars, typval_T *rettv)
13704{
13705 int retval = -1;
13706
13707 switch (argvars[0].v_type)
13708 {
13709 case VAR_UNKNOWN:
13710 case VAR_NUMBER:
13711 case VAR_FLOAT:
13712 case VAR_SPECIAL:
13713 case VAR_STRING:
13714 break;
13715 case VAR_JOB:
13716#ifdef FEAT_JOB_CHANNEL
13717 if (argvars[0].vval.v_job != NULL)
13718 retval = argvars[0].vval.v_job->jv_refcount - 1;
13719#endif
13720 break;
13721 case VAR_CHANNEL:
13722#ifdef FEAT_JOB_CHANNEL
13723 if (argvars[0].vval.v_channel != NULL)
13724 retval = argvars[0].vval.v_channel->ch_refcount - 1;
13725#endif
13726 break;
13727 case VAR_FUNC:
13728 if (argvars[0].vval.v_string != NULL)
13729 {
13730 ufunc_T *fp;
13731
13732 fp = find_func(argvars[0].vval.v_string);
13733 if (fp != NULL)
13734 retval = fp->uf_refcount;
13735 }
13736 break;
13737 case VAR_PARTIAL:
13738 if (argvars[0].vval.v_partial != NULL)
13739 retval = argvars[0].vval.v_partial->pt_refcount - 1;
13740 break;
13741 case VAR_BLOB:
13742 if (argvars[0].vval.v_blob != NULL)
13743 retval = argvars[0].vval.v_blob->bv_refcount - 1;
13744 break;
13745 case VAR_LIST:
13746 if (argvars[0].vval.v_list != NULL)
13747 retval = argvars[0].vval.v_list->lv_refcount - 1;
13748 break;
13749 case VAR_DICT:
13750 if (argvars[0].vval.v_dict != NULL)
13751 retval = argvars[0].vval.v_dict->dv_refcount - 1;
13752 break;
13753 }
13754
13755 rettv->v_type = VAR_NUMBER;
13756 rettv->vval.v_number = retval;
13757
13758}
13759
13760/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013761 * "test_garbagecollect_now()" function
13762 */
13763 static void
13764f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13765{
13766 /* This is dangerous, any Lists and Dicts used internally may be freed
13767 * while still in use. */
13768 garbage_collect(TRUE);
13769}
13770
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013771/*
Bram Moolenaaradc67142019-06-22 01:40:42 +020013772 * "test_garbagecollect_soon()" function
13773 */
13774 static void
13775f_test_garbagecollect_soon(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13776{
13777 may_garbage_collect = TRUE;
13778}
13779
13780/*
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013781 * "test_ignore_error()" function
13782 */
13783 static void
13784f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13785{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013786 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013787}
13788
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013789 static void
13790f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13791{
13792 rettv->v_type = VAR_BLOB;
13793 rettv->vval.v_blob = NULL;
13794}
13795
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013796#ifdef FEAT_JOB_CHANNEL
13797 static void
13798f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13799{
13800 rettv->v_type = VAR_CHANNEL;
13801 rettv->vval.v_channel = NULL;
13802}
13803#endif
13804
13805 static void
13806f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13807{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013808 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013809}
13810
13811#ifdef FEAT_JOB_CHANNEL
13812 static void
13813f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13814{
13815 rettv->v_type = VAR_JOB;
13816 rettv->vval.v_job = NULL;
13817}
13818#endif
13819
13820 static void
13821f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13822{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013823 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013824}
13825
13826 static void
13827f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13828{
13829 rettv->v_type = VAR_PARTIAL;
13830 rettv->vval.v_partial = NULL;
13831}
13832
13833 static void
13834f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13835{
13836 rettv->v_type = VAR_STRING;
13837 rettv->vval.v_string = NULL;
13838}
13839
Bram Moolenaarab186732018-09-14 21:27:06 +020013840#ifdef FEAT_GUI
13841 static void
13842f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13843{
13844 char_u *which;
13845 long value;
13846 int dragging;
13847 scrollbar_T *sb = NULL;
13848
13849 if (argvars[0].v_type != VAR_STRING
13850 || (argvars[1].v_type) != VAR_NUMBER
13851 || (argvars[2].v_type) != VAR_NUMBER)
13852 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013853 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013854 return;
13855 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013856 which = tv_get_string(&argvars[0]);
13857 value = tv_get_number(&argvars[1]);
13858 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013859
13860 if (STRCMP(which, "left") == 0)
13861 sb = &curwin->w_scrollbars[SBAR_LEFT];
13862 else if (STRCMP(which, "right") == 0)
13863 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13864 else if (STRCMP(which, "hor") == 0)
13865 sb = &gui.bottom_sbar;
13866 if (sb == NULL)
13867 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013868 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020013869 return;
13870 }
13871 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013872# ifndef USE_ON_FLY_SCROLL
13873 // need to loop through normal_cmd() to handle the scroll events
13874 exec_normal(FALSE, TRUE, FALSE);
13875# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013876}
13877#endif
13878
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013879#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013880 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013881f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
13882{
13883 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
13884 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
13885}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013886#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013887
13888 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013889f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13890{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013891 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013892}
13893
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013894/*
13895 * Get a callback from "arg". It can be a Funcref or a function name.
13896 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013897 * "cb_name" is not allocated.
13898 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013899 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013900 callback_T
13901get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013902{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013903 callback_T res;
13904
13905 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013906 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13907 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013908 res.cb_partial = arg->vval.v_partial;
13909 ++res.cb_partial->pt_refcount;
13910 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013911 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013912 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013913 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013914 res.cb_partial = NULL;
13915 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
13916 {
13917 // Note that we don't make a copy of the string.
13918 res.cb_name = arg->vval.v_string;
13919 func_ref(res.cb_name);
13920 }
13921 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13922 {
13923 res.cb_name = (char_u *)"";
13924 }
13925 else
13926 {
13927 emsg(_("E921: Invalid callback argument"));
13928 res.cb_name = NULL;
13929 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013930 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013931 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013932}
13933
13934/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013935 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013936 */
13937 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013938put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013939{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013940 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013941 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013942 tv->v_type = VAR_PARTIAL;
13943 tv->vval.v_partial = cb->cb_partial;
13944 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013945 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013946 else
13947 {
13948 tv->v_type = VAR_FUNC;
13949 tv->vval.v_string = vim_strsave(cb->cb_name);
13950 func_ref(cb->cb_name);
13951 }
13952}
13953
13954/*
13955 * Make a copy of "src" into "dest", allocating the function name if needed,
13956 * without incrementing the refcount.
13957 */
13958 void
13959set_callback(callback_T *dest, callback_T *src)
13960{
13961 if (src->cb_partial == NULL)
13962 {
13963 // just a function name, make a copy
13964 dest->cb_name = vim_strsave(src->cb_name);
13965 dest->cb_free_name = TRUE;
13966 }
13967 else
13968 {
13969 // cb_name is a pointer into cb_partial
13970 dest->cb_name = src->cb_name;
13971 dest->cb_free_name = FALSE;
13972 }
13973 dest->cb_partial = src->cb_partial;
13974}
13975
13976/*
13977 * Unref/free "callback" returned by get_callback() or set_callback().
13978 */
13979 void
13980free_callback(callback_T *callback)
13981{
13982 if (callback->cb_partial != NULL)
13983 {
13984 partial_unref(callback->cb_partial);
13985 callback->cb_partial = NULL;
13986 }
13987 else if (callback->cb_name != NULL)
13988 func_unref(callback->cb_name);
13989 if (callback->cb_free_name)
13990 {
13991 vim_free(callback->cb_name);
13992 callback->cb_free_name = FALSE;
13993 }
13994 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013995}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013996
13997#ifdef FEAT_TIMERS
13998/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013999 * "timer_info([timer])" function
14000 */
14001 static void
14002f_timer_info(typval_T *argvars, typval_T *rettv)
14003{
14004 timer_T *timer = NULL;
14005
14006 if (rettv_list_alloc(rettv) != OK)
14007 return;
14008 if (argvars[0].v_type != VAR_UNKNOWN)
14009 {
14010 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014011 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014012 else
14013 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014014 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014015 if (timer != NULL)
14016 add_timer_info(rettv, timer);
14017 }
14018 }
14019 else
14020 add_timer_info_all(rettv);
14021}
14022
14023/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014024 * "timer_pause(timer, paused)" function
14025 */
14026 static void
14027f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14028{
14029 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014030 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014031
14032 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014033 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014034 else
14035 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014036 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014037 if (timer != NULL)
14038 timer->tr_paused = paused;
14039 }
14040}
14041
14042/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014043 * "timer_start(time, callback [, options])" function
14044 */
14045 static void
14046f_timer_start(typval_T *argvars, typval_T *rettv)
14047{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014048 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014049 timer_T *timer;
14050 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014051 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014052 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014053
Bram Moolenaar75537a92016-09-05 22:45:28 +020014054 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014055 if (check_secure())
14056 return;
14057 if (argvars[2].v_type != VAR_UNKNOWN)
14058 {
14059 if (argvars[2].v_type != VAR_DICT
14060 || (dict = argvars[2].vval.v_dict) == NULL)
14061 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014062 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014063 return;
14064 }
14065 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014066 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014067 }
14068
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014069 callback = get_callback(&argvars[1]);
14070 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014071 return;
14072
14073 timer = create_timer(msec, repeat);
14074 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014075 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014076 else
14077 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014078 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014079 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014080 }
14081}
14082
14083/*
14084 * "timer_stop(timer)" function
14085 */
14086 static void
14087f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14088{
14089 timer_T *timer;
14090
14091 if (argvars[0].v_type != VAR_NUMBER)
14092 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014093 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014094 return;
14095 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014096 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014097 if (timer != NULL)
14098 stop_timer(timer);
14099}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014100
14101/*
14102 * "timer_stopall()" function
14103 */
14104 static void
14105f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14106{
14107 stop_all_timers();
14108}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014109#endif
14110
14111/*
14112 * "tolower(string)" function
14113 */
14114 static void
14115f_tolower(typval_T *argvars, typval_T *rettv)
14116{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014117 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014118 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014119}
14120
14121/*
14122 * "toupper(string)" function
14123 */
14124 static void
14125f_toupper(typval_T *argvars, typval_T *rettv)
14126{
14127 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014128 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014129}
14130
14131/*
14132 * "tr(string, fromstr, tostr)" function
14133 */
14134 static void
14135f_tr(typval_T *argvars, typval_T *rettv)
14136{
14137 char_u *in_str;
14138 char_u *fromstr;
14139 char_u *tostr;
14140 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014141 int inlen;
14142 int fromlen;
14143 int tolen;
14144 int idx;
14145 char_u *cpstr;
14146 int cplen;
14147 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014148 char_u buf[NUMBUFLEN];
14149 char_u buf2[NUMBUFLEN];
14150 garray_T ga;
14151
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014152 in_str = tv_get_string(&argvars[0]);
14153 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14154 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014155
14156 /* Default return value: empty string. */
14157 rettv->v_type = VAR_STRING;
14158 rettv->vval.v_string = NULL;
14159 if (fromstr == NULL || tostr == NULL)
14160 return; /* type error; errmsg already given */
14161 ga_init2(&ga, (int)sizeof(char), 80);
14162
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014163 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014164 /* not multi-byte: fromstr and tostr must be the same length */
14165 if (STRLEN(fromstr) != STRLEN(tostr))
14166 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014167error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014168 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014169 ga_clear(&ga);
14170 return;
14171 }
14172
14173 /* fromstr and tostr have to contain the same number of chars */
14174 while (*in_str != NUL)
14175 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014176 if (has_mbyte)
14177 {
14178 inlen = (*mb_ptr2len)(in_str);
14179 cpstr = in_str;
14180 cplen = inlen;
14181 idx = 0;
14182 for (p = fromstr; *p != NUL; p += fromlen)
14183 {
14184 fromlen = (*mb_ptr2len)(p);
14185 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14186 {
14187 for (p = tostr; *p != NUL; p += tolen)
14188 {
14189 tolen = (*mb_ptr2len)(p);
14190 if (idx-- == 0)
14191 {
14192 cplen = tolen;
14193 cpstr = p;
14194 break;
14195 }
14196 }
14197 if (*p == NUL) /* tostr is shorter than fromstr */
14198 goto error;
14199 break;
14200 }
14201 ++idx;
14202 }
14203
14204 if (first && cpstr == in_str)
14205 {
14206 /* Check that fromstr and tostr have the same number of
14207 * (multi-byte) characters. Done only once when a character
14208 * of in_str doesn't appear in fromstr. */
14209 first = FALSE;
14210 for (p = tostr; *p != NUL; p += tolen)
14211 {
14212 tolen = (*mb_ptr2len)(p);
14213 --idx;
14214 }
14215 if (idx != 0)
14216 goto error;
14217 }
14218
14219 (void)ga_grow(&ga, cplen);
14220 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14221 ga.ga_len += cplen;
14222
14223 in_str += inlen;
14224 }
14225 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014226 {
14227 /* When not using multi-byte chars we can do it faster. */
14228 p = vim_strchr(fromstr, *in_str);
14229 if (p != NULL)
14230 ga_append(&ga, tostr[p - fromstr]);
14231 else
14232 ga_append(&ga, *in_str);
14233 ++in_str;
14234 }
14235 }
14236
14237 /* add a terminating NUL */
14238 (void)ga_grow(&ga, 1);
14239 ga_append(&ga, NUL);
14240
14241 rettv->vval.v_string = ga.ga_data;
14242}
14243
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014244/*
14245 * "trim({expr})" function
14246 */
14247 static void
14248f_trim(typval_T *argvars, typval_T *rettv)
14249{
14250 char_u buf1[NUMBUFLEN];
14251 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014252 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014253 char_u *mask = NULL;
14254 char_u *tail;
14255 char_u *prev;
14256 char_u *p;
14257 int c1;
14258
14259 rettv->v_type = VAR_STRING;
14260 if (head == NULL)
14261 {
14262 rettv->vval.v_string = NULL;
14263 return;
14264 }
14265
14266 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014267 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014268
14269 while (*head != NUL)
14270 {
14271 c1 = PTR2CHAR(head);
14272 if (mask == NULL)
14273 {
14274 if (c1 > ' ' && c1 != 0xa0)
14275 break;
14276 }
14277 else
14278 {
14279 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14280 if (c1 == PTR2CHAR(p))
14281 break;
14282 if (*p == NUL)
14283 break;
14284 }
14285 MB_PTR_ADV(head);
14286 }
14287
14288 for (tail = head + STRLEN(head); tail > head; tail = prev)
14289 {
14290 prev = tail;
14291 MB_PTR_BACK(head, prev);
14292 c1 = PTR2CHAR(prev);
14293 if (mask == NULL)
14294 {
14295 if (c1 > ' ' && c1 != 0xa0)
14296 break;
14297 }
14298 else
14299 {
14300 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14301 if (c1 == PTR2CHAR(p))
14302 break;
14303 if (*p == NUL)
14304 break;
14305 }
14306 }
14307 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14308}
14309
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014310#ifdef FEAT_FLOAT
14311/*
14312 * "trunc({float})" function
14313 */
14314 static void
14315f_trunc(typval_T *argvars, typval_T *rettv)
14316{
14317 float_T f = 0.0;
14318
14319 rettv->v_type = VAR_FLOAT;
14320 if (get_float_arg(argvars, &f) == OK)
14321 /* trunc() is not in C90, use floor() or ceil() instead. */
14322 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14323 else
14324 rettv->vval.v_float = 0.0;
14325}
14326#endif
14327
14328/*
14329 * "type(expr)" function
14330 */
14331 static void
14332f_type(typval_T *argvars, typval_T *rettv)
14333{
14334 int n = -1;
14335
14336 switch (argvars[0].v_type)
14337 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014338 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14339 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014340 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014341 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14342 case VAR_LIST: n = VAR_TYPE_LIST; break;
14343 case VAR_DICT: n = VAR_TYPE_DICT; break;
14344 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014345 case VAR_SPECIAL:
14346 if (argvars[0].vval.v_number == VVAL_FALSE
14347 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014348 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014349 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014350 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014351 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014352 case VAR_JOB: n = VAR_TYPE_JOB; break;
14353 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014354 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014355 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014356 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014357 n = -1;
14358 break;
14359 }
14360 rettv->vval.v_number = n;
14361}
14362
14363/*
14364 * "undofile(name)" function
14365 */
14366 static void
14367f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14368{
14369 rettv->v_type = VAR_STRING;
14370#ifdef FEAT_PERSISTENT_UNDO
14371 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014372 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014373
14374 if (*fname == NUL)
14375 {
14376 /* If there is no file name there will be no undo file. */
14377 rettv->vval.v_string = NULL;
14378 }
14379 else
14380 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020014381 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014382
14383 if (ffname != NULL)
14384 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14385 vim_free(ffname);
14386 }
14387 }
14388#else
14389 rettv->vval.v_string = NULL;
14390#endif
14391}
14392
14393/*
14394 * "undotree()" function
14395 */
14396 static void
14397f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14398{
14399 if (rettv_dict_alloc(rettv) == OK)
14400 {
14401 dict_T *dict = rettv->vval.v_dict;
14402 list_T *list;
14403
Bram Moolenaare0be1672018-07-08 16:50:37 +020014404 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14405 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14406 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14407 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14408 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14409 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014410
14411 list = list_alloc();
14412 if (list != NULL)
14413 {
14414 u_eval_tree(curbuf->b_u_oldhead, list);
14415 dict_add_list(dict, "entries", list);
14416 }
14417 }
14418}
14419
14420/*
14421 * "values(dict)" function
14422 */
14423 static void
14424f_values(typval_T *argvars, typval_T *rettv)
14425{
14426 dict_list(argvars, rettv, 1);
14427}
14428
14429/*
14430 * "virtcol(string)" function
14431 */
14432 static void
14433f_virtcol(typval_T *argvars, typval_T *rettv)
14434{
14435 colnr_T vcol = 0;
14436 pos_T *fp;
14437 int fnum = curbuf->b_fnum;
14438
14439 fp = var2fpos(&argvars[0], FALSE, &fnum);
14440 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14441 && fnum == curbuf->b_fnum)
14442 {
14443 getvvcol(curwin, fp, NULL, NULL, &vcol);
14444 ++vcol;
14445 }
14446
14447 rettv->vval.v_number = vcol;
14448}
14449
14450/*
14451 * "visualmode()" function
14452 */
14453 static void
14454f_visualmode(typval_T *argvars, typval_T *rettv)
14455{
14456 char_u str[2];
14457
14458 rettv->v_type = VAR_STRING;
14459 str[0] = curbuf->b_visual_mode_eval;
14460 str[1] = NUL;
14461 rettv->vval.v_string = vim_strsave(str);
14462
14463 /* A non-zero number or non-empty string argument: reset mode. */
14464 if (non_zero_arg(&argvars[0]))
14465 curbuf->b_visual_mode_eval = NUL;
14466}
14467
14468/*
14469 * "wildmenumode()" function
14470 */
14471 static void
14472f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14473{
14474#ifdef FEAT_WILDMENU
14475 if (wild_menu_showing)
14476 rettv->vval.v_number = 1;
14477#endif
14478}
14479
14480/*
14481 * "winbufnr(nr)" function
14482 */
14483 static void
14484f_winbufnr(typval_T *argvars, typval_T *rettv)
14485{
14486 win_T *wp;
14487
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014488 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014489 if (wp == NULL)
14490 rettv->vval.v_number = -1;
14491 else
14492 rettv->vval.v_number = wp->w_buffer->b_fnum;
14493}
14494
14495/*
14496 * "wincol()" function
14497 */
14498 static void
14499f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14500{
14501 validate_cursor();
14502 rettv->vval.v_number = curwin->w_wcol + 1;
14503}
14504
14505/*
14506 * "winheight(nr)" function
14507 */
14508 static void
14509f_winheight(typval_T *argvars, typval_T *rettv)
14510{
14511 win_T *wp;
14512
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014513 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014514 if (wp == NULL)
14515 rettv->vval.v_number = -1;
14516 else
14517 rettv->vval.v_number = wp->w_height;
14518}
14519
14520/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014521 * "winlayout()" function
14522 */
14523 static void
14524f_winlayout(typval_T *argvars, typval_T *rettv)
14525{
14526 tabpage_T *tp;
14527
14528 if (rettv_list_alloc(rettv) != OK)
14529 return;
14530
14531 if (argvars[0].v_type == VAR_UNKNOWN)
14532 tp = curtab;
14533 else
14534 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014535 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014536 if (tp == NULL)
14537 return;
14538 }
14539
14540 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14541}
14542
14543/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014544 * "winline()" function
14545 */
14546 static void
14547f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14548{
14549 validate_cursor();
14550 rettv->vval.v_number = curwin->w_wrow + 1;
14551}
14552
14553/*
14554 * "winnr()" function
14555 */
14556 static void
14557f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14558{
14559 int nr = 1;
14560
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014561 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014562 rettv->vval.v_number = nr;
14563}
14564
14565/*
14566 * "winrestcmd()" function
14567 */
14568 static void
14569f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14570{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014571 win_T *wp;
14572 int winnr = 1;
14573 garray_T ga;
14574 char_u buf[50];
14575
14576 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014577 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014578 {
14579 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14580 ga_concat(&ga, buf);
14581 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14582 ga_concat(&ga, buf);
14583 ++winnr;
14584 }
14585 ga_append(&ga, NUL);
14586
14587 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014588 rettv->v_type = VAR_STRING;
14589}
14590
14591/*
14592 * "winrestview()" function
14593 */
14594 static void
14595f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14596{
14597 dict_T *dict;
14598
14599 if (argvars[0].v_type != VAR_DICT
14600 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014601 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014602 else
14603 {
14604 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014605 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014606 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014607 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014608 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014609 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014610 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14611 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014612 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014613 curwin->w_set_curswant = FALSE;
14614 }
14615
14616 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014617 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014618#ifdef FEAT_DIFF
14619 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014620 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014621#endif
14622 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014623 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014624 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014625 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014626
14627 check_cursor();
14628 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014629 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014630 changed_window_setting();
14631
14632 if (curwin->w_topline <= 0)
14633 curwin->w_topline = 1;
14634 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14635 curwin->w_topline = curbuf->b_ml.ml_line_count;
14636#ifdef FEAT_DIFF
14637 check_topfill(curwin, TRUE);
14638#endif
14639 }
14640}
14641
14642/*
14643 * "winsaveview()" function
14644 */
14645 static void
14646f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14647{
14648 dict_T *dict;
14649
14650 if (rettv_dict_alloc(rettv) == FAIL)
14651 return;
14652 dict = rettv->vval.v_dict;
14653
Bram Moolenaare0be1672018-07-08 16:50:37 +020014654 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14655 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020014656 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014657 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014658 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014659
Bram Moolenaare0be1672018-07-08 16:50:37 +020014660 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014661#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014662 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014663#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014664 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14665 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014666}
14667
14668/*
14669 * "winwidth(nr)" function
14670 */
14671 static void
14672f_winwidth(typval_T *argvars, typval_T *rettv)
14673{
14674 win_T *wp;
14675
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014676 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014677 if (wp == NULL)
14678 rettv->vval.v_number = -1;
14679 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014680 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014681}
14682
14683/*
14684 * "wordcount()" function
14685 */
14686 static void
14687f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14688{
14689 if (rettv_dict_alloc(rettv) == FAIL)
14690 return;
14691 cursor_pos_info(rettv->vval.v_dict);
14692}
14693
14694/*
14695 * "writefile()" function
14696 */
14697 static void
14698f_writefile(typval_T *argvars, typval_T *rettv)
14699{
14700 int binary = FALSE;
14701 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014702#ifdef HAVE_FSYNC
14703 int do_fsync = p_fs;
14704#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014705 char_u *fname;
14706 FILE *fd;
14707 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014708 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014709 list_T *list = NULL;
14710 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014711
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014712 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010014713 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014714 return;
14715
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014716 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014717 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014718 list = argvars[0].vval.v_list;
14719 if (list == NULL)
14720 return;
14721 for (li = list->lv_first; li != NULL; li = li->li_next)
14722 if (tv_get_string_chk(&li->li_tv) == NULL)
14723 return;
14724 }
14725 else if (argvars[0].v_type == VAR_BLOB)
14726 {
14727 blob = argvars[0].vval.v_blob;
14728 if (blob == NULL)
14729 return;
14730 }
14731 else
14732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014733 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014734 return;
14735 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014736
14737 if (argvars[2].v_type != VAR_UNKNOWN)
14738 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014739 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014740
14741 if (arg2 == NULL)
14742 return;
14743 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014744 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014745 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014746 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014747#ifdef HAVE_FSYNC
14748 if (vim_strchr(arg2, 's') != NULL)
14749 do_fsync = TRUE;
14750 else if (vim_strchr(arg2, 'S') != NULL)
14751 do_fsync = FALSE;
14752#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014753 }
14754
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014755 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014756 if (fname == NULL)
14757 return;
14758
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014759 /* Always open the file in binary mode, library functions have a mind of
14760 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014761 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14762 append ? APPENDBIN : WRITEBIN)) == NULL)
14763 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014764 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014765 ret = -1;
14766 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014767 else if (blob)
14768 {
14769 if (write_blob(fd, blob) == FAIL)
14770 ret = -1;
14771#ifdef HAVE_FSYNC
14772 else if (do_fsync)
14773 // Ignore the error, the user wouldn't know what to do about it.
14774 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010014775 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014776#endif
14777 fclose(fd);
14778 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014779 else
14780 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014781 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014782 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014783#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014784 else if (do_fsync)
14785 /* Ignore the error, the user wouldn't know what to do about it.
14786 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010014787 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014788#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014789 fclose(fd);
14790 }
14791
14792 rettv->vval.v_number = ret;
14793}
14794
14795/*
14796 * "xor(expr, expr)" function
14797 */
14798 static void
14799f_xor(typval_T *argvars, typval_T *rettv)
14800{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014801 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14802 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014803}
14804
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014805#endif /* FEAT_EVAL */