blob: 06f81556913381b582918379ab5cb3ae812b074a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010032static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033static char *e_stringreq = N_("E928: String required");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020034
35#ifdef FEAT_FLOAT
36static void f_abs(typval_T *argvars, typval_T *rettv);
37static void f_acos(typval_T *argvars, typval_T *rettv);
38#endif
39static void f_add(typval_T *argvars, typval_T *rettv);
40static void f_and(typval_T *argvars, typval_T *rettv);
41static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020042static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020043static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010047static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010049static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_assert_exception(typval_T *argvars, typval_T *rettv);
51static void f_assert_fails(typval_T *argvars, typval_T *rettv);
52static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020053static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020054static void f_assert_match(typval_T *argvars, typval_T *rettv);
55static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
56static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010057static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020058static void f_assert_true(typval_T *argvars, typval_T *rettv);
59#ifdef FEAT_FLOAT
60static void f_asin(typval_T *argvars, typval_T *rettv);
61static void f_atan(typval_T *argvars, typval_T *rettv);
62static void f_atan2(typval_T *argvars, typval_T *rettv);
63#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010064#ifdef FEAT_BEVAL
65static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010067static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010069#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_browse(typval_T *argvars, typval_T *rettv);
71static void f_browsedir(typval_T *argvars, typval_T *rettv);
72static void f_bufexists(typval_T *argvars, typval_T *rettv);
73static void f_buflisted(typval_T *argvars, typval_T *rettv);
74static 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
87#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010088static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020090static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
92static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
93static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
94static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
95static void f_ch_info(typval_T *argvars, typval_T *rettv);
96static void f_ch_log(typval_T *argvars, typval_T *rettv);
97static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
98static void f_ch_open(typval_T *argvars, typval_T *rettv);
99static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100100static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200101static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
102static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
103static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
105static void f_ch_status(typval_T *argvars, typval_T *rettv);
106#endif
107static void f_changenr(typval_T *argvars, typval_T *rettv);
108static void f_char2nr(typval_T *argvars, typval_T *rettv);
109static void f_cindent(typval_T *argvars, typval_T *rettv);
110static void f_clearmatches(typval_T *argvars, typval_T *rettv);
111static void f_col(typval_T *argvars, typval_T *rettv);
112#if defined(FEAT_INS_EXPAND)
113static void f_complete(typval_T *argvars, typval_T *rettv);
114static void f_complete_add(typval_T *argvars, typval_T *rettv);
115static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +0100116static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200117#endif
118static void f_confirm(typval_T *argvars, typval_T *rettv);
119static void f_copy(typval_T *argvars, typval_T *rettv);
120#ifdef FEAT_FLOAT
121static void f_cos(typval_T *argvars, typval_T *rettv);
122static void f_cosh(typval_T *argvars, typval_T *rettv);
123#endif
124static void f_count(typval_T *argvars, typval_T *rettv);
125static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
126static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100127#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200128static void f_debugbreak(typval_T *argvars, typval_T *rettv);
129#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200130static void f_deepcopy(typval_T *argvars, typval_T *rettv);
131static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200132static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_did_filetype(typval_T *argvars, typval_T *rettv);
134static void f_diff_filler(typval_T *argvars, typval_T *rettv);
135static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
136static void f_empty(typval_T *argvars, typval_T *rettv);
137static void f_escape(typval_T *argvars, typval_T *rettv);
138static void f_eval(typval_T *argvars, typval_T *rettv);
139static void f_eventhandler(typval_T *argvars, typval_T *rettv);
140static void f_executable(typval_T *argvars, typval_T *rettv);
141static void f_execute(typval_T *argvars, typval_T *rettv);
142static void f_exepath(typval_T *argvars, typval_T *rettv);
143static void f_exists(typval_T *argvars, typval_T *rettv);
144#ifdef FEAT_FLOAT
145static void f_exp(typval_T *argvars, typval_T *rettv);
146#endif
147static void f_expand(typval_T *argvars, typval_T *rettv);
148static void f_extend(typval_T *argvars, typval_T *rettv);
149static void f_feedkeys(typval_T *argvars, typval_T *rettv);
150static void f_filereadable(typval_T *argvars, typval_T *rettv);
151static void f_filewritable(typval_T *argvars, typval_T *rettv);
152static void f_filter(typval_T *argvars, typval_T *rettv);
153static void f_finddir(typval_T *argvars, typval_T *rettv);
154static void f_findfile(typval_T *argvars, typval_T *rettv);
155#ifdef FEAT_FLOAT
156static void f_float2nr(typval_T *argvars, typval_T *rettv);
157static void f_floor(typval_T *argvars, typval_T *rettv);
158static void f_fmod(typval_T *argvars, typval_T *rettv);
159#endif
160static void f_fnameescape(typval_T *argvars, typval_T *rettv);
161static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
162static void f_foldclosed(typval_T *argvars, typval_T *rettv);
163static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
164static void f_foldlevel(typval_T *argvars, typval_T *rettv);
165static void f_foldtext(typval_T *argvars, typval_T *rettv);
166static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
167static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200168static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200169static void f_function(typval_T *argvars, typval_T *rettv);
170static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
171static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200172static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200173static void f_getbufline(typval_T *argvars, typval_T *rettv);
174static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100175static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200176static void f_getchar(typval_T *argvars, typval_T *rettv);
177static void f_getcharmod(typval_T *argvars, typval_T *rettv);
178static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
179static void f_getcmdline(typval_T *argvars, typval_T *rettv);
180#if defined(FEAT_CMDL_COMPL)
181static void f_getcompletion(typval_T *argvars, typval_T *rettv);
182#endif
183static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
184static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
185static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
186static void f_getcwd(typval_T *argvars, typval_T *rettv);
187static void f_getfontname(typval_T *argvars, typval_T *rettv);
188static void f_getfperm(typval_T *argvars, typval_T *rettv);
189static void f_getfsize(typval_T *argvars, typval_T *rettv);
190static void f_getftime(typval_T *argvars, typval_T *rettv);
191static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100192static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200194static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200195static void f_getmatches(typval_T *argvars, typval_T *rettv);
196static void f_getpid(typval_T *argvars, typval_T *rettv);
197static void f_getcurpos(typval_T *argvars, typval_T *rettv);
198static void f_getpos(typval_T *argvars, typval_T *rettv);
199static void f_getqflist(typval_T *argvars, typval_T *rettv);
200static void f_getreg(typval_T *argvars, typval_T *rettv);
201static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200202static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200203static void f_gettabvar(typval_T *argvars, typval_T *rettv);
204static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100205static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200206static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100207static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200208static void f_getwinposx(typval_T *argvars, typval_T *rettv);
209static void f_getwinposy(typval_T *argvars, typval_T *rettv);
210static void f_getwinvar(typval_T *argvars, typval_T *rettv);
211static void f_glob(typval_T *argvars, typval_T *rettv);
212static void f_globpath(typval_T *argvars, typval_T *rettv);
213static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
214static void f_has(typval_T *argvars, typval_T *rettv);
215static void f_has_key(typval_T *argvars, typval_T *rettv);
216static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
217static void f_hasmapto(typval_T *argvars, typval_T *rettv);
218static void f_histadd(typval_T *argvars, typval_T *rettv);
219static void f_histdel(typval_T *argvars, typval_T *rettv);
220static void f_histget(typval_T *argvars, typval_T *rettv);
221static void f_histnr(typval_T *argvars, typval_T *rettv);
222static void f_hlID(typval_T *argvars, typval_T *rettv);
223static void f_hlexists(typval_T *argvars, typval_T *rettv);
224static void f_hostname(typval_T *argvars, typval_T *rettv);
225static void f_iconv(typval_T *argvars, typval_T *rettv);
226static void f_indent(typval_T *argvars, typval_T *rettv);
227static void f_index(typval_T *argvars, typval_T *rettv);
228static void f_input(typval_T *argvars, typval_T *rettv);
229static void f_inputdialog(typval_T *argvars, typval_T *rettv);
230static void f_inputlist(typval_T *argvars, typval_T *rettv);
231static void f_inputrestore(typval_T *argvars, typval_T *rettv);
232static void f_inputsave(typval_T *argvars, typval_T *rettv);
233static void f_inputsecret(typval_T *argvars, typval_T *rettv);
234static void f_insert(typval_T *argvars, typval_T *rettv);
235static void f_invert(typval_T *argvars, typval_T *rettv);
236static void f_isdirectory(typval_T *argvars, typval_T *rettv);
237static void f_islocked(typval_T *argvars, typval_T *rettv);
238#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
239static void f_isnan(typval_T *argvars, typval_T *rettv);
240#endif
241static void f_items(typval_T *argvars, typval_T *rettv);
242#ifdef FEAT_JOB_CHANNEL
243static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
244static void f_job_info(typval_T *argvars, typval_T *rettv);
245static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
246static void f_job_start(typval_T *argvars, typval_T *rettv);
247static void f_job_stop(typval_T *argvars, typval_T *rettv);
248static void f_job_status(typval_T *argvars, typval_T *rettv);
249#endif
250static void f_join(typval_T *argvars, typval_T *rettv);
251static void f_js_decode(typval_T *argvars, typval_T *rettv);
252static void f_js_encode(typval_T *argvars, typval_T *rettv);
253static void f_json_decode(typval_T *argvars, typval_T *rettv);
254static void f_json_encode(typval_T *argvars, typval_T *rettv);
255static void f_keys(typval_T *argvars, typval_T *rettv);
256static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
257static void f_len(typval_T *argvars, typval_T *rettv);
258static void f_libcall(typval_T *argvars, typval_T *rettv);
259static void f_libcallnr(typval_T *argvars, typval_T *rettv);
260static void f_line(typval_T *argvars, typval_T *rettv);
261static void f_line2byte(typval_T *argvars, typval_T *rettv);
262static void f_lispindent(typval_T *argvars, typval_T *rettv);
263static void f_localtime(typval_T *argvars, typval_T *rettv);
264#ifdef FEAT_FLOAT
265static void f_log(typval_T *argvars, typval_T *rettv);
266static void f_log10(typval_T *argvars, typval_T *rettv);
267#endif
268#ifdef FEAT_LUA
269static void f_luaeval(typval_T *argvars, typval_T *rettv);
270#endif
271static void f_map(typval_T *argvars, typval_T *rettv);
272static void f_maparg(typval_T *argvars, typval_T *rettv);
273static void f_mapcheck(typval_T *argvars, typval_T *rettv);
274static void f_match(typval_T *argvars, typval_T *rettv);
275static void f_matchadd(typval_T *argvars, typval_T *rettv);
276static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
277static void f_matcharg(typval_T *argvars, typval_T *rettv);
278static void f_matchdelete(typval_T *argvars, typval_T *rettv);
279static void f_matchend(typval_T *argvars, typval_T *rettv);
280static void f_matchlist(typval_T *argvars, typval_T *rettv);
281static void f_matchstr(typval_T *argvars, typval_T *rettv);
282static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
283static void f_max(typval_T *argvars, typval_T *rettv);
284static void f_min(typval_T *argvars, typval_T *rettv);
285#ifdef vim_mkdir
286static void f_mkdir(typval_T *argvars, typval_T *rettv);
287#endif
288static void f_mode(typval_T *argvars, typval_T *rettv);
289#ifdef FEAT_MZSCHEME
290static void f_mzeval(typval_T *argvars, typval_T *rettv);
291#endif
292static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
293static void f_nr2char(typval_T *argvars, typval_T *rettv);
294static void f_or(typval_T *argvars, typval_T *rettv);
295static void f_pathshorten(typval_T *argvars, typval_T *rettv);
296#ifdef FEAT_PERL
297static void f_perleval(typval_T *argvars, typval_T *rettv);
298#endif
299#ifdef FEAT_FLOAT
300static void f_pow(typval_T *argvars, typval_T *rettv);
301#endif
302static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
303static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200304#ifdef FEAT_JOB_CHANNEL
305static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200306static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200307static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
308#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200309static void f_pumvisible(typval_T *argvars, typval_T *rettv);
310#ifdef FEAT_PYTHON3
311static void f_py3eval(typval_T *argvars, typval_T *rettv);
312#endif
313#ifdef FEAT_PYTHON
314static void f_pyeval(typval_T *argvars, typval_T *rettv);
315#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100316#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
317static void f_pyxeval(typval_T *argvars, typval_T *rettv);
318#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200319static void f_range(typval_T *argvars, typval_T *rettv);
320static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200321static void f_reg_executing(typval_T *argvars, typval_T *rettv);
322static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200323static void f_reltime(typval_T *argvars, typval_T *rettv);
324#ifdef FEAT_FLOAT
325static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
326#endif
327static void f_reltimestr(typval_T *argvars, typval_T *rettv);
328static void f_remote_expr(typval_T *argvars, typval_T *rettv);
329static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
330static void f_remote_peek(typval_T *argvars, typval_T *rettv);
331static void f_remote_read(typval_T *argvars, typval_T *rettv);
332static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100333static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200334static void f_remove(typval_T *argvars, typval_T *rettv);
335static void f_rename(typval_T *argvars, typval_T *rettv);
336static void f_repeat(typval_T *argvars, typval_T *rettv);
337static void f_resolve(typval_T *argvars, typval_T *rettv);
338static void f_reverse(typval_T *argvars, typval_T *rettv);
339#ifdef FEAT_FLOAT
340static void f_round(typval_T *argvars, typval_T *rettv);
341#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100342#ifdef FEAT_RUBY
343static void f_rubyeval(typval_T *argvars, typval_T *rettv);
344#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200345static void f_screenattr(typval_T *argvars, typval_T *rettv);
346static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100347static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200348static void f_screencol(typval_T *argvars, typval_T *rettv);
349static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100350static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200351static void f_search(typval_T *argvars, typval_T *rettv);
352static void f_searchdecl(typval_T *argvars, typval_T *rettv);
353static void f_searchpair(typval_T *argvars, typval_T *rettv);
354static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
355static void f_searchpos(typval_T *argvars, typval_T *rettv);
356static void f_server2client(typval_T *argvars, typval_T *rettv);
357static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200358static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200359static void f_setbufvar(typval_T *argvars, typval_T *rettv);
360static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
361static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
362static void f_setfperm(typval_T *argvars, typval_T *rettv);
363static void f_setline(typval_T *argvars, typval_T *rettv);
364static void f_setloclist(typval_T *argvars, typval_T *rettv);
365static void f_setmatches(typval_T *argvars, typval_T *rettv);
366static void f_setpos(typval_T *argvars, typval_T *rettv);
367static void f_setqflist(typval_T *argvars, typval_T *rettv);
368static void f_setreg(typval_T *argvars, typval_T *rettv);
369static void f_settabvar(typval_T *argvars, typval_T *rettv);
370static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100371static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200372static void f_setwinvar(typval_T *argvars, typval_T *rettv);
373#ifdef FEAT_CRYPT
374static void f_sha256(typval_T *argvars, typval_T *rettv);
375#endif /* FEAT_CRYPT */
376static void f_shellescape(typval_T *argvars, typval_T *rettv);
377static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100378#ifdef FEAT_SIGNS
379static void f_sign_define(typval_T *argvars, typval_T *rettv);
380static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
381static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100382static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100383static void f_sign_place(typval_T *argvars, typval_T *rettv);
384static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
385static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
386#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200387static void f_simplify(typval_T *argvars, typval_T *rettv);
388#ifdef FEAT_FLOAT
389static void f_sin(typval_T *argvars, typval_T *rettv);
390static void f_sinh(typval_T *argvars, typval_T *rettv);
391#endif
392static void f_sort(typval_T *argvars, typval_T *rettv);
393static void f_soundfold(typval_T *argvars, typval_T *rettv);
394static void f_spellbadword(typval_T *argvars, typval_T *rettv);
395static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
396static void f_split(typval_T *argvars, typval_T *rettv);
397#ifdef FEAT_FLOAT
398static void f_sqrt(typval_T *argvars, typval_T *rettv);
399static void f_str2float(typval_T *argvars, typval_T *rettv);
400#endif
401static void f_str2nr(typval_T *argvars, typval_T *rettv);
402static void f_strchars(typval_T *argvars, typval_T *rettv);
403#ifdef HAVE_STRFTIME
404static void f_strftime(typval_T *argvars, typval_T *rettv);
405#endif
406static void f_strgetchar(typval_T *argvars, typval_T *rettv);
407static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200408static void f_strlen(typval_T *argvars, typval_T *rettv);
409static void f_strcharpart(typval_T *argvars, typval_T *rettv);
410static void f_strpart(typval_T *argvars, typval_T *rettv);
411static void f_strridx(typval_T *argvars, typval_T *rettv);
412static void f_strtrans(typval_T *argvars, typval_T *rettv);
413static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
414static void f_strwidth(typval_T *argvars, typval_T *rettv);
415static void f_submatch(typval_T *argvars, typval_T *rettv);
416static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200417static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200418static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200419static void f_synID(typval_T *argvars, typval_T *rettv);
420static void f_synIDattr(typval_T *argvars, typval_T *rettv);
421static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
422static void f_synstack(typval_T *argvars, typval_T *rettv);
423static void f_synconcealed(typval_T *argvars, typval_T *rettv);
424static void f_system(typval_T *argvars, typval_T *rettv);
425static void f_systemlist(typval_T *argvars, typval_T *rettv);
426static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
427static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
428static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
429static void f_taglist(typval_T *argvars, typval_T *rettv);
430static void f_tagfiles(typval_T *argvars, typval_T *rettv);
431static void f_tempname(typval_T *argvars, typval_T *rettv);
432static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
433static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200434static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200435static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100436static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100437static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200438static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100439static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100440static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200441#ifdef FEAT_JOB_CHANNEL
442static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
443#endif
444static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
445#ifdef FEAT_JOB_CHANNEL
446static void f_test_null_job(typval_T *argvars, typval_T *rettv);
447#endif
448static void f_test_null_list(typval_T *argvars, typval_T *rettv);
449static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
450static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200451#ifdef FEAT_GUI
452static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
453#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200454static void f_test_settime(typval_T *argvars, typval_T *rettv);
455#ifdef FEAT_FLOAT
456static void f_tan(typval_T *argvars, typval_T *rettv);
457static void f_tanh(typval_T *argvars, typval_T *rettv);
458#endif
459#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200460static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200461static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200462static void f_timer_start(typval_T *argvars, typval_T *rettv);
463static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200464static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200465#endif
466static void f_tolower(typval_T *argvars, typval_T *rettv);
467static void f_toupper(typval_T *argvars, typval_T *rettv);
468static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100469static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200470#ifdef FEAT_FLOAT
471static void f_trunc(typval_T *argvars, typval_T *rettv);
472#endif
473static void f_type(typval_T *argvars, typval_T *rettv);
474static void f_undofile(typval_T *argvars, typval_T *rettv);
475static void f_undotree(typval_T *argvars, typval_T *rettv);
476static void f_uniq(typval_T *argvars, typval_T *rettv);
477static void f_values(typval_T *argvars, typval_T *rettv);
478static void f_virtcol(typval_T *argvars, typval_T *rettv);
479static void f_visualmode(typval_T *argvars, typval_T *rettv);
480static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
481static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
482static void f_win_getid(typval_T *argvars, typval_T *rettv);
483static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
484static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
485static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100486static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200487static void f_winbufnr(typval_T *argvars, typval_T *rettv);
488static void f_wincol(typval_T *argvars, typval_T *rettv);
489static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200490static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200491static void f_winline(typval_T *argvars, typval_T *rettv);
492static void f_winnr(typval_T *argvars, typval_T *rettv);
493static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
494static void f_winrestview(typval_T *argvars, typval_T *rettv);
495static void f_winsaveview(typval_T *argvars, typval_T *rettv);
496static void f_winwidth(typval_T *argvars, typval_T *rettv);
497static void f_writefile(typval_T *argvars, typval_T *rettv);
498static void f_wordcount(typval_T *argvars, typval_T *rettv);
499static void f_xor(typval_T *argvars, typval_T *rettv);
500
501/*
502 * Array with names and number of arguments of all internal functions
503 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
504 */
505static struct fst
506{
507 char *f_name; /* function name */
508 char f_min_argc; /* minimal number of arguments */
509 char f_max_argc; /* maximal number of arguments */
510 void (*f_func)(typval_T *args, typval_T *rvar);
511 /* implementation of function */
512} functions[] =
513{
514#ifdef FEAT_FLOAT
515 {"abs", 1, 1, f_abs},
516 {"acos", 1, 1, f_acos}, /* WJMc */
517#endif
518 {"add", 2, 2, f_add},
519 {"and", 2, 2, f_and},
520 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200521 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200522 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200523 {"argidx", 0, 0, f_argidx},
524 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200525 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200526#ifdef FEAT_FLOAT
527 {"asin", 1, 1, f_asin}, /* WJMc */
528#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100529 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200530 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100531 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200532 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200533 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200534 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100535 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200536 {"assert_match", 2, 3, f_assert_match},
537 {"assert_notequal", 2, 3, f_assert_notequal},
538 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100539 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200540 {"assert_true", 1, 2, f_assert_true},
541#ifdef FEAT_FLOAT
542 {"atan", 1, 1, f_atan},
543 {"atan2", 2, 2, f_atan2},
544#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100545#ifdef FEAT_BEVAL
546 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100547# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100548 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100549# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100550#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200551 {"browse", 4, 4, f_browse},
552 {"browsedir", 2, 2, f_browsedir},
553 {"bufexists", 1, 1, f_bufexists},
554 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
555 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
556 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
557 {"buflisted", 1, 1, f_buflisted},
558 {"bufloaded", 1, 1, f_bufloaded},
559 {"bufname", 1, 1, f_bufname},
560 {"bufnr", 1, 2, f_bufnr},
561 {"bufwinid", 1, 1, f_bufwinid},
562 {"bufwinnr", 1, 1, f_bufwinnr},
563 {"byte2line", 1, 1, f_byte2line},
564 {"byteidx", 2, 2, f_byteidx},
565 {"byteidxcomp", 2, 2, f_byteidxcomp},
566 {"call", 2, 3, f_call},
567#ifdef FEAT_FLOAT
568 {"ceil", 1, 1, f_ceil},
569#endif
570#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100571 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200572 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200573 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200574 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
575 {"ch_evalraw", 2, 3, f_ch_evalraw},
576 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
577 {"ch_getjob", 1, 1, f_ch_getjob},
578 {"ch_info", 1, 1, f_ch_info},
579 {"ch_log", 1, 2, f_ch_log},
580 {"ch_logfile", 1, 2, f_ch_logfile},
581 {"ch_open", 1, 2, f_ch_open},
582 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100583 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200584 {"ch_readraw", 1, 2, f_ch_readraw},
585 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
586 {"ch_sendraw", 2, 3, f_ch_sendraw},
587 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200588 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589#endif
590 {"changenr", 0, 0, f_changenr},
591 {"char2nr", 1, 2, f_char2nr},
592 {"cindent", 1, 1, f_cindent},
593 {"clearmatches", 0, 0, f_clearmatches},
594 {"col", 1, 1, f_col},
595#if defined(FEAT_INS_EXPAND)
596 {"complete", 2, 2, f_complete},
597 {"complete_add", 1, 1, f_complete_add},
598 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100599 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200600#endif
601 {"confirm", 1, 4, f_confirm},
602 {"copy", 1, 1, f_copy},
603#ifdef FEAT_FLOAT
604 {"cos", 1, 1, f_cos},
605 {"cosh", 1, 1, f_cosh},
606#endif
607 {"count", 2, 4, f_count},
608 {"cscope_connection",0,3, f_cscope_connection},
609 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100610#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200611 {"debugbreak", 1, 1, f_debugbreak},
612#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200613 {"deepcopy", 1, 2, f_deepcopy},
614 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200615 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200616 {"did_filetype", 0, 0, f_did_filetype},
617 {"diff_filler", 1, 1, f_diff_filler},
618 {"diff_hlID", 2, 2, f_diff_hlID},
619 {"empty", 1, 1, f_empty},
620 {"escape", 2, 2, f_escape},
621 {"eval", 1, 1, f_eval},
622 {"eventhandler", 0, 0, f_eventhandler},
623 {"executable", 1, 1, f_executable},
624 {"execute", 1, 2, f_execute},
625 {"exepath", 1, 1, f_exepath},
626 {"exists", 1, 1, f_exists},
627#ifdef FEAT_FLOAT
628 {"exp", 1, 1, f_exp},
629#endif
630 {"expand", 1, 3, f_expand},
631 {"extend", 2, 3, f_extend},
632 {"feedkeys", 1, 2, f_feedkeys},
633 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
634 {"filereadable", 1, 1, f_filereadable},
635 {"filewritable", 1, 1, f_filewritable},
636 {"filter", 2, 2, f_filter},
637 {"finddir", 1, 3, f_finddir},
638 {"findfile", 1, 3, f_findfile},
639#ifdef FEAT_FLOAT
640 {"float2nr", 1, 1, f_float2nr},
641 {"floor", 1, 1, f_floor},
642 {"fmod", 2, 2, f_fmod},
643#endif
644 {"fnameescape", 1, 1, f_fnameescape},
645 {"fnamemodify", 2, 2, f_fnamemodify},
646 {"foldclosed", 1, 1, f_foldclosed},
647 {"foldclosedend", 1, 1, f_foldclosedend},
648 {"foldlevel", 1, 1, f_foldlevel},
649 {"foldtext", 0, 0, f_foldtext},
650 {"foldtextresult", 1, 1, f_foldtextresult},
651 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200652 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200653 {"function", 1, 3, f_function},
654 {"garbagecollect", 0, 1, f_garbagecollect},
655 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200656 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200657 {"getbufline", 2, 3, f_getbufline},
658 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100659 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200660 {"getchar", 0, 1, f_getchar},
661 {"getcharmod", 0, 0, f_getcharmod},
662 {"getcharsearch", 0, 0, f_getcharsearch},
663 {"getcmdline", 0, 0, f_getcmdline},
664 {"getcmdpos", 0, 0, f_getcmdpos},
665 {"getcmdtype", 0, 0, f_getcmdtype},
666 {"getcmdwintype", 0, 0, f_getcmdwintype},
667#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200668 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200669#endif
670 {"getcurpos", 0, 0, f_getcurpos},
671 {"getcwd", 0, 2, f_getcwd},
672 {"getfontname", 0, 1, f_getfontname},
673 {"getfperm", 1, 1, f_getfperm},
674 {"getfsize", 1, 1, f_getfsize},
675 {"getftime", 1, 1, f_getftime},
676 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100677 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200678 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200679 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200680 {"getmatches", 0, 0, f_getmatches},
681 {"getpid", 0, 0, f_getpid},
682 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200683 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200684 {"getreg", 0, 3, f_getreg},
685 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200686 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200687 {"gettabvar", 2, 3, f_gettabvar},
688 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100689 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200690 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100691 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200692 {"getwinposx", 0, 0, f_getwinposx},
693 {"getwinposy", 0, 0, f_getwinposy},
694 {"getwinvar", 2, 3, f_getwinvar},
695 {"glob", 1, 4, f_glob},
696 {"glob2regpat", 1, 1, f_glob2regpat},
697 {"globpath", 2, 5, f_globpath},
698 {"has", 1, 1, f_has},
699 {"has_key", 2, 2, f_has_key},
700 {"haslocaldir", 0, 2, f_haslocaldir},
701 {"hasmapto", 1, 3, f_hasmapto},
702 {"highlightID", 1, 1, f_hlID}, /* obsolete */
703 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
704 {"histadd", 2, 2, f_histadd},
705 {"histdel", 1, 2, f_histdel},
706 {"histget", 1, 2, f_histget},
707 {"histnr", 1, 1, f_histnr},
708 {"hlID", 1, 1, f_hlID},
709 {"hlexists", 1, 1, f_hlexists},
710 {"hostname", 0, 0, f_hostname},
711 {"iconv", 3, 3, f_iconv},
712 {"indent", 1, 1, f_indent},
713 {"index", 2, 4, f_index},
714 {"input", 1, 3, f_input},
715 {"inputdialog", 1, 3, f_inputdialog},
716 {"inputlist", 1, 1, f_inputlist},
717 {"inputrestore", 0, 0, f_inputrestore},
718 {"inputsave", 0, 0, f_inputsave},
719 {"inputsecret", 1, 2, f_inputsecret},
720 {"insert", 2, 3, f_insert},
721 {"invert", 1, 1, f_invert},
722 {"isdirectory", 1, 1, f_isdirectory},
723 {"islocked", 1, 1, f_islocked},
724#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
725 {"isnan", 1, 1, f_isnan},
726#endif
727 {"items", 1, 1, f_items},
728#ifdef FEAT_JOB_CHANNEL
729 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200730 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200731 {"job_setoptions", 2, 2, f_job_setoptions},
732 {"job_start", 1, 2, f_job_start},
733 {"job_status", 1, 1, f_job_status},
734 {"job_stop", 1, 2, f_job_stop},
735#endif
736 {"join", 1, 2, f_join},
737 {"js_decode", 1, 1, f_js_decode},
738 {"js_encode", 1, 1, f_js_encode},
739 {"json_decode", 1, 1, f_json_decode},
740 {"json_encode", 1, 1, f_json_encode},
741 {"keys", 1, 1, f_keys},
742 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
743 {"len", 1, 1, f_len},
744 {"libcall", 3, 3, f_libcall},
745 {"libcallnr", 3, 3, f_libcallnr},
746 {"line", 1, 1, f_line},
747 {"line2byte", 1, 1, f_line2byte},
748 {"lispindent", 1, 1, f_lispindent},
749 {"localtime", 0, 0, f_localtime},
750#ifdef FEAT_FLOAT
751 {"log", 1, 1, f_log},
752 {"log10", 1, 1, f_log10},
753#endif
754#ifdef FEAT_LUA
755 {"luaeval", 1, 2, f_luaeval},
756#endif
757 {"map", 2, 2, f_map},
758 {"maparg", 1, 4, f_maparg},
759 {"mapcheck", 1, 3, f_mapcheck},
760 {"match", 2, 4, f_match},
761 {"matchadd", 2, 5, f_matchadd},
762 {"matchaddpos", 2, 5, f_matchaddpos},
763 {"matcharg", 1, 1, f_matcharg},
764 {"matchdelete", 1, 1, f_matchdelete},
765 {"matchend", 2, 4, f_matchend},
766 {"matchlist", 2, 4, f_matchlist},
767 {"matchstr", 2, 4, f_matchstr},
768 {"matchstrpos", 2, 4, f_matchstrpos},
769 {"max", 1, 1, f_max},
770 {"min", 1, 1, f_min},
771#ifdef vim_mkdir
772 {"mkdir", 1, 3, f_mkdir},
773#endif
774 {"mode", 0, 1, f_mode},
775#ifdef FEAT_MZSCHEME
776 {"mzeval", 1, 1, f_mzeval},
777#endif
778 {"nextnonblank", 1, 1, f_nextnonblank},
779 {"nr2char", 1, 2, f_nr2char},
780 {"or", 2, 2, f_or},
781 {"pathshorten", 1, 1, f_pathshorten},
782#ifdef FEAT_PERL
783 {"perleval", 1, 1, f_perleval},
784#endif
785#ifdef FEAT_FLOAT
786 {"pow", 2, 2, f_pow},
787#endif
788 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100789 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200790#ifdef FEAT_JOB_CHANNEL
791 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200792 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200793 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
794#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100795#ifdef FEAT_TEXT_PROP
796 {"prop_add", 3, 3, f_prop_add},
797 {"prop_clear", 1, 3, f_prop_clear},
798 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100799 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100800 {"prop_type_add", 2, 2, f_prop_type_add},
801 {"prop_type_change", 2, 2, f_prop_type_change},
802 {"prop_type_delete", 1, 2, f_prop_type_delete},
803 {"prop_type_get", 1, 2, f_prop_type_get},
804 {"prop_type_list", 0, 1, f_prop_type_list},
805#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200806 {"pumvisible", 0, 0, f_pumvisible},
807#ifdef FEAT_PYTHON3
808 {"py3eval", 1, 1, f_py3eval},
809#endif
810#ifdef FEAT_PYTHON
811 {"pyeval", 1, 1, f_pyeval},
812#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100813#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
814 {"pyxeval", 1, 1, f_pyxeval},
815#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200816 {"range", 1, 3, f_range},
817 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200818 {"reg_executing", 0, 0, f_reg_executing},
819 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200820 {"reltime", 0, 2, f_reltime},
821#ifdef FEAT_FLOAT
822 {"reltimefloat", 1, 1, f_reltimefloat},
823#endif
824 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100825 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200826 {"remote_foreground", 1, 1, f_remote_foreground},
827 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100828 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200829 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100830 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200831 {"remove", 2, 3, f_remove},
832 {"rename", 2, 2, f_rename},
833 {"repeat", 2, 2, f_repeat},
834 {"resolve", 1, 1, f_resolve},
835 {"reverse", 1, 1, f_reverse},
836#ifdef FEAT_FLOAT
837 {"round", 1, 1, f_round},
838#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100839#ifdef FEAT_RUBY
840 {"rubyeval", 1, 1, f_rubyeval},
841#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200842 {"screenattr", 2, 2, f_screenattr},
843 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100844 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200845 {"screencol", 0, 0, f_screencol},
846 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100847 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200848 {"search", 1, 4, f_search},
849 {"searchdecl", 1, 3, f_searchdecl},
850 {"searchpair", 3, 7, f_searchpair},
851 {"searchpairpos", 3, 7, f_searchpairpos},
852 {"searchpos", 1, 4, f_searchpos},
853 {"server2client", 2, 2, f_server2client},
854 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200855 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200856 {"setbufvar", 3, 3, f_setbufvar},
857 {"setcharsearch", 1, 1, f_setcharsearch},
858 {"setcmdpos", 1, 1, f_setcmdpos},
859 {"setfperm", 2, 2, f_setfperm},
860 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200861 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200862 {"setmatches", 1, 1, f_setmatches},
863 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200864 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865 {"setreg", 2, 3, f_setreg},
866 {"settabvar", 3, 3, f_settabvar},
867 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100868 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200869 {"setwinvar", 3, 3, f_setwinvar},
870#ifdef FEAT_CRYPT
871 {"sha256", 1, 1, f_sha256},
872#endif
873 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100874 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100875#ifdef FEAT_SIGNS
876 {"sign_define", 1, 2, f_sign_define},
877 {"sign_getdefined", 0, 1, f_sign_getdefined},
878 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100879 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100880 {"sign_place", 4, 5, f_sign_place},
881 {"sign_undefine", 0, 1, f_sign_undefine},
882 {"sign_unplace", 1, 2, f_sign_unplace},
883#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200884 {"simplify", 1, 1, f_simplify},
885#ifdef FEAT_FLOAT
886 {"sin", 1, 1, f_sin},
887 {"sinh", 1, 1, f_sinh},
888#endif
889 {"sort", 1, 3, f_sort},
890 {"soundfold", 1, 1, f_soundfold},
891 {"spellbadword", 0, 1, f_spellbadword},
892 {"spellsuggest", 1, 3, f_spellsuggest},
893 {"split", 1, 3, f_split},
894#ifdef FEAT_FLOAT
895 {"sqrt", 1, 1, f_sqrt},
896 {"str2float", 1, 1, f_str2float},
897#endif
898 {"str2nr", 1, 2, f_str2nr},
899 {"strcharpart", 2, 3, f_strcharpart},
900 {"strchars", 1, 2, f_strchars},
901 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
902#ifdef HAVE_STRFTIME
903 {"strftime", 1, 2, f_strftime},
904#endif
905 {"strgetchar", 2, 2, f_strgetchar},
906 {"stridx", 2, 3, f_stridx},
907 {"string", 1, 1, f_string},
908 {"strlen", 1, 1, f_strlen},
909 {"strpart", 2, 3, f_strpart},
910 {"strridx", 2, 3, f_strridx},
911 {"strtrans", 1, 1, f_strtrans},
912 {"strwidth", 1, 1, f_strwidth},
913 {"submatch", 1, 2, f_submatch},
914 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200915 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200916 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200917 {"synID", 3, 3, f_synID},
918 {"synIDattr", 2, 3, f_synIDattr},
919 {"synIDtrans", 1, 1, f_synIDtrans},
920 {"synconcealed", 2, 2, f_synconcealed},
921 {"synstack", 2, 2, f_synstack},
922 {"system", 1, 2, f_system},
923 {"systemlist", 1, 2, f_systemlist},
924 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
925 {"tabpagenr", 0, 1, f_tabpagenr},
926 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
927 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100928 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200929#ifdef FEAT_FLOAT
930 {"tan", 1, 1, f_tan},
931 {"tanh", 1, 1, f_tanh},
932#endif
933 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200934#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100935 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
936 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100937 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200938 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200939# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
940 {"term_getansicolors", 1, 1, f_term_getansicolors},
941# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200942 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200943 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200944 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200945 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200946 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200947 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200948 {"term_getstatus", 1, 1, f_term_getstatus},
949 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200950 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200951 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200952 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200953 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200954# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
955 {"term_setansicolors", 2, 2, f_term_setansicolors},
956# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100957 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100958 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200959 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200960 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200961 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200962#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200963 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
964 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200965 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200966 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100967 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100968 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200969#ifdef FEAT_JOB_CHANNEL
970 {"test_null_channel", 0, 0, f_test_null_channel},
971#endif
972 {"test_null_dict", 0, 0, f_test_null_dict},
973#ifdef FEAT_JOB_CHANNEL
974 {"test_null_job", 0, 0, f_test_null_job},
975#endif
976 {"test_null_list", 0, 0, f_test_null_list},
977 {"test_null_partial", 0, 0, f_test_null_partial},
978 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200979 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100980 {"test_override", 2, 2, f_test_override},
981 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200982#ifdef FEAT_GUI
983 {"test_scrollbar", 3, 3, f_test_scrollbar},
984#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200985 {"test_settime", 1, 1, f_test_settime},
986#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200987 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200988 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200989 {"timer_start", 2, 3, f_timer_start},
990 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200991 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200992#endif
993 {"tolower", 1, 1, f_tolower},
994 {"toupper", 1, 1, f_toupper},
995 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100996 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200997#ifdef FEAT_FLOAT
998 {"trunc", 1, 1, f_trunc},
999#endif
1000 {"type", 1, 1, f_type},
1001 {"undofile", 1, 1, f_undofile},
1002 {"undotree", 0, 0, f_undotree},
1003 {"uniq", 1, 3, f_uniq},
1004 {"values", 1, 1, f_values},
1005 {"virtcol", 1, 1, f_virtcol},
1006 {"visualmode", 0, 1, f_visualmode},
1007 {"wildmenumode", 0, 0, f_wildmenumode},
1008 {"win_findbuf", 1, 1, f_win_findbuf},
1009 {"win_getid", 0, 2, f_win_getid},
1010 {"win_gotoid", 1, 1, f_win_gotoid},
1011 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1012 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001013 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001014 {"winbufnr", 1, 1, f_winbufnr},
1015 {"wincol", 0, 0, f_wincol},
1016 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001017 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001018 {"winline", 0, 0, f_winline},
1019 {"winnr", 0, 1, f_winnr},
1020 {"winrestcmd", 0, 0, f_winrestcmd},
1021 {"winrestview", 1, 1, f_winrestview},
1022 {"winsaveview", 0, 0, f_winsaveview},
1023 {"winwidth", 1, 1, f_winwidth},
1024 {"wordcount", 0, 0, f_wordcount},
1025 {"writefile", 2, 3, f_writefile},
1026 {"xor", 2, 2, f_xor},
1027};
1028
1029#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1030
1031/*
1032 * Function given to ExpandGeneric() to obtain the list of internal
1033 * or user defined function names.
1034 */
1035 char_u *
1036get_function_name(expand_T *xp, int idx)
1037{
1038 static int intidx = -1;
1039 char_u *name;
1040
1041 if (idx == 0)
1042 intidx = -1;
1043 if (intidx < 0)
1044 {
1045 name = get_user_func_name(xp, idx);
1046 if (name != NULL)
1047 return name;
1048 }
1049 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1050 {
1051 STRCPY(IObuff, functions[intidx].f_name);
1052 STRCAT(IObuff, "(");
1053 if (functions[intidx].f_max_argc == 0)
1054 STRCAT(IObuff, ")");
1055 return IObuff;
1056 }
1057
1058 return NULL;
1059}
1060
1061/*
1062 * Function given to ExpandGeneric() to obtain the list of internal or
1063 * user defined variable or function names.
1064 */
1065 char_u *
1066get_expr_name(expand_T *xp, int idx)
1067{
1068 static int intidx = -1;
1069 char_u *name;
1070
1071 if (idx == 0)
1072 intidx = -1;
1073 if (intidx < 0)
1074 {
1075 name = get_function_name(xp, idx);
1076 if (name != NULL)
1077 return name;
1078 }
1079 return get_user_var_name(xp, ++intidx);
1080}
1081
1082#endif /* FEAT_CMDL_COMPL */
1083
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001084/*
1085 * Find internal function in table above.
1086 * Return index, or -1 if not found
1087 */
1088 int
1089find_internal_func(
1090 char_u *name) /* name of the function */
1091{
1092 int first = 0;
1093 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1094 int cmp;
1095 int x;
1096
1097 /*
1098 * Find the function name in the table. Binary search.
1099 */
1100 while (first <= last)
1101 {
1102 x = first + ((unsigned)(last - first) >> 1);
1103 cmp = STRCMP(name, functions[x].f_name);
1104 if (cmp < 0)
1105 last = x - 1;
1106 else if (cmp > 0)
1107 first = x + 1;
1108 else
1109 return x;
1110 }
1111 return -1;
1112}
1113
1114 int
1115call_internal_func(
1116 char_u *name,
1117 int argcount,
1118 typval_T *argvars,
1119 typval_T *rettv)
1120{
1121 int i;
1122
1123 i = find_internal_func(name);
1124 if (i < 0)
1125 return ERROR_UNKNOWN;
1126 if (argcount < functions[i].f_min_argc)
1127 return ERROR_TOOFEW;
1128 if (argcount > functions[i].f_max_argc)
1129 return ERROR_TOOMANY;
1130 argvars[argcount].v_type = VAR_UNKNOWN;
1131 functions[i].f_func(argvars, rettv);
1132 return ERROR_NONE;
1133}
1134
1135/*
1136 * Return TRUE for a non-zero Number and a non-empty String.
1137 */
1138 static int
1139non_zero_arg(typval_T *argvars)
1140{
1141 return ((argvars[0].v_type == VAR_NUMBER
1142 && argvars[0].vval.v_number != 0)
1143 || (argvars[0].v_type == VAR_SPECIAL
1144 && argvars[0].vval.v_number == VVAL_TRUE)
1145 || (argvars[0].v_type == VAR_STRING
1146 && argvars[0].vval.v_string != NULL
1147 && *argvars[0].vval.v_string != NUL));
1148}
1149
1150/*
1151 * Get the lnum from the first argument.
1152 * Also accepts ".", "$", etc., but that only works for the current buffer.
1153 * Returns -1 on error.
1154 */
1155 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001156tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001157{
1158 typval_T rettv;
1159 linenr_T lnum;
1160
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001161 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001162 if (lnum == 0) /* no valid number, try using line() */
1163 {
1164 rettv.v_type = VAR_NUMBER;
1165 f_line(argvars, &rettv);
1166 lnum = (linenr_T)rettv.vval.v_number;
1167 clear_tv(&rettv);
1168 }
1169 return lnum;
1170}
1171
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001172/*
1173 * Get the lnum from the first argument.
1174 * Also accepts "$", then "buf" is used.
1175 * Returns 0 on error.
1176 */
1177 static linenr_T
1178tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1179{
1180 if (argvars[0].v_type == VAR_STRING
1181 && argvars[0].vval.v_string != NULL
1182 && argvars[0].vval.v_string[0] == '$'
1183 && buf != NULL)
1184 return buf->b_ml.ml_line_count;
1185 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1186}
1187
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001188#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001189/*
1190 * Get the float value of "argvars[0]" into "f".
1191 * Returns FAIL when the argument is not a Number or Float.
1192 */
1193 static int
1194get_float_arg(typval_T *argvars, float_T *f)
1195{
1196 if (argvars[0].v_type == VAR_FLOAT)
1197 {
1198 *f = argvars[0].vval.v_float;
1199 return OK;
1200 }
1201 if (argvars[0].v_type == VAR_NUMBER)
1202 {
1203 *f = (float_T)argvars[0].vval.v_number;
1204 return OK;
1205 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001206 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001207 return FAIL;
1208}
1209
1210/*
1211 * "abs(expr)" function
1212 */
1213 static void
1214f_abs(typval_T *argvars, typval_T *rettv)
1215{
1216 if (argvars[0].v_type == VAR_FLOAT)
1217 {
1218 rettv->v_type = VAR_FLOAT;
1219 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1220 }
1221 else
1222 {
1223 varnumber_T n;
1224 int error = FALSE;
1225
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001226 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001227 if (error)
1228 rettv->vval.v_number = -1;
1229 else if (n > 0)
1230 rettv->vval.v_number = n;
1231 else
1232 rettv->vval.v_number = -n;
1233 }
1234}
1235
1236/*
1237 * "acos()" function
1238 */
1239 static void
1240f_acos(typval_T *argvars, typval_T *rettv)
1241{
1242 float_T f = 0.0;
1243
1244 rettv->v_type = VAR_FLOAT;
1245 if (get_float_arg(argvars, &f) == OK)
1246 rettv->vval.v_float = acos(f);
1247 else
1248 rettv->vval.v_float = 0.0;
1249}
1250#endif
1251
1252/*
1253 * "add(list, item)" function
1254 */
1255 static void
1256f_add(typval_T *argvars, typval_T *rettv)
1257{
1258 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001259 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001260
1261 rettv->vval.v_number = 1; /* Default: Failed */
1262 if (argvars[0].v_type == VAR_LIST)
1263 {
1264 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001265 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001266 (char_u *)N_("add() argument"), TRUE)
1267 && list_append_tv(l, &argvars[1]) == OK)
1268 copy_tv(&argvars[0], rettv);
1269 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001270 else if (argvars[0].v_type == VAR_BLOB)
1271 {
1272 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001273 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001274 (char_u *)N_("add() argument"), TRUE))
1275 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001276 int error = FALSE;
1277 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1278
1279 if (!error)
1280 {
1281 ga_append(&b->bv_ga, (int)n);
1282 copy_tv(&argvars[0], rettv);
1283 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001284 }
1285 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001286 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001287 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001288}
1289
1290/*
1291 * "and(expr, expr)" function
1292 */
1293 static void
1294f_and(typval_T *argvars, typval_T *rettv)
1295{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001296 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1297 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001298}
1299
1300/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001301 * If there is a window for "curbuf", make it the current window.
1302 */
1303 static void
1304find_win_for_curbuf(void)
1305{
1306 wininfo_T *wip;
1307
1308 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1309 {
1310 if (wip->wi_win != NULL)
1311 {
1312 curwin = wip->wi_win;
1313 break;
1314 }
1315 }
1316}
1317
1318/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001319 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001320 */
1321 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001322set_buffer_lines(
1323 buf_T *buf,
1324 linenr_T lnum_arg,
1325 int append,
1326 typval_T *lines,
1327 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001328{
Bram Moolenaarca851592018-06-06 21:04:07 +02001329 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1330 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001331 list_T *l = NULL;
1332 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001333 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001334 linenr_T append_lnum;
1335 buf_T *curbuf_save = NULL;
1336 win_T *curwin_save = NULL;
1337 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001338
Bram Moolenaarca851592018-06-06 21:04:07 +02001339 /* When using the current buffer ml_mfp will be set if needed. Useful when
1340 * setline() is used on startup. For other buffers the buffer must be
1341 * loaded. */
1342 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001343 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001344 rettv->vval.v_number = 1; /* FAIL */
1345 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001346 }
1347
Bram Moolenaarca851592018-06-06 21:04:07 +02001348 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001349 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001350 curbuf_save = curbuf;
1351 curwin_save = curwin;
1352 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001353 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001354 }
1355
1356 if (append)
1357 // appendbufline() uses the line number below which we insert
1358 append_lnum = lnum - 1;
1359 else
1360 // setbufline() uses the line number above which we insert, we only
1361 // append if it's below the last line
1362 append_lnum = curbuf->b_ml.ml_line_count;
1363
1364 if (lines->v_type == VAR_LIST)
1365 {
1366 l = lines->vval.v_list;
1367 li = l->lv_first;
1368 }
1369 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001370 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001371
1372 /* default result is zero == OK */
1373 for (;;)
1374 {
1375 if (l != NULL)
1376 {
1377 /* list argument, get next string */
1378 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001379 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001380 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001381 li = li->li_next;
1382 }
1383
Bram Moolenaarca851592018-06-06 21:04:07 +02001384 rettv->vval.v_number = 1; /* FAIL */
1385 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1386 break;
1387
1388 /* When coming here from Insert mode, sync undo, so that this can be
1389 * undone separately from what was previously inserted. */
1390 if (u_sync_once == 2)
1391 {
1392 u_sync_once = 1; /* notify that u_sync() was called */
1393 u_sync(TRUE);
1394 }
1395
1396 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1397 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001398 // Existing line, replace it.
1399 // Removes any existing text properties.
1400 if (u_savesub(lnum) == OK && ml_replace_len(
1401 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001402 {
1403 changed_bytes(lnum, 0);
1404 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1405 check_cursor_col();
1406 rettv->vval.v_number = 0; /* OK */
1407 }
1408 }
1409 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1410 {
1411 /* append the line */
1412 ++added;
1413 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1414 rettv->vval.v_number = 0; /* OK */
1415 }
1416
1417 if (l == NULL) /* only one string argument */
1418 break;
1419 ++lnum;
1420 }
1421
1422 if (added > 0)
1423 {
1424 win_T *wp;
1425 tabpage_T *tp;
1426
1427 appended_lines_mark(append_lnum, added);
1428 FOR_ALL_TAB_WINDOWS(tp, wp)
1429 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1430 wp->w_cursor.lnum += added;
1431 check_cursor_col();
1432
Bram Moolenaarf2732452018-06-03 14:47:35 +02001433#ifdef FEAT_JOB_CHANNEL
1434 if (bt_prompt(curbuf) && (State & INSERT))
1435 // show the line with the prompt
1436 update_topline();
1437#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001438 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001439
1440 if (!is_curbuf)
1441 {
1442 curbuf = curbuf_save;
1443 curwin = curwin_save;
1444 }
1445}
1446
1447/*
1448 * "append(lnum, string/list)" function
1449 */
1450 static void
1451f_append(typval_T *argvars, typval_T *rettv)
1452{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001453 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001454
1455 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1456}
1457
1458/*
1459 * "appendbufline(buf, lnum, string/list)" function
1460 */
1461 static void
1462f_appendbufline(typval_T *argvars, typval_T *rettv)
1463{
1464 linenr_T lnum;
1465 buf_T *buf;
1466
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001467 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001468 if (buf == NULL)
1469 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001470 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001471 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001472 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001473 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1474 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001475}
1476
1477/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001478 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001479 */
1480 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001481f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001482{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001483 win_T *wp;
1484
1485 if (argvars[0].v_type == VAR_UNKNOWN)
1486 // use the current window
1487 rettv->vval.v_number = ARGCOUNT;
1488 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001489 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001490 // use the global argument list
1491 rettv->vval.v_number = GARGCOUNT;
1492 else
1493 {
1494 // use the argument list of the specified window
1495 wp = find_win_by_nr_or_id(&argvars[0]);
1496 if (wp != NULL)
1497 rettv->vval.v_number = WARGCOUNT(wp);
1498 else
1499 rettv->vval.v_number = -1;
1500 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001501}
1502
1503/*
1504 * "argidx()" function
1505 */
1506 static void
1507f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1508{
1509 rettv->vval.v_number = curwin->w_arg_idx;
1510}
1511
1512/*
1513 * "arglistid()" function
1514 */
1515 static void
1516f_arglistid(typval_T *argvars, typval_T *rettv)
1517{
1518 win_T *wp;
1519
1520 rettv->vval.v_number = -1;
1521 wp = find_tabwin(&argvars[0], &argvars[1]);
1522 if (wp != NULL)
1523 rettv->vval.v_number = wp->w_alist->id;
1524}
1525
1526/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001527 * Get the argument list for a given window
1528 */
1529 static void
1530get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1531{
1532 int idx;
1533
1534 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1535 for (idx = 0; idx < argcount; ++idx)
1536 list_append_string(rettv->vval.v_list,
1537 alist_name(&arglist[idx]), -1);
1538}
1539
1540/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001541 * "argv(nr)" function
1542 */
1543 static void
1544f_argv(typval_T *argvars, typval_T *rettv)
1545{
1546 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001547 aentry_T *arglist = NULL;
1548 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001549
1550 if (argvars[0].v_type != VAR_UNKNOWN)
1551 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001552 if (argvars[1].v_type == VAR_UNKNOWN)
1553 {
1554 arglist = ARGLIST;
1555 argcount = ARGCOUNT;
1556 }
1557 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001558 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001559 {
1560 arglist = GARGLIST;
1561 argcount = GARGCOUNT;
1562 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001563 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001564 {
1565 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1566
1567 if (wp != NULL)
1568 {
1569 /* Use the argument list of the specified window */
1570 arglist = WARGLIST(wp);
1571 argcount = WARGCOUNT(wp);
1572 }
1573 }
1574
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001575 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001576 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001577 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001578 if (arglist != NULL && idx >= 0 && idx < argcount)
1579 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1580 else if (idx == -1)
1581 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001582 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001583 else
1584 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001585}
1586
1587/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001588 * "assert_beeps(cmd [, error])" function
1589 */
1590 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001591f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001592{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001593 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001594}
1595
1596/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001597 * "assert_equal(expected, actual[, msg])" function
1598 */
1599 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001600f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001601{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001602 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001603}
1604
1605/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001606 * "assert_equalfile(fname-one, fname-two)" function
1607 */
1608 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001609f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001610{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001611 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001612}
1613
1614/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001615 * "assert_notequal(expected, actual[, msg])" function
1616 */
1617 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001618f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001619{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001620 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001621}
1622
1623/*
1624 * "assert_exception(string[, msg])" function
1625 */
1626 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001627f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001628{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001629 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001630}
1631
1632/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001633 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001634 */
1635 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001636f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001637{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001638 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001639}
1640
1641/*
1642 * "assert_false(actual[, msg])" function
1643 */
1644 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001645f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001646{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001647 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001648}
1649
1650/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001651 * "assert_inrange(lower, upper[, msg])" function
1652 */
1653 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001654f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001655{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001656 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001657}
1658
1659/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001660 * "assert_match(pattern, actual[, msg])" function
1661 */
1662 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001663f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001664{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001665 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001666}
1667
1668/*
1669 * "assert_notmatch(pattern, actual[, msg])" function
1670 */
1671 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001672f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001673{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001674 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001675}
1676
1677/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001678 * "assert_report(msg)" function
1679 */
1680 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001681f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001682{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001683 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001684}
1685
1686/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001687 * "assert_true(actual[, msg])" function
1688 */
1689 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001690f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001691{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001692 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001693}
1694
1695#ifdef FEAT_FLOAT
1696/*
1697 * "asin()" function
1698 */
1699 static void
1700f_asin(typval_T *argvars, typval_T *rettv)
1701{
1702 float_T f = 0.0;
1703
1704 rettv->v_type = VAR_FLOAT;
1705 if (get_float_arg(argvars, &f) == OK)
1706 rettv->vval.v_float = asin(f);
1707 else
1708 rettv->vval.v_float = 0.0;
1709}
1710
1711/*
1712 * "atan()" function
1713 */
1714 static void
1715f_atan(typval_T *argvars, typval_T *rettv)
1716{
1717 float_T f = 0.0;
1718
1719 rettv->v_type = VAR_FLOAT;
1720 if (get_float_arg(argvars, &f) == OK)
1721 rettv->vval.v_float = atan(f);
1722 else
1723 rettv->vval.v_float = 0.0;
1724}
1725
1726/*
1727 * "atan2()" function
1728 */
1729 static void
1730f_atan2(typval_T *argvars, typval_T *rettv)
1731{
1732 float_T fx = 0.0, fy = 0.0;
1733
1734 rettv->v_type = VAR_FLOAT;
1735 if (get_float_arg(argvars, &fx) == OK
1736 && get_float_arg(&argvars[1], &fy) == OK)
1737 rettv->vval.v_float = atan2(fx, fy);
1738 else
1739 rettv->vval.v_float = 0.0;
1740}
1741#endif
1742
1743/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001744 * "balloon_show()" function
1745 */
1746#ifdef FEAT_BEVAL
1747 static void
1748f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1749{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001750 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001751 {
1752 if (argvars[0].v_type == VAR_LIST
1753# ifdef FEAT_GUI
1754 && !gui.in_use
1755# endif
1756 )
1757 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1758 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001759 post_balloon(balloonEval, tv_get_string_chk(&argvars[0]), NULL);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001760 }
1761}
1762
Bram Moolenaar669a8282017-11-19 20:13:05 +01001763# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001764 static void
1765f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1766{
1767 if (rettv_list_alloc(rettv) == OK)
1768 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001769 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001770
1771 if (msg != NULL)
1772 {
1773 pumitem_T *array;
1774 int size = split_message(msg, &array);
1775 int i;
1776
1777 /* Skip the first and last item, they are always empty. */
1778 for (i = 1; i < size - 1; ++i)
1779 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001780 while (size > 0)
1781 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001782 vim_free(array);
1783 }
1784 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001785}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001786# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001787#endif
1788
1789/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001790 * "browse(save, title, initdir, default)" function
1791 */
1792 static void
1793f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1794{
1795#ifdef FEAT_BROWSE
1796 int save;
1797 char_u *title;
1798 char_u *initdir;
1799 char_u *defname;
1800 char_u buf[NUMBUFLEN];
1801 char_u buf2[NUMBUFLEN];
1802 int error = FALSE;
1803
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001804 save = (int)tv_get_number_chk(&argvars[0], &error);
1805 title = tv_get_string_chk(&argvars[1]);
1806 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1807 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001808
1809 if (error || title == NULL || initdir == NULL || defname == NULL)
1810 rettv->vval.v_string = NULL;
1811 else
1812 rettv->vval.v_string =
1813 do_browse(save ? BROWSE_SAVE : 0,
1814 title, defname, NULL, initdir, NULL, curbuf);
1815#else
1816 rettv->vval.v_string = NULL;
1817#endif
1818 rettv->v_type = VAR_STRING;
1819}
1820
1821/*
1822 * "browsedir(title, initdir)" function
1823 */
1824 static void
1825f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1826{
1827#ifdef FEAT_BROWSE
1828 char_u *title;
1829 char_u *initdir;
1830 char_u buf[NUMBUFLEN];
1831
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001832 title = tv_get_string_chk(&argvars[0]);
1833 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001834
1835 if (title == NULL || initdir == NULL)
1836 rettv->vval.v_string = NULL;
1837 else
1838 rettv->vval.v_string = do_browse(BROWSE_DIR,
1839 title, NULL, NULL, initdir, NULL, curbuf);
1840#else
1841 rettv->vval.v_string = NULL;
1842#endif
1843 rettv->v_type = VAR_STRING;
1844}
1845
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001846/*
1847 * Find a buffer by number or exact name.
1848 */
1849 static buf_T *
1850find_buffer(typval_T *avar)
1851{
1852 buf_T *buf = NULL;
1853
1854 if (avar->v_type == VAR_NUMBER)
1855 buf = buflist_findnr((int)avar->vval.v_number);
1856 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1857 {
1858 buf = buflist_findname_exp(avar->vval.v_string);
1859 if (buf == NULL)
1860 {
1861 /* No full path name match, try a match with a URL or a "nofile"
1862 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001863 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001864 if (buf->b_fname != NULL
1865 && (path_with_url(buf->b_fname)
1866#ifdef FEAT_QUICKFIX
1867 || bt_nofile(buf)
1868#endif
1869 )
1870 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1871 break;
1872 }
1873 }
1874 return buf;
1875}
1876
1877/*
1878 * "bufexists(expr)" function
1879 */
1880 static void
1881f_bufexists(typval_T *argvars, typval_T *rettv)
1882{
1883 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1884}
1885
1886/*
1887 * "buflisted(expr)" function
1888 */
1889 static void
1890f_buflisted(typval_T *argvars, typval_T *rettv)
1891{
1892 buf_T *buf;
1893
1894 buf = find_buffer(&argvars[0]);
1895 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1896}
1897
1898/*
1899 * "bufloaded(expr)" function
1900 */
1901 static void
1902f_bufloaded(typval_T *argvars, typval_T *rettv)
1903{
1904 buf_T *buf;
1905
1906 buf = find_buffer(&argvars[0]);
1907 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1908}
1909
1910 buf_T *
1911buflist_find_by_name(char_u *name, int curtab_only)
1912{
1913 int save_magic;
1914 char_u *save_cpo;
1915 buf_T *buf;
1916
1917 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1918 save_magic = p_magic;
1919 p_magic = TRUE;
1920 save_cpo = p_cpo;
1921 p_cpo = (char_u *)"";
1922
1923 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1924 TRUE, FALSE, curtab_only));
1925
1926 p_magic = save_magic;
1927 p_cpo = save_cpo;
1928 return buf;
1929}
1930
1931/*
1932 * Get buffer by number or pattern.
1933 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001934 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001935tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001936{
1937 char_u *name = tv->vval.v_string;
1938 buf_T *buf;
1939
1940 if (tv->v_type == VAR_NUMBER)
1941 return buflist_findnr((int)tv->vval.v_number);
1942 if (tv->v_type != VAR_STRING)
1943 return NULL;
1944 if (name == NULL || *name == NUL)
1945 return curbuf;
1946 if (name[0] == '$' && name[1] == NUL)
1947 return lastbuf;
1948
1949 buf = buflist_find_by_name(name, curtab_only);
1950
1951 /* If not found, try expanding the name, like done for bufexists(). */
1952 if (buf == NULL)
1953 buf = find_buffer(tv);
1954
1955 return buf;
1956}
1957
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001958#ifdef FEAT_SIGNS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001959/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001960 * Get the buffer from "arg" and give an error and return NULL if it is not
1961 * valid.
1962 */
1963 static buf_T *
1964get_buf_arg(typval_T *arg)
1965{
1966 buf_T *buf;
1967
1968 ++emsg_off;
1969 buf = tv_get_buf(arg, FALSE);
1970 --emsg_off;
1971 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001972 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001973 return buf;
1974}
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001975#endif
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001976
1977/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001978 * "bufname(expr)" function
1979 */
1980 static void
1981f_bufname(typval_T *argvars, typval_T *rettv)
1982{
1983 buf_T *buf;
1984
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001985 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001986 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001987 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001988 rettv->v_type = VAR_STRING;
1989 if (buf != NULL && buf->b_fname != NULL)
1990 rettv->vval.v_string = vim_strsave(buf->b_fname);
1991 else
1992 rettv->vval.v_string = NULL;
1993 --emsg_off;
1994}
1995
1996/*
1997 * "bufnr(expr)" function
1998 */
1999 static void
2000f_bufnr(typval_T *argvars, typval_T *rettv)
2001{
2002 buf_T *buf;
2003 int error = FALSE;
2004 char_u *name;
2005
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002006 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002007 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002008 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002009 --emsg_off;
2010
2011 /* If the buffer isn't found and the second argument is not zero create a
2012 * new buffer. */
2013 if (buf == NULL
2014 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002015 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002016 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002017 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018 && !error)
2019 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2020
2021 if (buf != NULL)
2022 rettv->vval.v_number = buf->b_fnum;
2023 else
2024 rettv->vval.v_number = -1;
2025}
2026
2027 static void
2028buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2029{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002030 win_T *wp;
2031 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002032 buf_T *buf;
2033
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002034 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002035 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002036 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002037 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002038 {
2039 ++winnr;
2040 if (wp->w_buffer == buf)
2041 break;
2042 }
2043 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002044 --emsg_off;
2045}
2046
2047/*
2048 * "bufwinid(nr)" function
2049 */
2050 static void
2051f_bufwinid(typval_T *argvars, typval_T *rettv)
2052{
2053 buf_win_common(argvars, rettv, FALSE);
2054}
2055
2056/*
2057 * "bufwinnr(nr)" function
2058 */
2059 static void
2060f_bufwinnr(typval_T *argvars, typval_T *rettv)
2061{
2062 buf_win_common(argvars, rettv, TRUE);
2063}
2064
2065/*
2066 * "byte2line(byte)" function
2067 */
2068 static void
2069f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2070{
2071#ifndef FEAT_BYTEOFF
2072 rettv->vval.v_number = -1;
2073#else
2074 long boff = 0;
2075
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002076 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002077 if (boff < 0)
2078 rettv->vval.v_number = -1;
2079 else
2080 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2081 (linenr_T)0, &boff);
2082#endif
2083}
2084
2085 static void
2086byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2087{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002088 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002089 char_u *str;
2090 varnumber_T idx;
2091
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002092 str = tv_get_string_chk(&argvars[0]);
2093 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002094 rettv->vval.v_number = -1;
2095 if (str == NULL || idx < 0)
2096 return;
2097
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002098 t = str;
2099 for ( ; idx > 0; idx--)
2100 {
2101 if (*t == NUL) /* EOL reached */
2102 return;
2103 if (enc_utf8 && comp)
2104 t += utf_ptr2len(t);
2105 else
2106 t += (*mb_ptr2len)(t);
2107 }
2108 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002109}
2110
2111/*
2112 * "byteidx()" function
2113 */
2114 static void
2115f_byteidx(typval_T *argvars, typval_T *rettv)
2116{
2117 byteidx(argvars, rettv, FALSE);
2118}
2119
2120/*
2121 * "byteidxcomp()" function
2122 */
2123 static void
2124f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2125{
2126 byteidx(argvars, rettv, TRUE);
2127}
2128
2129/*
2130 * "call(func, arglist [, dict])" function
2131 */
2132 static void
2133f_call(typval_T *argvars, typval_T *rettv)
2134{
2135 char_u *func;
2136 partial_T *partial = NULL;
2137 dict_T *selfdict = NULL;
2138
2139 if (argvars[1].v_type != VAR_LIST)
2140 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002141 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002142 return;
2143 }
2144 if (argvars[1].vval.v_list == NULL)
2145 return;
2146
2147 if (argvars[0].v_type == VAR_FUNC)
2148 func = argvars[0].vval.v_string;
2149 else if (argvars[0].v_type == VAR_PARTIAL)
2150 {
2151 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002152 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002153 }
2154 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002155 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002156 if (*func == NUL)
2157 return; /* type error or empty name */
2158
2159 if (argvars[2].v_type != VAR_UNKNOWN)
2160 {
2161 if (argvars[2].v_type != VAR_DICT)
2162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002163 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164 return;
2165 }
2166 selfdict = argvars[2].vval.v_dict;
2167 }
2168
2169 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2170}
2171
2172#ifdef FEAT_FLOAT
2173/*
2174 * "ceil({float})" function
2175 */
2176 static void
2177f_ceil(typval_T *argvars, typval_T *rettv)
2178{
2179 float_T f = 0.0;
2180
2181 rettv->v_type = VAR_FLOAT;
2182 if (get_float_arg(argvars, &f) == OK)
2183 rettv->vval.v_float = ceil(f);
2184 else
2185 rettv->vval.v_float = 0.0;
2186}
2187#endif
2188
2189#ifdef FEAT_JOB_CHANNEL
2190/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002191 * "ch_canread()" function
2192 */
2193 static void
2194f_ch_canread(typval_T *argvars, typval_T *rettv)
2195{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002196 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002197
2198 rettv->vval.v_number = 0;
2199 if (channel != NULL)
2200 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2201 || channel_has_readahead(channel, PART_OUT)
2202 || channel_has_readahead(channel, PART_ERR);
2203}
2204
2205/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002206 * "ch_close()" function
2207 */
2208 static void
2209f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2210{
2211 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2212
2213 if (channel != NULL)
2214 {
2215 channel_close(channel, FALSE);
2216 channel_clear(channel);
2217 }
2218}
2219
2220/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002221 * "ch_close()" function
2222 */
2223 static void
2224f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2225{
2226 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2227
2228 if (channel != NULL)
2229 channel_close_in(channel);
2230}
2231
2232/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002233 * "ch_getbufnr()" function
2234 */
2235 static void
2236f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2237{
2238 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2239
2240 rettv->vval.v_number = -1;
2241 if (channel != NULL)
2242 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002243 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002244 int part;
2245
2246 if (STRCMP(what, "err") == 0)
2247 part = PART_ERR;
2248 else if (STRCMP(what, "out") == 0)
2249 part = PART_OUT;
2250 else if (STRCMP(what, "in") == 0)
2251 part = PART_IN;
2252 else
2253 part = PART_SOCK;
2254 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2255 rettv->vval.v_number =
2256 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2257 }
2258}
2259
2260/*
2261 * "ch_getjob()" function
2262 */
2263 static void
2264f_ch_getjob(typval_T *argvars, typval_T *rettv)
2265{
2266 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2267
2268 if (channel != NULL)
2269 {
2270 rettv->v_type = VAR_JOB;
2271 rettv->vval.v_job = channel->ch_job;
2272 if (channel->ch_job != NULL)
2273 ++channel->ch_job->jv_refcount;
2274 }
2275}
2276
2277/*
2278 * "ch_info()" function
2279 */
2280 static void
2281f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2282{
2283 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2284
2285 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2286 channel_info(channel, rettv->vval.v_dict);
2287}
2288
2289/*
2290 * "ch_log()" function
2291 */
2292 static void
2293f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2294{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002295 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002296 channel_T *channel = NULL;
2297
2298 if (argvars[1].v_type != VAR_UNKNOWN)
2299 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2300
Bram Moolenaard5359b22018-04-05 22:44:39 +02002301 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002302}
2303
2304/*
2305 * "ch_logfile()" function
2306 */
2307 static void
2308f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2309{
2310 char_u *fname;
2311 char_u *opt = (char_u *)"";
2312 char_u buf[NUMBUFLEN];
2313
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002314 /* Don't open a file in restricted mode. */
2315 if (check_restricted() || check_secure())
2316 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002317 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002318 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002319 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002320 ch_logfile(fname, opt);
2321}
2322
2323/*
2324 * "ch_open()" function
2325 */
2326 static void
2327f_ch_open(typval_T *argvars, typval_T *rettv)
2328{
2329 rettv->v_type = VAR_CHANNEL;
2330 if (check_restricted() || check_secure())
2331 return;
2332 rettv->vval.v_channel = channel_open_func(argvars);
2333}
2334
2335/*
2336 * "ch_read()" function
2337 */
2338 static void
2339f_ch_read(typval_T *argvars, typval_T *rettv)
2340{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002341 common_channel_read(argvars, rettv, FALSE, FALSE);
2342}
2343
2344/*
2345 * "ch_readblob()" function
2346 */
2347 static void
2348f_ch_readblob(typval_T *argvars, typval_T *rettv)
2349{
2350 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002351}
2352
2353/*
2354 * "ch_readraw()" function
2355 */
2356 static void
2357f_ch_readraw(typval_T *argvars, typval_T *rettv)
2358{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002359 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002360}
2361
2362/*
2363 * "ch_evalexpr()" function
2364 */
2365 static void
2366f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2367{
2368 ch_expr_common(argvars, rettv, TRUE);
2369}
2370
2371/*
2372 * "ch_sendexpr()" function
2373 */
2374 static void
2375f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2376{
2377 ch_expr_common(argvars, rettv, FALSE);
2378}
2379
2380/*
2381 * "ch_evalraw()" function
2382 */
2383 static void
2384f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2385{
2386 ch_raw_common(argvars, rettv, TRUE);
2387}
2388
2389/*
2390 * "ch_sendraw()" function
2391 */
2392 static void
2393f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2394{
2395 ch_raw_common(argvars, rettv, FALSE);
2396}
2397
2398/*
2399 * "ch_setoptions()" function
2400 */
2401 static void
2402f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2403{
2404 channel_T *channel;
2405 jobopt_T opt;
2406
2407 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2408 if (channel == NULL)
2409 return;
2410 clear_job_options(&opt);
2411 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002412 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002413 channel_set_options(channel, &opt);
2414 free_job_options(&opt);
2415}
2416
2417/*
2418 * "ch_status()" function
2419 */
2420 static void
2421f_ch_status(typval_T *argvars, typval_T *rettv)
2422{
2423 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002424 jobopt_T opt;
2425 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002426
2427 /* return an empty string by default */
2428 rettv->v_type = VAR_STRING;
2429 rettv->vval.v_string = NULL;
2430
2431 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002432
2433 if (argvars[1].v_type != VAR_UNKNOWN)
2434 {
2435 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002436 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002437 && (opt.jo_set & JO_PART))
2438 part = opt.jo_part;
2439 }
2440
2441 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002442}
2443#endif
2444
2445/*
2446 * "changenr()" function
2447 */
2448 static void
2449f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2450{
2451 rettv->vval.v_number = curbuf->b_u_seq_cur;
2452}
2453
2454/*
2455 * "char2nr(string)" function
2456 */
2457 static void
2458f_char2nr(typval_T *argvars, typval_T *rettv)
2459{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002460 if (has_mbyte)
2461 {
2462 int utf8 = 0;
2463
2464 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002465 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002466
2467 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002468 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002469 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002470 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002471 }
2472 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002473 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002474}
2475
2476/*
2477 * "cindent(lnum)" function
2478 */
2479 static void
2480f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2481{
2482#ifdef FEAT_CINDENT
2483 pos_T pos;
2484 linenr_T lnum;
2485
2486 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002487 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002488 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2489 {
2490 curwin->w_cursor.lnum = lnum;
2491 rettv->vval.v_number = get_c_indent();
2492 curwin->w_cursor = pos;
2493 }
2494 else
2495#endif
2496 rettv->vval.v_number = -1;
2497}
2498
2499/*
2500 * "clearmatches()" function
2501 */
2502 static void
2503f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2504{
2505#ifdef FEAT_SEARCH_EXTRA
2506 clear_matches(curwin);
2507#endif
2508}
2509
2510/*
2511 * "col(string)" function
2512 */
2513 static void
2514f_col(typval_T *argvars, typval_T *rettv)
2515{
2516 colnr_T col = 0;
2517 pos_T *fp;
2518 int fnum = curbuf->b_fnum;
2519
2520 fp = var2fpos(&argvars[0], FALSE, &fnum);
2521 if (fp != NULL && fnum == curbuf->b_fnum)
2522 {
2523 if (fp->col == MAXCOL)
2524 {
2525 /* '> can be MAXCOL, get the length of the line then */
2526 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2527 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2528 else
2529 col = MAXCOL;
2530 }
2531 else
2532 {
2533 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002534 /* col(".") when the cursor is on the NUL at the end of the line
2535 * because of "coladd" can be seen as an extra column. */
2536 if (virtual_active() && fp == &curwin->w_cursor)
2537 {
2538 char_u *p = ml_get_cursor();
2539
2540 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2541 curwin->w_virtcol - curwin->w_cursor.coladd))
2542 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002543 int l;
2544
2545 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2546 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002547 }
2548 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002549 }
2550 }
2551 rettv->vval.v_number = col;
2552}
2553
2554#if defined(FEAT_INS_EXPAND)
2555/*
2556 * "complete()" function
2557 */
2558 static void
2559f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2560{
2561 int startcol;
2562
2563 if ((State & INSERT) == 0)
2564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002565 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002566 return;
2567 }
2568
2569 /* Check for undo allowed here, because if something was already inserted
2570 * the line was already saved for undo and this check isn't done. */
2571 if (!undo_allowed())
2572 return;
2573
2574 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002576 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002577 return;
2578 }
2579
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002580 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002581 if (startcol <= 0)
2582 return;
2583
2584 set_completion(startcol - 1, argvars[1].vval.v_list);
2585}
2586
2587/*
2588 * "complete_add()" function
2589 */
2590 static void
2591f_complete_add(typval_T *argvars, typval_T *rettv)
2592{
2593 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2594}
2595
2596/*
2597 * "complete_check()" function
2598 */
2599 static void
2600f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2601{
2602 int saved = RedrawingDisabled;
2603
2604 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002605 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002606 rettv->vval.v_number = compl_interrupted;
2607 RedrawingDisabled = saved;
2608}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002609
2610/*
2611 * "complete_info()" function
2612 */
2613 static void
2614f_complete_info(typval_T *argvars, typval_T *rettv)
2615{
2616 list_T *what_list = NULL;
2617
2618 if (rettv_dict_alloc(rettv) != OK)
2619 return;
2620
2621 if (argvars[0].v_type != VAR_UNKNOWN)
2622 {
2623 if (argvars[0].v_type != VAR_LIST)
2624 {
2625 emsg(_(e_listreq));
2626 return;
2627 }
2628 what_list = argvars[0].vval.v_list;
2629 }
2630 get_complete_info(what_list, rettv->vval.v_dict);
2631}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002632#endif
2633
2634/*
2635 * "confirm(message, buttons[, default [, type]])" function
2636 */
2637 static void
2638f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2639{
2640#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2641 char_u *message;
2642 char_u *buttons = NULL;
2643 char_u buf[NUMBUFLEN];
2644 char_u buf2[NUMBUFLEN];
2645 int def = 1;
2646 int type = VIM_GENERIC;
2647 char_u *typestr;
2648 int error = FALSE;
2649
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002650 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002651 if (message == NULL)
2652 error = TRUE;
2653 if (argvars[1].v_type != VAR_UNKNOWN)
2654 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002655 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002656 if (buttons == NULL)
2657 error = TRUE;
2658 if (argvars[2].v_type != VAR_UNKNOWN)
2659 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002660 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002661 if (argvars[3].v_type != VAR_UNKNOWN)
2662 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002663 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002664 if (typestr == NULL)
2665 error = TRUE;
2666 else
2667 {
2668 switch (TOUPPER_ASC(*typestr))
2669 {
2670 case 'E': type = VIM_ERROR; break;
2671 case 'Q': type = VIM_QUESTION; break;
2672 case 'I': type = VIM_INFO; break;
2673 case 'W': type = VIM_WARNING; break;
2674 case 'G': type = VIM_GENERIC; break;
2675 }
2676 }
2677 }
2678 }
2679 }
2680
2681 if (buttons == NULL || *buttons == NUL)
2682 buttons = (char_u *)_("&Ok");
2683
2684 if (!error)
2685 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2686 def, NULL, FALSE);
2687#endif
2688}
2689
2690/*
2691 * "copy()" function
2692 */
2693 static void
2694f_copy(typval_T *argvars, typval_T *rettv)
2695{
2696 item_copy(&argvars[0], rettv, FALSE, 0);
2697}
2698
2699#ifdef FEAT_FLOAT
2700/*
2701 * "cos()" function
2702 */
2703 static void
2704f_cos(typval_T *argvars, typval_T *rettv)
2705{
2706 float_T f = 0.0;
2707
2708 rettv->v_type = VAR_FLOAT;
2709 if (get_float_arg(argvars, &f) == OK)
2710 rettv->vval.v_float = cos(f);
2711 else
2712 rettv->vval.v_float = 0.0;
2713}
2714
2715/*
2716 * "cosh()" function
2717 */
2718 static void
2719f_cosh(typval_T *argvars, typval_T *rettv)
2720{
2721 float_T f = 0.0;
2722
2723 rettv->v_type = VAR_FLOAT;
2724 if (get_float_arg(argvars, &f) == OK)
2725 rettv->vval.v_float = cosh(f);
2726 else
2727 rettv->vval.v_float = 0.0;
2728}
2729#endif
2730
2731/*
2732 * "count()" function
2733 */
2734 static void
2735f_count(typval_T *argvars, typval_T *rettv)
2736{
2737 long n = 0;
2738 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002739 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002740
Bram Moolenaar9966b212017-07-28 16:46:57 +02002741 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002742 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002743
2744 if (argvars[0].v_type == VAR_STRING)
2745 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002746 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002747 char_u *p = argvars[0].vval.v_string;
2748 char_u *next;
2749
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002750 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002751 {
2752 if (ic)
2753 {
2754 size_t len = STRLEN(expr);
2755
2756 while (*p != NUL)
2757 {
2758 if (MB_STRNICMP(p, expr, len) == 0)
2759 {
2760 ++n;
2761 p += len;
2762 }
2763 else
2764 MB_PTR_ADV(p);
2765 }
2766 }
2767 else
2768 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2769 != NULL)
2770 {
2771 ++n;
2772 p = next + STRLEN(expr);
2773 }
2774 }
2775
2776 }
2777 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 {
2779 listitem_T *li;
2780 list_T *l;
2781 long idx;
2782
2783 if ((l = argvars[0].vval.v_list) != NULL)
2784 {
2785 li = l->lv_first;
2786 if (argvars[2].v_type != VAR_UNKNOWN)
2787 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002788 if (argvars[3].v_type != VAR_UNKNOWN)
2789 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002790 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002791 if (!error)
2792 {
2793 li = list_find(l, idx);
2794 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002795 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002796 }
2797 }
2798 if (error)
2799 li = NULL;
2800 }
2801
2802 for ( ; li != NULL; li = li->li_next)
2803 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2804 ++n;
2805 }
2806 }
2807 else if (argvars[0].v_type == VAR_DICT)
2808 {
2809 int todo;
2810 dict_T *d;
2811 hashitem_T *hi;
2812
2813 if ((d = argvars[0].vval.v_dict) != NULL)
2814 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002815 if (argvars[2].v_type != VAR_UNKNOWN)
2816 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002817 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002818 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002819 }
2820
2821 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2822 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2823 {
2824 if (!HASHITEM_EMPTY(hi))
2825 {
2826 --todo;
2827 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2828 ++n;
2829 }
2830 }
2831 }
2832 }
2833 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002834 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002835 rettv->vval.v_number = n;
2836}
2837
2838/*
2839 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2840 *
2841 * Checks the existence of a cscope connection.
2842 */
2843 static void
2844f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2845{
2846#ifdef FEAT_CSCOPE
2847 int num = 0;
2848 char_u *dbpath = NULL;
2849 char_u *prepend = NULL;
2850 char_u buf[NUMBUFLEN];
2851
2852 if (argvars[0].v_type != VAR_UNKNOWN
2853 && argvars[1].v_type != VAR_UNKNOWN)
2854 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002855 num = (int)tv_get_number(&argvars[0]);
2856 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002857 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002858 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002859 }
2860
2861 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2862#endif
2863}
2864
2865/*
2866 * "cursor(lnum, col)" function, or
2867 * "cursor(list)"
2868 *
2869 * Moves the cursor to the specified line and column.
2870 * Returns 0 when the position could be set, -1 otherwise.
2871 */
2872 static void
2873f_cursor(typval_T *argvars, typval_T *rettv)
2874{
2875 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002876 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002877 int set_curswant = TRUE;
2878
2879 rettv->vval.v_number = -1;
2880 if (argvars[1].v_type == VAR_UNKNOWN)
2881 {
2882 pos_T pos;
2883 colnr_T curswant = -1;
2884
2885 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002887 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002888 return;
2889 }
2890 line = pos.lnum;
2891 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002892 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002893 if (curswant >= 0)
2894 {
2895 curwin->w_curswant = curswant - 1;
2896 set_curswant = FALSE;
2897 }
2898 }
2899 else
2900 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002901 line = tv_get_lnum(argvars);
2902 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002903 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002904 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002905 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002906 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002907 return; /* type error; errmsg already given */
2908 if (line > 0)
2909 curwin->w_cursor.lnum = line;
2910 if (col > 0)
2911 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002912 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002913
2914 /* Make sure the cursor is in a valid position. */
2915 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002916 /* Correct cursor for multi-byte character. */
2917 if (has_mbyte)
2918 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002919
2920 curwin->w_set_curswant = set_curswant;
2921 rettv->vval.v_number = 0;
2922}
2923
Bram Moolenaar4f974752019-02-17 17:44:42 +01002924#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002925/*
2926 * "debugbreak()" function
2927 */
2928 static void
2929f_debugbreak(typval_T *argvars, typval_T *rettv)
2930{
2931 int pid;
2932
2933 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002934 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002935 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002936 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002937 else
2938 {
2939 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2940
2941 if (hProcess != NULL)
2942 {
2943 DebugBreakProcess(hProcess);
2944 CloseHandle(hProcess);
2945 rettv->vval.v_number = OK;
2946 }
2947 }
2948}
2949#endif
2950
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002951/*
2952 * "deepcopy()" function
2953 */
2954 static void
2955f_deepcopy(typval_T *argvars, typval_T *rettv)
2956{
2957 int noref = 0;
2958 int copyID;
2959
2960 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002961 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002962 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002963 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002964 else
2965 {
2966 copyID = get_copyID();
2967 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2968 }
2969}
2970
2971/*
2972 * "delete()" function
2973 */
2974 static void
2975f_delete(typval_T *argvars, typval_T *rettv)
2976{
2977 char_u nbuf[NUMBUFLEN];
2978 char_u *name;
2979 char_u *flags;
2980
2981 rettv->vval.v_number = -1;
2982 if (check_restricted() || check_secure())
2983 return;
2984
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002985 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002986 if (name == NULL || *name == NUL)
2987 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002988 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002989 return;
2990 }
2991
2992 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002993 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002994 else
2995 flags = (char_u *)"";
2996
2997 if (*flags == NUL)
2998 /* delete a file */
2999 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3000 else if (STRCMP(flags, "d") == 0)
3001 /* delete an empty directory */
3002 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3003 else if (STRCMP(flags, "rf") == 0)
3004 /* delete a directory recursively */
3005 rettv->vval.v_number = delete_recursive(name);
3006 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003007 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003008}
3009
3010/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003011 * "deletebufline()" function
3012 */
3013 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003014f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003015{
3016 buf_T *buf;
3017 linenr_T first, last;
3018 linenr_T lnum;
3019 long count;
3020 int is_curbuf;
3021 buf_T *curbuf_save = NULL;
3022 win_T *curwin_save = NULL;
3023 tabpage_T *tp;
3024 win_T *wp;
3025
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003026 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003027 if (buf == NULL)
3028 {
3029 rettv->vval.v_number = 1; /* FAIL */
3030 return;
3031 }
3032 is_curbuf = buf == curbuf;
3033
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003034 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003035 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003036 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003037 else
3038 last = first;
3039
3040 if (buf->b_ml.ml_mfp == NULL || first < 1
3041 || first > buf->b_ml.ml_line_count || last < first)
3042 {
3043 rettv->vval.v_number = 1; /* FAIL */
3044 return;
3045 }
3046
3047 if (!is_curbuf)
3048 {
3049 curbuf_save = curbuf;
3050 curwin_save = curwin;
3051 curbuf = buf;
3052 find_win_for_curbuf();
3053 }
3054 if (last > curbuf->b_ml.ml_line_count)
3055 last = curbuf->b_ml.ml_line_count;
3056 count = last - first + 1;
3057
3058 // When coming here from Insert mode, sync undo, so that this can be
3059 // undone separately from what was previously inserted.
3060 if (u_sync_once == 2)
3061 {
3062 u_sync_once = 1; // notify that u_sync() was called
3063 u_sync(TRUE);
3064 }
3065
3066 if (u_save(first - 1, last + 1) == FAIL)
3067 {
3068 rettv->vval.v_number = 1; /* FAIL */
3069 return;
3070 }
3071
3072 for (lnum = first; lnum <= last; ++lnum)
3073 ml_delete(first, TRUE);
3074
3075 FOR_ALL_TAB_WINDOWS(tp, wp)
3076 if (wp->w_buffer == buf)
3077 {
3078 if (wp->w_cursor.lnum > last)
3079 wp->w_cursor.lnum -= count;
3080 else if (wp->w_cursor.lnum> first)
3081 wp->w_cursor.lnum = first;
3082 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3083 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3084 }
3085 check_cursor_col();
3086 deleted_lines_mark(first, count);
3087
3088 if (!is_curbuf)
3089 {
3090 curbuf = curbuf_save;
3091 curwin = curwin_save;
3092 }
3093}
3094
3095/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003096 * "did_filetype()" function
3097 */
3098 static void
3099f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3100{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003101 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003102}
3103
3104/*
3105 * "diff_filler()" function
3106 */
3107 static void
3108f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3109{
3110#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003111 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003112#endif
3113}
3114
3115/*
3116 * "diff_hlID()" function
3117 */
3118 static void
3119f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3120{
3121#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003122 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003123 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003124 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003125 static int fnum = 0;
3126 static int change_start = 0;
3127 static int change_end = 0;
3128 static hlf_T hlID = (hlf_T)0;
3129 int filler_lines;
3130 int col;
3131
3132 if (lnum < 0) /* ignore type error in {lnum} arg */
3133 lnum = 0;
3134 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003135 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003136 || fnum != curbuf->b_fnum)
3137 {
3138 /* New line, buffer, change: need to get the values. */
3139 filler_lines = diff_check(curwin, lnum);
3140 if (filler_lines < 0)
3141 {
3142 if (filler_lines == -1)
3143 {
3144 change_start = MAXCOL;
3145 change_end = -1;
3146 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3147 hlID = HLF_ADD; /* added line */
3148 else
3149 hlID = HLF_CHD; /* changed line */
3150 }
3151 else
3152 hlID = HLF_ADD; /* added line */
3153 }
3154 else
3155 hlID = (hlf_T)0;
3156 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003157 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003158 fnum = curbuf->b_fnum;
3159 }
3160
3161 if (hlID == HLF_CHD || hlID == HLF_TXD)
3162 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003163 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003164 if (col >= change_start && col <= change_end)
3165 hlID = HLF_TXD; /* changed text */
3166 else
3167 hlID = HLF_CHD; /* changed line */
3168 }
3169 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3170#endif
3171}
3172
3173/*
3174 * "empty({expr})" function
3175 */
3176 static void
3177f_empty(typval_T *argvars, typval_T *rettv)
3178{
3179 int n = FALSE;
3180
3181 switch (argvars[0].v_type)
3182 {
3183 case VAR_STRING:
3184 case VAR_FUNC:
3185 n = argvars[0].vval.v_string == NULL
3186 || *argvars[0].vval.v_string == NUL;
3187 break;
3188 case VAR_PARTIAL:
3189 n = FALSE;
3190 break;
3191 case VAR_NUMBER:
3192 n = argvars[0].vval.v_number == 0;
3193 break;
3194 case VAR_FLOAT:
3195#ifdef FEAT_FLOAT
3196 n = argvars[0].vval.v_float == 0.0;
3197 break;
3198#endif
3199 case VAR_LIST:
3200 n = argvars[0].vval.v_list == NULL
3201 || argvars[0].vval.v_list->lv_first == NULL;
3202 break;
3203 case VAR_DICT:
3204 n = argvars[0].vval.v_dict == NULL
3205 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3206 break;
3207 case VAR_SPECIAL:
3208 n = argvars[0].vval.v_number != VVAL_TRUE;
3209 break;
3210
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003211 case VAR_BLOB:
3212 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003213 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3214 break;
3215
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003216 case VAR_JOB:
3217#ifdef FEAT_JOB_CHANNEL
3218 n = argvars[0].vval.v_job == NULL
3219 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3220 break;
3221#endif
3222 case VAR_CHANNEL:
3223#ifdef FEAT_JOB_CHANNEL
3224 n = argvars[0].vval.v_channel == NULL
3225 || !channel_is_open(argvars[0].vval.v_channel);
3226 break;
3227#endif
3228 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003229 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003230 n = TRUE;
3231 break;
3232 }
3233
3234 rettv->vval.v_number = n;
3235}
3236
3237/*
3238 * "escape({string}, {chars})" function
3239 */
3240 static void
3241f_escape(typval_T *argvars, typval_T *rettv)
3242{
3243 char_u buf[NUMBUFLEN];
3244
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003245 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3246 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003247 rettv->v_type = VAR_STRING;
3248}
3249
3250/*
3251 * "eval()" function
3252 */
3253 static void
3254f_eval(typval_T *argvars, typval_T *rettv)
3255{
3256 char_u *s, *p;
3257
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003258 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003259 if (s != NULL)
3260 s = skipwhite(s);
3261
3262 p = s;
3263 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3264 {
3265 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003266 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003267 need_clr_eos = FALSE;
3268 rettv->v_type = VAR_NUMBER;
3269 rettv->vval.v_number = 0;
3270 }
3271 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003272 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003273}
3274
3275/*
3276 * "eventhandler()" function
3277 */
3278 static void
3279f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3280{
3281 rettv->vval.v_number = vgetc_busy;
3282}
3283
3284/*
3285 * "executable()" function
3286 */
3287 static void
3288f_executable(typval_T *argvars, typval_T *rettv)
3289{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003290 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003291
3292 /* Check in $PATH and also check directly if there is a directory name. */
3293 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3294 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3295}
3296
3297static garray_T redir_execute_ga;
3298
3299/*
3300 * Append "value[value_len]" to the execute() output.
3301 */
3302 void
3303execute_redir_str(char_u *value, int value_len)
3304{
3305 int len;
3306
3307 if (value_len == -1)
3308 len = (int)STRLEN(value); /* Append the entire string */
3309 else
3310 len = value_len; /* Append only "value_len" characters */
3311 if (ga_grow(&redir_execute_ga, len) == OK)
3312 {
3313 mch_memmove((char *)redir_execute_ga.ga_data
3314 + redir_execute_ga.ga_len, value, len);
3315 redir_execute_ga.ga_len += len;
3316 }
3317}
3318
3319/*
3320 * Get next line from a list.
3321 * Called by do_cmdline() to get the next line.
3322 * Returns allocated string, or NULL for end of function.
3323 */
3324
3325 static char_u *
3326get_list_line(
3327 int c UNUSED,
3328 void *cookie,
3329 int indent UNUSED)
3330{
3331 listitem_T **p = (listitem_T **)cookie;
3332 listitem_T *item = *p;
3333 char_u buf[NUMBUFLEN];
3334 char_u *s;
3335
3336 if (item == NULL)
3337 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003338 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003339 *p = item->li_next;
3340 return s == NULL ? NULL : vim_strsave(s);
3341}
3342
3343/*
3344 * "execute()" function
3345 */
3346 static void
3347f_execute(typval_T *argvars, typval_T *rettv)
3348{
3349 char_u *cmd = NULL;
3350 list_T *list = NULL;
3351 int save_msg_silent = msg_silent;
3352 int save_emsg_silent = emsg_silent;
3353 int save_emsg_noredir = emsg_noredir;
3354 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003355 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003356 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003357 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003358 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003359
3360 rettv->vval.v_string = NULL;
3361 rettv->v_type = VAR_STRING;
3362
3363 if (argvars[0].v_type == VAR_LIST)
3364 {
3365 list = argvars[0].vval.v_list;
3366 if (list == NULL || list->lv_first == NULL)
3367 /* empty list, no commands, empty output */
3368 return;
3369 ++list->lv_refcount;
3370 }
3371 else
3372 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003373 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003374 if (cmd == NULL)
3375 return;
3376 }
3377
3378 if (argvars[1].v_type != VAR_UNKNOWN)
3379 {
3380 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003381 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003382
3383 if (s == NULL)
3384 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003385 if (*s == NUL)
3386 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003387 if (STRNCMP(s, "silent", 6) == 0)
3388 ++msg_silent;
3389 if (STRCMP(s, "silent!") == 0)
3390 {
3391 emsg_silent = TRUE;
3392 emsg_noredir = TRUE;
3393 }
3394 }
3395 else
3396 ++msg_silent;
3397
3398 if (redir_execute)
3399 save_ga = redir_execute_ga;
3400 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3401 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003402 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003403 if (!echo_output)
3404 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405
3406 if (cmd != NULL)
3407 do_cmdline_cmd(cmd);
3408 else
3409 {
3410 listitem_T *item = list->lv_first;
3411
3412 do_cmdline(NULL, get_list_line, (void *)&item,
3413 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3414 --list->lv_refcount;
3415 }
3416
Bram Moolenaard297f352017-01-29 20:31:21 +01003417 /* Need to append a NUL to the result. */
3418 if (ga_grow(&redir_execute_ga, 1) == OK)
3419 {
3420 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3421 rettv->vval.v_string = redir_execute_ga.ga_data;
3422 }
3423 else
3424 {
3425 ga_clear(&redir_execute_ga);
3426 rettv->vval.v_string = NULL;
3427 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003428 msg_silent = save_msg_silent;
3429 emsg_silent = save_emsg_silent;
3430 emsg_noredir = save_emsg_noredir;
3431
3432 redir_execute = save_redir_execute;
3433 if (redir_execute)
3434 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003435 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003436
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003437 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003438 if (echo_output)
3439 // When not working silently: put it in column zero. A following
3440 // "echon" will overwrite the message, unavoidably.
3441 msg_col = 0;
3442 else
3443 // When working silently: Put it back where it was, since nothing
3444 // should have been written.
3445 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003446}
3447
3448/*
3449 * "exepath()" function
3450 */
3451 static void
3452f_exepath(typval_T *argvars, typval_T *rettv)
3453{
3454 char_u *p = NULL;
3455
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003456 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003457 rettv->v_type = VAR_STRING;
3458 rettv->vval.v_string = p;
3459}
3460
3461/*
3462 * "exists()" function
3463 */
3464 static void
3465f_exists(typval_T *argvars, typval_T *rettv)
3466{
3467 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003468 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003469
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003470 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003471 if (*p == '$') /* environment variable */
3472 {
3473 /* first try "normal" environment variables (fast) */
3474 if (mch_getenv(p + 1) != NULL)
3475 n = TRUE;
3476 else
3477 {
3478 /* try expanding things like $VIM and ${HOME} */
3479 p = expand_env_save(p);
3480 if (p != NULL && *p != '$')
3481 n = TRUE;
3482 vim_free(p);
3483 }
3484 }
3485 else if (*p == '&' || *p == '+') /* option */
3486 {
3487 n = (get_option_tv(&p, NULL, TRUE) == OK);
3488 if (*skipwhite(p) != NUL)
3489 n = FALSE; /* trailing garbage */
3490 }
3491 else if (*p == '*') /* internal or user defined function */
3492 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003493 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003494 }
3495 else if (*p == ':')
3496 {
3497 n = cmd_exists(p + 1);
3498 }
3499 else if (*p == '#')
3500 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003501 if (p[1] == '#')
3502 n = autocmd_supported(p + 2);
3503 else
3504 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003505 }
3506 else /* internal variable */
3507 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003508 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003509 }
3510
3511 rettv->vval.v_number = n;
3512}
3513
3514#ifdef FEAT_FLOAT
3515/*
3516 * "exp()" function
3517 */
3518 static void
3519f_exp(typval_T *argvars, typval_T *rettv)
3520{
3521 float_T f = 0.0;
3522
3523 rettv->v_type = VAR_FLOAT;
3524 if (get_float_arg(argvars, &f) == OK)
3525 rettv->vval.v_float = exp(f);
3526 else
3527 rettv->vval.v_float = 0.0;
3528}
3529#endif
3530
3531/*
3532 * "expand()" function
3533 */
3534 static void
3535f_expand(typval_T *argvars, typval_T *rettv)
3536{
3537 char_u *s;
3538 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003539 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003540 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3541 expand_T xpc;
3542 int error = FALSE;
3543 char_u *result;
3544
3545 rettv->v_type = VAR_STRING;
3546 if (argvars[1].v_type != VAR_UNKNOWN
3547 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003548 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003549 && !error)
3550 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003551 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003552 }
3553
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003554 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003555 if (*s == '%' || *s == '#' || *s == '<')
3556 {
3557 ++emsg_off;
3558 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3559 --emsg_off;
3560 if (rettv->v_type == VAR_LIST)
3561 {
3562 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3563 list_append_string(rettv->vval.v_list, result, -1);
3564 else
3565 vim_free(result);
3566 }
3567 else
3568 rettv->vval.v_string = result;
3569 }
3570 else
3571 {
3572 /* When the optional second argument is non-zero, don't remove matches
3573 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3574 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003575 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003576 options |= WILD_KEEP_ALL;
3577 if (!error)
3578 {
3579 ExpandInit(&xpc);
3580 xpc.xp_context = EXPAND_FILES;
3581 if (p_wic)
3582 options += WILD_ICASE;
3583 if (rettv->v_type == VAR_STRING)
3584 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3585 options, WILD_ALL);
3586 else if (rettv_list_alloc(rettv) != FAIL)
3587 {
3588 int i;
3589
3590 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3591 for (i = 0; i < xpc.xp_numfiles; i++)
3592 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3593 ExpandCleanup(&xpc);
3594 }
3595 }
3596 else
3597 rettv->vval.v_string = NULL;
3598 }
3599}
3600
3601/*
3602 * "extend(list, list [, idx])" function
3603 * "extend(dict, dict [, action])" function
3604 */
3605 static void
3606f_extend(typval_T *argvars, typval_T *rettv)
3607{
3608 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3609
3610 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3611 {
3612 list_T *l1, *l2;
3613 listitem_T *item;
3614 long before;
3615 int error = FALSE;
3616
3617 l1 = argvars[0].vval.v_list;
3618 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003619 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003620 && l2 != NULL)
3621 {
3622 if (argvars[2].v_type != VAR_UNKNOWN)
3623 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003624 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003625 if (error)
3626 return; /* type error; errmsg already given */
3627
3628 if (before == l1->lv_len)
3629 item = NULL;
3630 else
3631 {
3632 item = list_find(l1, before);
3633 if (item == NULL)
3634 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003635 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003636 return;
3637 }
3638 }
3639 }
3640 else
3641 item = NULL;
3642 list_extend(l1, l2, item);
3643
3644 copy_tv(&argvars[0], rettv);
3645 }
3646 }
3647 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3648 {
3649 dict_T *d1, *d2;
3650 char_u *action;
3651 int i;
3652
3653 d1 = argvars[0].vval.v_dict;
3654 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003655 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003656 && d2 != NULL)
3657 {
3658 /* Check the third argument. */
3659 if (argvars[2].v_type != VAR_UNKNOWN)
3660 {
3661 static char *(av[]) = {"keep", "force", "error"};
3662
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003663 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003664 if (action == NULL)
3665 return; /* type error; errmsg already given */
3666 for (i = 0; i < 3; ++i)
3667 if (STRCMP(action, av[i]) == 0)
3668 break;
3669 if (i == 3)
3670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003671 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003672 return;
3673 }
3674 }
3675 else
3676 action = (char_u *)"force";
3677
3678 dict_extend(d1, d2, action);
3679
3680 copy_tv(&argvars[0], rettv);
3681 }
3682 }
3683 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003684 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003685}
3686
3687/*
3688 * "feedkeys()" function
3689 */
3690 static void
3691f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3692{
3693 int remap = TRUE;
3694 int insert = FALSE;
3695 char_u *keys, *flags;
3696 char_u nbuf[NUMBUFLEN];
3697 int typed = FALSE;
3698 int execute = FALSE;
3699 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003700 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003701 char_u *keys_esc;
3702
3703 /* This is not allowed in the sandbox. If the commands would still be
3704 * executed in the sandbox it would be OK, but it probably happens later,
3705 * when "sandbox" is no longer set. */
3706 if (check_secure())
3707 return;
3708
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003709 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003710
3711 if (argvars[1].v_type != VAR_UNKNOWN)
3712 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003713 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003714 for ( ; *flags != NUL; ++flags)
3715 {
3716 switch (*flags)
3717 {
3718 case 'n': remap = FALSE; break;
3719 case 'm': remap = TRUE; break;
3720 case 't': typed = TRUE; break;
3721 case 'i': insert = TRUE; break;
3722 case 'x': execute = TRUE; break;
3723 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003724 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003725 }
3726 }
3727 }
3728
3729 if (*keys != NUL || execute)
3730 {
3731 /* Need to escape K_SPECIAL and CSI before putting the string in the
3732 * typeahead buffer. */
3733 keys_esc = vim_strsave_escape_csi(keys);
3734 if (keys_esc != NULL)
3735 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003736 if (lowlevel)
3737 {
3738#ifdef USE_INPUT_BUF
3739 add_to_input_buf(keys, (int)STRLEN(keys));
3740#else
3741 emsg(_("E980: lowlevel input not supported"));
3742#endif
3743 }
3744 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003745 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003746 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003747 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003748 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003749#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003750 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003751#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003752 )
3753 typebuf_was_filled = TRUE;
3754 }
3755 vim_free(keys_esc);
3756
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003757 if (execute)
3758 {
3759 int save_msg_scroll = msg_scroll;
3760
3761 /* Avoid a 1 second delay when the keys start Insert mode. */
3762 msg_scroll = FALSE;
3763
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003764 if (!dangerous)
3765 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003766 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003767 if (!dangerous)
3768 --ex_normal_busy;
3769
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003770 msg_scroll |= save_msg_scroll;
3771 }
3772 }
3773 }
3774}
3775
3776/*
3777 * "filereadable()" function
3778 */
3779 static void
3780f_filereadable(typval_T *argvars, typval_T *rettv)
3781{
3782 int fd;
3783 char_u *p;
3784 int n;
3785
3786#ifndef O_NONBLOCK
3787# define O_NONBLOCK 0
3788#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003789 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003790 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3791 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3792 {
3793 n = TRUE;
3794 close(fd);
3795 }
3796 else
3797 n = FALSE;
3798
3799 rettv->vval.v_number = n;
3800}
3801
3802/*
3803 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3804 * rights to write into.
3805 */
3806 static void
3807f_filewritable(typval_T *argvars, typval_T *rettv)
3808{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003809 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003810}
3811
3812 static void
3813findfilendir(
3814 typval_T *argvars UNUSED,
3815 typval_T *rettv,
3816 int find_what UNUSED)
3817{
3818#ifdef FEAT_SEARCHPATH
3819 char_u *fname;
3820 char_u *fresult = NULL;
3821 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3822 char_u *p;
3823 char_u pathbuf[NUMBUFLEN];
3824 int count = 1;
3825 int first = TRUE;
3826 int error = FALSE;
3827#endif
3828
3829 rettv->vval.v_string = NULL;
3830 rettv->v_type = VAR_STRING;
3831
3832#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003833 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003834
3835 if (argvars[1].v_type != VAR_UNKNOWN)
3836 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003837 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003838 if (p == NULL)
3839 error = TRUE;
3840 else
3841 {
3842 if (*p != NUL)
3843 path = p;
3844
3845 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003846 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003847 }
3848 }
3849
3850 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3851 error = TRUE;
3852
3853 if (*fname != NUL && !error)
3854 {
3855 do
3856 {
3857 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3858 vim_free(fresult);
3859 fresult = find_file_in_path_option(first ? fname : NULL,
3860 first ? (int)STRLEN(fname) : 0,
3861 0, first, path,
3862 find_what,
3863 curbuf->b_ffname,
3864 find_what == FINDFILE_DIR
3865 ? (char_u *)"" : curbuf->b_p_sua);
3866 first = FALSE;
3867
3868 if (fresult != NULL && rettv->v_type == VAR_LIST)
3869 list_append_string(rettv->vval.v_list, fresult, -1);
3870
3871 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3872 }
3873
3874 if (rettv->v_type == VAR_STRING)
3875 rettv->vval.v_string = fresult;
3876#endif
3877}
3878
3879/*
3880 * "filter()" function
3881 */
3882 static void
3883f_filter(typval_T *argvars, typval_T *rettv)
3884{
3885 filter_map(argvars, rettv, FALSE);
3886}
3887
3888/*
3889 * "finddir({fname}[, {path}[, {count}]])" function
3890 */
3891 static void
3892f_finddir(typval_T *argvars, typval_T *rettv)
3893{
3894 findfilendir(argvars, rettv, FINDFILE_DIR);
3895}
3896
3897/*
3898 * "findfile({fname}[, {path}[, {count}]])" function
3899 */
3900 static void
3901f_findfile(typval_T *argvars, typval_T *rettv)
3902{
3903 findfilendir(argvars, rettv, FINDFILE_FILE);
3904}
3905
3906#ifdef FEAT_FLOAT
3907/*
3908 * "float2nr({float})" function
3909 */
3910 static void
3911f_float2nr(typval_T *argvars, typval_T *rettv)
3912{
3913 float_T f = 0.0;
3914
3915 if (get_float_arg(argvars, &f) == OK)
3916 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003917 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003918 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003919 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003920 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003921 else
3922 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003923 }
3924}
3925
3926/*
3927 * "floor({float})" function
3928 */
3929 static void
3930f_floor(typval_T *argvars, typval_T *rettv)
3931{
3932 float_T f = 0.0;
3933
3934 rettv->v_type = VAR_FLOAT;
3935 if (get_float_arg(argvars, &f) == OK)
3936 rettv->vval.v_float = floor(f);
3937 else
3938 rettv->vval.v_float = 0.0;
3939}
3940
3941/*
3942 * "fmod()" function
3943 */
3944 static void
3945f_fmod(typval_T *argvars, typval_T *rettv)
3946{
3947 float_T fx = 0.0, fy = 0.0;
3948
3949 rettv->v_type = VAR_FLOAT;
3950 if (get_float_arg(argvars, &fx) == OK
3951 && get_float_arg(&argvars[1], &fy) == OK)
3952 rettv->vval.v_float = fmod(fx, fy);
3953 else
3954 rettv->vval.v_float = 0.0;
3955}
3956#endif
3957
3958/*
3959 * "fnameescape({string})" function
3960 */
3961 static void
3962f_fnameescape(typval_T *argvars, typval_T *rettv)
3963{
3964 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003965 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003966 rettv->v_type = VAR_STRING;
3967}
3968
3969/*
3970 * "fnamemodify({fname}, {mods})" function
3971 */
3972 static void
3973f_fnamemodify(typval_T *argvars, typval_T *rettv)
3974{
3975 char_u *fname;
3976 char_u *mods;
3977 int usedlen = 0;
3978 int len;
3979 char_u *fbuf = NULL;
3980 char_u buf[NUMBUFLEN];
3981
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003982 fname = tv_get_string_chk(&argvars[0]);
3983 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003984 if (fname == NULL || mods == NULL)
3985 fname = NULL;
3986 else
3987 {
3988 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003989 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003990 }
3991
3992 rettv->v_type = VAR_STRING;
3993 if (fname == NULL)
3994 rettv->vval.v_string = NULL;
3995 else
3996 rettv->vval.v_string = vim_strnsave(fname, len);
3997 vim_free(fbuf);
3998}
3999
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004000/*
4001 * "foldclosed()" function
4002 */
4003 static void
4004foldclosed_both(
4005 typval_T *argvars UNUSED,
4006 typval_T *rettv,
4007 int end UNUSED)
4008{
4009#ifdef FEAT_FOLDING
4010 linenr_T lnum;
4011 linenr_T first, last;
4012
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004013 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004014 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4015 {
4016 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4017 {
4018 if (end)
4019 rettv->vval.v_number = (varnumber_T)last;
4020 else
4021 rettv->vval.v_number = (varnumber_T)first;
4022 return;
4023 }
4024 }
4025#endif
4026 rettv->vval.v_number = -1;
4027}
4028
4029/*
4030 * "foldclosed()" function
4031 */
4032 static void
4033f_foldclosed(typval_T *argvars, typval_T *rettv)
4034{
4035 foldclosed_both(argvars, rettv, FALSE);
4036}
4037
4038/*
4039 * "foldclosedend()" function
4040 */
4041 static void
4042f_foldclosedend(typval_T *argvars, typval_T *rettv)
4043{
4044 foldclosed_both(argvars, rettv, TRUE);
4045}
4046
4047/*
4048 * "foldlevel()" function
4049 */
4050 static void
4051f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4052{
4053#ifdef FEAT_FOLDING
4054 linenr_T lnum;
4055
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004056 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004057 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4058 rettv->vval.v_number = foldLevel(lnum);
4059#endif
4060}
4061
4062/*
4063 * "foldtext()" function
4064 */
4065 static void
4066f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4067{
4068#ifdef FEAT_FOLDING
4069 linenr_T foldstart;
4070 linenr_T foldend;
4071 char_u *dashes;
4072 linenr_T lnum;
4073 char_u *s;
4074 char_u *r;
4075 int len;
4076 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004077 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004078#endif
4079
4080 rettv->v_type = VAR_STRING;
4081 rettv->vval.v_string = NULL;
4082#ifdef FEAT_FOLDING
4083 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4084 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4085 dashes = get_vim_var_str(VV_FOLDDASHES);
4086 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4087 && dashes != NULL)
4088 {
4089 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004090 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004091 if (!linewhite(lnum))
4092 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004093
4094 /* Find interesting text in this line. */
4095 s = skipwhite(ml_get(lnum));
4096 /* skip C comment-start */
4097 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4098 {
4099 s = skipwhite(s + 2);
4100 if (*skipwhite(s) == NUL
4101 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4102 {
4103 s = skipwhite(ml_get(lnum + 1));
4104 if (*s == '*')
4105 s = skipwhite(s + 1);
4106 }
4107 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004108 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004109 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004110 r = alloc((unsigned)(STRLEN(txt)
4111 + STRLEN(dashes) /* for %s */
4112 + 20 /* for %3ld */
4113 + STRLEN(s))); /* concatenated */
4114 if (r != NULL)
4115 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004116 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004117 len = (int)STRLEN(r);
4118 STRCAT(r, s);
4119 /* remove 'foldmarker' and 'commentstring' */
4120 foldtext_cleanup(r + len);
4121 rettv->vval.v_string = r;
4122 }
4123 }
4124#endif
4125}
4126
4127/*
4128 * "foldtextresult(lnum)" function
4129 */
4130 static void
4131f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4132{
4133#ifdef FEAT_FOLDING
4134 linenr_T lnum;
4135 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004136 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004137 foldinfo_T foldinfo;
4138 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004139 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004140#endif
4141
4142 rettv->v_type = VAR_STRING;
4143 rettv->vval.v_string = NULL;
4144#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004145 if (entered)
4146 return; /* reject recursive use */
4147 entered = TRUE;
4148
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004149 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004150 /* treat illegal types and illegal string values for {lnum} the same */
4151 if (lnum < 0)
4152 lnum = 0;
4153 fold_count = foldedCount(curwin, lnum, &foldinfo);
4154 if (fold_count > 0)
4155 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004156 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4157 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004158 if (text == buf)
4159 text = vim_strsave(text);
4160 rettv->vval.v_string = text;
4161 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004162
4163 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004164#endif
4165}
4166
4167/*
4168 * "foreground()" function
4169 */
4170 static void
4171f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4172{
4173#ifdef FEAT_GUI
4174 if (gui.in_use)
4175 gui_mch_set_foreground();
4176#else
Bram Moolenaar4f974752019-02-17 17:44:42 +01004177# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004178 win32_set_foreground();
4179# endif
4180#endif
4181}
4182
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004183 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004184common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004185{
4186 char_u *s;
4187 char_u *name;
4188 int use_string = FALSE;
4189 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004190 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004191
4192 if (argvars[0].v_type == VAR_FUNC)
4193 {
4194 /* function(MyFunc, [arg], dict) */
4195 s = argvars[0].vval.v_string;
4196 }
4197 else if (argvars[0].v_type == VAR_PARTIAL
4198 && argvars[0].vval.v_partial != NULL)
4199 {
4200 /* function(dict.MyFunc, [arg]) */
4201 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004202 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004203 }
4204 else
4205 {
4206 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004207 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004208 use_string = TRUE;
4209 }
4210
Bram Moolenaar843b8842016-08-21 14:36:15 +02004211 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004212 {
4213 name = s;
4214 trans_name = trans_function_name(&name, FALSE,
4215 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4216 if (*name != NUL)
4217 s = NULL;
4218 }
4219
Bram Moolenaar843b8842016-08-21 14:36:15 +02004220 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4221 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004222 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004223 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004224 else if (trans_name != NULL && (is_funcref
4225 ? find_func(trans_name) == NULL
4226 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004227 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004228 else
4229 {
4230 int dict_idx = 0;
4231 int arg_idx = 0;
4232 list_T *list = NULL;
4233
4234 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4235 {
4236 char sid_buf[25];
4237 int off = *s == 's' ? 2 : 5;
4238
4239 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4240 * also be called from another script. Using trans_function_name()
4241 * would also work, but some plugins depend on the name being
4242 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004243 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004244 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4245 if (name != NULL)
4246 {
4247 STRCPY(name, sid_buf);
4248 STRCAT(name, s + off);
4249 }
4250 }
4251 else
4252 name = vim_strsave(s);
4253
4254 if (argvars[1].v_type != VAR_UNKNOWN)
4255 {
4256 if (argvars[2].v_type != VAR_UNKNOWN)
4257 {
4258 /* function(name, [args], dict) */
4259 arg_idx = 1;
4260 dict_idx = 2;
4261 }
4262 else if (argvars[1].v_type == VAR_DICT)
4263 /* function(name, dict) */
4264 dict_idx = 1;
4265 else
4266 /* function(name, [args]) */
4267 arg_idx = 1;
4268 if (dict_idx > 0)
4269 {
4270 if (argvars[dict_idx].v_type != VAR_DICT)
4271 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004272 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004273 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004274 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004275 }
4276 if (argvars[dict_idx].vval.v_dict == NULL)
4277 dict_idx = 0;
4278 }
4279 if (arg_idx > 0)
4280 {
4281 if (argvars[arg_idx].v_type != VAR_LIST)
4282 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004283 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004284 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004285 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004286 }
4287 list = argvars[arg_idx].vval.v_list;
4288 if (list == NULL || list->lv_len == 0)
4289 arg_idx = 0;
4290 }
4291 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004292 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004293 {
4294 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4295
4296 /* result is a VAR_PARTIAL */
4297 if (pt == NULL)
4298 vim_free(name);
4299 else
4300 {
4301 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4302 {
4303 listitem_T *li;
4304 int i = 0;
4305 int arg_len = 0;
4306 int lv_len = 0;
4307
4308 if (arg_pt != NULL)
4309 arg_len = arg_pt->pt_argc;
4310 if (list != NULL)
4311 lv_len = list->lv_len;
4312 pt->pt_argc = arg_len + lv_len;
4313 pt->pt_argv = (typval_T *)alloc(
4314 sizeof(typval_T) * pt->pt_argc);
4315 if (pt->pt_argv == NULL)
4316 {
4317 vim_free(pt);
4318 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004319 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004320 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004321 for (i = 0; i < arg_len; i++)
4322 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4323 if (lv_len > 0)
4324 for (li = list->lv_first; li != NULL;
4325 li = li->li_next)
4326 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004327 }
4328
4329 /* For "function(dict.func, [], dict)" and "func" is a partial
4330 * use "dict". That is backwards compatible. */
4331 if (dict_idx > 0)
4332 {
4333 /* The dict is bound explicitly, pt_auto is FALSE. */
4334 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4335 ++pt->pt_dict->dv_refcount;
4336 }
4337 else if (arg_pt != NULL)
4338 {
4339 /* If the dict was bound automatically the result is also
4340 * bound automatically. */
4341 pt->pt_dict = arg_pt->pt_dict;
4342 pt->pt_auto = arg_pt->pt_auto;
4343 if (pt->pt_dict != NULL)
4344 ++pt->pt_dict->dv_refcount;
4345 }
4346
4347 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004348 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4349 {
4350 pt->pt_func = arg_pt->pt_func;
4351 func_ptr_ref(pt->pt_func);
4352 vim_free(name);
4353 }
4354 else if (is_funcref)
4355 {
4356 pt->pt_func = find_func(trans_name);
4357 func_ptr_ref(pt->pt_func);
4358 vim_free(name);
4359 }
4360 else
4361 {
4362 pt->pt_name = name;
4363 func_ref(name);
4364 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004365 }
4366 rettv->v_type = VAR_PARTIAL;
4367 rettv->vval.v_partial = pt;
4368 }
4369 else
4370 {
4371 /* result is a VAR_FUNC */
4372 rettv->v_type = VAR_FUNC;
4373 rettv->vval.v_string = name;
4374 func_ref(name);
4375 }
4376 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004377theend:
4378 vim_free(trans_name);
4379}
4380
4381/*
4382 * "funcref()" function
4383 */
4384 static void
4385f_funcref(typval_T *argvars, typval_T *rettv)
4386{
4387 common_function(argvars, rettv, TRUE);
4388}
4389
4390/*
4391 * "function()" function
4392 */
4393 static void
4394f_function(typval_T *argvars, typval_T *rettv)
4395{
4396 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004397}
4398
4399/*
4400 * "garbagecollect()" function
4401 */
4402 static void
4403f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4404{
4405 /* This is postponed until we are back at the toplevel, because we may be
4406 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4407 want_garbage_collect = TRUE;
4408
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004409 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004410 garbage_collect_at_exit = TRUE;
4411}
4412
4413/*
4414 * "get()" function
4415 */
4416 static void
4417f_get(typval_T *argvars, typval_T *rettv)
4418{
4419 listitem_T *li;
4420 list_T *l;
4421 dictitem_T *di;
4422 dict_T *d;
4423 typval_T *tv = NULL;
4424
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004425 if (argvars[0].v_type == VAR_BLOB)
4426 {
4427 int error = FALSE;
4428 int idx = tv_get_number_chk(&argvars[1], &error);
4429
4430 if (!error)
4431 {
4432 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004433 if (idx < 0)
4434 idx = blob_len(argvars[0].vval.v_blob) + idx;
4435 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4436 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004437 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004438 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004439 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004440 tv = rettv;
4441 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004442 }
4443 }
4444 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004445 {
4446 if ((l = argvars[0].vval.v_list) != NULL)
4447 {
4448 int error = FALSE;
4449
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004450 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004451 if (!error && li != NULL)
4452 tv = &li->li_tv;
4453 }
4454 }
4455 else if (argvars[0].v_type == VAR_DICT)
4456 {
4457 if ((d = argvars[0].vval.v_dict) != NULL)
4458 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004459 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004460 if (di != NULL)
4461 tv = &di->di_tv;
4462 }
4463 }
4464 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4465 {
4466 partial_T *pt;
4467 partial_T fref_pt;
4468
4469 if (argvars[0].v_type == VAR_PARTIAL)
4470 pt = argvars[0].vval.v_partial;
4471 else
4472 {
4473 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4474 fref_pt.pt_name = argvars[0].vval.v_string;
4475 pt = &fref_pt;
4476 }
4477
4478 if (pt != NULL)
4479 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004480 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004481 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004482
4483 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4484 {
4485 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004486 n = partial_name(pt);
4487 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004488 rettv->vval.v_string = NULL;
4489 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004490 {
4491 rettv->vval.v_string = vim_strsave(n);
4492 if (rettv->v_type == VAR_FUNC)
4493 func_ref(rettv->vval.v_string);
4494 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004495 }
4496 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004497 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004498 else if (STRCMP(what, "args") == 0)
4499 {
4500 rettv->v_type = VAR_LIST;
4501 if (rettv_list_alloc(rettv) == OK)
4502 {
4503 int i;
4504
4505 for (i = 0; i < pt->pt_argc; ++i)
4506 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4507 }
4508 }
4509 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004510 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004511 return;
4512 }
4513 }
4514 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004515 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004516
4517 if (tv == NULL)
4518 {
4519 if (argvars[2].v_type != VAR_UNKNOWN)
4520 copy_tv(&argvars[2], rettv);
4521 }
4522 else
4523 copy_tv(tv, rettv);
4524}
4525
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004526/*
4527 * Returns buffer options, variables and other attributes in a dictionary.
4528 */
4529 static dict_T *
4530get_buffer_info(buf_T *buf)
4531{
4532 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004533 tabpage_T *tp;
4534 win_T *wp;
4535 list_T *windows;
4536
4537 dict = dict_alloc();
4538 if (dict == NULL)
4539 return NULL;
4540
Bram Moolenaare0be1672018-07-08 16:50:37 +02004541 dict_add_number(dict, "bufnr", buf->b_fnum);
4542 dict_add_string(dict, "name", buf->b_ffname);
4543 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4544 : buflist_findlnum(buf));
4545 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4546 dict_add_number(dict, "listed", buf->b_p_bl);
4547 dict_add_number(dict, "changed", bufIsChanged(buf));
4548 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4549 dict_add_number(dict, "hidden",
4550 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004551
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004552 /* Get a reference to buffer variables */
4553 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004554
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004555 /* List of windows displaying this buffer */
4556 windows = list_alloc();
4557 if (windows != NULL)
4558 {
4559 FOR_ALL_TAB_WINDOWS(tp, wp)
4560 if (wp->w_buffer == buf)
4561 list_append_number(windows, (varnumber_T)wp->w_id);
4562 dict_add_list(dict, "windows", windows);
4563 }
4564
4565#ifdef FEAT_SIGNS
4566 if (buf->b_signlist != NULL)
4567 {
4568 /* List of signs placed in this buffer */
4569 list_T *signs = list_alloc();
4570 if (signs != NULL)
4571 {
4572 get_buffer_signs(buf, signs);
4573 dict_add_list(dict, "signs", signs);
4574 }
4575 }
4576#endif
4577
4578 return dict;
4579}
4580
4581/*
4582 * "getbufinfo()" function
4583 */
4584 static void
4585f_getbufinfo(typval_T *argvars, typval_T *rettv)
4586{
4587 buf_T *buf = NULL;
4588 buf_T *argbuf = NULL;
4589 dict_T *d;
4590 int filtered = FALSE;
4591 int sel_buflisted = FALSE;
4592 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004593 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004594
4595 if (rettv_list_alloc(rettv) != OK)
4596 return;
4597
4598 /* List of all the buffers or selected buffers */
4599 if (argvars[0].v_type == VAR_DICT)
4600 {
4601 dict_T *sel_d = argvars[0].vval.v_dict;
4602
4603 if (sel_d != NULL)
4604 {
4605 dictitem_T *di;
4606
4607 filtered = TRUE;
4608
4609 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004610 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004611 sel_buflisted = TRUE;
4612
4613 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004614 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004615 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004616
4617 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004618 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004619 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004620 }
4621 }
4622 else if (argvars[0].v_type != VAR_UNKNOWN)
4623 {
4624 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004625 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004626 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004627 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004628 --emsg_off;
4629 if (argbuf == NULL)
4630 return;
4631 }
4632
4633 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004634 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004635 {
4636 if (argbuf != NULL && argbuf != buf)
4637 continue;
4638 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004639 || (sel_buflisted && !buf->b_p_bl)
4640 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004641 continue;
4642
4643 d = get_buffer_info(buf);
4644 if (d != NULL)
4645 list_append_dict(rettv->vval.v_list, d);
4646 if (argbuf != NULL)
4647 return;
4648 }
4649}
4650
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004651/*
4652 * Get line or list of lines from buffer "buf" into "rettv".
4653 * Return a range (from start to end) of lines in rettv from the specified
4654 * buffer.
4655 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4656 */
4657 static void
4658get_buffer_lines(
4659 buf_T *buf,
4660 linenr_T start,
4661 linenr_T end,
4662 int retlist,
4663 typval_T *rettv)
4664{
4665 char_u *p;
4666
4667 rettv->v_type = VAR_STRING;
4668 rettv->vval.v_string = NULL;
4669 if (retlist && rettv_list_alloc(rettv) == FAIL)
4670 return;
4671
4672 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4673 return;
4674
4675 if (!retlist)
4676 {
4677 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4678 p = ml_get_buf(buf, start, FALSE);
4679 else
4680 p = (char_u *)"";
4681 rettv->vval.v_string = vim_strsave(p);
4682 }
4683 else
4684 {
4685 if (end < start)
4686 return;
4687
4688 if (start < 1)
4689 start = 1;
4690 if (end > buf->b_ml.ml_line_count)
4691 end = buf->b_ml.ml_line_count;
4692 while (start <= end)
4693 if (list_append_string(rettv->vval.v_list,
4694 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4695 break;
4696 }
4697}
4698
4699/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004700 * "getbufline()" function
4701 */
4702 static void
4703f_getbufline(typval_T *argvars, typval_T *rettv)
4704{
4705 linenr_T lnum;
4706 linenr_T end;
4707 buf_T *buf;
4708
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004709 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004710 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004711 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004712 --emsg_off;
4713
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004714 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004715 if (argvars[2].v_type == VAR_UNKNOWN)
4716 end = lnum;
4717 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004718 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004719
4720 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4721}
4722
4723/*
4724 * "getbufvar()" function
4725 */
4726 static void
4727f_getbufvar(typval_T *argvars, typval_T *rettv)
4728{
4729 buf_T *buf;
4730 buf_T *save_curbuf;
4731 char_u *varname;
4732 dictitem_T *v;
4733 int done = FALSE;
4734
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004735 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4736 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004737 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004738 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004739
4740 rettv->v_type = VAR_STRING;
4741 rettv->vval.v_string = NULL;
4742
4743 if (buf != NULL && varname != NULL)
4744 {
4745 /* set curbuf to be our buf, temporarily */
4746 save_curbuf = curbuf;
4747 curbuf = buf;
4748
Bram Moolenaar30567352016-08-27 21:25:44 +02004749 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004750 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004751 if (varname[1] == NUL)
4752 {
4753 /* get all buffer-local options in a dict */
4754 dict_T *opts = get_winbuf_options(TRUE);
4755
4756 if (opts != NULL)
4757 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004758 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004759 done = TRUE;
4760 }
4761 }
4762 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4763 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004764 done = TRUE;
4765 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004766 else
4767 {
4768 /* Look up the variable. */
4769 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4770 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4771 'b', varname, FALSE);
4772 if (v != NULL)
4773 {
4774 copy_tv(&v->di_tv, rettv);
4775 done = TRUE;
4776 }
4777 }
4778
4779 /* restore previous notion of curbuf */
4780 curbuf = save_curbuf;
4781 }
4782
4783 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4784 /* use the default value */
4785 copy_tv(&argvars[2], rettv);
4786
4787 --emsg_off;
4788}
4789
4790/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004791 * "getchangelist()" function
4792 */
4793 static void
4794f_getchangelist(typval_T *argvars, typval_T *rettv)
4795{
4796#ifdef FEAT_JUMPLIST
4797 buf_T *buf;
4798 int i;
4799 list_T *l;
4800 dict_T *d;
4801#endif
4802
4803 if (rettv_list_alloc(rettv) != OK)
4804 return;
4805
4806#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004807 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004808 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004809 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004810 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004811 if (buf == NULL)
4812 return;
4813
4814 l = list_alloc();
4815 if (l == NULL)
4816 return;
4817
4818 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4819 return;
4820 /*
4821 * The current window change list index tracks only the position in the
4822 * current buffer change list. For other buffers, use the change list
4823 * length as the current index.
4824 */
4825 list_append_number(rettv->vval.v_list,
4826 (varnumber_T)((buf == curwin->w_buffer)
4827 ? curwin->w_changelistidx : buf->b_changelistlen));
4828
4829 for (i = 0; i < buf->b_changelistlen; ++i)
4830 {
4831 if (buf->b_changelist[i].lnum == 0)
4832 continue;
4833 if ((d = dict_alloc()) == NULL)
4834 return;
4835 if (list_append_dict(l, d) == FAIL)
4836 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004837 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4838 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004839 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004840 }
4841#endif
4842}
4843/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004844 * "getchar()" function
4845 */
4846 static void
4847f_getchar(typval_T *argvars, typval_T *rettv)
4848{
4849 varnumber_T n;
4850 int error = FALSE;
Bram Moolenaarf0fab302019-03-05 12:24:10 +01004851 int save_reg_executing = reg_executing;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004852
Bram Moolenaar84d93902018-09-11 20:10:20 +02004853#ifdef MESSAGE_QUEUE
4854 // vpeekc() used to check for messages, but that caused problems, invoking
4855 // a callback where it was not expected. Some plugins use getchar(1) in a
4856 // loop to await a message, therefore make sure we check for messages here.
4857 parse_queued_messages();
4858#endif
4859
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004860 /* Position the cursor. Needed after a message that ends in a space. */
4861 windgoto(msg_row, msg_col);
4862
4863 ++no_mapping;
4864 ++allow_keys;
4865 for (;;)
4866 {
4867 if (argvars[0].v_type == VAR_UNKNOWN)
4868 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004869 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004870 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004871 /* getchar(1): only check if char avail */
4872 n = vpeekc_any();
4873 else if (error || vpeekc_any() == NUL)
4874 /* illegal argument or getchar(0) and no char avail: return zero */
4875 n = 0;
4876 else
4877 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004878 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004879
4880 if (n == K_IGNORE)
4881 continue;
4882 break;
4883 }
4884 --no_mapping;
4885 --allow_keys;
Bram Moolenaarf0fab302019-03-05 12:24:10 +01004886 reg_executing = save_reg_executing;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004887
4888 set_vim_var_nr(VV_MOUSE_WIN, 0);
4889 set_vim_var_nr(VV_MOUSE_WINID, 0);
4890 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4891 set_vim_var_nr(VV_MOUSE_COL, 0);
4892
4893 rettv->vval.v_number = n;
4894 if (IS_SPECIAL(n) || mod_mask != 0)
4895 {
4896 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4897 int i = 0;
4898
4899 /* Turn a special key into three bytes, plus modifier. */
4900 if (mod_mask != 0)
4901 {
4902 temp[i++] = K_SPECIAL;
4903 temp[i++] = KS_MODIFIER;
4904 temp[i++] = mod_mask;
4905 }
4906 if (IS_SPECIAL(n))
4907 {
4908 temp[i++] = K_SPECIAL;
4909 temp[i++] = K_SECOND(n);
4910 temp[i++] = K_THIRD(n);
4911 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004912 else if (has_mbyte)
4913 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004914 else
4915 temp[i++] = n;
4916 temp[i++] = NUL;
4917 rettv->v_type = VAR_STRING;
4918 rettv->vval.v_string = vim_strsave(temp);
4919
4920#ifdef FEAT_MOUSE
4921 if (is_mouse_key(n))
4922 {
4923 int row = mouse_row;
4924 int col = mouse_col;
4925 win_T *win;
4926 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004927 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004928 int winnr = 1;
4929
4930 if (row >= 0 && col >= 0)
4931 {
4932 /* Find the window at the mouse coordinates and compute the
4933 * text position. */
4934 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004935 if (win == NULL)
4936 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004937 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004938 for (wp = firstwin; wp != win; wp = wp->w_next)
4939 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004940 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4941 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4942 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4943 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4944 }
4945 }
4946#endif
4947 }
4948}
4949
4950/*
4951 * "getcharmod()" function
4952 */
4953 static void
4954f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4955{
4956 rettv->vval.v_number = mod_mask;
4957}
4958
4959/*
4960 * "getcharsearch()" function
4961 */
4962 static void
4963f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4964{
4965 if (rettv_dict_alloc(rettv) != FAIL)
4966 {
4967 dict_T *dict = rettv->vval.v_dict;
4968
Bram Moolenaare0be1672018-07-08 16:50:37 +02004969 dict_add_string(dict, "char", last_csearch());
4970 dict_add_number(dict, "forward", last_csearch_forward());
4971 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004972 }
4973}
4974
4975/*
4976 * "getcmdline()" function
4977 */
4978 static void
4979f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4980{
4981 rettv->v_type = VAR_STRING;
4982 rettv->vval.v_string = get_cmdline_str();
4983}
4984
4985/*
4986 * "getcmdpos()" function
4987 */
4988 static void
4989f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4990{
4991 rettv->vval.v_number = get_cmdline_pos() + 1;
4992}
4993
4994/*
4995 * "getcmdtype()" function
4996 */
4997 static void
4998f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4999{
5000 rettv->v_type = VAR_STRING;
5001 rettv->vval.v_string = alloc(2);
5002 if (rettv->vval.v_string != NULL)
5003 {
5004 rettv->vval.v_string[0] = get_cmdline_type();
5005 rettv->vval.v_string[1] = NUL;
5006 }
5007}
5008
5009/*
5010 * "getcmdwintype()" function
5011 */
5012 static void
5013f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5014{
5015 rettv->v_type = VAR_STRING;
5016 rettv->vval.v_string = NULL;
5017#ifdef FEAT_CMDWIN
5018 rettv->vval.v_string = alloc(2);
5019 if (rettv->vval.v_string != NULL)
5020 {
5021 rettv->vval.v_string[0] = cmdwin_type;
5022 rettv->vval.v_string[1] = NUL;
5023 }
5024#endif
5025}
5026
5027#if defined(FEAT_CMDL_COMPL)
5028/*
5029 * "getcompletion()" function
5030 */
5031 static void
5032f_getcompletion(typval_T *argvars, typval_T *rettv)
5033{
5034 char_u *pat;
5035 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005036 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005037 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5038 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005039
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005040 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005041 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005042
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005043 if (p_wic)
5044 options |= WILD_ICASE;
5045
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005046 /* For filtered results, 'wildignore' is used */
5047 if (!filtered)
5048 options |= WILD_KEEP_ALL;
5049
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005050 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005051 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005052 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005053 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005054 if (xpc.xp_context == EXPAND_NOTHING)
5055 {
5056 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005057 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005058 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005059 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005060 return;
5061 }
5062
5063# if defined(FEAT_MENU)
5064 if (xpc.xp_context == EXPAND_MENUS)
5065 {
5066 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5067 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5068 }
5069# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005070#ifdef FEAT_CSCOPE
5071 if (xpc.xp_context == EXPAND_CSCOPE)
5072 {
5073 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5074 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5075 }
5076#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005077#ifdef FEAT_SIGNS
5078 if (xpc.xp_context == EXPAND_SIGN)
5079 {
5080 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5081 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5082 }
5083#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005084
5085 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5086 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5087 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005088 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005089
5090 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5091
5092 for (i = 0; i < xpc.xp_numfiles; i++)
5093 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5094 }
5095 vim_free(pat);
5096 ExpandCleanup(&xpc);
5097}
5098#endif
5099
5100/*
5101 * "getcwd()" function
5102 */
5103 static void
5104f_getcwd(typval_T *argvars, typval_T *rettv)
5105{
5106 win_T *wp = NULL;
5107 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005108 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005109
5110 rettv->v_type = VAR_STRING;
5111 rettv->vval.v_string = NULL;
5112
Bram Moolenaar54591292018-02-09 20:53:59 +01005113 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5114 global = TRUE;
5115 else
5116 wp = find_tabwin(&argvars[0], &argvars[1]);
5117
5118 if (wp != NULL && wp->w_localdir != NULL)
5119 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5120 else if (wp != 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/*
5142 * "getfontname()" function
5143 */
5144 static void
5145f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5146{
5147 rettv->v_type = VAR_STRING;
5148 rettv->vval.v_string = NULL;
5149#ifdef FEAT_GUI
5150 if (gui.in_use)
5151 {
5152 GuiFont font;
5153 char_u *name = NULL;
5154
5155 if (argvars[0].v_type == VAR_UNKNOWN)
5156 {
5157 /* Get the "Normal" font. Either the name saved by
5158 * hl_set_font_name() or from the font ID. */
5159 font = gui.norm_font;
5160 name = hl_get_font_name();
5161 }
5162 else
5163 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005164 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005165 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5166 return;
5167 font = gui_mch_get_font(name, FALSE);
5168 if (font == NOFONT)
5169 return; /* Invalid font name, return empty string. */
5170 }
5171 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5172 if (argvars[0].v_type != VAR_UNKNOWN)
5173 gui_mch_free_font(font);
5174 }
5175#endif
5176}
5177
5178/*
5179 * "getfperm({fname})" function
5180 */
5181 static void
5182f_getfperm(typval_T *argvars, typval_T *rettv)
5183{
5184 char_u *fname;
5185 stat_T st;
5186 char_u *perm = NULL;
5187 char_u flags[] = "rwx";
5188 int i;
5189
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005190 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005191
5192 rettv->v_type = VAR_STRING;
5193 if (mch_stat((char *)fname, &st) >= 0)
5194 {
5195 perm = vim_strsave((char_u *)"---------");
5196 if (perm != NULL)
5197 {
5198 for (i = 0; i < 9; i++)
5199 {
5200 if (st.st_mode & (1 << (8 - i)))
5201 perm[i] = flags[i % 3];
5202 }
5203 }
5204 }
5205 rettv->vval.v_string = perm;
5206}
5207
5208/*
5209 * "getfsize({fname})" function
5210 */
5211 static void
5212f_getfsize(typval_T *argvars, typval_T *rettv)
5213{
5214 char_u *fname;
5215 stat_T st;
5216
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005217 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005218
5219 rettv->v_type = VAR_NUMBER;
5220
5221 if (mch_stat((char *)fname, &st) >= 0)
5222 {
5223 if (mch_isdir(fname))
5224 rettv->vval.v_number = 0;
5225 else
5226 {
5227 rettv->vval.v_number = (varnumber_T)st.st_size;
5228
5229 /* non-perfect check for overflow */
5230 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5231 rettv->vval.v_number = -2;
5232 }
5233 }
5234 else
5235 rettv->vval.v_number = -1;
5236}
5237
5238/*
5239 * "getftime({fname})" function
5240 */
5241 static void
5242f_getftime(typval_T *argvars, typval_T *rettv)
5243{
5244 char_u *fname;
5245 stat_T st;
5246
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005247 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005248
5249 if (mch_stat((char *)fname, &st) >= 0)
5250 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5251 else
5252 rettv->vval.v_number = -1;
5253}
5254
5255/*
5256 * "getftype({fname})" function
5257 */
5258 static void
5259f_getftype(typval_T *argvars, typval_T *rettv)
5260{
5261 char_u *fname;
5262 stat_T st;
5263 char_u *type = NULL;
5264 char *t;
5265
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005266 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005267
5268 rettv->v_type = VAR_STRING;
5269 if (mch_lstat((char *)fname, &st) >= 0)
5270 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005271 if (S_ISREG(st.st_mode))
5272 t = "file";
5273 else if (S_ISDIR(st.st_mode))
5274 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005275 else if (S_ISLNK(st.st_mode))
5276 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005277 else if (S_ISBLK(st.st_mode))
5278 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005279 else if (S_ISCHR(st.st_mode))
5280 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005281 else if (S_ISFIFO(st.st_mode))
5282 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005283 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005284 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005285 else
5286 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005287 type = vim_strsave((char_u *)t);
5288 }
5289 rettv->vval.v_string = type;
5290}
5291
5292/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005293 * "getjumplist()" function
5294 */
5295 static void
5296f_getjumplist(typval_T *argvars, typval_T *rettv)
5297{
5298#ifdef FEAT_JUMPLIST
5299 win_T *wp;
5300 int i;
5301 list_T *l;
5302 dict_T *d;
5303#endif
5304
5305 if (rettv_list_alloc(rettv) != OK)
5306 return;
5307
5308#ifdef FEAT_JUMPLIST
5309 wp = find_tabwin(&argvars[0], &argvars[1]);
5310 if (wp == NULL)
5311 return;
5312
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005313 cleanup_jumplist(wp, TRUE);
5314
Bram Moolenaar4f505882018-02-10 21:06:32 +01005315 l = list_alloc();
5316 if (l == NULL)
5317 return;
5318
5319 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5320 return;
5321 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5322
5323 for (i = 0; i < wp->w_jumplistlen; ++i)
5324 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005325 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5326 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005327 if ((d = dict_alloc()) == NULL)
5328 return;
5329 if (list_append_dict(l, d) == FAIL)
5330 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005331 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5332 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005333 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005334 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005335 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005336 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005337 }
5338#endif
5339}
5340
5341/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005342 * "getline(lnum, [end])" function
5343 */
5344 static void
5345f_getline(typval_T *argvars, typval_T *rettv)
5346{
5347 linenr_T lnum;
5348 linenr_T end;
5349 int retlist;
5350
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005351 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005352 if (argvars[1].v_type == VAR_UNKNOWN)
5353 {
5354 end = 0;
5355 retlist = FALSE;
5356 }
5357 else
5358 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005359 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005360 retlist = TRUE;
5361 }
5362
5363 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5364}
5365
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005366#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005367 static void
5368get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5369{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005370 if (what_arg->v_type == VAR_UNKNOWN)
5371 {
5372 if (rettv_list_alloc(rettv) == OK)
5373 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005374 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005375 }
5376 else
5377 {
5378 if (rettv_dict_alloc(rettv) == OK)
5379 if (is_qf || (wp != NULL))
5380 {
5381 if (what_arg->v_type == VAR_DICT)
5382 {
5383 dict_T *d = what_arg->vval.v_dict;
5384
5385 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005386 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005387 }
5388 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005389 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005390 }
5391 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005392}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005393#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005394
5395/*
5396 * "getloclist()" function
5397 */
5398 static void
5399f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5400{
5401#ifdef FEAT_QUICKFIX
5402 win_T *wp;
5403
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005404 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005405 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5406#endif
5407}
5408
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005409/*
5410 * "getmatches()" function
5411 */
5412 static void
5413f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5414{
5415#ifdef FEAT_SEARCH_EXTRA
5416 dict_T *dict;
5417 matchitem_T *cur = curwin->w_match_head;
5418 int i;
5419
5420 if (rettv_list_alloc(rettv) == OK)
5421 {
5422 while (cur != NULL)
5423 {
5424 dict = dict_alloc();
5425 if (dict == NULL)
5426 return;
5427 if (cur->match.regprog == NULL)
5428 {
5429 /* match added with matchaddpos() */
5430 for (i = 0; i < MAXPOSMATCH; ++i)
5431 {
5432 llpos_T *llpos;
5433 char buf[6];
5434 list_T *l;
5435
5436 llpos = &cur->pos.pos[i];
5437 if (llpos->lnum == 0)
5438 break;
5439 l = list_alloc();
5440 if (l == NULL)
5441 break;
5442 list_append_number(l, (varnumber_T)llpos->lnum);
5443 if (llpos->col > 0)
5444 {
5445 list_append_number(l, (varnumber_T)llpos->col);
5446 list_append_number(l, (varnumber_T)llpos->len);
5447 }
5448 sprintf(buf, "pos%d", i + 1);
5449 dict_add_list(dict, buf, l);
5450 }
5451 }
5452 else
5453 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005454 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005455 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005456 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5457 dict_add_number(dict, "priority", (long)cur->priority);
5458 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar13505972019-01-24 15:04:48 +01005459# if defined(FEAT_CONCEAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005460 if (cur->conceal_char)
5461 {
5462 char_u buf[MB_MAXBYTES + 1];
5463
5464 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005465 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005466 }
5467# endif
5468 list_append_dict(rettv->vval.v_list, dict);
5469 cur = cur->next;
5470 }
5471 }
5472#endif
5473}
5474
5475/*
5476 * "getpid()" function
5477 */
5478 static void
5479f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5480{
5481 rettv->vval.v_number = mch_get_pid();
5482}
5483
5484 static void
5485getpos_both(
5486 typval_T *argvars,
5487 typval_T *rettv,
5488 int getcurpos)
5489{
5490 pos_T *fp;
5491 list_T *l;
5492 int fnum = -1;
5493
5494 if (rettv_list_alloc(rettv) == OK)
5495 {
5496 l = rettv->vval.v_list;
5497 if (getcurpos)
5498 fp = &curwin->w_cursor;
5499 else
5500 fp = var2fpos(&argvars[0], TRUE, &fnum);
5501 if (fnum != -1)
5502 list_append_number(l, (varnumber_T)fnum);
5503 else
5504 list_append_number(l, (varnumber_T)0);
5505 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5506 : (varnumber_T)0);
5507 list_append_number(l, (fp != NULL)
5508 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5509 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005510 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005511 (varnumber_T)0);
5512 if (getcurpos)
5513 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005514 int save_set_curswant = curwin->w_set_curswant;
5515 colnr_T save_curswant = curwin->w_curswant;
5516 colnr_T save_virtcol = curwin->w_virtcol;
5517
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005518 update_curswant();
5519 list_append_number(l, curwin->w_curswant == MAXCOL ?
5520 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005521
5522 // Do not change "curswant", as it is unexpected that a get
5523 // function has a side effect.
5524 if (save_set_curswant)
5525 {
5526 curwin->w_set_curswant = save_set_curswant;
5527 curwin->w_curswant = save_curswant;
5528 curwin->w_virtcol = save_virtcol;
5529 curwin->w_valid &= ~VALID_VIRTCOL;
5530 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005531 }
5532 }
5533 else
5534 rettv->vval.v_number = FALSE;
5535}
5536
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005537/*
5538 * "getcurpos()" function
5539 */
5540 static void
5541f_getcurpos(typval_T *argvars, typval_T *rettv)
5542{
5543 getpos_both(argvars, rettv, TRUE);
5544}
5545
5546/*
5547 * "getpos(string)" function
5548 */
5549 static void
5550f_getpos(typval_T *argvars, typval_T *rettv)
5551{
5552 getpos_both(argvars, rettv, FALSE);
5553}
5554
5555/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005556 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557 */
5558 static void
5559f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5560{
5561#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005562 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005563#endif
5564}
5565
5566/*
5567 * "getreg()" function
5568 */
5569 static void
5570f_getreg(typval_T *argvars, typval_T *rettv)
5571{
5572 char_u *strregname;
5573 int regname;
5574 int arg2 = FALSE;
5575 int return_list = FALSE;
5576 int error = FALSE;
5577
5578 if (argvars[0].v_type != VAR_UNKNOWN)
5579 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005580 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005581 error = strregname == NULL;
5582 if (argvars[1].v_type != VAR_UNKNOWN)
5583 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005584 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005585 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005586 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005587 }
5588 }
5589 else
5590 strregname = get_vim_var_str(VV_REG);
5591
5592 if (error)
5593 return;
5594
5595 regname = (strregname == NULL ? '"' : *strregname);
5596 if (regname == 0)
5597 regname = '"';
5598
5599 if (return_list)
5600 {
5601 rettv->v_type = VAR_LIST;
5602 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5603 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5604 if (rettv->vval.v_list == NULL)
5605 (void)rettv_list_alloc(rettv);
5606 else
5607 ++rettv->vval.v_list->lv_refcount;
5608 }
5609 else
5610 {
5611 rettv->v_type = VAR_STRING;
5612 rettv->vval.v_string = get_reg_contents(regname,
5613 arg2 ? GREG_EXPR_SRC : 0);
5614 }
5615}
5616
5617/*
5618 * "getregtype()" function
5619 */
5620 static void
5621f_getregtype(typval_T *argvars, typval_T *rettv)
5622{
5623 char_u *strregname;
5624 int regname;
5625 char_u buf[NUMBUFLEN + 2];
5626 long reglen = 0;
5627
5628 if (argvars[0].v_type != VAR_UNKNOWN)
5629 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005630 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005631 if (strregname == NULL) /* type error; errmsg already given */
5632 {
5633 rettv->v_type = VAR_STRING;
5634 rettv->vval.v_string = NULL;
5635 return;
5636 }
5637 }
5638 else
5639 /* Default to v:register */
5640 strregname = get_vim_var_str(VV_REG);
5641
5642 regname = (strregname == NULL ? '"' : *strregname);
5643 if (regname == 0)
5644 regname = '"';
5645
5646 buf[0] = NUL;
5647 buf[1] = NUL;
5648 switch (get_reg_type(regname, &reglen))
5649 {
5650 case MLINE: buf[0] = 'V'; break;
5651 case MCHAR: buf[0] = 'v'; break;
5652 case MBLOCK:
5653 buf[0] = Ctrl_V;
5654 sprintf((char *)buf + 1, "%ld", reglen + 1);
5655 break;
5656 }
5657 rettv->v_type = VAR_STRING;
5658 rettv->vval.v_string = vim_strsave(buf);
5659}
5660
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005661/*
5662 * Returns information (variables, options, etc.) about a tab page
5663 * as a dictionary.
5664 */
5665 static dict_T *
5666get_tabpage_info(tabpage_T *tp, int tp_idx)
5667{
5668 win_T *wp;
5669 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005670 list_T *l;
5671
5672 dict = dict_alloc();
5673 if (dict == NULL)
5674 return NULL;
5675
Bram Moolenaare0be1672018-07-08 16:50:37 +02005676 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005677
5678 l = list_alloc();
5679 if (l != NULL)
5680 {
5681 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5682 wp; wp = wp->w_next)
5683 list_append_number(l, (varnumber_T)wp->w_id);
5684 dict_add_list(dict, "windows", l);
5685 }
5686
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005687 /* Make a reference to tabpage variables */
5688 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005689
5690 return dict;
5691}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005692
5693/*
5694 * "gettabinfo()" function
5695 */
5696 static void
5697f_gettabinfo(typval_T *argvars, typval_T *rettv)
5698{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005699 tabpage_T *tp, *tparg = NULL;
5700 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005701 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005702
5703 if (rettv_list_alloc(rettv) != OK)
5704 return;
5705
5706 if (argvars[0].v_type != VAR_UNKNOWN)
5707 {
5708 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005709 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005710 if (tparg == NULL)
5711 return;
5712 }
5713
5714 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005715 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005716 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005717 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005718 if (tparg != NULL && tp != tparg)
5719 continue;
5720 d = get_tabpage_info(tp, tpnr);
5721 if (d != NULL)
5722 list_append_dict(rettv->vval.v_list, d);
5723 if (tparg != NULL)
5724 return;
5725 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005726}
5727
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005728/*
5729 * "gettabvar()" function
5730 */
5731 static void
5732f_gettabvar(typval_T *argvars, typval_T *rettv)
5733{
5734 win_T *oldcurwin;
5735 tabpage_T *tp, *oldtabpage;
5736 dictitem_T *v;
5737 char_u *varname;
5738 int done = FALSE;
5739
5740 rettv->v_type = VAR_STRING;
5741 rettv->vval.v_string = NULL;
5742
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005743 varname = tv_get_string_chk(&argvars[1]);
5744 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005745 if (tp != NULL && varname != NULL)
5746 {
5747 /* Set tp to be our tabpage, temporarily. Also set the window to the
5748 * first window in the tabpage, otherwise the window is not valid. */
5749 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005750 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5751 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005752 {
5753 /* look up the variable */
5754 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5755 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5756 if (v != NULL)
5757 {
5758 copy_tv(&v->di_tv, rettv);
5759 done = TRUE;
5760 }
5761 }
5762
5763 /* restore previous notion of curwin */
5764 restore_win(oldcurwin, oldtabpage, TRUE);
5765 }
5766
5767 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5768 copy_tv(&argvars[2], rettv);
5769}
5770
5771/*
5772 * "gettabwinvar()" function
5773 */
5774 static void
5775f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5776{
5777 getwinvar(argvars, rettv, 1);
5778}
5779
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005780/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005781 * "gettagstack()" function
5782 */
5783 static void
5784f_gettagstack(typval_T *argvars, typval_T *rettv)
5785{
5786 win_T *wp = curwin; // default is current window
5787
5788 if (rettv_dict_alloc(rettv) != OK)
5789 return;
5790
5791 if (argvars[0].v_type != VAR_UNKNOWN)
5792 {
5793 wp = find_win_by_nr_or_id(&argvars[0]);
5794 if (wp == NULL)
5795 return;
5796 }
5797
5798 get_tagstack(wp, rettv->vval.v_dict);
5799}
5800
5801/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005802 * Returns information about a window as a dictionary.
5803 */
5804 static dict_T *
5805get_win_info(win_T *wp, short tpnr, short winnr)
5806{
5807 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005808
5809 dict = dict_alloc();
5810 if (dict == NULL)
5811 return NULL;
5812
Bram Moolenaare0be1672018-07-08 16:50:37 +02005813 dict_add_number(dict, "tabnr", tpnr);
5814 dict_add_number(dict, "winnr", winnr);
5815 dict_add_number(dict, "winid", wp->w_id);
5816 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005817 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005818 dict_add_number(dict, "topline", wp->w_topline);
5819 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005820#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005821 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005822#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005823 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005824 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005825 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005826
Bram Moolenaar69905d12017-08-13 18:14:47 +02005827#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005828 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005829#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005830#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005831 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5832 dict_add_number(dict, "loclist",
5833 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005834#endif
5835
Bram Moolenaar30567352016-08-27 21:25:44 +02005836 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005837 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005838
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005839 return dict;
5840}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005841
5842/*
5843 * "getwininfo()" function
5844 */
5845 static void
5846f_getwininfo(typval_T *argvars, typval_T *rettv)
5847{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005848 tabpage_T *tp;
5849 win_T *wp = NULL, *wparg = NULL;
5850 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005851 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005852
5853 if (rettv_list_alloc(rettv) != OK)
5854 return;
5855
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005856 if (argvars[0].v_type != VAR_UNKNOWN)
5857 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005858 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005859 if (wparg == NULL)
5860 return;
5861 }
5862
5863 /* Collect information about either all the windows across all the tab
5864 * pages or one particular window.
5865 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005866 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005867 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005868 tabnr++;
5869 winnr = 0;
5870 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005871 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005872 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005873 if (wparg != NULL && wp != wparg)
5874 continue;
5875 d = get_win_info(wp, tabnr, winnr);
5876 if (d != NULL)
5877 list_append_dict(rettv->vval.v_list, d);
5878 if (wparg != NULL)
5879 /* found information about a specific window */
5880 return;
5881 }
5882 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005883}
5884
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005885/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005886 * "win_findbuf()" function
5887 */
5888 static void
5889f_win_findbuf(typval_T *argvars, typval_T *rettv)
5890{
5891 if (rettv_list_alloc(rettv) != FAIL)
5892 win_findbuf(argvars, rettv->vval.v_list);
5893}
5894
5895/*
5896 * "win_getid()" function
5897 */
5898 static void
5899f_win_getid(typval_T *argvars, typval_T *rettv)
5900{
5901 rettv->vval.v_number = win_getid(argvars);
5902}
5903
5904/*
5905 * "win_gotoid()" function
5906 */
5907 static void
5908f_win_gotoid(typval_T *argvars, typval_T *rettv)
5909{
5910 rettv->vval.v_number = win_gotoid(argvars);
5911}
5912
5913/*
5914 * "win_id2tabwin()" function
5915 */
5916 static void
5917f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5918{
5919 if (rettv_list_alloc(rettv) != FAIL)
5920 win_id2tabwin(argvars, rettv->vval.v_list);
5921}
5922
5923/*
5924 * "win_id2win()" function
5925 */
5926 static void
5927f_win_id2win(typval_T *argvars, typval_T *rettv)
5928{
5929 rettv->vval.v_number = win_id2win(argvars);
5930}
5931
5932/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005933 * "win_screenpos()" function
5934 */
5935 static void
5936f_win_screenpos(typval_T *argvars, typval_T *rettv)
5937{
5938 win_T *wp;
5939
5940 if (rettv_list_alloc(rettv) == FAIL)
5941 return;
5942
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005943 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005944 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5945 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5946}
5947
5948/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005949 * "getwinpos({timeout})" function
5950 */
5951 static void
5952f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5953{
5954 int x = -1;
5955 int y = -1;
5956
5957 if (rettv_list_alloc(rettv) == FAIL)
5958 return;
5959#ifdef FEAT_GUI
5960 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005961 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005962# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5963 else
5964# endif
5965#endif
5966#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5967 {
5968 varnumber_T timeout = 100;
5969
5970 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005971 timeout = tv_get_number(&argvars[0]);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005972 term_get_winpos(&x, &y, timeout);
5973 }
5974#endif
5975 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5976 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5977}
5978
5979
5980/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005981 * "getwinposx()" function
5982 */
5983 static void
5984f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5985{
5986 rettv->vval.v_number = -1;
5987#ifdef FEAT_GUI
5988 if (gui.in_use)
5989 {
5990 int x, y;
5991
5992 if (gui_mch_get_winpos(&x, &y) == OK)
5993 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005994 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005995 }
5996#endif
5997#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5998 {
5999 int x, y;
6000
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006001 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006002 rettv->vval.v_number = x;
6003 }
6004#endif
6005}
6006
6007/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006008 * "getwinposy()" function
6009 */
6010 static void
6011f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6012{
6013 rettv->vval.v_number = -1;
6014#ifdef FEAT_GUI
6015 if (gui.in_use)
6016 {
6017 int x, y;
6018
6019 if (gui_mch_get_winpos(&x, &y) == OK)
6020 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02006021 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006022 }
6023#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006024#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
6025 {
6026 int x, y;
6027
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006028 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006029 rettv->vval.v_number = y;
6030 }
6031#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006032}
6033
6034/*
6035 * "getwinvar()" function
6036 */
6037 static void
6038f_getwinvar(typval_T *argvars, typval_T *rettv)
6039{
6040 getwinvar(argvars, rettv, 0);
6041}
6042
6043/*
6044 * "glob()" function
6045 */
6046 static void
6047f_glob(typval_T *argvars, typval_T *rettv)
6048{
6049 int options = WILD_SILENT|WILD_USE_NL;
6050 expand_T xpc;
6051 int error = FALSE;
6052
6053 /* When the optional second argument is non-zero, don't remove matches
6054 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6055 rettv->v_type = VAR_STRING;
6056 if (argvars[1].v_type != VAR_UNKNOWN)
6057 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006058 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006059 options |= WILD_KEEP_ALL;
6060 if (argvars[2].v_type != VAR_UNKNOWN)
6061 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006062 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006063 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006064 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006065 }
6066 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006067 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006068 options |= WILD_ALLLINKS;
6069 }
6070 }
6071 if (!error)
6072 {
6073 ExpandInit(&xpc);
6074 xpc.xp_context = EXPAND_FILES;
6075 if (p_wic)
6076 options += WILD_ICASE;
6077 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006078 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006079 NULL, options, WILD_ALL);
6080 else if (rettv_list_alloc(rettv) != FAIL)
6081 {
6082 int i;
6083
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006084 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006085 NULL, options, WILD_ALL_KEEP);
6086 for (i = 0; i < xpc.xp_numfiles; i++)
6087 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6088
6089 ExpandCleanup(&xpc);
6090 }
6091 }
6092 else
6093 rettv->vval.v_string = NULL;
6094}
6095
6096/*
6097 * "globpath()" function
6098 */
6099 static void
6100f_globpath(typval_T *argvars, typval_T *rettv)
6101{
6102 int flags = 0;
6103 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006104 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006105 int error = FALSE;
6106 garray_T ga;
6107 int i;
6108
6109 /* When the optional second argument is non-zero, don't remove matches
6110 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6111 rettv->v_type = VAR_STRING;
6112 if (argvars[2].v_type != VAR_UNKNOWN)
6113 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006114 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006115 flags |= WILD_KEEP_ALL;
6116 if (argvars[3].v_type != VAR_UNKNOWN)
6117 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006118 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006119 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006120 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006121 }
6122 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006123 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006124 flags |= WILD_ALLLINKS;
6125 }
6126 }
6127 if (file != NULL && !error)
6128 {
6129 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006130 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006131 if (rettv->v_type == VAR_STRING)
6132 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6133 else if (rettv_list_alloc(rettv) != FAIL)
6134 for (i = 0; i < ga.ga_len; ++i)
6135 list_append_string(rettv->vval.v_list,
6136 ((char_u **)(ga.ga_data))[i], -1);
6137 ga_clear_strings(&ga);
6138 }
6139 else
6140 rettv->vval.v_string = NULL;
6141}
6142
6143/*
6144 * "glob2regpat()" function
6145 */
6146 static void
6147f_glob2regpat(typval_T *argvars, typval_T *rettv)
6148{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006149 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006150
6151 rettv->v_type = VAR_STRING;
6152 rettv->vval.v_string = (pat == NULL)
6153 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6154}
6155
6156/* for VIM_VERSION_ defines */
6157#include "version.h"
6158
6159/*
6160 * "has()" function
6161 */
6162 static void
6163f_has(typval_T *argvars, typval_T *rettv)
6164{
6165 int i;
6166 char_u *name;
6167 int n = FALSE;
6168 static char *(has_list[]) =
6169 {
6170#ifdef AMIGA
6171 "amiga",
6172# ifdef FEAT_ARP
6173 "arp",
6174# endif
6175#endif
6176#ifdef __BEOS__
6177 "beos",
6178#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006179#if defined(BSD) && !defined(MACOS_X)
6180 "bsd",
6181#endif
6182#ifdef hpux
6183 "hpux",
6184#endif
6185#ifdef __linux__
6186 "linux",
6187#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006188#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006189 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6190 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006191# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006192 "macunix", /* Mac OS X, with the darwin feature */
6193 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006194# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006195#endif
6196#ifdef __QNX__
6197 "qnx",
6198#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006199#ifdef SUN_SYSTEM
6200 "sun",
6201#else
6202 "moon",
6203#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006204#ifdef UNIX
6205 "unix",
6206#endif
6207#ifdef VMS
6208 "vms",
6209#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006210#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006211 "win32",
6212#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006213#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006214 "win32unix",
6215#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006216#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006217 "win64",
6218#endif
6219#ifdef EBCDIC
6220 "ebcdic",
6221#endif
6222#ifndef CASE_INSENSITIVE_FILENAME
6223 "fname_case",
6224#endif
6225#ifdef HAVE_ACL
6226 "acl",
6227#endif
6228#ifdef FEAT_ARABIC
6229 "arabic",
6230#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006231 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006232#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006233 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006234#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006235#ifdef FEAT_AUTOSERVERNAME
6236 "autoservername",
6237#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006238#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006239 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006240# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006241 "balloon_multiline",
6242# endif
6243#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006244#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006245 "balloon_eval_term",
6246#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6248 "builtin_terms",
6249# ifdef ALL_BUILTIN_TCAPS
6250 "all_builtin_terms",
6251# endif
6252#endif
6253#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006254 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006255 || defined(FEAT_GUI_MOTIF))
6256 "browsefilter",
6257#endif
6258#ifdef FEAT_BYTEOFF
6259 "byte_offset",
6260#endif
6261#ifdef FEAT_JOB_CHANNEL
6262 "channel",
6263#endif
6264#ifdef FEAT_CINDENT
6265 "cindent",
6266#endif
6267#ifdef FEAT_CLIENTSERVER
6268 "clientserver",
6269#endif
6270#ifdef FEAT_CLIPBOARD
6271 "clipboard",
6272#endif
6273#ifdef FEAT_CMDL_COMPL
6274 "cmdline_compl",
6275#endif
6276#ifdef FEAT_CMDHIST
6277 "cmdline_hist",
6278#endif
6279#ifdef FEAT_COMMENTS
6280 "comments",
6281#endif
6282#ifdef FEAT_CONCEAL
6283 "conceal",
6284#endif
6285#ifdef FEAT_CRYPT
6286 "cryptv",
6287 "crypt-blowfish",
6288 "crypt-blowfish2",
6289#endif
6290#ifdef FEAT_CSCOPE
6291 "cscope",
6292#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006293 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006294#ifdef CURSOR_SHAPE
6295 "cursorshape",
6296#endif
6297#ifdef DEBUG
6298 "debug",
6299#endif
6300#ifdef FEAT_CON_DIALOG
6301 "dialog_con",
6302#endif
6303#ifdef FEAT_GUI_DIALOG
6304 "dialog_gui",
6305#endif
6306#ifdef FEAT_DIFF
6307 "diff",
6308#endif
6309#ifdef FEAT_DIGRAPHS
6310 "digraphs",
6311#endif
6312#ifdef FEAT_DIRECTX
6313 "directx",
6314#endif
6315#ifdef FEAT_DND
6316 "dnd",
6317#endif
6318#ifdef FEAT_EMACS_TAGS
6319 "emacs_tags",
6320#endif
6321 "eval", /* always present, of course! */
6322 "ex_extra", /* graduated feature */
6323#ifdef FEAT_SEARCH_EXTRA
6324 "extra_search",
6325#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006326#ifdef FEAT_SEARCHPATH
6327 "file_in_path",
6328#endif
6329#ifdef FEAT_FILTERPIPE
6330 "filterpipe",
6331#endif
6332#ifdef FEAT_FIND_ID
6333 "find_in_path",
6334#endif
6335#ifdef FEAT_FLOAT
6336 "float",
6337#endif
6338#ifdef FEAT_FOLDING
6339 "folding",
6340#endif
6341#ifdef FEAT_FOOTER
6342 "footer",
6343#endif
6344#if !defined(USE_SYSTEM) && defined(UNIX)
6345 "fork",
6346#endif
6347#ifdef FEAT_GETTEXT
6348 "gettext",
6349#endif
6350#ifdef FEAT_GUI
6351 "gui",
6352#endif
6353#ifdef FEAT_GUI_ATHENA
6354# ifdef FEAT_GUI_NEXTAW
6355 "gui_neXtaw",
6356# else
6357 "gui_athena",
6358# endif
6359#endif
6360#ifdef FEAT_GUI_GTK
6361 "gui_gtk",
6362# ifdef USE_GTK3
6363 "gui_gtk3",
6364# else
6365 "gui_gtk2",
6366# endif
6367#endif
6368#ifdef FEAT_GUI_GNOME
6369 "gui_gnome",
6370#endif
6371#ifdef FEAT_GUI_MAC
6372 "gui_mac",
6373#endif
6374#ifdef FEAT_GUI_MOTIF
6375 "gui_motif",
6376#endif
6377#ifdef FEAT_GUI_PHOTON
6378 "gui_photon",
6379#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006380#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006381 "gui_win32",
6382#endif
6383#ifdef FEAT_HANGULIN
6384 "hangul_input",
6385#endif
6386#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6387 "iconv",
6388#endif
6389#ifdef FEAT_INS_EXPAND
6390 "insert_expand",
6391#endif
6392#ifdef FEAT_JOB_CHANNEL
6393 "job",
6394#endif
6395#ifdef FEAT_JUMPLIST
6396 "jumplist",
6397#endif
6398#ifdef FEAT_KEYMAP
6399 "keymap",
6400#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006401 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006402#ifdef FEAT_LANGMAP
6403 "langmap",
6404#endif
6405#ifdef FEAT_LIBCALL
6406 "libcall",
6407#endif
6408#ifdef FEAT_LINEBREAK
6409 "linebreak",
6410#endif
6411#ifdef FEAT_LISP
6412 "lispindent",
6413#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006414 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006415#ifdef FEAT_LOCALMAP
6416 "localmap",
6417#endif
6418#ifdef FEAT_LUA
6419# ifndef DYNAMIC_LUA
6420 "lua",
6421# endif
6422#endif
6423#ifdef FEAT_MENU
6424 "menu",
6425#endif
6426#ifdef FEAT_SESSION
6427 "mksession",
6428#endif
6429#ifdef FEAT_MODIFY_FNAME
6430 "modify_fname",
6431#endif
6432#ifdef FEAT_MOUSE
6433 "mouse",
6434#endif
6435#ifdef FEAT_MOUSESHAPE
6436 "mouseshape",
6437#endif
6438#if defined(UNIX) || defined(VMS)
6439# ifdef FEAT_MOUSE_DEC
6440 "mouse_dec",
6441# endif
6442# ifdef FEAT_MOUSE_GPM
6443 "mouse_gpm",
6444# endif
6445# ifdef FEAT_MOUSE_JSB
6446 "mouse_jsbterm",
6447# endif
6448# ifdef FEAT_MOUSE_NET
6449 "mouse_netterm",
6450# endif
6451# ifdef FEAT_MOUSE_PTERM
6452 "mouse_pterm",
6453# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006454# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006455 "mouse_sgr",
6456# endif
6457# ifdef FEAT_SYSMOUSE
6458 "mouse_sysmouse",
6459# endif
6460# ifdef FEAT_MOUSE_URXVT
6461 "mouse_urxvt",
6462# endif
6463# ifdef FEAT_MOUSE_XTERM
6464 "mouse_xterm",
6465# endif
6466#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006467 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006468#ifdef FEAT_MBYTE_IME
6469 "multi_byte_ime",
6470#endif
6471#ifdef FEAT_MULTI_LANG
6472 "multi_lang",
6473#endif
6474#ifdef FEAT_MZSCHEME
6475#ifndef DYNAMIC_MZSCHEME
6476 "mzscheme",
6477#endif
6478#endif
6479#ifdef FEAT_NUM64
6480 "num64",
6481#endif
6482#ifdef FEAT_OLE
6483 "ole",
6484#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006485#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006486 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006487#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006488#ifdef FEAT_PATH_EXTRA
6489 "path_extra",
6490#endif
6491#ifdef FEAT_PERL
6492#ifndef DYNAMIC_PERL
6493 "perl",
6494#endif
6495#endif
6496#ifdef FEAT_PERSISTENT_UNDO
6497 "persistent_undo",
6498#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006499#if defined(FEAT_PYTHON)
6500 "python_compiled",
6501# if defined(DYNAMIC_PYTHON)
6502 "python_dynamic",
6503# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006504 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006505 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006506# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006507#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006508#if defined(FEAT_PYTHON3)
6509 "python3_compiled",
6510# if defined(DYNAMIC_PYTHON3)
6511 "python3_dynamic",
6512# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006514 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006515# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516#endif
6517#ifdef FEAT_POSTSCRIPT
6518 "postscript",
6519#endif
6520#ifdef FEAT_PRINTER
6521 "printer",
6522#endif
6523#ifdef FEAT_PROFILE
6524 "profile",
6525#endif
6526#ifdef FEAT_RELTIME
6527 "reltime",
6528#endif
6529#ifdef FEAT_QUICKFIX
6530 "quickfix",
6531#endif
6532#ifdef FEAT_RIGHTLEFT
6533 "rightleft",
6534#endif
6535#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6536 "ruby",
6537#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006538 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006539#ifdef FEAT_CMDL_INFO
6540 "showcmd",
6541 "cmdline_info",
6542#endif
6543#ifdef FEAT_SIGNS
6544 "signs",
6545#endif
6546#ifdef FEAT_SMARTINDENT
6547 "smartindent",
6548#endif
6549#ifdef STARTUPTIME
6550 "startuptime",
6551#endif
6552#ifdef FEAT_STL_OPT
6553 "statusline",
6554#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006555#ifdef FEAT_NETBEANS_INTG
6556 "netbeans_intg",
6557#endif
6558#ifdef FEAT_SPELL
6559 "spell",
6560#endif
6561#ifdef FEAT_SYN_HL
6562 "syntax",
6563#endif
6564#if defined(USE_SYSTEM) || !defined(UNIX)
6565 "system",
6566#endif
6567#ifdef FEAT_TAG_BINS
6568 "tag_binary",
6569#endif
6570#ifdef FEAT_TAG_OLDSTATIC
6571 "tag_old_static",
6572#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006573#ifdef FEAT_TCL
6574# ifndef DYNAMIC_TCL
6575 "tcl",
6576# endif
6577#endif
6578#ifdef FEAT_TERMGUICOLORS
6579 "termguicolors",
6580#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006581#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006582 "terminal",
6583#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006584#ifdef TERMINFO
6585 "terminfo",
6586#endif
6587#ifdef FEAT_TERMRESPONSE
6588 "termresponse",
6589#endif
6590#ifdef FEAT_TEXTOBJ
6591 "textobjects",
6592#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006593#ifdef FEAT_TEXT_PROP
6594 "textprop",
6595#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006596#ifdef HAVE_TGETENT
6597 "tgetent",
6598#endif
6599#ifdef FEAT_TIMERS
6600 "timers",
6601#endif
6602#ifdef FEAT_TITLE
6603 "title",
6604#endif
6605#ifdef FEAT_TOOLBAR
6606 "toolbar",
6607#endif
6608#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6609 "unnamedplus",
6610#endif
6611#ifdef FEAT_USR_CMDS
6612 "user-commands", /* was accidentally included in 5.4 */
6613 "user_commands",
6614#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006615#ifdef FEAT_VARTABS
6616 "vartabs",
6617#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006618#ifdef FEAT_VIMINFO
6619 "viminfo",
6620#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006621 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006622 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006623 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006624 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006625 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006626#ifdef FEAT_VTP
6627 "vtp",
6628#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006629#ifdef FEAT_WILDIGN
6630 "wildignore",
6631#endif
6632#ifdef FEAT_WILDMENU
6633 "wildmenu",
6634#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006635 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006636#ifdef FEAT_WAK
6637 "winaltkeys",
6638#endif
6639#ifdef FEAT_WRITEBACKUP
6640 "writebackup",
6641#endif
6642#ifdef FEAT_XIM
6643 "xim",
6644#endif
6645#ifdef FEAT_XFONTSET
6646 "xfontset",
6647#endif
6648#ifdef FEAT_XPM_W32
6649 "xpm",
6650 "xpm_w32", /* for backward compatibility */
6651#else
6652# if defined(HAVE_XPM)
6653 "xpm",
6654# endif
6655#endif
6656#ifdef USE_XSMP
6657 "xsmp",
6658#endif
6659#ifdef USE_XSMP_INTERACT
6660 "xsmp_interact",
6661#endif
6662#ifdef FEAT_XCLIPBOARD
6663 "xterm_clipboard",
6664#endif
6665#ifdef FEAT_XTERM_SAVE
6666 "xterm_save",
6667#endif
6668#if defined(UNIX) && defined(FEAT_X11)
6669 "X11",
6670#endif
6671 NULL
6672 };
6673
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006674 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006675 for (i = 0; has_list[i] != NULL; ++i)
6676 if (STRICMP(name, has_list[i]) == 0)
6677 {
6678 n = TRUE;
6679 break;
6680 }
6681
6682 if (n == FALSE)
6683 {
6684 if (STRNICMP(name, "patch", 5) == 0)
6685 {
6686 if (name[5] == '-'
6687 && STRLEN(name) >= 11
6688 && vim_isdigit(name[6])
6689 && vim_isdigit(name[8])
6690 && vim_isdigit(name[10]))
6691 {
6692 int major = atoi((char *)name + 6);
6693 int minor = atoi((char *)name + 8);
6694
6695 /* Expect "patch-9.9.01234". */
6696 n = (major < VIM_VERSION_MAJOR
6697 || (major == VIM_VERSION_MAJOR
6698 && (minor < VIM_VERSION_MINOR
6699 || (minor == VIM_VERSION_MINOR
6700 && has_patch(atoi((char *)name + 10))))));
6701 }
6702 else
6703 n = has_patch(atoi((char *)name + 5));
6704 }
6705 else if (STRICMP(name, "vim_starting") == 0)
6706 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006707 else if (STRICMP(name, "ttyin") == 0)
6708 n = mch_input_isatty();
6709 else if (STRICMP(name, "ttyout") == 0)
6710 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006711 else if (STRICMP(name, "multi_byte_encoding") == 0)
6712 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006713#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006714 else if (STRICMP(name, "balloon_multiline") == 0)
6715 n = multiline_balloon_available();
6716#endif
6717#ifdef DYNAMIC_TCL
6718 else if (STRICMP(name, "tcl") == 0)
6719 n = tcl_enabled(FALSE);
6720#endif
6721#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6722 else if (STRICMP(name, "iconv") == 0)
6723 n = iconv_enabled(FALSE);
6724#endif
6725#ifdef DYNAMIC_LUA
6726 else if (STRICMP(name, "lua") == 0)
6727 n = lua_enabled(FALSE);
6728#endif
6729#ifdef DYNAMIC_MZSCHEME
6730 else if (STRICMP(name, "mzscheme") == 0)
6731 n = mzscheme_enabled(FALSE);
6732#endif
6733#ifdef DYNAMIC_RUBY
6734 else if (STRICMP(name, "ruby") == 0)
6735 n = ruby_enabled(FALSE);
6736#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006737#ifdef DYNAMIC_PYTHON
6738 else if (STRICMP(name, "python") == 0)
6739 n = python_enabled(FALSE);
6740#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006741#ifdef DYNAMIC_PYTHON3
6742 else if (STRICMP(name, "python3") == 0)
6743 n = python3_enabled(FALSE);
6744#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006745#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6746 else if (STRICMP(name, "pythonx") == 0)
6747 {
6748# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6749 if (p_pyx == 0)
6750 n = python3_enabled(FALSE) || python_enabled(FALSE);
6751 else if (p_pyx == 3)
6752 n = python3_enabled(FALSE);
6753 else if (p_pyx == 2)
6754 n = python_enabled(FALSE);
6755# elif defined(DYNAMIC_PYTHON)
6756 n = python_enabled(FALSE);
6757# elif defined(DYNAMIC_PYTHON3)
6758 n = python3_enabled(FALSE);
6759# endif
6760 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006761#endif
6762#ifdef DYNAMIC_PERL
6763 else if (STRICMP(name, "perl") == 0)
6764 n = perl_enabled(FALSE);
6765#endif
6766#ifdef FEAT_GUI
6767 else if (STRICMP(name, "gui_running") == 0)
6768 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006769# ifdef FEAT_BROWSE
6770 else if (STRICMP(name, "browse") == 0)
6771 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6772# endif
6773#endif
6774#ifdef FEAT_SYN_HL
6775 else if (STRICMP(name, "syntax_items") == 0)
6776 n = syntax_present(curwin);
6777#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006778#ifdef FEAT_VTP
6779 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006780 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006781#endif
6782#ifdef FEAT_NETBEANS_INTG
6783 else if (STRICMP(name, "netbeans_enabled") == 0)
6784 n = netbeans_active();
6785#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006786#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006787 else if (STRICMP(name, "terminal") == 0)
6788 n = terminal_enabled();
6789#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006790#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006791 else if (STRICMP(name, "conpty") == 0)
6792 n = use_conpty();
6793#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006794 }
6795
6796 rettv->vval.v_number = n;
6797}
6798
6799/*
6800 * "has_key()" function
6801 */
6802 static void
6803f_has_key(typval_T *argvars, typval_T *rettv)
6804{
6805 if (argvars[0].v_type != VAR_DICT)
6806 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006807 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006808 return;
6809 }
6810 if (argvars[0].vval.v_dict == NULL)
6811 return;
6812
6813 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006814 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006815}
6816
6817/*
6818 * "haslocaldir()" function
6819 */
6820 static void
6821f_haslocaldir(typval_T *argvars, typval_T *rettv)
6822{
6823 win_T *wp = NULL;
6824
6825 wp = find_tabwin(&argvars[0], &argvars[1]);
6826 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6827}
6828
6829/*
6830 * "hasmapto()" function
6831 */
6832 static void
6833f_hasmapto(typval_T *argvars, typval_T *rettv)
6834{
6835 char_u *name;
6836 char_u *mode;
6837 char_u buf[NUMBUFLEN];
6838 int abbr = FALSE;
6839
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006840 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006841 if (argvars[1].v_type == VAR_UNKNOWN)
6842 mode = (char_u *)"nvo";
6843 else
6844 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006845 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006846 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006847 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006848 }
6849
6850 if (map_to_exists(name, mode, abbr))
6851 rettv->vval.v_number = TRUE;
6852 else
6853 rettv->vval.v_number = FALSE;
6854}
6855
6856/*
6857 * "histadd()" function
6858 */
6859 static void
6860f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6861{
6862#ifdef FEAT_CMDHIST
6863 int histype;
6864 char_u *str;
6865 char_u buf[NUMBUFLEN];
6866#endif
6867
6868 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006869 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006870 return;
6871#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006872 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006873 histype = str != NULL ? get_histtype(str) : -1;
6874 if (histype >= 0)
6875 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006876 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006877 if (*str != NUL)
6878 {
6879 init_history();
6880 add_to_history(histype, str, FALSE, NUL);
6881 rettv->vval.v_number = TRUE;
6882 return;
6883 }
6884 }
6885#endif
6886}
6887
6888/*
6889 * "histdel()" function
6890 */
6891 static void
6892f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6893{
6894#ifdef FEAT_CMDHIST
6895 int n;
6896 char_u buf[NUMBUFLEN];
6897 char_u *str;
6898
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006899 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 if (str == NULL)
6901 n = 0;
6902 else if (argvars[1].v_type == VAR_UNKNOWN)
6903 /* only one argument: clear entire history */
6904 n = clr_history(get_histtype(str));
6905 else if (argvars[1].v_type == VAR_NUMBER)
6906 /* index given: remove that entry */
6907 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006908 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006909 else
6910 /* string given: remove all matching entries */
6911 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006912 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006913 rettv->vval.v_number = n;
6914#endif
6915}
6916
6917/*
6918 * "histget()" function
6919 */
6920 static void
6921f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6922{
6923#ifdef FEAT_CMDHIST
6924 int type;
6925 int idx;
6926 char_u *str;
6927
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006928 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006929 if (str == NULL)
6930 rettv->vval.v_string = NULL;
6931 else
6932 {
6933 type = get_histtype(str);
6934 if (argvars[1].v_type == VAR_UNKNOWN)
6935 idx = get_history_idx(type);
6936 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006937 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006938 /* -1 on type error */
6939 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6940 }
6941#else
6942 rettv->vval.v_string = NULL;
6943#endif
6944 rettv->v_type = VAR_STRING;
6945}
6946
6947/*
6948 * "histnr()" function
6949 */
6950 static void
6951f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6952{
6953 int i;
6954
6955#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006956 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006957
6958 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6959 if (i >= HIST_CMD && i < HIST_COUNT)
6960 i = get_history_idx(i);
6961 else
6962#endif
6963 i = -1;
6964 rettv->vval.v_number = i;
6965}
6966
6967/*
6968 * "highlightID(name)" function
6969 */
6970 static void
6971f_hlID(typval_T *argvars, typval_T *rettv)
6972{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006973 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006974}
6975
6976/*
6977 * "highlight_exists()" function
6978 */
6979 static void
6980f_hlexists(typval_T *argvars, typval_T *rettv)
6981{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006982 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006983}
6984
6985/*
6986 * "hostname()" function
6987 */
6988 static void
6989f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6990{
6991 char_u hostname[256];
6992
6993 mch_get_host_name(hostname, 256);
6994 rettv->v_type = VAR_STRING;
6995 rettv->vval.v_string = vim_strsave(hostname);
6996}
6997
6998/*
6999 * iconv() function
7000 */
7001 static void
7002f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7003{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007004 char_u buf1[NUMBUFLEN];
7005 char_u buf2[NUMBUFLEN];
7006 char_u *from, *to, *str;
7007 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007008
7009 rettv->v_type = VAR_STRING;
7010 rettv->vval.v_string = NULL;
7011
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007012 str = tv_get_string(&argvars[0]);
7013 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7014 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007015 vimconv.vc_type = CONV_NONE;
7016 convert_setup(&vimconv, from, to);
7017
7018 /* If the encodings are equal, no conversion needed. */
7019 if (vimconv.vc_type == CONV_NONE)
7020 rettv->vval.v_string = vim_strsave(str);
7021 else
7022 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7023
7024 convert_setup(&vimconv, NULL, NULL);
7025 vim_free(from);
7026 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027}
7028
7029/*
7030 * "indent()" function
7031 */
7032 static void
7033f_indent(typval_T *argvars, typval_T *rettv)
7034{
7035 linenr_T lnum;
7036
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007037 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007038 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7039 rettv->vval.v_number = get_indent_lnum(lnum);
7040 else
7041 rettv->vval.v_number = -1;
7042}
7043
7044/*
7045 * "index()" function
7046 */
7047 static void
7048f_index(typval_T *argvars, typval_T *rettv)
7049{
7050 list_T *l;
7051 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007052 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007053 long idx = 0;
7054 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007055 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007056
7057 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007058 if (argvars[0].v_type == VAR_BLOB)
7059 {
7060 typval_T tv;
7061 int start = 0;
7062
7063 if (argvars[2].v_type != VAR_UNKNOWN)
7064 {
7065 start = tv_get_number_chk(&argvars[2], &error);
7066 if (error)
7067 return;
7068 }
7069 b = argvars[0].vval.v_blob;
7070 if (b == NULL)
7071 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007072 if (start < 0)
7073 {
7074 start = blob_len(b) + start;
7075 if (start < 0)
7076 start = 0;
7077 }
7078
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007079 for (idx = start; idx < blob_len(b); ++idx)
7080 {
7081 tv.v_type = VAR_NUMBER;
7082 tv.vval.v_number = blob_get(b, idx);
7083 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7084 {
7085 rettv->vval.v_number = idx;
7086 return;
7087 }
7088 }
7089 return;
7090 }
7091 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007092 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007093 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007094 return;
7095 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007096
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007097 l = argvars[0].vval.v_list;
7098 if (l != NULL)
7099 {
7100 item = l->lv_first;
7101 if (argvars[2].v_type != VAR_UNKNOWN)
7102 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007103 /* Start at specified item. Use the cached index that list_find()
7104 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007105 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007106 idx = l->lv_idx;
7107 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007108 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007109 if (error)
7110 item = NULL;
7111 }
7112
7113 for ( ; item != NULL; item = item->li_next, ++idx)
7114 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7115 {
7116 rettv->vval.v_number = idx;
7117 break;
7118 }
7119 }
7120}
7121
7122static int inputsecret_flag = 0;
7123
7124/*
7125 * "input()" function
7126 * Also handles inputsecret() when inputsecret is set.
7127 */
7128 static void
7129f_input(typval_T *argvars, typval_T *rettv)
7130{
7131 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7132}
7133
7134/*
7135 * "inputdialog()" function
7136 */
7137 static void
7138f_inputdialog(typval_T *argvars, typval_T *rettv)
7139{
7140#if defined(FEAT_GUI_TEXTDIALOG)
7141 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7142 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7143 {
7144 char_u *message;
7145 char_u buf[NUMBUFLEN];
7146 char_u *defstr = (char_u *)"";
7147
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007148 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007149 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007150 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007151 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7152 else
7153 IObuff[0] = NUL;
7154 if (message != NULL && defstr != NULL
7155 && do_dialog(VIM_QUESTION, NULL, message,
7156 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7157 rettv->vval.v_string = vim_strsave(IObuff);
7158 else
7159 {
7160 if (message != NULL && defstr != NULL
7161 && argvars[1].v_type != VAR_UNKNOWN
7162 && argvars[2].v_type != VAR_UNKNOWN)
7163 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007164 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007165 else
7166 rettv->vval.v_string = NULL;
7167 }
7168 rettv->v_type = VAR_STRING;
7169 }
7170 else
7171#endif
7172 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7173}
7174
7175/*
7176 * "inputlist()" function
7177 */
7178 static void
7179f_inputlist(typval_T *argvars, typval_T *rettv)
7180{
7181 listitem_T *li;
7182 int selected;
7183 int mouse_used;
7184
7185#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007186 /* While starting up, there is no place to enter text. When running tests
7187 * with --not-a-term we assume feedkeys() will be used. */
7188 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007189 return;
7190#endif
7191 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7192 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007193 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007194 return;
7195 }
7196
7197 msg_start();
7198 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7199 lines_left = Rows; /* avoid more prompt */
7200 msg_scroll = TRUE;
7201 msg_clr_eos();
7202
7203 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7204 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007205 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007206 msg_putchar('\n');
7207 }
7208
7209 /* Ask for choice. */
7210 selected = prompt_for_number(&mouse_used);
7211 if (mouse_used)
7212 selected -= lines_left;
7213
7214 rettv->vval.v_number = selected;
7215}
7216
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007217static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7218
7219/*
7220 * "inputrestore()" function
7221 */
7222 static void
7223f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7224{
7225 if (ga_userinput.ga_len > 0)
7226 {
7227 --ga_userinput.ga_len;
7228 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7229 + ga_userinput.ga_len);
7230 /* default return is zero == OK */
7231 }
7232 else if (p_verbose > 1)
7233 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007234 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007235 rettv->vval.v_number = 1; /* Failed */
7236 }
7237}
7238
7239/*
7240 * "inputsave()" function
7241 */
7242 static void
7243f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7244{
7245 /* Add an entry to the stack of typeahead storage. */
7246 if (ga_grow(&ga_userinput, 1) == OK)
7247 {
7248 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7249 + ga_userinput.ga_len);
7250 ++ga_userinput.ga_len;
7251 /* default return is zero == OK */
7252 }
7253 else
7254 rettv->vval.v_number = 1; /* Failed */
7255}
7256
7257/*
7258 * "inputsecret()" function
7259 */
7260 static void
7261f_inputsecret(typval_T *argvars, typval_T *rettv)
7262{
7263 ++cmdline_star;
7264 ++inputsecret_flag;
7265 f_input(argvars, rettv);
7266 --cmdline_star;
7267 --inputsecret_flag;
7268}
7269
7270/*
7271 * "insert()" function
7272 */
7273 static void
7274f_insert(typval_T *argvars, typval_T *rettv)
7275{
7276 long before = 0;
7277 listitem_T *item;
7278 list_T *l;
7279 int error = FALSE;
7280
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007281 if (argvars[0].v_type == VAR_BLOB)
7282 {
7283 int val, len;
7284 char_u *p;
7285
7286 len = blob_len(argvars[0].vval.v_blob);
7287 if (argvars[2].v_type != VAR_UNKNOWN)
7288 {
7289 before = (long)tv_get_number_chk(&argvars[2], &error);
7290 if (error)
7291 return; // type error; errmsg already given
7292 if (before < 0 || before > len)
7293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007294 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007295 return;
7296 }
7297 }
7298 val = tv_get_number_chk(&argvars[1], &error);
7299 if (error)
7300 return;
7301 if (val < 0 || val > 255)
7302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007303 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007304 return;
7305 }
7306
7307 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7308 return;
7309 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7310 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7311 *(p + before) = val;
7312 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7313
7314 copy_tv(&argvars[0], rettv);
7315 }
7316 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007317 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007318 else if ((l = argvars[0].vval.v_list) != NULL
7319 && !var_check_lock(l->lv_lock,
7320 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007321 {
7322 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007323 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007324 if (error)
7325 return; /* type error; errmsg already given */
7326
7327 if (before == l->lv_len)
7328 item = NULL;
7329 else
7330 {
7331 item = list_find(l, before);
7332 if (item == NULL)
7333 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007334 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007335 l = NULL;
7336 }
7337 }
7338 if (l != NULL)
7339 {
7340 list_insert_tv(l, &argvars[1], item);
7341 copy_tv(&argvars[0], rettv);
7342 }
7343 }
7344}
7345
7346/*
7347 * "invert(expr)" function
7348 */
7349 static void
7350f_invert(typval_T *argvars, typval_T *rettv)
7351{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007352 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007353}
7354
7355/*
7356 * "isdirectory()" function
7357 */
7358 static void
7359f_isdirectory(typval_T *argvars, typval_T *rettv)
7360{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007361 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007362}
7363
7364/*
7365 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7366 * or it refers to a List or Dictionary that is locked.
7367 */
7368 static int
7369tv_islocked(typval_T *tv)
7370{
7371 return (tv->v_lock & VAR_LOCKED)
7372 || (tv->v_type == VAR_LIST
7373 && tv->vval.v_list != NULL
7374 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7375 || (tv->v_type == VAR_DICT
7376 && tv->vval.v_dict != NULL
7377 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7378}
7379
7380/*
7381 * "islocked()" function
7382 */
7383 static void
7384f_islocked(typval_T *argvars, typval_T *rettv)
7385{
7386 lval_T lv;
7387 char_u *end;
7388 dictitem_T *di;
7389
7390 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007391 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007392 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007393 if (end != NULL && lv.ll_name != NULL)
7394 {
7395 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007396 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007397 else
7398 {
7399 if (lv.ll_tv == NULL)
7400 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007401 di = find_var(lv.ll_name, NULL, TRUE);
7402 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007403 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007404 /* Consider a variable locked when:
7405 * 1. the variable itself is locked
7406 * 2. the value of the variable is locked.
7407 * 3. the List or Dict value is locked.
7408 */
7409 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7410 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007411 }
7412 }
7413 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007414 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007415 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007416 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007417 else if (lv.ll_list != NULL)
7418 /* List item. */
7419 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7420 else
7421 /* Dictionary item. */
7422 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7423 }
7424 }
7425
7426 clear_lval(&lv);
7427}
7428
7429#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7430/*
7431 * "isnan()" function
7432 */
7433 static void
7434f_isnan(typval_T *argvars, typval_T *rettv)
7435{
7436 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7437 && isnan(argvars[0].vval.v_float);
7438}
7439#endif
7440
7441/*
7442 * "items(dict)" function
7443 */
7444 static void
7445f_items(typval_T *argvars, typval_T *rettv)
7446{
7447 dict_list(argvars, rettv, 2);
7448}
7449
7450#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7451/*
7452 * Get the job from the argument.
7453 * Returns NULL if the job is invalid.
7454 */
7455 static job_T *
7456get_job_arg(typval_T *tv)
7457{
7458 job_T *job;
7459
7460 if (tv->v_type != VAR_JOB)
7461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007462 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007463 return NULL;
7464 }
7465 job = tv->vval.v_job;
7466
7467 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007468 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007469 return job;
7470}
7471
7472/*
7473 * "job_getchannel()" function
7474 */
7475 static void
7476f_job_getchannel(typval_T *argvars, typval_T *rettv)
7477{
7478 job_T *job = get_job_arg(&argvars[0]);
7479
7480 if (job != NULL)
7481 {
7482 rettv->v_type = VAR_CHANNEL;
7483 rettv->vval.v_channel = job->jv_channel;
7484 if (job->jv_channel != NULL)
7485 ++job->jv_channel->ch_refcount;
7486 }
7487}
7488
7489/*
7490 * "job_info()" function
7491 */
7492 static void
7493f_job_info(typval_T *argvars, typval_T *rettv)
7494{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007495 if (argvars[0].v_type != VAR_UNKNOWN)
7496 {
7497 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007498
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007499 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7500 job_info(job, rettv->vval.v_dict);
7501 }
7502 else if (rettv_list_alloc(rettv) == OK)
7503 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007504}
7505
7506/*
7507 * "job_setoptions()" function
7508 */
7509 static void
7510f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7511{
7512 job_T *job = get_job_arg(&argvars[0]);
7513 jobopt_T opt;
7514
7515 if (job == NULL)
7516 return;
7517 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007518 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007519 job_set_options(job, &opt);
7520 free_job_options(&opt);
7521}
7522
7523/*
7524 * "job_start()" function
7525 */
7526 static void
7527f_job_start(typval_T *argvars, typval_T *rettv)
7528{
7529 rettv->v_type = VAR_JOB;
7530 if (check_restricted() || check_secure())
7531 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007532 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007533}
7534
7535/*
7536 * "job_status()" function
7537 */
7538 static void
7539f_job_status(typval_T *argvars, typval_T *rettv)
7540{
7541 job_T *job = get_job_arg(&argvars[0]);
7542
7543 if (job != NULL)
7544 {
7545 rettv->v_type = VAR_STRING;
7546 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7547 }
7548}
7549
7550/*
7551 * "job_stop()" function
7552 */
7553 static void
7554f_job_stop(typval_T *argvars, typval_T *rettv)
7555{
7556 job_T *job = get_job_arg(&argvars[0]);
7557
7558 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007559 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007560}
7561#endif
7562
7563/*
7564 * "join()" function
7565 */
7566 static void
7567f_join(typval_T *argvars, typval_T *rettv)
7568{
7569 garray_T ga;
7570 char_u *sep;
7571
7572 if (argvars[0].v_type != VAR_LIST)
7573 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007574 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007575 return;
7576 }
7577 if (argvars[0].vval.v_list == NULL)
7578 return;
7579 if (argvars[1].v_type == VAR_UNKNOWN)
7580 sep = (char_u *)" ";
7581 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007582 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007583
7584 rettv->v_type = VAR_STRING;
7585
7586 if (sep != NULL)
7587 {
7588 ga_init2(&ga, (int)sizeof(char), 80);
7589 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7590 ga_append(&ga, NUL);
7591 rettv->vval.v_string = (char_u *)ga.ga_data;
7592 }
7593 else
7594 rettv->vval.v_string = NULL;
7595}
7596
7597/*
7598 * "js_decode()" function
7599 */
7600 static void
7601f_js_decode(typval_T *argvars, typval_T *rettv)
7602{
7603 js_read_T reader;
7604
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007605 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007606 reader.js_fill = NULL;
7607 reader.js_used = 0;
7608 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007609 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007610}
7611
7612/*
7613 * "js_encode()" function
7614 */
7615 static void
7616f_js_encode(typval_T *argvars, typval_T *rettv)
7617{
7618 rettv->v_type = VAR_STRING;
7619 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7620}
7621
7622/*
7623 * "json_decode()" function
7624 */
7625 static void
7626f_json_decode(typval_T *argvars, typval_T *rettv)
7627{
7628 js_read_T reader;
7629
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007630 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007631 reader.js_fill = NULL;
7632 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007633 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007634}
7635
7636/*
7637 * "json_encode()" function
7638 */
7639 static void
7640f_json_encode(typval_T *argvars, typval_T *rettv)
7641{
7642 rettv->v_type = VAR_STRING;
7643 rettv->vval.v_string = json_encode(&argvars[0], 0);
7644}
7645
7646/*
7647 * "keys()" function
7648 */
7649 static void
7650f_keys(typval_T *argvars, typval_T *rettv)
7651{
7652 dict_list(argvars, rettv, 0);
7653}
7654
7655/*
7656 * "last_buffer_nr()" function.
7657 */
7658 static void
7659f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7660{
7661 int n = 0;
7662 buf_T *buf;
7663
Bram Moolenaar29323592016-07-24 22:04:11 +02007664 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007665 if (n < buf->b_fnum)
7666 n = buf->b_fnum;
7667
7668 rettv->vval.v_number = n;
7669}
7670
7671/*
7672 * "len()" function
7673 */
7674 static void
7675f_len(typval_T *argvars, typval_T *rettv)
7676{
7677 switch (argvars[0].v_type)
7678 {
7679 case VAR_STRING:
7680 case VAR_NUMBER:
7681 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007682 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007683 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007684 case VAR_BLOB:
7685 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7686 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007687 case VAR_LIST:
7688 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7689 break;
7690 case VAR_DICT:
7691 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7692 break;
7693 case VAR_UNKNOWN:
7694 case VAR_SPECIAL:
7695 case VAR_FLOAT:
7696 case VAR_FUNC:
7697 case VAR_PARTIAL:
7698 case VAR_JOB:
7699 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007700 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007701 break;
7702 }
7703}
7704
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007705 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007706libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007707{
7708#ifdef FEAT_LIBCALL
7709 char_u *string_in;
7710 char_u **string_result;
7711 int nr_result;
7712#endif
7713
7714 rettv->v_type = type;
7715 if (type != VAR_NUMBER)
7716 rettv->vval.v_string = NULL;
7717
7718 if (check_restricted() || check_secure())
7719 return;
7720
7721#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007722 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007723 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7724 {
7725 string_in = NULL;
7726 if (argvars[2].v_type == VAR_STRING)
7727 string_in = argvars[2].vval.v_string;
7728 if (type == VAR_NUMBER)
7729 string_result = NULL;
7730 else
7731 string_result = &rettv->vval.v_string;
7732 if (mch_libcall(argvars[0].vval.v_string,
7733 argvars[1].vval.v_string,
7734 string_in,
7735 argvars[2].vval.v_number,
7736 string_result,
7737 &nr_result) == OK
7738 && type == VAR_NUMBER)
7739 rettv->vval.v_number = nr_result;
7740 }
7741#endif
7742}
7743
7744/*
7745 * "libcall()" function
7746 */
7747 static void
7748f_libcall(typval_T *argvars, typval_T *rettv)
7749{
7750 libcall_common(argvars, rettv, VAR_STRING);
7751}
7752
7753/*
7754 * "libcallnr()" function
7755 */
7756 static void
7757f_libcallnr(typval_T *argvars, typval_T *rettv)
7758{
7759 libcall_common(argvars, rettv, VAR_NUMBER);
7760}
7761
7762/*
7763 * "line(string)" function
7764 */
7765 static void
7766f_line(typval_T *argvars, typval_T *rettv)
7767{
7768 linenr_T lnum = 0;
7769 pos_T *fp;
7770 int fnum;
7771
7772 fp = var2fpos(&argvars[0], TRUE, &fnum);
7773 if (fp != NULL)
7774 lnum = fp->lnum;
7775 rettv->vval.v_number = lnum;
7776}
7777
7778/*
7779 * "line2byte(lnum)" function
7780 */
7781 static void
7782f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7783{
7784#ifndef FEAT_BYTEOFF
7785 rettv->vval.v_number = -1;
7786#else
7787 linenr_T lnum;
7788
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007789 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007790 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7791 rettv->vval.v_number = -1;
7792 else
7793 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7794 if (rettv->vval.v_number >= 0)
7795 ++rettv->vval.v_number;
7796#endif
7797}
7798
7799/*
7800 * "lispindent(lnum)" function
7801 */
7802 static void
7803f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7804{
7805#ifdef FEAT_LISP
7806 pos_T pos;
7807 linenr_T lnum;
7808
7809 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007810 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007811 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7812 {
7813 curwin->w_cursor.lnum = lnum;
7814 rettv->vval.v_number = get_lisp_indent();
7815 curwin->w_cursor = pos;
7816 }
7817 else
7818#endif
7819 rettv->vval.v_number = -1;
7820}
7821
7822/*
7823 * "localtime()" function
7824 */
7825 static void
7826f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7827{
7828 rettv->vval.v_number = (varnumber_T)time(NULL);
7829}
7830
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007831 static void
7832get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7833{
7834 char_u *keys;
7835 char_u *which;
7836 char_u buf[NUMBUFLEN];
7837 char_u *keys_buf = NULL;
7838 char_u *rhs;
7839 int mode;
7840 int abbr = FALSE;
7841 int get_dict = FALSE;
7842 mapblock_T *mp;
7843 int buffer_local;
7844
7845 /* return empty string for failure */
7846 rettv->v_type = VAR_STRING;
7847 rettv->vval.v_string = NULL;
7848
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007849 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007850 if (*keys == NUL)
7851 return;
7852
7853 if (argvars[1].v_type != VAR_UNKNOWN)
7854 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007855 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007856 if (argvars[2].v_type != VAR_UNKNOWN)
7857 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007858 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007859 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007860 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007861 }
7862 }
7863 else
7864 which = (char_u *)"";
7865 if (which == NULL)
7866 return;
7867
7868 mode = get_map_mode(&which, 0);
7869
7870 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7871 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7872 vim_free(keys_buf);
7873
7874 if (!get_dict)
7875 {
7876 /* Return a string. */
7877 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007878 {
7879 if (*rhs == NUL)
7880 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7881 else
7882 rettv->vval.v_string = str2special_save(rhs, FALSE);
7883 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007884
7885 }
7886 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7887 {
7888 /* Return a dictionary. */
7889 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7890 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7891 dict_T *dict = rettv->vval.v_dict;
7892
Bram Moolenaare0be1672018-07-08 16:50:37 +02007893 dict_add_string(dict, "lhs", lhs);
7894 dict_add_string(dict, "rhs", mp->m_orig_str);
7895 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7896 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7897 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007898 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7899 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007900 dict_add_number(dict, "buffer", (long)buffer_local);
7901 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7902 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007903
7904 vim_free(lhs);
7905 vim_free(mapmode);
7906 }
7907}
7908
7909#ifdef FEAT_FLOAT
7910/*
7911 * "log()" function
7912 */
7913 static void
7914f_log(typval_T *argvars, typval_T *rettv)
7915{
7916 float_T f = 0.0;
7917
7918 rettv->v_type = VAR_FLOAT;
7919 if (get_float_arg(argvars, &f) == OK)
7920 rettv->vval.v_float = log(f);
7921 else
7922 rettv->vval.v_float = 0.0;
7923}
7924
7925/*
7926 * "log10()" function
7927 */
7928 static void
7929f_log10(typval_T *argvars, typval_T *rettv)
7930{
7931 float_T f = 0.0;
7932
7933 rettv->v_type = VAR_FLOAT;
7934 if (get_float_arg(argvars, &f) == OK)
7935 rettv->vval.v_float = log10(f);
7936 else
7937 rettv->vval.v_float = 0.0;
7938}
7939#endif
7940
7941#ifdef FEAT_LUA
7942/*
7943 * "luaeval()" function
7944 */
7945 static void
7946f_luaeval(typval_T *argvars, typval_T *rettv)
7947{
7948 char_u *str;
7949 char_u buf[NUMBUFLEN];
7950
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007951 if (check_restricted() || check_secure())
7952 return;
7953
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007954 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007955 do_luaeval(str, argvars + 1, rettv);
7956}
7957#endif
7958
7959/*
7960 * "map()" function
7961 */
7962 static void
7963f_map(typval_T *argvars, typval_T *rettv)
7964{
7965 filter_map(argvars, rettv, TRUE);
7966}
7967
7968/*
7969 * "maparg()" function
7970 */
7971 static void
7972f_maparg(typval_T *argvars, typval_T *rettv)
7973{
7974 get_maparg(argvars, rettv, TRUE);
7975}
7976
7977/*
7978 * "mapcheck()" function
7979 */
7980 static void
7981f_mapcheck(typval_T *argvars, typval_T *rettv)
7982{
7983 get_maparg(argvars, rettv, FALSE);
7984}
7985
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007986typedef enum
7987{
7988 MATCH_END, /* matchend() */
7989 MATCH_MATCH, /* match() */
7990 MATCH_STR, /* matchstr() */
7991 MATCH_LIST, /* matchlist() */
7992 MATCH_POS /* matchstrpos() */
7993} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007994
7995 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007996find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007997{
7998 char_u *str = NULL;
7999 long len = 0;
8000 char_u *expr = NULL;
8001 char_u *pat;
8002 regmatch_T regmatch;
8003 char_u patbuf[NUMBUFLEN];
8004 char_u strbuf[NUMBUFLEN];
8005 char_u *save_cpo;
8006 long start = 0;
8007 long nth = 1;
8008 colnr_T startcol = 0;
8009 int match = 0;
8010 list_T *l = NULL;
8011 listitem_T *li = NULL;
8012 long idx = 0;
8013 char_u *tofree = NULL;
8014
8015 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8016 save_cpo = p_cpo;
8017 p_cpo = (char_u *)"";
8018
8019 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008020 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008021 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008022 /* type MATCH_LIST: return empty list when there are no matches.
8023 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008024 if (rettv_list_alloc(rettv) == FAIL)
8025 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008026 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008027 && (list_append_string(rettv->vval.v_list,
8028 (char_u *)"", 0) == FAIL
8029 || list_append_number(rettv->vval.v_list,
8030 (varnumber_T)-1) == FAIL
8031 || list_append_number(rettv->vval.v_list,
8032 (varnumber_T)-1) == FAIL
8033 || list_append_number(rettv->vval.v_list,
8034 (varnumber_T)-1) == FAIL))
8035 {
8036 list_free(rettv->vval.v_list);
8037 rettv->vval.v_list = NULL;
8038 goto theend;
8039 }
8040 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008041 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008042 {
8043 rettv->v_type = VAR_STRING;
8044 rettv->vval.v_string = NULL;
8045 }
8046
8047 if (argvars[0].v_type == VAR_LIST)
8048 {
8049 if ((l = argvars[0].vval.v_list) == NULL)
8050 goto theend;
8051 li = l->lv_first;
8052 }
8053 else
8054 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008055 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008056 len = (long)STRLEN(str);
8057 }
8058
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008059 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008060 if (pat == NULL)
8061 goto theend;
8062
8063 if (argvars[2].v_type != VAR_UNKNOWN)
8064 {
8065 int error = FALSE;
8066
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008067 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068 if (error)
8069 goto theend;
8070 if (l != NULL)
8071 {
8072 li = list_find(l, start);
8073 if (li == NULL)
8074 goto theend;
8075 idx = l->lv_idx; /* use the cached index */
8076 }
8077 else
8078 {
8079 if (start < 0)
8080 start = 0;
8081 if (start > len)
8082 goto theend;
8083 /* When "count" argument is there ignore matches before "start",
8084 * otherwise skip part of the string. Differs when pattern is "^"
8085 * or "\<". */
8086 if (argvars[3].v_type != VAR_UNKNOWN)
8087 startcol = start;
8088 else
8089 {
8090 str += start;
8091 len -= start;
8092 }
8093 }
8094
8095 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008096 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008097 if (error)
8098 goto theend;
8099 }
8100
8101 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8102 if (regmatch.regprog != NULL)
8103 {
8104 regmatch.rm_ic = p_ic;
8105
8106 for (;;)
8107 {
8108 if (l != NULL)
8109 {
8110 if (li == NULL)
8111 {
8112 match = FALSE;
8113 break;
8114 }
8115 vim_free(tofree);
8116 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8117 if (str == NULL)
8118 break;
8119 }
8120
8121 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8122
8123 if (match && --nth <= 0)
8124 break;
8125 if (l == NULL && !match)
8126 break;
8127
8128 /* Advance to just after the match. */
8129 if (l != NULL)
8130 {
8131 li = li->li_next;
8132 ++idx;
8133 }
8134 else
8135 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136 startcol = (colnr_T)(regmatch.startp[0]
8137 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008138 if (startcol > (colnr_T)len
8139 || str + startcol <= regmatch.startp[0])
8140 {
8141 match = FALSE;
8142 break;
8143 }
8144 }
8145 }
8146
8147 if (match)
8148 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008149 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150 {
8151 listitem_T *li1 = rettv->vval.v_list->lv_first;
8152 listitem_T *li2 = li1->li_next;
8153 listitem_T *li3 = li2->li_next;
8154 listitem_T *li4 = li3->li_next;
8155
8156 vim_free(li1->li_tv.vval.v_string);
8157 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8158 (int)(regmatch.endp[0] - regmatch.startp[0]));
8159 li3->li_tv.vval.v_number =
8160 (varnumber_T)(regmatch.startp[0] - expr);
8161 li4->li_tv.vval.v_number =
8162 (varnumber_T)(regmatch.endp[0] - expr);
8163 if (l != NULL)
8164 li2->li_tv.vval.v_number = (varnumber_T)idx;
8165 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008166 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167 {
8168 int i;
8169
8170 /* return list with matched string and submatches */
8171 for (i = 0; i < NSUBEXP; ++i)
8172 {
8173 if (regmatch.endp[i] == NULL)
8174 {
8175 if (list_append_string(rettv->vval.v_list,
8176 (char_u *)"", 0) == FAIL)
8177 break;
8178 }
8179 else if (list_append_string(rettv->vval.v_list,
8180 regmatch.startp[i],
8181 (int)(regmatch.endp[i] - regmatch.startp[i]))
8182 == FAIL)
8183 break;
8184 }
8185 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008186 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008187 {
8188 /* return matched string */
8189 if (l != NULL)
8190 copy_tv(&li->li_tv, rettv);
8191 else
8192 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8193 (int)(regmatch.endp[0] - regmatch.startp[0]));
8194 }
8195 else if (l != NULL)
8196 rettv->vval.v_number = idx;
8197 else
8198 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008199 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008200 rettv->vval.v_number =
8201 (varnumber_T)(regmatch.startp[0] - str);
8202 else
8203 rettv->vval.v_number =
8204 (varnumber_T)(regmatch.endp[0] - str);
8205 rettv->vval.v_number += (varnumber_T)(str - expr);
8206 }
8207 }
8208 vim_regfree(regmatch.regprog);
8209 }
8210
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008211theend:
8212 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008213 /* matchstrpos() without a list: drop the second item. */
8214 listitem_remove(rettv->vval.v_list,
8215 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008216 vim_free(tofree);
8217 p_cpo = save_cpo;
8218}
8219
8220/*
8221 * "match()" function
8222 */
8223 static void
8224f_match(typval_T *argvars, typval_T *rettv)
8225{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008226 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008227}
8228
Bram Moolenaar95e51472018-07-28 16:55:56 +02008229#ifdef FEAT_SEARCH_EXTRA
8230 static int
8231matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8232{
8233 dictitem_T *di;
8234
8235 if (tv->v_type != VAR_DICT)
8236 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008237 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008238 return FAIL;
8239 }
8240
8241 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008242 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008243 (char_u *)"conceal", FALSE);
8244
8245 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8246 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008247 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008248 if (*win == NULL)
8249 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008250 emsg(_("E957: Invalid window number"));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008251 return FAIL;
8252 }
8253 }
8254
8255 return OK;
8256}
8257#endif
8258
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008259/*
8260 * "matchadd()" function
8261 */
8262 static void
8263f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8264{
8265#ifdef FEAT_SEARCH_EXTRA
8266 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008267 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8268 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008269 int prio = 10; /* default priority */
8270 int id = -1;
8271 int error = FALSE;
8272 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008273 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008274
8275 rettv->vval.v_number = -1;
8276
8277 if (grp == NULL || pat == NULL)
8278 return;
8279 if (argvars[2].v_type != VAR_UNKNOWN)
8280 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008281 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008282 if (argvars[3].v_type != VAR_UNKNOWN)
8283 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008284 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008285 if (argvars[4].v_type != VAR_UNKNOWN
8286 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8287 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008288 }
8289 }
8290 if (error == TRUE)
8291 return;
8292 if (id >= 1 && id <= 3)
8293 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008294 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008295 return;
8296 }
8297
Bram Moolenaar95e51472018-07-28 16:55:56 +02008298 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299 conceal_char);
8300#endif
8301}
8302
8303/*
8304 * "matchaddpos()" function
8305 */
8306 static void
8307f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8308{
8309#ifdef FEAT_SEARCH_EXTRA
8310 char_u buf[NUMBUFLEN];
8311 char_u *group;
8312 int prio = 10;
8313 int id = -1;
8314 int error = FALSE;
8315 list_T *l;
8316 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008317 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008318
8319 rettv->vval.v_number = -1;
8320
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008321 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322 if (group == NULL)
8323 return;
8324
8325 if (argvars[1].v_type != VAR_LIST)
8326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008327 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 return;
8329 }
8330 l = argvars[1].vval.v_list;
8331 if (l == NULL)
8332 return;
8333
8334 if (argvars[2].v_type != VAR_UNKNOWN)
8335 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008336 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337 if (argvars[3].v_type != VAR_UNKNOWN)
8338 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008339 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008340
8341 if (argvars[4].v_type != VAR_UNKNOWN
8342 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8343 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008344 }
8345 }
8346 if (error == TRUE)
8347 return;
8348
8349 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8350 if (id == 1 || id == 2)
8351 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008352 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008353 return;
8354 }
8355
Bram Moolenaar95e51472018-07-28 16:55:56 +02008356 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008357 conceal_char);
8358#endif
8359}
8360
8361/*
8362 * "matcharg()" function
8363 */
8364 static void
8365f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8366{
8367 if (rettv_list_alloc(rettv) == OK)
8368 {
8369#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008370 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008371 matchitem_T *m;
8372
8373 if (id >= 1 && id <= 3)
8374 {
8375 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8376 {
8377 list_append_string(rettv->vval.v_list,
8378 syn_id2name(m->hlg_id), -1);
8379 list_append_string(rettv->vval.v_list, m->pattern, -1);
8380 }
8381 else
8382 {
8383 list_append_string(rettv->vval.v_list, NULL, -1);
8384 list_append_string(rettv->vval.v_list, NULL, -1);
8385 }
8386 }
8387#endif
8388 }
8389}
8390
8391/*
8392 * "matchdelete()" function
8393 */
8394 static void
8395f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8396{
8397#ifdef FEAT_SEARCH_EXTRA
8398 rettv->vval.v_number = match_delete(curwin,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008399 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008400#endif
8401}
8402
8403/*
8404 * "matchend()" function
8405 */
8406 static void
8407f_matchend(typval_T *argvars, typval_T *rettv)
8408{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008409 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008410}
8411
8412/*
8413 * "matchlist()" function
8414 */
8415 static void
8416f_matchlist(typval_T *argvars, typval_T *rettv)
8417{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008418 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008419}
8420
8421/*
8422 * "matchstr()" function
8423 */
8424 static void
8425f_matchstr(typval_T *argvars, typval_T *rettv)
8426{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008427 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008428}
8429
8430/*
8431 * "matchstrpos()" function
8432 */
8433 static void
8434f_matchstrpos(typval_T *argvars, typval_T *rettv)
8435{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008436 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008437}
8438
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008439 static void
8440max_min(typval_T *argvars, typval_T *rettv, int domax)
8441{
8442 varnumber_T n = 0;
8443 varnumber_T i;
8444 int error = FALSE;
8445
8446 if (argvars[0].v_type == VAR_LIST)
8447 {
8448 list_T *l;
8449 listitem_T *li;
8450
8451 l = argvars[0].vval.v_list;
8452 if (l != NULL)
8453 {
8454 li = l->lv_first;
8455 if (li != NULL)
8456 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008457 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008458 for (;;)
8459 {
8460 li = li->li_next;
8461 if (li == NULL)
8462 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008463 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008464 if (domax ? i > n : i < n)
8465 n = i;
8466 }
8467 }
8468 }
8469 }
8470 else if (argvars[0].v_type == VAR_DICT)
8471 {
8472 dict_T *d;
8473 int first = TRUE;
8474 hashitem_T *hi;
8475 int todo;
8476
8477 d = argvars[0].vval.v_dict;
8478 if (d != NULL)
8479 {
8480 todo = (int)d->dv_hashtab.ht_used;
8481 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8482 {
8483 if (!HASHITEM_EMPTY(hi))
8484 {
8485 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008486 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008487 if (first)
8488 {
8489 n = i;
8490 first = FALSE;
8491 }
8492 else if (domax ? i > n : i < n)
8493 n = i;
8494 }
8495 }
8496 }
8497 }
8498 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008499 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008500 rettv->vval.v_number = error ? 0 : n;
8501}
8502
8503/*
8504 * "max()" function
8505 */
8506 static void
8507f_max(typval_T *argvars, typval_T *rettv)
8508{
8509 max_min(argvars, rettv, TRUE);
8510}
8511
8512/*
8513 * "min()" function
8514 */
8515 static void
8516f_min(typval_T *argvars, typval_T *rettv)
8517{
8518 max_min(argvars, rettv, FALSE);
8519}
8520
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008521/*
8522 * Create the directory in which "dir" is located, and higher levels when
8523 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008524 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008525 */
8526 static int
8527mkdir_recurse(char_u *dir, int prot)
8528{
8529 char_u *p;
8530 char_u *updir;
8531 int r = FAIL;
8532
8533 /* Get end of directory name in "dir".
8534 * We're done when it's "/" or "c:/". */
8535 p = gettail_sep(dir);
8536 if (p <= get_past_head(dir))
8537 return OK;
8538
8539 /* If the directory exists we're done. Otherwise: create it.*/
8540 updir = vim_strnsave(dir, (int)(p - dir));
8541 if (updir == NULL)
8542 return FAIL;
8543 if (mch_isdir(updir))
8544 r = OK;
8545 else if (mkdir_recurse(updir, prot) == OK)
8546 r = vim_mkdir_emsg(updir, prot);
8547 vim_free(updir);
8548 return r;
8549}
8550
8551#ifdef vim_mkdir
8552/*
8553 * "mkdir()" function
8554 */
8555 static void
8556f_mkdir(typval_T *argvars, typval_T *rettv)
8557{
8558 char_u *dir;
8559 char_u buf[NUMBUFLEN];
8560 int prot = 0755;
8561
8562 rettv->vval.v_number = FAIL;
8563 if (check_restricted() || check_secure())
8564 return;
8565
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008566 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008567 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008568 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008569
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008570 if (*gettail(dir) == NUL)
8571 /* remove trailing slashes */
8572 *gettail_sep(dir) = NUL;
8573
8574 if (argvars[1].v_type != VAR_UNKNOWN)
8575 {
8576 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008578 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008579 if (prot == -1)
8580 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008582 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008583 {
8584 if (mch_isdir(dir))
8585 {
8586 /* With the "p" flag it's OK if the dir already exists. */
8587 rettv->vval.v_number = OK;
8588 return;
8589 }
8590 mkdir_recurse(dir, prot);
8591 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008592 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008593 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008594}
8595#endif
8596
8597/*
8598 * "mode()" function
8599 */
8600 static void
8601f_mode(typval_T *argvars, typval_T *rettv)
8602{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008603 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008604
Bram Moolenaar612cc382018-07-29 15:34:26 +02008605 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008606
8607 if (time_for_testing == 93784)
8608 {
8609 /* Testing the two-character code. */
8610 buf[0] = 'x';
8611 buf[1] = '!';
8612 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008613#ifdef FEAT_TERMINAL
8614 else if (term_use_loop())
8615 buf[0] = 't';
8616#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008617 else if (VIsual_active)
8618 {
8619 if (VIsual_select)
8620 buf[0] = VIsual_mode + 's' - 'v';
8621 else
8622 buf[0] = VIsual_mode;
8623 }
8624 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8625 || State == CONFIRM)
8626 {
8627 buf[0] = 'r';
8628 if (State == ASKMORE)
8629 buf[1] = 'm';
8630 else if (State == CONFIRM)
8631 buf[1] = '?';
8632 }
8633 else if (State == EXTERNCMD)
8634 buf[0] = '!';
8635 else if (State & INSERT)
8636 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008637 if (State & VREPLACE_FLAG)
8638 {
8639 buf[0] = 'R';
8640 buf[1] = 'v';
8641 }
8642 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008643 {
8644 if (State & REPLACE_FLAG)
8645 buf[0] = 'R';
8646 else
8647 buf[0] = 'i';
8648#ifdef FEAT_INS_EXPAND
8649 if (ins_compl_active())
8650 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008651 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008652 buf[1] = 'x';
8653#endif
8654 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008656 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657 {
8658 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008659 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008660 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008661 else if (exmode_active == EXMODE_NORMAL)
8662 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008663 }
8664 else
8665 {
8666 buf[0] = 'n';
8667 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008668 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008669 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008670 // to be able to detect force-linewise/blockwise/characterwise operations
8671 buf[2] = motion_force;
8672 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008673 else if (restart_edit == 'I' || restart_edit == 'R'
8674 || restart_edit == 'V')
8675 {
8676 buf[1] = 'i';
8677 buf[2] = restart_edit;
8678 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008679 }
8680
8681 /* Clear out the minor mode when the argument is not a non-zero number or
8682 * non-empty string. */
8683 if (!non_zero_arg(&argvars[0]))
8684 buf[1] = NUL;
8685
8686 rettv->vval.v_string = vim_strsave(buf);
8687 rettv->v_type = VAR_STRING;
8688}
8689
8690#if defined(FEAT_MZSCHEME) || defined(PROTO)
8691/*
8692 * "mzeval()" function
8693 */
8694 static void
8695f_mzeval(typval_T *argvars, typval_T *rettv)
8696{
8697 char_u *str;
8698 char_u buf[NUMBUFLEN];
8699
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008700 if (check_restricted() || check_secure())
8701 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008702 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008703 do_mzeval(str, rettv);
8704}
8705
8706 void
8707mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8708{
8709 typval_T argvars[3];
8710
8711 argvars[0].v_type = VAR_STRING;
8712 argvars[0].vval.v_string = name;
8713 copy_tv(args, &argvars[1]);
8714 argvars[2].v_type = VAR_UNKNOWN;
8715 f_call(argvars, rettv);
8716 clear_tv(&argvars[1]);
8717}
8718#endif
8719
8720/*
8721 * "nextnonblank()" function
8722 */
8723 static void
8724f_nextnonblank(typval_T *argvars, typval_T *rettv)
8725{
8726 linenr_T lnum;
8727
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008728 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008729 {
8730 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8731 {
8732 lnum = 0;
8733 break;
8734 }
8735 if (*skipwhite(ml_get(lnum)) != NUL)
8736 break;
8737 }
8738 rettv->vval.v_number = lnum;
8739}
8740
8741/*
8742 * "nr2char()" function
8743 */
8744 static void
8745f_nr2char(typval_T *argvars, typval_T *rettv)
8746{
8747 char_u buf[NUMBUFLEN];
8748
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008749 if (has_mbyte)
8750 {
8751 int utf8 = 0;
8752
8753 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008754 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008756 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008758 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008759 }
8760 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008761 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008762 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008763 buf[1] = NUL;
8764 }
8765 rettv->v_type = VAR_STRING;
8766 rettv->vval.v_string = vim_strsave(buf);
8767}
8768
8769/*
8770 * "or(expr, expr)" function
8771 */
8772 static void
8773f_or(typval_T *argvars, typval_T *rettv)
8774{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008775 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8776 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008777}
8778
8779/*
8780 * "pathshorten()" function
8781 */
8782 static void
8783f_pathshorten(typval_T *argvars, typval_T *rettv)
8784{
8785 char_u *p;
8786
8787 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008788 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789 if (p == NULL)
8790 rettv->vval.v_string = NULL;
8791 else
8792 {
8793 p = vim_strsave(p);
8794 rettv->vval.v_string = p;
8795 if (p != NULL)
8796 shorten_dir(p);
8797 }
8798}
8799
8800#ifdef FEAT_PERL
8801/*
8802 * "perleval()" function
8803 */
8804 static void
8805f_perleval(typval_T *argvars, typval_T *rettv)
8806{
8807 char_u *str;
8808 char_u buf[NUMBUFLEN];
8809
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008810 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008811 do_perleval(str, rettv);
8812}
8813#endif
8814
8815#ifdef FEAT_FLOAT
8816/*
8817 * "pow()" function
8818 */
8819 static void
8820f_pow(typval_T *argvars, typval_T *rettv)
8821{
8822 float_T fx = 0.0, fy = 0.0;
8823
8824 rettv->v_type = VAR_FLOAT;
8825 if (get_float_arg(argvars, &fx) == OK
8826 && get_float_arg(&argvars[1], &fy) == OK)
8827 rettv->vval.v_float = pow(fx, fy);
8828 else
8829 rettv->vval.v_float = 0.0;
8830}
8831#endif
8832
8833/*
8834 * "prevnonblank()" function
8835 */
8836 static void
8837f_prevnonblank(typval_T *argvars, typval_T *rettv)
8838{
8839 linenr_T lnum;
8840
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008841 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8843 lnum = 0;
8844 else
8845 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8846 --lnum;
8847 rettv->vval.v_number = lnum;
8848}
8849
8850/* This dummy va_list is here because:
8851 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8852 * - locally in the function results in a "used before set" warning
8853 * - using va_start() to initialize it gives "function with fixed args" error */
8854static va_list ap;
8855
8856/*
8857 * "printf()" function
8858 */
8859 static void
8860f_printf(typval_T *argvars, typval_T *rettv)
8861{
8862 char_u buf[NUMBUFLEN];
8863 int len;
8864 char_u *s;
8865 int saved_did_emsg = did_emsg;
8866 char *fmt;
8867
8868 rettv->v_type = VAR_STRING;
8869 rettv->vval.v_string = NULL;
8870
8871 /* Get the required length, allocate the buffer and do it for real. */
8872 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008873 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008874 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875 if (!did_emsg)
8876 {
8877 s = alloc(len + 1);
8878 if (s != NULL)
8879 {
8880 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008881 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8882 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883 }
8884 }
8885 did_emsg |= saved_did_emsg;
8886}
8887
Bram Moolenaarf2732452018-06-03 14:47:35 +02008888#ifdef FEAT_JOB_CHANNEL
8889/*
8890 * "prompt_setcallback({buffer}, {callback})" function
8891 */
8892 static void
8893f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8894{
8895 buf_T *buf;
8896 char_u *callback;
8897 partial_T *partial;
8898
8899 if (check_secure())
8900 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008901 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008902 if (buf == NULL)
8903 return;
8904
8905 callback = get_callback(&argvars[1], &partial);
8906 if (callback == NULL)
8907 return;
8908
8909 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8910 if (partial == NULL)
8911 buf->b_prompt_callback = vim_strsave(callback);
8912 else
8913 /* pointer into the partial */
8914 buf->b_prompt_callback = callback;
8915 buf->b_prompt_partial = partial;
8916}
8917
8918/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008919 * "prompt_setinterrupt({buffer}, {callback})" function
8920 */
8921 static void
8922f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8923{
8924 buf_T *buf;
8925 char_u *callback;
8926 partial_T *partial;
8927
8928 if (check_secure())
8929 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008930 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008931 if (buf == NULL)
8932 return;
8933
8934 callback = get_callback(&argvars[1], &partial);
8935 if (callback == NULL)
8936 return;
8937
8938 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8939 if (partial == NULL)
8940 buf->b_prompt_interrupt = vim_strsave(callback);
8941 else
8942 /* pointer into the partial */
8943 buf->b_prompt_interrupt = callback;
8944 buf->b_prompt_int_partial = partial;
8945}
8946
8947/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008948 * "prompt_setprompt({buffer}, {text})" function
8949 */
8950 static void
8951f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8952{
8953 buf_T *buf;
8954 char_u *text;
8955
8956 if (check_secure())
8957 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008958 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008959 if (buf == NULL)
8960 return;
8961
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008962 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008963 vim_free(buf->b_prompt_text);
8964 buf->b_prompt_text = vim_strsave(text);
8965}
8966#endif
8967
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008968/*
8969 * "pumvisible()" function
8970 */
8971 static void
8972f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8973{
8974#ifdef FEAT_INS_EXPAND
8975 if (pum_visible())
8976 rettv->vval.v_number = 1;
8977#endif
8978}
8979
8980#ifdef FEAT_PYTHON3
8981/*
8982 * "py3eval()" function
8983 */
8984 static void
8985f_py3eval(typval_T *argvars, typval_T *rettv)
8986{
8987 char_u *str;
8988 char_u buf[NUMBUFLEN];
8989
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008990 if (check_restricted() || check_secure())
8991 return;
8992
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008993 if (p_pyx == 0)
8994 p_pyx = 3;
8995
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008996 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997 do_py3eval(str, rettv);
8998}
8999#endif
9000
9001#ifdef FEAT_PYTHON
9002/*
9003 * "pyeval()" function
9004 */
9005 static void
9006f_pyeval(typval_T *argvars, typval_T *rettv)
9007{
9008 char_u *str;
9009 char_u buf[NUMBUFLEN];
9010
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009011 if (check_restricted() || check_secure())
9012 return;
9013
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009014 if (p_pyx == 0)
9015 p_pyx = 2;
9016
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009017 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009018 do_pyeval(str, rettv);
9019}
9020#endif
9021
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009022#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9023/*
9024 * "pyxeval()" function
9025 */
9026 static void
9027f_pyxeval(typval_T *argvars, typval_T *rettv)
9028{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009029 if (check_restricted() || check_secure())
9030 return;
9031
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009032# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9033 init_pyxversion();
9034 if (p_pyx == 2)
9035 f_pyeval(argvars, rettv);
9036 else
9037 f_py3eval(argvars, rettv);
9038# elif defined(FEAT_PYTHON)
9039 f_pyeval(argvars, rettv);
9040# elif defined(FEAT_PYTHON3)
9041 f_py3eval(argvars, rettv);
9042# endif
9043}
9044#endif
9045
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009046/*
9047 * "range()" function
9048 */
9049 static void
9050f_range(typval_T *argvars, typval_T *rettv)
9051{
9052 varnumber_T start;
9053 varnumber_T end;
9054 varnumber_T stride = 1;
9055 varnumber_T i;
9056 int error = FALSE;
9057
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009058 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009059 if (argvars[1].v_type == VAR_UNKNOWN)
9060 {
9061 end = start - 1;
9062 start = 0;
9063 }
9064 else
9065 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009066 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009067 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009068 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009069 }
9070
9071 if (error)
9072 return; /* type error; errmsg already given */
9073 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009074 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009076 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009077 else
9078 {
9079 if (rettv_list_alloc(rettv) == OK)
9080 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9081 if (list_append_number(rettv->vval.v_list,
9082 (varnumber_T)i) == FAIL)
9083 break;
9084 }
9085}
9086
9087/*
9088 * "readfile()" function
9089 */
9090 static void
9091f_readfile(typval_T *argvars, typval_T *rettv)
9092{
9093 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009094 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009095 int failed = FALSE;
9096 char_u *fname;
9097 FILE *fd;
9098 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9099 int io_size = sizeof(buf);
9100 int readlen; /* size of last fread() */
9101 char_u *prev = NULL; /* previously read bytes, if any */
9102 long prevlen = 0; /* length of data in prev */
9103 long prevsize = 0; /* size of prev buffer */
9104 long maxline = MAXLNUM;
9105 long cnt = 0;
9106 char_u *p; /* position in buf */
9107 char_u *start; /* start of current line */
9108
9109 if (argvars[1].v_type != VAR_UNKNOWN)
9110 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009111 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009113 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9114 blob = TRUE;
9115
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009116 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009117 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009118 }
9119
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009120 if (blob)
9121 {
9122 if (rettv_blob_alloc(rettv) == FAIL)
9123 return;
9124 }
9125 else
9126 {
9127 if (rettv_list_alloc(rettv) == FAIL)
9128 return;
9129 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009130
9131 /* Always open the file in binary mode, library functions have a mind of
9132 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009133 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009134 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9135 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009136 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009137 return;
9138 }
9139
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009140 if (blob)
9141 {
9142 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9143 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009144 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009145 blob_free(rettv->vval.v_blob);
9146 }
9147 fclose(fd);
9148 return;
9149 }
9150
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009151 while (cnt < maxline || maxline < 0)
9152 {
9153 readlen = (int)fread(buf, 1, io_size, fd);
9154
9155 /* This for loop processes what was read, but is also entered at end
9156 * of file so that either:
9157 * - an incomplete line gets written
9158 * - a "binary" file gets an empty line at the end if it ends in a
9159 * newline. */
9160 for (p = buf, start = buf;
9161 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9162 ++p)
9163 {
9164 if (*p == '\n' || readlen <= 0)
9165 {
9166 listitem_T *li;
9167 char_u *s = NULL;
9168 long_u len = p - start;
9169
9170 /* Finished a line. Remove CRs before NL. */
9171 if (readlen > 0 && !binary)
9172 {
9173 while (len > 0 && start[len - 1] == '\r')
9174 --len;
9175 /* removal may cross back to the "prev" string */
9176 if (len == 0)
9177 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9178 --prevlen;
9179 }
9180 if (prevlen == 0)
9181 s = vim_strnsave(start, (int)len);
9182 else
9183 {
9184 /* Change "prev" buffer to be the right size. This way
9185 * the bytes are only copied once, and very long lines are
9186 * allocated only once. */
9187 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9188 {
9189 mch_memmove(s + prevlen, start, len);
9190 s[prevlen + len] = NUL;
9191 prev = NULL; /* the list will own the string */
9192 prevlen = prevsize = 0;
9193 }
9194 }
9195 if (s == NULL)
9196 {
9197 do_outofmem_msg((long_u) prevlen + len + 1);
9198 failed = TRUE;
9199 break;
9200 }
9201
9202 if ((li = listitem_alloc()) == NULL)
9203 {
9204 vim_free(s);
9205 failed = TRUE;
9206 break;
9207 }
9208 li->li_tv.v_type = VAR_STRING;
9209 li->li_tv.v_lock = 0;
9210 li->li_tv.vval.v_string = s;
9211 list_append(rettv->vval.v_list, li);
9212
9213 start = p + 1; /* step over newline */
9214 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9215 break;
9216 }
9217 else if (*p == NUL)
9218 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009219 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9220 * when finding the BF and check the previous two bytes. */
9221 else if (*p == 0xbf && enc_utf8 && !binary)
9222 {
9223 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9224 * + 1, these may be in the "prev" string. */
9225 char_u back1 = p >= buf + 1 ? p[-1]
9226 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9227 char_u back2 = p >= buf + 2 ? p[-2]
9228 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9229 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9230
9231 if (back2 == 0xef && back1 == 0xbb)
9232 {
9233 char_u *dest = p - 2;
9234
9235 /* Usually a BOM is at the beginning of a file, and so at
9236 * the beginning of a line; then we can just step over it.
9237 */
9238 if (start == dest)
9239 start = p + 1;
9240 else
9241 {
9242 /* have to shuffle buf to close gap */
9243 int adjust_prevlen = 0;
9244
9245 if (dest < buf)
9246 {
9247 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9248 dest = buf;
9249 }
9250 if (readlen > p - buf + 1)
9251 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9252 readlen -= 3 - adjust_prevlen;
9253 prevlen -= adjust_prevlen;
9254 p = dest - 1;
9255 }
9256 }
9257 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009258 } /* for */
9259
9260 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9261 break;
9262 if (start < p)
9263 {
9264 /* There's part of a line in buf, store it in "prev". */
9265 if (p - start + prevlen >= prevsize)
9266 {
9267 /* need bigger "prev" buffer */
9268 char_u *newprev;
9269
9270 /* A common use case is ordinary text files and "prev" gets a
9271 * fragment of a line, so the first allocation is made
9272 * small, to avoid repeatedly 'allocing' large and
9273 * 'reallocing' small. */
9274 if (prevsize == 0)
9275 prevsize = (long)(p - start);
9276 else
9277 {
9278 long grow50pc = (prevsize * 3) / 2;
9279 long growmin = (long)((p - start) * 2 + prevlen);
9280 prevsize = grow50pc > growmin ? grow50pc : growmin;
9281 }
9282 newprev = prev == NULL ? alloc(prevsize)
9283 : vim_realloc(prev, prevsize);
9284 if (newprev == NULL)
9285 {
9286 do_outofmem_msg((long_u)prevsize);
9287 failed = TRUE;
9288 break;
9289 }
9290 prev = newprev;
9291 }
9292 /* Add the line part to end of "prev". */
9293 mch_memmove(prev + prevlen, start, p - start);
9294 prevlen += (long)(p - start);
9295 }
9296 } /* while */
9297
9298 /*
9299 * For a negative line count use only the lines at the end of the file,
9300 * free the rest.
9301 */
9302 if (!failed && maxline < 0)
9303 while (cnt > -maxline)
9304 {
9305 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9306 --cnt;
9307 }
9308
9309 if (failed)
9310 {
9311 list_free(rettv->vval.v_list);
9312 /* readfile doc says an empty list is returned on error */
9313 rettv->vval.v_list = list_alloc();
9314 }
9315
9316 vim_free(prev);
9317 fclose(fd);
9318}
9319
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009320 static void
9321return_register(int regname, typval_T *rettv)
9322{
9323 char_u buf[2] = {0, 0};
9324
9325 buf[0] = (char_u)regname;
9326 rettv->v_type = VAR_STRING;
9327 rettv->vval.v_string = vim_strsave(buf);
9328}
9329
9330/*
9331 * "reg_executing()" function
9332 */
9333 static void
9334f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9335{
9336 return_register(reg_executing, rettv);
9337}
9338
9339/*
9340 * "reg_recording()" function
9341 */
9342 static void
9343f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9344{
9345 return_register(reg_recording, rettv);
9346}
9347
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009349/*
9350 * Convert a List to proftime_T.
9351 * Return FAIL when there is something wrong.
9352 */
9353 static int
9354list2proftime(typval_T *arg, proftime_T *tm)
9355{
9356 long n1, n2;
9357 int error = FALSE;
9358
9359 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9360 || arg->vval.v_list->lv_len != 2)
9361 return FAIL;
9362 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9363 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009364# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365 tm->HighPart = n1;
9366 tm->LowPart = n2;
9367# else
9368 tm->tv_sec = n1;
9369 tm->tv_usec = n2;
9370# endif
9371 return error ? FAIL : OK;
9372}
9373#endif /* FEAT_RELTIME */
9374
9375/*
9376 * "reltime()" function
9377 */
9378 static void
9379f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9380{
9381#ifdef FEAT_RELTIME
9382 proftime_T res;
9383 proftime_T start;
9384
9385 if (argvars[0].v_type == VAR_UNKNOWN)
9386 {
9387 /* No arguments: get current time. */
9388 profile_start(&res);
9389 }
9390 else if (argvars[1].v_type == VAR_UNKNOWN)
9391 {
9392 if (list2proftime(&argvars[0], &res) == FAIL)
9393 return;
9394 profile_end(&res);
9395 }
9396 else
9397 {
9398 /* Two arguments: compute the difference. */
9399 if (list2proftime(&argvars[0], &start) == FAIL
9400 || list2proftime(&argvars[1], &res) == FAIL)
9401 return;
9402 profile_sub(&res, &start);
9403 }
9404
9405 if (rettv_list_alloc(rettv) == OK)
9406 {
9407 long n1, n2;
9408
Bram Moolenaar4f974752019-02-17 17:44:42 +01009409# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009410 n1 = res.HighPart;
9411 n2 = res.LowPart;
9412# else
9413 n1 = res.tv_sec;
9414 n2 = res.tv_usec;
9415# endif
9416 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9417 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9418 }
9419#endif
9420}
9421
9422#ifdef FEAT_FLOAT
9423/*
9424 * "reltimefloat()" function
9425 */
9426 static void
9427f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9428{
9429# ifdef FEAT_RELTIME
9430 proftime_T tm;
9431# endif
9432
9433 rettv->v_type = VAR_FLOAT;
9434 rettv->vval.v_float = 0;
9435# ifdef FEAT_RELTIME
9436 if (list2proftime(&argvars[0], &tm) == OK)
9437 rettv->vval.v_float = profile_float(&tm);
9438# endif
9439}
9440#endif
9441
9442/*
9443 * "reltimestr()" function
9444 */
9445 static void
9446f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9447{
9448#ifdef FEAT_RELTIME
9449 proftime_T tm;
9450#endif
9451
9452 rettv->v_type = VAR_STRING;
9453 rettv->vval.v_string = NULL;
9454#ifdef FEAT_RELTIME
9455 if (list2proftime(&argvars[0], &tm) == OK)
9456 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9457#endif
9458}
9459
9460#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009461 static void
9462make_connection(void)
9463{
9464 if (X_DISPLAY == NULL
9465# ifdef FEAT_GUI
9466 && !gui.in_use
9467# endif
9468 )
9469 {
9470 x_force_connect = TRUE;
9471 setup_term_clip();
9472 x_force_connect = FALSE;
9473 }
9474}
9475
9476 static int
9477check_connection(void)
9478{
9479 make_connection();
9480 if (X_DISPLAY == NULL)
9481 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009482 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009483 return FAIL;
9484 }
9485 return OK;
9486}
9487#endif
9488
9489#ifdef FEAT_CLIENTSERVER
9490 static void
9491remote_common(typval_T *argvars, typval_T *rettv, int expr)
9492{
9493 char_u *server_name;
9494 char_u *keys;
9495 char_u *r = NULL;
9496 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009497 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009498# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009499 HWND w;
9500# else
9501 Window w;
9502# endif
9503
9504 if (check_restricted() || check_secure())
9505 return;
9506
9507# ifdef FEAT_X11
9508 if (check_connection() == FAIL)
9509 return;
9510# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009511 if (argvars[2].v_type != VAR_UNKNOWN
9512 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009513 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009514
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009515 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009516 if (server_name == NULL)
9517 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009518 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009519# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009520 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009521# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009522 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9523 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009524# endif
9525 {
9526 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009527 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009528 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009529 vim_free(r);
9530 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009531 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009532 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009533 return;
9534 }
9535
9536 rettv->vval.v_string = r;
9537
9538 if (argvars[2].v_type != VAR_UNKNOWN)
9539 {
9540 dictitem_T v;
9541 char_u str[30];
9542 char_u *idvar;
9543
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009544 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009545 if (idvar != NULL && *idvar != NUL)
9546 {
9547 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9548 v.di_tv.v_type = VAR_STRING;
9549 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009550 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009551 vim_free(v.di_tv.vval.v_string);
9552 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009553 }
9554}
9555#endif
9556
9557/*
9558 * "remote_expr()" function
9559 */
9560 static void
9561f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9562{
9563 rettv->v_type = VAR_STRING;
9564 rettv->vval.v_string = NULL;
9565#ifdef FEAT_CLIENTSERVER
9566 remote_common(argvars, rettv, TRUE);
9567#endif
9568}
9569
9570/*
9571 * "remote_foreground()" function
9572 */
9573 static void
9574f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9575{
9576#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009577# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009578 /* On Win32 it's done in this application. */
9579 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009580 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009581
9582 if (server_name != NULL)
9583 serverForeground(server_name);
9584 }
9585# else
9586 /* Send a foreground() expression to the server. */
9587 argvars[1].v_type = VAR_STRING;
9588 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9589 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009590 rettv->v_type = VAR_STRING;
9591 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009592 remote_common(argvars, rettv, TRUE);
9593 vim_free(argvars[1].vval.v_string);
9594# endif
9595#endif
9596}
9597
9598 static void
9599f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9600{
9601#ifdef FEAT_CLIENTSERVER
9602 dictitem_T v;
9603 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009604# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009605 long_u n = 0;
9606# endif
9607 char_u *serverid;
9608
9609 if (check_restricted() || check_secure())
9610 {
9611 rettv->vval.v_number = -1;
9612 return;
9613 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009614 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009615 if (serverid == NULL)
9616 {
9617 rettv->vval.v_number = -1;
9618 return; /* type error; errmsg already given */
9619 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009620# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009621 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9622 if (n == 0)
9623 rettv->vval.v_number = -1;
9624 else
9625 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009626 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009627 rettv->vval.v_number = (s != NULL);
9628 }
9629# else
9630 if (check_connection() == FAIL)
9631 return;
9632
9633 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9634 serverStrToWin(serverid), &s);
9635# endif
9636
9637 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9638 {
9639 char_u *retvar;
9640
9641 v.di_tv.v_type = VAR_STRING;
9642 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009643 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009644 if (retvar != NULL)
9645 set_var(retvar, &v.di_tv, FALSE);
9646 vim_free(v.di_tv.vval.v_string);
9647 }
9648#else
9649 rettv->vval.v_number = -1;
9650#endif
9651}
9652
9653 static void
9654f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9655{
9656 char_u *r = NULL;
9657
9658#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009659 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009660
9661 if (serverid != NULL && !check_restricted() && !check_secure())
9662 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009663 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009664# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009665 /* The server's HWND is encoded in the 'id' parameter */
9666 long_u n = 0;
9667# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009668
9669 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009670 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009671
Bram Moolenaar4f974752019-02-17 17:44:42 +01009672# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009673 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9674 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009675 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009676 if (r == NULL)
9677# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009678 if (check_connection() == FAIL
9679 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9680 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009681# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009682 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683 }
9684#endif
9685 rettv->v_type = VAR_STRING;
9686 rettv->vval.v_string = r;
9687}
9688
9689/*
9690 * "remote_send()" function
9691 */
9692 static void
9693f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9694{
9695 rettv->v_type = VAR_STRING;
9696 rettv->vval.v_string = NULL;
9697#ifdef FEAT_CLIENTSERVER
9698 remote_common(argvars, rettv, FALSE);
9699#endif
9700}
9701
9702/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009703 * "remote_startserver()" function
9704 */
9705 static void
9706f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9707{
9708#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009709 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009710
9711 if (server == NULL)
9712 return; /* type error; errmsg already given */
9713 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009714 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009715 else
9716 {
9717# ifdef FEAT_X11
9718 if (check_connection() == OK)
9719 serverRegisterName(X_DISPLAY, server);
9720# else
9721 serverSetName(server);
9722# endif
9723 }
9724#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009725 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009726#endif
9727}
9728
9729/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009730 * "remove()" function
9731 */
9732 static void
9733f_remove(typval_T *argvars, typval_T *rettv)
9734{
9735 list_T *l;
9736 listitem_T *item, *item2;
9737 listitem_T *li;
9738 long idx;
9739 long end;
9740 char_u *key;
9741 dict_T *d;
9742 dictitem_T *di;
9743 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009744 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009745
9746 if (argvars[0].v_type == VAR_DICT)
9747 {
9748 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009749 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009750 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009751 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009752 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009753 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009754 if (key != NULL)
9755 {
9756 di = dict_find(d, key, -1);
9757 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009758 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009759 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9760 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9761 {
9762 *rettv = di->di_tv;
9763 init_tv(&di->di_tv);
9764 dictitem_remove(d, di);
9765 }
9766 }
9767 }
9768 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009769 else if (argvars[0].v_type == VAR_BLOB)
9770 {
9771 idx = (long)tv_get_number_chk(&argvars[1], &error);
9772 if (!error)
9773 {
9774 blob_T *b = argvars[0].vval.v_blob;
9775 int len = blob_len(b);
9776 char_u *p;
9777
9778 if (idx < 0)
9779 // count from the end
9780 idx = len + idx;
9781 if (idx < 0 || idx >= len)
9782 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009783 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009784 return;
9785 }
9786 if (argvars[2].v_type == VAR_UNKNOWN)
9787 {
9788 // Remove one item, return its value.
9789 p = (char_u *)b->bv_ga.ga_data;
9790 rettv->vval.v_number = (varnumber_T) *(p + idx);
9791 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9792 --b->bv_ga.ga_len;
9793 }
9794 else
9795 {
9796 blob_T *blob;
9797
9798 // Remove range of items, return list with values.
9799 end = (long)tv_get_number_chk(&argvars[2], &error);
9800 if (error)
9801 return;
9802 if (end < 0)
9803 // count from the end
9804 end = len + end;
9805 if (end >= len || idx > end)
9806 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009807 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009808 return;
9809 }
9810 blob = blob_alloc();
9811 if (blob == NULL)
9812 return;
9813 blob->bv_ga.ga_len = end - idx + 1;
9814 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9815 {
9816 vim_free(blob);
9817 return;
9818 }
9819 p = (char_u *)b->bv_ga.ga_data;
9820 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9821 (size_t)(end - idx + 1));
9822 ++blob->bv_refcount;
9823 rettv->v_type = VAR_BLOB;
9824 rettv->vval.v_blob = blob;
9825
9826 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9827 b->bv_ga.ga_len -= end - idx + 1;
9828 }
9829 }
9830 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009832 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009833 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009834 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009835 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009836 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009837 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009838 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009839 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009840 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009841 else
9842 {
9843 if (argvars[2].v_type == VAR_UNKNOWN)
9844 {
9845 /* Remove one item, return its value. */
9846 vimlist_remove(l, item, item);
9847 *rettv = item->li_tv;
9848 vim_free(item);
9849 }
9850 else
9851 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009852 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009853 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009854 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009855 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009856 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009857 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009858 else
9859 {
9860 int cnt = 0;
9861
9862 for (li = item; li != NULL; li = li->li_next)
9863 {
9864 ++cnt;
9865 if (li == item2)
9866 break;
9867 }
9868 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009869 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009870 else
9871 {
9872 vimlist_remove(l, item, item2);
9873 if (rettv_list_alloc(rettv) == OK)
9874 {
9875 l = rettv->vval.v_list;
9876 l->lv_first = item;
9877 l->lv_last = item2;
9878 item->li_prev = NULL;
9879 item2->li_next = NULL;
9880 l->lv_len = cnt;
9881 }
9882 }
9883 }
9884 }
9885 }
9886 }
9887}
9888
9889/*
9890 * "rename({from}, {to})" function
9891 */
9892 static void
9893f_rename(typval_T *argvars, typval_T *rettv)
9894{
9895 char_u buf[NUMBUFLEN];
9896
9897 if (check_restricted() || check_secure())
9898 rettv->vval.v_number = -1;
9899 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009900 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9901 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009902}
9903
9904/*
9905 * "repeat()" function
9906 */
9907 static void
9908f_repeat(typval_T *argvars, typval_T *rettv)
9909{
9910 char_u *p;
9911 int n;
9912 int slen;
9913 int len;
9914 char_u *r;
9915 int i;
9916
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009917 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009918 if (argvars[0].v_type == VAR_LIST)
9919 {
9920 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9921 while (n-- > 0)
9922 if (list_extend(rettv->vval.v_list,
9923 argvars[0].vval.v_list, NULL) == FAIL)
9924 break;
9925 }
9926 else
9927 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009928 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009929 rettv->v_type = VAR_STRING;
9930 rettv->vval.v_string = NULL;
9931
9932 slen = (int)STRLEN(p);
9933 len = slen * n;
9934 if (len <= 0)
9935 return;
9936
9937 r = alloc(len + 1);
9938 if (r != NULL)
9939 {
9940 for (i = 0; i < n; i++)
9941 mch_memmove(r + i * slen, p, (size_t)slen);
9942 r[len] = NUL;
9943 }
9944
9945 rettv->vval.v_string = r;
9946 }
9947}
9948
9949/*
9950 * "resolve()" function
9951 */
9952 static void
9953f_resolve(typval_T *argvars, typval_T *rettv)
9954{
9955 char_u *p;
9956#ifdef HAVE_READLINK
9957 char_u *buf = NULL;
9958#endif
9959
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009960 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009961#ifdef FEAT_SHORTCUT
9962 {
9963 char_u *v = NULL;
9964
Bram Moolenaardce1e892019-02-10 23:18:53 +01009965 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009966 if (v != NULL)
9967 rettv->vval.v_string = v;
9968 else
9969 rettv->vval.v_string = vim_strsave(p);
9970 }
9971#else
9972# ifdef HAVE_READLINK
9973 {
9974 char_u *cpy;
9975 int len;
9976 char_u *remain = NULL;
9977 char_u *q;
9978 int is_relative_to_current = FALSE;
9979 int has_trailing_pathsep = FALSE;
9980 int limit = 100;
9981
9982 p = vim_strsave(p);
9983
9984 if (p[0] == '.' && (vim_ispathsep(p[1])
9985 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9986 is_relative_to_current = TRUE;
9987
9988 len = STRLEN(p);
9989 if (len > 0 && after_pathsep(p, p + len))
9990 {
9991 has_trailing_pathsep = TRUE;
9992 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9993 }
9994
9995 q = getnextcomp(p);
9996 if (*q != NUL)
9997 {
9998 /* Separate the first path component in "p", and keep the
9999 * remainder (beginning with the path separator). */
10000 remain = vim_strsave(q - 1);
10001 q[-1] = NUL;
10002 }
10003
10004 buf = alloc(MAXPATHL + 1);
10005 if (buf == NULL)
10006 goto fail;
10007
10008 for (;;)
10009 {
10010 for (;;)
10011 {
10012 len = readlink((char *)p, (char *)buf, MAXPATHL);
10013 if (len <= 0)
10014 break;
10015 buf[len] = NUL;
10016
10017 if (limit-- == 0)
10018 {
10019 vim_free(p);
10020 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010021 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010022 rettv->vval.v_string = NULL;
10023 goto fail;
10024 }
10025
10026 /* Ensure that the result will have a trailing path separator
10027 * if the argument has one. */
10028 if (remain == NULL && has_trailing_pathsep)
10029 add_pathsep(buf);
10030
10031 /* Separate the first path component in the link value and
10032 * concatenate the remainders. */
10033 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10034 if (*q != NUL)
10035 {
10036 if (remain == NULL)
10037 remain = vim_strsave(q - 1);
10038 else
10039 {
10040 cpy = concat_str(q - 1, remain);
10041 if (cpy != NULL)
10042 {
10043 vim_free(remain);
10044 remain = cpy;
10045 }
10046 }
10047 q[-1] = NUL;
10048 }
10049
10050 q = gettail(p);
10051 if (q > p && *q == NUL)
10052 {
10053 /* Ignore trailing path separator. */
10054 q[-1] = NUL;
10055 q = gettail(p);
10056 }
10057 if (q > p && !mch_isFullName(buf))
10058 {
10059 /* symlink is relative to directory of argument */
10060 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10061 if (cpy != NULL)
10062 {
10063 STRCPY(cpy, p);
10064 STRCPY(gettail(cpy), buf);
10065 vim_free(p);
10066 p = cpy;
10067 }
10068 }
10069 else
10070 {
10071 vim_free(p);
10072 p = vim_strsave(buf);
10073 }
10074 }
10075
10076 if (remain == NULL)
10077 break;
10078
10079 /* Append the first path component of "remain" to "p". */
10080 q = getnextcomp(remain + 1);
10081 len = q - remain - (*q != NUL);
10082 cpy = vim_strnsave(p, STRLEN(p) + len);
10083 if (cpy != NULL)
10084 {
10085 STRNCAT(cpy, remain, len);
10086 vim_free(p);
10087 p = cpy;
10088 }
10089 /* Shorten "remain". */
10090 if (*q != NUL)
10091 STRMOVE(remain, q - 1);
10092 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010093 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010094 }
10095
10096 /* If the result is a relative path name, make it explicitly relative to
10097 * the current directory if and only if the argument had this form. */
10098 if (!vim_ispathsep(*p))
10099 {
10100 if (is_relative_to_current
10101 && *p != NUL
10102 && !(p[0] == '.'
10103 && (p[1] == NUL
10104 || vim_ispathsep(p[1])
10105 || (p[1] == '.'
10106 && (p[2] == NUL
10107 || vim_ispathsep(p[2]))))))
10108 {
10109 /* Prepend "./". */
10110 cpy = concat_str((char_u *)"./", p);
10111 if (cpy != NULL)
10112 {
10113 vim_free(p);
10114 p = cpy;
10115 }
10116 }
10117 else if (!is_relative_to_current)
10118 {
10119 /* Strip leading "./". */
10120 q = p;
10121 while (q[0] == '.' && vim_ispathsep(q[1]))
10122 q += 2;
10123 if (q > p)
10124 STRMOVE(p, p + 2);
10125 }
10126 }
10127
10128 /* Ensure that the result will have no trailing path separator
10129 * if the argument had none. But keep "/" or "//". */
10130 if (!has_trailing_pathsep)
10131 {
10132 q = p + STRLEN(p);
10133 if (after_pathsep(p, q))
10134 *gettail_sep(p) = NUL;
10135 }
10136
10137 rettv->vval.v_string = p;
10138 }
10139# else
10140 rettv->vval.v_string = vim_strsave(p);
10141# endif
10142#endif
10143
10144 simplify_filename(rettv->vval.v_string);
10145
10146#ifdef HAVE_READLINK
10147fail:
10148 vim_free(buf);
10149#endif
10150 rettv->v_type = VAR_STRING;
10151}
10152
10153/*
10154 * "reverse({list})" function
10155 */
10156 static void
10157f_reverse(typval_T *argvars, typval_T *rettv)
10158{
10159 list_T *l;
10160 listitem_T *li, *ni;
10161
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010162 if (argvars[0].v_type == VAR_BLOB)
10163 {
10164 blob_T *b = argvars[0].vval.v_blob;
10165 int i, len = blob_len(b);
10166
10167 for (i = 0; i < len / 2; i++)
10168 {
10169 int tmp = blob_get(b, i);
10170
10171 blob_set(b, i, blob_get(b, len - i - 1));
10172 blob_set(b, len - i - 1, tmp);
10173 }
10174 rettv_blob_set(rettv, b);
10175 return;
10176 }
10177
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010178 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010179 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010180 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010181 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010182 (char_u *)N_("reverse() argument"), TRUE))
10183 {
10184 li = l->lv_last;
10185 l->lv_first = l->lv_last = NULL;
10186 l->lv_len = 0;
10187 while (li != NULL)
10188 {
10189 ni = li->li_prev;
10190 list_append(l, li);
10191 li = ni;
10192 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010193 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010194 l->lv_idx = l->lv_len - l->lv_idx - 1;
10195 }
10196}
10197
10198#define SP_NOMOVE 0x01 /* don't move cursor */
10199#define SP_REPEAT 0x02 /* repeat to find outer pair */
10200#define SP_RETCOUNT 0x04 /* return matchcount */
10201#define SP_SETPCMARK 0x08 /* set previous context mark */
10202#define SP_START 0x10 /* accept match at start position */
10203#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10204#define SP_END 0x40 /* leave cursor at end of match */
10205#define SP_COLUMN 0x80 /* start at cursor column */
10206
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010207/*
10208 * Get flags for a search function.
10209 * Possibly sets "p_ws".
10210 * Returns BACKWARD, FORWARD or zero (for an error).
10211 */
10212 static int
10213get_search_arg(typval_T *varp, int *flagsp)
10214{
10215 int dir = FORWARD;
10216 char_u *flags;
10217 char_u nbuf[NUMBUFLEN];
10218 int mask;
10219
10220 if (varp->v_type != VAR_UNKNOWN)
10221 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010222 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010223 if (flags == NULL)
10224 return 0; /* type error; errmsg already given */
10225 while (*flags != NUL)
10226 {
10227 switch (*flags)
10228 {
10229 case 'b': dir = BACKWARD; break;
10230 case 'w': p_ws = TRUE; break;
10231 case 'W': p_ws = FALSE; break;
10232 default: mask = 0;
10233 if (flagsp != NULL)
10234 switch (*flags)
10235 {
10236 case 'c': mask = SP_START; break;
10237 case 'e': mask = SP_END; break;
10238 case 'm': mask = SP_RETCOUNT; break;
10239 case 'n': mask = SP_NOMOVE; break;
10240 case 'p': mask = SP_SUBPAT; break;
10241 case 'r': mask = SP_REPEAT; break;
10242 case 's': mask = SP_SETPCMARK; break;
10243 case 'z': mask = SP_COLUMN; break;
10244 }
10245 if (mask == 0)
10246 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010247 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010248 dir = 0;
10249 }
10250 else
10251 *flagsp |= mask;
10252 }
10253 if (dir == 0)
10254 break;
10255 ++flags;
10256 }
10257 }
10258 return dir;
10259}
10260
10261/*
10262 * Shared by search() and searchpos() functions.
10263 */
10264 static int
10265search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10266{
10267 int flags;
10268 char_u *pat;
10269 pos_T pos;
10270 pos_T save_cursor;
10271 int save_p_ws = p_ws;
10272 int dir;
10273 int retval = 0; /* default: FAIL */
10274 long lnum_stop = 0;
10275 proftime_T tm;
10276#ifdef FEAT_RELTIME
10277 long time_limit = 0;
10278#endif
10279 int options = SEARCH_KEEP;
10280 int subpatnum;
10281
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010282 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010283 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10284 if (dir == 0)
10285 goto theend;
10286 flags = *flagsp;
10287 if (flags & SP_START)
10288 options |= SEARCH_START;
10289 if (flags & SP_END)
10290 options |= SEARCH_END;
10291 if (flags & SP_COLUMN)
10292 options |= SEARCH_COL;
10293
10294 /* Optional arguments: line number to stop searching and timeout. */
10295 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010297 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010298 if (lnum_stop < 0)
10299 goto theend;
10300#ifdef FEAT_RELTIME
10301 if (argvars[3].v_type != VAR_UNKNOWN)
10302 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010303 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010304 if (time_limit < 0)
10305 goto theend;
10306 }
10307#endif
10308 }
10309
10310#ifdef FEAT_RELTIME
10311 /* Set the time limit, if there is one. */
10312 profile_setlimit(time_limit, &tm);
10313#endif
10314
10315 /*
10316 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10317 * Check to make sure only those flags are set.
10318 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10319 * flags cannot be set. Check for that condition also.
10320 */
10321 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10322 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10323 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010324 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010325 goto theend;
10326 }
10327
10328 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010329 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010330 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010331 if (subpatnum != FAIL)
10332 {
10333 if (flags & SP_SUBPAT)
10334 retval = subpatnum;
10335 else
10336 retval = pos.lnum;
10337 if (flags & SP_SETPCMARK)
10338 setpcmark();
10339 curwin->w_cursor = pos;
10340 if (match_pos != NULL)
10341 {
10342 /* Store the match cursor position */
10343 match_pos->lnum = pos.lnum;
10344 match_pos->col = pos.col + 1;
10345 }
10346 /* "/$" will put the cursor after the end of the line, may need to
10347 * correct that here */
10348 check_cursor();
10349 }
10350
10351 /* If 'n' flag is used: restore cursor position. */
10352 if (flags & SP_NOMOVE)
10353 curwin->w_cursor = save_cursor;
10354 else
10355 curwin->w_set_curswant = TRUE;
10356theend:
10357 p_ws = save_p_ws;
10358
10359 return retval;
10360}
10361
10362#ifdef FEAT_FLOAT
10363
10364/*
10365 * round() is not in C90, use ceil() or floor() instead.
10366 */
10367 float_T
10368vim_round(float_T f)
10369{
10370 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10371}
10372
10373/*
10374 * "round({float})" function
10375 */
10376 static void
10377f_round(typval_T *argvars, typval_T *rettv)
10378{
10379 float_T f = 0.0;
10380
10381 rettv->v_type = VAR_FLOAT;
10382 if (get_float_arg(argvars, &f) == OK)
10383 rettv->vval.v_float = vim_round(f);
10384 else
10385 rettv->vval.v_float = 0.0;
10386}
10387#endif
10388
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010389#ifdef FEAT_RUBY
10390/*
10391 * "rubyeval()" function
10392 */
10393 static void
10394f_rubyeval(typval_T *argvars, typval_T *rettv)
10395{
10396 char_u *str;
10397 char_u buf[NUMBUFLEN];
10398
10399 str = tv_get_string_buf(&argvars[0], buf);
10400 do_rubyeval(str, rettv);
10401}
10402#endif
10403
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010404/*
10405 * "screenattr()" function
10406 */
10407 static void
10408f_screenattr(typval_T *argvars, typval_T *rettv)
10409{
10410 int row;
10411 int col;
10412 int c;
10413
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010414 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10415 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010416 if (row < 0 || row >= screen_Rows
10417 || col < 0 || col >= screen_Columns)
10418 c = -1;
10419 else
10420 c = ScreenAttrs[LineOffset[row] + col];
10421 rettv->vval.v_number = c;
10422}
10423
10424/*
10425 * "screenchar()" function
10426 */
10427 static void
10428f_screenchar(typval_T *argvars, typval_T *rettv)
10429{
10430 int row;
10431 int col;
10432 int off;
10433 int c;
10434
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010435 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10436 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010437 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010438 c = -1;
10439 else
10440 {
10441 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010442 if (enc_utf8 && ScreenLinesUC[off] != 0)
10443 c = ScreenLinesUC[off];
10444 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010445 c = ScreenLines[off];
10446 }
10447 rettv->vval.v_number = c;
10448}
10449
10450/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010451 * "screenchars()" function
10452 */
10453 static void
10454f_screenchars(typval_T *argvars, typval_T *rettv)
10455{
10456 int row;
10457 int col;
10458 int off;
10459 int c;
10460 int i;
10461
10462 if (rettv_list_alloc(rettv) == FAIL)
10463 return;
10464 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10465 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10466 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10467 return;
10468
10469 off = LineOffset[row] + col;
10470 if (enc_utf8 && ScreenLinesUC[off] != 0)
10471 c = ScreenLinesUC[off];
10472 else
10473 c = ScreenLines[off];
10474 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10475
10476 if (enc_utf8)
10477
10478 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10479 list_append_number(rettv->vval.v_list,
10480 (varnumber_T)ScreenLinesC[i][off]);
10481}
10482
10483/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010484 * "screencol()" function
10485 *
10486 * First column is 1 to be consistent with virtcol().
10487 */
10488 static void
10489f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10490{
10491 rettv->vval.v_number = screen_screencol() + 1;
10492}
10493
10494/*
10495 * "screenrow()" function
10496 */
10497 static void
10498f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10499{
10500 rettv->vval.v_number = screen_screenrow() + 1;
10501}
10502
10503/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010504 * "screenstring()" function
10505 */
10506 static void
10507f_screenstring(typval_T *argvars, typval_T *rettv)
10508{
10509 int row;
10510 int col;
10511 int off;
10512 int c;
10513 int i;
10514 char_u buf[MB_MAXBYTES + 1];
10515 int buflen = 0;
10516
10517 rettv->vval.v_string = NULL;
10518 rettv->v_type = VAR_STRING;
10519
10520 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10521 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10522 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10523 return;
10524
10525 off = LineOffset[row] + col;
10526 if (enc_utf8 && ScreenLinesUC[off] != 0)
10527 c = ScreenLinesUC[off];
10528 else
10529 c = ScreenLines[off];
10530 buflen += mb_char2bytes(c, buf);
10531
10532 if (enc_utf8)
10533 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10534 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10535
10536 buf[buflen] = NUL;
10537 rettv->vval.v_string = vim_strsave(buf);
10538}
10539
10540/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010541 * "search()" function
10542 */
10543 static void
10544f_search(typval_T *argvars, typval_T *rettv)
10545{
10546 int flags = 0;
10547
10548 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10549}
10550
10551/*
10552 * "searchdecl()" function
10553 */
10554 static void
10555f_searchdecl(typval_T *argvars, typval_T *rettv)
10556{
10557 int locally = 1;
10558 int thisblock = 0;
10559 int error = FALSE;
10560 char_u *name;
10561
10562 rettv->vval.v_number = 1; /* default: FAIL */
10563
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010564 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010565 if (argvars[1].v_type != VAR_UNKNOWN)
10566 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010567 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010568 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010569 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010570 }
10571 if (!error && name != NULL)
10572 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10573 locally, thisblock, SEARCH_KEEP) == FAIL;
10574}
10575
10576/*
10577 * Used by searchpair() and searchpairpos()
10578 */
10579 static int
10580searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10581{
10582 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010583 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010584 int save_p_ws = p_ws;
10585 int dir;
10586 int flags = 0;
10587 char_u nbuf1[NUMBUFLEN];
10588 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010589 int retval = 0; /* default: FAIL */
10590 long lnum_stop = 0;
10591 long time_limit = 0;
10592
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010593 /* Get the three pattern arguments: start, middle, end. Will result in an
10594 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010595 spat = tv_get_string_chk(&argvars[0]);
10596 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10597 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010598 if (spat == NULL || mpat == NULL || epat == NULL)
10599 goto theend; /* type error */
10600
10601 /* Handle the optional fourth argument: flags */
10602 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10603 if (dir == 0)
10604 goto theend;
10605
10606 /* Don't accept SP_END or SP_SUBPAT.
10607 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10608 */
10609 if ((flags & (SP_END | SP_SUBPAT)) != 0
10610 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010612 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010613 goto theend;
10614 }
10615
10616 /* Using 'r' implies 'W', otherwise it doesn't work. */
10617 if (flags & SP_REPEAT)
10618 p_ws = FALSE;
10619
10620 /* Optional fifth argument: skip expression */
10621 if (argvars[3].v_type == VAR_UNKNOWN
10622 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010623 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010624 else
10625 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010626 skip = &argvars[4];
10627 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10628 && skip->v_type != VAR_STRING)
10629 {
10630 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010631 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010632 goto theend;
10633 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010634 if (argvars[5].v_type != VAR_UNKNOWN)
10635 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010636 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010637 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010638 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010639 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010640 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010641 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010642#ifdef FEAT_RELTIME
10643 if (argvars[6].v_type != VAR_UNKNOWN)
10644 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010645 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010646 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010648 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010649 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010650 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010651 }
10652#endif
10653 }
10654 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010655
10656 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10657 match_pos, lnum_stop, time_limit);
10658
10659theend:
10660 p_ws = save_p_ws;
10661
10662 return retval;
10663}
10664
10665/*
10666 * "searchpair()" function
10667 */
10668 static void
10669f_searchpair(typval_T *argvars, typval_T *rettv)
10670{
10671 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10672}
10673
10674/*
10675 * "searchpairpos()" function
10676 */
10677 static void
10678f_searchpairpos(typval_T *argvars, typval_T *rettv)
10679{
10680 pos_T match_pos;
10681 int lnum = 0;
10682 int col = 0;
10683
10684 if (rettv_list_alloc(rettv) == FAIL)
10685 return;
10686
10687 if (searchpair_cmn(argvars, &match_pos) > 0)
10688 {
10689 lnum = match_pos.lnum;
10690 col = match_pos.col;
10691 }
10692
10693 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10694 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10695}
10696
10697/*
10698 * Search for a start/middle/end thing.
10699 * Used by searchpair(), see its documentation for the details.
10700 * Returns 0 or -1 for no match,
10701 */
10702 long
10703do_searchpair(
10704 char_u *spat, /* start pattern */
10705 char_u *mpat, /* middle pattern */
10706 char_u *epat, /* end pattern */
10707 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010708 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010709 int flags, /* SP_SETPCMARK and other SP_ values */
10710 pos_T *match_pos,
10711 linenr_T lnum_stop, /* stop at this line if not zero */
10712 long time_limit UNUSED) /* stop after this many msec */
10713{
10714 char_u *save_cpo;
10715 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10716 long retval = 0;
10717 pos_T pos;
10718 pos_T firstpos;
10719 pos_T foundpos;
10720 pos_T save_cursor;
10721 pos_T save_pos;
10722 int n;
10723 int r;
10724 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010725 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010726 int err;
10727 int options = SEARCH_KEEP;
10728 proftime_T tm;
10729
10730 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10731 save_cpo = p_cpo;
10732 p_cpo = empty_option;
10733
10734#ifdef FEAT_RELTIME
10735 /* Set the time limit, if there is one. */
10736 profile_setlimit(time_limit, &tm);
10737#endif
10738
10739 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10740 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010741 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10742 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010743 if (pat2 == NULL || pat3 == NULL)
10744 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010745 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010746 if (*mpat == NUL)
10747 STRCPY(pat3, pat2);
10748 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010749 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010750 spat, epat, mpat);
10751 if (flags & SP_START)
10752 options |= SEARCH_START;
10753
Bram Moolenaar48570482017-10-30 21:48:41 +010010754 if (skip != NULL)
10755 {
10756 /* Empty string means to not use the skip expression. */
10757 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10758 use_skip = skip->vval.v_string != NULL
10759 && *skip->vval.v_string != NUL;
10760 }
10761
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010762 save_cursor = curwin->w_cursor;
10763 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010764 CLEAR_POS(&firstpos);
10765 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010766 pat = pat3;
10767 for (;;)
10768 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010769 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010770 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010771 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010772 /* didn't find it or found the first match again: FAIL */
10773 break;
10774
10775 if (firstpos.lnum == 0)
10776 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010777 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010778 {
10779 /* Found the same position again. Can happen with a pattern that
10780 * has "\zs" at the end and searching backwards. Advance one
10781 * character and try again. */
10782 if (dir == BACKWARD)
10783 decl(&pos);
10784 else
10785 incl(&pos);
10786 }
10787 foundpos = pos;
10788
10789 /* clear the start flag to avoid getting stuck here */
10790 options &= ~SEARCH_START;
10791
10792 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010793 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010794 {
10795 save_pos = curwin->w_cursor;
10796 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010797 err = FALSE;
10798 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010799 curwin->w_cursor = save_pos;
10800 if (err)
10801 {
10802 /* Evaluating {skip} caused an error, break here. */
10803 curwin->w_cursor = save_cursor;
10804 retval = -1;
10805 break;
10806 }
10807 if (r)
10808 continue;
10809 }
10810
10811 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10812 {
10813 /* Found end when searching backwards or start when searching
10814 * forward: nested pair. */
10815 ++nest;
10816 pat = pat2; /* nested, don't search for middle */
10817 }
10818 else
10819 {
10820 /* Found end when searching forward or start when searching
10821 * backward: end of (nested) pair; or found middle in outer pair. */
10822 if (--nest == 1)
10823 pat = pat3; /* outer level, search for middle */
10824 }
10825
10826 if (nest == 0)
10827 {
10828 /* Found the match: return matchcount or line number. */
10829 if (flags & SP_RETCOUNT)
10830 ++retval;
10831 else
10832 retval = pos.lnum;
10833 if (flags & SP_SETPCMARK)
10834 setpcmark();
10835 curwin->w_cursor = pos;
10836 if (!(flags & SP_REPEAT))
10837 break;
10838 nest = 1; /* search for next unmatched */
10839 }
10840 }
10841
10842 if (match_pos != NULL)
10843 {
10844 /* Store the match cursor position */
10845 match_pos->lnum = curwin->w_cursor.lnum;
10846 match_pos->col = curwin->w_cursor.col + 1;
10847 }
10848
10849 /* If 'n' flag is used or search failed: restore cursor position. */
10850 if ((flags & SP_NOMOVE) || retval == 0)
10851 curwin->w_cursor = save_cursor;
10852
10853theend:
10854 vim_free(pat2);
10855 vim_free(pat3);
10856 if (p_cpo == empty_option)
10857 p_cpo = save_cpo;
10858 else
10859 /* Darn, evaluating the {skip} expression changed the value. */
10860 free_string_option(save_cpo);
10861
10862 return retval;
10863}
10864
10865/*
10866 * "searchpos()" function
10867 */
10868 static void
10869f_searchpos(typval_T *argvars, typval_T *rettv)
10870{
10871 pos_T match_pos;
10872 int lnum = 0;
10873 int col = 0;
10874 int n;
10875 int flags = 0;
10876
10877 if (rettv_list_alloc(rettv) == FAIL)
10878 return;
10879
10880 n = search_cmn(argvars, &match_pos, &flags);
10881 if (n > 0)
10882 {
10883 lnum = match_pos.lnum;
10884 col = match_pos.col;
10885 }
10886
10887 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10888 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10889 if (flags & SP_SUBPAT)
10890 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10891}
10892
10893 static void
10894f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10895{
10896#ifdef FEAT_CLIENTSERVER
10897 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010898 char_u *server = tv_get_string_chk(&argvars[0]);
10899 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010900
10901 rettv->vval.v_number = -1;
10902 if (server == NULL || reply == NULL)
10903 return;
10904 if (check_restricted() || check_secure())
10905 return;
10906# ifdef FEAT_X11
10907 if (check_connection() == FAIL)
10908 return;
10909# endif
10910
10911 if (serverSendReply(server, reply) < 0)
10912 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010913 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010914 return;
10915 }
10916 rettv->vval.v_number = 0;
10917#else
10918 rettv->vval.v_number = -1;
10919#endif
10920}
10921
10922 static void
10923f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10924{
10925 char_u *r = NULL;
10926
10927#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010928# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010929 r = serverGetVimNames();
10930# else
10931 make_connection();
10932 if (X_DISPLAY != NULL)
10933 r = serverGetVimNames(X_DISPLAY);
10934# endif
10935#endif
10936 rettv->v_type = VAR_STRING;
10937 rettv->vval.v_string = r;
10938}
10939
10940/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010941 * "setbufline()" function
10942 */
10943 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010944f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010945{
10946 linenr_T lnum;
10947 buf_T *buf;
10948
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010949 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010950 if (buf == NULL)
10951 rettv->vval.v_number = 1; /* FAIL */
10952 else
10953 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010954 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010955 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010956 }
10957}
10958
10959/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010960 * "setbufvar()" function
10961 */
10962 static void
10963f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10964{
10965 buf_T *buf;
10966 char_u *varname, *bufvarname;
10967 typval_T *varp;
10968 char_u nbuf[NUMBUFLEN];
10969
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010970 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010971 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010972 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10973 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010974 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010975 varp = &argvars[2];
10976
10977 if (buf != NULL && varname != NULL && varp != NULL)
10978 {
10979 if (*varname == '&')
10980 {
10981 long numval;
10982 char_u *strval;
10983 int error = FALSE;
10984 aco_save_T aco;
10985
10986 /* set curbuf to be our buf, temporarily */
10987 aucmd_prepbuf(&aco, buf);
10988
10989 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010990 numval = (long)tv_get_number_chk(varp, &error);
10991 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010992 if (!error && strval != NULL)
10993 set_option_value(varname, numval, strval, OPT_LOCAL);
10994
10995 /* reset notion of buffer */
10996 aucmd_restbuf(&aco);
10997 }
10998 else
10999 {
11000 buf_T *save_curbuf = curbuf;
11001
11002 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11003 if (bufvarname != NULL)
11004 {
11005 curbuf = buf;
11006 STRCPY(bufvarname, "b:");
11007 STRCPY(bufvarname + 2, varname);
11008 set_var(bufvarname, varp, TRUE);
11009 vim_free(bufvarname);
11010 curbuf = save_curbuf;
11011 }
11012 }
11013 }
11014}
11015
11016 static void
11017f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11018{
11019 dict_T *d;
11020 dictitem_T *di;
11021 char_u *csearch;
11022
11023 if (argvars[0].v_type != VAR_DICT)
11024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011025 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011026 return;
11027 }
11028
11029 if ((d = argvars[0].vval.v_dict) != NULL)
11030 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011031 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011032 if (csearch != NULL)
11033 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011034 if (enc_utf8)
11035 {
11036 int pcc[MAX_MCO];
11037 int c = utfc_ptr2char(csearch, pcc);
11038
11039 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11040 }
11041 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011042 set_last_csearch(PTR2CHAR(csearch),
11043 csearch, MB_PTR2LEN(csearch));
11044 }
11045
11046 di = dict_find(d, (char_u *)"forward", -1);
11047 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011048 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011049 ? FORWARD : BACKWARD);
11050
11051 di = dict_find(d, (char_u *)"until", -1);
11052 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011053 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011054 }
11055}
11056
11057/*
11058 * "setcmdpos()" function
11059 */
11060 static void
11061f_setcmdpos(typval_T *argvars, typval_T *rettv)
11062{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011063 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011064
11065 if (pos >= 0)
11066 rettv->vval.v_number = set_cmdline_pos(pos);
11067}
11068
11069/*
11070 * "setfperm({fname}, {mode})" function
11071 */
11072 static void
11073f_setfperm(typval_T *argvars, typval_T *rettv)
11074{
11075 char_u *fname;
11076 char_u modebuf[NUMBUFLEN];
11077 char_u *mode_str;
11078 int i;
11079 int mask;
11080 int mode = 0;
11081
11082 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011083 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011084 if (fname == NULL)
11085 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011086 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011087 if (mode_str == NULL)
11088 return;
11089 if (STRLEN(mode_str) != 9)
11090 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011091 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011092 return;
11093 }
11094
11095 mask = 1;
11096 for (i = 8; i >= 0; --i)
11097 {
11098 if (mode_str[i] != '-')
11099 mode |= mask;
11100 mask = mask << 1;
11101 }
11102 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11103}
11104
11105/*
11106 * "setline()" function
11107 */
11108 static void
11109f_setline(typval_T *argvars, typval_T *rettv)
11110{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011111 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011112
Bram Moolenaarca851592018-06-06 21:04:07 +020011113 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011114}
11115
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011116/*
11117 * Used by "setqflist()" and "setloclist()" functions
11118 */
11119 static void
11120set_qf_ll_list(
11121 win_T *wp UNUSED,
11122 typval_T *list_arg UNUSED,
11123 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011124 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011125 typval_T *rettv)
11126{
11127#ifdef FEAT_QUICKFIX
11128 static char *e_invact = N_("E927: Invalid action: '%s'");
11129 char_u *act;
11130 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011131 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011132#endif
11133
11134 rettv->vval.v_number = -1;
11135
11136#ifdef FEAT_QUICKFIX
11137 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011138 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011139 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011140 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011141 else
11142 {
11143 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011144 dict_T *d = NULL;
11145 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011146
11147 if (action_arg->v_type == VAR_STRING)
11148 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011149 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011150 if (act == NULL)
11151 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011152 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11153 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011154 action = *act;
11155 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011156 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011157 }
11158 else if (action_arg->v_type == VAR_UNKNOWN)
11159 action = ' ';
11160 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011161 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011162
Bram Moolenaard823fa92016-08-12 16:29:27 +020011163 if (action_arg->v_type != VAR_UNKNOWN
11164 && what_arg->v_type != VAR_UNKNOWN)
11165 {
11166 if (what_arg->v_type == VAR_DICT)
11167 d = what_arg->vval.v_dict;
11168 else
11169 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011170 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011171 valid_dict = FALSE;
11172 }
11173 }
11174
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011175 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011176 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011177 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11178 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011179 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011180 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011181 }
11182#endif
11183}
11184
11185/*
11186 * "setloclist()" function
11187 */
11188 static void
11189f_setloclist(typval_T *argvars, typval_T *rettv)
11190{
11191 win_T *win;
11192
11193 rettv->vval.v_number = -1;
11194
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011195 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011196 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011197 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011198}
11199
11200/*
11201 * "setmatches()" function
11202 */
11203 static void
11204f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11205{
11206#ifdef FEAT_SEARCH_EXTRA
11207 list_T *l;
11208 listitem_T *li;
11209 dict_T *d;
11210 list_T *s = NULL;
11211
11212 rettv->vval.v_number = -1;
11213 if (argvars[0].v_type != VAR_LIST)
11214 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011215 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011216 return;
11217 }
11218 if ((l = argvars[0].vval.v_list) != NULL)
11219 {
11220
11221 /* To some extent make sure that we are dealing with a list from
11222 * "getmatches()". */
11223 li = l->lv_first;
11224 while (li != NULL)
11225 {
11226 if (li->li_tv.v_type != VAR_DICT
11227 || (d = li->li_tv.vval.v_dict) == NULL)
11228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011229 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011230 return;
11231 }
11232 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11233 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11234 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11235 && dict_find(d, (char_u *)"priority", -1) != NULL
11236 && dict_find(d, (char_u *)"id", -1) != NULL))
11237 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011238 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011239 return;
11240 }
11241 li = li->li_next;
11242 }
11243
11244 clear_matches(curwin);
11245 li = l->lv_first;
11246 while (li != NULL)
11247 {
11248 int i = 0;
11249 char_u buf[5];
11250 dictitem_T *di;
11251 char_u *group;
11252 int priority;
11253 int id;
11254 char_u *conceal;
11255
11256 d = li->li_tv.vval.v_dict;
11257 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11258 {
11259 if (s == NULL)
11260 {
11261 s = list_alloc();
11262 if (s == NULL)
11263 return;
11264 }
11265
11266 /* match from matchaddpos() */
11267 for (i = 1; i < 9; i++)
11268 {
11269 sprintf((char *)buf, (char *)"pos%d", i);
11270 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11271 {
11272 if (di->di_tv.v_type != VAR_LIST)
11273 return;
11274
11275 list_append_tv(s, &di->di_tv);
11276 s->lv_refcount++;
11277 }
11278 else
11279 break;
11280 }
11281 }
11282
Bram Moolenaar8f667172018-12-14 15:38:31 +010011283 group = dict_get_string(d, (char_u *)"group", TRUE);
11284 priority = (int)dict_get_number(d, (char_u *)"priority");
11285 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011286 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011287 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011288 : NULL;
11289 if (i == 0)
11290 {
11291 match_add(curwin, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011292 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011293 priority, id, NULL, conceal);
11294 }
11295 else
11296 {
11297 match_add(curwin, group, NULL, priority, id, s, conceal);
11298 list_unref(s);
11299 s = NULL;
11300 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011301 vim_free(group);
11302 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011303
11304 li = li->li_next;
11305 }
11306 rettv->vval.v_number = 0;
11307 }
11308#endif
11309}
11310
11311/*
11312 * "setpos()" function
11313 */
11314 static void
11315f_setpos(typval_T *argvars, typval_T *rettv)
11316{
11317 pos_T pos;
11318 int fnum;
11319 char_u *name;
11320 colnr_T curswant = -1;
11321
11322 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011323 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011324 if (name != NULL)
11325 {
11326 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11327 {
11328 if (--pos.col < 0)
11329 pos.col = 0;
11330 if (name[0] == '.' && name[1] == NUL)
11331 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011332 /* set cursor; "fnum" is ignored */
11333 curwin->w_cursor = pos;
11334 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011335 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011336 curwin->w_curswant = curswant - 1;
11337 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011338 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011339 check_cursor();
11340 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011341 }
11342 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11343 {
11344 /* set mark */
11345 if (setmark_pos(name[1], &pos, fnum) == OK)
11346 rettv->vval.v_number = 0;
11347 }
11348 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011349 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011350 }
11351 }
11352}
11353
11354/*
11355 * "setqflist()" function
11356 */
11357 static void
11358f_setqflist(typval_T *argvars, typval_T *rettv)
11359{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011360 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011361}
11362
11363/*
11364 * "setreg()" function
11365 */
11366 static void
11367f_setreg(typval_T *argvars, typval_T *rettv)
11368{
11369 int regname;
11370 char_u *strregname;
11371 char_u *stropt;
11372 char_u *strval;
11373 int append;
11374 char_u yank_type;
11375 long block_len;
11376
11377 block_len = -1;
11378 yank_type = MAUTO;
11379 append = FALSE;
11380
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011381 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011382 rettv->vval.v_number = 1; /* FAIL is default */
11383
11384 if (strregname == NULL)
11385 return; /* type error; errmsg already given */
11386 regname = *strregname;
11387 if (regname == 0 || regname == '@')
11388 regname = '"';
11389
11390 if (argvars[2].v_type != VAR_UNKNOWN)
11391 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011392 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011393 if (stropt == NULL)
11394 return; /* type error */
11395 for (; *stropt != NUL; ++stropt)
11396 switch (*stropt)
11397 {
11398 case 'a': case 'A': /* append */
11399 append = TRUE;
11400 break;
11401 case 'v': case 'c': /* character-wise selection */
11402 yank_type = MCHAR;
11403 break;
11404 case 'V': case 'l': /* line-wise selection */
11405 yank_type = MLINE;
11406 break;
11407 case 'b': case Ctrl_V: /* block-wise selection */
11408 yank_type = MBLOCK;
11409 if (VIM_ISDIGIT(stropt[1]))
11410 {
11411 ++stropt;
11412 block_len = getdigits(&stropt) - 1;
11413 --stropt;
11414 }
11415 break;
11416 }
11417 }
11418
11419 if (argvars[1].v_type == VAR_LIST)
11420 {
11421 char_u **lstval;
11422 char_u **allocval;
11423 char_u buf[NUMBUFLEN];
11424 char_u **curval;
11425 char_u **curallocval;
11426 list_T *ll = argvars[1].vval.v_list;
11427 listitem_T *li;
11428 int len;
11429
11430 /* If the list is NULL handle like an empty list. */
11431 len = ll == NULL ? 0 : ll->lv_len;
11432
11433 /* First half: use for pointers to result lines; second half: use for
11434 * pointers to allocated copies. */
11435 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11436 if (lstval == NULL)
11437 return;
11438 curval = lstval;
11439 allocval = lstval + len + 2;
11440 curallocval = allocval;
11441
11442 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11443 li = li->li_next)
11444 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011445 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011446 if (strval == NULL)
11447 goto free_lstval;
11448 if (strval == buf)
11449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011450 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011451 * overwrite the string. */
11452 strval = vim_strsave(buf);
11453 if (strval == NULL)
11454 goto free_lstval;
11455 *curallocval++ = strval;
11456 }
11457 *curval++ = strval;
11458 }
11459 *curval++ = NULL;
11460
11461 write_reg_contents_lst(regname, lstval, -1,
11462 append, yank_type, block_len);
11463free_lstval:
11464 while (curallocval > allocval)
11465 vim_free(*--curallocval);
11466 vim_free(lstval);
11467 }
11468 else
11469 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011470 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011471 if (strval == NULL)
11472 return;
11473 write_reg_contents_ex(regname, strval, -1,
11474 append, yank_type, block_len);
11475 }
11476 rettv->vval.v_number = 0;
11477}
11478
11479/*
11480 * "settabvar()" function
11481 */
11482 static void
11483f_settabvar(typval_T *argvars, typval_T *rettv)
11484{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011485 tabpage_T *save_curtab;
11486 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011487 char_u *varname, *tabvarname;
11488 typval_T *varp;
11489
11490 rettv->vval.v_number = 0;
11491
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011492 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011493 return;
11494
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011495 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11496 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011497 varp = &argvars[2];
11498
Bram Moolenaar4033c552017-09-16 20:54:51 +020011499 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011500 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011501 save_curtab = curtab;
11502 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011503
11504 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11505 if (tabvarname != NULL)
11506 {
11507 STRCPY(tabvarname, "t:");
11508 STRCPY(tabvarname + 2, varname);
11509 set_var(tabvarname, varp, TRUE);
11510 vim_free(tabvarname);
11511 }
11512
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011513 /* Restore current tabpage */
11514 if (valid_tabpage(save_curtab))
11515 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011516 }
11517}
11518
11519/*
11520 * "settabwinvar()" function
11521 */
11522 static void
11523f_settabwinvar(typval_T *argvars, typval_T *rettv)
11524{
11525 setwinvar(argvars, rettv, 1);
11526}
11527
11528/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011529 * "settagstack()" function
11530 */
11531 static void
11532f_settagstack(typval_T *argvars, typval_T *rettv)
11533{
11534 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11535 win_T *wp;
11536 dict_T *d;
11537 int action = 'r';
11538
11539 rettv->vval.v_number = -1;
11540
11541 // first argument: window number or id
11542 wp = find_win_by_nr_or_id(&argvars[0]);
11543 if (wp == NULL)
11544 return;
11545
11546 // second argument: dict with items to set in the tag stack
11547 if (argvars[1].v_type != VAR_DICT)
11548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011549 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011550 return;
11551 }
11552 d = argvars[1].vval.v_dict;
11553 if (d == NULL)
11554 return;
11555
11556 // third argument: action - 'a' for append and 'r' for replace.
11557 // default is to replace the stack.
11558 if (argvars[2].v_type == VAR_UNKNOWN)
11559 action = 'r';
11560 else if (argvars[2].v_type == VAR_STRING)
11561 {
11562 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011563 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011564 if (actstr == NULL)
11565 return;
11566 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11567 action = *actstr;
11568 else
11569 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011570 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011571 return;
11572 }
11573 }
11574 else
11575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011576 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011577 return;
11578 }
11579
11580 if (set_tagstack(wp, d, action) == OK)
11581 rettv->vval.v_number = 0;
11582}
11583
11584/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011585 * "setwinvar()" function
11586 */
11587 static void
11588f_setwinvar(typval_T *argvars, typval_T *rettv)
11589{
11590 setwinvar(argvars, rettv, 0);
11591}
11592
11593#ifdef FEAT_CRYPT
11594/*
11595 * "sha256({string})" function
11596 */
11597 static void
11598f_sha256(typval_T *argvars, typval_T *rettv)
11599{
11600 char_u *p;
11601
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011602 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011603 rettv->vval.v_string = vim_strsave(
11604 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11605 rettv->v_type = VAR_STRING;
11606}
11607#endif /* FEAT_CRYPT */
11608
11609/*
11610 * "shellescape({string})" function
11611 */
11612 static void
11613f_shellescape(typval_T *argvars, typval_T *rettv)
11614{
Bram Moolenaar20615522017-06-05 18:46:26 +020011615 int do_special = non_zero_arg(&argvars[1]);
11616
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011617 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011618 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011619 rettv->v_type = VAR_STRING;
11620}
11621
11622/*
11623 * shiftwidth() function
11624 */
11625 static void
11626f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11627{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011628 rettv->vval.v_number = 0;
11629
11630 if (argvars[0].v_type != VAR_UNKNOWN)
11631 {
11632 long col;
11633
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011634 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011635 if (col < 0)
11636 return; // type error; errmsg already given
11637#ifdef FEAT_VARTABS
11638 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11639 return;
11640#endif
11641 }
11642
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011643 rettv->vval.v_number = get_sw_value(curbuf);
11644}
11645
Bram Moolenaar162b7142018-12-21 15:17:36 +010011646#ifdef FEAT_SIGNS
11647/*
11648 * "sign_define()" function
11649 */
11650 static void
11651f_sign_define(typval_T *argvars, typval_T *rettv)
11652{
11653 char_u *name;
11654 dict_T *dict;
11655 char_u *icon = NULL;
11656 char_u *linehl = NULL;
11657 char_u *text = NULL;
11658 char_u *texthl = NULL;
11659
11660 rettv->vval.v_number = -1;
11661
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011662 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011663 if (name == NULL)
11664 return;
11665
11666 if (argvars[1].v_type != VAR_UNKNOWN)
11667 {
11668 if (argvars[1].v_type != VAR_DICT)
11669 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011670 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011671 return;
11672 }
11673
11674 // sign attributes
11675 dict = argvars[1].vval.v_dict;
11676 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11677 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11678 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11679 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11680 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11681 text = dict_get_string(dict, (char_u *)"text", TRUE);
11682 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11683 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11684 }
11685
11686 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11687 rettv->vval.v_number = 0;
11688
11689 vim_free(icon);
11690 vim_free(linehl);
11691 vim_free(text);
11692 vim_free(texthl);
11693}
11694
11695/*
11696 * "sign_getdefined()" function
11697 */
11698 static void
11699f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11700{
11701 char_u *name = NULL;
11702
11703 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11704 return;
11705
11706 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011707 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011708
11709 sign_getlist(name, rettv->vval.v_list);
11710}
11711
11712/*
11713 * "sign_getplaced()" function
11714 */
11715 static void
11716f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11717{
11718 buf_T *buf = NULL;
11719 dict_T *dict;
11720 dictitem_T *di;
11721 linenr_T lnum = 0;
11722 int sign_id = 0;
11723 char_u *group = NULL;
11724 int notanum = FALSE;
11725
11726 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11727 return;
11728
11729 if (argvars[0].v_type != VAR_UNKNOWN)
11730 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011731 // get signs placed in the specified buffer
11732 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011733 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011734 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011735
11736 if (argvars[1].v_type != VAR_UNKNOWN)
11737 {
11738 if (argvars[1].v_type != VAR_DICT ||
11739 ((dict = argvars[1].vval.v_dict) == NULL))
11740 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011741 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011742 return;
11743 }
11744 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11745 {
11746 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011747 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011748 if (notanum)
11749 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011750 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011751 }
11752 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
11753 {
11754 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011755 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011756 if (notanum)
11757 return;
11758 }
11759 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
11760 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011761 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011762 if (group == NULL)
11763 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010011764 if (*group == '\0') // empty string means global group
11765 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011766 }
11767 }
11768 }
11769
11770 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
11771}
11772
11773/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011774 * "sign_jump()" function
11775 */
11776 static void
11777f_sign_jump(typval_T *argvars, typval_T *rettv)
11778{
11779 int sign_id;
11780 char_u *sign_group = NULL;
11781 buf_T *buf;
11782 int notanum = FALSE;
11783
11784 rettv->vval.v_number = -1;
11785
Bram Moolenaarbdace832019-03-02 10:13:42 +010011786 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011787 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
11788 if (notanum)
11789 return;
11790 if (sign_id <= 0)
11791 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011792 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011793 return;
11794 }
11795
11796 // Sign group
11797 sign_group = tv_get_string_chk(&argvars[1]);
11798 if (sign_group == NULL)
11799 return;
11800 if (sign_group[0] == '\0')
11801 sign_group = NULL; // global sign group
11802 else
11803 {
11804 sign_group = vim_strsave(sign_group);
11805 if (sign_group == NULL)
11806 return;
11807 }
11808
11809 // Buffer to place the sign
11810 buf = get_buf_arg(&argvars[2]);
11811 if (buf == NULL)
11812 goto cleanup;
11813
11814 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
11815
11816cleanup:
11817 vim_free(sign_group);
11818}
11819
11820/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010011821 * "sign_place()" function
11822 */
11823 static void
11824f_sign_place(typval_T *argvars, typval_T *rettv)
11825{
11826 int sign_id;
11827 char_u *group = NULL;
11828 char_u *sign_name;
11829 buf_T *buf;
11830 dict_T *dict;
11831 dictitem_T *di;
11832 linenr_T lnum = 0;
11833 int prio = SIGN_DEF_PRIO;
11834 int notanum = FALSE;
11835
11836 rettv->vval.v_number = -1;
11837
Bram Moolenaarbdace832019-03-02 10:13:42 +010011838 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011839 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011840 if (notanum)
11841 return;
11842 if (sign_id < 0)
11843 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011844 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011845 return;
11846 }
11847
11848 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011849 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011850 if (group == NULL)
11851 return;
11852 if (group[0] == '\0')
11853 group = NULL; // global sign group
11854 else
11855 {
11856 group = vim_strsave(group);
11857 if (group == NULL)
11858 return;
11859 }
11860
11861 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011862 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011863 if (sign_name == NULL)
11864 goto cleanup;
11865
11866 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011867 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011868 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011869 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011870
11871 if (argvars[4].v_type != VAR_UNKNOWN)
11872 {
11873 if (argvars[4].v_type != VAR_DICT ||
11874 ((dict = argvars[4].vval.v_dict) == NULL))
11875 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011876 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011877 goto cleanup;
11878 }
11879
11880 // Line number where the sign is to be placed
11881 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11882 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011883 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011884 if (notanum)
11885 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011886 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011887 }
11888 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
11889 {
11890 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011891 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011892 if (notanum)
11893 goto cleanup;
11894 }
11895 }
11896
11897 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
11898 rettv->vval.v_number = sign_id;
11899
11900cleanup:
11901 vim_free(group);
11902}
11903
11904/*
11905 * "sign_undefine()" function
11906 */
11907 static void
11908f_sign_undefine(typval_T *argvars, typval_T *rettv)
11909{
11910 char_u *name;
11911
11912 rettv->vval.v_number = -1;
11913
11914 if (argvars[0].v_type == VAR_UNKNOWN)
11915 {
11916 // Free all the signs
11917 free_signs();
11918 rettv->vval.v_number = 0;
11919 }
11920 else
11921 {
11922 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011923 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011924 if (name == NULL)
11925 return;
11926
11927 if (sign_undefine_by_name(name) == OK)
11928 rettv->vval.v_number = 0;
11929 }
11930}
11931
11932/*
11933 * "sign_unplace()" function
11934 */
11935 static void
11936f_sign_unplace(typval_T *argvars, typval_T *rettv)
11937{
11938 dict_T *dict;
11939 dictitem_T *di;
11940 int sign_id = 0;
11941 buf_T *buf = NULL;
11942 char_u *group = NULL;
11943
11944 rettv->vval.v_number = -1;
11945
11946 if (argvars[0].v_type != VAR_STRING)
11947 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011948 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011949 return;
11950 }
11951
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011952 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011953 if (group[0] == '\0')
11954 group = NULL; // global sign group
11955 else
11956 {
11957 group = vim_strsave(group);
11958 if (group == NULL)
11959 return;
11960 }
11961
11962 if (argvars[1].v_type != VAR_UNKNOWN)
11963 {
11964 if (argvars[1].v_type != VAR_DICT)
11965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011966 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011967 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011968 }
11969 dict = argvars[1].vval.v_dict;
11970
11971 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
11972 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011973 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011974 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011975 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011976 }
11977 if (dict_find(dict, (char_u *)"id", -1) != NULL)
11978 sign_id = dict_get_number(dict, (char_u *)"id");
11979 }
11980
11981 if (buf == NULL)
11982 {
11983 // Delete the sign in all the buffers
11984 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011985 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011986 rettv->vval.v_number = 0;
11987 }
11988 else
11989 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011990 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011991 rettv->vval.v_number = 0;
11992 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011993
11994cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010011995 vim_free(group);
11996}
11997#endif
11998
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011999/*
12000 * "simplify()" function
12001 */
12002 static void
12003f_simplify(typval_T *argvars, typval_T *rettv)
12004{
12005 char_u *p;
12006
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012007 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012008 rettv->vval.v_string = vim_strsave(p);
12009 simplify_filename(rettv->vval.v_string); /* simplify in place */
12010 rettv->v_type = VAR_STRING;
12011}
12012
12013#ifdef FEAT_FLOAT
12014/*
12015 * "sin()" function
12016 */
12017 static void
12018f_sin(typval_T *argvars, typval_T *rettv)
12019{
12020 float_T f = 0.0;
12021
12022 rettv->v_type = VAR_FLOAT;
12023 if (get_float_arg(argvars, &f) == OK)
12024 rettv->vval.v_float = sin(f);
12025 else
12026 rettv->vval.v_float = 0.0;
12027}
12028
12029/*
12030 * "sinh()" function
12031 */
12032 static void
12033f_sinh(typval_T *argvars, typval_T *rettv)
12034{
12035 float_T f = 0.0;
12036
12037 rettv->v_type = VAR_FLOAT;
12038 if (get_float_arg(argvars, &f) == OK)
12039 rettv->vval.v_float = sinh(f);
12040 else
12041 rettv->vval.v_float = 0.0;
12042}
12043#endif
12044
12045static int
12046#ifdef __BORLANDC__
12047 _RTLENTRYF
12048#endif
12049 item_compare(const void *s1, const void *s2);
12050static int
12051#ifdef __BORLANDC__
12052 _RTLENTRYF
12053#endif
12054 item_compare2(const void *s1, const void *s2);
12055
12056/* struct used in the array that's given to qsort() */
12057typedef struct
12058{
12059 listitem_T *item;
12060 int idx;
12061} sortItem_T;
12062
12063/* struct storing information about current sort */
12064typedef struct
12065{
12066 int item_compare_ic;
12067 int item_compare_numeric;
12068 int item_compare_numbers;
12069#ifdef FEAT_FLOAT
12070 int item_compare_float;
12071#endif
12072 char_u *item_compare_func;
12073 partial_T *item_compare_partial;
12074 dict_T *item_compare_selfdict;
12075 int item_compare_func_err;
12076 int item_compare_keep_zero;
12077} sortinfo_T;
12078static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012079#define ITEM_COMPARE_FAIL 999
12080
12081/*
12082 * Compare functions for f_sort() and f_uniq() below.
12083 */
12084 static int
12085#ifdef __BORLANDC__
12086_RTLENTRYF
12087#endif
12088item_compare(const void *s1, const void *s2)
12089{
12090 sortItem_T *si1, *si2;
12091 typval_T *tv1, *tv2;
12092 char_u *p1, *p2;
12093 char_u *tofree1 = NULL, *tofree2 = NULL;
12094 int res;
12095 char_u numbuf1[NUMBUFLEN];
12096 char_u numbuf2[NUMBUFLEN];
12097
12098 si1 = (sortItem_T *)s1;
12099 si2 = (sortItem_T *)s2;
12100 tv1 = &si1->item->li_tv;
12101 tv2 = &si2->item->li_tv;
12102
12103 if (sortinfo->item_compare_numbers)
12104 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012105 varnumber_T v1 = tv_get_number(tv1);
12106 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012107
12108 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12109 }
12110
12111#ifdef FEAT_FLOAT
12112 if (sortinfo->item_compare_float)
12113 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012114 float_T v1 = tv_get_float(tv1);
12115 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012116
12117 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12118 }
12119#endif
12120
12121 /* tv2string() puts quotes around a string and allocates memory. Don't do
12122 * that for string variables. Use a single quote when comparing with a
12123 * non-string to do what the docs promise. */
12124 if (tv1->v_type == VAR_STRING)
12125 {
12126 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12127 p1 = (char_u *)"'";
12128 else
12129 p1 = tv1->vval.v_string;
12130 }
12131 else
12132 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12133 if (tv2->v_type == VAR_STRING)
12134 {
12135 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12136 p2 = (char_u *)"'";
12137 else
12138 p2 = tv2->vval.v_string;
12139 }
12140 else
12141 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12142 if (p1 == NULL)
12143 p1 = (char_u *)"";
12144 if (p2 == NULL)
12145 p2 = (char_u *)"";
12146 if (!sortinfo->item_compare_numeric)
12147 {
12148 if (sortinfo->item_compare_ic)
12149 res = STRICMP(p1, p2);
12150 else
12151 res = STRCMP(p1, p2);
12152 }
12153 else
12154 {
12155 double n1, n2;
12156 n1 = strtod((char *)p1, (char **)&p1);
12157 n2 = strtod((char *)p2, (char **)&p2);
12158 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12159 }
12160
12161 /* When the result would be zero, compare the item indexes. Makes the
12162 * sort stable. */
12163 if (res == 0 && !sortinfo->item_compare_keep_zero)
12164 res = si1->idx > si2->idx ? 1 : -1;
12165
12166 vim_free(tofree1);
12167 vim_free(tofree2);
12168 return res;
12169}
12170
12171 static int
12172#ifdef __BORLANDC__
12173_RTLENTRYF
12174#endif
12175item_compare2(const void *s1, const void *s2)
12176{
12177 sortItem_T *si1, *si2;
12178 int res;
12179 typval_T rettv;
12180 typval_T argv[3];
12181 int dummy;
12182 char_u *func_name;
12183 partial_T *partial = sortinfo->item_compare_partial;
12184
12185 /* shortcut after failure in previous call; compare all items equal */
12186 if (sortinfo->item_compare_func_err)
12187 return 0;
12188
12189 si1 = (sortItem_T *)s1;
12190 si2 = (sortItem_T *)s2;
12191
12192 if (partial == NULL)
12193 func_name = sortinfo->item_compare_func;
12194 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012195 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012196
12197 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12198 * in the copy without changing the original list items. */
12199 copy_tv(&si1->item->li_tv, &argv[0]);
12200 copy_tv(&si2->item->li_tv, &argv[1]);
12201
12202 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12203 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012204 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012205 partial, sortinfo->item_compare_selfdict);
12206 clear_tv(&argv[0]);
12207 clear_tv(&argv[1]);
12208
12209 if (res == FAIL)
12210 res = ITEM_COMPARE_FAIL;
12211 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012212 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012213 if (sortinfo->item_compare_func_err)
12214 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12215 clear_tv(&rettv);
12216
12217 /* When the result would be zero, compare the pointers themselves. Makes
12218 * the sort stable. */
12219 if (res == 0 && !sortinfo->item_compare_keep_zero)
12220 res = si1->idx > si2->idx ? 1 : -1;
12221
12222 return res;
12223}
12224
12225/*
12226 * "sort({list})" function
12227 */
12228 static void
12229do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12230{
12231 list_T *l;
12232 listitem_T *li;
12233 sortItem_T *ptrs;
12234 sortinfo_T *old_sortinfo;
12235 sortinfo_T info;
12236 long len;
12237 long i;
12238
12239 /* Pointer to current info struct used in compare function. Save and
12240 * restore the current one for nested calls. */
12241 old_sortinfo = sortinfo;
12242 sortinfo = &info;
12243
12244 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012245 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012246 else
12247 {
12248 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012249 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012250 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12251 TRUE))
12252 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012253 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012254
12255 len = list_len(l);
12256 if (len <= 1)
12257 goto theend; /* short list sorts pretty quickly */
12258
12259 info.item_compare_ic = FALSE;
12260 info.item_compare_numeric = FALSE;
12261 info.item_compare_numbers = FALSE;
12262#ifdef FEAT_FLOAT
12263 info.item_compare_float = FALSE;
12264#endif
12265 info.item_compare_func = NULL;
12266 info.item_compare_partial = NULL;
12267 info.item_compare_selfdict = NULL;
12268 if (argvars[1].v_type != VAR_UNKNOWN)
12269 {
12270 /* optional second argument: {func} */
12271 if (argvars[1].v_type == VAR_FUNC)
12272 info.item_compare_func = argvars[1].vval.v_string;
12273 else if (argvars[1].v_type == VAR_PARTIAL)
12274 info.item_compare_partial = argvars[1].vval.v_partial;
12275 else
12276 {
12277 int error = FALSE;
12278
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012279 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012280 if (error)
12281 goto theend; /* type error; errmsg already given */
12282 if (i == 1)
12283 info.item_compare_ic = TRUE;
12284 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012285 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012286 else if (i != 0)
12287 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012288 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012289 goto theend;
12290 }
12291 if (info.item_compare_func != NULL)
12292 {
12293 if (*info.item_compare_func == NUL)
12294 {
12295 /* empty string means default sort */
12296 info.item_compare_func = NULL;
12297 }
12298 else if (STRCMP(info.item_compare_func, "n") == 0)
12299 {
12300 info.item_compare_func = NULL;
12301 info.item_compare_numeric = TRUE;
12302 }
12303 else if (STRCMP(info.item_compare_func, "N") == 0)
12304 {
12305 info.item_compare_func = NULL;
12306 info.item_compare_numbers = TRUE;
12307 }
12308#ifdef FEAT_FLOAT
12309 else if (STRCMP(info.item_compare_func, "f") == 0)
12310 {
12311 info.item_compare_func = NULL;
12312 info.item_compare_float = TRUE;
12313 }
12314#endif
12315 else if (STRCMP(info.item_compare_func, "i") == 0)
12316 {
12317 info.item_compare_func = NULL;
12318 info.item_compare_ic = TRUE;
12319 }
12320 }
12321 }
12322
12323 if (argvars[2].v_type != VAR_UNKNOWN)
12324 {
12325 /* optional third argument: {dict} */
12326 if (argvars[2].v_type != VAR_DICT)
12327 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012328 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012329 goto theend;
12330 }
12331 info.item_compare_selfdict = argvars[2].vval.v_dict;
12332 }
12333 }
12334
12335 /* Make an array with each entry pointing to an item in the List. */
12336 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12337 if (ptrs == NULL)
12338 goto theend;
12339
12340 i = 0;
12341 if (sort)
12342 {
12343 /* sort(): ptrs will be the list to sort */
12344 for (li = l->lv_first; li != NULL; li = li->li_next)
12345 {
12346 ptrs[i].item = li;
12347 ptrs[i].idx = i;
12348 ++i;
12349 }
12350
12351 info.item_compare_func_err = FALSE;
12352 info.item_compare_keep_zero = FALSE;
12353 /* test the compare function */
12354 if ((info.item_compare_func != NULL
12355 || info.item_compare_partial != NULL)
12356 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12357 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012358 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012359 else
12360 {
12361 /* Sort the array with item pointers. */
12362 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12363 info.item_compare_func == NULL
12364 && info.item_compare_partial == NULL
12365 ? item_compare : item_compare2);
12366
12367 if (!info.item_compare_func_err)
12368 {
12369 /* Clear the List and append the items in sorted order. */
12370 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12371 l->lv_len = 0;
12372 for (i = 0; i < len; ++i)
12373 list_append(l, ptrs[i].item);
12374 }
12375 }
12376 }
12377 else
12378 {
12379 int (*item_compare_func_ptr)(const void *, const void *);
12380
12381 /* f_uniq(): ptrs will be a stack of items to remove */
12382 info.item_compare_func_err = FALSE;
12383 info.item_compare_keep_zero = TRUE;
12384 item_compare_func_ptr = info.item_compare_func != NULL
12385 || info.item_compare_partial != NULL
12386 ? item_compare2 : item_compare;
12387
12388 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12389 li = li->li_next)
12390 {
12391 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12392 == 0)
12393 ptrs[i++].item = li;
12394 if (info.item_compare_func_err)
12395 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012396 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012397 break;
12398 }
12399 }
12400
12401 if (!info.item_compare_func_err)
12402 {
12403 while (--i >= 0)
12404 {
12405 li = ptrs[i].item->li_next;
12406 ptrs[i].item->li_next = li->li_next;
12407 if (li->li_next != NULL)
12408 li->li_next->li_prev = ptrs[i].item;
12409 else
12410 l->lv_last = ptrs[i].item;
12411 list_fix_watch(l, li);
12412 listitem_free(li);
12413 l->lv_len--;
12414 }
12415 }
12416 }
12417
12418 vim_free(ptrs);
12419 }
12420theend:
12421 sortinfo = old_sortinfo;
12422}
12423
12424/*
12425 * "sort({list})" function
12426 */
12427 static void
12428f_sort(typval_T *argvars, typval_T *rettv)
12429{
12430 do_sort_uniq(argvars, rettv, TRUE);
12431}
12432
12433/*
12434 * "uniq({list})" function
12435 */
12436 static void
12437f_uniq(typval_T *argvars, typval_T *rettv)
12438{
12439 do_sort_uniq(argvars, rettv, FALSE);
12440}
12441
12442/*
12443 * "soundfold({word})" function
12444 */
12445 static void
12446f_soundfold(typval_T *argvars, typval_T *rettv)
12447{
12448 char_u *s;
12449
12450 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012451 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012452#ifdef FEAT_SPELL
12453 rettv->vval.v_string = eval_soundfold(s);
12454#else
12455 rettv->vval.v_string = vim_strsave(s);
12456#endif
12457}
12458
12459/*
12460 * "spellbadword()" function
12461 */
12462 static void
12463f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12464{
12465 char_u *word = (char_u *)"";
12466 hlf_T attr = HLF_COUNT;
12467 int len = 0;
12468
12469 if (rettv_list_alloc(rettv) == FAIL)
12470 return;
12471
12472#ifdef FEAT_SPELL
12473 if (argvars[0].v_type == VAR_UNKNOWN)
12474 {
12475 /* Find the start and length of the badly spelled word. */
12476 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12477 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012478 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012479 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012480 curwin->w_set_curswant = TRUE;
12481 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012482 }
12483 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12484 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012485 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012486 int capcol = -1;
12487
12488 if (str != NULL)
12489 {
12490 /* Check the argument for spelling. */
12491 while (*str != NUL)
12492 {
12493 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12494 if (attr != HLF_COUNT)
12495 {
12496 word = str;
12497 break;
12498 }
12499 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012500 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012501 }
12502 }
12503 }
12504#endif
12505
12506 list_append_string(rettv->vval.v_list, word, len);
12507 list_append_string(rettv->vval.v_list, (char_u *)(
12508 attr == HLF_SPB ? "bad" :
12509 attr == HLF_SPR ? "rare" :
12510 attr == HLF_SPL ? "local" :
12511 attr == HLF_SPC ? "caps" :
12512 ""), -1);
12513}
12514
12515/*
12516 * "spellsuggest()" function
12517 */
12518 static void
12519f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12520{
12521#ifdef FEAT_SPELL
12522 char_u *str;
12523 int typeerr = FALSE;
12524 int maxcount;
12525 garray_T ga;
12526 int i;
12527 listitem_T *li;
12528 int need_capital = FALSE;
12529#endif
12530
12531 if (rettv_list_alloc(rettv) == FAIL)
12532 return;
12533
12534#ifdef FEAT_SPELL
12535 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12536 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012537 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012538 if (argvars[1].v_type != VAR_UNKNOWN)
12539 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012540 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012541 if (maxcount <= 0)
12542 return;
12543 if (argvars[2].v_type != VAR_UNKNOWN)
12544 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012545 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012546 if (typeerr)
12547 return;
12548 }
12549 }
12550 else
12551 maxcount = 25;
12552
12553 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12554
12555 for (i = 0; i < ga.ga_len; ++i)
12556 {
12557 str = ((char_u **)ga.ga_data)[i];
12558
12559 li = listitem_alloc();
12560 if (li == NULL)
12561 vim_free(str);
12562 else
12563 {
12564 li->li_tv.v_type = VAR_STRING;
12565 li->li_tv.v_lock = 0;
12566 li->li_tv.vval.v_string = str;
12567 list_append(rettv->vval.v_list, li);
12568 }
12569 }
12570 ga_clear(&ga);
12571 }
12572#endif
12573}
12574
12575 static void
12576f_split(typval_T *argvars, typval_T *rettv)
12577{
12578 char_u *str;
12579 char_u *end;
12580 char_u *pat = NULL;
12581 regmatch_T regmatch;
12582 char_u patbuf[NUMBUFLEN];
12583 char_u *save_cpo;
12584 int match;
12585 colnr_T col = 0;
12586 int keepempty = FALSE;
12587 int typeerr = FALSE;
12588
12589 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12590 save_cpo = p_cpo;
12591 p_cpo = (char_u *)"";
12592
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012593 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012594 if (argvars[1].v_type != VAR_UNKNOWN)
12595 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012596 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012597 if (pat == NULL)
12598 typeerr = TRUE;
12599 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012600 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012601 }
12602 if (pat == NULL || *pat == NUL)
12603 pat = (char_u *)"[\\x01- ]\\+";
12604
12605 if (rettv_list_alloc(rettv) == FAIL)
12606 return;
12607 if (typeerr)
12608 return;
12609
12610 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12611 if (regmatch.regprog != NULL)
12612 {
12613 regmatch.rm_ic = FALSE;
12614 while (*str != NUL || keepempty)
12615 {
12616 if (*str == NUL)
12617 match = FALSE; /* empty item at the end */
12618 else
12619 match = vim_regexec_nl(&regmatch, str, col);
12620 if (match)
12621 end = regmatch.startp[0];
12622 else
12623 end = str + STRLEN(str);
12624 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12625 && *str != NUL && match && end < regmatch.endp[0]))
12626 {
12627 if (list_append_string(rettv->vval.v_list, str,
12628 (int)(end - str)) == FAIL)
12629 break;
12630 }
12631 if (!match)
12632 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012633 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012634 if (regmatch.endp[0] > str)
12635 col = 0;
12636 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012637 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012638 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012639 str = regmatch.endp[0];
12640 }
12641
12642 vim_regfree(regmatch.regprog);
12643 }
12644
12645 p_cpo = save_cpo;
12646}
12647
12648#ifdef FEAT_FLOAT
12649/*
12650 * "sqrt()" function
12651 */
12652 static void
12653f_sqrt(typval_T *argvars, typval_T *rettv)
12654{
12655 float_T f = 0.0;
12656
12657 rettv->v_type = VAR_FLOAT;
12658 if (get_float_arg(argvars, &f) == OK)
12659 rettv->vval.v_float = sqrt(f);
12660 else
12661 rettv->vval.v_float = 0.0;
12662}
12663
12664/*
12665 * "str2float()" function
12666 */
12667 static void
12668f_str2float(typval_T *argvars, typval_T *rettv)
12669{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012670 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012671 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012672
Bram Moolenaar08243d22017-01-10 16:12:29 +010012673 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012674 p = skipwhite(p + 1);
12675 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012676 if (isneg)
12677 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012678 rettv->v_type = VAR_FLOAT;
12679}
12680#endif
12681
12682/*
12683 * "str2nr()" function
12684 */
12685 static void
12686f_str2nr(typval_T *argvars, typval_T *rettv)
12687{
12688 int base = 10;
12689 char_u *p;
12690 varnumber_T n;
12691 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012692 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012693
12694 if (argvars[1].v_type != VAR_UNKNOWN)
12695 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012696 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012697 if (base != 2 && base != 8 && base != 10 && base != 16)
12698 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012699 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012700 return;
12701 }
12702 }
12703
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012704 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012705 isneg = (*p == '-');
12706 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012707 p = skipwhite(p + 1);
12708 switch (base)
12709 {
12710 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12711 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12712 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12713 default: what = 0;
12714 }
12715 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012716 if (isneg)
12717 rettv->vval.v_number = -n;
12718 else
12719 rettv->vval.v_number = n;
12720
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012721}
12722
12723#ifdef HAVE_STRFTIME
12724/*
12725 * "strftime({format}[, {time}])" function
12726 */
12727 static void
12728f_strftime(typval_T *argvars, typval_T *rettv)
12729{
12730 char_u result_buf[256];
12731 struct tm *curtime;
12732 time_t seconds;
12733 char_u *p;
12734
12735 rettv->v_type = VAR_STRING;
12736
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012737 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012738 if (argvars[1].v_type == VAR_UNKNOWN)
12739 seconds = time(NULL);
12740 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012741 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012742 curtime = localtime(&seconds);
12743 /* MSVC returns NULL for an invalid value of seconds. */
12744 if (curtime == NULL)
12745 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12746 else
12747 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012748 vimconv_T conv;
12749 char_u *enc;
12750
12751 conv.vc_type = CONV_NONE;
12752 enc = enc_locale();
12753 convert_setup(&conv, p_enc, enc);
12754 if (conv.vc_type != CONV_NONE)
12755 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012756 if (p != NULL)
12757 (void)strftime((char *)result_buf, sizeof(result_buf),
12758 (char *)p, curtime);
12759 else
12760 result_buf[0] = NUL;
12761
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012762 if (conv.vc_type != CONV_NONE)
12763 vim_free(p);
12764 convert_setup(&conv, enc, p_enc);
12765 if (conv.vc_type != CONV_NONE)
12766 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12767 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012768 rettv->vval.v_string = vim_strsave(result_buf);
12769
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012770 /* Release conversion descriptors */
12771 convert_setup(&conv, NULL, NULL);
12772 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012773 }
12774}
12775#endif
12776
12777/*
12778 * "strgetchar()" function
12779 */
12780 static void
12781f_strgetchar(typval_T *argvars, typval_T *rettv)
12782{
12783 char_u *str;
12784 int len;
12785 int error = FALSE;
12786 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012787 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012788
12789 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012790 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012791 if (str == NULL)
12792 return;
12793 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012794 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012795 if (error)
12796 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012797
Bram Moolenaar13505972019-01-24 15:04:48 +010012798 while (charidx >= 0 && byteidx < len)
12799 {
12800 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012801 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012802 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12803 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012804 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012805 --charidx;
12806 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012807 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012808}
12809
12810/*
12811 * "stridx()" function
12812 */
12813 static void
12814f_stridx(typval_T *argvars, typval_T *rettv)
12815{
12816 char_u buf[NUMBUFLEN];
12817 char_u *needle;
12818 char_u *haystack;
12819 char_u *save_haystack;
12820 char_u *pos;
12821 int start_idx;
12822
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012823 needle = tv_get_string_chk(&argvars[1]);
12824 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012825 rettv->vval.v_number = -1;
12826 if (needle == NULL || haystack == NULL)
12827 return; /* type error; errmsg already given */
12828
12829 if (argvars[2].v_type != VAR_UNKNOWN)
12830 {
12831 int error = FALSE;
12832
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012833 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012834 if (error || start_idx >= (int)STRLEN(haystack))
12835 return;
12836 if (start_idx >= 0)
12837 haystack += start_idx;
12838 }
12839
12840 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12841 if (pos != NULL)
12842 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12843}
12844
12845/*
12846 * "string()" function
12847 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012848 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012849f_string(typval_T *argvars, typval_T *rettv)
12850{
12851 char_u *tofree;
12852 char_u numbuf[NUMBUFLEN];
12853
12854 rettv->v_type = VAR_STRING;
12855 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12856 get_copyID());
12857 /* Make a copy if we have a value but it's not in allocated memory. */
12858 if (rettv->vval.v_string != NULL && tofree == NULL)
12859 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12860}
12861
12862/*
12863 * "strlen()" function
12864 */
12865 static void
12866f_strlen(typval_T *argvars, typval_T *rettv)
12867{
12868 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012869 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012870}
12871
12872/*
12873 * "strchars()" function
12874 */
12875 static void
12876f_strchars(typval_T *argvars, typval_T *rettv)
12877{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012878 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012879 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012880 varnumber_T len = 0;
12881 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012882
12883 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012884 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012885 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012886 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012887 else
12888 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012889 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12890 while (*s != NUL)
12891 {
12892 func_mb_ptr2char_adv(&s);
12893 ++len;
12894 }
12895 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012896 }
12897}
12898
12899/*
12900 * "strdisplaywidth()" function
12901 */
12902 static void
12903f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12904{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012905 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012906 int col = 0;
12907
12908 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012909 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012910
12911 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12912}
12913
12914/*
12915 * "strwidth()" function
12916 */
12917 static void
12918f_strwidth(typval_T *argvars, typval_T *rettv)
12919{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012920 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012921
Bram Moolenaar13505972019-01-24 15:04:48 +010012922 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012923}
12924
12925/*
12926 * "strcharpart()" function
12927 */
12928 static void
12929f_strcharpart(typval_T *argvars, typval_T *rettv)
12930{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012931 char_u *p;
12932 int nchar;
12933 int nbyte = 0;
12934 int charlen;
12935 int len = 0;
12936 int slen;
12937 int error = FALSE;
12938
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012939 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012940 slen = (int)STRLEN(p);
12941
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012942 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012943 if (!error)
12944 {
12945 if (nchar > 0)
12946 while (nchar > 0 && nbyte < slen)
12947 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012948 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012949 --nchar;
12950 }
12951 else
12952 nbyte = nchar;
12953 if (argvars[2].v_type != VAR_UNKNOWN)
12954 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012955 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012956 while (charlen > 0 && nbyte + len < slen)
12957 {
12958 int off = nbyte + len;
12959
12960 if (off < 0)
12961 len += 1;
12962 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012963 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012964 --charlen;
12965 }
12966 }
12967 else
12968 len = slen - nbyte; /* default: all bytes that are available. */
12969 }
12970
12971 /*
12972 * Only return the overlap between the specified part and the actual
12973 * string.
12974 */
12975 if (nbyte < 0)
12976 {
12977 len += nbyte;
12978 nbyte = 0;
12979 }
12980 else if (nbyte > slen)
12981 nbyte = slen;
12982 if (len < 0)
12983 len = 0;
12984 else if (nbyte + len > slen)
12985 len = slen - nbyte;
12986
12987 rettv->v_type = VAR_STRING;
12988 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012989}
12990
12991/*
12992 * "strpart()" function
12993 */
12994 static void
12995f_strpart(typval_T *argvars, typval_T *rettv)
12996{
12997 char_u *p;
12998 int n;
12999 int len;
13000 int slen;
13001 int error = FALSE;
13002
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013003 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013004 slen = (int)STRLEN(p);
13005
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013006 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013007 if (error)
13008 len = 0;
13009 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013010 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013011 else
13012 len = slen - n; /* default len: all bytes that are available. */
13013
13014 /*
13015 * Only return the overlap between the specified part and the actual
13016 * string.
13017 */
13018 if (n < 0)
13019 {
13020 len += n;
13021 n = 0;
13022 }
13023 else if (n > slen)
13024 n = slen;
13025 if (len < 0)
13026 len = 0;
13027 else if (n + len > slen)
13028 len = slen - n;
13029
13030 rettv->v_type = VAR_STRING;
13031 rettv->vval.v_string = vim_strnsave(p + n, len);
13032}
13033
13034/*
13035 * "strridx()" function
13036 */
13037 static void
13038f_strridx(typval_T *argvars, typval_T *rettv)
13039{
13040 char_u buf[NUMBUFLEN];
13041 char_u *needle;
13042 char_u *haystack;
13043 char_u *rest;
13044 char_u *lastmatch = NULL;
13045 int haystack_len, end_idx;
13046
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013047 needle = tv_get_string_chk(&argvars[1]);
13048 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013049
13050 rettv->vval.v_number = -1;
13051 if (needle == NULL || haystack == NULL)
13052 return; /* type error; errmsg already given */
13053
13054 haystack_len = (int)STRLEN(haystack);
13055 if (argvars[2].v_type != VAR_UNKNOWN)
13056 {
13057 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013058 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013059 if (end_idx < 0)
13060 return; /* can never find a match */
13061 }
13062 else
13063 end_idx = haystack_len;
13064
13065 if (*needle == NUL)
13066 {
13067 /* Empty string matches past the end. */
13068 lastmatch = haystack + end_idx;
13069 }
13070 else
13071 {
13072 for (rest = haystack; *rest != '\0'; ++rest)
13073 {
13074 rest = (char_u *)strstr((char *)rest, (char *)needle);
13075 if (rest == NULL || rest > haystack + end_idx)
13076 break;
13077 lastmatch = rest;
13078 }
13079 }
13080
13081 if (lastmatch == NULL)
13082 rettv->vval.v_number = -1;
13083 else
13084 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13085}
13086
13087/*
13088 * "strtrans()" function
13089 */
13090 static void
13091f_strtrans(typval_T *argvars, typval_T *rettv)
13092{
13093 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013094 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013095}
13096
13097/*
13098 * "submatch()" function
13099 */
13100 static void
13101f_submatch(typval_T *argvars, typval_T *rettv)
13102{
13103 int error = FALSE;
13104 int no;
13105 int retList = 0;
13106
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013107 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013108 if (error)
13109 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013110 if (no < 0 || no >= NSUBEXP)
13111 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013112 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013113 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013114 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013115 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013116 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013117 if (error)
13118 return;
13119
13120 if (retList == 0)
13121 {
13122 rettv->v_type = VAR_STRING;
13123 rettv->vval.v_string = reg_submatch(no);
13124 }
13125 else
13126 {
13127 rettv->v_type = VAR_LIST;
13128 rettv->vval.v_list = reg_submatch_list(no);
13129 }
13130}
13131
13132/*
13133 * "substitute()" function
13134 */
13135 static void
13136f_substitute(typval_T *argvars, typval_T *rettv)
13137{
13138 char_u patbuf[NUMBUFLEN];
13139 char_u subbuf[NUMBUFLEN];
13140 char_u flagsbuf[NUMBUFLEN];
13141
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013142 char_u *str = tv_get_string_chk(&argvars[0]);
13143 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013144 char_u *sub = NULL;
13145 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013146 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013147
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013148 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13149 expr = &argvars[2];
13150 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013151 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013152
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013153 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013154 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13155 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013156 rettv->vval.v_string = NULL;
13157 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013158 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013159}
13160
13161/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013162 * "swapinfo(swap_filename)" function
13163 */
13164 static void
13165f_swapinfo(typval_T *argvars, typval_T *rettv)
13166{
13167 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013168 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013169}
13170
13171/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013172 * "swapname(expr)" function
13173 */
13174 static void
13175f_swapname(typval_T *argvars, typval_T *rettv)
13176{
13177 buf_T *buf;
13178
13179 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013180 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013181 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13182 || buf->b_ml.ml_mfp->mf_fname == NULL)
13183 rettv->vval.v_string = NULL;
13184 else
13185 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13186}
13187
13188/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013189 * "synID(lnum, col, trans)" function
13190 */
13191 static void
13192f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13193{
13194 int id = 0;
13195#ifdef FEAT_SYN_HL
13196 linenr_T lnum;
13197 colnr_T col;
13198 int trans;
13199 int transerr = FALSE;
13200
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013201 lnum = tv_get_lnum(argvars); /* -1 on type error */
13202 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13203 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013204
13205 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13206 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13207 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13208#endif
13209
13210 rettv->vval.v_number = id;
13211}
13212
13213/*
13214 * "synIDattr(id, what [, mode])" function
13215 */
13216 static void
13217f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13218{
13219 char_u *p = NULL;
13220#ifdef FEAT_SYN_HL
13221 int id;
13222 char_u *what;
13223 char_u *mode;
13224 char_u modebuf[NUMBUFLEN];
13225 int modec;
13226
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013227 id = (int)tv_get_number(&argvars[0]);
13228 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013229 if (argvars[2].v_type != VAR_UNKNOWN)
13230 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013231 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013232 modec = TOLOWER_ASC(mode[0]);
13233 if (modec != 't' && modec != 'c' && modec != 'g')
13234 modec = 0; /* replace invalid with current */
13235 }
13236 else
13237 {
13238#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13239 if (USE_24BIT)
13240 modec = 'g';
13241 else
13242#endif
13243 if (t_colors > 1)
13244 modec = 'c';
13245 else
13246 modec = 't';
13247 }
13248
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013249 switch (TOLOWER_ASC(what[0]))
13250 {
13251 case 'b':
13252 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13253 p = highlight_color(id, what, modec);
13254 else /* bold */
13255 p = highlight_has_attr(id, HL_BOLD, modec);
13256 break;
13257
13258 case 'f': /* fg[#] or font */
13259 p = highlight_color(id, what, modec);
13260 break;
13261
13262 case 'i':
13263 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13264 p = highlight_has_attr(id, HL_INVERSE, modec);
13265 else /* italic */
13266 p = highlight_has_attr(id, HL_ITALIC, modec);
13267 break;
13268
13269 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013270 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013271 break;
13272
13273 case 'r': /* reverse */
13274 p = highlight_has_attr(id, HL_INVERSE, modec);
13275 break;
13276
13277 case 's':
13278 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13279 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013280 /* strikeout */
13281 else if (TOLOWER_ASC(what[1]) == 't' &&
13282 TOLOWER_ASC(what[2]) == 'r')
13283 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013284 else /* standout */
13285 p = highlight_has_attr(id, HL_STANDOUT, modec);
13286 break;
13287
13288 case 'u':
13289 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13290 /* underline */
13291 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13292 else
13293 /* undercurl */
13294 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13295 break;
13296 }
13297
13298 if (p != NULL)
13299 p = vim_strsave(p);
13300#endif
13301 rettv->v_type = VAR_STRING;
13302 rettv->vval.v_string = p;
13303}
13304
13305/*
13306 * "synIDtrans(id)" function
13307 */
13308 static void
13309f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13310{
13311 int id;
13312
13313#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013314 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013315
13316 if (id > 0)
13317 id = syn_get_final_id(id);
13318 else
13319#endif
13320 id = 0;
13321
13322 rettv->vval.v_number = id;
13323}
13324
13325/*
13326 * "synconcealed(lnum, col)" function
13327 */
13328 static void
13329f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13330{
13331#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13332 linenr_T lnum;
13333 colnr_T col;
13334 int syntax_flags = 0;
13335 int cchar;
13336 int matchid = 0;
13337 char_u str[NUMBUFLEN];
13338#endif
13339
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013340 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013341
13342#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013343 lnum = tv_get_lnum(argvars); /* -1 on type error */
13344 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013345
13346 vim_memset(str, NUL, sizeof(str));
13347
13348 if (rettv_list_alloc(rettv) != FAIL)
13349 {
13350 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13351 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13352 && curwin->w_p_cole > 0)
13353 {
13354 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13355 syntax_flags = get_syntax_info(&matchid);
13356
13357 /* get the conceal character */
13358 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13359 {
13360 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013361 if (cchar == NUL && curwin->w_p_cole == 1)
13362 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013363 if (cchar != NUL)
13364 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013365 if (has_mbyte)
13366 (*mb_char2bytes)(cchar, str);
13367 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013368 str[0] = cchar;
13369 }
13370 }
13371 }
13372
13373 list_append_number(rettv->vval.v_list,
13374 (syntax_flags & HL_CONCEAL) != 0);
13375 /* -1 to auto-determine strlen */
13376 list_append_string(rettv->vval.v_list, str, -1);
13377 list_append_number(rettv->vval.v_list, matchid);
13378 }
13379#endif
13380}
13381
13382/*
13383 * "synstack(lnum, col)" function
13384 */
13385 static void
13386f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13387{
13388#ifdef FEAT_SYN_HL
13389 linenr_T lnum;
13390 colnr_T col;
13391 int i;
13392 int id;
13393#endif
13394
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013395 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013396
13397#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013398 lnum = tv_get_lnum(argvars); /* -1 on type error */
13399 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013400
13401 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13402 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13403 && rettv_list_alloc(rettv) != FAIL)
13404 {
13405 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13406 for (i = 0; ; ++i)
13407 {
13408 id = syn_get_stack_item(i);
13409 if (id < 0)
13410 break;
13411 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13412 break;
13413 }
13414 }
13415#endif
13416}
13417
13418 static void
13419get_cmd_output_as_rettv(
13420 typval_T *argvars,
13421 typval_T *rettv,
13422 int retlist)
13423{
13424 char_u *res = NULL;
13425 char_u *p;
13426 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013427 int err = FALSE;
13428 FILE *fd;
13429 list_T *list = NULL;
13430 int flags = SHELL_SILENT;
13431
13432 rettv->v_type = VAR_STRING;
13433 rettv->vval.v_string = NULL;
13434 if (check_restricted() || check_secure())
13435 goto errret;
13436
13437 if (argvars[1].v_type != VAR_UNKNOWN)
13438 {
13439 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013440 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013441 * command.
13442 */
13443 if ((infile = vim_tempname('i', TRUE)) == NULL)
13444 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013445 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013446 goto errret;
13447 }
13448
13449 fd = mch_fopen((char *)infile, WRITEBIN);
13450 if (fd == NULL)
13451 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013452 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013453 goto errret;
13454 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013455 if (argvars[1].v_type == VAR_NUMBER)
13456 {
13457 linenr_T lnum;
13458 buf_T *buf;
13459
13460 buf = buflist_findnr(argvars[1].vval.v_number);
13461 if (buf == NULL)
13462 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013463 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013464 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013465 goto errret;
13466 }
13467
13468 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13469 {
13470 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13471 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13472 {
13473 err = TRUE;
13474 break;
13475 }
13476 if (putc(NL, fd) == EOF)
13477 {
13478 err = TRUE;
13479 break;
13480 }
13481 }
13482 }
13483 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013484 {
13485 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13486 err = TRUE;
13487 }
13488 else
13489 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013490 size_t len;
13491 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013492
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013493 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013494 if (p == NULL)
13495 {
13496 fclose(fd);
13497 goto errret; /* type error; errmsg already given */
13498 }
13499 len = STRLEN(p);
13500 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13501 err = TRUE;
13502 }
13503 if (fclose(fd) != 0)
13504 err = TRUE;
13505 if (err)
13506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013507 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013508 goto errret;
13509 }
13510 }
13511
13512 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13513 * echoes typeahead, that messes up the display. */
13514 if (!msg_silent)
13515 flags += SHELL_COOKED;
13516
13517 if (retlist)
13518 {
13519 int len;
13520 listitem_T *li;
13521 char_u *s = NULL;
13522 char_u *start;
13523 char_u *end;
13524 int i;
13525
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013526 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013527 if (res == NULL)
13528 goto errret;
13529
13530 list = list_alloc();
13531 if (list == NULL)
13532 goto errret;
13533
13534 for (i = 0; i < len; ++i)
13535 {
13536 start = res + i;
13537 while (i < len && res[i] != NL)
13538 ++i;
13539 end = res + i;
13540
13541 s = alloc((unsigned)(end - start + 1));
13542 if (s == NULL)
13543 goto errret;
13544
13545 for (p = s; start < end; ++p, ++start)
13546 *p = *start == NUL ? NL : *start;
13547 *p = NUL;
13548
13549 li = listitem_alloc();
13550 if (li == NULL)
13551 {
13552 vim_free(s);
13553 goto errret;
13554 }
13555 li->li_tv.v_type = VAR_STRING;
13556 li->li_tv.v_lock = 0;
13557 li->li_tv.vval.v_string = s;
13558 list_append(list, li);
13559 }
13560
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013561 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013562 list = NULL;
13563 }
13564 else
13565 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013566 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013567#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013568 /* translate <CR><NL> into <NL> */
13569 if (res != NULL)
13570 {
13571 char_u *s, *d;
13572
13573 d = res;
13574 for (s = res; *s; ++s)
13575 {
13576 if (s[0] == CAR && s[1] == NL)
13577 ++s;
13578 *d++ = *s;
13579 }
13580 *d = NUL;
13581 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013582#endif
13583 rettv->vval.v_string = res;
13584 res = NULL;
13585 }
13586
13587errret:
13588 if (infile != NULL)
13589 {
13590 mch_remove(infile);
13591 vim_free(infile);
13592 }
13593 if (res != NULL)
13594 vim_free(res);
13595 if (list != NULL)
13596 list_free(list);
13597}
13598
13599/*
13600 * "system()" function
13601 */
13602 static void
13603f_system(typval_T *argvars, typval_T *rettv)
13604{
13605 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13606}
13607
13608/*
13609 * "systemlist()" function
13610 */
13611 static void
13612f_systemlist(typval_T *argvars, typval_T *rettv)
13613{
13614 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13615}
13616
13617/*
13618 * "tabpagebuflist()" function
13619 */
13620 static void
13621f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13622{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013623 tabpage_T *tp;
13624 win_T *wp = NULL;
13625
13626 if (argvars[0].v_type == VAR_UNKNOWN)
13627 wp = firstwin;
13628 else
13629 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013630 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013631 if (tp != NULL)
13632 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13633 }
13634 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13635 {
13636 for (; wp != NULL; wp = wp->w_next)
13637 if (list_append_number(rettv->vval.v_list,
13638 wp->w_buffer->b_fnum) == FAIL)
13639 break;
13640 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013641}
13642
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013643/*
13644 * "tabpagenr()" function
13645 */
13646 static void
13647f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13648{
13649 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013650 char_u *arg;
13651
13652 if (argvars[0].v_type != VAR_UNKNOWN)
13653 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013654 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013655 nr = 0;
13656 if (arg != NULL)
13657 {
13658 if (STRCMP(arg, "$") == 0)
13659 nr = tabpage_index(NULL) - 1;
13660 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013661 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013662 }
13663 }
13664 else
13665 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013666 rettv->vval.v_number = nr;
13667}
13668
13669
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013670/*
13671 * Common code for tabpagewinnr() and winnr().
13672 */
13673 static int
13674get_winnr(tabpage_T *tp, typval_T *argvar)
13675{
13676 win_T *twin;
13677 int nr = 1;
13678 win_T *wp;
13679 char_u *arg;
13680
13681 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13682 if (argvar->v_type != VAR_UNKNOWN)
13683 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013684 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013685 if (arg == NULL)
13686 nr = 0; /* type error; errmsg already given */
13687 else if (STRCMP(arg, "$") == 0)
13688 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13689 else if (STRCMP(arg, "#") == 0)
13690 {
13691 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13692 if (twin == NULL)
13693 nr = 0;
13694 }
13695 else
13696 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013697 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013698 nr = 0;
13699 }
13700 }
13701
13702 if (nr > 0)
13703 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13704 wp != twin; wp = wp->w_next)
13705 {
13706 if (wp == NULL)
13707 {
13708 /* didn't find it in this tabpage */
13709 nr = 0;
13710 break;
13711 }
13712 ++nr;
13713 }
13714 return nr;
13715}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013716
13717/*
13718 * "tabpagewinnr()" function
13719 */
13720 static void
13721f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13722{
13723 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013724 tabpage_T *tp;
13725
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013726 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013727 if (tp == NULL)
13728 nr = 0;
13729 else
13730 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013731 rettv->vval.v_number = nr;
13732}
13733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013734/*
13735 * "tagfiles()" function
13736 */
13737 static void
13738f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13739{
13740 char_u *fname;
13741 tagname_T tn;
13742 int first;
13743
13744 if (rettv_list_alloc(rettv) == FAIL)
13745 return;
13746 fname = alloc(MAXPATHL);
13747 if (fname == NULL)
13748 return;
13749
13750 for (first = TRUE; ; first = FALSE)
13751 if (get_tagfname(&tn, first, fname) == FAIL
13752 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13753 break;
13754 tagname_free(&tn);
13755 vim_free(fname);
13756}
13757
13758/*
13759 * "taglist()" function
13760 */
13761 static void
13762f_taglist(typval_T *argvars, typval_T *rettv)
13763{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013764 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013765 char_u *tag_pattern;
13766
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013767 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013768
13769 rettv->vval.v_number = FALSE;
13770 if (*tag_pattern == NUL)
13771 return;
13772
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013773 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013774 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013775 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013776 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013777}
13778
13779/*
13780 * "tempname()" function
13781 */
13782 static void
13783f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13784{
13785 static int x = 'A';
13786
13787 rettv->v_type = VAR_STRING;
13788 rettv->vval.v_string = vim_tempname(x, FALSE);
13789
13790 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13791 * names. Skip 'I' and 'O', they are used for shell redirection. */
13792 do
13793 {
13794 if (x == 'Z')
13795 x = '0';
13796 else if (x == '9')
13797 x = 'A';
13798 else
13799 {
13800#ifdef EBCDIC
13801 if (x == 'I')
13802 x = 'J';
13803 else if (x == 'R')
13804 x = 'S';
13805 else
13806#endif
13807 ++x;
13808 }
13809 } while (x == 'I' || x == 'O');
13810}
13811
13812#ifdef FEAT_FLOAT
13813/*
13814 * "tan()" function
13815 */
13816 static void
13817f_tan(typval_T *argvars, typval_T *rettv)
13818{
13819 float_T f = 0.0;
13820
13821 rettv->v_type = VAR_FLOAT;
13822 if (get_float_arg(argvars, &f) == OK)
13823 rettv->vval.v_float = tan(f);
13824 else
13825 rettv->vval.v_float = 0.0;
13826}
13827
13828/*
13829 * "tanh()" function
13830 */
13831 static void
13832f_tanh(typval_T *argvars, typval_T *rettv)
13833{
13834 float_T f = 0.0;
13835
13836 rettv->v_type = VAR_FLOAT;
13837 if (get_float_arg(argvars, &f) == OK)
13838 rettv->vval.v_float = tanh(f);
13839 else
13840 rettv->vval.v_float = 0.0;
13841}
13842#endif
13843
13844/*
13845 * "test_alloc_fail(id, countdown, repeat)" function
13846 */
13847 static void
13848f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13849{
13850 if (argvars[0].v_type != VAR_NUMBER
13851 || argvars[0].vval.v_number <= 0
13852 || argvars[1].v_type != VAR_NUMBER
13853 || argvars[1].vval.v_number < 0
13854 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013855 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013856 else
13857 {
13858 alloc_fail_id = argvars[0].vval.v_number;
13859 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013860 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013861 alloc_fail_countdown = argvars[1].vval.v_number;
13862 alloc_fail_repeat = argvars[2].vval.v_number;
13863 did_outofmem_msg = FALSE;
13864 }
13865}
13866
13867/*
13868 * "test_autochdir()"
13869 */
13870 static void
13871f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13872{
13873#if defined(FEAT_AUTOCHDIR)
13874 test_autochdir = TRUE;
13875#endif
13876}
13877
13878/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013879 * "test_feedinput()"
13880 */
13881 static void
13882f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13883{
13884#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013885 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013886
13887 if (val != NULL)
13888 {
13889 trash_input_buf();
13890 add_to_input_buf_csi(val, (int)STRLEN(val));
13891 }
13892#endif
13893}
13894
13895/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013896 * "test_option_not_set({name})" function
13897 */
13898 static void
13899f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13900{
13901 char_u *name = (char_u *)"";
13902
13903 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013904 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013905 else
13906 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013907 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013908 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013909 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013910 }
13911}
13912
13913/*
13914 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013915 */
13916 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013917f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013918{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013919 char_u *name = (char_u *)"";
13920 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013921 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013922
13923 if (argvars[0].v_type != VAR_STRING
13924 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013925 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013926 else
13927 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013928 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013929 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013930
13931 if (STRCMP(name, (char_u *)"redraw") == 0)
13932 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013933 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13934 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013935 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13936 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013937 else if (STRCMP(name, (char_u *)"starting") == 0)
13938 {
13939 if (val)
13940 {
13941 if (save_starting < 0)
13942 save_starting = starting;
13943 starting = 0;
13944 }
13945 else
13946 {
13947 starting = save_starting;
13948 save_starting = -1;
13949 }
13950 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013951 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13952 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013953 else if (STRCMP(name, (char_u *)"ALL") == 0)
13954 {
13955 disable_char_avail_for_testing = FALSE;
13956 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013957 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013958 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013959 if (save_starting >= 0)
13960 {
13961 starting = save_starting;
13962 save_starting = -1;
13963 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013964 }
13965 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013966 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013967 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013968}
13969
13970/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010013971 * "test_refcount({expr})" function
13972 */
13973 static void
13974f_test_refcount(typval_T *argvars, typval_T *rettv)
13975{
13976 int retval = -1;
13977
13978 switch (argvars[0].v_type)
13979 {
13980 case VAR_UNKNOWN:
13981 case VAR_NUMBER:
13982 case VAR_FLOAT:
13983 case VAR_SPECIAL:
13984 case VAR_STRING:
13985 break;
13986 case VAR_JOB:
13987#ifdef FEAT_JOB_CHANNEL
13988 if (argvars[0].vval.v_job != NULL)
13989 retval = argvars[0].vval.v_job->jv_refcount - 1;
13990#endif
13991 break;
13992 case VAR_CHANNEL:
13993#ifdef FEAT_JOB_CHANNEL
13994 if (argvars[0].vval.v_channel != NULL)
13995 retval = argvars[0].vval.v_channel->ch_refcount - 1;
13996#endif
13997 break;
13998 case VAR_FUNC:
13999 if (argvars[0].vval.v_string != NULL)
14000 {
14001 ufunc_T *fp;
14002
14003 fp = find_func(argvars[0].vval.v_string);
14004 if (fp != NULL)
14005 retval = fp->uf_refcount;
14006 }
14007 break;
14008 case VAR_PARTIAL:
14009 if (argvars[0].vval.v_partial != NULL)
14010 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14011 break;
14012 case VAR_BLOB:
14013 if (argvars[0].vval.v_blob != NULL)
14014 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14015 break;
14016 case VAR_LIST:
14017 if (argvars[0].vval.v_list != NULL)
14018 retval = argvars[0].vval.v_list->lv_refcount - 1;
14019 break;
14020 case VAR_DICT:
14021 if (argvars[0].vval.v_dict != NULL)
14022 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14023 break;
14024 }
14025
14026 rettv->v_type = VAR_NUMBER;
14027 rettv->vval.v_number = retval;
14028
14029}
14030
14031/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014032 * "test_garbagecollect_now()" function
14033 */
14034 static void
14035f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14036{
14037 /* This is dangerous, any Lists and Dicts used internally may be freed
14038 * while still in use. */
14039 garbage_collect(TRUE);
14040}
14041
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014042/*
14043 * "test_ignore_error()" function
14044 */
14045 static void
14046f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14047{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014048 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014049}
14050
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014051 static void
14052f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14053{
14054 rettv->v_type = VAR_BLOB;
14055 rettv->vval.v_blob = NULL;
14056}
14057
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014058#ifdef FEAT_JOB_CHANNEL
14059 static void
14060f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14061{
14062 rettv->v_type = VAR_CHANNEL;
14063 rettv->vval.v_channel = NULL;
14064}
14065#endif
14066
14067 static void
14068f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14069{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014070 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014071}
14072
14073#ifdef FEAT_JOB_CHANNEL
14074 static void
14075f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14076{
14077 rettv->v_type = VAR_JOB;
14078 rettv->vval.v_job = NULL;
14079}
14080#endif
14081
14082 static void
14083f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14084{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014085 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014086}
14087
14088 static void
14089f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14090{
14091 rettv->v_type = VAR_PARTIAL;
14092 rettv->vval.v_partial = NULL;
14093}
14094
14095 static void
14096f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14097{
14098 rettv->v_type = VAR_STRING;
14099 rettv->vval.v_string = NULL;
14100}
14101
Bram Moolenaarab186732018-09-14 21:27:06 +020014102#ifdef FEAT_GUI
14103 static void
14104f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14105{
14106 char_u *which;
14107 long value;
14108 int dragging;
14109 scrollbar_T *sb = NULL;
14110
14111 if (argvars[0].v_type != VAR_STRING
14112 || (argvars[1].v_type) != VAR_NUMBER
14113 || (argvars[2].v_type) != VAR_NUMBER)
14114 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014115 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014116 return;
14117 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014118 which = tv_get_string(&argvars[0]);
14119 value = tv_get_number(&argvars[1]);
14120 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014121
14122 if (STRCMP(which, "left") == 0)
14123 sb = &curwin->w_scrollbars[SBAR_LEFT];
14124 else if (STRCMP(which, "right") == 0)
14125 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14126 else if (STRCMP(which, "hor") == 0)
14127 sb = &gui.bottom_sbar;
14128 if (sb == NULL)
14129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014130 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014131 return;
14132 }
14133 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014134# ifndef USE_ON_FLY_SCROLL
14135 // need to loop through normal_cmd() to handle the scroll events
14136 exec_normal(FALSE, TRUE, FALSE);
14137# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014138}
14139#endif
14140
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014141 static void
14142f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14143{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014144 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014145}
14146
14147#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
14148/*
14149 * Get a callback from "arg". It can be a Funcref or a function name.
14150 * When "arg" is zero return an empty string.
14151 * Return NULL for an invalid argument.
14152 */
14153 char_u *
14154get_callback(typval_T *arg, partial_T **pp)
14155{
14156 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14157 {
14158 *pp = arg->vval.v_partial;
14159 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014160 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014161 }
14162 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014163 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014164 {
14165 func_ref(arg->vval.v_string);
14166 return arg->vval.v_string;
14167 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014168 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14169 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014170 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014171 return NULL;
14172}
14173
14174/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014175 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014176 */
14177 void
14178free_callback(char_u *callback, partial_T *partial)
14179{
14180 if (partial != NULL)
14181 partial_unref(partial);
14182 else if (callback != NULL)
14183 {
14184 func_unref(callback);
14185 vim_free(callback);
14186 }
14187}
14188#endif
14189
14190#ifdef FEAT_TIMERS
14191/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014192 * "timer_info([timer])" function
14193 */
14194 static void
14195f_timer_info(typval_T *argvars, typval_T *rettv)
14196{
14197 timer_T *timer = NULL;
14198
14199 if (rettv_list_alloc(rettv) != OK)
14200 return;
14201 if (argvars[0].v_type != VAR_UNKNOWN)
14202 {
14203 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014204 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014205 else
14206 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014207 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014208 if (timer != NULL)
14209 add_timer_info(rettv, timer);
14210 }
14211 }
14212 else
14213 add_timer_info_all(rettv);
14214}
14215
14216/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014217 * "timer_pause(timer, paused)" function
14218 */
14219 static void
14220f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14221{
14222 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014223 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014224
14225 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014226 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014227 else
14228 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014229 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014230 if (timer != NULL)
14231 timer->tr_paused = paused;
14232 }
14233}
14234
14235/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014236 * "timer_start(time, callback [, options])" function
14237 */
14238 static void
14239f_timer_start(typval_T *argvars, typval_T *rettv)
14240{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014241 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014242 timer_T *timer;
14243 int repeat = 0;
14244 char_u *callback;
14245 dict_T *dict;
14246 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014247
Bram Moolenaar75537a92016-09-05 22:45:28 +020014248 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014249 if (check_secure())
14250 return;
14251 if (argvars[2].v_type != VAR_UNKNOWN)
14252 {
14253 if (argvars[2].v_type != VAR_DICT
14254 || (dict = argvars[2].vval.v_dict) == NULL)
14255 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014256 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014257 return;
14258 }
14259 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014260 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014261 }
14262
Bram Moolenaar75537a92016-09-05 22:45:28 +020014263 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014264 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014265 return;
14266
14267 timer = create_timer(msec, repeat);
14268 if (timer == NULL)
14269 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014270 else
14271 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014272 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014273 timer->tr_callback = vim_strsave(callback);
14274 else
14275 /* pointer into the partial */
14276 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014277 timer->tr_partial = partial;
14278 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014279 }
14280}
14281
14282/*
14283 * "timer_stop(timer)" function
14284 */
14285 static void
14286f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14287{
14288 timer_T *timer;
14289
14290 if (argvars[0].v_type != VAR_NUMBER)
14291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014292 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014293 return;
14294 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014295 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014296 if (timer != NULL)
14297 stop_timer(timer);
14298}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014299
14300/*
14301 * "timer_stopall()" function
14302 */
14303 static void
14304f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14305{
14306 stop_all_timers();
14307}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014308#endif
14309
14310/*
14311 * "tolower(string)" function
14312 */
14313 static void
14314f_tolower(typval_T *argvars, typval_T *rettv)
14315{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014316 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014317 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014318}
14319
14320/*
14321 * "toupper(string)" function
14322 */
14323 static void
14324f_toupper(typval_T *argvars, typval_T *rettv)
14325{
14326 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014327 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014328}
14329
14330/*
14331 * "tr(string, fromstr, tostr)" function
14332 */
14333 static void
14334f_tr(typval_T *argvars, typval_T *rettv)
14335{
14336 char_u *in_str;
14337 char_u *fromstr;
14338 char_u *tostr;
14339 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014340 int inlen;
14341 int fromlen;
14342 int tolen;
14343 int idx;
14344 char_u *cpstr;
14345 int cplen;
14346 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014347 char_u buf[NUMBUFLEN];
14348 char_u buf2[NUMBUFLEN];
14349 garray_T ga;
14350
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014351 in_str = tv_get_string(&argvars[0]);
14352 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14353 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014354
14355 /* Default return value: empty string. */
14356 rettv->v_type = VAR_STRING;
14357 rettv->vval.v_string = NULL;
14358 if (fromstr == NULL || tostr == NULL)
14359 return; /* type error; errmsg already given */
14360 ga_init2(&ga, (int)sizeof(char), 80);
14361
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014362 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014363 /* not multi-byte: fromstr and tostr must be the same length */
14364 if (STRLEN(fromstr) != STRLEN(tostr))
14365 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014366error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014367 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014368 ga_clear(&ga);
14369 return;
14370 }
14371
14372 /* fromstr and tostr have to contain the same number of chars */
14373 while (*in_str != NUL)
14374 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014375 if (has_mbyte)
14376 {
14377 inlen = (*mb_ptr2len)(in_str);
14378 cpstr = in_str;
14379 cplen = inlen;
14380 idx = 0;
14381 for (p = fromstr; *p != NUL; p += fromlen)
14382 {
14383 fromlen = (*mb_ptr2len)(p);
14384 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14385 {
14386 for (p = tostr; *p != NUL; p += tolen)
14387 {
14388 tolen = (*mb_ptr2len)(p);
14389 if (idx-- == 0)
14390 {
14391 cplen = tolen;
14392 cpstr = p;
14393 break;
14394 }
14395 }
14396 if (*p == NUL) /* tostr is shorter than fromstr */
14397 goto error;
14398 break;
14399 }
14400 ++idx;
14401 }
14402
14403 if (first && cpstr == in_str)
14404 {
14405 /* Check that fromstr and tostr have the same number of
14406 * (multi-byte) characters. Done only once when a character
14407 * of in_str doesn't appear in fromstr. */
14408 first = FALSE;
14409 for (p = tostr; *p != NUL; p += tolen)
14410 {
14411 tolen = (*mb_ptr2len)(p);
14412 --idx;
14413 }
14414 if (idx != 0)
14415 goto error;
14416 }
14417
14418 (void)ga_grow(&ga, cplen);
14419 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14420 ga.ga_len += cplen;
14421
14422 in_str += inlen;
14423 }
14424 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014425 {
14426 /* When not using multi-byte chars we can do it faster. */
14427 p = vim_strchr(fromstr, *in_str);
14428 if (p != NULL)
14429 ga_append(&ga, tostr[p - fromstr]);
14430 else
14431 ga_append(&ga, *in_str);
14432 ++in_str;
14433 }
14434 }
14435
14436 /* add a terminating NUL */
14437 (void)ga_grow(&ga, 1);
14438 ga_append(&ga, NUL);
14439
14440 rettv->vval.v_string = ga.ga_data;
14441}
14442
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014443/*
14444 * "trim({expr})" function
14445 */
14446 static void
14447f_trim(typval_T *argvars, typval_T *rettv)
14448{
14449 char_u buf1[NUMBUFLEN];
14450 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014451 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014452 char_u *mask = NULL;
14453 char_u *tail;
14454 char_u *prev;
14455 char_u *p;
14456 int c1;
14457
14458 rettv->v_type = VAR_STRING;
14459 if (head == NULL)
14460 {
14461 rettv->vval.v_string = NULL;
14462 return;
14463 }
14464
14465 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014466 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014467
14468 while (*head != NUL)
14469 {
14470 c1 = PTR2CHAR(head);
14471 if (mask == NULL)
14472 {
14473 if (c1 > ' ' && c1 != 0xa0)
14474 break;
14475 }
14476 else
14477 {
14478 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14479 if (c1 == PTR2CHAR(p))
14480 break;
14481 if (*p == NUL)
14482 break;
14483 }
14484 MB_PTR_ADV(head);
14485 }
14486
14487 for (tail = head + STRLEN(head); tail > head; tail = prev)
14488 {
14489 prev = tail;
14490 MB_PTR_BACK(head, prev);
14491 c1 = PTR2CHAR(prev);
14492 if (mask == NULL)
14493 {
14494 if (c1 > ' ' && c1 != 0xa0)
14495 break;
14496 }
14497 else
14498 {
14499 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14500 if (c1 == PTR2CHAR(p))
14501 break;
14502 if (*p == NUL)
14503 break;
14504 }
14505 }
14506 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14507}
14508
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014509#ifdef FEAT_FLOAT
14510/*
14511 * "trunc({float})" function
14512 */
14513 static void
14514f_trunc(typval_T *argvars, typval_T *rettv)
14515{
14516 float_T f = 0.0;
14517
14518 rettv->v_type = VAR_FLOAT;
14519 if (get_float_arg(argvars, &f) == OK)
14520 /* trunc() is not in C90, use floor() or ceil() instead. */
14521 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14522 else
14523 rettv->vval.v_float = 0.0;
14524}
14525#endif
14526
14527/*
14528 * "type(expr)" function
14529 */
14530 static void
14531f_type(typval_T *argvars, typval_T *rettv)
14532{
14533 int n = -1;
14534
14535 switch (argvars[0].v_type)
14536 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014537 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14538 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014539 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014540 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14541 case VAR_LIST: n = VAR_TYPE_LIST; break;
14542 case VAR_DICT: n = VAR_TYPE_DICT; break;
14543 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014544 case VAR_SPECIAL:
14545 if (argvars[0].vval.v_number == VVAL_FALSE
14546 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014547 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014548 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014549 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014550 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014551 case VAR_JOB: n = VAR_TYPE_JOB; break;
14552 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014553 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014554 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014555 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014556 n = -1;
14557 break;
14558 }
14559 rettv->vval.v_number = n;
14560}
14561
14562/*
14563 * "undofile(name)" function
14564 */
14565 static void
14566f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14567{
14568 rettv->v_type = VAR_STRING;
14569#ifdef FEAT_PERSISTENT_UNDO
14570 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014571 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014572
14573 if (*fname == NUL)
14574 {
14575 /* If there is no file name there will be no undo file. */
14576 rettv->vval.v_string = NULL;
14577 }
14578 else
14579 {
14580 char_u *ffname = FullName_save(fname, FALSE);
14581
14582 if (ffname != NULL)
14583 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14584 vim_free(ffname);
14585 }
14586 }
14587#else
14588 rettv->vval.v_string = NULL;
14589#endif
14590}
14591
14592/*
14593 * "undotree()" function
14594 */
14595 static void
14596f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14597{
14598 if (rettv_dict_alloc(rettv) == OK)
14599 {
14600 dict_T *dict = rettv->vval.v_dict;
14601 list_T *list;
14602
Bram Moolenaare0be1672018-07-08 16:50:37 +020014603 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14604 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14605 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14606 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14607 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14608 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014609
14610 list = list_alloc();
14611 if (list != NULL)
14612 {
14613 u_eval_tree(curbuf->b_u_oldhead, list);
14614 dict_add_list(dict, "entries", list);
14615 }
14616 }
14617}
14618
14619/*
14620 * "values(dict)" function
14621 */
14622 static void
14623f_values(typval_T *argvars, typval_T *rettv)
14624{
14625 dict_list(argvars, rettv, 1);
14626}
14627
14628/*
14629 * "virtcol(string)" function
14630 */
14631 static void
14632f_virtcol(typval_T *argvars, typval_T *rettv)
14633{
14634 colnr_T vcol = 0;
14635 pos_T *fp;
14636 int fnum = curbuf->b_fnum;
14637
14638 fp = var2fpos(&argvars[0], FALSE, &fnum);
14639 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14640 && fnum == curbuf->b_fnum)
14641 {
14642 getvvcol(curwin, fp, NULL, NULL, &vcol);
14643 ++vcol;
14644 }
14645
14646 rettv->vval.v_number = vcol;
14647}
14648
14649/*
14650 * "visualmode()" function
14651 */
14652 static void
14653f_visualmode(typval_T *argvars, typval_T *rettv)
14654{
14655 char_u str[2];
14656
14657 rettv->v_type = VAR_STRING;
14658 str[0] = curbuf->b_visual_mode_eval;
14659 str[1] = NUL;
14660 rettv->vval.v_string = vim_strsave(str);
14661
14662 /* A non-zero number or non-empty string argument: reset mode. */
14663 if (non_zero_arg(&argvars[0]))
14664 curbuf->b_visual_mode_eval = NUL;
14665}
14666
14667/*
14668 * "wildmenumode()" function
14669 */
14670 static void
14671f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14672{
14673#ifdef FEAT_WILDMENU
14674 if (wild_menu_showing)
14675 rettv->vval.v_number = 1;
14676#endif
14677}
14678
14679/*
14680 * "winbufnr(nr)" function
14681 */
14682 static void
14683f_winbufnr(typval_T *argvars, typval_T *rettv)
14684{
14685 win_T *wp;
14686
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014687 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014688 if (wp == NULL)
14689 rettv->vval.v_number = -1;
14690 else
14691 rettv->vval.v_number = wp->w_buffer->b_fnum;
14692}
14693
14694/*
14695 * "wincol()" function
14696 */
14697 static void
14698f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14699{
14700 validate_cursor();
14701 rettv->vval.v_number = curwin->w_wcol + 1;
14702}
14703
14704/*
14705 * "winheight(nr)" function
14706 */
14707 static void
14708f_winheight(typval_T *argvars, typval_T *rettv)
14709{
14710 win_T *wp;
14711
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014712 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014713 if (wp == NULL)
14714 rettv->vval.v_number = -1;
14715 else
14716 rettv->vval.v_number = wp->w_height;
14717}
14718
14719/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014720 * "winlayout()" function
14721 */
14722 static void
14723f_winlayout(typval_T *argvars, typval_T *rettv)
14724{
14725 tabpage_T *tp;
14726
14727 if (rettv_list_alloc(rettv) != OK)
14728 return;
14729
14730 if (argvars[0].v_type == VAR_UNKNOWN)
14731 tp = curtab;
14732 else
14733 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014734 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014735 if (tp == NULL)
14736 return;
14737 }
14738
14739 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14740}
14741
14742/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014743 * "winline()" function
14744 */
14745 static void
14746f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14747{
14748 validate_cursor();
14749 rettv->vval.v_number = curwin->w_wrow + 1;
14750}
14751
14752/*
14753 * "winnr()" function
14754 */
14755 static void
14756f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14757{
14758 int nr = 1;
14759
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014760 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014761 rettv->vval.v_number = nr;
14762}
14763
14764/*
14765 * "winrestcmd()" function
14766 */
14767 static void
14768f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14769{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014770 win_T *wp;
14771 int winnr = 1;
14772 garray_T ga;
14773 char_u buf[50];
14774
14775 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014776 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014777 {
14778 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14779 ga_concat(&ga, buf);
14780 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14781 ga_concat(&ga, buf);
14782 ++winnr;
14783 }
14784 ga_append(&ga, NUL);
14785
14786 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014787 rettv->v_type = VAR_STRING;
14788}
14789
14790/*
14791 * "winrestview()" function
14792 */
14793 static void
14794f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14795{
14796 dict_T *dict;
14797
14798 if (argvars[0].v_type != VAR_DICT
14799 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014800 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014801 else
14802 {
14803 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014804 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014805 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014806 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014807 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014808 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014809 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14810 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014811 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014812 curwin->w_set_curswant = FALSE;
14813 }
14814
14815 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014816 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014817#ifdef FEAT_DIFF
14818 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014819 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014820#endif
14821 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014822 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014823 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014824 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014825
14826 check_cursor();
14827 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014828 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014829 changed_window_setting();
14830
14831 if (curwin->w_topline <= 0)
14832 curwin->w_topline = 1;
14833 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14834 curwin->w_topline = curbuf->b_ml.ml_line_count;
14835#ifdef FEAT_DIFF
14836 check_topfill(curwin, TRUE);
14837#endif
14838 }
14839}
14840
14841/*
14842 * "winsaveview()" function
14843 */
14844 static void
14845f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14846{
14847 dict_T *dict;
14848
14849 if (rettv_dict_alloc(rettv) == FAIL)
14850 return;
14851 dict = rettv->vval.v_dict;
14852
Bram Moolenaare0be1672018-07-08 16:50:37 +020014853 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14854 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020014855 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014856 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014857 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014858
Bram Moolenaare0be1672018-07-08 16:50:37 +020014859 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014860#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014861 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014862#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014863 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14864 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014865}
14866
14867/*
14868 * "winwidth(nr)" function
14869 */
14870 static void
14871f_winwidth(typval_T *argvars, typval_T *rettv)
14872{
14873 win_T *wp;
14874
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014875 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014876 if (wp == NULL)
14877 rettv->vval.v_number = -1;
14878 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014879 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014880}
14881
14882/*
14883 * "wordcount()" function
14884 */
14885 static void
14886f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14887{
14888 if (rettv_dict_alloc(rettv) == FAIL)
14889 return;
14890 cursor_pos_info(rettv->vval.v_dict);
14891}
14892
14893/*
14894 * "writefile()" function
14895 */
14896 static void
14897f_writefile(typval_T *argvars, typval_T *rettv)
14898{
14899 int binary = FALSE;
14900 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014901#ifdef HAVE_FSYNC
14902 int do_fsync = p_fs;
14903#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014904 char_u *fname;
14905 FILE *fd;
14906 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014907 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014908 list_T *list = NULL;
14909 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014910
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014911 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010014912 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014913 return;
14914
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014915 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014916 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014917 list = argvars[0].vval.v_list;
14918 if (list == NULL)
14919 return;
14920 for (li = list->lv_first; li != NULL; li = li->li_next)
14921 if (tv_get_string_chk(&li->li_tv) == NULL)
14922 return;
14923 }
14924 else if (argvars[0].v_type == VAR_BLOB)
14925 {
14926 blob = argvars[0].vval.v_blob;
14927 if (blob == NULL)
14928 return;
14929 }
14930 else
14931 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014932 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014933 return;
14934 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014935
14936 if (argvars[2].v_type != VAR_UNKNOWN)
14937 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014938 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014939
14940 if (arg2 == NULL)
14941 return;
14942 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014943 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014944 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014945 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014946#ifdef HAVE_FSYNC
14947 if (vim_strchr(arg2, 's') != NULL)
14948 do_fsync = TRUE;
14949 else if (vim_strchr(arg2, 'S') != NULL)
14950 do_fsync = FALSE;
14951#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014952 }
14953
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014954 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014955 if (fname == NULL)
14956 return;
14957
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014958 /* Always open the file in binary mode, library functions have a mind of
14959 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014960 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14961 append ? APPENDBIN : WRITEBIN)) == NULL)
14962 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014963 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014964 ret = -1;
14965 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014966 else if (blob)
14967 {
14968 if (write_blob(fd, blob) == FAIL)
14969 ret = -1;
14970#ifdef HAVE_FSYNC
14971 else if (do_fsync)
14972 // Ignore the error, the user wouldn't know what to do about it.
14973 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010014974 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014975#endif
14976 fclose(fd);
14977 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014978 else
14979 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014980 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014981 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014982#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014983 else if (do_fsync)
14984 /* Ignore the error, the user wouldn't know what to do about it.
14985 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010014986 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014987#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014988 fclose(fd);
14989 }
14990
14991 rettv->vval.v_number = ret;
14992}
14993
14994/*
14995 * "xor(expr, expr)" function
14996 */
14997 static void
14998f_xor(typval_T *argvars, typval_T *rettv)
14999{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015000 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15001 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015002}
15003
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015004#endif /* FEAT_EVAL */