blob: d371c28b664fa220ba78c46b7c0c1700b14e3ac1 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010032static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033static char *e_stringreq = N_("E928: String required");
Bram Moolenaaraff74912019-03-30 18:11:49 +010034static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020035
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020066static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010067static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010069static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010070# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010071#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_browse(typval_T *argvars, typval_T *rettv);
73static void f_browsedir(typval_T *argvars, typval_T *rettv);
74static void f_bufexists(typval_T *argvars, typval_T *rettv);
75static void f_buflisted(typval_T *argvars, typval_T *rettv);
76static void f_bufloaded(typval_T *argvars, typval_T *rettv);
77static void f_bufname(typval_T *argvars, typval_T *rettv);
78static void f_bufnr(typval_T *argvars, typval_T *rettv);
79static void f_bufwinid(typval_T *argvars, typval_T *rettv);
80static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
81static void f_byte2line(typval_T *argvars, typval_T *rettv);
82static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
83static void f_byteidx(typval_T *argvars, typval_T *rettv);
84static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
85static void f_call(typval_T *argvars, typval_T *rettv);
86#ifdef FEAT_FLOAT
87static void f_ceil(typval_T *argvars, typval_T *rettv);
88#endif
89#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010090static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020092static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
94static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
95static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
96static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
97static void f_ch_info(typval_T *argvars, typval_T *rettv);
98static void f_ch_log(typval_T *argvars, typval_T *rettv);
99static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
100static void f_ch_open(typval_T *argvars, typval_T *rettv);
101static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100102static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200103static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
105static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
106static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
107static void f_ch_status(typval_T *argvars, typval_T *rettv);
108#endif
109static void f_changenr(typval_T *argvars, typval_T *rettv);
110static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200111static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200112static void f_cindent(typval_T *argvars, typval_T *rettv);
113static void f_clearmatches(typval_T *argvars, typval_T *rettv);
114static void f_col(typval_T *argvars, typval_T *rettv);
115#if defined(FEAT_INS_EXPAND)
116static void f_complete(typval_T *argvars, typval_T *rettv);
117static void f_complete_add(typval_T *argvars, typval_T *rettv);
118static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +0100119static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120#endif
121static void f_confirm(typval_T *argvars, typval_T *rettv);
122static void f_copy(typval_T *argvars, typval_T *rettv);
123#ifdef FEAT_FLOAT
124static void f_cos(typval_T *argvars, typval_T *rettv);
125static void f_cosh(typval_T *argvars, typval_T *rettv);
126#endif
127static void f_count(typval_T *argvars, typval_T *rettv);
128static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
129static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100130#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200131static void f_debugbreak(typval_T *argvars, typval_T *rettv);
132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_deepcopy(typval_T *argvars, typval_T *rettv);
134static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200135static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200136static void f_did_filetype(typval_T *argvars, typval_T *rettv);
137static void f_diff_filler(typval_T *argvars, typval_T *rettv);
138static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
139static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200140static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_escape(typval_T *argvars, typval_T *rettv);
142static void f_eval(typval_T *argvars, typval_T *rettv);
143static void f_eventhandler(typval_T *argvars, typval_T *rettv);
144static void f_executable(typval_T *argvars, typval_T *rettv);
145static void f_execute(typval_T *argvars, typval_T *rettv);
146static void f_exepath(typval_T *argvars, typval_T *rettv);
147static void f_exists(typval_T *argvars, typval_T *rettv);
148#ifdef FEAT_FLOAT
149static void f_exp(typval_T *argvars, typval_T *rettv);
150#endif
151static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200152static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153static void f_extend(typval_T *argvars, typval_T *rettv);
154static void f_feedkeys(typval_T *argvars, typval_T *rettv);
155static void f_filereadable(typval_T *argvars, typval_T *rettv);
156static void f_filewritable(typval_T *argvars, typval_T *rettv);
157static void f_filter(typval_T *argvars, typval_T *rettv);
158static void f_finddir(typval_T *argvars, typval_T *rettv);
159static void f_findfile(typval_T *argvars, typval_T *rettv);
160#ifdef FEAT_FLOAT
161static void f_float2nr(typval_T *argvars, typval_T *rettv);
162static void f_floor(typval_T *argvars, typval_T *rettv);
163static void f_fmod(typval_T *argvars, typval_T *rettv);
164#endif
165static void f_fnameescape(typval_T *argvars, typval_T *rettv);
166static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
167static void f_foldclosed(typval_T *argvars, typval_T *rettv);
168static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
169static void f_foldlevel(typval_T *argvars, typval_T *rettv);
170static void f_foldtext(typval_T *argvars, typval_T *rettv);
171static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
172static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200173static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_function(typval_T *argvars, typval_T *rettv);
175static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
176static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200177static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200178static void f_getbufline(typval_T *argvars, typval_T *rettv);
179static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100180static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200181static void f_getchar(typval_T *argvars, typval_T *rettv);
182static void f_getcharmod(typval_T *argvars, typval_T *rettv);
183static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
184static void f_getcmdline(typval_T *argvars, typval_T *rettv);
185#if defined(FEAT_CMDL_COMPL)
186static void f_getcompletion(typval_T *argvars, typval_T *rettv);
187#endif
188static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
189static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
190static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
191static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200192static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_getfontname(typval_T *argvars, typval_T *rettv);
194static void f_getfperm(typval_T *argvars, typval_T *rettv);
195static void f_getfsize(typval_T *argvars, typval_T *rettv);
196static void f_getftime(typval_T *argvars, typval_T *rettv);
197static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100198static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200199static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200200static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_getmatches(typval_T *argvars, typval_T *rettv);
202static void f_getpid(typval_T *argvars, typval_T *rettv);
203static void f_getcurpos(typval_T *argvars, typval_T *rettv);
204static void f_getpos(typval_T *argvars, typval_T *rettv);
205static void f_getqflist(typval_T *argvars, typval_T *rettv);
206static void f_getreg(typval_T *argvars, typval_T *rettv);
207static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200208static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209static void f_gettabvar(typval_T *argvars, typval_T *rettv);
210static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100211static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200212static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100213static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200214static void f_getwinposx(typval_T *argvars, typval_T *rettv);
215static void f_getwinposy(typval_T *argvars, typval_T *rettv);
216static void f_getwinvar(typval_T *argvars, typval_T *rettv);
217static void f_glob(typval_T *argvars, typval_T *rettv);
218static void f_globpath(typval_T *argvars, typval_T *rettv);
219static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
220static void f_has(typval_T *argvars, typval_T *rettv);
221static void f_has_key(typval_T *argvars, typval_T *rettv);
222static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
223static void f_hasmapto(typval_T *argvars, typval_T *rettv);
224static void f_histadd(typval_T *argvars, typval_T *rettv);
225static void f_histdel(typval_T *argvars, typval_T *rettv);
226static void f_histget(typval_T *argvars, typval_T *rettv);
227static void f_histnr(typval_T *argvars, typval_T *rettv);
228static void f_hlID(typval_T *argvars, typval_T *rettv);
229static void f_hlexists(typval_T *argvars, typval_T *rettv);
230static void f_hostname(typval_T *argvars, typval_T *rettv);
231static void f_iconv(typval_T *argvars, typval_T *rettv);
232static void f_indent(typval_T *argvars, typval_T *rettv);
233static void f_index(typval_T *argvars, typval_T *rettv);
234static void f_input(typval_T *argvars, typval_T *rettv);
235static void f_inputdialog(typval_T *argvars, typval_T *rettv);
236static void f_inputlist(typval_T *argvars, typval_T *rettv);
237static void f_inputrestore(typval_T *argvars, typval_T *rettv);
238static void f_inputsave(typval_T *argvars, typval_T *rettv);
239static void f_inputsecret(typval_T *argvars, typval_T *rettv);
240static void f_insert(typval_T *argvars, typval_T *rettv);
241static void f_invert(typval_T *argvars, typval_T *rettv);
242static void f_isdirectory(typval_T *argvars, typval_T *rettv);
243static void f_islocked(typval_T *argvars, typval_T *rettv);
244#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200245static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_isnan(typval_T *argvars, typval_T *rettv);
247#endif
248static void f_items(typval_T *argvars, typval_T *rettv);
249#ifdef FEAT_JOB_CHANNEL
250static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
251static void f_job_info(typval_T *argvars, typval_T *rettv);
252static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
253static void f_job_start(typval_T *argvars, typval_T *rettv);
254static void f_job_stop(typval_T *argvars, typval_T *rettv);
255static void f_job_status(typval_T *argvars, typval_T *rettv);
256#endif
257static void f_join(typval_T *argvars, typval_T *rettv);
258static void f_js_decode(typval_T *argvars, typval_T *rettv);
259static void f_js_encode(typval_T *argvars, typval_T *rettv);
260static void f_json_decode(typval_T *argvars, typval_T *rettv);
261static void f_json_encode(typval_T *argvars, typval_T *rettv);
262static void f_keys(typval_T *argvars, typval_T *rettv);
263static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
264static void f_len(typval_T *argvars, typval_T *rettv);
265static void f_libcall(typval_T *argvars, typval_T *rettv);
266static void f_libcallnr(typval_T *argvars, typval_T *rettv);
267static void f_line(typval_T *argvars, typval_T *rettv);
268static void f_line2byte(typval_T *argvars, typval_T *rettv);
269static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200270static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200271static void f_localtime(typval_T *argvars, typval_T *rettv);
272#ifdef FEAT_FLOAT
273static void f_log(typval_T *argvars, typval_T *rettv);
274static void f_log10(typval_T *argvars, typval_T *rettv);
275#endif
276#ifdef FEAT_LUA
277static void f_luaeval(typval_T *argvars, typval_T *rettv);
278#endif
279static void f_map(typval_T *argvars, typval_T *rettv);
280static void f_maparg(typval_T *argvars, typval_T *rettv);
281static void f_mapcheck(typval_T *argvars, typval_T *rettv);
282static void f_match(typval_T *argvars, typval_T *rettv);
283static void f_matchadd(typval_T *argvars, typval_T *rettv);
284static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
285static void f_matcharg(typval_T *argvars, typval_T *rettv);
286static void f_matchdelete(typval_T *argvars, typval_T *rettv);
287static void f_matchend(typval_T *argvars, typval_T *rettv);
288static void f_matchlist(typval_T *argvars, typval_T *rettv);
289static void f_matchstr(typval_T *argvars, typval_T *rettv);
290static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
291static void f_max(typval_T *argvars, typval_T *rettv);
292static void f_min(typval_T *argvars, typval_T *rettv);
293#ifdef vim_mkdir
294static void f_mkdir(typval_T *argvars, typval_T *rettv);
295#endif
296static void f_mode(typval_T *argvars, typval_T *rettv);
297#ifdef FEAT_MZSCHEME
298static void f_mzeval(typval_T *argvars, typval_T *rettv);
299#endif
300static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
301static void f_nr2char(typval_T *argvars, typval_T *rettv);
302static void f_or(typval_T *argvars, typval_T *rettv);
303static void f_pathshorten(typval_T *argvars, typval_T *rettv);
304#ifdef FEAT_PERL
305static void f_perleval(typval_T *argvars, typval_T *rettv);
306#endif
307#ifdef FEAT_FLOAT
308static void f_pow(typval_T *argvars, typval_T *rettv);
309#endif
310static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
311static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200312#ifdef FEAT_JOB_CHANNEL
313static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200314static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200315static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
316#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200317static void f_pumvisible(typval_T *argvars, typval_T *rettv);
318#ifdef FEAT_PYTHON3
319static void f_py3eval(typval_T *argvars, typval_T *rettv);
320#endif
321#ifdef FEAT_PYTHON
322static void f_pyeval(typval_T *argvars, typval_T *rettv);
323#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100324#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
325static void f_pyxeval(typval_T *argvars, typval_T *rettv);
326#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200327static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200328static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200329static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200330static void f_reg_executing(typval_T *argvars, typval_T *rettv);
331static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200332static void f_reltime(typval_T *argvars, typval_T *rettv);
333#ifdef FEAT_FLOAT
334static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
335#endif
336static void f_reltimestr(typval_T *argvars, typval_T *rettv);
337static void f_remote_expr(typval_T *argvars, typval_T *rettv);
338static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
339static void f_remote_peek(typval_T *argvars, typval_T *rettv);
340static void f_remote_read(typval_T *argvars, typval_T *rettv);
341static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100342static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200343static void f_remove(typval_T *argvars, typval_T *rettv);
344static void f_rename(typval_T *argvars, typval_T *rettv);
345static void f_repeat(typval_T *argvars, typval_T *rettv);
346static void f_resolve(typval_T *argvars, typval_T *rettv);
347static void f_reverse(typval_T *argvars, typval_T *rettv);
348#ifdef FEAT_FLOAT
349static void f_round(typval_T *argvars, typval_T *rettv);
350#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100351#ifdef FEAT_RUBY
352static void f_rubyeval(typval_T *argvars, typval_T *rettv);
353#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200354static void f_screenattr(typval_T *argvars, typval_T *rettv);
355static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100356static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200357static void f_screencol(typval_T *argvars, typval_T *rettv);
358static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100359static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200360static void f_search(typval_T *argvars, typval_T *rettv);
361static void f_searchdecl(typval_T *argvars, typval_T *rettv);
362static void f_searchpair(typval_T *argvars, typval_T *rettv);
363static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
364static void f_searchpos(typval_T *argvars, typval_T *rettv);
365static void f_server2client(typval_T *argvars, typval_T *rettv);
366static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200367static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200368static void f_setbufvar(typval_T *argvars, typval_T *rettv);
369static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
370static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200371static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200372static void f_setfperm(typval_T *argvars, typval_T *rettv);
373static void f_setline(typval_T *argvars, typval_T *rettv);
374static void f_setloclist(typval_T *argvars, typval_T *rettv);
375static void f_setmatches(typval_T *argvars, typval_T *rettv);
376static void f_setpos(typval_T *argvars, typval_T *rettv);
377static void f_setqflist(typval_T *argvars, typval_T *rettv);
378static void f_setreg(typval_T *argvars, typval_T *rettv);
379static void f_settabvar(typval_T *argvars, typval_T *rettv);
380static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100381static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200382static void f_setwinvar(typval_T *argvars, typval_T *rettv);
383#ifdef FEAT_CRYPT
384static void f_sha256(typval_T *argvars, typval_T *rettv);
385#endif /* FEAT_CRYPT */
386static void f_shellescape(typval_T *argvars, typval_T *rettv);
387static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100388#ifdef FEAT_SIGNS
389static void f_sign_define(typval_T *argvars, typval_T *rettv);
390static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
391static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100392static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100393static void f_sign_place(typval_T *argvars, typval_T *rettv);
394static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
395static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
396#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200397static void f_simplify(typval_T *argvars, typval_T *rettv);
398#ifdef FEAT_FLOAT
399static void f_sin(typval_T *argvars, typval_T *rettv);
400static void f_sinh(typval_T *argvars, typval_T *rettv);
401#endif
402static void f_sort(typval_T *argvars, typval_T *rettv);
403static void f_soundfold(typval_T *argvars, typval_T *rettv);
404static void f_spellbadword(typval_T *argvars, typval_T *rettv);
405static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
406static void f_split(typval_T *argvars, typval_T *rettv);
407#ifdef FEAT_FLOAT
408static void f_sqrt(typval_T *argvars, typval_T *rettv);
409static void f_str2float(typval_T *argvars, typval_T *rettv);
410#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200411static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200412static void f_str2nr(typval_T *argvars, typval_T *rettv);
413static void f_strchars(typval_T *argvars, typval_T *rettv);
414#ifdef HAVE_STRFTIME
415static void f_strftime(typval_T *argvars, typval_T *rettv);
416#endif
417static void f_strgetchar(typval_T *argvars, typval_T *rettv);
418static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200419static void f_strlen(typval_T *argvars, typval_T *rettv);
420static void f_strcharpart(typval_T *argvars, typval_T *rettv);
421static void f_strpart(typval_T *argvars, typval_T *rettv);
422static void f_strridx(typval_T *argvars, typval_T *rettv);
423static void f_strtrans(typval_T *argvars, typval_T *rettv);
424static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
425static void f_strwidth(typval_T *argvars, typval_T *rettv);
426static void f_submatch(typval_T *argvars, typval_T *rettv);
427static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200428static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200429static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200430static void f_synID(typval_T *argvars, typval_T *rettv);
431static void f_synIDattr(typval_T *argvars, typval_T *rettv);
432static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
433static void f_synstack(typval_T *argvars, typval_T *rettv);
434static void f_synconcealed(typval_T *argvars, typval_T *rettv);
435static void f_system(typval_T *argvars, typval_T *rettv);
436static void f_systemlist(typval_T *argvars, typval_T *rettv);
437static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
438static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
439static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
440static void f_taglist(typval_T *argvars, typval_T *rettv);
441static void f_tagfiles(typval_T *argvars, typval_T *rettv);
442static void f_tempname(typval_T *argvars, typval_T *rettv);
443static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
444static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200445static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareda65222019-05-16 20:29:44 +0200446static void f_test_getvalue(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200447static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100448static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100449static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200450static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100451static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100452static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200453#ifdef FEAT_JOB_CHANNEL
454static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
455#endif
456static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
457#ifdef FEAT_JOB_CHANNEL
458static void f_test_null_job(typval_T *argvars, typval_T *rettv);
459#endif
460static void f_test_null_list(typval_T *argvars, typval_T *rettv);
461static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
462static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200463#ifdef FEAT_GUI
464static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
465#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200466#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200467static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200468#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200469static void f_test_settime(typval_T *argvars, typval_T *rettv);
470#ifdef FEAT_FLOAT
471static void f_tan(typval_T *argvars, typval_T *rettv);
472static void f_tanh(typval_T *argvars, typval_T *rettv);
473#endif
474#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200475static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200476static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200477static void f_timer_start(typval_T *argvars, typval_T *rettv);
478static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200479static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200480#endif
481static void f_tolower(typval_T *argvars, typval_T *rettv);
482static void f_toupper(typval_T *argvars, typval_T *rettv);
483static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100484static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200485#ifdef FEAT_FLOAT
486static void f_trunc(typval_T *argvars, typval_T *rettv);
487#endif
488static void f_type(typval_T *argvars, typval_T *rettv);
489static void f_undofile(typval_T *argvars, typval_T *rettv);
490static void f_undotree(typval_T *argvars, typval_T *rettv);
491static void f_uniq(typval_T *argvars, typval_T *rettv);
492static void f_values(typval_T *argvars, typval_T *rettv);
493static void f_virtcol(typval_T *argvars, typval_T *rettv);
494static void f_visualmode(typval_T *argvars, typval_T *rettv);
495static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200496static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
498static void f_win_getid(typval_T *argvars, typval_T *rettv);
499static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
500static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
501static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100502static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200503static void f_winbufnr(typval_T *argvars, typval_T *rettv);
504static void f_wincol(typval_T *argvars, typval_T *rettv);
505static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200506static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507static void f_winline(typval_T *argvars, typval_T *rettv);
508static void f_winnr(typval_T *argvars, typval_T *rettv);
509static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
510static void f_winrestview(typval_T *argvars, typval_T *rettv);
511static void f_winsaveview(typval_T *argvars, typval_T *rettv);
512static void f_winwidth(typval_T *argvars, typval_T *rettv);
513static void f_writefile(typval_T *argvars, typval_T *rettv);
514static void f_wordcount(typval_T *argvars, typval_T *rettv);
515static void f_xor(typval_T *argvars, typval_T *rettv);
516
517/*
518 * Array with names and number of arguments of all internal functions
519 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
520 */
521static struct fst
522{
523 char *f_name; /* function name */
524 char f_min_argc; /* minimal number of arguments */
525 char f_max_argc; /* maximal number of arguments */
526 void (*f_func)(typval_T *args, typval_T *rvar);
527 /* implementation of function */
528} functions[] =
529{
530#ifdef FEAT_FLOAT
531 {"abs", 1, 1, f_abs},
532 {"acos", 1, 1, f_acos}, /* WJMc */
533#endif
534 {"add", 2, 2, f_add},
535 {"and", 2, 2, f_and},
536 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200537 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200538 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200539 {"argidx", 0, 0, f_argidx},
540 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200541 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200542#ifdef FEAT_FLOAT
543 {"asin", 1, 1, f_asin}, /* WJMc */
544#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100545 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200546 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100547 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200549 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100551 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200552 {"assert_match", 2, 3, f_assert_match},
553 {"assert_notequal", 2, 3, f_assert_notequal},
554 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100555 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200556 {"assert_true", 1, 2, f_assert_true},
557#ifdef FEAT_FLOAT
558 {"atan", 1, 1, f_atan},
559 {"atan2", 2, 2, f_atan2},
560#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100561#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200562 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100563 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100564# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100565 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100566# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100567#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200568 {"browse", 4, 4, f_browse},
569 {"browsedir", 2, 2, f_browsedir},
570 {"bufexists", 1, 1, f_bufexists},
571 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
572 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
573 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
574 {"buflisted", 1, 1, f_buflisted},
575 {"bufloaded", 1, 1, f_bufloaded},
576 {"bufname", 1, 1, f_bufname},
577 {"bufnr", 1, 2, f_bufnr},
578 {"bufwinid", 1, 1, f_bufwinid},
579 {"bufwinnr", 1, 1, f_bufwinnr},
580 {"byte2line", 1, 1, f_byte2line},
581 {"byteidx", 2, 2, f_byteidx},
582 {"byteidxcomp", 2, 2, f_byteidxcomp},
583 {"call", 2, 3, f_call},
584#ifdef FEAT_FLOAT
585 {"ceil", 1, 1, f_ceil},
586#endif
587#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100588 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200590 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200591 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
592 {"ch_evalraw", 2, 3, f_ch_evalraw},
593 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
594 {"ch_getjob", 1, 1, f_ch_getjob},
595 {"ch_info", 1, 1, f_ch_info},
596 {"ch_log", 1, 2, f_ch_log},
597 {"ch_logfile", 1, 2, f_ch_logfile},
598 {"ch_open", 1, 2, f_ch_open},
599 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100600 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200601 {"ch_readraw", 1, 2, f_ch_readraw},
602 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
603 {"ch_sendraw", 2, 3, f_ch_sendraw},
604 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200605 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200606#endif
607 {"changenr", 0, 0, f_changenr},
608 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200609 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100611 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200612 {"col", 1, 1, f_col},
613#if defined(FEAT_INS_EXPAND)
614 {"complete", 2, 2, f_complete},
615 {"complete_add", 1, 1, f_complete_add},
616 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100617 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200618#endif
619 {"confirm", 1, 4, f_confirm},
620 {"copy", 1, 1, f_copy},
621#ifdef FEAT_FLOAT
622 {"cos", 1, 1, f_cos},
623 {"cosh", 1, 1, f_cosh},
624#endif
625 {"count", 2, 4, f_count},
626 {"cscope_connection",0,3, f_cscope_connection},
627 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100628#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200629 {"debugbreak", 1, 1, f_debugbreak},
630#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200631 {"deepcopy", 1, 2, f_deepcopy},
632 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200633 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200634 {"did_filetype", 0, 0, f_did_filetype},
635 {"diff_filler", 1, 1, f_diff_filler},
636 {"diff_hlID", 2, 2, f_diff_hlID},
637 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200638 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"escape", 2, 2, f_escape},
640 {"eval", 1, 1, f_eval},
641 {"eventhandler", 0, 0, f_eventhandler},
642 {"executable", 1, 1, f_executable},
643 {"execute", 1, 2, f_execute},
644 {"exepath", 1, 1, f_exepath},
645 {"exists", 1, 1, f_exists},
646#ifdef FEAT_FLOAT
647 {"exp", 1, 1, f_exp},
648#endif
649 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200650 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200651 {"extend", 2, 3, f_extend},
652 {"feedkeys", 1, 2, f_feedkeys},
653 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
654 {"filereadable", 1, 1, f_filereadable},
655 {"filewritable", 1, 1, f_filewritable},
656 {"filter", 2, 2, f_filter},
657 {"finddir", 1, 3, f_finddir},
658 {"findfile", 1, 3, f_findfile},
659#ifdef FEAT_FLOAT
660 {"float2nr", 1, 1, f_float2nr},
661 {"floor", 1, 1, f_floor},
662 {"fmod", 2, 2, f_fmod},
663#endif
664 {"fnameescape", 1, 1, f_fnameescape},
665 {"fnamemodify", 2, 2, f_fnamemodify},
666 {"foldclosed", 1, 1, f_foldclosed},
667 {"foldclosedend", 1, 1, f_foldclosedend},
668 {"foldlevel", 1, 1, f_foldlevel},
669 {"foldtext", 0, 0, f_foldtext},
670 {"foldtextresult", 1, 1, f_foldtextresult},
671 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200672 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200673 {"function", 1, 3, f_function},
674 {"garbagecollect", 0, 1, f_garbagecollect},
675 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200676 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200677 {"getbufline", 2, 3, f_getbufline},
678 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100679 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200680 {"getchar", 0, 1, f_getchar},
681 {"getcharmod", 0, 0, f_getcharmod},
682 {"getcharsearch", 0, 0, f_getcharsearch},
683 {"getcmdline", 0, 0, f_getcmdline},
684 {"getcmdpos", 0, 0, f_getcmdpos},
685 {"getcmdtype", 0, 0, f_getcmdtype},
686 {"getcmdwintype", 0, 0, f_getcmdwintype},
687#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200688 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200689#endif
690 {"getcurpos", 0, 0, f_getcurpos},
691 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200692 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200693 {"getfontname", 0, 1, f_getfontname},
694 {"getfperm", 1, 1, f_getfperm},
695 {"getfsize", 1, 1, f_getfsize},
696 {"getftime", 1, 1, f_getftime},
697 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100698 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200699 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200700 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100701 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702 {"getpid", 0, 0, f_getpid},
703 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200704 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200705 {"getreg", 0, 3, f_getreg},
706 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200707 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200708 {"gettabvar", 2, 3, f_gettabvar},
709 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100710 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200711 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100712 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200713 {"getwinposx", 0, 0, f_getwinposx},
714 {"getwinposy", 0, 0, f_getwinposy},
715 {"getwinvar", 2, 3, f_getwinvar},
716 {"glob", 1, 4, f_glob},
717 {"glob2regpat", 1, 1, f_glob2regpat},
718 {"globpath", 2, 5, f_globpath},
719 {"has", 1, 1, f_has},
720 {"has_key", 2, 2, f_has_key},
721 {"haslocaldir", 0, 2, f_haslocaldir},
722 {"hasmapto", 1, 3, f_hasmapto},
723 {"highlightID", 1, 1, f_hlID}, /* obsolete */
724 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
725 {"histadd", 2, 2, f_histadd},
726 {"histdel", 1, 2, f_histdel},
727 {"histget", 1, 2, f_histget},
728 {"histnr", 1, 1, f_histnr},
729 {"hlID", 1, 1, f_hlID},
730 {"hlexists", 1, 1, f_hlexists},
731 {"hostname", 0, 0, f_hostname},
732 {"iconv", 3, 3, f_iconv},
733 {"indent", 1, 1, f_indent},
734 {"index", 2, 4, f_index},
735 {"input", 1, 3, f_input},
736 {"inputdialog", 1, 3, f_inputdialog},
737 {"inputlist", 1, 1, f_inputlist},
738 {"inputrestore", 0, 0, f_inputrestore},
739 {"inputsave", 0, 0, f_inputsave},
740 {"inputsecret", 1, 2, f_inputsecret},
741 {"insert", 2, 3, f_insert},
742 {"invert", 1, 1, f_invert},
743 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200744#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
745 {"isinf", 1, 1, f_isinf},
746#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200747 {"islocked", 1, 1, f_islocked},
748#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
749 {"isnan", 1, 1, f_isnan},
750#endif
751 {"items", 1, 1, f_items},
752#ifdef FEAT_JOB_CHANNEL
753 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200754 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200755 {"job_setoptions", 2, 2, f_job_setoptions},
756 {"job_start", 1, 2, f_job_start},
757 {"job_status", 1, 1, f_job_status},
758 {"job_stop", 1, 2, f_job_stop},
759#endif
760 {"join", 1, 2, f_join},
761 {"js_decode", 1, 1, f_js_decode},
762 {"js_encode", 1, 1, f_js_encode},
763 {"json_decode", 1, 1, f_json_decode},
764 {"json_encode", 1, 1, f_json_encode},
765 {"keys", 1, 1, f_keys},
766 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
767 {"len", 1, 1, f_len},
768 {"libcall", 3, 3, f_libcall},
769 {"libcallnr", 3, 3, f_libcallnr},
770 {"line", 1, 1, f_line},
771 {"line2byte", 1, 1, f_line2byte},
772 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200773 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200774 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200775 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200776 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777 {"localtime", 0, 0, f_localtime},
778#ifdef FEAT_FLOAT
779 {"log", 1, 1, f_log},
780 {"log10", 1, 1, f_log10},
781#endif
782#ifdef FEAT_LUA
783 {"luaeval", 1, 2, f_luaeval},
784#endif
785 {"map", 2, 2, f_map},
786 {"maparg", 1, 4, f_maparg},
787 {"mapcheck", 1, 3, f_mapcheck},
788 {"match", 2, 4, f_match},
789 {"matchadd", 2, 5, f_matchadd},
790 {"matchaddpos", 2, 5, f_matchaddpos},
791 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100792 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793 {"matchend", 2, 4, f_matchend},
794 {"matchlist", 2, 4, f_matchlist},
795 {"matchstr", 2, 4, f_matchstr},
796 {"matchstrpos", 2, 4, f_matchstrpos},
797 {"max", 1, 1, f_max},
798 {"min", 1, 1, f_min},
799#ifdef vim_mkdir
800 {"mkdir", 1, 3, f_mkdir},
801#endif
802 {"mode", 0, 1, f_mode},
803#ifdef FEAT_MZSCHEME
804 {"mzeval", 1, 1, f_mzeval},
805#endif
806 {"nextnonblank", 1, 1, f_nextnonblank},
807 {"nr2char", 1, 2, f_nr2char},
808 {"or", 2, 2, f_or},
809 {"pathshorten", 1, 1, f_pathshorten},
810#ifdef FEAT_PERL
811 {"perleval", 1, 1, f_perleval},
812#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200813#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200814 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200815 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200816 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200817 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200818 {"popup_dialog", 2, 2, f_popup_dialog},
819 {"popup_filter_yesno", 2, 2, f_popup_filter_yesno},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200820 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200821 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200822 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200823 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200824 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200825 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200826#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200827#ifdef FEAT_FLOAT
828 {"pow", 2, 2, f_pow},
829#endif
830 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100831 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200832#ifdef FEAT_JOB_CHANNEL
833 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200834 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200835 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
836#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100837#ifdef FEAT_TEXT_PROP
838 {"prop_add", 3, 3, f_prop_add},
839 {"prop_clear", 1, 3, f_prop_clear},
840 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100841 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100842 {"prop_type_add", 2, 2, f_prop_type_add},
843 {"prop_type_change", 2, 2, f_prop_type_change},
844 {"prop_type_delete", 1, 2, f_prop_type_delete},
845 {"prop_type_get", 1, 2, f_prop_type_get},
846 {"prop_type_list", 0, 1, f_prop_type_list},
847#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200848 {"pumvisible", 0, 0, f_pumvisible},
849#ifdef FEAT_PYTHON3
850 {"py3eval", 1, 1, f_py3eval},
851#endif
852#ifdef FEAT_PYTHON
853 {"pyeval", 1, 1, f_pyeval},
854#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100855#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
856 {"pyxeval", 1, 1, f_pyxeval},
857#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200858 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200859 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200860 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200861 {"reg_executing", 0, 0, f_reg_executing},
862 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200863 {"reltime", 0, 2, f_reltime},
864#ifdef FEAT_FLOAT
865 {"reltimefloat", 1, 1, f_reltimefloat},
866#endif
867 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100868 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200869 {"remote_foreground", 1, 1, f_remote_foreground},
870 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100871 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200872 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100873 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200874 {"remove", 2, 3, f_remove},
875 {"rename", 2, 2, f_rename},
876 {"repeat", 2, 2, f_repeat},
877 {"resolve", 1, 1, f_resolve},
878 {"reverse", 1, 1, f_reverse},
879#ifdef FEAT_FLOAT
880 {"round", 1, 1, f_round},
881#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100882#ifdef FEAT_RUBY
883 {"rubyeval", 1, 1, f_rubyeval},
884#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200885 {"screenattr", 2, 2, f_screenattr},
886 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100887 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200888 {"screencol", 0, 0, f_screencol},
889 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100890 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200891 {"search", 1, 4, f_search},
892 {"searchdecl", 1, 3, f_searchdecl},
893 {"searchpair", 3, 7, f_searchpair},
894 {"searchpairpos", 3, 7, f_searchpairpos},
895 {"searchpos", 1, 4, f_searchpos},
896 {"server2client", 2, 2, f_server2client},
897 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200898 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200899 {"setbufvar", 3, 3, f_setbufvar},
900 {"setcharsearch", 1, 1, f_setcharsearch},
901 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200902 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200903 {"setfperm", 2, 2, f_setfperm},
904 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200905 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100906 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200907 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200908 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200909 {"setreg", 2, 3, f_setreg},
910 {"settabvar", 3, 3, f_settabvar},
911 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100912 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200913 {"setwinvar", 3, 3, f_setwinvar},
914#ifdef FEAT_CRYPT
915 {"sha256", 1, 1, f_sha256},
916#endif
917 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100918 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100919#ifdef FEAT_SIGNS
920 {"sign_define", 1, 2, f_sign_define},
921 {"sign_getdefined", 0, 1, f_sign_getdefined},
922 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100923 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100924 {"sign_place", 4, 5, f_sign_place},
925 {"sign_undefine", 0, 1, f_sign_undefine},
926 {"sign_unplace", 1, 2, f_sign_unplace},
927#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200928 {"simplify", 1, 1, f_simplify},
929#ifdef FEAT_FLOAT
930 {"sin", 1, 1, f_sin},
931 {"sinh", 1, 1, f_sinh},
932#endif
933 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200934#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200935 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200936 {"sound_playevent", 1, 2, f_sound_playevent},
937 {"sound_playfile", 1, 2, f_sound_playfile},
938 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200939#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200940 {"soundfold", 1, 1, f_soundfold},
941 {"spellbadword", 0, 1, f_spellbadword},
942 {"spellsuggest", 1, 3, f_spellsuggest},
943 {"split", 1, 3, f_split},
944#ifdef FEAT_FLOAT
945 {"sqrt", 1, 1, f_sqrt},
946 {"str2float", 1, 1, f_str2float},
947#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200948 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200949 {"str2nr", 1, 2, f_str2nr},
950 {"strcharpart", 2, 3, f_strcharpart},
951 {"strchars", 1, 2, f_strchars},
952 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
953#ifdef HAVE_STRFTIME
954 {"strftime", 1, 2, f_strftime},
955#endif
956 {"strgetchar", 2, 2, f_strgetchar},
957 {"stridx", 2, 3, f_stridx},
958 {"string", 1, 1, f_string},
959 {"strlen", 1, 1, f_strlen},
960 {"strpart", 2, 3, f_strpart},
961 {"strridx", 2, 3, f_strridx},
962 {"strtrans", 1, 1, f_strtrans},
963 {"strwidth", 1, 1, f_strwidth},
964 {"submatch", 1, 2, f_submatch},
965 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200966 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200967 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200968 {"synID", 3, 3, f_synID},
969 {"synIDattr", 2, 3, f_synIDattr},
970 {"synIDtrans", 1, 1, f_synIDtrans},
971 {"synconcealed", 2, 2, f_synconcealed},
972 {"synstack", 2, 2, f_synstack},
973 {"system", 1, 2, f_system},
974 {"systemlist", 1, 2, f_systemlist},
975 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
976 {"tabpagenr", 0, 1, f_tabpagenr},
977 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
978 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100979 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200980#ifdef FEAT_FLOAT
981 {"tan", 1, 1, f_tan},
982 {"tanh", 1, 1, f_tanh},
983#endif
984 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200985#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100986 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
987 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100988 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200989 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200990# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
991 {"term_getansicolors", 1, 1, f_term_getansicolors},
992# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200993 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200994 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200995 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200996 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200997 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200998 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200999 {"term_getstatus", 1, 1, f_term_getstatus},
1000 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001001 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001002 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +02001003 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001004 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001005# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
1006 {"term_setansicolors", 2, 2, f_term_setansicolors},
1007# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001008 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001009 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +02001010 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001011 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +02001012 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001013#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001014 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
1015 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +02001016 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001017 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +02001018 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +01001019 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001020 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001021#ifdef FEAT_JOB_CHANNEL
1022 {"test_null_channel", 0, 0, f_test_null_channel},
1023#endif
1024 {"test_null_dict", 0, 0, f_test_null_dict},
1025#ifdef FEAT_JOB_CHANNEL
1026 {"test_null_job", 0, 0, f_test_null_job},
1027#endif
1028 {"test_null_list", 0, 0, f_test_null_list},
1029 {"test_null_partial", 0, 0, f_test_null_partial},
1030 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001031 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001032 {"test_override", 2, 2, f_test_override},
1033 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001034#ifdef FEAT_GUI
1035 {"test_scrollbar", 3, 3, f_test_scrollbar},
1036#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001037#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001038 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001039#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001040 {"test_settime", 1, 1, f_test_settime},
1041#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001042 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001043 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001044 {"timer_start", 2, 3, f_timer_start},
1045 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001046 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001047#endif
1048 {"tolower", 1, 1, f_tolower},
1049 {"toupper", 1, 1, f_toupper},
1050 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001051 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001052#ifdef FEAT_FLOAT
1053 {"trunc", 1, 1, f_trunc},
1054#endif
1055 {"type", 1, 1, f_type},
1056 {"undofile", 1, 1, f_undofile},
1057 {"undotree", 0, 0, f_undotree},
1058 {"uniq", 1, 3, f_uniq},
1059 {"values", 1, 1, f_values},
1060 {"virtcol", 1, 1, f_virtcol},
1061 {"visualmode", 0, 1, f_visualmode},
1062 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001063 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001064 {"win_findbuf", 1, 1, f_win_findbuf},
1065 {"win_getid", 0, 2, f_win_getid},
1066 {"win_gotoid", 1, 1, f_win_gotoid},
1067 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1068 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001069 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001070 {"winbufnr", 1, 1, f_winbufnr},
1071 {"wincol", 0, 0, f_wincol},
1072 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001073 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001074 {"winline", 0, 0, f_winline},
1075 {"winnr", 0, 1, f_winnr},
1076 {"winrestcmd", 0, 0, f_winrestcmd},
1077 {"winrestview", 1, 1, f_winrestview},
1078 {"winsaveview", 0, 0, f_winsaveview},
1079 {"winwidth", 1, 1, f_winwidth},
1080 {"wordcount", 0, 0, f_wordcount},
1081 {"writefile", 2, 3, f_writefile},
1082 {"xor", 2, 2, f_xor},
1083};
1084
1085#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1086
1087/*
1088 * Function given to ExpandGeneric() to obtain the list of internal
1089 * or user defined function names.
1090 */
1091 char_u *
1092get_function_name(expand_T *xp, int idx)
1093{
1094 static int intidx = -1;
1095 char_u *name;
1096
1097 if (idx == 0)
1098 intidx = -1;
1099 if (intidx < 0)
1100 {
1101 name = get_user_func_name(xp, idx);
1102 if (name != NULL)
1103 return name;
1104 }
1105 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1106 {
1107 STRCPY(IObuff, functions[intidx].f_name);
1108 STRCAT(IObuff, "(");
1109 if (functions[intidx].f_max_argc == 0)
1110 STRCAT(IObuff, ")");
1111 return IObuff;
1112 }
1113
1114 return NULL;
1115}
1116
1117/*
1118 * Function given to ExpandGeneric() to obtain the list of internal or
1119 * user defined variable or function names.
1120 */
1121 char_u *
1122get_expr_name(expand_T *xp, int idx)
1123{
1124 static int intidx = -1;
1125 char_u *name;
1126
1127 if (idx == 0)
1128 intidx = -1;
1129 if (intidx < 0)
1130 {
1131 name = get_function_name(xp, idx);
1132 if (name != NULL)
1133 return name;
1134 }
1135 return get_user_var_name(xp, ++intidx);
1136}
1137
1138#endif /* FEAT_CMDL_COMPL */
1139
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001140/*
1141 * Find internal function in table above.
1142 * Return index, or -1 if not found
1143 */
1144 int
1145find_internal_func(
1146 char_u *name) /* name of the function */
1147{
1148 int first = 0;
1149 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1150 int cmp;
1151 int x;
1152
1153 /*
1154 * Find the function name in the table. Binary search.
1155 */
1156 while (first <= last)
1157 {
1158 x = first + ((unsigned)(last - first) >> 1);
1159 cmp = STRCMP(name, functions[x].f_name);
1160 if (cmp < 0)
1161 last = x - 1;
1162 else if (cmp > 0)
1163 first = x + 1;
1164 else
1165 return x;
1166 }
1167 return -1;
1168}
1169
1170 int
1171call_internal_func(
1172 char_u *name,
1173 int argcount,
1174 typval_T *argvars,
1175 typval_T *rettv)
1176{
1177 int i;
1178
1179 i = find_internal_func(name);
1180 if (i < 0)
1181 return ERROR_UNKNOWN;
1182 if (argcount < functions[i].f_min_argc)
1183 return ERROR_TOOFEW;
1184 if (argcount > functions[i].f_max_argc)
1185 return ERROR_TOOMANY;
1186 argvars[argcount].v_type = VAR_UNKNOWN;
1187 functions[i].f_func(argvars, rettv);
1188 return ERROR_NONE;
1189}
1190
1191/*
1192 * Return TRUE for a non-zero Number and a non-empty String.
1193 */
1194 static int
1195non_zero_arg(typval_T *argvars)
1196{
1197 return ((argvars[0].v_type == VAR_NUMBER
1198 && argvars[0].vval.v_number != 0)
1199 || (argvars[0].v_type == VAR_SPECIAL
1200 && argvars[0].vval.v_number == VVAL_TRUE)
1201 || (argvars[0].v_type == VAR_STRING
1202 && argvars[0].vval.v_string != NULL
1203 && *argvars[0].vval.v_string != NUL));
1204}
1205
1206/*
1207 * Get the lnum from the first argument.
1208 * Also accepts ".", "$", etc., but that only works for the current buffer.
1209 * Returns -1 on error.
1210 */
1211 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001212tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001213{
1214 typval_T rettv;
1215 linenr_T lnum;
1216
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001217 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001218 if (lnum == 0) /* no valid number, try using line() */
1219 {
1220 rettv.v_type = VAR_NUMBER;
1221 f_line(argvars, &rettv);
1222 lnum = (linenr_T)rettv.vval.v_number;
1223 clear_tv(&rettv);
1224 }
1225 return lnum;
1226}
1227
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001228/*
1229 * Get the lnum from the first argument.
1230 * Also accepts "$", then "buf" is used.
1231 * Returns 0 on error.
1232 */
1233 static linenr_T
1234tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1235{
1236 if (argvars[0].v_type == VAR_STRING
1237 && argvars[0].vval.v_string != NULL
1238 && argvars[0].vval.v_string[0] == '$'
1239 && buf != NULL)
1240 return buf->b_ml.ml_line_count;
1241 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1242}
1243
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001244#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001245/*
1246 * Get the float value of "argvars[0]" into "f".
1247 * Returns FAIL when the argument is not a Number or Float.
1248 */
1249 static int
1250get_float_arg(typval_T *argvars, float_T *f)
1251{
1252 if (argvars[0].v_type == VAR_FLOAT)
1253 {
1254 *f = argvars[0].vval.v_float;
1255 return OK;
1256 }
1257 if (argvars[0].v_type == VAR_NUMBER)
1258 {
1259 *f = (float_T)argvars[0].vval.v_number;
1260 return OK;
1261 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001262 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001263 return FAIL;
1264}
1265
1266/*
1267 * "abs(expr)" function
1268 */
1269 static void
1270f_abs(typval_T *argvars, typval_T *rettv)
1271{
1272 if (argvars[0].v_type == VAR_FLOAT)
1273 {
1274 rettv->v_type = VAR_FLOAT;
1275 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1276 }
1277 else
1278 {
1279 varnumber_T n;
1280 int error = FALSE;
1281
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001282 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001283 if (error)
1284 rettv->vval.v_number = -1;
1285 else if (n > 0)
1286 rettv->vval.v_number = n;
1287 else
1288 rettv->vval.v_number = -n;
1289 }
1290}
1291
1292/*
1293 * "acos()" function
1294 */
1295 static void
1296f_acos(typval_T *argvars, typval_T *rettv)
1297{
1298 float_T f = 0.0;
1299
1300 rettv->v_type = VAR_FLOAT;
1301 if (get_float_arg(argvars, &f) == OK)
1302 rettv->vval.v_float = acos(f);
1303 else
1304 rettv->vval.v_float = 0.0;
1305}
1306#endif
1307
1308/*
1309 * "add(list, item)" function
1310 */
1311 static void
1312f_add(typval_T *argvars, typval_T *rettv)
1313{
1314 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001315 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001316
1317 rettv->vval.v_number = 1; /* Default: Failed */
1318 if (argvars[0].v_type == VAR_LIST)
1319 {
1320 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001321 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001322 (char_u *)N_("add() argument"), TRUE)
1323 && list_append_tv(l, &argvars[1]) == OK)
1324 copy_tv(&argvars[0], rettv);
1325 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001326 else if (argvars[0].v_type == VAR_BLOB)
1327 {
1328 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001329 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001330 (char_u *)N_("add() argument"), TRUE))
1331 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001332 int error = FALSE;
1333 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1334
1335 if (!error)
1336 {
1337 ga_append(&b->bv_ga, (int)n);
1338 copy_tv(&argvars[0], rettv);
1339 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001340 }
1341 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001342 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001343 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001344}
1345
1346/*
1347 * "and(expr, expr)" function
1348 */
1349 static void
1350f_and(typval_T *argvars, typval_T *rettv)
1351{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001352 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1353 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001354}
1355
1356/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001357 * If there is a window for "curbuf", make it the current window.
1358 */
1359 static void
1360find_win_for_curbuf(void)
1361{
1362 wininfo_T *wip;
1363
1364 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1365 {
1366 if (wip->wi_win != NULL)
1367 {
1368 curwin = wip->wi_win;
1369 break;
1370 }
1371 }
1372}
1373
1374/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001375 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001376 */
1377 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001378set_buffer_lines(
1379 buf_T *buf,
1380 linenr_T lnum_arg,
1381 int append,
1382 typval_T *lines,
1383 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001384{
Bram Moolenaarca851592018-06-06 21:04:07 +02001385 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1386 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001387 list_T *l = NULL;
1388 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001389 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001390 linenr_T append_lnum;
1391 buf_T *curbuf_save = NULL;
1392 win_T *curwin_save = NULL;
1393 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001394
Bram Moolenaarca851592018-06-06 21:04:07 +02001395 /* When using the current buffer ml_mfp will be set if needed. Useful when
1396 * setline() is used on startup. For other buffers the buffer must be
1397 * loaded. */
1398 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001399 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001400 rettv->vval.v_number = 1; /* FAIL */
1401 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001402 }
1403
Bram Moolenaarca851592018-06-06 21:04:07 +02001404 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001405 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001406 curbuf_save = curbuf;
1407 curwin_save = curwin;
1408 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001409 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001410 }
1411
1412 if (append)
1413 // appendbufline() uses the line number below which we insert
1414 append_lnum = lnum - 1;
1415 else
1416 // setbufline() uses the line number above which we insert, we only
1417 // append if it's below the last line
1418 append_lnum = curbuf->b_ml.ml_line_count;
1419
1420 if (lines->v_type == VAR_LIST)
1421 {
1422 l = lines->vval.v_list;
1423 li = l->lv_first;
1424 }
1425 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001426 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001427
1428 /* default result is zero == OK */
1429 for (;;)
1430 {
1431 if (l != NULL)
1432 {
1433 /* list argument, get next string */
1434 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001435 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001436 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001437 li = li->li_next;
1438 }
1439
Bram Moolenaarca851592018-06-06 21:04:07 +02001440 rettv->vval.v_number = 1; /* FAIL */
1441 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1442 break;
1443
1444 /* When coming here from Insert mode, sync undo, so that this can be
1445 * undone separately from what was previously inserted. */
1446 if (u_sync_once == 2)
1447 {
1448 u_sync_once = 1; /* notify that u_sync() was called */
1449 u_sync(TRUE);
1450 }
1451
1452 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1453 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001454 // Existing line, replace it.
1455 // Removes any existing text properties.
1456 if (u_savesub(lnum) == OK && ml_replace_len(
1457 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001458 {
1459 changed_bytes(lnum, 0);
1460 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1461 check_cursor_col();
1462 rettv->vval.v_number = 0; /* OK */
1463 }
1464 }
1465 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1466 {
1467 /* append the line */
1468 ++added;
1469 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1470 rettv->vval.v_number = 0; /* OK */
1471 }
1472
1473 if (l == NULL) /* only one string argument */
1474 break;
1475 ++lnum;
1476 }
1477
1478 if (added > 0)
1479 {
1480 win_T *wp;
1481 tabpage_T *tp;
1482
1483 appended_lines_mark(append_lnum, added);
1484 FOR_ALL_TAB_WINDOWS(tp, wp)
1485 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1486 wp->w_cursor.lnum += added;
1487 check_cursor_col();
1488
Bram Moolenaarf2732452018-06-03 14:47:35 +02001489#ifdef FEAT_JOB_CHANNEL
1490 if (bt_prompt(curbuf) && (State & INSERT))
1491 // show the line with the prompt
1492 update_topline();
1493#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001494 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001495
1496 if (!is_curbuf)
1497 {
1498 curbuf = curbuf_save;
1499 curwin = curwin_save;
1500 }
1501}
1502
1503/*
1504 * "append(lnum, string/list)" function
1505 */
1506 static void
1507f_append(typval_T *argvars, typval_T *rettv)
1508{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001509 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001510
1511 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1512}
1513
1514/*
1515 * "appendbufline(buf, lnum, string/list)" function
1516 */
1517 static void
1518f_appendbufline(typval_T *argvars, typval_T *rettv)
1519{
1520 linenr_T lnum;
1521 buf_T *buf;
1522
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001523 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001524 if (buf == NULL)
1525 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001526 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001527 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001528 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001529 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1530 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001531}
1532
1533/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001534 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001535 */
1536 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001537f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001538{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001539 win_T *wp;
1540
1541 if (argvars[0].v_type == VAR_UNKNOWN)
1542 // use the current window
1543 rettv->vval.v_number = ARGCOUNT;
1544 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001545 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001546 // use the global argument list
1547 rettv->vval.v_number = GARGCOUNT;
1548 else
1549 {
1550 // use the argument list of the specified window
1551 wp = find_win_by_nr_or_id(&argvars[0]);
1552 if (wp != NULL)
1553 rettv->vval.v_number = WARGCOUNT(wp);
1554 else
1555 rettv->vval.v_number = -1;
1556 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001557}
1558
1559/*
1560 * "argidx()" function
1561 */
1562 static void
1563f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1564{
1565 rettv->vval.v_number = curwin->w_arg_idx;
1566}
1567
1568/*
1569 * "arglistid()" function
1570 */
1571 static void
1572f_arglistid(typval_T *argvars, typval_T *rettv)
1573{
1574 win_T *wp;
1575
1576 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001577 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001578 if (wp != NULL)
1579 rettv->vval.v_number = wp->w_alist->id;
1580}
1581
1582/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001583 * Get the argument list for a given window
1584 */
1585 static void
1586get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1587{
1588 int idx;
1589
1590 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1591 for (idx = 0; idx < argcount; ++idx)
1592 list_append_string(rettv->vval.v_list,
1593 alist_name(&arglist[idx]), -1);
1594}
1595
1596/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001597 * "argv(nr)" function
1598 */
1599 static void
1600f_argv(typval_T *argvars, typval_T *rettv)
1601{
1602 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001603 aentry_T *arglist = NULL;
1604 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605
1606 if (argvars[0].v_type != VAR_UNKNOWN)
1607 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001608 if (argvars[1].v_type == VAR_UNKNOWN)
1609 {
1610 arglist = ARGLIST;
1611 argcount = ARGCOUNT;
1612 }
1613 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001614 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001615 {
1616 arglist = GARGLIST;
1617 argcount = GARGCOUNT;
1618 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001619 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001620 {
1621 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1622
1623 if (wp != NULL)
1624 {
1625 /* Use the argument list of the specified window */
1626 arglist = WARGLIST(wp);
1627 argcount = WARGCOUNT(wp);
1628 }
1629 }
1630
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001631 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001632 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001633 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001634 if (arglist != NULL && idx >= 0 && idx < argcount)
1635 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1636 else if (idx == -1)
1637 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001638 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001639 else
1640 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641}
1642
1643/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001644 * "assert_beeps(cmd [, error])" function
1645 */
1646 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001647f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001648{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001649 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001650}
1651
1652/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001653 * "assert_equal(expected, actual[, msg])" function
1654 */
1655 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001656f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001658 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659}
1660
1661/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001662 * "assert_equalfile(fname-one, fname-two)" function
1663 */
1664 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001665f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001666{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001667 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001668}
1669
1670/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001671 * "assert_notequal(expected, actual[, msg])" function
1672 */
1673 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001674f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001675{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001676 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001677}
1678
1679/*
1680 * "assert_exception(string[, msg])" function
1681 */
1682 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001683f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001684{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001685 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001686}
1687
1688/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001689 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001690 */
1691 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001692f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001693{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001694 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001695}
1696
1697/*
1698 * "assert_false(actual[, msg])" function
1699 */
1700 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001701f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001702{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001703 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001704}
1705
1706/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001707 * "assert_inrange(lower, upper[, msg])" function
1708 */
1709 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001710f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001711{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001712 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001713}
1714
1715/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001716 * "assert_match(pattern, actual[, msg])" function
1717 */
1718 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001719f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001720{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001721 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001722}
1723
1724/*
1725 * "assert_notmatch(pattern, actual[, msg])" function
1726 */
1727 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001728f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001729{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001730 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001731}
1732
1733/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001734 * "assert_report(msg)" function
1735 */
1736 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001737f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001738{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001739 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001740}
1741
1742/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001743 * "assert_true(actual[, msg])" function
1744 */
1745 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001746f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001747{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001748 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001749}
1750
1751#ifdef FEAT_FLOAT
1752/*
1753 * "asin()" function
1754 */
1755 static void
1756f_asin(typval_T *argvars, typval_T *rettv)
1757{
1758 float_T f = 0.0;
1759
1760 rettv->v_type = VAR_FLOAT;
1761 if (get_float_arg(argvars, &f) == OK)
1762 rettv->vval.v_float = asin(f);
1763 else
1764 rettv->vval.v_float = 0.0;
1765}
1766
1767/*
1768 * "atan()" function
1769 */
1770 static void
1771f_atan(typval_T *argvars, typval_T *rettv)
1772{
1773 float_T f = 0.0;
1774
1775 rettv->v_type = VAR_FLOAT;
1776 if (get_float_arg(argvars, &f) == OK)
1777 rettv->vval.v_float = atan(f);
1778 else
1779 rettv->vval.v_float = 0.0;
1780}
1781
1782/*
1783 * "atan2()" function
1784 */
1785 static void
1786f_atan2(typval_T *argvars, typval_T *rettv)
1787{
1788 float_T fx = 0.0, fy = 0.0;
1789
1790 rettv->v_type = VAR_FLOAT;
1791 if (get_float_arg(argvars, &fx) == OK
1792 && get_float_arg(&argvars[1], &fy) == OK)
1793 rettv->vval.v_float = atan2(fx, fy);
1794 else
1795 rettv->vval.v_float = 0.0;
1796}
1797#endif
1798
1799/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001800 * "balloon_show()" function
1801 */
1802#ifdef FEAT_BEVAL
1803 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001804f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1805{
1806 rettv->v_type = VAR_STRING;
1807 if (balloonEval != NULL)
1808 {
1809 if (balloonEval->msg == NULL)
1810 rettv->vval.v_string = NULL;
1811 else
1812 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1813 }
1814}
1815
1816 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001817f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1818{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001819 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001820 {
1821 if (argvars[0].v_type == VAR_LIST
1822# ifdef FEAT_GUI
1823 && !gui.in_use
1824# endif
1825 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001826 {
1827 list_T *l = argvars[0].vval.v_list;
1828
1829 // empty list removes the balloon
1830 post_balloon(balloonEval, NULL,
1831 l == NULL || l->lv_len == 0 ? NULL : l);
1832 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001833 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001834 {
1835 char_u *mesg = tv_get_string_chk(&argvars[0]);
1836
1837 if (mesg != NULL)
1838 // empty string removes the balloon
1839 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1840 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001841 }
1842}
1843
Bram Moolenaar669a8282017-11-19 20:13:05 +01001844# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001845 static void
1846f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1847{
1848 if (rettv_list_alloc(rettv) == OK)
1849 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001850 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001851
1852 if (msg != NULL)
1853 {
1854 pumitem_T *array;
1855 int size = split_message(msg, &array);
1856 int i;
1857
1858 /* Skip the first and last item, they are always empty. */
1859 for (i = 1; i < size - 1; ++i)
1860 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001861 while (size > 0)
1862 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001863 vim_free(array);
1864 }
1865 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001866}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001867# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001868#endif
1869
1870/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001871 * "browse(save, title, initdir, default)" function
1872 */
1873 static void
1874f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1875{
1876#ifdef FEAT_BROWSE
1877 int save;
1878 char_u *title;
1879 char_u *initdir;
1880 char_u *defname;
1881 char_u buf[NUMBUFLEN];
1882 char_u buf2[NUMBUFLEN];
1883 int error = FALSE;
1884
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001885 save = (int)tv_get_number_chk(&argvars[0], &error);
1886 title = tv_get_string_chk(&argvars[1]);
1887 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1888 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001889
1890 if (error || title == NULL || initdir == NULL || defname == NULL)
1891 rettv->vval.v_string = NULL;
1892 else
1893 rettv->vval.v_string =
1894 do_browse(save ? BROWSE_SAVE : 0,
1895 title, defname, NULL, initdir, NULL, curbuf);
1896#else
1897 rettv->vval.v_string = NULL;
1898#endif
1899 rettv->v_type = VAR_STRING;
1900}
1901
1902/*
1903 * "browsedir(title, initdir)" function
1904 */
1905 static void
1906f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1907{
1908#ifdef FEAT_BROWSE
1909 char_u *title;
1910 char_u *initdir;
1911 char_u buf[NUMBUFLEN];
1912
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001913 title = tv_get_string_chk(&argvars[0]);
1914 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915
1916 if (title == NULL || initdir == NULL)
1917 rettv->vval.v_string = NULL;
1918 else
1919 rettv->vval.v_string = do_browse(BROWSE_DIR,
1920 title, NULL, NULL, initdir, NULL, curbuf);
1921#else
1922 rettv->vval.v_string = NULL;
1923#endif
1924 rettv->v_type = VAR_STRING;
1925}
1926
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927/*
1928 * Find a buffer by number or exact name.
1929 */
1930 static buf_T *
1931find_buffer(typval_T *avar)
1932{
1933 buf_T *buf = NULL;
1934
1935 if (avar->v_type == VAR_NUMBER)
1936 buf = buflist_findnr((int)avar->vval.v_number);
1937 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1938 {
1939 buf = buflist_findname_exp(avar->vval.v_string);
1940 if (buf == NULL)
1941 {
1942 /* No full path name match, try a match with a URL or a "nofile"
1943 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001944 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001945 if (buf->b_fname != NULL
1946 && (path_with_url(buf->b_fname)
1947#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001948 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001949#endif
1950 )
1951 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1952 break;
1953 }
1954 }
1955 return buf;
1956}
1957
1958/*
1959 * "bufexists(expr)" function
1960 */
1961 static void
1962f_bufexists(typval_T *argvars, typval_T *rettv)
1963{
1964 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1965}
1966
1967/*
1968 * "buflisted(expr)" function
1969 */
1970 static void
1971f_buflisted(typval_T *argvars, typval_T *rettv)
1972{
1973 buf_T *buf;
1974
1975 buf = find_buffer(&argvars[0]);
1976 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1977}
1978
1979/*
1980 * "bufloaded(expr)" function
1981 */
1982 static void
1983f_bufloaded(typval_T *argvars, typval_T *rettv)
1984{
1985 buf_T *buf;
1986
1987 buf = find_buffer(&argvars[0]);
1988 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1989}
1990
1991 buf_T *
1992buflist_find_by_name(char_u *name, int curtab_only)
1993{
1994 int save_magic;
1995 char_u *save_cpo;
1996 buf_T *buf;
1997
1998 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1999 save_magic = p_magic;
2000 p_magic = TRUE;
2001 save_cpo = p_cpo;
2002 p_cpo = (char_u *)"";
2003
2004 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
2005 TRUE, FALSE, curtab_only));
2006
2007 p_magic = save_magic;
2008 p_cpo = save_cpo;
2009 return buf;
2010}
2011
2012/*
2013 * Get buffer by number or pattern.
2014 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002015 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002016tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002017{
2018 char_u *name = tv->vval.v_string;
2019 buf_T *buf;
2020
2021 if (tv->v_type == VAR_NUMBER)
2022 return buflist_findnr((int)tv->vval.v_number);
2023 if (tv->v_type != VAR_STRING)
2024 return NULL;
2025 if (name == NULL || *name == NUL)
2026 return curbuf;
2027 if (name[0] == '$' && name[1] == NUL)
2028 return lastbuf;
2029
2030 buf = buflist_find_by_name(name, curtab_only);
2031
2032 /* If not found, try expanding the name, like done for bufexists(). */
2033 if (buf == NULL)
2034 buf = find_buffer(tv);
2035
2036 return buf;
2037}
2038
2039/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002040 * Get the buffer from "arg" and give an error and return NULL if it is not
2041 * valid.
2042 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002043 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002044get_buf_arg(typval_T *arg)
2045{
2046 buf_T *buf;
2047
2048 ++emsg_off;
2049 buf = tv_get_buf(arg, FALSE);
2050 --emsg_off;
2051 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002052 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002053 return buf;
2054}
2055
2056/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002057 * "bufname(expr)" function
2058 */
2059 static void
2060f_bufname(typval_T *argvars, typval_T *rettv)
2061{
2062 buf_T *buf;
2063
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002064 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002065 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002066 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002067 rettv->v_type = VAR_STRING;
2068 if (buf != NULL && buf->b_fname != NULL)
2069 rettv->vval.v_string = vim_strsave(buf->b_fname);
2070 else
2071 rettv->vval.v_string = NULL;
2072 --emsg_off;
2073}
2074
2075/*
2076 * "bufnr(expr)" function
2077 */
2078 static void
2079f_bufnr(typval_T *argvars, typval_T *rettv)
2080{
2081 buf_T *buf;
2082 int error = FALSE;
2083 char_u *name;
2084
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002085 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002086 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002087 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002088 --emsg_off;
2089
2090 /* If the buffer isn't found and the second argument is not zero create a
2091 * new buffer. */
2092 if (buf == NULL
2093 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002094 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002095 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002096 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002097 && !error)
2098 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2099
2100 if (buf != NULL)
2101 rettv->vval.v_number = buf->b_fnum;
2102 else
2103 rettv->vval.v_number = -1;
2104}
2105
2106 static void
2107buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2108{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002109 win_T *wp;
2110 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002111 buf_T *buf;
2112
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002113 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002114 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002115 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002116 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002117 {
2118 ++winnr;
2119 if (wp->w_buffer == buf)
2120 break;
2121 }
2122 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002123 --emsg_off;
2124}
2125
2126/*
2127 * "bufwinid(nr)" function
2128 */
2129 static void
2130f_bufwinid(typval_T *argvars, typval_T *rettv)
2131{
2132 buf_win_common(argvars, rettv, FALSE);
2133}
2134
2135/*
2136 * "bufwinnr(nr)" function
2137 */
2138 static void
2139f_bufwinnr(typval_T *argvars, typval_T *rettv)
2140{
2141 buf_win_common(argvars, rettv, TRUE);
2142}
2143
2144/*
2145 * "byte2line(byte)" function
2146 */
2147 static void
2148f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2149{
2150#ifndef FEAT_BYTEOFF
2151 rettv->vval.v_number = -1;
2152#else
2153 long boff = 0;
2154
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002155 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002156 if (boff < 0)
2157 rettv->vval.v_number = -1;
2158 else
2159 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2160 (linenr_T)0, &boff);
2161#endif
2162}
2163
2164 static void
2165byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2166{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002167 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002168 char_u *str;
2169 varnumber_T idx;
2170
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002171 str = tv_get_string_chk(&argvars[0]);
2172 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002173 rettv->vval.v_number = -1;
2174 if (str == NULL || idx < 0)
2175 return;
2176
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002177 t = str;
2178 for ( ; idx > 0; idx--)
2179 {
2180 if (*t == NUL) /* EOL reached */
2181 return;
2182 if (enc_utf8 && comp)
2183 t += utf_ptr2len(t);
2184 else
2185 t += (*mb_ptr2len)(t);
2186 }
2187 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002188}
2189
2190/*
2191 * "byteidx()" function
2192 */
2193 static void
2194f_byteidx(typval_T *argvars, typval_T *rettv)
2195{
2196 byteidx(argvars, rettv, FALSE);
2197}
2198
2199/*
2200 * "byteidxcomp()" function
2201 */
2202 static void
2203f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2204{
2205 byteidx(argvars, rettv, TRUE);
2206}
2207
2208/*
2209 * "call(func, arglist [, dict])" function
2210 */
2211 static void
2212f_call(typval_T *argvars, typval_T *rettv)
2213{
2214 char_u *func;
2215 partial_T *partial = NULL;
2216 dict_T *selfdict = NULL;
2217
2218 if (argvars[1].v_type != VAR_LIST)
2219 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002220 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002221 return;
2222 }
2223 if (argvars[1].vval.v_list == NULL)
2224 return;
2225
2226 if (argvars[0].v_type == VAR_FUNC)
2227 func = argvars[0].vval.v_string;
2228 else if (argvars[0].v_type == VAR_PARTIAL)
2229 {
2230 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002231 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002232 }
2233 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002234 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002235 if (*func == NUL)
2236 return; /* type error or empty name */
2237
2238 if (argvars[2].v_type != VAR_UNKNOWN)
2239 {
2240 if (argvars[2].v_type != VAR_DICT)
2241 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002242 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002243 return;
2244 }
2245 selfdict = argvars[2].vval.v_dict;
2246 }
2247
2248 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2249}
2250
2251#ifdef FEAT_FLOAT
2252/*
2253 * "ceil({float})" function
2254 */
2255 static void
2256f_ceil(typval_T *argvars, typval_T *rettv)
2257{
2258 float_T f = 0.0;
2259
2260 rettv->v_type = VAR_FLOAT;
2261 if (get_float_arg(argvars, &f) == OK)
2262 rettv->vval.v_float = ceil(f);
2263 else
2264 rettv->vval.v_float = 0.0;
2265}
2266#endif
2267
2268#ifdef FEAT_JOB_CHANNEL
2269/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002270 * "ch_canread()" function
2271 */
2272 static void
2273f_ch_canread(typval_T *argvars, typval_T *rettv)
2274{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002275 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002276
2277 rettv->vval.v_number = 0;
2278 if (channel != NULL)
2279 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2280 || channel_has_readahead(channel, PART_OUT)
2281 || channel_has_readahead(channel, PART_ERR);
2282}
2283
2284/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002285 * "ch_close()" function
2286 */
2287 static void
2288f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2289{
2290 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2291
2292 if (channel != NULL)
2293 {
2294 channel_close(channel, FALSE);
2295 channel_clear(channel);
2296 }
2297}
2298
2299/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002300 * "ch_close()" function
2301 */
2302 static void
2303f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2304{
2305 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2306
2307 if (channel != NULL)
2308 channel_close_in(channel);
2309}
2310
2311/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002312 * "ch_getbufnr()" function
2313 */
2314 static void
2315f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2316{
2317 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2318
2319 rettv->vval.v_number = -1;
2320 if (channel != NULL)
2321 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002322 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002323 int part;
2324
2325 if (STRCMP(what, "err") == 0)
2326 part = PART_ERR;
2327 else if (STRCMP(what, "out") == 0)
2328 part = PART_OUT;
2329 else if (STRCMP(what, "in") == 0)
2330 part = PART_IN;
2331 else
2332 part = PART_SOCK;
2333 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2334 rettv->vval.v_number =
2335 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2336 }
2337}
2338
2339/*
2340 * "ch_getjob()" function
2341 */
2342 static void
2343f_ch_getjob(typval_T *argvars, typval_T *rettv)
2344{
2345 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2346
2347 if (channel != NULL)
2348 {
2349 rettv->v_type = VAR_JOB;
2350 rettv->vval.v_job = channel->ch_job;
2351 if (channel->ch_job != NULL)
2352 ++channel->ch_job->jv_refcount;
2353 }
2354}
2355
2356/*
2357 * "ch_info()" function
2358 */
2359 static void
2360f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2361{
2362 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2363
2364 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2365 channel_info(channel, rettv->vval.v_dict);
2366}
2367
2368/*
2369 * "ch_log()" function
2370 */
2371 static void
2372f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2373{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002374 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002375 channel_T *channel = NULL;
2376
2377 if (argvars[1].v_type != VAR_UNKNOWN)
2378 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2379
Bram Moolenaard5359b22018-04-05 22:44:39 +02002380 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002381}
2382
2383/*
2384 * "ch_logfile()" function
2385 */
2386 static void
2387f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2388{
2389 char_u *fname;
2390 char_u *opt = (char_u *)"";
2391 char_u buf[NUMBUFLEN];
2392
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002393 /* Don't open a file in restricted mode. */
2394 if (check_restricted() || check_secure())
2395 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002396 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002397 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002398 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002399 ch_logfile(fname, opt);
2400}
2401
2402/*
2403 * "ch_open()" function
2404 */
2405 static void
2406f_ch_open(typval_T *argvars, typval_T *rettv)
2407{
2408 rettv->v_type = VAR_CHANNEL;
2409 if (check_restricted() || check_secure())
2410 return;
2411 rettv->vval.v_channel = channel_open_func(argvars);
2412}
2413
2414/*
2415 * "ch_read()" function
2416 */
2417 static void
2418f_ch_read(typval_T *argvars, typval_T *rettv)
2419{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002420 common_channel_read(argvars, rettv, FALSE, FALSE);
2421}
2422
2423/*
2424 * "ch_readblob()" function
2425 */
2426 static void
2427f_ch_readblob(typval_T *argvars, typval_T *rettv)
2428{
2429 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002430}
2431
2432/*
2433 * "ch_readraw()" function
2434 */
2435 static void
2436f_ch_readraw(typval_T *argvars, typval_T *rettv)
2437{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002438 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002439}
2440
2441/*
2442 * "ch_evalexpr()" function
2443 */
2444 static void
2445f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2446{
2447 ch_expr_common(argvars, rettv, TRUE);
2448}
2449
2450/*
2451 * "ch_sendexpr()" function
2452 */
2453 static void
2454f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2455{
2456 ch_expr_common(argvars, rettv, FALSE);
2457}
2458
2459/*
2460 * "ch_evalraw()" function
2461 */
2462 static void
2463f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2464{
2465 ch_raw_common(argvars, rettv, TRUE);
2466}
2467
2468/*
2469 * "ch_sendraw()" function
2470 */
2471 static void
2472f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2473{
2474 ch_raw_common(argvars, rettv, FALSE);
2475}
2476
2477/*
2478 * "ch_setoptions()" function
2479 */
2480 static void
2481f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2482{
2483 channel_T *channel;
2484 jobopt_T opt;
2485
2486 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2487 if (channel == NULL)
2488 return;
2489 clear_job_options(&opt);
2490 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002491 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002492 channel_set_options(channel, &opt);
2493 free_job_options(&opt);
2494}
2495
2496/*
2497 * "ch_status()" function
2498 */
2499 static void
2500f_ch_status(typval_T *argvars, typval_T *rettv)
2501{
2502 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002503 jobopt_T opt;
2504 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002505
2506 /* return an empty string by default */
2507 rettv->v_type = VAR_STRING;
2508 rettv->vval.v_string = NULL;
2509
2510 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002511
2512 if (argvars[1].v_type != VAR_UNKNOWN)
2513 {
2514 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002515 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002516 && (opt.jo_set & JO_PART))
2517 part = opt.jo_part;
2518 }
2519
2520 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002521}
2522#endif
2523
2524/*
2525 * "changenr()" function
2526 */
2527 static void
2528f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2529{
2530 rettv->vval.v_number = curbuf->b_u_seq_cur;
2531}
2532
2533/*
2534 * "char2nr(string)" function
2535 */
2536 static void
2537f_char2nr(typval_T *argvars, typval_T *rettv)
2538{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002539 if (has_mbyte)
2540 {
2541 int utf8 = 0;
2542
2543 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002544 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002545
2546 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002547 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002548 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002549 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002550 }
2551 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002552 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002553}
2554
2555/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002556 * "chdir(dir)" function
2557 */
2558 static void
2559f_chdir(typval_T *argvars, typval_T *rettv)
2560{
2561 char_u *cwd;
2562 cdscope_T scope = CDSCOPE_GLOBAL;
2563
2564 rettv->v_type = VAR_STRING;
2565 rettv->vval.v_string = NULL;
2566
2567 if (argvars[0].v_type != VAR_STRING)
2568 return;
2569
2570 // Return the current directory
2571 cwd = alloc(MAXPATHL);
2572 if (cwd != NULL)
2573 {
2574 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2575 {
2576#ifdef BACKSLASH_IN_FILENAME
2577 slash_adjust(cwd);
2578#endif
2579 rettv->vval.v_string = vim_strsave(cwd);
2580 }
2581 vim_free(cwd);
2582 }
2583
2584 if (curwin->w_localdir != NULL)
2585 scope = CDSCOPE_WINDOW;
2586 else if (curtab->tp_localdir != NULL)
2587 scope = CDSCOPE_TABPAGE;
2588
2589 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2590 // Directory change failed
2591 VIM_CLEAR(rettv->vval.v_string);
2592}
2593
2594/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002595 * "cindent(lnum)" function
2596 */
2597 static void
2598f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2599{
2600#ifdef FEAT_CINDENT
2601 pos_T pos;
2602 linenr_T lnum;
2603
2604 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002605 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002606 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2607 {
2608 curwin->w_cursor.lnum = lnum;
2609 rettv->vval.v_number = get_c_indent();
2610 curwin->w_cursor = pos;
2611 }
2612 else
2613#endif
2614 rettv->vval.v_number = -1;
2615}
2616
Bram Moolenaaraff74912019-03-30 18:11:49 +01002617 static win_T *
2618get_optional_window(typval_T *argvars, int idx)
2619{
2620 win_T *win = curwin;
2621
2622 if (argvars[idx].v_type != VAR_UNKNOWN)
2623 {
2624 win = find_win_by_nr_or_id(&argvars[idx]);
2625 if (win == NULL)
2626 {
2627 emsg(_(e_invalwindow));
2628 return NULL;
2629 }
2630 }
2631 return win;
2632}
2633
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002634/*
2635 * "clearmatches()" function
2636 */
2637 static void
2638f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2639{
2640#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002641 win_T *win = get_optional_window(argvars, 0);
2642
2643 if (win != NULL)
2644 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002645#endif
2646}
2647
2648/*
2649 * "col(string)" function
2650 */
2651 static void
2652f_col(typval_T *argvars, typval_T *rettv)
2653{
2654 colnr_T col = 0;
2655 pos_T *fp;
2656 int fnum = curbuf->b_fnum;
2657
2658 fp = var2fpos(&argvars[0], FALSE, &fnum);
2659 if (fp != NULL && fnum == curbuf->b_fnum)
2660 {
2661 if (fp->col == MAXCOL)
2662 {
2663 /* '> can be MAXCOL, get the length of the line then */
2664 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2665 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2666 else
2667 col = MAXCOL;
2668 }
2669 else
2670 {
2671 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002672 /* col(".") when the cursor is on the NUL at the end of the line
2673 * because of "coladd" can be seen as an extra column. */
2674 if (virtual_active() && fp == &curwin->w_cursor)
2675 {
2676 char_u *p = ml_get_cursor();
2677
2678 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2679 curwin->w_virtcol - curwin->w_cursor.coladd))
2680 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002681 int l;
2682
2683 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2684 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002685 }
2686 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687 }
2688 }
2689 rettv->vval.v_number = col;
2690}
2691
2692#if defined(FEAT_INS_EXPAND)
2693/*
2694 * "complete()" function
2695 */
2696 static void
2697f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2698{
2699 int startcol;
2700
2701 if ((State & INSERT) == 0)
2702 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002703 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002704 return;
2705 }
2706
2707 /* Check for undo allowed here, because if something was already inserted
2708 * the line was already saved for undo and this check isn't done. */
2709 if (!undo_allowed())
2710 return;
2711
2712 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2713 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002714 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002715 return;
2716 }
2717
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002718 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002719 if (startcol <= 0)
2720 return;
2721
2722 set_completion(startcol - 1, argvars[1].vval.v_list);
2723}
2724
2725/*
2726 * "complete_add()" function
2727 */
2728 static void
2729f_complete_add(typval_T *argvars, typval_T *rettv)
2730{
2731 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2732}
2733
2734/*
2735 * "complete_check()" function
2736 */
2737 static void
2738f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2739{
2740 int saved = RedrawingDisabled;
2741
2742 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002743 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002744 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002745 RedrawingDisabled = saved;
2746}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002747
2748/*
2749 * "complete_info()" function
2750 */
2751 static void
2752f_complete_info(typval_T *argvars, typval_T *rettv)
2753{
2754 list_T *what_list = NULL;
2755
2756 if (rettv_dict_alloc(rettv) != OK)
2757 return;
2758
2759 if (argvars[0].v_type != VAR_UNKNOWN)
2760 {
2761 if (argvars[0].v_type != VAR_LIST)
2762 {
2763 emsg(_(e_listreq));
2764 return;
2765 }
2766 what_list = argvars[0].vval.v_list;
2767 }
2768 get_complete_info(what_list, rettv->vval.v_dict);
2769}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002770#endif
2771
2772/*
2773 * "confirm(message, buttons[, default [, type]])" function
2774 */
2775 static void
2776f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2777{
2778#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2779 char_u *message;
2780 char_u *buttons = NULL;
2781 char_u buf[NUMBUFLEN];
2782 char_u buf2[NUMBUFLEN];
2783 int def = 1;
2784 int type = VIM_GENERIC;
2785 char_u *typestr;
2786 int error = FALSE;
2787
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002788 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002789 if (message == NULL)
2790 error = TRUE;
2791 if (argvars[1].v_type != VAR_UNKNOWN)
2792 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002793 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002794 if (buttons == NULL)
2795 error = TRUE;
2796 if (argvars[2].v_type != VAR_UNKNOWN)
2797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002798 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002799 if (argvars[3].v_type != VAR_UNKNOWN)
2800 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002801 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002802 if (typestr == NULL)
2803 error = TRUE;
2804 else
2805 {
2806 switch (TOUPPER_ASC(*typestr))
2807 {
2808 case 'E': type = VIM_ERROR; break;
2809 case 'Q': type = VIM_QUESTION; break;
2810 case 'I': type = VIM_INFO; break;
2811 case 'W': type = VIM_WARNING; break;
2812 case 'G': type = VIM_GENERIC; break;
2813 }
2814 }
2815 }
2816 }
2817 }
2818
2819 if (buttons == NULL || *buttons == NUL)
2820 buttons = (char_u *)_("&Ok");
2821
2822 if (!error)
2823 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2824 def, NULL, FALSE);
2825#endif
2826}
2827
2828/*
2829 * "copy()" function
2830 */
2831 static void
2832f_copy(typval_T *argvars, typval_T *rettv)
2833{
2834 item_copy(&argvars[0], rettv, FALSE, 0);
2835}
2836
2837#ifdef FEAT_FLOAT
2838/*
2839 * "cos()" function
2840 */
2841 static void
2842f_cos(typval_T *argvars, typval_T *rettv)
2843{
2844 float_T f = 0.0;
2845
2846 rettv->v_type = VAR_FLOAT;
2847 if (get_float_arg(argvars, &f) == OK)
2848 rettv->vval.v_float = cos(f);
2849 else
2850 rettv->vval.v_float = 0.0;
2851}
2852
2853/*
2854 * "cosh()" function
2855 */
2856 static void
2857f_cosh(typval_T *argvars, typval_T *rettv)
2858{
2859 float_T f = 0.0;
2860
2861 rettv->v_type = VAR_FLOAT;
2862 if (get_float_arg(argvars, &f) == OK)
2863 rettv->vval.v_float = cosh(f);
2864 else
2865 rettv->vval.v_float = 0.0;
2866}
2867#endif
2868
2869/*
2870 * "count()" function
2871 */
2872 static void
2873f_count(typval_T *argvars, typval_T *rettv)
2874{
2875 long n = 0;
2876 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002877 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002878
Bram Moolenaar9966b212017-07-28 16:46:57 +02002879 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002880 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002881
2882 if (argvars[0].v_type == VAR_STRING)
2883 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002884 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002885 char_u *p = argvars[0].vval.v_string;
2886 char_u *next;
2887
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002888 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002889 {
2890 if (ic)
2891 {
2892 size_t len = STRLEN(expr);
2893
2894 while (*p != NUL)
2895 {
2896 if (MB_STRNICMP(p, expr, len) == 0)
2897 {
2898 ++n;
2899 p += len;
2900 }
2901 else
2902 MB_PTR_ADV(p);
2903 }
2904 }
2905 else
2906 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2907 != NULL)
2908 {
2909 ++n;
2910 p = next + STRLEN(expr);
2911 }
2912 }
2913
2914 }
2915 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002916 {
2917 listitem_T *li;
2918 list_T *l;
2919 long idx;
2920
2921 if ((l = argvars[0].vval.v_list) != NULL)
2922 {
2923 li = l->lv_first;
2924 if (argvars[2].v_type != VAR_UNKNOWN)
2925 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002926 if (argvars[3].v_type != VAR_UNKNOWN)
2927 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002928 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002929 if (!error)
2930 {
2931 li = list_find(l, idx);
2932 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002933 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002934 }
2935 }
2936 if (error)
2937 li = NULL;
2938 }
2939
2940 for ( ; li != NULL; li = li->li_next)
2941 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2942 ++n;
2943 }
2944 }
2945 else if (argvars[0].v_type == VAR_DICT)
2946 {
2947 int todo;
2948 dict_T *d;
2949 hashitem_T *hi;
2950
2951 if ((d = argvars[0].vval.v_dict) != NULL)
2952 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002953 if (argvars[2].v_type != VAR_UNKNOWN)
2954 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002955 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002956 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002957 }
2958
2959 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2960 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2961 {
2962 if (!HASHITEM_EMPTY(hi))
2963 {
2964 --todo;
2965 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2966 ++n;
2967 }
2968 }
2969 }
2970 }
2971 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002972 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002973 rettv->vval.v_number = n;
2974}
2975
2976/*
2977 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2978 *
2979 * Checks the existence of a cscope connection.
2980 */
2981 static void
2982f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2983{
2984#ifdef FEAT_CSCOPE
2985 int num = 0;
2986 char_u *dbpath = NULL;
2987 char_u *prepend = NULL;
2988 char_u buf[NUMBUFLEN];
2989
2990 if (argvars[0].v_type != VAR_UNKNOWN
2991 && argvars[1].v_type != VAR_UNKNOWN)
2992 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002993 num = (int)tv_get_number(&argvars[0]);
2994 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002996 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002997 }
2998
2999 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
3000#endif
3001}
3002
3003/*
3004 * "cursor(lnum, col)" function, or
3005 * "cursor(list)"
3006 *
3007 * Moves the cursor to the specified line and column.
3008 * Returns 0 when the position could be set, -1 otherwise.
3009 */
3010 static void
3011f_cursor(typval_T *argvars, typval_T *rettv)
3012{
3013 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003014 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003015 int set_curswant = TRUE;
3016
3017 rettv->vval.v_number = -1;
3018 if (argvars[1].v_type == VAR_UNKNOWN)
3019 {
3020 pos_T pos;
3021 colnr_T curswant = -1;
3022
3023 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003025 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003026 return;
3027 }
3028 line = pos.lnum;
3029 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003030 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031 if (curswant >= 0)
3032 {
3033 curwin->w_curswant = curswant - 1;
3034 set_curswant = FALSE;
3035 }
3036 }
3037 else
3038 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003039 line = tv_get_lnum(argvars);
3040 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003041 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003042 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003043 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003044 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003045 return; /* type error; errmsg already given */
3046 if (line > 0)
3047 curwin->w_cursor.lnum = line;
3048 if (col > 0)
3049 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003050 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003051
3052 /* Make sure the cursor is in a valid position. */
3053 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003054 /* Correct cursor for multi-byte character. */
3055 if (has_mbyte)
3056 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003057
3058 curwin->w_set_curswant = set_curswant;
3059 rettv->vval.v_number = 0;
3060}
3061
Bram Moolenaar4f974752019-02-17 17:44:42 +01003062#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003063/*
3064 * "debugbreak()" function
3065 */
3066 static void
3067f_debugbreak(typval_T *argvars, typval_T *rettv)
3068{
3069 int pid;
3070
3071 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003072 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003073 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003074 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003075 else
3076 {
3077 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3078
3079 if (hProcess != NULL)
3080 {
3081 DebugBreakProcess(hProcess);
3082 CloseHandle(hProcess);
3083 rettv->vval.v_number = OK;
3084 }
3085 }
3086}
3087#endif
3088
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003089/*
3090 * "deepcopy()" function
3091 */
3092 static void
3093f_deepcopy(typval_T *argvars, typval_T *rettv)
3094{
3095 int noref = 0;
3096 int copyID;
3097
3098 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003099 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003100 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003101 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003102 else
3103 {
3104 copyID = get_copyID();
3105 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3106 }
3107}
3108
3109/*
3110 * "delete()" function
3111 */
3112 static void
3113f_delete(typval_T *argvars, typval_T *rettv)
3114{
3115 char_u nbuf[NUMBUFLEN];
3116 char_u *name;
3117 char_u *flags;
3118
3119 rettv->vval.v_number = -1;
3120 if (check_restricted() || check_secure())
3121 return;
3122
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003123 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003124 if (name == NULL || *name == NUL)
3125 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003126 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003127 return;
3128 }
3129
3130 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003131 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003132 else
3133 flags = (char_u *)"";
3134
3135 if (*flags == NUL)
3136 /* delete a file */
3137 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3138 else if (STRCMP(flags, "d") == 0)
3139 /* delete an empty directory */
3140 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3141 else if (STRCMP(flags, "rf") == 0)
3142 /* delete a directory recursively */
3143 rettv->vval.v_number = delete_recursive(name);
3144 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003145 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003146}
3147
3148/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003149 * "deletebufline()" function
3150 */
3151 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003152f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003153{
3154 buf_T *buf;
3155 linenr_T first, last;
3156 linenr_T lnum;
3157 long count;
3158 int is_curbuf;
3159 buf_T *curbuf_save = NULL;
3160 win_T *curwin_save = NULL;
3161 tabpage_T *tp;
3162 win_T *wp;
3163
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003164 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003165 if (buf == NULL)
3166 {
3167 rettv->vval.v_number = 1; /* FAIL */
3168 return;
3169 }
3170 is_curbuf = buf == curbuf;
3171
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003172 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003173 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003174 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003175 else
3176 last = first;
3177
3178 if (buf->b_ml.ml_mfp == NULL || first < 1
3179 || first > buf->b_ml.ml_line_count || last < first)
3180 {
3181 rettv->vval.v_number = 1; /* FAIL */
3182 return;
3183 }
3184
3185 if (!is_curbuf)
3186 {
3187 curbuf_save = curbuf;
3188 curwin_save = curwin;
3189 curbuf = buf;
3190 find_win_for_curbuf();
3191 }
3192 if (last > curbuf->b_ml.ml_line_count)
3193 last = curbuf->b_ml.ml_line_count;
3194 count = last - first + 1;
3195
3196 // When coming here from Insert mode, sync undo, so that this can be
3197 // undone separately from what was previously inserted.
3198 if (u_sync_once == 2)
3199 {
3200 u_sync_once = 1; // notify that u_sync() was called
3201 u_sync(TRUE);
3202 }
3203
3204 if (u_save(first - 1, last + 1) == FAIL)
3205 {
3206 rettv->vval.v_number = 1; /* FAIL */
3207 return;
3208 }
3209
3210 for (lnum = first; lnum <= last; ++lnum)
3211 ml_delete(first, TRUE);
3212
3213 FOR_ALL_TAB_WINDOWS(tp, wp)
3214 if (wp->w_buffer == buf)
3215 {
3216 if (wp->w_cursor.lnum > last)
3217 wp->w_cursor.lnum -= count;
3218 else if (wp->w_cursor.lnum> first)
3219 wp->w_cursor.lnum = first;
3220 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3221 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3222 }
3223 check_cursor_col();
3224 deleted_lines_mark(first, count);
3225
3226 if (!is_curbuf)
3227 {
3228 curbuf = curbuf_save;
3229 curwin = curwin_save;
3230 }
3231}
3232
3233/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003234 * "did_filetype()" function
3235 */
3236 static void
3237f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3238{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003239 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003240}
3241
3242/*
3243 * "diff_filler()" function
3244 */
3245 static void
3246f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3247{
3248#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003249 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003250#endif
3251}
3252
3253/*
3254 * "diff_hlID()" function
3255 */
3256 static void
3257f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3258{
3259#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003260 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003261 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003262 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003263 static int fnum = 0;
3264 static int change_start = 0;
3265 static int change_end = 0;
3266 static hlf_T hlID = (hlf_T)0;
3267 int filler_lines;
3268 int col;
3269
3270 if (lnum < 0) /* ignore type error in {lnum} arg */
3271 lnum = 0;
3272 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003273 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003274 || fnum != curbuf->b_fnum)
3275 {
3276 /* New line, buffer, change: need to get the values. */
3277 filler_lines = diff_check(curwin, lnum);
3278 if (filler_lines < 0)
3279 {
3280 if (filler_lines == -1)
3281 {
3282 change_start = MAXCOL;
3283 change_end = -1;
3284 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3285 hlID = HLF_ADD; /* added line */
3286 else
3287 hlID = HLF_CHD; /* changed line */
3288 }
3289 else
3290 hlID = HLF_ADD; /* added line */
3291 }
3292 else
3293 hlID = (hlf_T)0;
3294 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003295 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003296 fnum = curbuf->b_fnum;
3297 }
3298
3299 if (hlID == HLF_CHD || hlID == HLF_TXD)
3300 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003301 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003302 if (col >= change_start && col <= change_end)
3303 hlID = HLF_TXD; /* changed text */
3304 else
3305 hlID = HLF_CHD; /* changed line */
3306 }
3307 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3308#endif
3309}
3310
3311/*
3312 * "empty({expr})" function
3313 */
3314 static void
3315f_empty(typval_T *argvars, typval_T *rettv)
3316{
3317 int n = FALSE;
3318
3319 switch (argvars[0].v_type)
3320 {
3321 case VAR_STRING:
3322 case VAR_FUNC:
3323 n = argvars[0].vval.v_string == NULL
3324 || *argvars[0].vval.v_string == NUL;
3325 break;
3326 case VAR_PARTIAL:
3327 n = FALSE;
3328 break;
3329 case VAR_NUMBER:
3330 n = argvars[0].vval.v_number == 0;
3331 break;
3332 case VAR_FLOAT:
3333#ifdef FEAT_FLOAT
3334 n = argvars[0].vval.v_float == 0.0;
3335 break;
3336#endif
3337 case VAR_LIST:
3338 n = argvars[0].vval.v_list == NULL
3339 || argvars[0].vval.v_list->lv_first == NULL;
3340 break;
3341 case VAR_DICT:
3342 n = argvars[0].vval.v_dict == NULL
3343 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3344 break;
3345 case VAR_SPECIAL:
3346 n = argvars[0].vval.v_number != VVAL_TRUE;
3347 break;
3348
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003349 case VAR_BLOB:
3350 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003351 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3352 break;
3353
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003354 case VAR_JOB:
3355#ifdef FEAT_JOB_CHANNEL
3356 n = argvars[0].vval.v_job == NULL
3357 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3358 break;
3359#endif
3360 case VAR_CHANNEL:
3361#ifdef FEAT_JOB_CHANNEL
3362 n = argvars[0].vval.v_channel == NULL
3363 || !channel_is_open(argvars[0].vval.v_channel);
3364 break;
3365#endif
3366 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003367 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003368 n = TRUE;
3369 break;
3370 }
3371
3372 rettv->vval.v_number = n;
3373}
3374
3375/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003376 * "environ()" function
3377 */
3378 static void
3379f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3380{
3381#if !defined(AMIGA)
3382 int i = 0;
3383 char_u *entry, *value;
3384# ifdef MSWIN
3385 extern wchar_t **_wenviron;
3386# else
3387 extern char **environ;
3388# endif
3389
3390 if (rettv_dict_alloc(rettv) != OK)
3391 return;
3392
3393# ifdef MSWIN
3394 if (*_wenviron == NULL)
3395 return;
3396# else
3397 if (*environ == NULL)
3398 return;
3399# endif
3400
3401 for (i = 0; ; ++i)
3402 {
3403# ifdef MSWIN
3404 short_u *p;
3405
3406 if ((p = (short_u *)_wenviron[i]) == NULL)
3407 return;
3408 entry = utf16_to_enc(p, NULL);
3409# else
3410 if ((entry = (char_u *)environ[i]) == NULL)
3411 return;
3412 entry = vim_strsave(entry);
3413# endif
3414 if (entry == NULL) // out of memory
3415 return;
3416 if ((value = vim_strchr(entry, '=')) == NULL)
3417 {
3418 vim_free(entry);
3419 continue;
3420 }
3421 *value++ = NUL;
3422 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3423 vim_free(entry);
3424 }
3425#endif
3426}
3427
3428/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003429 * "escape({string}, {chars})" function
3430 */
3431 static void
3432f_escape(typval_T *argvars, typval_T *rettv)
3433{
3434 char_u buf[NUMBUFLEN];
3435
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003436 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3437 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003438 rettv->v_type = VAR_STRING;
3439}
3440
3441/*
3442 * "eval()" function
3443 */
3444 static void
3445f_eval(typval_T *argvars, typval_T *rettv)
3446{
3447 char_u *s, *p;
3448
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003449 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003450 if (s != NULL)
3451 s = skipwhite(s);
3452
3453 p = s;
3454 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3455 {
3456 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003457 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003458 need_clr_eos = FALSE;
3459 rettv->v_type = VAR_NUMBER;
3460 rettv->vval.v_number = 0;
3461 }
3462 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003463 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003464}
3465
3466/*
3467 * "eventhandler()" function
3468 */
3469 static void
3470f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3471{
3472 rettv->vval.v_number = vgetc_busy;
3473}
3474
3475/*
3476 * "executable()" function
3477 */
3478 static void
3479f_executable(typval_T *argvars, typval_T *rettv)
3480{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003481 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003482
3483 /* Check in $PATH and also check directly if there is a directory name. */
3484 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3485 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3486}
3487
3488static garray_T redir_execute_ga;
3489
3490/*
3491 * Append "value[value_len]" to the execute() output.
3492 */
3493 void
3494execute_redir_str(char_u *value, int value_len)
3495{
3496 int len;
3497
3498 if (value_len == -1)
3499 len = (int)STRLEN(value); /* Append the entire string */
3500 else
3501 len = value_len; /* Append only "value_len" characters */
3502 if (ga_grow(&redir_execute_ga, len) == OK)
3503 {
3504 mch_memmove((char *)redir_execute_ga.ga_data
3505 + redir_execute_ga.ga_len, value, len);
3506 redir_execute_ga.ga_len += len;
3507 }
3508}
3509
3510/*
3511 * Get next line from a list.
3512 * Called by do_cmdline() to get the next line.
3513 * Returns allocated string, or NULL for end of function.
3514 */
3515
3516 static char_u *
3517get_list_line(
3518 int c UNUSED,
3519 void *cookie,
3520 int indent UNUSED)
3521{
3522 listitem_T **p = (listitem_T **)cookie;
3523 listitem_T *item = *p;
3524 char_u buf[NUMBUFLEN];
3525 char_u *s;
3526
3527 if (item == NULL)
3528 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003529 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003530 *p = item->li_next;
3531 return s == NULL ? NULL : vim_strsave(s);
3532}
3533
3534/*
3535 * "execute()" function
3536 */
3537 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003538execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003539{
3540 char_u *cmd = NULL;
3541 list_T *list = NULL;
3542 int save_msg_silent = msg_silent;
3543 int save_emsg_silent = emsg_silent;
3544 int save_emsg_noredir = emsg_noredir;
3545 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003546 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003548 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003549 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003550
3551 rettv->vval.v_string = NULL;
3552 rettv->v_type = VAR_STRING;
3553
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003554 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003555 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003556 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003557 if (list == NULL || list->lv_first == NULL)
3558 /* empty list, no commands, empty output */
3559 return;
3560 ++list->lv_refcount;
3561 }
3562 else
3563 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003564 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003565 if (cmd == NULL)
3566 return;
3567 }
3568
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003569 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003570 {
3571 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003572 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003573
3574 if (s == NULL)
3575 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003576 if (*s == NUL)
3577 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003578 if (STRNCMP(s, "silent", 6) == 0)
3579 ++msg_silent;
3580 if (STRCMP(s, "silent!") == 0)
3581 {
3582 emsg_silent = TRUE;
3583 emsg_noredir = TRUE;
3584 }
3585 }
3586 else
3587 ++msg_silent;
3588
3589 if (redir_execute)
3590 save_ga = redir_execute_ga;
3591 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3592 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003593 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003594 if (!echo_output)
3595 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003596
3597 if (cmd != NULL)
3598 do_cmdline_cmd(cmd);
3599 else
3600 {
3601 listitem_T *item = list->lv_first;
3602
3603 do_cmdline(NULL, get_list_line, (void *)&item,
3604 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3605 --list->lv_refcount;
3606 }
3607
Bram Moolenaard297f352017-01-29 20:31:21 +01003608 /* Need to append a NUL to the result. */
3609 if (ga_grow(&redir_execute_ga, 1) == OK)
3610 {
3611 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3612 rettv->vval.v_string = redir_execute_ga.ga_data;
3613 }
3614 else
3615 {
3616 ga_clear(&redir_execute_ga);
3617 rettv->vval.v_string = NULL;
3618 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003619 msg_silent = save_msg_silent;
3620 emsg_silent = save_emsg_silent;
3621 emsg_noredir = save_emsg_noredir;
3622
3623 redir_execute = save_redir_execute;
3624 if (redir_execute)
3625 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003626 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003627
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003628 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003629 if (echo_output)
3630 // When not working silently: put it in column zero. A following
3631 // "echon" will overwrite the message, unavoidably.
3632 msg_col = 0;
3633 else
3634 // When working silently: Put it back where it was, since nothing
3635 // should have been written.
3636 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003637}
3638
3639/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003640 * "execute()" function
3641 */
3642 static void
3643f_execute(typval_T *argvars, typval_T *rettv)
3644{
3645 execute_common(argvars, rettv, 0);
3646}
3647
3648/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003649 * "exepath()" function
3650 */
3651 static void
3652f_exepath(typval_T *argvars, typval_T *rettv)
3653{
3654 char_u *p = NULL;
3655
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003656 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003657 rettv->v_type = VAR_STRING;
3658 rettv->vval.v_string = p;
3659}
3660
3661/*
3662 * "exists()" function
3663 */
3664 static void
3665f_exists(typval_T *argvars, typval_T *rettv)
3666{
3667 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003668 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003669
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003670 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003671 if (*p == '$') /* environment variable */
3672 {
3673 /* first try "normal" environment variables (fast) */
3674 if (mch_getenv(p + 1) != NULL)
3675 n = TRUE;
3676 else
3677 {
3678 /* try expanding things like $VIM and ${HOME} */
3679 p = expand_env_save(p);
3680 if (p != NULL && *p != '$')
3681 n = TRUE;
3682 vim_free(p);
3683 }
3684 }
3685 else if (*p == '&' || *p == '+') /* option */
3686 {
3687 n = (get_option_tv(&p, NULL, TRUE) == OK);
3688 if (*skipwhite(p) != NUL)
3689 n = FALSE; /* trailing garbage */
3690 }
3691 else if (*p == '*') /* internal or user defined function */
3692 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003693 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003694 }
3695 else if (*p == ':')
3696 {
3697 n = cmd_exists(p + 1);
3698 }
3699 else if (*p == '#')
3700 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003701 if (p[1] == '#')
3702 n = autocmd_supported(p + 2);
3703 else
3704 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003705 }
3706 else /* internal variable */
3707 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003708 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003709 }
3710
3711 rettv->vval.v_number = n;
3712}
3713
3714#ifdef FEAT_FLOAT
3715/*
3716 * "exp()" function
3717 */
3718 static void
3719f_exp(typval_T *argvars, typval_T *rettv)
3720{
3721 float_T f = 0.0;
3722
3723 rettv->v_type = VAR_FLOAT;
3724 if (get_float_arg(argvars, &f) == OK)
3725 rettv->vval.v_float = exp(f);
3726 else
3727 rettv->vval.v_float = 0.0;
3728}
3729#endif
3730
3731/*
3732 * "expand()" function
3733 */
3734 static void
3735f_expand(typval_T *argvars, typval_T *rettv)
3736{
3737 char_u *s;
3738 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003739 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003740 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3741 expand_T xpc;
3742 int error = FALSE;
3743 char_u *result;
3744
3745 rettv->v_type = VAR_STRING;
3746 if (argvars[1].v_type != VAR_UNKNOWN
3747 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003748 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003750 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003751
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003752 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003753 if (*s == '%' || *s == '#' || *s == '<')
3754 {
3755 ++emsg_off;
3756 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3757 --emsg_off;
3758 if (rettv->v_type == VAR_LIST)
3759 {
3760 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3761 list_append_string(rettv->vval.v_list, result, -1);
3762 else
3763 vim_free(result);
3764 }
3765 else
3766 rettv->vval.v_string = result;
3767 }
3768 else
3769 {
3770 /* When the optional second argument is non-zero, don't remove matches
3771 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3772 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003773 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003774 options |= WILD_KEEP_ALL;
3775 if (!error)
3776 {
3777 ExpandInit(&xpc);
3778 xpc.xp_context = EXPAND_FILES;
3779 if (p_wic)
3780 options += WILD_ICASE;
3781 if (rettv->v_type == VAR_STRING)
3782 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3783 options, WILD_ALL);
3784 else if (rettv_list_alloc(rettv) != FAIL)
3785 {
3786 int i;
3787
3788 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3789 for (i = 0; i < xpc.xp_numfiles; i++)
3790 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3791 ExpandCleanup(&xpc);
3792 }
3793 }
3794 else
3795 rettv->vval.v_string = NULL;
3796 }
3797}
3798
3799/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003800 * "expandcmd()" function
3801 * Expand all the special characters in a command string.
3802 */
3803 static void
3804f_expandcmd(typval_T *argvars, typval_T *rettv)
3805{
3806 exarg_T eap;
3807 char_u *cmdstr;
3808 char *errormsg = NULL;
3809
3810 rettv->v_type = VAR_STRING;
3811 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3812
3813 memset(&eap, 0, sizeof(eap));
3814 eap.cmd = cmdstr;
3815 eap.arg = cmdstr;
3816 eap.argt |= NOSPC;
3817 eap.usefilter = FALSE;
3818 eap.nextcmd = NULL;
3819 eap.cmdidx = CMD_USER;
3820
3821 expand_filename(&eap, &cmdstr, &errormsg);
3822 if (errormsg != NULL && *errormsg != NUL)
3823 emsg(errormsg);
3824
3825 rettv->vval.v_string = cmdstr;
3826}
3827
3828/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003829 * "extend(list, list [, idx])" function
3830 * "extend(dict, dict [, action])" function
3831 */
3832 static void
3833f_extend(typval_T *argvars, typval_T *rettv)
3834{
3835 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3836
3837 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3838 {
3839 list_T *l1, *l2;
3840 listitem_T *item;
3841 long before;
3842 int error = FALSE;
3843
3844 l1 = argvars[0].vval.v_list;
3845 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003846 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003847 && l2 != NULL)
3848 {
3849 if (argvars[2].v_type != VAR_UNKNOWN)
3850 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003851 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003852 if (error)
3853 return; /* type error; errmsg already given */
3854
3855 if (before == l1->lv_len)
3856 item = NULL;
3857 else
3858 {
3859 item = list_find(l1, before);
3860 if (item == NULL)
3861 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003862 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003863 return;
3864 }
3865 }
3866 }
3867 else
3868 item = NULL;
3869 list_extend(l1, l2, item);
3870
3871 copy_tv(&argvars[0], rettv);
3872 }
3873 }
3874 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3875 {
3876 dict_T *d1, *d2;
3877 char_u *action;
3878 int i;
3879
3880 d1 = argvars[0].vval.v_dict;
3881 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003882 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003883 && d2 != NULL)
3884 {
3885 /* Check the third argument. */
3886 if (argvars[2].v_type != VAR_UNKNOWN)
3887 {
3888 static char *(av[]) = {"keep", "force", "error"};
3889
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003890 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003891 if (action == NULL)
3892 return; /* type error; errmsg already given */
3893 for (i = 0; i < 3; ++i)
3894 if (STRCMP(action, av[i]) == 0)
3895 break;
3896 if (i == 3)
3897 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003898 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003899 return;
3900 }
3901 }
3902 else
3903 action = (char_u *)"force";
3904
3905 dict_extend(d1, d2, action);
3906
3907 copy_tv(&argvars[0], rettv);
3908 }
3909 }
3910 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003911 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003912}
3913
3914/*
3915 * "feedkeys()" function
3916 */
3917 static void
3918f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3919{
3920 int remap = TRUE;
3921 int insert = FALSE;
3922 char_u *keys, *flags;
3923 char_u nbuf[NUMBUFLEN];
3924 int typed = FALSE;
3925 int execute = FALSE;
3926 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003927 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003928 char_u *keys_esc;
3929
3930 /* This is not allowed in the sandbox. If the commands would still be
3931 * executed in the sandbox it would be OK, but it probably happens later,
3932 * when "sandbox" is no longer set. */
3933 if (check_secure())
3934 return;
3935
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003936 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003937
3938 if (argvars[1].v_type != VAR_UNKNOWN)
3939 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003940 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003941 for ( ; *flags != NUL; ++flags)
3942 {
3943 switch (*flags)
3944 {
3945 case 'n': remap = FALSE; break;
3946 case 'm': remap = TRUE; break;
3947 case 't': typed = TRUE; break;
3948 case 'i': insert = TRUE; break;
3949 case 'x': execute = TRUE; break;
3950 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003951 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003952 }
3953 }
3954 }
3955
3956 if (*keys != NUL || execute)
3957 {
3958 /* Need to escape K_SPECIAL and CSI before putting the string in the
3959 * typeahead buffer. */
3960 keys_esc = vim_strsave_escape_csi(keys);
3961 if (keys_esc != NULL)
3962 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003963 if (lowlevel)
3964 {
3965#ifdef USE_INPUT_BUF
3966 add_to_input_buf(keys, (int)STRLEN(keys));
3967#else
3968 emsg(_("E980: lowlevel input not supported"));
3969#endif
3970 }
3971 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003972 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003973 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003974 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003975 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003976#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003977 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003978#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003979 )
3980 typebuf_was_filled = TRUE;
3981 }
3982 vim_free(keys_esc);
3983
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003984 if (execute)
3985 {
3986 int save_msg_scroll = msg_scroll;
3987
3988 /* Avoid a 1 second delay when the keys start Insert mode. */
3989 msg_scroll = FALSE;
3990
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003991 if (!dangerous)
3992 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003993 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003994 if (!dangerous)
3995 --ex_normal_busy;
3996
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003997 msg_scroll |= save_msg_scroll;
3998 }
3999 }
4000 }
4001}
4002
4003/*
4004 * "filereadable()" function
4005 */
4006 static void
4007f_filereadable(typval_T *argvars, typval_T *rettv)
4008{
4009 int fd;
4010 char_u *p;
4011 int n;
4012
4013#ifndef O_NONBLOCK
4014# define O_NONBLOCK 0
4015#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004016 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004017 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
4018 O_RDONLY | O_NONBLOCK, 0)) >= 0)
4019 {
4020 n = TRUE;
4021 close(fd);
4022 }
4023 else
4024 n = FALSE;
4025
4026 rettv->vval.v_number = n;
4027}
4028
4029/*
4030 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
4031 * rights to write into.
4032 */
4033 static void
4034f_filewritable(typval_T *argvars, typval_T *rettv)
4035{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004036 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004037}
4038
4039 static void
4040findfilendir(
4041 typval_T *argvars UNUSED,
4042 typval_T *rettv,
4043 int find_what UNUSED)
4044{
4045#ifdef FEAT_SEARCHPATH
4046 char_u *fname;
4047 char_u *fresult = NULL;
4048 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
4049 char_u *p;
4050 char_u pathbuf[NUMBUFLEN];
4051 int count = 1;
4052 int first = TRUE;
4053 int error = FALSE;
4054#endif
4055
4056 rettv->vval.v_string = NULL;
4057 rettv->v_type = VAR_STRING;
4058
4059#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004060 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004061
4062 if (argvars[1].v_type != VAR_UNKNOWN)
4063 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004064 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004065 if (p == NULL)
4066 error = TRUE;
4067 else
4068 {
4069 if (*p != NUL)
4070 path = p;
4071
4072 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004073 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004074 }
4075 }
4076
4077 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4078 error = TRUE;
4079
4080 if (*fname != NUL && !error)
4081 {
4082 do
4083 {
4084 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4085 vim_free(fresult);
4086 fresult = find_file_in_path_option(first ? fname : NULL,
4087 first ? (int)STRLEN(fname) : 0,
4088 0, first, path,
4089 find_what,
4090 curbuf->b_ffname,
4091 find_what == FINDFILE_DIR
4092 ? (char_u *)"" : curbuf->b_p_sua);
4093 first = FALSE;
4094
4095 if (fresult != NULL && rettv->v_type == VAR_LIST)
4096 list_append_string(rettv->vval.v_list, fresult, -1);
4097
4098 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4099 }
4100
4101 if (rettv->v_type == VAR_STRING)
4102 rettv->vval.v_string = fresult;
4103#endif
4104}
4105
4106/*
4107 * "filter()" function
4108 */
4109 static void
4110f_filter(typval_T *argvars, typval_T *rettv)
4111{
4112 filter_map(argvars, rettv, FALSE);
4113}
4114
4115/*
4116 * "finddir({fname}[, {path}[, {count}]])" function
4117 */
4118 static void
4119f_finddir(typval_T *argvars, typval_T *rettv)
4120{
4121 findfilendir(argvars, rettv, FINDFILE_DIR);
4122}
4123
4124/*
4125 * "findfile({fname}[, {path}[, {count}]])" function
4126 */
4127 static void
4128f_findfile(typval_T *argvars, typval_T *rettv)
4129{
4130 findfilendir(argvars, rettv, FINDFILE_FILE);
4131}
4132
4133#ifdef FEAT_FLOAT
4134/*
4135 * "float2nr({float})" function
4136 */
4137 static void
4138f_float2nr(typval_T *argvars, typval_T *rettv)
4139{
4140 float_T f = 0.0;
4141
4142 if (get_float_arg(argvars, &f) == OK)
4143 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004144 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004145 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004146 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004147 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004148 else
4149 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004150 }
4151}
4152
4153/*
4154 * "floor({float})" function
4155 */
4156 static void
4157f_floor(typval_T *argvars, typval_T *rettv)
4158{
4159 float_T f = 0.0;
4160
4161 rettv->v_type = VAR_FLOAT;
4162 if (get_float_arg(argvars, &f) == OK)
4163 rettv->vval.v_float = floor(f);
4164 else
4165 rettv->vval.v_float = 0.0;
4166}
4167
4168/*
4169 * "fmod()" function
4170 */
4171 static void
4172f_fmod(typval_T *argvars, typval_T *rettv)
4173{
4174 float_T fx = 0.0, fy = 0.0;
4175
4176 rettv->v_type = VAR_FLOAT;
4177 if (get_float_arg(argvars, &fx) == OK
4178 && get_float_arg(&argvars[1], &fy) == OK)
4179 rettv->vval.v_float = fmod(fx, fy);
4180 else
4181 rettv->vval.v_float = 0.0;
4182}
4183#endif
4184
4185/*
4186 * "fnameescape({string})" function
4187 */
4188 static void
4189f_fnameescape(typval_T *argvars, typval_T *rettv)
4190{
4191 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004192 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004193 rettv->v_type = VAR_STRING;
4194}
4195
4196/*
4197 * "fnamemodify({fname}, {mods})" function
4198 */
4199 static void
4200f_fnamemodify(typval_T *argvars, typval_T *rettv)
4201{
4202 char_u *fname;
4203 char_u *mods;
4204 int usedlen = 0;
4205 int len;
4206 char_u *fbuf = NULL;
4207 char_u buf[NUMBUFLEN];
4208
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004209 fname = tv_get_string_chk(&argvars[0]);
4210 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004211 if (fname == NULL || mods == NULL)
4212 fname = NULL;
4213 else
4214 {
4215 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004216 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004217 }
4218
4219 rettv->v_type = VAR_STRING;
4220 if (fname == NULL)
4221 rettv->vval.v_string = NULL;
4222 else
4223 rettv->vval.v_string = vim_strnsave(fname, len);
4224 vim_free(fbuf);
4225}
4226
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004227/*
4228 * "foldclosed()" function
4229 */
4230 static void
4231foldclosed_both(
4232 typval_T *argvars UNUSED,
4233 typval_T *rettv,
4234 int end UNUSED)
4235{
4236#ifdef FEAT_FOLDING
4237 linenr_T lnum;
4238 linenr_T first, last;
4239
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004240 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004241 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4242 {
4243 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4244 {
4245 if (end)
4246 rettv->vval.v_number = (varnumber_T)last;
4247 else
4248 rettv->vval.v_number = (varnumber_T)first;
4249 return;
4250 }
4251 }
4252#endif
4253 rettv->vval.v_number = -1;
4254}
4255
4256/*
4257 * "foldclosed()" function
4258 */
4259 static void
4260f_foldclosed(typval_T *argvars, typval_T *rettv)
4261{
4262 foldclosed_both(argvars, rettv, FALSE);
4263}
4264
4265/*
4266 * "foldclosedend()" function
4267 */
4268 static void
4269f_foldclosedend(typval_T *argvars, typval_T *rettv)
4270{
4271 foldclosed_both(argvars, rettv, TRUE);
4272}
4273
4274/*
4275 * "foldlevel()" function
4276 */
4277 static void
4278f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4279{
4280#ifdef FEAT_FOLDING
4281 linenr_T lnum;
4282
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004283 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004284 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4285 rettv->vval.v_number = foldLevel(lnum);
4286#endif
4287}
4288
4289/*
4290 * "foldtext()" function
4291 */
4292 static void
4293f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4294{
4295#ifdef FEAT_FOLDING
4296 linenr_T foldstart;
4297 linenr_T foldend;
4298 char_u *dashes;
4299 linenr_T lnum;
4300 char_u *s;
4301 char_u *r;
4302 int len;
4303 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004304 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004305#endif
4306
4307 rettv->v_type = VAR_STRING;
4308 rettv->vval.v_string = NULL;
4309#ifdef FEAT_FOLDING
4310 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4311 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4312 dashes = get_vim_var_str(VV_FOLDDASHES);
4313 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4314 && dashes != NULL)
4315 {
4316 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004317 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004318 if (!linewhite(lnum))
4319 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004320
4321 /* Find interesting text in this line. */
4322 s = skipwhite(ml_get(lnum));
4323 /* skip C comment-start */
4324 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4325 {
4326 s = skipwhite(s + 2);
4327 if (*skipwhite(s) == NUL
4328 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4329 {
4330 s = skipwhite(ml_get(lnum + 1));
4331 if (*s == '*')
4332 s = skipwhite(s + 1);
4333 }
4334 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004335 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004336 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004337 r = alloc(STRLEN(txt)
4338 + STRLEN(dashes) // for %s
4339 + 20 // for %3ld
4340 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004341 if (r != NULL)
4342 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004343 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004344 len = (int)STRLEN(r);
4345 STRCAT(r, s);
4346 /* remove 'foldmarker' and 'commentstring' */
4347 foldtext_cleanup(r + len);
4348 rettv->vval.v_string = r;
4349 }
4350 }
4351#endif
4352}
4353
4354/*
4355 * "foldtextresult(lnum)" function
4356 */
4357 static void
4358f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4359{
4360#ifdef FEAT_FOLDING
4361 linenr_T lnum;
4362 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004363 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004364 foldinfo_T foldinfo;
4365 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004366 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004367#endif
4368
4369 rettv->v_type = VAR_STRING;
4370 rettv->vval.v_string = NULL;
4371#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004372 if (entered)
4373 return; /* reject recursive use */
4374 entered = TRUE;
4375
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004376 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004377 /* treat illegal types and illegal string values for {lnum} the same */
4378 if (lnum < 0)
4379 lnum = 0;
4380 fold_count = foldedCount(curwin, lnum, &foldinfo);
4381 if (fold_count > 0)
4382 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004383 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4384 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004385 if (text == buf)
4386 text = vim_strsave(text);
4387 rettv->vval.v_string = text;
4388 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004389
4390 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004391#endif
4392}
4393
4394/*
4395 * "foreground()" function
4396 */
4397 static void
4398f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4399{
4400#ifdef FEAT_GUI
4401 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004402 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004403 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004404 return;
4405 }
4406#endif
4407#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004408 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004409#endif
4410}
4411
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004412 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004413common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004414{
4415 char_u *s;
4416 char_u *name;
4417 int use_string = FALSE;
4418 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004419 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004420
4421 if (argvars[0].v_type == VAR_FUNC)
4422 {
4423 /* function(MyFunc, [arg], dict) */
4424 s = argvars[0].vval.v_string;
4425 }
4426 else if (argvars[0].v_type == VAR_PARTIAL
4427 && argvars[0].vval.v_partial != NULL)
4428 {
4429 /* function(dict.MyFunc, [arg]) */
4430 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004431 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004432 }
4433 else
4434 {
4435 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004436 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004437 use_string = TRUE;
4438 }
4439
Bram Moolenaar843b8842016-08-21 14:36:15 +02004440 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004441 {
4442 name = s;
4443 trans_name = trans_function_name(&name, FALSE,
4444 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4445 if (*name != NUL)
4446 s = NULL;
4447 }
4448
Bram Moolenaar843b8842016-08-21 14:36:15 +02004449 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4450 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004451 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004452 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004453 else if (trans_name != NULL && (is_funcref
4454 ? find_func(trans_name) == NULL
4455 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004456 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004457 else
4458 {
4459 int dict_idx = 0;
4460 int arg_idx = 0;
4461 list_T *list = NULL;
4462
4463 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4464 {
4465 char sid_buf[25];
4466 int off = *s == 's' ? 2 : 5;
4467
4468 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4469 * also be called from another script. Using trans_function_name()
4470 * would also work, but some plugins depend on the name being
4471 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004472 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004473 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004474 if (name != NULL)
4475 {
4476 STRCPY(name, sid_buf);
4477 STRCAT(name, s + off);
4478 }
4479 }
4480 else
4481 name = vim_strsave(s);
4482
4483 if (argvars[1].v_type != VAR_UNKNOWN)
4484 {
4485 if (argvars[2].v_type != VAR_UNKNOWN)
4486 {
4487 /* function(name, [args], dict) */
4488 arg_idx = 1;
4489 dict_idx = 2;
4490 }
4491 else if (argvars[1].v_type == VAR_DICT)
4492 /* function(name, dict) */
4493 dict_idx = 1;
4494 else
4495 /* function(name, [args]) */
4496 arg_idx = 1;
4497 if (dict_idx > 0)
4498 {
4499 if (argvars[dict_idx].v_type != VAR_DICT)
4500 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004501 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004502 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004503 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004504 }
4505 if (argvars[dict_idx].vval.v_dict == NULL)
4506 dict_idx = 0;
4507 }
4508 if (arg_idx > 0)
4509 {
4510 if (argvars[arg_idx].v_type != VAR_LIST)
4511 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004512 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004513 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004514 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004515 }
4516 list = argvars[arg_idx].vval.v_list;
4517 if (list == NULL || list->lv_len == 0)
4518 arg_idx = 0;
4519 }
4520 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004521 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004522 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004523 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004524
4525 /* result is a VAR_PARTIAL */
4526 if (pt == NULL)
4527 vim_free(name);
4528 else
4529 {
4530 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4531 {
4532 listitem_T *li;
4533 int i = 0;
4534 int arg_len = 0;
4535 int lv_len = 0;
4536
4537 if (arg_pt != NULL)
4538 arg_len = arg_pt->pt_argc;
4539 if (list != NULL)
4540 lv_len = list->lv_len;
4541 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004542 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004543 if (pt->pt_argv == NULL)
4544 {
4545 vim_free(pt);
4546 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004547 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004548 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004549 for (i = 0; i < arg_len; i++)
4550 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4551 if (lv_len > 0)
4552 for (li = list->lv_first; li != NULL;
4553 li = li->li_next)
4554 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004555 }
4556
4557 /* For "function(dict.func, [], dict)" and "func" is a partial
4558 * use "dict". That is backwards compatible. */
4559 if (dict_idx > 0)
4560 {
4561 /* The dict is bound explicitly, pt_auto is FALSE. */
4562 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4563 ++pt->pt_dict->dv_refcount;
4564 }
4565 else if (arg_pt != NULL)
4566 {
4567 /* If the dict was bound automatically the result is also
4568 * bound automatically. */
4569 pt->pt_dict = arg_pt->pt_dict;
4570 pt->pt_auto = arg_pt->pt_auto;
4571 if (pt->pt_dict != NULL)
4572 ++pt->pt_dict->dv_refcount;
4573 }
4574
4575 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004576 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4577 {
4578 pt->pt_func = arg_pt->pt_func;
4579 func_ptr_ref(pt->pt_func);
4580 vim_free(name);
4581 }
4582 else if (is_funcref)
4583 {
4584 pt->pt_func = find_func(trans_name);
4585 func_ptr_ref(pt->pt_func);
4586 vim_free(name);
4587 }
4588 else
4589 {
4590 pt->pt_name = name;
4591 func_ref(name);
4592 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004593 }
4594 rettv->v_type = VAR_PARTIAL;
4595 rettv->vval.v_partial = pt;
4596 }
4597 else
4598 {
4599 /* result is a VAR_FUNC */
4600 rettv->v_type = VAR_FUNC;
4601 rettv->vval.v_string = name;
4602 func_ref(name);
4603 }
4604 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004605theend:
4606 vim_free(trans_name);
4607}
4608
4609/*
4610 * "funcref()" function
4611 */
4612 static void
4613f_funcref(typval_T *argvars, typval_T *rettv)
4614{
4615 common_function(argvars, rettv, TRUE);
4616}
4617
4618/*
4619 * "function()" function
4620 */
4621 static void
4622f_function(typval_T *argvars, typval_T *rettv)
4623{
4624 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004625}
4626
4627/*
4628 * "garbagecollect()" function
4629 */
4630 static void
4631f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4632{
4633 /* This is postponed until we are back at the toplevel, because we may be
4634 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4635 want_garbage_collect = TRUE;
4636
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004637 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004638 garbage_collect_at_exit = TRUE;
4639}
4640
4641/*
4642 * "get()" function
4643 */
4644 static void
4645f_get(typval_T *argvars, typval_T *rettv)
4646{
4647 listitem_T *li;
4648 list_T *l;
4649 dictitem_T *di;
4650 dict_T *d;
4651 typval_T *tv = NULL;
4652
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004653 if (argvars[0].v_type == VAR_BLOB)
4654 {
4655 int error = FALSE;
4656 int idx = tv_get_number_chk(&argvars[1], &error);
4657
4658 if (!error)
4659 {
4660 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004661 if (idx < 0)
4662 idx = blob_len(argvars[0].vval.v_blob) + idx;
4663 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4664 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004665 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004666 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004667 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004668 tv = rettv;
4669 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004670 }
4671 }
4672 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004673 {
4674 if ((l = argvars[0].vval.v_list) != NULL)
4675 {
4676 int error = FALSE;
4677
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004678 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004679 if (!error && li != NULL)
4680 tv = &li->li_tv;
4681 }
4682 }
4683 else if (argvars[0].v_type == VAR_DICT)
4684 {
4685 if ((d = argvars[0].vval.v_dict) != NULL)
4686 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004687 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004688 if (di != NULL)
4689 tv = &di->di_tv;
4690 }
4691 }
4692 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4693 {
4694 partial_T *pt;
4695 partial_T fref_pt;
4696
4697 if (argvars[0].v_type == VAR_PARTIAL)
4698 pt = argvars[0].vval.v_partial;
4699 else
4700 {
4701 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4702 fref_pt.pt_name = argvars[0].vval.v_string;
4703 pt = &fref_pt;
4704 }
4705
4706 if (pt != NULL)
4707 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004708 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004709 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004710
4711 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4712 {
4713 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004714 n = partial_name(pt);
4715 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004716 rettv->vval.v_string = NULL;
4717 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004718 {
4719 rettv->vval.v_string = vim_strsave(n);
4720 if (rettv->v_type == VAR_FUNC)
4721 func_ref(rettv->vval.v_string);
4722 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004723 }
4724 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004725 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004726 else if (STRCMP(what, "args") == 0)
4727 {
4728 rettv->v_type = VAR_LIST;
4729 if (rettv_list_alloc(rettv) == OK)
4730 {
4731 int i;
4732
4733 for (i = 0; i < pt->pt_argc; ++i)
4734 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4735 }
4736 }
4737 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004738 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004739 return;
4740 }
4741 }
4742 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004743 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004744
4745 if (tv == NULL)
4746 {
4747 if (argvars[2].v_type != VAR_UNKNOWN)
4748 copy_tv(&argvars[2], rettv);
4749 }
4750 else
4751 copy_tv(tv, rettv);
4752}
4753
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004754/*
4755 * Returns buffer options, variables and other attributes in a dictionary.
4756 */
4757 static dict_T *
4758get_buffer_info(buf_T *buf)
4759{
4760 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004761 tabpage_T *tp;
4762 win_T *wp;
4763 list_T *windows;
4764
4765 dict = dict_alloc();
4766 if (dict == NULL)
4767 return NULL;
4768
Bram Moolenaare0be1672018-07-08 16:50:37 +02004769 dict_add_number(dict, "bufnr", buf->b_fnum);
4770 dict_add_string(dict, "name", buf->b_ffname);
4771 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4772 : buflist_findlnum(buf));
4773 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4774 dict_add_number(dict, "listed", buf->b_p_bl);
4775 dict_add_number(dict, "changed", bufIsChanged(buf));
4776 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4777 dict_add_number(dict, "hidden",
4778 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004779
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004780 /* Get a reference to buffer variables */
4781 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004782
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004783 /* List of windows displaying this buffer */
4784 windows = list_alloc();
4785 if (windows != NULL)
4786 {
4787 FOR_ALL_TAB_WINDOWS(tp, wp)
4788 if (wp->w_buffer == buf)
4789 list_append_number(windows, (varnumber_T)wp->w_id);
4790 dict_add_list(dict, "windows", windows);
4791 }
4792
4793#ifdef FEAT_SIGNS
4794 if (buf->b_signlist != NULL)
4795 {
4796 /* List of signs placed in this buffer */
4797 list_T *signs = list_alloc();
4798 if (signs != NULL)
4799 {
4800 get_buffer_signs(buf, signs);
4801 dict_add_list(dict, "signs", signs);
4802 }
4803 }
4804#endif
4805
4806 return dict;
4807}
4808
4809/*
4810 * "getbufinfo()" function
4811 */
4812 static void
4813f_getbufinfo(typval_T *argvars, typval_T *rettv)
4814{
4815 buf_T *buf = NULL;
4816 buf_T *argbuf = NULL;
4817 dict_T *d;
4818 int filtered = FALSE;
4819 int sel_buflisted = FALSE;
4820 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004821 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004822
4823 if (rettv_list_alloc(rettv) != OK)
4824 return;
4825
4826 /* List of all the buffers or selected buffers */
4827 if (argvars[0].v_type == VAR_DICT)
4828 {
4829 dict_T *sel_d = argvars[0].vval.v_dict;
4830
4831 if (sel_d != NULL)
4832 {
4833 dictitem_T *di;
4834
4835 filtered = TRUE;
4836
4837 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004838 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004839 sel_buflisted = TRUE;
4840
4841 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004842 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004843 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004844
4845 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004846 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004847 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004848 }
4849 }
4850 else if (argvars[0].v_type != VAR_UNKNOWN)
4851 {
4852 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004853 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004854 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004855 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004856 --emsg_off;
4857 if (argbuf == NULL)
4858 return;
4859 }
4860
4861 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004862 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004863 {
4864 if (argbuf != NULL && argbuf != buf)
4865 continue;
4866 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004867 || (sel_buflisted && !buf->b_p_bl)
4868 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004869 continue;
4870
4871 d = get_buffer_info(buf);
4872 if (d != NULL)
4873 list_append_dict(rettv->vval.v_list, d);
4874 if (argbuf != NULL)
4875 return;
4876 }
4877}
4878
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004879/*
4880 * Get line or list of lines from buffer "buf" into "rettv".
4881 * Return a range (from start to end) of lines in rettv from the specified
4882 * buffer.
4883 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4884 */
4885 static void
4886get_buffer_lines(
4887 buf_T *buf,
4888 linenr_T start,
4889 linenr_T end,
4890 int retlist,
4891 typval_T *rettv)
4892{
4893 char_u *p;
4894
4895 rettv->v_type = VAR_STRING;
4896 rettv->vval.v_string = NULL;
4897 if (retlist && rettv_list_alloc(rettv) == FAIL)
4898 return;
4899
4900 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4901 return;
4902
4903 if (!retlist)
4904 {
4905 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4906 p = ml_get_buf(buf, start, FALSE);
4907 else
4908 p = (char_u *)"";
4909 rettv->vval.v_string = vim_strsave(p);
4910 }
4911 else
4912 {
4913 if (end < start)
4914 return;
4915
4916 if (start < 1)
4917 start = 1;
4918 if (end > buf->b_ml.ml_line_count)
4919 end = buf->b_ml.ml_line_count;
4920 while (start <= end)
4921 if (list_append_string(rettv->vval.v_list,
4922 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4923 break;
4924 }
4925}
4926
4927/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004928 * "getbufline()" function
4929 */
4930 static void
4931f_getbufline(typval_T *argvars, typval_T *rettv)
4932{
4933 linenr_T lnum;
4934 linenr_T end;
4935 buf_T *buf;
4936
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004937 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004938 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004939 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004940 --emsg_off;
4941
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004942 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004943 if (argvars[2].v_type == VAR_UNKNOWN)
4944 end = lnum;
4945 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004946 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004947
4948 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4949}
4950
4951/*
4952 * "getbufvar()" function
4953 */
4954 static void
4955f_getbufvar(typval_T *argvars, typval_T *rettv)
4956{
4957 buf_T *buf;
4958 buf_T *save_curbuf;
4959 char_u *varname;
4960 dictitem_T *v;
4961 int done = FALSE;
4962
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004963 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4964 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004965 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004966 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004967
4968 rettv->v_type = VAR_STRING;
4969 rettv->vval.v_string = NULL;
4970
4971 if (buf != NULL && varname != NULL)
4972 {
4973 /* set curbuf to be our buf, temporarily */
4974 save_curbuf = curbuf;
4975 curbuf = buf;
4976
Bram Moolenaar30567352016-08-27 21:25:44 +02004977 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004978 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004979 if (varname[1] == NUL)
4980 {
4981 /* get all buffer-local options in a dict */
4982 dict_T *opts = get_winbuf_options(TRUE);
4983
4984 if (opts != NULL)
4985 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004986 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004987 done = TRUE;
4988 }
4989 }
4990 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4991 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004992 done = TRUE;
4993 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004994 else
4995 {
4996 /* Look up the variable. */
4997 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4998 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4999 'b', varname, FALSE);
5000 if (v != NULL)
5001 {
5002 copy_tv(&v->di_tv, rettv);
5003 done = TRUE;
5004 }
5005 }
5006
5007 /* restore previous notion of curbuf */
5008 curbuf = save_curbuf;
5009 }
5010
5011 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5012 /* use the default value */
5013 copy_tv(&argvars[2], rettv);
5014
5015 --emsg_off;
5016}
5017
5018/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005019 * "getchangelist()" function
5020 */
5021 static void
5022f_getchangelist(typval_T *argvars, typval_T *rettv)
5023{
5024#ifdef FEAT_JUMPLIST
5025 buf_T *buf;
5026 int i;
5027 list_T *l;
5028 dict_T *d;
5029#endif
5030
5031 if (rettv_list_alloc(rettv) != OK)
5032 return;
5033
5034#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005035 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01005036 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01005037 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01005038 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005039 if (buf == NULL)
5040 return;
5041
5042 l = list_alloc();
5043 if (l == NULL)
5044 return;
5045
5046 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5047 return;
5048 /*
5049 * The current window change list index tracks only the position in the
5050 * current buffer change list. For other buffers, use the change list
5051 * length as the current index.
5052 */
5053 list_append_number(rettv->vval.v_list,
5054 (varnumber_T)((buf == curwin->w_buffer)
5055 ? curwin->w_changelistidx : buf->b_changelistlen));
5056
5057 for (i = 0; i < buf->b_changelistlen; ++i)
5058 {
5059 if (buf->b_changelist[i].lnum == 0)
5060 continue;
5061 if ((d = dict_alloc()) == NULL)
5062 return;
5063 if (list_append_dict(l, d) == FAIL)
5064 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005065 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5066 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005067 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005068 }
5069#endif
5070}
5071/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005072 * "getchar()" function
5073 */
5074 static void
5075f_getchar(typval_T *argvars, typval_T *rettv)
5076{
5077 varnumber_T n;
5078 int error = FALSE;
5079
Bram Moolenaar84d93902018-09-11 20:10:20 +02005080#ifdef MESSAGE_QUEUE
5081 // vpeekc() used to check for messages, but that caused problems, invoking
5082 // a callback where it was not expected. Some plugins use getchar(1) in a
5083 // loop to await a message, therefore make sure we check for messages here.
5084 parse_queued_messages();
5085#endif
5086
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005087 /* Position the cursor. Needed after a message that ends in a space. */
5088 windgoto(msg_row, msg_col);
5089
5090 ++no_mapping;
5091 ++allow_keys;
5092 for (;;)
5093 {
5094 if (argvars[0].v_type == VAR_UNKNOWN)
5095 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005096 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005097 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005098 /* getchar(1): only check if char avail */
5099 n = vpeekc_any();
5100 else if (error || vpeekc_any() == NUL)
5101 /* illegal argument or getchar(0) and no char avail: return zero */
5102 n = 0;
5103 else
5104 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005105 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005106
5107 if (n == K_IGNORE)
5108 continue;
5109 break;
5110 }
5111 --no_mapping;
5112 --allow_keys;
5113
5114 set_vim_var_nr(VV_MOUSE_WIN, 0);
5115 set_vim_var_nr(VV_MOUSE_WINID, 0);
5116 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5117 set_vim_var_nr(VV_MOUSE_COL, 0);
5118
5119 rettv->vval.v_number = n;
5120 if (IS_SPECIAL(n) || mod_mask != 0)
5121 {
5122 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5123 int i = 0;
5124
5125 /* Turn a special key into three bytes, plus modifier. */
5126 if (mod_mask != 0)
5127 {
5128 temp[i++] = K_SPECIAL;
5129 temp[i++] = KS_MODIFIER;
5130 temp[i++] = mod_mask;
5131 }
5132 if (IS_SPECIAL(n))
5133 {
5134 temp[i++] = K_SPECIAL;
5135 temp[i++] = K_SECOND(n);
5136 temp[i++] = K_THIRD(n);
5137 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005138 else if (has_mbyte)
5139 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005140 else
5141 temp[i++] = n;
5142 temp[i++] = NUL;
5143 rettv->v_type = VAR_STRING;
5144 rettv->vval.v_string = vim_strsave(temp);
5145
5146#ifdef FEAT_MOUSE
5147 if (is_mouse_key(n))
5148 {
5149 int row = mouse_row;
5150 int col = mouse_col;
5151 win_T *win;
5152 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005153 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005154 int winnr = 1;
5155
5156 if (row >= 0 && col >= 0)
5157 {
5158 /* Find the window at the mouse coordinates and compute the
5159 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005160 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005161 if (win == NULL)
5162 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005163 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005164# ifdef FEAT_TEXT_PROP
5165 if (bt_popup(win->w_buffer))
5166 winnr = 0;
5167 else
5168# endif
5169 for (wp = firstwin; wp != win && wp != NULL;
5170 wp = wp->w_next)
5171 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005172 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5173 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5174 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5175 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5176 }
5177 }
5178#endif
5179 }
5180}
5181
5182/*
5183 * "getcharmod()" function
5184 */
5185 static void
5186f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5187{
5188 rettv->vval.v_number = mod_mask;
5189}
5190
5191/*
5192 * "getcharsearch()" function
5193 */
5194 static void
5195f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5196{
5197 if (rettv_dict_alloc(rettv) != FAIL)
5198 {
5199 dict_T *dict = rettv->vval.v_dict;
5200
Bram Moolenaare0be1672018-07-08 16:50:37 +02005201 dict_add_string(dict, "char", last_csearch());
5202 dict_add_number(dict, "forward", last_csearch_forward());
5203 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005204 }
5205}
5206
5207/*
5208 * "getcmdline()" function
5209 */
5210 static void
5211f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5212{
5213 rettv->v_type = VAR_STRING;
5214 rettv->vval.v_string = get_cmdline_str();
5215}
5216
5217/*
5218 * "getcmdpos()" function
5219 */
5220 static void
5221f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5222{
5223 rettv->vval.v_number = get_cmdline_pos() + 1;
5224}
5225
5226/*
5227 * "getcmdtype()" function
5228 */
5229 static void
5230f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5231{
5232 rettv->v_type = VAR_STRING;
5233 rettv->vval.v_string = alloc(2);
5234 if (rettv->vval.v_string != NULL)
5235 {
5236 rettv->vval.v_string[0] = get_cmdline_type();
5237 rettv->vval.v_string[1] = NUL;
5238 }
5239}
5240
5241/*
5242 * "getcmdwintype()" function
5243 */
5244 static void
5245f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5246{
5247 rettv->v_type = VAR_STRING;
5248 rettv->vval.v_string = NULL;
5249#ifdef FEAT_CMDWIN
5250 rettv->vval.v_string = alloc(2);
5251 if (rettv->vval.v_string != NULL)
5252 {
5253 rettv->vval.v_string[0] = cmdwin_type;
5254 rettv->vval.v_string[1] = NUL;
5255 }
5256#endif
5257}
5258
5259#if defined(FEAT_CMDL_COMPL)
5260/*
5261 * "getcompletion()" function
5262 */
5263 static void
5264f_getcompletion(typval_T *argvars, typval_T *rettv)
5265{
5266 char_u *pat;
5267 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005268 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005269 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5270 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005271
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005272 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005273 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005274
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005275 if (p_wic)
5276 options |= WILD_ICASE;
5277
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005278 /* For filtered results, 'wildignore' is used */
5279 if (!filtered)
5280 options |= WILD_KEEP_ALL;
5281
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005282 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005283 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005284 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005285 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005286 if (xpc.xp_context == EXPAND_NOTHING)
5287 {
5288 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005289 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005290 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005291 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005292 return;
5293 }
5294
5295# if defined(FEAT_MENU)
5296 if (xpc.xp_context == EXPAND_MENUS)
5297 {
5298 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5299 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5300 }
5301# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005302#ifdef FEAT_CSCOPE
5303 if (xpc.xp_context == EXPAND_CSCOPE)
5304 {
5305 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5306 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5307 }
5308#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005309#ifdef FEAT_SIGNS
5310 if (xpc.xp_context == EXPAND_SIGN)
5311 {
5312 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5313 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5314 }
5315#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005316
5317 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5318 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5319 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005320 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005321
5322 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5323
5324 for (i = 0; i < xpc.xp_numfiles; i++)
5325 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5326 }
5327 vim_free(pat);
5328 ExpandCleanup(&xpc);
5329}
5330#endif
5331
5332/*
5333 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005334 *
5335 * Return the current working directory of a window in a tab page.
5336 * First optional argument 'winnr' is the window number or -1 and the second
5337 * optional argument 'tabnr' is the tab page number.
5338 *
5339 * If no arguments are supplied, then return the directory of the current
5340 * window.
5341 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5342 * the specified window.
5343 * If 'winnr' is 0 then return the directory of the current window.
5344 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5345 * directory of the specified tab page. Otherwise return the directory of the
5346 * specified window in the specified tab page.
5347 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005348 */
5349 static void
5350f_getcwd(typval_T *argvars, typval_T *rettv)
5351{
5352 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005353 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005354 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005355 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005356
5357 rettv->v_type = VAR_STRING;
5358 rettv->vval.v_string = NULL;
5359
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005360 if (argvars[0].v_type == VAR_NUMBER
5361 && argvars[0].vval.v_number == -1
5362 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005363 global = TRUE;
5364 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005365 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005366
5367 if (wp != NULL && wp->w_localdir != NULL)
5368 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005369 else if (tp != NULL && tp->tp_localdir != NULL)
5370 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5371 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005372 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005373 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005374 rettv->vval.v_string = vim_strsave(globaldir);
5375 else
5376 {
5377 cwd = alloc(MAXPATHL);
5378 if (cwd != NULL)
5379 {
5380 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5381 rettv->vval.v_string = vim_strsave(cwd);
5382 vim_free(cwd);
5383 }
5384 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005385 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005386#ifdef BACKSLASH_IN_FILENAME
5387 if (rettv->vval.v_string != NULL)
5388 slash_adjust(rettv->vval.v_string);
5389#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005390}
5391
5392/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005393 * "getenv()" function
5394 */
5395 static void
5396f_getenv(typval_T *argvars, typval_T *rettv)
5397{
5398 int mustfree = FALSE;
5399 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5400
5401 if (p == NULL)
5402 {
5403 rettv->v_type = VAR_SPECIAL;
5404 rettv->vval.v_number = VVAL_NULL;
5405 return;
5406 }
5407 if (!mustfree)
5408 p = vim_strsave(p);
5409 rettv->vval.v_string = p;
5410 rettv->v_type = VAR_STRING;
5411}
5412
5413/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005414 * "getfontname()" function
5415 */
5416 static void
5417f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5418{
5419 rettv->v_type = VAR_STRING;
5420 rettv->vval.v_string = NULL;
5421#ifdef FEAT_GUI
5422 if (gui.in_use)
5423 {
5424 GuiFont font;
5425 char_u *name = NULL;
5426
5427 if (argvars[0].v_type == VAR_UNKNOWN)
5428 {
5429 /* Get the "Normal" font. Either the name saved by
5430 * hl_set_font_name() or from the font ID. */
5431 font = gui.norm_font;
5432 name = hl_get_font_name();
5433 }
5434 else
5435 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005436 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005437 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5438 return;
5439 font = gui_mch_get_font(name, FALSE);
5440 if (font == NOFONT)
5441 return; /* Invalid font name, return empty string. */
5442 }
5443 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5444 if (argvars[0].v_type != VAR_UNKNOWN)
5445 gui_mch_free_font(font);
5446 }
5447#endif
5448}
5449
5450/*
5451 * "getfperm({fname})" function
5452 */
5453 static void
5454f_getfperm(typval_T *argvars, typval_T *rettv)
5455{
5456 char_u *fname;
5457 stat_T st;
5458 char_u *perm = NULL;
5459 char_u flags[] = "rwx";
5460 int i;
5461
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005462 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005463
5464 rettv->v_type = VAR_STRING;
5465 if (mch_stat((char *)fname, &st) >= 0)
5466 {
5467 perm = vim_strsave((char_u *)"---------");
5468 if (perm != NULL)
5469 {
5470 for (i = 0; i < 9; i++)
5471 {
5472 if (st.st_mode & (1 << (8 - i)))
5473 perm[i] = flags[i % 3];
5474 }
5475 }
5476 }
5477 rettv->vval.v_string = perm;
5478}
5479
5480/*
5481 * "getfsize({fname})" function
5482 */
5483 static void
5484f_getfsize(typval_T *argvars, typval_T *rettv)
5485{
5486 char_u *fname;
5487 stat_T st;
5488
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005489 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005490
5491 rettv->v_type = VAR_NUMBER;
5492
5493 if (mch_stat((char *)fname, &st) >= 0)
5494 {
5495 if (mch_isdir(fname))
5496 rettv->vval.v_number = 0;
5497 else
5498 {
5499 rettv->vval.v_number = (varnumber_T)st.st_size;
5500
5501 /* non-perfect check for overflow */
5502 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5503 rettv->vval.v_number = -2;
5504 }
5505 }
5506 else
5507 rettv->vval.v_number = -1;
5508}
5509
5510/*
5511 * "getftime({fname})" function
5512 */
5513 static void
5514f_getftime(typval_T *argvars, typval_T *rettv)
5515{
5516 char_u *fname;
5517 stat_T st;
5518
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005519 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005520
5521 if (mch_stat((char *)fname, &st) >= 0)
5522 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5523 else
5524 rettv->vval.v_number = -1;
5525}
5526
5527/*
5528 * "getftype({fname})" function
5529 */
5530 static void
5531f_getftype(typval_T *argvars, typval_T *rettv)
5532{
5533 char_u *fname;
5534 stat_T st;
5535 char_u *type = NULL;
5536 char *t;
5537
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005538 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005539
5540 rettv->v_type = VAR_STRING;
5541 if (mch_lstat((char *)fname, &st) >= 0)
5542 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005543 if (S_ISREG(st.st_mode))
5544 t = "file";
5545 else if (S_ISDIR(st.st_mode))
5546 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005547 else if (S_ISLNK(st.st_mode))
5548 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005549 else if (S_ISBLK(st.st_mode))
5550 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005551 else if (S_ISCHR(st.st_mode))
5552 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553 else if (S_ISFIFO(st.st_mode))
5554 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005555 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005556 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557 else
5558 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005559 type = vim_strsave((char_u *)t);
5560 }
5561 rettv->vval.v_string = type;
5562}
5563
5564/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005565 * "getjumplist()" function
5566 */
5567 static void
5568f_getjumplist(typval_T *argvars, typval_T *rettv)
5569{
5570#ifdef FEAT_JUMPLIST
5571 win_T *wp;
5572 int i;
5573 list_T *l;
5574 dict_T *d;
5575#endif
5576
5577 if (rettv_list_alloc(rettv) != OK)
5578 return;
5579
5580#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005581 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005582 if (wp == NULL)
5583 return;
5584
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005585 cleanup_jumplist(wp, TRUE);
5586
Bram Moolenaar4f505882018-02-10 21:06:32 +01005587 l = list_alloc();
5588 if (l == NULL)
5589 return;
5590
5591 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5592 return;
5593 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5594
5595 for (i = 0; i < wp->w_jumplistlen; ++i)
5596 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005597 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5598 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005599 if ((d = dict_alloc()) == NULL)
5600 return;
5601 if (list_append_dict(l, d) == FAIL)
5602 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005603 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5604 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005605 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005606 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005607 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005608 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005609 }
5610#endif
5611}
5612
5613/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614 * "getline(lnum, [end])" function
5615 */
5616 static void
5617f_getline(typval_T *argvars, typval_T *rettv)
5618{
5619 linenr_T lnum;
5620 linenr_T end;
5621 int retlist;
5622
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005623 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005624 if (argvars[1].v_type == VAR_UNKNOWN)
5625 {
5626 end = 0;
5627 retlist = FALSE;
5628 }
5629 else
5630 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005631 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005632 retlist = TRUE;
5633 }
5634
5635 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5636}
5637
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005638#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005639 static void
5640get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5641{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005642 if (what_arg->v_type == VAR_UNKNOWN)
5643 {
5644 if (rettv_list_alloc(rettv) == OK)
5645 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005646 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005647 }
5648 else
5649 {
5650 if (rettv_dict_alloc(rettv) == OK)
5651 if (is_qf || (wp != NULL))
5652 {
5653 if (what_arg->v_type == VAR_DICT)
5654 {
5655 dict_T *d = what_arg->vval.v_dict;
5656
5657 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005658 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005659 }
5660 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005661 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005662 }
5663 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005664}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005665#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005666
5667/*
5668 * "getloclist()" function
5669 */
5670 static void
5671f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5672{
5673#ifdef FEAT_QUICKFIX
5674 win_T *wp;
5675
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005676 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005677 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5678#endif
5679}
5680
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005681/*
5682 * "getmatches()" function
5683 */
5684 static void
5685f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5686{
5687#ifdef FEAT_SEARCH_EXTRA
5688 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005689 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005690 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005691 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005692
Bram Moolenaaraff74912019-03-30 18:11:49 +01005693 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5694 return;
5695
5696 cur = win->w_match_head;
5697 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005698 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005699 dict = dict_alloc();
5700 if (dict == NULL)
5701 return;
5702 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005703 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005704 /* match added with matchaddpos() */
5705 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005706 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005707 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005708 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005709 list_T *l;
5710
5711 llpos = &cur->pos.pos[i];
5712 if (llpos->lnum == 0)
5713 break;
5714 l = list_alloc();
5715 if (l == NULL)
5716 break;
5717 list_append_number(l, (varnumber_T)llpos->lnum);
5718 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005719 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005720 list_append_number(l, (varnumber_T)llpos->col);
5721 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005722 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005723 sprintf(buf, "pos%d", i + 1);
5724 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005725 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005726 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005727 else
5728 {
5729 dict_add_string(dict, "pattern", cur->pattern);
5730 }
5731 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5732 dict_add_number(dict, "priority", (long)cur->priority);
5733 dict_add_number(dict, "id", (long)cur->id);
5734# if defined(FEAT_CONCEAL)
5735 if (cur->conceal_char)
5736 {
5737 char_u buf[MB_MAXBYTES + 1];
5738
5739 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5740 dict_add_string(dict, "conceal", (char_u *)&buf);
5741 }
5742# endif
5743 list_append_dict(rettv->vval.v_list, dict);
5744 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005745 }
5746#endif
5747}
5748
5749/*
5750 * "getpid()" function
5751 */
5752 static void
5753f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5754{
5755 rettv->vval.v_number = mch_get_pid();
5756}
5757
5758 static void
5759getpos_both(
5760 typval_T *argvars,
5761 typval_T *rettv,
5762 int getcurpos)
5763{
5764 pos_T *fp;
5765 list_T *l;
5766 int fnum = -1;
5767
5768 if (rettv_list_alloc(rettv) == OK)
5769 {
5770 l = rettv->vval.v_list;
5771 if (getcurpos)
5772 fp = &curwin->w_cursor;
5773 else
5774 fp = var2fpos(&argvars[0], TRUE, &fnum);
5775 if (fnum != -1)
5776 list_append_number(l, (varnumber_T)fnum);
5777 else
5778 list_append_number(l, (varnumber_T)0);
5779 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5780 : (varnumber_T)0);
5781 list_append_number(l, (fp != NULL)
5782 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5783 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005784 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005785 (varnumber_T)0);
5786 if (getcurpos)
5787 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005788 int save_set_curswant = curwin->w_set_curswant;
5789 colnr_T save_curswant = curwin->w_curswant;
5790 colnr_T save_virtcol = curwin->w_virtcol;
5791
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005792 update_curswant();
5793 list_append_number(l, curwin->w_curswant == MAXCOL ?
5794 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005795
5796 // Do not change "curswant", as it is unexpected that a get
5797 // function has a side effect.
5798 if (save_set_curswant)
5799 {
5800 curwin->w_set_curswant = save_set_curswant;
5801 curwin->w_curswant = save_curswant;
5802 curwin->w_virtcol = save_virtcol;
5803 curwin->w_valid &= ~VALID_VIRTCOL;
5804 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005805 }
5806 }
5807 else
5808 rettv->vval.v_number = FALSE;
5809}
5810
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005811/*
5812 * "getcurpos()" function
5813 */
5814 static void
5815f_getcurpos(typval_T *argvars, typval_T *rettv)
5816{
5817 getpos_both(argvars, rettv, TRUE);
5818}
5819
5820/*
5821 * "getpos(string)" function
5822 */
5823 static void
5824f_getpos(typval_T *argvars, typval_T *rettv)
5825{
5826 getpos_both(argvars, rettv, FALSE);
5827}
5828
5829/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005830 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005831 */
5832 static void
5833f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5834{
5835#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005836 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005837#endif
5838}
5839
5840/*
5841 * "getreg()" function
5842 */
5843 static void
5844f_getreg(typval_T *argvars, typval_T *rettv)
5845{
5846 char_u *strregname;
5847 int regname;
5848 int arg2 = FALSE;
5849 int return_list = FALSE;
5850 int error = FALSE;
5851
5852 if (argvars[0].v_type != VAR_UNKNOWN)
5853 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005854 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005855 error = strregname == NULL;
5856 if (argvars[1].v_type != VAR_UNKNOWN)
5857 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005858 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005859 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005860 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005861 }
5862 }
5863 else
5864 strregname = get_vim_var_str(VV_REG);
5865
5866 if (error)
5867 return;
5868
5869 regname = (strregname == NULL ? '"' : *strregname);
5870 if (regname == 0)
5871 regname = '"';
5872
5873 if (return_list)
5874 {
5875 rettv->v_type = VAR_LIST;
5876 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5877 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5878 if (rettv->vval.v_list == NULL)
5879 (void)rettv_list_alloc(rettv);
5880 else
5881 ++rettv->vval.v_list->lv_refcount;
5882 }
5883 else
5884 {
5885 rettv->v_type = VAR_STRING;
5886 rettv->vval.v_string = get_reg_contents(regname,
5887 arg2 ? GREG_EXPR_SRC : 0);
5888 }
5889}
5890
5891/*
5892 * "getregtype()" function
5893 */
5894 static void
5895f_getregtype(typval_T *argvars, typval_T *rettv)
5896{
5897 char_u *strregname;
5898 int regname;
5899 char_u buf[NUMBUFLEN + 2];
5900 long reglen = 0;
5901
5902 if (argvars[0].v_type != VAR_UNKNOWN)
5903 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005904 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005905 if (strregname == NULL) /* type error; errmsg already given */
5906 {
5907 rettv->v_type = VAR_STRING;
5908 rettv->vval.v_string = NULL;
5909 return;
5910 }
5911 }
5912 else
5913 /* Default to v:register */
5914 strregname = get_vim_var_str(VV_REG);
5915
5916 regname = (strregname == NULL ? '"' : *strregname);
5917 if (regname == 0)
5918 regname = '"';
5919
5920 buf[0] = NUL;
5921 buf[1] = NUL;
5922 switch (get_reg_type(regname, &reglen))
5923 {
5924 case MLINE: buf[0] = 'V'; break;
5925 case MCHAR: buf[0] = 'v'; break;
5926 case MBLOCK:
5927 buf[0] = Ctrl_V;
5928 sprintf((char *)buf + 1, "%ld", reglen + 1);
5929 break;
5930 }
5931 rettv->v_type = VAR_STRING;
5932 rettv->vval.v_string = vim_strsave(buf);
5933}
5934
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005935/*
5936 * Returns information (variables, options, etc.) about a tab page
5937 * as a dictionary.
5938 */
5939 static dict_T *
5940get_tabpage_info(tabpage_T *tp, int tp_idx)
5941{
5942 win_T *wp;
5943 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005944 list_T *l;
5945
5946 dict = dict_alloc();
5947 if (dict == NULL)
5948 return NULL;
5949
Bram Moolenaare0be1672018-07-08 16:50:37 +02005950 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005951
5952 l = list_alloc();
5953 if (l != NULL)
5954 {
5955 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5956 wp; wp = wp->w_next)
5957 list_append_number(l, (varnumber_T)wp->w_id);
5958 dict_add_list(dict, "windows", l);
5959 }
5960
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005961 /* Make a reference to tabpage variables */
5962 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005963
5964 return dict;
5965}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005966
5967/*
5968 * "gettabinfo()" function
5969 */
5970 static void
5971f_gettabinfo(typval_T *argvars, typval_T *rettv)
5972{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005973 tabpage_T *tp, *tparg = NULL;
5974 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005975 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005976
5977 if (rettv_list_alloc(rettv) != OK)
5978 return;
5979
5980 if (argvars[0].v_type != VAR_UNKNOWN)
5981 {
5982 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005983 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005984 if (tparg == NULL)
5985 return;
5986 }
5987
5988 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005989 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005990 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005991 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005992 if (tparg != NULL && tp != tparg)
5993 continue;
5994 d = get_tabpage_info(tp, tpnr);
5995 if (d != NULL)
5996 list_append_dict(rettv->vval.v_list, d);
5997 if (tparg != NULL)
5998 return;
5999 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006000}
6001
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006002/*
6003 * "gettabvar()" function
6004 */
6005 static void
6006f_gettabvar(typval_T *argvars, typval_T *rettv)
6007{
6008 win_T *oldcurwin;
6009 tabpage_T *tp, *oldtabpage;
6010 dictitem_T *v;
6011 char_u *varname;
6012 int done = FALSE;
6013
6014 rettv->v_type = VAR_STRING;
6015 rettv->vval.v_string = NULL;
6016
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006017 varname = tv_get_string_chk(&argvars[1]);
6018 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006019 if (tp != NULL && varname != NULL)
6020 {
6021 /* Set tp to be our tabpage, temporarily. Also set the window to the
6022 * first window in the tabpage, otherwise the window is not valid. */
6023 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02006024 tp == curtab || tp->tp_firstwin == NULL ? firstwin
6025 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006026 {
6027 /* look up the variable */
6028 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
6029 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
6030 if (v != NULL)
6031 {
6032 copy_tv(&v->di_tv, rettv);
6033 done = TRUE;
6034 }
6035 }
6036
6037 /* restore previous notion of curwin */
6038 restore_win(oldcurwin, oldtabpage, TRUE);
6039 }
6040
6041 if (!done && argvars[2].v_type != VAR_UNKNOWN)
6042 copy_tv(&argvars[2], rettv);
6043}
6044
6045/*
6046 * "gettabwinvar()" function
6047 */
6048 static void
6049f_gettabwinvar(typval_T *argvars, typval_T *rettv)
6050{
6051 getwinvar(argvars, rettv, 1);
6052}
6053
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006054/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01006055 * "gettagstack()" function
6056 */
6057 static void
6058f_gettagstack(typval_T *argvars, typval_T *rettv)
6059{
6060 win_T *wp = curwin; // default is current window
6061
6062 if (rettv_dict_alloc(rettv) != OK)
6063 return;
6064
6065 if (argvars[0].v_type != VAR_UNKNOWN)
6066 {
6067 wp = find_win_by_nr_or_id(&argvars[0]);
6068 if (wp == NULL)
6069 return;
6070 }
6071
6072 get_tagstack(wp, rettv->vval.v_dict);
6073}
6074
6075/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006076 * Returns information about a window as a dictionary.
6077 */
6078 static dict_T *
6079get_win_info(win_T *wp, short tpnr, short winnr)
6080{
6081 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006082
6083 dict = dict_alloc();
6084 if (dict == NULL)
6085 return NULL;
6086
Bram Moolenaare0be1672018-07-08 16:50:37 +02006087 dict_add_number(dict, "tabnr", tpnr);
6088 dict_add_number(dict, "winnr", winnr);
6089 dict_add_number(dict, "winid", wp->w_id);
6090 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006091 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006092 dict_add_number(dict, "topline", wp->w_topline);
6093 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006094#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006095 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006096#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006097 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006098 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006099 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006100
Bram Moolenaar69905d12017-08-13 18:14:47 +02006101#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006102 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006103#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006104#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006105 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6106 dict_add_number(dict, "loclist",
6107 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006108#endif
6109
Bram Moolenaar30567352016-08-27 21:25:44 +02006110 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006111 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006112
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006113 return dict;
6114}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006115
6116/*
6117 * "getwininfo()" function
6118 */
6119 static void
6120f_getwininfo(typval_T *argvars, typval_T *rettv)
6121{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006122 tabpage_T *tp;
6123 win_T *wp = NULL, *wparg = NULL;
6124 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006125 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006126
6127 if (rettv_list_alloc(rettv) != OK)
6128 return;
6129
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006130 if (argvars[0].v_type != VAR_UNKNOWN)
6131 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006132 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006133 if (wparg == NULL)
6134 return;
6135 }
6136
6137 /* Collect information about either all the windows across all the tab
6138 * pages or one particular window.
6139 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006140 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006141 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006142 tabnr++;
6143 winnr = 0;
6144 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006145 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006146 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006147 if (wparg != NULL && wp != wparg)
6148 continue;
6149 d = get_win_info(wp, tabnr, winnr);
6150 if (d != NULL)
6151 list_append_dict(rettv->vval.v_list, d);
6152 if (wparg != NULL)
6153 /* found information about a specific window */
6154 return;
6155 }
6156 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006157}
6158
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006159/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006160 * "win_execute()" function
6161 */
6162 static void
6163f_win_execute(typval_T *argvars, typval_T *rettv)
6164{
6165 int id = (int)tv_get_number(argvars);
6166 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006167 win_T *save_curwin;
6168 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006169
6170 if (wp != NULL)
6171 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006172 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
6173 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006174 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006175 check_cursor();
6176 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006177 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006178 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006179 }
6180}
6181
6182/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006183 * "win_findbuf()" function
6184 */
6185 static void
6186f_win_findbuf(typval_T *argvars, typval_T *rettv)
6187{
6188 if (rettv_list_alloc(rettv) != FAIL)
6189 win_findbuf(argvars, rettv->vval.v_list);
6190}
6191
6192/*
6193 * "win_getid()" function
6194 */
6195 static void
6196f_win_getid(typval_T *argvars, typval_T *rettv)
6197{
6198 rettv->vval.v_number = win_getid(argvars);
6199}
6200
6201/*
6202 * "win_gotoid()" function
6203 */
6204 static void
6205f_win_gotoid(typval_T *argvars, typval_T *rettv)
6206{
6207 rettv->vval.v_number = win_gotoid(argvars);
6208}
6209
6210/*
6211 * "win_id2tabwin()" function
6212 */
6213 static void
6214f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6215{
6216 if (rettv_list_alloc(rettv) != FAIL)
6217 win_id2tabwin(argvars, rettv->vval.v_list);
6218}
6219
6220/*
6221 * "win_id2win()" function
6222 */
6223 static void
6224f_win_id2win(typval_T *argvars, typval_T *rettv)
6225{
6226 rettv->vval.v_number = win_id2win(argvars);
6227}
6228
6229/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006230 * "win_screenpos()" function
6231 */
6232 static void
6233f_win_screenpos(typval_T *argvars, typval_T *rettv)
6234{
6235 win_T *wp;
6236
6237 if (rettv_list_alloc(rettv) == FAIL)
6238 return;
6239
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006240 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006241 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6242 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6243}
6244
6245/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006246 * "getwinpos({timeout})" function
6247 */
6248 static void
6249f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6250{
6251 int x = -1;
6252 int y = -1;
6253
6254 if (rettv_list_alloc(rettv) == FAIL)
6255 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006256#if defined(FEAT_GUI) \
6257 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6258 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006259 {
6260 varnumber_T timeout = 100;
6261
6262 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006263 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006264
6265 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006266 }
6267#endif
6268 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6269 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6270}
6271
6272
6273/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006274 * "getwinposx()" function
6275 */
6276 static void
6277f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6278{
6279 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006280#if defined(FEAT_GUI) \
6281 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6282 || defined(MSWIN)
6283
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006284 {
6285 int x, y;
6286
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006287 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006288 rettv->vval.v_number = x;
6289 }
6290#endif
6291}
6292
6293/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006294 * "getwinposy()" function
6295 */
6296 static void
6297f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6298{
6299 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006300#if defined(FEAT_GUI) \
6301 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6302 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006303 {
6304 int x, y;
6305
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006306 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006307 rettv->vval.v_number = y;
6308 }
6309#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006310}
6311
6312/*
6313 * "getwinvar()" function
6314 */
6315 static void
6316f_getwinvar(typval_T *argvars, typval_T *rettv)
6317{
6318 getwinvar(argvars, rettv, 0);
6319}
6320
6321/*
6322 * "glob()" function
6323 */
6324 static void
6325f_glob(typval_T *argvars, typval_T *rettv)
6326{
6327 int options = WILD_SILENT|WILD_USE_NL;
6328 expand_T xpc;
6329 int error = FALSE;
6330
6331 /* When the optional second argument is non-zero, don't remove matches
6332 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6333 rettv->v_type = VAR_STRING;
6334 if (argvars[1].v_type != VAR_UNKNOWN)
6335 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006336 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006337 options |= WILD_KEEP_ALL;
6338 if (argvars[2].v_type != VAR_UNKNOWN)
6339 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006340 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006341 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006342 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006343 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006344 options |= WILD_ALLLINKS;
6345 }
6346 }
6347 if (!error)
6348 {
6349 ExpandInit(&xpc);
6350 xpc.xp_context = EXPAND_FILES;
6351 if (p_wic)
6352 options += WILD_ICASE;
6353 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006354 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006355 NULL, options, WILD_ALL);
6356 else if (rettv_list_alloc(rettv) != FAIL)
6357 {
6358 int i;
6359
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006360 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006361 NULL, options, WILD_ALL_KEEP);
6362 for (i = 0; i < xpc.xp_numfiles; i++)
6363 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6364
6365 ExpandCleanup(&xpc);
6366 }
6367 }
6368 else
6369 rettv->vval.v_string = NULL;
6370}
6371
6372/*
6373 * "globpath()" function
6374 */
6375 static void
6376f_globpath(typval_T *argvars, typval_T *rettv)
6377{
6378 int flags = 0;
6379 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006380 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006381 int error = FALSE;
6382 garray_T ga;
6383 int i;
6384
6385 /* When the optional second argument is non-zero, don't remove matches
6386 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6387 rettv->v_type = VAR_STRING;
6388 if (argvars[2].v_type != VAR_UNKNOWN)
6389 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006390 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006391 flags |= WILD_KEEP_ALL;
6392 if (argvars[3].v_type != VAR_UNKNOWN)
6393 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006394 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006395 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006396 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006397 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006398 flags |= WILD_ALLLINKS;
6399 }
6400 }
6401 if (file != NULL && !error)
6402 {
6403 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006404 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006405 if (rettv->v_type == VAR_STRING)
6406 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6407 else if (rettv_list_alloc(rettv) != FAIL)
6408 for (i = 0; i < ga.ga_len; ++i)
6409 list_append_string(rettv->vval.v_list,
6410 ((char_u **)(ga.ga_data))[i], -1);
6411 ga_clear_strings(&ga);
6412 }
6413 else
6414 rettv->vval.v_string = NULL;
6415}
6416
6417/*
6418 * "glob2regpat()" function
6419 */
6420 static void
6421f_glob2regpat(typval_T *argvars, typval_T *rettv)
6422{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006423 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006424
6425 rettv->v_type = VAR_STRING;
6426 rettv->vval.v_string = (pat == NULL)
6427 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6428}
6429
6430/* for VIM_VERSION_ defines */
6431#include "version.h"
6432
6433/*
6434 * "has()" function
6435 */
6436 static void
6437f_has(typval_T *argvars, typval_T *rettv)
6438{
6439 int i;
6440 char_u *name;
6441 int n = FALSE;
6442 static char *(has_list[]) =
6443 {
6444#ifdef AMIGA
6445 "amiga",
6446# ifdef FEAT_ARP
6447 "arp",
6448# endif
6449#endif
6450#ifdef __BEOS__
6451 "beos",
6452#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006453#if defined(BSD) && !defined(MACOS_X)
6454 "bsd",
6455#endif
6456#ifdef hpux
6457 "hpux",
6458#endif
6459#ifdef __linux__
6460 "linux",
6461#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006462#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006463 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6464 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006465# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006466 "macunix", /* Mac OS X, with the darwin feature */
6467 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006468# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006469#endif
6470#ifdef __QNX__
6471 "qnx",
6472#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006473#ifdef SUN_SYSTEM
6474 "sun",
6475#else
6476 "moon",
6477#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006478#ifdef UNIX
6479 "unix",
6480#endif
6481#ifdef VMS
6482 "vms",
6483#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006484#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006485 "win32",
6486#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006487#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006488 "win32unix",
6489#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006490#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006491 "win64",
6492#endif
6493#ifdef EBCDIC
6494 "ebcdic",
6495#endif
6496#ifndef CASE_INSENSITIVE_FILENAME
6497 "fname_case",
6498#endif
6499#ifdef HAVE_ACL
6500 "acl",
6501#endif
6502#ifdef FEAT_ARABIC
6503 "arabic",
6504#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006505 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006506#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006507 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006508#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006509#ifdef FEAT_AUTOSERVERNAME
6510 "autoservername",
6511#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006512#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006514# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006515 "balloon_multiline",
6516# endif
6517#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006518#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006519 "balloon_eval_term",
6520#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006521#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6522 "builtin_terms",
6523# ifdef ALL_BUILTIN_TCAPS
6524 "all_builtin_terms",
6525# endif
6526#endif
6527#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006528 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529 || defined(FEAT_GUI_MOTIF))
6530 "browsefilter",
6531#endif
6532#ifdef FEAT_BYTEOFF
6533 "byte_offset",
6534#endif
6535#ifdef FEAT_JOB_CHANNEL
6536 "channel",
6537#endif
6538#ifdef FEAT_CINDENT
6539 "cindent",
6540#endif
6541#ifdef FEAT_CLIENTSERVER
6542 "clientserver",
6543#endif
6544#ifdef FEAT_CLIPBOARD
6545 "clipboard",
6546#endif
6547#ifdef FEAT_CMDL_COMPL
6548 "cmdline_compl",
6549#endif
6550#ifdef FEAT_CMDHIST
6551 "cmdline_hist",
6552#endif
6553#ifdef FEAT_COMMENTS
6554 "comments",
6555#endif
6556#ifdef FEAT_CONCEAL
6557 "conceal",
6558#endif
6559#ifdef FEAT_CRYPT
6560 "cryptv",
6561 "crypt-blowfish",
6562 "crypt-blowfish2",
6563#endif
6564#ifdef FEAT_CSCOPE
6565 "cscope",
6566#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006567 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006568#ifdef CURSOR_SHAPE
6569 "cursorshape",
6570#endif
6571#ifdef DEBUG
6572 "debug",
6573#endif
6574#ifdef FEAT_CON_DIALOG
6575 "dialog_con",
6576#endif
6577#ifdef FEAT_GUI_DIALOG
6578 "dialog_gui",
6579#endif
6580#ifdef FEAT_DIFF
6581 "diff",
6582#endif
6583#ifdef FEAT_DIGRAPHS
6584 "digraphs",
6585#endif
6586#ifdef FEAT_DIRECTX
6587 "directx",
6588#endif
6589#ifdef FEAT_DND
6590 "dnd",
6591#endif
6592#ifdef FEAT_EMACS_TAGS
6593 "emacs_tags",
6594#endif
6595 "eval", /* always present, of course! */
6596 "ex_extra", /* graduated feature */
6597#ifdef FEAT_SEARCH_EXTRA
6598 "extra_search",
6599#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006600#ifdef FEAT_SEARCHPATH
6601 "file_in_path",
6602#endif
6603#ifdef FEAT_FILTERPIPE
6604 "filterpipe",
6605#endif
6606#ifdef FEAT_FIND_ID
6607 "find_in_path",
6608#endif
6609#ifdef FEAT_FLOAT
6610 "float",
6611#endif
6612#ifdef FEAT_FOLDING
6613 "folding",
6614#endif
6615#ifdef FEAT_FOOTER
6616 "footer",
6617#endif
6618#if !defined(USE_SYSTEM) && defined(UNIX)
6619 "fork",
6620#endif
6621#ifdef FEAT_GETTEXT
6622 "gettext",
6623#endif
6624#ifdef FEAT_GUI
6625 "gui",
6626#endif
6627#ifdef FEAT_GUI_ATHENA
6628# ifdef FEAT_GUI_NEXTAW
6629 "gui_neXtaw",
6630# else
6631 "gui_athena",
6632# endif
6633#endif
6634#ifdef FEAT_GUI_GTK
6635 "gui_gtk",
6636# ifdef USE_GTK3
6637 "gui_gtk3",
6638# else
6639 "gui_gtk2",
6640# endif
6641#endif
6642#ifdef FEAT_GUI_GNOME
6643 "gui_gnome",
6644#endif
6645#ifdef FEAT_GUI_MAC
6646 "gui_mac",
6647#endif
6648#ifdef FEAT_GUI_MOTIF
6649 "gui_motif",
6650#endif
6651#ifdef FEAT_GUI_PHOTON
6652 "gui_photon",
6653#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006654#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006655 "gui_win32",
6656#endif
6657#ifdef FEAT_HANGULIN
6658 "hangul_input",
6659#endif
6660#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6661 "iconv",
6662#endif
6663#ifdef FEAT_INS_EXPAND
6664 "insert_expand",
6665#endif
6666#ifdef FEAT_JOB_CHANNEL
6667 "job",
6668#endif
6669#ifdef FEAT_JUMPLIST
6670 "jumplist",
6671#endif
6672#ifdef FEAT_KEYMAP
6673 "keymap",
6674#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006675 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006676#ifdef FEAT_LANGMAP
6677 "langmap",
6678#endif
6679#ifdef FEAT_LIBCALL
6680 "libcall",
6681#endif
6682#ifdef FEAT_LINEBREAK
6683 "linebreak",
6684#endif
6685#ifdef FEAT_LISP
6686 "lispindent",
6687#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006688 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006689#ifdef FEAT_LOCALMAP
6690 "localmap",
6691#endif
6692#ifdef FEAT_LUA
6693# ifndef DYNAMIC_LUA
6694 "lua",
6695# endif
6696#endif
6697#ifdef FEAT_MENU
6698 "menu",
6699#endif
6700#ifdef FEAT_SESSION
6701 "mksession",
6702#endif
6703#ifdef FEAT_MODIFY_FNAME
6704 "modify_fname",
6705#endif
6706#ifdef FEAT_MOUSE
6707 "mouse",
6708#endif
6709#ifdef FEAT_MOUSESHAPE
6710 "mouseshape",
6711#endif
6712#if defined(UNIX) || defined(VMS)
6713# ifdef FEAT_MOUSE_DEC
6714 "mouse_dec",
6715# endif
6716# ifdef FEAT_MOUSE_GPM
6717 "mouse_gpm",
6718# endif
6719# ifdef FEAT_MOUSE_JSB
6720 "mouse_jsbterm",
6721# endif
6722# ifdef FEAT_MOUSE_NET
6723 "mouse_netterm",
6724# endif
6725# ifdef FEAT_MOUSE_PTERM
6726 "mouse_pterm",
6727# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006728# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729 "mouse_sgr",
6730# endif
6731# ifdef FEAT_SYSMOUSE
6732 "mouse_sysmouse",
6733# endif
6734# ifdef FEAT_MOUSE_URXVT
6735 "mouse_urxvt",
6736# endif
6737# ifdef FEAT_MOUSE_XTERM
6738 "mouse_xterm",
6739# endif
6740#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006741 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006742#ifdef FEAT_MBYTE_IME
6743 "multi_byte_ime",
6744#endif
6745#ifdef FEAT_MULTI_LANG
6746 "multi_lang",
6747#endif
6748#ifdef FEAT_MZSCHEME
6749#ifndef DYNAMIC_MZSCHEME
6750 "mzscheme",
6751#endif
6752#endif
6753#ifdef FEAT_NUM64
6754 "num64",
6755#endif
6756#ifdef FEAT_OLE
6757 "ole",
6758#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006759#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006760 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006761#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006762#ifdef FEAT_PATH_EXTRA
6763 "path_extra",
6764#endif
6765#ifdef FEAT_PERL
6766#ifndef DYNAMIC_PERL
6767 "perl",
6768#endif
6769#endif
6770#ifdef FEAT_PERSISTENT_UNDO
6771 "persistent_undo",
6772#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006773#if defined(FEAT_PYTHON)
6774 "python_compiled",
6775# if defined(DYNAMIC_PYTHON)
6776 "python_dynamic",
6777# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006778 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006779 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006780# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006781#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006782#if defined(FEAT_PYTHON3)
6783 "python3_compiled",
6784# if defined(DYNAMIC_PYTHON3)
6785 "python3_dynamic",
6786# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006787 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006788 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006789# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006790#endif
6791#ifdef FEAT_POSTSCRIPT
6792 "postscript",
6793#endif
6794#ifdef FEAT_PRINTER
6795 "printer",
6796#endif
6797#ifdef FEAT_PROFILE
6798 "profile",
6799#endif
6800#ifdef FEAT_RELTIME
6801 "reltime",
6802#endif
6803#ifdef FEAT_QUICKFIX
6804 "quickfix",
6805#endif
6806#ifdef FEAT_RIGHTLEFT
6807 "rightleft",
6808#endif
6809#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6810 "ruby",
6811#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006813#ifdef FEAT_CMDL_INFO
6814 "showcmd",
6815 "cmdline_info",
6816#endif
6817#ifdef FEAT_SIGNS
6818 "signs",
6819#endif
6820#ifdef FEAT_SMARTINDENT
6821 "smartindent",
6822#endif
6823#ifdef STARTUPTIME
6824 "startuptime",
6825#endif
6826#ifdef FEAT_STL_OPT
6827 "statusline",
6828#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006829#ifdef FEAT_NETBEANS_INTG
6830 "netbeans_intg",
6831#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006832#ifdef FEAT_SOUND
6833 "sound",
6834#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006835#ifdef FEAT_SPELL
6836 "spell",
6837#endif
6838#ifdef FEAT_SYN_HL
6839 "syntax",
6840#endif
6841#if defined(USE_SYSTEM) || !defined(UNIX)
6842 "system",
6843#endif
6844#ifdef FEAT_TAG_BINS
6845 "tag_binary",
6846#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006847#ifdef FEAT_TCL
6848# ifndef DYNAMIC_TCL
6849 "tcl",
6850# endif
6851#endif
6852#ifdef FEAT_TERMGUICOLORS
6853 "termguicolors",
6854#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006855#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006856 "terminal",
6857#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006858#ifdef TERMINFO
6859 "terminfo",
6860#endif
6861#ifdef FEAT_TERMRESPONSE
6862 "termresponse",
6863#endif
6864#ifdef FEAT_TEXTOBJ
6865 "textobjects",
6866#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006867#ifdef FEAT_TEXT_PROP
6868 "textprop",
6869#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006870#ifdef HAVE_TGETENT
6871 "tgetent",
6872#endif
6873#ifdef FEAT_TIMERS
6874 "timers",
6875#endif
6876#ifdef FEAT_TITLE
6877 "title",
6878#endif
6879#ifdef FEAT_TOOLBAR
6880 "toolbar",
6881#endif
6882#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6883 "unnamedplus",
6884#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006885 "user-commands", /* was accidentally included in 5.4 */
6886 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006887#ifdef FEAT_VARTABS
6888 "vartabs",
6889#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006890 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006891#ifdef FEAT_VIMINFO
6892 "viminfo",
6893#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006894 "vimscript-1",
6895 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006896 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006897 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006898 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006899 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006901#ifdef FEAT_VTP
6902 "vtp",
6903#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006904#ifdef FEAT_WILDIGN
6905 "wildignore",
6906#endif
6907#ifdef FEAT_WILDMENU
6908 "wildmenu",
6909#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006910 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911#ifdef FEAT_WAK
6912 "winaltkeys",
6913#endif
6914#ifdef FEAT_WRITEBACKUP
6915 "writebackup",
6916#endif
6917#ifdef FEAT_XIM
6918 "xim",
6919#endif
6920#ifdef FEAT_XFONTSET
6921 "xfontset",
6922#endif
6923#ifdef FEAT_XPM_W32
6924 "xpm",
6925 "xpm_w32", /* for backward compatibility */
6926#else
6927# if defined(HAVE_XPM)
6928 "xpm",
6929# endif
6930#endif
6931#ifdef USE_XSMP
6932 "xsmp",
6933#endif
6934#ifdef USE_XSMP_INTERACT
6935 "xsmp_interact",
6936#endif
6937#ifdef FEAT_XCLIPBOARD
6938 "xterm_clipboard",
6939#endif
6940#ifdef FEAT_XTERM_SAVE
6941 "xterm_save",
6942#endif
6943#if defined(UNIX) && defined(FEAT_X11)
6944 "X11",
6945#endif
6946 NULL
6947 };
6948
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006949 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006950 for (i = 0; has_list[i] != NULL; ++i)
6951 if (STRICMP(name, has_list[i]) == 0)
6952 {
6953 n = TRUE;
6954 break;
6955 }
6956
6957 if (n == FALSE)
6958 {
6959 if (STRNICMP(name, "patch", 5) == 0)
6960 {
6961 if (name[5] == '-'
6962 && STRLEN(name) >= 11
6963 && vim_isdigit(name[6])
6964 && vim_isdigit(name[8])
6965 && vim_isdigit(name[10]))
6966 {
6967 int major = atoi((char *)name + 6);
6968 int minor = atoi((char *)name + 8);
6969
6970 /* Expect "patch-9.9.01234". */
6971 n = (major < VIM_VERSION_MAJOR
6972 || (major == VIM_VERSION_MAJOR
6973 && (minor < VIM_VERSION_MINOR
6974 || (minor == VIM_VERSION_MINOR
6975 && has_patch(atoi((char *)name + 10))))));
6976 }
6977 else
6978 n = has_patch(atoi((char *)name + 5));
6979 }
6980 else if (STRICMP(name, "vim_starting") == 0)
6981 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006982 else if (STRICMP(name, "ttyin") == 0)
6983 n = mch_input_isatty();
6984 else if (STRICMP(name, "ttyout") == 0)
6985 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006986 else if (STRICMP(name, "multi_byte_encoding") == 0)
6987 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006988#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006989 else if (STRICMP(name, "balloon_multiline") == 0)
6990 n = multiline_balloon_available();
6991#endif
6992#ifdef DYNAMIC_TCL
6993 else if (STRICMP(name, "tcl") == 0)
6994 n = tcl_enabled(FALSE);
6995#endif
6996#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6997 else if (STRICMP(name, "iconv") == 0)
6998 n = iconv_enabled(FALSE);
6999#endif
7000#ifdef DYNAMIC_LUA
7001 else if (STRICMP(name, "lua") == 0)
7002 n = lua_enabled(FALSE);
7003#endif
7004#ifdef DYNAMIC_MZSCHEME
7005 else if (STRICMP(name, "mzscheme") == 0)
7006 n = mzscheme_enabled(FALSE);
7007#endif
7008#ifdef DYNAMIC_RUBY
7009 else if (STRICMP(name, "ruby") == 0)
7010 n = ruby_enabled(FALSE);
7011#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007012#ifdef DYNAMIC_PYTHON
7013 else if (STRICMP(name, "python") == 0)
7014 n = python_enabled(FALSE);
7015#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007016#ifdef DYNAMIC_PYTHON3
7017 else if (STRICMP(name, "python3") == 0)
7018 n = python3_enabled(FALSE);
7019#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007020#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
7021 else if (STRICMP(name, "pythonx") == 0)
7022 {
7023# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
7024 if (p_pyx == 0)
7025 n = python3_enabled(FALSE) || python_enabled(FALSE);
7026 else if (p_pyx == 3)
7027 n = python3_enabled(FALSE);
7028 else if (p_pyx == 2)
7029 n = python_enabled(FALSE);
7030# elif defined(DYNAMIC_PYTHON)
7031 n = python_enabled(FALSE);
7032# elif defined(DYNAMIC_PYTHON3)
7033 n = python3_enabled(FALSE);
7034# endif
7035 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036#endif
7037#ifdef DYNAMIC_PERL
7038 else if (STRICMP(name, "perl") == 0)
7039 n = perl_enabled(FALSE);
7040#endif
7041#ifdef FEAT_GUI
7042 else if (STRICMP(name, "gui_running") == 0)
7043 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044# ifdef FEAT_BROWSE
7045 else if (STRICMP(name, "browse") == 0)
7046 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
7047# endif
7048#endif
7049#ifdef FEAT_SYN_HL
7050 else if (STRICMP(name, "syntax_items") == 0)
7051 n = syntax_present(curwin);
7052#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007053#ifdef FEAT_VTP
7054 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02007055 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007056#endif
7057#ifdef FEAT_NETBEANS_INTG
7058 else if (STRICMP(name, "netbeans_enabled") == 0)
7059 n = netbeans_active();
7060#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007061#ifdef FEAT_MOUSE_GPM
7062 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
7063 n = gpm_enabled();
7064#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007065#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02007066 else if (STRICMP(name, "terminal") == 0)
7067 n = terminal_enabled();
7068#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007069#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007070 else if (STRICMP(name, "conpty") == 0)
7071 n = use_conpty();
7072#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007073 }
7074
7075 rettv->vval.v_number = n;
7076}
7077
7078/*
7079 * "has_key()" function
7080 */
7081 static void
7082f_has_key(typval_T *argvars, typval_T *rettv)
7083{
7084 if (argvars[0].v_type != VAR_DICT)
7085 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007086 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007087 return;
7088 }
7089 if (argvars[0].vval.v_dict == NULL)
7090 return;
7091
7092 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007093 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007094}
7095
7096/*
7097 * "haslocaldir()" function
7098 */
7099 static void
7100f_haslocaldir(typval_T *argvars, typval_T *rettv)
7101{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007102 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007103 win_T *wp = NULL;
7104
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007105 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7106
7107 // Check for window-local and tab-local directories
7108 if (wp != NULL && wp->w_localdir != NULL)
7109 rettv->vval.v_number = 1;
7110 else if (tp != NULL && tp->tp_localdir != NULL)
7111 rettv->vval.v_number = 2;
7112 else
7113 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007114}
7115
7116/*
7117 * "hasmapto()" function
7118 */
7119 static void
7120f_hasmapto(typval_T *argvars, typval_T *rettv)
7121{
7122 char_u *name;
7123 char_u *mode;
7124 char_u buf[NUMBUFLEN];
7125 int abbr = FALSE;
7126
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007127 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007128 if (argvars[1].v_type == VAR_UNKNOWN)
7129 mode = (char_u *)"nvo";
7130 else
7131 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007132 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007133 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007134 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007135 }
7136
7137 if (map_to_exists(name, mode, abbr))
7138 rettv->vval.v_number = TRUE;
7139 else
7140 rettv->vval.v_number = FALSE;
7141}
7142
7143/*
7144 * "histadd()" function
7145 */
7146 static void
7147f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7148{
7149#ifdef FEAT_CMDHIST
7150 int histype;
7151 char_u *str;
7152 char_u buf[NUMBUFLEN];
7153#endif
7154
7155 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007156 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007157 return;
7158#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007159 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007160 histype = str != NULL ? get_histtype(str) : -1;
7161 if (histype >= 0)
7162 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007163 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007164 if (*str != NUL)
7165 {
7166 init_history();
7167 add_to_history(histype, str, FALSE, NUL);
7168 rettv->vval.v_number = TRUE;
7169 return;
7170 }
7171 }
7172#endif
7173}
7174
7175/*
7176 * "histdel()" function
7177 */
7178 static void
7179f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7180{
7181#ifdef FEAT_CMDHIST
7182 int n;
7183 char_u buf[NUMBUFLEN];
7184 char_u *str;
7185
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007186 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007187 if (str == NULL)
7188 n = 0;
7189 else if (argvars[1].v_type == VAR_UNKNOWN)
7190 /* only one argument: clear entire history */
7191 n = clr_history(get_histtype(str));
7192 else if (argvars[1].v_type == VAR_NUMBER)
7193 /* index given: remove that entry */
7194 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007195 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007196 else
7197 /* string given: remove all matching entries */
7198 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007199 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007200 rettv->vval.v_number = n;
7201#endif
7202}
7203
7204/*
7205 * "histget()" function
7206 */
7207 static void
7208f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7209{
7210#ifdef FEAT_CMDHIST
7211 int type;
7212 int idx;
7213 char_u *str;
7214
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007215 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007216 if (str == NULL)
7217 rettv->vval.v_string = NULL;
7218 else
7219 {
7220 type = get_histtype(str);
7221 if (argvars[1].v_type == VAR_UNKNOWN)
7222 idx = get_history_idx(type);
7223 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007224 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007225 /* -1 on type error */
7226 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7227 }
7228#else
7229 rettv->vval.v_string = NULL;
7230#endif
7231 rettv->v_type = VAR_STRING;
7232}
7233
7234/*
7235 * "histnr()" function
7236 */
7237 static void
7238f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7239{
7240 int i;
7241
7242#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007243 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007244
7245 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7246 if (i >= HIST_CMD && i < HIST_COUNT)
7247 i = get_history_idx(i);
7248 else
7249#endif
7250 i = -1;
7251 rettv->vval.v_number = i;
7252}
7253
7254/*
7255 * "highlightID(name)" function
7256 */
7257 static void
7258f_hlID(typval_T *argvars, typval_T *rettv)
7259{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007260 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007261}
7262
7263/*
7264 * "highlight_exists()" function
7265 */
7266 static void
7267f_hlexists(typval_T *argvars, typval_T *rettv)
7268{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007269 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007270}
7271
7272/*
7273 * "hostname()" function
7274 */
7275 static void
7276f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7277{
7278 char_u hostname[256];
7279
7280 mch_get_host_name(hostname, 256);
7281 rettv->v_type = VAR_STRING;
7282 rettv->vval.v_string = vim_strsave(hostname);
7283}
7284
7285/*
7286 * iconv() function
7287 */
7288 static void
7289f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7290{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007291 char_u buf1[NUMBUFLEN];
7292 char_u buf2[NUMBUFLEN];
7293 char_u *from, *to, *str;
7294 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007295
7296 rettv->v_type = VAR_STRING;
7297 rettv->vval.v_string = NULL;
7298
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007299 str = tv_get_string(&argvars[0]);
7300 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7301 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007302 vimconv.vc_type = CONV_NONE;
7303 convert_setup(&vimconv, from, to);
7304
7305 /* If the encodings are equal, no conversion needed. */
7306 if (vimconv.vc_type == CONV_NONE)
7307 rettv->vval.v_string = vim_strsave(str);
7308 else
7309 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7310
7311 convert_setup(&vimconv, NULL, NULL);
7312 vim_free(from);
7313 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007314}
7315
7316/*
7317 * "indent()" function
7318 */
7319 static void
7320f_indent(typval_T *argvars, typval_T *rettv)
7321{
7322 linenr_T lnum;
7323
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007324 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007325 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7326 rettv->vval.v_number = get_indent_lnum(lnum);
7327 else
7328 rettv->vval.v_number = -1;
7329}
7330
7331/*
7332 * "index()" function
7333 */
7334 static void
7335f_index(typval_T *argvars, typval_T *rettv)
7336{
7337 list_T *l;
7338 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007339 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007340 long idx = 0;
7341 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007342 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007343
7344 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007345 if (argvars[0].v_type == VAR_BLOB)
7346 {
7347 typval_T tv;
7348 int start = 0;
7349
7350 if (argvars[2].v_type != VAR_UNKNOWN)
7351 {
7352 start = tv_get_number_chk(&argvars[2], &error);
7353 if (error)
7354 return;
7355 }
7356 b = argvars[0].vval.v_blob;
7357 if (b == NULL)
7358 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007359 if (start < 0)
7360 {
7361 start = blob_len(b) + start;
7362 if (start < 0)
7363 start = 0;
7364 }
7365
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007366 for (idx = start; idx < blob_len(b); ++idx)
7367 {
7368 tv.v_type = VAR_NUMBER;
7369 tv.vval.v_number = blob_get(b, idx);
7370 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7371 {
7372 rettv->vval.v_number = idx;
7373 return;
7374 }
7375 }
7376 return;
7377 }
7378 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007379 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007380 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007381 return;
7382 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007383
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007384 l = argvars[0].vval.v_list;
7385 if (l != NULL)
7386 {
7387 item = l->lv_first;
7388 if (argvars[2].v_type != VAR_UNKNOWN)
7389 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007390 /* Start at specified item. Use the cached index that list_find()
7391 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007392 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007393 idx = l->lv_idx;
7394 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007395 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007396 if (error)
7397 item = NULL;
7398 }
7399
7400 for ( ; item != NULL; item = item->li_next, ++idx)
7401 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7402 {
7403 rettv->vval.v_number = idx;
7404 break;
7405 }
7406 }
7407}
7408
7409static int inputsecret_flag = 0;
7410
7411/*
7412 * "input()" function
7413 * Also handles inputsecret() when inputsecret is set.
7414 */
7415 static void
7416f_input(typval_T *argvars, typval_T *rettv)
7417{
7418 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7419}
7420
7421/*
7422 * "inputdialog()" function
7423 */
7424 static void
7425f_inputdialog(typval_T *argvars, typval_T *rettv)
7426{
7427#if defined(FEAT_GUI_TEXTDIALOG)
7428 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7429 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7430 {
7431 char_u *message;
7432 char_u buf[NUMBUFLEN];
7433 char_u *defstr = (char_u *)"";
7434
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007435 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007437 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007438 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7439 else
7440 IObuff[0] = NUL;
7441 if (message != NULL && defstr != NULL
7442 && do_dialog(VIM_QUESTION, NULL, message,
7443 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7444 rettv->vval.v_string = vim_strsave(IObuff);
7445 else
7446 {
7447 if (message != NULL && defstr != NULL
7448 && argvars[1].v_type != VAR_UNKNOWN
7449 && argvars[2].v_type != VAR_UNKNOWN)
7450 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007451 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007452 else
7453 rettv->vval.v_string = NULL;
7454 }
7455 rettv->v_type = VAR_STRING;
7456 }
7457 else
7458#endif
7459 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7460}
7461
7462/*
7463 * "inputlist()" function
7464 */
7465 static void
7466f_inputlist(typval_T *argvars, typval_T *rettv)
7467{
7468 listitem_T *li;
7469 int selected;
7470 int mouse_used;
7471
7472#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007473 /* While starting up, there is no place to enter text. When running tests
7474 * with --not-a-term we assume feedkeys() will be used. */
7475 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476 return;
7477#endif
7478 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007480 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007481 return;
7482 }
7483
7484 msg_start();
7485 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7486 lines_left = Rows; /* avoid more prompt */
7487 msg_scroll = TRUE;
7488 msg_clr_eos();
7489
7490 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7491 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007492 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007493 msg_putchar('\n');
7494 }
7495
7496 /* Ask for choice. */
7497 selected = prompt_for_number(&mouse_used);
7498 if (mouse_used)
7499 selected -= lines_left;
7500
7501 rettv->vval.v_number = selected;
7502}
7503
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007504static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7505
7506/*
7507 * "inputrestore()" function
7508 */
7509 static void
7510f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7511{
7512 if (ga_userinput.ga_len > 0)
7513 {
7514 --ga_userinput.ga_len;
7515 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7516 + ga_userinput.ga_len);
7517 /* default return is zero == OK */
7518 }
7519 else if (p_verbose > 1)
7520 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007521 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007522 rettv->vval.v_number = 1; /* Failed */
7523 }
7524}
7525
7526/*
7527 * "inputsave()" function
7528 */
7529 static void
7530f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7531{
7532 /* Add an entry to the stack of typeahead storage. */
7533 if (ga_grow(&ga_userinput, 1) == OK)
7534 {
7535 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7536 + ga_userinput.ga_len);
7537 ++ga_userinput.ga_len;
7538 /* default return is zero == OK */
7539 }
7540 else
7541 rettv->vval.v_number = 1; /* Failed */
7542}
7543
7544/*
7545 * "inputsecret()" function
7546 */
7547 static void
7548f_inputsecret(typval_T *argvars, typval_T *rettv)
7549{
7550 ++cmdline_star;
7551 ++inputsecret_flag;
7552 f_input(argvars, rettv);
7553 --cmdline_star;
7554 --inputsecret_flag;
7555}
7556
7557/*
7558 * "insert()" function
7559 */
7560 static void
7561f_insert(typval_T *argvars, typval_T *rettv)
7562{
7563 long before = 0;
7564 listitem_T *item;
7565 list_T *l;
7566 int error = FALSE;
7567
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007568 if (argvars[0].v_type == VAR_BLOB)
7569 {
7570 int val, len;
7571 char_u *p;
7572
7573 len = blob_len(argvars[0].vval.v_blob);
7574 if (argvars[2].v_type != VAR_UNKNOWN)
7575 {
7576 before = (long)tv_get_number_chk(&argvars[2], &error);
7577 if (error)
7578 return; // type error; errmsg already given
7579 if (before < 0 || before > len)
7580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007581 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007582 return;
7583 }
7584 }
7585 val = tv_get_number_chk(&argvars[1], &error);
7586 if (error)
7587 return;
7588 if (val < 0 || val > 255)
7589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007590 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007591 return;
7592 }
7593
7594 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7595 return;
7596 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7597 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7598 *(p + before) = val;
7599 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7600
7601 copy_tv(&argvars[0], rettv);
7602 }
7603 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007604 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007605 else if ((l = argvars[0].vval.v_list) != NULL
7606 && !var_check_lock(l->lv_lock,
7607 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007608 {
7609 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007610 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007611 if (error)
7612 return; /* type error; errmsg already given */
7613
7614 if (before == l->lv_len)
7615 item = NULL;
7616 else
7617 {
7618 item = list_find(l, before);
7619 if (item == NULL)
7620 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007621 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007622 l = NULL;
7623 }
7624 }
7625 if (l != NULL)
7626 {
7627 list_insert_tv(l, &argvars[1], item);
7628 copy_tv(&argvars[0], rettv);
7629 }
7630 }
7631}
7632
7633/*
7634 * "invert(expr)" function
7635 */
7636 static void
7637f_invert(typval_T *argvars, typval_T *rettv)
7638{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007639 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007640}
7641
7642/*
7643 * "isdirectory()" function
7644 */
7645 static void
7646f_isdirectory(typval_T *argvars, typval_T *rettv)
7647{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007648 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007649}
7650
7651/*
7652 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7653 * or it refers to a List or Dictionary that is locked.
7654 */
7655 static int
7656tv_islocked(typval_T *tv)
7657{
7658 return (tv->v_lock & VAR_LOCKED)
7659 || (tv->v_type == VAR_LIST
7660 && tv->vval.v_list != NULL
7661 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7662 || (tv->v_type == VAR_DICT
7663 && tv->vval.v_dict != NULL
7664 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7665}
7666
7667/*
7668 * "islocked()" function
7669 */
7670 static void
7671f_islocked(typval_T *argvars, typval_T *rettv)
7672{
7673 lval_T lv;
7674 char_u *end;
7675 dictitem_T *di;
7676
7677 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007678 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007679 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007680 if (end != NULL && lv.ll_name != NULL)
7681 {
7682 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007683 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007684 else
7685 {
7686 if (lv.ll_tv == NULL)
7687 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007688 di = find_var(lv.ll_name, NULL, TRUE);
7689 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007690 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007691 /* Consider a variable locked when:
7692 * 1. the variable itself is locked
7693 * 2. the value of the variable is locked.
7694 * 3. the List or Dict value is locked.
7695 */
7696 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7697 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007698 }
7699 }
7700 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007701 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007702 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007703 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007704 else if (lv.ll_list != NULL)
7705 /* List item. */
7706 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7707 else
7708 /* Dictionary item. */
7709 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7710 }
7711 }
7712
7713 clear_lval(&lv);
7714}
7715
7716#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7717/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007718 * "isinf()" function
7719 */
7720 static void
7721f_isinf(typval_T *argvars, typval_T *rettv)
7722{
7723 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7724 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7725}
7726
7727/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007728 * "isnan()" function
7729 */
7730 static void
7731f_isnan(typval_T *argvars, typval_T *rettv)
7732{
7733 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7734 && isnan(argvars[0].vval.v_float);
7735}
7736#endif
7737
7738/*
7739 * "items(dict)" function
7740 */
7741 static void
7742f_items(typval_T *argvars, typval_T *rettv)
7743{
7744 dict_list(argvars, rettv, 2);
7745}
7746
7747#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7748/*
7749 * Get the job from the argument.
7750 * Returns NULL if the job is invalid.
7751 */
7752 static job_T *
7753get_job_arg(typval_T *tv)
7754{
7755 job_T *job;
7756
7757 if (tv->v_type != VAR_JOB)
7758 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007759 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007760 return NULL;
7761 }
7762 job = tv->vval.v_job;
7763
7764 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007765 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007766 return job;
7767}
7768
7769/*
7770 * "job_getchannel()" function
7771 */
7772 static void
7773f_job_getchannel(typval_T *argvars, typval_T *rettv)
7774{
7775 job_T *job = get_job_arg(&argvars[0]);
7776
7777 if (job != NULL)
7778 {
7779 rettv->v_type = VAR_CHANNEL;
7780 rettv->vval.v_channel = job->jv_channel;
7781 if (job->jv_channel != NULL)
7782 ++job->jv_channel->ch_refcount;
7783 }
7784}
7785
7786/*
7787 * "job_info()" function
7788 */
7789 static void
7790f_job_info(typval_T *argvars, typval_T *rettv)
7791{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007792 if (argvars[0].v_type != VAR_UNKNOWN)
7793 {
7794 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007795
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007796 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7797 job_info(job, rettv->vval.v_dict);
7798 }
7799 else if (rettv_list_alloc(rettv) == OK)
7800 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007801}
7802
7803/*
7804 * "job_setoptions()" function
7805 */
7806 static void
7807f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7808{
7809 job_T *job = get_job_arg(&argvars[0]);
7810 jobopt_T opt;
7811
7812 if (job == NULL)
7813 return;
7814 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007815 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007816 job_set_options(job, &opt);
7817 free_job_options(&opt);
7818}
7819
7820/*
7821 * "job_start()" function
7822 */
7823 static void
7824f_job_start(typval_T *argvars, typval_T *rettv)
7825{
7826 rettv->v_type = VAR_JOB;
7827 if (check_restricted() || check_secure())
7828 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007829 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007830}
7831
7832/*
7833 * "job_status()" function
7834 */
7835 static void
7836f_job_status(typval_T *argvars, typval_T *rettv)
7837{
7838 job_T *job = get_job_arg(&argvars[0]);
7839
7840 if (job != NULL)
7841 {
7842 rettv->v_type = VAR_STRING;
7843 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7844 }
7845}
7846
7847/*
7848 * "job_stop()" function
7849 */
7850 static void
7851f_job_stop(typval_T *argvars, typval_T *rettv)
7852{
7853 job_T *job = get_job_arg(&argvars[0]);
7854
7855 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007856 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007857}
7858#endif
7859
7860/*
7861 * "join()" function
7862 */
7863 static void
7864f_join(typval_T *argvars, typval_T *rettv)
7865{
7866 garray_T ga;
7867 char_u *sep;
7868
7869 if (argvars[0].v_type != VAR_LIST)
7870 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007871 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007872 return;
7873 }
7874 if (argvars[0].vval.v_list == NULL)
7875 return;
7876 if (argvars[1].v_type == VAR_UNKNOWN)
7877 sep = (char_u *)" ";
7878 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007879 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007880
7881 rettv->v_type = VAR_STRING;
7882
7883 if (sep != NULL)
7884 {
7885 ga_init2(&ga, (int)sizeof(char), 80);
7886 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7887 ga_append(&ga, NUL);
7888 rettv->vval.v_string = (char_u *)ga.ga_data;
7889 }
7890 else
7891 rettv->vval.v_string = NULL;
7892}
7893
7894/*
7895 * "js_decode()" function
7896 */
7897 static void
7898f_js_decode(typval_T *argvars, typval_T *rettv)
7899{
7900 js_read_T reader;
7901
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007902 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007903 reader.js_fill = NULL;
7904 reader.js_used = 0;
7905 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007906 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007907}
7908
7909/*
7910 * "js_encode()" function
7911 */
7912 static void
7913f_js_encode(typval_T *argvars, typval_T *rettv)
7914{
7915 rettv->v_type = VAR_STRING;
7916 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7917}
7918
7919/*
7920 * "json_decode()" function
7921 */
7922 static void
7923f_json_decode(typval_T *argvars, typval_T *rettv)
7924{
7925 js_read_T reader;
7926
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007927 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007928 reader.js_fill = NULL;
7929 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007930 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007931}
7932
7933/*
7934 * "json_encode()" function
7935 */
7936 static void
7937f_json_encode(typval_T *argvars, typval_T *rettv)
7938{
7939 rettv->v_type = VAR_STRING;
7940 rettv->vval.v_string = json_encode(&argvars[0], 0);
7941}
7942
7943/*
7944 * "keys()" function
7945 */
7946 static void
7947f_keys(typval_T *argvars, typval_T *rettv)
7948{
7949 dict_list(argvars, rettv, 0);
7950}
7951
7952/*
7953 * "last_buffer_nr()" function.
7954 */
7955 static void
7956f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7957{
7958 int n = 0;
7959 buf_T *buf;
7960
Bram Moolenaar29323592016-07-24 22:04:11 +02007961 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007962 if (n < buf->b_fnum)
7963 n = buf->b_fnum;
7964
7965 rettv->vval.v_number = n;
7966}
7967
7968/*
7969 * "len()" function
7970 */
7971 static void
7972f_len(typval_T *argvars, typval_T *rettv)
7973{
7974 switch (argvars[0].v_type)
7975 {
7976 case VAR_STRING:
7977 case VAR_NUMBER:
7978 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007979 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007980 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007981 case VAR_BLOB:
7982 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7983 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007984 case VAR_LIST:
7985 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7986 break;
7987 case VAR_DICT:
7988 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7989 break;
7990 case VAR_UNKNOWN:
7991 case VAR_SPECIAL:
7992 case VAR_FLOAT:
7993 case VAR_FUNC:
7994 case VAR_PARTIAL:
7995 case VAR_JOB:
7996 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007997 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007998 break;
7999 }
8000}
8001
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008002 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01008003libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008004{
8005#ifdef FEAT_LIBCALL
8006 char_u *string_in;
8007 char_u **string_result;
8008 int nr_result;
8009#endif
8010
8011 rettv->v_type = type;
8012 if (type != VAR_NUMBER)
8013 rettv->vval.v_string = NULL;
8014
8015 if (check_restricted() || check_secure())
8016 return;
8017
8018#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02008019 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008020 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
8021 {
8022 string_in = NULL;
8023 if (argvars[2].v_type == VAR_STRING)
8024 string_in = argvars[2].vval.v_string;
8025 if (type == VAR_NUMBER)
8026 string_result = NULL;
8027 else
8028 string_result = &rettv->vval.v_string;
8029 if (mch_libcall(argvars[0].vval.v_string,
8030 argvars[1].vval.v_string,
8031 string_in,
8032 argvars[2].vval.v_number,
8033 string_result,
8034 &nr_result) == OK
8035 && type == VAR_NUMBER)
8036 rettv->vval.v_number = nr_result;
8037 }
8038#endif
8039}
8040
8041/*
8042 * "libcall()" function
8043 */
8044 static void
8045f_libcall(typval_T *argvars, typval_T *rettv)
8046{
8047 libcall_common(argvars, rettv, VAR_STRING);
8048}
8049
8050/*
8051 * "libcallnr()" function
8052 */
8053 static void
8054f_libcallnr(typval_T *argvars, typval_T *rettv)
8055{
8056 libcall_common(argvars, rettv, VAR_NUMBER);
8057}
8058
8059/*
8060 * "line(string)" function
8061 */
8062 static void
8063f_line(typval_T *argvars, typval_T *rettv)
8064{
8065 linenr_T lnum = 0;
8066 pos_T *fp;
8067 int fnum;
8068
8069 fp = var2fpos(&argvars[0], TRUE, &fnum);
8070 if (fp != NULL)
8071 lnum = fp->lnum;
8072 rettv->vval.v_number = lnum;
8073}
8074
8075/*
8076 * "line2byte(lnum)" function
8077 */
8078 static void
8079f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
8080{
8081#ifndef FEAT_BYTEOFF
8082 rettv->vval.v_number = -1;
8083#else
8084 linenr_T lnum;
8085
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008086 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
8088 rettv->vval.v_number = -1;
8089 else
8090 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8091 if (rettv->vval.v_number >= 0)
8092 ++rettv->vval.v_number;
8093#endif
8094}
8095
8096/*
8097 * "lispindent(lnum)" function
8098 */
8099 static void
8100f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8101{
8102#ifdef FEAT_LISP
8103 pos_T pos;
8104 linenr_T lnum;
8105
8106 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008107 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008108 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8109 {
8110 curwin->w_cursor.lnum = lnum;
8111 rettv->vval.v_number = get_lisp_indent();
8112 curwin->w_cursor = pos;
8113 }
8114 else
8115#endif
8116 rettv->vval.v_number = -1;
8117}
8118
8119/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008120 * "list2str()" function
8121 */
8122 static void
8123f_list2str(typval_T *argvars, typval_T *rettv)
8124{
8125 list_T *l;
8126 listitem_T *li;
8127 garray_T ga;
8128 int utf8 = FALSE;
8129
8130 rettv->v_type = VAR_STRING;
8131 rettv->vval.v_string = NULL;
8132 if (argvars[0].v_type != VAR_LIST)
8133 {
8134 emsg(_(e_invarg));
8135 return;
8136 }
8137
8138 l = argvars[0].vval.v_list;
8139 if (l == NULL)
8140 return; // empty list results in empty string
8141
8142 if (argvars[1].v_type != VAR_UNKNOWN)
8143 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8144
8145 ga_init2(&ga, 1, 80);
8146 if (has_mbyte || utf8)
8147 {
8148 char_u buf[MB_MAXBYTES + 1];
8149 int (*char2bytes)(int, char_u *);
8150
8151 if (utf8 || enc_utf8)
8152 char2bytes = utf_char2bytes;
8153 else
8154 char2bytes = mb_char2bytes;
8155
8156 for (li = l->lv_first; li != NULL; li = li->li_next)
8157 {
8158 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8159 ga_concat(&ga, buf);
8160 }
8161 ga_append(&ga, NUL);
8162 }
8163 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8164 {
8165 for (li = l->lv_first; li != NULL; li = li->li_next)
8166 ga_append(&ga, tv_get_number(&li->li_tv));
8167 ga_append(&ga, NUL);
8168 }
8169
8170 rettv->v_type = VAR_STRING;
8171 rettv->vval.v_string = ga.ga_data;
8172}
8173
8174/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008175 * "localtime()" function
8176 */
8177 static void
8178f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8179{
8180 rettv->vval.v_number = (varnumber_T)time(NULL);
8181}
8182
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008183 static void
8184get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8185{
8186 char_u *keys;
8187 char_u *which;
8188 char_u buf[NUMBUFLEN];
8189 char_u *keys_buf = NULL;
8190 char_u *rhs;
8191 int mode;
8192 int abbr = FALSE;
8193 int get_dict = FALSE;
8194 mapblock_T *mp;
8195 int buffer_local;
8196
8197 /* return empty string for failure */
8198 rettv->v_type = VAR_STRING;
8199 rettv->vval.v_string = NULL;
8200
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008201 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008202 if (*keys == NUL)
8203 return;
8204
8205 if (argvars[1].v_type != VAR_UNKNOWN)
8206 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008207 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008208 if (argvars[2].v_type != VAR_UNKNOWN)
8209 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008210 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008211 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008212 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008213 }
8214 }
8215 else
8216 which = (char_u *)"";
8217 if (which == NULL)
8218 return;
8219
8220 mode = get_map_mode(&which, 0);
8221
8222 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8223 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8224 vim_free(keys_buf);
8225
8226 if (!get_dict)
8227 {
8228 /* Return a string. */
8229 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008230 {
8231 if (*rhs == NUL)
8232 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8233 else
8234 rettv->vval.v_string = str2special_save(rhs, FALSE);
8235 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008236
8237 }
8238 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8239 {
8240 /* Return a dictionary. */
8241 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8242 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8243 dict_T *dict = rettv->vval.v_dict;
8244
Bram Moolenaare0be1672018-07-08 16:50:37 +02008245 dict_add_string(dict, "lhs", lhs);
8246 dict_add_string(dict, "rhs", mp->m_orig_str);
8247 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8248 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8249 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008250 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8251 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008252 dict_add_number(dict, "buffer", (long)buffer_local);
8253 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8254 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008255
8256 vim_free(lhs);
8257 vim_free(mapmode);
8258 }
8259}
8260
8261#ifdef FEAT_FLOAT
8262/*
8263 * "log()" function
8264 */
8265 static void
8266f_log(typval_T *argvars, typval_T *rettv)
8267{
8268 float_T f = 0.0;
8269
8270 rettv->v_type = VAR_FLOAT;
8271 if (get_float_arg(argvars, &f) == OK)
8272 rettv->vval.v_float = log(f);
8273 else
8274 rettv->vval.v_float = 0.0;
8275}
8276
8277/*
8278 * "log10()" function
8279 */
8280 static void
8281f_log10(typval_T *argvars, typval_T *rettv)
8282{
8283 float_T f = 0.0;
8284
8285 rettv->v_type = VAR_FLOAT;
8286 if (get_float_arg(argvars, &f) == OK)
8287 rettv->vval.v_float = log10(f);
8288 else
8289 rettv->vval.v_float = 0.0;
8290}
8291#endif
8292
8293#ifdef FEAT_LUA
8294/*
8295 * "luaeval()" function
8296 */
8297 static void
8298f_luaeval(typval_T *argvars, typval_T *rettv)
8299{
8300 char_u *str;
8301 char_u buf[NUMBUFLEN];
8302
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008303 if (check_restricted() || check_secure())
8304 return;
8305
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008306 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008307 do_luaeval(str, argvars + 1, rettv);
8308}
8309#endif
8310
8311/*
8312 * "map()" function
8313 */
8314 static void
8315f_map(typval_T *argvars, typval_T *rettv)
8316{
8317 filter_map(argvars, rettv, TRUE);
8318}
8319
8320/*
8321 * "maparg()" function
8322 */
8323 static void
8324f_maparg(typval_T *argvars, typval_T *rettv)
8325{
8326 get_maparg(argvars, rettv, TRUE);
8327}
8328
8329/*
8330 * "mapcheck()" function
8331 */
8332 static void
8333f_mapcheck(typval_T *argvars, typval_T *rettv)
8334{
8335 get_maparg(argvars, rettv, FALSE);
8336}
8337
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008338typedef enum
8339{
8340 MATCH_END, /* matchend() */
8341 MATCH_MATCH, /* match() */
8342 MATCH_STR, /* matchstr() */
8343 MATCH_LIST, /* matchlist() */
8344 MATCH_POS /* matchstrpos() */
8345} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008346
8347 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008348find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008349{
8350 char_u *str = NULL;
8351 long len = 0;
8352 char_u *expr = NULL;
8353 char_u *pat;
8354 regmatch_T regmatch;
8355 char_u patbuf[NUMBUFLEN];
8356 char_u strbuf[NUMBUFLEN];
8357 char_u *save_cpo;
8358 long start = 0;
8359 long nth = 1;
8360 colnr_T startcol = 0;
8361 int match = 0;
8362 list_T *l = NULL;
8363 listitem_T *li = NULL;
8364 long idx = 0;
8365 char_u *tofree = NULL;
8366
8367 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8368 save_cpo = p_cpo;
8369 p_cpo = (char_u *)"";
8370
8371 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008372 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008373 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008374 /* type MATCH_LIST: return empty list when there are no matches.
8375 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008376 if (rettv_list_alloc(rettv) == FAIL)
8377 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008378 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008379 && (list_append_string(rettv->vval.v_list,
8380 (char_u *)"", 0) == FAIL
8381 || list_append_number(rettv->vval.v_list,
8382 (varnumber_T)-1) == FAIL
8383 || list_append_number(rettv->vval.v_list,
8384 (varnumber_T)-1) == FAIL
8385 || list_append_number(rettv->vval.v_list,
8386 (varnumber_T)-1) == FAIL))
8387 {
8388 list_free(rettv->vval.v_list);
8389 rettv->vval.v_list = NULL;
8390 goto theend;
8391 }
8392 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008393 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008394 {
8395 rettv->v_type = VAR_STRING;
8396 rettv->vval.v_string = NULL;
8397 }
8398
8399 if (argvars[0].v_type == VAR_LIST)
8400 {
8401 if ((l = argvars[0].vval.v_list) == NULL)
8402 goto theend;
8403 li = l->lv_first;
8404 }
8405 else
8406 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008407 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408 len = (long)STRLEN(str);
8409 }
8410
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008411 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008412 if (pat == NULL)
8413 goto theend;
8414
8415 if (argvars[2].v_type != VAR_UNKNOWN)
8416 {
8417 int error = FALSE;
8418
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008419 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008420 if (error)
8421 goto theend;
8422 if (l != NULL)
8423 {
8424 li = list_find(l, start);
8425 if (li == NULL)
8426 goto theend;
8427 idx = l->lv_idx; /* use the cached index */
8428 }
8429 else
8430 {
8431 if (start < 0)
8432 start = 0;
8433 if (start > len)
8434 goto theend;
8435 /* When "count" argument is there ignore matches before "start",
8436 * otherwise skip part of the string. Differs when pattern is "^"
8437 * or "\<". */
8438 if (argvars[3].v_type != VAR_UNKNOWN)
8439 startcol = start;
8440 else
8441 {
8442 str += start;
8443 len -= start;
8444 }
8445 }
8446
8447 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008448 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008449 if (error)
8450 goto theend;
8451 }
8452
8453 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8454 if (regmatch.regprog != NULL)
8455 {
8456 regmatch.rm_ic = p_ic;
8457
8458 for (;;)
8459 {
8460 if (l != NULL)
8461 {
8462 if (li == NULL)
8463 {
8464 match = FALSE;
8465 break;
8466 }
8467 vim_free(tofree);
8468 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8469 if (str == NULL)
8470 break;
8471 }
8472
8473 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8474
8475 if (match && --nth <= 0)
8476 break;
8477 if (l == NULL && !match)
8478 break;
8479
8480 /* Advance to just after the match. */
8481 if (l != NULL)
8482 {
8483 li = li->li_next;
8484 ++idx;
8485 }
8486 else
8487 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008488 startcol = (colnr_T)(regmatch.startp[0]
8489 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008490 if (startcol > (colnr_T)len
8491 || str + startcol <= regmatch.startp[0])
8492 {
8493 match = FALSE;
8494 break;
8495 }
8496 }
8497 }
8498
8499 if (match)
8500 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008501 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008502 {
8503 listitem_T *li1 = rettv->vval.v_list->lv_first;
8504 listitem_T *li2 = li1->li_next;
8505 listitem_T *li3 = li2->li_next;
8506 listitem_T *li4 = li3->li_next;
8507
8508 vim_free(li1->li_tv.vval.v_string);
8509 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8510 (int)(regmatch.endp[0] - regmatch.startp[0]));
8511 li3->li_tv.vval.v_number =
8512 (varnumber_T)(regmatch.startp[0] - expr);
8513 li4->li_tv.vval.v_number =
8514 (varnumber_T)(regmatch.endp[0] - expr);
8515 if (l != NULL)
8516 li2->li_tv.vval.v_number = (varnumber_T)idx;
8517 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008518 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519 {
8520 int i;
8521
8522 /* return list with matched string and submatches */
8523 for (i = 0; i < NSUBEXP; ++i)
8524 {
8525 if (regmatch.endp[i] == NULL)
8526 {
8527 if (list_append_string(rettv->vval.v_list,
8528 (char_u *)"", 0) == FAIL)
8529 break;
8530 }
8531 else if (list_append_string(rettv->vval.v_list,
8532 regmatch.startp[i],
8533 (int)(regmatch.endp[i] - regmatch.startp[i]))
8534 == FAIL)
8535 break;
8536 }
8537 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008538 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008539 {
8540 /* return matched string */
8541 if (l != NULL)
8542 copy_tv(&li->li_tv, rettv);
8543 else
8544 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8545 (int)(regmatch.endp[0] - regmatch.startp[0]));
8546 }
8547 else if (l != NULL)
8548 rettv->vval.v_number = idx;
8549 else
8550 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008551 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008552 rettv->vval.v_number =
8553 (varnumber_T)(regmatch.startp[0] - str);
8554 else
8555 rettv->vval.v_number =
8556 (varnumber_T)(regmatch.endp[0] - str);
8557 rettv->vval.v_number += (varnumber_T)(str - expr);
8558 }
8559 }
8560 vim_regfree(regmatch.regprog);
8561 }
8562
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008563theend:
8564 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008565 /* matchstrpos() without a list: drop the second item. */
8566 listitem_remove(rettv->vval.v_list,
8567 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008568 vim_free(tofree);
8569 p_cpo = save_cpo;
8570}
8571
8572/*
8573 * "match()" function
8574 */
8575 static void
8576f_match(typval_T *argvars, typval_T *rettv)
8577{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008578 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008579}
8580
Bram Moolenaar95e51472018-07-28 16:55:56 +02008581#ifdef FEAT_SEARCH_EXTRA
8582 static int
8583matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8584{
8585 dictitem_T *di;
8586
8587 if (tv->v_type != VAR_DICT)
8588 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008589 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008590 return FAIL;
8591 }
8592
8593 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008594 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008595 (char_u *)"conceal", FALSE);
8596
8597 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8598 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008599 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008600 if (*win == NULL)
8601 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008602 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008603 return FAIL;
8604 }
8605 }
8606
8607 return OK;
8608}
8609#endif
8610
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008611/*
8612 * "matchadd()" function
8613 */
8614 static void
8615f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8616{
8617#ifdef FEAT_SEARCH_EXTRA
8618 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008619 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8620 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008621 int prio = 10; /* default priority */
8622 int id = -1;
8623 int error = FALSE;
8624 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008625 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008626
8627 rettv->vval.v_number = -1;
8628
8629 if (grp == NULL || pat == NULL)
8630 return;
8631 if (argvars[2].v_type != VAR_UNKNOWN)
8632 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008633 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634 if (argvars[3].v_type != VAR_UNKNOWN)
8635 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008636 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008637 if (argvars[4].v_type != VAR_UNKNOWN
8638 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8639 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008640 }
8641 }
8642 if (error == TRUE)
8643 return;
8644 if (id >= 1 && id <= 3)
8645 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008646 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008647 return;
8648 }
8649
Bram Moolenaar95e51472018-07-28 16:55:56 +02008650 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651 conceal_char);
8652#endif
8653}
8654
8655/*
8656 * "matchaddpos()" function
8657 */
8658 static void
8659f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8660{
8661#ifdef FEAT_SEARCH_EXTRA
8662 char_u buf[NUMBUFLEN];
8663 char_u *group;
8664 int prio = 10;
8665 int id = -1;
8666 int error = FALSE;
8667 list_T *l;
8668 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008669 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008670
8671 rettv->vval.v_number = -1;
8672
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008673 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674 if (group == NULL)
8675 return;
8676
8677 if (argvars[1].v_type != VAR_LIST)
8678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008679 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680 return;
8681 }
8682 l = argvars[1].vval.v_list;
8683 if (l == NULL)
8684 return;
8685
8686 if (argvars[2].v_type != VAR_UNKNOWN)
8687 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008688 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008689 if (argvars[3].v_type != VAR_UNKNOWN)
8690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008691 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008692
8693 if (argvars[4].v_type != VAR_UNKNOWN
8694 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8695 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008696 }
8697 }
8698 if (error == TRUE)
8699 return;
8700
8701 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8702 if (id == 1 || id == 2)
8703 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008704 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008705 return;
8706 }
8707
Bram Moolenaar95e51472018-07-28 16:55:56 +02008708 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709 conceal_char);
8710#endif
8711}
8712
8713/*
8714 * "matcharg()" function
8715 */
8716 static void
8717f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8718{
8719 if (rettv_list_alloc(rettv) == OK)
8720 {
8721#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008722 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723 matchitem_T *m;
8724
8725 if (id >= 1 && id <= 3)
8726 {
8727 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8728 {
8729 list_append_string(rettv->vval.v_list,
8730 syn_id2name(m->hlg_id), -1);
8731 list_append_string(rettv->vval.v_list, m->pattern, -1);
8732 }
8733 else
8734 {
8735 list_append_string(rettv->vval.v_list, NULL, -1);
8736 list_append_string(rettv->vval.v_list, NULL, -1);
8737 }
8738 }
8739#endif
8740 }
8741}
8742
8743/*
8744 * "matchdelete()" function
8745 */
8746 static void
8747f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8748{
8749#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008750 win_T *win = get_optional_window(argvars, 1);
8751
8752 if (win == NULL)
8753 rettv->vval.v_number = -1;
8754 else
8755 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008756 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757#endif
8758}
8759
8760/*
8761 * "matchend()" function
8762 */
8763 static void
8764f_matchend(typval_T *argvars, typval_T *rettv)
8765{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008766 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008767}
8768
8769/*
8770 * "matchlist()" function
8771 */
8772 static void
8773f_matchlist(typval_T *argvars, typval_T *rettv)
8774{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008775 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776}
8777
8778/*
8779 * "matchstr()" function
8780 */
8781 static void
8782f_matchstr(typval_T *argvars, typval_T *rettv)
8783{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008784 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008785}
8786
8787/*
8788 * "matchstrpos()" function
8789 */
8790 static void
8791f_matchstrpos(typval_T *argvars, typval_T *rettv)
8792{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008793 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794}
8795
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 static void
8797max_min(typval_T *argvars, typval_T *rettv, int domax)
8798{
8799 varnumber_T n = 0;
8800 varnumber_T i;
8801 int error = FALSE;
8802
8803 if (argvars[0].v_type == VAR_LIST)
8804 {
8805 list_T *l;
8806 listitem_T *li;
8807
8808 l = argvars[0].vval.v_list;
8809 if (l != NULL)
8810 {
8811 li = l->lv_first;
8812 if (li != NULL)
8813 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008814 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008815 for (;;)
8816 {
8817 li = li->li_next;
8818 if (li == NULL)
8819 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008820 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008821 if (domax ? i > n : i < n)
8822 n = i;
8823 }
8824 }
8825 }
8826 }
8827 else if (argvars[0].v_type == VAR_DICT)
8828 {
8829 dict_T *d;
8830 int first = TRUE;
8831 hashitem_T *hi;
8832 int todo;
8833
8834 d = argvars[0].vval.v_dict;
8835 if (d != NULL)
8836 {
8837 todo = (int)d->dv_hashtab.ht_used;
8838 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8839 {
8840 if (!HASHITEM_EMPTY(hi))
8841 {
8842 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008843 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008844 if (first)
8845 {
8846 n = i;
8847 first = FALSE;
8848 }
8849 else if (domax ? i > n : i < n)
8850 n = i;
8851 }
8852 }
8853 }
8854 }
8855 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008856 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008857 rettv->vval.v_number = error ? 0 : n;
8858}
8859
8860/*
8861 * "max()" function
8862 */
8863 static void
8864f_max(typval_T *argvars, typval_T *rettv)
8865{
8866 max_min(argvars, rettv, TRUE);
8867}
8868
8869/*
8870 * "min()" function
8871 */
8872 static void
8873f_min(typval_T *argvars, typval_T *rettv)
8874{
8875 max_min(argvars, rettv, FALSE);
8876}
8877
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008878/*
8879 * Create the directory in which "dir" is located, and higher levels when
8880 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008881 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008882 */
8883 static int
8884mkdir_recurse(char_u *dir, int prot)
8885{
8886 char_u *p;
8887 char_u *updir;
8888 int r = FAIL;
8889
8890 /* Get end of directory name in "dir".
8891 * We're done when it's "/" or "c:/". */
8892 p = gettail_sep(dir);
8893 if (p <= get_past_head(dir))
8894 return OK;
8895
8896 /* If the directory exists we're done. Otherwise: create it.*/
8897 updir = vim_strnsave(dir, (int)(p - dir));
8898 if (updir == NULL)
8899 return FAIL;
8900 if (mch_isdir(updir))
8901 r = OK;
8902 else if (mkdir_recurse(updir, prot) == OK)
8903 r = vim_mkdir_emsg(updir, prot);
8904 vim_free(updir);
8905 return r;
8906}
8907
8908#ifdef vim_mkdir
8909/*
8910 * "mkdir()" function
8911 */
8912 static void
8913f_mkdir(typval_T *argvars, typval_T *rettv)
8914{
8915 char_u *dir;
8916 char_u buf[NUMBUFLEN];
8917 int prot = 0755;
8918
8919 rettv->vval.v_number = FAIL;
8920 if (check_restricted() || check_secure())
8921 return;
8922
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008923 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008925 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008927 if (*gettail(dir) == NUL)
8928 /* remove trailing slashes */
8929 *gettail_sep(dir) = NUL;
8930
8931 if (argvars[1].v_type != VAR_UNKNOWN)
8932 {
8933 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008934 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008935 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008936 if (prot == -1)
8937 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008938 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008939 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008940 {
8941 if (mch_isdir(dir))
8942 {
8943 /* With the "p" flag it's OK if the dir already exists. */
8944 rettv->vval.v_number = OK;
8945 return;
8946 }
8947 mkdir_recurse(dir, prot);
8948 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008949 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008950 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008951}
8952#endif
8953
8954/*
8955 * "mode()" function
8956 */
8957 static void
8958f_mode(typval_T *argvars, typval_T *rettv)
8959{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008960 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961
Bram Moolenaar612cc382018-07-29 15:34:26 +02008962 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008963
8964 if (time_for_testing == 93784)
8965 {
8966 /* Testing the two-character code. */
8967 buf[0] = 'x';
8968 buf[1] = '!';
8969 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008970#ifdef FEAT_TERMINAL
8971 else if (term_use_loop())
8972 buf[0] = 't';
8973#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008974 else if (VIsual_active)
8975 {
8976 if (VIsual_select)
8977 buf[0] = VIsual_mode + 's' - 'v';
8978 else
8979 buf[0] = VIsual_mode;
8980 }
8981 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8982 || State == CONFIRM)
8983 {
8984 buf[0] = 'r';
8985 if (State == ASKMORE)
8986 buf[1] = 'm';
8987 else if (State == CONFIRM)
8988 buf[1] = '?';
8989 }
8990 else if (State == EXTERNCMD)
8991 buf[0] = '!';
8992 else if (State & INSERT)
8993 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008994 if (State & VREPLACE_FLAG)
8995 {
8996 buf[0] = 'R';
8997 buf[1] = 'v';
8998 }
8999 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01009000 {
9001 if (State & REPLACE_FLAG)
9002 buf[0] = 'R';
9003 else
9004 buf[0] = 'i';
9005#ifdef FEAT_INS_EXPAND
9006 if (ins_compl_active())
9007 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01009008 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01009009 buf[1] = 'x';
9010#endif
9011 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009012 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01009013 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009014 {
9015 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01009016 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01009018 else if (exmode_active == EXMODE_NORMAL)
9019 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020 }
9021 else
9022 {
9023 buf[0] = 'n';
9024 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01009025 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01009027 // to be able to detect force-linewise/blockwise/characterwise operations
9028 buf[2] = motion_force;
9029 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02009030 else if (restart_edit == 'I' || restart_edit == 'R'
9031 || restart_edit == 'V')
9032 {
9033 buf[1] = 'i';
9034 buf[2] = restart_edit;
9035 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036 }
9037
9038 /* Clear out the minor mode when the argument is not a non-zero number or
9039 * non-empty string. */
9040 if (!non_zero_arg(&argvars[0]))
9041 buf[1] = NUL;
9042
9043 rettv->vval.v_string = vim_strsave(buf);
9044 rettv->v_type = VAR_STRING;
9045}
9046
9047#if defined(FEAT_MZSCHEME) || defined(PROTO)
9048/*
9049 * "mzeval()" function
9050 */
9051 static void
9052f_mzeval(typval_T *argvars, typval_T *rettv)
9053{
9054 char_u *str;
9055 char_u buf[NUMBUFLEN];
9056
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009057 if (check_restricted() || check_secure())
9058 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009059 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009060 do_mzeval(str, rettv);
9061}
9062
9063 void
9064mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
9065{
9066 typval_T argvars[3];
9067
9068 argvars[0].v_type = VAR_STRING;
9069 argvars[0].vval.v_string = name;
9070 copy_tv(args, &argvars[1]);
9071 argvars[2].v_type = VAR_UNKNOWN;
9072 f_call(argvars, rettv);
9073 clear_tv(&argvars[1]);
9074}
9075#endif
9076
9077/*
9078 * "nextnonblank()" function
9079 */
9080 static void
9081f_nextnonblank(typval_T *argvars, typval_T *rettv)
9082{
9083 linenr_T lnum;
9084
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009085 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009086 {
9087 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
9088 {
9089 lnum = 0;
9090 break;
9091 }
9092 if (*skipwhite(ml_get(lnum)) != NUL)
9093 break;
9094 }
9095 rettv->vval.v_number = lnum;
9096}
9097
9098/*
9099 * "nr2char()" function
9100 */
9101 static void
9102f_nr2char(typval_T *argvars, typval_T *rettv)
9103{
9104 char_u buf[NUMBUFLEN];
9105
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009106 if (has_mbyte)
9107 {
9108 int utf8 = 0;
9109
9110 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009111 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009113 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009114 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009115 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009116 }
9117 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009118 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009119 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009120 buf[1] = NUL;
9121 }
9122 rettv->v_type = VAR_STRING;
9123 rettv->vval.v_string = vim_strsave(buf);
9124}
9125
9126/*
9127 * "or(expr, expr)" function
9128 */
9129 static void
9130f_or(typval_T *argvars, typval_T *rettv)
9131{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009132 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9133 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009134}
9135
9136/*
9137 * "pathshorten()" function
9138 */
9139 static void
9140f_pathshorten(typval_T *argvars, typval_T *rettv)
9141{
9142 char_u *p;
9143
9144 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009145 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009146 if (p == NULL)
9147 rettv->vval.v_string = NULL;
9148 else
9149 {
9150 p = vim_strsave(p);
9151 rettv->vval.v_string = p;
9152 if (p != NULL)
9153 shorten_dir(p);
9154 }
9155}
9156
9157#ifdef FEAT_PERL
9158/*
9159 * "perleval()" function
9160 */
9161 static void
9162f_perleval(typval_T *argvars, typval_T *rettv)
9163{
9164 char_u *str;
9165 char_u buf[NUMBUFLEN];
9166
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009167 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009168 do_perleval(str, rettv);
9169}
9170#endif
9171
9172#ifdef FEAT_FLOAT
9173/*
9174 * "pow()" function
9175 */
9176 static void
9177f_pow(typval_T *argvars, typval_T *rettv)
9178{
9179 float_T fx = 0.0, fy = 0.0;
9180
9181 rettv->v_type = VAR_FLOAT;
9182 if (get_float_arg(argvars, &fx) == OK
9183 && get_float_arg(&argvars[1], &fy) == OK)
9184 rettv->vval.v_float = pow(fx, fy);
9185 else
9186 rettv->vval.v_float = 0.0;
9187}
9188#endif
9189
9190/*
9191 * "prevnonblank()" function
9192 */
9193 static void
9194f_prevnonblank(typval_T *argvars, typval_T *rettv)
9195{
9196 linenr_T lnum;
9197
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009198 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009199 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9200 lnum = 0;
9201 else
9202 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9203 --lnum;
9204 rettv->vval.v_number = lnum;
9205}
9206
9207/* This dummy va_list is here because:
9208 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9209 * - locally in the function results in a "used before set" warning
9210 * - using va_start() to initialize it gives "function with fixed args" error */
9211static va_list ap;
9212
9213/*
9214 * "printf()" function
9215 */
9216 static void
9217f_printf(typval_T *argvars, typval_T *rettv)
9218{
9219 char_u buf[NUMBUFLEN];
9220 int len;
9221 char_u *s;
9222 int saved_did_emsg = did_emsg;
9223 char *fmt;
9224
9225 rettv->v_type = VAR_STRING;
9226 rettv->vval.v_string = NULL;
9227
9228 /* Get the required length, allocate the buffer and do it for real. */
9229 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009230 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009231 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009232 if (!did_emsg)
9233 {
9234 s = alloc(len + 1);
9235 if (s != NULL)
9236 {
9237 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009238 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9239 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009240 }
9241 }
9242 did_emsg |= saved_did_emsg;
9243}
9244
Bram Moolenaarf2732452018-06-03 14:47:35 +02009245#ifdef FEAT_JOB_CHANNEL
9246/*
9247 * "prompt_setcallback({buffer}, {callback})" function
9248 */
9249 static void
9250f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9251{
9252 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009253 callback_T callback;
Bram Moolenaarf2732452018-06-03 14:47:35 +02009254
9255 if (check_secure())
9256 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009257 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009258 if (buf == NULL)
9259 return;
9260
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009261 callback = get_callback(&argvars[1]);
9262 if (callback.cb_name == NULL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02009263 return;
9264
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009265 free_callback(&buf->b_prompt_callback);
9266 set_callback(&buf->b_prompt_callback, &callback);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009267}
9268
9269/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009270 * "prompt_setinterrupt({buffer}, {callback})" function
9271 */
9272 static void
9273f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9274{
9275 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009276 callback_T callback;
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009277
9278 if (check_secure())
9279 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009280 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009281 if (buf == NULL)
9282 return;
9283
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009284 callback = get_callback(&argvars[1]);
9285 if (callback.cb_name == NULL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009286 return;
9287
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009288 free_callback(&buf->b_prompt_interrupt);
9289 set_callback(&buf->b_prompt_interrupt, &callback);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009290}
9291
9292/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009293 * "prompt_setprompt({buffer}, {text})" function
9294 */
9295 static void
9296f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9297{
9298 buf_T *buf;
9299 char_u *text;
9300
9301 if (check_secure())
9302 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009303 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009304 if (buf == NULL)
9305 return;
9306
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009307 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009308 vim_free(buf->b_prompt_text);
9309 buf->b_prompt_text = vim_strsave(text);
9310}
9311#endif
9312
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009313/*
9314 * "pumvisible()" function
9315 */
9316 static void
9317f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9318{
9319#ifdef FEAT_INS_EXPAND
9320 if (pum_visible())
9321 rettv->vval.v_number = 1;
9322#endif
9323}
9324
9325#ifdef FEAT_PYTHON3
9326/*
9327 * "py3eval()" function
9328 */
9329 static void
9330f_py3eval(typval_T *argvars, typval_T *rettv)
9331{
9332 char_u *str;
9333 char_u buf[NUMBUFLEN];
9334
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009335 if (check_restricted() || check_secure())
9336 return;
9337
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009338 if (p_pyx == 0)
9339 p_pyx = 3;
9340
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009341 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009342 do_py3eval(str, rettv);
9343}
9344#endif
9345
9346#ifdef FEAT_PYTHON
9347/*
9348 * "pyeval()" function
9349 */
9350 static void
9351f_pyeval(typval_T *argvars, typval_T *rettv)
9352{
9353 char_u *str;
9354 char_u buf[NUMBUFLEN];
9355
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009356 if (check_restricted() || check_secure())
9357 return;
9358
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009359 if (p_pyx == 0)
9360 p_pyx = 2;
9361
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009362 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009363 do_pyeval(str, rettv);
9364}
9365#endif
9366
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009367#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9368/*
9369 * "pyxeval()" function
9370 */
9371 static void
9372f_pyxeval(typval_T *argvars, typval_T *rettv)
9373{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009374 if (check_restricted() || check_secure())
9375 return;
9376
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009377# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9378 init_pyxversion();
9379 if (p_pyx == 2)
9380 f_pyeval(argvars, rettv);
9381 else
9382 f_py3eval(argvars, rettv);
9383# elif defined(FEAT_PYTHON)
9384 f_pyeval(argvars, rettv);
9385# elif defined(FEAT_PYTHON3)
9386 f_py3eval(argvars, rettv);
9387# endif
9388}
9389#endif
9390
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009391/*
9392 * "range()" function
9393 */
9394 static void
9395f_range(typval_T *argvars, typval_T *rettv)
9396{
9397 varnumber_T start;
9398 varnumber_T end;
9399 varnumber_T stride = 1;
9400 varnumber_T i;
9401 int error = FALSE;
9402
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009403 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009404 if (argvars[1].v_type == VAR_UNKNOWN)
9405 {
9406 end = start - 1;
9407 start = 0;
9408 }
9409 else
9410 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009411 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009413 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009414 }
9415
9416 if (error)
9417 return; /* type error; errmsg already given */
9418 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009419 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009420 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009421 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009422 else
9423 {
9424 if (rettv_list_alloc(rettv) == OK)
9425 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9426 if (list_append_number(rettv->vval.v_list,
9427 (varnumber_T)i) == FAIL)
9428 break;
9429 }
9430}
9431
9432/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009433 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009434 */
9435 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009436readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009437{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009438 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009439 typval_T save_val;
9440 typval_T rettv;
9441 typval_T argv[2];
9442 int retval = 0;
9443 int error = FALSE;
9444
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009445 if (expr->v_type == VAR_UNKNOWN)
9446 return 1;
9447
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009448 prepare_vimvar(VV_VAL, &save_val);
9449 set_vim_var_string(VV_VAL, name, -1);
9450 argv[0].v_type = VAR_STRING;
9451 argv[0].vval.v_string = name;
9452
9453 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9454 goto theend;
9455
9456 retval = tv_get_number_chk(&rettv, &error);
9457 if (error)
9458 retval = -1;
9459 clear_tv(&rettv);
9460
9461theend:
9462 set_vim_var_string(VV_VAL, NULL, 0);
9463 restore_vimvar(VV_VAL, &save_val);
9464 return retval;
9465}
9466
9467/*
9468 * "readdir()" function
9469 */
9470 static void
9471f_readdir(typval_T *argvars, typval_T *rettv)
9472{
9473 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009474 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009475 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009476 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009477 garray_T ga;
9478 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009479
9480 if (rettv_list_alloc(rettv) == FAIL)
9481 return;
9482 path = tv_get_string(&argvars[0]);
9483 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009484
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009485 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9486 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009487 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009488 for (i = 0; i < ga.ga_len; i++)
9489 {
9490 p = ((char_u **)ga.ga_data)[i];
9491 list_append_string(rettv->vval.v_list, p, -1);
9492 }
9493 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009494 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009495}
9496
9497/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009498 * "readfile()" function
9499 */
9500 static void
9501f_readfile(typval_T *argvars, typval_T *rettv)
9502{
9503 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009504 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009505 int failed = FALSE;
9506 char_u *fname;
9507 FILE *fd;
9508 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9509 int io_size = sizeof(buf);
9510 int readlen; /* size of last fread() */
9511 char_u *prev = NULL; /* previously read bytes, if any */
9512 long prevlen = 0; /* length of data in prev */
9513 long prevsize = 0; /* size of prev buffer */
9514 long maxline = MAXLNUM;
9515 long cnt = 0;
9516 char_u *p; /* position in buf */
9517 char_u *start; /* start of current line */
9518
9519 if (argvars[1].v_type != VAR_UNKNOWN)
9520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009521 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009522 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009523 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9524 blob = TRUE;
9525
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009526 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009527 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009528 }
9529
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009530 if (blob)
9531 {
9532 if (rettv_blob_alloc(rettv) == FAIL)
9533 return;
9534 }
9535 else
9536 {
9537 if (rettv_list_alloc(rettv) == FAIL)
9538 return;
9539 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009540
9541 /* Always open the file in binary mode, library functions have a mind of
9542 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009543 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009544 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9545 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009546 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009547 return;
9548 }
9549
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009550 if (blob)
9551 {
9552 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9553 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009554 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009555 blob_free(rettv->vval.v_blob);
9556 }
9557 fclose(fd);
9558 return;
9559 }
9560
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009561 while (cnt < maxline || maxline < 0)
9562 {
9563 readlen = (int)fread(buf, 1, io_size, fd);
9564
9565 /* This for loop processes what was read, but is also entered at end
9566 * of file so that either:
9567 * - an incomplete line gets written
9568 * - a "binary" file gets an empty line at the end if it ends in a
9569 * newline. */
9570 for (p = buf, start = buf;
9571 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9572 ++p)
9573 {
9574 if (*p == '\n' || readlen <= 0)
9575 {
9576 listitem_T *li;
9577 char_u *s = NULL;
9578 long_u len = p - start;
9579
9580 /* Finished a line. Remove CRs before NL. */
9581 if (readlen > 0 && !binary)
9582 {
9583 while (len > 0 && start[len - 1] == '\r')
9584 --len;
9585 /* removal may cross back to the "prev" string */
9586 if (len == 0)
9587 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9588 --prevlen;
9589 }
9590 if (prevlen == 0)
9591 s = vim_strnsave(start, (int)len);
9592 else
9593 {
9594 /* Change "prev" buffer to be the right size. This way
9595 * the bytes are only copied once, and very long lines are
9596 * allocated only once. */
9597 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9598 {
9599 mch_memmove(s + prevlen, start, len);
9600 s[prevlen + len] = NUL;
9601 prev = NULL; /* the list will own the string */
9602 prevlen = prevsize = 0;
9603 }
9604 }
9605 if (s == NULL)
9606 {
9607 do_outofmem_msg((long_u) prevlen + len + 1);
9608 failed = TRUE;
9609 break;
9610 }
9611
9612 if ((li = listitem_alloc()) == NULL)
9613 {
9614 vim_free(s);
9615 failed = TRUE;
9616 break;
9617 }
9618 li->li_tv.v_type = VAR_STRING;
9619 li->li_tv.v_lock = 0;
9620 li->li_tv.vval.v_string = s;
9621 list_append(rettv->vval.v_list, li);
9622
9623 start = p + 1; /* step over newline */
9624 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9625 break;
9626 }
9627 else if (*p == NUL)
9628 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009629 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9630 * when finding the BF and check the previous two bytes. */
9631 else if (*p == 0xbf && enc_utf8 && !binary)
9632 {
9633 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9634 * + 1, these may be in the "prev" string. */
9635 char_u back1 = p >= buf + 1 ? p[-1]
9636 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9637 char_u back2 = p >= buf + 2 ? p[-2]
9638 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9639 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9640
9641 if (back2 == 0xef && back1 == 0xbb)
9642 {
9643 char_u *dest = p - 2;
9644
9645 /* Usually a BOM is at the beginning of a file, and so at
9646 * the beginning of a line; then we can just step over it.
9647 */
9648 if (start == dest)
9649 start = p + 1;
9650 else
9651 {
9652 /* have to shuffle buf to close gap */
9653 int adjust_prevlen = 0;
9654
9655 if (dest < buf)
9656 {
9657 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9658 dest = buf;
9659 }
9660 if (readlen > p - buf + 1)
9661 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9662 readlen -= 3 - adjust_prevlen;
9663 prevlen -= adjust_prevlen;
9664 p = dest - 1;
9665 }
9666 }
9667 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009668 } /* for */
9669
9670 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9671 break;
9672 if (start < p)
9673 {
9674 /* There's part of a line in buf, store it in "prev". */
9675 if (p - start + prevlen >= prevsize)
9676 {
9677 /* need bigger "prev" buffer */
9678 char_u *newprev;
9679
9680 /* A common use case is ordinary text files and "prev" gets a
9681 * fragment of a line, so the first allocation is made
9682 * small, to avoid repeatedly 'allocing' large and
9683 * 'reallocing' small. */
9684 if (prevsize == 0)
9685 prevsize = (long)(p - start);
9686 else
9687 {
9688 long grow50pc = (prevsize * 3) / 2;
9689 long growmin = (long)((p - start) * 2 + prevlen);
9690 prevsize = grow50pc > growmin ? grow50pc : growmin;
9691 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009692 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009693 if (newprev == NULL)
9694 {
9695 do_outofmem_msg((long_u)prevsize);
9696 failed = TRUE;
9697 break;
9698 }
9699 prev = newprev;
9700 }
9701 /* Add the line part to end of "prev". */
9702 mch_memmove(prev + prevlen, start, p - start);
9703 prevlen += (long)(p - start);
9704 }
9705 } /* while */
9706
9707 /*
9708 * For a negative line count use only the lines at the end of the file,
9709 * free the rest.
9710 */
9711 if (!failed && maxline < 0)
9712 while (cnt > -maxline)
9713 {
9714 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9715 --cnt;
9716 }
9717
9718 if (failed)
9719 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009720 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009721 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009722 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009723 }
9724
9725 vim_free(prev);
9726 fclose(fd);
9727}
9728
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009729 static void
9730return_register(int regname, typval_T *rettv)
9731{
9732 char_u buf[2] = {0, 0};
9733
9734 buf[0] = (char_u)regname;
9735 rettv->v_type = VAR_STRING;
9736 rettv->vval.v_string = vim_strsave(buf);
9737}
9738
9739/*
9740 * "reg_executing()" function
9741 */
9742 static void
9743f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9744{
9745 return_register(reg_executing, rettv);
9746}
9747
9748/*
9749 * "reg_recording()" function
9750 */
9751 static void
9752f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9753{
9754 return_register(reg_recording, rettv);
9755}
9756
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009757#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009758/*
9759 * Convert a List to proftime_T.
9760 * Return FAIL when there is something wrong.
9761 */
9762 static int
9763list2proftime(typval_T *arg, proftime_T *tm)
9764{
9765 long n1, n2;
9766 int error = FALSE;
9767
9768 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9769 || arg->vval.v_list->lv_len != 2)
9770 return FAIL;
9771 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9772 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009773# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009774 tm->HighPart = n1;
9775 tm->LowPart = n2;
9776# else
9777 tm->tv_sec = n1;
9778 tm->tv_usec = n2;
9779# endif
9780 return error ? FAIL : OK;
9781}
9782#endif /* FEAT_RELTIME */
9783
9784/*
9785 * "reltime()" function
9786 */
9787 static void
9788f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9789{
9790#ifdef FEAT_RELTIME
9791 proftime_T res;
9792 proftime_T start;
9793
9794 if (argvars[0].v_type == VAR_UNKNOWN)
9795 {
9796 /* No arguments: get current time. */
9797 profile_start(&res);
9798 }
9799 else if (argvars[1].v_type == VAR_UNKNOWN)
9800 {
9801 if (list2proftime(&argvars[0], &res) == FAIL)
9802 return;
9803 profile_end(&res);
9804 }
9805 else
9806 {
9807 /* Two arguments: compute the difference. */
9808 if (list2proftime(&argvars[0], &start) == FAIL
9809 || list2proftime(&argvars[1], &res) == FAIL)
9810 return;
9811 profile_sub(&res, &start);
9812 }
9813
9814 if (rettv_list_alloc(rettv) == OK)
9815 {
9816 long n1, n2;
9817
Bram Moolenaar4f974752019-02-17 17:44:42 +01009818# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009819 n1 = res.HighPart;
9820 n2 = res.LowPart;
9821# else
9822 n1 = res.tv_sec;
9823 n2 = res.tv_usec;
9824# endif
9825 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9826 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9827 }
9828#endif
9829}
9830
9831#ifdef FEAT_FLOAT
9832/*
9833 * "reltimefloat()" function
9834 */
9835 static void
9836f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9837{
9838# ifdef FEAT_RELTIME
9839 proftime_T tm;
9840# endif
9841
9842 rettv->v_type = VAR_FLOAT;
9843 rettv->vval.v_float = 0;
9844# ifdef FEAT_RELTIME
9845 if (list2proftime(&argvars[0], &tm) == OK)
9846 rettv->vval.v_float = profile_float(&tm);
9847# endif
9848}
9849#endif
9850
9851/*
9852 * "reltimestr()" function
9853 */
9854 static void
9855f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9856{
9857#ifdef FEAT_RELTIME
9858 proftime_T tm;
9859#endif
9860
9861 rettv->v_type = VAR_STRING;
9862 rettv->vval.v_string = NULL;
9863#ifdef FEAT_RELTIME
9864 if (list2proftime(&argvars[0], &tm) == OK)
9865 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9866#endif
9867}
9868
9869#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009870 static void
9871make_connection(void)
9872{
9873 if (X_DISPLAY == NULL
9874# ifdef FEAT_GUI
9875 && !gui.in_use
9876# endif
9877 )
9878 {
9879 x_force_connect = TRUE;
9880 setup_term_clip();
9881 x_force_connect = FALSE;
9882 }
9883}
9884
9885 static int
9886check_connection(void)
9887{
9888 make_connection();
9889 if (X_DISPLAY == NULL)
9890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009891 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009892 return FAIL;
9893 }
9894 return OK;
9895}
9896#endif
9897
9898#ifdef FEAT_CLIENTSERVER
9899 static void
9900remote_common(typval_T *argvars, typval_T *rettv, int expr)
9901{
9902 char_u *server_name;
9903 char_u *keys;
9904 char_u *r = NULL;
9905 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009906 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009907# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009908 HWND w;
9909# else
9910 Window w;
9911# endif
9912
9913 if (check_restricted() || check_secure())
9914 return;
9915
9916# ifdef FEAT_X11
9917 if (check_connection() == FAIL)
9918 return;
9919# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009920 if (argvars[2].v_type != VAR_UNKNOWN
9921 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009922 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009923
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009924 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009925 if (server_name == NULL)
9926 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009927 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009928# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009929 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009930# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009931 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9932 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009933# endif
9934 {
9935 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009936 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009937 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009938 vim_free(r);
9939 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009940 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009941 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009942 return;
9943 }
9944
9945 rettv->vval.v_string = r;
9946
9947 if (argvars[2].v_type != VAR_UNKNOWN)
9948 {
9949 dictitem_T v;
9950 char_u str[30];
9951 char_u *idvar;
9952
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009953 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009954 if (idvar != NULL && *idvar != NUL)
9955 {
9956 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9957 v.di_tv.v_type = VAR_STRING;
9958 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009959 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009960 vim_free(v.di_tv.vval.v_string);
9961 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009962 }
9963}
9964#endif
9965
9966/*
9967 * "remote_expr()" function
9968 */
9969 static void
9970f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9971{
9972 rettv->v_type = VAR_STRING;
9973 rettv->vval.v_string = NULL;
9974#ifdef FEAT_CLIENTSERVER
9975 remote_common(argvars, rettv, TRUE);
9976#endif
9977}
9978
9979/*
9980 * "remote_foreground()" function
9981 */
9982 static void
9983f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9984{
9985#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009986# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009987 /* On Win32 it's done in this application. */
9988 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009989 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009990
9991 if (server_name != NULL)
9992 serverForeground(server_name);
9993 }
9994# else
9995 /* Send a foreground() expression to the server. */
9996 argvars[1].v_type = VAR_STRING;
9997 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9998 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009999 rettv->v_type = VAR_STRING;
10000 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010001 remote_common(argvars, rettv, TRUE);
10002 vim_free(argvars[1].vval.v_string);
10003# endif
10004#endif
10005}
10006
10007 static void
10008f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
10009{
10010#ifdef FEAT_CLIENTSERVER
10011 dictitem_T v;
10012 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010013# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010014 long_u n = 0;
10015# endif
10016 char_u *serverid;
10017
10018 if (check_restricted() || check_secure())
10019 {
10020 rettv->vval.v_number = -1;
10021 return;
10022 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010023 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010024 if (serverid == NULL)
10025 {
10026 rettv->vval.v_number = -1;
10027 return; /* type error; errmsg already given */
10028 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010029# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010030 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
10031 if (n == 0)
10032 rettv->vval.v_number = -1;
10033 else
10034 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010035 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010036 rettv->vval.v_number = (s != NULL);
10037 }
10038# else
10039 if (check_connection() == FAIL)
10040 return;
10041
10042 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10043 serverStrToWin(serverid), &s);
10044# endif
10045
10046 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10047 {
10048 char_u *retvar;
10049
10050 v.di_tv.v_type = VAR_STRING;
10051 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010052 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010053 if (retvar != NULL)
10054 set_var(retvar, &v.di_tv, FALSE);
10055 vim_free(v.di_tv.vval.v_string);
10056 }
10057#else
10058 rettv->vval.v_number = -1;
10059#endif
10060}
10061
10062 static void
10063f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
10064{
10065 char_u *r = NULL;
10066
10067#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010068 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010069
10070 if (serverid != NULL && !check_restricted() && !check_secure())
10071 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010072 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010073# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010074 /* The server's HWND is encoded in the 'id' parameter */
10075 long_u n = 0;
10076# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010077
10078 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010079 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010080
Bram Moolenaar4f974752019-02-17 17:44:42 +010010081# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010082 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10083 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010084 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010085 if (r == NULL)
10086# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010087 if (check_connection() == FAIL
10088 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10089 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010090# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010091 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010092 }
10093#endif
10094 rettv->v_type = VAR_STRING;
10095 rettv->vval.v_string = r;
10096}
10097
10098/*
10099 * "remote_send()" function
10100 */
10101 static void
10102f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10103{
10104 rettv->v_type = VAR_STRING;
10105 rettv->vval.v_string = NULL;
10106#ifdef FEAT_CLIENTSERVER
10107 remote_common(argvars, rettv, FALSE);
10108#endif
10109}
10110
10111/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010112 * "remote_startserver()" function
10113 */
10114 static void
10115f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10116{
10117#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010118 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010119
10120 if (server == NULL)
10121 return; /* type error; errmsg already given */
10122 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010123 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010124 else
10125 {
10126# ifdef FEAT_X11
10127 if (check_connection() == OK)
10128 serverRegisterName(X_DISPLAY, server);
10129# else
10130 serverSetName(server);
10131# endif
10132 }
10133#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010134 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010135#endif
10136}
10137
10138/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010139 * "remove()" function
10140 */
10141 static void
10142f_remove(typval_T *argvars, typval_T *rettv)
10143{
10144 list_T *l;
10145 listitem_T *item, *item2;
10146 listitem_T *li;
10147 long idx;
10148 long end;
10149 char_u *key;
10150 dict_T *d;
10151 dictitem_T *di;
10152 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010153 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010154
10155 if (argvars[0].v_type == VAR_DICT)
10156 {
10157 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010158 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010159 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010160 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010161 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010162 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010163 if (key != NULL)
10164 {
10165 di = dict_find(d, key, -1);
10166 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010167 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010168 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10169 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10170 {
10171 *rettv = di->di_tv;
10172 init_tv(&di->di_tv);
10173 dictitem_remove(d, di);
10174 }
10175 }
10176 }
10177 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010178 else if (argvars[0].v_type == VAR_BLOB)
10179 {
10180 idx = (long)tv_get_number_chk(&argvars[1], &error);
10181 if (!error)
10182 {
10183 blob_T *b = argvars[0].vval.v_blob;
10184 int len = blob_len(b);
10185 char_u *p;
10186
10187 if (idx < 0)
10188 // count from the end
10189 idx = len + idx;
10190 if (idx < 0 || idx >= len)
10191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010192 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010193 return;
10194 }
10195 if (argvars[2].v_type == VAR_UNKNOWN)
10196 {
10197 // Remove one item, return its value.
10198 p = (char_u *)b->bv_ga.ga_data;
10199 rettv->vval.v_number = (varnumber_T) *(p + idx);
10200 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10201 --b->bv_ga.ga_len;
10202 }
10203 else
10204 {
10205 blob_T *blob;
10206
10207 // Remove range of items, return list with values.
10208 end = (long)tv_get_number_chk(&argvars[2], &error);
10209 if (error)
10210 return;
10211 if (end < 0)
10212 // count from the end
10213 end = len + end;
10214 if (end >= len || idx > end)
10215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010216 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010217 return;
10218 }
10219 blob = blob_alloc();
10220 if (blob == NULL)
10221 return;
10222 blob->bv_ga.ga_len = end - idx + 1;
10223 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10224 {
10225 vim_free(blob);
10226 return;
10227 }
10228 p = (char_u *)b->bv_ga.ga_data;
10229 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10230 (size_t)(end - idx + 1));
10231 ++blob->bv_refcount;
10232 rettv->v_type = VAR_BLOB;
10233 rettv->vval.v_blob = blob;
10234
10235 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10236 b->bv_ga.ga_len -= end - idx + 1;
10237 }
10238 }
10239 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010240 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010241 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010242 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010243 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010244 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010245 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010246 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010247 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010248 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010249 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010250 else
10251 {
10252 if (argvars[2].v_type == VAR_UNKNOWN)
10253 {
10254 /* Remove one item, return its value. */
10255 vimlist_remove(l, item, item);
10256 *rettv = item->li_tv;
10257 vim_free(item);
10258 }
10259 else
10260 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010261 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010262 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010263 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010264 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010265 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010266 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010267 else
10268 {
10269 int cnt = 0;
10270
10271 for (li = item; li != NULL; li = li->li_next)
10272 {
10273 ++cnt;
10274 if (li == item2)
10275 break;
10276 }
10277 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010278 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010279 else
10280 {
10281 vimlist_remove(l, item, item2);
10282 if (rettv_list_alloc(rettv) == OK)
10283 {
10284 l = rettv->vval.v_list;
10285 l->lv_first = item;
10286 l->lv_last = item2;
10287 item->li_prev = NULL;
10288 item2->li_next = NULL;
10289 l->lv_len = cnt;
10290 }
10291 }
10292 }
10293 }
10294 }
10295 }
10296}
10297
10298/*
10299 * "rename({from}, {to})" function
10300 */
10301 static void
10302f_rename(typval_T *argvars, typval_T *rettv)
10303{
10304 char_u buf[NUMBUFLEN];
10305
10306 if (check_restricted() || check_secure())
10307 rettv->vval.v_number = -1;
10308 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010309 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10310 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010311}
10312
10313/*
10314 * "repeat()" function
10315 */
10316 static void
10317f_repeat(typval_T *argvars, typval_T *rettv)
10318{
10319 char_u *p;
10320 int n;
10321 int slen;
10322 int len;
10323 char_u *r;
10324 int i;
10325
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010326 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010327 if (argvars[0].v_type == VAR_LIST)
10328 {
10329 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10330 while (n-- > 0)
10331 if (list_extend(rettv->vval.v_list,
10332 argvars[0].vval.v_list, NULL) == FAIL)
10333 break;
10334 }
10335 else
10336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010337 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010338 rettv->v_type = VAR_STRING;
10339 rettv->vval.v_string = NULL;
10340
10341 slen = (int)STRLEN(p);
10342 len = slen * n;
10343 if (len <= 0)
10344 return;
10345
10346 r = alloc(len + 1);
10347 if (r != NULL)
10348 {
10349 for (i = 0; i < n; i++)
10350 mch_memmove(r + i * slen, p, (size_t)slen);
10351 r[len] = NUL;
10352 }
10353
10354 rettv->vval.v_string = r;
10355 }
10356}
10357
10358/*
10359 * "resolve()" function
10360 */
10361 static void
10362f_resolve(typval_T *argvars, typval_T *rettv)
10363{
10364 char_u *p;
10365#ifdef HAVE_READLINK
10366 char_u *buf = NULL;
10367#endif
10368
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010369 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010370#ifdef FEAT_SHORTCUT
10371 {
10372 char_u *v = NULL;
10373
Bram Moolenaardce1e892019-02-10 23:18:53 +010010374 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010375 if (v != NULL)
10376 rettv->vval.v_string = v;
10377 else
10378 rettv->vval.v_string = vim_strsave(p);
10379 }
10380#else
10381# ifdef HAVE_READLINK
10382 {
10383 char_u *cpy;
10384 int len;
10385 char_u *remain = NULL;
10386 char_u *q;
10387 int is_relative_to_current = FALSE;
10388 int has_trailing_pathsep = FALSE;
10389 int limit = 100;
10390
10391 p = vim_strsave(p);
10392
10393 if (p[0] == '.' && (vim_ispathsep(p[1])
10394 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10395 is_relative_to_current = TRUE;
10396
10397 len = STRLEN(p);
10398 if (len > 0 && after_pathsep(p, p + len))
10399 {
10400 has_trailing_pathsep = TRUE;
10401 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10402 }
10403
10404 q = getnextcomp(p);
10405 if (*q != NUL)
10406 {
10407 /* Separate the first path component in "p", and keep the
10408 * remainder (beginning with the path separator). */
10409 remain = vim_strsave(q - 1);
10410 q[-1] = NUL;
10411 }
10412
10413 buf = alloc(MAXPATHL + 1);
10414 if (buf == NULL)
10415 goto fail;
10416
10417 for (;;)
10418 {
10419 for (;;)
10420 {
10421 len = readlink((char *)p, (char *)buf, MAXPATHL);
10422 if (len <= 0)
10423 break;
10424 buf[len] = NUL;
10425
10426 if (limit-- == 0)
10427 {
10428 vim_free(p);
10429 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010430 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010431 rettv->vval.v_string = NULL;
10432 goto fail;
10433 }
10434
10435 /* Ensure that the result will have a trailing path separator
10436 * if the argument has one. */
10437 if (remain == NULL && has_trailing_pathsep)
10438 add_pathsep(buf);
10439
10440 /* Separate the first path component in the link value and
10441 * concatenate the remainders. */
10442 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10443 if (*q != NUL)
10444 {
10445 if (remain == NULL)
10446 remain = vim_strsave(q - 1);
10447 else
10448 {
10449 cpy = concat_str(q - 1, remain);
10450 if (cpy != NULL)
10451 {
10452 vim_free(remain);
10453 remain = cpy;
10454 }
10455 }
10456 q[-1] = NUL;
10457 }
10458
10459 q = gettail(p);
10460 if (q > p && *q == NUL)
10461 {
10462 /* Ignore trailing path separator. */
10463 q[-1] = NUL;
10464 q = gettail(p);
10465 }
10466 if (q > p && !mch_isFullName(buf))
10467 {
10468 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010469 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010470 if (cpy != NULL)
10471 {
10472 STRCPY(cpy, p);
10473 STRCPY(gettail(cpy), buf);
10474 vim_free(p);
10475 p = cpy;
10476 }
10477 }
10478 else
10479 {
10480 vim_free(p);
10481 p = vim_strsave(buf);
10482 }
10483 }
10484
10485 if (remain == NULL)
10486 break;
10487
10488 /* Append the first path component of "remain" to "p". */
10489 q = getnextcomp(remain + 1);
10490 len = q - remain - (*q != NUL);
10491 cpy = vim_strnsave(p, STRLEN(p) + len);
10492 if (cpy != NULL)
10493 {
10494 STRNCAT(cpy, remain, len);
10495 vim_free(p);
10496 p = cpy;
10497 }
10498 /* Shorten "remain". */
10499 if (*q != NUL)
10500 STRMOVE(remain, q - 1);
10501 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010502 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010503 }
10504
10505 /* If the result is a relative path name, make it explicitly relative to
10506 * the current directory if and only if the argument had this form. */
10507 if (!vim_ispathsep(*p))
10508 {
10509 if (is_relative_to_current
10510 && *p != NUL
10511 && !(p[0] == '.'
10512 && (p[1] == NUL
10513 || vim_ispathsep(p[1])
10514 || (p[1] == '.'
10515 && (p[2] == NUL
10516 || vim_ispathsep(p[2]))))))
10517 {
10518 /* Prepend "./". */
10519 cpy = concat_str((char_u *)"./", p);
10520 if (cpy != NULL)
10521 {
10522 vim_free(p);
10523 p = cpy;
10524 }
10525 }
10526 else if (!is_relative_to_current)
10527 {
10528 /* Strip leading "./". */
10529 q = p;
10530 while (q[0] == '.' && vim_ispathsep(q[1]))
10531 q += 2;
10532 if (q > p)
10533 STRMOVE(p, p + 2);
10534 }
10535 }
10536
10537 /* Ensure that the result will have no trailing path separator
10538 * if the argument had none. But keep "/" or "//". */
10539 if (!has_trailing_pathsep)
10540 {
10541 q = p + STRLEN(p);
10542 if (after_pathsep(p, q))
10543 *gettail_sep(p) = NUL;
10544 }
10545
10546 rettv->vval.v_string = p;
10547 }
10548# else
10549 rettv->vval.v_string = vim_strsave(p);
10550# endif
10551#endif
10552
10553 simplify_filename(rettv->vval.v_string);
10554
10555#ifdef HAVE_READLINK
10556fail:
10557 vim_free(buf);
10558#endif
10559 rettv->v_type = VAR_STRING;
10560}
10561
10562/*
10563 * "reverse({list})" function
10564 */
10565 static void
10566f_reverse(typval_T *argvars, typval_T *rettv)
10567{
10568 list_T *l;
10569 listitem_T *li, *ni;
10570
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010571 if (argvars[0].v_type == VAR_BLOB)
10572 {
10573 blob_T *b = argvars[0].vval.v_blob;
10574 int i, len = blob_len(b);
10575
10576 for (i = 0; i < len / 2; i++)
10577 {
10578 int tmp = blob_get(b, i);
10579
10580 blob_set(b, i, blob_get(b, len - i - 1));
10581 blob_set(b, len - i - 1, tmp);
10582 }
10583 rettv_blob_set(rettv, b);
10584 return;
10585 }
10586
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010587 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010588 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010589 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010590 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010591 (char_u *)N_("reverse() argument"), TRUE))
10592 {
10593 li = l->lv_last;
10594 l->lv_first = l->lv_last = NULL;
10595 l->lv_len = 0;
10596 while (li != NULL)
10597 {
10598 ni = li->li_prev;
10599 list_append(l, li);
10600 li = ni;
10601 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010602 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010603 l->lv_idx = l->lv_len - l->lv_idx - 1;
10604 }
10605}
10606
10607#define SP_NOMOVE 0x01 /* don't move cursor */
10608#define SP_REPEAT 0x02 /* repeat to find outer pair */
10609#define SP_RETCOUNT 0x04 /* return matchcount */
10610#define SP_SETPCMARK 0x08 /* set previous context mark */
10611#define SP_START 0x10 /* accept match at start position */
10612#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10613#define SP_END 0x40 /* leave cursor at end of match */
10614#define SP_COLUMN 0x80 /* start at cursor column */
10615
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010616/*
10617 * Get flags for a search function.
10618 * Possibly sets "p_ws".
10619 * Returns BACKWARD, FORWARD or zero (for an error).
10620 */
10621 static int
10622get_search_arg(typval_T *varp, int *flagsp)
10623{
10624 int dir = FORWARD;
10625 char_u *flags;
10626 char_u nbuf[NUMBUFLEN];
10627 int mask;
10628
10629 if (varp->v_type != VAR_UNKNOWN)
10630 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010631 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010632 if (flags == NULL)
10633 return 0; /* type error; errmsg already given */
10634 while (*flags != NUL)
10635 {
10636 switch (*flags)
10637 {
10638 case 'b': dir = BACKWARD; break;
10639 case 'w': p_ws = TRUE; break;
10640 case 'W': p_ws = FALSE; break;
10641 default: mask = 0;
10642 if (flagsp != NULL)
10643 switch (*flags)
10644 {
10645 case 'c': mask = SP_START; break;
10646 case 'e': mask = SP_END; break;
10647 case 'm': mask = SP_RETCOUNT; break;
10648 case 'n': mask = SP_NOMOVE; break;
10649 case 'p': mask = SP_SUBPAT; break;
10650 case 'r': mask = SP_REPEAT; break;
10651 case 's': mask = SP_SETPCMARK; break;
10652 case 'z': mask = SP_COLUMN; break;
10653 }
10654 if (mask == 0)
10655 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010656 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010657 dir = 0;
10658 }
10659 else
10660 *flagsp |= mask;
10661 }
10662 if (dir == 0)
10663 break;
10664 ++flags;
10665 }
10666 }
10667 return dir;
10668}
10669
10670/*
10671 * Shared by search() and searchpos() functions.
10672 */
10673 static int
10674search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10675{
10676 int flags;
10677 char_u *pat;
10678 pos_T pos;
10679 pos_T save_cursor;
10680 int save_p_ws = p_ws;
10681 int dir;
10682 int retval = 0; /* default: FAIL */
10683 long lnum_stop = 0;
10684 proftime_T tm;
10685#ifdef FEAT_RELTIME
10686 long time_limit = 0;
10687#endif
10688 int options = SEARCH_KEEP;
10689 int subpatnum;
10690
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010691 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010692 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10693 if (dir == 0)
10694 goto theend;
10695 flags = *flagsp;
10696 if (flags & SP_START)
10697 options |= SEARCH_START;
10698 if (flags & SP_END)
10699 options |= SEARCH_END;
10700 if (flags & SP_COLUMN)
10701 options |= SEARCH_COL;
10702
10703 /* Optional arguments: line number to stop searching and timeout. */
10704 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10705 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010706 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010707 if (lnum_stop < 0)
10708 goto theend;
10709#ifdef FEAT_RELTIME
10710 if (argvars[3].v_type != VAR_UNKNOWN)
10711 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010712 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010713 if (time_limit < 0)
10714 goto theend;
10715 }
10716#endif
10717 }
10718
10719#ifdef FEAT_RELTIME
10720 /* Set the time limit, if there is one. */
10721 profile_setlimit(time_limit, &tm);
10722#endif
10723
10724 /*
10725 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10726 * Check to make sure only those flags are set.
10727 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10728 * flags cannot be set. Check for that condition also.
10729 */
10730 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10731 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010733 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010734 goto theend;
10735 }
10736
10737 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010738 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010739 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010740 if (subpatnum != FAIL)
10741 {
10742 if (flags & SP_SUBPAT)
10743 retval = subpatnum;
10744 else
10745 retval = pos.lnum;
10746 if (flags & SP_SETPCMARK)
10747 setpcmark();
10748 curwin->w_cursor = pos;
10749 if (match_pos != NULL)
10750 {
10751 /* Store the match cursor position */
10752 match_pos->lnum = pos.lnum;
10753 match_pos->col = pos.col + 1;
10754 }
10755 /* "/$" will put the cursor after the end of the line, may need to
10756 * correct that here */
10757 check_cursor();
10758 }
10759
10760 /* If 'n' flag is used: restore cursor position. */
10761 if (flags & SP_NOMOVE)
10762 curwin->w_cursor = save_cursor;
10763 else
10764 curwin->w_set_curswant = TRUE;
10765theend:
10766 p_ws = save_p_ws;
10767
10768 return retval;
10769}
10770
10771#ifdef FEAT_FLOAT
10772
10773/*
10774 * round() is not in C90, use ceil() or floor() instead.
10775 */
10776 float_T
10777vim_round(float_T f)
10778{
10779 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10780}
10781
10782/*
10783 * "round({float})" function
10784 */
10785 static void
10786f_round(typval_T *argvars, typval_T *rettv)
10787{
10788 float_T f = 0.0;
10789
10790 rettv->v_type = VAR_FLOAT;
10791 if (get_float_arg(argvars, &f) == OK)
10792 rettv->vval.v_float = vim_round(f);
10793 else
10794 rettv->vval.v_float = 0.0;
10795}
10796#endif
10797
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010798#ifdef FEAT_RUBY
10799/*
10800 * "rubyeval()" function
10801 */
10802 static void
10803f_rubyeval(typval_T *argvars, typval_T *rettv)
10804{
10805 char_u *str;
10806 char_u buf[NUMBUFLEN];
10807
10808 str = tv_get_string_buf(&argvars[0], buf);
10809 do_rubyeval(str, rettv);
10810}
10811#endif
10812
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010813/*
10814 * "screenattr()" function
10815 */
10816 static void
10817f_screenattr(typval_T *argvars, typval_T *rettv)
10818{
10819 int row;
10820 int col;
10821 int c;
10822
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010823 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10824 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010825 if (row < 0 || row >= screen_Rows
10826 || col < 0 || col >= screen_Columns)
10827 c = -1;
10828 else
10829 c = ScreenAttrs[LineOffset[row] + col];
10830 rettv->vval.v_number = c;
10831}
10832
10833/*
10834 * "screenchar()" function
10835 */
10836 static void
10837f_screenchar(typval_T *argvars, typval_T *rettv)
10838{
10839 int row;
10840 int col;
10841 int off;
10842 int c;
10843
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010844 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10845 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010846 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010847 c = -1;
10848 else
10849 {
10850 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010851 if (enc_utf8 && ScreenLinesUC[off] != 0)
10852 c = ScreenLinesUC[off];
10853 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010854 c = ScreenLines[off];
10855 }
10856 rettv->vval.v_number = c;
10857}
10858
10859/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010860 * "screenchars()" function
10861 */
10862 static void
10863f_screenchars(typval_T *argvars, typval_T *rettv)
10864{
10865 int row;
10866 int col;
10867 int off;
10868 int c;
10869 int i;
10870
10871 if (rettv_list_alloc(rettv) == FAIL)
10872 return;
10873 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10874 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10875 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10876 return;
10877
10878 off = LineOffset[row] + col;
10879 if (enc_utf8 && ScreenLinesUC[off] != 0)
10880 c = ScreenLinesUC[off];
10881 else
10882 c = ScreenLines[off];
10883 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10884
10885 if (enc_utf8)
10886
10887 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10888 list_append_number(rettv->vval.v_list,
10889 (varnumber_T)ScreenLinesC[i][off]);
10890}
10891
10892/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010893 * "screencol()" function
10894 *
10895 * First column is 1 to be consistent with virtcol().
10896 */
10897 static void
10898f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10899{
10900 rettv->vval.v_number = screen_screencol() + 1;
10901}
10902
10903/*
10904 * "screenrow()" function
10905 */
10906 static void
10907f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10908{
10909 rettv->vval.v_number = screen_screenrow() + 1;
10910}
10911
10912/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010913 * "screenstring()" function
10914 */
10915 static void
10916f_screenstring(typval_T *argvars, typval_T *rettv)
10917{
10918 int row;
10919 int col;
10920 int off;
10921 int c;
10922 int i;
10923 char_u buf[MB_MAXBYTES + 1];
10924 int buflen = 0;
10925
10926 rettv->vval.v_string = NULL;
10927 rettv->v_type = VAR_STRING;
10928
10929 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10930 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10931 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10932 return;
10933
10934 off = LineOffset[row] + col;
10935 if (enc_utf8 && ScreenLinesUC[off] != 0)
10936 c = ScreenLinesUC[off];
10937 else
10938 c = ScreenLines[off];
10939 buflen += mb_char2bytes(c, buf);
10940
10941 if (enc_utf8)
10942 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10943 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10944
10945 buf[buflen] = NUL;
10946 rettv->vval.v_string = vim_strsave(buf);
10947}
10948
10949/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010950 * "search()" function
10951 */
10952 static void
10953f_search(typval_T *argvars, typval_T *rettv)
10954{
10955 int flags = 0;
10956
10957 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10958}
10959
10960/*
10961 * "searchdecl()" function
10962 */
10963 static void
10964f_searchdecl(typval_T *argvars, typval_T *rettv)
10965{
10966 int locally = 1;
10967 int thisblock = 0;
10968 int error = FALSE;
10969 char_u *name;
10970
10971 rettv->vval.v_number = 1; /* default: FAIL */
10972
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010973 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010974 if (argvars[1].v_type != VAR_UNKNOWN)
10975 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010976 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010977 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010978 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010979 }
10980 if (!error && name != NULL)
10981 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10982 locally, thisblock, SEARCH_KEEP) == FAIL;
10983}
10984
10985/*
10986 * Used by searchpair() and searchpairpos()
10987 */
10988 static int
10989searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10990{
10991 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010992 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010993 int save_p_ws = p_ws;
10994 int dir;
10995 int flags = 0;
10996 char_u nbuf1[NUMBUFLEN];
10997 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010998 int retval = 0; /* default: FAIL */
10999 long lnum_stop = 0;
11000 long time_limit = 0;
11001
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011002 /* Get the three pattern arguments: start, middle, end. Will result in an
11003 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011004 spat = tv_get_string_chk(&argvars[0]);
11005 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
11006 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011007 if (spat == NULL || mpat == NULL || epat == NULL)
11008 goto theend; /* type error */
11009
11010 /* Handle the optional fourth argument: flags */
11011 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
11012 if (dir == 0)
11013 goto theend;
11014
11015 /* Don't accept SP_END or SP_SUBPAT.
11016 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
11017 */
11018 if ((flags & (SP_END | SP_SUBPAT)) != 0
11019 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
11020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011021 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011022 goto theend;
11023 }
11024
11025 /* Using 'r' implies 'W', otherwise it doesn't work. */
11026 if (flags & SP_REPEAT)
11027 p_ws = FALSE;
11028
11029 /* Optional fifth argument: skip expression */
11030 if (argvars[3].v_type == VAR_UNKNOWN
11031 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010011032 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011033 else
11034 {
Bram Moolenaar48570482017-10-30 21:48:41 +010011035 skip = &argvars[4];
11036 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
11037 && skip->v_type != VAR_STRING)
11038 {
11039 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011040 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010011041 goto theend;
11042 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011043 if (argvars[5].v_type != VAR_UNKNOWN)
11044 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011045 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011046 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011047 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011048 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011049 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011050 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011051#ifdef FEAT_RELTIME
11052 if (argvars[6].v_type != VAR_UNKNOWN)
11053 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011054 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011055 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011056 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011057 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011058 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011059 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011060 }
11061#endif
11062 }
11063 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011064
11065 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
11066 match_pos, lnum_stop, time_limit);
11067
11068theend:
11069 p_ws = save_p_ws;
11070
11071 return retval;
11072}
11073
11074/*
11075 * "searchpair()" function
11076 */
11077 static void
11078f_searchpair(typval_T *argvars, typval_T *rettv)
11079{
11080 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11081}
11082
11083/*
11084 * "searchpairpos()" function
11085 */
11086 static void
11087f_searchpairpos(typval_T *argvars, typval_T *rettv)
11088{
11089 pos_T match_pos;
11090 int lnum = 0;
11091 int col = 0;
11092
11093 if (rettv_list_alloc(rettv) == FAIL)
11094 return;
11095
11096 if (searchpair_cmn(argvars, &match_pos) > 0)
11097 {
11098 lnum = match_pos.lnum;
11099 col = match_pos.col;
11100 }
11101
11102 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11103 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11104}
11105
11106/*
11107 * Search for a start/middle/end thing.
11108 * Used by searchpair(), see its documentation for the details.
11109 * Returns 0 or -1 for no match,
11110 */
11111 long
11112do_searchpair(
11113 char_u *spat, /* start pattern */
11114 char_u *mpat, /* middle pattern */
11115 char_u *epat, /* end pattern */
11116 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011117 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011118 int flags, /* SP_SETPCMARK and other SP_ values */
11119 pos_T *match_pos,
11120 linenr_T lnum_stop, /* stop at this line if not zero */
11121 long time_limit UNUSED) /* stop after this many msec */
11122{
11123 char_u *save_cpo;
11124 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11125 long retval = 0;
11126 pos_T pos;
11127 pos_T firstpos;
11128 pos_T foundpos;
11129 pos_T save_cursor;
11130 pos_T save_pos;
11131 int n;
11132 int r;
11133 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011134 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011135 int err;
11136 int options = SEARCH_KEEP;
11137 proftime_T tm;
11138
11139 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11140 save_cpo = p_cpo;
11141 p_cpo = empty_option;
11142
11143#ifdef FEAT_RELTIME
11144 /* Set the time limit, if there is one. */
11145 profile_setlimit(time_limit, &tm);
11146#endif
11147
11148 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11149 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011150 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11151 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011152 if (pat2 == NULL || pat3 == NULL)
11153 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011154 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011155 if (*mpat == NUL)
11156 STRCPY(pat3, pat2);
11157 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011158 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011159 spat, epat, mpat);
11160 if (flags & SP_START)
11161 options |= SEARCH_START;
11162
Bram Moolenaar48570482017-10-30 21:48:41 +010011163 if (skip != NULL)
11164 {
11165 /* Empty string means to not use the skip expression. */
11166 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11167 use_skip = skip->vval.v_string != NULL
11168 && *skip->vval.v_string != NUL;
11169 }
11170
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011171 save_cursor = curwin->w_cursor;
11172 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011173 CLEAR_POS(&firstpos);
11174 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011175 pat = pat3;
11176 for (;;)
11177 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011178 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011179 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011180 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011181 /* didn't find it or found the first match again: FAIL */
11182 break;
11183
11184 if (firstpos.lnum == 0)
11185 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011186 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011187 {
11188 /* Found the same position again. Can happen with a pattern that
11189 * has "\zs" at the end and searching backwards. Advance one
11190 * character and try again. */
11191 if (dir == BACKWARD)
11192 decl(&pos);
11193 else
11194 incl(&pos);
11195 }
11196 foundpos = pos;
11197
11198 /* clear the start flag to avoid getting stuck here */
11199 options &= ~SEARCH_START;
11200
11201 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011202 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011203 {
11204 save_pos = curwin->w_cursor;
11205 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011206 err = FALSE;
11207 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011208 curwin->w_cursor = save_pos;
11209 if (err)
11210 {
11211 /* Evaluating {skip} caused an error, break here. */
11212 curwin->w_cursor = save_cursor;
11213 retval = -1;
11214 break;
11215 }
11216 if (r)
11217 continue;
11218 }
11219
11220 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11221 {
11222 /* Found end when searching backwards or start when searching
11223 * forward: nested pair. */
11224 ++nest;
11225 pat = pat2; /* nested, don't search for middle */
11226 }
11227 else
11228 {
11229 /* Found end when searching forward or start when searching
11230 * backward: end of (nested) pair; or found middle in outer pair. */
11231 if (--nest == 1)
11232 pat = pat3; /* outer level, search for middle */
11233 }
11234
11235 if (nest == 0)
11236 {
11237 /* Found the match: return matchcount or line number. */
11238 if (flags & SP_RETCOUNT)
11239 ++retval;
11240 else
11241 retval = pos.lnum;
11242 if (flags & SP_SETPCMARK)
11243 setpcmark();
11244 curwin->w_cursor = pos;
11245 if (!(flags & SP_REPEAT))
11246 break;
11247 nest = 1; /* search for next unmatched */
11248 }
11249 }
11250
11251 if (match_pos != NULL)
11252 {
11253 /* Store the match cursor position */
11254 match_pos->lnum = curwin->w_cursor.lnum;
11255 match_pos->col = curwin->w_cursor.col + 1;
11256 }
11257
11258 /* If 'n' flag is used or search failed: restore cursor position. */
11259 if ((flags & SP_NOMOVE) || retval == 0)
11260 curwin->w_cursor = save_cursor;
11261
11262theend:
11263 vim_free(pat2);
11264 vim_free(pat3);
11265 if (p_cpo == empty_option)
11266 p_cpo = save_cpo;
11267 else
11268 /* Darn, evaluating the {skip} expression changed the value. */
11269 free_string_option(save_cpo);
11270
11271 return retval;
11272}
11273
11274/*
11275 * "searchpos()" function
11276 */
11277 static void
11278f_searchpos(typval_T *argvars, typval_T *rettv)
11279{
11280 pos_T match_pos;
11281 int lnum = 0;
11282 int col = 0;
11283 int n;
11284 int flags = 0;
11285
11286 if (rettv_list_alloc(rettv) == FAIL)
11287 return;
11288
11289 n = search_cmn(argvars, &match_pos, &flags);
11290 if (n > 0)
11291 {
11292 lnum = match_pos.lnum;
11293 col = match_pos.col;
11294 }
11295
11296 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11297 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11298 if (flags & SP_SUBPAT)
11299 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11300}
11301
11302 static void
11303f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11304{
11305#ifdef FEAT_CLIENTSERVER
11306 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011307 char_u *server = tv_get_string_chk(&argvars[0]);
11308 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011309
11310 rettv->vval.v_number = -1;
11311 if (server == NULL || reply == NULL)
11312 return;
11313 if (check_restricted() || check_secure())
11314 return;
11315# ifdef FEAT_X11
11316 if (check_connection() == FAIL)
11317 return;
11318# endif
11319
11320 if (serverSendReply(server, reply) < 0)
11321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011322 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011323 return;
11324 }
11325 rettv->vval.v_number = 0;
11326#else
11327 rettv->vval.v_number = -1;
11328#endif
11329}
11330
11331 static void
11332f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11333{
11334 char_u *r = NULL;
11335
11336#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011337# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011338 r = serverGetVimNames();
11339# else
11340 make_connection();
11341 if (X_DISPLAY != NULL)
11342 r = serverGetVimNames(X_DISPLAY);
11343# endif
11344#endif
11345 rettv->v_type = VAR_STRING;
11346 rettv->vval.v_string = r;
11347}
11348
11349/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011350 * "setbufline()" function
11351 */
11352 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011353f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011354{
11355 linenr_T lnum;
11356 buf_T *buf;
11357
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011358 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011359 if (buf == NULL)
11360 rettv->vval.v_number = 1; /* FAIL */
11361 else
11362 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011363 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011364 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011365 }
11366}
11367
11368/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011369 * "setbufvar()" function
11370 */
11371 static void
11372f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11373{
11374 buf_T *buf;
11375 char_u *varname, *bufvarname;
11376 typval_T *varp;
11377 char_u nbuf[NUMBUFLEN];
11378
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011379 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011380 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011381 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11382 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011383 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011384 varp = &argvars[2];
11385
11386 if (buf != NULL && varname != NULL && varp != NULL)
11387 {
11388 if (*varname == '&')
11389 {
11390 long numval;
11391 char_u *strval;
11392 int error = FALSE;
11393 aco_save_T aco;
11394
11395 /* set curbuf to be our buf, temporarily */
11396 aucmd_prepbuf(&aco, buf);
11397
11398 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011399 numval = (long)tv_get_number_chk(varp, &error);
11400 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011401 if (!error && strval != NULL)
11402 set_option_value(varname, numval, strval, OPT_LOCAL);
11403
11404 /* reset notion of buffer */
11405 aucmd_restbuf(&aco);
11406 }
11407 else
11408 {
11409 buf_T *save_curbuf = curbuf;
11410
Bram Moolenaar964b3742019-05-24 18:54:09 +020011411 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011412 if (bufvarname != NULL)
11413 {
11414 curbuf = buf;
11415 STRCPY(bufvarname, "b:");
11416 STRCPY(bufvarname + 2, varname);
11417 set_var(bufvarname, varp, TRUE);
11418 vim_free(bufvarname);
11419 curbuf = save_curbuf;
11420 }
11421 }
11422 }
11423}
11424
11425 static void
11426f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11427{
11428 dict_T *d;
11429 dictitem_T *di;
11430 char_u *csearch;
11431
11432 if (argvars[0].v_type != VAR_DICT)
11433 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011434 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011435 return;
11436 }
11437
11438 if ((d = argvars[0].vval.v_dict) != NULL)
11439 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011440 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011441 if (csearch != NULL)
11442 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011443 if (enc_utf8)
11444 {
11445 int pcc[MAX_MCO];
11446 int c = utfc_ptr2char(csearch, pcc);
11447
11448 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11449 }
11450 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011451 set_last_csearch(PTR2CHAR(csearch),
11452 csearch, MB_PTR2LEN(csearch));
11453 }
11454
11455 di = dict_find(d, (char_u *)"forward", -1);
11456 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011457 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011458 ? FORWARD : BACKWARD);
11459
11460 di = dict_find(d, (char_u *)"until", -1);
11461 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011462 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011463 }
11464}
11465
11466/*
11467 * "setcmdpos()" function
11468 */
11469 static void
11470f_setcmdpos(typval_T *argvars, typval_T *rettv)
11471{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011472 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011473
11474 if (pos >= 0)
11475 rettv->vval.v_number = set_cmdline_pos(pos);
11476}
11477
11478/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011479 * "setenv()" function
11480 */
11481 static void
11482f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11483{
11484 char_u namebuf[NUMBUFLEN];
11485 char_u valbuf[NUMBUFLEN];
11486 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11487
11488 if (argvars[1].v_type == VAR_SPECIAL
11489 && argvars[1].vval.v_number == VVAL_NULL)
11490 vim_unsetenv(name);
11491 else
11492 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11493}
11494
11495/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011496 * "setfperm({fname}, {mode})" function
11497 */
11498 static void
11499f_setfperm(typval_T *argvars, typval_T *rettv)
11500{
11501 char_u *fname;
11502 char_u modebuf[NUMBUFLEN];
11503 char_u *mode_str;
11504 int i;
11505 int mask;
11506 int mode = 0;
11507
11508 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011509 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011510 if (fname == NULL)
11511 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011512 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011513 if (mode_str == NULL)
11514 return;
11515 if (STRLEN(mode_str) != 9)
11516 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011517 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011518 return;
11519 }
11520
11521 mask = 1;
11522 for (i = 8; i >= 0; --i)
11523 {
11524 if (mode_str[i] != '-')
11525 mode |= mask;
11526 mask = mask << 1;
11527 }
11528 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11529}
11530
11531/*
11532 * "setline()" function
11533 */
11534 static void
11535f_setline(typval_T *argvars, typval_T *rettv)
11536{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011537 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011538
Bram Moolenaarca851592018-06-06 21:04:07 +020011539 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011540}
11541
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011542/*
11543 * Used by "setqflist()" and "setloclist()" functions
11544 */
11545 static void
11546set_qf_ll_list(
11547 win_T *wp UNUSED,
11548 typval_T *list_arg UNUSED,
11549 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011550 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011551 typval_T *rettv)
11552{
11553#ifdef FEAT_QUICKFIX
11554 static char *e_invact = N_("E927: Invalid action: '%s'");
11555 char_u *act;
11556 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011557 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011558#endif
11559
11560 rettv->vval.v_number = -1;
11561
11562#ifdef FEAT_QUICKFIX
11563 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011564 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011565 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011566 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011567 else
11568 {
11569 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011570 dict_T *d = NULL;
11571 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011572
11573 if (action_arg->v_type == VAR_STRING)
11574 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011575 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011576 if (act == NULL)
11577 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011578 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11579 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011580 action = *act;
11581 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011582 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011583 }
11584 else if (action_arg->v_type == VAR_UNKNOWN)
11585 action = ' ';
11586 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011587 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011588
Bram Moolenaard823fa92016-08-12 16:29:27 +020011589 if (action_arg->v_type != VAR_UNKNOWN
11590 && what_arg->v_type != VAR_UNKNOWN)
11591 {
11592 if (what_arg->v_type == VAR_DICT)
11593 d = what_arg->vval.v_dict;
11594 else
11595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011596 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011597 valid_dict = FALSE;
11598 }
11599 }
11600
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011601 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011602 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011603 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11604 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011605 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011606 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011607 }
11608#endif
11609}
11610
11611/*
11612 * "setloclist()" function
11613 */
11614 static void
11615f_setloclist(typval_T *argvars, typval_T *rettv)
11616{
11617 win_T *win;
11618
11619 rettv->vval.v_number = -1;
11620
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011621 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011622 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011623 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011624}
11625
11626/*
11627 * "setmatches()" function
11628 */
11629 static void
11630f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11631{
11632#ifdef FEAT_SEARCH_EXTRA
11633 list_T *l;
11634 listitem_T *li;
11635 dict_T *d;
11636 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011637 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011638
11639 rettv->vval.v_number = -1;
11640 if (argvars[0].v_type != VAR_LIST)
11641 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011642 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011643 return;
11644 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011645 if (win == NULL)
11646 return;
11647
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011648 if ((l = argvars[0].vval.v_list) != NULL)
11649 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011650 /* To some extent make sure that we are dealing with a list from
11651 * "getmatches()". */
11652 li = l->lv_first;
11653 while (li != NULL)
11654 {
11655 if (li->li_tv.v_type != VAR_DICT
11656 || (d = li->li_tv.vval.v_dict) == NULL)
11657 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011658 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011659 return;
11660 }
11661 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11662 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11663 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11664 && dict_find(d, (char_u *)"priority", -1) != NULL
11665 && dict_find(d, (char_u *)"id", -1) != NULL))
11666 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011667 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011668 return;
11669 }
11670 li = li->li_next;
11671 }
11672
Bram Moolenaaraff74912019-03-30 18:11:49 +010011673 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011674 li = l->lv_first;
11675 while (li != NULL)
11676 {
11677 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011678 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011679 dictitem_T *di;
11680 char_u *group;
11681 int priority;
11682 int id;
11683 char_u *conceal;
11684
11685 d = li->li_tv.vval.v_dict;
11686 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11687 {
11688 if (s == NULL)
11689 {
11690 s = list_alloc();
11691 if (s == NULL)
11692 return;
11693 }
11694
11695 /* match from matchaddpos() */
11696 for (i = 1; i < 9; i++)
11697 {
11698 sprintf((char *)buf, (char *)"pos%d", i);
11699 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11700 {
11701 if (di->di_tv.v_type != VAR_LIST)
11702 return;
11703
11704 list_append_tv(s, &di->di_tv);
11705 s->lv_refcount++;
11706 }
11707 else
11708 break;
11709 }
11710 }
11711
Bram Moolenaar8f667172018-12-14 15:38:31 +010011712 group = dict_get_string(d, (char_u *)"group", TRUE);
11713 priority = (int)dict_get_number(d, (char_u *)"priority");
11714 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011715 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011716 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011717 : NULL;
11718 if (i == 0)
11719 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011720 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011721 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011722 priority, id, NULL, conceal);
11723 }
11724 else
11725 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011726 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011727 list_unref(s);
11728 s = NULL;
11729 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011730 vim_free(group);
11731 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011732
11733 li = li->li_next;
11734 }
11735 rettv->vval.v_number = 0;
11736 }
11737#endif
11738}
11739
11740/*
11741 * "setpos()" function
11742 */
11743 static void
11744f_setpos(typval_T *argvars, typval_T *rettv)
11745{
11746 pos_T pos;
11747 int fnum;
11748 char_u *name;
11749 colnr_T curswant = -1;
11750
11751 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011752 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011753 if (name != NULL)
11754 {
11755 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11756 {
11757 if (--pos.col < 0)
11758 pos.col = 0;
11759 if (name[0] == '.' && name[1] == NUL)
11760 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011761 /* set cursor; "fnum" is ignored */
11762 curwin->w_cursor = pos;
11763 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011764 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011765 curwin->w_curswant = curswant - 1;
11766 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011767 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011768 check_cursor();
11769 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011770 }
11771 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11772 {
11773 /* set mark */
11774 if (setmark_pos(name[1], &pos, fnum) == OK)
11775 rettv->vval.v_number = 0;
11776 }
11777 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011778 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011779 }
11780 }
11781}
11782
11783/*
11784 * "setqflist()" function
11785 */
11786 static void
11787f_setqflist(typval_T *argvars, typval_T *rettv)
11788{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011789 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011790}
11791
11792/*
11793 * "setreg()" function
11794 */
11795 static void
11796f_setreg(typval_T *argvars, typval_T *rettv)
11797{
11798 int regname;
11799 char_u *strregname;
11800 char_u *stropt;
11801 char_u *strval;
11802 int append;
11803 char_u yank_type;
11804 long block_len;
11805
11806 block_len = -1;
11807 yank_type = MAUTO;
11808 append = FALSE;
11809
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011810 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011811 rettv->vval.v_number = 1; /* FAIL is default */
11812
11813 if (strregname == NULL)
11814 return; /* type error; errmsg already given */
11815 regname = *strregname;
11816 if (regname == 0 || regname == '@')
11817 regname = '"';
11818
11819 if (argvars[2].v_type != VAR_UNKNOWN)
11820 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011821 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011822 if (stropt == NULL)
11823 return; /* type error */
11824 for (; *stropt != NUL; ++stropt)
11825 switch (*stropt)
11826 {
11827 case 'a': case 'A': /* append */
11828 append = TRUE;
11829 break;
11830 case 'v': case 'c': /* character-wise selection */
11831 yank_type = MCHAR;
11832 break;
11833 case 'V': case 'l': /* line-wise selection */
11834 yank_type = MLINE;
11835 break;
11836 case 'b': case Ctrl_V: /* block-wise selection */
11837 yank_type = MBLOCK;
11838 if (VIM_ISDIGIT(stropt[1]))
11839 {
11840 ++stropt;
11841 block_len = getdigits(&stropt) - 1;
11842 --stropt;
11843 }
11844 break;
11845 }
11846 }
11847
11848 if (argvars[1].v_type == VAR_LIST)
11849 {
11850 char_u **lstval;
11851 char_u **allocval;
11852 char_u buf[NUMBUFLEN];
11853 char_u **curval;
11854 char_u **curallocval;
11855 list_T *ll = argvars[1].vval.v_list;
11856 listitem_T *li;
11857 int len;
11858
11859 /* If the list is NULL handle like an empty list. */
11860 len = ll == NULL ? 0 : ll->lv_len;
11861
11862 /* First half: use for pointers to result lines; second half: use for
11863 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011864 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011865 if (lstval == NULL)
11866 return;
11867 curval = lstval;
11868 allocval = lstval + len + 2;
11869 curallocval = allocval;
11870
11871 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11872 li = li->li_next)
11873 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011874 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011875 if (strval == NULL)
11876 goto free_lstval;
11877 if (strval == buf)
11878 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011879 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011880 * overwrite the string. */
11881 strval = vim_strsave(buf);
11882 if (strval == NULL)
11883 goto free_lstval;
11884 *curallocval++ = strval;
11885 }
11886 *curval++ = strval;
11887 }
11888 *curval++ = NULL;
11889
11890 write_reg_contents_lst(regname, lstval, -1,
11891 append, yank_type, block_len);
11892free_lstval:
11893 while (curallocval > allocval)
11894 vim_free(*--curallocval);
11895 vim_free(lstval);
11896 }
11897 else
11898 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011899 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011900 if (strval == NULL)
11901 return;
11902 write_reg_contents_ex(regname, strval, -1,
11903 append, yank_type, block_len);
11904 }
11905 rettv->vval.v_number = 0;
11906}
11907
11908/*
11909 * "settabvar()" function
11910 */
11911 static void
11912f_settabvar(typval_T *argvars, typval_T *rettv)
11913{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011914 tabpage_T *save_curtab;
11915 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011916 char_u *varname, *tabvarname;
11917 typval_T *varp;
11918
11919 rettv->vval.v_number = 0;
11920
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011921 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011922 return;
11923
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011924 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11925 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011926 varp = &argvars[2];
11927
Bram Moolenaar4033c552017-09-16 20:54:51 +020011928 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011929 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011930 save_curtab = curtab;
11931 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011932
Bram Moolenaar964b3742019-05-24 18:54:09 +020011933 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011934 if (tabvarname != NULL)
11935 {
11936 STRCPY(tabvarname, "t:");
11937 STRCPY(tabvarname + 2, varname);
11938 set_var(tabvarname, varp, TRUE);
11939 vim_free(tabvarname);
11940 }
11941
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011942 /* Restore current tabpage */
11943 if (valid_tabpage(save_curtab))
11944 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011945 }
11946}
11947
11948/*
11949 * "settabwinvar()" function
11950 */
11951 static void
11952f_settabwinvar(typval_T *argvars, typval_T *rettv)
11953{
11954 setwinvar(argvars, rettv, 1);
11955}
11956
11957/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011958 * "settagstack()" function
11959 */
11960 static void
11961f_settagstack(typval_T *argvars, typval_T *rettv)
11962{
11963 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11964 win_T *wp;
11965 dict_T *d;
11966 int action = 'r';
11967
11968 rettv->vval.v_number = -1;
11969
11970 // first argument: window number or id
11971 wp = find_win_by_nr_or_id(&argvars[0]);
11972 if (wp == NULL)
11973 return;
11974
11975 // second argument: dict with items to set in the tag stack
11976 if (argvars[1].v_type != VAR_DICT)
11977 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011978 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011979 return;
11980 }
11981 d = argvars[1].vval.v_dict;
11982 if (d == NULL)
11983 return;
11984
11985 // third argument: action - 'a' for append and 'r' for replace.
11986 // default is to replace the stack.
11987 if (argvars[2].v_type == VAR_UNKNOWN)
11988 action = 'r';
11989 else if (argvars[2].v_type == VAR_STRING)
11990 {
11991 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011992 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011993 if (actstr == NULL)
11994 return;
11995 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11996 action = *actstr;
11997 else
11998 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011999 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012000 return;
12001 }
12002 }
12003 else
12004 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012005 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012006 return;
12007 }
12008
12009 if (set_tagstack(wp, d, action) == OK)
12010 rettv->vval.v_number = 0;
12011}
12012
12013/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012014 * "setwinvar()" function
12015 */
12016 static void
12017f_setwinvar(typval_T *argvars, typval_T *rettv)
12018{
12019 setwinvar(argvars, rettv, 0);
12020}
12021
12022#ifdef FEAT_CRYPT
12023/*
12024 * "sha256({string})" function
12025 */
12026 static void
12027f_sha256(typval_T *argvars, typval_T *rettv)
12028{
12029 char_u *p;
12030
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012031 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012032 rettv->vval.v_string = vim_strsave(
12033 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
12034 rettv->v_type = VAR_STRING;
12035}
12036#endif /* FEAT_CRYPT */
12037
12038/*
12039 * "shellescape({string})" function
12040 */
12041 static void
12042f_shellescape(typval_T *argvars, typval_T *rettv)
12043{
Bram Moolenaar20615522017-06-05 18:46:26 +020012044 int do_special = non_zero_arg(&argvars[1]);
12045
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012046 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012047 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012048 rettv->v_type = VAR_STRING;
12049}
12050
12051/*
12052 * shiftwidth() function
12053 */
12054 static void
12055f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
12056{
Bram Moolenaarf9514162018-11-22 03:08:29 +010012057 rettv->vval.v_number = 0;
12058
12059 if (argvars[0].v_type != VAR_UNKNOWN)
12060 {
12061 long col;
12062
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012063 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010012064 if (col < 0)
12065 return; // type error; errmsg already given
12066#ifdef FEAT_VARTABS
12067 rettv->vval.v_number = get_sw_value_col(curbuf, col);
12068 return;
12069#endif
12070 }
12071
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012072 rettv->vval.v_number = get_sw_value(curbuf);
12073}
12074
Bram Moolenaar162b7142018-12-21 15:17:36 +010012075#ifdef FEAT_SIGNS
12076/*
12077 * "sign_define()" function
12078 */
12079 static void
12080f_sign_define(typval_T *argvars, typval_T *rettv)
12081{
12082 char_u *name;
12083 dict_T *dict;
12084 char_u *icon = NULL;
12085 char_u *linehl = NULL;
12086 char_u *text = NULL;
12087 char_u *texthl = NULL;
12088
12089 rettv->vval.v_number = -1;
12090
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012091 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012092 if (name == NULL)
12093 return;
12094
12095 if (argvars[1].v_type != VAR_UNKNOWN)
12096 {
12097 if (argvars[1].v_type != VAR_DICT)
12098 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012099 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012100 return;
12101 }
12102
12103 // sign attributes
12104 dict = argvars[1].vval.v_dict;
12105 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12106 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12107 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12108 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12109 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12110 text = dict_get_string(dict, (char_u *)"text", TRUE);
12111 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12112 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12113 }
12114
12115 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12116 rettv->vval.v_number = 0;
12117
12118 vim_free(icon);
12119 vim_free(linehl);
12120 vim_free(text);
12121 vim_free(texthl);
12122}
12123
12124/*
12125 * "sign_getdefined()" function
12126 */
12127 static void
12128f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12129{
12130 char_u *name = NULL;
12131
12132 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12133 return;
12134
12135 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012136 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012137
12138 sign_getlist(name, rettv->vval.v_list);
12139}
12140
12141/*
12142 * "sign_getplaced()" function
12143 */
12144 static void
12145f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12146{
12147 buf_T *buf = NULL;
12148 dict_T *dict;
12149 dictitem_T *di;
12150 linenr_T lnum = 0;
12151 int sign_id = 0;
12152 char_u *group = NULL;
12153 int notanum = FALSE;
12154
12155 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12156 return;
12157
12158 if (argvars[0].v_type != VAR_UNKNOWN)
12159 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012160 // get signs placed in the specified buffer
12161 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012162 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012163 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012164
12165 if (argvars[1].v_type != VAR_UNKNOWN)
12166 {
12167 if (argvars[1].v_type != VAR_DICT ||
12168 ((dict = argvars[1].vval.v_dict) == NULL))
12169 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012170 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012171 return;
12172 }
12173 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12174 {
12175 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012176 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012177 if (notanum)
12178 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012179 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012180 }
12181 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12182 {
12183 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012184 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012185 if (notanum)
12186 return;
12187 }
12188 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12189 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012190 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012191 if (group == NULL)
12192 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012193 if (*group == '\0') // empty string means global group
12194 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012195 }
12196 }
12197 }
12198
12199 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12200}
12201
12202/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012203 * "sign_jump()" function
12204 */
12205 static void
12206f_sign_jump(typval_T *argvars, typval_T *rettv)
12207{
12208 int sign_id;
12209 char_u *sign_group = NULL;
12210 buf_T *buf;
12211 int notanum = FALSE;
12212
12213 rettv->vval.v_number = -1;
12214
Bram Moolenaarbdace832019-03-02 10:13:42 +010012215 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012216 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12217 if (notanum)
12218 return;
12219 if (sign_id <= 0)
12220 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012221 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012222 return;
12223 }
12224
12225 // Sign group
12226 sign_group = tv_get_string_chk(&argvars[1]);
12227 if (sign_group == NULL)
12228 return;
12229 if (sign_group[0] == '\0')
12230 sign_group = NULL; // global sign group
12231 else
12232 {
12233 sign_group = vim_strsave(sign_group);
12234 if (sign_group == NULL)
12235 return;
12236 }
12237
12238 // Buffer to place the sign
12239 buf = get_buf_arg(&argvars[2]);
12240 if (buf == NULL)
12241 goto cleanup;
12242
12243 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12244
12245cleanup:
12246 vim_free(sign_group);
12247}
12248
12249/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012250 * "sign_place()" function
12251 */
12252 static void
12253f_sign_place(typval_T *argvars, typval_T *rettv)
12254{
12255 int sign_id;
12256 char_u *group = NULL;
12257 char_u *sign_name;
12258 buf_T *buf;
12259 dict_T *dict;
12260 dictitem_T *di;
12261 linenr_T lnum = 0;
12262 int prio = SIGN_DEF_PRIO;
12263 int notanum = FALSE;
12264
12265 rettv->vval.v_number = -1;
12266
Bram Moolenaarbdace832019-03-02 10:13:42 +010012267 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012268 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012269 if (notanum)
12270 return;
12271 if (sign_id < 0)
12272 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012273 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012274 return;
12275 }
12276
12277 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012278 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012279 if (group == NULL)
12280 return;
12281 if (group[0] == '\0')
12282 group = NULL; // global sign group
12283 else
12284 {
12285 group = vim_strsave(group);
12286 if (group == NULL)
12287 return;
12288 }
12289
12290 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012291 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012292 if (sign_name == NULL)
12293 goto cleanup;
12294
12295 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012296 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012297 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012298 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012299
12300 if (argvars[4].v_type != VAR_UNKNOWN)
12301 {
12302 if (argvars[4].v_type != VAR_DICT ||
12303 ((dict = argvars[4].vval.v_dict) == NULL))
12304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012305 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012306 goto cleanup;
12307 }
12308
12309 // Line number where the sign is to be placed
12310 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12311 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012312 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012313 if (notanum)
12314 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012315 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012316 }
12317 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12318 {
12319 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012320 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012321 if (notanum)
12322 goto cleanup;
12323 }
12324 }
12325
12326 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12327 rettv->vval.v_number = sign_id;
12328
12329cleanup:
12330 vim_free(group);
12331}
12332
12333/*
12334 * "sign_undefine()" function
12335 */
12336 static void
12337f_sign_undefine(typval_T *argvars, typval_T *rettv)
12338{
12339 char_u *name;
12340
12341 rettv->vval.v_number = -1;
12342
12343 if (argvars[0].v_type == VAR_UNKNOWN)
12344 {
12345 // Free all the signs
12346 free_signs();
12347 rettv->vval.v_number = 0;
12348 }
12349 else
12350 {
12351 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012352 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012353 if (name == NULL)
12354 return;
12355
12356 if (sign_undefine_by_name(name) == OK)
12357 rettv->vval.v_number = 0;
12358 }
12359}
12360
12361/*
12362 * "sign_unplace()" function
12363 */
12364 static void
12365f_sign_unplace(typval_T *argvars, typval_T *rettv)
12366{
12367 dict_T *dict;
12368 dictitem_T *di;
12369 int sign_id = 0;
12370 buf_T *buf = NULL;
12371 char_u *group = NULL;
12372
12373 rettv->vval.v_number = -1;
12374
12375 if (argvars[0].v_type != VAR_STRING)
12376 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012377 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012378 return;
12379 }
12380
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012381 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012382 if (group[0] == '\0')
12383 group = NULL; // global sign group
12384 else
12385 {
12386 group = vim_strsave(group);
12387 if (group == NULL)
12388 return;
12389 }
12390
12391 if (argvars[1].v_type != VAR_UNKNOWN)
12392 {
12393 if (argvars[1].v_type != VAR_DICT)
12394 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012395 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012396 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012397 }
12398 dict = argvars[1].vval.v_dict;
12399
12400 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12401 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012402 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012403 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012404 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012405 }
12406 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12407 sign_id = dict_get_number(dict, (char_u *)"id");
12408 }
12409
12410 if (buf == NULL)
12411 {
12412 // Delete the sign in all the buffers
12413 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012414 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012415 rettv->vval.v_number = 0;
12416 }
12417 else
12418 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012419 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012420 rettv->vval.v_number = 0;
12421 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012422
12423cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012424 vim_free(group);
12425}
12426#endif
12427
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012428/*
12429 * "simplify()" function
12430 */
12431 static void
12432f_simplify(typval_T *argvars, typval_T *rettv)
12433{
12434 char_u *p;
12435
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012436 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012437 rettv->vval.v_string = vim_strsave(p);
12438 simplify_filename(rettv->vval.v_string); /* simplify in place */
12439 rettv->v_type = VAR_STRING;
12440}
12441
12442#ifdef FEAT_FLOAT
12443/*
12444 * "sin()" function
12445 */
12446 static void
12447f_sin(typval_T *argvars, typval_T *rettv)
12448{
12449 float_T f = 0.0;
12450
12451 rettv->v_type = VAR_FLOAT;
12452 if (get_float_arg(argvars, &f) == OK)
12453 rettv->vval.v_float = sin(f);
12454 else
12455 rettv->vval.v_float = 0.0;
12456}
12457
12458/*
12459 * "sinh()" function
12460 */
12461 static void
12462f_sinh(typval_T *argvars, typval_T *rettv)
12463{
12464 float_T f = 0.0;
12465
12466 rettv->v_type = VAR_FLOAT;
12467 if (get_float_arg(argvars, &f) == OK)
12468 rettv->vval.v_float = sinh(f);
12469 else
12470 rettv->vval.v_float = 0.0;
12471}
12472#endif
12473
Bram Moolenaareae1b912019-05-09 15:12:55 +020012474static int item_compare(const void *s1, const void *s2);
12475static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012476
12477/* struct used in the array that's given to qsort() */
12478typedef struct
12479{
12480 listitem_T *item;
12481 int idx;
12482} sortItem_T;
12483
12484/* struct storing information about current sort */
12485typedef struct
12486{
12487 int item_compare_ic;
12488 int item_compare_numeric;
12489 int item_compare_numbers;
12490#ifdef FEAT_FLOAT
12491 int item_compare_float;
12492#endif
12493 char_u *item_compare_func;
12494 partial_T *item_compare_partial;
12495 dict_T *item_compare_selfdict;
12496 int item_compare_func_err;
12497 int item_compare_keep_zero;
12498} sortinfo_T;
12499static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012500#define ITEM_COMPARE_FAIL 999
12501
12502/*
12503 * Compare functions for f_sort() and f_uniq() below.
12504 */
12505 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012506item_compare(const void *s1, const void *s2)
12507{
12508 sortItem_T *si1, *si2;
12509 typval_T *tv1, *tv2;
12510 char_u *p1, *p2;
12511 char_u *tofree1 = NULL, *tofree2 = NULL;
12512 int res;
12513 char_u numbuf1[NUMBUFLEN];
12514 char_u numbuf2[NUMBUFLEN];
12515
12516 si1 = (sortItem_T *)s1;
12517 si2 = (sortItem_T *)s2;
12518 tv1 = &si1->item->li_tv;
12519 tv2 = &si2->item->li_tv;
12520
12521 if (sortinfo->item_compare_numbers)
12522 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012523 varnumber_T v1 = tv_get_number(tv1);
12524 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012525
12526 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12527 }
12528
12529#ifdef FEAT_FLOAT
12530 if (sortinfo->item_compare_float)
12531 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012532 float_T v1 = tv_get_float(tv1);
12533 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012534
12535 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12536 }
12537#endif
12538
12539 /* tv2string() puts quotes around a string and allocates memory. Don't do
12540 * that for string variables. Use a single quote when comparing with a
12541 * non-string to do what the docs promise. */
12542 if (tv1->v_type == VAR_STRING)
12543 {
12544 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12545 p1 = (char_u *)"'";
12546 else
12547 p1 = tv1->vval.v_string;
12548 }
12549 else
12550 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12551 if (tv2->v_type == VAR_STRING)
12552 {
12553 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12554 p2 = (char_u *)"'";
12555 else
12556 p2 = tv2->vval.v_string;
12557 }
12558 else
12559 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12560 if (p1 == NULL)
12561 p1 = (char_u *)"";
12562 if (p2 == NULL)
12563 p2 = (char_u *)"";
12564 if (!sortinfo->item_compare_numeric)
12565 {
12566 if (sortinfo->item_compare_ic)
12567 res = STRICMP(p1, p2);
12568 else
12569 res = STRCMP(p1, p2);
12570 }
12571 else
12572 {
12573 double n1, n2;
12574 n1 = strtod((char *)p1, (char **)&p1);
12575 n2 = strtod((char *)p2, (char **)&p2);
12576 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12577 }
12578
12579 /* When the result would be zero, compare the item indexes. Makes the
12580 * sort stable. */
12581 if (res == 0 && !sortinfo->item_compare_keep_zero)
12582 res = si1->idx > si2->idx ? 1 : -1;
12583
12584 vim_free(tofree1);
12585 vim_free(tofree2);
12586 return res;
12587}
12588
12589 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012590item_compare2(const void *s1, const void *s2)
12591{
12592 sortItem_T *si1, *si2;
12593 int res;
12594 typval_T rettv;
12595 typval_T argv[3];
12596 int dummy;
12597 char_u *func_name;
12598 partial_T *partial = sortinfo->item_compare_partial;
12599
12600 /* shortcut after failure in previous call; compare all items equal */
12601 if (sortinfo->item_compare_func_err)
12602 return 0;
12603
12604 si1 = (sortItem_T *)s1;
12605 si2 = (sortItem_T *)s2;
12606
12607 if (partial == NULL)
12608 func_name = sortinfo->item_compare_func;
12609 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012610 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012611
12612 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12613 * in the copy without changing the original list items. */
12614 copy_tv(&si1->item->li_tv, &argv[0]);
12615 copy_tv(&si2->item->li_tv, &argv[1]);
12616
12617 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012618 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012619 partial, sortinfo->item_compare_selfdict);
12620 clear_tv(&argv[0]);
12621 clear_tv(&argv[1]);
12622
12623 if (res == FAIL)
12624 res = ITEM_COMPARE_FAIL;
12625 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012626 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012627 if (sortinfo->item_compare_func_err)
12628 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12629 clear_tv(&rettv);
12630
12631 /* When the result would be zero, compare the pointers themselves. Makes
12632 * the sort stable. */
12633 if (res == 0 && !sortinfo->item_compare_keep_zero)
12634 res = si1->idx > si2->idx ? 1 : -1;
12635
12636 return res;
12637}
12638
12639/*
12640 * "sort({list})" function
12641 */
12642 static void
12643do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12644{
12645 list_T *l;
12646 listitem_T *li;
12647 sortItem_T *ptrs;
12648 sortinfo_T *old_sortinfo;
12649 sortinfo_T info;
12650 long len;
12651 long i;
12652
12653 /* Pointer to current info struct used in compare function. Save and
12654 * restore the current one for nested calls. */
12655 old_sortinfo = sortinfo;
12656 sortinfo = &info;
12657
12658 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012659 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012660 else
12661 {
12662 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012663 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012664 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12665 TRUE))
12666 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012667 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012668
12669 len = list_len(l);
12670 if (len <= 1)
12671 goto theend; /* short list sorts pretty quickly */
12672
12673 info.item_compare_ic = FALSE;
12674 info.item_compare_numeric = FALSE;
12675 info.item_compare_numbers = FALSE;
12676#ifdef FEAT_FLOAT
12677 info.item_compare_float = FALSE;
12678#endif
12679 info.item_compare_func = NULL;
12680 info.item_compare_partial = NULL;
12681 info.item_compare_selfdict = NULL;
12682 if (argvars[1].v_type != VAR_UNKNOWN)
12683 {
12684 /* optional second argument: {func} */
12685 if (argvars[1].v_type == VAR_FUNC)
12686 info.item_compare_func = argvars[1].vval.v_string;
12687 else if (argvars[1].v_type == VAR_PARTIAL)
12688 info.item_compare_partial = argvars[1].vval.v_partial;
12689 else
12690 {
12691 int error = FALSE;
12692
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012693 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012694 if (error)
12695 goto theend; /* type error; errmsg already given */
12696 if (i == 1)
12697 info.item_compare_ic = TRUE;
12698 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012699 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012700 else if (i != 0)
12701 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012702 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012703 goto theend;
12704 }
12705 if (info.item_compare_func != NULL)
12706 {
12707 if (*info.item_compare_func == NUL)
12708 {
12709 /* empty string means default sort */
12710 info.item_compare_func = NULL;
12711 }
12712 else if (STRCMP(info.item_compare_func, "n") == 0)
12713 {
12714 info.item_compare_func = NULL;
12715 info.item_compare_numeric = TRUE;
12716 }
12717 else if (STRCMP(info.item_compare_func, "N") == 0)
12718 {
12719 info.item_compare_func = NULL;
12720 info.item_compare_numbers = TRUE;
12721 }
12722#ifdef FEAT_FLOAT
12723 else if (STRCMP(info.item_compare_func, "f") == 0)
12724 {
12725 info.item_compare_func = NULL;
12726 info.item_compare_float = TRUE;
12727 }
12728#endif
12729 else if (STRCMP(info.item_compare_func, "i") == 0)
12730 {
12731 info.item_compare_func = NULL;
12732 info.item_compare_ic = TRUE;
12733 }
12734 }
12735 }
12736
12737 if (argvars[2].v_type != VAR_UNKNOWN)
12738 {
12739 /* optional third argument: {dict} */
12740 if (argvars[2].v_type != VAR_DICT)
12741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012742 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012743 goto theend;
12744 }
12745 info.item_compare_selfdict = argvars[2].vval.v_dict;
12746 }
12747 }
12748
12749 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020012750 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012751 if (ptrs == NULL)
12752 goto theend;
12753
12754 i = 0;
12755 if (sort)
12756 {
12757 /* sort(): ptrs will be the list to sort */
12758 for (li = l->lv_first; li != NULL; li = li->li_next)
12759 {
12760 ptrs[i].item = li;
12761 ptrs[i].idx = i;
12762 ++i;
12763 }
12764
12765 info.item_compare_func_err = FALSE;
12766 info.item_compare_keep_zero = FALSE;
12767 /* test the compare function */
12768 if ((info.item_compare_func != NULL
12769 || info.item_compare_partial != NULL)
12770 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12771 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012772 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012773 else
12774 {
12775 /* Sort the array with item pointers. */
12776 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12777 info.item_compare_func == NULL
12778 && info.item_compare_partial == NULL
12779 ? item_compare : item_compare2);
12780
12781 if (!info.item_compare_func_err)
12782 {
12783 /* Clear the List and append the items in sorted order. */
12784 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12785 l->lv_len = 0;
12786 for (i = 0; i < len; ++i)
12787 list_append(l, ptrs[i].item);
12788 }
12789 }
12790 }
12791 else
12792 {
12793 int (*item_compare_func_ptr)(const void *, const void *);
12794
12795 /* f_uniq(): ptrs will be a stack of items to remove */
12796 info.item_compare_func_err = FALSE;
12797 info.item_compare_keep_zero = TRUE;
12798 item_compare_func_ptr = info.item_compare_func != NULL
12799 || info.item_compare_partial != NULL
12800 ? item_compare2 : item_compare;
12801
12802 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12803 li = li->li_next)
12804 {
12805 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12806 == 0)
12807 ptrs[i++].item = li;
12808 if (info.item_compare_func_err)
12809 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012810 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012811 break;
12812 }
12813 }
12814
12815 if (!info.item_compare_func_err)
12816 {
12817 while (--i >= 0)
12818 {
12819 li = ptrs[i].item->li_next;
12820 ptrs[i].item->li_next = li->li_next;
12821 if (li->li_next != NULL)
12822 li->li_next->li_prev = ptrs[i].item;
12823 else
12824 l->lv_last = ptrs[i].item;
12825 list_fix_watch(l, li);
12826 listitem_free(li);
12827 l->lv_len--;
12828 }
12829 }
12830 }
12831
12832 vim_free(ptrs);
12833 }
12834theend:
12835 sortinfo = old_sortinfo;
12836}
12837
12838/*
12839 * "sort({list})" function
12840 */
12841 static void
12842f_sort(typval_T *argvars, typval_T *rettv)
12843{
12844 do_sort_uniq(argvars, rettv, TRUE);
12845}
12846
12847/*
12848 * "uniq({list})" function
12849 */
12850 static void
12851f_uniq(typval_T *argvars, typval_T *rettv)
12852{
12853 do_sort_uniq(argvars, rettv, FALSE);
12854}
12855
12856/*
12857 * "soundfold({word})" function
12858 */
12859 static void
12860f_soundfold(typval_T *argvars, typval_T *rettv)
12861{
12862 char_u *s;
12863
12864 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012865 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012866#ifdef FEAT_SPELL
12867 rettv->vval.v_string = eval_soundfold(s);
12868#else
12869 rettv->vval.v_string = vim_strsave(s);
12870#endif
12871}
12872
12873/*
12874 * "spellbadword()" function
12875 */
12876 static void
12877f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12878{
12879 char_u *word = (char_u *)"";
12880 hlf_T attr = HLF_COUNT;
12881 int len = 0;
12882
12883 if (rettv_list_alloc(rettv) == FAIL)
12884 return;
12885
12886#ifdef FEAT_SPELL
12887 if (argvars[0].v_type == VAR_UNKNOWN)
12888 {
12889 /* Find the start and length of the badly spelled word. */
12890 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12891 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012892 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012893 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012894 curwin->w_set_curswant = TRUE;
12895 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012896 }
12897 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12898 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012899 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012900 int capcol = -1;
12901
12902 if (str != NULL)
12903 {
12904 /* Check the argument for spelling. */
12905 while (*str != NUL)
12906 {
12907 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12908 if (attr != HLF_COUNT)
12909 {
12910 word = str;
12911 break;
12912 }
12913 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012914 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012915 }
12916 }
12917 }
12918#endif
12919
12920 list_append_string(rettv->vval.v_list, word, len);
12921 list_append_string(rettv->vval.v_list, (char_u *)(
12922 attr == HLF_SPB ? "bad" :
12923 attr == HLF_SPR ? "rare" :
12924 attr == HLF_SPL ? "local" :
12925 attr == HLF_SPC ? "caps" :
12926 ""), -1);
12927}
12928
12929/*
12930 * "spellsuggest()" function
12931 */
12932 static void
12933f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12934{
12935#ifdef FEAT_SPELL
12936 char_u *str;
12937 int typeerr = FALSE;
12938 int maxcount;
12939 garray_T ga;
12940 int i;
12941 listitem_T *li;
12942 int need_capital = FALSE;
12943#endif
12944
12945 if (rettv_list_alloc(rettv) == FAIL)
12946 return;
12947
12948#ifdef FEAT_SPELL
12949 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12950 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012951 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012952 if (argvars[1].v_type != VAR_UNKNOWN)
12953 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012954 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012955 if (maxcount <= 0)
12956 return;
12957 if (argvars[2].v_type != VAR_UNKNOWN)
12958 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012959 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012960 if (typeerr)
12961 return;
12962 }
12963 }
12964 else
12965 maxcount = 25;
12966
12967 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12968
12969 for (i = 0; i < ga.ga_len; ++i)
12970 {
12971 str = ((char_u **)ga.ga_data)[i];
12972
12973 li = listitem_alloc();
12974 if (li == NULL)
12975 vim_free(str);
12976 else
12977 {
12978 li->li_tv.v_type = VAR_STRING;
12979 li->li_tv.v_lock = 0;
12980 li->li_tv.vval.v_string = str;
12981 list_append(rettv->vval.v_list, li);
12982 }
12983 }
12984 ga_clear(&ga);
12985 }
12986#endif
12987}
12988
12989 static void
12990f_split(typval_T *argvars, typval_T *rettv)
12991{
12992 char_u *str;
12993 char_u *end;
12994 char_u *pat = NULL;
12995 regmatch_T regmatch;
12996 char_u patbuf[NUMBUFLEN];
12997 char_u *save_cpo;
12998 int match;
12999 colnr_T col = 0;
13000 int keepempty = FALSE;
13001 int typeerr = FALSE;
13002
13003 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13004 save_cpo = p_cpo;
13005 p_cpo = (char_u *)"";
13006
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013007 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013008 if (argvars[1].v_type != VAR_UNKNOWN)
13009 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013010 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013011 if (pat == NULL)
13012 typeerr = TRUE;
13013 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013014 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013015 }
13016 if (pat == NULL || *pat == NUL)
13017 pat = (char_u *)"[\\x01- ]\\+";
13018
13019 if (rettv_list_alloc(rettv) == FAIL)
13020 return;
13021 if (typeerr)
13022 return;
13023
13024 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13025 if (regmatch.regprog != NULL)
13026 {
13027 regmatch.rm_ic = FALSE;
13028 while (*str != NUL || keepempty)
13029 {
13030 if (*str == NUL)
13031 match = FALSE; /* empty item at the end */
13032 else
13033 match = vim_regexec_nl(&regmatch, str, col);
13034 if (match)
13035 end = regmatch.startp[0];
13036 else
13037 end = str + STRLEN(str);
13038 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
13039 && *str != NUL && match && end < regmatch.endp[0]))
13040 {
13041 if (list_append_string(rettv->vval.v_list, str,
13042 (int)(end - str)) == FAIL)
13043 break;
13044 }
13045 if (!match)
13046 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010013047 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013048 if (regmatch.endp[0] > str)
13049 col = 0;
13050 else
Bram Moolenaar13505972019-01-24 15:04:48 +010013051 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013052 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013053 str = regmatch.endp[0];
13054 }
13055
13056 vim_regfree(regmatch.regprog);
13057 }
13058
13059 p_cpo = save_cpo;
13060}
13061
13062#ifdef FEAT_FLOAT
13063/*
13064 * "sqrt()" function
13065 */
13066 static void
13067f_sqrt(typval_T *argvars, typval_T *rettv)
13068{
13069 float_T f = 0.0;
13070
13071 rettv->v_type = VAR_FLOAT;
13072 if (get_float_arg(argvars, &f) == OK)
13073 rettv->vval.v_float = sqrt(f);
13074 else
13075 rettv->vval.v_float = 0.0;
13076}
13077
13078/*
13079 * "str2float()" function
13080 */
13081 static void
13082f_str2float(typval_T *argvars, typval_T *rettv)
13083{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013084 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013085 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013086
Bram Moolenaar08243d22017-01-10 16:12:29 +010013087 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013088 p = skipwhite(p + 1);
13089 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013090 if (isneg)
13091 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013092 rettv->v_type = VAR_FLOAT;
13093}
13094#endif
13095
13096/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013097 * "str2list()" function
13098 */
13099 static void
13100f_str2list(typval_T *argvars, typval_T *rettv)
13101{
13102 char_u *p;
13103 int utf8 = FALSE;
13104
13105 if (rettv_list_alloc(rettv) == FAIL)
13106 return;
13107
13108 if (argvars[1].v_type != VAR_UNKNOWN)
13109 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13110
13111 p = tv_get_string(&argvars[0]);
13112
13113 if (has_mbyte || utf8)
13114 {
13115 int (*ptr2len)(char_u *);
13116 int (*ptr2char)(char_u *);
13117
13118 if (utf8 || enc_utf8)
13119 {
13120 ptr2len = utf_ptr2len;
13121 ptr2char = utf_ptr2char;
13122 }
13123 else
13124 {
13125 ptr2len = mb_ptr2len;
13126 ptr2char = mb_ptr2char;
13127 }
13128
13129 for ( ; *p != NUL; p += (*ptr2len)(p))
13130 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13131 }
13132 else
13133 for ( ; *p != NUL; ++p)
13134 list_append_number(rettv->vval.v_list, *p);
13135}
13136
13137/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013138 * "str2nr()" function
13139 */
13140 static void
13141f_str2nr(typval_T *argvars, typval_T *rettv)
13142{
13143 int base = 10;
13144 char_u *p;
13145 varnumber_T n;
13146 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013147 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013148
13149 if (argvars[1].v_type != VAR_UNKNOWN)
13150 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013151 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013152 if (base != 2 && base != 8 && base != 10 && base != 16)
13153 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013154 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013155 return;
13156 }
13157 }
13158
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013159 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013160 isneg = (*p == '-');
13161 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013162 p = skipwhite(p + 1);
13163 switch (base)
13164 {
13165 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13166 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13167 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13168 default: what = 0;
13169 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013170 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13171 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013172 if (isneg)
13173 rettv->vval.v_number = -n;
13174 else
13175 rettv->vval.v_number = n;
13176
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013177}
13178
13179#ifdef HAVE_STRFTIME
13180/*
13181 * "strftime({format}[, {time}])" function
13182 */
13183 static void
13184f_strftime(typval_T *argvars, typval_T *rettv)
13185{
13186 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013187# ifdef HAVE_LOCALTIME_R
13188 struct tm tmval;
13189# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013190 struct tm *curtime;
13191 time_t seconds;
13192 char_u *p;
13193
13194 rettv->v_type = VAR_STRING;
13195
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013196 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013197 if (argvars[1].v_type == VAR_UNKNOWN)
13198 seconds = time(NULL);
13199 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013200 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013201# ifdef HAVE_LOCALTIME_R
13202 curtime = localtime_r(&seconds, &tmval);
13203# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013204 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013205# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013206 /* MSVC returns NULL for an invalid value of seconds. */
13207 if (curtime == NULL)
13208 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13209 else
13210 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013211 vimconv_T conv;
13212 char_u *enc;
13213
13214 conv.vc_type = CONV_NONE;
13215 enc = enc_locale();
13216 convert_setup(&conv, p_enc, enc);
13217 if (conv.vc_type != CONV_NONE)
13218 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013219 if (p != NULL)
13220 (void)strftime((char *)result_buf, sizeof(result_buf),
13221 (char *)p, curtime);
13222 else
13223 result_buf[0] = NUL;
13224
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013225 if (conv.vc_type != CONV_NONE)
13226 vim_free(p);
13227 convert_setup(&conv, enc, p_enc);
13228 if (conv.vc_type != CONV_NONE)
13229 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13230 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013231 rettv->vval.v_string = vim_strsave(result_buf);
13232
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013233 /* Release conversion descriptors */
13234 convert_setup(&conv, NULL, NULL);
13235 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013236 }
13237}
13238#endif
13239
13240/*
13241 * "strgetchar()" function
13242 */
13243 static void
13244f_strgetchar(typval_T *argvars, typval_T *rettv)
13245{
13246 char_u *str;
13247 int len;
13248 int error = FALSE;
13249 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013250 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013251
13252 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013253 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013254 if (str == NULL)
13255 return;
13256 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013257 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013258 if (error)
13259 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013260
Bram Moolenaar13505972019-01-24 15:04:48 +010013261 while (charidx >= 0 && byteidx < len)
13262 {
13263 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013264 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013265 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13266 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013267 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013268 --charidx;
13269 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013270 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013271}
13272
13273/*
13274 * "stridx()" function
13275 */
13276 static void
13277f_stridx(typval_T *argvars, typval_T *rettv)
13278{
13279 char_u buf[NUMBUFLEN];
13280 char_u *needle;
13281 char_u *haystack;
13282 char_u *save_haystack;
13283 char_u *pos;
13284 int start_idx;
13285
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013286 needle = tv_get_string_chk(&argvars[1]);
13287 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013288 rettv->vval.v_number = -1;
13289 if (needle == NULL || haystack == NULL)
13290 return; /* type error; errmsg already given */
13291
13292 if (argvars[2].v_type != VAR_UNKNOWN)
13293 {
13294 int error = FALSE;
13295
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013296 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013297 if (error || start_idx >= (int)STRLEN(haystack))
13298 return;
13299 if (start_idx >= 0)
13300 haystack += start_idx;
13301 }
13302
13303 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13304 if (pos != NULL)
13305 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13306}
13307
13308/*
13309 * "string()" function
13310 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013311 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013312f_string(typval_T *argvars, typval_T *rettv)
13313{
13314 char_u *tofree;
13315 char_u numbuf[NUMBUFLEN];
13316
13317 rettv->v_type = VAR_STRING;
13318 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13319 get_copyID());
13320 /* Make a copy if we have a value but it's not in allocated memory. */
13321 if (rettv->vval.v_string != NULL && tofree == NULL)
13322 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13323}
13324
13325/*
13326 * "strlen()" function
13327 */
13328 static void
13329f_strlen(typval_T *argvars, typval_T *rettv)
13330{
13331 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013332 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013333}
13334
13335/*
13336 * "strchars()" function
13337 */
13338 static void
13339f_strchars(typval_T *argvars, typval_T *rettv)
13340{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013341 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013342 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013343 varnumber_T len = 0;
13344 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013345
13346 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013347 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013348 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013349 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013350 else
13351 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013352 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13353 while (*s != NUL)
13354 {
13355 func_mb_ptr2char_adv(&s);
13356 ++len;
13357 }
13358 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013359 }
13360}
13361
13362/*
13363 * "strdisplaywidth()" function
13364 */
13365 static void
13366f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13367{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013368 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013369 int col = 0;
13370
13371 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013372 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013373
13374 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13375}
13376
13377/*
13378 * "strwidth()" function
13379 */
13380 static void
13381f_strwidth(typval_T *argvars, typval_T *rettv)
13382{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013383 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013384
Bram Moolenaar13505972019-01-24 15:04:48 +010013385 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013386}
13387
13388/*
13389 * "strcharpart()" function
13390 */
13391 static void
13392f_strcharpart(typval_T *argvars, typval_T *rettv)
13393{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013394 char_u *p;
13395 int nchar;
13396 int nbyte = 0;
13397 int charlen;
13398 int len = 0;
13399 int slen;
13400 int error = FALSE;
13401
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013402 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013403 slen = (int)STRLEN(p);
13404
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013405 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013406 if (!error)
13407 {
13408 if (nchar > 0)
13409 while (nchar > 0 && nbyte < slen)
13410 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013411 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013412 --nchar;
13413 }
13414 else
13415 nbyte = nchar;
13416 if (argvars[2].v_type != VAR_UNKNOWN)
13417 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013418 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013419 while (charlen > 0 && nbyte + len < slen)
13420 {
13421 int off = nbyte + len;
13422
13423 if (off < 0)
13424 len += 1;
13425 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013426 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013427 --charlen;
13428 }
13429 }
13430 else
13431 len = slen - nbyte; /* default: all bytes that are available. */
13432 }
13433
13434 /*
13435 * Only return the overlap between the specified part and the actual
13436 * string.
13437 */
13438 if (nbyte < 0)
13439 {
13440 len += nbyte;
13441 nbyte = 0;
13442 }
13443 else if (nbyte > slen)
13444 nbyte = slen;
13445 if (len < 0)
13446 len = 0;
13447 else if (nbyte + len > slen)
13448 len = slen - nbyte;
13449
13450 rettv->v_type = VAR_STRING;
13451 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013452}
13453
13454/*
13455 * "strpart()" function
13456 */
13457 static void
13458f_strpart(typval_T *argvars, typval_T *rettv)
13459{
13460 char_u *p;
13461 int n;
13462 int len;
13463 int slen;
13464 int error = FALSE;
13465
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013466 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013467 slen = (int)STRLEN(p);
13468
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013469 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013470 if (error)
13471 len = 0;
13472 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013473 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013474 else
13475 len = slen - n; /* default len: all bytes that are available. */
13476
13477 /*
13478 * Only return the overlap between the specified part and the actual
13479 * string.
13480 */
13481 if (n < 0)
13482 {
13483 len += n;
13484 n = 0;
13485 }
13486 else if (n > slen)
13487 n = slen;
13488 if (len < 0)
13489 len = 0;
13490 else if (n + len > slen)
13491 len = slen - n;
13492
13493 rettv->v_type = VAR_STRING;
13494 rettv->vval.v_string = vim_strnsave(p + n, len);
13495}
13496
13497/*
13498 * "strridx()" function
13499 */
13500 static void
13501f_strridx(typval_T *argvars, typval_T *rettv)
13502{
13503 char_u buf[NUMBUFLEN];
13504 char_u *needle;
13505 char_u *haystack;
13506 char_u *rest;
13507 char_u *lastmatch = NULL;
13508 int haystack_len, end_idx;
13509
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013510 needle = tv_get_string_chk(&argvars[1]);
13511 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013512
13513 rettv->vval.v_number = -1;
13514 if (needle == NULL || haystack == NULL)
13515 return; /* type error; errmsg already given */
13516
13517 haystack_len = (int)STRLEN(haystack);
13518 if (argvars[2].v_type != VAR_UNKNOWN)
13519 {
13520 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013521 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013522 if (end_idx < 0)
13523 return; /* can never find a match */
13524 }
13525 else
13526 end_idx = haystack_len;
13527
13528 if (*needle == NUL)
13529 {
13530 /* Empty string matches past the end. */
13531 lastmatch = haystack + end_idx;
13532 }
13533 else
13534 {
13535 for (rest = haystack; *rest != '\0'; ++rest)
13536 {
13537 rest = (char_u *)strstr((char *)rest, (char *)needle);
13538 if (rest == NULL || rest > haystack + end_idx)
13539 break;
13540 lastmatch = rest;
13541 }
13542 }
13543
13544 if (lastmatch == NULL)
13545 rettv->vval.v_number = -1;
13546 else
13547 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13548}
13549
13550/*
13551 * "strtrans()" function
13552 */
13553 static void
13554f_strtrans(typval_T *argvars, typval_T *rettv)
13555{
13556 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013557 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013558}
13559
13560/*
13561 * "submatch()" function
13562 */
13563 static void
13564f_submatch(typval_T *argvars, typval_T *rettv)
13565{
13566 int error = FALSE;
13567 int no;
13568 int retList = 0;
13569
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013570 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013571 if (error)
13572 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013573 if (no < 0 || no >= NSUBEXP)
13574 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013575 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013576 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013577 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013578 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013579 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013580 if (error)
13581 return;
13582
13583 if (retList == 0)
13584 {
13585 rettv->v_type = VAR_STRING;
13586 rettv->vval.v_string = reg_submatch(no);
13587 }
13588 else
13589 {
13590 rettv->v_type = VAR_LIST;
13591 rettv->vval.v_list = reg_submatch_list(no);
13592 }
13593}
13594
13595/*
13596 * "substitute()" function
13597 */
13598 static void
13599f_substitute(typval_T *argvars, typval_T *rettv)
13600{
13601 char_u patbuf[NUMBUFLEN];
13602 char_u subbuf[NUMBUFLEN];
13603 char_u flagsbuf[NUMBUFLEN];
13604
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013605 char_u *str = tv_get_string_chk(&argvars[0]);
13606 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013607 char_u *sub = NULL;
13608 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013609 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013610
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013611 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13612 expr = &argvars[2];
13613 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013614 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013615
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013616 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013617 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13618 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013619 rettv->vval.v_string = NULL;
13620 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013621 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013622}
13623
13624/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013625 * "swapinfo(swap_filename)" function
13626 */
13627 static void
13628f_swapinfo(typval_T *argvars, typval_T *rettv)
13629{
13630 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013631 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013632}
13633
13634/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013635 * "swapname(expr)" function
13636 */
13637 static void
13638f_swapname(typval_T *argvars, typval_T *rettv)
13639{
13640 buf_T *buf;
13641
13642 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013643 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013644 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13645 || buf->b_ml.ml_mfp->mf_fname == NULL)
13646 rettv->vval.v_string = NULL;
13647 else
13648 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13649}
13650
13651/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013652 * "synID(lnum, col, trans)" function
13653 */
13654 static void
13655f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13656{
13657 int id = 0;
13658#ifdef FEAT_SYN_HL
13659 linenr_T lnum;
13660 colnr_T col;
13661 int trans;
13662 int transerr = FALSE;
13663
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013664 lnum = tv_get_lnum(argvars); /* -1 on type error */
13665 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13666 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013667
13668 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13669 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13670 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13671#endif
13672
13673 rettv->vval.v_number = id;
13674}
13675
13676/*
13677 * "synIDattr(id, what [, mode])" function
13678 */
13679 static void
13680f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13681{
13682 char_u *p = NULL;
13683#ifdef FEAT_SYN_HL
13684 int id;
13685 char_u *what;
13686 char_u *mode;
13687 char_u modebuf[NUMBUFLEN];
13688 int modec;
13689
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013690 id = (int)tv_get_number(&argvars[0]);
13691 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013692 if (argvars[2].v_type != VAR_UNKNOWN)
13693 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013694 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013695 modec = TOLOWER_ASC(mode[0]);
13696 if (modec != 't' && modec != 'c' && modec != 'g')
13697 modec = 0; /* replace invalid with current */
13698 }
13699 else
13700 {
13701#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13702 if (USE_24BIT)
13703 modec = 'g';
13704 else
13705#endif
13706 if (t_colors > 1)
13707 modec = 'c';
13708 else
13709 modec = 't';
13710 }
13711
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013712 switch (TOLOWER_ASC(what[0]))
13713 {
13714 case 'b':
13715 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13716 p = highlight_color(id, what, modec);
13717 else /* bold */
13718 p = highlight_has_attr(id, HL_BOLD, modec);
13719 break;
13720
13721 case 'f': /* fg[#] or font */
13722 p = highlight_color(id, what, modec);
13723 break;
13724
13725 case 'i':
13726 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13727 p = highlight_has_attr(id, HL_INVERSE, modec);
13728 else /* italic */
13729 p = highlight_has_attr(id, HL_ITALIC, modec);
13730 break;
13731
13732 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013733 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013734 break;
13735
13736 case 'r': /* reverse */
13737 p = highlight_has_attr(id, HL_INVERSE, modec);
13738 break;
13739
13740 case 's':
13741 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13742 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013743 /* strikeout */
13744 else if (TOLOWER_ASC(what[1]) == 't' &&
13745 TOLOWER_ASC(what[2]) == 'r')
13746 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013747 else /* standout */
13748 p = highlight_has_attr(id, HL_STANDOUT, modec);
13749 break;
13750
13751 case 'u':
13752 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13753 /* underline */
13754 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13755 else
13756 /* undercurl */
13757 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13758 break;
13759 }
13760
13761 if (p != NULL)
13762 p = vim_strsave(p);
13763#endif
13764 rettv->v_type = VAR_STRING;
13765 rettv->vval.v_string = p;
13766}
13767
13768/*
13769 * "synIDtrans(id)" function
13770 */
13771 static void
13772f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13773{
13774 int id;
13775
13776#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013777 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013778
13779 if (id > 0)
13780 id = syn_get_final_id(id);
13781 else
13782#endif
13783 id = 0;
13784
13785 rettv->vval.v_number = id;
13786}
13787
13788/*
13789 * "synconcealed(lnum, col)" function
13790 */
13791 static void
13792f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13793{
13794#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13795 linenr_T lnum;
13796 colnr_T col;
13797 int syntax_flags = 0;
13798 int cchar;
13799 int matchid = 0;
13800 char_u str[NUMBUFLEN];
13801#endif
13802
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013803 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013804
13805#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013806 lnum = tv_get_lnum(argvars); /* -1 on type error */
13807 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013808
13809 vim_memset(str, NUL, sizeof(str));
13810
13811 if (rettv_list_alloc(rettv) != FAIL)
13812 {
13813 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13814 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13815 && curwin->w_p_cole > 0)
13816 {
13817 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13818 syntax_flags = get_syntax_info(&matchid);
13819
13820 /* get the conceal character */
13821 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13822 {
13823 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013824 if (cchar == NUL && curwin->w_p_cole == 1)
13825 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013826 if (cchar != NUL)
13827 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013828 if (has_mbyte)
13829 (*mb_char2bytes)(cchar, str);
13830 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013831 str[0] = cchar;
13832 }
13833 }
13834 }
13835
13836 list_append_number(rettv->vval.v_list,
13837 (syntax_flags & HL_CONCEAL) != 0);
13838 /* -1 to auto-determine strlen */
13839 list_append_string(rettv->vval.v_list, str, -1);
13840 list_append_number(rettv->vval.v_list, matchid);
13841 }
13842#endif
13843}
13844
13845/*
13846 * "synstack(lnum, col)" function
13847 */
13848 static void
13849f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13850{
13851#ifdef FEAT_SYN_HL
13852 linenr_T lnum;
13853 colnr_T col;
13854 int i;
13855 int id;
13856#endif
13857
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013858 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013859
13860#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013861 lnum = tv_get_lnum(argvars); /* -1 on type error */
13862 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013863
13864 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13865 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13866 && rettv_list_alloc(rettv) != FAIL)
13867 {
13868 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13869 for (i = 0; ; ++i)
13870 {
13871 id = syn_get_stack_item(i);
13872 if (id < 0)
13873 break;
13874 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13875 break;
13876 }
13877 }
13878#endif
13879}
13880
13881 static void
13882get_cmd_output_as_rettv(
13883 typval_T *argvars,
13884 typval_T *rettv,
13885 int retlist)
13886{
13887 char_u *res = NULL;
13888 char_u *p;
13889 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013890 int err = FALSE;
13891 FILE *fd;
13892 list_T *list = NULL;
13893 int flags = SHELL_SILENT;
13894
13895 rettv->v_type = VAR_STRING;
13896 rettv->vval.v_string = NULL;
13897 if (check_restricted() || check_secure())
13898 goto errret;
13899
13900 if (argvars[1].v_type != VAR_UNKNOWN)
13901 {
13902 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013903 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013904 * command.
13905 */
13906 if ((infile = vim_tempname('i', TRUE)) == NULL)
13907 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013908 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013909 goto errret;
13910 }
13911
13912 fd = mch_fopen((char *)infile, WRITEBIN);
13913 if (fd == NULL)
13914 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013915 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013916 goto errret;
13917 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013918 if (argvars[1].v_type == VAR_NUMBER)
13919 {
13920 linenr_T lnum;
13921 buf_T *buf;
13922
13923 buf = buflist_findnr(argvars[1].vval.v_number);
13924 if (buf == NULL)
13925 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013926 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013927 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013928 goto errret;
13929 }
13930
13931 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13932 {
13933 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13934 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13935 {
13936 err = TRUE;
13937 break;
13938 }
13939 if (putc(NL, fd) == EOF)
13940 {
13941 err = TRUE;
13942 break;
13943 }
13944 }
13945 }
13946 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013947 {
13948 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13949 err = TRUE;
13950 }
13951 else
13952 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013953 size_t len;
13954 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013955
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013956 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013957 if (p == NULL)
13958 {
13959 fclose(fd);
13960 goto errret; /* type error; errmsg already given */
13961 }
13962 len = STRLEN(p);
13963 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13964 err = TRUE;
13965 }
13966 if (fclose(fd) != 0)
13967 err = TRUE;
13968 if (err)
13969 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013970 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013971 goto errret;
13972 }
13973 }
13974
13975 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13976 * echoes typeahead, that messes up the display. */
13977 if (!msg_silent)
13978 flags += SHELL_COOKED;
13979
13980 if (retlist)
13981 {
13982 int len;
13983 listitem_T *li;
13984 char_u *s = NULL;
13985 char_u *start;
13986 char_u *end;
13987 int i;
13988
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013989 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013990 if (res == NULL)
13991 goto errret;
13992
13993 list = list_alloc();
13994 if (list == NULL)
13995 goto errret;
13996
13997 for (i = 0; i < len; ++i)
13998 {
13999 start = res + i;
14000 while (i < len && res[i] != NL)
14001 ++i;
14002 end = res + i;
14003
Bram Moolenaar964b3742019-05-24 18:54:09 +020014004 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014005 if (s == NULL)
14006 goto errret;
14007
14008 for (p = s; start < end; ++p, ++start)
14009 *p = *start == NUL ? NL : *start;
14010 *p = NUL;
14011
14012 li = listitem_alloc();
14013 if (li == NULL)
14014 {
14015 vim_free(s);
14016 goto errret;
14017 }
14018 li->li_tv.v_type = VAR_STRING;
14019 li->li_tv.v_lock = 0;
14020 li->li_tv.vval.v_string = s;
14021 list_append(list, li);
14022 }
14023
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014024 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014025 list = NULL;
14026 }
14027 else
14028 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014029 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010014030#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014031 /* translate <CR><NL> into <NL> */
14032 if (res != NULL)
14033 {
14034 char_u *s, *d;
14035
14036 d = res;
14037 for (s = res; *s; ++s)
14038 {
14039 if (s[0] == CAR && s[1] == NL)
14040 ++s;
14041 *d++ = *s;
14042 }
14043 *d = NUL;
14044 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014045#endif
14046 rettv->vval.v_string = res;
14047 res = NULL;
14048 }
14049
14050errret:
14051 if (infile != NULL)
14052 {
14053 mch_remove(infile);
14054 vim_free(infile);
14055 }
14056 if (res != NULL)
14057 vim_free(res);
14058 if (list != NULL)
14059 list_free(list);
14060}
14061
14062/*
14063 * "system()" function
14064 */
14065 static void
14066f_system(typval_T *argvars, typval_T *rettv)
14067{
14068 get_cmd_output_as_rettv(argvars, rettv, FALSE);
14069}
14070
14071/*
14072 * "systemlist()" function
14073 */
14074 static void
14075f_systemlist(typval_T *argvars, typval_T *rettv)
14076{
14077 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14078}
14079
14080/*
14081 * "tabpagebuflist()" function
14082 */
14083 static void
14084f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14085{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014086 tabpage_T *tp;
14087 win_T *wp = NULL;
14088
14089 if (argvars[0].v_type == VAR_UNKNOWN)
14090 wp = firstwin;
14091 else
14092 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014093 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014094 if (tp != NULL)
14095 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14096 }
14097 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14098 {
14099 for (; wp != NULL; wp = wp->w_next)
14100 if (list_append_number(rettv->vval.v_list,
14101 wp->w_buffer->b_fnum) == FAIL)
14102 break;
14103 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014104}
14105
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014106/*
14107 * "tabpagenr()" function
14108 */
14109 static void
14110f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14111{
14112 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014113 char_u *arg;
14114
14115 if (argvars[0].v_type != VAR_UNKNOWN)
14116 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014117 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014118 nr = 0;
14119 if (arg != NULL)
14120 {
14121 if (STRCMP(arg, "$") == 0)
14122 nr = tabpage_index(NULL) - 1;
14123 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014124 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014125 }
14126 }
14127 else
14128 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014129 rettv->vval.v_number = nr;
14130}
14131
14132
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014133/*
14134 * Common code for tabpagewinnr() and winnr().
14135 */
14136 static int
14137get_winnr(tabpage_T *tp, typval_T *argvar)
14138{
14139 win_T *twin;
14140 int nr = 1;
14141 win_T *wp;
14142 char_u *arg;
14143
14144 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14145 if (argvar->v_type != VAR_UNKNOWN)
14146 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014147 int invalid_arg = FALSE;
14148
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014149 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014150 if (arg == NULL)
14151 nr = 0; /* type error; errmsg already given */
14152 else if (STRCMP(arg, "$") == 0)
14153 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14154 else if (STRCMP(arg, "#") == 0)
14155 {
14156 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14157 if (twin == NULL)
14158 nr = 0;
14159 }
14160 else
14161 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014162 long count;
14163 char_u *endp;
14164
14165 // Extract the window count (if specified). e.g. winnr('3j')
14166 count = strtol((char *)arg, (char **)&endp, 10);
14167 if (count <= 0)
14168 count = 1; // if count is not specified, default to 1
14169 if (endp != NULL && *endp != '\0')
14170 {
14171 if (STRCMP(endp, "j") == 0)
14172 twin = win_vert_neighbor(tp, twin, FALSE, count);
14173 else if (STRCMP(endp, "k") == 0)
14174 twin = win_vert_neighbor(tp, twin, TRUE, count);
14175 else if (STRCMP(endp, "h") == 0)
14176 twin = win_horz_neighbor(tp, twin, TRUE, count);
14177 else if (STRCMP(endp, "l") == 0)
14178 twin = win_horz_neighbor(tp, twin, FALSE, count);
14179 else
14180 invalid_arg = TRUE;
14181 }
14182 else
14183 invalid_arg = TRUE;
14184 }
14185
14186 if (invalid_arg)
14187 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014188 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014189 nr = 0;
14190 }
14191 }
14192
14193 if (nr > 0)
14194 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14195 wp != twin; wp = wp->w_next)
14196 {
14197 if (wp == NULL)
14198 {
14199 /* didn't find it in this tabpage */
14200 nr = 0;
14201 break;
14202 }
14203 ++nr;
14204 }
14205 return nr;
14206}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014207
14208/*
14209 * "tabpagewinnr()" function
14210 */
14211 static void
14212f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14213{
14214 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014215 tabpage_T *tp;
14216
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014217 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014218 if (tp == NULL)
14219 nr = 0;
14220 else
14221 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014222 rettv->vval.v_number = nr;
14223}
14224
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014225/*
14226 * "tagfiles()" function
14227 */
14228 static void
14229f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14230{
14231 char_u *fname;
14232 tagname_T tn;
14233 int first;
14234
14235 if (rettv_list_alloc(rettv) == FAIL)
14236 return;
14237 fname = alloc(MAXPATHL);
14238 if (fname == NULL)
14239 return;
14240
14241 for (first = TRUE; ; first = FALSE)
14242 if (get_tagfname(&tn, first, fname) == FAIL
14243 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14244 break;
14245 tagname_free(&tn);
14246 vim_free(fname);
14247}
14248
14249/*
14250 * "taglist()" function
14251 */
14252 static void
14253f_taglist(typval_T *argvars, typval_T *rettv)
14254{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014255 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014256 char_u *tag_pattern;
14257
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014258 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014259
14260 rettv->vval.v_number = FALSE;
14261 if (*tag_pattern == NUL)
14262 return;
14263
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014264 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014265 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014266 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014267 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014268}
14269
14270/*
14271 * "tempname()" function
14272 */
14273 static void
14274f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14275{
14276 static int x = 'A';
14277
14278 rettv->v_type = VAR_STRING;
14279 rettv->vval.v_string = vim_tempname(x, FALSE);
14280
14281 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14282 * names. Skip 'I' and 'O', they are used for shell redirection. */
14283 do
14284 {
14285 if (x == 'Z')
14286 x = '0';
14287 else if (x == '9')
14288 x = 'A';
14289 else
14290 {
14291#ifdef EBCDIC
14292 if (x == 'I')
14293 x = 'J';
14294 else if (x == 'R')
14295 x = 'S';
14296 else
14297#endif
14298 ++x;
14299 }
14300 } while (x == 'I' || x == 'O');
14301}
14302
14303#ifdef FEAT_FLOAT
14304/*
14305 * "tan()" function
14306 */
14307 static void
14308f_tan(typval_T *argvars, typval_T *rettv)
14309{
14310 float_T f = 0.0;
14311
14312 rettv->v_type = VAR_FLOAT;
14313 if (get_float_arg(argvars, &f) == OK)
14314 rettv->vval.v_float = tan(f);
14315 else
14316 rettv->vval.v_float = 0.0;
14317}
14318
14319/*
14320 * "tanh()" function
14321 */
14322 static void
14323f_tanh(typval_T *argvars, typval_T *rettv)
14324{
14325 float_T f = 0.0;
14326
14327 rettv->v_type = VAR_FLOAT;
14328 if (get_float_arg(argvars, &f) == OK)
14329 rettv->vval.v_float = tanh(f);
14330 else
14331 rettv->vval.v_float = 0.0;
14332}
14333#endif
14334
14335/*
14336 * "test_alloc_fail(id, countdown, repeat)" function
14337 */
14338 static void
14339f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14340{
14341 if (argvars[0].v_type != VAR_NUMBER
14342 || argvars[0].vval.v_number <= 0
14343 || argvars[1].v_type != VAR_NUMBER
14344 || argvars[1].vval.v_number < 0
14345 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014346 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014347 else
14348 {
14349 alloc_fail_id = argvars[0].vval.v_number;
14350 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014351 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014352 alloc_fail_countdown = argvars[1].vval.v_number;
14353 alloc_fail_repeat = argvars[2].vval.v_number;
14354 did_outofmem_msg = FALSE;
14355 }
14356}
14357
14358/*
14359 * "test_autochdir()"
14360 */
14361 static void
14362f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14363{
14364#if defined(FEAT_AUTOCHDIR)
14365 test_autochdir = TRUE;
14366#endif
14367}
14368
14369/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014370 * "test_feedinput()"
14371 */
14372 static void
14373f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14374{
14375#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014376 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014377
14378 if (val != NULL)
14379 {
14380 trash_input_buf();
14381 add_to_input_buf_csi(val, (int)STRLEN(val));
14382 }
14383#endif
14384}
14385
14386/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014387 * "test_getvalue({name})" function
14388 */
14389 static void
14390f_test_getvalue(typval_T *argvars, typval_T *rettv)
14391{
14392 if (argvars[0].v_type != VAR_STRING)
14393 emsg(_(e_invarg));
14394 else
14395 {
14396 char_u *name = tv_get_string(&argvars[0]);
14397
14398 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14399 rettv->vval.v_number = need_fileinfo;
14400 else
14401 semsg(_(e_invarg2), name);
14402 }
14403}
14404
14405/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014406 * "test_option_not_set({name})" function
14407 */
14408 static void
14409f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14410{
14411 char_u *name = (char_u *)"";
14412
14413 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014414 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014415 else
14416 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014417 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014418 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014419 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014420 }
14421}
14422
14423/*
14424 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014425 */
14426 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014427f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014428{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014429 char_u *name = (char_u *)"";
14430 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014431 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014432
14433 if (argvars[0].v_type != VAR_STRING
14434 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014435 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014436 else
14437 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014438 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014439 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014440
14441 if (STRCMP(name, (char_u *)"redraw") == 0)
14442 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014443 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14444 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014445 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14446 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014447 else if (STRCMP(name, (char_u *)"starting") == 0)
14448 {
14449 if (val)
14450 {
14451 if (save_starting < 0)
14452 save_starting = starting;
14453 starting = 0;
14454 }
14455 else
14456 {
14457 starting = save_starting;
14458 save_starting = -1;
14459 }
14460 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014461 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14462 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014463 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14464 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014465 else if (STRCMP(name, (char_u *)"ALL") == 0)
14466 {
14467 disable_char_avail_for_testing = FALSE;
14468 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014469 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014470 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014471 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014472 if (save_starting >= 0)
14473 {
14474 starting = save_starting;
14475 save_starting = -1;
14476 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014477 }
14478 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014479 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014480 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014481}
14482
14483/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014484 * "test_refcount({expr})" function
14485 */
14486 static void
14487f_test_refcount(typval_T *argvars, typval_T *rettv)
14488{
14489 int retval = -1;
14490
14491 switch (argvars[0].v_type)
14492 {
14493 case VAR_UNKNOWN:
14494 case VAR_NUMBER:
14495 case VAR_FLOAT:
14496 case VAR_SPECIAL:
14497 case VAR_STRING:
14498 break;
14499 case VAR_JOB:
14500#ifdef FEAT_JOB_CHANNEL
14501 if (argvars[0].vval.v_job != NULL)
14502 retval = argvars[0].vval.v_job->jv_refcount - 1;
14503#endif
14504 break;
14505 case VAR_CHANNEL:
14506#ifdef FEAT_JOB_CHANNEL
14507 if (argvars[0].vval.v_channel != NULL)
14508 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14509#endif
14510 break;
14511 case VAR_FUNC:
14512 if (argvars[0].vval.v_string != NULL)
14513 {
14514 ufunc_T *fp;
14515
14516 fp = find_func(argvars[0].vval.v_string);
14517 if (fp != NULL)
14518 retval = fp->uf_refcount;
14519 }
14520 break;
14521 case VAR_PARTIAL:
14522 if (argvars[0].vval.v_partial != NULL)
14523 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14524 break;
14525 case VAR_BLOB:
14526 if (argvars[0].vval.v_blob != NULL)
14527 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14528 break;
14529 case VAR_LIST:
14530 if (argvars[0].vval.v_list != NULL)
14531 retval = argvars[0].vval.v_list->lv_refcount - 1;
14532 break;
14533 case VAR_DICT:
14534 if (argvars[0].vval.v_dict != NULL)
14535 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14536 break;
14537 }
14538
14539 rettv->v_type = VAR_NUMBER;
14540 rettv->vval.v_number = retval;
14541
14542}
14543
14544/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014545 * "test_garbagecollect_now()" function
14546 */
14547 static void
14548f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14549{
14550 /* This is dangerous, any Lists and Dicts used internally may be freed
14551 * while still in use. */
14552 garbage_collect(TRUE);
14553}
14554
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014555/*
14556 * "test_ignore_error()" function
14557 */
14558 static void
14559f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14560{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014561 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014562}
14563
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014564 static void
14565f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14566{
14567 rettv->v_type = VAR_BLOB;
14568 rettv->vval.v_blob = NULL;
14569}
14570
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014571#ifdef FEAT_JOB_CHANNEL
14572 static void
14573f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14574{
14575 rettv->v_type = VAR_CHANNEL;
14576 rettv->vval.v_channel = NULL;
14577}
14578#endif
14579
14580 static void
14581f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14582{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014583 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014584}
14585
14586#ifdef FEAT_JOB_CHANNEL
14587 static void
14588f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14589{
14590 rettv->v_type = VAR_JOB;
14591 rettv->vval.v_job = NULL;
14592}
14593#endif
14594
14595 static void
14596f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14597{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014598 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014599}
14600
14601 static void
14602f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14603{
14604 rettv->v_type = VAR_PARTIAL;
14605 rettv->vval.v_partial = NULL;
14606}
14607
14608 static void
14609f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14610{
14611 rettv->v_type = VAR_STRING;
14612 rettv->vval.v_string = NULL;
14613}
14614
Bram Moolenaarab186732018-09-14 21:27:06 +020014615#ifdef FEAT_GUI
14616 static void
14617f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14618{
14619 char_u *which;
14620 long value;
14621 int dragging;
14622 scrollbar_T *sb = NULL;
14623
14624 if (argvars[0].v_type != VAR_STRING
14625 || (argvars[1].v_type) != VAR_NUMBER
14626 || (argvars[2].v_type) != VAR_NUMBER)
14627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014628 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014629 return;
14630 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014631 which = tv_get_string(&argvars[0]);
14632 value = tv_get_number(&argvars[1]);
14633 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014634
14635 if (STRCMP(which, "left") == 0)
14636 sb = &curwin->w_scrollbars[SBAR_LEFT];
14637 else if (STRCMP(which, "right") == 0)
14638 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14639 else if (STRCMP(which, "hor") == 0)
14640 sb = &gui.bottom_sbar;
14641 if (sb == NULL)
14642 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014643 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014644 return;
14645 }
14646 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014647# ifndef USE_ON_FLY_SCROLL
14648 // need to loop through normal_cmd() to handle the scroll events
14649 exec_normal(FALSE, TRUE, FALSE);
14650# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014651}
14652#endif
14653
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014654#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014655 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014656f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14657{
14658 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14659 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14660}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014661#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014662
14663 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014664f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14665{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014666 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014667}
14668
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014669/*
14670 * Get a callback from "arg". It can be a Funcref or a function name.
14671 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014672 * "cb_name" is not allocated.
14673 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014674 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014675 callback_T
14676get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014677{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014678 callback_T res;
14679
14680 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014681 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14682 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014683 res.cb_partial = arg->vval.v_partial;
14684 ++res.cb_partial->pt_refcount;
14685 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014686 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014687 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014688 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014689 res.cb_partial = NULL;
14690 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
14691 {
14692 // Note that we don't make a copy of the string.
14693 res.cb_name = arg->vval.v_string;
14694 func_ref(res.cb_name);
14695 }
14696 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14697 {
14698 res.cb_name = (char_u *)"";
14699 }
14700 else
14701 {
14702 emsg(_("E921: Invalid callback argument"));
14703 res.cb_name = NULL;
14704 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014705 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014706 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014707}
14708
14709/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014710 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014711 */
14712 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014713put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014714{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014715 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014716 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014717 tv->v_type = VAR_PARTIAL;
14718 tv->vval.v_partial = cb->cb_partial;
14719 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014720 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014721 else
14722 {
14723 tv->v_type = VAR_FUNC;
14724 tv->vval.v_string = vim_strsave(cb->cb_name);
14725 func_ref(cb->cb_name);
14726 }
14727}
14728
14729/*
14730 * Make a copy of "src" into "dest", allocating the function name if needed,
14731 * without incrementing the refcount.
14732 */
14733 void
14734set_callback(callback_T *dest, callback_T *src)
14735{
14736 if (src->cb_partial == NULL)
14737 {
14738 // just a function name, make a copy
14739 dest->cb_name = vim_strsave(src->cb_name);
14740 dest->cb_free_name = TRUE;
14741 }
14742 else
14743 {
14744 // cb_name is a pointer into cb_partial
14745 dest->cb_name = src->cb_name;
14746 dest->cb_free_name = FALSE;
14747 }
14748 dest->cb_partial = src->cb_partial;
14749}
14750
14751/*
14752 * Unref/free "callback" returned by get_callback() or set_callback().
14753 */
14754 void
14755free_callback(callback_T *callback)
14756{
14757 if (callback->cb_partial != NULL)
14758 {
14759 partial_unref(callback->cb_partial);
14760 callback->cb_partial = NULL;
14761 }
14762 else if (callback->cb_name != NULL)
14763 func_unref(callback->cb_name);
14764 if (callback->cb_free_name)
14765 {
14766 vim_free(callback->cb_name);
14767 callback->cb_free_name = FALSE;
14768 }
14769 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014770}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014771
14772#ifdef FEAT_TIMERS
14773/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014774 * "timer_info([timer])" function
14775 */
14776 static void
14777f_timer_info(typval_T *argvars, typval_T *rettv)
14778{
14779 timer_T *timer = NULL;
14780
14781 if (rettv_list_alloc(rettv) != OK)
14782 return;
14783 if (argvars[0].v_type != VAR_UNKNOWN)
14784 {
14785 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014786 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014787 else
14788 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014789 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014790 if (timer != NULL)
14791 add_timer_info(rettv, timer);
14792 }
14793 }
14794 else
14795 add_timer_info_all(rettv);
14796}
14797
14798/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014799 * "timer_pause(timer, paused)" function
14800 */
14801 static void
14802f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14803{
14804 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014805 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014806
14807 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014808 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014809 else
14810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014811 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014812 if (timer != NULL)
14813 timer->tr_paused = paused;
14814 }
14815}
14816
14817/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014818 * "timer_start(time, callback [, options])" function
14819 */
14820 static void
14821f_timer_start(typval_T *argvars, typval_T *rettv)
14822{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014823 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014824 timer_T *timer;
14825 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014826 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014827 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014828
Bram Moolenaar75537a92016-09-05 22:45:28 +020014829 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014830 if (check_secure())
14831 return;
14832 if (argvars[2].v_type != VAR_UNKNOWN)
14833 {
14834 if (argvars[2].v_type != VAR_DICT
14835 || (dict = argvars[2].vval.v_dict) == NULL)
14836 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014837 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014838 return;
14839 }
14840 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014841 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014842 }
14843
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014844 callback = get_callback(&argvars[1]);
14845 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014846 return;
14847
14848 timer = create_timer(msec, repeat);
14849 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014850 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014851 else
14852 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014853 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014854 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014855 }
14856}
14857
14858/*
14859 * "timer_stop(timer)" function
14860 */
14861 static void
14862f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14863{
14864 timer_T *timer;
14865
14866 if (argvars[0].v_type != VAR_NUMBER)
14867 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014868 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014869 return;
14870 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014871 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014872 if (timer != NULL)
14873 stop_timer(timer);
14874}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014875
14876/*
14877 * "timer_stopall()" function
14878 */
14879 static void
14880f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14881{
14882 stop_all_timers();
14883}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014884#endif
14885
14886/*
14887 * "tolower(string)" function
14888 */
14889 static void
14890f_tolower(typval_T *argvars, typval_T *rettv)
14891{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014892 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014893 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014894}
14895
14896/*
14897 * "toupper(string)" function
14898 */
14899 static void
14900f_toupper(typval_T *argvars, typval_T *rettv)
14901{
14902 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014903 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014904}
14905
14906/*
14907 * "tr(string, fromstr, tostr)" function
14908 */
14909 static void
14910f_tr(typval_T *argvars, typval_T *rettv)
14911{
14912 char_u *in_str;
14913 char_u *fromstr;
14914 char_u *tostr;
14915 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014916 int inlen;
14917 int fromlen;
14918 int tolen;
14919 int idx;
14920 char_u *cpstr;
14921 int cplen;
14922 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014923 char_u buf[NUMBUFLEN];
14924 char_u buf2[NUMBUFLEN];
14925 garray_T ga;
14926
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014927 in_str = tv_get_string(&argvars[0]);
14928 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14929 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014930
14931 /* Default return value: empty string. */
14932 rettv->v_type = VAR_STRING;
14933 rettv->vval.v_string = NULL;
14934 if (fromstr == NULL || tostr == NULL)
14935 return; /* type error; errmsg already given */
14936 ga_init2(&ga, (int)sizeof(char), 80);
14937
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014938 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014939 /* not multi-byte: fromstr and tostr must be the same length */
14940 if (STRLEN(fromstr) != STRLEN(tostr))
14941 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014942error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014943 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014944 ga_clear(&ga);
14945 return;
14946 }
14947
14948 /* fromstr and tostr have to contain the same number of chars */
14949 while (*in_str != NUL)
14950 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014951 if (has_mbyte)
14952 {
14953 inlen = (*mb_ptr2len)(in_str);
14954 cpstr = in_str;
14955 cplen = inlen;
14956 idx = 0;
14957 for (p = fromstr; *p != NUL; p += fromlen)
14958 {
14959 fromlen = (*mb_ptr2len)(p);
14960 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14961 {
14962 for (p = tostr; *p != NUL; p += tolen)
14963 {
14964 tolen = (*mb_ptr2len)(p);
14965 if (idx-- == 0)
14966 {
14967 cplen = tolen;
14968 cpstr = p;
14969 break;
14970 }
14971 }
14972 if (*p == NUL) /* tostr is shorter than fromstr */
14973 goto error;
14974 break;
14975 }
14976 ++idx;
14977 }
14978
14979 if (first && cpstr == in_str)
14980 {
14981 /* Check that fromstr and tostr have the same number of
14982 * (multi-byte) characters. Done only once when a character
14983 * of in_str doesn't appear in fromstr. */
14984 first = FALSE;
14985 for (p = tostr; *p != NUL; p += tolen)
14986 {
14987 tolen = (*mb_ptr2len)(p);
14988 --idx;
14989 }
14990 if (idx != 0)
14991 goto error;
14992 }
14993
14994 (void)ga_grow(&ga, cplen);
14995 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14996 ga.ga_len += cplen;
14997
14998 in_str += inlen;
14999 }
15000 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015001 {
15002 /* When not using multi-byte chars we can do it faster. */
15003 p = vim_strchr(fromstr, *in_str);
15004 if (p != NULL)
15005 ga_append(&ga, tostr[p - fromstr]);
15006 else
15007 ga_append(&ga, *in_str);
15008 ++in_str;
15009 }
15010 }
15011
15012 /* add a terminating NUL */
15013 (void)ga_grow(&ga, 1);
15014 ga_append(&ga, NUL);
15015
15016 rettv->vval.v_string = ga.ga_data;
15017}
15018
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015019/*
15020 * "trim({expr})" function
15021 */
15022 static void
15023f_trim(typval_T *argvars, typval_T *rettv)
15024{
15025 char_u buf1[NUMBUFLEN];
15026 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015027 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015028 char_u *mask = NULL;
15029 char_u *tail;
15030 char_u *prev;
15031 char_u *p;
15032 int c1;
15033
15034 rettv->v_type = VAR_STRING;
15035 if (head == NULL)
15036 {
15037 rettv->vval.v_string = NULL;
15038 return;
15039 }
15040
15041 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015042 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015043
15044 while (*head != NUL)
15045 {
15046 c1 = PTR2CHAR(head);
15047 if (mask == NULL)
15048 {
15049 if (c1 > ' ' && c1 != 0xa0)
15050 break;
15051 }
15052 else
15053 {
15054 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15055 if (c1 == PTR2CHAR(p))
15056 break;
15057 if (*p == NUL)
15058 break;
15059 }
15060 MB_PTR_ADV(head);
15061 }
15062
15063 for (tail = head + STRLEN(head); tail > head; tail = prev)
15064 {
15065 prev = tail;
15066 MB_PTR_BACK(head, prev);
15067 c1 = PTR2CHAR(prev);
15068 if (mask == NULL)
15069 {
15070 if (c1 > ' ' && c1 != 0xa0)
15071 break;
15072 }
15073 else
15074 {
15075 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15076 if (c1 == PTR2CHAR(p))
15077 break;
15078 if (*p == NUL)
15079 break;
15080 }
15081 }
15082 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
15083}
15084
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015085#ifdef FEAT_FLOAT
15086/*
15087 * "trunc({float})" function
15088 */
15089 static void
15090f_trunc(typval_T *argvars, typval_T *rettv)
15091{
15092 float_T f = 0.0;
15093
15094 rettv->v_type = VAR_FLOAT;
15095 if (get_float_arg(argvars, &f) == OK)
15096 /* trunc() is not in C90, use floor() or ceil() instead. */
15097 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
15098 else
15099 rettv->vval.v_float = 0.0;
15100}
15101#endif
15102
15103/*
15104 * "type(expr)" function
15105 */
15106 static void
15107f_type(typval_T *argvars, typval_T *rettv)
15108{
15109 int n = -1;
15110
15111 switch (argvars[0].v_type)
15112 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020015113 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
15114 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015115 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020015116 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
15117 case VAR_LIST: n = VAR_TYPE_LIST; break;
15118 case VAR_DICT: n = VAR_TYPE_DICT; break;
15119 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015120 case VAR_SPECIAL:
15121 if (argvars[0].vval.v_number == VVAL_FALSE
15122 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020015123 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015124 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020015125 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015126 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020015127 case VAR_JOB: n = VAR_TYPE_JOB; break;
15128 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015129 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015130 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015131 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015132 n = -1;
15133 break;
15134 }
15135 rettv->vval.v_number = n;
15136}
15137
15138/*
15139 * "undofile(name)" function
15140 */
15141 static void
15142f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15143{
15144 rettv->v_type = VAR_STRING;
15145#ifdef FEAT_PERSISTENT_UNDO
15146 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015147 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015148
15149 if (*fname == NUL)
15150 {
15151 /* If there is no file name there will be no undo file. */
15152 rettv->vval.v_string = NULL;
15153 }
15154 else
15155 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015156 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015157
15158 if (ffname != NULL)
15159 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15160 vim_free(ffname);
15161 }
15162 }
15163#else
15164 rettv->vval.v_string = NULL;
15165#endif
15166}
15167
15168/*
15169 * "undotree()" function
15170 */
15171 static void
15172f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15173{
15174 if (rettv_dict_alloc(rettv) == OK)
15175 {
15176 dict_T *dict = rettv->vval.v_dict;
15177 list_T *list;
15178
Bram Moolenaare0be1672018-07-08 16:50:37 +020015179 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15180 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15181 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15182 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15183 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15184 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015185
15186 list = list_alloc();
15187 if (list != NULL)
15188 {
15189 u_eval_tree(curbuf->b_u_oldhead, list);
15190 dict_add_list(dict, "entries", list);
15191 }
15192 }
15193}
15194
15195/*
15196 * "values(dict)" function
15197 */
15198 static void
15199f_values(typval_T *argvars, typval_T *rettv)
15200{
15201 dict_list(argvars, rettv, 1);
15202}
15203
15204/*
15205 * "virtcol(string)" function
15206 */
15207 static void
15208f_virtcol(typval_T *argvars, typval_T *rettv)
15209{
15210 colnr_T vcol = 0;
15211 pos_T *fp;
15212 int fnum = curbuf->b_fnum;
15213
15214 fp = var2fpos(&argvars[0], FALSE, &fnum);
15215 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15216 && fnum == curbuf->b_fnum)
15217 {
15218 getvvcol(curwin, fp, NULL, NULL, &vcol);
15219 ++vcol;
15220 }
15221
15222 rettv->vval.v_number = vcol;
15223}
15224
15225/*
15226 * "visualmode()" function
15227 */
15228 static void
15229f_visualmode(typval_T *argvars, typval_T *rettv)
15230{
15231 char_u str[2];
15232
15233 rettv->v_type = VAR_STRING;
15234 str[0] = curbuf->b_visual_mode_eval;
15235 str[1] = NUL;
15236 rettv->vval.v_string = vim_strsave(str);
15237
15238 /* A non-zero number or non-empty string argument: reset mode. */
15239 if (non_zero_arg(&argvars[0]))
15240 curbuf->b_visual_mode_eval = NUL;
15241}
15242
15243/*
15244 * "wildmenumode()" function
15245 */
15246 static void
15247f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15248{
15249#ifdef FEAT_WILDMENU
15250 if (wild_menu_showing)
15251 rettv->vval.v_number = 1;
15252#endif
15253}
15254
15255/*
15256 * "winbufnr(nr)" function
15257 */
15258 static void
15259f_winbufnr(typval_T *argvars, typval_T *rettv)
15260{
15261 win_T *wp;
15262
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015263 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015264 if (wp == NULL)
15265 rettv->vval.v_number = -1;
15266 else
15267 rettv->vval.v_number = wp->w_buffer->b_fnum;
15268}
15269
15270/*
15271 * "wincol()" function
15272 */
15273 static void
15274f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15275{
15276 validate_cursor();
15277 rettv->vval.v_number = curwin->w_wcol + 1;
15278}
15279
15280/*
15281 * "winheight(nr)" function
15282 */
15283 static void
15284f_winheight(typval_T *argvars, typval_T *rettv)
15285{
15286 win_T *wp;
15287
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015288 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015289 if (wp == NULL)
15290 rettv->vval.v_number = -1;
15291 else
15292 rettv->vval.v_number = wp->w_height;
15293}
15294
15295/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015296 * "winlayout()" function
15297 */
15298 static void
15299f_winlayout(typval_T *argvars, typval_T *rettv)
15300{
15301 tabpage_T *tp;
15302
15303 if (rettv_list_alloc(rettv) != OK)
15304 return;
15305
15306 if (argvars[0].v_type == VAR_UNKNOWN)
15307 tp = curtab;
15308 else
15309 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015310 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015311 if (tp == NULL)
15312 return;
15313 }
15314
15315 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15316}
15317
15318/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015319 * "winline()" function
15320 */
15321 static void
15322f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15323{
15324 validate_cursor();
15325 rettv->vval.v_number = curwin->w_wrow + 1;
15326}
15327
15328/*
15329 * "winnr()" function
15330 */
15331 static void
15332f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15333{
15334 int nr = 1;
15335
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015336 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015337 rettv->vval.v_number = nr;
15338}
15339
15340/*
15341 * "winrestcmd()" function
15342 */
15343 static void
15344f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15345{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015346 win_T *wp;
15347 int winnr = 1;
15348 garray_T ga;
15349 char_u buf[50];
15350
15351 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015352 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015353 {
15354 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15355 ga_concat(&ga, buf);
15356 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15357 ga_concat(&ga, buf);
15358 ++winnr;
15359 }
15360 ga_append(&ga, NUL);
15361
15362 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015363 rettv->v_type = VAR_STRING;
15364}
15365
15366/*
15367 * "winrestview()" function
15368 */
15369 static void
15370f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15371{
15372 dict_T *dict;
15373
15374 if (argvars[0].v_type != VAR_DICT
15375 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015376 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015377 else
15378 {
15379 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015380 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015381 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015382 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015383 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015384 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015385 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15386 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015387 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015388 curwin->w_set_curswant = FALSE;
15389 }
15390
15391 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015392 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015393#ifdef FEAT_DIFF
15394 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015395 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015396#endif
15397 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015398 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015399 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015400 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015401
15402 check_cursor();
15403 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015404 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015405 changed_window_setting();
15406
15407 if (curwin->w_topline <= 0)
15408 curwin->w_topline = 1;
15409 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15410 curwin->w_topline = curbuf->b_ml.ml_line_count;
15411#ifdef FEAT_DIFF
15412 check_topfill(curwin, TRUE);
15413#endif
15414 }
15415}
15416
15417/*
15418 * "winsaveview()" function
15419 */
15420 static void
15421f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15422{
15423 dict_T *dict;
15424
15425 if (rettv_dict_alloc(rettv) == FAIL)
15426 return;
15427 dict = rettv->vval.v_dict;
15428
Bram Moolenaare0be1672018-07-08 16:50:37 +020015429 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15430 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015431 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015432 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015433 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015434
Bram Moolenaare0be1672018-07-08 16:50:37 +020015435 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015436#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015437 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015438#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015439 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15440 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015441}
15442
15443/*
15444 * "winwidth(nr)" function
15445 */
15446 static void
15447f_winwidth(typval_T *argvars, typval_T *rettv)
15448{
15449 win_T *wp;
15450
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015451 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015452 if (wp == NULL)
15453 rettv->vval.v_number = -1;
15454 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015455 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015456}
15457
15458/*
15459 * "wordcount()" function
15460 */
15461 static void
15462f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15463{
15464 if (rettv_dict_alloc(rettv) == FAIL)
15465 return;
15466 cursor_pos_info(rettv->vval.v_dict);
15467}
15468
15469/*
15470 * "writefile()" function
15471 */
15472 static void
15473f_writefile(typval_T *argvars, typval_T *rettv)
15474{
15475 int binary = FALSE;
15476 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015477#ifdef HAVE_FSYNC
15478 int do_fsync = p_fs;
15479#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015480 char_u *fname;
15481 FILE *fd;
15482 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015483 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015484 list_T *list = NULL;
15485 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015486
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015487 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015488 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015489 return;
15490
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015491 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015492 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015493 list = argvars[0].vval.v_list;
15494 if (list == NULL)
15495 return;
15496 for (li = list->lv_first; li != NULL; li = li->li_next)
15497 if (tv_get_string_chk(&li->li_tv) == NULL)
15498 return;
15499 }
15500 else if (argvars[0].v_type == VAR_BLOB)
15501 {
15502 blob = argvars[0].vval.v_blob;
15503 if (blob == NULL)
15504 return;
15505 }
15506 else
15507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015508 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015509 return;
15510 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015511
15512 if (argvars[2].v_type != VAR_UNKNOWN)
15513 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015514 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015515
15516 if (arg2 == NULL)
15517 return;
15518 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015519 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015520 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015521 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015522#ifdef HAVE_FSYNC
15523 if (vim_strchr(arg2, 's') != NULL)
15524 do_fsync = TRUE;
15525 else if (vim_strchr(arg2, 'S') != NULL)
15526 do_fsync = FALSE;
15527#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015528 }
15529
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015530 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015531 if (fname == NULL)
15532 return;
15533
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015534 /* Always open the file in binary mode, library functions have a mind of
15535 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015536 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15537 append ? APPENDBIN : WRITEBIN)) == NULL)
15538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015539 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015540 ret = -1;
15541 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015542 else if (blob)
15543 {
15544 if (write_blob(fd, blob) == FAIL)
15545 ret = -1;
15546#ifdef HAVE_FSYNC
15547 else if (do_fsync)
15548 // Ignore the error, the user wouldn't know what to do about it.
15549 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015550 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015551#endif
15552 fclose(fd);
15553 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015554 else
15555 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015556 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015557 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015558#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015559 else if (do_fsync)
15560 /* Ignore the error, the user wouldn't know what to do about it.
15561 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015562 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015563#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015564 fclose(fd);
15565 }
15566
15567 rettv->vval.v_number = ret;
15568}
15569
15570/*
15571 * "xor(expr, expr)" function
15572 */
15573 static void
15574f_xor(typval_T *argvars, typval_T *rettv)
15575{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015576 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15577 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015578}
15579
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015580#endif /* FEAT_EVAL */