blob: 925040d2b04d9d9b7446db0453e9df283d54be7c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaard0573012017-10-28 21:11:06 +020023#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020024# include <time.h> /* for time_t */
25#endif
26
27static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010028static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020029static char *e_stringreq = N_("E928: String required");
Bram Moolenaaraff74912019-03-30 18:11:49 +010030static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020031
32#ifdef FEAT_FLOAT
33static void f_abs(typval_T *argvars, typval_T *rettv);
34static void f_acos(typval_T *argvars, typval_T *rettv);
35#endif
36static void f_add(typval_T *argvars, typval_T *rettv);
37static void f_and(typval_T *argvars, typval_T *rettv);
38static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020039static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_argc(typval_T *argvars, typval_T *rettv);
41static void f_argidx(typval_T *argvars, typval_T *rettv);
42static void f_arglistid(typval_T *argvars, typval_T *rettv);
43static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010044static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020045static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010046static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020047static void f_assert_exception(typval_T *argvars, typval_T *rettv);
48static void f_assert_fails(typval_T *argvars, typval_T *rettv);
49static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020050static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_match(typval_T *argvars, typval_T *rettv);
52static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
53static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010054static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_true(typval_T *argvars, typval_T *rettv);
56#ifdef FEAT_FLOAT
57static void f_asin(typval_T *argvars, typval_T *rettv);
58static void f_atan(typval_T *argvars, typval_T *rettv);
59static void f_atan2(typval_T *argvars, typval_T *rettv);
60#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010061#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020062static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010063static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010064# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010065static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010067#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_browse(typval_T *argvars, typval_T *rettv);
69static void f_browsedir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020070static void f_bufadd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_bufexists(typval_T *argvars, typval_T *rettv);
72static void f_buflisted(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020073static void f_bufload(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020074static void f_bufloaded(typval_T *argvars, typval_T *rettv);
75static void f_bufname(typval_T *argvars, typval_T *rettv);
76static void f_bufnr(typval_T *argvars, typval_T *rettv);
77static void f_bufwinid(typval_T *argvars, typval_T *rettv);
78static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
79static void f_byte2line(typval_T *argvars, typval_T *rettv);
80static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
81static void f_byteidx(typval_T *argvars, typval_T *rettv);
82static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
83static void f_call(typval_T *argvars, typval_T *rettv);
84#ifdef FEAT_FLOAT
85static void f_ceil(typval_T *argvars, typval_T *rettv);
86#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_changenr(typval_T *argvars, typval_T *rettv);
88static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020089static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_cindent(typval_T *argvars, typval_T *rettv);
91static void f_clearmatches(typval_T *argvars, typval_T *rettv);
92static void f_col(typval_T *argvars, typval_T *rettv);
93#if defined(FEAT_INS_EXPAND)
94static void f_complete(typval_T *argvars, typval_T *rettv);
95static void f_complete_add(typval_T *argvars, typval_T *rettv);
96static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +010097static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020098#endif
99static void f_confirm(typval_T *argvars, typval_T *rettv);
100static void f_copy(typval_T *argvars, typval_T *rettv);
101#ifdef FEAT_FLOAT
102static void f_cos(typval_T *argvars, typval_T *rettv);
103static void f_cosh(typval_T *argvars, typval_T *rettv);
104#endif
105static void f_count(typval_T *argvars, typval_T *rettv);
106static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
107static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100108#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200109static void f_debugbreak(typval_T *argvars, typval_T *rettv);
110#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200111static void f_deepcopy(typval_T *argvars, typval_T *rettv);
112static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200113static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200114static void f_did_filetype(typval_T *argvars, typval_T *rettv);
115static void f_diff_filler(typval_T *argvars, typval_T *rettv);
116static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
117static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200118static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119static void f_escape(typval_T *argvars, typval_T *rettv);
120static void f_eval(typval_T *argvars, typval_T *rettv);
121static void f_eventhandler(typval_T *argvars, typval_T *rettv);
122static void f_executable(typval_T *argvars, typval_T *rettv);
123static void f_execute(typval_T *argvars, typval_T *rettv);
124static void f_exepath(typval_T *argvars, typval_T *rettv);
125static void f_exists(typval_T *argvars, typval_T *rettv);
126#ifdef FEAT_FLOAT
127static void f_exp(typval_T *argvars, typval_T *rettv);
128#endif
129static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200130static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131static void f_extend(typval_T *argvars, typval_T *rettv);
132static void f_feedkeys(typval_T *argvars, typval_T *rettv);
133static void f_filereadable(typval_T *argvars, typval_T *rettv);
134static void f_filewritable(typval_T *argvars, typval_T *rettv);
135static void f_filter(typval_T *argvars, typval_T *rettv);
136static void f_finddir(typval_T *argvars, typval_T *rettv);
137static void f_findfile(typval_T *argvars, typval_T *rettv);
138#ifdef FEAT_FLOAT
139static void f_float2nr(typval_T *argvars, typval_T *rettv);
140static void f_floor(typval_T *argvars, typval_T *rettv);
141static void f_fmod(typval_T *argvars, typval_T *rettv);
142#endif
143static void f_fnameescape(typval_T *argvars, typval_T *rettv);
144static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
145static void f_foldclosed(typval_T *argvars, typval_T *rettv);
146static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
147static void f_foldlevel(typval_T *argvars, typval_T *rettv);
148static void f_foldtext(typval_T *argvars, typval_T *rettv);
149static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
150static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200151static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200152static void f_function(typval_T *argvars, typval_T *rettv);
153static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
154static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200155static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200156static void f_getbufline(typval_T *argvars, typval_T *rettv);
157static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100158static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200159static void f_getchar(typval_T *argvars, typval_T *rettv);
160static void f_getcharmod(typval_T *argvars, typval_T *rettv);
161static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
162static void f_getcmdline(typval_T *argvars, typval_T *rettv);
163#if defined(FEAT_CMDL_COMPL)
164static void f_getcompletion(typval_T *argvars, typval_T *rettv);
165#endif
166static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
167static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
168static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
169static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200170static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200171static void f_getfontname(typval_T *argvars, typval_T *rettv);
172static void f_getfperm(typval_T *argvars, typval_T *rettv);
173static void f_getfsize(typval_T *argvars, typval_T *rettv);
174static void f_getftime(typval_T *argvars, typval_T *rettv);
175static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100176static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200178static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179static void f_getmatches(typval_T *argvars, typval_T *rettv);
180static void f_getpid(typval_T *argvars, typval_T *rettv);
181static void f_getcurpos(typval_T *argvars, typval_T *rettv);
182static void f_getpos(typval_T *argvars, typval_T *rettv);
183static void f_getqflist(typval_T *argvars, typval_T *rettv);
184static void f_getreg(typval_T *argvars, typval_T *rettv);
185static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200186static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200187static void f_gettabvar(typval_T *argvars, typval_T *rettv);
188static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100189static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200190static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100191static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getwinposx(typval_T *argvars, typval_T *rettv);
193static void f_getwinposy(typval_T *argvars, typval_T *rettv);
194static void f_getwinvar(typval_T *argvars, typval_T *rettv);
195static void f_glob(typval_T *argvars, typval_T *rettv);
196static void f_globpath(typval_T *argvars, typval_T *rettv);
197static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
198static void f_has(typval_T *argvars, typval_T *rettv);
199static void f_has_key(typval_T *argvars, typval_T *rettv);
200static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
201static void f_hasmapto(typval_T *argvars, typval_T *rettv);
202static void f_histadd(typval_T *argvars, typval_T *rettv);
203static void f_histdel(typval_T *argvars, typval_T *rettv);
204static void f_histget(typval_T *argvars, typval_T *rettv);
205static void f_histnr(typval_T *argvars, typval_T *rettv);
206static void f_hlID(typval_T *argvars, typval_T *rettv);
207static void f_hlexists(typval_T *argvars, typval_T *rettv);
208static void f_hostname(typval_T *argvars, typval_T *rettv);
209static void f_iconv(typval_T *argvars, typval_T *rettv);
210static void f_indent(typval_T *argvars, typval_T *rettv);
211static void f_index(typval_T *argvars, typval_T *rettv);
212static void f_input(typval_T *argvars, typval_T *rettv);
213static void f_inputdialog(typval_T *argvars, typval_T *rettv);
214static void f_inputlist(typval_T *argvars, typval_T *rettv);
215static void f_inputrestore(typval_T *argvars, typval_T *rettv);
216static void f_inputsave(typval_T *argvars, typval_T *rettv);
217static void f_inputsecret(typval_T *argvars, typval_T *rettv);
218static void f_insert(typval_T *argvars, typval_T *rettv);
219static void f_invert(typval_T *argvars, typval_T *rettv);
220static void f_isdirectory(typval_T *argvars, typval_T *rettv);
221static void f_islocked(typval_T *argvars, typval_T *rettv);
222#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200223static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200224static void f_isnan(typval_T *argvars, typval_T *rettv);
225#endif
226static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200227static void f_join(typval_T *argvars, typval_T *rettv);
228static void f_js_decode(typval_T *argvars, typval_T *rettv);
229static void f_js_encode(typval_T *argvars, typval_T *rettv);
230static void f_json_decode(typval_T *argvars, typval_T *rettv);
231static void f_json_encode(typval_T *argvars, typval_T *rettv);
232static void f_keys(typval_T *argvars, typval_T *rettv);
233static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
234static void f_len(typval_T *argvars, typval_T *rettv);
235static void f_libcall(typval_T *argvars, typval_T *rettv);
236static void f_libcallnr(typval_T *argvars, typval_T *rettv);
237static void f_line(typval_T *argvars, typval_T *rettv);
238static void f_line2byte(typval_T *argvars, typval_T *rettv);
239static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200240static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200241static void f_localtime(typval_T *argvars, typval_T *rettv);
242#ifdef FEAT_FLOAT
243static void f_log(typval_T *argvars, typval_T *rettv);
244static void f_log10(typval_T *argvars, typval_T *rettv);
245#endif
246#ifdef FEAT_LUA
247static void f_luaeval(typval_T *argvars, typval_T *rettv);
248#endif
249static void f_map(typval_T *argvars, typval_T *rettv);
250static void f_maparg(typval_T *argvars, typval_T *rettv);
251static void f_mapcheck(typval_T *argvars, typval_T *rettv);
252static void f_match(typval_T *argvars, typval_T *rettv);
253static void f_matchadd(typval_T *argvars, typval_T *rettv);
254static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
255static void f_matcharg(typval_T *argvars, typval_T *rettv);
256static void f_matchdelete(typval_T *argvars, typval_T *rettv);
257static void f_matchend(typval_T *argvars, typval_T *rettv);
258static void f_matchlist(typval_T *argvars, typval_T *rettv);
259static void f_matchstr(typval_T *argvars, typval_T *rettv);
260static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
261static void f_max(typval_T *argvars, typval_T *rettv);
262static void f_min(typval_T *argvars, typval_T *rettv);
263#ifdef vim_mkdir
264static void f_mkdir(typval_T *argvars, typval_T *rettv);
265#endif
266static void f_mode(typval_T *argvars, typval_T *rettv);
267#ifdef FEAT_MZSCHEME
268static void f_mzeval(typval_T *argvars, typval_T *rettv);
269#endif
270static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
271static void f_nr2char(typval_T *argvars, typval_T *rettv);
272static void f_or(typval_T *argvars, typval_T *rettv);
273static void f_pathshorten(typval_T *argvars, typval_T *rettv);
274#ifdef FEAT_PERL
275static void f_perleval(typval_T *argvars, typval_T *rettv);
276#endif
277#ifdef FEAT_FLOAT
278static void f_pow(typval_T *argvars, typval_T *rettv);
279#endif
280static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
281static void f_printf(typval_T *argvars, typval_T *rettv);
282static void f_pumvisible(typval_T *argvars, typval_T *rettv);
283#ifdef FEAT_PYTHON3
284static void f_py3eval(typval_T *argvars, typval_T *rettv);
285#endif
286#ifdef FEAT_PYTHON
287static void f_pyeval(typval_T *argvars, typval_T *rettv);
288#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100289#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
290static void f_pyxeval(typval_T *argvars, typval_T *rettv);
291#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200292static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200293static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200294static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200295static void f_reg_executing(typval_T *argvars, typval_T *rettv);
296static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200297static void f_reltime(typval_T *argvars, typval_T *rettv);
298#ifdef FEAT_FLOAT
299static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
300#endif
301static void f_reltimestr(typval_T *argvars, typval_T *rettv);
302static void f_remote_expr(typval_T *argvars, typval_T *rettv);
303static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
304static void f_remote_peek(typval_T *argvars, typval_T *rettv);
305static void f_remote_read(typval_T *argvars, typval_T *rettv);
306static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100307static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200308static void f_remove(typval_T *argvars, typval_T *rettv);
309static void f_rename(typval_T *argvars, typval_T *rettv);
310static void f_repeat(typval_T *argvars, typval_T *rettv);
311static void f_resolve(typval_T *argvars, typval_T *rettv);
312static void f_reverse(typval_T *argvars, typval_T *rettv);
313#ifdef FEAT_FLOAT
314static void f_round(typval_T *argvars, typval_T *rettv);
315#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100316#ifdef FEAT_RUBY
317static void f_rubyeval(typval_T *argvars, typval_T *rettv);
318#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200319static void f_screenattr(typval_T *argvars, typval_T *rettv);
320static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100321static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200322static void f_screencol(typval_T *argvars, typval_T *rettv);
323static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100324static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200325static void f_search(typval_T *argvars, typval_T *rettv);
326static void f_searchdecl(typval_T *argvars, typval_T *rettv);
327static void f_searchpair(typval_T *argvars, typval_T *rettv);
328static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
329static void f_searchpos(typval_T *argvars, typval_T *rettv);
330static void f_server2client(typval_T *argvars, typval_T *rettv);
331static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200332static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200333static void f_setbufvar(typval_T *argvars, typval_T *rettv);
334static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
335static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200336static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200337static void f_setfperm(typval_T *argvars, typval_T *rettv);
338static void f_setline(typval_T *argvars, typval_T *rettv);
339static void f_setloclist(typval_T *argvars, typval_T *rettv);
340static void f_setmatches(typval_T *argvars, typval_T *rettv);
341static void f_setpos(typval_T *argvars, typval_T *rettv);
342static void f_setqflist(typval_T *argvars, typval_T *rettv);
343static void f_setreg(typval_T *argvars, typval_T *rettv);
344static void f_settabvar(typval_T *argvars, typval_T *rettv);
345static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100346static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200347static void f_setwinvar(typval_T *argvars, typval_T *rettv);
348#ifdef FEAT_CRYPT
349static void f_sha256(typval_T *argvars, typval_T *rettv);
350#endif /* FEAT_CRYPT */
351static void f_shellescape(typval_T *argvars, typval_T *rettv);
352static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
353static void f_simplify(typval_T *argvars, typval_T *rettv);
354#ifdef FEAT_FLOAT
355static void f_sin(typval_T *argvars, typval_T *rettv);
356static void f_sinh(typval_T *argvars, typval_T *rettv);
357#endif
358static void f_sort(typval_T *argvars, typval_T *rettv);
359static void f_soundfold(typval_T *argvars, typval_T *rettv);
360static void f_spellbadword(typval_T *argvars, typval_T *rettv);
361static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
362static void f_split(typval_T *argvars, typval_T *rettv);
363#ifdef FEAT_FLOAT
364static void f_sqrt(typval_T *argvars, typval_T *rettv);
365static void f_str2float(typval_T *argvars, typval_T *rettv);
366#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200367static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200368static void f_str2nr(typval_T *argvars, typval_T *rettv);
369static void f_strchars(typval_T *argvars, typval_T *rettv);
370#ifdef HAVE_STRFTIME
371static void f_strftime(typval_T *argvars, typval_T *rettv);
372#endif
373static void f_strgetchar(typval_T *argvars, typval_T *rettv);
374static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200375static void f_strlen(typval_T *argvars, typval_T *rettv);
376static void f_strcharpart(typval_T *argvars, typval_T *rettv);
377static void f_strpart(typval_T *argvars, typval_T *rettv);
378static void f_strridx(typval_T *argvars, typval_T *rettv);
379static void f_strtrans(typval_T *argvars, typval_T *rettv);
380static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
381static void f_strwidth(typval_T *argvars, typval_T *rettv);
382static void f_submatch(typval_T *argvars, typval_T *rettv);
383static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200384static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200385static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200386static void f_synID(typval_T *argvars, typval_T *rettv);
387static void f_synIDattr(typval_T *argvars, typval_T *rettv);
388static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
389static void f_synstack(typval_T *argvars, typval_T *rettv);
390static void f_synconcealed(typval_T *argvars, typval_T *rettv);
391static void f_system(typval_T *argvars, typval_T *rettv);
392static void f_systemlist(typval_T *argvars, typval_T *rettv);
393static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
394static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
395static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
396static void f_taglist(typval_T *argvars, typval_T *rettv);
397static void f_tagfiles(typval_T *argvars, typval_T *rettv);
398static void f_tempname(typval_T *argvars, typval_T *rettv);
399static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
400static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200401static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareda65222019-05-16 20:29:44 +0200402static void f_test_getvalue(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200403static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100404static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100405static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200406static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaaradc67142019-06-22 01:40:42 +0200407static void f_test_garbagecollect_soon(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100408static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100409static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200410#ifdef FEAT_JOB_CHANNEL
411static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
412#endif
413static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
414#ifdef FEAT_JOB_CHANNEL
415static void f_test_null_job(typval_T *argvars, typval_T *rettv);
416#endif
417static void f_test_null_list(typval_T *argvars, typval_T *rettv);
418static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
419static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200420#ifdef FEAT_GUI
421static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
422#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200423#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200424static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200425#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200426static void f_test_settime(typval_T *argvars, typval_T *rettv);
427#ifdef FEAT_FLOAT
428static void f_tan(typval_T *argvars, typval_T *rettv);
429static void f_tanh(typval_T *argvars, typval_T *rettv);
430#endif
431#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200432static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200433static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200434static void f_timer_start(typval_T *argvars, typval_T *rettv);
435static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200436static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200437#endif
438static void f_tolower(typval_T *argvars, typval_T *rettv);
439static void f_toupper(typval_T *argvars, typval_T *rettv);
440static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100441static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200442#ifdef FEAT_FLOAT
443static void f_trunc(typval_T *argvars, typval_T *rettv);
444#endif
445static void f_type(typval_T *argvars, typval_T *rettv);
446static void f_undofile(typval_T *argvars, typval_T *rettv);
447static void f_undotree(typval_T *argvars, typval_T *rettv);
448static void f_uniq(typval_T *argvars, typval_T *rettv);
449static void f_values(typval_T *argvars, typval_T *rettv);
450static void f_virtcol(typval_T *argvars, typval_T *rettv);
451static void f_visualmode(typval_T *argvars, typval_T *rettv);
452static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200453static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200454static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
455static void f_win_getid(typval_T *argvars, typval_T *rettv);
456static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
457static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
458static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100459static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200460static void f_winbufnr(typval_T *argvars, typval_T *rettv);
461static void f_wincol(typval_T *argvars, typval_T *rettv);
462static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200463static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200464static void f_winline(typval_T *argvars, typval_T *rettv);
465static void f_winnr(typval_T *argvars, typval_T *rettv);
466static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
467static void f_winrestview(typval_T *argvars, typval_T *rettv);
468static void f_winsaveview(typval_T *argvars, typval_T *rettv);
469static void f_winwidth(typval_T *argvars, typval_T *rettv);
470static void f_writefile(typval_T *argvars, typval_T *rettv);
471static void f_wordcount(typval_T *argvars, typval_T *rettv);
472static void f_xor(typval_T *argvars, typval_T *rettv);
473
474/*
475 * Array with names and number of arguments of all internal functions
476 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
477 */
478static struct fst
479{
480 char *f_name; /* function name */
481 char f_min_argc; /* minimal number of arguments */
482 char f_max_argc; /* maximal number of arguments */
483 void (*f_func)(typval_T *args, typval_T *rvar);
484 /* implementation of function */
485} functions[] =
486{
487#ifdef FEAT_FLOAT
488 {"abs", 1, 1, f_abs},
489 {"acos", 1, 1, f_acos}, /* WJMc */
490#endif
491 {"add", 2, 2, f_add},
492 {"and", 2, 2, f_and},
493 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200494 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200495 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200496 {"argidx", 0, 0, f_argidx},
497 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200498 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200499#ifdef FEAT_FLOAT
500 {"asin", 1, 1, f_asin}, /* WJMc */
501#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100502 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200503 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100504 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200506 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100508 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200509 {"assert_match", 2, 3, f_assert_match},
510 {"assert_notequal", 2, 3, f_assert_notequal},
511 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100512 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200513 {"assert_true", 1, 2, f_assert_true},
514#ifdef FEAT_FLOAT
515 {"atan", 1, 1, f_atan},
516 {"atan2", 2, 2, f_atan2},
517#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100518#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200519 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100520 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100521# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100522 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100523# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100524#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200525 {"browse", 4, 4, f_browse},
526 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200527 {"bufadd", 1, 1, f_bufadd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200528 {"bufexists", 1, 1, f_bufexists},
529 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
530 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
531 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
532 {"buflisted", 1, 1, f_buflisted},
Bram Moolenaar15e248e2019-06-30 20:21:37 +0200533 {"bufload", 1, 1, f_bufload},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200534 {"bufloaded", 1, 1, f_bufloaded},
535 {"bufname", 1, 1, f_bufname},
536 {"bufnr", 1, 2, f_bufnr},
537 {"bufwinid", 1, 1, f_bufwinid},
538 {"bufwinnr", 1, 1, f_bufwinnr},
539 {"byte2line", 1, 1, f_byte2line},
540 {"byteidx", 2, 2, f_byteidx},
541 {"byteidxcomp", 2, 2, f_byteidxcomp},
542 {"call", 2, 3, f_call},
543#ifdef FEAT_FLOAT
544 {"ceil", 1, 1, f_ceil},
545#endif
546#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100547 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200549 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
551 {"ch_evalraw", 2, 3, f_ch_evalraw},
552 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
553 {"ch_getjob", 1, 1, f_ch_getjob},
554 {"ch_info", 1, 1, f_ch_info},
555 {"ch_log", 1, 2, f_ch_log},
556 {"ch_logfile", 1, 2, f_ch_logfile},
557 {"ch_open", 1, 2, f_ch_open},
558 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100559 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200560 {"ch_readraw", 1, 2, f_ch_readraw},
561 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
562 {"ch_sendraw", 2, 3, f_ch_sendraw},
563 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200564 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200565#endif
566 {"changenr", 0, 0, f_changenr},
567 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200568 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200569 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100570 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200571 {"col", 1, 1, f_col},
572#if defined(FEAT_INS_EXPAND)
573 {"complete", 2, 2, f_complete},
574 {"complete_add", 1, 1, f_complete_add},
575 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100576 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200577#endif
578 {"confirm", 1, 4, f_confirm},
579 {"copy", 1, 1, f_copy},
580#ifdef FEAT_FLOAT
581 {"cos", 1, 1, f_cos},
582 {"cosh", 1, 1, f_cosh},
583#endif
584 {"count", 2, 4, f_count},
585 {"cscope_connection",0,3, f_cscope_connection},
586 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100587#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200588 {"debugbreak", 1, 1, f_debugbreak},
589#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200590 {"deepcopy", 1, 2, f_deepcopy},
591 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200592 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200593 {"did_filetype", 0, 0, f_did_filetype},
594 {"diff_filler", 1, 1, f_diff_filler},
595 {"diff_hlID", 2, 2, f_diff_hlID},
596 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200597 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200598 {"escape", 2, 2, f_escape},
599 {"eval", 1, 1, f_eval},
600 {"eventhandler", 0, 0, f_eventhandler},
601 {"executable", 1, 1, f_executable},
602 {"execute", 1, 2, f_execute},
603 {"exepath", 1, 1, f_exepath},
604 {"exists", 1, 1, f_exists},
605#ifdef FEAT_FLOAT
606 {"exp", 1, 1, f_exp},
607#endif
608 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200609 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610 {"extend", 2, 3, f_extend},
611 {"feedkeys", 1, 2, f_feedkeys},
612 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
613 {"filereadable", 1, 1, f_filereadable},
614 {"filewritable", 1, 1, f_filewritable},
615 {"filter", 2, 2, f_filter},
616 {"finddir", 1, 3, f_finddir},
617 {"findfile", 1, 3, f_findfile},
618#ifdef FEAT_FLOAT
619 {"float2nr", 1, 1, f_float2nr},
620 {"floor", 1, 1, f_floor},
621 {"fmod", 2, 2, f_fmod},
622#endif
623 {"fnameescape", 1, 1, f_fnameescape},
624 {"fnamemodify", 2, 2, f_fnamemodify},
625 {"foldclosed", 1, 1, f_foldclosed},
626 {"foldclosedend", 1, 1, f_foldclosedend},
627 {"foldlevel", 1, 1, f_foldlevel},
628 {"foldtext", 0, 0, f_foldtext},
629 {"foldtextresult", 1, 1, f_foldtextresult},
630 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200631 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"function", 1, 3, f_function},
633 {"garbagecollect", 0, 1, f_garbagecollect},
634 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200635 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636 {"getbufline", 2, 3, f_getbufline},
637 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100638 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"getchar", 0, 1, f_getchar},
640 {"getcharmod", 0, 0, f_getcharmod},
641 {"getcharsearch", 0, 0, f_getcharsearch},
642 {"getcmdline", 0, 0, f_getcmdline},
643 {"getcmdpos", 0, 0, f_getcmdpos},
644 {"getcmdtype", 0, 0, f_getcmdtype},
645 {"getcmdwintype", 0, 0, f_getcmdwintype},
646#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200647 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200648#endif
649 {"getcurpos", 0, 0, f_getcurpos},
650 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200651 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200652 {"getfontname", 0, 1, f_getfontname},
653 {"getfperm", 1, 1, f_getfperm},
654 {"getfsize", 1, 1, f_getfsize},
655 {"getftime", 1, 1, f_getftime},
656 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100657 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200658 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200659 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100660 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200661 {"getpid", 0, 0, f_getpid},
662 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200663 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200664 {"getreg", 0, 3, f_getreg},
665 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200666 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200667 {"gettabvar", 2, 3, f_gettabvar},
668 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100669 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200670 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100671 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200672 {"getwinposx", 0, 0, f_getwinposx},
673 {"getwinposy", 0, 0, f_getwinposy},
674 {"getwinvar", 2, 3, f_getwinvar},
675 {"glob", 1, 4, f_glob},
676 {"glob2regpat", 1, 1, f_glob2regpat},
677 {"globpath", 2, 5, f_globpath},
678 {"has", 1, 1, f_has},
679 {"has_key", 2, 2, f_has_key},
680 {"haslocaldir", 0, 2, f_haslocaldir},
681 {"hasmapto", 1, 3, f_hasmapto},
682 {"highlightID", 1, 1, f_hlID}, /* obsolete */
683 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
684 {"histadd", 2, 2, f_histadd},
685 {"histdel", 1, 2, f_histdel},
686 {"histget", 1, 2, f_histget},
687 {"histnr", 1, 1, f_histnr},
688 {"hlID", 1, 1, f_hlID},
689 {"hlexists", 1, 1, f_hlexists},
690 {"hostname", 0, 0, f_hostname},
691 {"iconv", 3, 3, f_iconv},
692 {"indent", 1, 1, f_indent},
693 {"index", 2, 4, f_index},
694 {"input", 1, 3, f_input},
695 {"inputdialog", 1, 3, f_inputdialog},
696 {"inputlist", 1, 1, f_inputlist},
697 {"inputrestore", 0, 0, f_inputrestore},
698 {"inputsave", 0, 0, f_inputsave},
699 {"inputsecret", 1, 2, f_inputsecret},
700 {"insert", 2, 3, f_insert},
701 {"invert", 1, 1, f_invert},
702 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200703#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
704 {"isinf", 1, 1, f_isinf},
705#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200706 {"islocked", 1, 1, f_islocked},
707#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
708 {"isnan", 1, 1, f_isnan},
709#endif
710 {"items", 1, 1, f_items},
711#ifdef FEAT_JOB_CHANNEL
712 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200713 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200714 {"job_setoptions", 2, 2, f_job_setoptions},
715 {"job_start", 1, 2, f_job_start},
716 {"job_status", 1, 1, f_job_status},
717 {"job_stop", 1, 2, f_job_stop},
718#endif
719 {"join", 1, 2, f_join},
720 {"js_decode", 1, 1, f_js_decode},
721 {"js_encode", 1, 1, f_js_encode},
722 {"json_decode", 1, 1, f_json_decode},
723 {"json_encode", 1, 1, f_json_encode},
724 {"keys", 1, 1, f_keys},
725 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
726 {"len", 1, 1, f_len},
727 {"libcall", 3, 3, f_libcall},
728 {"libcallnr", 3, 3, f_libcallnr},
729 {"line", 1, 1, f_line},
730 {"line2byte", 1, 1, f_line2byte},
731 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200732 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200733 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200734 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200735 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200736 {"localtime", 0, 0, f_localtime},
737#ifdef FEAT_FLOAT
738 {"log", 1, 1, f_log},
739 {"log10", 1, 1, f_log10},
740#endif
741#ifdef FEAT_LUA
742 {"luaeval", 1, 2, f_luaeval},
743#endif
744 {"map", 2, 2, f_map},
745 {"maparg", 1, 4, f_maparg},
746 {"mapcheck", 1, 3, f_mapcheck},
747 {"match", 2, 4, f_match},
748 {"matchadd", 2, 5, f_matchadd},
749 {"matchaddpos", 2, 5, f_matchaddpos},
750 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100751 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200752 {"matchend", 2, 4, f_matchend},
753 {"matchlist", 2, 4, f_matchlist},
754 {"matchstr", 2, 4, f_matchstr},
755 {"matchstrpos", 2, 4, f_matchstrpos},
756 {"max", 1, 1, f_max},
757 {"min", 1, 1, f_min},
758#ifdef vim_mkdir
759 {"mkdir", 1, 3, f_mkdir},
760#endif
761 {"mode", 0, 1, f_mode},
762#ifdef FEAT_MZSCHEME
763 {"mzeval", 1, 1, f_mzeval},
764#endif
765 {"nextnonblank", 1, 1, f_nextnonblank},
766 {"nr2char", 1, 2, f_nr2char},
767 {"or", 2, 2, f_or},
768 {"pathshorten", 1, 1, f_pathshorten},
769#ifdef FEAT_PERL
770 {"perleval", 1, 1, f_perleval},
771#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200772#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200773 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200774 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200775 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200776 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200777 {"popup_dialog", 2, 2, f_popup_dialog},
Bram Moolenaara730e552019-06-16 19:05:31 +0200778 {"popup_filter_menu", 2, 2, f_popup_filter_menu},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200779 {"popup_filter_yesno", 2, 2, f_popup_filter_yesno},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200780 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200781 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200782 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaara730e552019-06-16 19:05:31 +0200783 {"popup_menu", 2, 2, f_popup_menu},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200784 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200785 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaarae943152019-06-16 22:54:14 +0200786 {"popup_setoptions", 2, 2, f_popup_setoptions},
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200787 {"popup_settext", 2, 2, f_popup_settext},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200788 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200789#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200790#ifdef FEAT_FLOAT
791 {"pow", 2, 2, f_pow},
792#endif
793 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100794 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200795#ifdef FEAT_JOB_CHANNEL
796 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200797 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200798 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
799#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100800#ifdef FEAT_TEXT_PROP
801 {"prop_add", 3, 3, f_prop_add},
802 {"prop_clear", 1, 3, f_prop_clear},
803 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100804 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100805 {"prop_type_add", 2, 2, f_prop_type_add},
806 {"prop_type_change", 2, 2, f_prop_type_change},
807 {"prop_type_delete", 1, 2, f_prop_type_delete},
808 {"prop_type_get", 1, 2, f_prop_type_get},
809 {"prop_type_list", 0, 1, f_prop_type_list},
810#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200811 {"pumvisible", 0, 0, f_pumvisible},
812#ifdef FEAT_PYTHON3
813 {"py3eval", 1, 1, f_py3eval},
814#endif
815#ifdef FEAT_PYTHON
816 {"pyeval", 1, 1, f_pyeval},
817#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100818#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
819 {"pyxeval", 1, 1, f_pyxeval},
820#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200821 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200822 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200823 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200824 {"reg_executing", 0, 0, f_reg_executing},
825 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200826 {"reltime", 0, 2, f_reltime},
827#ifdef FEAT_FLOAT
828 {"reltimefloat", 1, 1, f_reltimefloat},
829#endif
830 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100831 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200832 {"remote_foreground", 1, 1, f_remote_foreground},
833 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100834 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200835 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100836 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200837 {"remove", 2, 3, f_remove},
838 {"rename", 2, 2, f_rename},
839 {"repeat", 2, 2, f_repeat},
840 {"resolve", 1, 1, f_resolve},
841 {"reverse", 1, 1, f_reverse},
842#ifdef FEAT_FLOAT
843 {"round", 1, 1, f_round},
844#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100845#ifdef FEAT_RUBY
846 {"rubyeval", 1, 1, f_rubyeval},
847#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200848 {"screenattr", 2, 2, f_screenattr},
849 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100850 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200851 {"screencol", 0, 0, f_screencol},
852 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100853 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200854 {"search", 1, 4, f_search},
855 {"searchdecl", 1, 3, f_searchdecl},
856 {"searchpair", 3, 7, f_searchpair},
857 {"searchpairpos", 3, 7, f_searchpairpos},
858 {"searchpos", 1, 4, f_searchpos},
859 {"server2client", 2, 2, f_server2client},
860 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200861 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200862 {"setbufvar", 3, 3, f_setbufvar},
863 {"setcharsearch", 1, 1, f_setcharsearch},
864 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200865 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200866 {"setfperm", 2, 2, f_setfperm},
867 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200868 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100869 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200870 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200871 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200872 {"setreg", 2, 3, f_setreg},
873 {"settabvar", 3, 3, f_settabvar},
874 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100875 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200876 {"setwinvar", 3, 3, f_setwinvar},
877#ifdef FEAT_CRYPT
878 {"sha256", 1, 1, f_sha256},
879#endif
880 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100881 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100882#ifdef FEAT_SIGNS
883 {"sign_define", 1, 2, f_sign_define},
884 {"sign_getdefined", 0, 1, f_sign_getdefined},
885 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100886 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100887 {"sign_place", 4, 5, f_sign_place},
888 {"sign_undefine", 0, 1, f_sign_undefine},
889 {"sign_unplace", 1, 2, f_sign_unplace},
890#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200891 {"simplify", 1, 1, f_simplify},
892#ifdef FEAT_FLOAT
893 {"sin", 1, 1, f_sin},
894 {"sinh", 1, 1, f_sinh},
895#endif
896 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200897#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200898 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200899 {"sound_playevent", 1, 2, f_sound_playevent},
900 {"sound_playfile", 1, 2, f_sound_playfile},
901 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200902#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200903 {"soundfold", 1, 1, f_soundfold},
904 {"spellbadword", 0, 1, f_spellbadword},
905 {"spellsuggest", 1, 3, f_spellsuggest},
906 {"split", 1, 3, f_split},
907#ifdef FEAT_FLOAT
908 {"sqrt", 1, 1, f_sqrt},
909 {"str2float", 1, 1, f_str2float},
910#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200911 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200912 {"str2nr", 1, 2, f_str2nr},
913 {"strcharpart", 2, 3, f_strcharpart},
914 {"strchars", 1, 2, f_strchars},
915 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
916#ifdef HAVE_STRFTIME
917 {"strftime", 1, 2, f_strftime},
918#endif
919 {"strgetchar", 2, 2, f_strgetchar},
920 {"stridx", 2, 3, f_stridx},
921 {"string", 1, 1, f_string},
922 {"strlen", 1, 1, f_strlen},
923 {"strpart", 2, 3, f_strpart},
924 {"strridx", 2, 3, f_strridx},
925 {"strtrans", 1, 1, f_strtrans},
926 {"strwidth", 1, 1, f_strwidth},
927 {"submatch", 1, 2, f_submatch},
928 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200929 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200930 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200931 {"synID", 3, 3, f_synID},
932 {"synIDattr", 2, 3, f_synIDattr},
933 {"synIDtrans", 1, 1, f_synIDtrans},
934 {"synconcealed", 2, 2, f_synconcealed},
935 {"synstack", 2, 2, f_synstack},
936 {"system", 1, 2, f_system},
937 {"systemlist", 1, 2, f_systemlist},
938 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
939 {"tabpagenr", 0, 1, f_tabpagenr},
940 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
941 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100942 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200943#ifdef FEAT_FLOAT
944 {"tan", 1, 1, f_tan},
945 {"tanh", 1, 1, f_tanh},
946#endif
947 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200948#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100949 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
950 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100951 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200952 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200953# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
954 {"term_getansicolors", 1, 1, f_term_getansicolors},
955# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200956 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200957 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200958 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200959 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200960 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200961 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200962 {"term_getstatus", 1, 1, f_term_getstatus},
963 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200964 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200965 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200966 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200967 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200968# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
969 {"term_setansicolors", 2, 2, f_term_setansicolors},
970# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100971 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100972 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200973 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200974 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200975 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200976#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200977 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
978 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200979 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200980 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaaradc67142019-06-22 01:40:42 +0200981 {"test_garbagecollect_soon", 0, 0, f_test_garbagecollect_soon},
Bram Moolenaareda65222019-05-16 20:29:44 +0200982 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100983 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100984 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200985#ifdef FEAT_JOB_CHANNEL
986 {"test_null_channel", 0, 0, f_test_null_channel},
987#endif
988 {"test_null_dict", 0, 0, f_test_null_dict},
989#ifdef FEAT_JOB_CHANNEL
990 {"test_null_job", 0, 0, f_test_null_job},
991#endif
992 {"test_null_list", 0, 0, f_test_null_list},
993 {"test_null_partial", 0, 0, f_test_null_partial},
994 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200995 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100996 {"test_override", 2, 2, f_test_override},
997 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200998#ifdef FEAT_GUI
999 {"test_scrollbar", 3, 3, f_test_scrollbar},
1000#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001001#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001002 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001003#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001004 {"test_settime", 1, 1, f_test_settime},
1005#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001006 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001007 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001008 {"timer_start", 2, 3, f_timer_start},
1009 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001010 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001011#endif
1012 {"tolower", 1, 1, f_tolower},
1013 {"toupper", 1, 1, f_toupper},
1014 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001015 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001016#ifdef FEAT_FLOAT
1017 {"trunc", 1, 1, f_trunc},
1018#endif
1019 {"type", 1, 1, f_type},
1020 {"undofile", 1, 1, f_undofile},
1021 {"undotree", 0, 0, f_undotree},
1022 {"uniq", 1, 3, f_uniq},
1023 {"values", 1, 1, f_values},
1024 {"virtcol", 1, 1, f_virtcol},
1025 {"visualmode", 0, 1, f_visualmode},
1026 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001027 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001028 {"win_findbuf", 1, 1, f_win_findbuf},
1029 {"win_getid", 0, 2, f_win_getid},
1030 {"win_gotoid", 1, 1, f_win_gotoid},
1031 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1032 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001033 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001034 {"winbufnr", 1, 1, f_winbufnr},
1035 {"wincol", 0, 0, f_wincol},
1036 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001037 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001038 {"winline", 0, 0, f_winline},
1039 {"winnr", 0, 1, f_winnr},
1040 {"winrestcmd", 0, 0, f_winrestcmd},
1041 {"winrestview", 1, 1, f_winrestview},
1042 {"winsaveview", 0, 0, f_winsaveview},
1043 {"winwidth", 1, 1, f_winwidth},
1044 {"wordcount", 0, 0, f_wordcount},
1045 {"writefile", 2, 3, f_writefile},
1046 {"xor", 2, 2, f_xor},
1047};
1048
1049#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1050
1051/*
1052 * Function given to ExpandGeneric() to obtain the list of internal
1053 * or user defined function names.
1054 */
1055 char_u *
1056get_function_name(expand_T *xp, int idx)
1057{
1058 static int intidx = -1;
1059 char_u *name;
1060
1061 if (idx == 0)
1062 intidx = -1;
1063 if (intidx < 0)
1064 {
1065 name = get_user_func_name(xp, idx);
1066 if (name != NULL)
1067 return name;
1068 }
1069 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1070 {
1071 STRCPY(IObuff, functions[intidx].f_name);
1072 STRCAT(IObuff, "(");
1073 if (functions[intidx].f_max_argc == 0)
1074 STRCAT(IObuff, ")");
1075 return IObuff;
1076 }
1077
1078 return NULL;
1079}
1080
1081/*
1082 * Function given to ExpandGeneric() to obtain the list of internal or
1083 * user defined variable or function names.
1084 */
1085 char_u *
1086get_expr_name(expand_T *xp, int idx)
1087{
1088 static int intidx = -1;
1089 char_u *name;
1090
1091 if (idx == 0)
1092 intidx = -1;
1093 if (intidx < 0)
1094 {
1095 name = get_function_name(xp, idx);
1096 if (name != NULL)
1097 return name;
1098 }
1099 return get_user_var_name(xp, ++intidx);
1100}
1101
1102#endif /* FEAT_CMDL_COMPL */
1103
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001104/*
1105 * Find internal function in table above.
1106 * Return index, or -1 if not found
1107 */
1108 int
1109find_internal_func(
1110 char_u *name) /* name of the function */
1111{
1112 int first = 0;
1113 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1114 int cmp;
1115 int x;
1116
1117 /*
1118 * Find the function name in the table. Binary search.
1119 */
1120 while (first <= last)
1121 {
1122 x = first + ((unsigned)(last - first) >> 1);
1123 cmp = STRCMP(name, functions[x].f_name);
1124 if (cmp < 0)
1125 last = x - 1;
1126 else if (cmp > 0)
1127 first = x + 1;
1128 else
1129 return x;
1130 }
1131 return -1;
1132}
1133
1134 int
1135call_internal_func(
1136 char_u *name,
1137 int argcount,
1138 typval_T *argvars,
1139 typval_T *rettv)
1140{
1141 int i;
1142
1143 i = find_internal_func(name);
1144 if (i < 0)
1145 return ERROR_UNKNOWN;
1146 if (argcount < functions[i].f_min_argc)
1147 return ERROR_TOOFEW;
1148 if (argcount > functions[i].f_max_argc)
1149 return ERROR_TOOMANY;
1150 argvars[argcount].v_type = VAR_UNKNOWN;
1151 functions[i].f_func(argvars, rettv);
1152 return ERROR_NONE;
1153}
1154
1155/*
1156 * Return TRUE for a non-zero Number and a non-empty String.
1157 */
1158 static int
1159non_zero_arg(typval_T *argvars)
1160{
1161 return ((argvars[0].v_type == VAR_NUMBER
1162 && argvars[0].vval.v_number != 0)
1163 || (argvars[0].v_type == VAR_SPECIAL
1164 && argvars[0].vval.v_number == VVAL_TRUE)
1165 || (argvars[0].v_type == VAR_STRING
1166 && argvars[0].vval.v_string != NULL
1167 && *argvars[0].vval.v_string != NUL));
1168}
1169
1170/*
1171 * Get the lnum from the first argument.
1172 * Also accepts ".", "$", etc., but that only works for the current buffer.
1173 * Returns -1 on error.
1174 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001175 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001176tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001177{
1178 typval_T rettv;
1179 linenr_T lnum;
1180
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001181 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001182 if (lnum == 0) /* no valid number, try using line() */
1183 {
1184 rettv.v_type = VAR_NUMBER;
1185 f_line(argvars, &rettv);
1186 lnum = (linenr_T)rettv.vval.v_number;
1187 clear_tv(&rettv);
1188 }
1189 return lnum;
1190}
1191
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001192/*
1193 * Get the lnum from the first argument.
1194 * Also accepts "$", then "buf" is used.
1195 * Returns 0 on error.
1196 */
1197 static linenr_T
1198tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1199{
1200 if (argvars[0].v_type == VAR_STRING
1201 && argvars[0].vval.v_string != NULL
1202 && argvars[0].vval.v_string[0] == '$'
1203 && buf != NULL)
1204 return buf->b_ml.ml_line_count;
1205 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1206}
1207
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001208#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001209/*
1210 * Get the float value of "argvars[0]" into "f".
1211 * Returns FAIL when the argument is not a Number or Float.
1212 */
1213 static int
1214get_float_arg(typval_T *argvars, float_T *f)
1215{
1216 if (argvars[0].v_type == VAR_FLOAT)
1217 {
1218 *f = argvars[0].vval.v_float;
1219 return OK;
1220 }
1221 if (argvars[0].v_type == VAR_NUMBER)
1222 {
1223 *f = (float_T)argvars[0].vval.v_number;
1224 return OK;
1225 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001226 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001227 return FAIL;
1228}
1229
1230/*
1231 * "abs(expr)" function
1232 */
1233 static void
1234f_abs(typval_T *argvars, typval_T *rettv)
1235{
1236 if (argvars[0].v_type == VAR_FLOAT)
1237 {
1238 rettv->v_type = VAR_FLOAT;
1239 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1240 }
1241 else
1242 {
1243 varnumber_T n;
1244 int error = FALSE;
1245
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001246 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001247 if (error)
1248 rettv->vval.v_number = -1;
1249 else if (n > 0)
1250 rettv->vval.v_number = n;
1251 else
1252 rettv->vval.v_number = -n;
1253 }
1254}
1255
1256/*
1257 * "acos()" function
1258 */
1259 static void
1260f_acos(typval_T *argvars, typval_T *rettv)
1261{
1262 float_T f = 0.0;
1263
1264 rettv->v_type = VAR_FLOAT;
1265 if (get_float_arg(argvars, &f) == OK)
1266 rettv->vval.v_float = acos(f);
1267 else
1268 rettv->vval.v_float = 0.0;
1269}
1270#endif
1271
1272/*
1273 * "add(list, item)" function
1274 */
1275 static void
1276f_add(typval_T *argvars, typval_T *rettv)
1277{
1278 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001279 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001280
1281 rettv->vval.v_number = 1; /* Default: Failed */
1282 if (argvars[0].v_type == VAR_LIST)
1283 {
1284 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001285 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001286 (char_u *)N_("add() argument"), TRUE)
1287 && list_append_tv(l, &argvars[1]) == OK)
1288 copy_tv(&argvars[0], rettv);
1289 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001290 else if (argvars[0].v_type == VAR_BLOB)
1291 {
1292 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001293 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294 (char_u *)N_("add() argument"), TRUE))
1295 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001296 int error = FALSE;
1297 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1298
1299 if (!error)
1300 {
1301 ga_append(&b->bv_ga, (int)n);
1302 copy_tv(&argvars[0], rettv);
1303 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001304 }
1305 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001306 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001307 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001308}
1309
1310/*
1311 * "and(expr, expr)" function
1312 */
1313 static void
1314f_and(typval_T *argvars, typval_T *rettv)
1315{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001316 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1317 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001318}
1319
1320/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001321 * If there is a window for "curbuf", make it the current window.
1322 */
1323 static void
1324find_win_for_curbuf(void)
1325{
1326 wininfo_T *wip;
1327
1328 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1329 {
1330 if (wip->wi_win != NULL)
1331 {
1332 curwin = wip->wi_win;
1333 break;
1334 }
1335 }
1336}
1337
1338/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001339 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001340 */
1341 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001342set_buffer_lines(
1343 buf_T *buf,
1344 linenr_T lnum_arg,
1345 int append,
1346 typval_T *lines,
1347 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001348{
Bram Moolenaarca851592018-06-06 21:04:07 +02001349 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1350 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001351 list_T *l = NULL;
1352 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001354 linenr_T append_lnum;
1355 buf_T *curbuf_save = NULL;
1356 win_T *curwin_save = NULL;
1357 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358
Bram Moolenaarca851592018-06-06 21:04:07 +02001359 /* When using the current buffer ml_mfp will be set if needed. Useful when
1360 * setline() is used on startup. For other buffers the buffer must be
1361 * loaded. */
1362 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001363 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001364 rettv->vval.v_number = 1; /* FAIL */
1365 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001366 }
1367
Bram Moolenaarca851592018-06-06 21:04:07 +02001368 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001369 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001370 curbuf_save = curbuf;
1371 curwin_save = curwin;
1372 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001373 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001374 }
1375
1376 if (append)
1377 // appendbufline() uses the line number below which we insert
1378 append_lnum = lnum - 1;
1379 else
1380 // setbufline() uses the line number above which we insert, we only
1381 // append if it's below the last line
1382 append_lnum = curbuf->b_ml.ml_line_count;
1383
1384 if (lines->v_type == VAR_LIST)
1385 {
1386 l = lines->vval.v_list;
1387 li = l->lv_first;
1388 }
1389 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001390 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001391
1392 /* default result is zero == OK */
1393 for (;;)
1394 {
1395 if (l != NULL)
1396 {
1397 /* list argument, get next string */
1398 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001399 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001400 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001401 li = li->li_next;
1402 }
1403
Bram Moolenaarca851592018-06-06 21:04:07 +02001404 rettv->vval.v_number = 1; /* FAIL */
1405 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1406 break;
1407
1408 /* When coming here from Insert mode, sync undo, so that this can be
1409 * undone separately from what was previously inserted. */
1410 if (u_sync_once == 2)
1411 {
1412 u_sync_once = 1; /* notify that u_sync() was called */
1413 u_sync(TRUE);
1414 }
1415
1416 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1417 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001418 // Existing line, replace it.
1419 // Removes any existing text properties.
1420 if (u_savesub(lnum) == OK && ml_replace_len(
1421 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001422 {
1423 changed_bytes(lnum, 0);
1424 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1425 check_cursor_col();
1426 rettv->vval.v_number = 0; /* OK */
1427 }
1428 }
1429 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1430 {
1431 /* append the line */
1432 ++added;
1433 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1434 rettv->vval.v_number = 0; /* OK */
1435 }
1436
1437 if (l == NULL) /* only one string argument */
1438 break;
1439 ++lnum;
1440 }
1441
1442 if (added > 0)
1443 {
1444 win_T *wp;
1445 tabpage_T *tp;
1446
1447 appended_lines_mark(append_lnum, added);
1448 FOR_ALL_TAB_WINDOWS(tp, wp)
1449 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1450 wp->w_cursor.lnum += added;
1451 check_cursor_col();
1452
Bram Moolenaarf2732452018-06-03 14:47:35 +02001453#ifdef FEAT_JOB_CHANNEL
1454 if (bt_prompt(curbuf) && (State & INSERT))
1455 // show the line with the prompt
1456 update_topline();
1457#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001458 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001459
1460 if (!is_curbuf)
1461 {
1462 curbuf = curbuf_save;
1463 curwin = curwin_save;
1464 }
1465}
1466
1467/*
1468 * "append(lnum, string/list)" function
1469 */
1470 static void
1471f_append(typval_T *argvars, typval_T *rettv)
1472{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001473 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001474
1475 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1476}
1477
1478/*
1479 * "appendbufline(buf, lnum, string/list)" function
1480 */
1481 static void
1482f_appendbufline(typval_T *argvars, typval_T *rettv)
1483{
1484 linenr_T lnum;
1485 buf_T *buf;
1486
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001487 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001488 if (buf == NULL)
1489 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001490 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001491 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001492 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001493 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1494 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001495}
1496
1497/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001498 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001499 */
1500 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001501f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001502{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001503 win_T *wp;
1504
1505 if (argvars[0].v_type == VAR_UNKNOWN)
1506 // use the current window
1507 rettv->vval.v_number = ARGCOUNT;
1508 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001509 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001510 // use the global argument list
1511 rettv->vval.v_number = GARGCOUNT;
1512 else
1513 {
1514 // use the argument list of the specified window
1515 wp = find_win_by_nr_or_id(&argvars[0]);
1516 if (wp != NULL)
1517 rettv->vval.v_number = WARGCOUNT(wp);
1518 else
1519 rettv->vval.v_number = -1;
1520 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001521}
1522
1523/*
1524 * "argidx()" function
1525 */
1526 static void
1527f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1528{
1529 rettv->vval.v_number = curwin->w_arg_idx;
1530}
1531
1532/*
1533 * "arglistid()" function
1534 */
1535 static void
1536f_arglistid(typval_T *argvars, typval_T *rettv)
1537{
1538 win_T *wp;
1539
1540 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001541 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001542 if (wp != NULL)
1543 rettv->vval.v_number = wp->w_alist->id;
1544}
1545
1546/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001547 * Get the argument list for a given window
1548 */
1549 static void
1550get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1551{
1552 int idx;
1553
1554 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1555 for (idx = 0; idx < argcount; ++idx)
1556 list_append_string(rettv->vval.v_list,
1557 alist_name(&arglist[idx]), -1);
1558}
1559
1560/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561 * "argv(nr)" function
1562 */
1563 static void
1564f_argv(typval_T *argvars, typval_T *rettv)
1565{
1566 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001567 aentry_T *arglist = NULL;
1568 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001569
1570 if (argvars[0].v_type != VAR_UNKNOWN)
1571 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001572 if (argvars[1].v_type == VAR_UNKNOWN)
1573 {
1574 arglist = ARGLIST;
1575 argcount = ARGCOUNT;
1576 }
1577 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001578 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001579 {
1580 arglist = GARGLIST;
1581 argcount = GARGCOUNT;
1582 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001584 {
1585 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1586
1587 if (wp != NULL)
1588 {
1589 /* Use the argument list of the specified window */
1590 arglist = WARGLIST(wp);
1591 argcount = WARGCOUNT(wp);
1592 }
1593 }
1594
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001595 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001596 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001597 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001598 if (arglist != NULL && idx >= 0 && idx < argcount)
1599 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1600 else if (idx == -1)
1601 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001602 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001603 else
1604 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605}
1606
1607/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001608 * "assert_beeps(cmd [, error])" function
1609 */
1610 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001611f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001612{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001613 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001614}
1615
1616/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001617 * "assert_equal(expected, actual[, msg])" function
1618 */
1619 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001620f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001621{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001622 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001623}
1624
1625/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001626 * "assert_equalfile(fname-one, fname-two)" function
1627 */
1628 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001629f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001630{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001631 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001632}
1633
1634/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001635 * "assert_notequal(expected, actual[, msg])" function
1636 */
1637 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001638f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001639{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001640 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641}
1642
1643/*
1644 * "assert_exception(string[, msg])" function
1645 */
1646 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001647f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001648{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001649 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001650}
1651
1652/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001653 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001654 */
1655 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001656f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001658 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659}
1660
1661/*
1662 * "assert_false(actual[, msg])" function
1663 */
1664 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001665f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001666{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001667 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001668}
1669
1670/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001671 * "assert_inrange(lower, upper[, msg])" function
1672 */
1673 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001674f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001675{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001676 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001677}
1678
1679/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001680 * "assert_match(pattern, actual[, msg])" function
1681 */
1682 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001683f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001684{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001685 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001686}
1687
1688/*
1689 * "assert_notmatch(pattern, actual[, msg])" function
1690 */
1691 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001692f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001693{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001694 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001695}
1696
1697/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001698 * "assert_report(msg)" function
1699 */
1700 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001701f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001702{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001703 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001704}
1705
1706/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001707 * "assert_true(actual[, msg])" function
1708 */
1709 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001710f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001711{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001712 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001713}
1714
1715#ifdef FEAT_FLOAT
1716/*
1717 * "asin()" function
1718 */
1719 static void
1720f_asin(typval_T *argvars, typval_T *rettv)
1721{
1722 float_T f = 0.0;
1723
1724 rettv->v_type = VAR_FLOAT;
1725 if (get_float_arg(argvars, &f) == OK)
1726 rettv->vval.v_float = asin(f);
1727 else
1728 rettv->vval.v_float = 0.0;
1729}
1730
1731/*
1732 * "atan()" function
1733 */
1734 static void
1735f_atan(typval_T *argvars, typval_T *rettv)
1736{
1737 float_T f = 0.0;
1738
1739 rettv->v_type = VAR_FLOAT;
1740 if (get_float_arg(argvars, &f) == OK)
1741 rettv->vval.v_float = atan(f);
1742 else
1743 rettv->vval.v_float = 0.0;
1744}
1745
1746/*
1747 * "atan2()" function
1748 */
1749 static void
1750f_atan2(typval_T *argvars, typval_T *rettv)
1751{
1752 float_T fx = 0.0, fy = 0.0;
1753
1754 rettv->v_type = VAR_FLOAT;
1755 if (get_float_arg(argvars, &fx) == OK
1756 && get_float_arg(&argvars[1], &fy) == OK)
1757 rettv->vval.v_float = atan2(fx, fy);
1758 else
1759 rettv->vval.v_float = 0.0;
1760}
1761#endif
1762
1763/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001764 * "balloon_show()" function
1765 */
1766#ifdef FEAT_BEVAL
1767 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001768f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1769{
1770 rettv->v_type = VAR_STRING;
1771 if (balloonEval != NULL)
1772 {
1773 if (balloonEval->msg == NULL)
1774 rettv->vval.v_string = NULL;
1775 else
1776 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1777 }
1778}
1779
1780 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001781f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1782{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001783 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001784 {
1785 if (argvars[0].v_type == VAR_LIST
1786# ifdef FEAT_GUI
1787 && !gui.in_use
1788# endif
1789 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001790 {
1791 list_T *l = argvars[0].vval.v_list;
1792
1793 // empty list removes the balloon
1794 post_balloon(balloonEval, NULL,
1795 l == NULL || l->lv_len == 0 ? NULL : l);
1796 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001797 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001798 {
1799 char_u *mesg = tv_get_string_chk(&argvars[0]);
1800
1801 if (mesg != NULL)
1802 // empty string removes the balloon
1803 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1804 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001805 }
1806}
1807
Bram Moolenaar669a8282017-11-19 20:13:05 +01001808# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001809 static void
1810f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1811{
1812 if (rettv_list_alloc(rettv) == OK)
1813 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001814 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001815
1816 if (msg != NULL)
1817 {
1818 pumitem_T *array;
1819 int size = split_message(msg, &array);
1820 int i;
1821
1822 /* Skip the first and last item, they are always empty. */
1823 for (i = 1; i < size - 1; ++i)
1824 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001825 while (size > 0)
1826 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001827 vim_free(array);
1828 }
1829 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001830}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001831# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001832#endif
1833
1834/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001835 * "browse(save, title, initdir, default)" function
1836 */
1837 static void
1838f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1839{
1840#ifdef FEAT_BROWSE
1841 int save;
1842 char_u *title;
1843 char_u *initdir;
1844 char_u *defname;
1845 char_u buf[NUMBUFLEN];
1846 char_u buf2[NUMBUFLEN];
1847 int error = FALSE;
1848
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001849 save = (int)tv_get_number_chk(&argvars[0], &error);
1850 title = tv_get_string_chk(&argvars[1]);
1851 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1852 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853
1854 if (error || title == NULL || initdir == NULL || defname == NULL)
1855 rettv->vval.v_string = NULL;
1856 else
1857 rettv->vval.v_string =
1858 do_browse(save ? BROWSE_SAVE : 0,
1859 title, defname, NULL, initdir, NULL, curbuf);
1860#else
1861 rettv->vval.v_string = NULL;
1862#endif
1863 rettv->v_type = VAR_STRING;
1864}
1865
1866/*
1867 * "browsedir(title, initdir)" function
1868 */
1869 static void
1870f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1871{
1872#ifdef FEAT_BROWSE
1873 char_u *title;
1874 char_u *initdir;
1875 char_u buf[NUMBUFLEN];
1876
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001877 title = tv_get_string_chk(&argvars[0]);
1878 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001879
1880 if (title == NULL || initdir == NULL)
1881 rettv->vval.v_string = NULL;
1882 else
1883 rettv->vval.v_string = do_browse(BROWSE_DIR,
1884 title, NULL, NULL, initdir, NULL, curbuf);
1885#else
1886 rettv->vval.v_string = NULL;
1887#endif
1888 rettv->v_type = VAR_STRING;
1889}
1890
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001891/*
1892 * Find a buffer by number or exact name.
1893 */
1894 static buf_T *
1895find_buffer(typval_T *avar)
1896{
1897 buf_T *buf = NULL;
1898
1899 if (avar->v_type == VAR_NUMBER)
1900 buf = buflist_findnr((int)avar->vval.v_number);
1901 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1902 {
1903 buf = buflist_findname_exp(avar->vval.v_string);
1904 if (buf == NULL)
1905 {
1906 /* No full path name match, try a match with a URL or a "nofile"
1907 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001908 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001909 if (buf->b_fname != NULL
1910 && (path_with_url(buf->b_fname)
1911#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001912 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913#endif
1914 )
1915 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1916 break;
1917 }
1918 }
1919 return buf;
1920}
1921
1922/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001923 * "bufadd(expr)" function
1924 */
1925 static void
1926f_bufadd(typval_T *argvars, typval_T *rettv)
1927{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001928 char_u *name = tv_get_string(&argvars[0]);
1929
1930 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001931}
1932
1933/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001934 * "bufexists(expr)" function
1935 */
1936 static void
1937f_bufexists(typval_T *argvars, typval_T *rettv)
1938{
1939 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1940}
1941
1942/*
1943 * "buflisted(expr)" function
1944 */
1945 static void
1946f_buflisted(typval_T *argvars, typval_T *rettv)
1947{
1948 buf_T *buf;
1949
1950 buf = find_buffer(&argvars[0]);
1951 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1952}
1953
1954/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001955 * "bufload(expr)" function
1956 */
1957 static void
1958f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1959{
1960 buf_T *buf = get_buf_arg(&argvars[0]);
1961
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001962 if (buf != NULL)
1963 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001964}
1965
1966/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001967 * "bufloaded(expr)" function
1968 */
1969 static void
1970f_bufloaded(typval_T *argvars, typval_T *rettv)
1971{
1972 buf_T *buf;
1973
1974 buf = find_buffer(&argvars[0]);
1975 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1976}
1977
1978 buf_T *
1979buflist_find_by_name(char_u *name, int curtab_only)
1980{
1981 int save_magic;
1982 char_u *save_cpo;
1983 buf_T *buf;
1984
1985 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1986 save_magic = p_magic;
1987 p_magic = TRUE;
1988 save_cpo = p_cpo;
1989 p_cpo = (char_u *)"";
1990
1991 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1992 TRUE, FALSE, curtab_only));
1993
1994 p_magic = save_magic;
1995 p_cpo = save_cpo;
1996 return buf;
1997}
1998
1999/*
2000 * Get buffer by number or pattern.
2001 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002002 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002003tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002004{
2005 char_u *name = tv->vval.v_string;
2006 buf_T *buf;
2007
2008 if (tv->v_type == VAR_NUMBER)
2009 return buflist_findnr((int)tv->vval.v_number);
2010 if (tv->v_type != VAR_STRING)
2011 return NULL;
2012 if (name == NULL || *name == NUL)
2013 return curbuf;
2014 if (name[0] == '$' && name[1] == NUL)
2015 return lastbuf;
2016
2017 buf = buflist_find_by_name(name, curtab_only);
2018
2019 /* If not found, try expanding the name, like done for bufexists(). */
2020 if (buf == NULL)
2021 buf = find_buffer(tv);
2022
2023 return buf;
2024}
2025
2026/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002027 * Get the buffer from "arg" and give an error and return NULL if it is not
2028 * valid.
2029 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002030 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002031get_buf_arg(typval_T *arg)
2032{
2033 buf_T *buf;
2034
2035 ++emsg_off;
2036 buf = tv_get_buf(arg, FALSE);
2037 --emsg_off;
2038 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002039 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002040 return buf;
2041}
2042
2043/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002044 * "bufname(expr)" function
2045 */
2046 static void
2047f_bufname(typval_T *argvars, typval_T *rettv)
2048{
2049 buf_T *buf;
2050
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002051 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002052 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002053 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002054 rettv->v_type = VAR_STRING;
2055 if (buf != NULL && buf->b_fname != NULL)
2056 rettv->vval.v_string = vim_strsave(buf->b_fname);
2057 else
2058 rettv->vval.v_string = NULL;
2059 --emsg_off;
2060}
2061
2062/*
2063 * "bufnr(expr)" function
2064 */
2065 static void
2066f_bufnr(typval_T *argvars, typval_T *rettv)
2067{
2068 buf_T *buf;
2069 int error = FALSE;
2070 char_u *name;
2071
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002072 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002073 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002074 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002075 --emsg_off;
2076
2077 /* If the buffer isn't found and the second argument is not zero create a
2078 * new buffer. */
2079 if (buf == NULL
2080 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002081 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002082 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002083 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002084 && !error)
2085 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2086
2087 if (buf != NULL)
2088 rettv->vval.v_number = buf->b_fnum;
2089 else
2090 rettv->vval.v_number = -1;
2091}
2092
2093 static void
2094buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2095{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002096 win_T *wp;
2097 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002098 buf_T *buf;
2099
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002100 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002101 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002102 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002103 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002104 {
2105 ++winnr;
2106 if (wp->w_buffer == buf)
2107 break;
2108 }
2109 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002110 --emsg_off;
2111}
2112
2113/*
2114 * "bufwinid(nr)" function
2115 */
2116 static void
2117f_bufwinid(typval_T *argvars, typval_T *rettv)
2118{
2119 buf_win_common(argvars, rettv, FALSE);
2120}
2121
2122/*
2123 * "bufwinnr(nr)" function
2124 */
2125 static void
2126f_bufwinnr(typval_T *argvars, typval_T *rettv)
2127{
2128 buf_win_common(argvars, rettv, TRUE);
2129}
2130
2131/*
2132 * "byte2line(byte)" function
2133 */
2134 static void
2135f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2136{
2137#ifndef FEAT_BYTEOFF
2138 rettv->vval.v_number = -1;
2139#else
2140 long boff = 0;
2141
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002142 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002143 if (boff < 0)
2144 rettv->vval.v_number = -1;
2145 else
2146 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2147 (linenr_T)0, &boff);
2148#endif
2149}
2150
2151 static void
2152byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2153{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002155 char_u *str;
2156 varnumber_T idx;
2157
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002158 str = tv_get_string_chk(&argvars[0]);
2159 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002160 rettv->vval.v_number = -1;
2161 if (str == NULL || idx < 0)
2162 return;
2163
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164 t = str;
2165 for ( ; idx > 0; idx--)
2166 {
2167 if (*t == NUL) /* EOL reached */
2168 return;
2169 if (enc_utf8 && comp)
2170 t += utf_ptr2len(t);
2171 else
2172 t += (*mb_ptr2len)(t);
2173 }
2174 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002175}
2176
2177/*
2178 * "byteidx()" function
2179 */
2180 static void
2181f_byteidx(typval_T *argvars, typval_T *rettv)
2182{
2183 byteidx(argvars, rettv, FALSE);
2184}
2185
2186/*
2187 * "byteidxcomp()" function
2188 */
2189 static void
2190f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2191{
2192 byteidx(argvars, rettv, TRUE);
2193}
2194
2195/*
2196 * "call(func, arglist [, dict])" function
2197 */
2198 static void
2199f_call(typval_T *argvars, typval_T *rettv)
2200{
2201 char_u *func;
2202 partial_T *partial = NULL;
2203 dict_T *selfdict = NULL;
2204
2205 if (argvars[1].v_type != VAR_LIST)
2206 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002207 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002208 return;
2209 }
2210 if (argvars[1].vval.v_list == NULL)
2211 return;
2212
2213 if (argvars[0].v_type == VAR_FUNC)
2214 func = argvars[0].vval.v_string;
2215 else if (argvars[0].v_type == VAR_PARTIAL)
2216 {
2217 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002218 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002219 }
2220 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002221 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002222 if (*func == NUL)
2223 return; /* type error or empty name */
2224
2225 if (argvars[2].v_type != VAR_UNKNOWN)
2226 {
2227 if (argvars[2].v_type != VAR_DICT)
2228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002229 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002230 return;
2231 }
2232 selfdict = argvars[2].vval.v_dict;
2233 }
2234
2235 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2236}
2237
2238#ifdef FEAT_FLOAT
2239/*
2240 * "ceil({float})" function
2241 */
2242 static void
2243f_ceil(typval_T *argvars, typval_T *rettv)
2244{
2245 float_T f = 0.0;
2246
2247 rettv->v_type = VAR_FLOAT;
2248 if (get_float_arg(argvars, &f) == OK)
2249 rettv->vval.v_float = ceil(f);
2250 else
2251 rettv->vval.v_float = 0.0;
2252}
2253#endif
2254
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002255/*
2256 * "changenr()" function
2257 */
2258 static void
2259f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2260{
2261 rettv->vval.v_number = curbuf->b_u_seq_cur;
2262}
2263
2264/*
2265 * "char2nr(string)" function
2266 */
2267 static void
2268f_char2nr(typval_T *argvars, typval_T *rettv)
2269{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002270 if (has_mbyte)
2271 {
2272 int utf8 = 0;
2273
2274 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002275 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002276
2277 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002278 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002279 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002280 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002281 }
2282 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002283 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002284}
2285
2286/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002287 * "chdir(dir)" function
2288 */
2289 static void
2290f_chdir(typval_T *argvars, typval_T *rettv)
2291{
2292 char_u *cwd;
2293 cdscope_T scope = CDSCOPE_GLOBAL;
2294
2295 rettv->v_type = VAR_STRING;
2296 rettv->vval.v_string = NULL;
2297
2298 if (argvars[0].v_type != VAR_STRING)
2299 return;
2300
2301 // Return the current directory
2302 cwd = alloc(MAXPATHL);
2303 if (cwd != NULL)
2304 {
2305 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2306 {
2307#ifdef BACKSLASH_IN_FILENAME
2308 slash_adjust(cwd);
2309#endif
2310 rettv->vval.v_string = vim_strsave(cwd);
2311 }
2312 vim_free(cwd);
2313 }
2314
2315 if (curwin->w_localdir != NULL)
2316 scope = CDSCOPE_WINDOW;
2317 else if (curtab->tp_localdir != NULL)
2318 scope = CDSCOPE_TABPAGE;
2319
2320 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2321 // Directory change failed
2322 VIM_CLEAR(rettv->vval.v_string);
2323}
2324
2325/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002326 * "cindent(lnum)" function
2327 */
2328 static void
2329f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2330{
2331#ifdef FEAT_CINDENT
2332 pos_T pos;
2333 linenr_T lnum;
2334
2335 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002336 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002337 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2338 {
2339 curwin->w_cursor.lnum = lnum;
2340 rettv->vval.v_number = get_c_indent();
2341 curwin->w_cursor = pos;
2342 }
2343 else
2344#endif
2345 rettv->vval.v_number = -1;
2346}
2347
Bram Moolenaaraff74912019-03-30 18:11:49 +01002348 static win_T *
2349get_optional_window(typval_T *argvars, int idx)
2350{
2351 win_T *win = curwin;
2352
2353 if (argvars[idx].v_type != VAR_UNKNOWN)
2354 {
2355 win = find_win_by_nr_or_id(&argvars[idx]);
2356 if (win == NULL)
2357 {
2358 emsg(_(e_invalwindow));
2359 return NULL;
2360 }
2361 }
2362 return win;
2363}
2364
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002365/*
2366 * "clearmatches()" function
2367 */
2368 static void
2369f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2370{
2371#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002372 win_T *win = get_optional_window(argvars, 0);
2373
2374 if (win != NULL)
2375 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002376#endif
2377}
2378
2379/*
2380 * "col(string)" function
2381 */
2382 static void
2383f_col(typval_T *argvars, typval_T *rettv)
2384{
2385 colnr_T col = 0;
2386 pos_T *fp;
2387 int fnum = curbuf->b_fnum;
2388
2389 fp = var2fpos(&argvars[0], FALSE, &fnum);
2390 if (fp != NULL && fnum == curbuf->b_fnum)
2391 {
2392 if (fp->col == MAXCOL)
2393 {
2394 /* '> can be MAXCOL, get the length of the line then */
2395 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2396 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2397 else
2398 col = MAXCOL;
2399 }
2400 else
2401 {
2402 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002403 /* col(".") when the cursor is on the NUL at the end of the line
2404 * because of "coladd" can be seen as an extra column. */
2405 if (virtual_active() && fp == &curwin->w_cursor)
2406 {
2407 char_u *p = ml_get_cursor();
2408
2409 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2410 curwin->w_virtcol - curwin->w_cursor.coladd))
2411 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412 int l;
2413
2414 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2415 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002416 }
2417 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002418 }
2419 }
2420 rettv->vval.v_number = col;
2421}
2422
2423#if defined(FEAT_INS_EXPAND)
2424/*
2425 * "complete()" function
2426 */
2427 static void
2428f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2429{
2430 int startcol;
2431
2432 if ((State & INSERT) == 0)
2433 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002434 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002435 return;
2436 }
2437
2438 /* Check for undo allowed here, because if something was already inserted
2439 * the line was already saved for undo and this check isn't done. */
2440 if (!undo_allowed())
2441 return;
2442
2443 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2444 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002445 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002446 return;
2447 }
2448
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002449 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002450 if (startcol <= 0)
2451 return;
2452
2453 set_completion(startcol - 1, argvars[1].vval.v_list);
2454}
2455
2456/*
2457 * "complete_add()" function
2458 */
2459 static void
2460f_complete_add(typval_T *argvars, typval_T *rettv)
2461{
2462 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2463}
2464
2465/*
2466 * "complete_check()" function
2467 */
2468 static void
2469f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2470{
2471 int saved = RedrawingDisabled;
2472
2473 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002474 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002475 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002476 RedrawingDisabled = saved;
2477}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002478
2479/*
2480 * "complete_info()" function
2481 */
2482 static void
2483f_complete_info(typval_T *argvars, typval_T *rettv)
2484{
2485 list_T *what_list = NULL;
2486
2487 if (rettv_dict_alloc(rettv) != OK)
2488 return;
2489
2490 if (argvars[0].v_type != VAR_UNKNOWN)
2491 {
2492 if (argvars[0].v_type != VAR_LIST)
2493 {
2494 emsg(_(e_listreq));
2495 return;
2496 }
2497 what_list = argvars[0].vval.v_list;
2498 }
2499 get_complete_info(what_list, rettv->vval.v_dict);
2500}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002501#endif
2502
2503/*
2504 * "confirm(message, buttons[, default [, type]])" function
2505 */
2506 static void
2507f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2508{
2509#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2510 char_u *message;
2511 char_u *buttons = NULL;
2512 char_u buf[NUMBUFLEN];
2513 char_u buf2[NUMBUFLEN];
2514 int def = 1;
2515 int type = VIM_GENERIC;
2516 char_u *typestr;
2517 int error = FALSE;
2518
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002519 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002520 if (message == NULL)
2521 error = TRUE;
2522 if (argvars[1].v_type != VAR_UNKNOWN)
2523 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002524 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002525 if (buttons == NULL)
2526 error = TRUE;
2527 if (argvars[2].v_type != VAR_UNKNOWN)
2528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002529 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002530 if (argvars[3].v_type != VAR_UNKNOWN)
2531 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002532 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002533 if (typestr == NULL)
2534 error = TRUE;
2535 else
2536 {
2537 switch (TOUPPER_ASC(*typestr))
2538 {
2539 case 'E': type = VIM_ERROR; break;
2540 case 'Q': type = VIM_QUESTION; break;
2541 case 'I': type = VIM_INFO; break;
2542 case 'W': type = VIM_WARNING; break;
2543 case 'G': type = VIM_GENERIC; break;
2544 }
2545 }
2546 }
2547 }
2548 }
2549
2550 if (buttons == NULL || *buttons == NUL)
2551 buttons = (char_u *)_("&Ok");
2552
2553 if (!error)
2554 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2555 def, NULL, FALSE);
2556#endif
2557}
2558
2559/*
2560 * "copy()" function
2561 */
2562 static void
2563f_copy(typval_T *argvars, typval_T *rettv)
2564{
2565 item_copy(&argvars[0], rettv, FALSE, 0);
2566}
2567
2568#ifdef FEAT_FLOAT
2569/*
2570 * "cos()" function
2571 */
2572 static void
2573f_cos(typval_T *argvars, typval_T *rettv)
2574{
2575 float_T f = 0.0;
2576
2577 rettv->v_type = VAR_FLOAT;
2578 if (get_float_arg(argvars, &f) == OK)
2579 rettv->vval.v_float = cos(f);
2580 else
2581 rettv->vval.v_float = 0.0;
2582}
2583
2584/*
2585 * "cosh()" function
2586 */
2587 static void
2588f_cosh(typval_T *argvars, typval_T *rettv)
2589{
2590 float_T f = 0.0;
2591
2592 rettv->v_type = VAR_FLOAT;
2593 if (get_float_arg(argvars, &f) == OK)
2594 rettv->vval.v_float = cosh(f);
2595 else
2596 rettv->vval.v_float = 0.0;
2597}
2598#endif
2599
2600/*
2601 * "count()" function
2602 */
2603 static void
2604f_count(typval_T *argvars, typval_T *rettv)
2605{
2606 long n = 0;
2607 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002608 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002609
Bram Moolenaar9966b212017-07-28 16:46:57 +02002610 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002611 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002612
2613 if (argvars[0].v_type == VAR_STRING)
2614 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002615 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002616 char_u *p = argvars[0].vval.v_string;
2617 char_u *next;
2618
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002619 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002620 {
2621 if (ic)
2622 {
2623 size_t len = STRLEN(expr);
2624
2625 while (*p != NUL)
2626 {
2627 if (MB_STRNICMP(p, expr, len) == 0)
2628 {
2629 ++n;
2630 p += len;
2631 }
2632 else
2633 MB_PTR_ADV(p);
2634 }
2635 }
2636 else
2637 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2638 != NULL)
2639 {
2640 ++n;
2641 p = next + STRLEN(expr);
2642 }
2643 }
2644
2645 }
2646 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002647 {
2648 listitem_T *li;
2649 list_T *l;
2650 long idx;
2651
2652 if ((l = argvars[0].vval.v_list) != NULL)
2653 {
2654 li = l->lv_first;
2655 if (argvars[2].v_type != VAR_UNKNOWN)
2656 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002657 if (argvars[3].v_type != VAR_UNKNOWN)
2658 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002659 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660 if (!error)
2661 {
2662 li = list_find(l, idx);
2663 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002664 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002665 }
2666 }
2667 if (error)
2668 li = NULL;
2669 }
2670
2671 for ( ; li != NULL; li = li->li_next)
2672 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2673 ++n;
2674 }
2675 }
2676 else if (argvars[0].v_type == VAR_DICT)
2677 {
2678 int todo;
2679 dict_T *d;
2680 hashitem_T *hi;
2681
2682 if ((d = argvars[0].vval.v_dict) != NULL)
2683 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002684 if (argvars[2].v_type != VAR_UNKNOWN)
2685 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002686 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002687 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 }
2689
2690 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2691 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2692 {
2693 if (!HASHITEM_EMPTY(hi))
2694 {
2695 --todo;
2696 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2697 ++n;
2698 }
2699 }
2700 }
2701 }
2702 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002703 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002704 rettv->vval.v_number = n;
2705}
2706
2707/*
2708 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2709 *
2710 * Checks the existence of a cscope connection.
2711 */
2712 static void
2713f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2714{
2715#ifdef FEAT_CSCOPE
2716 int num = 0;
2717 char_u *dbpath = NULL;
2718 char_u *prepend = NULL;
2719 char_u buf[NUMBUFLEN];
2720
2721 if (argvars[0].v_type != VAR_UNKNOWN
2722 && argvars[1].v_type != VAR_UNKNOWN)
2723 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002724 num = (int)tv_get_number(&argvars[0]);
2725 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002726 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002727 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002728 }
2729
2730 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2731#endif
2732}
2733
2734/*
2735 * "cursor(lnum, col)" function, or
2736 * "cursor(list)"
2737 *
2738 * Moves the cursor to the specified line and column.
2739 * Returns 0 when the position could be set, -1 otherwise.
2740 */
2741 static void
2742f_cursor(typval_T *argvars, typval_T *rettv)
2743{
2744 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002745 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002746 int set_curswant = TRUE;
2747
2748 rettv->vval.v_number = -1;
2749 if (argvars[1].v_type == VAR_UNKNOWN)
2750 {
2751 pos_T pos;
2752 colnr_T curswant = -1;
2753
2754 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002756 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002757 return;
2758 }
2759 line = pos.lnum;
2760 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002761 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002762 if (curswant >= 0)
2763 {
2764 curwin->w_curswant = curswant - 1;
2765 set_curswant = FALSE;
2766 }
2767 }
2768 else
2769 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002770 line = tv_get_lnum(argvars);
2771 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002772 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002773 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002774 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002775 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002776 return; /* type error; errmsg already given */
2777 if (line > 0)
2778 curwin->w_cursor.lnum = line;
2779 if (col > 0)
2780 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002781 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782
2783 /* Make sure the cursor is in a valid position. */
2784 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785 /* Correct cursor for multi-byte character. */
2786 if (has_mbyte)
2787 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002788
2789 curwin->w_set_curswant = set_curswant;
2790 rettv->vval.v_number = 0;
2791}
2792
Bram Moolenaar4f974752019-02-17 17:44:42 +01002793#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002794/*
2795 * "debugbreak()" function
2796 */
2797 static void
2798f_debugbreak(typval_T *argvars, typval_T *rettv)
2799{
2800 int pid;
2801
2802 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002803 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002804 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002805 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002806 else
2807 {
2808 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2809
2810 if (hProcess != NULL)
2811 {
2812 DebugBreakProcess(hProcess);
2813 CloseHandle(hProcess);
2814 rettv->vval.v_number = OK;
2815 }
2816 }
2817}
2818#endif
2819
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002820/*
2821 * "deepcopy()" function
2822 */
2823 static void
2824f_deepcopy(typval_T *argvars, typval_T *rettv)
2825{
2826 int noref = 0;
2827 int copyID;
2828
2829 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002830 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002831 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002832 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002833 else
2834 {
2835 copyID = get_copyID();
2836 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2837 }
2838}
2839
2840/*
2841 * "delete()" function
2842 */
2843 static void
2844f_delete(typval_T *argvars, typval_T *rettv)
2845{
2846 char_u nbuf[NUMBUFLEN];
2847 char_u *name;
2848 char_u *flags;
2849
2850 rettv->vval.v_number = -1;
2851 if (check_restricted() || check_secure())
2852 return;
2853
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002854 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002855 if (name == NULL || *name == NUL)
2856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002857 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002858 return;
2859 }
2860
2861 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002862 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002863 else
2864 flags = (char_u *)"";
2865
2866 if (*flags == NUL)
2867 /* delete a file */
2868 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2869 else if (STRCMP(flags, "d") == 0)
2870 /* delete an empty directory */
2871 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2872 else if (STRCMP(flags, "rf") == 0)
2873 /* delete a directory recursively */
2874 rettv->vval.v_number = delete_recursive(name);
2875 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002876 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002877}
2878
2879/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002880 * "deletebufline()" function
2881 */
2882 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002883f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002884{
2885 buf_T *buf;
2886 linenr_T first, last;
2887 linenr_T lnum;
2888 long count;
2889 int is_curbuf;
2890 buf_T *curbuf_save = NULL;
2891 win_T *curwin_save = NULL;
2892 tabpage_T *tp;
2893 win_T *wp;
2894
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002895 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002896 if (buf == NULL)
2897 {
2898 rettv->vval.v_number = 1; /* FAIL */
2899 return;
2900 }
2901 is_curbuf = buf == curbuf;
2902
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002903 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002904 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002905 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002906 else
2907 last = first;
2908
2909 if (buf->b_ml.ml_mfp == NULL || first < 1
2910 || first > buf->b_ml.ml_line_count || last < first)
2911 {
2912 rettv->vval.v_number = 1; /* FAIL */
2913 return;
2914 }
2915
2916 if (!is_curbuf)
2917 {
2918 curbuf_save = curbuf;
2919 curwin_save = curwin;
2920 curbuf = buf;
2921 find_win_for_curbuf();
2922 }
2923 if (last > curbuf->b_ml.ml_line_count)
2924 last = curbuf->b_ml.ml_line_count;
2925 count = last - first + 1;
2926
2927 // When coming here from Insert mode, sync undo, so that this can be
2928 // undone separately from what was previously inserted.
2929 if (u_sync_once == 2)
2930 {
2931 u_sync_once = 1; // notify that u_sync() was called
2932 u_sync(TRUE);
2933 }
2934
2935 if (u_save(first - 1, last + 1) == FAIL)
2936 {
2937 rettv->vval.v_number = 1; /* FAIL */
2938 return;
2939 }
2940
2941 for (lnum = first; lnum <= last; ++lnum)
2942 ml_delete(first, TRUE);
2943
2944 FOR_ALL_TAB_WINDOWS(tp, wp)
2945 if (wp->w_buffer == buf)
2946 {
2947 if (wp->w_cursor.lnum > last)
2948 wp->w_cursor.lnum -= count;
2949 else if (wp->w_cursor.lnum> first)
2950 wp->w_cursor.lnum = first;
2951 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2952 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2953 }
2954 check_cursor_col();
2955 deleted_lines_mark(first, count);
2956
2957 if (!is_curbuf)
2958 {
2959 curbuf = curbuf_save;
2960 curwin = curwin_save;
2961 }
2962}
2963
2964/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002965 * "did_filetype()" function
2966 */
2967 static void
2968f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2969{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002970 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971}
2972
2973/*
2974 * "diff_filler()" function
2975 */
2976 static void
2977f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2978{
2979#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002980 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002981#endif
2982}
2983
2984/*
2985 * "diff_hlID()" function
2986 */
2987 static void
2988f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2989{
2990#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002991 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002992 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002993 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002994 static int fnum = 0;
2995 static int change_start = 0;
2996 static int change_end = 0;
2997 static hlf_T hlID = (hlf_T)0;
2998 int filler_lines;
2999 int col;
3000
3001 if (lnum < 0) /* ignore type error in {lnum} arg */
3002 lnum = 0;
3003 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003004 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003005 || fnum != curbuf->b_fnum)
3006 {
3007 /* New line, buffer, change: need to get the values. */
3008 filler_lines = diff_check(curwin, lnum);
3009 if (filler_lines < 0)
3010 {
3011 if (filler_lines == -1)
3012 {
3013 change_start = MAXCOL;
3014 change_end = -1;
3015 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3016 hlID = HLF_ADD; /* added line */
3017 else
3018 hlID = HLF_CHD; /* changed line */
3019 }
3020 else
3021 hlID = HLF_ADD; /* added line */
3022 }
3023 else
3024 hlID = (hlf_T)0;
3025 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003026 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027 fnum = curbuf->b_fnum;
3028 }
3029
3030 if (hlID == HLF_CHD || hlID == HLF_TXD)
3031 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003032 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033 if (col >= change_start && col <= change_end)
3034 hlID = HLF_TXD; /* changed text */
3035 else
3036 hlID = HLF_CHD; /* changed line */
3037 }
3038 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3039#endif
3040}
3041
3042/*
3043 * "empty({expr})" function
3044 */
3045 static void
3046f_empty(typval_T *argvars, typval_T *rettv)
3047{
3048 int n = FALSE;
3049
3050 switch (argvars[0].v_type)
3051 {
3052 case VAR_STRING:
3053 case VAR_FUNC:
3054 n = argvars[0].vval.v_string == NULL
3055 || *argvars[0].vval.v_string == NUL;
3056 break;
3057 case VAR_PARTIAL:
3058 n = FALSE;
3059 break;
3060 case VAR_NUMBER:
3061 n = argvars[0].vval.v_number == 0;
3062 break;
3063 case VAR_FLOAT:
3064#ifdef FEAT_FLOAT
3065 n = argvars[0].vval.v_float == 0.0;
3066 break;
3067#endif
3068 case VAR_LIST:
3069 n = argvars[0].vval.v_list == NULL
3070 || argvars[0].vval.v_list->lv_first == NULL;
3071 break;
3072 case VAR_DICT:
3073 n = argvars[0].vval.v_dict == NULL
3074 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3075 break;
3076 case VAR_SPECIAL:
3077 n = argvars[0].vval.v_number != VVAL_TRUE;
3078 break;
3079
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003080 case VAR_BLOB:
3081 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003082 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3083 break;
3084
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003085 case VAR_JOB:
3086#ifdef FEAT_JOB_CHANNEL
3087 n = argvars[0].vval.v_job == NULL
3088 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3089 break;
3090#endif
3091 case VAR_CHANNEL:
3092#ifdef FEAT_JOB_CHANNEL
3093 n = argvars[0].vval.v_channel == NULL
3094 || !channel_is_open(argvars[0].vval.v_channel);
3095 break;
3096#endif
3097 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003098 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003099 n = TRUE;
3100 break;
3101 }
3102
3103 rettv->vval.v_number = n;
3104}
3105
3106/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003107 * "environ()" function
3108 */
3109 static void
3110f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3111{
3112#if !defined(AMIGA)
3113 int i = 0;
3114 char_u *entry, *value;
3115# ifdef MSWIN
3116 extern wchar_t **_wenviron;
3117# else
3118 extern char **environ;
3119# endif
3120
3121 if (rettv_dict_alloc(rettv) != OK)
3122 return;
3123
3124# ifdef MSWIN
3125 if (*_wenviron == NULL)
3126 return;
3127# else
3128 if (*environ == NULL)
3129 return;
3130# endif
3131
3132 for (i = 0; ; ++i)
3133 {
3134# ifdef MSWIN
3135 short_u *p;
3136
3137 if ((p = (short_u *)_wenviron[i]) == NULL)
3138 return;
3139 entry = utf16_to_enc(p, NULL);
3140# else
3141 if ((entry = (char_u *)environ[i]) == NULL)
3142 return;
3143 entry = vim_strsave(entry);
3144# endif
3145 if (entry == NULL) // out of memory
3146 return;
3147 if ((value = vim_strchr(entry, '=')) == NULL)
3148 {
3149 vim_free(entry);
3150 continue;
3151 }
3152 *value++ = NUL;
3153 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3154 vim_free(entry);
3155 }
3156#endif
3157}
3158
3159/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003160 * "escape({string}, {chars})" function
3161 */
3162 static void
3163f_escape(typval_T *argvars, typval_T *rettv)
3164{
3165 char_u buf[NUMBUFLEN];
3166
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003167 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3168 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003169 rettv->v_type = VAR_STRING;
3170}
3171
3172/*
3173 * "eval()" function
3174 */
3175 static void
3176f_eval(typval_T *argvars, typval_T *rettv)
3177{
3178 char_u *s, *p;
3179
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003180 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003181 if (s != NULL)
3182 s = skipwhite(s);
3183
3184 p = s;
3185 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3186 {
3187 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003188 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003189 need_clr_eos = FALSE;
3190 rettv->v_type = VAR_NUMBER;
3191 rettv->vval.v_number = 0;
3192 }
3193 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003194 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003195}
3196
3197/*
3198 * "eventhandler()" function
3199 */
3200 static void
3201f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3202{
3203 rettv->vval.v_number = vgetc_busy;
3204}
3205
3206/*
3207 * "executable()" function
3208 */
3209 static void
3210f_executable(typval_T *argvars, typval_T *rettv)
3211{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003212 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003213
3214 /* Check in $PATH and also check directly if there is a directory name. */
3215 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3216 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3217}
3218
3219static garray_T redir_execute_ga;
3220
3221/*
3222 * Append "value[value_len]" to the execute() output.
3223 */
3224 void
3225execute_redir_str(char_u *value, int value_len)
3226{
3227 int len;
3228
3229 if (value_len == -1)
3230 len = (int)STRLEN(value); /* Append the entire string */
3231 else
3232 len = value_len; /* Append only "value_len" characters */
3233 if (ga_grow(&redir_execute_ga, len) == OK)
3234 {
3235 mch_memmove((char *)redir_execute_ga.ga_data
3236 + redir_execute_ga.ga_len, value, len);
3237 redir_execute_ga.ga_len += len;
3238 }
3239}
3240
3241/*
3242 * Get next line from a list.
3243 * Called by do_cmdline() to get the next line.
3244 * Returns allocated string, or NULL for end of function.
3245 */
3246
3247 static char_u *
3248get_list_line(
3249 int c UNUSED,
3250 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003251 int indent UNUSED,
3252 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003253{
3254 listitem_T **p = (listitem_T **)cookie;
3255 listitem_T *item = *p;
3256 char_u buf[NUMBUFLEN];
3257 char_u *s;
3258
3259 if (item == NULL)
3260 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003261 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003262 *p = item->li_next;
3263 return s == NULL ? NULL : vim_strsave(s);
3264}
3265
3266/*
3267 * "execute()" function
3268 */
3269 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003270execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003271{
3272 char_u *cmd = NULL;
3273 list_T *list = NULL;
3274 int save_msg_silent = msg_silent;
3275 int save_emsg_silent = emsg_silent;
3276 int save_emsg_noredir = emsg_noredir;
3277 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003278 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003280 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003281 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003282
3283 rettv->vval.v_string = NULL;
3284 rettv->v_type = VAR_STRING;
3285
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003286 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003287 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003288 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003289 if (list == NULL || list->lv_first == NULL)
3290 /* empty list, no commands, empty output */
3291 return;
3292 ++list->lv_refcount;
3293 }
3294 else
3295 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003296 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 if (cmd == NULL)
3298 return;
3299 }
3300
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003301 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003302 {
3303 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003304 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003305
3306 if (s == NULL)
3307 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003308 if (*s == NUL)
3309 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003310 if (STRNCMP(s, "silent", 6) == 0)
3311 ++msg_silent;
3312 if (STRCMP(s, "silent!") == 0)
3313 {
3314 emsg_silent = TRUE;
3315 emsg_noredir = TRUE;
3316 }
3317 }
3318 else
3319 ++msg_silent;
3320
3321 if (redir_execute)
3322 save_ga = redir_execute_ga;
3323 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3324 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003325 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003326 if (!echo_output)
3327 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003328
3329 if (cmd != NULL)
3330 do_cmdline_cmd(cmd);
3331 else
3332 {
3333 listitem_T *item = list->lv_first;
3334
3335 do_cmdline(NULL, get_list_line, (void *)&item,
3336 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3337 --list->lv_refcount;
3338 }
3339
Bram Moolenaard297f352017-01-29 20:31:21 +01003340 /* Need to append a NUL to the result. */
3341 if (ga_grow(&redir_execute_ga, 1) == OK)
3342 {
3343 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3344 rettv->vval.v_string = redir_execute_ga.ga_data;
3345 }
3346 else
3347 {
3348 ga_clear(&redir_execute_ga);
3349 rettv->vval.v_string = NULL;
3350 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003351 msg_silent = save_msg_silent;
3352 emsg_silent = save_emsg_silent;
3353 emsg_noredir = save_emsg_noredir;
3354
3355 redir_execute = save_redir_execute;
3356 if (redir_execute)
3357 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003358 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003359
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003360 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003361 if (echo_output)
3362 // When not working silently: put it in column zero. A following
3363 // "echon" will overwrite the message, unavoidably.
3364 msg_col = 0;
3365 else
3366 // When working silently: Put it back where it was, since nothing
3367 // should have been written.
3368 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003369}
3370
3371/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003372 * "execute()" function
3373 */
3374 static void
3375f_execute(typval_T *argvars, typval_T *rettv)
3376{
3377 execute_common(argvars, rettv, 0);
3378}
3379
3380/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003381 * "exepath()" function
3382 */
3383 static void
3384f_exepath(typval_T *argvars, typval_T *rettv)
3385{
3386 char_u *p = NULL;
3387
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003388 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003389 rettv->v_type = VAR_STRING;
3390 rettv->vval.v_string = p;
3391}
3392
3393/*
3394 * "exists()" function
3395 */
3396 static void
3397f_exists(typval_T *argvars, typval_T *rettv)
3398{
3399 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003400 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003401
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003402 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003403 if (*p == '$') /* environment variable */
3404 {
3405 /* first try "normal" environment variables (fast) */
3406 if (mch_getenv(p + 1) != NULL)
3407 n = TRUE;
3408 else
3409 {
3410 /* try expanding things like $VIM and ${HOME} */
3411 p = expand_env_save(p);
3412 if (p != NULL && *p != '$')
3413 n = TRUE;
3414 vim_free(p);
3415 }
3416 }
3417 else if (*p == '&' || *p == '+') /* option */
3418 {
3419 n = (get_option_tv(&p, NULL, TRUE) == OK);
3420 if (*skipwhite(p) != NUL)
3421 n = FALSE; /* trailing garbage */
3422 }
3423 else if (*p == '*') /* internal or user defined function */
3424 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003425 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003426 }
3427 else if (*p == ':')
3428 {
3429 n = cmd_exists(p + 1);
3430 }
3431 else if (*p == '#')
3432 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003433 if (p[1] == '#')
3434 n = autocmd_supported(p + 2);
3435 else
3436 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003437 }
3438 else /* internal variable */
3439 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003440 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441 }
3442
3443 rettv->vval.v_number = n;
3444}
3445
3446#ifdef FEAT_FLOAT
3447/*
3448 * "exp()" function
3449 */
3450 static void
3451f_exp(typval_T *argvars, typval_T *rettv)
3452{
3453 float_T f = 0.0;
3454
3455 rettv->v_type = VAR_FLOAT;
3456 if (get_float_arg(argvars, &f) == OK)
3457 rettv->vval.v_float = exp(f);
3458 else
3459 rettv->vval.v_float = 0.0;
3460}
3461#endif
3462
3463/*
3464 * "expand()" function
3465 */
3466 static void
3467f_expand(typval_T *argvars, typval_T *rettv)
3468{
3469 char_u *s;
3470 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003471 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3473 expand_T xpc;
3474 int error = FALSE;
3475 char_u *result;
3476
3477 rettv->v_type = VAR_STRING;
3478 if (argvars[1].v_type != VAR_UNKNOWN
3479 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003480 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003481 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003482 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003484 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003485 if (*s == '%' || *s == '#' || *s == '<')
3486 {
3487 ++emsg_off;
3488 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3489 --emsg_off;
3490 if (rettv->v_type == VAR_LIST)
3491 {
3492 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3493 list_append_string(rettv->vval.v_list, result, -1);
3494 else
3495 vim_free(result);
3496 }
3497 else
3498 rettv->vval.v_string = result;
3499 }
3500 else
3501 {
3502 /* When the optional second argument is non-zero, don't remove matches
3503 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3504 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003505 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003506 options |= WILD_KEEP_ALL;
3507 if (!error)
3508 {
3509 ExpandInit(&xpc);
3510 xpc.xp_context = EXPAND_FILES;
3511 if (p_wic)
3512 options += WILD_ICASE;
3513 if (rettv->v_type == VAR_STRING)
3514 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3515 options, WILD_ALL);
3516 else if (rettv_list_alloc(rettv) != FAIL)
3517 {
3518 int i;
3519
3520 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3521 for (i = 0; i < xpc.xp_numfiles; i++)
3522 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3523 ExpandCleanup(&xpc);
3524 }
3525 }
3526 else
3527 rettv->vval.v_string = NULL;
3528 }
3529}
3530
3531/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003532 * "expandcmd()" function
3533 * Expand all the special characters in a command string.
3534 */
3535 static void
3536f_expandcmd(typval_T *argvars, typval_T *rettv)
3537{
3538 exarg_T eap;
3539 char_u *cmdstr;
3540 char *errormsg = NULL;
3541
3542 rettv->v_type = VAR_STRING;
3543 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3544
3545 memset(&eap, 0, sizeof(eap));
3546 eap.cmd = cmdstr;
3547 eap.arg = cmdstr;
3548 eap.argt |= NOSPC;
3549 eap.usefilter = FALSE;
3550 eap.nextcmd = NULL;
3551 eap.cmdidx = CMD_USER;
3552
3553 expand_filename(&eap, &cmdstr, &errormsg);
3554 if (errormsg != NULL && *errormsg != NUL)
3555 emsg(errormsg);
3556
3557 rettv->vval.v_string = cmdstr;
3558}
3559
3560/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561 * "extend(list, list [, idx])" function
3562 * "extend(dict, dict [, action])" function
3563 */
3564 static void
3565f_extend(typval_T *argvars, typval_T *rettv)
3566{
3567 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3568
3569 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3570 {
3571 list_T *l1, *l2;
3572 listitem_T *item;
3573 long before;
3574 int error = FALSE;
3575
3576 l1 = argvars[0].vval.v_list;
3577 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003578 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003579 && l2 != NULL)
3580 {
3581 if (argvars[2].v_type != VAR_UNKNOWN)
3582 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003583 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003584 if (error)
3585 return; /* type error; errmsg already given */
3586
3587 if (before == l1->lv_len)
3588 item = NULL;
3589 else
3590 {
3591 item = list_find(l1, before);
3592 if (item == NULL)
3593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003594 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003595 return;
3596 }
3597 }
3598 }
3599 else
3600 item = NULL;
3601 list_extend(l1, l2, item);
3602
3603 copy_tv(&argvars[0], rettv);
3604 }
3605 }
3606 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3607 {
3608 dict_T *d1, *d2;
3609 char_u *action;
3610 int i;
3611
3612 d1 = argvars[0].vval.v_dict;
3613 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003614 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003615 && d2 != NULL)
3616 {
3617 /* Check the third argument. */
3618 if (argvars[2].v_type != VAR_UNKNOWN)
3619 {
3620 static char *(av[]) = {"keep", "force", "error"};
3621
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003622 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003623 if (action == NULL)
3624 return; /* type error; errmsg already given */
3625 for (i = 0; i < 3; ++i)
3626 if (STRCMP(action, av[i]) == 0)
3627 break;
3628 if (i == 3)
3629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003630 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003631 return;
3632 }
3633 }
3634 else
3635 action = (char_u *)"force";
3636
3637 dict_extend(d1, d2, action);
3638
3639 copy_tv(&argvars[0], rettv);
3640 }
3641 }
3642 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003643 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003644}
3645
3646/*
3647 * "feedkeys()" function
3648 */
3649 static void
3650f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3651{
3652 int remap = TRUE;
3653 int insert = FALSE;
3654 char_u *keys, *flags;
3655 char_u nbuf[NUMBUFLEN];
3656 int typed = FALSE;
3657 int execute = FALSE;
3658 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003659 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003660 char_u *keys_esc;
3661
3662 /* This is not allowed in the sandbox. If the commands would still be
3663 * executed in the sandbox it would be OK, but it probably happens later,
3664 * when "sandbox" is no longer set. */
3665 if (check_secure())
3666 return;
3667
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003668 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003669
3670 if (argvars[1].v_type != VAR_UNKNOWN)
3671 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003672 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003673 for ( ; *flags != NUL; ++flags)
3674 {
3675 switch (*flags)
3676 {
3677 case 'n': remap = FALSE; break;
3678 case 'm': remap = TRUE; break;
3679 case 't': typed = TRUE; break;
3680 case 'i': insert = TRUE; break;
3681 case 'x': execute = TRUE; break;
3682 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003683 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003684 }
3685 }
3686 }
3687
3688 if (*keys != NUL || execute)
3689 {
3690 /* Need to escape K_SPECIAL and CSI before putting the string in the
3691 * typeahead buffer. */
3692 keys_esc = vim_strsave_escape_csi(keys);
3693 if (keys_esc != NULL)
3694 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003695 if (lowlevel)
3696 {
3697#ifdef USE_INPUT_BUF
3698 add_to_input_buf(keys, (int)STRLEN(keys));
3699#else
3700 emsg(_("E980: lowlevel input not supported"));
3701#endif
3702 }
3703 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003704 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003705 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003706 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003707 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003708#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003709 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003710#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003711 )
3712 typebuf_was_filled = TRUE;
3713 }
3714 vim_free(keys_esc);
3715
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003716 if (execute)
3717 {
3718 int save_msg_scroll = msg_scroll;
3719
3720 /* Avoid a 1 second delay when the keys start Insert mode. */
3721 msg_scroll = FALSE;
3722
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003723 if (!dangerous)
3724 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003725 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003726 if (!dangerous)
3727 --ex_normal_busy;
3728
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003729 msg_scroll |= save_msg_scroll;
3730 }
3731 }
3732 }
3733}
3734
3735/*
3736 * "filereadable()" function
3737 */
3738 static void
3739f_filereadable(typval_T *argvars, typval_T *rettv)
3740{
3741 int fd;
3742 char_u *p;
3743 int n;
3744
3745#ifndef O_NONBLOCK
3746# define O_NONBLOCK 0
3747#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003748 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3750 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3751 {
3752 n = TRUE;
3753 close(fd);
3754 }
3755 else
3756 n = FALSE;
3757
3758 rettv->vval.v_number = n;
3759}
3760
3761/*
3762 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3763 * rights to write into.
3764 */
3765 static void
3766f_filewritable(typval_T *argvars, typval_T *rettv)
3767{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003768 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003769}
3770
3771 static void
3772findfilendir(
3773 typval_T *argvars UNUSED,
3774 typval_T *rettv,
3775 int find_what UNUSED)
3776{
3777#ifdef FEAT_SEARCHPATH
3778 char_u *fname;
3779 char_u *fresult = NULL;
3780 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3781 char_u *p;
3782 char_u pathbuf[NUMBUFLEN];
3783 int count = 1;
3784 int first = TRUE;
3785 int error = FALSE;
3786#endif
3787
3788 rettv->vval.v_string = NULL;
3789 rettv->v_type = VAR_STRING;
3790
3791#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003792 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003793
3794 if (argvars[1].v_type != VAR_UNKNOWN)
3795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003796 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003797 if (p == NULL)
3798 error = TRUE;
3799 else
3800 {
3801 if (*p != NUL)
3802 path = p;
3803
3804 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003805 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003806 }
3807 }
3808
3809 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3810 error = TRUE;
3811
3812 if (*fname != NUL && !error)
3813 {
3814 do
3815 {
3816 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3817 vim_free(fresult);
3818 fresult = find_file_in_path_option(first ? fname : NULL,
3819 first ? (int)STRLEN(fname) : 0,
3820 0, first, path,
3821 find_what,
3822 curbuf->b_ffname,
3823 find_what == FINDFILE_DIR
3824 ? (char_u *)"" : curbuf->b_p_sua);
3825 first = FALSE;
3826
3827 if (fresult != NULL && rettv->v_type == VAR_LIST)
3828 list_append_string(rettv->vval.v_list, fresult, -1);
3829
3830 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3831 }
3832
3833 if (rettv->v_type == VAR_STRING)
3834 rettv->vval.v_string = fresult;
3835#endif
3836}
3837
3838/*
3839 * "filter()" function
3840 */
3841 static void
3842f_filter(typval_T *argvars, typval_T *rettv)
3843{
3844 filter_map(argvars, rettv, FALSE);
3845}
3846
3847/*
3848 * "finddir({fname}[, {path}[, {count}]])" function
3849 */
3850 static void
3851f_finddir(typval_T *argvars, typval_T *rettv)
3852{
3853 findfilendir(argvars, rettv, FINDFILE_DIR);
3854}
3855
3856/*
3857 * "findfile({fname}[, {path}[, {count}]])" function
3858 */
3859 static void
3860f_findfile(typval_T *argvars, typval_T *rettv)
3861{
3862 findfilendir(argvars, rettv, FINDFILE_FILE);
3863}
3864
3865#ifdef FEAT_FLOAT
3866/*
3867 * "float2nr({float})" function
3868 */
3869 static void
3870f_float2nr(typval_T *argvars, typval_T *rettv)
3871{
3872 float_T f = 0.0;
3873
3874 if (get_float_arg(argvars, &f) == OK)
3875 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003876 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003877 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003878 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003879 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003880 else
3881 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003882 }
3883}
3884
3885/*
3886 * "floor({float})" function
3887 */
3888 static void
3889f_floor(typval_T *argvars, typval_T *rettv)
3890{
3891 float_T f = 0.0;
3892
3893 rettv->v_type = VAR_FLOAT;
3894 if (get_float_arg(argvars, &f) == OK)
3895 rettv->vval.v_float = floor(f);
3896 else
3897 rettv->vval.v_float = 0.0;
3898}
3899
3900/*
3901 * "fmod()" function
3902 */
3903 static void
3904f_fmod(typval_T *argvars, typval_T *rettv)
3905{
3906 float_T fx = 0.0, fy = 0.0;
3907
3908 rettv->v_type = VAR_FLOAT;
3909 if (get_float_arg(argvars, &fx) == OK
3910 && get_float_arg(&argvars[1], &fy) == OK)
3911 rettv->vval.v_float = fmod(fx, fy);
3912 else
3913 rettv->vval.v_float = 0.0;
3914}
3915#endif
3916
3917/*
3918 * "fnameescape({string})" function
3919 */
3920 static void
3921f_fnameescape(typval_T *argvars, typval_T *rettv)
3922{
3923 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003924 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003925 rettv->v_type = VAR_STRING;
3926}
3927
3928/*
3929 * "fnamemodify({fname}, {mods})" function
3930 */
3931 static void
3932f_fnamemodify(typval_T *argvars, typval_T *rettv)
3933{
3934 char_u *fname;
3935 char_u *mods;
3936 int usedlen = 0;
3937 int len;
3938 char_u *fbuf = NULL;
3939 char_u buf[NUMBUFLEN];
3940
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003941 fname = tv_get_string_chk(&argvars[0]);
3942 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003943 if (fname == NULL || mods == NULL)
3944 fname = NULL;
3945 else
3946 {
3947 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003948 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003949 }
3950
3951 rettv->v_type = VAR_STRING;
3952 if (fname == NULL)
3953 rettv->vval.v_string = NULL;
3954 else
3955 rettv->vval.v_string = vim_strnsave(fname, len);
3956 vim_free(fbuf);
3957}
3958
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003959/*
3960 * "foldclosed()" function
3961 */
3962 static void
3963foldclosed_both(
3964 typval_T *argvars UNUSED,
3965 typval_T *rettv,
3966 int end UNUSED)
3967{
3968#ifdef FEAT_FOLDING
3969 linenr_T lnum;
3970 linenr_T first, last;
3971
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003972 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003973 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3974 {
3975 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3976 {
3977 if (end)
3978 rettv->vval.v_number = (varnumber_T)last;
3979 else
3980 rettv->vval.v_number = (varnumber_T)first;
3981 return;
3982 }
3983 }
3984#endif
3985 rettv->vval.v_number = -1;
3986}
3987
3988/*
3989 * "foldclosed()" function
3990 */
3991 static void
3992f_foldclosed(typval_T *argvars, typval_T *rettv)
3993{
3994 foldclosed_both(argvars, rettv, FALSE);
3995}
3996
3997/*
3998 * "foldclosedend()" function
3999 */
4000 static void
4001f_foldclosedend(typval_T *argvars, typval_T *rettv)
4002{
4003 foldclosed_both(argvars, rettv, TRUE);
4004}
4005
4006/*
4007 * "foldlevel()" function
4008 */
4009 static void
4010f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4011{
4012#ifdef FEAT_FOLDING
4013 linenr_T lnum;
4014
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004015 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004016 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4017 rettv->vval.v_number = foldLevel(lnum);
4018#endif
4019}
4020
4021/*
4022 * "foldtext()" function
4023 */
4024 static void
4025f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4026{
4027#ifdef FEAT_FOLDING
4028 linenr_T foldstart;
4029 linenr_T foldend;
4030 char_u *dashes;
4031 linenr_T lnum;
4032 char_u *s;
4033 char_u *r;
4034 int len;
4035 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004036 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004037#endif
4038
4039 rettv->v_type = VAR_STRING;
4040 rettv->vval.v_string = NULL;
4041#ifdef FEAT_FOLDING
4042 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4043 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4044 dashes = get_vim_var_str(VV_FOLDDASHES);
4045 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4046 && dashes != NULL)
4047 {
4048 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004049 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004050 if (!linewhite(lnum))
4051 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004052
4053 /* Find interesting text in this line. */
4054 s = skipwhite(ml_get(lnum));
4055 /* skip C comment-start */
4056 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4057 {
4058 s = skipwhite(s + 2);
4059 if (*skipwhite(s) == NUL
4060 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4061 {
4062 s = skipwhite(ml_get(lnum + 1));
4063 if (*s == '*')
4064 s = skipwhite(s + 1);
4065 }
4066 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004067 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004068 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004069 r = alloc(STRLEN(txt)
4070 + STRLEN(dashes) // for %s
4071 + 20 // for %3ld
4072 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004073 if (r != NULL)
4074 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004075 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004076 len = (int)STRLEN(r);
4077 STRCAT(r, s);
4078 /* remove 'foldmarker' and 'commentstring' */
4079 foldtext_cleanup(r + len);
4080 rettv->vval.v_string = r;
4081 }
4082 }
4083#endif
4084}
4085
4086/*
4087 * "foldtextresult(lnum)" function
4088 */
4089 static void
4090f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4091{
4092#ifdef FEAT_FOLDING
4093 linenr_T lnum;
4094 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004095 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004096 foldinfo_T foldinfo;
4097 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004098 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004099#endif
4100
4101 rettv->v_type = VAR_STRING;
4102 rettv->vval.v_string = NULL;
4103#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004104 if (entered)
4105 return; /* reject recursive use */
4106 entered = TRUE;
4107
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004108 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004109 /* treat illegal types and illegal string values for {lnum} the same */
4110 if (lnum < 0)
4111 lnum = 0;
4112 fold_count = foldedCount(curwin, lnum, &foldinfo);
4113 if (fold_count > 0)
4114 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004115 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4116 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004117 if (text == buf)
4118 text = vim_strsave(text);
4119 rettv->vval.v_string = text;
4120 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004121
4122 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004123#endif
4124}
4125
4126/*
4127 * "foreground()" function
4128 */
4129 static void
4130f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4131{
4132#ifdef FEAT_GUI
4133 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004134 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004135 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004136 return;
4137 }
4138#endif
4139#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004140 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004141#endif
4142}
4143
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004144 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004145common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004146{
4147 char_u *s;
4148 char_u *name;
4149 int use_string = FALSE;
4150 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004151 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004152
4153 if (argvars[0].v_type == VAR_FUNC)
4154 {
4155 /* function(MyFunc, [arg], dict) */
4156 s = argvars[0].vval.v_string;
4157 }
4158 else if (argvars[0].v_type == VAR_PARTIAL
4159 && argvars[0].vval.v_partial != NULL)
4160 {
4161 /* function(dict.MyFunc, [arg]) */
4162 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004163 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004164 }
4165 else
4166 {
4167 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004168 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004169 use_string = TRUE;
4170 }
4171
Bram Moolenaar843b8842016-08-21 14:36:15 +02004172 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004173 {
4174 name = s;
4175 trans_name = trans_function_name(&name, FALSE,
4176 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4177 if (*name != NUL)
4178 s = NULL;
4179 }
4180
Bram Moolenaar843b8842016-08-21 14:36:15 +02004181 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4182 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004183 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004184 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004185 else if (trans_name != NULL && (is_funcref
4186 ? find_func(trans_name) == NULL
4187 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004188 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004189 else
4190 {
4191 int dict_idx = 0;
4192 int arg_idx = 0;
4193 list_T *list = NULL;
4194
4195 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4196 {
4197 char sid_buf[25];
4198 int off = *s == 's' ? 2 : 5;
4199
4200 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4201 * also be called from another script. Using trans_function_name()
4202 * would also work, but some plugins depend on the name being
4203 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004204 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004205 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004206 if (name != NULL)
4207 {
4208 STRCPY(name, sid_buf);
4209 STRCAT(name, s + off);
4210 }
4211 }
4212 else
4213 name = vim_strsave(s);
4214
4215 if (argvars[1].v_type != VAR_UNKNOWN)
4216 {
4217 if (argvars[2].v_type != VAR_UNKNOWN)
4218 {
4219 /* function(name, [args], dict) */
4220 arg_idx = 1;
4221 dict_idx = 2;
4222 }
4223 else if (argvars[1].v_type == VAR_DICT)
4224 /* function(name, dict) */
4225 dict_idx = 1;
4226 else
4227 /* function(name, [args]) */
4228 arg_idx = 1;
4229 if (dict_idx > 0)
4230 {
4231 if (argvars[dict_idx].v_type != VAR_DICT)
4232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004233 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004234 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004235 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004236 }
4237 if (argvars[dict_idx].vval.v_dict == NULL)
4238 dict_idx = 0;
4239 }
4240 if (arg_idx > 0)
4241 {
4242 if (argvars[arg_idx].v_type != VAR_LIST)
4243 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004244 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004245 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004246 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004247 }
4248 list = argvars[arg_idx].vval.v_list;
4249 if (list == NULL || list->lv_len == 0)
4250 arg_idx = 0;
4251 }
4252 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004253 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004254 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004255 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004256
4257 /* result is a VAR_PARTIAL */
4258 if (pt == NULL)
4259 vim_free(name);
4260 else
4261 {
4262 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4263 {
4264 listitem_T *li;
4265 int i = 0;
4266 int arg_len = 0;
4267 int lv_len = 0;
4268
4269 if (arg_pt != NULL)
4270 arg_len = arg_pt->pt_argc;
4271 if (list != NULL)
4272 lv_len = list->lv_len;
4273 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004274 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004275 if (pt->pt_argv == NULL)
4276 {
4277 vim_free(pt);
4278 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004279 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004280 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004281 for (i = 0; i < arg_len; i++)
4282 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4283 if (lv_len > 0)
4284 for (li = list->lv_first; li != NULL;
4285 li = li->li_next)
4286 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004287 }
4288
4289 /* For "function(dict.func, [], dict)" and "func" is a partial
4290 * use "dict". That is backwards compatible. */
4291 if (dict_idx > 0)
4292 {
4293 /* The dict is bound explicitly, pt_auto is FALSE. */
4294 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4295 ++pt->pt_dict->dv_refcount;
4296 }
4297 else if (arg_pt != NULL)
4298 {
4299 /* If the dict was bound automatically the result is also
4300 * bound automatically. */
4301 pt->pt_dict = arg_pt->pt_dict;
4302 pt->pt_auto = arg_pt->pt_auto;
4303 if (pt->pt_dict != NULL)
4304 ++pt->pt_dict->dv_refcount;
4305 }
4306
4307 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004308 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4309 {
4310 pt->pt_func = arg_pt->pt_func;
4311 func_ptr_ref(pt->pt_func);
4312 vim_free(name);
4313 }
4314 else if (is_funcref)
4315 {
4316 pt->pt_func = find_func(trans_name);
4317 func_ptr_ref(pt->pt_func);
4318 vim_free(name);
4319 }
4320 else
4321 {
4322 pt->pt_name = name;
4323 func_ref(name);
4324 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004325 }
4326 rettv->v_type = VAR_PARTIAL;
4327 rettv->vval.v_partial = pt;
4328 }
4329 else
4330 {
4331 /* result is a VAR_FUNC */
4332 rettv->v_type = VAR_FUNC;
4333 rettv->vval.v_string = name;
4334 func_ref(name);
4335 }
4336 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004337theend:
4338 vim_free(trans_name);
4339}
4340
4341/*
4342 * "funcref()" function
4343 */
4344 static void
4345f_funcref(typval_T *argvars, typval_T *rettv)
4346{
4347 common_function(argvars, rettv, TRUE);
4348}
4349
4350/*
4351 * "function()" function
4352 */
4353 static void
4354f_function(typval_T *argvars, typval_T *rettv)
4355{
4356 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004357}
4358
4359/*
4360 * "garbagecollect()" function
4361 */
4362 static void
4363f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4364{
4365 /* This is postponed until we are back at the toplevel, because we may be
4366 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4367 want_garbage_collect = TRUE;
4368
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004369 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004370 garbage_collect_at_exit = TRUE;
4371}
4372
4373/*
4374 * "get()" function
4375 */
4376 static void
4377f_get(typval_T *argvars, typval_T *rettv)
4378{
4379 listitem_T *li;
4380 list_T *l;
4381 dictitem_T *di;
4382 dict_T *d;
4383 typval_T *tv = NULL;
4384
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004385 if (argvars[0].v_type == VAR_BLOB)
4386 {
4387 int error = FALSE;
4388 int idx = tv_get_number_chk(&argvars[1], &error);
4389
4390 if (!error)
4391 {
4392 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004393 if (idx < 0)
4394 idx = blob_len(argvars[0].vval.v_blob) + idx;
4395 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4396 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004397 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004398 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004399 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004400 tv = rettv;
4401 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004402 }
4403 }
4404 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004405 {
4406 if ((l = argvars[0].vval.v_list) != NULL)
4407 {
4408 int error = FALSE;
4409
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004410 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004411 if (!error && li != NULL)
4412 tv = &li->li_tv;
4413 }
4414 }
4415 else if (argvars[0].v_type == VAR_DICT)
4416 {
4417 if ((d = argvars[0].vval.v_dict) != NULL)
4418 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004419 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004420 if (di != NULL)
4421 tv = &di->di_tv;
4422 }
4423 }
4424 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4425 {
4426 partial_T *pt;
4427 partial_T fref_pt;
4428
4429 if (argvars[0].v_type == VAR_PARTIAL)
4430 pt = argvars[0].vval.v_partial;
4431 else
4432 {
4433 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4434 fref_pt.pt_name = argvars[0].vval.v_string;
4435 pt = &fref_pt;
4436 }
4437
4438 if (pt != NULL)
4439 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004440 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004441 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004442
4443 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4444 {
4445 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004446 n = partial_name(pt);
4447 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004448 rettv->vval.v_string = NULL;
4449 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004450 {
4451 rettv->vval.v_string = vim_strsave(n);
4452 if (rettv->v_type == VAR_FUNC)
4453 func_ref(rettv->vval.v_string);
4454 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004455 }
4456 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004457 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004458 else if (STRCMP(what, "args") == 0)
4459 {
4460 rettv->v_type = VAR_LIST;
4461 if (rettv_list_alloc(rettv) == OK)
4462 {
4463 int i;
4464
4465 for (i = 0; i < pt->pt_argc; ++i)
4466 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4467 }
4468 }
4469 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004470 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004471 return;
4472 }
4473 }
4474 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004475 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004476
4477 if (tv == NULL)
4478 {
4479 if (argvars[2].v_type != VAR_UNKNOWN)
4480 copy_tv(&argvars[2], rettv);
4481 }
4482 else
4483 copy_tv(tv, rettv);
4484}
4485
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004486/*
4487 * Returns buffer options, variables and other attributes in a dictionary.
4488 */
4489 static dict_T *
4490get_buffer_info(buf_T *buf)
4491{
4492 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004493 tabpage_T *tp;
4494 win_T *wp;
4495 list_T *windows;
4496
4497 dict = dict_alloc();
4498 if (dict == NULL)
4499 return NULL;
4500
Bram Moolenaare0be1672018-07-08 16:50:37 +02004501 dict_add_number(dict, "bufnr", buf->b_fnum);
4502 dict_add_string(dict, "name", buf->b_ffname);
4503 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4504 : buflist_findlnum(buf));
4505 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4506 dict_add_number(dict, "listed", buf->b_p_bl);
4507 dict_add_number(dict, "changed", bufIsChanged(buf));
4508 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4509 dict_add_number(dict, "hidden",
4510 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004511
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004512 // Get a reference to buffer variables
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004513 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004514
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004515 // List of windows displaying this buffer
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004516 windows = list_alloc();
4517 if (windows != NULL)
4518 {
4519 FOR_ALL_TAB_WINDOWS(tp, wp)
4520 if (wp->w_buffer == buf)
4521 list_append_number(windows, (varnumber_T)wp->w_id);
4522 dict_add_list(dict, "windows", windows);
4523 }
4524
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004525#ifdef FEAT_TEXT_PROP
4526 // List of popup windows displaying this buffer
4527 windows = list_alloc();
4528 if (windows != NULL)
4529 {
4530 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4531 if (wp->w_buffer == buf)
4532 list_append_number(windows, (varnumber_T)wp->w_id);
4533 FOR_ALL_TABPAGES(tp)
4534 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
4535 if (wp->w_buffer == buf)
4536 list_append_number(windows, (varnumber_T)wp->w_id);
4537
4538 dict_add_list(dict, "popups", windows);
4539 }
4540#endif
4541
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004542#ifdef FEAT_SIGNS
4543 if (buf->b_signlist != NULL)
4544 {
4545 /* List of signs placed in this buffer */
4546 list_T *signs = list_alloc();
4547 if (signs != NULL)
4548 {
4549 get_buffer_signs(buf, signs);
4550 dict_add_list(dict, "signs", signs);
4551 }
4552 }
4553#endif
4554
4555 return dict;
4556}
4557
4558/*
4559 * "getbufinfo()" function
4560 */
4561 static void
4562f_getbufinfo(typval_T *argvars, typval_T *rettv)
4563{
4564 buf_T *buf = NULL;
4565 buf_T *argbuf = NULL;
4566 dict_T *d;
4567 int filtered = FALSE;
4568 int sel_buflisted = FALSE;
4569 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004570 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004571
4572 if (rettv_list_alloc(rettv) != OK)
4573 return;
4574
4575 /* List of all the buffers or selected buffers */
4576 if (argvars[0].v_type == VAR_DICT)
4577 {
4578 dict_T *sel_d = argvars[0].vval.v_dict;
4579
4580 if (sel_d != NULL)
4581 {
4582 dictitem_T *di;
4583
4584 filtered = TRUE;
4585
4586 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004587 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004588 sel_buflisted = TRUE;
4589
4590 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004591 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004592 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004593
4594 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004595 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004596 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004597 }
4598 }
4599 else if (argvars[0].v_type != VAR_UNKNOWN)
4600 {
4601 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004602 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004603 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004604 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004605 --emsg_off;
4606 if (argbuf == NULL)
4607 return;
4608 }
4609
4610 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004611 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004612 {
4613 if (argbuf != NULL && argbuf != buf)
4614 continue;
4615 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004616 || (sel_buflisted && !buf->b_p_bl)
4617 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004618 continue;
4619
4620 d = get_buffer_info(buf);
4621 if (d != NULL)
4622 list_append_dict(rettv->vval.v_list, d);
4623 if (argbuf != NULL)
4624 return;
4625 }
4626}
4627
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004628/*
4629 * Get line or list of lines from buffer "buf" into "rettv".
4630 * Return a range (from start to end) of lines in rettv from the specified
4631 * buffer.
4632 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4633 */
4634 static void
4635get_buffer_lines(
4636 buf_T *buf,
4637 linenr_T start,
4638 linenr_T end,
4639 int retlist,
4640 typval_T *rettv)
4641{
4642 char_u *p;
4643
4644 rettv->v_type = VAR_STRING;
4645 rettv->vval.v_string = NULL;
4646 if (retlist && rettv_list_alloc(rettv) == FAIL)
4647 return;
4648
4649 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4650 return;
4651
4652 if (!retlist)
4653 {
4654 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4655 p = ml_get_buf(buf, start, FALSE);
4656 else
4657 p = (char_u *)"";
4658 rettv->vval.v_string = vim_strsave(p);
4659 }
4660 else
4661 {
4662 if (end < start)
4663 return;
4664
4665 if (start < 1)
4666 start = 1;
4667 if (end > buf->b_ml.ml_line_count)
4668 end = buf->b_ml.ml_line_count;
4669 while (start <= end)
4670 if (list_append_string(rettv->vval.v_list,
4671 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4672 break;
4673 }
4674}
4675
4676/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004677 * "getbufline()" function
4678 */
4679 static void
4680f_getbufline(typval_T *argvars, typval_T *rettv)
4681{
4682 linenr_T lnum;
4683 linenr_T end;
4684 buf_T *buf;
4685
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004686 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004687 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004688 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004689 --emsg_off;
4690
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004691 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004692 if (argvars[2].v_type == VAR_UNKNOWN)
4693 end = lnum;
4694 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004695 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004696
4697 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4698}
4699
4700/*
4701 * "getbufvar()" function
4702 */
4703 static void
4704f_getbufvar(typval_T *argvars, typval_T *rettv)
4705{
4706 buf_T *buf;
4707 buf_T *save_curbuf;
4708 char_u *varname;
4709 dictitem_T *v;
4710 int done = FALSE;
4711
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004712 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4713 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004714 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004715 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004716
4717 rettv->v_type = VAR_STRING;
4718 rettv->vval.v_string = NULL;
4719
4720 if (buf != NULL && varname != NULL)
4721 {
4722 /* set curbuf to be our buf, temporarily */
4723 save_curbuf = curbuf;
4724 curbuf = buf;
4725
Bram Moolenaar30567352016-08-27 21:25:44 +02004726 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004727 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004728 if (varname[1] == NUL)
4729 {
4730 /* get all buffer-local options in a dict */
4731 dict_T *opts = get_winbuf_options(TRUE);
4732
4733 if (opts != NULL)
4734 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004735 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004736 done = TRUE;
4737 }
4738 }
4739 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4740 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004741 done = TRUE;
4742 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004743 else
4744 {
4745 /* Look up the variable. */
4746 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4747 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4748 'b', varname, FALSE);
4749 if (v != NULL)
4750 {
4751 copy_tv(&v->di_tv, rettv);
4752 done = TRUE;
4753 }
4754 }
4755
4756 /* restore previous notion of curbuf */
4757 curbuf = save_curbuf;
4758 }
4759
4760 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4761 /* use the default value */
4762 copy_tv(&argvars[2], rettv);
4763
4764 --emsg_off;
4765}
4766
4767/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004768 * "getchangelist()" function
4769 */
4770 static void
4771f_getchangelist(typval_T *argvars, typval_T *rettv)
4772{
4773#ifdef FEAT_JUMPLIST
4774 buf_T *buf;
4775 int i;
4776 list_T *l;
4777 dict_T *d;
4778#endif
4779
4780 if (rettv_list_alloc(rettv) != OK)
4781 return;
4782
4783#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004784 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004785 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004786 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004787 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004788 if (buf == NULL)
4789 return;
4790
4791 l = list_alloc();
4792 if (l == NULL)
4793 return;
4794
4795 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4796 return;
4797 /*
4798 * The current window change list index tracks only the position in the
4799 * current buffer change list. For other buffers, use the change list
4800 * length as the current index.
4801 */
4802 list_append_number(rettv->vval.v_list,
4803 (varnumber_T)((buf == curwin->w_buffer)
4804 ? curwin->w_changelistidx : buf->b_changelistlen));
4805
4806 for (i = 0; i < buf->b_changelistlen; ++i)
4807 {
4808 if (buf->b_changelist[i].lnum == 0)
4809 continue;
4810 if ((d = dict_alloc()) == NULL)
4811 return;
4812 if (list_append_dict(l, d) == FAIL)
4813 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004814 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4815 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004816 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004817 }
4818#endif
4819}
4820/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004821 * "getchar()" function
4822 */
4823 static void
4824f_getchar(typval_T *argvars, typval_T *rettv)
4825{
4826 varnumber_T n;
4827 int error = FALSE;
4828
Bram Moolenaar84d93902018-09-11 20:10:20 +02004829#ifdef MESSAGE_QUEUE
4830 // vpeekc() used to check for messages, but that caused problems, invoking
4831 // a callback where it was not expected. Some plugins use getchar(1) in a
4832 // loop to await a message, therefore make sure we check for messages here.
4833 parse_queued_messages();
4834#endif
4835
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004836 /* Position the cursor. Needed after a message that ends in a space. */
4837 windgoto(msg_row, msg_col);
4838
4839 ++no_mapping;
4840 ++allow_keys;
4841 for (;;)
4842 {
4843 if (argvars[0].v_type == VAR_UNKNOWN)
4844 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004845 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004846 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004847 /* getchar(1): only check if char avail */
4848 n = vpeekc_any();
4849 else if (error || vpeekc_any() == NUL)
4850 /* illegal argument or getchar(0) and no char avail: return zero */
4851 n = 0;
4852 else
4853 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004854 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004855
4856 if (n == K_IGNORE)
4857 continue;
4858 break;
4859 }
4860 --no_mapping;
4861 --allow_keys;
4862
4863 set_vim_var_nr(VV_MOUSE_WIN, 0);
4864 set_vim_var_nr(VV_MOUSE_WINID, 0);
4865 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4866 set_vim_var_nr(VV_MOUSE_COL, 0);
4867
4868 rettv->vval.v_number = n;
4869 if (IS_SPECIAL(n) || mod_mask != 0)
4870 {
4871 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4872 int i = 0;
4873
4874 /* Turn a special key into three bytes, plus modifier. */
4875 if (mod_mask != 0)
4876 {
4877 temp[i++] = K_SPECIAL;
4878 temp[i++] = KS_MODIFIER;
4879 temp[i++] = mod_mask;
4880 }
4881 if (IS_SPECIAL(n))
4882 {
4883 temp[i++] = K_SPECIAL;
4884 temp[i++] = K_SECOND(n);
4885 temp[i++] = K_THIRD(n);
4886 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004887 else if (has_mbyte)
4888 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004889 else
4890 temp[i++] = n;
4891 temp[i++] = NUL;
4892 rettv->v_type = VAR_STRING;
4893 rettv->vval.v_string = vim_strsave(temp);
4894
4895#ifdef FEAT_MOUSE
4896 if (is_mouse_key(n))
4897 {
4898 int row = mouse_row;
4899 int col = mouse_col;
4900 win_T *win;
4901 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004902 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004903 int winnr = 1;
4904
4905 if (row >= 0 && col >= 0)
4906 {
4907 /* Find the window at the mouse coordinates and compute the
4908 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004909 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004910 if (win == NULL)
4911 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004912 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004913# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004914 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004915 winnr = 0;
4916 else
4917# endif
4918 for (wp = firstwin; wp != win && wp != NULL;
4919 wp = wp->w_next)
4920 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004921 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4922 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4923 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4924 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4925 }
4926 }
4927#endif
4928 }
4929}
4930
4931/*
4932 * "getcharmod()" function
4933 */
4934 static void
4935f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4936{
4937 rettv->vval.v_number = mod_mask;
4938}
4939
4940/*
4941 * "getcharsearch()" function
4942 */
4943 static void
4944f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4945{
4946 if (rettv_dict_alloc(rettv) != FAIL)
4947 {
4948 dict_T *dict = rettv->vval.v_dict;
4949
Bram Moolenaare0be1672018-07-08 16:50:37 +02004950 dict_add_string(dict, "char", last_csearch());
4951 dict_add_number(dict, "forward", last_csearch_forward());
4952 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004953 }
4954}
4955
4956/*
4957 * "getcmdline()" function
4958 */
4959 static void
4960f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4961{
4962 rettv->v_type = VAR_STRING;
4963 rettv->vval.v_string = get_cmdline_str();
4964}
4965
4966/*
4967 * "getcmdpos()" function
4968 */
4969 static void
4970f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4971{
4972 rettv->vval.v_number = get_cmdline_pos() + 1;
4973}
4974
4975/*
4976 * "getcmdtype()" function
4977 */
4978 static void
4979f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4980{
4981 rettv->v_type = VAR_STRING;
4982 rettv->vval.v_string = alloc(2);
4983 if (rettv->vval.v_string != NULL)
4984 {
4985 rettv->vval.v_string[0] = get_cmdline_type();
4986 rettv->vval.v_string[1] = NUL;
4987 }
4988}
4989
4990/*
4991 * "getcmdwintype()" function
4992 */
4993 static void
4994f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4995{
4996 rettv->v_type = VAR_STRING;
4997 rettv->vval.v_string = NULL;
4998#ifdef FEAT_CMDWIN
4999 rettv->vval.v_string = alloc(2);
5000 if (rettv->vval.v_string != NULL)
5001 {
5002 rettv->vval.v_string[0] = cmdwin_type;
5003 rettv->vval.v_string[1] = NUL;
5004 }
5005#endif
5006}
5007
5008#if defined(FEAT_CMDL_COMPL)
5009/*
5010 * "getcompletion()" function
5011 */
5012 static void
5013f_getcompletion(typval_T *argvars, typval_T *rettv)
5014{
5015 char_u *pat;
5016 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005017 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005018 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5019 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005020
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005021 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005022 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005023
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005024 if (p_wic)
5025 options |= WILD_ICASE;
5026
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005027 /* For filtered results, 'wildignore' is used */
5028 if (!filtered)
5029 options |= WILD_KEEP_ALL;
5030
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005031 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005032 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005033 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005034 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005035 if (xpc.xp_context == EXPAND_NOTHING)
5036 {
5037 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005038 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005039 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005040 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005041 return;
5042 }
5043
5044# if defined(FEAT_MENU)
5045 if (xpc.xp_context == EXPAND_MENUS)
5046 {
5047 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5048 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5049 }
5050# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005051#ifdef FEAT_CSCOPE
5052 if (xpc.xp_context == EXPAND_CSCOPE)
5053 {
5054 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5055 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5056 }
5057#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005058#ifdef FEAT_SIGNS
5059 if (xpc.xp_context == EXPAND_SIGN)
5060 {
5061 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5062 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5063 }
5064#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005065
5066 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5067 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5068 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005069 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005070
5071 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5072
5073 for (i = 0; i < xpc.xp_numfiles; i++)
5074 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5075 }
5076 vim_free(pat);
5077 ExpandCleanup(&xpc);
5078}
5079#endif
5080
5081/*
5082 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005083 *
5084 * Return the current working directory of a window in a tab page.
5085 * First optional argument 'winnr' is the window number or -1 and the second
5086 * optional argument 'tabnr' is the tab page number.
5087 *
5088 * If no arguments are supplied, then return the directory of the current
5089 * window.
5090 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5091 * the specified window.
5092 * If 'winnr' is 0 then return the directory of the current window.
5093 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5094 * directory of the specified tab page. Otherwise return the directory of the
5095 * specified window in the specified tab page.
5096 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005097 */
5098 static void
5099f_getcwd(typval_T *argvars, typval_T *rettv)
5100{
5101 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005102 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005103 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005104 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005105
5106 rettv->v_type = VAR_STRING;
5107 rettv->vval.v_string = NULL;
5108
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005109 if (argvars[0].v_type == VAR_NUMBER
5110 && argvars[0].vval.v_number == -1
5111 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005112 global = TRUE;
5113 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005114 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005115
5116 if (wp != NULL && wp->w_localdir != NULL)
5117 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005118 else if (tp != NULL && tp->tp_localdir != NULL)
5119 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5120 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005121 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005122 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005123 rettv->vval.v_string = vim_strsave(globaldir);
5124 else
5125 {
5126 cwd = alloc(MAXPATHL);
5127 if (cwd != NULL)
5128 {
5129 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5130 rettv->vval.v_string = vim_strsave(cwd);
5131 vim_free(cwd);
5132 }
5133 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005134 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005135#ifdef BACKSLASH_IN_FILENAME
5136 if (rettv->vval.v_string != NULL)
5137 slash_adjust(rettv->vval.v_string);
5138#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005139}
5140
5141/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005142 * "getenv()" function
5143 */
5144 static void
5145f_getenv(typval_T *argvars, typval_T *rettv)
5146{
5147 int mustfree = FALSE;
5148 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5149
5150 if (p == NULL)
5151 {
5152 rettv->v_type = VAR_SPECIAL;
5153 rettv->vval.v_number = VVAL_NULL;
5154 return;
5155 }
5156 if (!mustfree)
5157 p = vim_strsave(p);
5158 rettv->vval.v_string = p;
5159 rettv->v_type = VAR_STRING;
5160}
5161
5162/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005163 * "getfontname()" function
5164 */
5165 static void
5166f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5167{
5168 rettv->v_type = VAR_STRING;
5169 rettv->vval.v_string = NULL;
5170#ifdef FEAT_GUI
5171 if (gui.in_use)
5172 {
5173 GuiFont font;
5174 char_u *name = NULL;
5175
5176 if (argvars[0].v_type == VAR_UNKNOWN)
5177 {
5178 /* Get the "Normal" font. Either the name saved by
5179 * hl_set_font_name() or from the font ID. */
5180 font = gui.norm_font;
5181 name = hl_get_font_name();
5182 }
5183 else
5184 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005185 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005186 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5187 return;
5188 font = gui_mch_get_font(name, FALSE);
5189 if (font == NOFONT)
5190 return; /* Invalid font name, return empty string. */
5191 }
5192 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5193 if (argvars[0].v_type != VAR_UNKNOWN)
5194 gui_mch_free_font(font);
5195 }
5196#endif
5197}
5198
5199/*
5200 * "getfperm({fname})" function
5201 */
5202 static void
5203f_getfperm(typval_T *argvars, typval_T *rettv)
5204{
5205 char_u *fname;
5206 stat_T st;
5207 char_u *perm = NULL;
5208 char_u flags[] = "rwx";
5209 int i;
5210
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005211 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005212
5213 rettv->v_type = VAR_STRING;
5214 if (mch_stat((char *)fname, &st) >= 0)
5215 {
5216 perm = vim_strsave((char_u *)"---------");
5217 if (perm != NULL)
5218 {
5219 for (i = 0; i < 9; i++)
5220 {
5221 if (st.st_mode & (1 << (8 - i)))
5222 perm[i] = flags[i % 3];
5223 }
5224 }
5225 }
5226 rettv->vval.v_string = perm;
5227}
5228
5229/*
5230 * "getfsize({fname})" function
5231 */
5232 static void
5233f_getfsize(typval_T *argvars, typval_T *rettv)
5234{
5235 char_u *fname;
5236 stat_T st;
5237
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005238 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005239
5240 rettv->v_type = VAR_NUMBER;
5241
5242 if (mch_stat((char *)fname, &st) >= 0)
5243 {
5244 if (mch_isdir(fname))
5245 rettv->vval.v_number = 0;
5246 else
5247 {
5248 rettv->vval.v_number = (varnumber_T)st.st_size;
5249
5250 /* non-perfect check for overflow */
5251 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5252 rettv->vval.v_number = -2;
5253 }
5254 }
5255 else
5256 rettv->vval.v_number = -1;
5257}
5258
5259/*
5260 * "getftime({fname})" function
5261 */
5262 static void
5263f_getftime(typval_T *argvars, typval_T *rettv)
5264{
5265 char_u *fname;
5266 stat_T st;
5267
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005268 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005269
5270 if (mch_stat((char *)fname, &st) >= 0)
5271 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5272 else
5273 rettv->vval.v_number = -1;
5274}
5275
5276/*
5277 * "getftype({fname})" function
5278 */
5279 static void
5280f_getftype(typval_T *argvars, typval_T *rettv)
5281{
5282 char_u *fname;
5283 stat_T st;
5284 char_u *type = NULL;
5285 char *t;
5286
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005287 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005288
5289 rettv->v_type = VAR_STRING;
5290 if (mch_lstat((char *)fname, &st) >= 0)
5291 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005292 if (S_ISREG(st.st_mode))
5293 t = "file";
5294 else if (S_ISDIR(st.st_mode))
5295 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005296 else if (S_ISLNK(st.st_mode))
5297 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005298 else if (S_ISBLK(st.st_mode))
5299 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005300 else if (S_ISCHR(st.st_mode))
5301 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005302 else if (S_ISFIFO(st.st_mode))
5303 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005304 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005305 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005306 else
5307 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005308 type = vim_strsave((char_u *)t);
5309 }
5310 rettv->vval.v_string = type;
5311}
5312
5313/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005314 * "getjumplist()" function
5315 */
5316 static void
5317f_getjumplist(typval_T *argvars, typval_T *rettv)
5318{
5319#ifdef FEAT_JUMPLIST
5320 win_T *wp;
5321 int i;
5322 list_T *l;
5323 dict_T *d;
5324#endif
5325
5326 if (rettv_list_alloc(rettv) != OK)
5327 return;
5328
5329#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005330 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005331 if (wp == NULL)
5332 return;
5333
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005334 cleanup_jumplist(wp, TRUE);
5335
Bram Moolenaar4f505882018-02-10 21:06:32 +01005336 l = list_alloc();
5337 if (l == NULL)
5338 return;
5339
5340 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5341 return;
5342 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5343
5344 for (i = 0; i < wp->w_jumplistlen; ++i)
5345 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005346 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5347 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005348 if ((d = dict_alloc()) == NULL)
5349 return;
5350 if (list_append_dict(l, d) == FAIL)
5351 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005352 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5353 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005354 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005355 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005356 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005357 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005358 }
5359#endif
5360}
5361
5362/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005363 * "getline(lnum, [end])" function
5364 */
5365 static void
5366f_getline(typval_T *argvars, typval_T *rettv)
5367{
5368 linenr_T lnum;
5369 linenr_T end;
5370 int retlist;
5371
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005372 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005373 if (argvars[1].v_type == VAR_UNKNOWN)
5374 {
5375 end = 0;
5376 retlist = FALSE;
5377 }
5378 else
5379 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005380 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005381 retlist = TRUE;
5382 }
5383
5384 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5385}
5386
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005387#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005388 static void
5389get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5390{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005391 if (what_arg->v_type == VAR_UNKNOWN)
5392 {
5393 if (rettv_list_alloc(rettv) == OK)
5394 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005395 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005396 }
5397 else
5398 {
5399 if (rettv_dict_alloc(rettv) == OK)
5400 if (is_qf || (wp != NULL))
5401 {
5402 if (what_arg->v_type == VAR_DICT)
5403 {
5404 dict_T *d = what_arg->vval.v_dict;
5405
5406 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005407 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005408 }
5409 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005410 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005411 }
5412 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005413}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005414#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005415
5416/*
5417 * "getloclist()" function
5418 */
5419 static void
5420f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5421{
5422#ifdef FEAT_QUICKFIX
5423 win_T *wp;
5424
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005425 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005426 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5427#endif
5428}
5429
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005430/*
5431 * "getmatches()" function
5432 */
5433 static void
5434f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5435{
5436#ifdef FEAT_SEARCH_EXTRA
5437 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005438 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005439 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005440 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005441
Bram Moolenaaraff74912019-03-30 18:11:49 +01005442 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5443 return;
5444
5445 cur = win->w_match_head;
5446 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005447 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005448 dict = dict_alloc();
5449 if (dict == NULL)
5450 return;
5451 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005452 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005453 /* match added with matchaddpos() */
5454 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005455 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005456 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005457 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005458 list_T *l;
5459
5460 llpos = &cur->pos.pos[i];
5461 if (llpos->lnum == 0)
5462 break;
5463 l = list_alloc();
5464 if (l == NULL)
5465 break;
5466 list_append_number(l, (varnumber_T)llpos->lnum);
5467 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005468 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005469 list_append_number(l, (varnumber_T)llpos->col);
5470 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005471 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005472 sprintf(buf, "pos%d", i + 1);
5473 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005474 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005475 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005476 else
5477 {
5478 dict_add_string(dict, "pattern", cur->pattern);
5479 }
5480 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5481 dict_add_number(dict, "priority", (long)cur->priority);
5482 dict_add_number(dict, "id", (long)cur->id);
5483# if defined(FEAT_CONCEAL)
5484 if (cur->conceal_char)
5485 {
5486 char_u buf[MB_MAXBYTES + 1];
5487
5488 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5489 dict_add_string(dict, "conceal", (char_u *)&buf);
5490 }
5491# endif
5492 list_append_dict(rettv->vval.v_list, dict);
5493 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005494 }
5495#endif
5496}
5497
5498/*
5499 * "getpid()" function
5500 */
5501 static void
5502f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5503{
5504 rettv->vval.v_number = mch_get_pid();
5505}
5506
5507 static void
5508getpos_both(
5509 typval_T *argvars,
5510 typval_T *rettv,
5511 int getcurpos)
5512{
5513 pos_T *fp;
5514 list_T *l;
5515 int fnum = -1;
5516
5517 if (rettv_list_alloc(rettv) == OK)
5518 {
5519 l = rettv->vval.v_list;
5520 if (getcurpos)
5521 fp = &curwin->w_cursor;
5522 else
5523 fp = var2fpos(&argvars[0], TRUE, &fnum);
5524 if (fnum != -1)
5525 list_append_number(l, (varnumber_T)fnum);
5526 else
5527 list_append_number(l, (varnumber_T)0);
5528 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5529 : (varnumber_T)0);
5530 list_append_number(l, (fp != NULL)
5531 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5532 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005533 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005534 (varnumber_T)0);
5535 if (getcurpos)
5536 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005537 int save_set_curswant = curwin->w_set_curswant;
5538 colnr_T save_curswant = curwin->w_curswant;
5539 colnr_T save_virtcol = curwin->w_virtcol;
5540
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005541 update_curswant();
5542 list_append_number(l, curwin->w_curswant == MAXCOL ?
5543 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005544
5545 // Do not change "curswant", as it is unexpected that a get
5546 // function has a side effect.
5547 if (save_set_curswant)
5548 {
5549 curwin->w_set_curswant = save_set_curswant;
5550 curwin->w_curswant = save_curswant;
5551 curwin->w_virtcol = save_virtcol;
5552 curwin->w_valid &= ~VALID_VIRTCOL;
5553 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005554 }
5555 }
5556 else
5557 rettv->vval.v_number = FALSE;
5558}
5559
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005560/*
5561 * "getcurpos()" function
5562 */
5563 static void
5564f_getcurpos(typval_T *argvars, typval_T *rettv)
5565{
5566 getpos_both(argvars, rettv, TRUE);
5567}
5568
5569/*
5570 * "getpos(string)" function
5571 */
5572 static void
5573f_getpos(typval_T *argvars, typval_T *rettv)
5574{
5575 getpos_both(argvars, rettv, FALSE);
5576}
5577
5578/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005579 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005580 */
5581 static void
5582f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5583{
5584#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005585 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005586#endif
5587}
5588
5589/*
5590 * "getreg()" function
5591 */
5592 static void
5593f_getreg(typval_T *argvars, typval_T *rettv)
5594{
5595 char_u *strregname;
5596 int regname;
5597 int arg2 = FALSE;
5598 int return_list = FALSE;
5599 int error = FALSE;
5600
5601 if (argvars[0].v_type != VAR_UNKNOWN)
5602 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005603 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005604 error = strregname == NULL;
5605 if (argvars[1].v_type != VAR_UNKNOWN)
5606 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005607 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005608 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005609 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005610 }
5611 }
5612 else
5613 strregname = get_vim_var_str(VV_REG);
5614
5615 if (error)
5616 return;
5617
5618 regname = (strregname == NULL ? '"' : *strregname);
5619 if (regname == 0)
5620 regname = '"';
5621
5622 if (return_list)
5623 {
5624 rettv->v_type = VAR_LIST;
5625 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5626 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5627 if (rettv->vval.v_list == NULL)
5628 (void)rettv_list_alloc(rettv);
5629 else
5630 ++rettv->vval.v_list->lv_refcount;
5631 }
5632 else
5633 {
5634 rettv->v_type = VAR_STRING;
5635 rettv->vval.v_string = get_reg_contents(regname,
5636 arg2 ? GREG_EXPR_SRC : 0);
5637 }
5638}
5639
5640/*
5641 * "getregtype()" function
5642 */
5643 static void
5644f_getregtype(typval_T *argvars, typval_T *rettv)
5645{
5646 char_u *strregname;
5647 int regname;
5648 char_u buf[NUMBUFLEN + 2];
5649 long reglen = 0;
5650
5651 if (argvars[0].v_type != VAR_UNKNOWN)
5652 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005653 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005654 if (strregname == NULL) /* type error; errmsg already given */
5655 {
5656 rettv->v_type = VAR_STRING;
5657 rettv->vval.v_string = NULL;
5658 return;
5659 }
5660 }
5661 else
5662 /* Default to v:register */
5663 strregname = get_vim_var_str(VV_REG);
5664
5665 regname = (strregname == NULL ? '"' : *strregname);
5666 if (regname == 0)
5667 regname = '"';
5668
5669 buf[0] = NUL;
5670 buf[1] = NUL;
5671 switch (get_reg_type(regname, &reglen))
5672 {
5673 case MLINE: buf[0] = 'V'; break;
5674 case MCHAR: buf[0] = 'v'; break;
5675 case MBLOCK:
5676 buf[0] = Ctrl_V;
5677 sprintf((char *)buf + 1, "%ld", reglen + 1);
5678 break;
5679 }
5680 rettv->v_type = VAR_STRING;
5681 rettv->vval.v_string = vim_strsave(buf);
5682}
5683
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005684/*
5685 * Returns information (variables, options, etc.) about a tab page
5686 * as a dictionary.
5687 */
5688 static dict_T *
5689get_tabpage_info(tabpage_T *tp, int tp_idx)
5690{
5691 win_T *wp;
5692 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005693 list_T *l;
5694
5695 dict = dict_alloc();
5696 if (dict == NULL)
5697 return NULL;
5698
Bram Moolenaare0be1672018-07-08 16:50:37 +02005699 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005700
5701 l = list_alloc();
5702 if (l != NULL)
5703 {
5704 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02005705 wp != NULL; wp = wp->w_next)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005706 list_append_number(l, (varnumber_T)wp->w_id);
5707 dict_add_list(dict, "windows", l);
5708 }
5709
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005710 /* Make a reference to tabpage variables */
5711 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005712
5713 return dict;
5714}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005715
5716/*
5717 * "gettabinfo()" function
5718 */
5719 static void
5720f_gettabinfo(typval_T *argvars, typval_T *rettv)
5721{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005722 tabpage_T *tp, *tparg = NULL;
5723 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005724 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005725
5726 if (rettv_list_alloc(rettv) != OK)
5727 return;
5728
5729 if (argvars[0].v_type != VAR_UNKNOWN)
5730 {
5731 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005732 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005733 if (tparg == NULL)
5734 return;
5735 }
5736
5737 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005738 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005739 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005740 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005741 if (tparg != NULL && tp != tparg)
5742 continue;
5743 d = get_tabpage_info(tp, tpnr);
5744 if (d != NULL)
5745 list_append_dict(rettv->vval.v_list, d);
5746 if (tparg != NULL)
5747 return;
5748 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005749}
5750
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005751/*
5752 * "gettabvar()" function
5753 */
5754 static void
5755f_gettabvar(typval_T *argvars, typval_T *rettv)
5756{
5757 win_T *oldcurwin;
5758 tabpage_T *tp, *oldtabpage;
5759 dictitem_T *v;
5760 char_u *varname;
5761 int done = FALSE;
5762
5763 rettv->v_type = VAR_STRING;
5764 rettv->vval.v_string = NULL;
5765
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005766 varname = tv_get_string_chk(&argvars[1]);
5767 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005768 if (tp != NULL && varname != NULL)
5769 {
5770 /* Set tp to be our tabpage, temporarily. Also set the window to the
5771 * first window in the tabpage, otherwise the window is not valid. */
5772 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005773 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5774 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005775 {
5776 /* look up the variable */
5777 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5778 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5779 if (v != NULL)
5780 {
5781 copy_tv(&v->di_tv, rettv);
5782 done = TRUE;
5783 }
5784 }
5785
5786 /* restore previous notion of curwin */
5787 restore_win(oldcurwin, oldtabpage, TRUE);
5788 }
5789
5790 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5791 copy_tv(&argvars[2], rettv);
5792}
5793
5794/*
5795 * "gettabwinvar()" function
5796 */
5797 static void
5798f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5799{
5800 getwinvar(argvars, rettv, 1);
5801}
5802
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005803/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005804 * "gettagstack()" function
5805 */
5806 static void
5807f_gettagstack(typval_T *argvars, typval_T *rettv)
5808{
5809 win_T *wp = curwin; // default is current window
5810
5811 if (rettv_dict_alloc(rettv) != OK)
5812 return;
5813
5814 if (argvars[0].v_type != VAR_UNKNOWN)
5815 {
5816 wp = find_win_by_nr_or_id(&argvars[0]);
5817 if (wp == NULL)
5818 return;
5819 }
5820
5821 get_tagstack(wp, rettv->vval.v_dict);
5822}
5823
5824/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005825 * Returns information about a window as a dictionary.
5826 */
5827 static dict_T *
5828get_win_info(win_T *wp, short tpnr, short winnr)
5829{
5830 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005831
5832 dict = dict_alloc();
5833 if (dict == NULL)
5834 return NULL;
5835
Bram Moolenaare0be1672018-07-08 16:50:37 +02005836 dict_add_number(dict, "tabnr", tpnr);
5837 dict_add_number(dict, "winnr", winnr);
5838 dict_add_number(dict, "winid", wp->w_id);
5839 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005840 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005841 dict_add_number(dict, "topline", wp->w_topline);
5842 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005843#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005844 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005845#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005846 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005847 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005848 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005849
Bram Moolenaar69905d12017-08-13 18:14:47 +02005850#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005851 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005852#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005853#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005854 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5855 dict_add_number(dict, "loclist",
5856 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005857#endif
5858
Bram Moolenaar30567352016-08-27 21:25:44 +02005859 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005860 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005861
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005862 return dict;
5863}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005864
5865/*
5866 * "getwininfo()" function
5867 */
5868 static void
5869f_getwininfo(typval_T *argvars, typval_T *rettv)
5870{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005871 tabpage_T *tp;
5872 win_T *wp = NULL, *wparg = NULL;
5873 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005874 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005875
5876 if (rettv_list_alloc(rettv) != OK)
5877 return;
5878
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005879 if (argvars[0].v_type != VAR_UNKNOWN)
5880 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005881 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005882 if (wparg == NULL)
5883 return;
5884 }
5885
5886 /* Collect information about either all the windows across all the tab
5887 * pages or one particular window.
5888 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005889 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005890 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005891 tabnr++;
5892 winnr = 0;
5893 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005894 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005895 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005896 if (wparg != NULL && wp != wparg)
5897 continue;
5898 d = get_win_info(wp, tabnr, winnr);
5899 if (d != NULL)
5900 list_append_dict(rettv->vval.v_list, d);
5901 if (wparg != NULL)
5902 /* found information about a specific window */
5903 return;
5904 }
5905 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005906}
5907
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005908/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005909 * "win_execute()" function
5910 */
5911 static void
5912f_win_execute(typval_T *argvars, typval_T *rettv)
5913{
5914 int id = (int)tv_get_number(argvars);
5915 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005916 win_T *save_curwin;
5917 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005918
5919 if (wp != NULL)
5920 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005921 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5922 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005923 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005924 check_cursor();
5925 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005926 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005927 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005928 }
5929}
5930
5931/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005932 * "win_findbuf()" function
5933 */
5934 static void
5935f_win_findbuf(typval_T *argvars, typval_T *rettv)
5936{
5937 if (rettv_list_alloc(rettv) != FAIL)
5938 win_findbuf(argvars, rettv->vval.v_list);
5939}
5940
5941/*
5942 * "win_getid()" function
5943 */
5944 static void
5945f_win_getid(typval_T *argvars, typval_T *rettv)
5946{
5947 rettv->vval.v_number = win_getid(argvars);
5948}
5949
5950/*
5951 * "win_gotoid()" function
5952 */
5953 static void
5954f_win_gotoid(typval_T *argvars, typval_T *rettv)
5955{
5956 rettv->vval.v_number = win_gotoid(argvars);
5957}
5958
5959/*
5960 * "win_id2tabwin()" function
5961 */
5962 static void
5963f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5964{
5965 if (rettv_list_alloc(rettv) != FAIL)
5966 win_id2tabwin(argvars, rettv->vval.v_list);
5967}
5968
5969/*
5970 * "win_id2win()" function
5971 */
5972 static void
5973f_win_id2win(typval_T *argvars, typval_T *rettv)
5974{
5975 rettv->vval.v_number = win_id2win(argvars);
5976}
5977
5978/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005979 * "win_screenpos()" function
5980 */
5981 static void
5982f_win_screenpos(typval_T *argvars, typval_T *rettv)
5983{
5984 win_T *wp;
5985
5986 if (rettv_list_alloc(rettv) == FAIL)
5987 return;
5988
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005989 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005990 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5991 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5992}
5993
5994/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005995 * "getwinpos({timeout})" function
5996 */
5997 static void
5998f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5999{
6000 int x = -1;
6001 int y = -1;
6002
6003 if (rettv_list_alloc(rettv) == FAIL)
6004 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006005#if defined(FEAT_GUI) \
6006 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6007 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006008 {
6009 varnumber_T timeout = 100;
6010
6011 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006012 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006013
6014 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006015 }
6016#endif
6017 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6018 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6019}
6020
6021
6022/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006023 * "getwinposx()" function
6024 */
6025 static void
6026f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6027{
6028 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006029#if defined(FEAT_GUI) \
6030 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6031 || defined(MSWIN)
6032
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006033 {
6034 int x, y;
6035
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006036 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006037 rettv->vval.v_number = x;
6038 }
6039#endif
6040}
6041
6042/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006043 * "getwinposy()" function
6044 */
6045 static void
6046f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6047{
6048 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006049#if defined(FEAT_GUI) \
6050 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6051 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006052 {
6053 int x, y;
6054
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006055 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006056 rettv->vval.v_number = y;
6057 }
6058#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006059}
6060
6061/*
6062 * "getwinvar()" function
6063 */
6064 static void
6065f_getwinvar(typval_T *argvars, typval_T *rettv)
6066{
6067 getwinvar(argvars, rettv, 0);
6068}
6069
6070/*
6071 * "glob()" function
6072 */
6073 static void
6074f_glob(typval_T *argvars, typval_T *rettv)
6075{
6076 int options = WILD_SILENT|WILD_USE_NL;
6077 expand_T xpc;
6078 int error = FALSE;
6079
6080 /* When the optional second argument is non-zero, don't remove matches
6081 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6082 rettv->v_type = VAR_STRING;
6083 if (argvars[1].v_type != VAR_UNKNOWN)
6084 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006085 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006086 options |= WILD_KEEP_ALL;
6087 if (argvars[2].v_type != VAR_UNKNOWN)
6088 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006089 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006090 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006091 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006092 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006093 options |= WILD_ALLLINKS;
6094 }
6095 }
6096 if (!error)
6097 {
6098 ExpandInit(&xpc);
6099 xpc.xp_context = EXPAND_FILES;
6100 if (p_wic)
6101 options += WILD_ICASE;
6102 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006103 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006104 NULL, options, WILD_ALL);
6105 else if (rettv_list_alloc(rettv) != FAIL)
6106 {
6107 int i;
6108
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006109 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006110 NULL, options, WILD_ALL_KEEP);
6111 for (i = 0; i < xpc.xp_numfiles; i++)
6112 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6113
6114 ExpandCleanup(&xpc);
6115 }
6116 }
6117 else
6118 rettv->vval.v_string = NULL;
6119}
6120
6121/*
6122 * "globpath()" function
6123 */
6124 static void
6125f_globpath(typval_T *argvars, typval_T *rettv)
6126{
6127 int flags = 0;
6128 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006129 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006130 int error = FALSE;
6131 garray_T ga;
6132 int i;
6133
6134 /* When the optional second argument is non-zero, don't remove matches
6135 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6136 rettv->v_type = VAR_STRING;
6137 if (argvars[2].v_type != VAR_UNKNOWN)
6138 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006139 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006140 flags |= WILD_KEEP_ALL;
6141 if (argvars[3].v_type != VAR_UNKNOWN)
6142 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006143 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006144 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006145 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006146 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006147 flags |= WILD_ALLLINKS;
6148 }
6149 }
6150 if (file != NULL && !error)
6151 {
6152 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006153 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006154 if (rettv->v_type == VAR_STRING)
6155 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6156 else if (rettv_list_alloc(rettv) != FAIL)
6157 for (i = 0; i < ga.ga_len; ++i)
6158 list_append_string(rettv->vval.v_list,
6159 ((char_u **)(ga.ga_data))[i], -1);
6160 ga_clear_strings(&ga);
6161 }
6162 else
6163 rettv->vval.v_string = NULL;
6164}
6165
6166/*
6167 * "glob2regpat()" function
6168 */
6169 static void
6170f_glob2regpat(typval_T *argvars, typval_T *rettv)
6171{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006172 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006173
6174 rettv->v_type = VAR_STRING;
6175 rettv->vval.v_string = (pat == NULL)
6176 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6177}
6178
6179/* for VIM_VERSION_ defines */
6180#include "version.h"
6181
6182/*
6183 * "has()" function
6184 */
6185 static void
6186f_has(typval_T *argvars, typval_T *rettv)
6187{
6188 int i;
6189 char_u *name;
6190 int n = FALSE;
6191 static char *(has_list[]) =
6192 {
6193#ifdef AMIGA
6194 "amiga",
6195# ifdef FEAT_ARP
6196 "arp",
6197# endif
6198#endif
6199#ifdef __BEOS__
6200 "beos",
6201#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006202#if defined(BSD) && !defined(MACOS_X)
6203 "bsd",
6204#endif
6205#ifdef hpux
6206 "hpux",
6207#endif
6208#ifdef __linux__
6209 "linux",
6210#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006211#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006212 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6213 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006214# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006215 "macunix", /* Mac OS X, with the darwin feature */
6216 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006217# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006218#endif
6219#ifdef __QNX__
6220 "qnx",
6221#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006222#ifdef SUN_SYSTEM
6223 "sun",
6224#else
6225 "moon",
6226#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006227#ifdef UNIX
6228 "unix",
6229#endif
6230#ifdef VMS
6231 "vms",
6232#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006233#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006234 "win32",
6235#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006236#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006237 "win32unix",
6238#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006239#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006240 "win64",
6241#endif
6242#ifdef EBCDIC
6243 "ebcdic",
6244#endif
6245#ifndef CASE_INSENSITIVE_FILENAME
6246 "fname_case",
6247#endif
6248#ifdef HAVE_ACL
6249 "acl",
6250#endif
6251#ifdef FEAT_ARABIC
6252 "arabic",
6253#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006255#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006256 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006257#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006258#ifdef FEAT_AUTOSERVERNAME
6259 "autoservername",
6260#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006261#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006262 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006263# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006264 "balloon_multiline",
6265# endif
6266#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006267#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006268 "balloon_eval_term",
6269#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006270#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6271 "builtin_terms",
6272# ifdef ALL_BUILTIN_TCAPS
6273 "all_builtin_terms",
6274# endif
6275#endif
6276#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006277 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006278 || defined(FEAT_GUI_MOTIF))
6279 "browsefilter",
6280#endif
6281#ifdef FEAT_BYTEOFF
6282 "byte_offset",
6283#endif
6284#ifdef FEAT_JOB_CHANNEL
6285 "channel",
6286#endif
6287#ifdef FEAT_CINDENT
6288 "cindent",
6289#endif
6290#ifdef FEAT_CLIENTSERVER
6291 "clientserver",
6292#endif
6293#ifdef FEAT_CLIPBOARD
6294 "clipboard",
6295#endif
6296#ifdef FEAT_CMDL_COMPL
6297 "cmdline_compl",
6298#endif
6299#ifdef FEAT_CMDHIST
6300 "cmdline_hist",
6301#endif
6302#ifdef FEAT_COMMENTS
6303 "comments",
6304#endif
6305#ifdef FEAT_CONCEAL
6306 "conceal",
6307#endif
6308#ifdef FEAT_CRYPT
6309 "cryptv",
6310 "crypt-blowfish",
6311 "crypt-blowfish2",
6312#endif
6313#ifdef FEAT_CSCOPE
6314 "cscope",
6315#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006316 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006317#ifdef CURSOR_SHAPE
6318 "cursorshape",
6319#endif
6320#ifdef DEBUG
6321 "debug",
6322#endif
6323#ifdef FEAT_CON_DIALOG
6324 "dialog_con",
6325#endif
6326#ifdef FEAT_GUI_DIALOG
6327 "dialog_gui",
6328#endif
6329#ifdef FEAT_DIFF
6330 "diff",
6331#endif
6332#ifdef FEAT_DIGRAPHS
6333 "digraphs",
6334#endif
6335#ifdef FEAT_DIRECTX
6336 "directx",
6337#endif
6338#ifdef FEAT_DND
6339 "dnd",
6340#endif
6341#ifdef FEAT_EMACS_TAGS
6342 "emacs_tags",
6343#endif
6344 "eval", /* always present, of course! */
6345 "ex_extra", /* graduated feature */
6346#ifdef FEAT_SEARCH_EXTRA
6347 "extra_search",
6348#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006349#ifdef FEAT_SEARCHPATH
6350 "file_in_path",
6351#endif
6352#ifdef FEAT_FILTERPIPE
6353 "filterpipe",
6354#endif
6355#ifdef FEAT_FIND_ID
6356 "find_in_path",
6357#endif
6358#ifdef FEAT_FLOAT
6359 "float",
6360#endif
6361#ifdef FEAT_FOLDING
6362 "folding",
6363#endif
6364#ifdef FEAT_FOOTER
6365 "footer",
6366#endif
6367#if !defined(USE_SYSTEM) && defined(UNIX)
6368 "fork",
6369#endif
6370#ifdef FEAT_GETTEXT
6371 "gettext",
6372#endif
6373#ifdef FEAT_GUI
6374 "gui",
6375#endif
6376#ifdef FEAT_GUI_ATHENA
6377# ifdef FEAT_GUI_NEXTAW
6378 "gui_neXtaw",
6379# else
6380 "gui_athena",
6381# endif
6382#endif
6383#ifdef FEAT_GUI_GTK
6384 "gui_gtk",
6385# ifdef USE_GTK3
6386 "gui_gtk3",
6387# else
6388 "gui_gtk2",
6389# endif
6390#endif
6391#ifdef FEAT_GUI_GNOME
6392 "gui_gnome",
6393#endif
6394#ifdef FEAT_GUI_MAC
6395 "gui_mac",
6396#endif
6397#ifdef FEAT_GUI_MOTIF
6398 "gui_motif",
6399#endif
6400#ifdef FEAT_GUI_PHOTON
6401 "gui_photon",
6402#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006403#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006404 "gui_win32",
6405#endif
6406#ifdef FEAT_HANGULIN
6407 "hangul_input",
6408#endif
6409#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6410 "iconv",
6411#endif
6412#ifdef FEAT_INS_EXPAND
6413 "insert_expand",
6414#endif
6415#ifdef FEAT_JOB_CHANNEL
6416 "job",
6417#endif
6418#ifdef FEAT_JUMPLIST
6419 "jumplist",
6420#endif
6421#ifdef FEAT_KEYMAP
6422 "keymap",
6423#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006424 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006425#ifdef FEAT_LANGMAP
6426 "langmap",
6427#endif
6428#ifdef FEAT_LIBCALL
6429 "libcall",
6430#endif
6431#ifdef FEAT_LINEBREAK
6432 "linebreak",
6433#endif
6434#ifdef FEAT_LISP
6435 "lispindent",
6436#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006437 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006438#ifdef FEAT_LOCALMAP
6439 "localmap",
6440#endif
6441#ifdef FEAT_LUA
6442# ifndef DYNAMIC_LUA
6443 "lua",
6444# endif
6445#endif
6446#ifdef FEAT_MENU
6447 "menu",
6448#endif
6449#ifdef FEAT_SESSION
6450 "mksession",
6451#endif
6452#ifdef FEAT_MODIFY_FNAME
6453 "modify_fname",
6454#endif
6455#ifdef FEAT_MOUSE
6456 "mouse",
6457#endif
6458#ifdef FEAT_MOUSESHAPE
6459 "mouseshape",
6460#endif
6461#if defined(UNIX) || defined(VMS)
6462# ifdef FEAT_MOUSE_DEC
6463 "mouse_dec",
6464# endif
6465# ifdef FEAT_MOUSE_GPM
6466 "mouse_gpm",
6467# endif
6468# ifdef FEAT_MOUSE_JSB
6469 "mouse_jsbterm",
6470# endif
6471# ifdef FEAT_MOUSE_NET
6472 "mouse_netterm",
6473# endif
6474# ifdef FEAT_MOUSE_PTERM
6475 "mouse_pterm",
6476# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006477# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006478 "mouse_sgr",
6479# endif
6480# ifdef FEAT_SYSMOUSE
6481 "mouse_sysmouse",
6482# endif
6483# ifdef FEAT_MOUSE_URXVT
6484 "mouse_urxvt",
6485# endif
6486# ifdef FEAT_MOUSE_XTERM
6487 "mouse_xterm",
6488# endif
6489#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006490 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006491#ifdef FEAT_MBYTE_IME
6492 "multi_byte_ime",
6493#endif
6494#ifdef FEAT_MULTI_LANG
6495 "multi_lang",
6496#endif
6497#ifdef FEAT_MZSCHEME
6498#ifndef DYNAMIC_MZSCHEME
6499 "mzscheme",
6500#endif
6501#endif
6502#ifdef FEAT_NUM64
6503 "num64",
6504#endif
6505#ifdef FEAT_OLE
6506 "ole",
6507#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006508#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006509 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006510#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006511#ifdef FEAT_PATH_EXTRA
6512 "path_extra",
6513#endif
6514#ifdef FEAT_PERL
6515#ifndef DYNAMIC_PERL
6516 "perl",
6517#endif
6518#endif
6519#ifdef FEAT_PERSISTENT_UNDO
6520 "persistent_undo",
6521#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006522#if defined(FEAT_PYTHON)
6523 "python_compiled",
6524# if defined(DYNAMIC_PYTHON)
6525 "python_dynamic",
6526# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006527 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006528 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006529# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006530#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006531#if defined(FEAT_PYTHON3)
6532 "python3_compiled",
6533# if defined(DYNAMIC_PYTHON3)
6534 "python3_dynamic",
6535# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006536 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006537 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006538# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006539#endif
6540#ifdef FEAT_POSTSCRIPT
6541 "postscript",
6542#endif
6543#ifdef FEAT_PRINTER
6544 "printer",
6545#endif
6546#ifdef FEAT_PROFILE
6547 "profile",
6548#endif
6549#ifdef FEAT_RELTIME
6550 "reltime",
6551#endif
6552#ifdef FEAT_QUICKFIX
6553 "quickfix",
6554#endif
6555#ifdef FEAT_RIGHTLEFT
6556 "rightleft",
6557#endif
6558#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6559 "ruby",
6560#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006561 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006562#ifdef FEAT_CMDL_INFO
6563 "showcmd",
6564 "cmdline_info",
6565#endif
6566#ifdef FEAT_SIGNS
6567 "signs",
6568#endif
6569#ifdef FEAT_SMARTINDENT
6570 "smartindent",
6571#endif
6572#ifdef STARTUPTIME
6573 "startuptime",
6574#endif
6575#ifdef FEAT_STL_OPT
6576 "statusline",
6577#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006578#ifdef FEAT_NETBEANS_INTG
6579 "netbeans_intg",
6580#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006581#ifdef FEAT_SOUND
6582 "sound",
6583#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006584#ifdef FEAT_SPELL
6585 "spell",
6586#endif
6587#ifdef FEAT_SYN_HL
6588 "syntax",
6589#endif
6590#if defined(USE_SYSTEM) || !defined(UNIX)
6591 "system",
6592#endif
6593#ifdef FEAT_TAG_BINS
6594 "tag_binary",
6595#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006596#ifdef FEAT_TCL
6597# ifndef DYNAMIC_TCL
6598 "tcl",
6599# endif
6600#endif
6601#ifdef FEAT_TERMGUICOLORS
6602 "termguicolors",
6603#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006604#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006605 "terminal",
6606#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006607#ifdef TERMINFO
6608 "terminfo",
6609#endif
6610#ifdef FEAT_TERMRESPONSE
6611 "termresponse",
6612#endif
6613#ifdef FEAT_TEXTOBJ
6614 "textobjects",
6615#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006616#ifdef FEAT_TEXT_PROP
6617 "textprop",
6618#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006619#ifdef HAVE_TGETENT
6620 "tgetent",
6621#endif
6622#ifdef FEAT_TIMERS
6623 "timers",
6624#endif
6625#ifdef FEAT_TITLE
6626 "title",
6627#endif
6628#ifdef FEAT_TOOLBAR
6629 "toolbar",
6630#endif
6631#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6632 "unnamedplus",
6633#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006634 "user-commands", /* was accidentally included in 5.4 */
6635 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006636#ifdef FEAT_VARTABS
6637 "vartabs",
6638#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006639 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006640#ifdef FEAT_VIMINFO
6641 "viminfo",
6642#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006643 "vimscript-1",
6644 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006645 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006646 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006647 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006648 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006649 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006650#ifdef FEAT_VTP
6651 "vtp",
6652#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006653#ifdef FEAT_WILDIGN
6654 "wildignore",
6655#endif
6656#ifdef FEAT_WILDMENU
6657 "wildmenu",
6658#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006659 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006660#ifdef FEAT_WAK
6661 "winaltkeys",
6662#endif
6663#ifdef FEAT_WRITEBACKUP
6664 "writebackup",
6665#endif
6666#ifdef FEAT_XIM
6667 "xim",
6668#endif
6669#ifdef FEAT_XFONTSET
6670 "xfontset",
6671#endif
6672#ifdef FEAT_XPM_W32
6673 "xpm",
6674 "xpm_w32", /* for backward compatibility */
6675#else
6676# if defined(HAVE_XPM)
6677 "xpm",
6678# endif
6679#endif
6680#ifdef USE_XSMP
6681 "xsmp",
6682#endif
6683#ifdef USE_XSMP_INTERACT
6684 "xsmp_interact",
6685#endif
6686#ifdef FEAT_XCLIPBOARD
6687 "xterm_clipboard",
6688#endif
6689#ifdef FEAT_XTERM_SAVE
6690 "xterm_save",
6691#endif
6692#if defined(UNIX) && defined(FEAT_X11)
6693 "X11",
6694#endif
6695 NULL
6696 };
6697
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006698 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006699 for (i = 0; has_list[i] != NULL; ++i)
6700 if (STRICMP(name, has_list[i]) == 0)
6701 {
6702 n = TRUE;
6703 break;
6704 }
6705
6706 if (n == FALSE)
6707 {
6708 if (STRNICMP(name, "patch", 5) == 0)
6709 {
6710 if (name[5] == '-'
6711 && STRLEN(name) >= 11
6712 && vim_isdigit(name[6])
6713 && vim_isdigit(name[8])
6714 && vim_isdigit(name[10]))
6715 {
6716 int major = atoi((char *)name + 6);
6717 int minor = atoi((char *)name + 8);
6718
6719 /* Expect "patch-9.9.01234". */
6720 n = (major < VIM_VERSION_MAJOR
6721 || (major == VIM_VERSION_MAJOR
6722 && (minor < VIM_VERSION_MINOR
6723 || (minor == VIM_VERSION_MINOR
6724 && has_patch(atoi((char *)name + 10))))));
6725 }
6726 else
6727 n = has_patch(atoi((char *)name + 5));
6728 }
6729 else if (STRICMP(name, "vim_starting") == 0)
6730 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006731 else if (STRICMP(name, "ttyin") == 0)
6732 n = mch_input_isatty();
6733 else if (STRICMP(name, "ttyout") == 0)
6734 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006735 else if (STRICMP(name, "multi_byte_encoding") == 0)
6736 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006737#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006738 else if (STRICMP(name, "balloon_multiline") == 0)
6739 n = multiline_balloon_available();
6740#endif
6741#ifdef DYNAMIC_TCL
6742 else if (STRICMP(name, "tcl") == 0)
6743 n = tcl_enabled(FALSE);
6744#endif
6745#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6746 else if (STRICMP(name, "iconv") == 0)
6747 n = iconv_enabled(FALSE);
6748#endif
6749#ifdef DYNAMIC_LUA
6750 else if (STRICMP(name, "lua") == 0)
6751 n = lua_enabled(FALSE);
6752#endif
6753#ifdef DYNAMIC_MZSCHEME
6754 else if (STRICMP(name, "mzscheme") == 0)
6755 n = mzscheme_enabled(FALSE);
6756#endif
6757#ifdef DYNAMIC_RUBY
6758 else if (STRICMP(name, "ruby") == 0)
6759 n = ruby_enabled(FALSE);
6760#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006761#ifdef DYNAMIC_PYTHON
6762 else if (STRICMP(name, "python") == 0)
6763 n = python_enabled(FALSE);
6764#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006765#ifdef DYNAMIC_PYTHON3
6766 else if (STRICMP(name, "python3") == 0)
6767 n = python3_enabled(FALSE);
6768#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006769#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6770 else if (STRICMP(name, "pythonx") == 0)
6771 {
6772# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6773 if (p_pyx == 0)
6774 n = python3_enabled(FALSE) || python_enabled(FALSE);
6775 else if (p_pyx == 3)
6776 n = python3_enabled(FALSE);
6777 else if (p_pyx == 2)
6778 n = python_enabled(FALSE);
6779# elif defined(DYNAMIC_PYTHON)
6780 n = python_enabled(FALSE);
6781# elif defined(DYNAMIC_PYTHON3)
6782 n = python3_enabled(FALSE);
6783# endif
6784 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006785#endif
6786#ifdef DYNAMIC_PERL
6787 else if (STRICMP(name, "perl") == 0)
6788 n = perl_enabled(FALSE);
6789#endif
6790#ifdef FEAT_GUI
6791 else if (STRICMP(name, "gui_running") == 0)
6792 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006793# ifdef FEAT_BROWSE
6794 else if (STRICMP(name, "browse") == 0)
6795 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6796# endif
6797#endif
6798#ifdef FEAT_SYN_HL
6799 else if (STRICMP(name, "syntax_items") == 0)
6800 n = syntax_present(curwin);
6801#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006802#ifdef FEAT_VTP
6803 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006804 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006805#endif
6806#ifdef FEAT_NETBEANS_INTG
6807 else if (STRICMP(name, "netbeans_enabled") == 0)
6808 n = netbeans_active();
6809#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006810#ifdef FEAT_MOUSE_GPM
6811 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6812 n = gpm_enabled();
6813#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006814#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006815 else if (STRICMP(name, "terminal") == 0)
6816 n = terminal_enabled();
6817#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006818#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006819 else if (STRICMP(name, "conpty") == 0)
6820 n = use_conpty();
6821#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006822 }
6823
6824 rettv->vval.v_number = n;
6825}
6826
6827/*
6828 * "has_key()" function
6829 */
6830 static void
6831f_has_key(typval_T *argvars, typval_T *rettv)
6832{
6833 if (argvars[0].v_type != VAR_DICT)
6834 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006835 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006836 return;
6837 }
6838 if (argvars[0].vval.v_dict == NULL)
6839 return;
6840
6841 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006842 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006843}
6844
6845/*
6846 * "haslocaldir()" function
6847 */
6848 static void
6849f_haslocaldir(typval_T *argvars, typval_T *rettv)
6850{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006851 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006852 win_T *wp = NULL;
6853
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006854 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6855
6856 // Check for window-local and tab-local directories
6857 if (wp != NULL && wp->w_localdir != NULL)
6858 rettv->vval.v_number = 1;
6859 else if (tp != NULL && tp->tp_localdir != NULL)
6860 rettv->vval.v_number = 2;
6861 else
6862 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006863}
6864
6865/*
6866 * "hasmapto()" function
6867 */
6868 static void
6869f_hasmapto(typval_T *argvars, typval_T *rettv)
6870{
6871 char_u *name;
6872 char_u *mode;
6873 char_u buf[NUMBUFLEN];
6874 int abbr = FALSE;
6875
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006876 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006877 if (argvars[1].v_type == VAR_UNKNOWN)
6878 mode = (char_u *)"nvo";
6879 else
6880 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006881 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006882 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006883 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006884 }
6885
6886 if (map_to_exists(name, mode, abbr))
6887 rettv->vval.v_number = TRUE;
6888 else
6889 rettv->vval.v_number = FALSE;
6890}
6891
6892/*
6893 * "histadd()" function
6894 */
6895 static void
6896f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6897{
6898#ifdef FEAT_CMDHIST
6899 int histype;
6900 char_u *str;
6901 char_u buf[NUMBUFLEN];
6902#endif
6903
6904 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006905 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006906 return;
6907#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006908 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006909 histype = str != NULL ? get_histtype(str) : -1;
6910 if (histype >= 0)
6911 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006912 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006913 if (*str != NUL)
6914 {
6915 init_history();
6916 add_to_history(histype, str, FALSE, NUL);
6917 rettv->vval.v_number = TRUE;
6918 return;
6919 }
6920 }
6921#endif
6922}
6923
6924/*
6925 * "histdel()" function
6926 */
6927 static void
6928f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6929{
6930#ifdef FEAT_CMDHIST
6931 int n;
6932 char_u buf[NUMBUFLEN];
6933 char_u *str;
6934
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006935 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006936 if (str == NULL)
6937 n = 0;
6938 else if (argvars[1].v_type == VAR_UNKNOWN)
6939 /* only one argument: clear entire history */
6940 n = clr_history(get_histtype(str));
6941 else if (argvars[1].v_type == VAR_NUMBER)
6942 /* index given: remove that entry */
6943 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006944 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006945 else
6946 /* string given: remove all matching entries */
6947 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006948 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006949 rettv->vval.v_number = n;
6950#endif
6951}
6952
6953/*
6954 * "histget()" function
6955 */
6956 static void
6957f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6958{
6959#ifdef FEAT_CMDHIST
6960 int type;
6961 int idx;
6962 char_u *str;
6963
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006964 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006965 if (str == NULL)
6966 rettv->vval.v_string = NULL;
6967 else
6968 {
6969 type = get_histtype(str);
6970 if (argvars[1].v_type == VAR_UNKNOWN)
6971 idx = get_history_idx(type);
6972 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006973 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006974 /* -1 on type error */
6975 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6976 }
6977#else
6978 rettv->vval.v_string = NULL;
6979#endif
6980 rettv->v_type = VAR_STRING;
6981}
6982
6983/*
6984 * "histnr()" function
6985 */
6986 static void
6987f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6988{
6989 int i;
6990
6991#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006992 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006993
6994 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6995 if (i >= HIST_CMD && i < HIST_COUNT)
6996 i = get_history_idx(i);
6997 else
6998#endif
6999 i = -1;
7000 rettv->vval.v_number = i;
7001}
7002
7003/*
7004 * "highlightID(name)" function
7005 */
7006 static void
7007f_hlID(typval_T *argvars, typval_T *rettv)
7008{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007009 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007010}
7011
7012/*
7013 * "highlight_exists()" function
7014 */
7015 static void
7016f_hlexists(typval_T *argvars, typval_T *rettv)
7017{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007018 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007019}
7020
7021/*
7022 * "hostname()" function
7023 */
7024 static void
7025f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7026{
7027 char_u hostname[256];
7028
7029 mch_get_host_name(hostname, 256);
7030 rettv->v_type = VAR_STRING;
7031 rettv->vval.v_string = vim_strsave(hostname);
7032}
7033
7034/*
7035 * iconv() function
7036 */
7037 static void
7038f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7039{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007040 char_u buf1[NUMBUFLEN];
7041 char_u buf2[NUMBUFLEN];
7042 char_u *from, *to, *str;
7043 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044
7045 rettv->v_type = VAR_STRING;
7046 rettv->vval.v_string = NULL;
7047
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007048 str = tv_get_string(&argvars[0]);
7049 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7050 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007051 vimconv.vc_type = CONV_NONE;
7052 convert_setup(&vimconv, from, to);
7053
7054 /* If the encodings are equal, no conversion needed. */
7055 if (vimconv.vc_type == CONV_NONE)
7056 rettv->vval.v_string = vim_strsave(str);
7057 else
7058 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7059
7060 convert_setup(&vimconv, NULL, NULL);
7061 vim_free(from);
7062 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007063}
7064
7065/*
7066 * "indent()" function
7067 */
7068 static void
7069f_indent(typval_T *argvars, typval_T *rettv)
7070{
7071 linenr_T lnum;
7072
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007073 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007074 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7075 rettv->vval.v_number = get_indent_lnum(lnum);
7076 else
7077 rettv->vval.v_number = -1;
7078}
7079
7080/*
7081 * "index()" function
7082 */
7083 static void
7084f_index(typval_T *argvars, typval_T *rettv)
7085{
7086 list_T *l;
7087 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007088 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007089 long idx = 0;
7090 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007091 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007092
7093 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007094 if (argvars[0].v_type == VAR_BLOB)
7095 {
7096 typval_T tv;
7097 int start = 0;
7098
7099 if (argvars[2].v_type != VAR_UNKNOWN)
7100 {
7101 start = tv_get_number_chk(&argvars[2], &error);
7102 if (error)
7103 return;
7104 }
7105 b = argvars[0].vval.v_blob;
7106 if (b == NULL)
7107 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007108 if (start < 0)
7109 {
7110 start = blob_len(b) + start;
7111 if (start < 0)
7112 start = 0;
7113 }
7114
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007115 for (idx = start; idx < blob_len(b); ++idx)
7116 {
7117 tv.v_type = VAR_NUMBER;
7118 tv.vval.v_number = blob_get(b, idx);
7119 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7120 {
7121 rettv->vval.v_number = idx;
7122 return;
7123 }
7124 }
7125 return;
7126 }
7127 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007128 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007129 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007130 return;
7131 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007132
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007133 l = argvars[0].vval.v_list;
7134 if (l != NULL)
7135 {
7136 item = l->lv_first;
7137 if (argvars[2].v_type != VAR_UNKNOWN)
7138 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007139 /* Start at specified item. Use the cached index that list_find()
7140 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007141 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007142 idx = l->lv_idx;
7143 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007144 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007145 if (error)
7146 item = NULL;
7147 }
7148
7149 for ( ; item != NULL; item = item->li_next, ++idx)
7150 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7151 {
7152 rettv->vval.v_number = idx;
7153 break;
7154 }
7155 }
7156}
7157
7158static int inputsecret_flag = 0;
7159
7160/*
7161 * "input()" function
7162 * Also handles inputsecret() when inputsecret is set.
7163 */
7164 static void
7165f_input(typval_T *argvars, typval_T *rettv)
7166{
7167 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7168}
7169
7170/*
7171 * "inputdialog()" function
7172 */
7173 static void
7174f_inputdialog(typval_T *argvars, typval_T *rettv)
7175{
7176#if defined(FEAT_GUI_TEXTDIALOG)
7177 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7178 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7179 {
7180 char_u *message;
7181 char_u buf[NUMBUFLEN];
7182 char_u *defstr = (char_u *)"";
7183
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007184 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007185 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007186 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007187 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7188 else
7189 IObuff[0] = NUL;
7190 if (message != NULL && defstr != NULL
7191 && do_dialog(VIM_QUESTION, NULL, message,
7192 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7193 rettv->vval.v_string = vim_strsave(IObuff);
7194 else
7195 {
7196 if (message != NULL && defstr != NULL
7197 && argvars[1].v_type != VAR_UNKNOWN
7198 && argvars[2].v_type != VAR_UNKNOWN)
7199 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007200 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007201 else
7202 rettv->vval.v_string = NULL;
7203 }
7204 rettv->v_type = VAR_STRING;
7205 }
7206 else
7207#endif
7208 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7209}
7210
7211/*
7212 * "inputlist()" function
7213 */
7214 static void
7215f_inputlist(typval_T *argvars, typval_T *rettv)
7216{
7217 listitem_T *li;
7218 int selected;
7219 int mouse_used;
7220
7221#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007222 /* While starting up, there is no place to enter text. When running tests
7223 * with --not-a-term we assume feedkeys() will be used. */
7224 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007225 return;
7226#endif
7227 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007229 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007230 return;
7231 }
7232
7233 msg_start();
7234 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7235 lines_left = Rows; /* avoid more prompt */
7236 msg_scroll = TRUE;
7237 msg_clr_eos();
7238
7239 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7240 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007241 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007242 msg_putchar('\n');
7243 }
7244
7245 /* Ask for choice. */
7246 selected = prompt_for_number(&mouse_used);
7247 if (mouse_used)
7248 selected -= lines_left;
7249
7250 rettv->vval.v_number = selected;
7251}
7252
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007253static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7254
7255/*
7256 * "inputrestore()" function
7257 */
7258 static void
7259f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7260{
7261 if (ga_userinput.ga_len > 0)
7262 {
7263 --ga_userinput.ga_len;
7264 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7265 + ga_userinput.ga_len);
7266 /* default return is zero == OK */
7267 }
7268 else if (p_verbose > 1)
7269 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007270 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007271 rettv->vval.v_number = 1; /* Failed */
7272 }
7273}
7274
7275/*
7276 * "inputsave()" function
7277 */
7278 static void
7279f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7280{
7281 /* Add an entry to the stack of typeahead storage. */
7282 if (ga_grow(&ga_userinput, 1) == OK)
7283 {
7284 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7285 + ga_userinput.ga_len);
7286 ++ga_userinput.ga_len;
7287 /* default return is zero == OK */
7288 }
7289 else
7290 rettv->vval.v_number = 1; /* Failed */
7291}
7292
7293/*
7294 * "inputsecret()" function
7295 */
7296 static void
7297f_inputsecret(typval_T *argvars, typval_T *rettv)
7298{
7299 ++cmdline_star;
7300 ++inputsecret_flag;
7301 f_input(argvars, rettv);
7302 --cmdline_star;
7303 --inputsecret_flag;
7304}
7305
7306/*
7307 * "insert()" function
7308 */
7309 static void
7310f_insert(typval_T *argvars, typval_T *rettv)
7311{
7312 long before = 0;
7313 listitem_T *item;
7314 list_T *l;
7315 int error = FALSE;
7316
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007317 if (argvars[0].v_type == VAR_BLOB)
7318 {
7319 int val, len;
7320 char_u *p;
7321
7322 len = blob_len(argvars[0].vval.v_blob);
7323 if (argvars[2].v_type != VAR_UNKNOWN)
7324 {
7325 before = (long)tv_get_number_chk(&argvars[2], &error);
7326 if (error)
7327 return; // type error; errmsg already given
7328 if (before < 0 || before > len)
7329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007330 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007331 return;
7332 }
7333 }
7334 val = tv_get_number_chk(&argvars[1], &error);
7335 if (error)
7336 return;
7337 if (val < 0 || val > 255)
7338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007339 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007340 return;
7341 }
7342
7343 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7344 return;
7345 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7346 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7347 *(p + before) = val;
7348 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7349
7350 copy_tv(&argvars[0], rettv);
7351 }
7352 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007353 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007354 else if ((l = argvars[0].vval.v_list) != NULL
7355 && !var_check_lock(l->lv_lock,
7356 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007357 {
7358 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007359 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007360 if (error)
7361 return; /* type error; errmsg already given */
7362
7363 if (before == l->lv_len)
7364 item = NULL;
7365 else
7366 {
7367 item = list_find(l, before);
7368 if (item == NULL)
7369 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007370 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007371 l = NULL;
7372 }
7373 }
7374 if (l != NULL)
7375 {
7376 list_insert_tv(l, &argvars[1], item);
7377 copy_tv(&argvars[0], rettv);
7378 }
7379 }
7380}
7381
7382/*
7383 * "invert(expr)" function
7384 */
7385 static void
7386f_invert(typval_T *argvars, typval_T *rettv)
7387{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007388 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007389}
7390
7391/*
7392 * "isdirectory()" function
7393 */
7394 static void
7395f_isdirectory(typval_T *argvars, typval_T *rettv)
7396{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007397 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007398}
7399
7400/*
7401 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7402 * or it refers to a List or Dictionary that is locked.
7403 */
7404 static int
7405tv_islocked(typval_T *tv)
7406{
7407 return (tv->v_lock & VAR_LOCKED)
7408 || (tv->v_type == VAR_LIST
7409 && tv->vval.v_list != NULL
7410 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7411 || (tv->v_type == VAR_DICT
7412 && tv->vval.v_dict != NULL
7413 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7414}
7415
7416/*
7417 * "islocked()" function
7418 */
7419 static void
7420f_islocked(typval_T *argvars, typval_T *rettv)
7421{
7422 lval_T lv;
7423 char_u *end;
7424 dictitem_T *di;
7425
7426 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007427 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007428 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007429 if (end != NULL && lv.ll_name != NULL)
7430 {
7431 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007432 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007433 else
7434 {
7435 if (lv.ll_tv == NULL)
7436 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007437 di = find_var(lv.ll_name, NULL, TRUE);
7438 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007439 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007440 /* Consider a variable locked when:
7441 * 1. the variable itself is locked
7442 * 2. the value of the variable is locked.
7443 * 3. the List or Dict value is locked.
7444 */
7445 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7446 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007447 }
7448 }
7449 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007450 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007451 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007452 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007453 else if (lv.ll_list != NULL)
7454 /* List item. */
7455 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7456 else
7457 /* Dictionary item. */
7458 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7459 }
7460 }
7461
7462 clear_lval(&lv);
7463}
7464
7465#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7466/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007467 * "isinf()" function
7468 */
7469 static void
7470f_isinf(typval_T *argvars, typval_T *rettv)
7471{
7472 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7473 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7474}
7475
7476/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007477 * "isnan()" function
7478 */
7479 static void
7480f_isnan(typval_T *argvars, typval_T *rettv)
7481{
7482 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7483 && isnan(argvars[0].vval.v_float);
7484}
7485#endif
7486
7487/*
7488 * "items(dict)" function
7489 */
7490 static void
7491f_items(typval_T *argvars, typval_T *rettv)
7492{
7493 dict_list(argvars, rettv, 2);
7494}
7495
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007496/*
7497 * "join()" function
7498 */
7499 static void
7500f_join(typval_T *argvars, typval_T *rettv)
7501{
7502 garray_T ga;
7503 char_u *sep;
7504
7505 if (argvars[0].v_type != VAR_LIST)
7506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007507 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007508 return;
7509 }
7510 if (argvars[0].vval.v_list == NULL)
7511 return;
7512 if (argvars[1].v_type == VAR_UNKNOWN)
7513 sep = (char_u *)" ";
7514 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007515 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007516
7517 rettv->v_type = VAR_STRING;
7518
7519 if (sep != NULL)
7520 {
7521 ga_init2(&ga, (int)sizeof(char), 80);
7522 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7523 ga_append(&ga, NUL);
7524 rettv->vval.v_string = (char_u *)ga.ga_data;
7525 }
7526 else
7527 rettv->vval.v_string = NULL;
7528}
7529
7530/*
7531 * "js_decode()" function
7532 */
7533 static void
7534f_js_decode(typval_T *argvars, typval_T *rettv)
7535{
7536 js_read_T reader;
7537
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007538 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007539 reader.js_fill = NULL;
7540 reader.js_used = 0;
7541 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007542 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007543}
7544
7545/*
7546 * "js_encode()" function
7547 */
7548 static void
7549f_js_encode(typval_T *argvars, typval_T *rettv)
7550{
7551 rettv->v_type = VAR_STRING;
7552 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7553}
7554
7555/*
7556 * "json_decode()" function
7557 */
7558 static void
7559f_json_decode(typval_T *argvars, typval_T *rettv)
7560{
7561 js_read_T reader;
7562
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007563 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007564 reader.js_fill = NULL;
7565 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007566 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007567}
7568
7569/*
7570 * "json_encode()" function
7571 */
7572 static void
7573f_json_encode(typval_T *argvars, typval_T *rettv)
7574{
7575 rettv->v_type = VAR_STRING;
7576 rettv->vval.v_string = json_encode(&argvars[0], 0);
7577}
7578
7579/*
7580 * "keys()" function
7581 */
7582 static void
7583f_keys(typval_T *argvars, typval_T *rettv)
7584{
7585 dict_list(argvars, rettv, 0);
7586}
7587
7588/*
7589 * "last_buffer_nr()" function.
7590 */
7591 static void
7592f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7593{
7594 int n = 0;
7595 buf_T *buf;
7596
Bram Moolenaar29323592016-07-24 22:04:11 +02007597 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598 if (n < buf->b_fnum)
7599 n = buf->b_fnum;
7600
7601 rettv->vval.v_number = n;
7602}
7603
7604/*
7605 * "len()" function
7606 */
7607 static void
7608f_len(typval_T *argvars, typval_T *rettv)
7609{
7610 switch (argvars[0].v_type)
7611 {
7612 case VAR_STRING:
7613 case VAR_NUMBER:
7614 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007615 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007616 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007617 case VAR_BLOB:
7618 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7619 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007620 case VAR_LIST:
7621 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7622 break;
7623 case VAR_DICT:
7624 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7625 break;
7626 case VAR_UNKNOWN:
7627 case VAR_SPECIAL:
7628 case VAR_FLOAT:
7629 case VAR_FUNC:
7630 case VAR_PARTIAL:
7631 case VAR_JOB:
7632 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007633 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007634 break;
7635 }
7636}
7637
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007638 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007639libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007640{
7641#ifdef FEAT_LIBCALL
7642 char_u *string_in;
7643 char_u **string_result;
7644 int nr_result;
7645#endif
7646
7647 rettv->v_type = type;
7648 if (type != VAR_NUMBER)
7649 rettv->vval.v_string = NULL;
7650
7651 if (check_restricted() || check_secure())
7652 return;
7653
7654#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007655 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007656 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7657 {
7658 string_in = NULL;
7659 if (argvars[2].v_type == VAR_STRING)
7660 string_in = argvars[2].vval.v_string;
7661 if (type == VAR_NUMBER)
7662 string_result = NULL;
7663 else
7664 string_result = &rettv->vval.v_string;
7665 if (mch_libcall(argvars[0].vval.v_string,
7666 argvars[1].vval.v_string,
7667 string_in,
7668 argvars[2].vval.v_number,
7669 string_result,
7670 &nr_result) == OK
7671 && type == VAR_NUMBER)
7672 rettv->vval.v_number = nr_result;
7673 }
7674#endif
7675}
7676
7677/*
7678 * "libcall()" function
7679 */
7680 static void
7681f_libcall(typval_T *argvars, typval_T *rettv)
7682{
7683 libcall_common(argvars, rettv, VAR_STRING);
7684}
7685
7686/*
7687 * "libcallnr()" function
7688 */
7689 static void
7690f_libcallnr(typval_T *argvars, typval_T *rettv)
7691{
7692 libcall_common(argvars, rettv, VAR_NUMBER);
7693}
7694
7695/*
7696 * "line(string)" function
7697 */
7698 static void
7699f_line(typval_T *argvars, typval_T *rettv)
7700{
7701 linenr_T lnum = 0;
7702 pos_T *fp;
7703 int fnum;
7704
7705 fp = var2fpos(&argvars[0], TRUE, &fnum);
7706 if (fp != NULL)
7707 lnum = fp->lnum;
7708 rettv->vval.v_number = lnum;
7709}
7710
7711/*
7712 * "line2byte(lnum)" function
7713 */
7714 static void
7715f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7716{
7717#ifndef FEAT_BYTEOFF
7718 rettv->vval.v_number = -1;
7719#else
7720 linenr_T lnum;
7721
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007722 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007723 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7724 rettv->vval.v_number = -1;
7725 else
7726 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7727 if (rettv->vval.v_number >= 0)
7728 ++rettv->vval.v_number;
7729#endif
7730}
7731
7732/*
7733 * "lispindent(lnum)" function
7734 */
7735 static void
7736f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7737{
7738#ifdef FEAT_LISP
7739 pos_T pos;
7740 linenr_T lnum;
7741
7742 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007743 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007744 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7745 {
7746 curwin->w_cursor.lnum = lnum;
7747 rettv->vval.v_number = get_lisp_indent();
7748 curwin->w_cursor = pos;
7749 }
7750 else
7751#endif
7752 rettv->vval.v_number = -1;
7753}
7754
7755/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007756 * "list2str()" function
7757 */
7758 static void
7759f_list2str(typval_T *argvars, typval_T *rettv)
7760{
7761 list_T *l;
7762 listitem_T *li;
7763 garray_T ga;
7764 int utf8 = FALSE;
7765
7766 rettv->v_type = VAR_STRING;
7767 rettv->vval.v_string = NULL;
7768 if (argvars[0].v_type != VAR_LIST)
7769 {
7770 emsg(_(e_invarg));
7771 return;
7772 }
7773
7774 l = argvars[0].vval.v_list;
7775 if (l == NULL)
7776 return; // empty list results in empty string
7777
7778 if (argvars[1].v_type != VAR_UNKNOWN)
7779 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7780
7781 ga_init2(&ga, 1, 80);
7782 if (has_mbyte || utf8)
7783 {
7784 char_u buf[MB_MAXBYTES + 1];
7785 int (*char2bytes)(int, char_u *);
7786
7787 if (utf8 || enc_utf8)
7788 char2bytes = utf_char2bytes;
7789 else
7790 char2bytes = mb_char2bytes;
7791
7792 for (li = l->lv_first; li != NULL; li = li->li_next)
7793 {
7794 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7795 ga_concat(&ga, buf);
7796 }
7797 ga_append(&ga, NUL);
7798 }
7799 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7800 {
7801 for (li = l->lv_first; li != NULL; li = li->li_next)
7802 ga_append(&ga, tv_get_number(&li->li_tv));
7803 ga_append(&ga, NUL);
7804 }
7805
7806 rettv->v_type = VAR_STRING;
7807 rettv->vval.v_string = ga.ga_data;
7808}
7809
7810/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007811 * "localtime()" function
7812 */
7813 static void
7814f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7815{
7816 rettv->vval.v_number = (varnumber_T)time(NULL);
7817}
7818
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007819 static void
7820get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7821{
7822 char_u *keys;
7823 char_u *which;
7824 char_u buf[NUMBUFLEN];
7825 char_u *keys_buf = NULL;
7826 char_u *rhs;
7827 int mode;
7828 int abbr = FALSE;
7829 int get_dict = FALSE;
7830 mapblock_T *mp;
7831 int buffer_local;
7832
7833 /* return empty string for failure */
7834 rettv->v_type = VAR_STRING;
7835 rettv->vval.v_string = NULL;
7836
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007837 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007838 if (*keys == NUL)
7839 return;
7840
7841 if (argvars[1].v_type != VAR_UNKNOWN)
7842 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007843 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007844 if (argvars[2].v_type != VAR_UNKNOWN)
7845 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007846 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007847 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007848 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007849 }
7850 }
7851 else
7852 which = (char_u *)"";
7853 if (which == NULL)
7854 return;
7855
7856 mode = get_map_mode(&which, 0);
7857
7858 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7859 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7860 vim_free(keys_buf);
7861
7862 if (!get_dict)
7863 {
7864 /* Return a string. */
7865 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007866 {
7867 if (*rhs == NUL)
7868 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7869 else
7870 rettv->vval.v_string = str2special_save(rhs, FALSE);
7871 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007872
7873 }
7874 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7875 {
7876 /* Return a dictionary. */
7877 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7878 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7879 dict_T *dict = rettv->vval.v_dict;
7880
Bram Moolenaare0be1672018-07-08 16:50:37 +02007881 dict_add_string(dict, "lhs", lhs);
7882 dict_add_string(dict, "rhs", mp->m_orig_str);
7883 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7884 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7885 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007886 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7887 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007888 dict_add_number(dict, "buffer", (long)buffer_local);
7889 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7890 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007891
7892 vim_free(lhs);
7893 vim_free(mapmode);
7894 }
7895}
7896
7897#ifdef FEAT_FLOAT
7898/*
7899 * "log()" function
7900 */
7901 static void
7902f_log(typval_T *argvars, typval_T *rettv)
7903{
7904 float_T f = 0.0;
7905
7906 rettv->v_type = VAR_FLOAT;
7907 if (get_float_arg(argvars, &f) == OK)
7908 rettv->vval.v_float = log(f);
7909 else
7910 rettv->vval.v_float = 0.0;
7911}
7912
7913/*
7914 * "log10()" function
7915 */
7916 static void
7917f_log10(typval_T *argvars, typval_T *rettv)
7918{
7919 float_T f = 0.0;
7920
7921 rettv->v_type = VAR_FLOAT;
7922 if (get_float_arg(argvars, &f) == OK)
7923 rettv->vval.v_float = log10(f);
7924 else
7925 rettv->vval.v_float = 0.0;
7926}
7927#endif
7928
7929#ifdef FEAT_LUA
7930/*
7931 * "luaeval()" function
7932 */
7933 static void
7934f_luaeval(typval_T *argvars, typval_T *rettv)
7935{
7936 char_u *str;
7937 char_u buf[NUMBUFLEN];
7938
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007939 if (check_restricted() || check_secure())
7940 return;
7941
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007942 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007943 do_luaeval(str, argvars + 1, rettv);
7944}
7945#endif
7946
7947/*
7948 * "map()" function
7949 */
7950 static void
7951f_map(typval_T *argvars, typval_T *rettv)
7952{
7953 filter_map(argvars, rettv, TRUE);
7954}
7955
7956/*
7957 * "maparg()" function
7958 */
7959 static void
7960f_maparg(typval_T *argvars, typval_T *rettv)
7961{
7962 get_maparg(argvars, rettv, TRUE);
7963}
7964
7965/*
7966 * "mapcheck()" function
7967 */
7968 static void
7969f_mapcheck(typval_T *argvars, typval_T *rettv)
7970{
7971 get_maparg(argvars, rettv, FALSE);
7972}
7973
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007974typedef enum
7975{
7976 MATCH_END, /* matchend() */
7977 MATCH_MATCH, /* match() */
7978 MATCH_STR, /* matchstr() */
7979 MATCH_LIST, /* matchlist() */
7980 MATCH_POS /* matchstrpos() */
7981} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007982
7983 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007984find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007985{
7986 char_u *str = NULL;
7987 long len = 0;
7988 char_u *expr = NULL;
7989 char_u *pat;
7990 regmatch_T regmatch;
7991 char_u patbuf[NUMBUFLEN];
7992 char_u strbuf[NUMBUFLEN];
7993 char_u *save_cpo;
7994 long start = 0;
7995 long nth = 1;
7996 colnr_T startcol = 0;
7997 int match = 0;
7998 list_T *l = NULL;
7999 listitem_T *li = NULL;
8000 long idx = 0;
8001 char_u *tofree = NULL;
8002
8003 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8004 save_cpo = p_cpo;
8005 p_cpo = (char_u *)"";
8006
8007 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008008 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008009 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008010 /* type MATCH_LIST: return empty list when there are no matches.
8011 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008012 if (rettv_list_alloc(rettv) == FAIL)
8013 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008014 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008015 && (list_append_string(rettv->vval.v_list,
8016 (char_u *)"", 0) == FAIL
8017 || list_append_number(rettv->vval.v_list,
8018 (varnumber_T)-1) == FAIL
8019 || list_append_number(rettv->vval.v_list,
8020 (varnumber_T)-1) == FAIL
8021 || list_append_number(rettv->vval.v_list,
8022 (varnumber_T)-1) == FAIL))
8023 {
8024 list_free(rettv->vval.v_list);
8025 rettv->vval.v_list = NULL;
8026 goto theend;
8027 }
8028 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008029 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008030 {
8031 rettv->v_type = VAR_STRING;
8032 rettv->vval.v_string = NULL;
8033 }
8034
8035 if (argvars[0].v_type == VAR_LIST)
8036 {
8037 if ((l = argvars[0].vval.v_list) == NULL)
8038 goto theend;
8039 li = l->lv_first;
8040 }
8041 else
8042 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008043 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008044 len = (long)STRLEN(str);
8045 }
8046
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008047 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008048 if (pat == NULL)
8049 goto theend;
8050
8051 if (argvars[2].v_type != VAR_UNKNOWN)
8052 {
8053 int error = FALSE;
8054
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008055 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008056 if (error)
8057 goto theend;
8058 if (l != NULL)
8059 {
8060 li = list_find(l, start);
8061 if (li == NULL)
8062 goto theend;
8063 idx = l->lv_idx; /* use the cached index */
8064 }
8065 else
8066 {
8067 if (start < 0)
8068 start = 0;
8069 if (start > len)
8070 goto theend;
8071 /* When "count" argument is there ignore matches before "start",
8072 * otherwise skip part of the string. Differs when pattern is "^"
8073 * or "\<". */
8074 if (argvars[3].v_type != VAR_UNKNOWN)
8075 startcol = start;
8076 else
8077 {
8078 str += start;
8079 len -= start;
8080 }
8081 }
8082
8083 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008084 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008085 if (error)
8086 goto theend;
8087 }
8088
8089 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8090 if (regmatch.regprog != NULL)
8091 {
8092 regmatch.rm_ic = p_ic;
8093
8094 for (;;)
8095 {
8096 if (l != NULL)
8097 {
8098 if (li == NULL)
8099 {
8100 match = FALSE;
8101 break;
8102 }
8103 vim_free(tofree);
8104 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8105 if (str == NULL)
8106 break;
8107 }
8108
8109 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8110
8111 if (match && --nth <= 0)
8112 break;
8113 if (l == NULL && !match)
8114 break;
8115
8116 /* Advance to just after the match. */
8117 if (l != NULL)
8118 {
8119 li = li->li_next;
8120 ++idx;
8121 }
8122 else
8123 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008124 startcol = (colnr_T)(regmatch.startp[0]
8125 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008126 if (startcol > (colnr_T)len
8127 || str + startcol <= regmatch.startp[0])
8128 {
8129 match = FALSE;
8130 break;
8131 }
8132 }
8133 }
8134
8135 if (match)
8136 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008137 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008138 {
8139 listitem_T *li1 = rettv->vval.v_list->lv_first;
8140 listitem_T *li2 = li1->li_next;
8141 listitem_T *li3 = li2->li_next;
8142 listitem_T *li4 = li3->li_next;
8143
8144 vim_free(li1->li_tv.vval.v_string);
8145 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8146 (int)(regmatch.endp[0] - regmatch.startp[0]));
8147 li3->li_tv.vval.v_number =
8148 (varnumber_T)(regmatch.startp[0] - expr);
8149 li4->li_tv.vval.v_number =
8150 (varnumber_T)(regmatch.endp[0] - expr);
8151 if (l != NULL)
8152 li2->li_tv.vval.v_number = (varnumber_T)idx;
8153 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008154 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008155 {
8156 int i;
8157
8158 /* return list with matched string and submatches */
8159 for (i = 0; i < NSUBEXP; ++i)
8160 {
8161 if (regmatch.endp[i] == NULL)
8162 {
8163 if (list_append_string(rettv->vval.v_list,
8164 (char_u *)"", 0) == FAIL)
8165 break;
8166 }
8167 else if (list_append_string(rettv->vval.v_list,
8168 regmatch.startp[i],
8169 (int)(regmatch.endp[i] - regmatch.startp[i]))
8170 == FAIL)
8171 break;
8172 }
8173 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008174 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008175 {
8176 /* return matched string */
8177 if (l != NULL)
8178 copy_tv(&li->li_tv, rettv);
8179 else
8180 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8181 (int)(regmatch.endp[0] - regmatch.startp[0]));
8182 }
8183 else if (l != NULL)
8184 rettv->vval.v_number = idx;
8185 else
8186 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008187 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008188 rettv->vval.v_number =
8189 (varnumber_T)(regmatch.startp[0] - str);
8190 else
8191 rettv->vval.v_number =
8192 (varnumber_T)(regmatch.endp[0] - str);
8193 rettv->vval.v_number += (varnumber_T)(str - expr);
8194 }
8195 }
8196 vim_regfree(regmatch.regprog);
8197 }
8198
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008199theend:
8200 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008201 /* matchstrpos() without a list: drop the second item. */
8202 listitem_remove(rettv->vval.v_list,
8203 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008204 vim_free(tofree);
8205 p_cpo = save_cpo;
8206}
8207
8208/*
8209 * "match()" function
8210 */
8211 static void
8212f_match(typval_T *argvars, typval_T *rettv)
8213{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008214 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008215}
8216
Bram Moolenaar95e51472018-07-28 16:55:56 +02008217#ifdef FEAT_SEARCH_EXTRA
8218 static int
8219matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8220{
8221 dictitem_T *di;
8222
8223 if (tv->v_type != VAR_DICT)
8224 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008225 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008226 return FAIL;
8227 }
8228
8229 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008230 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008231 (char_u *)"conceal", FALSE);
8232
8233 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8234 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008235 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008236 if (*win == NULL)
8237 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008238 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008239 return FAIL;
8240 }
8241 }
8242
8243 return OK;
8244}
8245#endif
8246
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008247/*
8248 * "matchadd()" function
8249 */
8250 static void
8251f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8252{
8253#ifdef FEAT_SEARCH_EXTRA
8254 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008255 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8256 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008257 int prio = 10; /* default priority */
8258 int id = -1;
8259 int error = FALSE;
8260 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008261 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008262
8263 rettv->vval.v_number = -1;
8264
8265 if (grp == NULL || pat == NULL)
8266 return;
8267 if (argvars[2].v_type != VAR_UNKNOWN)
8268 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008269 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008270 if (argvars[3].v_type != VAR_UNKNOWN)
8271 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008272 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008273 if (argvars[4].v_type != VAR_UNKNOWN
8274 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8275 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008276 }
8277 }
8278 if (error == TRUE)
8279 return;
8280 if (id >= 1 && id <= 3)
8281 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008282 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008283 return;
8284 }
8285
Bram Moolenaar95e51472018-07-28 16:55:56 +02008286 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287 conceal_char);
8288#endif
8289}
8290
8291/*
8292 * "matchaddpos()" function
8293 */
8294 static void
8295f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8296{
8297#ifdef FEAT_SEARCH_EXTRA
8298 char_u buf[NUMBUFLEN];
8299 char_u *group;
8300 int prio = 10;
8301 int id = -1;
8302 int error = FALSE;
8303 list_T *l;
8304 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008305 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008306
8307 rettv->vval.v_number = -1;
8308
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008309 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008310 if (group == NULL)
8311 return;
8312
8313 if (argvars[1].v_type != VAR_LIST)
8314 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008315 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008316 return;
8317 }
8318 l = argvars[1].vval.v_list;
8319 if (l == NULL)
8320 return;
8321
8322 if (argvars[2].v_type != VAR_UNKNOWN)
8323 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008324 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008325 if (argvars[3].v_type != VAR_UNKNOWN)
8326 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008327 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008328
8329 if (argvars[4].v_type != VAR_UNKNOWN
8330 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8331 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008332 }
8333 }
8334 if (error == TRUE)
8335 return;
8336
8337 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8338 if (id == 1 || id == 2)
8339 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008340 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008341 return;
8342 }
8343
Bram Moolenaar95e51472018-07-28 16:55:56 +02008344 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008345 conceal_char);
8346#endif
8347}
8348
8349/*
8350 * "matcharg()" function
8351 */
8352 static void
8353f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8354{
8355 if (rettv_list_alloc(rettv) == OK)
8356 {
8357#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008358 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008359 matchitem_T *m;
8360
8361 if (id >= 1 && id <= 3)
8362 {
8363 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8364 {
8365 list_append_string(rettv->vval.v_list,
8366 syn_id2name(m->hlg_id), -1);
8367 list_append_string(rettv->vval.v_list, m->pattern, -1);
8368 }
8369 else
8370 {
8371 list_append_string(rettv->vval.v_list, NULL, -1);
8372 list_append_string(rettv->vval.v_list, NULL, -1);
8373 }
8374 }
8375#endif
8376 }
8377}
8378
8379/*
8380 * "matchdelete()" function
8381 */
8382 static void
8383f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8384{
8385#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008386 win_T *win = get_optional_window(argvars, 1);
8387
8388 if (win == NULL)
8389 rettv->vval.v_number = -1;
8390 else
8391 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008392 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008393#endif
8394}
8395
8396/*
8397 * "matchend()" function
8398 */
8399 static void
8400f_matchend(typval_T *argvars, typval_T *rettv)
8401{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008402 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403}
8404
8405/*
8406 * "matchlist()" function
8407 */
8408 static void
8409f_matchlist(typval_T *argvars, typval_T *rettv)
8410{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008411 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008412}
8413
8414/*
8415 * "matchstr()" function
8416 */
8417 static void
8418f_matchstr(typval_T *argvars, typval_T *rettv)
8419{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008420 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008421}
8422
8423/*
8424 * "matchstrpos()" function
8425 */
8426 static void
8427f_matchstrpos(typval_T *argvars, typval_T *rettv)
8428{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008429 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008430}
8431
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008432 static void
8433max_min(typval_T *argvars, typval_T *rettv, int domax)
8434{
8435 varnumber_T n = 0;
8436 varnumber_T i;
8437 int error = FALSE;
8438
8439 if (argvars[0].v_type == VAR_LIST)
8440 {
8441 list_T *l;
8442 listitem_T *li;
8443
8444 l = argvars[0].vval.v_list;
8445 if (l != NULL)
8446 {
8447 li = l->lv_first;
8448 if (li != NULL)
8449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008450 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008451 for (;;)
8452 {
8453 li = li->li_next;
8454 if (li == NULL)
8455 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008456 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008457 if (domax ? i > n : i < n)
8458 n = i;
8459 }
8460 }
8461 }
8462 }
8463 else if (argvars[0].v_type == VAR_DICT)
8464 {
8465 dict_T *d;
8466 int first = TRUE;
8467 hashitem_T *hi;
8468 int todo;
8469
8470 d = argvars[0].vval.v_dict;
8471 if (d != NULL)
8472 {
8473 todo = (int)d->dv_hashtab.ht_used;
8474 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8475 {
8476 if (!HASHITEM_EMPTY(hi))
8477 {
8478 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008479 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008480 if (first)
8481 {
8482 n = i;
8483 first = FALSE;
8484 }
8485 else if (domax ? i > n : i < n)
8486 n = i;
8487 }
8488 }
8489 }
8490 }
8491 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008492 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493 rettv->vval.v_number = error ? 0 : n;
8494}
8495
8496/*
8497 * "max()" function
8498 */
8499 static void
8500f_max(typval_T *argvars, typval_T *rettv)
8501{
8502 max_min(argvars, rettv, TRUE);
8503}
8504
8505/*
8506 * "min()" function
8507 */
8508 static void
8509f_min(typval_T *argvars, typval_T *rettv)
8510{
8511 max_min(argvars, rettv, FALSE);
8512}
8513
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008514/*
8515 * Create the directory in which "dir" is located, and higher levels when
8516 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008517 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008518 */
8519 static int
8520mkdir_recurse(char_u *dir, int prot)
8521{
8522 char_u *p;
8523 char_u *updir;
8524 int r = FAIL;
8525
8526 /* Get end of directory name in "dir".
8527 * We're done when it's "/" or "c:/". */
8528 p = gettail_sep(dir);
8529 if (p <= get_past_head(dir))
8530 return OK;
8531
8532 /* If the directory exists we're done. Otherwise: create it.*/
8533 updir = vim_strnsave(dir, (int)(p - dir));
8534 if (updir == NULL)
8535 return FAIL;
8536 if (mch_isdir(updir))
8537 r = OK;
8538 else if (mkdir_recurse(updir, prot) == OK)
8539 r = vim_mkdir_emsg(updir, prot);
8540 vim_free(updir);
8541 return r;
8542}
8543
8544#ifdef vim_mkdir
8545/*
8546 * "mkdir()" function
8547 */
8548 static void
8549f_mkdir(typval_T *argvars, typval_T *rettv)
8550{
8551 char_u *dir;
8552 char_u buf[NUMBUFLEN];
8553 int prot = 0755;
8554
8555 rettv->vval.v_number = FAIL;
8556 if (check_restricted() || check_secure())
8557 return;
8558
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008559 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008561 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008563 if (*gettail(dir) == NUL)
8564 /* remove trailing slashes */
8565 *gettail_sep(dir) = NUL;
8566
8567 if (argvars[1].v_type != VAR_UNKNOWN)
8568 {
8569 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008570 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008571 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008572 if (prot == -1)
8573 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008575 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008576 {
8577 if (mch_isdir(dir))
8578 {
8579 /* With the "p" flag it's OK if the dir already exists. */
8580 rettv->vval.v_number = OK;
8581 return;
8582 }
8583 mkdir_recurse(dir, prot);
8584 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008585 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008586 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587}
8588#endif
8589
8590/*
8591 * "mode()" function
8592 */
8593 static void
8594f_mode(typval_T *argvars, typval_T *rettv)
8595{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008596 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008597
Bram Moolenaar612cc382018-07-29 15:34:26 +02008598 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008599
8600 if (time_for_testing == 93784)
8601 {
8602 /* Testing the two-character code. */
8603 buf[0] = 'x';
8604 buf[1] = '!';
8605 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008606#ifdef FEAT_TERMINAL
8607 else if (term_use_loop())
8608 buf[0] = 't';
8609#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610 else if (VIsual_active)
8611 {
8612 if (VIsual_select)
8613 buf[0] = VIsual_mode + 's' - 'v';
8614 else
8615 buf[0] = VIsual_mode;
8616 }
8617 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8618 || State == CONFIRM)
8619 {
8620 buf[0] = 'r';
8621 if (State == ASKMORE)
8622 buf[1] = 'm';
8623 else if (State == CONFIRM)
8624 buf[1] = '?';
8625 }
8626 else if (State == EXTERNCMD)
8627 buf[0] = '!';
8628 else if (State & INSERT)
8629 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630 if (State & VREPLACE_FLAG)
8631 {
8632 buf[0] = 'R';
8633 buf[1] = 'v';
8634 }
8635 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008636 {
8637 if (State & REPLACE_FLAG)
8638 buf[0] = 'R';
8639 else
8640 buf[0] = 'i';
8641#ifdef FEAT_INS_EXPAND
8642 if (ins_compl_active())
8643 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008644 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008645 buf[1] = 'x';
8646#endif
8647 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008648 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008649 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008650 {
8651 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008652 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008653 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008654 else if (exmode_active == EXMODE_NORMAL)
8655 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656 }
8657 else
8658 {
8659 buf[0] = 'n';
8660 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008661 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008663 // to be able to detect force-linewise/blockwise/characterwise operations
8664 buf[2] = motion_force;
8665 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008666 else if (restart_edit == 'I' || restart_edit == 'R'
8667 || restart_edit == 'V')
8668 {
8669 buf[1] = 'i';
8670 buf[2] = restart_edit;
8671 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672 }
8673
8674 /* Clear out the minor mode when the argument is not a non-zero number or
8675 * non-empty string. */
8676 if (!non_zero_arg(&argvars[0]))
8677 buf[1] = NUL;
8678
8679 rettv->vval.v_string = vim_strsave(buf);
8680 rettv->v_type = VAR_STRING;
8681}
8682
8683#if defined(FEAT_MZSCHEME) || defined(PROTO)
8684/*
8685 * "mzeval()" function
8686 */
8687 static void
8688f_mzeval(typval_T *argvars, typval_T *rettv)
8689{
8690 char_u *str;
8691 char_u buf[NUMBUFLEN];
8692
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008693 if (check_restricted() || check_secure())
8694 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008695 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008696 do_mzeval(str, rettv);
8697}
8698
8699 void
8700mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8701{
8702 typval_T argvars[3];
8703
8704 argvars[0].v_type = VAR_STRING;
8705 argvars[0].vval.v_string = name;
8706 copy_tv(args, &argvars[1]);
8707 argvars[2].v_type = VAR_UNKNOWN;
8708 f_call(argvars, rettv);
8709 clear_tv(&argvars[1]);
8710}
8711#endif
8712
8713/*
8714 * "nextnonblank()" function
8715 */
8716 static void
8717f_nextnonblank(typval_T *argvars, typval_T *rettv)
8718{
8719 linenr_T lnum;
8720
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008721 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008722 {
8723 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8724 {
8725 lnum = 0;
8726 break;
8727 }
8728 if (*skipwhite(ml_get(lnum)) != NUL)
8729 break;
8730 }
8731 rettv->vval.v_number = lnum;
8732}
8733
8734/*
8735 * "nr2char()" function
8736 */
8737 static void
8738f_nr2char(typval_T *argvars, typval_T *rettv)
8739{
8740 char_u buf[NUMBUFLEN];
8741
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742 if (has_mbyte)
8743 {
8744 int utf8 = 0;
8745
8746 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008747 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008748 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008749 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008750 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008751 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008752 }
8753 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008755 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008756 buf[1] = NUL;
8757 }
8758 rettv->v_type = VAR_STRING;
8759 rettv->vval.v_string = vim_strsave(buf);
8760}
8761
8762/*
8763 * "or(expr, expr)" function
8764 */
8765 static void
8766f_or(typval_T *argvars, typval_T *rettv)
8767{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008768 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8769 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770}
8771
8772/*
8773 * "pathshorten()" function
8774 */
8775 static void
8776f_pathshorten(typval_T *argvars, typval_T *rettv)
8777{
8778 char_u *p;
8779
8780 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008781 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008782 if (p == NULL)
8783 rettv->vval.v_string = NULL;
8784 else
8785 {
8786 p = vim_strsave(p);
8787 rettv->vval.v_string = p;
8788 if (p != NULL)
8789 shorten_dir(p);
8790 }
8791}
8792
8793#ifdef FEAT_PERL
8794/*
8795 * "perleval()" function
8796 */
8797 static void
8798f_perleval(typval_T *argvars, typval_T *rettv)
8799{
8800 char_u *str;
8801 char_u buf[NUMBUFLEN];
8802
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008803 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008804 do_perleval(str, rettv);
8805}
8806#endif
8807
8808#ifdef FEAT_FLOAT
8809/*
8810 * "pow()" function
8811 */
8812 static void
8813f_pow(typval_T *argvars, typval_T *rettv)
8814{
8815 float_T fx = 0.0, fy = 0.0;
8816
8817 rettv->v_type = VAR_FLOAT;
8818 if (get_float_arg(argvars, &fx) == OK
8819 && get_float_arg(&argvars[1], &fy) == OK)
8820 rettv->vval.v_float = pow(fx, fy);
8821 else
8822 rettv->vval.v_float = 0.0;
8823}
8824#endif
8825
8826/*
8827 * "prevnonblank()" function
8828 */
8829 static void
8830f_prevnonblank(typval_T *argvars, typval_T *rettv)
8831{
8832 linenr_T lnum;
8833
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008834 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8836 lnum = 0;
8837 else
8838 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8839 --lnum;
8840 rettv->vval.v_number = lnum;
8841}
8842
8843/* This dummy va_list is here because:
8844 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8845 * - locally in the function results in a "used before set" warning
8846 * - using va_start() to initialize it gives "function with fixed args" error */
8847static va_list ap;
8848
8849/*
8850 * "printf()" function
8851 */
8852 static void
8853f_printf(typval_T *argvars, typval_T *rettv)
8854{
8855 char_u buf[NUMBUFLEN];
8856 int len;
8857 char_u *s;
8858 int saved_did_emsg = did_emsg;
8859 char *fmt;
8860
8861 rettv->v_type = VAR_STRING;
8862 rettv->vval.v_string = NULL;
8863
8864 /* Get the required length, allocate the buffer and do it for real. */
8865 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008866 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008867 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008868 if (!did_emsg)
8869 {
8870 s = alloc(len + 1);
8871 if (s != NULL)
8872 {
8873 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008874 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8875 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876 }
8877 }
8878 did_emsg |= saved_did_emsg;
8879}
8880
8881/*
8882 * "pumvisible()" function
8883 */
8884 static void
8885f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8886{
8887#ifdef FEAT_INS_EXPAND
8888 if (pum_visible())
8889 rettv->vval.v_number = 1;
8890#endif
8891}
8892
8893#ifdef FEAT_PYTHON3
8894/*
8895 * "py3eval()" function
8896 */
8897 static void
8898f_py3eval(typval_T *argvars, typval_T *rettv)
8899{
8900 char_u *str;
8901 char_u buf[NUMBUFLEN];
8902
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008903 if (check_restricted() || check_secure())
8904 return;
8905
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008906 if (p_pyx == 0)
8907 p_pyx = 3;
8908
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008909 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910 do_py3eval(str, rettv);
8911}
8912#endif
8913
8914#ifdef FEAT_PYTHON
8915/*
8916 * "pyeval()" function
8917 */
8918 static void
8919f_pyeval(typval_T *argvars, typval_T *rettv)
8920{
8921 char_u *str;
8922 char_u buf[NUMBUFLEN];
8923
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008924 if (check_restricted() || check_secure())
8925 return;
8926
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008927 if (p_pyx == 0)
8928 p_pyx = 2;
8929
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008930 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008931 do_pyeval(str, rettv);
8932}
8933#endif
8934
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008935#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8936/*
8937 * "pyxeval()" function
8938 */
8939 static void
8940f_pyxeval(typval_T *argvars, typval_T *rettv)
8941{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008942 if (check_restricted() || check_secure())
8943 return;
8944
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008945# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8946 init_pyxversion();
8947 if (p_pyx == 2)
8948 f_pyeval(argvars, rettv);
8949 else
8950 f_py3eval(argvars, rettv);
8951# elif defined(FEAT_PYTHON)
8952 f_pyeval(argvars, rettv);
8953# elif defined(FEAT_PYTHON3)
8954 f_py3eval(argvars, rettv);
8955# endif
8956}
8957#endif
8958
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008959/*
8960 * "range()" function
8961 */
8962 static void
8963f_range(typval_T *argvars, typval_T *rettv)
8964{
8965 varnumber_T start;
8966 varnumber_T end;
8967 varnumber_T stride = 1;
8968 varnumber_T i;
8969 int error = FALSE;
8970
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008971 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008972 if (argvars[1].v_type == VAR_UNKNOWN)
8973 {
8974 end = start - 1;
8975 start = 0;
8976 }
8977 else
8978 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008979 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008980 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008981 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008982 }
8983
8984 if (error)
8985 return; /* type error; errmsg already given */
8986 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008987 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008988 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008989 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008990 else
8991 {
8992 if (rettv_list_alloc(rettv) == OK)
8993 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8994 if (list_append_number(rettv->vval.v_list,
8995 (varnumber_T)i) == FAIL)
8996 break;
8997 }
8998}
8999
9000/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009001 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009002 */
9003 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009004readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009005{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009006 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009007 typval_T save_val;
9008 typval_T rettv;
9009 typval_T argv[2];
9010 int retval = 0;
9011 int error = FALSE;
9012
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009013 if (expr->v_type == VAR_UNKNOWN)
9014 return 1;
9015
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009016 prepare_vimvar(VV_VAL, &save_val);
9017 set_vim_var_string(VV_VAL, name, -1);
9018 argv[0].v_type = VAR_STRING;
9019 argv[0].vval.v_string = name;
9020
9021 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9022 goto theend;
9023
9024 retval = tv_get_number_chk(&rettv, &error);
9025 if (error)
9026 retval = -1;
9027 clear_tv(&rettv);
9028
9029theend:
9030 set_vim_var_string(VV_VAL, NULL, 0);
9031 restore_vimvar(VV_VAL, &save_val);
9032 return retval;
9033}
9034
9035/*
9036 * "readdir()" function
9037 */
9038 static void
9039f_readdir(typval_T *argvars, typval_T *rettv)
9040{
9041 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009042 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009043 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009044 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009045 garray_T ga;
9046 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009047
9048 if (rettv_list_alloc(rettv) == FAIL)
9049 return;
9050 path = tv_get_string(&argvars[0]);
9051 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009052
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009053 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9054 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009055 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009056 for (i = 0; i < ga.ga_len; i++)
9057 {
9058 p = ((char_u **)ga.ga_data)[i];
9059 list_append_string(rettv->vval.v_list, p, -1);
9060 }
9061 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009062 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009063}
9064
9065/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009066 * "readfile()" function
9067 */
9068 static void
9069f_readfile(typval_T *argvars, typval_T *rettv)
9070{
9071 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009072 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009073 int failed = FALSE;
9074 char_u *fname;
9075 FILE *fd;
9076 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9077 int io_size = sizeof(buf);
9078 int readlen; /* size of last fread() */
9079 char_u *prev = NULL; /* previously read bytes, if any */
9080 long prevlen = 0; /* length of data in prev */
9081 long prevsize = 0; /* size of prev buffer */
9082 long maxline = MAXLNUM;
9083 long cnt = 0;
9084 char_u *p; /* position in buf */
9085 char_u *start; /* start of current line */
9086
9087 if (argvars[1].v_type != VAR_UNKNOWN)
9088 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009089 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009090 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009091 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9092 blob = TRUE;
9093
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009094 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009095 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009096 }
9097
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009098 if (blob)
9099 {
9100 if (rettv_blob_alloc(rettv) == FAIL)
9101 return;
9102 }
9103 else
9104 {
9105 if (rettv_list_alloc(rettv) == FAIL)
9106 return;
9107 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009108
9109 /* Always open the file in binary mode, library functions have a mind of
9110 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009111 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009114 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009115 return;
9116 }
9117
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009118 if (blob)
9119 {
9120 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9121 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009122 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009123 blob_free(rettv->vval.v_blob);
9124 }
9125 fclose(fd);
9126 return;
9127 }
9128
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009129 while (cnt < maxline || maxline < 0)
9130 {
9131 readlen = (int)fread(buf, 1, io_size, fd);
9132
9133 /* This for loop processes what was read, but is also entered at end
9134 * of file so that either:
9135 * - an incomplete line gets written
9136 * - a "binary" file gets an empty line at the end if it ends in a
9137 * newline. */
9138 for (p = buf, start = buf;
9139 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9140 ++p)
9141 {
9142 if (*p == '\n' || readlen <= 0)
9143 {
9144 listitem_T *li;
9145 char_u *s = NULL;
9146 long_u len = p - start;
9147
9148 /* Finished a line. Remove CRs before NL. */
9149 if (readlen > 0 && !binary)
9150 {
9151 while (len > 0 && start[len - 1] == '\r')
9152 --len;
9153 /* removal may cross back to the "prev" string */
9154 if (len == 0)
9155 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9156 --prevlen;
9157 }
9158 if (prevlen == 0)
9159 s = vim_strnsave(start, (int)len);
9160 else
9161 {
9162 /* Change "prev" buffer to be the right size. This way
9163 * the bytes are only copied once, and very long lines are
9164 * allocated only once. */
9165 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9166 {
9167 mch_memmove(s + prevlen, start, len);
9168 s[prevlen + len] = NUL;
9169 prev = NULL; /* the list will own the string */
9170 prevlen = prevsize = 0;
9171 }
9172 }
9173 if (s == NULL)
9174 {
9175 do_outofmem_msg((long_u) prevlen + len + 1);
9176 failed = TRUE;
9177 break;
9178 }
9179
9180 if ((li = listitem_alloc()) == NULL)
9181 {
9182 vim_free(s);
9183 failed = TRUE;
9184 break;
9185 }
9186 li->li_tv.v_type = VAR_STRING;
9187 li->li_tv.v_lock = 0;
9188 li->li_tv.vval.v_string = s;
9189 list_append(rettv->vval.v_list, li);
9190
9191 start = p + 1; /* step over newline */
9192 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9193 break;
9194 }
9195 else if (*p == NUL)
9196 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009197 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9198 * when finding the BF and check the previous two bytes. */
9199 else if (*p == 0xbf && enc_utf8 && !binary)
9200 {
9201 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9202 * + 1, these may be in the "prev" string. */
9203 char_u back1 = p >= buf + 1 ? p[-1]
9204 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9205 char_u back2 = p >= buf + 2 ? p[-2]
9206 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9207 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9208
9209 if (back2 == 0xef && back1 == 0xbb)
9210 {
9211 char_u *dest = p - 2;
9212
9213 /* Usually a BOM is at the beginning of a file, and so at
9214 * the beginning of a line; then we can just step over it.
9215 */
9216 if (start == dest)
9217 start = p + 1;
9218 else
9219 {
9220 /* have to shuffle buf to close gap */
9221 int adjust_prevlen = 0;
9222
9223 if (dest < buf)
9224 {
9225 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9226 dest = buf;
9227 }
9228 if (readlen > p - buf + 1)
9229 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9230 readlen -= 3 - adjust_prevlen;
9231 prevlen -= adjust_prevlen;
9232 p = dest - 1;
9233 }
9234 }
9235 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009236 } /* for */
9237
9238 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9239 break;
9240 if (start < p)
9241 {
9242 /* There's part of a line in buf, store it in "prev". */
9243 if (p - start + prevlen >= prevsize)
9244 {
9245 /* need bigger "prev" buffer */
9246 char_u *newprev;
9247
9248 /* A common use case is ordinary text files and "prev" gets a
9249 * fragment of a line, so the first allocation is made
9250 * small, to avoid repeatedly 'allocing' large and
9251 * 'reallocing' small. */
9252 if (prevsize == 0)
9253 prevsize = (long)(p - start);
9254 else
9255 {
9256 long grow50pc = (prevsize * 3) / 2;
9257 long growmin = (long)((p - start) * 2 + prevlen);
9258 prevsize = grow50pc > growmin ? grow50pc : growmin;
9259 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009260 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009261 if (newprev == NULL)
9262 {
9263 do_outofmem_msg((long_u)prevsize);
9264 failed = TRUE;
9265 break;
9266 }
9267 prev = newprev;
9268 }
9269 /* Add the line part to end of "prev". */
9270 mch_memmove(prev + prevlen, start, p - start);
9271 prevlen += (long)(p - start);
9272 }
9273 } /* while */
9274
9275 /*
9276 * For a negative line count use only the lines at the end of the file,
9277 * free the rest.
9278 */
9279 if (!failed && maxline < 0)
9280 while (cnt > -maxline)
9281 {
9282 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9283 --cnt;
9284 }
9285
9286 if (failed)
9287 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009288 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009289 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009290 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009291 }
9292
9293 vim_free(prev);
9294 fclose(fd);
9295}
9296
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009297 static void
9298return_register(int regname, typval_T *rettv)
9299{
9300 char_u buf[2] = {0, 0};
9301
9302 buf[0] = (char_u)regname;
9303 rettv->v_type = VAR_STRING;
9304 rettv->vval.v_string = vim_strsave(buf);
9305}
9306
9307/*
9308 * "reg_executing()" function
9309 */
9310 static void
9311f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9312{
9313 return_register(reg_executing, rettv);
9314}
9315
9316/*
9317 * "reg_recording()" function
9318 */
9319 static void
9320f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9321{
9322 return_register(reg_recording, rettv);
9323}
9324
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009326/*
9327 * Convert a List to proftime_T.
9328 * Return FAIL when there is something wrong.
9329 */
9330 static int
9331list2proftime(typval_T *arg, proftime_T *tm)
9332{
9333 long n1, n2;
9334 int error = FALSE;
9335
9336 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9337 || arg->vval.v_list->lv_len != 2)
9338 return FAIL;
9339 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9340 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009341# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009342 tm->HighPart = n1;
9343 tm->LowPart = n2;
9344# else
9345 tm->tv_sec = n1;
9346 tm->tv_usec = n2;
9347# endif
9348 return error ? FAIL : OK;
9349}
9350#endif /* FEAT_RELTIME */
9351
9352/*
9353 * "reltime()" function
9354 */
9355 static void
9356f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9357{
9358#ifdef FEAT_RELTIME
9359 proftime_T res;
9360 proftime_T start;
9361
9362 if (argvars[0].v_type == VAR_UNKNOWN)
9363 {
9364 /* No arguments: get current time. */
9365 profile_start(&res);
9366 }
9367 else if (argvars[1].v_type == VAR_UNKNOWN)
9368 {
9369 if (list2proftime(&argvars[0], &res) == FAIL)
9370 return;
9371 profile_end(&res);
9372 }
9373 else
9374 {
9375 /* Two arguments: compute the difference. */
9376 if (list2proftime(&argvars[0], &start) == FAIL
9377 || list2proftime(&argvars[1], &res) == FAIL)
9378 return;
9379 profile_sub(&res, &start);
9380 }
9381
9382 if (rettv_list_alloc(rettv) == OK)
9383 {
9384 long n1, n2;
9385
Bram Moolenaar4f974752019-02-17 17:44:42 +01009386# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009387 n1 = res.HighPart;
9388 n2 = res.LowPart;
9389# else
9390 n1 = res.tv_sec;
9391 n2 = res.tv_usec;
9392# endif
9393 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9394 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9395 }
9396#endif
9397}
9398
9399#ifdef FEAT_FLOAT
9400/*
9401 * "reltimefloat()" function
9402 */
9403 static void
9404f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9405{
9406# ifdef FEAT_RELTIME
9407 proftime_T tm;
9408# endif
9409
9410 rettv->v_type = VAR_FLOAT;
9411 rettv->vval.v_float = 0;
9412# ifdef FEAT_RELTIME
9413 if (list2proftime(&argvars[0], &tm) == OK)
9414 rettv->vval.v_float = profile_float(&tm);
9415# endif
9416}
9417#endif
9418
9419/*
9420 * "reltimestr()" function
9421 */
9422 static void
9423f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9424{
9425#ifdef FEAT_RELTIME
9426 proftime_T tm;
9427#endif
9428
9429 rettv->v_type = VAR_STRING;
9430 rettv->vval.v_string = NULL;
9431#ifdef FEAT_RELTIME
9432 if (list2proftime(&argvars[0], &tm) == OK)
9433 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9434#endif
9435}
9436
9437#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009438 static void
9439make_connection(void)
9440{
9441 if (X_DISPLAY == NULL
9442# ifdef FEAT_GUI
9443 && !gui.in_use
9444# endif
9445 )
9446 {
9447 x_force_connect = TRUE;
9448 setup_term_clip();
9449 x_force_connect = FALSE;
9450 }
9451}
9452
9453 static int
9454check_connection(void)
9455{
9456 make_connection();
9457 if (X_DISPLAY == NULL)
9458 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009459 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009460 return FAIL;
9461 }
9462 return OK;
9463}
9464#endif
9465
9466#ifdef FEAT_CLIENTSERVER
9467 static void
9468remote_common(typval_T *argvars, typval_T *rettv, int expr)
9469{
9470 char_u *server_name;
9471 char_u *keys;
9472 char_u *r = NULL;
9473 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009474 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009475# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009476 HWND w;
9477# else
9478 Window w;
9479# endif
9480
9481 if (check_restricted() || check_secure())
9482 return;
9483
9484# ifdef FEAT_X11
9485 if (check_connection() == FAIL)
9486 return;
9487# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009488 if (argvars[2].v_type != VAR_UNKNOWN
9489 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009490 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009491
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009492 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009493 if (server_name == NULL)
9494 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009495 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009496# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009497 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009498# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009499 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9500 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501# endif
9502 {
9503 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009504 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009505 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009506 vim_free(r);
9507 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009508 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009509 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009510 return;
9511 }
9512
9513 rettv->vval.v_string = r;
9514
9515 if (argvars[2].v_type != VAR_UNKNOWN)
9516 {
9517 dictitem_T v;
9518 char_u str[30];
9519 char_u *idvar;
9520
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009521 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009522 if (idvar != NULL && *idvar != NUL)
9523 {
9524 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9525 v.di_tv.v_type = VAR_STRING;
9526 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009527 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009528 vim_free(v.di_tv.vval.v_string);
9529 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009530 }
9531}
9532#endif
9533
9534/*
9535 * "remote_expr()" function
9536 */
9537 static void
9538f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9539{
9540 rettv->v_type = VAR_STRING;
9541 rettv->vval.v_string = NULL;
9542#ifdef FEAT_CLIENTSERVER
9543 remote_common(argvars, rettv, TRUE);
9544#endif
9545}
9546
9547/*
9548 * "remote_foreground()" function
9549 */
9550 static void
9551f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9552{
9553#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009554# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009555 /* On Win32 it's done in this application. */
9556 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009557 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009558
9559 if (server_name != NULL)
9560 serverForeground(server_name);
9561 }
9562# else
9563 /* Send a foreground() expression to the server. */
9564 argvars[1].v_type = VAR_STRING;
9565 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9566 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009567 rettv->v_type = VAR_STRING;
9568 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009569 remote_common(argvars, rettv, TRUE);
9570 vim_free(argvars[1].vval.v_string);
9571# endif
9572#endif
9573}
9574
9575 static void
9576f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9577{
9578#ifdef FEAT_CLIENTSERVER
9579 dictitem_T v;
9580 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009581# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009582 long_u n = 0;
9583# endif
9584 char_u *serverid;
9585
9586 if (check_restricted() || check_secure())
9587 {
9588 rettv->vval.v_number = -1;
9589 return;
9590 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009591 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009592 if (serverid == NULL)
9593 {
9594 rettv->vval.v_number = -1;
9595 return; /* type error; errmsg already given */
9596 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009597# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009598 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9599 if (n == 0)
9600 rettv->vval.v_number = -1;
9601 else
9602 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009603 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009604 rettv->vval.v_number = (s != NULL);
9605 }
9606# else
9607 if (check_connection() == FAIL)
9608 return;
9609
9610 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9611 serverStrToWin(serverid), &s);
9612# endif
9613
9614 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9615 {
9616 char_u *retvar;
9617
9618 v.di_tv.v_type = VAR_STRING;
9619 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009620 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009621 if (retvar != NULL)
9622 set_var(retvar, &v.di_tv, FALSE);
9623 vim_free(v.di_tv.vval.v_string);
9624 }
9625#else
9626 rettv->vval.v_number = -1;
9627#endif
9628}
9629
9630 static void
9631f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9632{
9633 char_u *r = NULL;
9634
9635#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009636 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009637
9638 if (serverid != NULL && !check_restricted() && !check_secure())
9639 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009640 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009641# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009642 /* The server's HWND is encoded in the 'id' parameter */
9643 long_u n = 0;
9644# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009645
9646 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009647 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009648
Bram Moolenaar4f974752019-02-17 17:44:42 +01009649# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009650 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9651 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009652 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009653 if (r == NULL)
9654# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009655 if (check_connection() == FAIL
9656 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9657 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009658# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009659 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009660 }
9661#endif
9662 rettv->v_type = VAR_STRING;
9663 rettv->vval.v_string = r;
9664}
9665
9666/*
9667 * "remote_send()" function
9668 */
9669 static void
9670f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9671{
9672 rettv->v_type = VAR_STRING;
9673 rettv->vval.v_string = NULL;
9674#ifdef FEAT_CLIENTSERVER
9675 remote_common(argvars, rettv, FALSE);
9676#endif
9677}
9678
9679/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009680 * "remote_startserver()" function
9681 */
9682 static void
9683f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9684{
9685#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009686 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009687
9688 if (server == NULL)
9689 return; /* type error; errmsg already given */
9690 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009691 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009692 else
9693 {
9694# ifdef FEAT_X11
9695 if (check_connection() == OK)
9696 serverRegisterName(X_DISPLAY, server);
9697# else
9698 serverSetName(server);
9699# endif
9700 }
9701#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009702 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009703#endif
9704}
9705
9706/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009707 * "remove()" function
9708 */
9709 static void
9710f_remove(typval_T *argvars, typval_T *rettv)
9711{
9712 list_T *l;
9713 listitem_T *item, *item2;
9714 listitem_T *li;
9715 long idx;
9716 long end;
9717 char_u *key;
9718 dict_T *d;
9719 dictitem_T *di;
9720 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009721 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009722
9723 if (argvars[0].v_type == VAR_DICT)
9724 {
9725 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009726 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009727 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009728 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009729 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009730 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009731 if (key != NULL)
9732 {
9733 di = dict_find(d, key, -1);
9734 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009735 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009736 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9737 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9738 {
9739 *rettv = di->di_tv;
9740 init_tv(&di->di_tv);
9741 dictitem_remove(d, di);
9742 }
9743 }
9744 }
9745 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009746 else if (argvars[0].v_type == VAR_BLOB)
9747 {
9748 idx = (long)tv_get_number_chk(&argvars[1], &error);
9749 if (!error)
9750 {
9751 blob_T *b = argvars[0].vval.v_blob;
9752 int len = blob_len(b);
9753 char_u *p;
9754
9755 if (idx < 0)
9756 // count from the end
9757 idx = len + idx;
9758 if (idx < 0 || idx >= len)
9759 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009760 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009761 return;
9762 }
9763 if (argvars[2].v_type == VAR_UNKNOWN)
9764 {
9765 // Remove one item, return its value.
9766 p = (char_u *)b->bv_ga.ga_data;
9767 rettv->vval.v_number = (varnumber_T) *(p + idx);
9768 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9769 --b->bv_ga.ga_len;
9770 }
9771 else
9772 {
9773 blob_T *blob;
9774
9775 // Remove range of items, return list with values.
9776 end = (long)tv_get_number_chk(&argvars[2], &error);
9777 if (error)
9778 return;
9779 if (end < 0)
9780 // count from the end
9781 end = len + end;
9782 if (end >= len || idx > end)
9783 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009784 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009785 return;
9786 }
9787 blob = blob_alloc();
9788 if (blob == NULL)
9789 return;
9790 blob->bv_ga.ga_len = end - idx + 1;
9791 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9792 {
9793 vim_free(blob);
9794 return;
9795 }
9796 p = (char_u *)b->bv_ga.ga_data;
9797 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9798 (size_t)(end - idx + 1));
9799 ++blob->bv_refcount;
9800 rettv->v_type = VAR_BLOB;
9801 rettv->vval.v_blob = blob;
9802
9803 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9804 b->bv_ga.ga_len -= end - idx + 1;
9805 }
9806 }
9807 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009808 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009809 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009810 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009811 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009812 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009813 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009814 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009815 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009816 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009817 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818 else
9819 {
9820 if (argvars[2].v_type == VAR_UNKNOWN)
9821 {
9822 /* Remove one item, return its value. */
9823 vimlist_remove(l, item, item);
9824 *rettv = item->li_tv;
9825 vim_free(item);
9826 }
9827 else
9828 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009829 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009830 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009832 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009833 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009834 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009835 else
9836 {
9837 int cnt = 0;
9838
9839 for (li = item; li != NULL; li = li->li_next)
9840 {
9841 ++cnt;
9842 if (li == item2)
9843 break;
9844 }
9845 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009846 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009847 else
9848 {
9849 vimlist_remove(l, item, item2);
9850 if (rettv_list_alloc(rettv) == OK)
9851 {
9852 l = rettv->vval.v_list;
9853 l->lv_first = item;
9854 l->lv_last = item2;
9855 item->li_prev = NULL;
9856 item2->li_next = NULL;
9857 l->lv_len = cnt;
9858 }
9859 }
9860 }
9861 }
9862 }
9863 }
9864}
9865
9866/*
9867 * "rename({from}, {to})" function
9868 */
9869 static void
9870f_rename(typval_T *argvars, typval_T *rettv)
9871{
9872 char_u buf[NUMBUFLEN];
9873
9874 if (check_restricted() || check_secure())
9875 rettv->vval.v_number = -1;
9876 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009877 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9878 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009879}
9880
9881/*
9882 * "repeat()" function
9883 */
9884 static void
9885f_repeat(typval_T *argvars, typval_T *rettv)
9886{
9887 char_u *p;
9888 int n;
9889 int slen;
9890 int len;
9891 char_u *r;
9892 int i;
9893
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009894 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009895 if (argvars[0].v_type == VAR_LIST)
9896 {
9897 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9898 while (n-- > 0)
9899 if (list_extend(rettv->vval.v_list,
9900 argvars[0].vval.v_list, NULL) == FAIL)
9901 break;
9902 }
9903 else
9904 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009905 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009906 rettv->v_type = VAR_STRING;
9907 rettv->vval.v_string = NULL;
9908
9909 slen = (int)STRLEN(p);
9910 len = slen * n;
9911 if (len <= 0)
9912 return;
9913
9914 r = alloc(len + 1);
9915 if (r != NULL)
9916 {
9917 for (i = 0; i < n; i++)
9918 mch_memmove(r + i * slen, p, (size_t)slen);
9919 r[len] = NUL;
9920 }
9921
9922 rettv->vval.v_string = r;
9923 }
9924}
9925
9926/*
9927 * "resolve()" function
9928 */
9929 static void
9930f_resolve(typval_T *argvars, typval_T *rettv)
9931{
9932 char_u *p;
9933#ifdef HAVE_READLINK
9934 char_u *buf = NULL;
9935#endif
9936
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009937 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009938#ifdef FEAT_SHORTCUT
9939 {
9940 char_u *v = NULL;
9941
Bram Moolenaardce1e892019-02-10 23:18:53 +01009942 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943 if (v != NULL)
9944 rettv->vval.v_string = v;
9945 else
9946 rettv->vval.v_string = vim_strsave(p);
9947 }
9948#else
9949# ifdef HAVE_READLINK
9950 {
9951 char_u *cpy;
9952 int len;
9953 char_u *remain = NULL;
9954 char_u *q;
9955 int is_relative_to_current = FALSE;
9956 int has_trailing_pathsep = FALSE;
9957 int limit = 100;
9958
9959 p = vim_strsave(p);
9960
9961 if (p[0] == '.' && (vim_ispathsep(p[1])
9962 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9963 is_relative_to_current = TRUE;
9964
9965 len = STRLEN(p);
9966 if (len > 0 && after_pathsep(p, p + len))
9967 {
9968 has_trailing_pathsep = TRUE;
9969 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9970 }
9971
9972 q = getnextcomp(p);
9973 if (*q != NUL)
9974 {
9975 /* Separate the first path component in "p", and keep the
9976 * remainder (beginning with the path separator). */
9977 remain = vim_strsave(q - 1);
9978 q[-1] = NUL;
9979 }
9980
9981 buf = alloc(MAXPATHL + 1);
9982 if (buf == NULL)
9983 goto fail;
9984
9985 for (;;)
9986 {
9987 for (;;)
9988 {
9989 len = readlink((char *)p, (char *)buf, MAXPATHL);
9990 if (len <= 0)
9991 break;
9992 buf[len] = NUL;
9993
9994 if (limit-- == 0)
9995 {
9996 vim_free(p);
9997 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009998 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009999 rettv->vval.v_string = NULL;
10000 goto fail;
10001 }
10002
10003 /* Ensure that the result will have a trailing path separator
10004 * if the argument has one. */
10005 if (remain == NULL && has_trailing_pathsep)
10006 add_pathsep(buf);
10007
10008 /* Separate the first path component in the link value and
10009 * concatenate the remainders. */
10010 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10011 if (*q != NUL)
10012 {
10013 if (remain == NULL)
10014 remain = vim_strsave(q - 1);
10015 else
10016 {
10017 cpy = concat_str(q - 1, remain);
10018 if (cpy != NULL)
10019 {
10020 vim_free(remain);
10021 remain = cpy;
10022 }
10023 }
10024 q[-1] = NUL;
10025 }
10026
10027 q = gettail(p);
10028 if (q > p && *q == NUL)
10029 {
10030 /* Ignore trailing path separator. */
10031 q[-1] = NUL;
10032 q = gettail(p);
10033 }
10034 if (q > p && !mch_isFullName(buf))
10035 {
10036 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010037 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010038 if (cpy != NULL)
10039 {
10040 STRCPY(cpy, p);
10041 STRCPY(gettail(cpy), buf);
10042 vim_free(p);
10043 p = cpy;
10044 }
10045 }
10046 else
10047 {
10048 vim_free(p);
10049 p = vim_strsave(buf);
10050 }
10051 }
10052
10053 if (remain == NULL)
10054 break;
10055
10056 /* Append the first path component of "remain" to "p". */
10057 q = getnextcomp(remain + 1);
10058 len = q - remain - (*q != NUL);
10059 cpy = vim_strnsave(p, STRLEN(p) + len);
10060 if (cpy != NULL)
10061 {
10062 STRNCAT(cpy, remain, len);
10063 vim_free(p);
10064 p = cpy;
10065 }
10066 /* Shorten "remain". */
10067 if (*q != NUL)
10068 STRMOVE(remain, q - 1);
10069 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010070 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010071 }
10072
10073 /* If the result is a relative path name, make it explicitly relative to
10074 * the current directory if and only if the argument had this form. */
10075 if (!vim_ispathsep(*p))
10076 {
10077 if (is_relative_to_current
10078 && *p != NUL
10079 && !(p[0] == '.'
10080 && (p[1] == NUL
10081 || vim_ispathsep(p[1])
10082 || (p[1] == '.'
10083 && (p[2] == NUL
10084 || vim_ispathsep(p[2]))))))
10085 {
10086 /* Prepend "./". */
10087 cpy = concat_str((char_u *)"./", p);
10088 if (cpy != NULL)
10089 {
10090 vim_free(p);
10091 p = cpy;
10092 }
10093 }
10094 else if (!is_relative_to_current)
10095 {
10096 /* Strip leading "./". */
10097 q = p;
10098 while (q[0] == '.' && vim_ispathsep(q[1]))
10099 q += 2;
10100 if (q > p)
10101 STRMOVE(p, p + 2);
10102 }
10103 }
10104
10105 /* Ensure that the result will have no trailing path separator
10106 * if the argument had none. But keep "/" or "//". */
10107 if (!has_trailing_pathsep)
10108 {
10109 q = p + STRLEN(p);
10110 if (after_pathsep(p, q))
10111 *gettail_sep(p) = NUL;
10112 }
10113
10114 rettv->vval.v_string = p;
10115 }
10116# else
10117 rettv->vval.v_string = vim_strsave(p);
10118# endif
10119#endif
10120
10121 simplify_filename(rettv->vval.v_string);
10122
10123#ifdef HAVE_READLINK
10124fail:
10125 vim_free(buf);
10126#endif
10127 rettv->v_type = VAR_STRING;
10128}
10129
10130/*
10131 * "reverse({list})" function
10132 */
10133 static void
10134f_reverse(typval_T *argvars, typval_T *rettv)
10135{
10136 list_T *l;
10137 listitem_T *li, *ni;
10138
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010139 if (argvars[0].v_type == VAR_BLOB)
10140 {
10141 blob_T *b = argvars[0].vval.v_blob;
10142 int i, len = blob_len(b);
10143
10144 for (i = 0; i < len / 2; i++)
10145 {
10146 int tmp = blob_get(b, i);
10147
10148 blob_set(b, i, blob_get(b, len - i - 1));
10149 blob_set(b, len - i - 1, tmp);
10150 }
10151 rettv_blob_set(rettv, b);
10152 return;
10153 }
10154
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010155 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010156 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010157 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010158 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010159 (char_u *)N_("reverse() argument"), TRUE))
10160 {
10161 li = l->lv_last;
10162 l->lv_first = l->lv_last = NULL;
10163 l->lv_len = 0;
10164 while (li != NULL)
10165 {
10166 ni = li->li_prev;
10167 list_append(l, li);
10168 li = ni;
10169 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010170 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010171 l->lv_idx = l->lv_len - l->lv_idx - 1;
10172 }
10173}
10174
10175#define SP_NOMOVE 0x01 /* don't move cursor */
10176#define SP_REPEAT 0x02 /* repeat to find outer pair */
10177#define SP_RETCOUNT 0x04 /* return matchcount */
10178#define SP_SETPCMARK 0x08 /* set previous context mark */
10179#define SP_START 0x10 /* accept match at start position */
10180#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10181#define SP_END 0x40 /* leave cursor at end of match */
10182#define SP_COLUMN 0x80 /* start at cursor column */
10183
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010184/*
10185 * Get flags for a search function.
10186 * Possibly sets "p_ws".
10187 * Returns BACKWARD, FORWARD or zero (for an error).
10188 */
10189 static int
10190get_search_arg(typval_T *varp, int *flagsp)
10191{
10192 int dir = FORWARD;
10193 char_u *flags;
10194 char_u nbuf[NUMBUFLEN];
10195 int mask;
10196
10197 if (varp->v_type != VAR_UNKNOWN)
10198 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010199 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010200 if (flags == NULL)
10201 return 0; /* type error; errmsg already given */
10202 while (*flags != NUL)
10203 {
10204 switch (*flags)
10205 {
10206 case 'b': dir = BACKWARD; break;
10207 case 'w': p_ws = TRUE; break;
10208 case 'W': p_ws = FALSE; break;
10209 default: mask = 0;
10210 if (flagsp != NULL)
10211 switch (*flags)
10212 {
10213 case 'c': mask = SP_START; break;
10214 case 'e': mask = SP_END; break;
10215 case 'm': mask = SP_RETCOUNT; break;
10216 case 'n': mask = SP_NOMOVE; break;
10217 case 'p': mask = SP_SUBPAT; break;
10218 case 'r': mask = SP_REPEAT; break;
10219 case 's': mask = SP_SETPCMARK; break;
10220 case 'z': mask = SP_COLUMN; break;
10221 }
10222 if (mask == 0)
10223 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010224 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010225 dir = 0;
10226 }
10227 else
10228 *flagsp |= mask;
10229 }
10230 if (dir == 0)
10231 break;
10232 ++flags;
10233 }
10234 }
10235 return dir;
10236}
10237
10238/*
10239 * Shared by search() and searchpos() functions.
10240 */
10241 static int
10242search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10243{
10244 int flags;
10245 char_u *pat;
10246 pos_T pos;
10247 pos_T save_cursor;
10248 int save_p_ws = p_ws;
10249 int dir;
10250 int retval = 0; /* default: FAIL */
10251 long lnum_stop = 0;
10252 proftime_T tm;
10253#ifdef FEAT_RELTIME
10254 long time_limit = 0;
10255#endif
10256 int options = SEARCH_KEEP;
10257 int subpatnum;
10258
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010259 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010260 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10261 if (dir == 0)
10262 goto theend;
10263 flags = *flagsp;
10264 if (flags & SP_START)
10265 options |= SEARCH_START;
10266 if (flags & SP_END)
10267 options |= SEARCH_END;
10268 if (flags & SP_COLUMN)
10269 options |= SEARCH_COL;
10270
10271 /* Optional arguments: line number to stop searching and timeout. */
10272 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10273 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010274 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010275 if (lnum_stop < 0)
10276 goto theend;
10277#ifdef FEAT_RELTIME
10278 if (argvars[3].v_type != VAR_UNKNOWN)
10279 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010280 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010281 if (time_limit < 0)
10282 goto theend;
10283 }
10284#endif
10285 }
10286
10287#ifdef FEAT_RELTIME
10288 /* Set the time limit, if there is one. */
10289 profile_setlimit(time_limit, &tm);
10290#endif
10291
10292 /*
10293 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10294 * Check to make sure only those flags are set.
10295 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10296 * flags cannot be set. Check for that condition also.
10297 */
10298 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10299 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10300 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010301 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010302 goto theend;
10303 }
10304
10305 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010306 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010307 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010308 if (subpatnum != FAIL)
10309 {
10310 if (flags & SP_SUBPAT)
10311 retval = subpatnum;
10312 else
10313 retval = pos.lnum;
10314 if (flags & SP_SETPCMARK)
10315 setpcmark();
10316 curwin->w_cursor = pos;
10317 if (match_pos != NULL)
10318 {
10319 /* Store the match cursor position */
10320 match_pos->lnum = pos.lnum;
10321 match_pos->col = pos.col + 1;
10322 }
10323 /* "/$" will put the cursor after the end of the line, may need to
10324 * correct that here */
10325 check_cursor();
10326 }
10327
10328 /* If 'n' flag is used: restore cursor position. */
10329 if (flags & SP_NOMOVE)
10330 curwin->w_cursor = save_cursor;
10331 else
10332 curwin->w_set_curswant = TRUE;
10333theend:
10334 p_ws = save_p_ws;
10335
10336 return retval;
10337}
10338
10339#ifdef FEAT_FLOAT
10340
10341/*
10342 * round() is not in C90, use ceil() or floor() instead.
10343 */
10344 float_T
10345vim_round(float_T f)
10346{
10347 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10348}
10349
10350/*
10351 * "round({float})" function
10352 */
10353 static void
10354f_round(typval_T *argvars, typval_T *rettv)
10355{
10356 float_T f = 0.0;
10357
10358 rettv->v_type = VAR_FLOAT;
10359 if (get_float_arg(argvars, &f) == OK)
10360 rettv->vval.v_float = vim_round(f);
10361 else
10362 rettv->vval.v_float = 0.0;
10363}
10364#endif
10365
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010366#ifdef FEAT_RUBY
10367/*
10368 * "rubyeval()" function
10369 */
10370 static void
10371f_rubyeval(typval_T *argvars, typval_T *rettv)
10372{
10373 char_u *str;
10374 char_u buf[NUMBUFLEN];
10375
10376 str = tv_get_string_buf(&argvars[0], buf);
10377 do_rubyeval(str, rettv);
10378}
10379#endif
10380
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010381/*
10382 * "screenattr()" function
10383 */
10384 static void
10385f_screenattr(typval_T *argvars, typval_T *rettv)
10386{
10387 int row;
10388 int col;
10389 int c;
10390
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010391 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10392 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010393 if (row < 0 || row >= screen_Rows
10394 || col < 0 || col >= screen_Columns)
10395 c = -1;
10396 else
10397 c = ScreenAttrs[LineOffset[row] + col];
10398 rettv->vval.v_number = c;
10399}
10400
10401/*
10402 * "screenchar()" function
10403 */
10404 static void
10405f_screenchar(typval_T *argvars, typval_T *rettv)
10406{
10407 int row;
10408 int col;
10409 int off;
10410 int c;
10411
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010412 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10413 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010414 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010415 c = -1;
10416 else
10417 {
10418 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010419 if (enc_utf8 && ScreenLinesUC[off] != 0)
10420 c = ScreenLinesUC[off];
10421 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010422 c = ScreenLines[off];
10423 }
10424 rettv->vval.v_number = c;
10425}
10426
10427/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010428 * "screenchars()" function
10429 */
10430 static void
10431f_screenchars(typval_T *argvars, typval_T *rettv)
10432{
10433 int row;
10434 int col;
10435 int off;
10436 int c;
10437 int i;
10438
10439 if (rettv_list_alloc(rettv) == FAIL)
10440 return;
10441 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10442 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10443 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10444 return;
10445
10446 off = LineOffset[row] + col;
10447 if (enc_utf8 && ScreenLinesUC[off] != 0)
10448 c = ScreenLinesUC[off];
10449 else
10450 c = ScreenLines[off];
10451 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10452
10453 if (enc_utf8)
10454
10455 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10456 list_append_number(rettv->vval.v_list,
10457 (varnumber_T)ScreenLinesC[i][off]);
10458}
10459
10460/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010461 * "screencol()" function
10462 *
10463 * First column is 1 to be consistent with virtcol().
10464 */
10465 static void
10466f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10467{
10468 rettv->vval.v_number = screen_screencol() + 1;
10469}
10470
10471/*
10472 * "screenrow()" function
10473 */
10474 static void
10475f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10476{
10477 rettv->vval.v_number = screen_screenrow() + 1;
10478}
10479
10480/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010481 * "screenstring()" function
10482 */
10483 static void
10484f_screenstring(typval_T *argvars, typval_T *rettv)
10485{
10486 int row;
10487 int col;
10488 int off;
10489 int c;
10490 int i;
10491 char_u buf[MB_MAXBYTES + 1];
10492 int buflen = 0;
10493
10494 rettv->vval.v_string = NULL;
10495 rettv->v_type = VAR_STRING;
10496
10497 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10498 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10499 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10500 return;
10501
10502 off = LineOffset[row] + col;
10503 if (enc_utf8 && ScreenLinesUC[off] != 0)
10504 c = ScreenLinesUC[off];
10505 else
10506 c = ScreenLines[off];
10507 buflen += mb_char2bytes(c, buf);
10508
10509 if (enc_utf8)
10510 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10511 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10512
10513 buf[buflen] = NUL;
10514 rettv->vval.v_string = vim_strsave(buf);
10515}
10516
10517/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010518 * "search()" function
10519 */
10520 static void
10521f_search(typval_T *argvars, typval_T *rettv)
10522{
10523 int flags = 0;
10524
10525 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10526}
10527
10528/*
10529 * "searchdecl()" function
10530 */
10531 static void
10532f_searchdecl(typval_T *argvars, typval_T *rettv)
10533{
10534 int locally = 1;
10535 int thisblock = 0;
10536 int error = FALSE;
10537 char_u *name;
10538
10539 rettv->vval.v_number = 1; /* default: FAIL */
10540
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010541 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010542 if (argvars[1].v_type != VAR_UNKNOWN)
10543 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010544 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010545 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010546 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010547 }
10548 if (!error && name != NULL)
10549 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10550 locally, thisblock, SEARCH_KEEP) == FAIL;
10551}
10552
10553/*
10554 * Used by searchpair() and searchpairpos()
10555 */
10556 static int
10557searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10558{
10559 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010560 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010561 int save_p_ws = p_ws;
10562 int dir;
10563 int flags = 0;
10564 char_u nbuf1[NUMBUFLEN];
10565 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010566 int retval = 0; /* default: FAIL */
10567 long lnum_stop = 0;
10568 long time_limit = 0;
10569
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010570 /* Get the three pattern arguments: start, middle, end. Will result in an
10571 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010572 spat = tv_get_string_chk(&argvars[0]);
10573 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10574 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010575 if (spat == NULL || mpat == NULL || epat == NULL)
10576 goto theend; /* type error */
10577
10578 /* Handle the optional fourth argument: flags */
10579 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10580 if (dir == 0)
10581 goto theend;
10582
10583 /* Don't accept SP_END or SP_SUBPAT.
10584 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10585 */
10586 if ((flags & (SP_END | SP_SUBPAT)) != 0
10587 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10588 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010589 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010590 goto theend;
10591 }
10592
10593 /* Using 'r' implies 'W', otherwise it doesn't work. */
10594 if (flags & SP_REPEAT)
10595 p_ws = FALSE;
10596
10597 /* Optional fifth argument: skip expression */
10598 if (argvars[3].v_type == VAR_UNKNOWN
10599 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010600 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010601 else
10602 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010603 skip = &argvars[4];
10604 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10605 && skip->v_type != VAR_STRING)
10606 {
10607 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010608 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010609 goto theend;
10610 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010611 if (argvars[5].v_type != VAR_UNKNOWN)
10612 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010613 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010614 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010615 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010616 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010617 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010618 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619#ifdef FEAT_RELTIME
10620 if (argvars[6].v_type != VAR_UNKNOWN)
10621 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010622 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010623 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010624 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010625 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010626 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010627 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010628 }
10629#endif
10630 }
10631 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010632
10633 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10634 match_pos, lnum_stop, time_limit);
10635
10636theend:
10637 p_ws = save_p_ws;
10638
10639 return retval;
10640}
10641
10642/*
10643 * "searchpair()" function
10644 */
10645 static void
10646f_searchpair(typval_T *argvars, typval_T *rettv)
10647{
10648 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10649}
10650
10651/*
10652 * "searchpairpos()" function
10653 */
10654 static void
10655f_searchpairpos(typval_T *argvars, typval_T *rettv)
10656{
10657 pos_T match_pos;
10658 int lnum = 0;
10659 int col = 0;
10660
10661 if (rettv_list_alloc(rettv) == FAIL)
10662 return;
10663
10664 if (searchpair_cmn(argvars, &match_pos) > 0)
10665 {
10666 lnum = match_pos.lnum;
10667 col = match_pos.col;
10668 }
10669
10670 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10671 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10672}
10673
10674/*
10675 * Search for a start/middle/end thing.
10676 * Used by searchpair(), see its documentation for the details.
10677 * Returns 0 or -1 for no match,
10678 */
10679 long
10680do_searchpair(
10681 char_u *spat, /* start pattern */
10682 char_u *mpat, /* middle pattern */
10683 char_u *epat, /* end pattern */
10684 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010685 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010686 int flags, /* SP_SETPCMARK and other SP_ values */
10687 pos_T *match_pos,
10688 linenr_T lnum_stop, /* stop at this line if not zero */
10689 long time_limit UNUSED) /* stop after this many msec */
10690{
10691 char_u *save_cpo;
10692 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10693 long retval = 0;
10694 pos_T pos;
10695 pos_T firstpos;
10696 pos_T foundpos;
10697 pos_T save_cursor;
10698 pos_T save_pos;
10699 int n;
10700 int r;
10701 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010702 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010703 int err;
10704 int options = SEARCH_KEEP;
10705 proftime_T tm;
10706
10707 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10708 save_cpo = p_cpo;
10709 p_cpo = empty_option;
10710
10711#ifdef FEAT_RELTIME
10712 /* Set the time limit, if there is one. */
10713 profile_setlimit(time_limit, &tm);
10714#endif
10715
10716 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10717 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010718 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
10719 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010720 if (pat2 == NULL || pat3 == NULL)
10721 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010722 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010723 if (*mpat == NUL)
10724 STRCPY(pat3, pat2);
10725 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010726 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010727 spat, epat, mpat);
10728 if (flags & SP_START)
10729 options |= SEARCH_START;
10730
Bram Moolenaar48570482017-10-30 21:48:41 +010010731 if (skip != NULL)
10732 {
10733 /* Empty string means to not use the skip expression. */
10734 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10735 use_skip = skip->vval.v_string != NULL
10736 && *skip->vval.v_string != NUL;
10737 }
10738
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010739 save_cursor = curwin->w_cursor;
10740 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010741 CLEAR_POS(&firstpos);
10742 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010743 pat = pat3;
10744 for (;;)
10745 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010746 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010747 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010748 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010749 /* didn't find it or found the first match again: FAIL */
10750 break;
10751
10752 if (firstpos.lnum == 0)
10753 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010754 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010755 {
10756 /* Found the same position again. Can happen with a pattern that
10757 * has "\zs" at the end and searching backwards. Advance one
10758 * character and try again. */
10759 if (dir == BACKWARD)
10760 decl(&pos);
10761 else
10762 incl(&pos);
10763 }
10764 foundpos = pos;
10765
10766 /* clear the start flag to avoid getting stuck here */
10767 options &= ~SEARCH_START;
10768
10769 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010770 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010771 {
10772 save_pos = curwin->w_cursor;
10773 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010774 err = FALSE;
10775 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010776 curwin->w_cursor = save_pos;
10777 if (err)
10778 {
10779 /* Evaluating {skip} caused an error, break here. */
10780 curwin->w_cursor = save_cursor;
10781 retval = -1;
10782 break;
10783 }
10784 if (r)
10785 continue;
10786 }
10787
10788 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10789 {
10790 /* Found end when searching backwards or start when searching
10791 * forward: nested pair. */
10792 ++nest;
10793 pat = pat2; /* nested, don't search for middle */
10794 }
10795 else
10796 {
10797 /* Found end when searching forward or start when searching
10798 * backward: end of (nested) pair; or found middle in outer pair. */
10799 if (--nest == 1)
10800 pat = pat3; /* outer level, search for middle */
10801 }
10802
10803 if (nest == 0)
10804 {
10805 /* Found the match: return matchcount or line number. */
10806 if (flags & SP_RETCOUNT)
10807 ++retval;
10808 else
10809 retval = pos.lnum;
10810 if (flags & SP_SETPCMARK)
10811 setpcmark();
10812 curwin->w_cursor = pos;
10813 if (!(flags & SP_REPEAT))
10814 break;
10815 nest = 1; /* search for next unmatched */
10816 }
10817 }
10818
10819 if (match_pos != NULL)
10820 {
10821 /* Store the match cursor position */
10822 match_pos->lnum = curwin->w_cursor.lnum;
10823 match_pos->col = curwin->w_cursor.col + 1;
10824 }
10825
10826 /* If 'n' flag is used or search failed: restore cursor position. */
10827 if ((flags & SP_NOMOVE) || retval == 0)
10828 curwin->w_cursor = save_cursor;
10829
10830theend:
10831 vim_free(pat2);
10832 vim_free(pat3);
10833 if (p_cpo == empty_option)
10834 p_cpo = save_cpo;
10835 else
10836 /* Darn, evaluating the {skip} expression changed the value. */
10837 free_string_option(save_cpo);
10838
10839 return retval;
10840}
10841
10842/*
10843 * "searchpos()" function
10844 */
10845 static void
10846f_searchpos(typval_T *argvars, typval_T *rettv)
10847{
10848 pos_T match_pos;
10849 int lnum = 0;
10850 int col = 0;
10851 int n;
10852 int flags = 0;
10853
10854 if (rettv_list_alloc(rettv) == FAIL)
10855 return;
10856
10857 n = search_cmn(argvars, &match_pos, &flags);
10858 if (n > 0)
10859 {
10860 lnum = match_pos.lnum;
10861 col = match_pos.col;
10862 }
10863
10864 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10865 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10866 if (flags & SP_SUBPAT)
10867 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10868}
10869
10870 static void
10871f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10872{
10873#ifdef FEAT_CLIENTSERVER
10874 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010875 char_u *server = tv_get_string_chk(&argvars[0]);
10876 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010877
10878 rettv->vval.v_number = -1;
10879 if (server == NULL || reply == NULL)
10880 return;
10881 if (check_restricted() || check_secure())
10882 return;
10883# ifdef FEAT_X11
10884 if (check_connection() == FAIL)
10885 return;
10886# endif
10887
10888 if (serverSendReply(server, reply) < 0)
10889 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010890 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010891 return;
10892 }
10893 rettv->vval.v_number = 0;
10894#else
10895 rettv->vval.v_number = -1;
10896#endif
10897}
10898
10899 static void
10900f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10901{
10902 char_u *r = NULL;
10903
10904#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010905# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010906 r = serverGetVimNames();
10907# else
10908 make_connection();
10909 if (X_DISPLAY != NULL)
10910 r = serverGetVimNames(X_DISPLAY);
10911# endif
10912#endif
10913 rettv->v_type = VAR_STRING;
10914 rettv->vval.v_string = r;
10915}
10916
10917/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010918 * "setbufline()" function
10919 */
10920 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010921f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010922{
10923 linenr_T lnum;
10924 buf_T *buf;
10925
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010926 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010927 if (buf == NULL)
10928 rettv->vval.v_number = 1; /* FAIL */
10929 else
10930 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010931 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010932 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010933 }
10934}
10935
10936/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010937 * "setbufvar()" function
10938 */
10939 static void
10940f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10941{
10942 buf_T *buf;
10943 char_u *varname, *bufvarname;
10944 typval_T *varp;
10945 char_u nbuf[NUMBUFLEN];
10946
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010947 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010948 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010949 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10950 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010951 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010952 varp = &argvars[2];
10953
10954 if (buf != NULL && varname != NULL && varp != NULL)
10955 {
10956 if (*varname == '&')
10957 {
10958 long numval;
10959 char_u *strval;
10960 int error = FALSE;
10961 aco_save_T aco;
10962
10963 /* set curbuf to be our buf, temporarily */
10964 aucmd_prepbuf(&aco, buf);
10965
10966 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010967 numval = (long)tv_get_number_chk(varp, &error);
10968 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010969 if (!error && strval != NULL)
10970 set_option_value(varname, numval, strval, OPT_LOCAL);
10971
10972 /* reset notion of buffer */
10973 aucmd_restbuf(&aco);
10974 }
10975 else
10976 {
10977 buf_T *save_curbuf = curbuf;
10978
Bram Moolenaar964b3742019-05-24 18:54:09 +020010979 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010980 if (bufvarname != NULL)
10981 {
10982 curbuf = buf;
10983 STRCPY(bufvarname, "b:");
10984 STRCPY(bufvarname + 2, varname);
10985 set_var(bufvarname, varp, TRUE);
10986 vim_free(bufvarname);
10987 curbuf = save_curbuf;
10988 }
10989 }
10990 }
10991}
10992
10993 static void
10994f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10995{
10996 dict_T *d;
10997 dictitem_T *di;
10998 char_u *csearch;
10999
11000 if (argvars[0].v_type != VAR_DICT)
11001 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011002 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011003 return;
11004 }
11005
11006 if ((d = argvars[0].vval.v_dict) != NULL)
11007 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011008 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011009 if (csearch != NULL)
11010 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011011 if (enc_utf8)
11012 {
11013 int pcc[MAX_MCO];
11014 int c = utfc_ptr2char(csearch, pcc);
11015
11016 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11017 }
11018 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011019 set_last_csearch(PTR2CHAR(csearch),
11020 csearch, MB_PTR2LEN(csearch));
11021 }
11022
11023 di = dict_find(d, (char_u *)"forward", -1);
11024 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011025 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011026 ? FORWARD : BACKWARD);
11027
11028 di = dict_find(d, (char_u *)"until", -1);
11029 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011030 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011031 }
11032}
11033
11034/*
11035 * "setcmdpos()" function
11036 */
11037 static void
11038f_setcmdpos(typval_T *argvars, typval_T *rettv)
11039{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011040 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011041
11042 if (pos >= 0)
11043 rettv->vval.v_number = set_cmdline_pos(pos);
11044}
11045
11046/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011047 * "setenv()" function
11048 */
11049 static void
11050f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11051{
11052 char_u namebuf[NUMBUFLEN];
11053 char_u valbuf[NUMBUFLEN];
11054 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11055
11056 if (argvars[1].v_type == VAR_SPECIAL
11057 && argvars[1].vval.v_number == VVAL_NULL)
11058 vim_unsetenv(name);
11059 else
11060 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11061}
11062
11063/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011064 * "setfperm({fname}, {mode})" function
11065 */
11066 static void
11067f_setfperm(typval_T *argvars, typval_T *rettv)
11068{
11069 char_u *fname;
11070 char_u modebuf[NUMBUFLEN];
11071 char_u *mode_str;
11072 int i;
11073 int mask;
11074 int mode = 0;
11075
11076 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011077 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011078 if (fname == NULL)
11079 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011080 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011081 if (mode_str == NULL)
11082 return;
11083 if (STRLEN(mode_str) != 9)
11084 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011085 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011086 return;
11087 }
11088
11089 mask = 1;
11090 for (i = 8; i >= 0; --i)
11091 {
11092 if (mode_str[i] != '-')
11093 mode |= mask;
11094 mask = mask << 1;
11095 }
11096 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11097}
11098
11099/*
11100 * "setline()" function
11101 */
11102 static void
11103f_setline(typval_T *argvars, typval_T *rettv)
11104{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011105 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011106
Bram Moolenaarca851592018-06-06 21:04:07 +020011107 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011108}
11109
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011110/*
11111 * Used by "setqflist()" and "setloclist()" functions
11112 */
11113 static void
11114set_qf_ll_list(
11115 win_T *wp UNUSED,
11116 typval_T *list_arg UNUSED,
11117 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011118 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011119 typval_T *rettv)
11120{
11121#ifdef FEAT_QUICKFIX
11122 static char *e_invact = N_("E927: Invalid action: '%s'");
11123 char_u *act;
11124 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011125 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011126#endif
11127
11128 rettv->vval.v_number = -1;
11129
11130#ifdef FEAT_QUICKFIX
11131 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011132 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011133 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011134 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011135 else
11136 {
11137 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011138 dict_T *d = NULL;
11139 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011140
11141 if (action_arg->v_type == VAR_STRING)
11142 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011143 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011144 if (act == NULL)
11145 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011146 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11147 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011148 action = *act;
11149 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011150 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011151 }
11152 else if (action_arg->v_type == VAR_UNKNOWN)
11153 action = ' ';
11154 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011155 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011156
Bram Moolenaard823fa92016-08-12 16:29:27 +020011157 if (action_arg->v_type != VAR_UNKNOWN
11158 && what_arg->v_type != VAR_UNKNOWN)
11159 {
11160 if (what_arg->v_type == VAR_DICT)
11161 d = what_arg->vval.v_dict;
11162 else
11163 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011164 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011165 valid_dict = FALSE;
11166 }
11167 }
11168
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011169 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011170 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011171 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11172 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011173 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011174 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011175 }
11176#endif
11177}
11178
11179/*
11180 * "setloclist()" function
11181 */
11182 static void
11183f_setloclist(typval_T *argvars, typval_T *rettv)
11184{
11185 win_T *win;
11186
11187 rettv->vval.v_number = -1;
11188
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011189 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011190 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011191 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011192}
11193
11194/*
11195 * "setmatches()" function
11196 */
11197 static void
11198f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11199{
11200#ifdef FEAT_SEARCH_EXTRA
11201 list_T *l;
11202 listitem_T *li;
11203 dict_T *d;
11204 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011205 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011206
11207 rettv->vval.v_number = -1;
11208 if (argvars[0].v_type != VAR_LIST)
11209 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011210 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011211 return;
11212 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011213 if (win == NULL)
11214 return;
11215
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011216 if ((l = argvars[0].vval.v_list) != NULL)
11217 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011218 /* To some extent make sure that we are dealing with a list from
11219 * "getmatches()". */
11220 li = l->lv_first;
11221 while (li != NULL)
11222 {
11223 if (li->li_tv.v_type != VAR_DICT
11224 || (d = li->li_tv.vval.v_dict) == NULL)
11225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011226 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011227 return;
11228 }
11229 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11230 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11231 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11232 && dict_find(d, (char_u *)"priority", -1) != NULL
11233 && dict_find(d, (char_u *)"id", -1) != NULL))
11234 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011235 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011236 return;
11237 }
11238 li = li->li_next;
11239 }
11240
Bram Moolenaaraff74912019-03-30 18:11:49 +010011241 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011242 li = l->lv_first;
11243 while (li != NULL)
11244 {
11245 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011246 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011247 dictitem_T *di;
11248 char_u *group;
11249 int priority;
11250 int id;
11251 char_u *conceal;
11252
11253 d = li->li_tv.vval.v_dict;
11254 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11255 {
11256 if (s == NULL)
11257 {
11258 s = list_alloc();
11259 if (s == NULL)
11260 return;
11261 }
11262
11263 /* match from matchaddpos() */
11264 for (i = 1; i < 9; i++)
11265 {
11266 sprintf((char *)buf, (char *)"pos%d", i);
11267 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11268 {
11269 if (di->di_tv.v_type != VAR_LIST)
11270 return;
11271
11272 list_append_tv(s, &di->di_tv);
11273 s->lv_refcount++;
11274 }
11275 else
11276 break;
11277 }
11278 }
11279
Bram Moolenaar8f667172018-12-14 15:38:31 +010011280 group = dict_get_string(d, (char_u *)"group", TRUE);
11281 priority = (int)dict_get_number(d, (char_u *)"priority");
11282 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011283 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011284 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011285 : NULL;
11286 if (i == 0)
11287 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011288 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011289 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011290 priority, id, NULL, conceal);
11291 }
11292 else
11293 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011294 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011295 list_unref(s);
11296 s = NULL;
11297 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011298 vim_free(group);
11299 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011300
11301 li = li->li_next;
11302 }
11303 rettv->vval.v_number = 0;
11304 }
11305#endif
11306}
11307
11308/*
11309 * "setpos()" function
11310 */
11311 static void
11312f_setpos(typval_T *argvars, typval_T *rettv)
11313{
11314 pos_T pos;
11315 int fnum;
11316 char_u *name;
11317 colnr_T curswant = -1;
11318
11319 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011320 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011321 if (name != NULL)
11322 {
11323 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11324 {
11325 if (--pos.col < 0)
11326 pos.col = 0;
11327 if (name[0] == '.' && name[1] == NUL)
11328 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011329 /* set cursor; "fnum" is ignored */
11330 curwin->w_cursor = pos;
11331 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011332 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011333 curwin->w_curswant = curswant - 1;
11334 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011335 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011336 check_cursor();
11337 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011338 }
11339 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11340 {
11341 /* set mark */
11342 if (setmark_pos(name[1], &pos, fnum) == OK)
11343 rettv->vval.v_number = 0;
11344 }
11345 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011346 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011347 }
11348 }
11349}
11350
11351/*
11352 * "setqflist()" function
11353 */
11354 static void
11355f_setqflist(typval_T *argvars, typval_T *rettv)
11356{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011357 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011358}
11359
11360/*
11361 * "setreg()" function
11362 */
11363 static void
11364f_setreg(typval_T *argvars, typval_T *rettv)
11365{
11366 int regname;
11367 char_u *strregname;
11368 char_u *stropt;
11369 char_u *strval;
11370 int append;
11371 char_u yank_type;
11372 long block_len;
11373
11374 block_len = -1;
11375 yank_type = MAUTO;
11376 append = FALSE;
11377
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011378 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011379 rettv->vval.v_number = 1; /* FAIL is default */
11380
11381 if (strregname == NULL)
11382 return; /* type error; errmsg already given */
11383 regname = *strregname;
11384 if (regname == 0 || regname == '@')
11385 regname = '"';
11386
11387 if (argvars[2].v_type != VAR_UNKNOWN)
11388 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011389 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011390 if (stropt == NULL)
11391 return; /* type error */
11392 for (; *stropt != NUL; ++stropt)
11393 switch (*stropt)
11394 {
11395 case 'a': case 'A': /* append */
11396 append = TRUE;
11397 break;
11398 case 'v': case 'c': /* character-wise selection */
11399 yank_type = MCHAR;
11400 break;
11401 case 'V': case 'l': /* line-wise selection */
11402 yank_type = MLINE;
11403 break;
11404 case 'b': case Ctrl_V: /* block-wise selection */
11405 yank_type = MBLOCK;
11406 if (VIM_ISDIGIT(stropt[1]))
11407 {
11408 ++stropt;
11409 block_len = getdigits(&stropt) - 1;
11410 --stropt;
11411 }
11412 break;
11413 }
11414 }
11415
11416 if (argvars[1].v_type == VAR_LIST)
11417 {
11418 char_u **lstval;
11419 char_u **allocval;
11420 char_u buf[NUMBUFLEN];
11421 char_u **curval;
11422 char_u **curallocval;
11423 list_T *ll = argvars[1].vval.v_list;
11424 listitem_T *li;
11425 int len;
11426
11427 /* If the list is NULL handle like an empty list. */
11428 len = ll == NULL ? 0 : ll->lv_len;
11429
11430 /* First half: use for pointers to result lines; second half: use for
11431 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011432 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011433 if (lstval == NULL)
11434 return;
11435 curval = lstval;
11436 allocval = lstval + len + 2;
11437 curallocval = allocval;
11438
11439 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11440 li = li->li_next)
11441 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011442 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011443 if (strval == NULL)
11444 goto free_lstval;
11445 if (strval == buf)
11446 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011447 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011448 * overwrite the string. */
11449 strval = vim_strsave(buf);
11450 if (strval == NULL)
11451 goto free_lstval;
11452 *curallocval++ = strval;
11453 }
11454 *curval++ = strval;
11455 }
11456 *curval++ = NULL;
11457
11458 write_reg_contents_lst(regname, lstval, -1,
11459 append, yank_type, block_len);
11460free_lstval:
11461 while (curallocval > allocval)
11462 vim_free(*--curallocval);
11463 vim_free(lstval);
11464 }
11465 else
11466 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011467 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011468 if (strval == NULL)
11469 return;
11470 write_reg_contents_ex(regname, strval, -1,
11471 append, yank_type, block_len);
11472 }
11473 rettv->vval.v_number = 0;
11474}
11475
11476/*
11477 * "settabvar()" function
11478 */
11479 static void
11480f_settabvar(typval_T *argvars, typval_T *rettv)
11481{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011482 tabpage_T *save_curtab;
11483 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011484 char_u *varname, *tabvarname;
11485 typval_T *varp;
11486
11487 rettv->vval.v_number = 0;
11488
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011489 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011490 return;
11491
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011492 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11493 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011494 varp = &argvars[2];
11495
Bram Moolenaar4033c552017-09-16 20:54:51 +020011496 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011497 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011498 save_curtab = curtab;
11499 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011500
Bram Moolenaar964b3742019-05-24 18:54:09 +020011501 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011502 if (tabvarname != NULL)
11503 {
11504 STRCPY(tabvarname, "t:");
11505 STRCPY(tabvarname + 2, varname);
11506 set_var(tabvarname, varp, TRUE);
11507 vim_free(tabvarname);
11508 }
11509
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011510 /* Restore current tabpage */
11511 if (valid_tabpage(save_curtab))
11512 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011513 }
11514}
11515
11516/*
11517 * "settabwinvar()" function
11518 */
11519 static void
11520f_settabwinvar(typval_T *argvars, typval_T *rettv)
11521{
11522 setwinvar(argvars, rettv, 1);
11523}
11524
11525/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011526 * "settagstack()" function
11527 */
11528 static void
11529f_settagstack(typval_T *argvars, typval_T *rettv)
11530{
11531 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11532 win_T *wp;
11533 dict_T *d;
11534 int action = 'r';
11535
11536 rettv->vval.v_number = -1;
11537
11538 // first argument: window number or id
11539 wp = find_win_by_nr_or_id(&argvars[0]);
11540 if (wp == NULL)
11541 return;
11542
11543 // second argument: dict with items to set in the tag stack
11544 if (argvars[1].v_type != VAR_DICT)
11545 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011546 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011547 return;
11548 }
11549 d = argvars[1].vval.v_dict;
11550 if (d == NULL)
11551 return;
11552
11553 // third argument: action - 'a' for append and 'r' for replace.
11554 // default is to replace the stack.
11555 if (argvars[2].v_type == VAR_UNKNOWN)
11556 action = 'r';
11557 else if (argvars[2].v_type == VAR_STRING)
11558 {
11559 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011560 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011561 if (actstr == NULL)
11562 return;
11563 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11564 action = *actstr;
11565 else
11566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011567 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011568 return;
11569 }
11570 }
11571 else
11572 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011573 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011574 return;
11575 }
11576
11577 if (set_tagstack(wp, d, action) == OK)
11578 rettv->vval.v_number = 0;
11579}
11580
11581/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011582 * "setwinvar()" function
11583 */
11584 static void
11585f_setwinvar(typval_T *argvars, typval_T *rettv)
11586{
11587 setwinvar(argvars, rettv, 0);
11588}
11589
11590#ifdef FEAT_CRYPT
11591/*
11592 * "sha256({string})" function
11593 */
11594 static void
11595f_sha256(typval_T *argvars, typval_T *rettv)
11596{
11597 char_u *p;
11598
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011599 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011600 rettv->vval.v_string = vim_strsave(
11601 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11602 rettv->v_type = VAR_STRING;
11603}
11604#endif /* FEAT_CRYPT */
11605
11606/*
11607 * "shellescape({string})" function
11608 */
11609 static void
11610f_shellescape(typval_T *argvars, typval_T *rettv)
11611{
Bram Moolenaar20615522017-06-05 18:46:26 +020011612 int do_special = non_zero_arg(&argvars[1]);
11613
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011614 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011615 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011616 rettv->v_type = VAR_STRING;
11617}
11618
11619/*
11620 * shiftwidth() function
11621 */
11622 static void
11623f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11624{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011625 rettv->vval.v_number = 0;
11626
11627 if (argvars[0].v_type != VAR_UNKNOWN)
11628 {
11629 long col;
11630
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011631 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011632 if (col < 0)
11633 return; // type error; errmsg already given
11634#ifdef FEAT_VARTABS
11635 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11636 return;
11637#endif
11638 }
11639
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011640 rettv->vval.v_number = get_sw_value(curbuf);
11641}
11642
11643/*
11644 * "simplify()" function
11645 */
11646 static void
11647f_simplify(typval_T *argvars, typval_T *rettv)
11648{
11649 char_u *p;
11650
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011651 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011652 rettv->vval.v_string = vim_strsave(p);
11653 simplify_filename(rettv->vval.v_string); /* simplify in place */
11654 rettv->v_type = VAR_STRING;
11655}
11656
11657#ifdef FEAT_FLOAT
11658/*
11659 * "sin()" function
11660 */
11661 static void
11662f_sin(typval_T *argvars, typval_T *rettv)
11663{
11664 float_T f = 0.0;
11665
11666 rettv->v_type = VAR_FLOAT;
11667 if (get_float_arg(argvars, &f) == OK)
11668 rettv->vval.v_float = sin(f);
11669 else
11670 rettv->vval.v_float = 0.0;
11671}
11672
11673/*
11674 * "sinh()" function
11675 */
11676 static void
11677f_sinh(typval_T *argvars, typval_T *rettv)
11678{
11679 float_T f = 0.0;
11680
11681 rettv->v_type = VAR_FLOAT;
11682 if (get_float_arg(argvars, &f) == OK)
11683 rettv->vval.v_float = sinh(f);
11684 else
11685 rettv->vval.v_float = 0.0;
11686}
11687#endif
11688
Bram Moolenaareae1b912019-05-09 15:12:55 +020011689static int item_compare(const void *s1, const void *s2);
11690static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011691
11692/* struct used in the array that's given to qsort() */
11693typedef struct
11694{
11695 listitem_T *item;
11696 int idx;
11697} sortItem_T;
11698
11699/* struct storing information about current sort */
11700typedef struct
11701{
11702 int item_compare_ic;
11703 int item_compare_numeric;
11704 int item_compare_numbers;
11705#ifdef FEAT_FLOAT
11706 int item_compare_float;
11707#endif
11708 char_u *item_compare_func;
11709 partial_T *item_compare_partial;
11710 dict_T *item_compare_selfdict;
11711 int item_compare_func_err;
11712 int item_compare_keep_zero;
11713} sortinfo_T;
11714static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011715#define ITEM_COMPARE_FAIL 999
11716
11717/*
11718 * Compare functions for f_sort() and f_uniq() below.
11719 */
11720 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011721item_compare(const void *s1, const void *s2)
11722{
11723 sortItem_T *si1, *si2;
11724 typval_T *tv1, *tv2;
11725 char_u *p1, *p2;
11726 char_u *tofree1 = NULL, *tofree2 = NULL;
11727 int res;
11728 char_u numbuf1[NUMBUFLEN];
11729 char_u numbuf2[NUMBUFLEN];
11730
11731 si1 = (sortItem_T *)s1;
11732 si2 = (sortItem_T *)s2;
11733 tv1 = &si1->item->li_tv;
11734 tv2 = &si2->item->li_tv;
11735
11736 if (sortinfo->item_compare_numbers)
11737 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011738 varnumber_T v1 = tv_get_number(tv1);
11739 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011740
11741 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11742 }
11743
11744#ifdef FEAT_FLOAT
11745 if (sortinfo->item_compare_float)
11746 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011747 float_T v1 = tv_get_float(tv1);
11748 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011749
11750 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11751 }
11752#endif
11753
11754 /* tv2string() puts quotes around a string and allocates memory. Don't do
11755 * that for string variables. Use a single quote when comparing with a
11756 * non-string to do what the docs promise. */
11757 if (tv1->v_type == VAR_STRING)
11758 {
11759 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11760 p1 = (char_u *)"'";
11761 else
11762 p1 = tv1->vval.v_string;
11763 }
11764 else
11765 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11766 if (tv2->v_type == VAR_STRING)
11767 {
11768 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11769 p2 = (char_u *)"'";
11770 else
11771 p2 = tv2->vval.v_string;
11772 }
11773 else
11774 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11775 if (p1 == NULL)
11776 p1 = (char_u *)"";
11777 if (p2 == NULL)
11778 p2 = (char_u *)"";
11779 if (!sortinfo->item_compare_numeric)
11780 {
11781 if (sortinfo->item_compare_ic)
11782 res = STRICMP(p1, p2);
11783 else
11784 res = STRCMP(p1, p2);
11785 }
11786 else
11787 {
11788 double n1, n2;
11789 n1 = strtod((char *)p1, (char **)&p1);
11790 n2 = strtod((char *)p2, (char **)&p2);
11791 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11792 }
11793
11794 /* When the result would be zero, compare the item indexes. Makes the
11795 * sort stable. */
11796 if (res == 0 && !sortinfo->item_compare_keep_zero)
11797 res = si1->idx > si2->idx ? 1 : -1;
11798
11799 vim_free(tofree1);
11800 vim_free(tofree2);
11801 return res;
11802}
11803
11804 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011805item_compare2(const void *s1, const void *s2)
11806{
11807 sortItem_T *si1, *si2;
11808 int res;
11809 typval_T rettv;
11810 typval_T argv[3];
11811 int dummy;
11812 char_u *func_name;
11813 partial_T *partial = sortinfo->item_compare_partial;
11814
11815 /* shortcut after failure in previous call; compare all items equal */
11816 if (sortinfo->item_compare_func_err)
11817 return 0;
11818
11819 si1 = (sortItem_T *)s1;
11820 si2 = (sortItem_T *)s2;
11821
11822 if (partial == NULL)
11823 func_name = sortinfo->item_compare_func;
11824 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011825 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011826
11827 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11828 * in the copy without changing the original list items. */
11829 copy_tv(&si1->item->li_tv, &argv[0]);
11830 copy_tv(&si2->item->li_tv, &argv[1]);
11831
11832 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020011833 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011834 partial, sortinfo->item_compare_selfdict);
11835 clear_tv(&argv[0]);
11836 clear_tv(&argv[1]);
11837
11838 if (res == FAIL)
11839 res = ITEM_COMPARE_FAIL;
11840 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011841 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011842 if (sortinfo->item_compare_func_err)
11843 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11844 clear_tv(&rettv);
11845
11846 /* When the result would be zero, compare the pointers themselves. Makes
11847 * the sort stable. */
11848 if (res == 0 && !sortinfo->item_compare_keep_zero)
11849 res = si1->idx > si2->idx ? 1 : -1;
11850
11851 return res;
11852}
11853
11854/*
11855 * "sort({list})" function
11856 */
11857 static void
11858do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11859{
11860 list_T *l;
11861 listitem_T *li;
11862 sortItem_T *ptrs;
11863 sortinfo_T *old_sortinfo;
11864 sortinfo_T info;
11865 long len;
11866 long i;
11867
11868 /* Pointer to current info struct used in compare function. Save and
11869 * restore the current one for nested calls. */
11870 old_sortinfo = sortinfo;
11871 sortinfo = &info;
11872
11873 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011874 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011875 else
11876 {
11877 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011878 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011879 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11880 TRUE))
11881 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011882 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011883
11884 len = list_len(l);
11885 if (len <= 1)
11886 goto theend; /* short list sorts pretty quickly */
11887
11888 info.item_compare_ic = FALSE;
11889 info.item_compare_numeric = FALSE;
11890 info.item_compare_numbers = FALSE;
11891#ifdef FEAT_FLOAT
11892 info.item_compare_float = FALSE;
11893#endif
11894 info.item_compare_func = NULL;
11895 info.item_compare_partial = NULL;
11896 info.item_compare_selfdict = NULL;
11897 if (argvars[1].v_type != VAR_UNKNOWN)
11898 {
11899 /* optional second argument: {func} */
11900 if (argvars[1].v_type == VAR_FUNC)
11901 info.item_compare_func = argvars[1].vval.v_string;
11902 else if (argvars[1].v_type == VAR_PARTIAL)
11903 info.item_compare_partial = argvars[1].vval.v_partial;
11904 else
11905 {
11906 int error = FALSE;
11907
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011908 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011909 if (error)
11910 goto theend; /* type error; errmsg already given */
11911 if (i == 1)
11912 info.item_compare_ic = TRUE;
11913 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011914 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011915 else if (i != 0)
11916 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011917 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011918 goto theend;
11919 }
11920 if (info.item_compare_func != NULL)
11921 {
11922 if (*info.item_compare_func == NUL)
11923 {
11924 /* empty string means default sort */
11925 info.item_compare_func = NULL;
11926 }
11927 else if (STRCMP(info.item_compare_func, "n") == 0)
11928 {
11929 info.item_compare_func = NULL;
11930 info.item_compare_numeric = TRUE;
11931 }
11932 else if (STRCMP(info.item_compare_func, "N") == 0)
11933 {
11934 info.item_compare_func = NULL;
11935 info.item_compare_numbers = TRUE;
11936 }
11937#ifdef FEAT_FLOAT
11938 else if (STRCMP(info.item_compare_func, "f") == 0)
11939 {
11940 info.item_compare_func = NULL;
11941 info.item_compare_float = TRUE;
11942 }
11943#endif
11944 else if (STRCMP(info.item_compare_func, "i") == 0)
11945 {
11946 info.item_compare_func = NULL;
11947 info.item_compare_ic = TRUE;
11948 }
11949 }
11950 }
11951
11952 if (argvars[2].v_type != VAR_UNKNOWN)
11953 {
11954 /* optional third argument: {dict} */
11955 if (argvars[2].v_type != VAR_DICT)
11956 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011957 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011958 goto theend;
11959 }
11960 info.item_compare_selfdict = argvars[2].vval.v_dict;
11961 }
11962 }
11963
11964 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011965 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011966 if (ptrs == NULL)
11967 goto theend;
11968
11969 i = 0;
11970 if (sort)
11971 {
11972 /* sort(): ptrs will be the list to sort */
11973 for (li = l->lv_first; li != NULL; li = li->li_next)
11974 {
11975 ptrs[i].item = li;
11976 ptrs[i].idx = i;
11977 ++i;
11978 }
11979
11980 info.item_compare_func_err = FALSE;
11981 info.item_compare_keep_zero = FALSE;
11982 /* test the compare function */
11983 if ((info.item_compare_func != NULL
11984 || info.item_compare_partial != NULL)
11985 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11986 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011987 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011988 else
11989 {
11990 /* Sort the array with item pointers. */
11991 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11992 info.item_compare_func == NULL
11993 && info.item_compare_partial == NULL
11994 ? item_compare : item_compare2);
11995
11996 if (!info.item_compare_func_err)
11997 {
11998 /* Clear the List and append the items in sorted order. */
11999 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12000 l->lv_len = 0;
12001 for (i = 0; i < len; ++i)
12002 list_append(l, ptrs[i].item);
12003 }
12004 }
12005 }
12006 else
12007 {
12008 int (*item_compare_func_ptr)(const void *, const void *);
12009
12010 /* f_uniq(): ptrs will be a stack of items to remove */
12011 info.item_compare_func_err = FALSE;
12012 info.item_compare_keep_zero = TRUE;
12013 item_compare_func_ptr = info.item_compare_func != NULL
12014 || info.item_compare_partial != NULL
12015 ? item_compare2 : item_compare;
12016
12017 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12018 li = li->li_next)
12019 {
12020 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12021 == 0)
12022 ptrs[i++].item = li;
12023 if (info.item_compare_func_err)
12024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012025 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012026 break;
12027 }
12028 }
12029
12030 if (!info.item_compare_func_err)
12031 {
12032 while (--i >= 0)
12033 {
12034 li = ptrs[i].item->li_next;
12035 ptrs[i].item->li_next = li->li_next;
12036 if (li->li_next != NULL)
12037 li->li_next->li_prev = ptrs[i].item;
12038 else
12039 l->lv_last = ptrs[i].item;
12040 list_fix_watch(l, li);
12041 listitem_free(li);
12042 l->lv_len--;
12043 }
12044 }
12045 }
12046
12047 vim_free(ptrs);
12048 }
12049theend:
12050 sortinfo = old_sortinfo;
12051}
12052
12053/*
12054 * "sort({list})" function
12055 */
12056 static void
12057f_sort(typval_T *argvars, typval_T *rettv)
12058{
12059 do_sort_uniq(argvars, rettv, TRUE);
12060}
12061
12062/*
12063 * "uniq({list})" function
12064 */
12065 static void
12066f_uniq(typval_T *argvars, typval_T *rettv)
12067{
12068 do_sort_uniq(argvars, rettv, FALSE);
12069}
12070
12071/*
12072 * "soundfold({word})" function
12073 */
12074 static void
12075f_soundfold(typval_T *argvars, typval_T *rettv)
12076{
12077 char_u *s;
12078
12079 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012080 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012081#ifdef FEAT_SPELL
12082 rettv->vval.v_string = eval_soundfold(s);
12083#else
12084 rettv->vval.v_string = vim_strsave(s);
12085#endif
12086}
12087
12088/*
12089 * "spellbadword()" function
12090 */
12091 static void
12092f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12093{
12094 char_u *word = (char_u *)"";
12095 hlf_T attr = HLF_COUNT;
12096 int len = 0;
12097
12098 if (rettv_list_alloc(rettv) == FAIL)
12099 return;
12100
12101#ifdef FEAT_SPELL
12102 if (argvars[0].v_type == VAR_UNKNOWN)
12103 {
12104 /* Find the start and length of the badly spelled word. */
12105 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12106 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012107 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012108 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012109 curwin->w_set_curswant = TRUE;
12110 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012111 }
12112 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12113 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012114 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012115 int capcol = -1;
12116
12117 if (str != NULL)
12118 {
12119 /* Check the argument for spelling. */
12120 while (*str != NUL)
12121 {
12122 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12123 if (attr != HLF_COUNT)
12124 {
12125 word = str;
12126 break;
12127 }
12128 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012129 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012130 }
12131 }
12132 }
12133#endif
12134
12135 list_append_string(rettv->vval.v_list, word, len);
12136 list_append_string(rettv->vval.v_list, (char_u *)(
12137 attr == HLF_SPB ? "bad" :
12138 attr == HLF_SPR ? "rare" :
12139 attr == HLF_SPL ? "local" :
12140 attr == HLF_SPC ? "caps" :
12141 ""), -1);
12142}
12143
12144/*
12145 * "spellsuggest()" function
12146 */
12147 static void
12148f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12149{
12150#ifdef FEAT_SPELL
12151 char_u *str;
12152 int typeerr = FALSE;
12153 int maxcount;
12154 garray_T ga;
12155 int i;
12156 listitem_T *li;
12157 int need_capital = FALSE;
12158#endif
12159
12160 if (rettv_list_alloc(rettv) == FAIL)
12161 return;
12162
12163#ifdef FEAT_SPELL
12164 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12165 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012166 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012167 if (argvars[1].v_type != VAR_UNKNOWN)
12168 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012169 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012170 if (maxcount <= 0)
12171 return;
12172 if (argvars[2].v_type != VAR_UNKNOWN)
12173 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012174 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012175 if (typeerr)
12176 return;
12177 }
12178 }
12179 else
12180 maxcount = 25;
12181
12182 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12183
12184 for (i = 0; i < ga.ga_len; ++i)
12185 {
12186 str = ((char_u **)ga.ga_data)[i];
12187
12188 li = listitem_alloc();
12189 if (li == NULL)
12190 vim_free(str);
12191 else
12192 {
12193 li->li_tv.v_type = VAR_STRING;
12194 li->li_tv.v_lock = 0;
12195 li->li_tv.vval.v_string = str;
12196 list_append(rettv->vval.v_list, li);
12197 }
12198 }
12199 ga_clear(&ga);
12200 }
12201#endif
12202}
12203
12204 static void
12205f_split(typval_T *argvars, typval_T *rettv)
12206{
12207 char_u *str;
12208 char_u *end;
12209 char_u *pat = NULL;
12210 regmatch_T regmatch;
12211 char_u patbuf[NUMBUFLEN];
12212 char_u *save_cpo;
12213 int match;
12214 colnr_T col = 0;
12215 int keepempty = FALSE;
12216 int typeerr = FALSE;
12217
12218 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12219 save_cpo = p_cpo;
12220 p_cpo = (char_u *)"";
12221
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012222 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012223 if (argvars[1].v_type != VAR_UNKNOWN)
12224 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012225 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012226 if (pat == NULL)
12227 typeerr = TRUE;
12228 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012229 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012230 }
12231 if (pat == NULL || *pat == NUL)
12232 pat = (char_u *)"[\\x01- ]\\+";
12233
12234 if (rettv_list_alloc(rettv) == FAIL)
12235 return;
12236 if (typeerr)
12237 return;
12238
12239 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12240 if (regmatch.regprog != NULL)
12241 {
12242 regmatch.rm_ic = FALSE;
12243 while (*str != NUL || keepempty)
12244 {
12245 if (*str == NUL)
12246 match = FALSE; /* empty item at the end */
12247 else
12248 match = vim_regexec_nl(&regmatch, str, col);
12249 if (match)
12250 end = regmatch.startp[0];
12251 else
12252 end = str + STRLEN(str);
12253 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12254 && *str != NUL && match && end < regmatch.endp[0]))
12255 {
12256 if (list_append_string(rettv->vval.v_list, str,
12257 (int)(end - str)) == FAIL)
12258 break;
12259 }
12260 if (!match)
12261 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012262 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012263 if (regmatch.endp[0] > str)
12264 col = 0;
12265 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012266 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012267 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012268 str = regmatch.endp[0];
12269 }
12270
12271 vim_regfree(regmatch.regprog);
12272 }
12273
12274 p_cpo = save_cpo;
12275}
12276
12277#ifdef FEAT_FLOAT
12278/*
12279 * "sqrt()" function
12280 */
12281 static void
12282f_sqrt(typval_T *argvars, typval_T *rettv)
12283{
12284 float_T f = 0.0;
12285
12286 rettv->v_type = VAR_FLOAT;
12287 if (get_float_arg(argvars, &f) == OK)
12288 rettv->vval.v_float = sqrt(f);
12289 else
12290 rettv->vval.v_float = 0.0;
12291}
12292
12293/*
12294 * "str2float()" function
12295 */
12296 static void
12297f_str2float(typval_T *argvars, typval_T *rettv)
12298{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012299 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012300 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012301
Bram Moolenaar08243d22017-01-10 16:12:29 +010012302 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012303 p = skipwhite(p + 1);
12304 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012305 if (isneg)
12306 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012307 rettv->v_type = VAR_FLOAT;
12308}
12309#endif
12310
12311/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012312 * "str2list()" function
12313 */
12314 static void
12315f_str2list(typval_T *argvars, typval_T *rettv)
12316{
12317 char_u *p;
12318 int utf8 = FALSE;
12319
12320 if (rettv_list_alloc(rettv) == FAIL)
12321 return;
12322
12323 if (argvars[1].v_type != VAR_UNKNOWN)
12324 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12325
12326 p = tv_get_string(&argvars[0]);
12327
12328 if (has_mbyte || utf8)
12329 {
12330 int (*ptr2len)(char_u *);
12331 int (*ptr2char)(char_u *);
12332
12333 if (utf8 || enc_utf8)
12334 {
12335 ptr2len = utf_ptr2len;
12336 ptr2char = utf_ptr2char;
12337 }
12338 else
12339 {
12340 ptr2len = mb_ptr2len;
12341 ptr2char = mb_ptr2char;
12342 }
12343
12344 for ( ; *p != NUL; p += (*ptr2len)(p))
12345 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
12346 }
12347 else
12348 for ( ; *p != NUL; ++p)
12349 list_append_number(rettv->vval.v_list, *p);
12350}
12351
12352/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012353 * "str2nr()" function
12354 */
12355 static void
12356f_str2nr(typval_T *argvars, typval_T *rettv)
12357{
12358 int base = 10;
12359 char_u *p;
12360 varnumber_T n;
12361 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012362 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012363
12364 if (argvars[1].v_type != VAR_UNKNOWN)
12365 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012366 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012367 if (base != 2 && base != 8 && base != 10 && base != 16)
12368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012369 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012370 return;
12371 }
12372 }
12373
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012374 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012375 isneg = (*p == '-');
12376 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012377 p = skipwhite(p + 1);
12378 switch (base)
12379 {
12380 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12381 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12382 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12383 default: what = 0;
12384 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020012385 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
12386 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010012387 if (isneg)
12388 rettv->vval.v_number = -n;
12389 else
12390 rettv->vval.v_number = n;
12391
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012392}
12393
12394#ifdef HAVE_STRFTIME
12395/*
12396 * "strftime({format}[, {time}])" function
12397 */
12398 static void
12399f_strftime(typval_T *argvars, typval_T *rettv)
12400{
12401 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020012402 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012403 struct tm *curtime;
12404 time_t seconds;
12405 char_u *p;
12406
12407 rettv->v_type = VAR_STRING;
12408
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012409 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012410 if (argvars[1].v_type == VAR_UNKNOWN)
12411 seconds = time(NULL);
12412 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012413 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020012414 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012415 /* MSVC returns NULL for an invalid value of seconds. */
12416 if (curtime == NULL)
12417 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12418 else
12419 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012420 vimconv_T conv;
12421 char_u *enc;
12422
12423 conv.vc_type = CONV_NONE;
12424 enc = enc_locale();
12425 convert_setup(&conv, p_enc, enc);
12426 if (conv.vc_type != CONV_NONE)
12427 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012428 if (p != NULL)
12429 (void)strftime((char *)result_buf, sizeof(result_buf),
12430 (char *)p, curtime);
12431 else
12432 result_buf[0] = NUL;
12433
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012434 if (conv.vc_type != CONV_NONE)
12435 vim_free(p);
12436 convert_setup(&conv, enc, p_enc);
12437 if (conv.vc_type != CONV_NONE)
12438 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12439 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012440 rettv->vval.v_string = vim_strsave(result_buf);
12441
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012442 /* Release conversion descriptors */
12443 convert_setup(&conv, NULL, NULL);
12444 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012445 }
12446}
12447#endif
12448
12449/*
12450 * "strgetchar()" function
12451 */
12452 static void
12453f_strgetchar(typval_T *argvars, typval_T *rettv)
12454{
12455 char_u *str;
12456 int len;
12457 int error = FALSE;
12458 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012459 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012460
12461 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012462 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012463 if (str == NULL)
12464 return;
12465 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012466 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012467 if (error)
12468 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012469
Bram Moolenaar13505972019-01-24 15:04:48 +010012470 while (charidx >= 0 && byteidx < len)
12471 {
12472 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012473 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012474 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12475 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012476 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012477 --charidx;
12478 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012479 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012480}
12481
12482/*
12483 * "stridx()" function
12484 */
12485 static void
12486f_stridx(typval_T *argvars, typval_T *rettv)
12487{
12488 char_u buf[NUMBUFLEN];
12489 char_u *needle;
12490 char_u *haystack;
12491 char_u *save_haystack;
12492 char_u *pos;
12493 int start_idx;
12494
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012495 needle = tv_get_string_chk(&argvars[1]);
12496 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012497 rettv->vval.v_number = -1;
12498 if (needle == NULL || haystack == NULL)
12499 return; /* type error; errmsg already given */
12500
12501 if (argvars[2].v_type != VAR_UNKNOWN)
12502 {
12503 int error = FALSE;
12504
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012505 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012506 if (error || start_idx >= (int)STRLEN(haystack))
12507 return;
12508 if (start_idx >= 0)
12509 haystack += start_idx;
12510 }
12511
12512 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12513 if (pos != NULL)
12514 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12515}
12516
12517/*
12518 * "string()" function
12519 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012520 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012521f_string(typval_T *argvars, typval_T *rettv)
12522{
12523 char_u *tofree;
12524 char_u numbuf[NUMBUFLEN];
12525
12526 rettv->v_type = VAR_STRING;
12527 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12528 get_copyID());
12529 /* Make a copy if we have a value but it's not in allocated memory. */
12530 if (rettv->vval.v_string != NULL && tofree == NULL)
12531 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12532}
12533
12534/*
12535 * "strlen()" function
12536 */
12537 static void
12538f_strlen(typval_T *argvars, typval_T *rettv)
12539{
12540 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012541 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012542}
12543
12544/*
12545 * "strchars()" function
12546 */
12547 static void
12548f_strchars(typval_T *argvars, typval_T *rettv)
12549{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012550 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012551 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012552 varnumber_T len = 0;
12553 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012554
12555 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012556 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012557 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012558 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012559 else
12560 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012561 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12562 while (*s != NUL)
12563 {
12564 func_mb_ptr2char_adv(&s);
12565 ++len;
12566 }
12567 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012568 }
12569}
12570
12571/*
12572 * "strdisplaywidth()" function
12573 */
12574 static void
12575f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12576{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012577 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012578 int col = 0;
12579
12580 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012581 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012582
12583 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12584}
12585
12586/*
12587 * "strwidth()" function
12588 */
12589 static void
12590f_strwidth(typval_T *argvars, typval_T *rettv)
12591{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012592 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012593
Bram Moolenaar13505972019-01-24 15:04:48 +010012594 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012595}
12596
12597/*
12598 * "strcharpart()" function
12599 */
12600 static void
12601f_strcharpart(typval_T *argvars, typval_T *rettv)
12602{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012603 char_u *p;
12604 int nchar;
12605 int nbyte = 0;
12606 int charlen;
12607 int len = 0;
12608 int slen;
12609 int error = FALSE;
12610
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012611 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012612 slen = (int)STRLEN(p);
12613
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012614 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012615 if (!error)
12616 {
12617 if (nchar > 0)
12618 while (nchar > 0 && nbyte < slen)
12619 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012620 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012621 --nchar;
12622 }
12623 else
12624 nbyte = nchar;
12625 if (argvars[2].v_type != VAR_UNKNOWN)
12626 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012627 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012628 while (charlen > 0 && nbyte + len < slen)
12629 {
12630 int off = nbyte + len;
12631
12632 if (off < 0)
12633 len += 1;
12634 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012635 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012636 --charlen;
12637 }
12638 }
12639 else
12640 len = slen - nbyte; /* default: all bytes that are available. */
12641 }
12642
12643 /*
12644 * Only return the overlap between the specified part and the actual
12645 * string.
12646 */
12647 if (nbyte < 0)
12648 {
12649 len += nbyte;
12650 nbyte = 0;
12651 }
12652 else if (nbyte > slen)
12653 nbyte = slen;
12654 if (len < 0)
12655 len = 0;
12656 else if (nbyte + len > slen)
12657 len = slen - nbyte;
12658
12659 rettv->v_type = VAR_STRING;
12660 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012661}
12662
12663/*
12664 * "strpart()" function
12665 */
12666 static void
12667f_strpart(typval_T *argvars, typval_T *rettv)
12668{
12669 char_u *p;
12670 int n;
12671 int len;
12672 int slen;
12673 int error = FALSE;
12674
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012675 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012676 slen = (int)STRLEN(p);
12677
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012678 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012679 if (error)
12680 len = 0;
12681 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012682 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012683 else
12684 len = slen - n; /* default len: all bytes that are available. */
12685
12686 /*
12687 * Only return the overlap between the specified part and the actual
12688 * string.
12689 */
12690 if (n < 0)
12691 {
12692 len += n;
12693 n = 0;
12694 }
12695 else if (n > slen)
12696 n = slen;
12697 if (len < 0)
12698 len = 0;
12699 else if (n + len > slen)
12700 len = slen - n;
12701
12702 rettv->v_type = VAR_STRING;
12703 rettv->vval.v_string = vim_strnsave(p + n, len);
12704}
12705
12706/*
12707 * "strridx()" function
12708 */
12709 static void
12710f_strridx(typval_T *argvars, typval_T *rettv)
12711{
12712 char_u buf[NUMBUFLEN];
12713 char_u *needle;
12714 char_u *haystack;
12715 char_u *rest;
12716 char_u *lastmatch = NULL;
12717 int haystack_len, end_idx;
12718
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012719 needle = tv_get_string_chk(&argvars[1]);
12720 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012721
12722 rettv->vval.v_number = -1;
12723 if (needle == NULL || haystack == NULL)
12724 return; /* type error; errmsg already given */
12725
12726 haystack_len = (int)STRLEN(haystack);
12727 if (argvars[2].v_type != VAR_UNKNOWN)
12728 {
12729 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012730 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012731 if (end_idx < 0)
12732 return; /* can never find a match */
12733 }
12734 else
12735 end_idx = haystack_len;
12736
12737 if (*needle == NUL)
12738 {
12739 /* Empty string matches past the end. */
12740 lastmatch = haystack + end_idx;
12741 }
12742 else
12743 {
12744 for (rest = haystack; *rest != '\0'; ++rest)
12745 {
12746 rest = (char_u *)strstr((char *)rest, (char *)needle);
12747 if (rest == NULL || rest > haystack + end_idx)
12748 break;
12749 lastmatch = rest;
12750 }
12751 }
12752
12753 if (lastmatch == NULL)
12754 rettv->vval.v_number = -1;
12755 else
12756 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12757}
12758
12759/*
12760 * "strtrans()" function
12761 */
12762 static void
12763f_strtrans(typval_T *argvars, typval_T *rettv)
12764{
12765 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012766 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012767}
12768
12769/*
12770 * "submatch()" function
12771 */
12772 static void
12773f_submatch(typval_T *argvars, typval_T *rettv)
12774{
12775 int error = FALSE;
12776 int no;
12777 int retList = 0;
12778
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012779 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012780 if (error)
12781 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012782 if (no < 0 || no >= NSUBEXP)
12783 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012784 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012785 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012786 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012787 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012788 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012789 if (error)
12790 return;
12791
12792 if (retList == 0)
12793 {
12794 rettv->v_type = VAR_STRING;
12795 rettv->vval.v_string = reg_submatch(no);
12796 }
12797 else
12798 {
12799 rettv->v_type = VAR_LIST;
12800 rettv->vval.v_list = reg_submatch_list(no);
12801 }
12802}
12803
12804/*
12805 * "substitute()" function
12806 */
12807 static void
12808f_substitute(typval_T *argvars, typval_T *rettv)
12809{
12810 char_u patbuf[NUMBUFLEN];
12811 char_u subbuf[NUMBUFLEN];
12812 char_u flagsbuf[NUMBUFLEN];
12813
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012814 char_u *str = tv_get_string_chk(&argvars[0]);
12815 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012816 char_u *sub = NULL;
12817 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012818 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012819
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012820 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12821 expr = &argvars[2];
12822 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012823 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012824
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012825 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012826 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12827 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012828 rettv->vval.v_string = NULL;
12829 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012830 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012831}
12832
12833/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012834 * "swapinfo(swap_filename)" function
12835 */
12836 static void
12837f_swapinfo(typval_T *argvars, typval_T *rettv)
12838{
12839 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012840 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012841}
12842
12843/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012844 * "swapname(expr)" function
12845 */
12846 static void
12847f_swapname(typval_T *argvars, typval_T *rettv)
12848{
12849 buf_T *buf;
12850
12851 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010012852 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020012853 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12854 || buf->b_ml.ml_mfp->mf_fname == NULL)
12855 rettv->vval.v_string = NULL;
12856 else
12857 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12858}
12859
12860/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012861 * "synID(lnum, col, trans)" function
12862 */
12863 static void
12864f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12865{
12866 int id = 0;
12867#ifdef FEAT_SYN_HL
12868 linenr_T lnum;
12869 colnr_T col;
12870 int trans;
12871 int transerr = FALSE;
12872
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012873 lnum = tv_get_lnum(argvars); /* -1 on type error */
12874 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12875 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012876
12877 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12878 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12879 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12880#endif
12881
12882 rettv->vval.v_number = id;
12883}
12884
12885/*
12886 * "synIDattr(id, what [, mode])" function
12887 */
12888 static void
12889f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12890{
12891 char_u *p = NULL;
12892#ifdef FEAT_SYN_HL
12893 int id;
12894 char_u *what;
12895 char_u *mode;
12896 char_u modebuf[NUMBUFLEN];
12897 int modec;
12898
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012899 id = (int)tv_get_number(&argvars[0]);
12900 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012901 if (argvars[2].v_type != VAR_UNKNOWN)
12902 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012903 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012904 modec = TOLOWER_ASC(mode[0]);
12905 if (modec != 't' && modec != 'c' && modec != 'g')
12906 modec = 0; /* replace invalid with current */
12907 }
12908 else
12909 {
12910#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12911 if (USE_24BIT)
12912 modec = 'g';
12913 else
12914#endif
12915 if (t_colors > 1)
12916 modec = 'c';
12917 else
12918 modec = 't';
12919 }
12920
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012921 switch (TOLOWER_ASC(what[0]))
12922 {
12923 case 'b':
12924 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12925 p = highlight_color(id, what, modec);
12926 else /* bold */
12927 p = highlight_has_attr(id, HL_BOLD, modec);
12928 break;
12929
12930 case 'f': /* fg[#] or font */
12931 p = highlight_color(id, what, modec);
12932 break;
12933
12934 case 'i':
12935 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12936 p = highlight_has_attr(id, HL_INVERSE, modec);
12937 else /* italic */
12938 p = highlight_has_attr(id, HL_ITALIC, modec);
12939 break;
12940
12941 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012942 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012943 break;
12944
12945 case 'r': /* reverse */
12946 p = highlight_has_attr(id, HL_INVERSE, modec);
12947 break;
12948
12949 case 's':
12950 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12951 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012952 /* strikeout */
12953 else if (TOLOWER_ASC(what[1]) == 't' &&
12954 TOLOWER_ASC(what[2]) == 'r')
12955 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012956 else /* standout */
12957 p = highlight_has_attr(id, HL_STANDOUT, modec);
12958 break;
12959
12960 case 'u':
12961 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12962 /* underline */
12963 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12964 else
12965 /* undercurl */
12966 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12967 break;
12968 }
12969
12970 if (p != NULL)
12971 p = vim_strsave(p);
12972#endif
12973 rettv->v_type = VAR_STRING;
12974 rettv->vval.v_string = p;
12975}
12976
12977/*
12978 * "synIDtrans(id)" function
12979 */
12980 static void
12981f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12982{
12983 int id;
12984
12985#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012986 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012987
12988 if (id > 0)
12989 id = syn_get_final_id(id);
12990 else
12991#endif
12992 id = 0;
12993
12994 rettv->vval.v_number = id;
12995}
12996
12997/*
12998 * "synconcealed(lnum, col)" function
12999 */
13000 static void
13001f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13002{
13003#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13004 linenr_T lnum;
13005 colnr_T col;
13006 int syntax_flags = 0;
13007 int cchar;
13008 int matchid = 0;
13009 char_u str[NUMBUFLEN];
13010#endif
13011
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013012 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013013
13014#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013015 lnum = tv_get_lnum(argvars); /* -1 on type error */
13016 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013017
13018 vim_memset(str, NUL, sizeof(str));
13019
13020 if (rettv_list_alloc(rettv) != FAIL)
13021 {
13022 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13023 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13024 && curwin->w_p_cole > 0)
13025 {
13026 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13027 syntax_flags = get_syntax_info(&matchid);
13028
13029 /* get the conceal character */
13030 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13031 {
13032 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013033 if (cchar == NUL && curwin->w_p_cole == 1)
13034 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013035 if (cchar != NUL)
13036 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013037 if (has_mbyte)
13038 (*mb_char2bytes)(cchar, str);
13039 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013040 str[0] = cchar;
13041 }
13042 }
13043 }
13044
13045 list_append_number(rettv->vval.v_list,
13046 (syntax_flags & HL_CONCEAL) != 0);
13047 /* -1 to auto-determine strlen */
13048 list_append_string(rettv->vval.v_list, str, -1);
13049 list_append_number(rettv->vval.v_list, matchid);
13050 }
13051#endif
13052}
13053
13054/*
13055 * "synstack(lnum, col)" function
13056 */
13057 static void
13058f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13059{
13060#ifdef FEAT_SYN_HL
13061 linenr_T lnum;
13062 colnr_T col;
13063 int i;
13064 int id;
13065#endif
13066
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013067 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013068
13069#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013070 lnum = tv_get_lnum(argvars); /* -1 on type error */
13071 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013072
13073 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13074 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13075 && rettv_list_alloc(rettv) != FAIL)
13076 {
13077 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13078 for (i = 0; ; ++i)
13079 {
13080 id = syn_get_stack_item(i);
13081 if (id < 0)
13082 break;
13083 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13084 break;
13085 }
13086 }
13087#endif
13088}
13089
13090 static void
13091get_cmd_output_as_rettv(
13092 typval_T *argvars,
13093 typval_T *rettv,
13094 int retlist)
13095{
13096 char_u *res = NULL;
13097 char_u *p;
13098 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013099 int err = FALSE;
13100 FILE *fd;
13101 list_T *list = NULL;
13102 int flags = SHELL_SILENT;
13103
13104 rettv->v_type = VAR_STRING;
13105 rettv->vval.v_string = NULL;
13106 if (check_restricted() || check_secure())
13107 goto errret;
13108
13109 if (argvars[1].v_type != VAR_UNKNOWN)
13110 {
13111 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013112 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013113 * command.
13114 */
13115 if ((infile = vim_tempname('i', TRUE)) == NULL)
13116 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013117 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013118 goto errret;
13119 }
13120
13121 fd = mch_fopen((char *)infile, WRITEBIN);
13122 if (fd == NULL)
13123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013124 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013125 goto errret;
13126 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013127 if (argvars[1].v_type == VAR_NUMBER)
13128 {
13129 linenr_T lnum;
13130 buf_T *buf;
13131
13132 buf = buflist_findnr(argvars[1].vval.v_number);
13133 if (buf == NULL)
13134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013135 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013136 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013137 goto errret;
13138 }
13139
13140 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13141 {
13142 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13143 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13144 {
13145 err = TRUE;
13146 break;
13147 }
13148 if (putc(NL, fd) == EOF)
13149 {
13150 err = TRUE;
13151 break;
13152 }
13153 }
13154 }
13155 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013156 {
13157 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13158 err = TRUE;
13159 }
13160 else
13161 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013162 size_t len;
13163 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013164
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013165 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013166 if (p == NULL)
13167 {
13168 fclose(fd);
13169 goto errret; /* type error; errmsg already given */
13170 }
13171 len = STRLEN(p);
13172 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13173 err = TRUE;
13174 }
13175 if (fclose(fd) != 0)
13176 err = TRUE;
13177 if (err)
13178 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013179 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180 goto errret;
13181 }
13182 }
13183
13184 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13185 * echoes typeahead, that messes up the display. */
13186 if (!msg_silent)
13187 flags += SHELL_COOKED;
13188
13189 if (retlist)
13190 {
13191 int len;
13192 listitem_T *li;
13193 char_u *s = NULL;
13194 char_u *start;
13195 char_u *end;
13196 int i;
13197
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013198 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013199 if (res == NULL)
13200 goto errret;
13201
13202 list = list_alloc();
13203 if (list == NULL)
13204 goto errret;
13205
13206 for (i = 0; i < len; ++i)
13207 {
13208 start = res + i;
13209 while (i < len && res[i] != NL)
13210 ++i;
13211 end = res + i;
13212
Bram Moolenaar964b3742019-05-24 18:54:09 +020013213 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013214 if (s == NULL)
13215 goto errret;
13216
13217 for (p = s; start < end; ++p, ++start)
13218 *p = *start == NUL ? NL : *start;
13219 *p = NUL;
13220
13221 li = listitem_alloc();
13222 if (li == NULL)
13223 {
13224 vim_free(s);
13225 goto errret;
13226 }
13227 li->li_tv.v_type = VAR_STRING;
13228 li->li_tv.v_lock = 0;
13229 li->li_tv.vval.v_string = s;
13230 list_append(list, li);
13231 }
13232
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013233 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013234 list = NULL;
13235 }
13236 else
13237 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013238 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013239#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013240 /* translate <CR><NL> into <NL> */
13241 if (res != NULL)
13242 {
13243 char_u *s, *d;
13244
13245 d = res;
13246 for (s = res; *s; ++s)
13247 {
13248 if (s[0] == CAR && s[1] == NL)
13249 ++s;
13250 *d++ = *s;
13251 }
13252 *d = NUL;
13253 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013254#endif
13255 rettv->vval.v_string = res;
13256 res = NULL;
13257 }
13258
13259errret:
13260 if (infile != NULL)
13261 {
13262 mch_remove(infile);
13263 vim_free(infile);
13264 }
13265 if (res != NULL)
13266 vim_free(res);
13267 if (list != NULL)
13268 list_free(list);
13269}
13270
13271/*
13272 * "system()" function
13273 */
13274 static void
13275f_system(typval_T *argvars, typval_T *rettv)
13276{
13277 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13278}
13279
13280/*
13281 * "systemlist()" function
13282 */
13283 static void
13284f_systemlist(typval_T *argvars, typval_T *rettv)
13285{
13286 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13287}
13288
13289/*
13290 * "tabpagebuflist()" function
13291 */
13292 static void
13293f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13294{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013295 tabpage_T *tp;
13296 win_T *wp = NULL;
13297
13298 if (argvars[0].v_type == VAR_UNKNOWN)
13299 wp = firstwin;
13300 else
13301 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013302 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013303 if (tp != NULL)
13304 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13305 }
13306 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13307 {
13308 for (; wp != NULL; wp = wp->w_next)
13309 if (list_append_number(rettv->vval.v_list,
13310 wp->w_buffer->b_fnum) == FAIL)
13311 break;
13312 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013313}
13314
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013315/*
13316 * "tabpagenr()" function
13317 */
13318 static void
13319f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13320{
13321 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013322 char_u *arg;
13323
13324 if (argvars[0].v_type != VAR_UNKNOWN)
13325 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013326 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013327 nr = 0;
13328 if (arg != NULL)
13329 {
13330 if (STRCMP(arg, "$") == 0)
13331 nr = tabpage_index(NULL) - 1;
13332 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013333 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013334 }
13335 }
13336 else
13337 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013338 rettv->vval.v_number = nr;
13339}
13340
13341
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013342/*
13343 * Common code for tabpagewinnr() and winnr().
13344 */
13345 static int
13346get_winnr(tabpage_T *tp, typval_T *argvar)
13347{
13348 win_T *twin;
13349 int nr = 1;
13350 win_T *wp;
13351 char_u *arg;
13352
13353 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13354 if (argvar->v_type != VAR_UNKNOWN)
13355 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013356 int invalid_arg = FALSE;
13357
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013358 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013359 if (arg == NULL)
13360 nr = 0; /* type error; errmsg already given */
13361 else if (STRCMP(arg, "$") == 0)
13362 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13363 else if (STRCMP(arg, "#") == 0)
13364 {
13365 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13366 if (twin == NULL)
13367 nr = 0;
13368 }
13369 else
13370 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013371 long count;
13372 char_u *endp;
13373
13374 // Extract the window count (if specified). e.g. winnr('3j')
13375 count = strtol((char *)arg, (char **)&endp, 10);
13376 if (count <= 0)
13377 count = 1; // if count is not specified, default to 1
13378 if (endp != NULL && *endp != '\0')
13379 {
13380 if (STRCMP(endp, "j") == 0)
13381 twin = win_vert_neighbor(tp, twin, FALSE, count);
13382 else if (STRCMP(endp, "k") == 0)
13383 twin = win_vert_neighbor(tp, twin, TRUE, count);
13384 else if (STRCMP(endp, "h") == 0)
13385 twin = win_horz_neighbor(tp, twin, TRUE, count);
13386 else if (STRCMP(endp, "l") == 0)
13387 twin = win_horz_neighbor(tp, twin, FALSE, count);
13388 else
13389 invalid_arg = TRUE;
13390 }
13391 else
13392 invalid_arg = TRUE;
13393 }
13394
13395 if (invalid_arg)
13396 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013397 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013398 nr = 0;
13399 }
13400 }
13401
13402 if (nr > 0)
13403 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13404 wp != twin; wp = wp->w_next)
13405 {
13406 if (wp == NULL)
13407 {
13408 /* didn't find it in this tabpage */
13409 nr = 0;
13410 break;
13411 }
13412 ++nr;
13413 }
13414 return nr;
13415}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013416
13417/*
13418 * "tabpagewinnr()" function
13419 */
13420 static void
13421f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13422{
13423 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013424 tabpage_T *tp;
13425
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013426 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013427 if (tp == NULL)
13428 nr = 0;
13429 else
13430 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013431 rettv->vval.v_number = nr;
13432}
13433
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013434/*
13435 * "tagfiles()" function
13436 */
13437 static void
13438f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13439{
13440 char_u *fname;
13441 tagname_T tn;
13442 int first;
13443
13444 if (rettv_list_alloc(rettv) == FAIL)
13445 return;
13446 fname = alloc(MAXPATHL);
13447 if (fname == NULL)
13448 return;
13449
13450 for (first = TRUE; ; first = FALSE)
13451 if (get_tagfname(&tn, first, fname) == FAIL
13452 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13453 break;
13454 tagname_free(&tn);
13455 vim_free(fname);
13456}
13457
13458/*
13459 * "taglist()" function
13460 */
13461 static void
13462f_taglist(typval_T *argvars, typval_T *rettv)
13463{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013464 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013465 char_u *tag_pattern;
13466
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013467 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013468
13469 rettv->vval.v_number = FALSE;
13470 if (*tag_pattern == NUL)
13471 return;
13472
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013473 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013474 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013475 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013476 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013477}
13478
13479/*
13480 * "tempname()" function
13481 */
13482 static void
13483f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13484{
13485 static int x = 'A';
13486
13487 rettv->v_type = VAR_STRING;
13488 rettv->vval.v_string = vim_tempname(x, FALSE);
13489
13490 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13491 * names. Skip 'I' and 'O', they are used for shell redirection. */
13492 do
13493 {
13494 if (x == 'Z')
13495 x = '0';
13496 else if (x == '9')
13497 x = 'A';
13498 else
13499 {
13500#ifdef EBCDIC
13501 if (x == 'I')
13502 x = 'J';
13503 else if (x == 'R')
13504 x = 'S';
13505 else
13506#endif
13507 ++x;
13508 }
13509 } while (x == 'I' || x == 'O');
13510}
13511
13512#ifdef FEAT_FLOAT
13513/*
13514 * "tan()" function
13515 */
13516 static void
13517f_tan(typval_T *argvars, typval_T *rettv)
13518{
13519 float_T f = 0.0;
13520
13521 rettv->v_type = VAR_FLOAT;
13522 if (get_float_arg(argvars, &f) == OK)
13523 rettv->vval.v_float = tan(f);
13524 else
13525 rettv->vval.v_float = 0.0;
13526}
13527
13528/*
13529 * "tanh()" function
13530 */
13531 static void
13532f_tanh(typval_T *argvars, typval_T *rettv)
13533{
13534 float_T f = 0.0;
13535
13536 rettv->v_type = VAR_FLOAT;
13537 if (get_float_arg(argvars, &f) == OK)
13538 rettv->vval.v_float = tanh(f);
13539 else
13540 rettv->vval.v_float = 0.0;
13541}
13542#endif
13543
13544/*
13545 * "test_alloc_fail(id, countdown, repeat)" function
13546 */
13547 static void
13548f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13549{
13550 if (argvars[0].v_type != VAR_NUMBER
13551 || argvars[0].vval.v_number <= 0
13552 || argvars[1].v_type != VAR_NUMBER
13553 || argvars[1].vval.v_number < 0
13554 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013555 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013556 else
13557 {
13558 alloc_fail_id = argvars[0].vval.v_number;
13559 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013560 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013561 alloc_fail_countdown = argvars[1].vval.v_number;
13562 alloc_fail_repeat = argvars[2].vval.v_number;
13563 did_outofmem_msg = FALSE;
13564 }
13565}
13566
13567/*
13568 * "test_autochdir()"
13569 */
13570 static void
13571f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13572{
13573#if defined(FEAT_AUTOCHDIR)
13574 test_autochdir = TRUE;
13575#endif
13576}
13577
13578/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013579 * "test_feedinput()"
13580 */
13581 static void
13582f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13583{
13584#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013585 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013586
13587 if (val != NULL)
13588 {
13589 trash_input_buf();
13590 add_to_input_buf_csi(val, (int)STRLEN(val));
13591 }
13592#endif
13593}
13594
13595/*
Bram Moolenaareda65222019-05-16 20:29:44 +020013596 * "test_getvalue({name})" function
13597 */
13598 static void
13599f_test_getvalue(typval_T *argvars, typval_T *rettv)
13600{
13601 if (argvars[0].v_type != VAR_STRING)
13602 emsg(_(e_invarg));
13603 else
13604 {
13605 char_u *name = tv_get_string(&argvars[0]);
13606
13607 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
13608 rettv->vval.v_number = need_fileinfo;
13609 else
13610 semsg(_(e_invarg2), name);
13611 }
13612}
13613
13614/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013615 * "test_option_not_set({name})" function
13616 */
13617 static void
13618f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13619{
13620 char_u *name = (char_u *)"";
13621
13622 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013623 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013624 else
13625 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013626 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013627 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013628 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013629 }
13630}
13631
13632/*
13633 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013634 */
13635 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013636f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013637{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013638 char_u *name = (char_u *)"";
13639 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013640 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013641
13642 if (argvars[0].v_type != VAR_STRING
13643 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013644 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013645 else
13646 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013647 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013648 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013649
13650 if (STRCMP(name, (char_u *)"redraw") == 0)
13651 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013652 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13653 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013654 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13655 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013656 else if (STRCMP(name, (char_u *)"starting") == 0)
13657 {
13658 if (val)
13659 {
13660 if (save_starting < 0)
13661 save_starting = starting;
13662 starting = 0;
13663 }
13664 else
13665 {
13666 starting = save_starting;
13667 save_starting = -1;
13668 }
13669 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013670 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13671 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013672 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
13673 no_query_mouse_for_testing = val;
Bram Moolenaaradc67142019-06-22 01:40:42 +020013674 else if (STRCMP(name, (char_u *)"no_wait_return") == 0)
13675 no_wait_return = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013676 else if (STRCMP(name, (char_u *)"ALL") == 0)
13677 {
13678 disable_char_avail_for_testing = FALSE;
13679 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013680 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013681 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013682 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013683 if (save_starting >= 0)
13684 {
13685 starting = save_starting;
13686 save_starting = -1;
13687 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013688 }
13689 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013690 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013691 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013692}
13693
13694/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010013695 * "test_refcount({expr})" function
13696 */
13697 static void
13698f_test_refcount(typval_T *argvars, typval_T *rettv)
13699{
13700 int retval = -1;
13701
13702 switch (argvars[0].v_type)
13703 {
13704 case VAR_UNKNOWN:
13705 case VAR_NUMBER:
13706 case VAR_FLOAT:
13707 case VAR_SPECIAL:
13708 case VAR_STRING:
13709 break;
13710 case VAR_JOB:
13711#ifdef FEAT_JOB_CHANNEL
13712 if (argvars[0].vval.v_job != NULL)
13713 retval = argvars[0].vval.v_job->jv_refcount - 1;
13714#endif
13715 break;
13716 case VAR_CHANNEL:
13717#ifdef FEAT_JOB_CHANNEL
13718 if (argvars[0].vval.v_channel != NULL)
13719 retval = argvars[0].vval.v_channel->ch_refcount - 1;
13720#endif
13721 break;
13722 case VAR_FUNC:
13723 if (argvars[0].vval.v_string != NULL)
13724 {
13725 ufunc_T *fp;
13726
13727 fp = find_func(argvars[0].vval.v_string);
13728 if (fp != NULL)
13729 retval = fp->uf_refcount;
13730 }
13731 break;
13732 case VAR_PARTIAL:
13733 if (argvars[0].vval.v_partial != NULL)
13734 retval = argvars[0].vval.v_partial->pt_refcount - 1;
13735 break;
13736 case VAR_BLOB:
13737 if (argvars[0].vval.v_blob != NULL)
13738 retval = argvars[0].vval.v_blob->bv_refcount - 1;
13739 break;
13740 case VAR_LIST:
13741 if (argvars[0].vval.v_list != NULL)
13742 retval = argvars[0].vval.v_list->lv_refcount - 1;
13743 break;
13744 case VAR_DICT:
13745 if (argvars[0].vval.v_dict != NULL)
13746 retval = argvars[0].vval.v_dict->dv_refcount - 1;
13747 break;
13748 }
13749
13750 rettv->v_type = VAR_NUMBER;
13751 rettv->vval.v_number = retval;
13752
13753}
13754
13755/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013756 * "test_garbagecollect_now()" function
13757 */
13758 static void
13759f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13760{
13761 /* This is dangerous, any Lists and Dicts used internally may be freed
13762 * while still in use. */
13763 garbage_collect(TRUE);
13764}
13765
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013766/*
Bram Moolenaaradc67142019-06-22 01:40:42 +020013767 * "test_garbagecollect_soon()" function
13768 */
13769 static void
13770f_test_garbagecollect_soon(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13771{
13772 may_garbage_collect = TRUE;
13773}
13774
13775/*
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013776 * "test_ignore_error()" function
13777 */
13778 static void
13779f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13780{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013781 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013782}
13783
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013784 static void
13785f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13786{
13787 rettv->v_type = VAR_BLOB;
13788 rettv->vval.v_blob = NULL;
13789}
13790
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013791#ifdef FEAT_JOB_CHANNEL
13792 static void
13793f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13794{
13795 rettv->v_type = VAR_CHANNEL;
13796 rettv->vval.v_channel = NULL;
13797}
13798#endif
13799
13800 static void
13801f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13802{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013803 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013804}
13805
13806#ifdef FEAT_JOB_CHANNEL
13807 static void
13808f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13809{
13810 rettv->v_type = VAR_JOB;
13811 rettv->vval.v_job = NULL;
13812}
13813#endif
13814
13815 static void
13816f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13817{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013818 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013819}
13820
13821 static void
13822f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13823{
13824 rettv->v_type = VAR_PARTIAL;
13825 rettv->vval.v_partial = NULL;
13826}
13827
13828 static void
13829f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13830{
13831 rettv->v_type = VAR_STRING;
13832 rettv->vval.v_string = NULL;
13833}
13834
Bram Moolenaarab186732018-09-14 21:27:06 +020013835#ifdef FEAT_GUI
13836 static void
13837f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13838{
13839 char_u *which;
13840 long value;
13841 int dragging;
13842 scrollbar_T *sb = NULL;
13843
13844 if (argvars[0].v_type != VAR_STRING
13845 || (argvars[1].v_type) != VAR_NUMBER
13846 || (argvars[2].v_type) != VAR_NUMBER)
13847 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013848 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013849 return;
13850 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013851 which = tv_get_string(&argvars[0]);
13852 value = tv_get_number(&argvars[1]);
13853 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013854
13855 if (STRCMP(which, "left") == 0)
13856 sb = &curwin->w_scrollbars[SBAR_LEFT];
13857 else if (STRCMP(which, "right") == 0)
13858 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13859 else if (STRCMP(which, "hor") == 0)
13860 sb = &gui.bottom_sbar;
13861 if (sb == NULL)
13862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013863 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020013864 return;
13865 }
13866 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013867# ifndef USE_ON_FLY_SCROLL
13868 // need to loop through normal_cmd() to handle the scroll events
13869 exec_normal(FALSE, TRUE, FALSE);
13870# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013871}
13872#endif
13873
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013874#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013875 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013876f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
13877{
13878 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
13879 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
13880}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013881#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013882
13883 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013884f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13885{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013886 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013887}
13888
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013889/*
13890 * Get a callback from "arg". It can be a Funcref or a function name.
13891 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013892 * "cb_name" is not allocated.
13893 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013894 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013895 callback_T
13896get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013897{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013898 callback_T res;
13899
13900 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013901 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13902 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013903 res.cb_partial = arg->vval.v_partial;
13904 ++res.cb_partial->pt_refcount;
13905 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013906 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013907 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013908 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013909 res.cb_partial = NULL;
13910 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
13911 {
13912 // Note that we don't make a copy of the string.
13913 res.cb_name = arg->vval.v_string;
13914 func_ref(res.cb_name);
13915 }
13916 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13917 {
13918 res.cb_name = (char_u *)"";
13919 }
13920 else
13921 {
13922 emsg(_("E921: Invalid callback argument"));
13923 res.cb_name = NULL;
13924 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013925 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013926 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013927}
13928
13929/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013930 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013931 */
13932 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013933put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013934{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013935 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013936 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013937 tv->v_type = VAR_PARTIAL;
13938 tv->vval.v_partial = cb->cb_partial;
13939 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013940 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013941 else
13942 {
13943 tv->v_type = VAR_FUNC;
13944 tv->vval.v_string = vim_strsave(cb->cb_name);
13945 func_ref(cb->cb_name);
13946 }
13947}
13948
13949/*
13950 * Make a copy of "src" into "dest", allocating the function name if needed,
13951 * without incrementing the refcount.
13952 */
13953 void
13954set_callback(callback_T *dest, callback_T *src)
13955{
13956 if (src->cb_partial == NULL)
13957 {
13958 // just a function name, make a copy
13959 dest->cb_name = vim_strsave(src->cb_name);
13960 dest->cb_free_name = TRUE;
13961 }
13962 else
13963 {
13964 // cb_name is a pointer into cb_partial
13965 dest->cb_name = src->cb_name;
13966 dest->cb_free_name = FALSE;
13967 }
13968 dest->cb_partial = src->cb_partial;
13969}
13970
13971/*
13972 * Unref/free "callback" returned by get_callback() or set_callback().
13973 */
13974 void
13975free_callback(callback_T *callback)
13976{
13977 if (callback->cb_partial != NULL)
13978 {
13979 partial_unref(callback->cb_partial);
13980 callback->cb_partial = NULL;
13981 }
13982 else if (callback->cb_name != NULL)
13983 func_unref(callback->cb_name);
13984 if (callback->cb_free_name)
13985 {
13986 vim_free(callback->cb_name);
13987 callback->cb_free_name = FALSE;
13988 }
13989 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013990}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013991
13992#ifdef FEAT_TIMERS
13993/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013994 * "timer_info([timer])" function
13995 */
13996 static void
13997f_timer_info(typval_T *argvars, typval_T *rettv)
13998{
13999 timer_T *timer = NULL;
14000
14001 if (rettv_list_alloc(rettv) != OK)
14002 return;
14003 if (argvars[0].v_type != VAR_UNKNOWN)
14004 {
14005 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014006 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014007 else
14008 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014009 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014010 if (timer != NULL)
14011 add_timer_info(rettv, timer);
14012 }
14013 }
14014 else
14015 add_timer_info_all(rettv);
14016}
14017
14018/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014019 * "timer_pause(timer, paused)" function
14020 */
14021 static void
14022f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14023{
14024 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014025 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014026
14027 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014028 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014029 else
14030 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014031 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014032 if (timer != NULL)
14033 timer->tr_paused = paused;
14034 }
14035}
14036
14037/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014038 * "timer_start(time, callback [, options])" function
14039 */
14040 static void
14041f_timer_start(typval_T *argvars, typval_T *rettv)
14042{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014043 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014044 timer_T *timer;
14045 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014046 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014047 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014048
Bram Moolenaar75537a92016-09-05 22:45:28 +020014049 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014050 if (check_secure())
14051 return;
14052 if (argvars[2].v_type != VAR_UNKNOWN)
14053 {
14054 if (argvars[2].v_type != VAR_DICT
14055 || (dict = argvars[2].vval.v_dict) == NULL)
14056 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014057 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014058 return;
14059 }
14060 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014061 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014062 }
14063
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014064 callback = get_callback(&argvars[1]);
14065 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014066 return;
14067
14068 timer = create_timer(msec, repeat);
14069 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014070 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014071 else
14072 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014073 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014074 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014075 }
14076}
14077
14078/*
14079 * "timer_stop(timer)" function
14080 */
14081 static void
14082f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14083{
14084 timer_T *timer;
14085
14086 if (argvars[0].v_type != VAR_NUMBER)
14087 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014088 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014089 return;
14090 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014091 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014092 if (timer != NULL)
14093 stop_timer(timer);
14094}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014095
14096/*
14097 * "timer_stopall()" function
14098 */
14099 static void
14100f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14101{
14102 stop_all_timers();
14103}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014104#endif
14105
14106/*
14107 * "tolower(string)" function
14108 */
14109 static void
14110f_tolower(typval_T *argvars, typval_T *rettv)
14111{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014112 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014113 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014114}
14115
14116/*
14117 * "toupper(string)" function
14118 */
14119 static void
14120f_toupper(typval_T *argvars, typval_T *rettv)
14121{
14122 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014123 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014124}
14125
14126/*
14127 * "tr(string, fromstr, tostr)" function
14128 */
14129 static void
14130f_tr(typval_T *argvars, typval_T *rettv)
14131{
14132 char_u *in_str;
14133 char_u *fromstr;
14134 char_u *tostr;
14135 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014136 int inlen;
14137 int fromlen;
14138 int tolen;
14139 int idx;
14140 char_u *cpstr;
14141 int cplen;
14142 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014143 char_u buf[NUMBUFLEN];
14144 char_u buf2[NUMBUFLEN];
14145 garray_T ga;
14146
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014147 in_str = tv_get_string(&argvars[0]);
14148 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14149 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014150
14151 /* Default return value: empty string. */
14152 rettv->v_type = VAR_STRING;
14153 rettv->vval.v_string = NULL;
14154 if (fromstr == NULL || tostr == NULL)
14155 return; /* type error; errmsg already given */
14156 ga_init2(&ga, (int)sizeof(char), 80);
14157
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014158 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014159 /* not multi-byte: fromstr and tostr must be the same length */
14160 if (STRLEN(fromstr) != STRLEN(tostr))
14161 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014162error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014163 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014164 ga_clear(&ga);
14165 return;
14166 }
14167
14168 /* fromstr and tostr have to contain the same number of chars */
14169 while (*in_str != NUL)
14170 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014171 if (has_mbyte)
14172 {
14173 inlen = (*mb_ptr2len)(in_str);
14174 cpstr = in_str;
14175 cplen = inlen;
14176 idx = 0;
14177 for (p = fromstr; *p != NUL; p += fromlen)
14178 {
14179 fromlen = (*mb_ptr2len)(p);
14180 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14181 {
14182 for (p = tostr; *p != NUL; p += tolen)
14183 {
14184 tolen = (*mb_ptr2len)(p);
14185 if (idx-- == 0)
14186 {
14187 cplen = tolen;
14188 cpstr = p;
14189 break;
14190 }
14191 }
14192 if (*p == NUL) /* tostr is shorter than fromstr */
14193 goto error;
14194 break;
14195 }
14196 ++idx;
14197 }
14198
14199 if (first && cpstr == in_str)
14200 {
14201 /* Check that fromstr and tostr have the same number of
14202 * (multi-byte) characters. Done only once when a character
14203 * of in_str doesn't appear in fromstr. */
14204 first = FALSE;
14205 for (p = tostr; *p != NUL; p += tolen)
14206 {
14207 tolen = (*mb_ptr2len)(p);
14208 --idx;
14209 }
14210 if (idx != 0)
14211 goto error;
14212 }
14213
14214 (void)ga_grow(&ga, cplen);
14215 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14216 ga.ga_len += cplen;
14217
14218 in_str += inlen;
14219 }
14220 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014221 {
14222 /* When not using multi-byte chars we can do it faster. */
14223 p = vim_strchr(fromstr, *in_str);
14224 if (p != NULL)
14225 ga_append(&ga, tostr[p - fromstr]);
14226 else
14227 ga_append(&ga, *in_str);
14228 ++in_str;
14229 }
14230 }
14231
14232 /* add a terminating NUL */
14233 (void)ga_grow(&ga, 1);
14234 ga_append(&ga, NUL);
14235
14236 rettv->vval.v_string = ga.ga_data;
14237}
14238
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014239/*
14240 * "trim({expr})" function
14241 */
14242 static void
14243f_trim(typval_T *argvars, typval_T *rettv)
14244{
14245 char_u buf1[NUMBUFLEN];
14246 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014247 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014248 char_u *mask = NULL;
14249 char_u *tail;
14250 char_u *prev;
14251 char_u *p;
14252 int c1;
14253
14254 rettv->v_type = VAR_STRING;
14255 if (head == NULL)
14256 {
14257 rettv->vval.v_string = NULL;
14258 return;
14259 }
14260
14261 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014262 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014263
14264 while (*head != NUL)
14265 {
14266 c1 = PTR2CHAR(head);
14267 if (mask == NULL)
14268 {
14269 if (c1 > ' ' && c1 != 0xa0)
14270 break;
14271 }
14272 else
14273 {
14274 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14275 if (c1 == PTR2CHAR(p))
14276 break;
14277 if (*p == NUL)
14278 break;
14279 }
14280 MB_PTR_ADV(head);
14281 }
14282
14283 for (tail = head + STRLEN(head); tail > head; tail = prev)
14284 {
14285 prev = tail;
14286 MB_PTR_BACK(head, prev);
14287 c1 = PTR2CHAR(prev);
14288 if (mask == NULL)
14289 {
14290 if (c1 > ' ' && c1 != 0xa0)
14291 break;
14292 }
14293 else
14294 {
14295 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14296 if (c1 == PTR2CHAR(p))
14297 break;
14298 if (*p == NUL)
14299 break;
14300 }
14301 }
14302 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14303}
14304
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014305#ifdef FEAT_FLOAT
14306/*
14307 * "trunc({float})" function
14308 */
14309 static void
14310f_trunc(typval_T *argvars, typval_T *rettv)
14311{
14312 float_T f = 0.0;
14313
14314 rettv->v_type = VAR_FLOAT;
14315 if (get_float_arg(argvars, &f) == OK)
14316 /* trunc() is not in C90, use floor() or ceil() instead. */
14317 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14318 else
14319 rettv->vval.v_float = 0.0;
14320}
14321#endif
14322
14323/*
14324 * "type(expr)" function
14325 */
14326 static void
14327f_type(typval_T *argvars, typval_T *rettv)
14328{
14329 int n = -1;
14330
14331 switch (argvars[0].v_type)
14332 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014333 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14334 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014335 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014336 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14337 case VAR_LIST: n = VAR_TYPE_LIST; break;
14338 case VAR_DICT: n = VAR_TYPE_DICT; break;
14339 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014340 case VAR_SPECIAL:
14341 if (argvars[0].vval.v_number == VVAL_FALSE
14342 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014343 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014344 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014345 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014346 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014347 case VAR_JOB: n = VAR_TYPE_JOB; break;
14348 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014349 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014350 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014351 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014352 n = -1;
14353 break;
14354 }
14355 rettv->vval.v_number = n;
14356}
14357
14358/*
14359 * "undofile(name)" function
14360 */
14361 static void
14362f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14363{
14364 rettv->v_type = VAR_STRING;
14365#ifdef FEAT_PERSISTENT_UNDO
14366 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014367 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014368
14369 if (*fname == NUL)
14370 {
14371 /* If there is no file name there will be no undo file. */
14372 rettv->vval.v_string = NULL;
14373 }
14374 else
14375 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020014376 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014377
14378 if (ffname != NULL)
14379 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14380 vim_free(ffname);
14381 }
14382 }
14383#else
14384 rettv->vval.v_string = NULL;
14385#endif
14386}
14387
14388/*
14389 * "undotree()" function
14390 */
14391 static void
14392f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14393{
14394 if (rettv_dict_alloc(rettv) == OK)
14395 {
14396 dict_T *dict = rettv->vval.v_dict;
14397 list_T *list;
14398
Bram Moolenaare0be1672018-07-08 16:50:37 +020014399 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14400 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14401 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14402 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14403 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14404 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014405
14406 list = list_alloc();
14407 if (list != NULL)
14408 {
14409 u_eval_tree(curbuf->b_u_oldhead, list);
14410 dict_add_list(dict, "entries", list);
14411 }
14412 }
14413}
14414
14415/*
14416 * "values(dict)" function
14417 */
14418 static void
14419f_values(typval_T *argvars, typval_T *rettv)
14420{
14421 dict_list(argvars, rettv, 1);
14422}
14423
14424/*
14425 * "virtcol(string)" function
14426 */
14427 static void
14428f_virtcol(typval_T *argvars, typval_T *rettv)
14429{
14430 colnr_T vcol = 0;
14431 pos_T *fp;
14432 int fnum = curbuf->b_fnum;
14433
14434 fp = var2fpos(&argvars[0], FALSE, &fnum);
14435 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14436 && fnum == curbuf->b_fnum)
14437 {
14438 getvvcol(curwin, fp, NULL, NULL, &vcol);
14439 ++vcol;
14440 }
14441
14442 rettv->vval.v_number = vcol;
14443}
14444
14445/*
14446 * "visualmode()" function
14447 */
14448 static void
14449f_visualmode(typval_T *argvars, typval_T *rettv)
14450{
14451 char_u str[2];
14452
14453 rettv->v_type = VAR_STRING;
14454 str[0] = curbuf->b_visual_mode_eval;
14455 str[1] = NUL;
14456 rettv->vval.v_string = vim_strsave(str);
14457
14458 /* A non-zero number or non-empty string argument: reset mode. */
14459 if (non_zero_arg(&argvars[0]))
14460 curbuf->b_visual_mode_eval = NUL;
14461}
14462
14463/*
14464 * "wildmenumode()" function
14465 */
14466 static void
14467f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14468{
14469#ifdef FEAT_WILDMENU
14470 if (wild_menu_showing)
14471 rettv->vval.v_number = 1;
14472#endif
14473}
14474
14475/*
14476 * "winbufnr(nr)" function
14477 */
14478 static void
14479f_winbufnr(typval_T *argvars, typval_T *rettv)
14480{
14481 win_T *wp;
14482
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014483 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014484 if (wp == NULL)
14485 rettv->vval.v_number = -1;
14486 else
14487 rettv->vval.v_number = wp->w_buffer->b_fnum;
14488}
14489
14490/*
14491 * "wincol()" function
14492 */
14493 static void
14494f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14495{
14496 validate_cursor();
14497 rettv->vval.v_number = curwin->w_wcol + 1;
14498}
14499
14500/*
14501 * "winheight(nr)" function
14502 */
14503 static void
14504f_winheight(typval_T *argvars, typval_T *rettv)
14505{
14506 win_T *wp;
14507
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014508 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014509 if (wp == NULL)
14510 rettv->vval.v_number = -1;
14511 else
14512 rettv->vval.v_number = wp->w_height;
14513}
14514
14515/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014516 * "winlayout()" function
14517 */
14518 static void
14519f_winlayout(typval_T *argvars, typval_T *rettv)
14520{
14521 tabpage_T *tp;
14522
14523 if (rettv_list_alloc(rettv) != OK)
14524 return;
14525
14526 if (argvars[0].v_type == VAR_UNKNOWN)
14527 tp = curtab;
14528 else
14529 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014530 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014531 if (tp == NULL)
14532 return;
14533 }
14534
14535 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14536}
14537
14538/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014539 * "winline()" function
14540 */
14541 static void
14542f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14543{
14544 validate_cursor();
14545 rettv->vval.v_number = curwin->w_wrow + 1;
14546}
14547
14548/*
14549 * "winnr()" function
14550 */
14551 static void
14552f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14553{
14554 int nr = 1;
14555
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014556 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014557 rettv->vval.v_number = nr;
14558}
14559
14560/*
14561 * "winrestcmd()" function
14562 */
14563 static void
14564f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14565{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014566 win_T *wp;
14567 int winnr = 1;
14568 garray_T ga;
14569 char_u buf[50];
14570
14571 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014572 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014573 {
14574 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14575 ga_concat(&ga, buf);
14576 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14577 ga_concat(&ga, buf);
14578 ++winnr;
14579 }
14580 ga_append(&ga, NUL);
14581
14582 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014583 rettv->v_type = VAR_STRING;
14584}
14585
14586/*
14587 * "winrestview()" function
14588 */
14589 static void
14590f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14591{
14592 dict_T *dict;
14593
14594 if (argvars[0].v_type != VAR_DICT
14595 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014596 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014597 else
14598 {
14599 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014600 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014601 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014602 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014603 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014604 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014605 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14606 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014607 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014608 curwin->w_set_curswant = FALSE;
14609 }
14610
14611 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014612 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014613#ifdef FEAT_DIFF
14614 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014615 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014616#endif
14617 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014618 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014619 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014620 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014621
14622 check_cursor();
14623 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014624 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014625 changed_window_setting();
14626
14627 if (curwin->w_topline <= 0)
14628 curwin->w_topline = 1;
14629 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14630 curwin->w_topline = curbuf->b_ml.ml_line_count;
14631#ifdef FEAT_DIFF
14632 check_topfill(curwin, TRUE);
14633#endif
14634 }
14635}
14636
14637/*
14638 * "winsaveview()" function
14639 */
14640 static void
14641f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14642{
14643 dict_T *dict;
14644
14645 if (rettv_dict_alloc(rettv) == FAIL)
14646 return;
14647 dict = rettv->vval.v_dict;
14648
Bram Moolenaare0be1672018-07-08 16:50:37 +020014649 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14650 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020014651 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014652 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014653 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014654
Bram Moolenaare0be1672018-07-08 16:50:37 +020014655 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014656#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014657 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014658#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014659 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14660 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014661}
14662
14663/*
14664 * "winwidth(nr)" function
14665 */
14666 static void
14667f_winwidth(typval_T *argvars, typval_T *rettv)
14668{
14669 win_T *wp;
14670
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014671 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014672 if (wp == NULL)
14673 rettv->vval.v_number = -1;
14674 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014675 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014676}
14677
14678/*
14679 * "wordcount()" function
14680 */
14681 static void
14682f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14683{
14684 if (rettv_dict_alloc(rettv) == FAIL)
14685 return;
14686 cursor_pos_info(rettv->vval.v_dict);
14687}
14688
14689/*
14690 * "writefile()" function
14691 */
14692 static void
14693f_writefile(typval_T *argvars, typval_T *rettv)
14694{
14695 int binary = FALSE;
14696 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014697#ifdef HAVE_FSYNC
14698 int do_fsync = p_fs;
14699#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014700 char_u *fname;
14701 FILE *fd;
14702 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014703 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014704 list_T *list = NULL;
14705 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014706
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014707 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010014708 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014709 return;
14710
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014711 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014712 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014713 list = argvars[0].vval.v_list;
14714 if (list == NULL)
14715 return;
14716 for (li = list->lv_first; li != NULL; li = li->li_next)
14717 if (tv_get_string_chk(&li->li_tv) == NULL)
14718 return;
14719 }
14720 else if (argvars[0].v_type == VAR_BLOB)
14721 {
14722 blob = argvars[0].vval.v_blob;
14723 if (blob == NULL)
14724 return;
14725 }
14726 else
14727 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014728 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014729 return;
14730 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014731
14732 if (argvars[2].v_type != VAR_UNKNOWN)
14733 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014734 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014735
14736 if (arg2 == NULL)
14737 return;
14738 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014739 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014740 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014741 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014742#ifdef HAVE_FSYNC
14743 if (vim_strchr(arg2, 's') != NULL)
14744 do_fsync = TRUE;
14745 else if (vim_strchr(arg2, 'S') != NULL)
14746 do_fsync = FALSE;
14747#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014748 }
14749
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014750 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014751 if (fname == NULL)
14752 return;
14753
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014754 /* Always open the file in binary mode, library functions have a mind of
14755 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014756 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14757 append ? APPENDBIN : WRITEBIN)) == NULL)
14758 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014759 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014760 ret = -1;
14761 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014762 else if (blob)
14763 {
14764 if (write_blob(fd, blob) == FAIL)
14765 ret = -1;
14766#ifdef HAVE_FSYNC
14767 else if (do_fsync)
14768 // Ignore the error, the user wouldn't know what to do about it.
14769 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010014770 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014771#endif
14772 fclose(fd);
14773 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014774 else
14775 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014776 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014777 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014778#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014779 else if (do_fsync)
14780 /* Ignore the error, the user wouldn't know what to do about it.
14781 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010014782 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014783#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014784 fclose(fd);
14785 }
14786
14787 rettv->vval.v_number = ret;
14788}
14789
14790/*
14791 * "xor(expr, expr)" function
14792 */
14793 static void
14794f_xor(typval_T *argvars, typval_T *rettv)
14795{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014796 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14797 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014798}
14799
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014800#endif /* FEAT_EVAL */