blob: 03e326024439ecd097266dbcf4ded5af73992ac4 [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);
152static void f_extend(typval_T *argvars, typval_T *rettv);
153static void f_feedkeys(typval_T *argvars, typval_T *rettv);
154static void f_filereadable(typval_T *argvars, typval_T *rettv);
155static void f_filewritable(typval_T *argvars, typval_T *rettv);
156static void f_filter(typval_T *argvars, typval_T *rettv);
157static void f_finddir(typval_T *argvars, typval_T *rettv);
158static void f_findfile(typval_T *argvars, typval_T *rettv);
159#ifdef FEAT_FLOAT
160static void f_float2nr(typval_T *argvars, typval_T *rettv);
161static void f_floor(typval_T *argvars, typval_T *rettv);
162static void f_fmod(typval_T *argvars, typval_T *rettv);
163#endif
164static void f_fnameescape(typval_T *argvars, typval_T *rettv);
165static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
166static void f_foldclosed(typval_T *argvars, typval_T *rettv);
167static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
168static void f_foldlevel(typval_T *argvars, typval_T *rettv);
169static void f_foldtext(typval_T *argvars, typval_T *rettv);
170static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
171static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200172static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200173static void f_function(typval_T *argvars, typval_T *rettv);
174static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
175static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200176static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_getbufline(typval_T *argvars, typval_T *rettv);
178static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100179static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_getchar(typval_T *argvars, typval_T *rettv);
181static void f_getcharmod(typval_T *argvars, typval_T *rettv);
182static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
183static void f_getcmdline(typval_T *argvars, typval_T *rettv);
184#if defined(FEAT_CMDL_COMPL)
185static void f_getcompletion(typval_T *argvars, typval_T *rettv);
186#endif
187static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
188static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
189static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
190static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200191static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getfontname(typval_T *argvars, typval_T *rettv);
193static void f_getfperm(typval_T *argvars, typval_T *rettv);
194static void f_getfsize(typval_T *argvars, typval_T *rettv);
195static void f_getftime(typval_T *argvars, typval_T *rettv);
196static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100197static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200198static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200199static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_getmatches(typval_T *argvars, typval_T *rettv);
201static void f_getpid(typval_T *argvars, typval_T *rettv);
202static void f_getcurpos(typval_T *argvars, typval_T *rettv);
203static void f_getpos(typval_T *argvars, typval_T *rettv);
204static void f_getqflist(typval_T *argvars, typval_T *rettv);
205static void f_getreg(typval_T *argvars, typval_T *rettv);
206static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200207static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200208static void f_gettabvar(typval_T *argvars, typval_T *rettv);
209static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100210static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200211static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100212static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200213static void f_getwinposx(typval_T *argvars, typval_T *rettv);
214static void f_getwinposy(typval_T *argvars, typval_T *rettv);
215static void f_getwinvar(typval_T *argvars, typval_T *rettv);
216static void f_glob(typval_T *argvars, typval_T *rettv);
217static void f_globpath(typval_T *argvars, typval_T *rettv);
218static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
219static void f_has(typval_T *argvars, typval_T *rettv);
220static void f_has_key(typval_T *argvars, typval_T *rettv);
221static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
222static void f_hasmapto(typval_T *argvars, typval_T *rettv);
223static void f_histadd(typval_T *argvars, typval_T *rettv);
224static void f_histdel(typval_T *argvars, typval_T *rettv);
225static void f_histget(typval_T *argvars, typval_T *rettv);
226static void f_histnr(typval_T *argvars, typval_T *rettv);
227static void f_hlID(typval_T *argvars, typval_T *rettv);
228static void f_hlexists(typval_T *argvars, typval_T *rettv);
229static void f_hostname(typval_T *argvars, typval_T *rettv);
230static void f_iconv(typval_T *argvars, typval_T *rettv);
231static void f_indent(typval_T *argvars, typval_T *rettv);
232static void f_index(typval_T *argvars, typval_T *rettv);
233static void f_input(typval_T *argvars, typval_T *rettv);
234static void f_inputdialog(typval_T *argvars, typval_T *rettv);
235static void f_inputlist(typval_T *argvars, typval_T *rettv);
236static void f_inputrestore(typval_T *argvars, typval_T *rettv);
237static void f_inputsave(typval_T *argvars, typval_T *rettv);
238static void f_inputsecret(typval_T *argvars, typval_T *rettv);
239static void f_insert(typval_T *argvars, typval_T *rettv);
240static void f_invert(typval_T *argvars, typval_T *rettv);
241static void f_isdirectory(typval_T *argvars, typval_T *rettv);
242static void f_islocked(typval_T *argvars, typval_T *rettv);
243#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200244static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_isnan(typval_T *argvars, typval_T *rettv);
246#endif
247static void f_items(typval_T *argvars, typval_T *rettv);
248#ifdef FEAT_JOB_CHANNEL
249static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
250static void f_job_info(typval_T *argvars, typval_T *rettv);
251static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
252static void f_job_start(typval_T *argvars, typval_T *rettv);
253static void f_job_stop(typval_T *argvars, typval_T *rettv);
254static void f_job_status(typval_T *argvars, typval_T *rettv);
255#endif
256static void f_join(typval_T *argvars, typval_T *rettv);
257static void f_js_decode(typval_T *argvars, typval_T *rettv);
258static void f_js_encode(typval_T *argvars, typval_T *rettv);
259static void f_json_decode(typval_T *argvars, typval_T *rettv);
260static void f_json_encode(typval_T *argvars, typval_T *rettv);
261static void f_keys(typval_T *argvars, typval_T *rettv);
262static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
263static void f_len(typval_T *argvars, typval_T *rettv);
264static void f_libcall(typval_T *argvars, typval_T *rettv);
265static void f_libcallnr(typval_T *argvars, typval_T *rettv);
266static void f_line(typval_T *argvars, typval_T *rettv);
267static void f_line2byte(typval_T *argvars, typval_T *rettv);
268static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200269static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200270static void f_localtime(typval_T *argvars, typval_T *rettv);
271#ifdef FEAT_FLOAT
272static void f_log(typval_T *argvars, typval_T *rettv);
273static void f_log10(typval_T *argvars, typval_T *rettv);
274#endif
275#ifdef FEAT_LUA
276static void f_luaeval(typval_T *argvars, typval_T *rettv);
277#endif
278static void f_map(typval_T *argvars, typval_T *rettv);
279static void f_maparg(typval_T *argvars, typval_T *rettv);
280static void f_mapcheck(typval_T *argvars, typval_T *rettv);
281static void f_match(typval_T *argvars, typval_T *rettv);
282static void f_matchadd(typval_T *argvars, typval_T *rettv);
283static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
284static void f_matcharg(typval_T *argvars, typval_T *rettv);
285static void f_matchdelete(typval_T *argvars, typval_T *rettv);
286static void f_matchend(typval_T *argvars, typval_T *rettv);
287static void f_matchlist(typval_T *argvars, typval_T *rettv);
288static void f_matchstr(typval_T *argvars, typval_T *rettv);
289static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
290static void f_max(typval_T *argvars, typval_T *rettv);
291static void f_min(typval_T *argvars, typval_T *rettv);
292#ifdef vim_mkdir
293static void f_mkdir(typval_T *argvars, typval_T *rettv);
294#endif
295static void f_mode(typval_T *argvars, typval_T *rettv);
296#ifdef FEAT_MZSCHEME
297static void f_mzeval(typval_T *argvars, typval_T *rettv);
298#endif
299static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
300static void f_nr2char(typval_T *argvars, typval_T *rettv);
301static void f_or(typval_T *argvars, typval_T *rettv);
302static void f_pathshorten(typval_T *argvars, typval_T *rettv);
303#ifdef FEAT_PERL
304static void f_perleval(typval_T *argvars, typval_T *rettv);
305#endif
306#ifdef FEAT_FLOAT
307static void f_pow(typval_T *argvars, typval_T *rettv);
308#endif
309static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
310static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200311#ifdef FEAT_JOB_CHANNEL
312static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200313static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200314static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
315#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200316static void f_pumvisible(typval_T *argvars, typval_T *rettv);
317#ifdef FEAT_PYTHON3
318static void f_py3eval(typval_T *argvars, typval_T *rettv);
319#endif
320#ifdef FEAT_PYTHON
321static void f_pyeval(typval_T *argvars, typval_T *rettv);
322#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100323#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
324static void f_pyxeval(typval_T *argvars, typval_T *rettv);
325#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200326static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200327static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200328static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200329static void f_reg_executing(typval_T *argvars, typval_T *rettv);
330static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200331static void f_reltime(typval_T *argvars, typval_T *rettv);
332#ifdef FEAT_FLOAT
333static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
334#endif
335static void f_reltimestr(typval_T *argvars, typval_T *rettv);
336static void f_remote_expr(typval_T *argvars, typval_T *rettv);
337static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
338static void f_remote_peek(typval_T *argvars, typval_T *rettv);
339static void f_remote_read(typval_T *argvars, typval_T *rettv);
340static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100341static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200342static void f_remove(typval_T *argvars, typval_T *rettv);
343static void f_rename(typval_T *argvars, typval_T *rettv);
344static void f_repeat(typval_T *argvars, typval_T *rettv);
345static void f_resolve(typval_T *argvars, typval_T *rettv);
346static void f_reverse(typval_T *argvars, typval_T *rettv);
347#ifdef FEAT_FLOAT
348static void f_round(typval_T *argvars, typval_T *rettv);
349#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100350#ifdef FEAT_RUBY
351static void f_rubyeval(typval_T *argvars, typval_T *rettv);
352#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200353static void f_screenattr(typval_T *argvars, typval_T *rettv);
354static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100355static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200356static void f_screencol(typval_T *argvars, typval_T *rettv);
357static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100358static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200359static void f_search(typval_T *argvars, typval_T *rettv);
360static void f_searchdecl(typval_T *argvars, typval_T *rettv);
361static void f_searchpair(typval_T *argvars, typval_T *rettv);
362static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
363static void f_searchpos(typval_T *argvars, typval_T *rettv);
364static void f_server2client(typval_T *argvars, typval_T *rettv);
365static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200366static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200367static void f_setbufvar(typval_T *argvars, typval_T *rettv);
368static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
369static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200370static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200371static void f_setfperm(typval_T *argvars, typval_T *rettv);
372static void f_setline(typval_T *argvars, typval_T *rettv);
373static void f_setloclist(typval_T *argvars, typval_T *rettv);
374static void f_setmatches(typval_T *argvars, typval_T *rettv);
375static void f_setpos(typval_T *argvars, typval_T *rettv);
376static void f_setqflist(typval_T *argvars, typval_T *rettv);
377static void f_setreg(typval_T *argvars, typval_T *rettv);
378static void f_settabvar(typval_T *argvars, typval_T *rettv);
379static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100380static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200381static void f_setwinvar(typval_T *argvars, typval_T *rettv);
382#ifdef FEAT_CRYPT
383static void f_sha256(typval_T *argvars, typval_T *rettv);
384#endif /* FEAT_CRYPT */
385static void f_shellescape(typval_T *argvars, typval_T *rettv);
386static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100387#ifdef FEAT_SIGNS
388static void f_sign_define(typval_T *argvars, typval_T *rettv);
389static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
390static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100391static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100392static void f_sign_place(typval_T *argvars, typval_T *rettv);
393static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
394static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
395#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200396static void f_simplify(typval_T *argvars, typval_T *rettv);
397#ifdef FEAT_FLOAT
398static void f_sin(typval_T *argvars, typval_T *rettv);
399static void f_sinh(typval_T *argvars, typval_T *rettv);
400#endif
401static void f_sort(typval_T *argvars, typval_T *rettv);
402static void f_soundfold(typval_T *argvars, typval_T *rettv);
403static void f_spellbadword(typval_T *argvars, typval_T *rettv);
404static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
405static void f_split(typval_T *argvars, typval_T *rettv);
406#ifdef FEAT_FLOAT
407static void f_sqrt(typval_T *argvars, typval_T *rettv);
408static void f_str2float(typval_T *argvars, typval_T *rettv);
409#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200410static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200411static void f_str2nr(typval_T *argvars, typval_T *rettv);
412static void f_strchars(typval_T *argvars, typval_T *rettv);
413#ifdef HAVE_STRFTIME
414static void f_strftime(typval_T *argvars, typval_T *rettv);
415#endif
416static void f_strgetchar(typval_T *argvars, typval_T *rettv);
417static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200418static void f_strlen(typval_T *argvars, typval_T *rettv);
419static void f_strcharpart(typval_T *argvars, typval_T *rettv);
420static void f_strpart(typval_T *argvars, typval_T *rettv);
421static void f_strridx(typval_T *argvars, typval_T *rettv);
422static void f_strtrans(typval_T *argvars, typval_T *rettv);
423static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
424static void f_strwidth(typval_T *argvars, typval_T *rettv);
425static void f_submatch(typval_T *argvars, typval_T *rettv);
426static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200427static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200428static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200429static void f_synID(typval_T *argvars, typval_T *rettv);
430static void f_synIDattr(typval_T *argvars, typval_T *rettv);
431static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
432static void f_synstack(typval_T *argvars, typval_T *rettv);
433static void f_synconcealed(typval_T *argvars, typval_T *rettv);
434static void f_system(typval_T *argvars, typval_T *rettv);
435static void f_systemlist(typval_T *argvars, typval_T *rettv);
436static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
437static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
438static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
439static void f_taglist(typval_T *argvars, typval_T *rettv);
440static void f_tagfiles(typval_T *argvars, typval_T *rettv);
441static void f_tempname(typval_T *argvars, typval_T *rettv);
442static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
443static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200444static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareda65222019-05-16 20:29:44 +0200445static void f_test_getvalue(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200446static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100447static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100448static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200449static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100450static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100451static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200452#ifdef FEAT_JOB_CHANNEL
453static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
454#endif
455static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
456#ifdef FEAT_JOB_CHANNEL
457static void f_test_null_job(typval_T *argvars, typval_T *rettv);
458#endif
459static void f_test_null_list(typval_T *argvars, typval_T *rettv);
460static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
461static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200462#ifdef FEAT_GUI
463static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
464#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200465#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200466static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200467#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200468static void f_test_settime(typval_T *argvars, typval_T *rettv);
469#ifdef FEAT_FLOAT
470static void f_tan(typval_T *argvars, typval_T *rettv);
471static void f_tanh(typval_T *argvars, typval_T *rettv);
472#endif
473#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200474static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200475static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200476static void f_timer_start(typval_T *argvars, typval_T *rettv);
477static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200478static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200479#endif
480static void f_tolower(typval_T *argvars, typval_T *rettv);
481static void f_toupper(typval_T *argvars, typval_T *rettv);
482static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100483static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200484#ifdef FEAT_FLOAT
485static void f_trunc(typval_T *argvars, typval_T *rettv);
486#endif
487static void f_type(typval_T *argvars, typval_T *rettv);
488static void f_undofile(typval_T *argvars, typval_T *rettv);
489static void f_undotree(typval_T *argvars, typval_T *rettv);
490static void f_uniq(typval_T *argvars, typval_T *rettv);
491static void f_values(typval_T *argvars, typval_T *rettv);
492static void f_virtcol(typval_T *argvars, typval_T *rettv);
493static void f_visualmode(typval_T *argvars, typval_T *rettv);
494static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
495static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
496static void f_win_getid(typval_T *argvars, typval_T *rettv);
497static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
498static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
499static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100500static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200501static void f_winbufnr(typval_T *argvars, typval_T *rettv);
502static void f_wincol(typval_T *argvars, typval_T *rettv);
503static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200504static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505static void f_winline(typval_T *argvars, typval_T *rettv);
506static void f_winnr(typval_T *argvars, typval_T *rettv);
507static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
508static void f_winrestview(typval_T *argvars, typval_T *rettv);
509static void f_winsaveview(typval_T *argvars, typval_T *rettv);
510static void f_winwidth(typval_T *argvars, typval_T *rettv);
511static void f_writefile(typval_T *argvars, typval_T *rettv);
512static void f_wordcount(typval_T *argvars, typval_T *rettv);
513static void f_xor(typval_T *argvars, typval_T *rettv);
514
515/*
516 * Array with names and number of arguments of all internal functions
517 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
518 */
519static struct fst
520{
521 char *f_name; /* function name */
522 char f_min_argc; /* minimal number of arguments */
523 char f_max_argc; /* maximal number of arguments */
524 void (*f_func)(typval_T *args, typval_T *rvar);
525 /* implementation of function */
526} functions[] =
527{
528#ifdef FEAT_FLOAT
529 {"abs", 1, 1, f_abs},
530 {"acos", 1, 1, f_acos}, /* WJMc */
531#endif
532 {"add", 2, 2, f_add},
533 {"and", 2, 2, f_and},
534 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200535 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200536 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200537 {"argidx", 0, 0, f_argidx},
538 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200539 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200540#ifdef FEAT_FLOAT
541 {"asin", 1, 1, f_asin}, /* WJMc */
542#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100543 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200544 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100545 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200546 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200547 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100549 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"assert_match", 2, 3, f_assert_match},
551 {"assert_notequal", 2, 3, f_assert_notequal},
552 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100553 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200554 {"assert_true", 1, 2, f_assert_true},
555#ifdef FEAT_FLOAT
556 {"atan", 1, 1, f_atan},
557 {"atan2", 2, 2, f_atan2},
558#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100559#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200560 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100561 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100562# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100563 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100564# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100565#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200566 {"browse", 4, 4, f_browse},
567 {"browsedir", 2, 2, f_browsedir},
568 {"bufexists", 1, 1, f_bufexists},
569 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
570 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
571 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
572 {"buflisted", 1, 1, f_buflisted},
573 {"bufloaded", 1, 1, f_bufloaded},
574 {"bufname", 1, 1, f_bufname},
575 {"bufnr", 1, 2, f_bufnr},
576 {"bufwinid", 1, 1, f_bufwinid},
577 {"bufwinnr", 1, 1, f_bufwinnr},
578 {"byte2line", 1, 1, f_byte2line},
579 {"byteidx", 2, 2, f_byteidx},
580 {"byteidxcomp", 2, 2, f_byteidxcomp},
581 {"call", 2, 3, f_call},
582#ifdef FEAT_FLOAT
583 {"ceil", 1, 1, f_ceil},
584#endif
585#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100586 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200587 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200588 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
590 {"ch_evalraw", 2, 3, f_ch_evalraw},
591 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
592 {"ch_getjob", 1, 1, f_ch_getjob},
593 {"ch_info", 1, 1, f_ch_info},
594 {"ch_log", 1, 2, f_ch_log},
595 {"ch_logfile", 1, 2, f_ch_logfile},
596 {"ch_open", 1, 2, f_ch_open},
597 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100598 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200599 {"ch_readraw", 1, 2, f_ch_readraw},
600 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
601 {"ch_sendraw", 2, 3, f_ch_sendraw},
602 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200603 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200604#endif
605 {"changenr", 0, 0, f_changenr},
606 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200607 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100609 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610 {"col", 1, 1, f_col},
611#if defined(FEAT_INS_EXPAND)
612 {"complete", 2, 2, f_complete},
613 {"complete_add", 1, 1, f_complete_add},
614 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100615 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200616#endif
617 {"confirm", 1, 4, f_confirm},
618 {"copy", 1, 1, f_copy},
619#ifdef FEAT_FLOAT
620 {"cos", 1, 1, f_cos},
621 {"cosh", 1, 1, f_cosh},
622#endif
623 {"count", 2, 4, f_count},
624 {"cscope_connection",0,3, f_cscope_connection},
625 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100626#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200627 {"debugbreak", 1, 1, f_debugbreak},
628#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200629 {"deepcopy", 1, 2, f_deepcopy},
630 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200631 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"did_filetype", 0, 0, f_did_filetype},
633 {"diff_filler", 1, 1, f_diff_filler},
634 {"diff_hlID", 2, 2, f_diff_hlID},
635 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200636 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200637 {"escape", 2, 2, f_escape},
638 {"eval", 1, 1, f_eval},
639 {"eventhandler", 0, 0, f_eventhandler},
640 {"executable", 1, 1, f_executable},
641 {"execute", 1, 2, f_execute},
642 {"exepath", 1, 1, f_exepath},
643 {"exists", 1, 1, f_exists},
644#ifdef FEAT_FLOAT
645 {"exp", 1, 1, f_exp},
646#endif
647 {"expand", 1, 3, f_expand},
648 {"extend", 2, 3, f_extend},
649 {"feedkeys", 1, 2, f_feedkeys},
650 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
651 {"filereadable", 1, 1, f_filereadable},
652 {"filewritable", 1, 1, f_filewritable},
653 {"filter", 2, 2, f_filter},
654 {"finddir", 1, 3, f_finddir},
655 {"findfile", 1, 3, f_findfile},
656#ifdef FEAT_FLOAT
657 {"float2nr", 1, 1, f_float2nr},
658 {"floor", 1, 1, f_floor},
659 {"fmod", 2, 2, f_fmod},
660#endif
661 {"fnameescape", 1, 1, f_fnameescape},
662 {"fnamemodify", 2, 2, f_fnamemodify},
663 {"foldclosed", 1, 1, f_foldclosed},
664 {"foldclosedend", 1, 1, f_foldclosedend},
665 {"foldlevel", 1, 1, f_foldlevel},
666 {"foldtext", 0, 0, f_foldtext},
667 {"foldtextresult", 1, 1, f_foldtextresult},
668 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200669 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200670 {"function", 1, 3, f_function},
671 {"garbagecollect", 0, 1, f_garbagecollect},
672 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200673 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200674 {"getbufline", 2, 3, f_getbufline},
675 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100676 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200677 {"getchar", 0, 1, f_getchar},
678 {"getcharmod", 0, 0, f_getcharmod},
679 {"getcharsearch", 0, 0, f_getcharsearch},
680 {"getcmdline", 0, 0, f_getcmdline},
681 {"getcmdpos", 0, 0, f_getcmdpos},
682 {"getcmdtype", 0, 0, f_getcmdtype},
683 {"getcmdwintype", 0, 0, f_getcmdwintype},
684#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200685 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200686#endif
687 {"getcurpos", 0, 0, f_getcurpos},
688 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200689 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200690 {"getfontname", 0, 1, f_getfontname},
691 {"getfperm", 1, 1, f_getfperm},
692 {"getfsize", 1, 1, f_getfsize},
693 {"getftime", 1, 1, f_getftime},
694 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100695 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200696 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200697 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100698 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200699 {"getpid", 0, 0, f_getpid},
700 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200701 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702 {"getreg", 0, 3, f_getreg},
703 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200704 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200705 {"gettabvar", 2, 3, f_gettabvar},
706 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100707 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200708 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100709 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200710 {"getwinposx", 0, 0, f_getwinposx},
711 {"getwinposy", 0, 0, f_getwinposy},
712 {"getwinvar", 2, 3, f_getwinvar},
713 {"glob", 1, 4, f_glob},
714 {"glob2regpat", 1, 1, f_glob2regpat},
715 {"globpath", 2, 5, f_globpath},
716 {"has", 1, 1, f_has},
717 {"has_key", 2, 2, f_has_key},
718 {"haslocaldir", 0, 2, f_haslocaldir},
719 {"hasmapto", 1, 3, f_hasmapto},
720 {"highlightID", 1, 1, f_hlID}, /* obsolete */
721 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
722 {"histadd", 2, 2, f_histadd},
723 {"histdel", 1, 2, f_histdel},
724 {"histget", 1, 2, f_histget},
725 {"histnr", 1, 1, f_histnr},
726 {"hlID", 1, 1, f_hlID},
727 {"hlexists", 1, 1, f_hlexists},
728 {"hostname", 0, 0, f_hostname},
729 {"iconv", 3, 3, f_iconv},
730 {"indent", 1, 1, f_indent},
731 {"index", 2, 4, f_index},
732 {"input", 1, 3, f_input},
733 {"inputdialog", 1, 3, f_inputdialog},
734 {"inputlist", 1, 1, f_inputlist},
735 {"inputrestore", 0, 0, f_inputrestore},
736 {"inputsave", 0, 0, f_inputsave},
737 {"inputsecret", 1, 2, f_inputsecret},
738 {"insert", 2, 3, f_insert},
739 {"invert", 1, 1, f_invert},
740 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200741#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
742 {"isinf", 1, 1, f_isinf},
743#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200744 {"islocked", 1, 1, f_islocked},
745#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
746 {"isnan", 1, 1, f_isnan},
747#endif
748 {"items", 1, 1, f_items},
749#ifdef FEAT_JOB_CHANNEL
750 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200751 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200752 {"job_setoptions", 2, 2, f_job_setoptions},
753 {"job_start", 1, 2, f_job_start},
754 {"job_status", 1, 1, f_job_status},
755 {"job_stop", 1, 2, f_job_stop},
756#endif
757 {"join", 1, 2, f_join},
758 {"js_decode", 1, 1, f_js_decode},
759 {"js_encode", 1, 1, f_js_encode},
760 {"json_decode", 1, 1, f_json_decode},
761 {"json_encode", 1, 1, f_json_encode},
762 {"keys", 1, 1, f_keys},
763 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
764 {"len", 1, 1, f_len},
765 {"libcall", 3, 3, f_libcall},
766 {"libcallnr", 3, 3, f_libcallnr},
767 {"line", 1, 1, f_line},
768 {"line2byte", 1, 1, f_line2byte},
769 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200770 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200771 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200772 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200773 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200774 {"localtime", 0, 0, f_localtime},
775#ifdef FEAT_FLOAT
776 {"log", 1, 1, f_log},
777 {"log10", 1, 1, f_log10},
778#endif
779#ifdef FEAT_LUA
780 {"luaeval", 1, 2, f_luaeval},
781#endif
782 {"map", 2, 2, f_map},
783 {"maparg", 1, 4, f_maparg},
784 {"mapcheck", 1, 3, f_mapcheck},
785 {"match", 2, 4, f_match},
786 {"matchadd", 2, 5, f_matchadd},
787 {"matchaddpos", 2, 5, f_matchaddpos},
788 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100789 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200790 {"matchend", 2, 4, f_matchend},
791 {"matchlist", 2, 4, f_matchlist},
792 {"matchstr", 2, 4, f_matchstr},
793 {"matchstrpos", 2, 4, f_matchstrpos},
794 {"max", 1, 1, f_max},
795 {"min", 1, 1, f_min},
796#ifdef vim_mkdir
797 {"mkdir", 1, 3, f_mkdir},
798#endif
799 {"mode", 0, 1, f_mode},
800#ifdef FEAT_MZSCHEME
801 {"mzeval", 1, 1, f_mzeval},
802#endif
803 {"nextnonblank", 1, 1, f_nextnonblank},
804 {"nr2char", 1, 2, f_nr2char},
805 {"or", 2, 2, f_or},
806 {"pathshorten", 1, 1, f_pathshorten},
807#ifdef FEAT_PERL
808 {"perleval", 1, 1, f_perleval},
809#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200810#ifdef FEAT_TEXT_PROP
811 {"popup_close", 1, 1, f_popup_close},
812 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200813 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200814 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200815 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200816#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200817#ifdef FEAT_FLOAT
818 {"pow", 2, 2, f_pow},
819#endif
820 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100821 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200822#ifdef FEAT_JOB_CHANNEL
823 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200824 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200825 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
826#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100827#ifdef FEAT_TEXT_PROP
828 {"prop_add", 3, 3, f_prop_add},
829 {"prop_clear", 1, 3, f_prop_clear},
830 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100831 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100832 {"prop_type_add", 2, 2, f_prop_type_add},
833 {"prop_type_change", 2, 2, f_prop_type_change},
834 {"prop_type_delete", 1, 2, f_prop_type_delete},
835 {"prop_type_get", 1, 2, f_prop_type_get},
836 {"prop_type_list", 0, 1, f_prop_type_list},
837#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200838 {"pumvisible", 0, 0, f_pumvisible},
839#ifdef FEAT_PYTHON3
840 {"py3eval", 1, 1, f_py3eval},
841#endif
842#ifdef FEAT_PYTHON
843 {"pyeval", 1, 1, f_pyeval},
844#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100845#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
846 {"pyxeval", 1, 1, f_pyxeval},
847#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200848 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200849 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200850 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200851 {"reg_executing", 0, 0, f_reg_executing},
852 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200853 {"reltime", 0, 2, f_reltime},
854#ifdef FEAT_FLOAT
855 {"reltimefloat", 1, 1, f_reltimefloat},
856#endif
857 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100858 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200859 {"remote_foreground", 1, 1, f_remote_foreground},
860 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100861 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200862 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100863 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200864 {"remove", 2, 3, f_remove},
865 {"rename", 2, 2, f_rename},
866 {"repeat", 2, 2, f_repeat},
867 {"resolve", 1, 1, f_resolve},
868 {"reverse", 1, 1, f_reverse},
869#ifdef FEAT_FLOAT
870 {"round", 1, 1, f_round},
871#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100872#ifdef FEAT_RUBY
873 {"rubyeval", 1, 1, f_rubyeval},
874#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200875 {"screenattr", 2, 2, f_screenattr},
876 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100877 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200878 {"screencol", 0, 0, f_screencol},
879 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100880 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200881 {"search", 1, 4, f_search},
882 {"searchdecl", 1, 3, f_searchdecl},
883 {"searchpair", 3, 7, f_searchpair},
884 {"searchpairpos", 3, 7, f_searchpairpos},
885 {"searchpos", 1, 4, f_searchpos},
886 {"server2client", 2, 2, f_server2client},
887 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200888 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200889 {"setbufvar", 3, 3, f_setbufvar},
890 {"setcharsearch", 1, 1, f_setcharsearch},
891 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200892 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200893 {"setfperm", 2, 2, f_setfperm},
894 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200895 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100896 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200897 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200898 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200899 {"setreg", 2, 3, f_setreg},
900 {"settabvar", 3, 3, f_settabvar},
901 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100902 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200903 {"setwinvar", 3, 3, f_setwinvar},
904#ifdef FEAT_CRYPT
905 {"sha256", 1, 1, f_sha256},
906#endif
907 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100908 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100909#ifdef FEAT_SIGNS
910 {"sign_define", 1, 2, f_sign_define},
911 {"sign_getdefined", 0, 1, f_sign_getdefined},
912 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100913 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100914 {"sign_place", 4, 5, f_sign_place},
915 {"sign_undefine", 0, 1, f_sign_undefine},
916 {"sign_unplace", 1, 2, f_sign_unplace},
917#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200918 {"simplify", 1, 1, f_simplify},
919#ifdef FEAT_FLOAT
920 {"sin", 1, 1, f_sin},
921 {"sinh", 1, 1, f_sinh},
922#endif
923 {"sort", 1, 3, f_sort},
924 {"soundfold", 1, 1, f_soundfold},
925 {"spellbadword", 0, 1, f_spellbadword},
926 {"spellsuggest", 1, 3, f_spellsuggest},
927 {"split", 1, 3, f_split},
928#ifdef FEAT_FLOAT
929 {"sqrt", 1, 1, f_sqrt},
930 {"str2float", 1, 1, f_str2float},
931#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200932 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200933 {"str2nr", 1, 2, f_str2nr},
934 {"strcharpart", 2, 3, f_strcharpart},
935 {"strchars", 1, 2, f_strchars},
936 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
937#ifdef HAVE_STRFTIME
938 {"strftime", 1, 2, f_strftime},
939#endif
940 {"strgetchar", 2, 2, f_strgetchar},
941 {"stridx", 2, 3, f_stridx},
942 {"string", 1, 1, f_string},
943 {"strlen", 1, 1, f_strlen},
944 {"strpart", 2, 3, f_strpart},
945 {"strridx", 2, 3, f_strridx},
946 {"strtrans", 1, 1, f_strtrans},
947 {"strwidth", 1, 1, f_strwidth},
948 {"submatch", 1, 2, f_submatch},
949 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200950 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200951 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200952 {"synID", 3, 3, f_synID},
953 {"synIDattr", 2, 3, f_synIDattr},
954 {"synIDtrans", 1, 1, f_synIDtrans},
955 {"synconcealed", 2, 2, f_synconcealed},
956 {"synstack", 2, 2, f_synstack},
957 {"system", 1, 2, f_system},
958 {"systemlist", 1, 2, f_systemlist},
959 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
960 {"tabpagenr", 0, 1, f_tabpagenr},
961 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
962 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100963 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200964#ifdef FEAT_FLOAT
965 {"tan", 1, 1, f_tan},
966 {"tanh", 1, 1, f_tanh},
967#endif
968 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200969#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100970 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
971 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100972 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200973 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200974# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
975 {"term_getansicolors", 1, 1, f_term_getansicolors},
976# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200977 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200978 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200979 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200980 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200981 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200982 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200983 {"term_getstatus", 1, 1, f_term_getstatus},
984 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200985 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200986 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200987 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200988 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200989# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
990 {"term_setansicolors", 2, 2, f_term_setansicolors},
991# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100992 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100993 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200994 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200995 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200996 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200997#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200998 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
999 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +02001000 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001001 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +02001002 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +01001003 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001004 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001005#ifdef FEAT_JOB_CHANNEL
1006 {"test_null_channel", 0, 0, f_test_null_channel},
1007#endif
1008 {"test_null_dict", 0, 0, f_test_null_dict},
1009#ifdef FEAT_JOB_CHANNEL
1010 {"test_null_job", 0, 0, f_test_null_job},
1011#endif
1012 {"test_null_list", 0, 0, f_test_null_list},
1013 {"test_null_partial", 0, 0, f_test_null_partial},
1014 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001015 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001016 {"test_override", 2, 2, f_test_override},
1017 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001018#ifdef FEAT_GUI
1019 {"test_scrollbar", 3, 3, f_test_scrollbar},
1020#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001021#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001022 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001023#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001024 {"test_settime", 1, 1, f_test_settime},
1025#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001026 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001027 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001028 {"timer_start", 2, 3, f_timer_start},
1029 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001030 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001031#endif
1032 {"tolower", 1, 1, f_tolower},
1033 {"toupper", 1, 1, f_toupper},
1034 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001035 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001036#ifdef FEAT_FLOAT
1037 {"trunc", 1, 1, f_trunc},
1038#endif
1039 {"type", 1, 1, f_type},
1040 {"undofile", 1, 1, f_undofile},
1041 {"undotree", 0, 0, f_undotree},
1042 {"uniq", 1, 3, f_uniq},
1043 {"values", 1, 1, f_values},
1044 {"virtcol", 1, 1, f_virtcol},
1045 {"visualmode", 0, 1, f_visualmode},
1046 {"wildmenumode", 0, 0, f_wildmenumode},
1047 {"win_findbuf", 1, 1, f_win_findbuf},
1048 {"win_getid", 0, 2, f_win_getid},
1049 {"win_gotoid", 1, 1, f_win_gotoid},
1050 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1051 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001052 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001053 {"winbufnr", 1, 1, f_winbufnr},
1054 {"wincol", 0, 0, f_wincol},
1055 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001056 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001057 {"winline", 0, 0, f_winline},
1058 {"winnr", 0, 1, f_winnr},
1059 {"winrestcmd", 0, 0, f_winrestcmd},
1060 {"winrestview", 1, 1, f_winrestview},
1061 {"winsaveview", 0, 0, f_winsaveview},
1062 {"winwidth", 1, 1, f_winwidth},
1063 {"wordcount", 0, 0, f_wordcount},
1064 {"writefile", 2, 3, f_writefile},
1065 {"xor", 2, 2, f_xor},
1066};
1067
1068#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1069
1070/*
1071 * Function given to ExpandGeneric() to obtain the list of internal
1072 * or user defined function names.
1073 */
1074 char_u *
1075get_function_name(expand_T *xp, int idx)
1076{
1077 static int intidx = -1;
1078 char_u *name;
1079
1080 if (idx == 0)
1081 intidx = -1;
1082 if (intidx < 0)
1083 {
1084 name = get_user_func_name(xp, idx);
1085 if (name != NULL)
1086 return name;
1087 }
1088 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1089 {
1090 STRCPY(IObuff, functions[intidx].f_name);
1091 STRCAT(IObuff, "(");
1092 if (functions[intidx].f_max_argc == 0)
1093 STRCAT(IObuff, ")");
1094 return IObuff;
1095 }
1096
1097 return NULL;
1098}
1099
1100/*
1101 * Function given to ExpandGeneric() to obtain the list of internal or
1102 * user defined variable or function names.
1103 */
1104 char_u *
1105get_expr_name(expand_T *xp, int idx)
1106{
1107 static int intidx = -1;
1108 char_u *name;
1109
1110 if (idx == 0)
1111 intidx = -1;
1112 if (intidx < 0)
1113 {
1114 name = get_function_name(xp, idx);
1115 if (name != NULL)
1116 return name;
1117 }
1118 return get_user_var_name(xp, ++intidx);
1119}
1120
1121#endif /* FEAT_CMDL_COMPL */
1122
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001123/*
1124 * Find internal function in table above.
1125 * Return index, or -1 if not found
1126 */
1127 int
1128find_internal_func(
1129 char_u *name) /* name of the function */
1130{
1131 int first = 0;
1132 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1133 int cmp;
1134 int x;
1135
1136 /*
1137 * Find the function name in the table. Binary search.
1138 */
1139 while (first <= last)
1140 {
1141 x = first + ((unsigned)(last - first) >> 1);
1142 cmp = STRCMP(name, functions[x].f_name);
1143 if (cmp < 0)
1144 last = x - 1;
1145 else if (cmp > 0)
1146 first = x + 1;
1147 else
1148 return x;
1149 }
1150 return -1;
1151}
1152
1153 int
1154call_internal_func(
1155 char_u *name,
1156 int argcount,
1157 typval_T *argvars,
1158 typval_T *rettv)
1159{
1160 int i;
1161
1162 i = find_internal_func(name);
1163 if (i < 0)
1164 return ERROR_UNKNOWN;
1165 if (argcount < functions[i].f_min_argc)
1166 return ERROR_TOOFEW;
1167 if (argcount > functions[i].f_max_argc)
1168 return ERROR_TOOMANY;
1169 argvars[argcount].v_type = VAR_UNKNOWN;
1170 functions[i].f_func(argvars, rettv);
1171 return ERROR_NONE;
1172}
1173
1174/*
1175 * Return TRUE for a non-zero Number and a non-empty String.
1176 */
1177 static int
1178non_zero_arg(typval_T *argvars)
1179{
1180 return ((argvars[0].v_type == VAR_NUMBER
1181 && argvars[0].vval.v_number != 0)
1182 || (argvars[0].v_type == VAR_SPECIAL
1183 && argvars[0].vval.v_number == VVAL_TRUE)
1184 || (argvars[0].v_type == VAR_STRING
1185 && argvars[0].vval.v_string != NULL
1186 && *argvars[0].vval.v_string != NUL));
1187}
1188
1189/*
1190 * Get the lnum from the first argument.
1191 * Also accepts ".", "$", etc., but that only works for the current buffer.
1192 * Returns -1 on error.
1193 */
1194 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001195tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001196{
1197 typval_T rettv;
1198 linenr_T lnum;
1199
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001200 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001201 if (lnum == 0) /* no valid number, try using line() */
1202 {
1203 rettv.v_type = VAR_NUMBER;
1204 f_line(argvars, &rettv);
1205 lnum = (linenr_T)rettv.vval.v_number;
1206 clear_tv(&rettv);
1207 }
1208 return lnum;
1209}
1210
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001211/*
1212 * Get the lnum from the first argument.
1213 * Also accepts "$", then "buf" is used.
1214 * Returns 0 on error.
1215 */
1216 static linenr_T
1217tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1218{
1219 if (argvars[0].v_type == VAR_STRING
1220 && argvars[0].vval.v_string != NULL
1221 && argvars[0].vval.v_string[0] == '$'
1222 && buf != NULL)
1223 return buf->b_ml.ml_line_count;
1224 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1225}
1226
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001227#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001228/*
1229 * Get the float value of "argvars[0]" into "f".
1230 * Returns FAIL when the argument is not a Number or Float.
1231 */
1232 static int
1233get_float_arg(typval_T *argvars, float_T *f)
1234{
1235 if (argvars[0].v_type == VAR_FLOAT)
1236 {
1237 *f = argvars[0].vval.v_float;
1238 return OK;
1239 }
1240 if (argvars[0].v_type == VAR_NUMBER)
1241 {
1242 *f = (float_T)argvars[0].vval.v_number;
1243 return OK;
1244 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001245 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001246 return FAIL;
1247}
1248
1249/*
1250 * "abs(expr)" function
1251 */
1252 static void
1253f_abs(typval_T *argvars, typval_T *rettv)
1254{
1255 if (argvars[0].v_type == VAR_FLOAT)
1256 {
1257 rettv->v_type = VAR_FLOAT;
1258 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1259 }
1260 else
1261 {
1262 varnumber_T n;
1263 int error = FALSE;
1264
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001265 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001266 if (error)
1267 rettv->vval.v_number = -1;
1268 else if (n > 0)
1269 rettv->vval.v_number = n;
1270 else
1271 rettv->vval.v_number = -n;
1272 }
1273}
1274
1275/*
1276 * "acos()" function
1277 */
1278 static void
1279f_acos(typval_T *argvars, typval_T *rettv)
1280{
1281 float_T f = 0.0;
1282
1283 rettv->v_type = VAR_FLOAT;
1284 if (get_float_arg(argvars, &f) == OK)
1285 rettv->vval.v_float = acos(f);
1286 else
1287 rettv->vval.v_float = 0.0;
1288}
1289#endif
1290
1291/*
1292 * "add(list, item)" function
1293 */
1294 static void
1295f_add(typval_T *argvars, typval_T *rettv)
1296{
1297 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001298 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001299
1300 rettv->vval.v_number = 1; /* Default: Failed */
1301 if (argvars[0].v_type == VAR_LIST)
1302 {
1303 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001304 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001305 (char_u *)N_("add() argument"), TRUE)
1306 && list_append_tv(l, &argvars[1]) == OK)
1307 copy_tv(&argvars[0], rettv);
1308 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001309 else if (argvars[0].v_type == VAR_BLOB)
1310 {
1311 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001312 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001313 (char_u *)N_("add() argument"), TRUE))
1314 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001315 int error = FALSE;
1316 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1317
1318 if (!error)
1319 {
1320 ga_append(&b->bv_ga, (int)n);
1321 copy_tv(&argvars[0], rettv);
1322 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001323 }
1324 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001325 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001326 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001327}
1328
1329/*
1330 * "and(expr, expr)" function
1331 */
1332 static void
1333f_and(typval_T *argvars, typval_T *rettv)
1334{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001335 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1336 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001337}
1338
1339/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001340 * If there is a window for "curbuf", make it the current window.
1341 */
1342 static void
1343find_win_for_curbuf(void)
1344{
1345 wininfo_T *wip;
1346
1347 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1348 {
1349 if (wip->wi_win != NULL)
1350 {
1351 curwin = wip->wi_win;
1352 break;
1353 }
1354 }
1355}
1356
1357/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001358 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001359 */
1360 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001361set_buffer_lines(
1362 buf_T *buf,
1363 linenr_T lnum_arg,
1364 int append,
1365 typval_T *lines,
1366 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001367{
Bram Moolenaarca851592018-06-06 21:04:07 +02001368 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1369 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001370 list_T *l = NULL;
1371 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001373 linenr_T append_lnum;
1374 buf_T *curbuf_save = NULL;
1375 win_T *curwin_save = NULL;
1376 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001377
Bram Moolenaarca851592018-06-06 21:04:07 +02001378 /* When using the current buffer ml_mfp will be set if needed. Useful when
1379 * setline() is used on startup. For other buffers the buffer must be
1380 * loaded. */
1381 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001382 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001383 rettv->vval.v_number = 1; /* FAIL */
1384 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001385 }
1386
Bram Moolenaarca851592018-06-06 21:04:07 +02001387 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001388 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001389 curbuf_save = curbuf;
1390 curwin_save = curwin;
1391 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001392 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001393 }
1394
1395 if (append)
1396 // appendbufline() uses the line number below which we insert
1397 append_lnum = lnum - 1;
1398 else
1399 // setbufline() uses the line number above which we insert, we only
1400 // append if it's below the last line
1401 append_lnum = curbuf->b_ml.ml_line_count;
1402
1403 if (lines->v_type == VAR_LIST)
1404 {
1405 l = lines->vval.v_list;
1406 li = l->lv_first;
1407 }
1408 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001409 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001410
1411 /* default result is zero == OK */
1412 for (;;)
1413 {
1414 if (l != NULL)
1415 {
1416 /* list argument, get next string */
1417 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001418 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001419 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001420 li = li->li_next;
1421 }
1422
Bram Moolenaarca851592018-06-06 21:04:07 +02001423 rettv->vval.v_number = 1; /* FAIL */
1424 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1425 break;
1426
1427 /* When coming here from Insert mode, sync undo, so that this can be
1428 * undone separately from what was previously inserted. */
1429 if (u_sync_once == 2)
1430 {
1431 u_sync_once = 1; /* notify that u_sync() was called */
1432 u_sync(TRUE);
1433 }
1434
1435 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1436 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001437 // Existing line, replace it.
1438 // Removes any existing text properties.
1439 if (u_savesub(lnum) == OK && ml_replace_len(
1440 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001441 {
1442 changed_bytes(lnum, 0);
1443 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1444 check_cursor_col();
1445 rettv->vval.v_number = 0; /* OK */
1446 }
1447 }
1448 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1449 {
1450 /* append the line */
1451 ++added;
1452 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1453 rettv->vval.v_number = 0; /* OK */
1454 }
1455
1456 if (l == NULL) /* only one string argument */
1457 break;
1458 ++lnum;
1459 }
1460
1461 if (added > 0)
1462 {
1463 win_T *wp;
1464 tabpage_T *tp;
1465
1466 appended_lines_mark(append_lnum, added);
1467 FOR_ALL_TAB_WINDOWS(tp, wp)
1468 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1469 wp->w_cursor.lnum += added;
1470 check_cursor_col();
1471
Bram Moolenaarf2732452018-06-03 14:47:35 +02001472#ifdef FEAT_JOB_CHANNEL
1473 if (bt_prompt(curbuf) && (State & INSERT))
1474 // show the line with the prompt
1475 update_topline();
1476#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001477 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001478
1479 if (!is_curbuf)
1480 {
1481 curbuf = curbuf_save;
1482 curwin = curwin_save;
1483 }
1484}
1485
1486/*
1487 * "append(lnum, string/list)" function
1488 */
1489 static void
1490f_append(typval_T *argvars, typval_T *rettv)
1491{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001492 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001493
1494 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1495}
1496
1497/*
1498 * "appendbufline(buf, lnum, string/list)" function
1499 */
1500 static void
1501f_appendbufline(typval_T *argvars, typval_T *rettv)
1502{
1503 linenr_T lnum;
1504 buf_T *buf;
1505
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001506 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001507 if (buf == NULL)
1508 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001509 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001510 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001511 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001512 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1513 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001514}
1515
1516/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001517 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001518 */
1519 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001520f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001521{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001522 win_T *wp;
1523
1524 if (argvars[0].v_type == VAR_UNKNOWN)
1525 // use the current window
1526 rettv->vval.v_number = ARGCOUNT;
1527 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001528 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001529 // use the global argument list
1530 rettv->vval.v_number = GARGCOUNT;
1531 else
1532 {
1533 // use the argument list of the specified window
1534 wp = find_win_by_nr_or_id(&argvars[0]);
1535 if (wp != NULL)
1536 rettv->vval.v_number = WARGCOUNT(wp);
1537 else
1538 rettv->vval.v_number = -1;
1539 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001540}
1541
1542/*
1543 * "argidx()" function
1544 */
1545 static void
1546f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1547{
1548 rettv->vval.v_number = curwin->w_arg_idx;
1549}
1550
1551/*
1552 * "arglistid()" function
1553 */
1554 static void
1555f_arglistid(typval_T *argvars, typval_T *rettv)
1556{
1557 win_T *wp;
1558
1559 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001560 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561 if (wp != NULL)
1562 rettv->vval.v_number = wp->w_alist->id;
1563}
1564
1565/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001566 * Get the argument list for a given window
1567 */
1568 static void
1569get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1570{
1571 int idx;
1572
1573 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1574 for (idx = 0; idx < argcount; ++idx)
1575 list_append_string(rettv->vval.v_list,
1576 alist_name(&arglist[idx]), -1);
1577}
1578
1579/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001580 * "argv(nr)" function
1581 */
1582 static void
1583f_argv(typval_T *argvars, typval_T *rettv)
1584{
1585 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001586 aentry_T *arglist = NULL;
1587 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001588
1589 if (argvars[0].v_type != VAR_UNKNOWN)
1590 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001591 if (argvars[1].v_type == VAR_UNKNOWN)
1592 {
1593 arglist = ARGLIST;
1594 argcount = ARGCOUNT;
1595 }
1596 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001597 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001598 {
1599 arglist = GARGLIST;
1600 argcount = GARGCOUNT;
1601 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001602 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001603 {
1604 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1605
1606 if (wp != NULL)
1607 {
1608 /* Use the argument list of the specified window */
1609 arglist = WARGLIST(wp);
1610 argcount = WARGCOUNT(wp);
1611 }
1612 }
1613
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001614 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001615 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001616 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001617 if (arglist != NULL && idx >= 0 && idx < argcount)
1618 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1619 else if (idx == -1)
1620 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001621 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001622 else
1623 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001624}
1625
1626/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001627 * "assert_beeps(cmd [, error])" function
1628 */
1629 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001630f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001631{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001632 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001633}
1634
1635/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001636 * "assert_equal(expected, actual[, msg])" function
1637 */
1638 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001639f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001640{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001641 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001642}
1643
1644/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001645 * "assert_equalfile(fname-one, fname-two)" function
1646 */
1647 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001648f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001649{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001650 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001651}
1652
1653/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001654 * "assert_notequal(expected, actual[, msg])" function
1655 */
1656 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001657f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001658{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001659 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001660}
1661
1662/*
1663 * "assert_exception(string[, msg])" function
1664 */
1665 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001666f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001667{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001668 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001669}
1670
1671/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001672 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001673 */
1674 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001675f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001676{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001677 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001678}
1679
1680/*
1681 * "assert_false(actual[, msg])" function
1682 */
1683 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001684f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001685{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001686 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001687}
1688
1689/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001690 * "assert_inrange(lower, upper[, msg])" function
1691 */
1692 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001693f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001694{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001695 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001696}
1697
1698/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001699 * "assert_match(pattern, actual[, msg])" function
1700 */
1701 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001702f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001703{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001704 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001705}
1706
1707/*
1708 * "assert_notmatch(pattern, actual[, msg])" function
1709 */
1710 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001711f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001712{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001713 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001714}
1715
1716/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001717 * "assert_report(msg)" function
1718 */
1719 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001720f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001721{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001722 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001723}
1724
1725/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001726 * "assert_true(actual[, msg])" function
1727 */
1728 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001729f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001730{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001731 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001732}
1733
1734#ifdef FEAT_FLOAT
1735/*
1736 * "asin()" function
1737 */
1738 static void
1739f_asin(typval_T *argvars, typval_T *rettv)
1740{
1741 float_T f = 0.0;
1742
1743 rettv->v_type = VAR_FLOAT;
1744 if (get_float_arg(argvars, &f) == OK)
1745 rettv->vval.v_float = asin(f);
1746 else
1747 rettv->vval.v_float = 0.0;
1748}
1749
1750/*
1751 * "atan()" function
1752 */
1753 static void
1754f_atan(typval_T *argvars, typval_T *rettv)
1755{
1756 float_T f = 0.0;
1757
1758 rettv->v_type = VAR_FLOAT;
1759 if (get_float_arg(argvars, &f) == OK)
1760 rettv->vval.v_float = atan(f);
1761 else
1762 rettv->vval.v_float = 0.0;
1763}
1764
1765/*
1766 * "atan2()" function
1767 */
1768 static void
1769f_atan2(typval_T *argvars, typval_T *rettv)
1770{
1771 float_T fx = 0.0, fy = 0.0;
1772
1773 rettv->v_type = VAR_FLOAT;
1774 if (get_float_arg(argvars, &fx) == OK
1775 && get_float_arg(&argvars[1], &fy) == OK)
1776 rettv->vval.v_float = atan2(fx, fy);
1777 else
1778 rettv->vval.v_float = 0.0;
1779}
1780#endif
1781
1782/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001783 * "balloon_show()" function
1784 */
1785#ifdef FEAT_BEVAL
1786 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001787f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1788{
1789 rettv->v_type = VAR_STRING;
1790 if (balloonEval != NULL)
1791 {
1792 if (balloonEval->msg == NULL)
1793 rettv->vval.v_string = NULL;
1794 else
1795 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1796 }
1797}
1798
1799 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001800f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1801{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001802 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001803 {
1804 if (argvars[0].v_type == VAR_LIST
1805# ifdef FEAT_GUI
1806 && !gui.in_use
1807# endif
1808 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001809 {
1810 list_T *l = argvars[0].vval.v_list;
1811
1812 // empty list removes the balloon
1813 post_balloon(balloonEval, NULL,
1814 l == NULL || l->lv_len == 0 ? NULL : l);
1815 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001816 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001817 {
1818 char_u *mesg = tv_get_string_chk(&argvars[0]);
1819
1820 if (mesg != NULL)
1821 // empty string removes the balloon
1822 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1823 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001824 }
1825}
1826
Bram Moolenaar669a8282017-11-19 20:13:05 +01001827# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001828 static void
1829f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1830{
1831 if (rettv_list_alloc(rettv) == OK)
1832 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001833 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001834
1835 if (msg != NULL)
1836 {
1837 pumitem_T *array;
1838 int size = split_message(msg, &array);
1839 int i;
1840
1841 /* Skip the first and last item, they are always empty. */
1842 for (i = 1; i < size - 1; ++i)
1843 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001844 while (size > 0)
1845 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001846 vim_free(array);
1847 }
1848 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001849}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001850# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001851#endif
1852
1853/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001854 * "browse(save, title, initdir, default)" function
1855 */
1856 static void
1857f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1858{
1859#ifdef FEAT_BROWSE
1860 int save;
1861 char_u *title;
1862 char_u *initdir;
1863 char_u *defname;
1864 char_u buf[NUMBUFLEN];
1865 char_u buf2[NUMBUFLEN];
1866 int error = FALSE;
1867
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001868 save = (int)tv_get_number_chk(&argvars[0], &error);
1869 title = tv_get_string_chk(&argvars[1]);
1870 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1871 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001872
1873 if (error || title == NULL || initdir == NULL || defname == NULL)
1874 rettv->vval.v_string = NULL;
1875 else
1876 rettv->vval.v_string =
1877 do_browse(save ? BROWSE_SAVE : 0,
1878 title, defname, NULL, initdir, NULL, curbuf);
1879#else
1880 rettv->vval.v_string = NULL;
1881#endif
1882 rettv->v_type = VAR_STRING;
1883}
1884
1885/*
1886 * "browsedir(title, initdir)" function
1887 */
1888 static void
1889f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1890{
1891#ifdef FEAT_BROWSE
1892 char_u *title;
1893 char_u *initdir;
1894 char_u buf[NUMBUFLEN];
1895
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001896 title = tv_get_string_chk(&argvars[0]);
1897 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001898
1899 if (title == NULL || initdir == NULL)
1900 rettv->vval.v_string = NULL;
1901 else
1902 rettv->vval.v_string = do_browse(BROWSE_DIR,
1903 title, NULL, NULL, initdir, NULL, curbuf);
1904#else
1905 rettv->vval.v_string = NULL;
1906#endif
1907 rettv->v_type = VAR_STRING;
1908}
1909
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001910/*
1911 * Find a buffer by number or exact name.
1912 */
1913 static buf_T *
1914find_buffer(typval_T *avar)
1915{
1916 buf_T *buf = NULL;
1917
1918 if (avar->v_type == VAR_NUMBER)
1919 buf = buflist_findnr((int)avar->vval.v_number);
1920 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1921 {
1922 buf = buflist_findname_exp(avar->vval.v_string);
1923 if (buf == NULL)
1924 {
1925 /* No full path name match, try a match with a URL or a "nofile"
1926 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001927 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001928 if (buf->b_fname != NULL
1929 && (path_with_url(buf->b_fname)
1930#ifdef FEAT_QUICKFIX
1931 || bt_nofile(buf)
1932#endif
1933 )
1934 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1935 break;
1936 }
1937 }
1938 return buf;
1939}
1940
1941/*
1942 * "bufexists(expr)" function
1943 */
1944 static void
1945f_bufexists(typval_T *argvars, typval_T *rettv)
1946{
1947 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1948}
1949
1950/*
1951 * "buflisted(expr)" function
1952 */
1953 static void
1954f_buflisted(typval_T *argvars, typval_T *rettv)
1955{
1956 buf_T *buf;
1957
1958 buf = find_buffer(&argvars[0]);
1959 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1960}
1961
1962/*
1963 * "bufloaded(expr)" function
1964 */
1965 static void
1966f_bufloaded(typval_T *argvars, typval_T *rettv)
1967{
1968 buf_T *buf;
1969
1970 buf = find_buffer(&argvars[0]);
1971 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1972}
1973
1974 buf_T *
1975buflist_find_by_name(char_u *name, int curtab_only)
1976{
1977 int save_magic;
1978 char_u *save_cpo;
1979 buf_T *buf;
1980
1981 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1982 save_magic = p_magic;
1983 p_magic = TRUE;
1984 save_cpo = p_cpo;
1985 p_cpo = (char_u *)"";
1986
1987 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1988 TRUE, FALSE, curtab_only));
1989
1990 p_magic = save_magic;
1991 p_cpo = save_cpo;
1992 return buf;
1993}
1994
1995/*
1996 * Get buffer by number or pattern.
1997 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001998 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001999tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002000{
2001 char_u *name = tv->vval.v_string;
2002 buf_T *buf;
2003
2004 if (tv->v_type == VAR_NUMBER)
2005 return buflist_findnr((int)tv->vval.v_number);
2006 if (tv->v_type != VAR_STRING)
2007 return NULL;
2008 if (name == NULL || *name == NUL)
2009 return curbuf;
2010 if (name[0] == '$' && name[1] == NUL)
2011 return lastbuf;
2012
2013 buf = buflist_find_by_name(name, curtab_only);
2014
2015 /* If not found, try expanding the name, like done for bufexists(). */
2016 if (buf == NULL)
2017 buf = find_buffer(tv);
2018
2019 return buf;
2020}
2021
2022/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002023 * Get the buffer from "arg" and give an error and return NULL if it is not
2024 * valid.
2025 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002026 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002027get_buf_arg(typval_T *arg)
2028{
2029 buf_T *buf;
2030
2031 ++emsg_off;
2032 buf = tv_get_buf(arg, FALSE);
2033 --emsg_off;
2034 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002035 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002036 return buf;
2037}
2038
2039/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002040 * "bufname(expr)" function
2041 */
2042 static void
2043f_bufname(typval_T *argvars, typval_T *rettv)
2044{
2045 buf_T *buf;
2046
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002047 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002048 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002049 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002050 rettv->v_type = VAR_STRING;
2051 if (buf != NULL && buf->b_fname != NULL)
2052 rettv->vval.v_string = vim_strsave(buf->b_fname);
2053 else
2054 rettv->vval.v_string = NULL;
2055 --emsg_off;
2056}
2057
2058/*
2059 * "bufnr(expr)" function
2060 */
2061 static void
2062f_bufnr(typval_T *argvars, typval_T *rettv)
2063{
2064 buf_T *buf;
2065 int error = FALSE;
2066 char_u *name;
2067
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002068 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002069 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002070 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002071 --emsg_off;
2072
2073 /* If the buffer isn't found and the second argument is not zero create a
2074 * new buffer. */
2075 if (buf == NULL
2076 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002077 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002078 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002079 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002080 && !error)
2081 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2082
2083 if (buf != NULL)
2084 rettv->vval.v_number = buf->b_fnum;
2085 else
2086 rettv->vval.v_number = -1;
2087}
2088
2089 static void
2090buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2091{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002092 win_T *wp;
2093 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002094 buf_T *buf;
2095
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002096 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002097 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002098 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002099 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002100 {
2101 ++winnr;
2102 if (wp->w_buffer == buf)
2103 break;
2104 }
2105 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002106 --emsg_off;
2107}
2108
2109/*
2110 * "bufwinid(nr)" function
2111 */
2112 static void
2113f_bufwinid(typval_T *argvars, typval_T *rettv)
2114{
2115 buf_win_common(argvars, rettv, FALSE);
2116}
2117
2118/*
2119 * "bufwinnr(nr)" function
2120 */
2121 static void
2122f_bufwinnr(typval_T *argvars, typval_T *rettv)
2123{
2124 buf_win_common(argvars, rettv, TRUE);
2125}
2126
2127/*
2128 * "byte2line(byte)" function
2129 */
2130 static void
2131f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2132{
2133#ifndef FEAT_BYTEOFF
2134 rettv->vval.v_number = -1;
2135#else
2136 long boff = 0;
2137
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002138 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002139 if (boff < 0)
2140 rettv->vval.v_number = -1;
2141 else
2142 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2143 (linenr_T)0, &boff);
2144#endif
2145}
2146
2147 static void
2148byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2149{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002150 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002151 char_u *str;
2152 varnumber_T idx;
2153
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002154 str = tv_get_string_chk(&argvars[0]);
2155 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002156 rettv->vval.v_number = -1;
2157 if (str == NULL || idx < 0)
2158 return;
2159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002160 t = str;
2161 for ( ; idx > 0; idx--)
2162 {
2163 if (*t == NUL) /* EOL reached */
2164 return;
2165 if (enc_utf8 && comp)
2166 t += utf_ptr2len(t);
2167 else
2168 t += (*mb_ptr2len)(t);
2169 }
2170 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002171}
2172
2173/*
2174 * "byteidx()" function
2175 */
2176 static void
2177f_byteidx(typval_T *argvars, typval_T *rettv)
2178{
2179 byteidx(argvars, rettv, FALSE);
2180}
2181
2182/*
2183 * "byteidxcomp()" function
2184 */
2185 static void
2186f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2187{
2188 byteidx(argvars, rettv, TRUE);
2189}
2190
2191/*
2192 * "call(func, arglist [, dict])" function
2193 */
2194 static void
2195f_call(typval_T *argvars, typval_T *rettv)
2196{
2197 char_u *func;
2198 partial_T *partial = NULL;
2199 dict_T *selfdict = NULL;
2200
2201 if (argvars[1].v_type != VAR_LIST)
2202 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002203 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002204 return;
2205 }
2206 if (argvars[1].vval.v_list == NULL)
2207 return;
2208
2209 if (argvars[0].v_type == VAR_FUNC)
2210 func = argvars[0].vval.v_string;
2211 else if (argvars[0].v_type == VAR_PARTIAL)
2212 {
2213 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002214 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002215 }
2216 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002217 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002218 if (*func == NUL)
2219 return; /* type error or empty name */
2220
2221 if (argvars[2].v_type != VAR_UNKNOWN)
2222 {
2223 if (argvars[2].v_type != VAR_DICT)
2224 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002225 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002226 return;
2227 }
2228 selfdict = argvars[2].vval.v_dict;
2229 }
2230
2231 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2232}
2233
2234#ifdef FEAT_FLOAT
2235/*
2236 * "ceil({float})" function
2237 */
2238 static void
2239f_ceil(typval_T *argvars, typval_T *rettv)
2240{
2241 float_T f = 0.0;
2242
2243 rettv->v_type = VAR_FLOAT;
2244 if (get_float_arg(argvars, &f) == OK)
2245 rettv->vval.v_float = ceil(f);
2246 else
2247 rettv->vval.v_float = 0.0;
2248}
2249#endif
2250
2251#ifdef FEAT_JOB_CHANNEL
2252/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002253 * "ch_canread()" function
2254 */
2255 static void
2256f_ch_canread(typval_T *argvars, typval_T *rettv)
2257{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002258 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002259
2260 rettv->vval.v_number = 0;
2261 if (channel != NULL)
2262 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2263 || channel_has_readahead(channel, PART_OUT)
2264 || channel_has_readahead(channel, PART_ERR);
2265}
2266
2267/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002268 * "ch_close()" function
2269 */
2270 static void
2271f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2272{
2273 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2274
2275 if (channel != NULL)
2276 {
2277 channel_close(channel, FALSE);
2278 channel_clear(channel);
2279 }
2280}
2281
2282/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002283 * "ch_close()" function
2284 */
2285 static void
2286f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2287{
2288 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2289
2290 if (channel != NULL)
2291 channel_close_in(channel);
2292}
2293
2294/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002295 * "ch_getbufnr()" function
2296 */
2297 static void
2298f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2299{
2300 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2301
2302 rettv->vval.v_number = -1;
2303 if (channel != NULL)
2304 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002305 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002306 int part;
2307
2308 if (STRCMP(what, "err") == 0)
2309 part = PART_ERR;
2310 else if (STRCMP(what, "out") == 0)
2311 part = PART_OUT;
2312 else if (STRCMP(what, "in") == 0)
2313 part = PART_IN;
2314 else
2315 part = PART_SOCK;
2316 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2317 rettv->vval.v_number =
2318 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2319 }
2320}
2321
2322/*
2323 * "ch_getjob()" function
2324 */
2325 static void
2326f_ch_getjob(typval_T *argvars, typval_T *rettv)
2327{
2328 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2329
2330 if (channel != NULL)
2331 {
2332 rettv->v_type = VAR_JOB;
2333 rettv->vval.v_job = channel->ch_job;
2334 if (channel->ch_job != NULL)
2335 ++channel->ch_job->jv_refcount;
2336 }
2337}
2338
2339/*
2340 * "ch_info()" function
2341 */
2342 static void
2343f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2344{
2345 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2346
2347 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2348 channel_info(channel, rettv->vval.v_dict);
2349}
2350
2351/*
2352 * "ch_log()" function
2353 */
2354 static void
2355f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2356{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002357 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002358 channel_T *channel = NULL;
2359
2360 if (argvars[1].v_type != VAR_UNKNOWN)
2361 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2362
Bram Moolenaard5359b22018-04-05 22:44:39 +02002363 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002364}
2365
2366/*
2367 * "ch_logfile()" function
2368 */
2369 static void
2370f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2371{
2372 char_u *fname;
2373 char_u *opt = (char_u *)"";
2374 char_u buf[NUMBUFLEN];
2375
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002376 /* Don't open a file in restricted mode. */
2377 if (check_restricted() || check_secure())
2378 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002379 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002380 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002381 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002382 ch_logfile(fname, opt);
2383}
2384
2385/*
2386 * "ch_open()" function
2387 */
2388 static void
2389f_ch_open(typval_T *argvars, typval_T *rettv)
2390{
2391 rettv->v_type = VAR_CHANNEL;
2392 if (check_restricted() || check_secure())
2393 return;
2394 rettv->vval.v_channel = channel_open_func(argvars);
2395}
2396
2397/*
2398 * "ch_read()" function
2399 */
2400 static void
2401f_ch_read(typval_T *argvars, typval_T *rettv)
2402{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002403 common_channel_read(argvars, rettv, FALSE, FALSE);
2404}
2405
2406/*
2407 * "ch_readblob()" function
2408 */
2409 static void
2410f_ch_readblob(typval_T *argvars, typval_T *rettv)
2411{
2412 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002413}
2414
2415/*
2416 * "ch_readraw()" function
2417 */
2418 static void
2419f_ch_readraw(typval_T *argvars, typval_T *rettv)
2420{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002421 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002422}
2423
2424/*
2425 * "ch_evalexpr()" function
2426 */
2427 static void
2428f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2429{
2430 ch_expr_common(argvars, rettv, TRUE);
2431}
2432
2433/*
2434 * "ch_sendexpr()" function
2435 */
2436 static void
2437f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2438{
2439 ch_expr_common(argvars, rettv, FALSE);
2440}
2441
2442/*
2443 * "ch_evalraw()" function
2444 */
2445 static void
2446f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2447{
2448 ch_raw_common(argvars, rettv, TRUE);
2449}
2450
2451/*
2452 * "ch_sendraw()" function
2453 */
2454 static void
2455f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2456{
2457 ch_raw_common(argvars, rettv, FALSE);
2458}
2459
2460/*
2461 * "ch_setoptions()" function
2462 */
2463 static void
2464f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2465{
2466 channel_T *channel;
2467 jobopt_T opt;
2468
2469 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2470 if (channel == NULL)
2471 return;
2472 clear_job_options(&opt);
2473 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002474 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002475 channel_set_options(channel, &opt);
2476 free_job_options(&opt);
2477}
2478
2479/*
2480 * "ch_status()" function
2481 */
2482 static void
2483f_ch_status(typval_T *argvars, typval_T *rettv)
2484{
2485 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002486 jobopt_T opt;
2487 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002488
2489 /* return an empty string by default */
2490 rettv->v_type = VAR_STRING;
2491 rettv->vval.v_string = NULL;
2492
2493 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002494
2495 if (argvars[1].v_type != VAR_UNKNOWN)
2496 {
2497 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002498 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002499 && (opt.jo_set & JO_PART))
2500 part = opt.jo_part;
2501 }
2502
2503 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002504}
2505#endif
2506
2507/*
2508 * "changenr()" function
2509 */
2510 static void
2511f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2512{
2513 rettv->vval.v_number = curbuf->b_u_seq_cur;
2514}
2515
2516/*
2517 * "char2nr(string)" function
2518 */
2519 static void
2520f_char2nr(typval_T *argvars, typval_T *rettv)
2521{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002522 if (has_mbyte)
2523 {
2524 int utf8 = 0;
2525
2526 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002527 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002528
2529 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002530 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002531 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002532 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002533 }
2534 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002535 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002536}
2537
2538/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002539 * "chdir(dir)" function
2540 */
2541 static void
2542f_chdir(typval_T *argvars, typval_T *rettv)
2543{
2544 char_u *cwd;
2545 cdscope_T scope = CDSCOPE_GLOBAL;
2546
2547 rettv->v_type = VAR_STRING;
2548 rettv->vval.v_string = NULL;
2549
2550 if (argvars[0].v_type != VAR_STRING)
2551 return;
2552
2553 // Return the current directory
2554 cwd = alloc(MAXPATHL);
2555 if (cwd != NULL)
2556 {
2557 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2558 {
2559#ifdef BACKSLASH_IN_FILENAME
2560 slash_adjust(cwd);
2561#endif
2562 rettv->vval.v_string = vim_strsave(cwd);
2563 }
2564 vim_free(cwd);
2565 }
2566
2567 if (curwin->w_localdir != NULL)
2568 scope = CDSCOPE_WINDOW;
2569 else if (curtab->tp_localdir != NULL)
2570 scope = CDSCOPE_TABPAGE;
2571
2572 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2573 // Directory change failed
2574 VIM_CLEAR(rettv->vval.v_string);
2575}
2576
2577/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002578 * "cindent(lnum)" function
2579 */
2580 static void
2581f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2582{
2583#ifdef FEAT_CINDENT
2584 pos_T pos;
2585 linenr_T lnum;
2586
2587 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002588 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002589 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2590 {
2591 curwin->w_cursor.lnum = lnum;
2592 rettv->vval.v_number = get_c_indent();
2593 curwin->w_cursor = pos;
2594 }
2595 else
2596#endif
2597 rettv->vval.v_number = -1;
2598}
2599
Bram Moolenaaraff74912019-03-30 18:11:49 +01002600 static win_T *
2601get_optional_window(typval_T *argvars, int idx)
2602{
2603 win_T *win = curwin;
2604
2605 if (argvars[idx].v_type != VAR_UNKNOWN)
2606 {
2607 win = find_win_by_nr_or_id(&argvars[idx]);
2608 if (win == NULL)
2609 {
2610 emsg(_(e_invalwindow));
2611 return NULL;
2612 }
2613 }
2614 return win;
2615}
2616
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002617/*
2618 * "clearmatches()" function
2619 */
2620 static void
2621f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2622{
2623#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002624 win_T *win = get_optional_window(argvars, 0);
2625
2626 if (win != NULL)
2627 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002628#endif
2629}
2630
2631/*
2632 * "col(string)" function
2633 */
2634 static void
2635f_col(typval_T *argvars, typval_T *rettv)
2636{
2637 colnr_T col = 0;
2638 pos_T *fp;
2639 int fnum = curbuf->b_fnum;
2640
2641 fp = var2fpos(&argvars[0], FALSE, &fnum);
2642 if (fp != NULL && fnum == curbuf->b_fnum)
2643 {
2644 if (fp->col == MAXCOL)
2645 {
2646 /* '> can be MAXCOL, get the length of the line then */
2647 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2648 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2649 else
2650 col = MAXCOL;
2651 }
2652 else
2653 {
2654 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002655 /* col(".") when the cursor is on the NUL at the end of the line
2656 * because of "coladd" can be seen as an extra column. */
2657 if (virtual_active() && fp == &curwin->w_cursor)
2658 {
2659 char_u *p = ml_get_cursor();
2660
2661 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2662 curwin->w_virtcol - curwin->w_cursor.coladd))
2663 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002664 int l;
2665
2666 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2667 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002668 }
2669 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002670 }
2671 }
2672 rettv->vval.v_number = col;
2673}
2674
2675#if defined(FEAT_INS_EXPAND)
2676/*
2677 * "complete()" function
2678 */
2679 static void
2680f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2681{
2682 int startcol;
2683
2684 if ((State & INSERT) == 0)
2685 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002686 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687 return;
2688 }
2689
2690 /* Check for undo allowed here, because if something was already inserted
2691 * the line was already saved for undo and this check isn't done. */
2692 if (!undo_allowed())
2693 return;
2694
2695 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2696 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002697 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002698 return;
2699 }
2700
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002701 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002702 if (startcol <= 0)
2703 return;
2704
2705 set_completion(startcol - 1, argvars[1].vval.v_list);
2706}
2707
2708/*
2709 * "complete_add()" function
2710 */
2711 static void
2712f_complete_add(typval_T *argvars, typval_T *rettv)
2713{
2714 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2715}
2716
2717/*
2718 * "complete_check()" function
2719 */
2720 static void
2721f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2722{
2723 int saved = RedrawingDisabled;
2724
2725 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002726 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002727 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002728 RedrawingDisabled = saved;
2729}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002730
2731/*
2732 * "complete_info()" function
2733 */
2734 static void
2735f_complete_info(typval_T *argvars, typval_T *rettv)
2736{
2737 list_T *what_list = NULL;
2738
2739 if (rettv_dict_alloc(rettv) != OK)
2740 return;
2741
2742 if (argvars[0].v_type != VAR_UNKNOWN)
2743 {
2744 if (argvars[0].v_type != VAR_LIST)
2745 {
2746 emsg(_(e_listreq));
2747 return;
2748 }
2749 what_list = argvars[0].vval.v_list;
2750 }
2751 get_complete_info(what_list, rettv->vval.v_dict);
2752}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002753#endif
2754
2755/*
2756 * "confirm(message, buttons[, default [, type]])" function
2757 */
2758 static void
2759f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2760{
2761#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2762 char_u *message;
2763 char_u *buttons = NULL;
2764 char_u buf[NUMBUFLEN];
2765 char_u buf2[NUMBUFLEN];
2766 int def = 1;
2767 int type = VIM_GENERIC;
2768 char_u *typestr;
2769 int error = FALSE;
2770
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002771 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002772 if (message == NULL)
2773 error = TRUE;
2774 if (argvars[1].v_type != VAR_UNKNOWN)
2775 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002776 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002777 if (buttons == NULL)
2778 error = TRUE;
2779 if (argvars[2].v_type != VAR_UNKNOWN)
2780 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002781 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 if (argvars[3].v_type != VAR_UNKNOWN)
2783 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002784 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785 if (typestr == NULL)
2786 error = TRUE;
2787 else
2788 {
2789 switch (TOUPPER_ASC(*typestr))
2790 {
2791 case 'E': type = VIM_ERROR; break;
2792 case 'Q': type = VIM_QUESTION; break;
2793 case 'I': type = VIM_INFO; break;
2794 case 'W': type = VIM_WARNING; break;
2795 case 'G': type = VIM_GENERIC; break;
2796 }
2797 }
2798 }
2799 }
2800 }
2801
2802 if (buttons == NULL || *buttons == NUL)
2803 buttons = (char_u *)_("&Ok");
2804
2805 if (!error)
2806 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2807 def, NULL, FALSE);
2808#endif
2809}
2810
2811/*
2812 * "copy()" function
2813 */
2814 static void
2815f_copy(typval_T *argvars, typval_T *rettv)
2816{
2817 item_copy(&argvars[0], rettv, FALSE, 0);
2818}
2819
2820#ifdef FEAT_FLOAT
2821/*
2822 * "cos()" function
2823 */
2824 static void
2825f_cos(typval_T *argvars, typval_T *rettv)
2826{
2827 float_T f = 0.0;
2828
2829 rettv->v_type = VAR_FLOAT;
2830 if (get_float_arg(argvars, &f) == OK)
2831 rettv->vval.v_float = cos(f);
2832 else
2833 rettv->vval.v_float = 0.0;
2834}
2835
2836/*
2837 * "cosh()" function
2838 */
2839 static void
2840f_cosh(typval_T *argvars, typval_T *rettv)
2841{
2842 float_T f = 0.0;
2843
2844 rettv->v_type = VAR_FLOAT;
2845 if (get_float_arg(argvars, &f) == OK)
2846 rettv->vval.v_float = cosh(f);
2847 else
2848 rettv->vval.v_float = 0.0;
2849}
2850#endif
2851
2852/*
2853 * "count()" function
2854 */
2855 static void
2856f_count(typval_T *argvars, typval_T *rettv)
2857{
2858 long n = 0;
2859 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002860 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002861
Bram Moolenaar9966b212017-07-28 16:46:57 +02002862 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002863 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002864
2865 if (argvars[0].v_type == VAR_STRING)
2866 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002867 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002868 char_u *p = argvars[0].vval.v_string;
2869 char_u *next;
2870
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002871 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002872 {
2873 if (ic)
2874 {
2875 size_t len = STRLEN(expr);
2876
2877 while (*p != NUL)
2878 {
2879 if (MB_STRNICMP(p, expr, len) == 0)
2880 {
2881 ++n;
2882 p += len;
2883 }
2884 else
2885 MB_PTR_ADV(p);
2886 }
2887 }
2888 else
2889 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2890 != NULL)
2891 {
2892 ++n;
2893 p = next + STRLEN(expr);
2894 }
2895 }
2896
2897 }
2898 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002899 {
2900 listitem_T *li;
2901 list_T *l;
2902 long idx;
2903
2904 if ((l = argvars[0].vval.v_list) != NULL)
2905 {
2906 li = l->lv_first;
2907 if (argvars[2].v_type != VAR_UNKNOWN)
2908 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002909 if (argvars[3].v_type != VAR_UNKNOWN)
2910 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002911 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002912 if (!error)
2913 {
2914 li = list_find(l, idx);
2915 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002916 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002917 }
2918 }
2919 if (error)
2920 li = NULL;
2921 }
2922
2923 for ( ; li != NULL; li = li->li_next)
2924 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2925 ++n;
2926 }
2927 }
2928 else if (argvars[0].v_type == VAR_DICT)
2929 {
2930 int todo;
2931 dict_T *d;
2932 hashitem_T *hi;
2933
2934 if ((d = argvars[0].vval.v_dict) != NULL)
2935 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002936 if (argvars[2].v_type != VAR_UNKNOWN)
2937 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002938 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002939 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002940 }
2941
2942 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2943 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2944 {
2945 if (!HASHITEM_EMPTY(hi))
2946 {
2947 --todo;
2948 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2949 ++n;
2950 }
2951 }
2952 }
2953 }
2954 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002955 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956 rettv->vval.v_number = n;
2957}
2958
2959/*
2960 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2961 *
2962 * Checks the existence of a cscope connection.
2963 */
2964 static void
2965f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2966{
2967#ifdef FEAT_CSCOPE
2968 int num = 0;
2969 char_u *dbpath = NULL;
2970 char_u *prepend = NULL;
2971 char_u buf[NUMBUFLEN];
2972
2973 if (argvars[0].v_type != VAR_UNKNOWN
2974 && argvars[1].v_type != VAR_UNKNOWN)
2975 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002976 num = (int)tv_get_number(&argvars[0]);
2977 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002978 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002979 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002980 }
2981
2982 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2983#endif
2984}
2985
2986/*
2987 * "cursor(lnum, col)" function, or
2988 * "cursor(list)"
2989 *
2990 * Moves the cursor to the specified line and column.
2991 * Returns 0 when the position could be set, -1 otherwise.
2992 */
2993 static void
2994f_cursor(typval_T *argvars, typval_T *rettv)
2995{
2996 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002997 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002998 int set_curswant = TRUE;
2999
3000 rettv->vval.v_number = -1;
3001 if (argvars[1].v_type == VAR_UNKNOWN)
3002 {
3003 pos_T pos;
3004 colnr_T curswant = -1;
3005
3006 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003008 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009 return;
3010 }
3011 line = pos.lnum;
3012 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003013 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003014 if (curswant >= 0)
3015 {
3016 curwin->w_curswant = curswant - 1;
3017 set_curswant = FALSE;
3018 }
3019 }
3020 else
3021 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003022 line = tv_get_lnum(argvars);
3023 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003024 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003025 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003026 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003027 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003028 return; /* type error; errmsg already given */
3029 if (line > 0)
3030 curwin->w_cursor.lnum = line;
3031 if (col > 0)
3032 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003034
3035 /* Make sure the cursor is in a valid position. */
3036 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003037 /* Correct cursor for multi-byte character. */
3038 if (has_mbyte)
3039 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003040
3041 curwin->w_set_curswant = set_curswant;
3042 rettv->vval.v_number = 0;
3043}
3044
Bram Moolenaar4f974752019-02-17 17:44:42 +01003045#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003046/*
3047 * "debugbreak()" function
3048 */
3049 static void
3050f_debugbreak(typval_T *argvars, typval_T *rettv)
3051{
3052 int pid;
3053
3054 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003055 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003056 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003057 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003058 else
3059 {
3060 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3061
3062 if (hProcess != NULL)
3063 {
3064 DebugBreakProcess(hProcess);
3065 CloseHandle(hProcess);
3066 rettv->vval.v_number = OK;
3067 }
3068 }
3069}
3070#endif
3071
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003072/*
3073 * "deepcopy()" function
3074 */
3075 static void
3076f_deepcopy(typval_T *argvars, typval_T *rettv)
3077{
3078 int noref = 0;
3079 int copyID;
3080
3081 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003082 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003083 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003084 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003085 else
3086 {
3087 copyID = get_copyID();
3088 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3089 }
3090}
3091
3092/*
3093 * "delete()" function
3094 */
3095 static void
3096f_delete(typval_T *argvars, typval_T *rettv)
3097{
3098 char_u nbuf[NUMBUFLEN];
3099 char_u *name;
3100 char_u *flags;
3101
3102 rettv->vval.v_number = -1;
3103 if (check_restricted() || check_secure())
3104 return;
3105
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003106 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003107 if (name == NULL || *name == NUL)
3108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003109 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003110 return;
3111 }
3112
3113 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003114 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 else
3116 flags = (char_u *)"";
3117
3118 if (*flags == NUL)
3119 /* delete a file */
3120 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3121 else if (STRCMP(flags, "d") == 0)
3122 /* delete an empty directory */
3123 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3124 else if (STRCMP(flags, "rf") == 0)
3125 /* delete a directory recursively */
3126 rettv->vval.v_number = delete_recursive(name);
3127 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003128 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003129}
3130
3131/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003132 * "deletebufline()" function
3133 */
3134 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003135f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003136{
3137 buf_T *buf;
3138 linenr_T first, last;
3139 linenr_T lnum;
3140 long count;
3141 int is_curbuf;
3142 buf_T *curbuf_save = NULL;
3143 win_T *curwin_save = NULL;
3144 tabpage_T *tp;
3145 win_T *wp;
3146
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003147 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003148 if (buf == NULL)
3149 {
3150 rettv->vval.v_number = 1; /* FAIL */
3151 return;
3152 }
3153 is_curbuf = buf == curbuf;
3154
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003155 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003156 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003157 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003158 else
3159 last = first;
3160
3161 if (buf->b_ml.ml_mfp == NULL || first < 1
3162 || first > buf->b_ml.ml_line_count || last < first)
3163 {
3164 rettv->vval.v_number = 1; /* FAIL */
3165 return;
3166 }
3167
3168 if (!is_curbuf)
3169 {
3170 curbuf_save = curbuf;
3171 curwin_save = curwin;
3172 curbuf = buf;
3173 find_win_for_curbuf();
3174 }
3175 if (last > curbuf->b_ml.ml_line_count)
3176 last = curbuf->b_ml.ml_line_count;
3177 count = last - first + 1;
3178
3179 // When coming here from Insert mode, sync undo, so that this can be
3180 // undone separately from what was previously inserted.
3181 if (u_sync_once == 2)
3182 {
3183 u_sync_once = 1; // notify that u_sync() was called
3184 u_sync(TRUE);
3185 }
3186
3187 if (u_save(first - 1, last + 1) == FAIL)
3188 {
3189 rettv->vval.v_number = 1; /* FAIL */
3190 return;
3191 }
3192
3193 for (lnum = first; lnum <= last; ++lnum)
3194 ml_delete(first, TRUE);
3195
3196 FOR_ALL_TAB_WINDOWS(tp, wp)
3197 if (wp->w_buffer == buf)
3198 {
3199 if (wp->w_cursor.lnum > last)
3200 wp->w_cursor.lnum -= count;
3201 else if (wp->w_cursor.lnum> first)
3202 wp->w_cursor.lnum = first;
3203 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3204 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3205 }
3206 check_cursor_col();
3207 deleted_lines_mark(first, count);
3208
3209 if (!is_curbuf)
3210 {
3211 curbuf = curbuf_save;
3212 curwin = curwin_save;
3213 }
3214}
3215
3216/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003217 * "did_filetype()" function
3218 */
3219 static void
3220f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3221{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003222 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003223}
3224
3225/*
3226 * "diff_filler()" function
3227 */
3228 static void
3229f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3230{
3231#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003232 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003233#endif
3234}
3235
3236/*
3237 * "diff_hlID()" function
3238 */
3239 static void
3240f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3241{
3242#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003243 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003244 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003245 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003246 static int fnum = 0;
3247 static int change_start = 0;
3248 static int change_end = 0;
3249 static hlf_T hlID = (hlf_T)0;
3250 int filler_lines;
3251 int col;
3252
3253 if (lnum < 0) /* ignore type error in {lnum} arg */
3254 lnum = 0;
3255 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003256 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003257 || fnum != curbuf->b_fnum)
3258 {
3259 /* New line, buffer, change: need to get the values. */
3260 filler_lines = diff_check(curwin, lnum);
3261 if (filler_lines < 0)
3262 {
3263 if (filler_lines == -1)
3264 {
3265 change_start = MAXCOL;
3266 change_end = -1;
3267 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3268 hlID = HLF_ADD; /* added line */
3269 else
3270 hlID = HLF_CHD; /* changed line */
3271 }
3272 else
3273 hlID = HLF_ADD; /* added line */
3274 }
3275 else
3276 hlID = (hlf_T)0;
3277 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003278 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 fnum = curbuf->b_fnum;
3280 }
3281
3282 if (hlID == HLF_CHD || hlID == HLF_TXD)
3283 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003284 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003285 if (col >= change_start && col <= change_end)
3286 hlID = HLF_TXD; /* changed text */
3287 else
3288 hlID = HLF_CHD; /* changed line */
3289 }
3290 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3291#endif
3292}
3293
3294/*
3295 * "empty({expr})" function
3296 */
3297 static void
3298f_empty(typval_T *argvars, typval_T *rettv)
3299{
3300 int n = FALSE;
3301
3302 switch (argvars[0].v_type)
3303 {
3304 case VAR_STRING:
3305 case VAR_FUNC:
3306 n = argvars[0].vval.v_string == NULL
3307 || *argvars[0].vval.v_string == NUL;
3308 break;
3309 case VAR_PARTIAL:
3310 n = FALSE;
3311 break;
3312 case VAR_NUMBER:
3313 n = argvars[0].vval.v_number == 0;
3314 break;
3315 case VAR_FLOAT:
3316#ifdef FEAT_FLOAT
3317 n = argvars[0].vval.v_float == 0.0;
3318 break;
3319#endif
3320 case VAR_LIST:
3321 n = argvars[0].vval.v_list == NULL
3322 || argvars[0].vval.v_list->lv_first == NULL;
3323 break;
3324 case VAR_DICT:
3325 n = argvars[0].vval.v_dict == NULL
3326 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3327 break;
3328 case VAR_SPECIAL:
3329 n = argvars[0].vval.v_number != VVAL_TRUE;
3330 break;
3331
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003332 case VAR_BLOB:
3333 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003334 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3335 break;
3336
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003337 case VAR_JOB:
3338#ifdef FEAT_JOB_CHANNEL
3339 n = argvars[0].vval.v_job == NULL
3340 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3341 break;
3342#endif
3343 case VAR_CHANNEL:
3344#ifdef FEAT_JOB_CHANNEL
3345 n = argvars[0].vval.v_channel == NULL
3346 || !channel_is_open(argvars[0].vval.v_channel);
3347 break;
3348#endif
3349 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003350 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003351 n = TRUE;
3352 break;
3353 }
3354
3355 rettv->vval.v_number = n;
3356}
3357
3358/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003359 * "environ()" function
3360 */
3361 static void
3362f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3363{
3364#if !defined(AMIGA)
3365 int i = 0;
3366 char_u *entry, *value;
3367# ifdef MSWIN
3368 extern wchar_t **_wenviron;
3369# else
3370 extern char **environ;
3371# endif
3372
3373 if (rettv_dict_alloc(rettv) != OK)
3374 return;
3375
3376# ifdef MSWIN
3377 if (*_wenviron == NULL)
3378 return;
3379# else
3380 if (*environ == NULL)
3381 return;
3382# endif
3383
3384 for (i = 0; ; ++i)
3385 {
3386# ifdef MSWIN
3387 short_u *p;
3388
3389 if ((p = (short_u *)_wenviron[i]) == NULL)
3390 return;
3391 entry = utf16_to_enc(p, NULL);
3392# else
3393 if ((entry = (char_u *)environ[i]) == NULL)
3394 return;
3395 entry = vim_strsave(entry);
3396# endif
3397 if (entry == NULL) // out of memory
3398 return;
3399 if ((value = vim_strchr(entry, '=')) == NULL)
3400 {
3401 vim_free(entry);
3402 continue;
3403 }
3404 *value++ = NUL;
3405 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3406 vim_free(entry);
3407 }
3408#endif
3409}
3410
3411/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003412 * "escape({string}, {chars})" function
3413 */
3414 static void
3415f_escape(typval_T *argvars, typval_T *rettv)
3416{
3417 char_u buf[NUMBUFLEN];
3418
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003419 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3420 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003421 rettv->v_type = VAR_STRING;
3422}
3423
3424/*
3425 * "eval()" function
3426 */
3427 static void
3428f_eval(typval_T *argvars, typval_T *rettv)
3429{
3430 char_u *s, *p;
3431
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003432 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003433 if (s != NULL)
3434 s = skipwhite(s);
3435
3436 p = s;
3437 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3438 {
3439 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003440 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441 need_clr_eos = FALSE;
3442 rettv->v_type = VAR_NUMBER;
3443 rettv->vval.v_number = 0;
3444 }
3445 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003446 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003447}
3448
3449/*
3450 * "eventhandler()" function
3451 */
3452 static void
3453f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3454{
3455 rettv->vval.v_number = vgetc_busy;
3456}
3457
3458/*
3459 * "executable()" function
3460 */
3461 static void
3462f_executable(typval_T *argvars, typval_T *rettv)
3463{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003464 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003465
3466 /* Check in $PATH and also check directly if there is a directory name. */
3467 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3468 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3469}
3470
3471static garray_T redir_execute_ga;
3472
3473/*
3474 * Append "value[value_len]" to the execute() output.
3475 */
3476 void
3477execute_redir_str(char_u *value, int value_len)
3478{
3479 int len;
3480
3481 if (value_len == -1)
3482 len = (int)STRLEN(value); /* Append the entire string */
3483 else
3484 len = value_len; /* Append only "value_len" characters */
3485 if (ga_grow(&redir_execute_ga, len) == OK)
3486 {
3487 mch_memmove((char *)redir_execute_ga.ga_data
3488 + redir_execute_ga.ga_len, value, len);
3489 redir_execute_ga.ga_len += len;
3490 }
3491}
3492
3493/*
3494 * Get next line from a list.
3495 * Called by do_cmdline() to get the next line.
3496 * Returns allocated string, or NULL for end of function.
3497 */
3498
3499 static char_u *
3500get_list_line(
3501 int c UNUSED,
3502 void *cookie,
3503 int indent UNUSED)
3504{
3505 listitem_T **p = (listitem_T **)cookie;
3506 listitem_T *item = *p;
3507 char_u buf[NUMBUFLEN];
3508 char_u *s;
3509
3510 if (item == NULL)
3511 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003512 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003513 *p = item->li_next;
3514 return s == NULL ? NULL : vim_strsave(s);
3515}
3516
3517/*
3518 * "execute()" function
3519 */
3520 static void
3521f_execute(typval_T *argvars, typval_T *rettv)
3522{
3523 char_u *cmd = NULL;
3524 list_T *list = NULL;
3525 int save_msg_silent = msg_silent;
3526 int save_emsg_silent = emsg_silent;
3527 int save_emsg_noredir = emsg_noredir;
3528 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003529 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003530 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003531 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003532 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003533
3534 rettv->vval.v_string = NULL;
3535 rettv->v_type = VAR_STRING;
3536
3537 if (argvars[0].v_type == VAR_LIST)
3538 {
3539 list = argvars[0].vval.v_list;
3540 if (list == NULL || list->lv_first == NULL)
3541 /* empty list, no commands, empty output */
3542 return;
3543 ++list->lv_refcount;
3544 }
3545 else
3546 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003547 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003548 if (cmd == NULL)
3549 return;
3550 }
3551
3552 if (argvars[1].v_type != VAR_UNKNOWN)
3553 {
3554 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003555 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003556
3557 if (s == NULL)
3558 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003559 if (*s == NUL)
3560 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561 if (STRNCMP(s, "silent", 6) == 0)
3562 ++msg_silent;
3563 if (STRCMP(s, "silent!") == 0)
3564 {
3565 emsg_silent = TRUE;
3566 emsg_noredir = TRUE;
3567 }
3568 }
3569 else
3570 ++msg_silent;
3571
3572 if (redir_execute)
3573 save_ga = redir_execute_ga;
3574 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3575 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003576 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003577 if (!echo_output)
3578 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003579
3580 if (cmd != NULL)
3581 do_cmdline_cmd(cmd);
3582 else
3583 {
3584 listitem_T *item = list->lv_first;
3585
3586 do_cmdline(NULL, get_list_line, (void *)&item,
3587 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3588 --list->lv_refcount;
3589 }
3590
Bram Moolenaard297f352017-01-29 20:31:21 +01003591 /* Need to append a NUL to the result. */
3592 if (ga_grow(&redir_execute_ga, 1) == OK)
3593 {
3594 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3595 rettv->vval.v_string = redir_execute_ga.ga_data;
3596 }
3597 else
3598 {
3599 ga_clear(&redir_execute_ga);
3600 rettv->vval.v_string = NULL;
3601 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003602 msg_silent = save_msg_silent;
3603 emsg_silent = save_emsg_silent;
3604 emsg_noredir = save_emsg_noredir;
3605
3606 redir_execute = save_redir_execute;
3607 if (redir_execute)
3608 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003609 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003610
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003611 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003612 if (echo_output)
3613 // When not working silently: put it in column zero. A following
3614 // "echon" will overwrite the message, unavoidably.
3615 msg_col = 0;
3616 else
3617 // When working silently: Put it back where it was, since nothing
3618 // should have been written.
3619 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003620}
3621
3622/*
3623 * "exepath()" function
3624 */
3625 static void
3626f_exepath(typval_T *argvars, typval_T *rettv)
3627{
3628 char_u *p = NULL;
3629
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003630 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003631 rettv->v_type = VAR_STRING;
3632 rettv->vval.v_string = p;
3633}
3634
3635/*
3636 * "exists()" function
3637 */
3638 static void
3639f_exists(typval_T *argvars, typval_T *rettv)
3640{
3641 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003642 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003644 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003645 if (*p == '$') /* environment variable */
3646 {
3647 /* first try "normal" environment variables (fast) */
3648 if (mch_getenv(p + 1) != NULL)
3649 n = TRUE;
3650 else
3651 {
3652 /* try expanding things like $VIM and ${HOME} */
3653 p = expand_env_save(p);
3654 if (p != NULL && *p != '$')
3655 n = TRUE;
3656 vim_free(p);
3657 }
3658 }
3659 else if (*p == '&' || *p == '+') /* option */
3660 {
3661 n = (get_option_tv(&p, NULL, TRUE) == OK);
3662 if (*skipwhite(p) != NUL)
3663 n = FALSE; /* trailing garbage */
3664 }
3665 else if (*p == '*') /* internal or user defined function */
3666 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003667 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003668 }
3669 else if (*p == ':')
3670 {
3671 n = cmd_exists(p + 1);
3672 }
3673 else if (*p == '#')
3674 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003675 if (p[1] == '#')
3676 n = autocmd_supported(p + 2);
3677 else
3678 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003679 }
3680 else /* internal variable */
3681 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003682 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003683 }
3684
3685 rettv->vval.v_number = n;
3686}
3687
3688#ifdef FEAT_FLOAT
3689/*
3690 * "exp()" function
3691 */
3692 static void
3693f_exp(typval_T *argvars, typval_T *rettv)
3694{
3695 float_T f = 0.0;
3696
3697 rettv->v_type = VAR_FLOAT;
3698 if (get_float_arg(argvars, &f) == OK)
3699 rettv->vval.v_float = exp(f);
3700 else
3701 rettv->vval.v_float = 0.0;
3702}
3703#endif
3704
3705/*
3706 * "expand()" function
3707 */
3708 static void
3709f_expand(typval_T *argvars, typval_T *rettv)
3710{
3711 char_u *s;
3712 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003713 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003714 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3715 expand_T xpc;
3716 int error = FALSE;
3717 char_u *result;
3718
3719 rettv->v_type = VAR_STRING;
3720 if (argvars[1].v_type != VAR_UNKNOWN
3721 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003722 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003723 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003724 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003725
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003726 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003727 if (*s == '%' || *s == '#' || *s == '<')
3728 {
3729 ++emsg_off;
3730 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3731 --emsg_off;
3732 if (rettv->v_type == VAR_LIST)
3733 {
3734 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3735 list_append_string(rettv->vval.v_list, result, -1);
3736 else
3737 vim_free(result);
3738 }
3739 else
3740 rettv->vval.v_string = result;
3741 }
3742 else
3743 {
3744 /* When the optional second argument is non-zero, don't remove matches
3745 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3746 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003747 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003748 options |= WILD_KEEP_ALL;
3749 if (!error)
3750 {
3751 ExpandInit(&xpc);
3752 xpc.xp_context = EXPAND_FILES;
3753 if (p_wic)
3754 options += WILD_ICASE;
3755 if (rettv->v_type == VAR_STRING)
3756 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3757 options, WILD_ALL);
3758 else if (rettv_list_alloc(rettv) != FAIL)
3759 {
3760 int i;
3761
3762 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3763 for (i = 0; i < xpc.xp_numfiles; i++)
3764 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3765 ExpandCleanup(&xpc);
3766 }
3767 }
3768 else
3769 rettv->vval.v_string = NULL;
3770 }
3771}
3772
3773/*
3774 * "extend(list, list [, idx])" function
3775 * "extend(dict, dict [, action])" function
3776 */
3777 static void
3778f_extend(typval_T *argvars, typval_T *rettv)
3779{
3780 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3781
3782 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3783 {
3784 list_T *l1, *l2;
3785 listitem_T *item;
3786 long before;
3787 int error = FALSE;
3788
3789 l1 = argvars[0].vval.v_list;
3790 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003791 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003792 && l2 != NULL)
3793 {
3794 if (argvars[2].v_type != VAR_UNKNOWN)
3795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003796 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003797 if (error)
3798 return; /* type error; errmsg already given */
3799
3800 if (before == l1->lv_len)
3801 item = NULL;
3802 else
3803 {
3804 item = list_find(l1, before);
3805 if (item == NULL)
3806 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003807 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003808 return;
3809 }
3810 }
3811 }
3812 else
3813 item = NULL;
3814 list_extend(l1, l2, item);
3815
3816 copy_tv(&argvars[0], rettv);
3817 }
3818 }
3819 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3820 {
3821 dict_T *d1, *d2;
3822 char_u *action;
3823 int i;
3824
3825 d1 = argvars[0].vval.v_dict;
3826 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003827 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003828 && d2 != NULL)
3829 {
3830 /* Check the third argument. */
3831 if (argvars[2].v_type != VAR_UNKNOWN)
3832 {
3833 static char *(av[]) = {"keep", "force", "error"};
3834
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003835 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003836 if (action == NULL)
3837 return; /* type error; errmsg already given */
3838 for (i = 0; i < 3; ++i)
3839 if (STRCMP(action, av[i]) == 0)
3840 break;
3841 if (i == 3)
3842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003843 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003844 return;
3845 }
3846 }
3847 else
3848 action = (char_u *)"force";
3849
3850 dict_extend(d1, d2, action);
3851
3852 copy_tv(&argvars[0], rettv);
3853 }
3854 }
3855 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003856 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003857}
3858
3859/*
3860 * "feedkeys()" function
3861 */
3862 static void
3863f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3864{
3865 int remap = TRUE;
3866 int insert = FALSE;
3867 char_u *keys, *flags;
3868 char_u nbuf[NUMBUFLEN];
3869 int typed = FALSE;
3870 int execute = FALSE;
3871 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003872 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003873 char_u *keys_esc;
3874
3875 /* This is not allowed in the sandbox. If the commands would still be
3876 * executed in the sandbox it would be OK, but it probably happens later,
3877 * when "sandbox" is no longer set. */
3878 if (check_secure())
3879 return;
3880
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003881 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003882
3883 if (argvars[1].v_type != VAR_UNKNOWN)
3884 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003885 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003886 for ( ; *flags != NUL; ++flags)
3887 {
3888 switch (*flags)
3889 {
3890 case 'n': remap = FALSE; break;
3891 case 'm': remap = TRUE; break;
3892 case 't': typed = TRUE; break;
3893 case 'i': insert = TRUE; break;
3894 case 'x': execute = TRUE; break;
3895 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003896 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003897 }
3898 }
3899 }
3900
3901 if (*keys != NUL || execute)
3902 {
3903 /* Need to escape K_SPECIAL and CSI before putting the string in the
3904 * typeahead buffer. */
3905 keys_esc = vim_strsave_escape_csi(keys);
3906 if (keys_esc != NULL)
3907 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003908 if (lowlevel)
3909 {
3910#ifdef USE_INPUT_BUF
3911 add_to_input_buf(keys, (int)STRLEN(keys));
3912#else
3913 emsg(_("E980: lowlevel input not supported"));
3914#endif
3915 }
3916 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003917 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003918 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003919 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003920 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003921#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003922 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003923#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003924 )
3925 typebuf_was_filled = TRUE;
3926 }
3927 vim_free(keys_esc);
3928
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003929 if (execute)
3930 {
3931 int save_msg_scroll = msg_scroll;
3932
3933 /* Avoid a 1 second delay when the keys start Insert mode. */
3934 msg_scroll = FALSE;
3935
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003936 if (!dangerous)
3937 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003938 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003939 if (!dangerous)
3940 --ex_normal_busy;
3941
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003942 msg_scroll |= save_msg_scroll;
3943 }
3944 }
3945 }
3946}
3947
3948/*
3949 * "filereadable()" function
3950 */
3951 static void
3952f_filereadable(typval_T *argvars, typval_T *rettv)
3953{
3954 int fd;
3955 char_u *p;
3956 int n;
3957
3958#ifndef O_NONBLOCK
3959# define O_NONBLOCK 0
3960#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003961 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003962 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3963 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3964 {
3965 n = TRUE;
3966 close(fd);
3967 }
3968 else
3969 n = FALSE;
3970
3971 rettv->vval.v_number = n;
3972}
3973
3974/*
3975 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3976 * rights to write into.
3977 */
3978 static void
3979f_filewritable(typval_T *argvars, typval_T *rettv)
3980{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003981 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003982}
3983
3984 static void
3985findfilendir(
3986 typval_T *argvars UNUSED,
3987 typval_T *rettv,
3988 int find_what UNUSED)
3989{
3990#ifdef FEAT_SEARCHPATH
3991 char_u *fname;
3992 char_u *fresult = NULL;
3993 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3994 char_u *p;
3995 char_u pathbuf[NUMBUFLEN];
3996 int count = 1;
3997 int first = TRUE;
3998 int error = FALSE;
3999#endif
4000
4001 rettv->vval.v_string = NULL;
4002 rettv->v_type = VAR_STRING;
4003
4004#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004005 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004006
4007 if (argvars[1].v_type != VAR_UNKNOWN)
4008 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004009 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004010 if (p == NULL)
4011 error = TRUE;
4012 else
4013 {
4014 if (*p != NUL)
4015 path = p;
4016
4017 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004018 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004019 }
4020 }
4021
4022 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4023 error = TRUE;
4024
4025 if (*fname != NUL && !error)
4026 {
4027 do
4028 {
4029 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4030 vim_free(fresult);
4031 fresult = find_file_in_path_option(first ? fname : NULL,
4032 first ? (int)STRLEN(fname) : 0,
4033 0, first, path,
4034 find_what,
4035 curbuf->b_ffname,
4036 find_what == FINDFILE_DIR
4037 ? (char_u *)"" : curbuf->b_p_sua);
4038 first = FALSE;
4039
4040 if (fresult != NULL && rettv->v_type == VAR_LIST)
4041 list_append_string(rettv->vval.v_list, fresult, -1);
4042
4043 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4044 }
4045
4046 if (rettv->v_type == VAR_STRING)
4047 rettv->vval.v_string = fresult;
4048#endif
4049}
4050
4051/*
4052 * "filter()" function
4053 */
4054 static void
4055f_filter(typval_T *argvars, typval_T *rettv)
4056{
4057 filter_map(argvars, rettv, FALSE);
4058}
4059
4060/*
4061 * "finddir({fname}[, {path}[, {count}]])" function
4062 */
4063 static void
4064f_finddir(typval_T *argvars, typval_T *rettv)
4065{
4066 findfilendir(argvars, rettv, FINDFILE_DIR);
4067}
4068
4069/*
4070 * "findfile({fname}[, {path}[, {count}]])" function
4071 */
4072 static void
4073f_findfile(typval_T *argvars, typval_T *rettv)
4074{
4075 findfilendir(argvars, rettv, FINDFILE_FILE);
4076}
4077
4078#ifdef FEAT_FLOAT
4079/*
4080 * "float2nr({float})" function
4081 */
4082 static void
4083f_float2nr(typval_T *argvars, typval_T *rettv)
4084{
4085 float_T f = 0.0;
4086
4087 if (get_float_arg(argvars, &f) == OK)
4088 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004089 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004090 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004091 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004092 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004093 else
4094 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004095 }
4096}
4097
4098/*
4099 * "floor({float})" function
4100 */
4101 static void
4102f_floor(typval_T *argvars, typval_T *rettv)
4103{
4104 float_T f = 0.0;
4105
4106 rettv->v_type = VAR_FLOAT;
4107 if (get_float_arg(argvars, &f) == OK)
4108 rettv->vval.v_float = floor(f);
4109 else
4110 rettv->vval.v_float = 0.0;
4111}
4112
4113/*
4114 * "fmod()" function
4115 */
4116 static void
4117f_fmod(typval_T *argvars, typval_T *rettv)
4118{
4119 float_T fx = 0.0, fy = 0.0;
4120
4121 rettv->v_type = VAR_FLOAT;
4122 if (get_float_arg(argvars, &fx) == OK
4123 && get_float_arg(&argvars[1], &fy) == OK)
4124 rettv->vval.v_float = fmod(fx, fy);
4125 else
4126 rettv->vval.v_float = 0.0;
4127}
4128#endif
4129
4130/*
4131 * "fnameescape({string})" function
4132 */
4133 static void
4134f_fnameescape(typval_T *argvars, typval_T *rettv)
4135{
4136 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004137 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004138 rettv->v_type = VAR_STRING;
4139}
4140
4141/*
4142 * "fnamemodify({fname}, {mods})" function
4143 */
4144 static void
4145f_fnamemodify(typval_T *argvars, typval_T *rettv)
4146{
4147 char_u *fname;
4148 char_u *mods;
4149 int usedlen = 0;
4150 int len;
4151 char_u *fbuf = NULL;
4152 char_u buf[NUMBUFLEN];
4153
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004154 fname = tv_get_string_chk(&argvars[0]);
4155 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004156 if (fname == NULL || mods == NULL)
4157 fname = NULL;
4158 else
4159 {
4160 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004161 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004162 }
4163
4164 rettv->v_type = VAR_STRING;
4165 if (fname == NULL)
4166 rettv->vval.v_string = NULL;
4167 else
4168 rettv->vval.v_string = vim_strnsave(fname, len);
4169 vim_free(fbuf);
4170}
4171
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004172/*
4173 * "foldclosed()" function
4174 */
4175 static void
4176foldclosed_both(
4177 typval_T *argvars UNUSED,
4178 typval_T *rettv,
4179 int end UNUSED)
4180{
4181#ifdef FEAT_FOLDING
4182 linenr_T lnum;
4183 linenr_T first, last;
4184
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004185 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004186 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4187 {
4188 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4189 {
4190 if (end)
4191 rettv->vval.v_number = (varnumber_T)last;
4192 else
4193 rettv->vval.v_number = (varnumber_T)first;
4194 return;
4195 }
4196 }
4197#endif
4198 rettv->vval.v_number = -1;
4199}
4200
4201/*
4202 * "foldclosed()" function
4203 */
4204 static void
4205f_foldclosed(typval_T *argvars, typval_T *rettv)
4206{
4207 foldclosed_both(argvars, rettv, FALSE);
4208}
4209
4210/*
4211 * "foldclosedend()" function
4212 */
4213 static void
4214f_foldclosedend(typval_T *argvars, typval_T *rettv)
4215{
4216 foldclosed_both(argvars, rettv, TRUE);
4217}
4218
4219/*
4220 * "foldlevel()" function
4221 */
4222 static void
4223f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4224{
4225#ifdef FEAT_FOLDING
4226 linenr_T lnum;
4227
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004228 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004229 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4230 rettv->vval.v_number = foldLevel(lnum);
4231#endif
4232}
4233
4234/*
4235 * "foldtext()" function
4236 */
4237 static void
4238f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4239{
4240#ifdef FEAT_FOLDING
4241 linenr_T foldstart;
4242 linenr_T foldend;
4243 char_u *dashes;
4244 linenr_T lnum;
4245 char_u *s;
4246 char_u *r;
4247 int len;
4248 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004249 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004250#endif
4251
4252 rettv->v_type = VAR_STRING;
4253 rettv->vval.v_string = NULL;
4254#ifdef FEAT_FOLDING
4255 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4256 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4257 dashes = get_vim_var_str(VV_FOLDDASHES);
4258 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4259 && dashes != NULL)
4260 {
4261 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004262 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004263 if (!linewhite(lnum))
4264 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004265
4266 /* Find interesting text in this line. */
4267 s = skipwhite(ml_get(lnum));
4268 /* skip C comment-start */
4269 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4270 {
4271 s = skipwhite(s + 2);
4272 if (*skipwhite(s) == NUL
4273 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4274 {
4275 s = skipwhite(ml_get(lnum + 1));
4276 if (*s == '*')
4277 s = skipwhite(s + 1);
4278 }
4279 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004280 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004281 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004282 r = alloc(STRLEN(txt)
4283 + STRLEN(dashes) // for %s
4284 + 20 // for %3ld
4285 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004286 if (r != NULL)
4287 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004288 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004289 len = (int)STRLEN(r);
4290 STRCAT(r, s);
4291 /* remove 'foldmarker' and 'commentstring' */
4292 foldtext_cleanup(r + len);
4293 rettv->vval.v_string = r;
4294 }
4295 }
4296#endif
4297}
4298
4299/*
4300 * "foldtextresult(lnum)" function
4301 */
4302 static void
4303f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4304{
4305#ifdef FEAT_FOLDING
4306 linenr_T lnum;
4307 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004308 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004309 foldinfo_T foldinfo;
4310 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004311 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004312#endif
4313
4314 rettv->v_type = VAR_STRING;
4315 rettv->vval.v_string = NULL;
4316#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004317 if (entered)
4318 return; /* reject recursive use */
4319 entered = TRUE;
4320
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004321 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004322 /* treat illegal types and illegal string values for {lnum} the same */
4323 if (lnum < 0)
4324 lnum = 0;
4325 fold_count = foldedCount(curwin, lnum, &foldinfo);
4326 if (fold_count > 0)
4327 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004328 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4329 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004330 if (text == buf)
4331 text = vim_strsave(text);
4332 rettv->vval.v_string = text;
4333 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004334
4335 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004336#endif
4337}
4338
4339/*
4340 * "foreground()" function
4341 */
4342 static void
4343f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4344{
4345#ifdef FEAT_GUI
4346 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004347 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004348 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004349 return;
4350 }
4351#endif
4352#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004353 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004354#endif
4355}
4356
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004357 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004358common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004359{
4360 char_u *s;
4361 char_u *name;
4362 int use_string = FALSE;
4363 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004364 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004365
4366 if (argvars[0].v_type == VAR_FUNC)
4367 {
4368 /* function(MyFunc, [arg], dict) */
4369 s = argvars[0].vval.v_string;
4370 }
4371 else if (argvars[0].v_type == VAR_PARTIAL
4372 && argvars[0].vval.v_partial != NULL)
4373 {
4374 /* function(dict.MyFunc, [arg]) */
4375 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004376 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004377 }
4378 else
4379 {
4380 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004381 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004382 use_string = TRUE;
4383 }
4384
Bram Moolenaar843b8842016-08-21 14:36:15 +02004385 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004386 {
4387 name = s;
4388 trans_name = trans_function_name(&name, FALSE,
4389 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4390 if (*name != NUL)
4391 s = NULL;
4392 }
4393
Bram Moolenaar843b8842016-08-21 14:36:15 +02004394 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4395 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004396 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004397 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004398 else if (trans_name != NULL && (is_funcref
4399 ? find_func(trans_name) == NULL
4400 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004401 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004402 else
4403 {
4404 int dict_idx = 0;
4405 int arg_idx = 0;
4406 list_T *list = NULL;
4407
4408 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4409 {
4410 char sid_buf[25];
4411 int off = *s == 's' ? 2 : 5;
4412
4413 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4414 * also be called from another script. Using trans_function_name()
4415 * would also work, but some plugins depend on the name being
4416 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004417 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004418 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004419 if (name != NULL)
4420 {
4421 STRCPY(name, sid_buf);
4422 STRCAT(name, s + off);
4423 }
4424 }
4425 else
4426 name = vim_strsave(s);
4427
4428 if (argvars[1].v_type != VAR_UNKNOWN)
4429 {
4430 if (argvars[2].v_type != VAR_UNKNOWN)
4431 {
4432 /* function(name, [args], dict) */
4433 arg_idx = 1;
4434 dict_idx = 2;
4435 }
4436 else if (argvars[1].v_type == VAR_DICT)
4437 /* function(name, dict) */
4438 dict_idx = 1;
4439 else
4440 /* function(name, [args]) */
4441 arg_idx = 1;
4442 if (dict_idx > 0)
4443 {
4444 if (argvars[dict_idx].v_type != VAR_DICT)
4445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004446 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004447 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004448 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004449 }
4450 if (argvars[dict_idx].vval.v_dict == NULL)
4451 dict_idx = 0;
4452 }
4453 if (arg_idx > 0)
4454 {
4455 if (argvars[arg_idx].v_type != VAR_LIST)
4456 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004457 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004458 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004459 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004460 }
4461 list = argvars[arg_idx].vval.v_list;
4462 if (list == NULL || list->lv_len == 0)
4463 arg_idx = 0;
4464 }
4465 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004466 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004467 {
4468 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4469
4470 /* result is a VAR_PARTIAL */
4471 if (pt == NULL)
4472 vim_free(name);
4473 else
4474 {
4475 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4476 {
4477 listitem_T *li;
4478 int i = 0;
4479 int arg_len = 0;
4480 int lv_len = 0;
4481
4482 if (arg_pt != NULL)
4483 arg_len = arg_pt->pt_argc;
4484 if (list != NULL)
4485 lv_len = list->lv_len;
4486 pt->pt_argc = arg_len + lv_len;
4487 pt->pt_argv = (typval_T *)alloc(
4488 sizeof(typval_T) * pt->pt_argc);
4489 if (pt->pt_argv == NULL)
4490 {
4491 vim_free(pt);
4492 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004493 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004494 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004495 for (i = 0; i < arg_len; i++)
4496 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4497 if (lv_len > 0)
4498 for (li = list->lv_first; li != NULL;
4499 li = li->li_next)
4500 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004501 }
4502
4503 /* For "function(dict.func, [], dict)" and "func" is a partial
4504 * use "dict". That is backwards compatible. */
4505 if (dict_idx > 0)
4506 {
4507 /* The dict is bound explicitly, pt_auto is FALSE. */
4508 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4509 ++pt->pt_dict->dv_refcount;
4510 }
4511 else if (arg_pt != NULL)
4512 {
4513 /* If the dict was bound automatically the result is also
4514 * bound automatically. */
4515 pt->pt_dict = arg_pt->pt_dict;
4516 pt->pt_auto = arg_pt->pt_auto;
4517 if (pt->pt_dict != NULL)
4518 ++pt->pt_dict->dv_refcount;
4519 }
4520
4521 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004522 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4523 {
4524 pt->pt_func = arg_pt->pt_func;
4525 func_ptr_ref(pt->pt_func);
4526 vim_free(name);
4527 }
4528 else if (is_funcref)
4529 {
4530 pt->pt_func = find_func(trans_name);
4531 func_ptr_ref(pt->pt_func);
4532 vim_free(name);
4533 }
4534 else
4535 {
4536 pt->pt_name = name;
4537 func_ref(name);
4538 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004539 }
4540 rettv->v_type = VAR_PARTIAL;
4541 rettv->vval.v_partial = pt;
4542 }
4543 else
4544 {
4545 /* result is a VAR_FUNC */
4546 rettv->v_type = VAR_FUNC;
4547 rettv->vval.v_string = name;
4548 func_ref(name);
4549 }
4550 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004551theend:
4552 vim_free(trans_name);
4553}
4554
4555/*
4556 * "funcref()" function
4557 */
4558 static void
4559f_funcref(typval_T *argvars, typval_T *rettv)
4560{
4561 common_function(argvars, rettv, TRUE);
4562}
4563
4564/*
4565 * "function()" function
4566 */
4567 static void
4568f_function(typval_T *argvars, typval_T *rettv)
4569{
4570 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004571}
4572
4573/*
4574 * "garbagecollect()" function
4575 */
4576 static void
4577f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4578{
4579 /* This is postponed until we are back at the toplevel, because we may be
4580 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4581 want_garbage_collect = TRUE;
4582
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004583 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004584 garbage_collect_at_exit = TRUE;
4585}
4586
4587/*
4588 * "get()" function
4589 */
4590 static void
4591f_get(typval_T *argvars, typval_T *rettv)
4592{
4593 listitem_T *li;
4594 list_T *l;
4595 dictitem_T *di;
4596 dict_T *d;
4597 typval_T *tv = NULL;
4598
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004599 if (argvars[0].v_type == VAR_BLOB)
4600 {
4601 int error = FALSE;
4602 int idx = tv_get_number_chk(&argvars[1], &error);
4603
4604 if (!error)
4605 {
4606 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004607 if (idx < 0)
4608 idx = blob_len(argvars[0].vval.v_blob) + idx;
4609 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4610 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004611 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004612 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004613 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004614 tv = rettv;
4615 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004616 }
4617 }
4618 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004619 {
4620 if ((l = argvars[0].vval.v_list) != NULL)
4621 {
4622 int error = FALSE;
4623
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004624 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004625 if (!error && li != NULL)
4626 tv = &li->li_tv;
4627 }
4628 }
4629 else if (argvars[0].v_type == VAR_DICT)
4630 {
4631 if ((d = argvars[0].vval.v_dict) != NULL)
4632 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004633 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004634 if (di != NULL)
4635 tv = &di->di_tv;
4636 }
4637 }
4638 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4639 {
4640 partial_T *pt;
4641 partial_T fref_pt;
4642
4643 if (argvars[0].v_type == VAR_PARTIAL)
4644 pt = argvars[0].vval.v_partial;
4645 else
4646 {
4647 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4648 fref_pt.pt_name = argvars[0].vval.v_string;
4649 pt = &fref_pt;
4650 }
4651
4652 if (pt != NULL)
4653 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004654 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004655 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004656
4657 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4658 {
4659 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004660 n = partial_name(pt);
4661 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004662 rettv->vval.v_string = NULL;
4663 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004664 {
4665 rettv->vval.v_string = vim_strsave(n);
4666 if (rettv->v_type == VAR_FUNC)
4667 func_ref(rettv->vval.v_string);
4668 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004669 }
4670 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004671 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004672 else if (STRCMP(what, "args") == 0)
4673 {
4674 rettv->v_type = VAR_LIST;
4675 if (rettv_list_alloc(rettv) == OK)
4676 {
4677 int i;
4678
4679 for (i = 0; i < pt->pt_argc; ++i)
4680 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4681 }
4682 }
4683 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004684 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004685 return;
4686 }
4687 }
4688 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004689 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004690
4691 if (tv == NULL)
4692 {
4693 if (argvars[2].v_type != VAR_UNKNOWN)
4694 copy_tv(&argvars[2], rettv);
4695 }
4696 else
4697 copy_tv(tv, rettv);
4698}
4699
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004700/*
4701 * Returns buffer options, variables and other attributes in a dictionary.
4702 */
4703 static dict_T *
4704get_buffer_info(buf_T *buf)
4705{
4706 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004707 tabpage_T *tp;
4708 win_T *wp;
4709 list_T *windows;
4710
4711 dict = dict_alloc();
4712 if (dict == NULL)
4713 return NULL;
4714
Bram Moolenaare0be1672018-07-08 16:50:37 +02004715 dict_add_number(dict, "bufnr", buf->b_fnum);
4716 dict_add_string(dict, "name", buf->b_ffname);
4717 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4718 : buflist_findlnum(buf));
4719 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4720 dict_add_number(dict, "listed", buf->b_p_bl);
4721 dict_add_number(dict, "changed", bufIsChanged(buf));
4722 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4723 dict_add_number(dict, "hidden",
4724 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004725
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004726 /* Get a reference to buffer variables */
4727 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004728
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004729 /* List of windows displaying this buffer */
4730 windows = list_alloc();
4731 if (windows != NULL)
4732 {
4733 FOR_ALL_TAB_WINDOWS(tp, wp)
4734 if (wp->w_buffer == buf)
4735 list_append_number(windows, (varnumber_T)wp->w_id);
4736 dict_add_list(dict, "windows", windows);
4737 }
4738
4739#ifdef FEAT_SIGNS
4740 if (buf->b_signlist != NULL)
4741 {
4742 /* List of signs placed in this buffer */
4743 list_T *signs = list_alloc();
4744 if (signs != NULL)
4745 {
4746 get_buffer_signs(buf, signs);
4747 dict_add_list(dict, "signs", signs);
4748 }
4749 }
4750#endif
4751
4752 return dict;
4753}
4754
4755/*
4756 * "getbufinfo()" function
4757 */
4758 static void
4759f_getbufinfo(typval_T *argvars, typval_T *rettv)
4760{
4761 buf_T *buf = NULL;
4762 buf_T *argbuf = NULL;
4763 dict_T *d;
4764 int filtered = FALSE;
4765 int sel_buflisted = FALSE;
4766 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004767 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004768
4769 if (rettv_list_alloc(rettv) != OK)
4770 return;
4771
4772 /* List of all the buffers or selected buffers */
4773 if (argvars[0].v_type == VAR_DICT)
4774 {
4775 dict_T *sel_d = argvars[0].vval.v_dict;
4776
4777 if (sel_d != NULL)
4778 {
4779 dictitem_T *di;
4780
4781 filtered = TRUE;
4782
4783 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004784 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004785 sel_buflisted = TRUE;
4786
4787 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004788 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004789 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004790
4791 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004792 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004793 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004794 }
4795 }
4796 else if (argvars[0].v_type != VAR_UNKNOWN)
4797 {
4798 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004799 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004800 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004801 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004802 --emsg_off;
4803 if (argbuf == NULL)
4804 return;
4805 }
4806
4807 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004808 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004809 {
4810 if (argbuf != NULL && argbuf != buf)
4811 continue;
4812 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004813 || (sel_buflisted && !buf->b_p_bl)
4814 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004815 continue;
4816
4817 d = get_buffer_info(buf);
4818 if (d != NULL)
4819 list_append_dict(rettv->vval.v_list, d);
4820 if (argbuf != NULL)
4821 return;
4822 }
4823}
4824
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004825/*
4826 * Get line or list of lines from buffer "buf" into "rettv".
4827 * Return a range (from start to end) of lines in rettv from the specified
4828 * buffer.
4829 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4830 */
4831 static void
4832get_buffer_lines(
4833 buf_T *buf,
4834 linenr_T start,
4835 linenr_T end,
4836 int retlist,
4837 typval_T *rettv)
4838{
4839 char_u *p;
4840
4841 rettv->v_type = VAR_STRING;
4842 rettv->vval.v_string = NULL;
4843 if (retlist && rettv_list_alloc(rettv) == FAIL)
4844 return;
4845
4846 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4847 return;
4848
4849 if (!retlist)
4850 {
4851 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4852 p = ml_get_buf(buf, start, FALSE);
4853 else
4854 p = (char_u *)"";
4855 rettv->vval.v_string = vim_strsave(p);
4856 }
4857 else
4858 {
4859 if (end < start)
4860 return;
4861
4862 if (start < 1)
4863 start = 1;
4864 if (end > buf->b_ml.ml_line_count)
4865 end = buf->b_ml.ml_line_count;
4866 while (start <= end)
4867 if (list_append_string(rettv->vval.v_list,
4868 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4869 break;
4870 }
4871}
4872
4873/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004874 * "getbufline()" function
4875 */
4876 static void
4877f_getbufline(typval_T *argvars, typval_T *rettv)
4878{
4879 linenr_T lnum;
4880 linenr_T end;
4881 buf_T *buf;
4882
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004883 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004884 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004885 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004886 --emsg_off;
4887
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004888 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004889 if (argvars[2].v_type == VAR_UNKNOWN)
4890 end = lnum;
4891 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004892 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004893
4894 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4895}
4896
4897/*
4898 * "getbufvar()" function
4899 */
4900 static void
4901f_getbufvar(typval_T *argvars, typval_T *rettv)
4902{
4903 buf_T *buf;
4904 buf_T *save_curbuf;
4905 char_u *varname;
4906 dictitem_T *v;
4907 int done = FALSE;
4908
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004909 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4910 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004911 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004912 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004913
4914 rettv->v_type = VAR_STRING;
4915 rettv->vval.v_string = NULL;
4916
4917 if (buf != NULL && varname != NULL)
4918 {
4919 /* set curbuf to be our buf, temporarily */
4920 save_curbuf = curbuf;
4921 curbuf = buf;
4922
Bram Moolenaar30567352016-08-27 21:25:44 +02004923 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004924 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004925 if (varname[1] == NUL)
4926 {
4927 /* get all buffer-local options in a dict */
4928 dict_T *opts = get_winbuf_options(TRUE);
4929
4930 if (opts != NULL)
4931 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004932 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004933 done = TRUE;
4934 }
4935 }
4936 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4937 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004938 done = TRUE;
4939 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004940 else
4941 {
4942 /* Look up the variable. */
4943 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4944 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4945 'b', varname, FALSE);
4946 if (v != NULL)
4947 {
4948 copy_tv(&v->di_tv, rettv);
4949 done = TRUE;
4950 }
4951 }
4952
4953 /* restore previous notion of curbuf */
4954 curbuf = save_curbuf;
4955 }
4956
4957 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4958 /* use the default value */
4959 copy_tv(&argvars[2], rettv);
4960
4961 --emsg_off;
4962}
4963
4964/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004965 * "getchangelist()" function
4966 */
4967 static void
4968f_getchangelist(typval_T *argvars, typval_T *rettv)
4969{
4970#ifdef FEAT_JUMPLIST
4971 buf_T *buf;
4972 int i;
4973 list_T *l;
4974 dict_T *d;
4975#endif
4976
4977 if (rettv_list_alloc(rettv) != OK)
4978 return;
4979
4980#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004981 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004982 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004983 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004984 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004985 if (buf == NULL)
4986 return;
4987
4988 l = list_alloc();
4989 if (l == NULL)
4990 return;
4991
4992 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4993 return;
4994 /*
4995 * The current window change list index tracks only the position in the
4996 * current buffer change list. For other buffers, use the change list
4997 * length as the current index.
4998 */
4999 list_append_number(rettv->vval.v_list,
5000 (varnumber_T)((buf == curwin->w_buffer)
5001 ? curwin->w_changelistidx : buf->b_changelistlen));
5002
5003 for (i = 0; i < buf->b_changelistlen; ++i)
5004 {
5005 if (buf->b_changelist[i].lnum == 0)
5006 continue;
5007 if ((d = dict_alloc()) == NULL)
5008 return;
5009 if (list_append_dict(l, d) == FAIL)
5010 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005011 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5012 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005013 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005014 }
5015#endif
5016}
5017/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005018 * "getchar()" function
5019 */
5020 static void
5021f_getchar(typval_T *argvars, typval_T *rettv)
5022{
5023 varnumber_T n;
5024 int error = FALSE;
5025
Bram Moolenaar84d93902018-09-11 20:10:20 +02005026#ifdef MESSAGE_QUEUE
5027 // vpeekc() used to check for messages, but that caused problems, invoking
5028 // a callback where it was not expected. Some plugins use getchar(1) in a
5029 // loop to await a message, therefore make sure we check for messages here.
5030 parse_queued_messages();
5031#endif
5032
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005033 /* Position the cursor. Needed after a message that ends in a space. */
5034 windgoto(msg_row, msg_col);
5035
5036 ++no_mapping;
5037 ++allow_keys;
5038 for (;;)
5039 {
5040 if (argvars[0].v_type == VAR_UNKNOWN)
5041 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005042 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005043 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005044 /* getchar(1): only check if char avail */
5045 n = vpeekc_any();
5046 else if (error || vpeekc_any() == NUL)
5047 /* illegal argument or getchar(0) and no char avail: return zero */
5048 n = 0;
5049 else
5050 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005051 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005052
5053 if (n == K_IGNORE)
5054 continue;
5055 break;
5056 }
5057 --no_mapping;
5058 --allow_keys;
5059
5060 set_vim_var_nr(VV_MOUSE_WIN, 0);
5061 set_vim_var_nr(VV_MOUSE_WINID, 0);
5062 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5063 set_vim_var_nr(VV_MOUSE_COL, 0);
5064
5065 rettv->vval.v_number = n;
5066 if (IS_SPECIAL(n) || mod_mask != 0)
5067 {
5068 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5069 int i = 0;
5070
5071 /* Turn a special key into three bytes, plus modifier. */
5072 if (mod_mask != 0)
5073 {
5074 temp[i++] = K_SPECIAL;
5075 temp[i++] = KS_MODIFIER;
5076 temp[i++] = mod_mask;
5077 }
5078 if (IS_SPECIAL(n))
5079 {
5080 temp[i++] = K_SPECIAL;
5081 temp[i++] = K_SECOND(n);
5082 temp[i++] = K_THIRD(n);
5083 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005084 else if (has_mbyte)
5085 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005086 else
5087 temp[i++] = n;
5088 temp[i++] = NUL;
5089 rettv->v_type = VAR_STRING;
5090 rettv->vval.v_string = vim_strsave(temp);
5091
5092#ifdef FEAT_MOUSE
5093 if (is_mouse_key(n))
5094 {
5095 int row = mouse_row;
5096 int col = mouse_col;
5097 win_T *win;
5098 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005100 int winnr = 1;
5101
5102 if (row >= 0 && col >= 0)
5103 {
5104 /* Find the window at the mouse coordinates and compute the
5105 * text position. */
5106 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005107 if (win == NULL)
5108 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005109 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110 for (wp = firstwin; wp != win; wp = wp->w_next)
5111 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5113 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5114 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5115 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5116 }
5117 }
5118#endif
5119 }
5120}
5121
5122/*
5123 * "getcharmod()" function
5124 */
5125 static void
5126f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5127{
5128 rettv->vval.v_number = mod_mask;
5129}
5130
5131/*
5132 * "getcharsearch()" function
5133 */
5134 static void
5135f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5136{
5137 if (rettv_dict_alloc(rettv) != FAIL)
5138 {
5139 dict_T *dict = rettv->vval.v_dict;
5140
Bram Moolenaare0be1672018-07-08 16:50:37 +02005141 dict_add_string(dict, "char", last_csearch());
5142 dict_add_number(dict, "forward", last_csearch_forward());
5143 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005144 }
5145}
5146
5147/*
5148 * "getcmdline()" function
5149 */
5150 static void
5151f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5152{
5153 rettv->v_type = VAR_STRING;
5154 rettv->vval.v_string = get_cmdline_str();
5155}
5156
5157/*
5158 * "getcmdpos()" function
5159 */
5160 static void
5161f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5162{
5163 rettv->vval.v_number = get_cmdline_pos() + 1;
5164}
5165
5166/*
5167 * "getcmdtype()" function
5168 */
5169 static void
5170f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5171{
5172 rettv->v_type = VAR_STRING;
5173 rettv->vval.v_string = alloc(2);
5174 if (rettv->vval.v_string != NULL)
5175 {
5176 rettv->vval.v_string[0] = get_cmdline_type();
5177 rettv->vval.v_string[1] = NUL;
5178 }
5179}
5180
5181/*
5182 * "getcmdwintype()" function
5183 */
5184 static void
5185f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5186{
5187 rettv->v_type = VAR_STRING;
5188 rettv->vval.v_string = NULL;
5189#ifdef FEAT_CMDWIN
5190 rettv->vval.v_string = alloc(2);
5191 if (rettv->vval.v_string != NULL)
5192 {
5193 rettv->vval.v_string[0] = cmdwin_type;
5194 rettv->vval.v_string[1] = NUL;
5195 }
5196#endif
5197}
5198
5199#if defined(FEAT_CMDL_COMPL)
5200/*
5201 * "getcompletion()" function
5202 */
5203 static void
5204f_getcompletion(typval_T *argvars, typval_T *rettv)
5205{
5206 char_u *pat;
5207 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005208 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005209 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5210 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005211
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005212 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005213 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005214
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005215 if (p_wic)
5216 options |= WILD_ICASE;
5217
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005218 /* For filtered results, 'wildignore' is used */
5219 if (!filtered)
5220 options |= WILD_KEEP_ALL;
5221
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005222 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005223 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005224 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005225 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005226 if (xpc.xp_context == EXPAND_NOTHING)
5227 {
5228 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005229 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005230 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005231 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005232 return;
5233 }
5234
5235# if defined(FEAT_MENU)
5236 if (xpc.xp_context == EXPAND_MENUS)
5237 {
5238 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5239 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5240 }
5241# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005242#ifdef FEAT_CSCOPE
5243 if (xpc.xp_context == EXPAND_CSCOPE)
5244 {
5245 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5246 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5247 }
5248#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005249#ifdef FEAT_SIGNS
5250 if (xpc.xp_context == EXPAND_SIGN)
5251 {
5252 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5253 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5254 }
5255#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005256
5257 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5258 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5259 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005260 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005261
5262 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5263
5264 for (i = 0; i < xpc.xp_numfiles; i++)
5265 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5266 }
5267 vim_free(pat);
5268 ExpandCleanup(&xpc);
5269}
5270#endif
5271
5272/*
5273 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005274 *
5275 * Return the current working directory of a window in a tab page.
5276 * First optional argument 'winnr' is the window number or -1 and the second
5277 * optional argument 'tabnr' is the tab page number.
5278 *
5279 * If no arguments are supplied, then return the directory of the current
5280 * window.
5281 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5282 * the specified window.
5283 * If 'winnr' is 0 then return the directory of the current window.
5284 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5285 * directory of the specified tab page. Otherwise return the directory of the
5286 * specified window in the specified tab page.
5287 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005288 */
5289 static void
5290f_getcwd(typval_T *argvars, typval_T *rettv)
5291{
5292 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005293 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005294 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005295 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005296
5297 rettv->v_type = VAR_STRING;
5298 rettv->vval.v_string = NULL;
5299
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005300 if (argvars[0].v_type == VAR_NUMBER
5301 && argvars[0].vval.v_number == -1
5302 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005303 global = TRUE;
5304 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005305 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005306
5307 if (wp != NULL && wp->w_localdir != NULL)
5308 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005309 else if (tp != NULL && tp->tp_localdir != NULL)
5310 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5311 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005312 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005313 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005314 rettv->vval.v_string = vim_strsave(globaldir);
5315 else
5316 {
5317 cwd = alloc(MAXPATHL);
5318 if (cwd != NULL)
5319 {
5320 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5321 rettv->vval.v_string = vim_strsave(cwd);
5322 vim_free(cwd);
5323 }
5324 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005326#ifdef BACKSLASH_IN_FILENAME
5327 if (rettv->vval.v_string != NULL)
5328 slash_adjust(rettv->vval.v_string);
5329#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005330}
5331
5332/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005333 * "getenv()" function
5334 */
5335 static void
5336f_getenv(typval_T *argvars, typval_T *rettv)
5337{
5338 int mustfree = FALSE;
5339 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5340
5341 if (p == NULL)
5342 {
5343 rettv->v_type = VAR_SPECIAL;
5344 rettv->vval.v_number = VVAL_NULL;
5345 return;
5346 }
5347 if (!mustfree)
5348 p = vim_strsave(p);
5349 rettv->vval.v_string = p;
5350 rettv->v_type = VAR_STRING;
5351}
5352
5353/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005354 * "getfontname()" function
5355 */
5356 static void
5357f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5358{
5359 rettv->v_type = VAR_STRING;
5360 rettv->vval.v_string = NULL;
5361#ifdef FEAT_GUI
5362 if (gui.in_use)
5363 {
5364 GuiFont font;
5365 char_u *name = NULL;
5366
5367 if (argvars[0].v_type == VAR_UNKNOWN)
5368 {
5369 /* Get the "Normal" font. Either the name saved by
5370 * hl_set_font_name() or from the font ID. */
5371 font = gui.norm_font;
5372 name = hl_get_font_name();
5373 }
5374 else
5375 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005376 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005377 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5378 return;
5379 font = gui_mch_get_font(name, FALSE);
5380 if (font == NOFONT)
5381 return; /* Invalid font name, return empty string. */
5382 }
5383 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5384 if (argvars[0].v_type != VAR_UNKNOWN)
5385 gui_mch_free_font(font);
5386 }
5387#endif
5388}
5389
5390/*
5391 * "getfperm({fname})" function
5392 */
5393 static void
5394f_getfperm(typval_T *argvars, typval_T *rettv)
5395{
5396 char_u *fname;
5397 stat_T st;
5398 char_u *perm = NULL;
5399 char_u flags[] = "rwx";
5400 int i;
5401
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005402 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005403
5404 rettv->v_type = VAR_STRING;
5405 if (mch_stat((char *)fname, &st) >= 0)
5406 {
5407 perm = vim_strsave((char_u *)"---------");
5408 if (perm != NULL)
5409 {
5410 for (i = 0; i < 9; i++)
5411 {
5412 if (st.st_mode & (1 << (8 - i)))
5413 perm[i] = flags[i % 3];
5414 }
5415 }
5416 }
5417 rettv->vval.v_string = perm;
5418}
5419
5420/*
5421 * "getfsize({fname})" function
5422 */
5423 static void
5424f_getfsize(typval_T *argvars, typval_T *rettv)
5425{
5426 char_u *fname;
5427 stat_T st;
5428
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005429 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005430
5431 rettv->v_type = VAR_NUMBER;
5432
5433 if (mch_stat((char *)fname, &st) >= 0)
5434 {
5435 if (mch_isdir(fname))
5436 rettv->vval.v_number = 0;
5437 else
5438 {
5439 rettv->vval.v_number = (varnumber_T)st.st_size;
5440
5441 /* non-perfect check for overflow */
5442 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5443 rettv->vval.v_number = -2;
5444 }
5445 }
5446 else
5447 rettv->vval.v_number = -1;
5448}
5449
5450/*
5451 * "getftime({fname})" function
5452 */
5453 static void
5454f_getftime(typval_T *argvars, typval_T *rettv)
5455{
5456 char_u *fname;
5457 stat_T st;
5458
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005459 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005460
5461 if (mch_stat((char *)fname, &st) >= 0)
5462 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5463 else
5464 rettv->vval.v_number = -1;
5465}
5466
5467/*
5468 * "getftype({fname})" function
5469 */
5470 static void
5471f_getftype(typval_T *argvars, typval_T *rettv)
5472{
5473 char_u *fname;
5474 stat_T st;
5475 char_u *type = NULL;
5476 char *t;
5477
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005478 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005479
5480 rettv->v_type = VAR_STRING;
5481 if (mch_lstat((char *)fname, &st) >= 0)
5482 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005483 if (S_ISREG(st.st_mode))
5484 t = "file";
5485 else if (S_ISDIR(st.st_mode))
5486 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005487 else if (S_ISLNK(st.st_mode))
5488 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005489 else if (S_ISBLK(st.st_mode))
5490 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005491 else if (S_ISCHR(st.st_mode))
5492 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005493 else if (S_ISFIFO(st.st_mode))
5494 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005495 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005496 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005497 else
5498 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005499 type = vim_strsave((char_u *)t);
5500 }
5501 rettv->vval.v_string = type;
5502}
5503
5504/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005505 * "getjumplist()" function
5506 */
5507 static void
5508f_getjumplist(typval_T *argvars, typval_T *rettv)
5509{
5510#ifdef FEAT_JUMPLIST
5511 win_T *wp;
5512 int i;
5513 list_T *l;
5514 dict_T *d;
5515#endif
5516
5517 if (rettv_list_alloc(rettv) != OK)
5518 return;
5519
5520#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005521 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005522 if (wp == NULL)
5523 return;
5524
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005525 cleanup_jumplist(wp, TRUE);
5526
Bram Moolenaar4f505882018-02-10 21:06:32 +01005527 l = list_alloc();
5528 if (l == NULL)
5529 return;
5530
5531 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5532 return;
5533 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5534
5535 for (i = 0; i < wp->w_jumplistlen; ++i)
5536 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005537 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5538 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005539 if ((d = dict_alloc()) == NULL)
5540 return;
5541 if (list_append_dict(l, d) == FAIL)
5542 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005543 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5544 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005545 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005546 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005547 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005548 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005549 }
5550#endif
5551}
5552
5553/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005554 * "getline(lnum, [end])" function
5555 */
5556 static void
5557f_getline(typval_T *argvars, typval_T *rettv)
5558{
5559 linenr_T lnum;
5560 linenr_T end;
5561 int retlist;
5562
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005563 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005564 if (argvars[1].v_type == VAR_UNKNOWN)
5565 {
5566 end = 0;
5567 retlist = FALSE;
5568 }
5569 else
5570 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005571 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005572 retlist = TRUE;
5573 }
5574
5575 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5576}
5577
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005578#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005579 static void
5580get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5581{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005582 if (what_arg->v_type == VAR_UNKNOWN)
5583 {
5584 if (rettv_list_alloc(rettv) == OK)
5585 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005586 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005587 }
5588 else
5589 {
5590 if (rettv_dict_alloc(rettv) == OK)
5591 if (is_qf || (wp != NULL))
5592 {
5593 if (what_arg->v_type == VAR_DICT)
5594 {
5595 dict_T *d = what_arg->vval.v_dict;
5596
5597 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005598 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005599 }
5600 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005601 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005602 }
5603 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005604}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005605#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005606
5607/*
5608 * "getloclist()" function
5609 */
5610 static void
5611f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5612{
5613#ifdef FEAT_QUICKFIX
5614 win_T *wp;
5615
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005616 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005617 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5618#endif
5619}
5620
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005621/*
5622 * "getmatches()" function
5623 */
5624 static void
5625f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5626{
5627#ifdef FEAT_SEARCH_EXTRA
5628 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005629 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005630 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005631 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005632
Bram Moolenaaraff74912019-03-30 18:11:49 +01005633 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5634 return;
5635
5636 cur = win->w_match_head;
5637 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005638 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005639 dict = dict_alloc();
5640 if (dict == NULL)
5641 return;
5642 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005643 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005644 /* match added with matchaddpos() */
5645 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005646 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005647 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005648 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005649 list_T *l;
5650
5651 llpos = &cur->pos.pos[i];
5652 if (llpos->lnum == 0)
5653 break;
5654 l = list_alloc();
5655 if (l == NULL)
5656 break;
5657 list_append_number(l, (varnumber_T)llpos->lnum);
5658 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005659 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005660 list_append_number(l, (varnumber_T)llpos->col);
5661 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005662 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005663 sprintf(buf, "pos%d", i + 1);
5664 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005665 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005666 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005667 else
5668 {
5669 dict_add_string(dict, "pattern", cur->pattern);
5670 }
5671 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5672 dict_add_number(dict, "priority", (long)cur->priority);
5673 dict_add_number(dict, "id", (long)cur->id);
5674# if defined(FEAT_CONCEAL)
5675 if (cur->conceal_char)
5676 {
5677 char_u buf[MB_MAXBYTES + 1];
5678
5679 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5680 dict_add_string(dict, "conceal", (char_u *)&buf);
5681 }
5682# endif
5683 list_append_dict(rettv->vval.v_list, dict);
5684 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005685 }
5686#endif
5687}
5688
5689/*
5690 * "getpid()" function
5691 */
5692 static void
5693f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5694{
5695 rettv->vval.v_number = mch_get_pid();
5696}
5697
5698 static void
5699getpos_both(
5700 typval_T *argvars,
5701 typval_T *rettv,
5702 int getcurpos)
5703{
5704 pos_T *fp;
5705 list_T *l;
5706 int fnum = -1;
5707
5708 if (rettv_list_alloc(rettv) == OK)
5709 {
5710 l = rettv->vval.v_list;
5711 if (getcurpos)
5712 fp = &curwin->w_cursor;
5713 else
5714 fp = var2fpos(&argvars[0], TRUE, &fnum);
5715 if (fnum != -1)
5716 list_append_number(l, (varnumber_T)fnum);
5717 else
5718 list_append_number(l, (varnumber_T)0);
5719 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5720 : (varnumber_T)0);
5721 list_append_number(l, (fp != NULL)
5722 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5723 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005724 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005725 (varnumber_T)0);
5726 if (getcurpos)
5727 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005728 int save_set_curswant = curwin->w_set_curswant;
5729 colnr_T save_curswant = curwin->w_curswant;
5730 colnr_T save_virtcol = curwin->w_virtcol;
5731
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005732 update_curswant();
5733 list_append_number(l, curwin->w_curswant == MAXCOL ?
5734 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005735
5736 // Do not change "curswant", as it is unexpected that a get
5737 // function has a side effect.
5738 if (save_set_curswant)
5739 {
5740 curwin->w_set_curswant = save_set_curswant;
5741 curwin->w_curswant = save_curswant;
5742 curwin->w_virtcol = save_virtcol;
5743 curwin->w_valid &= ~VALID_VIRTCOL;
5744 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005745 }
5746 }
5747 else
5748 rettv->vval.v_number = FALSE;
5749}
5750
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005751/*
5752 * "getcurpos()" function
5753 */
5754 static void
5755f_getcurpos(typval_T *argvars, typval_T *rettv)
5756{
5757 getpos_both(argvars, rettv, TRUE);
5758}
5759
5760/*
5761 * "getpos(string)" function
5762 */
5763 static void
5764f_getpos(typval_T *argvars, typval_T *rettv)
5765{
5766 getpos_both(argvars, rettv, FALSE);
5767}
5768
5769/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005770 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005771 */
5772 static void
5773f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5774{
5775#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005776 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005777#endif
5778}
5779
5780/*
5781 * "getreg()" function
5782 */
5783 static void
5784f_getreg(typval_T *argvars, typval_T *rettv)
5785{
5786 char_u *strregname;
5787 int regname;
5788 int arg2 = FALSE;
5789 int return_list = FALSE;
5790 int error = FALSE;
5791
5792 if (argvars[0].v_type != VAR_UNKNOWN)
5793 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005794 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005795 error = strregname == NULL;
5796 if (argvars[1].v_type != VAR_UNKNOWN)
5797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005798 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005799 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005800 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005801 }
5802 }
5803 else
5804 strregname = get_vim_var_str(VV_REG);
5805
5806 if (error)
5807 return;
5808
5809 regname = (strregname == NULL ? '"' : *strregname);
5810 if (regname == 0)
5811 regname = '"';
5812
5813 if (return_list)
5814 {
5815 rettv->v_type = VAR_LIST;
5816 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5817 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5818 if (rettv->vval.v_list == NULL)
5819 (void)rettv_list_alloc(rettv);
5820 else
5821 ++rettv->vval.v_list->lv_refcount;
5822 }
5823 else
5824 {
5825 rettv->v_type = VAR_STRING;
5826 rettv->vval.v_string = get_reg_contents(regname,
5827 arg2 ? GREG_EXPR_SRC : 0);
5828 }
5829}
5830
5831/*
5832 * "getregtype()" function
5833 */
5834 static void
5835f_getregtype(typval_T *argvars, typval_T *rettv)
5836{
5837 char_u *strregname;
5838 int regname;
5839 char_u buf[NUMBUFLEN + 2];
5840 long reglen = 0;
5841
5842 if (argvars[0].v_type != VAR_UNKNOWN)
5843 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005844 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005845 if (strregname == NULL) /* type error; errmsg already given */
5846 {
5847 rettv->v_type = VAR_STRING;
5848 rettv->vval.v_string = NULL;
5849 return;
5850 }
5851 }
5852 else
5853 /* Default to v:register */
5854 strregname = get_vim_var_str(VV_REG);
5855
5856 regname = (strregname == NULL ? '"' : *strregname);
5857 if (regname == 0)
5858 regname = '"';
5859
5860 buf[0] = NUL;
5861 buf[1] = NUL;
5862 switch (get_reg_type(regname, &reglen))
5863 {
5864 case MLINE: buf[0] = 'V'; break;
5865 case MCHAR: buf[0] = 'v'; break;
5866 case MBLOCK:
5867 buf[0] = Ctrl_V;
5868 sprintf((char *)buf + 1, "%ld", reglen + 1);
5869 break;
5870 }
5871 rettv->v_type = VAR_STRING;
5872 rettv->vval.v_string = vim_strsave(buf);
5873}
5874
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005875/*
5876 * Returns information (variables, options, etc.) about a tab page
5877 * as a dictionary.
5878 */
5879 static dict_T *
5880get_tabpage_info(tabpage_T *tp, int tp_idx)
5881{
5882 win_T *wp;
5883 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005884 list_T *l;
5885
5886 dict = dict_alloc();
5887 if (dict == NULL)
5888 return NULL;
5889
Bram Moolenaare0be1672018-07-08 16:50:37 +02005890 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005891
5892 l = list_alloc();
5893 if (l != NULL)
5894 {
5895 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5896 wp; wp = wp->w_next)
5897 list_append_number(l, (varnumber_T)wp->w_id);
5898 dict_add_list(dict, "windows", l);
5899 }
5900
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005901 /* Make a reference to tabpage variables */
5902 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005903
5904 return dict;
5905}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005906
5907/*
5908 * "gettabinfo()" function
5909 */
5910 static void
5911f_gettabinfo(typval_T *argvars, typval_T *rettv)
5912{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005913 tabpage_T *tp, *tparg = NULL;
5914 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005915 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005916
5917 if (rettv_list_alloc(rettv) != OK)
5918 return;
5919
5920 if (argvars[0].v_type != VAR_UNKNOWN)
5921 {
5922 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005923 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005924 if (tparg == NULL)
5925 return;
5926 }
5927
5928 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005929 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005930 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005931 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005932 if (tparg != NULL && tp != tparg)
5933 continue;
5934 d = get_tabpage_info(tp, tpnr);
5935 if (d != NULL)
5936 list_append_dict(rettv->vval.v_list, d);
5937 if (tparg != NULL)
5938 return;
5939 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005940}
5941
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005942/*
5943 * "gettabvar()" function
5944 */
5945 static void
5946f_gettabvar(typval_T *argvars, typval_T *rettv)
5947{
5948 win_T *oldcurwin;
5949 tabpage_T *tp, *oldtabpage;
5950 dictitem_T *v;
5951 char_u *varname;
5952 int done = FALSE;
5953
5954 rettv->v_type = VAR_STRING;
5955 rettv->vval.v_string = NULL;
5956
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005957 varname = tv_get_string_chk(&argvars[1]);
5958 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005959 if (tp != NULL && varname != NULL)
5960 {
5961 /* Set tp to be our tabpage, temporarily. Also set the window to the
5962 * first window in the tabpage, otherwise the window is not valid. */
5963 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005964 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5965 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005966 {
5967 /* look up the variable */
5968 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5969 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5970 if (v != NULL)
5971 {
5972 copy_tv(&v->di_tv, rettv);
5973 done = TRUE;
5974 }
5975 }
5976
5977 /* restore previous notion of curwin */
5978 restore_win(oldcurwin, oldtabpage, TRUE);
5979 }
5980
5981 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5982 copy_tv(&argvars[2], rettv);
5983}
5984
5985/*
5986 * "gettabwinvar()" function
5987 */
5988 static void
5989f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5990{
5991 getwinvar(argvars, rettv, 1);
5992}
5993
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005994/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005995 * "gettagstack()" function
5996 */
5997 static void
5998f_gettagstack(typval_T *argvars, typval_T *rettv)
5999{
6000 win_T *wp = curwin; // default is current window
6001
6002 if (rettv_dict_alloc(rettv) != OK)
6003 return;
6004
6005 if (argvars[0].v_type != VAR_UNKNOWN)
6006 {
6007 wp = find_win_by_nr_or_id(&argvars[0]);
6008 if (wp == NULL)
6009 return;
6010 }
6011
6012 get_tagstack(wp, rettv->vval.v_dict);
6013}
6014
6015/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006016 * Returns information about a window as a dictionary.
6017 */
6018 static dict_T *
6019get_win_info(win_T *wp, short tpnr, short winnr)
6020{
6021 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006022
6023 dict = dict_alloc();
6024 if (dict == NULL)
6025 return NULL;
6026
Bram Moolenaare0be1672018-07-08 16:50:37 +02006027 dict_add_number(dict, "tabnr", tpnr);
6028 dict_add_number(dict, "winnr", winnr);
6029 dict_add_number(dict, "winid", wp->w_id);
6030 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006031 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006032 dict_add_number(dict, "topline", wp->w_topline);
6033 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006034#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006035 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006036#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006037 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006038 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006039 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006040
Bram Moolenaar69905d12017-08-13 18:14:47 +02006041#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006042 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006043#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006044#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006045 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6046 dict_add_number(dict, "loclist",
6047 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006048#endif
6049
Bram Moolenaar30567352016-08-27 21:25:44 +02006050 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006051 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006052
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006053 return dict;
6054}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006055
6056/*
6057 * "getwininfo()" function
6058 */
6059 static void
6060f_getwininfo(typval_T *argvars, typval_T *rettv)
6061{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006062 tabpage_T *tp;
6063 win_T *wp = NULL, *wparg = NULL;
6064 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006065 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006066
6067 if (rettv_list_alloc(rettv) != OK)
6068 return;
6069
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006070 if (argvars[0].v_type != VAR_UNKNOWN)
6071 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006072 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006073 if (wparg == NULL)
6074 return;
6075 }
6076
6077 /* Collect information about either all the windows across all the tab
6078 * pages or one particular window.
6079 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006080 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006081 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006082 tabnr++;
6083 winnr = 0;
6084 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006085 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006086 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006087 if (wparg != NULL && wp != wparg)
6088 continue;
6089 d = get_win_info(wp, tabnr, winnr);
6090 if (d != NULL)
6091 list_append_dict(rettv->vval.v_list, d);
6092 if (wparg != NULL)
6093 /* found information about a specific window */
6094 return;
6095 }
6096 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006097}
6098
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006099/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006100 * "win_findbuf()" function
6101 */
6102 static void
6103f_win_findbuf(typval_T *argvars, typval_T *rettv)
6104{
6105 if (rettv_list_alloc(rettv) != FAIL)
6106 win_findbuf(argvars, rettv->vval.v_list);
6107}
6108
6109/*
6110 * "win_getid()" function
6111 */
6112 static void
6113f_win_getid(typval_T *argvars, typval_T *rettv)
6114{
6115 rettv->vval.v_number = win_getid(argvars);
6116}
6117
6118/*
6119 * "win_gotoid()" function
6120 */
6121 static void
6122f_win_gotoid(typval_T *argvars, typval_T *rettv)
6123{
6124 rettv->vval.v_number = win_gotoid(argvars);
6125}
6126
6127/*
6128 * "win_id2tabwin()" function
6129 */
6130 static void
6131f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6132{
6133 if (rettv_list_alloc(rettv) != FAIL)
6134 win_id2tabwin(argvars, rettv->vval.v_list);
6135}
6136
6137/*
6138 * "win_id2win()" function
6139 */
6140 static void
6141f_win_id2win(typval_T *argvars, typval_T *rettv)
6142{
6143 rettv->vval.v_number = win_id2win(argvars);
6144}
6145
6146/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006147 * "win_screenpos()" function
6148 */
6149 static void
6150f_win_screenpos(typval_T *argvars, typval_T *rettv)
6151{
6152 win_T *wp;
6153
6154 if (rettv_list_alloc(rettv) == FAIL)
6155 return;
6156
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006157 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006158 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6159 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6160}
6161
6162/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006163 * "getwinpos({timeout})" function
6164 */
6165 static void
6166f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6167{
6168 int x = -1;
6169 int y = -1;
6170
6171 if (rettv_list_alloc(rettv) == FAIL)
6172 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006173#if defined(FEAT_GUI) \
6174 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6175 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006176 {
6177 varnumber_T timeout = 100;
6178
6179 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006180 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006181
6182 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006183 }
6184#endif
6185 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6186 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6187}
6188
6189
6190/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006191 * "getwinposx()" function
6192 */
6193 static void
6194f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6195{
6196 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006197#if defined(FEAT_GUI) \
6198 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6199 || defined(MSWIN)
6200
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006201 {
6202 int x, y;
6203
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006204 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006205 rettv->vval.v_number = x;
6206 }
6207#endif
6208}
6209
6210/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006211 * "getwinposy()" function
6212 */
6213 static void
6214f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6215{
6216 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006217#if defined(FEAT_GUI) \
6218 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6219 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220 {
6221 int x, y;
6222
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006223 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006224 rettv->vval.v_number = y;
6225 }
6226#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006227}
6228
6229/*
6230 * "getwinvar()" function
6231 */
6232 static void
6233f_getwinvar(typval_T *argvars, typval_T *rettv)
6234{
6235 getwinvar(argvars, rettv, 0);
6236}
6237
6238/*
6239 * "glob()" function
6240 */
6241 static void
6242f_glob(typval_T *argvars, typval_T *rettv)
6243{
6244 int options = WILD_SILENT|WILD_USE_NL;
6245 expand_T xpc;
6246 int error = FALSE;
6247
6248 /* When the optional second argument is non-zero, don't remove matches
6249 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6250 rettv->v_type = VAR_STRING;
6251 if (argvars[1].v_type != VAR_UNKNOWN)
6252 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006253 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254 options |= WILD_KEEP_ALL;
6255 if (argvars[2].v_type != VAR_UNKNOWN)
6256 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006257 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006258 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006259 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006260 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006261 options |= WILD_ALLLINKS;
6262 }
6263 }
6264 if (!error)
6265 {
6266 ExpandInit(&xpc);
6267 xpc.xp_context = EXPAND_FILES;
6268 if (p_wic)
6269 options += WILD_ICASE;
6270 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006271 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006272 NULL, options, WILD_ALL);
6273 else if (rettv_list_alloc(rettv) != FAIL)
6274 {
6275 int i;
6276
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006277 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006278 NULL, options, WILD_ALL_KEEP);
6279 for (i = 0; i < xpc.xp_numfiles; i++)
6280 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6281
6282 ExpandCleanup(&xpc);
6283 }
6284 }
6285 else
6286 rettv->vval.v_string = NULL;
6287}
6288
6289/*
6290 * "globpath()" function
6291 */
6292 static void
6293f_globpath(typval_T *argvars, typval_T *rettv)
6294{
6295 int flags = 0;
6296 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006297 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006298 int error = FALSE;
6299 garray_T ga;
6300 int i;
6301
6302 /* When the optional second argument is non-zero, don't remove matches
6303 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6304 rettv->v_type = VAR_STRING;
6305 if (argvars[2].v_type != VAR_UNKNOWN)
6306 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006307 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308 flags |= WILD_KEEP_ALL;
6309 if (argvars[3].v_type != VAR_UNKNOWN)
6310 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006311 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006312 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006313 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006314 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006315 flags |= WILD_ALLLINKS;
6316 }
6317 }
6318 if (file != NULL && !error)
6319 {
6320 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006321 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006322 if (rettv->v_type == VAR_STRING)
6323 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6324 else if (rettv_list_alloc(rettv) != FAIL)
6325 for (i = 0; i < ga.ga_len; ++i)
6326 list_append_string(rettv->vval.v_list,
6327 ((char_u **)(ga.ga_data))[i], -1);
6328 ga_clear_strings(&ga);
6329 }
6330 else
6331 rettv->vval.v_string = NULL;
6332}
6333
6334/*
6335 * "glob2regpat()" function
6336 */
6337 static void
6338f_glob2regpat(typval_T *argvars, typval_T *rettv)
6339{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006340 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006341
6342 rettv->v_type = VAR_STRING;
6343 rettv->vval.v_string = (pat == NULL)
6344 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6345}
6346
6347/* for VIM_VERSION_ defines */
6348#include "version.h"
6349
6350/*
6351 * "has()" function
6352 */
6353 static void
6354f_has(typval_T *argvars, typval_T *rettv)
6355{
6356 int i;
6357 char_u *name;
6358 int n = FALSE;
6359 static char *(has_list[]) =
6360 {
6361#ifdef AMIGA
6362 "amiga",
6363# ifdef FEAT_ARP
6364 "arp",
6365# endif
6366#endif
6367#ifdef __BEOS__
6368 "beos",
6369#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006370#if defined(BSD) && !defined(MACOS_X)
6371 "bsd",
6372#endif
6373#ifdef hpux
6374 "hpux",
6375#endif
6376#ifdef __linux__
6377 "linux",
6378#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006379#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006380 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6381 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006382# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006383 "macunix", /* Mac OS X, with the darwin feature */
6384 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006385# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006386#endif
6387#ifdef __QNX__
6388 "qnx",
6389#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006390#ifdef SUN_SYSTEM
6391 "sun",
6392#else
6393 "moon",
6394#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006395#ifdef UNIX
6396 "unix",
6397#endif
6398#ifdef VMS
6399 "vms",
6400#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006401#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006402 "win32",
6403#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006404#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006405 "win32unix",
6406#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006407#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408 "win64",
6409#endif
6410#ifdef EBCDIC
6411 "ebcdic",
6412#endif
6413#ifndef CASE_INSENSITIVE_FILENAME
6414 "fname_case",
6415#endif
6416#ifdef HAVE_ACL
6417 "acl",
6418#endif
6419#ifdef FEAT_ARABIC
6420 "arabic",
6421#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006422 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006423#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006424 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006425#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006426#ifdef FEAT_AUTOSERVERNAME
6427 "autoservername",
6428#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006429#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006430 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006431# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006432 "balloon_multiline",
6433# endif
6434#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006435#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006436 "balloon_eval_term",
6437#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006438#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6439 "builtin_terms",
6440# ifdef ALL_BUILTIN_TCAPS
6441 "all_builtin_terms",
6442# endif
6443#endif
6444#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006445 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006446 || defined(FEAT_GUI_MOTIF))
6447 "browsefilter",
6448#endif
6449#ifdef FEAT_BYTEOFF
6450 "byte_offset",
6451#endif
6452#ifdef FEAT_JOB_CHANNEL
6453 "channel",
6454#endif
6455#ifdef FEAT_CINDENT
6456 "cindent",
6457#endif
6458#ifdef FEAT_CLIENTSERVER
6459 "clientserver",
6460#endif
6461#ifdef FEAT_CLIPBOARD
6462 "clipboard",
6463#endif
6464#ifdef FEAT_CMDL_COMPL
6465 "cmdline_compl",
6466#endif
6467#ifdef FEAT_CMDHIST
6468 "cmdline_hist",
6469#endif
6470#ifdef FEAT_COMMENTS
6471 "comments",
6472#endif
6473#ifdef FEAT_CONCEAL
6474 "conceal",
6475#endif
6476#ifdef FEAT_CRYPT
6477 "cryptv",
6478 "crypt-blowfish",
6479 "crypt-blowfish2",
6480#endif
6481#ifdef FEAT_CSCOPE
6482 "cscope",
6483#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006484 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006485#ifdef CURSOR_SHAPE
6486 "cursorshape",
6487#endif
6488#ifdef DEBUG
6489 "debug",
6490#endif
6491#ifdef FEAT_CON_DIALOG
6492 "dialog_con",
6493#endif
6494#ifdef FEAT_GUI_DIALOG
6495 "dialog_gui",
6496#endif
6497#ifdef FEAT_DIFF
6498 "diff",
6499#endif
6500#ifdef FEAT_DIGRAPHS
6501 "digraphs",
6502#endif
6503#ifdef FEAT_DIRECTX
6504 "directx",
6505#endif
6506#ifdef FEAT_DND
6507 "dnd",
6508#endif
6509#ifdef FEAT_EMACS_TAGS
6510 "emacs_tags",
6511#endif
6512 "eval", /* always present, of course! */
6513 "ex_extra", /* graduated feature */
6514#ifdef FEAT_SEARCH_EXTRA
6515 "extra_search",
6516#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006517#ifdef FEAT_SEARCHPATH
6518 "file_in_path",
6519#endif
6520#ifdef FEAT_FILTERPIPE
6521 "filterpipe",
6522#endif
6523#ifdef FEAT_FIND_ID
6524 "find_in_path",
6525#endif
6526#ifdef FEAT_FLOAT
6527 "float",
6528#endif
6529#ifdef FEAT_FOLDING
6530 "folding",
6531#endif
6532#ifdef FEAT_FOOTER
6533 "footer",
6534#endif
6535#if !defined(USE_SYSTEM) && defined(UNIX)
6536 "fork",
6537#endif
6538#ifdef FEAT_GETTEXT
6539 "gettext",
6540#endif
6541#ifdef FEAT_GUI
6542 "gui",
6543#endif
6544#ifdef FEAT_GUI_ATHENA
6545# ifdef FEAT_GUI_NEXTAW
6546 "gui_neXtaw",
6547# else
6548 "gui_athena",
6549# endif
6550#endif
6551#ifdef FEAT_GUI_GTK
6552 "gui_gtk",
6553# ifdef USE_GTK3
6554 "gui_gtk3",
6555# else
6556 "gui_gtk2",
6557# endif
6558#endif
6559#ifdef FEAT_GUI_GNOME
6560 "gui_gnome",
6561#endif
6562#ifdef FEAT_GUI_MAC
6563 "gui_mac",
6564#endif
6565#ifdef FEAT_GUI_MOTIF
6566 "gui_motif",
6567#endif
6568#ifdef FEAT_GUI_PHOTON
6569 "gui_photon",
6570#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006571#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006572 "gui_win32",
6573#endif
6574#ifdef FEAT_HANGULIN
6575 "hangul_input",
6576#endif
6577#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6578 "iconv",
6579#endif
6580#ifdef FEAT_INS_EXPAND
6581 "insert_expand",
6582#endif
6583#ifdef FEAT_JOB_CHANNEL
6584 "job",
6585#endif
6586#ifdef FEAT_JUMPLIST
6587 "jumplist",
6588#endif
6589#ifdef FEAT_KEYMAP
6590 "keymap",
6591#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006592 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006593#ifdef FEAT_LANGMAP
6594 "langmap",
6595#endif
6596#ifdef FEAT_LIBCALL
6597 "libcall",
6598#endif
6599#ifdef FEAT_LINEBREAK
6600 "linebreak",
6601#endif
6602#ifdef FEAT_LISP
6603 "lispindent",
6604#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006605 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006606#ifdef FEAT_LOCALMAP
6607 "localmap",
6608#endif
6609#ifdef FEAT_LUA
6610# ifndef DYNAMIC_LUA
6611 "lua",
6612# endif
6613#endif
6614#ifdef FEAT_MENU
6615 "menu",
6616#endif
6617#ifdef FEAT_SESSION
6618 "mksession",
6619#endif
6620#ifdef FEAT_MODIFY_FNAME
6621 "modify_fname",
6622#endif
6623#ifdef FEAT_MOUSE
6624 "mouse",
6625#endif
6626#ifdef FEAT_MOUSESHAPE
6627 "mouseshape",
6628#endif
6629#if defined(UNIX) || defined(VMS)
6630# ifdef FEAT_MOUSE_DEC
6631 "mouse_dec",
6632# endif
6633# ifdef FEAT_MOUSE_GPM
6634 "mouse_gpm",
6635# endif
6636# ifdef FEAT_MOUSE_JSB
6637 "mouse_jsbterm",
6638# endif
6639# ifdef FEAT_MOUSE_NET
6640 "mouse_netterm",
6641# endif
6642# ifdef FEAT_MOUSE_PTERM
6643 "mouse_pterm",
6644# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006645# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006646 "mouse_sgr",
6647# endif
6648# ifdef FEAT_SYSMOUSE
6649 "mouse_sysmouse",
6650# endif
6651# ifdef FEAT_MOUSE_URXVT
6652 "mouse_urxvt",
6653# endif
6654# ifdef FEAT_MOUSE_XTERM
6655 "mouse_xterm",
6656# endif
6657#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006658 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006659#ifdef FEAT_MBYTE_IME
6660 "multi_byte_ime",
6661#endif
6662#ifdef FEAT_MULTI_LANG
6663 "multi_lang",
6664#endif
6665#ifdef FEAT_MZSCHEME
6666#ifndef DYNAMIC_MZSCHEME
6667 "mzscheme",
6668#endif
6669#endif
6670#ifdef FEAT_NUM64
6671 "num64",
6672#endif
6673#ifdef FEAT_OLE
6674 "ole",
6675#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006676#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006677 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006678#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006679#ifdef FEAT_PATH_EXTRA
6680 "path_extra",
6681#endif
6682#ifdef FEAT_PERL
6683#ifndef DYNAMIC_PERL
6684 "perl",
6685#endif
6686#endif
6687#ifdef FEAT_PERSISTENT_UNDO
6688 "persistent_undo",
6689#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006690#if defined(FEAT_PYTHON)
6691 "python_compiled",
6692# if defined(DYNAMIC_PYTHON)
6693 "python_dynamic",
6694# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006696 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006697# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006698#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006699#if defined(FEAT_PYTHON3)
6700 "python3_compiled",
6701# if defined(DYNAMIC_PYTHON3)
6702 "python3_dynamic",
6703# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006704 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006705 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006706# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006707#endif
6708#ifdef FEAT_POSTSCRIPT
6709 "postscript",
6710#endif
6711#ifdef FEAT_PRINTER
6712 "printer",
6713#endif
6714#ifdef FEAT_PROFILE
6715 "profile",
6716#endif
6717#ifdef FEAT_RELTIME
6718 "reltime",
6719#endif
6720#ifdef FEAT_QUICKFIX
6721 "quickfix",
6722#endif
6723#ifdef FEAT_RIGHTLEFT
6724 "rightleft",
6725#endif
6726#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6727 "ruby",
6728#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006730#ifdef FEAT_CMDL_INFO
6731 "showcmd",
6732 "cmdline_info",
6733#endif
6734#ifdef FEAT_SIGNS
6735 "signs",
6736#endif
6737#ifdef FEAT_SMARTINDENT
6738 "smartindent",
6739#endif
6740#ifdef STARTUPTIME
6741 "startuptime",
6742#endif
6743#ifdef FEAT_STL_OPT
6744 "statusline",
6745#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006746#ifdef FEAT_NETBEANS_INTG
6747 "netbeans_intg",
6748#endif
6749#ifdef FEAT_SPELL
6750 "spell",
6751#endif
6752#ifdef FEAT_SYN_HL
6753 "syntax",
6754#endif
6755#if defined(USE_SYSTEM) || !defined(UNIX)
6756 "system",
6757#endif
6758#ifdef FEAT_TAG_BINS
6759 "tag_binary",
6760#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006761#ifdef FEAT_TCL
6762# ifndef DYNAMIC_TCL
6763 "tcl",
6764# endif
6765#endif
6766#ifdef FEAT_TERMGUICOLORS
6767 "termguicolors",
6768#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006769#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006770 "terminal",
6771#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006772#ifdef TERMINFO
6773 "terminfo",
6774#endif
6775#ifdef FEAT_TERMRESPONSE
6776 "termresponse",
6777#endif
6778#ifdef FEAT_TEXTOBJ
6779 "textobjects",
6780#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006781#ifdef FEAT_TEXT_PROP
6782 "textprop",
6783#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006784#ifdef HAVE_TGETENT
6785 "tgetent",
6786#endif
6787#ifdef FEAT_TIMERS
6788 "timers",
6789#endif
6790#ifdef FEAT_TITLE
6791 "title",
6792#endif
6793#ifdef FEAT_TOOLBAR
6794 "toolbar",
6795#endif
6796#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6797 "unnamedplus",
6798#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006799 "user-commands", /* was accidentally included in 5.4 */
6800 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006801#ifdef FEAT_VARTABS
6802 "vartabs",
6803#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006804 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006805#ifdef FEAT_VIMINFO
6806 "viminfo",
6807#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006808 "vimscript-1",
6809 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006810 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006811 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006813 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006814 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006815#ifdef FEAT_VTP
6816 "vtp",
6817#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006818#ifdef FEAT_WILDIGN
6819 "wildignore",
6820#endif
6821#ifdef FEAT_WILDMENU
6822 "wildmenu",
6823#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006824 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006825#ifdef FEAT_WAK
6826 "winaltkeys",
6827#endif
6828#ifdef FEAT_WRITEBACKUP
6829 "writebackup",
6830#endif
6831#ifdef FEAT_XIM
6832 "xim",
6833#endif
6834#ifdef FEAT_XFONTSET
6835 "xfontset",
6836#endif
6837#ifdef FEAT_XPM_W32
6838 "xpm",
6839 "xpm_w32", /* for backward compatibility */
6840#else
6841# if defined(HAVE_XPM)
6842 "xpm",
6843# endif
6844#endif
6845#ifdef USE_XSMP
6846 "xsmp",
6847#endif
6848#ifdef USE_XSMP_INTERACT
6849 "xsmp_interact",
6850#endif
6851#ifdef FEAT_XCLIPBOARD
6852 "xterm_clipboard",
6853#endif
6854#ifdef FEAT_XTERM_SAVE
6855 "xterm_save",
6856#endif
6857#if defined(UNIX) && defined(FEAT_X11)
6858 "X11",
6859#endif
6860 NULL
6861 };
6862
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006863 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006864 for (i = 0; has_list[i] != NULL; ++i)
6865 if (STRICMP(name, has_list[i]) == 0)
6866 {
6867 n = TRUE;
6868 break;
6869 }
6870
6871 if (n == FALSE)
6872 {
6873 if (STRNICMP(name, "patch", 5) == 0)
6874 {
6875 if (name[5] == '-'
6876 && STRLEN(name) >= 11
6877 && vim_isdigit(name[6])
6878 && vim_isdigit(name[8])
6879 && vim_isdigit(name[10]))
6880 {
6881 int major = atoi((char *)name + 6);
6882 int minor = atoi((char *)name + 8);
6883
6884 /* Expect "patch-9.9.01234". */
6885 n = (major < VIM_VERSION_MAJOR
6886 || (major == VIM_VERSION_MAJOR
6887 && (minor < VIM_VERSION_MINOR
6888 || (minor == VIM_VERSION_MINOR
6889 && has_patch(atoi((char *)name + 10))))));
6890 }
6891 else
6892 n = has_patch(atoi((char *)name + 5));
6893 }
6894 else if (STRICMP(name, "vim_starting") == 0)
6895 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006896 else if (STRICMP(name, "ttyin") == 0)
6897 n = mch_input_isatty();
6898 else if (STRICMP(name, "ttyout") == 0)
6899 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 else if (STRICMP(name, "multi_byte_encoding") == 0)
6901 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006902#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006903 else if (STRICMP(name, "balloon_multiline") == 0)
6904 n = multiline_balloon_available();
6905#endif
6906#ifdef DYNAMIC_TCL
6907 else if (STRICMP(name, "tcl") == 0)
6908 n = tcl_enabled(FALSE);
6909#endif
6910#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6911 else if (STRICMP(name, "iconv") == 0)
6912 n = iconv_enabled(FALSE);
6913#endif
6914#ifdef DYNAMIC_LUA
6915 else if (STRICMP(name, "lua") == 0)
6916 n = lua_enabled(FALSE);
6917#endif
6918#ifdef DYNAMIC_MZSCHEME
6919 else if (STRICMP(name, "mzscheme") == 0)
6920 n = mzscheme_enabled(FALSE);
6921#endif
6922#ifdef DYNAMIC_RUBY
6923 else if (STRICMP(name, "ruby") == 0)
6924 n = ruby_enabled(FALSE);
6925#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006926#ifdef DYNAMIC_PYTHON
6927 else if (STRICMP(name, "python") == 0)
6928 n = python_enabled(FALSE);
6929#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006930#ifdef DYNAMIC_PYTHON3
6931 else if (STRICMP(name, "python3") == 0)
6932 n = python3_enabled(FALSE);
6933#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006934#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6935 else if (STRICMP(name, "pythonx") == 0)
6936 {
6937# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6938 if (p_pyx == 0)
6939 n = python3_enabled(FALSE) || python_enabled(FALSE);
6940 else if (p_pyx == 3)
6941 n = python3_enabled(FALSE);
6942 else if (p_pyx == 2)
6943 n = python_enabled(FALSE);
6944# elif defined(DYNAMIC_PYTHON)
6945 n = python_enabled(FALSE);
6946# elif defined(DYNAMIC_PYTHON3)
6947 n = python3_enabled(FALSE);
6948# endif
6949 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006950#endif
6951#ifdef DYNAMIC_PERL
6952 else if (STRICMP(name, "perl") == 0)
6953 n = perl_enabled(FALSE);
6954#endif
6955#ifdef FEAT_GUI
6956 else if (STRICMP(name, "gui_running") == 0)
6957 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006958# ifdef FEAT_BROWSE
6959 else if (STRICMP(name, "browse") == 0)
6960 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6961# endif
6962#endif
6963#ifdef FEAT_SYN_HL
6964 else if (STRICMP(name, "syntax_items") == 0)
6965 n = syntax_present(curwin);
6966#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006967#ifdef FEAT_VTP
6968 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006969 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006970#endif
6971#ifdef FEAT_NETBEANS_INTG
6972 else if (STRICMP(name, "netbeans_enabled") == 0)
6973 n = netbeans_active();
6974#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006975#ifdef FEAT_MOUSE_GPM
6976 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6977 n = gpm_enabled();
6978#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006979#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006980 else if (STRICMP(name, "terminal") == 0)
6981 n = terminal_enabled();
6982#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006983#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006984 else if (STRICMP(name, "conpty") == 0)
6985 n = use_conpty();
6986#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006987 }
6988
6989 rettv->vval.v_number = n;
6990}
6991
6992/*
6993 * "has_key()" function
6994 */
6995 static void
6996f_has_key(typval_T *argvars, typval_T *rettv)
6997{
6998 if (argvars[0].v_type != VAR_DICT)
6999 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007000 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007001 return;
7002 }
7003 if (argvars[0].vval.v_dict == NULL)
7004 return;
7005
7006 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007007 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007008}
7009
7010/*
7011 * "haslocaldir()" function
7012 */
7013 static void
7014f_haslocaldir(typval_T *argvars, typval_T *rettv)
7015{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007016 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007017 win_T *wp = NULL;
7018
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007019 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7020
7021 // Check for window-local and tab-local directories
7022 if (wp != NULL && wp->w_localdir != NULL)
7023 rettv->vval.v_number = 1;
7024 else if (tp != NULL && tp->tp_localdir != NULL)
7025 rettv->vval.v_number = 2;
7026 else
7027 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028}
7029
7030/*
7031 * "hasmapto()" function
7032 */
7033 static void
7034f_hasmapto(typval_T *argvars, typval_T *rettv)
7035{
7036 char_u *name;
7037 char_u *mode;
7038 char_u buf[NUMBUFLEN];
7039 int abbr = FALSE;
7040
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007041 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042 if (argvars[1].v_type == VAR_UNKNOWN)
7043 mode = (char_u *)"nvo";
7044 else
7045 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007046 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007047 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007048 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007049 }
7050
7051 if (map_to_exists(name, mode, abbr))
7052 rettv->vval.v_number = TRUE;
7053 else
7054 rettv->vval.v_number = FALSE;
7055}
7056
7057/*
7058 * "histadd()" function
7059 */
7060 static void
7061f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7062{
7063#ifdef FEAT_CMDHIST
7064 int histype;
7065 char_u *str;
7066 char_u buf[NUMBUFLEN];
7067#endif
7068
7069 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007070 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007071 return;
7072#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007073 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007074 histype = str != NULL ? get_histtype(str) : -1;
7075 if (histype >= 0)
7076 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007077 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007078 if (*str != NUL)
7079 {
7080 init_history();
7081 add_to_history(histype, str, FALSE, NUL);
7082 rettv->vval.v_number = TRUE;
7083 return;
7084 }
7085 }
7086#endif
7087}
7088
7089/*
7090 * "histdel()" function
7091 */
7092 static void
7093f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7094{
7095#ifdef FEAT_CMDHIST
7096 int n;
7097 char_u buf[NUMBUFLEN];
7098 char_u *str;
7099
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007100 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007101 if (str == NULL)
7102 n = 0;
7103 else if (argvars[1].v_type == VAR_UNKNOWN)
7104 /* only one argument: clear entire history */
7105 n = clr_history(get_histtype(str));
7106 else if (argvars[1].v_type == VAR_NUMBER)
7107 /* index given: remove that entry */
7108 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007109 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007110 else
7111 /* string given: remove all matching entries */
7112 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007113 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007114 rettv->vval.v_number = n;
7115#endif
7116}
7117
7118/*
7119 * "histget()" function
7120 */
7121 static void
7122f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7123{
7124#ifdef FEAT_CMDHIST
7125 int type;
7126 int idx;
7127 char_u *str;
7128
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007129 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007130 if (str == NULL)
7131 rettv->vval.v_string = NULL;
7132 else
7133 {
7134 type = get_histtype(str);
7135 if (argvars[1].v_type == VAR_UNKNOWN)
7136 idx = get_history_idx(type);
7137 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007138 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007139 /* -1 on type error */
7140 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7141 }
7142#else
7143 rettv->vval.v_string = NULL;
7144#endif
7145 rettv->v_type = VAR_STRING;
7146}
7147
7148/*
7149 * "histnr()" function
7150 */
7151 static void
7152f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7153{
7154 int i;
7155
7156#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007157 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007158
7159 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7160 if (i >= HIST_CMD && i < HIST_COUNT)
7161 i = get_history_idx(i);
7162 else
7163#endif
7164 i = -1;
7165 rettv->vval.v_number = i;
7166}
7167
7168/*
7169 * "highlightID(name)" function
7170 */
7171 static void
7172f_hlID(typval_T *argvars, typval_T *rettv)
7173{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007174 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007175}
7176
7177/*
7178 * "highlight_exists()" function
7179 */
7180 static void
7181f_hlexists(typval_T *argvars, typval_T *rettv)
7182{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007183 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007184}
7185
7186/*
7187 * "hostname()" function
7188 */
7189 static void
7190f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7191{
7192 char_u hostname[256];
7193
7194 mch_get_host_name(hostname, 256);
7195 rettv->v_type = VAR_STRING;
7196 rettv->vval.v_string = vim_strsave(hostname);
7197}
7198
7199/*
7200 * iconv() function
7201 */
7202 static void
7203f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7204{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007205 char_u buf1[NUMBUFLEN];
7206 char_u buf2[NUMBUFLEN];
7207 char_u *from, *to, *str;
7208 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007209
7210 rettv->v_type = VAR_STRING;
7211 rettv->vval.v_string = NULL;
7212
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007213 str = tv_get_string(&argvars[0]);
7214 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7215 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007216 vimconv.vc_type = CONV_NONE;
7217 convert_setup(&vimconv, from, to);
7218
7219 /* If the encodings are equal, no conversion needed. */
7220 if (vimconv.vc_type == CONV_NONE)
7221 rettv->vval.v_string = vim_strsave(str);
7222 else
7223 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7224
7225 convert_setup(&vimconv, NULL, NULL);
7226 vim_free(from);
7227 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007228}
7229
7230/*
7231 * "indent()" function
7232 */
7233 static void
7234f_indent(typval_T *argvars, typval_T *rettv)
7235{
7236 linenr_T lnum;
7237
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007238 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007239 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7240 rettv->vval.v_number = get_indent_lnum(lnum);
7241 else
7242 rettv->vval.v_number = -1;
7243}
7244
7245/*
7246 * "index()" function
7247 */
7248 static void
7249f_index(typval_T *argvars, typval_T *rettv)
7250{
7251 list_T *l;
7252 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007253 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007254 long idx = 0;
7255 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007256 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007257
7258 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007259 if (argvars[0].v_type == VAR_BLOB)
7260 {
7261 typval_T tv;
7262 int start = 0;
7263
7264 if (argvars[2].v_type != VAR_UNKNOWN)
7265 {
7266 start = tv_get_number_chk(&argvars[2], &error);
7267 if (error)
7268 return;
7269 }
7270 b = argvars[0].vval.v_blob;
7271 if (b == NULL)
7272 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007273 if (start < 0)
7274 {
7275 start = blob_len(b) + start;
7276 if (start < 0)
7277 start = 0;
7278 }
7279
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007280 for (idx = start; idx < blob_len(b); ++idx)
7281 {
7282 tv.v_type = VAR_NUMBER;
7283 tv.vval.v_number = blob_get(b, idx);
7284 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7285 {
7286 rettv->vval.v_number = idx;
7287 return;
7288 }
7289 }
7290 return;
7291 }
7292 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007294 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007295 return;
7296 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007297
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007298 l = argvars[0].vval.v_list;
7299 if (l != NULL)
7300 {
7301 item = l->lv_first;
7302 if (argvars[2].v_type != VAR_UNKNOWN)
7303 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007304 /* Start at specified item. Use the cached index that list_find()
7305 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007306 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007307 idx = l->lv_idx;
7308 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007309 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007310 if (error)
7311 item = NULL;
7312 }
7313
7314 for ( ; item != NULL; item = item->li_next, ++idx)
7315 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7316 {
7317 rettv->vval.v_number = idx;
7318 break;
7319 }
7320 }
7321}
7322
7323static int inputsecret_flag = 0;
7324
7325/*
7326 * "input()" function
7327 * Also handles inputsecret() when inputsecret is set.
7328 */
7329 static void
7330f_input(typval_T *argvars, typval_T *rettv)
7331{
7332 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7333}
7334
7335/*
7336 * "inputdialog()" function
7337 */
7338 static void
7339f_inputdialog(typval_T *argvars, typval_T *rettv)
7340{
7341#if defined(FEAT_GUI_TEXTDIALOG)
7342 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7343 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7344 {
7345 char_u *message;
7346 char_u buf[NUMBUFLEN];
7347 char_u *defstr = (char_u *)"";
7348
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007349 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007350 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007351 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007352 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7353 else
7354 IObuff[0] = NUL;
7355 if (message != NULL && defstr != NULL
7356 && do_dialog(VIM_QUESTION, NULL, message,
7357 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7358 rettv->vval.v_string = vim_strsave(IObuff);
7359 else
7360 {
7361 if (message != NULL && defstr != NULL
7362 && argvars[1].v_type != VAR_UNKNOWN
7363 && argvars[2].v_type != VAR_UNKNOWN)
7364 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007365 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007366 else
7367 rettv->vval.v_string = NULL;
7368 }
7369 rettv->v_type = VAR_STRING;
7370 }
7371 else
7372#endif
7373 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7374}
7375
7376/*
7377 * "inputlist()" function
7378 */
7379 static void
7380f_inputlist(typval_T *argvars, typval_T *rettv)
7381{
7382 listitem_T *li;
7383 int selected;
7384 int mouse_used;
7385
7386#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007387 /* While starting up, there is no place to enter text. When running tests
7388 * with --not-a-term we assume feedkeys() will be used. */
7389 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007390 return;
7391#endif
7392 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7393 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007394 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007395 return;
7396 }
7397
7398 msg_start();
7399 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7400 lines_left = Rows; /* avoid more prompt */
7401 msg_scroll = TRUE;
7402 msg_clr_eos();
7403
7404 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7405 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007406 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007407 msg_putchar('\n');
7408 }
7409
7410 /* Ask for choice. */
7411 selected = prompt_for_number(&mouse_used);
7412 if (mouse_used)
7413 selected -= lines_left;
7414
7415 rettv->vval.v_number = selected;
7416}
7417
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007418static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7419
7420/*
7421 * "inputrestore()" function
7422 */
7423 static void
7424f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7425{
7426 if (ga_userinput.ga_len > 0)
7427 {
7428 --ga_userinput.ga_len;
7429 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7430 + ga_userinput.ga_len);
7431 /* default return is zero == OK */
7432 }
7433 else if (p_verbose > 1)
7434 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007435 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436 rettv->vval.v_number = 1; /* Failed */
7437 }
7438}
7439
7440/*
7441 * "inputsave()" function
7442 */
7443 static void
7444f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7445{
7446 /* Add an entry to the stack of typeahead storage. */
7447 if (ga_grow(&ga_userinput, 1) == OK)
7448 {
7449 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7450 + ga_userinput.ga_len);
7451 ++ga_userinput.ga_len;
7452 /* default return is zero == OK */
7453 }
7454 else
7455 rettv->vval.v_number = 1; /* Failed */
7456}
7457
7458/*
7459 * "inputsecret()" function
7460 */
7461 static void
7462f_inputsecret(typval_T *argvars, typval_T *rettv)
7463{
7464 ++cmdline_star;
7465 ++inputsecret_flag;
7466 f_input(argvars, rettv);
7467 --cmdline_star;
7468 --inputsecret_flag;
7469}
7470
7471/*
7472 * "insert()" function
7473 */
7474 static void
7475f_insert(typval_T *argvars, typval_T *rettv)
7476{
7477 long before = 0;
7478 listitem_T *item;
7479 list_T *l;
7480 int error = FALSE;
7481
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007482 if (argvars[0].v_type == VAR_BLOB)
7483 {
7484 int val, len;
7485 char_u *p;
7486
7487 len = blob_len(argvars[0].vval.v_blob);
7488 if (argvars[2].v_type != VAR_UNKNOWN)
7489 {
7490 before = (long)tv_get_number_chk(&argvars[2], &error);
7491 if (error)
7492 return; // type error; errmsg already given
7493 if (before < 0 || before > len)
7494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007495 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007496 return;
7497 }
7498 }
7499 val = tv_get_number_chk(&argvars[1], &error);
7500 if (error)
7501 return;
7502 if (val < 0 || val > 255)
7503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007504 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007505 return;
7506 }
7507
7508 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7509 return;
7510 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7511 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7512 *(p + before) = val;
7513 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7514
7515 copy_tv(&argvars[0], rettv);
7516 }
7517 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007518 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007519 else if ((l = argvars[0].vval.v_list) != NULL
7520 && !var_check_lock(l->lv_lock,
7521 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007522 {
7523 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007524 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007525 if (error)
7526 return; /* type error; errmsg already given */
7527
7528 if (before == l->lv_len)
7529 item = NULL;
7530 else
7531 {
7532 item = list_find(l, before);
7533 if (item == NULL)
7534 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007535 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007536 l = NULL;
7537 }
7538 }
7539 if (l != NULL)
7540 {
7541 list_insert_tv(l, &argvars[1], item);
7542 copy_tv(&argvars[0], rettv);
7543 }
7544 }
7545}
7546
7547/*
7548 * "invert(expr)" function
7549 */
7550 static void
7551f_invert(typval_T *argvars, typval_T *rettv)
7552{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007553 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007554}
7555
7556/*
7557 * "isdirectory()" function
7558 */
7559 static void
7560f_isdirectory(typval_T *argvars, typval_T *rettv)
7561{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007562 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007563}
7564
7565/*
7566 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7567 * or it refers to a List or Dictionary that is locked.
7568 */
7569 static int
7570tv_islocked(typval_T *tv)
7571{
7572 return (tv->v_lock & VAR_LOCKED)
7573 || (tv->v_type == VAR_LIST
7574 && tv->vval.v_list != NULL
7575 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7576 || (tv->v_type == VAR_DICT
7577 && tv->vval.v_dict != NULL
7578 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7579}
7580
7581/*
7582 * "islocked()" function
7583 */
7584 static void
7585f_islocked(typval_T *argvars, typval_T *rettv)
7586{
7587 lval_T lv;
7588 char_u *end;
7589 dictitem_T *di;
7590
7591 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007592 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007593 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007594 if (end != NULL && lv.ll_name != NULL)
7595 {
7596 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007597 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598 else
7599 {
7600 if (lv.ll_tv == NULL)
7601 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007602 di = find_var(lv.ll_name, NULL, TRUE);
7603 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007604 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007605 /* Consider a variable locked when:
7606 * 1. the variable itself is locked
7607 * 2. the value of the variable is locked.
7608 * 3. the List or Dict value is locked.
7609 */
7610 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7611 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007612 }
7613 }
7614 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007615 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007616 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007617 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007618 else if (lv.ll_list != NULL)
7619 /* List item. */
7620 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7621 else
7622 /* Dictionary item. */
7623 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7624 }
7625 }
7626
7627 clear_lval(&lv);
7628}
7629
7630#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7631/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007632 * "isinf()" function
7633 */
7634 static void
7635f_isinf(typval_T *argvars, typval_T *rettv)
7636{
7637 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7638 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7639}
7640
7641/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007642 * "isnan()" function
7643 */
7644 static void
7645f_isnan(typval_T *argvars, typval_T *rettv)
7646{
7647 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7648 && isnan(argvars[0].vval.v_float);
7649}
7650#endif
7651
7652/*
7653 * "items(dict)" function
7654 */
7655 static void
7656f_items(typval_T *argvars, typval_T *rettv)
7657{
7658 dict_list(argvars, rettv, 2);
7659}
7660
7661#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7662/*
7663 * Get the job from the argument.
7664 * Returns NULL if the job is invalid.
7665 */
7666 static job_T *
7667get_job_arg(typval_T *tv)
7668{
7669 job_T *job;
7670
7671 if (tv->v_type != VAR_JOB)
7672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007673 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674 return NULL;
7675 }
7676 job = tv->vval.v_job;
7677
7678 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007679 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007680 return job;
7681}
7682
7683/*
7684 * "job_getchannel()" function
7685 */
7686 static void
7687f_job_getchannel(typval_T *argvars, typval_T *rettv)
7688{
7689 job_T *job = get_job_arg(&argvars[0]);
7690
7691 if (job != NULL)
7692 {
7693 rettv->v_type = VAR_CHANNEL;
7694 rettv->vval.v_channel = job->jv_channel;
7695 if (job->jv_channel != NULL)
7696 ++job->jv_channel->ch_refcount;
7697 }
7698}
7699
7700/*
7701 * "job_info()" function
7702 */
7703 static void
7704f_job_info(typval_T *argvars, typval_T *rettv)
7705{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007706 if (argvars[0].v_type != VAR_UNKNOWN)
7707 {
7708 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007709
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007710 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7711 job_info(job, rettv->vval.v_dict);
7712 }
7713 else if (rettv_list_alloc(rettv) == OK)
7714 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007715}
7716
7717/*
7718 * "job_setoptions()" function
7719 */
7720 static void
7721f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7722{
7723 job_T *job = get_job_arg(&argvars[0]);
7724 jobopt_T opt;
7725
7726 if (job == NULL)
7727 return;
7728 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007729 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007730 job_set_options(job, &opt);
7731 free_job_options(&opt);
7732}
7733
7734/*
7735 * "job_start()" function
7736 */
7737 static void
7738f_job_start(typval_T *argvars, typval_T *rettv)
7739{
7740 rettv->v_type = VAR_JOB;
7741 if (check_restricted() || check_secure())
7742 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007743 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007744}
7745
7746/*
7747 * "job_status()" function
7748 */
7749 static void
7750f_job_status(typval_T *argvars, typval_T *rettv)
7751{
7752 job_T *job = get_job_arg(&argvars[0]);
7753
7754 if (job != NULL)
7755 {
7756 rettv->v_type = VAR_STRING;
7757 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7758 }
7759}
7760
7761/*
7762 * "job_stop()" function
7763 */
7764 static void
7765f_job_stop(typval_T *argvars, typval_T *rettv)
7766{
7767 job_T *job = get_job_arg(&argvars[0]);
7768
7769 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007770 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007771}
7772#endif
7773
7774/*
7775 * "join()" function
7776 */
7777 static void
7778f_join(typval_T *argvars, typval_T *rettv)
7779{
7780 garray_T ga;
7781 char_u *sep;
7782
7783 if (argvars[0].v_type != VAR_LIST)
7784 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007785 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007786 return;
7787 }
7788 if (argvars[0].vval.v_list == NULL)
7789 return;
7790 if (argvars[1].v_type == VAR_UNKNOWN)
7791 sep = (char_u *)" ";
7792 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007793 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007794
7795 rettv->v_type = VAR_STRING;
7796
7797 if (sep != NULL)
7798 {
7799 ga_init2(&ga, (int)sizeof(char), 80);
7800 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7801 ga_append(&ga, NUL);
7802 rettv->vval.v_string = (char_u *)ga.ga_data;
7803 }
7804 else
7805 rettv->vval.v_string = NULL;
7806}
7807
7808/*
7809 * "js_decode()" function
7810 */
7811 static void
7812f_js_decode(typval_T *argvars, typval_T *rettv)
7813{
7814 js_read_T reader;
7815
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007816 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007817 reader.js_fill = NULL;
7818 reader.js_used = 0;
7819 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007820 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007821}
7822
7823/*
7824 * "js_encode()" function
7825 */
7826 static void
7827f_js_encode(typval_T *argvars, typval_T *rettv)
7828{
7829 rettv->v_type = VAR_STRING;
7830 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7831}
7832
7833/*
7834 * "json_decode()" function
7835 */
7836 static void
7837f_json_decode(typval_T *argvars, typval_T *rettv)
7838{
7839 js_read_T reader;
7840
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007841 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007842 reader.js_fill = NULL;
7843 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007844 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007845}
7846
7847/*
7848 * "json_encode()" function
7849 */
7850 static void
7851f_json_encode(typval_T *argvars, typval_T *rettv)
7852{
7853 rettv->v_type = VAR_STRING;
7854 rettv->vval.v_string = json_encode(&argvars[0], 0);
7855}
7856
7857/*
7858 * "keys()" function
7859 */
7860 static void
7861f_keys(typval_T *argvars, typval_T *rettv)
7862{
7863 dict_list(argvars, rettv, 0);
7864}
7865
7866/*
7867 * "last_buffer_nr()" function.
7868 */
7869 static void
7870f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7871{
7872 int n = 0;
7873 buf_T *buf;
7874
Bram Moolenaar29323592016-07-24 22:04:11 +02007875 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007876 if (n < buf->b_fnum)
7877 n = buf->b_fnum;
7878
7879 rettv->vval.v_number = n;
7880}
7881
7882/*
7883 * "len()" function
7884 */
7885 static void
7886f_len(typval_T *argvars, typval_T *rettv)
7887{
7888 switch (argvars[0].v_type)
7889 {
7890 case VAR_STRING:
7891 case VAR_NUMBER:
7892 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007893 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007895 case VAR_BLOB:
7896 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7897 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007898 case VAR_LIST:
7899 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7900 break;
7901 case VAR_DICT:
7902 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7903 break;
7904 case VAR_UNKNOWN:
7905 case VAR_SPECIAL:
7906 case VAR_FLOAT:
7907 case VAR_FUNC:
7908 case VAR_PARTIAL:
7909 case VAR_JOB:
7910 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007911 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912 break;
7913 }
7914}
7915
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007916 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007917libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007918{
7919#ifdef FEAT_LIBCALL
7920 char_u *string_in;
7921 char_u **string_result;
7922 int nr_result;
7923#endif
7924
7925 rettv->v_type = type;
7926 if (type != VAR_NUMBER)
7927 rettv->vval.v_string = NULL;
7928
7929 if (check_restricted() || check_secure())
7930 return;
7931
7932#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007933 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7935 {
7936 string_in = NULL;
7937 if (argvars[2].v_type == VAR_STRING)
7938 string_in = argvars[2].vval.v_string;
7939 if (type == VAR_NUMBER)
7940 string_result = NULL;
7941 else
7942 string_result = &rettv->vval.v_string;
7943 if (mch_libcall(argvars[0].vval.v_string,
7944 argvars[1].vval.v_string,
7945 string_in,
7946 argvars[2].vval.v_number,
7947 string_result,
7948 &nr_result) == OK
7949 && type == VAR_NUMBER)
7950 rettv->vval.v_number = nr_result;
7951 }
7952#endif
7953}
7954
7955/*
7956 * "libcall()" function
7957 */
7958 static void
7959f_libcall(typval_T *argvars, typval_T *rettv)
7960{
7961 libcall_common(argvars, rettv, VAR_STRING);
7962}
7963
7964/*
7965 * "libcallnr()" function
7966 */
7967 static void
7968f_libcallnr(typval_T *argvars, typval_T *rettv)
7969{
7970 libcall_common(argvars, rettv, VAR_NUMBER);
7971}
7972
7973/*
7974 * "line(string)" function
7975 */
7976 static void
7977f_line(typval_T *argvars, typval_T *rettv)
7978{
7979 linenr_T lnum = 0;
7980 pos_T *fp;
7981 int fnum;
7982
7983 fp = var2fpos(&argvars[0], TRUE, &fnum);
7984 if (fp != NULL)
7985 lnum = fp->lnum;
7986 rettv->vval.v_number = lnum;
7987}
7988
7989/*
7990 * "line2byte(lnum)" function
7991 */
7992 static void
7993f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7994{
7995#ifndef FEAT_BYTEOFF
7996 rettv->vval.v_number = -1;
7997#else
7998 linenr_T lnum;
7999
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008000 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
8002 rettv->vval.v_number = -1;
8003 else
8004 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8005 if (rettv->vval.v_number >= 0)
8006 ++rettv->vval.v_number;
8007#endif
8008}
8009
8010/*
8011 * "lispindent(lnum)" function
8012 */
8013 static void
8014f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8015{
8016#ifdef FEAT_LISP
8017 pos_T pos;
8018 linenr_T lnum;
8019
8020 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008021 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8023 {
8024 curwin->w_cursor.lnum = lnum;
8025 rettv->vval.v_number = get_lisp_indent();
8026 curwin->w_cursor = pos;
8027 }
8028 else
8029#endif
8030 rettv->vval.v_number = -1;
8031}
8032
8033/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008034 * "list2str()" function
8035 */
8036 static void
8037f_list2str(typval_T *argvars, typval_T *rettv)
8038{
8039 list_T *l;
8040 listitem_T *li;
8041 garray_T ga;
8042 int utf8 = FALSE;
8043
8044 rettv->v_type = VAR_STRING;
8045 rettv->vval.v_string = NULL;
8046 if (argvars[0].v_type != VAR_LIST)
8047 {
8048 emsg(_(e_invarg));
8049 return;
8050 }
8051
8052 l = argvars[0].vval.v_list;
8053 if (l == NULL)
8054 return; // empty list results in empty string
8055
8056 if (argvars[1].v_type != VAR_UNKNOWN)
8057 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8058
8059 ga_init2(&ga, 1, 80);
8060 if (has_mbyte || utf8)
8061 {
8062 char_u buf[MB_MAXBYTES + 1];
8063 int (*char2bytes)(int, char_u *);
8064
8065 if (utf8 || enc_utf8)
8066 char2bytes = utf_char2bytes;
8067 else
8068 char2bytes = mb_char2bytes;
8069
8070 for (li = l->lv_first; li != NULL; li = li->li_next)
8071 {
8072 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8073 ga_concat(&ga, buf);
8074 }
8075 ga_append(&ga, NUL);
8076 }
8077 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8078 {
8079 for (li = l->lv_first; li != NULL; li = li->li_next)
8080 ga_append(&ga, tv_get_number(&li->li_tv));
8081 ga_append(&ga, NUL);
8082 }
8083
8084 rettv->v_type = VAR_STRING;
8085 rettv->vval.v_string = ga.ga_data;
8086}
8087
8088/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008089 * "localtime()" function
8090 */
8091 static void
8092f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8093{
8094 rettv->vval.v_number = (varnumber_T)time(NULL);
8095}
8096
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008097 static void
8098get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8099{
8100 char_u *keys;
8101 char_u *which;
8102 char_u buf[NUMBUFLEN];
8103 char_u *keys_buf = NULL;
8104 char_u *rhs;
8105 int mode;
8106 int abbr = FALSE;
8107 int get_dict = FALSE;
8108 mapblock_T *mp;
8109 int buffer_local;
8110
8111 /* return empty string for failure */
8112 rettv->v_type = VAR_STRING;
8113 rettv->vval.v_string = NULL;
8114
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008115 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008116 if (*keys == NUL)
8117 return;
8118
8119 if (argvars[1].v_type != VAR_UNKNOWN)
8120 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008121 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008122 if (argvars[2].v_type != VAR_UNKNOWN)
8123 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008124 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008126 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008127 }
8128 }
8129 else
8130 which = (char_u *)"";
8131 if (which == NULL)
8132 return;
8133
8134 mode = get_map_mode(&which, 0);
8135
8136 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8137 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8138 vim_free(keys_buf);
8139
8140 if (!get_dict)
8141 {
8142 /* Return a string. */
8143 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008144 {
8145 if (*rhs == NUL)
8146 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8147 else
8148 rettv->vval.v_string = str2special_save(rhs, FALSE);
8149 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150
8151 }
8152 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8153 {
8154 /* Return a dictionary. */
8155 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8156 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8157 dict_T *dict = rettv->vval.v_dict;
8158
Bram Moolenaare0be1672018-07-08 16:50:37 +02008159 dict_add_string(dict, "lhs", lhs);
8160 dict_add_string(dict, "rhs", mp->m_orig_str);
8161 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8162 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8163 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008164 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8165 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008166 dict_add_number(dict, "buffer", (long)buffer_local);
8167 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8168 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008169
8170 vim_free(lhs);
8171 vim_free(mapmode);
8172 }
8173}
8174
8175#ifdef FEAT_FLOAT
8176/*
8177 * "log()" function
8178 */
8179 static void
8180f_log(typval_T *argvars, typval_T *rettv)
8181{
8182 float_T f = 0.0;
8183
8184 rettv->v_type = VAR_FLOAT;
8185 if (get_float_arg(argvars, &f) == OK)
8186 rettv->vval.v_float = log(f);
8187 else
8188 rettv->vval.v_float = 0.0;
8189}
8190
8191/*
8192 * "log10()" function
8193 */
8194 static void
8195f_log10(typval_T *argvars, typval_T *rettv)
8196{
8197 float_T f = 0.0;
8198
8199 rettv->v_type = VAR_FLOAT;
8200 if (get_float_arg(argvars, &f) == OK)
8201 rettv->vval.v_float = log10(f);
8202 else
8203 rettv->vval.v_float = 0.0;
8204}
8205#endif
8206
8207#ifdef FEAT_LUA
8208/*
8209 * "luaeval()" function
8210 */
8211 static void
8212f_luaeval(typval_T *argvars, typval_T *rettv)
8213{
8214 char_u *str;
8215 char_u buf[NUMBUFLEN];
8216
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008217 if (check_restricted() || check_secure())
8218 return;
8219
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008220 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008221 do_luaeval(str, argvars + 1, rettv);
8222}
8223#endif
8224
8225/*
8226 * "map()" function
8227 */
8228 static void
8229f_map(typval_T *argvars, typval_T *rettv)
8230{
8231 filter_map(argvars, rettv, TRUE);
8232}
8233
8234/*
8235 * "maparg()" function
8236 */
8237 static void
8238f_maparg(typval_T *argvars, typval_T *rettv)
8239{
8240 get_maparg(argvars, rettv, TRUE);
8241}
8242
8243/*
8244 * "mapcheck()" function
8245 */
8246 static void
8247f_mapcheck(typval_T *argvars, typval_T *rettv)
8248{
8249 get_maparg(argvars, rettv, FALSE);
8250}
8251
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008252typedef enum
8253{
8254 MATCH_END, /* matchend() */
8255 MATCH_MATCH, /* match() */
8256 MATCH_STR, /* matchstr() */
8257 MATCH_LIST, /* matchlist() */
8258 MATCH_POS /* matchstrpos() */
8259} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008260
8261 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008262find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008263{
8264 char_u *str = NULL;
8265 long len = 0;
8266 char_u *expr = NULL;
8267 char_u *pat;
8268 regmatch_T regmatch;
8269 char_u patbuf[NUMBUFLEN];
8270 char_u strbuf[NUMBUFLEN];
8271 char_u *save_cpo;
8272 long start = 0;
8273 long nth = 1;
8274 colnr_T startcol = 0;
8275 int match = 0;
8276 list_T *l = NULL;
8277 listitem_T *li = NULL;
8278 long idx = 0;
8279 char_u *tofree = NULL;
8280
8281 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8282 save_cpo = p_cpo;
8283 p_cpo = (char_u *)"";
8284
8285 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008286 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008288 /* type MATCH_LIST: return empty list when there are no matches.
8289 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008290 if (rettv_list_alloc(rettv) == FAIL)
8291 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008292 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293 && (list_append_string(rettv->vval.v_list,
8294 (char_u *)"", 0) == FAIL
8295 || list_append_number(rettv->vval.v_list,
8296 (varnumber_T)-1) == FAIL
8297 || list_append_number(rettv->vval.v_list,
8298 (varnumber_T)-1) == FAIL
8299 || list_append_number(rettv->vval.v_list,
8300 (varnumber_T)-1) == FAIL))
8301 {
8302 list_free(rettv->vval.v_list);
8303 rettv->vval.v_list = NULL;
8304 goto theend;
8305 }
8306 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008307 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308 {
8309 rettv->v_type = VAR_STRING;
8310 rettv->vval.v_string = NULL;
8311 }
8312
8313 if (argvars[0].v_type == VAR_LIST)
8314 {
8315 if ((l = argvars[0].vval.v_list) == NULL)
8316 goto theend;
8317 li = l->lv_first;
8318 }
8319 else
8320 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008321 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322 len = (long)STRLEN(str);
8323 }
8324
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008325 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326 if (pat == NULL)
8327 goto theend;
8328
8329 if (argvars[2].v_type != VAR_UNKNOWN)
8330 {
8331 int error = FALSE;
8332
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008333 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008334 if (error)
8335 goto theend;
8336 if (l != NULL)
8337 {
8338 li = list_find(l, start);
8339 if (li == NULL)
8340 goto theend;
8341 idx = l->lv_idx; /* use the cached index */
8342 }
8343 else
8344 {
8345 if (start < 0)
8346 start = 0;
8347 if (start > len)
8348 goto theend;
8349 /* When "count" argument is there ignore matches before "start",
8350 * otherwise skip part of the string. Differs when pattern is "^"
8351 * or "\<". */
8352 if (argvars[3].v_type != VAR_UNKNOWN)
8353 startcol = start;
8354 else
8355 {
8356 str += start;
8357 len -= start;
8358 }
8359 }
8360
8361 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008362 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008363 if (error)
8364 goto theend;
8365 }
8366
8367 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8368 if (regmatch.regprog != NULL)
8369 {
8370 regmatch.rm_ic = p_ic;
8371
8372 for (;;)
8373 {
8374 if (l != NULL)
8375 {
8376 if (li == NULL)
8377 {
8378 match = FALSE;
8379 break;
8380 }
8381 vim_free(tofree);
8382 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8383 if (str == NULL)
8384 break;
8385 }
8386
8387 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8388
8389 if (match && --nth <= 0)
8390 break;
8391 if (l == NULL && !match)
8392 break;
8393
8394 /* Advance to just after the match. */
8395 if (l != NULL)
8396 {
8397 li = li->li_next;
8398 ++idx;
8399 }
8400 else
8401 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008402 startcol = (colnr_T)(regmatch.startp[0]
8403 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404 if (startcol > (colnr_T)len
8405 || str + startcol <= regmatch.startp[0])
8406 {
8407 match = FALSE;
8408 break;
8409 }
8410 }
8411 }
8412
8413 if (match)
8414 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008415 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008416 {
8417 listitem_T *li1 = rettv->vval.v_list->lv_first;
8418 listitem_T *li2 = li1->li_next;
8419 listitem_T *li3 = li2->li_next;
8420 listitem_T *li4 = li3->li_next;
8421
8422 vim_free(li1->li_tv.vval.v_string);
8423 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8424 (int)(regmatch.endp[0] - regmatch.startp[0]));
8425 li3->li_tv.vval.v_number =
8426 (varnumber_T)(regmatch.startp[0] - expr);
8427 li4->li_tv.vval.v_number =
8428 (varnumber_T)(regmatch.endp[0] - expr);
8429 if (l != NULL)
8430 li2->li_tv.vval.v_number = (varnumber_T)idx;
8431 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008432 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008433 {
8434 int i;
8435
8436 /* return list with matched string and submatches */
8437 for (i = 0; i < NSUBEXP; ++i)
8438 {
8439 if (regmatch.endp[i] == NULL)
8440 {
8441 if (list_append_string(rettv->vval.v_list,
8442 (char_u *)"", 0) == FAIL)
8443 break;
8444 }
8445 else if (list_append_string(rettv->vval.v_list,
8446 regmatch.startp[i],
8447 (int)(regmatch.endp[i] - regmatch.startp[i]))
8448 == FAIL)
8449 break;
8450 }
8451 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008452 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008453 {
8454 /* return matched string */
8455 if (l != NULL)
8456 copy_tv(&li->li_tv, rettv);
8457 else
8458 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8459 (int)(regmatch.endp[0] - regmatch.startp[0]));
8460 }
8461 else if (l != NULL)
8462 rettv->vval.v_number = idx;
8463 else
8464 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008465 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008466 rettv->vval.v_number =
8467 (varnumber_T)(regmatch.startp[0] - str);
8468 else
8469 rettv->vval.v_number =
8470 (varnumber_T)(regmatch.endp[0] - str);
8471 rettv->vval.v_number += (varnumber_T)(str - expr);
8472 }
8473 }
8474 vim_regfree(regmatch.regprog);
8475 }
8476
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008477theend:
8478 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008479 /* matchstrpos() without a list: drop the second item. */
8480 listitem_remove(rettv->vval.v_list,
8481 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008482 vim_free(tofree);
8483 p_cpo = save_cpo;
8484}
8485
8486/*
8487 * "match()" function
8488 */
8489 static void
8490f_match(typval_T *argvars, typval_T *rettv)
8491{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008492 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493}
8494
Bram Moolenaar95e51472018-07-28 16:55:56 +02008495#ifdef FEAT_SEARCH_EXTRA
8496 static int
8497matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8498{
8499 dictitem_T *di;
8500
8501 if (tv->v_type != VAR_DICT)
8502 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008503 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008504 return FAIL;
8505 }
8506
8507 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008508 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008509 (char_u *)"conceal", FALSE);
8510
8511 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8512 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008513 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008514 if (*win == NULL)
8515 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008516 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008517 return FAIL;
8518 }
8519 }
8520
8521 return OK;
8522}
8523#endif
8524
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008525/*
8526 * "matchadd()" function
8527 */
8528 static void
8529f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8530{
8531#ifdef FEAT_SEARCH_EXTRA
8532 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008533 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8534 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008535 int prio = 10; /* default priority */
8536 int id = -1;
8537 int error = FALSE;
8538 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008539 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008540
8541 rettv->vval.v_number = -1;
8542
8543 if (grp == NULL || pat == NULL)
8544 return;
8545 if (argvars[2].v_type != VAR_UNKNOWN)
8546 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008547 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548 if (argvars[3].v_type != VAR_UNKNOWN)
8549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008550 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008551 if (argvars[4].v_type != VAR_UNKNOWN
8552 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8553 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008554 }
8555 }
8556 if (error == TRUE)
8557 return;
8558 if (id >= 1 && id <= 3)
8559 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008560 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561 return;
8562 }
8563
Bram Moolenaar95e51472018-07-28 16:55:56 +02008564 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008565 conceal_char);
8566#endif
8567}
8568
8569/*
8570 * "matchaddpos()" function
8571 */
8572 static void
8573f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8574{
8575#ifdef FEAT_SEARCH_EXTRA
8576 char_u buf[NUMBUFLEN];
8577 char_u *group;
8578 int prio = 10;
8579 int id = -1;
8580 int error = FALSE;
8581 list_T *l;
8582 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008583 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008584
8585 rettv->vval.v_number = -1;
8586
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008587 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008588 if (group == NULL)
8589 return;
8590
8591 if (argvars[1].v_type != VAR_LIST)
8592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008593 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008594 return;
8595 }
8596 l = argvars[1].vval.v_list;
8597 if (l == NULL)
8598 return;
8599
8600 if (argvars[2].v_type != VAR_UNKNOWN)
8601 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008602 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 if (argvars[3].v_type != VAR_UNKNOWN)
8604 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008605 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008606
8607 if (argvars[4].v_type != VAR_UNKNOWN
8608 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8609 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610 }
8611 }
8612 if (error == TRUE)
8613 return;
8614
8615 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8616 if (id == 1 || id == 2)
8617 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008618 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008619 return;
8620 }
8621
Bram Moolenaar95e51472018-07-28 16:55:56 +02008622 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008623 conceal_char);
8624#endif
8625}
8626
8627/*
8628 * "matcharg()" function
8629 */
8630 static void
8631f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8632{
8633 if (rettv_list_alloc(rettv) == OK)
8634 {
8635#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008636 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008637 matchitem_T *m;
8638
8639 if (id >= 1 && id <= 3)
8640 {
8641 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8642 {
8643 list_append_string(rettv->vval.v_list,
8644 syn_id2name(m->hlg_id), -1);
8645 list_append_string(rettv->vval.v_list, m->pattern, -1);
8646 }
8647 else
8648 {
8649 list_append_string(rettv->vval.v_list, NULL, -1);
8650 list_append_string(rettv->vval.v_list, NULL, -1);
8651 }
8652 }
8653#endif
8654 }
8655}
8656
8657/*
8658 * "matchdelete()" function
8659 */
8660 static void
8661f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8662{
8663#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008664 win_T *win = get_optional_window(argvars, 1);
8665
8666 if (win == NULL)
8667 rettv->vval.v_number = -1;
8668 else
8669 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008670 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008671#endif
8672}
8673
8674/*
8675 * "matchend()" function
8676 */
8677 static void
8678f_matchend(typval_T *argvars, typval_T *rettv)
8679{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008680 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681}
8682
8683/*
8684 * "matchlist()" function
8685 */
8686 static void
8687f_matchlist(typval_T *argvars, typval_T *rettv)
8688{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008689 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690}
8691
8692/*
8693 * "matchstr()" function
8694 */
8695 static void
8696f_matchstr(typval_T *argvars, typval_T *rettv)
8697{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008698 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699}
8700
8701/*
8702 * "matchstrpos()" function
8703 */
8704 static void
8705f_matchstrpos(typval_T *argvars, typval_T *rettv)
8706{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008707 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008708}
8709
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710 static void
8711max_min(typval_T *argvars, typval_T *rettv, int domax)
8712{
8713 varnumber_T n = 0;
8714 varnumber_T i;
8715 int error = FALSE;
8716
8717 if (argvars[0].v_type == VAR_LIST)
8718 {
8719 list_T *l;
8720 listitem_T *li;
8721
8722 l = argvars[0].vval.v_list;
8723 if (l != NULL)
8724 {
8725 li = l->lv_first;
8726 if (li != NULL)
8727 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008728 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008729 for (;;)
8730 {
8731 li = li->li_next;
8732 if (li == NULL)
8733 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008734 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735 if (domax ? i > n : i < n)
8736 n = i;
8737 }
8738 }
8739 }
8740 }
8741 else if (argvars[0].v_type == VAR_DICT)
8742 {
8743 dict_T *d;
8744 int first = TRUE;
8745 hashitem_T *hi;
8746 int todo;
8747
8748 d = argvars[0].vval.v_dict;
8749 if (d != NULL)
8750 {
8751 todo = (int)d->dv_hashtab.ht_used;
8752 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8753 {
8754 if (!HASHITEM_EMPTY(hi))
8755 {
8756 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008757 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758 if (first)
8759 {
8760 n = i;
8761 first = FALSE;
8762 }
8763 else if (domax ? i > n : i < n)
8764 n = i;
8765 }
8766 }
8767 }
8768 }
8769 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008770 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008771 rettv->vval.v_number = error ? 0 : n;
8772}
8773
8774/*
8775 * "max()" function
8776 */
8777 static void
8778f_max(typval_T *argvars, typval_T *rettv)
8779{
8780 max_min(argvars, rettv, TRUE);
8781}
8782
8783/*
8784 * "min()" function
8785 */
8786 static void
8787f_min(typval_T *argvars, typval_T *rettv)
8788{
8789 max_min(argvars, rettv, FALSE);
8790}
8791
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008792/*
8793 * Create the directory in which "dir" is located, and higher levels when
8794 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008795 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 */
8797 static int
8798mkdir_recurse(char_u *dir, int prot)
8799{
8800 char_u *p;
8801 char_u *updir;
8802 int r = FAIL;
8803
8804 /* Get end of directory name in "dir".
8805 * We're done when it's "/" or "c:/". */
8806 p = gettail_sep(dir);
8807 if (p <= get_past_head(dir))
8808 return OK;
8809
8810 /* If the directory exists we're done. Otherwise: create it.*/
8811 updir = vim_strnsave(dir, (int)(p - dir));
8812 if (updir == NULL)
8813 return FAIL;
8814 if (mch_isdir(updir))
8815 r = OK;
8816 else if (mkdir_recurse(updir, prot) == OK)
8817 r = vim_mkdir_emsg(updir, prot);
8818 vim_free(updir);
8819 return r;
8820}
8821
8822#ifdef vim_mkdir
8823/*
8824 * "mkdir()" function
8825 */
8826 static void
8827f_mkdir(typval_T *argvars, typval_T *rettv)
8828{
8829 char_u *dir;
8830 char_u buf[NUMBUFLEN];
8831 int prot = 0755;
8832
8833 rettv->vval.v_number = FAIL;
8834 if (check_restricted() || check_secure())
8835 return;
8836
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008837 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008838 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008839 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008841 if (*gettail(dir) == NUL)
8842 /* remove trailing slashes */
8843 *gettail_sep(dir) = NUL;
8844
8845 if (argvars[1].v_type != VAR_UNKNOWN)
8846 {
8847 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008848 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008849 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008850 if (prot == -1)
8851 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008852 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008853 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008854 {
8855 if (mch_isdir(dir))
8856 {
8857 /* With the "p" flag it's OK if the dir already exists. */
8858 rettv->vval.v_number = OK;
8859 return;
8860 }
8861 mkdir_recurse(dir, prot);
8862 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008863 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008864 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008865}
8866#endif
8867
8868/*
8869 * "mode()" function
8870 */
8871 static void
8872f_mode(typval_T *argvars, typval_T *rettv)
8873{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008874 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875
Bram Moolenaar612cc382018-07-29 15:34:26 +02008876 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008877
8878 if (time_for_testing == 93784)
8879 {
8880 /* Testing the two-character code. */
8881 buf[0] = 'x';
8882 buf[1] = '!';
8883 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008884#ifdef FEAT_TERMINAL
8885 else if (term_use_loop())
8886 buf[0] = 't';
8887#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888 else if (VIsual_active)
8889 {
8890 if (VIsual_select)
8891 buf[0] = VIsual_mode + 's' - 'v';
8892 else
8893 buf[0] = VIsual_mode;
8894 }
8895 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8896 || State == CONFIRM)
8897 {
8898 buf[0] = 'r';
8899 if (State == ASKMORE)
8900 buf[1] = 'm';
8901 else if (State == CONFIRM)
8902 buf[1] = '?';
8903 }
8904 else if (State == EXTERNCMD)
8905 buf[0] = '!';
8906 else if (State & INSERT)
8907 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008908 if (State & VREPLACE_FLAG)
8909 {
8910 buf[0] = 'R';
8911 buf[1] = 'v';
8912 }
8913 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008914 {
8915 if (State & REPLACE_FLAG)
8916 buf[0] = 'R';
8917 else
8918 buf[0] = 'i';
8919#ifdef FEAT_INS_EXPAND
8920 if (ins_compl_active())
8921 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008922 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008923 buf[1] = 'x';
8924#endif
8925 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008927 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008928 {
8929 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008930 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008931 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008932 else if (exmode_active == EXMODE_NORMAL)
8933 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008934 }
8935 else
8936 {
8937 buf[0] = 'n';
8938 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008939 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008940 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008941 // to be able to detect force-linewise/blockwise/characterwise operations
8942 buf[2] = motion_force;
8943 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008944 else if (restart_edit == 'I' || restart_edit == 'R'
8945 || restart_edit == 'V')
8946 {
8947 buf[1] = 'i';
8948 buf[2] = restart_edit;
8949 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008950 }
8951
8952 /* Clear out the minor mode when the argument is not a non-zero number or
8953 * non-empty string. */
8954 if (!non_zero_arg(&argvars[0]))
8955 buf[1] = NUL;
8956
8957 rettv->vval.v_string = vim_strsave(buf);
8958 rettv->v_type = VAR_STRING;
8959}
8960
8961#if defined(FEAT_MZSCHEME) || defined(PROTO)
8962/*
8963 * "mzeval()" function
8964 */
8965 static void
8966f_mzeval(typval_T *argvars, typval_T *rettv)
8967{
8968 char_u *str;
8969 char_u buf[NUMBUFLEN];
8970
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008971 if (check_restricted() || check_secure())
8972 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008973 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008974 do_mzeval(str, rettv);
8975}
8976
8977 void
8978mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8979{
8980 typval_T argvars[3];
8981
8982 argvars[0].v_type = VAR_STRING;
8983 argvars[0].vval.v_string = name;
8984 copy_tv(args, &argvars[1]);
8985 argvars[2].v_type = VAR_UNKNOWN;
8986 f_call(argvars, rettv);
8987 clear_tv(&argvars[1]);
8988}
8989#endif
8990
8991/*
8992 * "nextnonblank()" function
8993 */
8994 static void
8995f_nextnonblank(typval_T *argvars, typval_T *rettv)
8996{
8997 linenr_T lnum;
8998
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008999 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009000 {
9001 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
9002 {
9003 lnum = 0;
9004 break;
9005 }
9006 if (*skipwhite(ml_get(lnum)) != NUL)
9007 break;
9008 }
9009 rettv->vval.v_number = lnum;
9010}
9011
9012/*
9013 * "nr2char()" function
9014 */
9015 static void
9016f_nr2char(typval_T *argvars, typval_T *rettv)
9017{
9018 char_u buf[NUMBUFLEN];
9019
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020 if (has_mbyte)
9021 {
9022 int utf8 = 0;
9023
9024 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009025 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009027 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009028 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009029 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009030 }
9031 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009032 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009033 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009034 buf[1] = NUL;
9035 }
9036 rettv->v_type = VAR_STRING;
9037 rettv->vval.v_string = vim_strsave(buf);
9038}
9039
9040/*
9041 * "or(expr, expr)" function
9042 */
9043 static void
9044f_or(typval_T *argvars, typval_T *rettv)
9045{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009046 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9047 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009048}
9049
9050/*
9051 * "pathshorten()" function
9052 */
9053 static void
9054f_pathshorten(typval_T *argvars, typval_T *rettv)
9055{
9056 char_u *p;
9057
9058 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009059 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009060 if (p == NULL)
9061 rettv->vval.v_string = NULL;
9062 else
9063 {
9064 p = vim_strsave(p);
9065 rettv->vval.v_string = p;
9066 if (p != NULL)
9067 shorten_dir(p);
9068 }
9069}
9070
9071#ifdef FEAT_PERL
9072/*
9073 * "perleval()" function
9074 */
9075 static void
9076f_perleval(typval_T *argvars, typval_T *rettv)
9077{
9078 char_u *str;
9079 char_u buf[NUMBUFLEN];
9080
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009081 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009082 do_perleval(str, rettv);
9083}
9084#endif
9085
9086#ifdef FEAT_FLOAT
9087/*
9088 * "pow()" function
9089 */
9090 static void
9091f_pow(typval_T *argvars, typval_T *rettv)
9092{
9093 float_T fx = 0.0, fy = 0.0;
9094
9095 rettv->v_type = VAR_FLOAT;
9096 if (get_float_arg(argvars, &fx) == OK
9097 && get_float_arg(&argvars[1], &fy) == OK)
9098 rettv->vval.v_float = pow(fx, fy);
9099 else
9100 rettv->vval.v_float = 0.0;
9101}
9102#endif
9103
9104/*
9105 * "prevnonblank()" function
9106 */
9107 static void
9108f_prevnonblank(typval_T *argvars, typval_T *rettv)
9109{
9110 linenr_T lnum;
9111
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009112 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009113 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9114 lnum = 0;
9115 else
9116 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9117 --lnum;
9118 rettv->vval.v_number = lnum;
9119}
9120
9121/* This dummy va_list is here because:
9122 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9123 * - locally in the function results in a "used before set" warning
9124 * - using va_start() to initialize it gives "function with fixed args" error */
9125static va_list ap;
9126
9127/*
9128 * "printf()" function
9129 */
9130 static void
9131f_printf(typval_T *argvars, typval_T *rettv)
9132{
9133 char_u buf[NUMBUFLEN];
9134 int len;
9135 char_u *s;
9136 int saved_did_emsg = did_emsg;
9137 char *fmt;
9138
9139 rettv->v_type = VAR_STRING;
9140 rettv->vval.v_string = NULL;
9141
9142 /* Get the required length, allocate the buffer and do it for real. */
9143 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009144 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009145 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009146 if (!did_emsg)
9147 {
9148 s = alloc(len + 1);
9149 if (s != NULL)
9150 {
9151 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009152 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9153 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009154 }
9155 }
9156 did_emsg |= saved_did_emsg;
9157}
9158
Bram Moolenaarf2732452018-06-03 14:47:35 +02009159#ifdef FEAT_JOB_CHANNEL
9160/*
9161 * "prompt_setcallback({buffer}, {callback})" function
9162 */
9163 static void
9164f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9165{
9166 buf_T *buf;
9167 char_u *callback;
9168 partial_T *partial;
9169
9170 if (check_secure())
9171 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009172 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009173 if (buf == NULL)
9174 return;
9175
9176 callback = get_callback(&argvars[1], &partial);
9177 if (callback == NULL)
9178 return;
9179
9180 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
9181 if (partial == NULL)
9182 buf->b_prompt_callback = vim_strsave(callback);
9183 else
9184 /* pointer into the partial */
9185 buf->b_prompt_callback = callback;
9186 buf->b_prompt_partial = partial;
9187}
9188
9189/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009190 * "prompt_setinterrupt({buffer}, {callback})" function
9191 */
9192 static void
9193f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9194{
9195 buf_T *buf;
9196 char_u *callback;
9197 partial_T *partial;
9198
9199 if (check_secure())
9200 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009201 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009202 if (buf == NULL)
9203 return;
9204
9205 callback = get_callback(&argvars[1], &partial);
9206 if (callback == NULL)
9207 return;
9208
9209 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9210 if (partial == NULL)
9211 buf->b_prompt_interrupt = vim_strsave(callback);
9212 else
9213 /* pointer into the partial */
9214 buf->b_prompt_interrupt = callback;
9215 buf->b_prompt_int_partial = partial;
9216}
9217
9218/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009219 * "prompt_setprompt({buffer}, {text})" function
9220 */
9221 static void
9222f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9223{
9224 buf_T *buf;
9225 char_u *text;
9226
9227 if (check_secure())
9228 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009229 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009230 if (buf == NULL)
9231 return;
9232
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009233 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009234 vim_free(buf->b_prompt_text);
9235 buf->b_prompt_text = vim_strsave(text);
9236}
9237#endif
9238
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009239/*
9240 * "pumvisible()" function
9241 */
9242 static void
9243f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9244{
9245#ifdef FEAT_INS_EXPAND
9246 if (pum_visible())
9247 rettv->vval.v_number = 1;
9248#endif
9249}
9250
9251#ifdef FEAT_PYTHON3
9252/*
9253 * "py3eval()" function
9254 */
9255 static void
9256f_py3eval(typval_T *argvars, typval_T *rettv)
9257{
9258 char_u *str;
9259 char_u buf[NUMBUFLEN];
9260
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009261 if (check_restricted() || check_secure())
9262 return;
9263
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009264 if (p_pyx == 0)
9265 p_pyx = 3;
9266
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009267 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009268 do_py3eval(str, rettv);
9269}
9270#endif
9271
9272#ifdef FEAT_PYTHON
9273/*
9274 * "pyeval()" function
9275 */
9276 static void
9277f_pyeval(typval_T *argvars, typval_T *rettv)
9278{
9279 char_u *str;
9280 char_u buf[NUMBUFLEN];
9281
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009282 if (check_restricted() || check_secure())
9283 return;
9284
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009285 if (p_pyx == 0)
9286 p_pyx = 2;
9287
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009288 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009289 do_pyeval(str, rettv);
9290}
9291#endif
9292
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009293#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9294/*
9295 * "pyxeval()" function
9296 */
9297 static void
9298f_pyxeval(typval_T *argvars, typval_T *rettv)
9299{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009300 if (check_restricted() || check_secure())
9301 return;
9302
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009303# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9304 init_pyxversion();
9305 if (p_pyx == 2)
9306 f_pyeval(argvars, rettv);
9307 else
9308 f_py3eval(argvars, rettv);
9309# elif defined(FEAT_PYTHON)
9310 f_pyeval(argvars, rettv);
9311# elif defined(FEAT_PYTHON3)
9312 f_py3eval(argvars, rettv);
9313# endif
9314}
9315#endif
9316
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009317/*
9318 * "range()" function
9319 */
9320 static void
9321f_range(typval_T *argvars, typval_T *rettv)
9322{
9323 varnumber_T start;
9324 varnumber_T end;
9325 varnumber_T stride = 1;
9326 varnumber_T i;
9327 int error = FALSE;
9328
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009329 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009330 if (argvars[1].v_type == VAR_UNKNOWN)
9331 {
9332 end = start - 1;
9333 start = 0;
9334 }
9335 else
9336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009337 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009338 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009339 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009340 }
9341
9342 if (error)
9343 return; /* type error; errmsg already given */
9344 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009345 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009346 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009347 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348 else
9349 {
9350 if (rettv_list_alloc(rettv) == OK)
9351 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9352 if (list_append_number(rettv->vval.v_list,
9353 (varnumber_T)i) == FAIL)
9354 break;
9355 }
9356}
9357
9358/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009359 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009360 */
9361 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009362readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009363{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009364 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009365 typval_T save_val;
9366 typval_T rettv;
9367 typval_T argv[2];
9368 int retval = 0;
9369 int error = FALSE;
9370
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009371 if (expr->v_type == VAR_UNKNOWN)
9372 return 1;
9373
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009374 prepare_vimvar(VV_VAL, &save_val);
9375 set_vim_var_string(VV_VAL, name, -1);
9376 argv[0].v_type = VAR_STRING;
9377 argv[0].vval.v_string = name;
9378
9379 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9380 goto theend;
9381
9382 retval = tv_get_number_chk(&rettv, &error);
9383 if (error)
9384 retval = -1;
9385 clear_tv(&rettv);
9386
9387theend:
9388 set_vim_var_string(VV_VAL, NULL, 0);
9389 restore_vimvar(VV_VAL, &save_val);
9390 return retval;
9391}
9392
9393/*
9394 * "readdir()" function
9395 */
9396 static void
9397f_readdir(typval_T *argvars, typval_T *rettv)
9398{
9399 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009400 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009401 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009402 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009403 garray_T ga;
9404 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009405
9406 if (rettv_list_alloc(rettv) == FAIL)
9407 return;
9408 path = tv_get_string(&argvars[0]);
9409 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009410
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009411 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9412 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009413 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009414 for (i = 0; i < ga.ga_len; i++)
9415 {
9416 p = ((char_u **)ga.ga_data)[i];
9417 list_append_string(rettv->vval.v_list, p, -1);
9418 }
9419 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009420 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009421}
9422
9423/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009424 * "readfile()" function
9425 */
9426 static void
9427f_readfile(typval_T *argvars, typval_T *rettv)
9428{
9429 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009430 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009431 int failed = FALSE;
9432 char_u *fname;
9433 FILE *fd;
9434 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9435 int io_size = sizeof(buf);
9436 int readlen; /* size of last fread() */
9437 char_u *prev = NULL; /* previously read bytes, if any */
9438 long prevlen = 0; /* length of data in prev */
9439 long prevsize = 0; /* size of prev buffer */
9440 long maxline = MAXLNUM;
9441 long cnt = 0;
9442 char_u *p; /* position in buf */
9443 char_u *start; /* start of current line */
9444
9445 if (argvars[1].v_type != VAR_UNKNOWN)
9446 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009447 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009448 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009449 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9450 blob = TRUE;
9451
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009452 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009453 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009454 }
9455
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009456 if (blob)
9457 {
9458 if (rettv_blob_alloc(rettv) == FAIL)
9459 return;
9460 }
9461 else
9462 {
9463 if (rettv_list_alloc(rettv) == FAIL)
9464 return;
9465 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009466
9467 /* Always open the file in binary mode, library functions have a mind of
9468 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009469 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009470 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9471 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009472 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009473 return;
9474 }
9475
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009476 if (blob)
9477 {
9478 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009480 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009481 blob_free(rettv->vval.v_blob);
9482 }
9483 fclose(fd);
9484 return;
9485 }
9486
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009487 while (cnt < maxline || maxline < 0)
9488 {
9489 readlen = (int)fread(buf, 1, io_size, fd);
9490
9491 /* This for loop processes what was read, but is also entered at end
9492 * of file so that either:
9493 * - an incomplete line gets written
9494 * - a "binary" file gets an empty line at the end if it ends in a
9495 * newline. */
9496 for (p = buf, start = buf;
9497 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9498 ++p)
9499 {
9500 if (*p == '\n' || readlen <= 0)
9501 {
9502 listitem_T *li;
9503 char_u *s = NULL;
9504 long_u len = p - start;
9505
9506 /* Finished a line. Remove CRs before NL. */
9507 if (readlen > 0 && !binary)
9508 {
9509 while (len > 0 && start[len - 1] == '\r')
9510 --len;
9511 /* removal may cross back to the "prev" string */
9512 if (len == 0)
9513 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9514 --prevlen;
9515 }
9516 if (prevlen == 0)
9517 s = vim_strnsave(start, (int)len);
9518 else
9519 {
9520 /* Change "prev" buffer to be the right size. This way
9521 * the bytes are only copied once, and very long lines are
9522 * allocated only once. */
9523 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9524 {
9525 mch_memmove(s + prevlen, start, len);
9526 s[prevlen + len] = NUL;
9527 prev = NULL; /* the list will own the string */
9528 prevlen = prevsize = 0;
9529 }
9530 }
9531 if (s == NULL)
9532 {
9533 do_outofmem_msg((long_u) prevlen + len + 1);
9534 failed = TRUE;
9535 break;
9536 }
9537
9538 if ((li = listitem_alloc()) == NULL)
9539 {
9540 vim_free(s);
9541 failed = TRUE;
9542 break;
9543 }
9544 li->li_tv.v_type = VAR_STRING;
9545 li->li_tv.v_lock = 0;
9546 li->li_tv.vval.v_string = s;
9547 list_append(rettv->vval.v_list, li);
9548
9549 start = p + 1; /* step over newline */
9550 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9551 break;
9552 }
9553 else if (*p == NUL)
9554 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009555 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9556 * when finding the BF and check the previous two bytes. */
9557 else if (*p == 0xbf && enc_utf8 && !binary)
9558 {
9559 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9560 * + 1, these may be in the "prev" string. */
9561 char_u back1 = p >= buf + 1 ? p[-1]
9562 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9563 char_u back2 = p >= buf + 2 ? p[-2]
9564 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9565 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9566
9567 if (back2 == 0xef && back1 == 0xbb)
9568 {
9569 char_u *dest = p - 2;
9570
9571 /* Usually a BOM is at the beginning of a file, and so at
9572 * the beginning of a line; then we can just step over it.
9573 */
9574 if (start == dest)
9575 start = p + 1;
9576 else
9577 {
9578 /* have to shuffle buf to close gap */
9579 int adjust_prevlen = 0;
9580
9581 if (dest < buf)
9582 {
9583 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9584 dest = buf;
9585 }
9586 if (readlen > p - buf + 1)
9587 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9588 readlen -= 3 - adjust_prevlen;
9589 prevlen -= adjust_prevlen;
9590 p = dest - 1;
9591 }
9592 }
9593 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009594 } /* for */
9595
9596 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9597 break;
9598 if (start < p)
9599 {
9600 /* There's part of a line in buf, store it in "prev". */
9601 if (p - start + prevlen >= prevsize)
9602 {
9603 /* need bigger "prev" buffer */
9604 char_u *newprev;
9605
9606 /* A common use case is ordinary text files and "prev" gets a
9607 * fragment of a line, so the first allocation is made
9608 * small, to avoid repeatedly 'allocing' large and
9609 * 'reallocing' small. */
9610 if (prevsize == 0)
9611 prevsize = (long)(p - start);
9612 else
9613 {
9614 long grow50pc = (prevsize * 3) / 2;
9615 long growmin = (long)((p - start) * 2 + prevlen);
9616 prevsize = grow50pc > growmin ? grow50pc : growmin;
9617 }
9618 newprev = prev == NULL ? alloc(prevsize)
9619 : vim_realloc(prev, prevsize);
9620 if (newprev == NULL)
9621 {
9622 do_outofmem_msg((long_u)prevsize);
9623 failed = TRUE;
9624 break;
9625 }
9626 prev = newprev;
9627 }
9628 /* Add the line part to end of "prev". */
9629 mch_memmove(prev + prevlen, start, p - start);
9630 prevlen += (long)(p - start);
9631 }
9632 } /* while */
9633
9634 /*
9635 * For a negative line count use only the lines at the end of the file,
9636 * free the rest.
9637 */
9638 if (!failed && maxline < 0)
9639 while (cnt > -maxline)
9640 {
9641 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9642 --cnt;
9643 }
9644
9645 if (failed)
9646 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009647 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009648 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009649 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009650 }
9651
9652 vim_free(prev);
9653 fclose(fd);
9654}
9655
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009656 static void
9657return_register(int regname, typval_T *rettv)
9658{
9659 char_u buf[2] = {0, 0};
9660
9661 buf[0] = (char_u)regname;
9662 rettv->v_type = VAR_STRING;
9663 rettv->vval.v_string = vim_strsave(buf);
9664}
9665
9666/*
9667 * "reg_executing()" function
9668 */
9669 static void
9670f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9671{
9672 return_register(reg_executing, rettv);
9673}
9674
9675/*
9676 * "reg_recording()" function
9677 */
9678 static void
9679f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9680{
9681 return_register(reg_recording, rettv);
9682}
9683
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009684#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009685/*
9686 * Convert a List to proftime_T.
9687 * Return FAIL when there is something wrong.
9688 */
9689 static int
9690list2proftime(typval_T *arg, proftime_T *tm)
9691{
9692 long n1, n2;
9693 int error = FALSE;
9694
9695 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9696 || arg->vval.v_list->lv_len != 2)
9697 return FAIL;
9698 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9699 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009700# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009701 tm->HighPart = n1;
9702 tm->LowPart = n2;
9703# else
9704 tm->tv_sec = n1;
9705 tm->tv_usec = n2;
9706# endif
9707 return error ? FAIL : OK;
9708}
9709#endif /* FEAT_RELTIME */
9710
9711/*
9712 * "reltime()" function
9713 */
9714 static void
9715f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9716{
9717#ifdef FEAT_RELTIME
9718 proftime_T res;
9719 proftime_T start;
9720
9721 if (argvars[0].v_type == VAR_UNKNOWN)
9722 {
9723 /* No arguments: get current time. */
9724 profile_start(&res);
9725 }
9726 else if (argvars[1].v_type == VAR_UNKNOWN)
9727 {
9728 if (list2proftime(&argvars[0], &res) == FAIL)
9729 return;
9730 profile_end(&res);
9731 }
9732 else
9733 {
9734 /* Two arguments: compute the difference. */
9735 if (list2proftime(&argvars[0], &start) == FAIL
9736 || list2proftime(&argvars[1], &res) == FAIL)
9737 return;
9738 profile_sub(&res, &start);
9739 }
9740
9741 if (rettv_list_alloc(rettv) == OK)
9742 {
9743 long n1, n2;
9744
Bram Moolenaar4f974752019-02-17 17:44:42 +01009745# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009746 n1 = res.HighPart;
9747 n2 = res.LowPart;
9748# else
9749 n1 = res.tv_sec;
9750 n2 = res.tv_usec;
9751# endif
9752 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9753 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9754 }
9755#endif
9756}
9757
9758#ifdef FEAT_FLOAT
9759/*
9760 * "reltimefloat()" function
9761 */
9762 static void
9763f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9764{
9765# ifdef FEAT_RELTIME
9766 proftime_T tm;
9767# endif
9768
9769 rettv->v_type = VAR_FLOAT;
9770 rettv->vval.v_float = 0;
9771# ifdef FEAT_RELTIME
9772 if (list2proftime(&argvars[0], &tm) == OK)
9773 rettv->vval.v_float = profile_float(&tm);
9774# endif
9775}
9776#endif
9777
9778/*
9779 * "reltimestr()" function
9780 */
9781 static void
9782f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9783{
9784#ifdef FEAT_RELTIME
9785 proftime_T tm;
9786#endif
9787
9788 rettv->v_type = VAR_STRING;
9789 rettv->vval.v_string = NULL;
9790#ifdef FEAT_RELTIME
9791 if (list2proftime(&argvars[0], &tm) == OK)
9792 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9793#endif
9794}
9795
9796#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009797 static void
9798make_connection(void)
9799{
9800 if (X_DISPLAY == NULL
9801# ifdef FEAT_GUI
9802 && !gui.in_use
9803# endif
9804 )
9805 {
9806 x_force_connect = TRUE;
9807 setup_term_clip();
9808 x_force_connect = FALSE;
9809 }
9810}
9811
9812 static int
9813check_connection(void)
9814{
9815 make_connection();
9816 if (X_DISPLAY == NULL)
9817 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009818 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009819 return FAIL;
9820 }
9821 return OK;
9822}
9823#endif
9824
9825#ifdef FEAT_CLIENTSERVER
9826 static void
9827remote_common(typval_T *argvars, typval_T *rettv, int expr)
9828{
9829 char_u *server_name;
9830 char_u *keys;
9831 char_u *r = NULL;
9832 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009833 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009834# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009835 HWND w;
9836# else
9837 Window w;
9838# endif
9839
9840 if (check_restricted() || check_secure())
9841 return;
9842
9843# ifdef FEAT_X11
9844 if (check_connection() == FAIL)
9845 return;
9846# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009847 if (argvars[2].v_type != VAR_UNKNOWN
9848 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009849 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009850
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009851 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009852 if (server_name == NULL)
9853 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009854 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009855# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009856 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009857# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009858 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9859 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009860# endif
9861 {
9862 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009863 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009864 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009865 vim_free(r);
9866 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009867 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009868 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009869 return;
9870 }
9871
9872 rettv->vval.v_string = r;
9873
9874 if (argvars[2].v_type != VAR_UNKNOWN)
9875 {
9876 dictitem_T v;
9877 char_u str[30];
9878 char_u *idvar;
9879
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009880 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009881 if (idvar != NULL && *idvar != NUL)
9882 {
9883 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9884 v.di_tv.v_type = VAR_STRING;
9885 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009886 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009887 vim_free(v.di_tv.vval.v_string);
9888 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009889 }
9890}
9891#endif
9892
9893/*
9894 * "remote_expr()" function
9895 */
9896 static void
9897f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9898{
9899 rettv->v_type = VAR_STRING;
9900 rettv->vval.v_string = NULL;
9901#ifdef FEAT_CLIENTSERVER
9902 remote_common(argvars, rettv, TRUE);
9903#endif
9904}
9905
9906/*
9907 * "remote_foreground()" function
9908 */
9909 static void
9910f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9911{
9912#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009913# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009914 /* On Win32 it's done in this application. */
9915 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009916 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009917
9918 if (server_name != NULL)
9919 serverForeground(server_name);
9920 }
9921# else
9922 /* Send a foreground() expression to the server. */
9923 argvars[1].v_type = VAR_STRING;
9924 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9925 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009926 rettv->v_type = VAR_STRING;
9927 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009928 remote_common(argvars, rettv, TRUE);
9929 vim_free(argvars[1].vval.v_string);
9930# endif
9931#endif
9932}
9933
9934 static void
9935f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9936{
9937#ifdef FEAT_CLIENTSERVER
9938 dictitem_T v;
9939 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009940# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009941 long_u n = 0;
9942# endif
9943 char_u *serverid;
9944
9945 if (check_restricted() || check_secure())
9946 {
9947 rettv->vval.v_number = -1;
9948 return;
9949 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009950 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009951 if (serverid == NULL)
9952 {
9953 rettv->vval.v_number = -1;
9954 return; /* type error; errmsg already given */
9955 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009956# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009957 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9958 if (n == 0)
9959 rettv->vval.v_number = -1;
9960 else
9961 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009962 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009963 rettv->vval.v_number = (s != NULL);
9964 }
9965# else
9966 if (check_connection() == FAIL)
9967 return;
9968
9969 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9970 serverStrToWin(serverid), &s);
9971# endif
9972
9973 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9974 {
9975 char_u *retvar;
9976
9977 v.di_tv.v_type = VAR_STRING;
9978 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009979 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009980 if (retvar != NULL)
9981 set_var(retvar, &v.di_tv, FALSE);
9982 vim_free(v.di_tv.vval.v_string);
9983 }
9984#else
9985 rettv->vval.v_number = -1;
9986#endif
9987}
9988
9989 static void
9990f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9991{
9992 char_u *r = NULL;
9993
9994#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009995 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009996
9997 if (serverid != NULL && !check_restricted() && !check_secure())
9998 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009999 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010000# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010001 /* The server's HWND is encoded in the 'id' parameter */
10002 long_u n = 0;
10003# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010004
10005 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010006 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010007
Bram Moolenaar4f974752019-02-17 17:44:42 +010010008# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010009 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10010 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010011 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010012 if (r == NULL)
10013# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010014 if (check_connection() == FAIL
10015 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10016 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010017# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010018 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010019 }
10020#endif
10021 rettv->v_type = VAR_STRING;
10022 rettv->vval.v_string = r;
10023}
10024
10025/*
10026 * "remote_send()" function
10027 */
10028 static void
10029f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10030{
10031 rettv->v_type = VAR_STRING;
10032 rettv->vval.v_string = NULL;
10033#ifdef FEAT_CLIENTSERVER
10034 remote_common(argvars, rettv, FALSE);
10035#endif
10036}
10037
10038/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010039 * "remote_startserver()" function
10040 */
10041 static void
10042f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10043{
10044#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010045 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010046
10047 if (server == NULL)
10048 return; /* type error; errmsg already given */
10049 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010050 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010051 else
10052 {
10053# ifdef FEAT_X11
10054 if (check_connection() == OK)
10055 serverRegisterName(X_DISPLAY, server);
10056# else
10057 serverSetName(server);
10058# endif
10059 }
10060#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010061 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010062#endif
10063}
10064
10065/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010066 * "remove()" function
10067 */
10068 static void
10069f_remove(typval_T *argvars, typval_T *rettv)
10070{
10071 list_T *l;
10072 listitem_T *item, *item2;
10073 listitem_T *li;
10074 long idx;
10075 long end;
10076 char_u *key;
10077 dict_T *d;
10078 dictitem_T *di;
10079 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010080 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010081
10082 if (argvars[0].v_type == VAR_DICT)
10083 {
10084 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010085 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010086 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010087 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010088 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010089 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010090 if (key != NULL)
10091 {
10092 di = dict_find(d, key, -1);
10093 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010094 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010095 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10096 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10097 {
10098 *rettv = di->di_tv;
10099 init_tv(&di->di_tv);
10100 dictitem_remove(d, di);
10101 }
10102 }
10103 }
10104 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010105 else if (argvars[0].v_type == VAR_BLOB)
10106 {
10107 idx = (long)tv_get_number_chk(&argvars[1], &error);
10108 if (!error)
10109 {
10110 blob_T *b = argvars[0].vval.v_blob;
10111 int len = blob_len(b);
10112 char_u *p;
10113
10114 if (idx < 0)
10115 // count from the end
10116 idx = len + idx;
10117 if (idx < 0 || idx >= len)
10118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010119 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010120 return;
10121 }
10122 if (argvars[2].v_type == VAR_UNKNOWN)
10123 {
10124 // Remove one item, return its value.
10125 p = (char_u *)b->bv_ga.ga_data;
10126 rettv->vval.v_number = (varnumber_T) *(p + idx);
10127 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10128 --b->bv_ga.ga_len;
10129 }
10130 else
10131 {
10132 blob_T *blob;
10133
10134 // Remove range of items, return list with values.
10135 end = (long)tv_get_number_chk(&argvars[2], &error);
10136 if (error)
10137 return;
10138 if (end < 0)
10139 // count from the end
10140 end = len + end;
10141 if (end >= len || idx > end)
10142 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010143 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010144 return;
10145 }
10146 blob = blob_alloc();
10147 if (blob == NULL)
10148 return;
10149 blob->bv_ga.ga_len = end - idx + 1;
10150 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10151 {
10152 vim_free(blob);
10153 return;
10154 }
10155 p = (char_u *)b->bv_ga.ga_data;
10156 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10157 (size_t)(end - idx + 1));
10158 ++blob->bv_refcount;
10159 rettv->v_type = VAR_BLOB;
10160 rettv->vval.v_blob = blob;
10161
10162 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10163 b->bv_ga.ga_len -= end - idx + 1;
10164 }
10165 }
10166 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010167 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010168 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010169 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010170 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010171 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010172 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010173 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010174 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010175 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010176 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010177 else
10178 {
10179 if (argvars[2].v_type == VAR_UNKNOWN)
10180 {
10181 /* Remove one item, return its value. */
10182 vimlist_remove(l, item, item);
10183 *rettv = item->li_tv;
10184 vim_free(item);
10185 }
10186 else
10187 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010188 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010189 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010190 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010191 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010192 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010193 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010194 else
10195 {
10196 int cnt = 0;
10197
10198 for (li = item; li != NULL; li = li->li_next)
10199 {
10200 ++cnt;
10201 if (li == item2)
10202 break;
10203 }
10204 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010205 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010206 else
10207 {
10208 vimlist_remove(l, item, item2);
10209 if (rettv_list_alloc(rettv) == OK)
10210 {
10211 l = rettv->vval.v_list;
10212 l->lv_first = item;
10213 l->lv_last = item2;
10214 item->li_prev = NULL;
10215 item2->li_next = NULL;
10216 l->lv_len = cnt;
10217 }
10218 }
10219 }
10220 }
10221 }
10222 }
10223}
10224
10225/*
10226 * "rename({from}, {to})" function
10227 */
10228 static void
10229f_rename(typval_T *argvars, typval_T *rettv)
10230{
10231 char_u buf[NUMBUFLEN];
10232
10233 if (check_restricted() || check_secure())
10234 rettv->vval.v_number = -1;
10235 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010236 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10237 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010238}
10239
10240/*
10241 * "repeat()" function
10242 */
10243 static void
10244f_repeat(typval_T *argvars, typval_T *rettv)
10245{
10246 char_u *p;
10247 int n;
10248 int slen;
10249 int len;
10250 char_u *r;
10251 int i;
10252
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010253 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010254 if (argvars[0].v_type == VAR_LIST)
10255 {
10256 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10257 while (n-- > 0)
10258 if (list_extend(rettv->vval.v_list,
10259 argvars[0].vval.v_list, NULL) == FAIL)
10260 break;
10261 }
10262 else
10263 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010264 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010265 rettv->v_type = VAR_STRING;
10266 rettv->vval.v_string = NULL;
10267
10268 slen = (int)STRLEN(p);
10269 len = slen * n;
10270 if (len <= 0)
10271 return;
10272
10273 r = alloc(len + 1);
10274 if (r != NULL)
10275 {
10276 for (i = 0; i < n; i++)
10277 mch_memmove(r + i * slen, p, (size_t)slen);
10278 r[len] = NUL;
10279 }
10280
10281 rettv->vval.v_string = r;
10282 }
10283}
10284
10285/*
10286 * "resolve()" function
10287 */
10288 static void
10289f_resolve(typval_T *argvars, typval_T *rettv)
10290{
10291 char_u *p;
10292#ifdef HAVE_READLINK
10293 char_u *buf = NULL;
10294#endif
10295
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010296 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010297#ifdef FEAT_SHORTCUT
10298 {
10299 char_u *v = NULL;
10300
Bram Moolenaardce1e892019-02-10 23:18:53 +010010301 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010302 if (v != NULL)
10303 rettv->vval.v_string = v;
10304 else
10305 rettv->vval.v_string = vim_strsave(p);
10306 }
10307#else
10308# ifdef HAVE_READLINK
10309 {
10310 char_u *cpy;
10311 int len;
10312 char_u *remain = NULL;
10313 char_u *q;
10314 int is_relative_to_current = FALSE;
10315 int has_trailing_pathsep = FALSE;
10316 int limit = 100;
10317
10318 p = vim_strsave(p);
10319
10320 if (p[0] == '.' && (vim_ispathsep(p[1])
10321 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10322 is_relative_to_current = TRUE;
10323
10324 len = STRLEN(p);
10325 if (len > 0 && after_pathsep(p, p + len))
10326 {
10327 has_trailing_pathsep = TRUE;
10328 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10329 }
10330
10331 q = getnextcomp(p);
10332 if (*q != NUL)
10333 {
10334 /* Separate the first path component in "p", and keep the
10335 * remainder (beginning with the path separator). */
10336 remain = vim_strsave(q - 1);
10337 q[-1] = NUL;
10338 }
10339
10340 buf = alloc(MAXPATHL + 1);
10341 if (buf == NULL)
10342 goto fail;
10343
10344 for (;;)
10345 {
10346 for (;;)
10347 {
10348 len = readlink((char *)p, (char *)buf, MAXPATHL);
10349 if (len <= 0)
10350 break;
10351 buf[len] = NUL;
10352
10353 if (limit-- == 0)
10354 {
10355 vim_free(p);
10356 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010357 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010358 rettv->vval.v_string = NULL;
10359 goto fail;
10360 }
10361
10362 /* Ensure that the result will have a trailing path separator
10363 * if the argument has one. */
10364 if (remain == NULL && has_trailing_pathsep)
10365 add_pathsep(buf);
10366
10367 /* Separate the first path component in the link value and
10368 * concatenate the remainders. */
10369 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10370 if (*q != NUL)
10371 {
10372 if (remain == NULL)
10373 remain = vim_strsave(q - 1);
10374 else
10375 {
10376 cpy = concat_str(q - 1, remain);
10377 if (cpy != NULL)
10378 {
10379 vim_free(remain);
10380 remain = cpy;
10381 }
10382 }
10383 q[-1] = NUL;
10384 }
10385
10386 q = gettail(p);
10387 if (q > p && *q == NUL)
10388 {
10389 /* Ignore trailing path separator. */
10390 q[-1] = NUL;
10391 q = gettail(p);
10392 }
10393 if (q > p && !mch_isFullName(buf))
10394 {
10395 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010396 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010397 if (cpy != NULL)
10398 {
10399 STRCPY(cpy, p);
10400 STRCPY(gettail(cpy), buf);
10401 vim_free(p);
10402 p = cpy;
10403 }
10404 }
10405 else
10406 {
10407 vim_free(p);
10408 p = vim_strsave(buf);
10409 }
10410 }
10411
10412 if (remain == NULL)
10413 break;
10414
10415 /* Append the first path component of "remain" to "p". */
10416 q = getnextcomp(remain + 1);
10417 len = q - remain - (*q != NUL);
10418 cpy = vim_strnsave(p, STRLEN(p) + len);
10419 if (cpy != NULL)
10420 {
10421 STRNCAT(cpy, remain, len);
10422 vim_free(p);
10423 p = cpy;
10424 }
10425 /* Shorten "remain". */
10426 if (*q != NUL)
10427 STRMOVE(remain, q - 1);
10428 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010429 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010430 }
10431
10432 /* If the result is a relative path name, make it explicitly relative to
10433 * the current directory if and only if the argument had this form. */
10434 if (!vim_ispathsep(*p))
10435 {
10436 if (is_relative_to_current
10437 && *p != NUL
10438 && !(p[0] == '.'
10439 && (p[1] == NUL
10440 || vim_ispathsep(p[1])
10441 || (p[1] == '.'
10442 && (p[2] == NUL
10443 || vim_ispathsep(p[2]))))))
10444 {
10445 /* Prepend "./". */
10446 cpy = concat_str((char_u *)"./", p);
10447 if (cpy != NULL)
10448 {
10449 vim_free(p);
10450 p = cpy;
10451 }
10452 }
10453 else if (!is_relative_to_current)
10454 {
10455 /* Strip leading "./". */
10456 q = p;
10457 while (q[0] == '.' && vim_ispathsep(q[1]))
10458 q += 2;
10459 if (q > p)
10460 STRMOVE(p, p + 2);
10461 }
10462 }
10463
10464 /* Ensure that the result will have no trailing path separator
10465 * if the argument had none. But keep "/" or "//". */
10466 if (!has_trailing_pathsep)
10467 {
10468 q = p + STRLEN(p);
10469 if (after_pathsep(p, q))
10470 *gettail_sep(p) = NUL;
10471 }
10472
10473 rettv->vval.v_string = p;
10474 }
10475# else
10476 rettv->vval.v_string = vim_strsave(p);
10477# endif
10478#endif
10479
10480 simplify_filename(rettv->vval.v_string);
10481
10482#ifdef HAVE_READLINK
10483fail:
10484 vim_free(buf);
10485#endif
10486 rettv->v_type = VAR_STRING;
10487}
10488
10489/*
10490 * "reverse({list})" function
10491 */
10492 static void
10493f_reverse(typval_T *argvars, typval_T *rettv)
10494{
10495 list_T *l;
10496 listitem_T *li, *ni;
10497
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010498 if (argvars[0].v_type == VAR_BLOB)
10499 {
10500 blob_T *b = argvars[0].vval.v_blob;
10501 int i, len = blob_len(b);
10502
10503 for (i = 0; i < len / 2; i++)
10504 {
10505 int tmp = blob_get(b, i);
10506
10507 blob_set(b, i, blob_get(b, len - i - 1));
10508 blob_set(b, len - i - 1, tmp);
10509 }
10510 rettv_blob_set(rettv, b);
10511 return;
10512 }
10513
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010514 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010515 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010516 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010517 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010518 (char_u *)N_("reverse() argument"), TRUE))
10519 {
10520 li = l->lv_last;
10521 l->lv_first = l->lv_last = NULL;
10522 l->lv_len = 0;
10523 while (li != NULL)
10524 {
10525 ni = li->li_prev;
10526 list_append(l, li);
10527 li = ni;
10528 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010529 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010530 l->lv_idx = l->lv_len - l->lv_idx - 1;
10531 }
10532}
10533
10534#define SP_NOMOVE 0x01 /* don't move cursor */
10535#define SP_REPEAT 0x02 /* repeat to find outer pair */
10536#define SP_RETCOUNT 0x04 /* return matchcount */
10537#define SP_SETPCMARK 0x08 /* set previous context mark */
10538#define SP_START 0x10 /* accept match at start position */
10539#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10540#define SP_END 0x40 /* leave cursor at end of match */
10541#define SP_COLUMN 0x80 /* start at cursor column */
10542
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010543/*
10544 * Get flags for a search function.
10545 * Possibly sets "p_ws".
10546 * Returns BACKWARD, FORWARD or zero (for an error).
10547 */
10548 static int
10549get_search_arg(typval_T *varp, int *flagsp)
10550{
10551 int dir = FORWARD;
10552 char_u *flags;
10553 char_u nbuf[NUMBUFLEN];
10554 int mask;
10555
10556 if (varp->v_type != VAR_UNKNOWN)
10557 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010558 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010559 if (flags == NULL)
10560 return 0; /* type error; errmsg already given */
10561 while (*flags != NUL)
10562 {
10563 switch (*flags)
10564 {
10565 case 'b': dir = BACKWARD; break;
10566 case 'w': p_ws = TRUE; break;
10567 case 'W': p_ws = FALSE; break;
10568 default: mask = 0;
10569 if (flagsp != NULL)
10570 switch (*flags)
10571 {
10572 case 'c': mask = SP_START; break;
10573 case 'e': mask = SP_END; break;
10574 case 'm': mask = SP_RETCOUNT; break;
10575 case 'n': mask = SP_NOMOVE; break;
10576 case 'p': mask = SP_SUBPAT; break;
10577 case 'r': mask = SP_REPEAT; break;
10578 case 's': mask = SP_SETPCMARK; break;
10579 case 'z': mask = SP_COLUMN; break;
10580 }
10581 if (mask == 0)
10582 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010583 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010584 dir = 0;
10585 }
10586 else
10587 *flagsp |= mask;
10588 }
10589 if (dir == 0)
10590 break;
10591 ++flags;
10592 }
10593 }
10594 return dir;
10595}
10596
10597/*
10598 * Shared by search() and searchpos() functions.
10599 */
10600 static int
10601search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10602{
10603 int flags;
10604 char_u *pat;
10605 pos_T pos;
10606 pos_T save_cursor;
10607 int save_p_ws = p_ws;
10608 int dir;
10609 int retval = 0; /* default: FAIL */
10610 long lnum_stop = 0;
10611 proftime_T tm;
10612#ifdef FEAT_RELTIME
10613 long time_limit = 0;
10614#endif
10615 int options = SEARCH_KEEP;
10616 int subpatnum;
10617
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010618 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10620 if (dir == 0)
10621 goto theend;
10622 flags = *flagsp;
10623 if (flags & SP_START)
10624 options |= SEARCH_START;
10625 if (flags & SP_END)
10626 options |= SEARCH_END;
10627 if (flags & SP_COLUMN)
10628 options |= SEARCH_COL;
10629
10630 /* Optional arguments: line number to stop searching and timeout. */
10631 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10632 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010633 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010634 if (lnum_stop < 0)
10635 goto theend;
10636#ifdef FEAT_RELTIME
10637 if (argvars[3].v_type != VAR_UNKNOWN)
10638 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010639 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010640 if (time_limit < 0)
10641 goto theend;
10642 }
10643#endif
10644 }
10645
10646#ifdef FEAT_RELTIME
10647 /* Set the time limit, if there is one. */
10648 profile_setlimit(time_limit, &tm);
10649#endif
10650
10651 /*
10652 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10653 * Check to make sure only those flags are set.
10654 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10655 * flags cannot be set. Check for that condition also.
10656 */
10657 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10658 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10659 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010660 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010661 goto theend;
10662 }
10663
10664 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010665 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010666 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010667 if (subpatnum != FAIL)
10668 {
10669 if (flags & SP_SUBPAT)
10670 retval = subpatnum;
10671 else
10672 retval = pos.lnum;
10673 if (flags & SP_SETPCMARK)
10674 setpcmark();
10675 curwin->w_cursor = pos;
10676 if (match_pos != NULL)
10677 {
10678 /* Store the match cursor position */
10679 match_pos->lnum = pos.lnum;
10680 match_pos->col = pos.col + 1;
10681 }
10682 /* "/$" will put the cursor after the end of the line, may need to
10683 * correct that here */
10684 check_cursor();
10685 }
10686
10687 /* If 'n' flag is used: restore cursor position. */
10688 if (flags & SP_NOMOVE)
10689 curwin->w_cursor = save_cursor;
10690 else
10691 curwin->w_set_curswant = TRUE;
10692theend:
10693 p_ws = save_p_ws;
10694
10695 return retval;
10696}
10697
10698#ifdef FEAT_FLOAT
10699
10700/*
10701 * round() is not in C90, use ceil() or floor() instead.
10702 */
10703 float_T
10704vim_round(float_T f)
10705{
10706 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10707}
10708
10709/*
10710 * "round({float})" function
10711 */
10712 static void
10713f_round(typval_T *argvars, typval_T *rettv)
10714{
10715 float_T f = 0.0;
10716
10717 rettv->v_type = VAR_FLOAT;
10718 if (get_float_arg(argvars, &f) == OK)
10719 rettv->vval.v_float = vim_round(f);
10720 else
10721 rettv->vval.v_float = 0.0;
10722}
10723#endif
10724
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010725#ifdef FEAT_RUBY
10726/*
10727 * "rubyeval()" function
10728 */
10729 static void
10730f_rubyeval(typval_T *argvars, typval_T *rettv)
10731{
10732 char_u *str;
10733 char_u buf[NUMBUFLEN];
10734
10735 str = tv_get_string_buf(&argvars[0], buf);
10736 do_rubyeval(str, rettv);
10737}
10738#endif
10739
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010740/*
10741 * "screenattr()" function
10742 */
10743 static void
10744f_screenattr(typval_T *argvars, typval_T *rettv)
10745{
10746 int row;
10747 int col;
10748 int c;
10749
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010750 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10751 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010752 if (row < 0 || row >= screen_Rows
10753 || col < 0 || col >= screen_Columns)
10754 c = -1;
10755 else
10756 c = ScreenAttrs[LineOffset[row] + col];
10757 rettv->vval.v_number = c;
10758}
10759
10760/*
10761 * "screenchar()" function
10762 */
10763 static void
10764f_screenchar(typval_T *argvars, typval_T *rettv)
10765{
10766 int row;
10767 int col;
10768 int off;
10769 int c;
10770
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010771 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10772 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010773 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010774 c = -1;
10775 else
10776 {
10777 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010778 if (enc_utf8 && ScreenLinesUC[off] != 0)
10779 c = ScreenLinesUC[off];
10780 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010781 c = ScreenLines[off];
10782 }
10783 rettv->vval.v_number = c;
10784}
10785
10786/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010787 * "screenchars()" function
10788 */
10789 static void
10790f_screenchars(typval_T *argvars, typval_T *rettv)
10791{
10792 int row;
10793 int col;
10794 int off;
10795 int c;
10796 int i;
10797
10798 if (rettv_list_alloc(rettv) == FAIL)
10799 return;
10800 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10801 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10802 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10803 return;
10804
10805 off = LineOffset[row] + col;
10806 if (enc_utf8 && ScreenLinesUC[off] != 0)
10807 c = ScreenLinesUC[off];
10808 else
10809 c = ScreenLines[off];
10810 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10811
10812 if (enc_utf8)
10813
10814 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10815 list_append_number(rettv->vval.v_list,
10816 (varnumber_T)ScreenLinesC[i][off]);
10817}
10818
10819/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010820 * "screencol()" function
10821 *
10822 * First column is 1 to be consistent with virtcol().
10823 */
10824 static void
10825f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10826{
10827 rettv->vval.v_number = screen_screencol() + 1;
10828}
10829
10830/*
10831 * "screenrow()" function
10832 */
10833 static void
10834f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10835{
10836 rettv->vval.v_number = screen_screenrow() + 1;
10837}
10838
10839/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010840 * "screenstring()" function
10841 */
10842 static void
10843f_screenstring(typval_T *argvars, typval_T *rettv)
10844{
10845 int row;
10846 int col;
10847 int off;
10848 int c;
10849 int i;
10850 char_u buf[MB_MAXBYTES + 1];
10851 int buflen = 0;
10852
10853 rettv->vval.v_string = NULL;
10854 rettv->v_type = VAR_STRING;
10855
10856 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10857 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10858 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10859 return;
10860
10861 off = LineOffset[row] + col;
10862 if (enc_utf8 && ScreenLinesUC[off] != 0)
10863 c = ScreenLinesUC[off];
10864 else
10865 c = ScreenLines[off];
10866 buflen += mb_char2bytes(c, buf);
10867
10868 if (enc_utf8)
10869 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10870 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10871
10872 buf[buflen] = NUL;
10873 rettv->vval.v_string = vim_strsave(buf);
10874}
10875
10876/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010877 * "search()" function
10878 */
10879 static void
10880f_search(typval_T *argvars, typval_T *rettv)
10881{
10882 int flags = 0;
10883
10884 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10885}
10886
10887/*
10888 * "searchdecl()" function
10889 */
10890 static void
10891f_searchdecl(typval_T *argvars, typval_T *rettv)
10892{
10893 int locally = 1;
10894 int thisblock = 0;
10895 int error = FALSE;
10896 char_u *name;
10897
10898 rettv->vval.v_number = 1; /* default: FAIL */
10899
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010900 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010901 if (argvars[1].v_type != VAR_UNKNOWN)
10902 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010903 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010904 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010905 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010906 }
10907 if (!error && name != NULL)
10908 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10909 locally, thisblock, SEARCH_KEEP) == FAIL;
10910}
10911
10912/*
10913 * Used by searchpair() and searchpairpos()
10914 */
10915 static int
10916searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10917{
10918 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010919 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010920 int save_p_ws = p_ws;
10921 int dir;
10922 int flags = 0;
10923 char_u nbuf1[NUMBUFLEN];
10924 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010925 int retval = 0; /* default: FAIL */
10926 long lnum_stop = 0;
10927 long time_limit = 0;
10928
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010929 /* Get the three pattern arguments: start, middle, end. Will result in an
10930 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010931 spat = tv_get_string_chk(&argvars[0]);
10932 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10933 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010934 if (spat == NULL || mpat == NULL || epat == NULL)
10935 goto theend; /* type error */
10936
10937 /* Handle the optional fourth argument: flags */
10938 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10939 if (dir == 0)
10940 goto theend;
10941
10942 /* Don't accept SP_END or SP_SUBPAT.
10943 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10944 */
10945 if ((flags & (SP_END | SP_SUBPAT)) != 0
10946 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10947 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010948 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010949 goto theend;
10950 }
10951
10952 /* Using 'r' implies 'W', otherwise it doesn't work. */
10953 if (flags & SP_REPEAT)
10954 p_ws = FALSE;
10955
10956 /* Optional fifth argument: skip expression */
10957 if (argvars[3].v_type == VAR_UNKNOWN
10958 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010959 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010960 else
10961 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010962 skip = &argvars[4];
10963 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10964 && skip->v_type != VAR_STRING)
10965 {
10966 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010967 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010968 goto theend;
10969 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010970 if (argvars[5].v_type != VAR_UNKNOWN)
10971 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010972 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010973 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010974 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010975 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010976 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010977 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010978#ifdef FEAT_RELTIME
10979 if (argvars[6].v_type != VAR_UNKNOWN)
10980 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010981 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010982 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010983 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010984 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010985 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010986 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010987 }
10988#endif
10989 }
10990 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010991
10992 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10993 match_pos, lnum_stop, time_limit);
10994
10995theend:
10996 p_ws = save_p_ws;
10997
10998 return retval;
10999}
11000
11001/*
11002 * "searchpair()" function
11003 */
11004 static void
11005f_searchpair(typval_T *argvars, typval_T *rettv)
11006{
11007 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11008}
11009
11010/*
11011 * "searchpairpos()" function
11012 */
11013 static void
11014f_searchpairpos(typval_T *argvars, typval_T *rettv)
11015{
11016 pos_T match_pos;
11017 int lnum = 0;
11018 int col = 0;
11019
11020 if (rettv_list_alloc(rettv) == FAIL)
11021 return;
11022
11023 if (searchpair_cmn(argvars, &match_pos) > 0)
11024 {
11025 lnum = match_pos.lnum;
11026 col = match_pos.col;
11027 }
11028
11029 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11030 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11031}
11032
11033/*
11034 * Search for a start/middle/end thing.
11035 * Used by searchpair(), see its documentation for the details.
11036 * Returns 0 or -1 for no match,
11037 */
11038 long
11039do_searchpair(
11040 char_u *spat, /* start pattern */
11041 char_u *mpat, /* middle pattern */
11042 char_u *epat, /* end pattern */
11043 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011044 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011045 int flags, /* SP_SETPCMARK and other SP_ values */
11046 pos_T *match_pos,
11047 linenr_T lnum_stop, /* stop at this line if not zero */
11048 long time_limit UNUSED) /* stop after this many msec */
11049{
11050 char_u *save_cpo;
11051 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11052 long retval = 0;
11053 pos_T pos;
11054 pos_T firstpos;
11055 pos_T foundpos;
11056 pos_T save_cursor;
11057 pos_T save_pos;
11058 int n;
11059 int r;
11060 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011061 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011062 int err;
11063 int options = SEARCH_KEEP;
11064 proftime_T tm;
11065
11066 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11067 save_cpo = p_cpo;
11068 p_cpo = empty_option;
11069
11070#ifdef FEAT_RELTIME
11071 /* Set the time limit, if there is one. */
11072 profile_setlimit(time_limit, &tm);
11073#endif
11074
11075 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11076 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011077 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11078 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011079 if (pat2 == NULL || pat3 == NULL)
11080 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011081 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011082 if (*mpat == NUL)
11083 STRCPY(pat3, pat2);
11084 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011085 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011086 spat, epat, mpat);
11087 if (flags & SP_START)
11088 options |= SEARCH_START;
11089
Bram Moolenaar48570482017-10-30 21:48:41 +010011090 if (skip != NULL)
11091 {
11092 /* Empty string means to not use the skip expression. */
11093 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11094 use_skip = skip->vval.v_string != NULL
11095 && *skip->vval.v_string != NUL;
11096 }
11097
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011098 save_cursor = curwin->w_cursor;
11099 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011100 CLEAR_POS(&firstpos);
11101 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011102 pat = pat3;
11103 for (;;)
11104 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011105 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011106 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011107 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011108 /* didn't find it or found the first match again: FAIL */
11109 break;
11110
11111 if (firstpos.lnum == 0)
11112 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011113 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011114 {
11115 /* Found the same position again. Can happen with a pattern that
11116 * has "\zs" at the end and searching backwards. Advance one
11117 * character and try again. */
11118 if (dir == BACKWARD)
11119 decl(&pos);
11120 else
11121 incl(&pos);
11122 }
11123 foundpos = pos;
11124
11125 /* clear the start flag to avoid getting stuck here */
11126 options &= ~SEARCH_START;
11127
11128 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011129 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011130 {
11131 save_pos = curwin->w_cursor;
11132 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011133 err = FALSE;
11134 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011135 curwin->w_cursor = save_pos;
11136 if (err)
11137 {
11138 /* Evaluating {skip} caused an error, break here. */
11139 curwin->w_cursor = save_cursor;
11140 retval = -1;
11141 break;
11142 }
11143 if (r)
11144 continue;
11145 }
11146
11147 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11148 {
11149 /* Found end when searching backwards or start when searching
11150 * forward: nested pair. */
11151 ++nest;
11152 pat = pat2; /* nested, don't search for middle */
11153 }
11154 else
11155 {
11156 /* Found end when searching forward or start when searching
11157 * backward: end of (nested) pair; or found middle in outer pair. */
11158 if (--nest == 1)
11159 pat = pat3; /* outer level, search for middle */
11160 }
11161
11162 if (nest == 0)
11163 {
11164 /* Found the match: return matchcount or line number. */
11165 if (flags & SP_RETCOUNT)
11166 ++retval;
11167 else
11168 retval = pos.lnum;
11169 if (flags & SP_SETPCMARK)
11170 setpcmark();
11171 curwin->w_cursor = pos;
11172 if (!(flags & SP_REPEAT))
11173 break;
11174 nest = 1; /* search for next unmatched */
11175 }
11176 }
11177
11178 if (match_pos != NULL)
11179 {
11180 /* Store the match cursor position */
11181 match_pos->lnum = curwin->w_cursor.lnum;
11182 match_pos->col = curwin->w_cursor.col + 1;
11183 }
11184
11185 /* If 'n' flag is used or search failed: restore cursor position. */
11186 if ((flags & SP_NOMOVE) || retval == 0)
11187 curwin->w_cursor = save_cursor;
11188
11189theend:
11190 vim_free(pat2);
11191 vim_free(pat3);
11192 if (p_cpo == empty_option)
11193 p_cpo = save_cpo;
11194 else
11195 /* Darn, evaluating the {skip} expression changed the value. */
11196 free_string_option(save_cpo);
11197
11198 return retval;
11199}
11200
11201/*
11202 * "searchpos()" function
11203 */
11204 static void
11205f_searchpos(typval_T *argvars, typval_T *rettv)
11206{
11207 pos_T match_pos;
11208 int lnum = 0;
11209 int col = 0;
11210 int n;
11211 int flags = 0;
11212
11213 if (rettv_list_alloc(rettv) == FAIL)
11214 return;
11215
11216 n = search_cmn(argvars, &match_pos, &flags);
11217 if (n > 0)
11218 {
11219 lnum = match_pos.lnum;
11220 col = match_pos.col;
11221 }
11222
11223 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11224 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11225 if (flags & SP_SUBPAT)
11226 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11227}
11228
11229 static void
11230f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11231{
11232#ifdef FEAT_CLIENTSERVER
11233 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011234 char_u *server = tv_get_string_chk(&argvars[0]);
11235 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011236
11237 rettv->vval.v_number = -1;
11238 if (server == NULL || reply == NULL)
11239 return;
11240 if (check_restricted() || check_secure())
11241 return;
11242# ifdef FEAT_X11
11243 if (check_connection() == FAIL)
11244 return;
11245# endif
11246
11247 if (serverSendReply(server, reply) < 0)
11248 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011249 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011250 return;
11251 }
11252 rettv->vval.v_number = 0;
11253#else
11254 rettv->vval.v_number = -1;
11255#endif
11256}
11257
11258 static void
11259f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11260{
11261 char_u *r = NULL;
11262
11263#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011264# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011265 r = serverGetVimNames();
11266# else
11267 make_connection();
11268 if (X_DISPLAY != NULL)
11269 r = serverGetVimNames(X_DISPLAY);
11270# endif
11271#endif
11272 rettv->v_type = VAR_STRING;
11273 rettv->vval.v_string = r;
11274}
11275
11276/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011277 * "setbufline()" function
11278 */
11279 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011280f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011281{
11282 linenr_T lnum;
11283 buf_T *buf;
11284
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011285 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011286 if (buf == NULL)
11287 rettv->vval.v_number = 1; /* FAIL */
11288 else
11289 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011290 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011291 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011292 }
11293}
11294
11295/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011296 * "setbufvar()" function
11297 */
11298 static void
11299f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11300{
11301 buf_T *buf;
11302 char_u *varname, *bufvarname;
11303 typval_T *varp;
11304 char_u nbuf[NUMBUFLEN];
11305
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011306 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011307 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011308 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11309 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011310 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011311 varp = &argvars[2];
11312
11313 if (buf != NULL && varname != NULL && varp != NULL)
11314 {
11315 if (*varname == '&')
11316 {
11317 long numval;
11318 char_u *strval;
11319 int error = FALSE;
11320 aco_save_T aco;
11321
11322 /* set curbuf to be our buf, temporarily */
11323 aucmd_prepbuf(&aco, buf);
11324
11325 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011326 numval = (long)tv_get_number_chk(varp, &error);
11327 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011328 if (!error && strval != NULL)
11329 set_option_value(varname, numval, strval, OPT_LOCAL);
11330
11331 /* reset notion of buffer */
11332 aucmd_restbuf(&aco);
11333 }
11334 else
11335 {
11336 buf_T *save_curbuf = curbuf;
11337
Bram Moolenaar964b3742019-05-24 18:54:09 +020011338 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011339 if (bufvarname != NULL)
11340 {
11341 curbuf = buf;
11342 STRCPY(bufvarname, "b:");
11343 STRCPY(bufvarname + 2, varname);
11344 set_var(bufvarname, varp, TRUE);
11345 vim_free(bufvarname);
11346 curbuf = save_curbuf;
11347 }
11348 }
11349 }
11350}
11351
11352 static void
11353f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11354{
11355 dict_T *d;
11356 dictitem_T *di;
11357 char_u *csearch;
11358
11359 if (argvars[0].v_type != VAR_DICT)
11360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011361 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011362 return;
11363 }
11364
11365 if ((d = argvars[0].vval.v_dict) != NULL)
11366 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011367 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011368 if (csearch != NULL)
11369 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011370 if (enc_utf8)
11371 {
11372 int pcc[MAX_MCO];
11373 int c = utfc_ptr2char(csearch, pcc);
11374
11375 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11376 }
11377 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011378 set_last_csearch(PTR2CHAR(csearch),
11379 csearch, MB_PTR2LEN(csearch));
11380 }
11381
11382 di = dict_find(d, (char_u *)"forward", -1);
11383 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011384 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011385 ? FORWARD : BACKWARD);
11386
11387 di = dict_find(d, (char_u *)"until", -1);
11388 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011389 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011390 }
11391}
11392
11393/*
11394 * "setcmdpos()" function
11395 */
11396 static void
11397f_setcmdpos(typval_T *argvars, typval_T *rettv)
11398{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011399 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011400
11401 if (pos >= 0)
11402 rettv->vval.v_number = set_cmdline_pos(pos);
11403}
11404
11405/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011406 * "setenv()" function
11407 */
11408 static void
11409f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11410{
11411 char_u namebuf[NUMBUFLEN];
11412 char_u valbuf[NUMBUFLEN];
11413 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11414
11415 if (argvars[1].v_type == VAR_SPECIAL
11416 && argvars[1].vval.v_number == VVAL_NULL)
11417 vim_unsetenv(name);
11418 else
11419 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11420}
11421
11422/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011423 * "setfperm({fname}, {mode})" function
11424 */
11425 static void
11426f_setfperm(typval_T *argvars, typval_T *rettv)
11427{
11428 char_u *fname;
11429 char_u modebuf[NUMBUFLEN];
11430 char_u *mode_str;
11431 int i;
11432 int mask;
11433 int mode = 0;
11434
11435 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011436 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011437 if (fname == NULL)
11438 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011439 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011440 if (mode_str == NULL)
11441 return;
11442 if (STRLEN(mode_str) != 9)
11443 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011444 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011445 return;
11446 }
11447
11448 mask = 1;
11449 for (i = 8; i >= 0; --i)
11450 {
11451 if (mode_str[i] != '-')
11452 mode |= mask;
11453 mask = mask << 1;
11454 }
11455 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11456}
11457
11458/*
11459 * "setline()" function
11460 */
11461 static void
11462f_setline(typval_T *argvars, typval_T *rettv)
11463{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011464 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011465
Bram Moolenaarca851592018-06-06 21:04:07 +020011466 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011467}
11468
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011469/*
11470 * Used by "setqflist()" and "setloclist()" functions
11471 */
11472 static void
11473set_qf_ll_list(
11474 win_T *wp UNUSED,
11475 typval_T *list_arg UNUSED,
11476 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011477 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011478 typval_T *rettv)
11479{
11480#ifdef FEAT_QUICKFIX
11481 static char *e_invact = N_("E927: Invalid action: '%s'");
11482 char_u *act;
11483 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011484 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011485#endif
11486
11487 rettv->vval.v_number = -1;
11488
11489#ifdef FEAT_QUICKFIX
11490 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011491 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011492 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011493 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011494 else
11495 {
11496 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011497 dict_T *d = NULL;
11498 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011499
11500 if (action_arg->v_type == VAR_STRING)
11501 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011502 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011503 if (act == NULL)
11504 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011505 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11506 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011507 action = *act;
11508 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011509 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011510 }
11511 else if (action_arg->v_type == VAR_UNKNOWN)
11512 action = ' ';
11513 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011514 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011515
Bram Moolenaard823fa92016-08-12 16:29:27 +020011516 if (action_arg->v_type != VAR_UNKNOWN
11517 && what_arg->v_type != VAR_UNKNOWN)
11518 {
11519 if (what_arg->v_type == VAR_DICT)
11520 d = what_arg->vval.v_dict;
11521 else
11522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011523 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011524 valid_dict = FALSE;
11525 }
11526 }
11527
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011528 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011529 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011530 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11531 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011532 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011533 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011534 }
11535#endif
11536}
11537
11538/*
11539 * "setloclist()" function
11540 */
11541 static void
11542f_setloclist(typval_T *argvars, typval_T *rettv)
11543{
11544 win_T *win;
11545
11546 rettv->vval.v_number = -1;
11547
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011548 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011549 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011550 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011551}
11552
11553/*
11554 * "setmatches()" function
11555 */
11556 static void
11557f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11558{
11559#ifdef FEAT_SEARCH_EXTRA
11560 list_T *l;
11561 listitem_T *li;
11562 dict_T *d;
11563 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011564 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011565
11566 rettv->vval.v_number = -1;
11567 if (argvars[0].v_type != VAR_LIST)
11568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011569 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011570 return;
11571 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011572 if (win == NULL)
11573 return;
11574
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011575 if ((l = argvars[0].vval.v_list) != NULL)
11576 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011577 /* To some extent make sure that we are dealing with a list from
11578 * "getmatches()". */
11579 li = l->lv_first;
11580 while (li != NULL)
11581 {
11582 if (li->li_tv.v_type != VAR_DICT
11583 || (d = li->li_tv.vval.v_dict) == NULL)
11584 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011585 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011586 return;
11587 }
11588 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11589 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11590 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11591 && dict_find(d, (char_u *)"priority", -1) != NULL
11592 && dict_find(d, (char_u *)"id", -1) != NULL))
11593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011594 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011595 return;
11596 }
11597 li = li->li_next;
11598 }
11599
Bram Moolenaaraff74912019-03-30 18:11:49 +010011600 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011601 li = l->lv_first;
11602 while (li != NULL)
11603 {
11604 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011605 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011606 dictitem_T *di;
11607 char_u *group;
11608 int priority;
11609 int id;
11610 char_u *conceal;
11611
11612 d = li->li_tv.vval.v_dict;
11613 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11614 {
11615 if (s == NULL)
11616 {
11617 s = list_alloc();
11618 if (s == NULL)
11619 return;
11620 }
11621
11622 /* match from matchaddpos() */
11623 for (i = 1; i < 9; i++)
11624 {
11625 sprintf((char *)buf, (char *)"pos%d", i);
11626 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11627 {
11628 if (di->di_tv.v_type != VAR_LIST)
11629 return;
11630
11631 list_append_tv(s, &di->di_tv);
11632 s->lv_refcount++;
11633 }
11634 else
11635 break;
11636 }
11637 }
11638
Bram Moolenaar8f667172018-12-14 15:38:31 +010011639 group = dict_get_string(d, (char_u *)"group", TRUE);
11640 priority = (int)dict_get_number(d, (char_u *)"priority");
11641 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011642 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011643 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011644 : NULL;
11645 if (i == 0)
11646 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011647 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011648 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011649 priority, id, NULL, conceal);
11650 }
11651 else
11652 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011653 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011654 list_unref(s);
11655 s = NULL;
11656 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011657 vim_free(group);
11658 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011659
11660 li = li->li_next;
11661 }
11662 rettv->vval.v_number = 0;
11663 }
11664#endif
11665}
11666
11667/*
11668 * "setpos()" function
11669 */
11670 static void
11671f_setpos(typval_T *argvars, typval_T *rettv)
11672{
11673 pos_T pos;
11674 int fnum;
11675 char_u *name;
11676 colnr_T curswant = -1;
11677
11678 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011679 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011680 if (name != NULL)
11681 {
11682 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11683 {
11684 if (--pos.col < 0)
11685 pos.col = 0;
11686 if (name[0] == '.' && name[1] == NUL)
11687 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011688 /* set cursor; "fnum" is ignored */
11689 curwin->w_cursor = pos;
11690 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011691 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011692 curwin->w_curswant = curswant - 1;
11693 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011694 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011695 check_cursor();
11696 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011697 }
11698 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11699 {
11700 /* set mark */
11701 if (setmark_pos(name[1], &pos, fnum) == OK)
11702 rettv->vval.v_number = 0;
11703 }
11704 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011705 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011706 }
11707 }
11708}
11709
11710/*
11711 * "setqflist()" function
11712 */
11713 static void
11714f_setqflist(typval_T *argvars, typval_T *rettv)
11715{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011716 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011717}
11718
11719/*
11720 * "setreg()" function
11721 */
11722 static void
11723f_setreg(typval_T *argvars, typval_T *rettv)
11724{
11725 int regname;
11726 char_u *strregname;
11727 char_u *stropt;
11728 char_u *strval;
11729 int append;
11730 char_u yank_type;
11731 long block_len;
11732
11733 block_len = -1;
11734 yank_type = MAUTO;
11735 append = FALSE;
11736
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011737 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011738 rettv->vval.v_number = 1; /* FAIL is default */
11739
11740 if (strregname == NULL)
11741 return; /* type error; errmsg already given */
11742 regname = *strregname;
11743 if (regname == 0 || regname == '@')
11744 regname = '"';
11745
11746 if (argvars[2].v_type != VAR_UNKNOWN)
11747 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011748 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011749 if (stropt == NULL)
11750 return; /* type error */
11751 for (; *stropt != NUL; ++stropt)
11752 switch (*stropt)
11753 {
11754 case 'a': case 'A': /* append */
11755 append = TRUE;
11756 break;
11757 case 'v': case 'c': /* character-wise selection */
11758 yank_type = MCHAR;
11759 break;
11760 case 'V': case 'l': /* line-wise selection */
11761 yank_type = MLINE;
11762 break;
11763 case 'b': case Ctrl_V: /* block-wise selection */
11764 yank_type = MBLOCK;
11765 if (VIM_ISDIGIT(stropt[1]))
11766 {
11767 ++stropt;
11768 block_len = getdigits(&stropt) - 1;
11769 --stropt;
11770 }
11771 break;
11772 }
11773 }
11774
11775 if (argvars[1].v_type == VAR_LIST)
11776 {
11777 char_u **lstval;
11778 char_u **allocval;
11779 char_u buf[NUMBUFLEN];
11780 char_u **curval;
11781 char_u **curallocval;
11782 list_T *ll = argvars[1].vval.v_list;
11783 listitem_T *li;
11784 int len;
11785
11786 /* If the list is NULL handle like an empty list. */
11787 len = ll == NULL ? 0 : ll->lv_len;
11788
11789 /* First half: use for pointers to result lines; second half: use for
11790 * pointers to allocated copies. */
11791 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11792 if (lstval == NULL)
11793 return;
11794 curval = lstval;
11795 allocval = lstval + len + 2;
11796 curallocval = allocval;
11797
11798 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11799 li = li->li_next)
11800 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011801 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011802 if (strval == NULL)
11803 goto free_lstval;
11804 if (strval == buf)
11805 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011806 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011807 * overwrite the string. */
11808 strval = vim_strsave(buf);
11809 if (strval == NULL)
11810 goto free_lstval;
11811 *curallocval++ = strval;
11812 }
11813 *curval++ = strval;
11814 }
11815 *curval++ = NULL;
11816
11817 write_reg_contents_lst(regname, lstval, -1,
11818 append, yank_type, block_len);
11819free_lstval:
11820 while (curallocval > allocval)
11821 vim_free(*--curallocval);
11822 vim_free(lstval);
11823 }
11824 else
11825 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011826 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011827 if (strval == NULL)
11828 return;
11829 write_reg_contents_ex(regname, strval, -1,
11830 append, yank_type, block_len);
11831 }
11832 rettv->vval.v_number = 0;
11833}
11834
11835/*
11836 * "settabvar()" function
11837 */
11838 static void
11839f_settabvar(typval_T *argvars, typval_T *rettv)
11840{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011841 tabpage_T *save_curtab;
11842 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011843 char_u *varname, *tabvarname;
11844 typval_T *varp;
11845
11846 rettv->vval.v_number = 0;
11847
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011848 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011849 return;
11850
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011851 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11852 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011853 varp = &argvars[2];
11854
Bram Moolenaar4033c552017-09-16 20:54:51 +020011855 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011856 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011857 save_curtab = curtab;
11858 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011859
Bram Moolenaar964b3742019-05-24 18:54:09 +020011860 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011861 if (tabvarname != NULL)
11862 {
11863 STRCPY(tabvarname, "t:");
11864 STRCPY(tabvarname + 2, varname);
11865 set_var(tabvarname, varp, TRUE);
11866 vim_free(tabvarname);
11867 }
11868
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011869 /* Restore current tabpage */
11870 if (valid_tabpage(save_curtab))
11871 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011872 }
11873}
11874
11875/*
11876 * "settabwinvar()" function
11877 */
11878 static void
11879f_settabwinvar(typval_T *argvars, typval_T *rettv)
11880{
11881 setwinvar(argvars, rettv, 1);
11882}
11883
11884/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011885 * "settagstack()" function
11886 */
11887 static void
11888f_settagstack(typval_T *argvars, typval_T *rettv)
11889{
11890 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11891 win_T *wp;
11892 dict_T *d;
11893 int action = 'r';
11894
11895 rettv->vval.v_number = -1;
11896
11897 // first argument: window number or id
11898 wp = find_win_by_nr_or_id(&argvars[0]);
11899 if (wp == NULL)
11900 return;
11901
11902 // second argument: dict with items to set in the tag stack
11903 if (argvars[1].v_type != VAR_DICT)
11904 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011905 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011906 return;
11907 }
11908 d = argvars[1].vval.v_dict;
11909 if (d == NULL)
11910 return;
11911
11912 // third argument: action - 'a' for append and 'r' for replace.
11913 // default is to replace the stack.
11914 if (argvars[2].v_type == VAR_UNKNOWN)
11915 action = 'r';
11916 else if (argvars[2].v_type == VAR_STRING)
11917 {
11918 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011919 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011920 if (actstr == NULL)
11921 return;
11922 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11923 action = *actstr;
11924 else
11925 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011926 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011927 return;
11928 }
11929 }
11930 else
11931 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011932 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011933 return;
11934 }
11935
11936 if (set_tagstack(wp, d, action) == OK)
11937 rettv->vval.v_number = 0;
11938}
11939
11940/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011941 * "setwinvar()" function
11942 */
11943 static void
11944f_setwinvar(typval_T *argvars, typval_T *rettv)
11945{
11946 setwinvar(argvars, rettv, 0);
11947}
11948
11949#ifdef FEAT_CRYPT
11950/*
11951 * "sha256({string})" function
11952 */
11953 static void
11954f_sha256(typval_T *argvars, typval_T *rettv)
11955{
11956 char_u *p;
11957
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011958 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011959 rettv->vval.v_string = vim_strsave(
11960 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11961 rettv->v_type = VAR_STRING;
11962}
11963#endif /* FEAT_CRYPT */
11964
11965/*
11966 * "shellescape({string})" function
11967 */
11968 static void
11969f_shellescape(typval_T *argvars, typval_T *rettv)
11970{
Bram Moolenaar20615522017-06-05 18:46:26 +020011971 int do_special = non_zero_arg(&argvars[1]);
11972
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011973 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011974 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011975 rettv->v_type = VAR_STRING;
11976}
11977
11978/*
11979 * shiftwidth() function
11980 */
11981 static void
11982f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11983{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011984 rettv->vval.v_number = 0;
11985
11986 if (argvars[0].v_type != VAR_UNKNOWN)
11987 {
11988 long col;
11989
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011990 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011991 if (col < 0)
11992 return; // type error; errmsg already given
11993#ifdef FEAT_VARTABS
11994 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11995 return;
11996#endif
11997 }
11998
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011999 rettv->vval.v_number = get_sw_value(curbuf);
12000}
12001
Bram Moolenaar162b7142018-12-21 15:17:36 +010012002#ifdef FEAT_SIGNS
12003/*
12004 * "sign_define()" function
12005 */
12006 static void
12007f_sign_define(typval_T *argvars, typval_T *rettv)
12008{
12009 char_u *name;
12010 dict_T *dict;
12011 char_u *icon = NULL;
12012 char_u *linehl = NULL;
12013 char_u *text = NULL;
12014 char_u *texthl = NULL;
12015
12016 rettv->vval.v_number = -1;
12017
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012018 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012019 if (name == NULL)
12020 return;
12021
12022 if (argvars[1].v_type != VAR_UNKNOWN)
12023 {
12024 if (argvars[1].v_type != VAR_DICT)
12025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012026 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012027 return;
12028 }
12029
12030 // sign attributes
12031 dict = argvars[1].vval.v_dict;
12032 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12033 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12034 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12035 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12036 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12037 text = dict_get_string(dict, (char_u *)"text", TRUE);
12038 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12039 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12040 }
12041
12042 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12043 rettv->vval.v_number = 0;
12044
12045 vim_free(icon);
12046 vim_free(linehl);
12047 vim_free(text);
12048 vim_free(texthl);
12049}
12050
12051/*
12052 * "sign_getdefined()" function
12053 */
12054 static void
12055f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12056{
12057 char_u *name = NULL;
12058
12059 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12060 return;
12061
12062 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012063 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012064
12065 sign_getlist(name, rettv->vval.v_list);
12066}
12067
12068/*
12069 * "sign_getplaced()" function
12070 */
12071 static void
12072f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12073{
12074 buf_T *buf = NULL;
12075 dict_T *dict;
12076 dictitem_T *di;
12077 linenr_T lnum = 0;
12078 int sign_id = 0;
12079 char_u *group = NULL;
12080 int notanum = FALSE;
12081
12082 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12083 return;
12084
12085 if (argvars[0].v_type != VAR_UNKNOWN)
12086 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012087 // get signs placed in the specified buffer
12088 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012089 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012090 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012091
12092 if (argvars[1].v_type != VAR_UNKNOWN)
12093 {
12094 if (argvars[1].v_type != VAR_DICT ||
12095 ((dict = argvars[1].vval.v_dict) == NULL))
12096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012097 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012098 return;
12099 }
12100 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12101 {
12102 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012103 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012104 if (notanum)
12105 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012106 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012107 }
12108 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12109 {
12110 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012111 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012112 if (notanum)
12113 return;
12114 }
12115 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12116 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012117 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012118 if (group == NULL)
12119 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012120 if (*group == '\0') // empty string means global group
12121 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012122 }
12123 }
12124 }
12125
12126 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12127}
12128
12129/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012130 * "sign_jump()" function
12131 */
12132 static void
12133f_sign_jump(typval_T *argvars, typval_T *rettv)
12134{
12135 int sign_id;
12136 char_u *sign_group = NULL;
12137 buf_T *buf;
12138 int notanum = FALSE;
12139
12140 rettv->vval.v_number = -1;
12141
Bram Moolenaarbdace832019-03-02 10:13:42 +010012142 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012143 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12144 if (notanum)
12145 return;
12146 if (sign_id <= 0)
12147 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012148 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012149 return;
12150 }
12151
12152 // Sign group
12153 sign_group = tv_get_string_chk(&argvars[1]);
12154 if (sign_group == NULL)
12155 return;
12156 if (sign_group[0] == '\0')
12157 sign_group = NULL; // global sign group
12158 else
12159 {
12160 sign_group = vim_strsave(sign_group);
12161 if (sign_group == NULL)
12162 return;
12163 }
12164
12165 // Buffer to place the sign
12166 buf = get_buf_arg(&argvars[2]);
12167 if (buf == NULL)
12168 goto cleanup;
12169
12170 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12171
12172cleanup:
12173 vim_free(sign_group);
12174}
12175
12176/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012177 * "sign_place()" function
12178 */
12179 static void
12180f_sign_place(typval_T *argvars, typval_T *rettv)
12181{
12182 int sign_id;
12183 char_u *group = NULL;
12184 char_u *sign_name;
12185 buf_T *buf;
12186 dict_T *dict;
12187 dictitem_T *di;
12188 linenr_T lnum = 0;
12189 int prio = SIGN_DEF_PRIO;
12190 int notanum = FALSE;
12191
12192 rettv->vval.v_number = -1;
12193
Bram Moolenaarbdace832019-03-02 10:13:42 +010012194 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012195 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012196 if (notanum)
12197 return;
12198 if (sign_id < 0)
12199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012200 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012201 return;
12202 }
12203
12204 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012205 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012206 if (group == NULL)
12207 return;
12208 if (group[0] == '\0')
12209 group = NULL; // global sign group
12210 else
12211 {
12212 group = vim_strsave(group);
12213 if (group == NULL)
12214 return;
12215 }
12216
12217 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012218 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012219 if (sign_name == NULL)
12220 goto cleanup;
12221
12222 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012223 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012224 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012225 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012226
12227 if (argvars[4].v_type != VAR_UNKNOWN)
12228 {
12229 if (argvars[4].v_type != VAR_DICT ||
12230 ((dict = argvars[4].vval.v_dict) == NULL))
12231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012232 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012233 goto cleanup;
12234 }
12235
12236 // Line number where the sign is to be placed
12237 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12238 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012239 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012240 if (notanum)
12241 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012242 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012243 }
12244 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12245 {
12246 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012247 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012248 if (notanum)
12249 goto cleanup;
12250 }
12251 }
12252
12253 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12254 rettv->vval.v_number = sign_id;
12255
12256cleanup:
12257 vim_free(group);
12258}
12259
12260/*
12261 * "sign_undefine()" function
12262 */
12263 static void
12264f_sign_undefine(typval_T *argvars, typval_T *rettv)
12265{
12266 char_u *name;
12267
12268 rettv->vval.v_number = -1;
12269
12270 if (argvars[0].v_type == VAR_UNKNOWN)
12271 {
12272 // Free all the signs
12273 free_signs();
12274 rettv->vval.v_number = 0;
12275 }
12276 else
12277 {
12278 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012279 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012280 if (name == NULL)
12281 return;
12282
12283 if (sign_undefine_by_name(name) == OK)
12284 rettv->vval.v_number = 0;
12285 }
12286}
12287
12288/*
12289 * "sign_unplace()" function
12290 */
12291 static void
12292f_sign_unplace(typval_T *argvars, typval_T *rettv)
12293{
12294 dict_T *dict;
12295 dictitem_T *di;
12296 int sign_id = 0;
12297 buf_T *buf = NULL;
12298 char_u *group = NULL;
12299
12300 rettv->vval.v_number = -1;
12301
12302 if (argvars[0].v_type != VAR_STRING)
12303 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012304 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012305 return;
12306 }
12307
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012308 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012309 if (group[0] == '\0')
12310 group = NULL; // global sign group
12311 else
12312 {
12313 group = vim_strsave(group);
12314 if (group == NULL)
12315 return;
12316 }
12317
12318 if (argvars[1].v_type != VAR_UNKNOWN)
12319 {
12320 if (argvars[1].v_type != VAR_DICT)
12321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012322 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012323 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012324 }
12325 dict = argvars[1].vval.v_dict;
12326
12327 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12328 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012329 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012330 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012331 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012332 }
12333 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12334 sign_id = dict_get_number(dict, (char_u *)"id");
12335 }
12336
12337 if (buf == NULL)
12338 {
12339 // Delete the sign in all the buffers
12340 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012341 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012342 rettv->vval.v_number = 0;
12343 }
12344 else
12345 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012346 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012347 rettv->vval.v_number = 0;
12348 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012349
12350cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012351 vim_free(group);
12352}
12353#endif
12354
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012355/*
12356 * "simplify()" function
12357 */
12358 static void
12359f_simplify(typval_T *argvars, typval_T *rettv)
12360{
12361 char_u *p;
12362
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012363 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012364 rettv->vval.v_string = vim_strsave(p);
12365 simplify_filename(rettv->vval.v_string); /* simplify in place */
12366 rettv->v_type = VAR_STRING;
12367}
12368
12369#ifdef FEAT_FLOAT
12370/*
12371 * "sin()" function
12372 */
12373 static void
12374f_sin(typval_T *argvars, typval_T *rettv)
12375{
12376 float_T f = 0.0;
12377
12378 rettv->v_type = VAR_FLOAT;
12379 if (get_float_arg(argvars, &f) == OK)
12380 rettv->vval.v_float = sin(f);
12381 else
12382 rettv->vval.v_float = 0.0;
12383}
12384
12385/*
12386 * "sinh()" function
12387 */
12388 static void
12389f_sinh(typval_T *argvars, typval_T *rettv)
12390{
12391 float_T f = 0.0;
12392
12393 rettv->v_type = VAR_FLOAT;
12394 if (get_float_arg(argvars, &f) == OK)
12395 rettv->vval.v_float = sinh(f);
12396 else
12397 rettv->vval.v_float = 0.0;
12398}
12399#endif
12400
Bram Moolenaareae1b912019-05-09 15:12:55 +020012401static int item_compare(const void *s1, const void *s2);
12402static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012403
12404/* struct used in the array that's given to qsort() */
12405typedef struct
12406{
12407 listitem_T *item;
12408 int idx;
12409} sortItem_T;
12410
12411/* struct storing information about current sort */
12412typedef struct
12413{
12414 int item_compare_ic;
12415 int item_compare_numeric;
12416 int item_compare_numbers;
12417#ifdef FEAT_FLOAT
12418 int item_compare_float;
12419#endif
12420 char_u *item_compare_func;
12421 partial_T *item_compare_partial;
12422 dict_T *item_compare_selfdict;
12423 int item_compare_func_err;
12424 int item_compare_keep_zero;
12425} sortinfo_T;
12426static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012427#define ITEM_COMPARE_FAIL 999
12428
12429/*
12430 * Compare functions for f_sort() and f_uniq() below.
12431 */
12432 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012433item_compare(const void *s1, const void *s2)
12434{
12435 sortItem_T *si1, *si2;
12436 typval_T *tv1, *tv2;
12437 char_u *p1, *p2;
12438 char_u *tofree1 = NULL, *tofree2 = NULL;
12439 int res;
12440 char_u numbuf1[NUMBUFLEN];
12441 char_u numbuf2[NUMBUFLEN];
12442
12443 si1 = (sortItem_T *)s1;
12444 si2 = (sortItem_T *)s2;
12445 tv1 = &si1->item->li_tv;
12446 tv2 = &si2->item->li_tv;
12447
12448 if (sortinfo->item_compare_numbers)
12449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012450 varnumber_T v1 = tv_get_number(tv1);
12451 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012452
12453 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12454 }
12455
12456#ifdef FEAT_FLOAT
12457 if (sortinfo->item_compare_float)
12458 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012459 float_T v1 = tv_get_float(tv1);
12460 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012461
12462 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12463 }
12464#endif
12465
12466 /* tv2string() puts quotes around a string and allocates memory. Don't do
12467 * that for string variables. Use a single quote when comparing with a
12468 * non-string to do what the docs promise. */
12469 if (tv1->v_type == VAR_STRING)
12470 {
12471 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12472 p1 = (char_u *)"'";
12473 else
12474 p1 = tv1->vval.v_string;
12475 }
12476 else
12477 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12478 if (tv2->v_type == VAR_STRING)
12479 {
12480 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12481 p2 = (char_u *)"'";
12482 else
12483 p2 = tv2->vval.v_string;
12484 }
12485 else
12486 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12487 if (p1 == NULL)
12488 p1 = (char_u *)"";
12489 if (p2 == NULL)
12490 p2 = (char_u *)"";
12491 if (!sortinfo->item_compare_numeric)
12492 {
12493 if (sortinfo->item_compare_ic)
12494 res = STRICMP(p1, p2);
12495 else
12496 res = STRCMP(p1, p2);
12497 }
12498 else
12499 {
12500 double n1, n2;
12501 n1 = strtod((char *)p1, (char **)&p1);
12502 n2 = strtod((char *)p2, (char **)&p2);
12503 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12504 }
12505
12506 /* When the result would be zero, compare the item indexes. Makes the
12507 * sort stable. */
12508 if (res == 0 && !sortinfo->item_compare_keep_zero)
12509 res = si1->idx > si2->idx ? 1 : -1;
12510
12511 vim_free(tofree1);
12512 vim_free(tofree2);
12513 return res;
12514}
12515
12516 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012517item_compare2(const void *s1, const void *s2)
12518{
12519 sortItem_T *si1, *si2;
12520 int res;
12521 typval_T rettv;
12522 typval_T argv[3];
12523 int dummy;
12524 char_u *func_name;
12525 partial_T *partial = sortinfo->item_compare_partial;
12526
12527 /* shortcut after failure in previous call; compare all items equal */
12528 if (sortinfo->item_compare_func_err)
12529 return 0;
12530
12531 si1 = (sortItem_T *)s1;
12532 si2 = (sortItem_T *)s2;
12533
12534 if (partial == NULL)
12535 func_name = sortinfo->item_compare_func;
12536 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012537 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012538
12539 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12540 * in the copy without changing the original list items. */
12541 copy_tv(&si1->item->li_tv, &argv[0]);
12542 copy_tv(&si2->item->li_tv, &argv[1]);
12543
12544 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012545 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012546 partial, sortinfo->item_compare_selfdict);
12547 clear_tv(&argv[0]);
12548 clear_tv(&argv[1]);
12549
12550 if (res == FAIL)
12551 res = ITEM_COMPARE_FAIL;
12552 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012553 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012554 if (sortinfo->item_compare_func_err)
12555 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12556 clear_tv(&rettv);
12557
12558 /* When the result would be zero, compare the pointers themselves. Makes
12559 * the sort stable. */
12560 if (res == 0 && !sortinfo->item_compare_keep_zero)
12561 res = si1->idx > si2->idx ? 1 : -1;
12562
12563 return res;
12564}
12565
12566/*
12567 * "sort({list})" function
12568 */
12569 static void
12570do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12571{
12572 list_T *l;
12573 listitem_T *li;
12574 sortItem_T *ptrs;
12575 sortinfo_T *old_sortinfo;
12576 sortinfo_T info;
12577 long len;
12578 long i;
12579
12580 /* Pointer to current info struct used in compare function. Save and
12581 * restore the current one for nested calls. */
12582 old_sortinfo = sortinfo;
12583 sortinfo = &info;
12584
12585 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012586 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012587 else
12588 {
12589 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012590 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012591 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12592 TRUE))
12593 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012594 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012595
12596 len = list_len(l);
12597 if (len <= 1)
12598 goto theend; /* short list sorts pretty quickly */
12599
12600 info.item_compare_ic = FALSE;
12601 info.item_compare_numeric = FALSE;
12602 info.item_compare_numbers = FALSE;
12603#ifdef FEAT_FLOAT
12604 info.item_compare_float = FALSE;
12605#endif
12606 info.item_compare_func = NULL;
12607 info.item_compare_partial = NULL;
12608 info.item_compare_selfdict = NULL;
12609 if (argvars[1].v_type != VAR_UNKNOWN)
12610 {
12611 /* optional second argument: {func} */
12612 if (argvars[1].v_type == VAR_FUNC)
12613 info.item_compare_func = argvars[1].vval.v_string;
12614 else if (argvars[1].v_type == VAR_PARTIAL)
12615 info.item_compare_partial = argvars[1].vval.v_partial;
12616 else
12617 {
12618 int error = FALSE;
12619
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012620 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012621 if (error)
12622 goto theend; /* type error; errmsg already given */
12623 if (i == 1)
12624 info.item_compare_ic = TRUE;
12625 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012626 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012627 else if (i != 0)
12628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012629 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012630 goto theend;
12631 }
12632 if (info.item_compare_func != NULL)
12633 {
12634 if (*info.item_compare_func == NUL)
12635 {
12636 /* empty string means default sort */
12637 info.item_compare_func = NULL;
12638 }
12639 else if (STRCMP(info.item_compare_func, "n") == 0)
12640 {
12641 info.item_compare_func = NULL;
12642 info.item_compare_numeric = TRUE;
12643 }
12644 else if (STRCMP(info.item_compare_func, "N") == 0)
12645 {
12646 info.item_compare_func = NULL;
12647 info.item_compare_numbers = TRUE;
12648 }
12649#ifdef FEAT_FLOAT
12650 else if (STRCMP(info.item_compare_func, "f") == 0)
12651 {
12652 info.item_compare_func = NULL;
12653 info.item_compare_float = TRUE;
12654 }
12655#endif
12656 else if (STRCMP(info.item_compare_func, "i") == 0)
12657 {
12658 info.item_compare_func = NULL;
12659 info.item_compare_ic = TRUE;
12660 }
12661 }
12662 }
12663
12664 if (argvars[2].v_type != VAR_UNKNOWN)
12665 {
12666 /* optional third argument: {dict} */
12667 if (argvars[2].v_type != VAR_DICT)
12668 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012669 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012670 goto theend;
12671 }
12672 info.item_compare_selfdict = argvars[2].vval.v_dict;
12673 }
12674 }
12675
12676 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar51e14382019-05-25 20:21:28 +020012677 ptrs = (sortItem_T *)alloc(len * sizeof(sortItem_T));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012678 if (ptrs == NULL)
12679 goto theend;
12680
12681 i = 0;
12682 if (sort)
12683 {
12684 /* sort(): ptrs will be the list to sort */
12685 for (li = l->lv_first; li != NULL; li = li->li_next)
12686 {
12687 ptrs[i].item = li;
12688 ptrs[i].idx = i;
12689 ++i;
12690 }
12691
12692 info.item_compare_func_err = FALSE;
12693 info.item_compare_keep_zero = FALSE;
12694 /* test the compare function */
12695 if ((info.item_compare_func != NULL
12696 || info.item_compare_partial != NULL)
12697 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12698 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012699 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012700 else
12701 {
12702 /* Sort the array with item pointers. */
12703 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12704 info.item_compare_func == NULL
12705 && info.item_compare_partial == NULL
12706 ? item_compare : item_compare2);
12707
12708 if (!info.item_compare_func_err)
12709 {
12710 /* Clear the List and append the items in sorted order. */
12711 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12712 l->lv_len = 0;
12713 for (i = 0; i < len; ++i)
12714 list_append(l, ptrs[i].item);
12715 }
12716 }
12717 }
12718 else
12719 {
12720 int (*item_compare_func_ptr)(const void *, const void *);
12721
12722 /* f_uniq(): ptrs will be a stack of items to remove */
12723 info.item_compare_func_err = FALSE;
12724 info.item_compare_keep_zero = TRUE;
12725 item_compare_func_ptr = info.item_compare_func != NULL
12726 || info.item_compare_partial != NULL
12727 ? item_compare2 : item_compare;
12728
12729 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12730 li = li->li_next)
12731 {
12732 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12733 == 0)
12734 ptrs[i++].item = li;
12735 if (info.item_compare_func_err)
12736 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012737 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012738 break;
12739 }
12740 }
12741
12742 if (!info.item_compare_func_err)
12743 {
12744 while (--i >= 0)
12745 {
12746 li = ptrs[i].item->li_next;
12747 ptrs[i].item->li_next = li->li_next;
12748 if (li->li_next != NULL)
12749 li->li_next->li_prev = ptrs[i].item;
12750 else
12751 l->lv_last = ptrs[i].item;
12752 list_fix_watch(l, li);
12753 listitem_free(li);
12754 l->lv_len--;
12755 }
12756 }
12757 }
12758
12759 vim_free(ptrs);
12760 }
12761theend:
12762 sortinfo = old_sortinfo;
12763}
12764
12765/*
12766 * "sort({list})" function
12767 */
12768 static void
12769f_sort(typval_T *argvars, typval_T *rettv)
12770{
12771 do_sort_uniq(argvars, rettv, TRUE);
12772}
12773
12774/*
12775 * "uniq({list})" function
12776 */
12777 static void
12778f_uniq(typval_T *argvars, typval_T *rettv)
12779{
12780 do_sort_uniq(argvars, rettv, FALSE);
12781}
12782
12783/*
12784 * "soundfold({word})" function
12785 */
12786 static void
12787f_soundfold(typval_T *argvars, typval_T *rettv)
12788{
12789 char_u *s;
12790
12791 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012792 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012793#ifdef FEAT_SPELL
12794 rettv->vval.v_string = eval_soundfold(s);
12795#else
12796 rettv->vval.v_string = vim_strsave(s);
12797#endif
12798}
12799
12800/*
12801 * "spellbadword()" function
12802 */
12803 static void
12804f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12805{
12806 char_u *word = (char_u *)"";
12807 hlf_T attr = HLF_COUNT;
12808 int len = 0;
12809
12810 if (rettv_list_alloc(rettv) == FAIL)
12811 return;
12812
12813#ifdef FEAT_SPELL
12814 if (argvars[0].v_type == VAR_UNKNOWN)
12815 {
12816 /* Find the start and length of the badly spelled word. */
12817 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12818 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012819 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012820 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012821 curwin->w_set_curswant = TRUE;
12822 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012823 }
12824 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12825 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012826 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012827 int capcol = -1;
12828
12829 if (str != NULL)
12830 {
12831 /* Check the argument for spelling. */
12832 while (*str != NUL)
12833 {
12834 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12835 if (attr != HLF_COUNT)
12836 {
12837 word = str;
12838 break;
12839 }
12840 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012841 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012842 }
12843 }
12844 }
12845#endif
12846
12847 list_append_string(rettv->vval.v_list, word, len);
12848 list_append_string(rettv->vval.v_list, (char_u *)(
12849 attr == HLF_SPB ? "bad" :
12850 attr == HLF_SPR ? "rare" :
12851 attr == HLF_SPL ? "local" :
12852 attr == HLF_SPC ? "caps" :
12853 ""), -1);
12854}
12855
12856/*
12857 * "spellsuggest()" function
12858 */
12859 static void
12860f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12861{
12862#ifdef FEAT_SPELL
12863 char_u *str;
12864 int typeerr = FALSE;
12865 int maxcount;
12866 garray_T ga;
12867 int i;
12868 listitem_T *li;
12869 int need_capital = FALSE;
12870#endif
12871
12872 if (rettv_list_alloc(rettv) == FAIL)
12873 return;
12874
12875#ifdef FEAT_SPELL
12876 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12877 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012878 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012879 if (argvars[1].v_type != VAR_UNKNOWN)
12880 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012881 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012882 if (maxcount <= 0)
12883 return;
12884 if (argvars[2].v_type != VAR_UNKNOWN)
12885 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012886 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012887 if (typeerr)
12888 return;
12889 }
12890 }
12891 else
12892 maxcount = 25;
12893
12894 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12895
12896 for (i = 0; i < ga.ga_len; ++i)
12897 {
12898 str = ((char_u **)ga.ga_data)[i];
12899
12900 li = listitem_alloc();
12901 if (li == NULL)
12902 vim_free(str);
12903 else
12904 {
12905 li->li_tv.v_type = VAR_STRING;
12906 li->li_tv.v_lock = 0;
12907 li->li_tv.vval.v_string = str;
12908 list_append(rettv->vval.v_list, li);
12909 }
12910 }
12911 ga_clear(&ga);
12912 }
12913#endif
12914}
12915
12916 static void
12917f_split(typval_T *argvars, typval_T *rettv)
12918{
12919 char_u *str;
12920 char_u *end;
12921 char_u *pat = NULL;
12922 regmatch_T regmatch;
12923 char_u patbuf[NUMBUFLEN];
12924 char_u *save_cpo;
12925 int match;
12926 colnr_T col = 0;
12927 int keepempty = FALSE;
12928 int typeerr = FALSE;
12929
12930 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12931 save_cpo = p_cpo;
12932 p_cpo = (char_u *)"";
12933
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012934 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012935 if (argvars[1].v_type != VAR_UNKNOWN)
12936 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012937 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012938 if (pat == NULL)
12939 typeerr = TRUE;
12940 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012941 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012942 }
12943 if (pat == NULL || *pat == NUL)
12944 pat = (char_u *)"[\\x01- ]\\+";
12945
12946 if (rettv_list_alloc(rettv) == FAIL)
12947 return;
12948 if (typeerr)
12949 return;
12950
12951 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12952 if (regmatch.regprog != NULL)
12953 {
12954 regmatch.rm_ic = FALSE;
12955 while (*str != NUL || keepempty)
12956 {
12957 if (*str == NUL)
12958 match = FALSE; /* empty item at the end */
12959 else
12960 match = vim_regexec_nl(&regmatch, str, col);
12961 if (match)
12962 end = regmatch.startp[0];
12963 else
12964 end = str + STRLEN(str);
12965 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12966 && *str != NUL && match && end < regmatch.endp[0]))
12967 {
12968 if (list_append_string(rettv->vval.v_list, str,
12969 (int)(end - str)) == FAIL)
12970 break;
12971 }
12972 if (!match)
12973 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012974 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012975 if (regmatch.endp[0] > str)
12976 col = 0;
12977 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012978 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012979 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012980 str = regmatch.endp[0];
12981 }
12982
12983 vim_regfree(regmatch.regprog);
12984 }
12985
12986 p_cpo = save_cpo;
12987}
12988
12989#ifdef FEAT_FLOAT
12990/*
12991 * "sqrt()" function
12992 */
12993 static void
12994f_sqrt(typval_T *argvars, typval_T *rettv)
12995{
12996 float_T f = 0.0;
12997
12998 rettv->v_type = VAR_FLOAT;
12999 if (get_float_arg(argvars, &f) == OK)
13000 rettv->vval.v_float = sqrt(f);
13001 else
13002 rettv->vval.v_float = 0.0;
13003}
13004
13005/*
13006 * "str2float()" function
13007 */
13008 static void
13009f_str2float(typval_T *argvars, typval_T *rettv)
13010{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013011 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013012 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013013
Bram Moolenaar08243d22017-01-10 16:12:29 +010013014 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013015 p = skipwhite(p + 1);
13016 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013017 if (isneg)
13018 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013019 rettv->v_type = VAR_FLOAT;
13020}
13021#endif
13022
13023/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013024 * "str2list()" function
13025 */
13026 static void
13027f_str2list(typval_T *argvars, typval_T *rettv)
13028{
13029 char_u *p;
13030 int utf8 = FALSE;
13031
13032 if (rettv_list_alloc(rettv) == FAIL)
13033 return;
13034
13035 if (argvars[1].v_type != VAR_UNKNOWN)
13036 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13037
13038 p = tv_get_string(&argvars[0]);
13039
13040 if (has_mbyte || utf8)
13041 {
13042 int (*ptr2len)(char_u *);
13043 int (*ptr2char)(char_u *);
13044
13045 if (utf8 || enc_utf8)
13046 {
13047 ptr2len = utf_ptr2len;
13048 ptr2char = utf_ptr2char;
13049 }
13050 else
13051 {
13052 ptr2len = mb_ptr2len;
13053 ptr2char = mb_ptr2char;
13054 }
13055
13056 for ( ; *p != NUL; p += (*ptr2len)(p))
13057 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13058 }
13059 else
13060 for ( ; *p != NUL; ++p)
13061 list_append_number(rettv->vval.v_list, *p);
13062}
13063
13064/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013065 * "str2nr()" function
13066 */
13067 static void
13068f_str2nr(typval_T *argvars, typval_T *rettv)
13069{
13070 int base = 10;
13071 char_u *p;
13072 varnumber_T n;
13073 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013074 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013075
13076 if (argvars[1].v_type != VAR_UNKNOWN)
13077 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013078 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013079 if (base != 2 && base != 8 && base != 10 && base != 16)
13080 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013081 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013082 return;
13083 }
13084 }
13085
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013086 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013087 isneg = (*p == '-');
13088 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013089 p = skipwhite(p + 1);
13090 switch (base)
13091 {
13092 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13093 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13094 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13095 default: what = 0;
13096 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013097 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13098 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013099 if (isneg)
13100 rettv->vval.v_number = -n;
13101 else
13102 rettv->vval.v_number = n;
13103
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013104}
13105
13106#ifdef HAVE_STRFTIME
13107/*
13108 * "strftime({format}[, {time}])" function
13109 */
13110 static void
13111f_strftime(typval_T *argvars, typval_T *rettv)
13112{
13113 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013114# ifdef HAVE_LOCALTIME_R
13115 struct tm tmval;
13116# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013117 struct tm *curtime;
13118 time_t seconds;
13119 char_u *p;
13120
13121 rettv->v_type = VAR_STRING;
13122
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013123 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013124 if (argvars[1].v_type == VAR_UNKNOWN)
13125 seconds = time(NULL);
13126 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013127 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013128# ifdef HAVE_LOCALTIME_R
13129 curtime = localtime_r(&seconds, &tmval);
13130# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013131 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013132# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013133 /* MSVC returns NULL for an invalid value of seconds. */
13134 if (curtime == NULL)
13135 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13136 else
13137 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013138 vimconv_T conv;
13139 char_u *enc;
13140
13141 conv.vc_type = CONV_NONE;
13142 enc = enc_locale();
13143 convert_setup(&conv, p_enc, enc);
13144 if (conv.vc_type != CONV_NONE)
13145 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013146 if (p != NULL)
13147 (void)strftime((char *)result_buf, sizeof(result_buf),
13148 (char *)p, curtime);
13149 else
13150 result_buf[0] = NUL;
13151
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013152 if (conv.vc_type != CONV_NONE)
13153 vim_free(p);
13154 convert_setup(&conv, enc, p_enc);
13155 if (conv.vc_type != CONV_NONE)
13156 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13157 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013158 rettv->vval.v_string = vim_strsave(result_buf);
13159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013160 /* Release conversion descriptors */
13161 convert_setup(&conv, NULL, NULL);
13162 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013163 }
13164}
13165#endif
13166
13167/*
13168 * "strgetchar()" function
13169 */
13170 static void
13171f_strgetchar(typval_T *argvars, typval_T *rettv)
13172{
13173 char_u *str;
13174 int len;
13175 int error = FALSE;
13176 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013177 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013178
13179 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013180 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013181 if (str == NULL)
13182 return;
13183 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013184 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013185 if (error)
13186 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013187
Bram Moolenaar13505972019-01-24 15:04:48 +010013188 while (charidx >= 0 && byteidx < len)
13189 {
13190 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013191 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013192 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13193 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013194 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013195 --charidx;
13196 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013197 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013198}
13199
13200/*
13201 * "stridx()" function
13202 */
13203 static void
13204f_stridx(typval_T *argvars, typval_T *rettv)
13205{
13206 char_u buf[NUMBUFLEN];
13207 char_u *needle;
13208 char_u *haystack;
13209 char_u *save_haystack;
13210 char_u *pos;
13211 int start_idx;
13212
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013213 needle = tv_get_string_chk(&argvars[1]);
13214 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013215 rettv->vval.v_number = -1;
13216 if (needle == NULL || haystack == NULL)
13217 return; /* type error; errmsg already given */
13218
13219 if (argvars[2].v_type != VAR_UNKNOWN)
13220 {
13221 int error = FALSE;
13222
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013223 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013224 if (error || start_idx >= (int)STRLEN(haystack))
13225 return;
13226 if (start_idx >= 0)
13227 haystack += start_idx;
13228 }
13229
13230 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13231 if (pos != NULL)
13232 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13233}
13234
13235/*
13236 * "string()" function
13237 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013238 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013239f_string(typval_T *argvars, typval_T *rettv)
13240{
13241 char_u *tofree;
13242 char_u numbuf[NUMBUFLEN];
13243
13244 rettv->v_type = VAR_STRING;
13245 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13246 get_copyID());
13247 /* Make a copy if we have a value but it's not in allocated memory. */
13248 if (rettv->vval.v_string != NULL && tofree == NULL)
13249 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13250}
13251
13252/*
13253 * "strlen()" function
13254 */
13255 static void
13256f_strlen(typval_T *argvars, typval_T *rettv)
13257{
13258 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013259 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013260}
13261
13262/*
13263 * "strchars()" function
13264 */
13265 static void
13266f_strchars(typval_T *argvars, typval_T *rettv)
13267{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013268 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013269 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013270 varnumber_T len = 0;
13271 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013272
13273 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013274 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013275 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013276 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013277 else
13278 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013279 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13280 while (*s != NUL)
13281 {
13282 func_mb_ptr2char_adv(&s);
13283 ++len;
13284 }
13285 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013286 }
13287}
13288
13289/*
13290 * "strdisplaywidth()" function
13291 */
13292 static void
13293f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13294{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013295 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013296 int col = 0;
13297
13298 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013299 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013300
13301 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13302}
13303
13304/*
13305 * "strwidth()" function
13306 */
13307 static void
13308f_strwidth(typval_T *argvars, typval_T *rettv)
13309{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013310 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013311
Bram Moolenaar13505972019-01-24 15:04:48 +010013312 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013313}
13314
13315/*
13316 * "strcharpart()" function
13317 */
13318 static void
13319f_strcharpart(typval_T *argvars, typval_T *rettv)
13320{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013321 char_u *p;
13322 int nchar;
13323 int nbyte = 0;
13324 int charlen;
13325 int len = 0;
13326 int slen;
13327 int error = FALSE;
13328
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013329 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013330 slen = (int)STRLEN(p);
13331
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013332 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013333 if (!error)
13334 {
13335 if (nchar > 0)
13336 while (nchar > 0 && nbyte < slen)
13337 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013338 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013339 --nchar;
13340 }
13341 else
13342 nbyte = nchar;
13343 if (argvars[2].v_type != VAR_UNKNOWN)
13344 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013345 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013346 while (charlen > 0 && nbyte + len < slen)
13347 {
13348 int off = nbyte + len;
13349
13350 if (off < 0)
13351 len += 1;
13352 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013353 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013354 --charlen;
13355 }
13356 }
13357 else
13358 len = slen - nbyte; /* default: all bytes that are available. */
13359 }
13360
13361 /*
13362 * Only return the overlap between the specified part and the actual
13363 * string.
13364 */
13365 if (nbyte < 0)
13366 {
13367 len += nbyte;
13368 nbyte = 0;
13369 }
13370 else if (nbyte > slen)
13371 nbyte = slen;
13372 if (len < 0)
13373 len = 0;
13374 else if (nbyte + len > slen)
13375 len = slen - nbyte;
13376
13377 rettv->v_type = VAR_STRING;
13378 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013379}
13380
13381/*
13382 * "strpart()" function
13383 */
13384 static void
13385f_strpart(typval_T *argvars, typval_T *rettv)
13386{
13387 char_u *p;
13388 int n;
13389 int len;
13390 int slen;
13391 int error = FALSE;
13392
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013393 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013394 slen = (int)STRLEN(p);
13395
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013396 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013397 if (error)
13398 len = 0;
13399 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013400 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013401 else
13402 len = slen - n; /* default len: all bytes that are available. */
13403
13404 /*
13405 * Only return the overlap between the specified part and the actual
13406 * string.
13407 */
13408 if (n < 0)
13409 {
13410 len += n;
13411 n = 0;
13412 }
13413 else if (n > slen)
13414 n = slen;
13415 if (len < 0)
13416 len = 0;
13417 else if (n + len > slen)
13418 len = slen - n;
13419
13420 rettv->v_type = VAR_STRING;
13421 rettv->vval.v_string = vim_strnsave(p + n, len);
13422}
13423
13424/*
13425 * "strridx()" function
13426 */
13427 static void
13428f_strridx(typval_T *argvars, typval_T *rettv)
13429{
13430 char_u buf[NUMBUFLEN];
13431 char_u *needle;
13432 char_u *haystack;
13433 char_u *rest;
13434 char_u *lastmatch = NULL;
13435 int haystack_len, end_idx;
13436
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013437 needle = tv_get_string_chk(&argvars[1]);
13438 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013439
13440 rettv->vval.v_number = -1;
13441 if (needle == NULL || haystack == NULL)
13442 return; /* type error; errmsg already given */
13443
13444 haystack_len = (int)STRLEN(haystack);
13445 if (argvars[2].v_type != VAR_UNKNOWN)
13446 {
13447 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013448 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013449 if (end_idx < 0)
13450 return; /* can never find a match */
13451 }
13452 else
13453 end_idx = haystack_len;
13454
13455 if (*needle == NUL)
13456 {
13457 /* Empty string matches past the end. */
13458 lastmatch = haystack + end_idx;
13459 }
13460 else
13461 {
13462 for (rest = haystack; *rest != '\0'; ++rest)
13463 {
13464 rest = (char_u *)strstr((char *)rest, (char *)needle);
13465 if (rest == NULL || rest > haystack + end_idx)
13466 break;
13467 lastmatch = rest;
13468 }
13469 }
13470
13471 if (lastmatch == NULL)
13472 rettv->vval.v_number = -1;
13473 else
13474 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13475}
13476
13477/*
13478 * "strtrans()" function
13479 */
13480 static void
13481f_strtrans(typval_T *argvars, typval_T *rettv)
13482{
13483 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013484 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013485}
13486
13487/*
13488 * "submatch()" function
13489 */
13490 static void
13491f_submatch(typval_T *argvars, typval_T *rettv)
13492{
13493 int error = FALSE;
13494 int no;
13495 int retList = 0;
13496
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013497 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013498 if (error)
13499 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013500 if (no < 0 || no >= NSUBEXP)
13501 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013502 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013503 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013504 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013505 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013506 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013507 if (error)
13508 return;
13509
13510 if (retList == 0)
13511 {
13512 rettv->v_type = VAR_STRING;
13513 rettv->vval.v_string = reg_submatch(no);
13514 }
13515 else
13516 {
13517 rettv->v_type = VAR_LIST;
13518 rettv->vval.v_list = reg_submatch_list(no);
13519 }
13520}
13521
13522/*
13523 * "substitute()" function
13524 */
13525 static void
13526f_substitute(typval_T *argvars, typval_T *rettv)
13527{
13528 char_u patbuf[NUMBUFLEN];
13529 char_u subbuf[NUMBUFLEN];
13530 char_u flagsbuf[NUMBUFLEN];
13531
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013532 char_u *str = tv_get_string_chk(&argvars[0]);
13533 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013534 char_u *sub = NULL;
13535 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013536 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013537
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013538 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13539 expr = &argvars[2];
13540 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013541 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013542
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013543 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013544 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13545 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013546 rettv->vval.v_string = NULL;
13547 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013548 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013549}
13550
13551/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013552 * "swapinfo(swap_filename)" function
13553 */
13554 static void
13555f_swapinfo(typval_T *argvars, typval_T *rettv)
13556{
13557 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013558 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013559}
13560
13561/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013562 * "swapname(expr)" function
13563 */
13564 static void
13565f_swapname(typval_T *argvars, typval_T *rettv)
13566{
13567 buf_T *buf;
13568
13569 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013570 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013571 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13572 || buf->b_ml.ml_mfp->mf_fname == NULL)
13573 rettv->vval.v_string = NULL;
13574 else
13575 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13576}
13577
13578/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013579 * "synID(lnum, col, trans)" function
13580 */
13581 static void
13582f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13583{
13584 int id = 0;
13585#ifdef FEAT_SYN_HL
13586 linenr_T lnum;
13587 colnr_T col;
13588 int trans;
13589 int transerr = FALSE;
13590
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013591 lnum = tv_get_lnum(argvars); /* -1 on type error */
13592 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13593 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013594
13595 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13596 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13597 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13598#endif
13599
13600 rettv->vval.v_number = id;
13601}
13602
13603/*
13604 * "synIDattr(id, what [, mode])" function
13605 */
13606 static void
13607f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13608{
13609 char_u *p = NULL;
13610#ifdef FEAT_SYN_HL
13611 int id;
13612 char_u *what;
13613 char_u *mode;
13614 char_u modebuf[NUMBUFLEN];
13615 int modec;
13616
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013617 id = (int)tv_get_number(&argvars[0]);
13618 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013619 if (argvars[2].v_type != VAR_UNKNOWN)
13620 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013621 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013622 modec = TOLOWER_ASC(mode[0]);
13623 if (modec != 't' && modec != 'c' && modec != 'g')
13624 modec = 0; /* replace invalid with current */
13625 }
13626 else
13627 {
13628#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13629 if (USE_24BIT)
13630 modec = 'g';
13631 else
13632#endif
13633 if (t_colors > 1)
13634 modec = 'c';
13635 else
13636 modec = 't';
13637 }
13638
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013639 switch (TOLOWER_ASC(what[0]))
13640 {
13641 case 'b':
13642 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13643 p = highlight_color(id, what, modec);
13644 else /* bold */
13645 p = highlight_has_attr(id, HL_BOLD, modec);
13646 break;
13647
13648 case 'f': /* fg[#] or font */
13649 p = highlight_color(id, what, modec);
13650 break;
13651
13652 case 'i':
13653 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13654 p = highlight_has_attr(id, HL_INVERSE, modec);
13655 else /* italic */
13656 p = highlight_has_attr(id, HL_ITALIC, modec);
13657 break;
13658
13659 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013660 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013661 break;
13662
13663 case 'r': /* reverse */
13664 p = highlight_has_attr(id, HL_INVERSE, modec);
13665 break;
13666
13667 case 's':
13668 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13669 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013670 /* strikeout */
13671 else if (TOLOWER_ASC(what[1]) == 't' &&
13672 TOLOWER_ASC(what[2]) == 'r')
13673 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013674 else /* standout */
13675 p = highlight_has_attr(id, HL_STANDOUT, modec);
13676 break;
13677
13678 case 'u':
13679 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13680 /* underline */
13681 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13682 else
13683 /* undercurl */
13684 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13685 break;
13686 }
13687
13688 if (p != NULL)
13689 p = vim_strsave(p);
13690#endif
13691 rettv->v_type = VAR_STRING;
13692 rettv->vval.v_string = p;
13693}
13694
13695/*
13696 * "synIDtrans(id)" function
13697 */
13698 static void
13699f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13700{
13701 int id;
13702
13703#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013704 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013705
13706 if (id > 0)
13707 id = syn_get_final_id(id);
13708 else
13709#endif
13710 id = 0;
13711
13712 rettv->vval.v_number = id;
13713}
13714
13715/*
13716 * "synconcealed(lnum, col)" function
13717 */
13718 static void
13719f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13720{
13721#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13722 linenr_T lnum;
13723 colnr_T col;
13724 int syntax_flags = 0;
13725 int cchar;
13726 int matchid = 0;
13727 char_u str[NUMBUFLEN];
13728#endif
13729
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013730 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013731
13732#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013733 lnum = tv_get_lnum(argvars); /* -1 on type error */
13734 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013735
13736 vim_memset(str, NUL, sizeof(str));
13737
13738 if (rettv_list_alloc(rettv) != FAIL)
13739 {
13740 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13741 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13742 && curwin->w_p_cole > 0)
13743 {
13744 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13745 syntax_flags = get_syntax_info(&matchid);
13746
13747 /* get the conceal character */
13748 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13749 {
13750 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013751 if (cchar == NUL && curwin->w_p_cole == 1)
13752 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013753 if (cchar != NUL)
13754 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013755 if (has_mbyte)
13756 (*mb_char2bytes)(cchar, str);
13757 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013758 str[0] = cchar;
13759 }
13760 }
13761 }
13762
13763 list_append_number(rettv->vval.v_list,
13764 (syntax_flags & HL_CONCEAL) != 0);
13765 /* -1 to auto-determine strlen */
13766 list_append_string(rettv->vval.v_list, str, -1);
13767 list_append_number(rettv->vval.v_list, matchid);
13768 }
13769#endif
13770}
13771
13772/*
13773 * "synstack(lnum, col)" function
13774 */
13775 static void
13776f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13777{
13778#ifdef FEAT_SYN_HL
13779 linenr_T lnum;
13780 colnr_T col;
13781 int i;
13782 int id;
13783#endif
13784
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013785 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013786
13787#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013788 lnum = tv_get_lnum(argvars); /* -1 on type error */
13789 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013790
13791 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13792 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13793 && rettv_list_alloc(rettv) != FAIL)
13794 {
13795 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13796 for (i = 0; ; ++i)
13797 {
13798 id = syn_get_stack_item(i);
13799 if (id < 0)
13800 break;
13801 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13802 break;
13803 }
13804 }
13805#endif
13806}
13807
13808 static void
13809get_cmd_output_as_rettv(
13810 typval_T *argvars,
13811 typval_T *rettv,
13812 int retlist)
13813{
13814 char_u *res = NULL;
13815 char_u *p;
13816 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013817 int err = FALSE;
13818 FILE *fd;
13819 list_T *list = NULL;
13820 int flags = SHELL_SILENT;
13821
13822 rettv->v_type = VAR_STRING;
13823 rettv->vval.v_string = NULL;
13824 if (check_restricted() || check_secure())
13825 goto errret;
13826
13827 if (argvars[1].v_type != VAR_UNKNOWN)
13828 {
13829 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013830 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013831 * command.
13832 */
13833 if ((infile = vim_tempname('i', TRUE)) == NULL)
13834 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013835 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013836 goto errret;
13837 }
13838
13839 fd = mch_fopen((char *)infile, WRITEBIN);
13840 if (fd == NULL)
13841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013842 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013843 goto errret;
13844 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013845 if (argvars[1].v_type == VAR_NUMBER)
13846 {
13847 linenr_T lnum;
13848 buf_T *buf;
13849
13850 buf = buflist_findnr(argvars[1].vval.v_number);
13851 if (buf == NULL)
13852 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013853 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013854 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013855 goto errret;
13856 }
13857
13858 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13859 {
13860 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13861 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13862 {
13863 err = TRUE;
13864 break;
13865 }
13866 if (putc(NL, fd) == EOF)
13867 {
13868 err = TRUE;
13869 break;
13870 }
13871 }
13872 }
13873 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013874 {
13875 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13876 err = TRUE;
13877 }
13878 else
13879 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013880 size_t len;
13881 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013882
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013883 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013884 if (p == NULL)
13885 {
13886 fclose(fd);
13887 goto errret; /* type error; errmsg already given */
13888 }
13889 len = STRLEN(p);
13890 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13891 err = TRUE;
13892 }
13893 if (fclose(fd) != 0)
13894 err = TRUE;
13895 if (err)
13896 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013897 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013898 goto errret;
13899 }
13900 }
13901
13902 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13903 * echoes typeahead, that messes up the display. */
13904 if (!msg_silent)
13905 flags += SHELL_COOKED;
13906
13907 if (retlist)
13908 {
13909 int len;
13910 listitem_T *li;
13911 char_u *s = NULL;
13912 char_u *start;
13913 char_u *end;
13914 int i;
13915
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013916 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013917 if (res == NULL)
13918 goto errret;
13919
13920 list = list_alloc();
13921 if (list == NULL)
13922 goto errret;
13923
13924 for (i = 0; i < len; ++i)
13925 {
13926 start = res + i;
13927 while (i < len && res[i] != NL)
13928 ++i;
13929 end = res + i;
13930
Bram Moolenaar964b3742019-05-24 18:54:09 +020013931 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013932 if (s == NULL)
13933 goto errret;
13934
13935 for (p = s; start < end; ++p, ++start)
13936 *p = *start == NUL ? NL : *start;
13937 *p = NUL;
13938
13939 li = listitem_alloc();
13940 if (li == NULL)
13941 {
13942 vim_free(s);
13943 goto errret;
13944 }
13945 li->li_tv.v_type = VAR_STRING;
13946 li->li_tv.v_lock = 0;
13947 li->li_tv.vval.v_string = s;
13948 list_append(list, li);
13949 }
13950
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013951 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013952 list = NULL;
13953 }
13954 else
13955 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013956 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013957#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013958 /* translate <CR><NL> into <NL> */
13959 if (res != NULL)
13960 {
13961 char_u *s, *d;
13962
13963 d = res;
13964 for (s = res; *s; ++s)
13965 {
13966 if (s[0] == CAR && s[1] == NL)
13967 ++s;
13968 *d++ = *s;
13969 }
13970 *d = NUL;
13971 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013972#endif
13973 rettv->vval.v_string = res;
13974 res = NULL;
13975 }
13976
13977errret:
13978 if (infile != NULL)
13979 {
13980 mch_remove(infile);
13981 vim_free(infile);
13982 }
13983 if (res != NULL)
13984 vim_free(res);
13985 if (list != NULL)
13986 list_free(list);
13987}
13988
13989/*
13990 * "system()" function
13991 */
13992 static void
13993f_system(typval_T *argvars, typval_T *rettv)
13994{
13995 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13996}
13997
13998/*
13999 * "systemlist()" function
14000 */
14001 static void
14002f_systemlist(typval_T *argvars, typval_T *rettv)
14003{
14004 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14005}
14006
14007/*
14008 * "tabpagebuflist()" function
14009 */
14010 static void
14011f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14012{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014013 tabpage_T *tp;
14014 win_T *wp = NULL;
14015
14016 if (argvars[0].v_type == VAR_UNKNOWN)
14017 wp = firstwin;
14018 else
14019 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014020 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014021 if (tp != NULL)
14022 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14023 }
14024 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14025 {
14026 for (; wp != NULL; wp = wp->w_next)
14027 if (list_append_number(rettv->vval.v_list,
14028 wp->w_buffer->b_fnum) == FAIL)
14029 break;
14030 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014031}
14032
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014033/*
14034 * "tabpagenr()" function
14035 */
14036 static void
14037f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14038{
14039 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014040 char_u *arg;
14041
14042 if (argvars[0].v_type != VAR_UNKNOWN)
14043 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014044 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014045 nr = 0;
14046 if (arg != NULL)
14047 {
14048 if (STRCMP(arg, "$") == 0)
14049 nr = tabpage_index(NULL) - 1;
14050 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014051 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014052 }
14053 }
14054 else
14055 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014056 rettv->vval.v_number = nr;
14057}
14058
14059
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014060/*
14061 * Common code for tabpagewinnr() and winnr().
14062 */
14063 static int
14064get_winnr(tabpage_T *tp, typval_T *argvar)
14065{
14066 win_T *twin;
14067 int nr = 1;
14068 win_T *wp;
14069 char_u *arg;
14070
14071 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14072 if (argvar->v_type != VAR_UNKNOWN)
14073 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014074 int invalid_arg = FALSE;
14075
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014076 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014077 if (arg == NULL)
14078 nr = 0; /* type error; errmsg already given */
14079 else if (STRCMP(arg, "$") == 0)
14080 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14081 else if (STRCMP(arg, "#") == 0)
14082 {
14083 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14084 if (twin == NULL)
14085 nr = 0;
14086 }
14087 else
14088 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014089 long count;
14090 char_u *endp;
14091
14092 // Extract the window count (if specified). e.g. winnr('3j')
14093 count = strtol((char *)arg, (char **)&endp, 10);
14094 if (count <= 0)
14095 count = 1; // if count is not specified, default to 1
14096 if (endp != NULL && *endp != '\0')
14097 {
14098 if (STRCMP(endp, "j") == 0)
14099 twin = win_vert_neighbor(tp, twin, FALSE, count);
14100 else if (STRCMP(endp, "k") == 0)
14101 twin = win_vert_neighbor(tp, twin, TRUE, count);
14102 else if (STRCMP(endp, "h") == 0)
14103 twin = win_horz_neighbor(tp, twin, TRUE, count);
14104 else if (STRCMP(endp, "l") == 0)
14105 twin = win_horz_neighbor(tp, twin, FALSE, count);
14106 else
14107 invalid_arg = TRUE;
14108 }
14109 else
14110 invalid_arg = TRUE;
14111 }
14112
14113 if (invalid_arg)
14114 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014115 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014116 nr = 0;
14117 }
14118 }
14119
14120 if (nr > 0)
14121 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14122 wp != twin; wp = wp->w_next)
14123 {
14124 if (wp == NULL)
14125 {
14126 /* didn't find it in this tabpage */
14127 nr = 0;
14128 break;
14129 }
14130 ++nr;
14131 }
14132 return nr;
14133}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014134
14135/*
14136 * "tabpagewinnr()" function
14137 */
14138 static void
14139f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14140{
14141 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014142 tabpage_T *tp;
14143
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014144 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014145 if (tp == NULL)
14146 nr = 0;
14147 else
14148 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014149 rettv->vval.v_number = nr;
14150}
14151
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014152/*
14153 * "tagfiles()" function
14154 */
14155 static void
14156f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14157{
14158 char_u *fname;
14159 tagname_T tn;
14160 int first;
14161
14162 if (rettv_list_alloc(rettv) == FAIL)
14163 return;
14164 fname = alloc(MAXPATHL);
14165 if (fname == NULL)
14166 return;
14167
14168 for (first = TRUE; ; first = FALSE)
14169 if (get_tagfname(&tn, first, fname) == FAIL
14170 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14171 break;
14172 tagname_free(&tn);
14173 vim_free(fname);
14174}
14175
14176/*
14177 * "taglist()" function
14178 */
14179 static void
14180f_taglist(typval_T *argvars, typval_T *rettv)
14181{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014182 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014183 char_u *tag_pattern;
14184
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014185 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014186
14187 rettv->vval.v_number = FALSE;
14188 if (*tag_pattern == NUL)
14189 return;
14190
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014191 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014192 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014193 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014194 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014195}
14196
14197/*
14198 * "tempname()" function
14199 */
14200 static void
14201f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14202{
14203 static int x = 'A';
14204
14205 rettv->v_type = VAR_STRING;
14206 rettv->vval.v_string = vim_tempname(x, FALSE);
14207
14208 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14209 * names. Skip 'I' and 'O', they are used for shell redirection. */
14210 do
14211 {
14212 if (x == 'Z')
14213 x = '0';
14214 else if (x == '9')
14215 x = 'A';
14216 else
14217 {
14218#ifdef EBCDIC
14219 if (x == 'I')
14220 x = 'J';
14221 else if (x == 'R')
14222 x = 'S';
14223 else
14224#endif
14225 ++x;
14226 }
14227 } while (x == 'I' || x == 'O');
14228}
14229
14230#ifdef FEAT_FLOAT
14231/*
14232 * "tan()" function
14233 */
14234 static void
14235f_tan(typval_T *argvars, typval_T *rettv)
14236{
14237 float_T f = 0.0;
14238
14239 rettv->v_type = VAR_FLOAT;
14240 if (get_float_arg(argvars, &f) == OK)
14241 rettv->vval.v_float = tan(f);
14242 else
14243 rettv->vval.v_float = 0.0;
14244}
14245
14246/*
14247 * "tanh()" function
14248 */
14249 static void
14250f_tanh(typval_T *argvars, typval_T *rettv)
14251{
14252 float_T f = 0.0;
14253
14254 rettv->v_type = VAR_FLOAT;
14255 if (get_float_arg(argvars, &f) == OK)
14256 rettv->vval.v_float = tanh(f);
14257 else
14258 rettv->vval.v_float = 0.0;
14259}
14260#endif
14261
14262/*
14263 * "test_alloc_fail(id, countdown, repeat)" function
14264 */
14265 static void
14266f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14267{
14268 if (argvars[0].v_type != VAR_NUMBER
14269 || argvars[0].vval.v_number <= 0
14270 || argvars[1].v_type != VAR_NUMBER
14271 || argvars[1].vval.v_number < 0
14272 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014273 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014274 else
14275 {
14276 alloc_fail_id = argvars[0].vval.v_number;
14277 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014278 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014279 alloc_fail_countdown = argvars[1].vval.v_number;
14280 alloc_fail_repeat = argvars[2].vval.v_number;
14281 did_outofmem_msg = FALSE;
14282 }
14283}
14284
14285/*
14286 * "test_autochdir()"
14287 */
14288 static void
14289f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14290{
14291#if defined(FEAT_AUTOCHDIR)
14292 test_autochdir = TRUE;
14293#endif
14294}
14295
14296/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014297 * "test_feedinput()"
14298 */
14299 static void
14300f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14301{
14302#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014303 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014304
14305 if (val != NULL)
14306 {
14307 trash_input_buf();
14308 add_to_input_buf_csi(val, (int)STRLEN(val));
14309 }
14310#endif
14311}
14312
14313/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014314 * "test_getvalue({name})" function
14315 */
14316 static void
14317f_test_getvalue(typval_T *argvars, typval_T *rettv)
14318{
14319 if (argvars[0].v_type != VAR_STRING)
14320 emsg(_(e_invarg));
14321 else
14322 {
14323 char_u *name = tv_get_string(&argvars[0]);
14324
14325 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14326 rettv->vval.v_number = need_fileinfo;
14327 else
14328 semsg(_(e_invarg2), name);
14329 }
14330}
14331
14332/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014333 * "test_option_not_set({name})" function
14334 */
14335 static void
14336f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14337{
14338 char_u *name = (char_u *)"";
14339
14340 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014341 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014342 else
14343 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014344 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014345 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014346 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014347 }
14348}
14349
14350/*
14351 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014352 */
14353 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014354f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014355{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014356 char_u *name = (char_u *)"";
14357 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014358 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014359
14360 if (argvars[0].v_type != VAR_STRING
14361 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014362 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014363 else
14364 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014365 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014366 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014367
14368 if (STRCMP(name, (char_u *)"redraw") == 0)
14369 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014370 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14371 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014372 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14373 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014374 else if (STRCMP(name, (char_u *)"starting") == 0)
14375 {
14376 if (val)
14377 {
14378 if (save_starting < 0)
14379 save_starting = starting;
14380 starting = 0;
14381 }
14382 else
14383 {
14384 starting = save_starting;
14385 save_starting = -1;
14386 }
14387 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014388 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14389 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014390 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14391 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014392 else if (STRCMP(name, (char_u *)"ALL") == 0)
14393 {
14394 disable_char_avail_for_testing = FALSE;
14395 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014396 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014397 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014398 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014399 if (save_starting >= 0)
14400 {
14401 starting = save_starting;
14402 save_starting = -1;
14403 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014404 }
14405 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014406 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014407 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014408}
14409
14410/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014411 * "test_refcount({expr})" function
14412 */
14413 static void
14414f_test_refcount(typval_T *argvars, typval_T *rettv)
14415{
14416 int retval = -1;
14417
14418 switch (argvars[0].v_type)
14419 {
14420 case VAR_UNKNOWN:
14421 case VAR_NUMBER:
14422 case VAR_FLOAT:
14423 case VAR_SPECIAL:
14424 case VAR_STRING:
14425 break;
14426 case VAR_JOB:
14427#ifdef FEAT_JOB_CHANNEL
14428 if (argvars[0].vval.v_job != NULL)
14429 retval = argvars[0].vval.v_job->jv_refcount - 1;
14430#endif
14431 break;
14432 case VAR_CHANNEL:
14433#ifdef FEAT_JOB_CHANNEL
14434 if (argvars[0].vval.v_channel != NULL)
14435 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14436#endif
14437 break;
14438 case VAR_FUNC:
14439 if (argvars[0].vval.v_string != NULL)
14440 {
14441 ufunc_T *fp;
14442
14443 fp = find_func(argvars[0].vval.v_string);
14444 if (fp != NULL)
14445 retval = fp->uf_refcount;
14446 }
14447 break;
14448 case VAR_PARTIAL:
14449 if (argvars[0].vval.v_partial != NULL)
14450 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14451 break;
14452 case VAR_BLOB:
14453 if (argvars[0].vval.v_blob != NULL)
14454 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14455 break;
14456 case VAR_LIST:
14457 if (argvars[0].vval.v_list != NULL)
14458 retval = argvars[0].vval.v_list->lv_refcount - 1;
14459 break;
14460 case VAR_DICT:
14461 if (argvars[0].vval.v_dict != NULL)
14462 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14463 break;
14464 }
14465
14466 rettv->v_type = VAR_NUMBER;
14467 rettv->vval.v_number = retval;
14468
14469}
14470
14471/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014472 * "test_garbagecollect_now()" function
14473 */
14474 static void
14475f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14476{
14477 /* This is dangerous, any Lists and Dicts used internally may be freed
14478 * while still in use. */
14479 garbage_collect(TRUE);
14480}
14481
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014482/*
14483 * "test_ignore_error()" function
14484 */
14485 static void
14486f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14487{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014488 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014489}
14490
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014491 static void
14492f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14493{
14494 rettv->v_type = VAR_BLOB;
14495 rettv->vval.v_blob = NULL;
14496}
14497
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014498#ifdef FEAT_JOB_CHANNEL
14499 static void
14500f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14501{
14502 rettv->v_type = VAR_CHANNEL;
14503 rettv->vval.v_channel = NULL;
14504}
14505#endif
14506
14507 static void
14508f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14509{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014510 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014511}
14512
14513#ifdef FEAT_JOB_CHANNEL
14514 static void
14515f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14516{
14517 rettv->v_type = VAR_JOB;
14518 rettv->vval.v_job = NULL;
14519}
14520#endif
14521
14522 static void
14523f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14524{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014525 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014526}
14527
14528 static void
14529f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14530{
14531 rettv->v_type = VAR_PARTIAL;
14532 rettv->vval.v_partial = NULL;
14533}
14534
14535 static void
14536f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14537{
14538 rettv->v_type = VAR_STRING;
14539 rettv->vval.v_string = NULL;
14540}
14541
Bram Moolenaarab186732018-09-14 21:27:06 +020014542#ifdef FEAT_GUI
14543 static void
14544f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14545{
14546 char_u *which;
14547 long value;
14548 int dragging;
14549 scrollbar_T *sb = NULL;
14550
14551 if (argvars[0].v_type != VAR_STRING
14552 || (argvars[1].v_type) != VAR_NUMBER
14553 || (argvars[2].v_type) != VAR_NUMBER)
14554 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014555 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014556 return;
14557 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014558 which = tv_get_string(&argvars[0]);
14559 value = tv_get_number(&argvars[1]);
14560 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014561
14562 if (STRCMP(which, "left") == 0)
14563 sb = &curwin->w_scrollbars[SBAR_LEFT];
14564 else if (STRCMP(which, "right") == 0)
14565 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14566 else if (STRCMP(which, "hor") == 0)
14567 sb = &gui.bottom_sbar;
14568 if (sb == NULL)
14569 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014570 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014571 return;
14572 }
14573 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014574# ifndef USE_ON_FLY_SCROLL
14575 // need to loop through normal_cmd() to handle the scroll events
14576 exec_normal(FALSE, TRUE, FALSE);
14577# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014578}
14579#endif
14580
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014581#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014582 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014583f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14584{
14585 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14586 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14587}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014588#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014589
14590 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014591f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14592{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014593 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014594}
14595
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014596/*
14597 * Get a callback from "arg". It can be a Funcref or a function name.
14598 * When "arg" is zero return an empty string.
14599 * Return NULL for an invalid argument.
14600 */
14601 char_u *
14602get_callback(typval_T *arg, partial_T **pp)
14603{
14604 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14605 {
14606 *pp = arg->vval.v_partial;
14607 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014608 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014609 }
14610 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014611 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014612 {
14613 func_ref(arg->vval.v_string);
14614 return arg->vval.v_string;
14615 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014616 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14617 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014618 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014619 return NULL;
14620}
14621
14622/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014623 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014624 */
14625 void
14626free_callback(char_u *callback, partial_T *partial)
14627{
14628 if (partial != NULL)
14629 partial_unref(partial);
14630 else if (callback != NULL)
14631 {
14632 func_unref(callback);
14633 vim_free(callback);
14634 }
14635}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014636
14637#ifdef FEAT_TIMERS
14638/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014639 * "timer_info([timer])" function
14640 */
14641 static void
14642f_timer_info(typval_T *argvars, typval_T *rettv)
14643{
14644 timer_T *timer = NULL;
14645
14646 if (rettv_list_alloc(rettv) != OK)
14647 return;
14648 if (argvars[0].v_type != VAR_UNKNOWN)
14649 {
14650 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014651 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014652 else
14653 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014654 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014655 if (timer != NULL)
14656 add_timer_info(rettv, timer);
14657 }
14658 }
14659 else
14660 add_timer_info_all(rettv);
14661}
14662
14663/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014664 * "timer_pause(timer, paused)" function
14665 */
14666 static void
14667f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14668{
14669 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014670 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014671
14672 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014673 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014674 else
14675 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014676 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014677 if (timer != NULL)
14678 timer->tr_paused = paused;
14679 }
14680}
14681
14682/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014683 * "timer_start(time, callback [, options])" function
14684 */
14685 static void
14686f_timer_start(typval_T *argvars, typval_T *rettv)
14687{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014688 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014689 timer_T *timer;
14690 int repeat = 0;
14691 char_u *callback;
14692 dict_T *dict;
14693 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014694
Bram Moolenaar75537a92016-09-05 22:45:28 +020014695 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014696 if (check_secure())
14697 return;
14698 if (argvars[2].v_type != VAR_UNKNOWN)
14699 {
14700 if (argvars[2].v_type != VAR_DICT
14701 || (dict = argvars[2].vval.v_dict) == NULL)
14702 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014703 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014704 return;
14705 }
14706 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014707 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014708 }
14709
Bram Moolenaar75537a92016-09-05 22:45:28 +020014710 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014711 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014712 return;
14713
14714 timer = create_timer(msec, repeat);
14715 if (timer == NULL)
14716 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014717 else
14718 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014719 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014720 timer->tr_callback = vim_strsave(callback);
14721 else
14722 /* pointer into the partial */
14723 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014724 timer->tr_partial = partial;
14725 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014726 }
14727}
14728
14729/*
14730 * "timer_stop(timer)" function
14731 */
14732 static void
14733f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14734{
14735 timer_T *timer;
14736
14737 if (argvars[0].v_type != VAR_NUMBER)
14738 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014739 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014740 return;
14741 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014742 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014743 if (timer != NULL)
14744 stop_timer(timer);
14745}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014746
14747/*
14748 * "timer_stopall()" function
14749 */
14750 static void
14751f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14752{
14753 stop_all_timers();
14754}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014755#endif
14756
14757/*
14758 * "tolower(string)" function
14759 */
14760 static void
14761f_tolower(typval_T *argvars, typval_T *rettv)
14762{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014763 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014764 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014765}
14766
14767/*
14768 * "toupper(string)" function
14769 */
14770 static void
14771f_toupper(typval_T *argvars, typval_T *rettv)
14772{
14773 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014774 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014775}
14776
14777/*
14778 * "tr(string, fromstr, tostr)" function
14779 */
14780 static void
14781f_tr(typval_T *argvars, typval_T *rettv)
14782{
14783 char_u *in_str;
14784 char_u *fromstr;
14785 char_u *tostr;
14786 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014787 int inlen;
14788 int fromlen;
14789 int tolen;
14790 int idx;
14791 char_u *cpstr;
14792 int cplen;
14793 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014794 char_u buf[NUMBUFLEN];
14795 char_u buf2[NUMBUFLEN];
14796 garray_T ga;
14797
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014798 in_str = tv_get_string(&argvars[0]);
14799 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14800 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014801
14802 /* Default return value: empty string. */
14803 rettv->v_type = VAR_STRING;
14804 rettv->vval.v_string = NULL;
14805 if (fromstr == NULL || tostr == NULL)
14806 return; /* type error; errmsg already given */
14807 ga_init2(&ga, (int)sizeof(char), 80);
14808
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014809 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014810 /* not multi-byte: fromstr and tostr must be the same length */
14811 if (STRLEN(fromstr) != STRLEN(tostr))
14812 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014813error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014814 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014815 ga_clear(&ga);
14816 return;
14817 }
14818
14819 /* fromstr and tostr have to contain the same number of chars */
14820 while (*in_str != NUL)
14821 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014822 if (has_mbyte)
14823 {
14824 inlen = (*mb_ptr2len)(in_str);
14825 cpstr = in_str;
14826 cplen = inlen;
14827 idx = 0;
14828 for (p = fromstr; *p != NUL; p += fromlen)
14829 {
14830 fromlen = (*mb_ptr2len)(p);
14831 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14832 {
14833 for (p = tostr; *p != NUL; p += tolen)
14834 {
14835 tolen = (*mb_ptr2len)(p);
14836 if (idx-- == 0)
14837 {
14838 cplen = tolen;
14839 cpstr = p;
14840 break;
14841 }
14842 }
14843 if (*p == NUL) /* tostr is shorter than fromstr */
14844 goto error;
14845 break;
14846 }
14847 ++idx;
14848 }
14849
14850 if (first && cpstr == in_str)
14851 {
14852 /* Check that fromstr and tostr have the same number of
14853 * (multi-byte) characters. Done only once when a character
14854 * of in_str doesn't appear in fromstr. */
14855 first = FALSE;
14856 for (p = tostr; *p != NUL; p += tolen)
14857 {
14858 tolen = (*mb_ptr2len)(p);
14859 --idx;
14860 }
14861 if (idx != 0)
14862 goto error;
14863 }
14864
14865 (void)ga_grow(&ga, cplen);
14866 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14867 ga.ga_len += cplen;
14868
14869 in_str += inlen;
14870 }
14871 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014872 {
14873 /* When not using multi-byte chars we can do it faster. */
14874 p = vim_strchr(fromstr, *in_str);
14875 if (p != NULL)
14876 ga_append(&ga, tostr[p - fromstr]);
14877 else
14878 ga_append(&ga, *in_str);
14879 ++in_str;
14880 }
14881 }
14882
14883 /* add a terminating NUL */
14884 (void)ga_grow(&ga, 1);
14885 ga_append(&ga, NUL);
14886
14887 rettv->vval.v_string = ga.ga_data;
14888}
14889
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014890/*
14891 * "trim({expr})" function
14892 */
14893 static void
14894f_trim(typval_T *argvars, typval_T *rettv)
14895{
14896 char_u buf1[NUMBUFLEN];
14897 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014898 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014899 char_u *mask = NULL;
14900 char_u *tail;
14901 char_u *prev;
14902 char_u *p;
14903 int c1;
14904
14905 rettv->v_type = VAR_STRING;
14906 if (head == NULL)
14907 {
14908 rettv->vval.v_string = NULL;
14909 return;
14910 }
14911
14912 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014913 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014914
14915 while (*head != NUL)
14916 {
14917 c1 = PTR2CHAR(head);
14918 if (mask == NULL)
14919 {
14920 if (c1 > ' ' && c1 != 0xa0)
14921 break;
14922 }
14923 else
14924 {
14925 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14926 if (c1 == PTR2CHAR(p))
14927 break;
14928 if (*p == NUL)
14929 break;
14930 }
14931 MB_PTR_ADV(head);
14932 }
14933
14934 for (tail = head + STRLEN(head); tail > head; tail = prev)
14935 {
14936 prev = tail;
14937 MB_PTR_BACK(head, prev);
14938 c1 = PTR2CHAR(prev);
14939 if (mask == NULL)
14940 {
14941 if (c1 > ' ' && c1 != 0xa0)
14942 break;
14943 }
14944 else
14945 {
14946 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14947 if (c1 == PTR2CHAR(p))
14948 break;
14949 if (*p == NUL)
14950 break;
14951 }
14952 }
14953 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14954}
14955
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014956#ifdef FEAT_FLOAT
14957/*
14958 * "trunc({float})" function
14959 */
14960 static void
14961f_trunc(typval_T *argvars, typval_T *rettv)
14962{
14963 float_T f = 0.0;
14964
14965 rettv->v_type = VAR_FLOAT;
14966 if (get_float_arg(argvars, &f) == OK)
14967 /* trunc() is not in C90, use floor() or ceil() instead. */
14968 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14969 else
14970 rettv->vval.v_float = 0.0;
14971}
14972#endif
14973
14974/*
14975 * "type(expr)" function
14976 */
14977 static void
14978f_type(typval_T *argvars, typval_T *rettv)
14979{
14980 int n = -1;
14981
14982 switch (argvars[0].v_type)
14983 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014984 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14985 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014986 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014987 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14988 case VAR_LIST: n = VAR_TYPE_LIST; break;
14989 case VAR_DICT: n = VAR_TYPE_DICT; break;
14990 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014991 case VAR_SPECIAL:
14992 if (argvars[0].vval.v_number == VVAL_FALSE
14993 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014994 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014995 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014996 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014997 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014998 case VAR_JOB: n = VAR_TYPE_JOB; break;
14999 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015000 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015001 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015002 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015003 n = -1;
15004 break;
15005 }
15006 rettv->vval.v_number = n;
15007}
15008
15009/*
15010 * "undofile(name)" function
15011 */
15012 static void
15013f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15014{
15015 rettv->v_type = VAR_STRING;
15016#ifdef FEAT_PERSISTENT_UNDO
15017 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015018 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015019
15020 if (*fname == NUL)
15021 {
15022 /* If there is no file name there will be no undo file. */
15023 rettv->vval.v_string = NULL;
15024 }
15025 else
15026 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015027 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015028
15029 if (ffname != NULL)
15030 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15031 vim_free(ffname);
15032 }
15033 }
15034#else
15035 rettv->vval.v_string = NULL;
15036#endif
15037}
15038
15039/*
15040 * "undotree()" function
15041 */
15042 static void
15043f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15044{
15045 if (rettv_dict_alloc(rettv) == OK)
15046 {
15047 dict_T *dict = rettv->vval.v_dict;
15048 list_T *list;
15049
Bram Moolenaare0be1672018-07-08 16:50:37 +020015050 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15051 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15052 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15053 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15054 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15055 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015056
15057 list = list_alloc();
15058 if (list != NULL)
15059 {
15060 u_eval_tree(curbuf->b_u_oldhead, list);
15061 dict_add_list(dict, "entries", list);
15062 }
15063 }
15064}
15065
15066/*
15067 * "values(dict)" function
15068 */
15069 static void
15070f_values(typval_T *argvars, typval_T *rettv)
15071{
15072 dict_list(argvars, rettv, 1);
15073}
15074
15075/*
15076 * "virtcol(string)" function
15077 */
15078 static void
15079f_virtcol(typval_T *argvars, typval_T *rettv)
15080{
15081 colnr_T vcol = 0;
15082 pos_T *fp;
15083 int fnum = curbuf->b_fnum;
15084
15085 fp = var2fpos(&argvars[0], FALSE, &fnum);
15086 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15087 && fnum == curbuf->b_fnum)
15088 {
15089 getvvcol(curwin, fp, NULL, NULL, &vcol);
15090 ++vcol;
15091 }
15092
15093 rettv->vval.v_number = vcol;
15094}
15095
15096/*
15097 * "visualmode()" function
15098 */
15099 static void
15100f_visualmode(typval_T *argvars, typval_T *rettv)
15101{
15102 char_u str[2];
15103
15104 rettv->v_type = VAR_STRING;
15105 str[0] = curbuf->b_visual_mode_eval;
15106 str[1] = NUL;
15107 rettv->vval.v_string = vim_strsave(str);
15108
15109 /* A non-zero number or non-empty string argument: reset mode. */
15110 if (non_zero_arg(&argvars[0]))
15111 curbuf->b_visual_mode_eval = NUL;
15112}
15113
15114/*
15115 * "wildmenumode()" function
15116 */
15117 static void
15118f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15119{
15120#ifdef FEAT_WILDMENU
15121 if (wild_menu_showing)
15122 rettv->vval.v_number = 1;
15123#endif
15124}
15125
15126/*
15127 * "winbufnr(nr)" function
15128 */
15129 static void
15130f_winbufnr(typval_T *argvars, typval_T *rettv)
15131{
15132 win_T *wp;
15133
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015134 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015135 if (wp == NULL)
15136 rettv->vval.v_number = -1;
15137 else
15138 rettv->vval.v_number = wp->w_buffer->b_fnum;
15139}
15140
15141/*
15142 * "wincol()" function
15143 */
15144 static void
15145f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15146{
15147 validate_cursor();
15148 rettv->vval.v_number = curwin->w_wcol + 1;
15149}
15150
15151/*
15152 * "winheight(nr)" function
15153 */
15154 static void
15155f_winheight(typval_T *argvars, typval_T *rettv)
15156{
15157 win_T *wp;
15158
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015159 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015160 if (wp == NULL)
15161 rettv->vval.v_number = -1;
15162 else
15163 rettv->vval.v_number = wp->w_height;
15164}
15165
15166/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015167 * "winlayout()" function
15168 */
15169 static void
15170f_winlayout(typval_T *argvars, typval_T *rettv)
15171{
15172 tabpage_T *tp;
15173
15174 if (rettv_list_alloc(rettv) != OK)
15175 return;
15176
15177 if (argvars[0].v_type == VAR_UNKNOWN)
15178 tp = curtab;
15179 else
15180 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015181 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015182 if (tp == NULL)
15183 return;
15184 }
15185
15186 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15187}
15188
15189/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015190 * "winline()" function
15191 */
15192 static void
15193f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15194{
15195 validate_cursor();
15196 rettv->vval.v_number = curwin->w_wrow + 1;
15197}
15198
15199/*
15200 * "winnr()" function
15201 */
15202 static void
15203f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15204{
15205 int nr = 1;
15206
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015207 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015208 rettv->vval.v_number = nr;
15209}
15210
15211/*
15212 * "winrestcmd()" function
15213 */
15214 static void
15215f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15216{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015217 win_T *wp;
15218 int winnr = 1;
15219 garray_T ga;
15220 char_u buf[50];
15221
15222 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015223 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015224 {
15225 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15226 ga_concat(&ga, buf);
15227 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15228 ga_concat(&ga, buf);
15229 ++winnr;
15230 }
15231 ga_append(&ga, NUL);
15232
15233 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015234 rettv->v_type = VAR_STRING;
15235}
15236
15237/*
15238 * "winrestview()" function
15239 */
15240 static void
15241f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15242{
15243 dict_T *dict;
15244
15245 if (argvars[0].v_type != VAR_DICT
15246 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015247 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015248 else
15249 {
15250 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015251 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015252 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015253 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015254 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015255 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015256 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15257 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015258 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015259 curwin->w_set_curswant = FALSE;
15260 }
15261
15262 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015263 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015264#ifdef FEAT_DIFF
15265 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015266 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015267#endif
15268 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015269 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015270 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015271 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015272
15273 check_cursor();
15274 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015275 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015276 changed_window_setting();
15277
15278 if (curwin->w_topline <= 0)
15279 curwin->w_topline = 1;
15280 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15281 curwin->w_topline = curbuf->b_ml.ml_line_count;
15282#ifdef FEAT_DIFF
15283 check_topfill(curwin, TRUE);
15284#endif
15285 }
15286}
15287
15288/*
15289 * "winsaveview()" function
15290 */
15291 static void
15292f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15293{
15294 dict_T *dict;
15295
15296 if (rettv_dict_alloc(rettv) == FAIL)
15297 return;
15298 dict = rettv->vval.v_dict;
15299
Bram Moolenaare0be1672018-07-08 16:50:37 +020015300 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15301 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015302 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015303 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015304 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015305
Bram Moolenaare0be1672018-07-08 16:50:37 +020015306 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015307#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015308 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015309#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015310 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15311 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015312}
15313
15314/*
15315 * "winwidth(nr)" function
15316 */
15317 static void
15318f_winwidth(typval_T *argvars, typval_T *rettv)
15319{
15320 win_T *wp;
15321
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015322 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015323 if (wp == NULL)
15324 rettv->vval.v_number = -1;
15325 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015326 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015327}
15328
15329/*
15330 * "wordcount()" function
15331 */
15332 static void
15333f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15334{
15335 if (rettv_dict_alloc(rettv) == FAIL)
15336 return;
15337 cursor_pos_info(rettv->vval.v_dict);
15338}
15339
15340/*
15341 * "writefile()" function
15342 */
15343 static void
15344f_writefile(typval_T *argvars, typval_T *rettv)
15345{
15346 int binary = FALSE;
15347 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015348#ifdef HAVE_FSYNC
15349 int do_fsync = p_fs;
15350#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015351 char_u *fname;
15352 FILE *fd;
15353 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015354 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015355 list_T *list = NULL;
15356 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015357
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015358 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015359 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015360 return;
15361
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015362 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015363 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015364 list = argvars[0].vval.v_list;
15365 if (list == NULL)
15366 return;
15367 for (li = list->lv_first; li != NULL; li = li->li_next)
15368 if (tv_get_string_chk(&li->li_tv) == NULL)
15369 return;
15370 }
15371 else if (argvars[0].v_type == VAR_BLOB)
15372 {
15373 blob = argvars[0].vval.v_blob;
15374 if (blob == NULL)
15375 return;
15376 }
15377 else
15378 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015379 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015380 return;
15381 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015382
15383 if (argvars[2].v_type != VAR_UNKNOWN)
15384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015385 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015386
15387 if (arg2 == NULL)
15388 return;
15389 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015390 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015391 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015392 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015393#ifdef HAVE_FSYNC
15394 if (vim_strchr(arg2, 's') != NULL)
15395 do_fsync = TRUE;
15396 else if (vim_strchr(arg2, 'S') != NULL)
15397 do_fsync = FALSE;
15398#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015399 }
15400
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015401 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015402 if (fname == NULL)
15403 return;
15404
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015405 /* Always open the file in binary mode, library functions have a mind of
15406 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015407 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15408 append ? APPENDBIN : WRITEBIN)) == NULL)
15409 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015410 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015411 ret = -1;
15412 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015413 else if (blob)
15414 {
15415 if (write_blob(fd, blob) == FAIL)
15416 ret = -1;
15417#ifdef HAVE_FSYNC
15418 else if (do_fsync)
15419 // Ignore the error, the user wouldn't know what to do about it.
15420 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015421 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015422#endif
15423 fclose(fd);
15424 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015425 else
15426 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015427 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015428 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015429#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015430 else if (do_fsync)
15431 /* Ignore the error, the user wouldn't know what to do about it.
15432 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015433 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015434#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015435 fclose(fd);
15436 }
15437
15438 rettv->vval.v_number = ret;
15439}
15440
15441/*
15442 * "xor(expr, expr)" function
15443 */
15444 static void
15445f_xor(typval_T *argvars, typval_T *rettv)
15446{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015447 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15448 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015449}
15450
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015451#endif /* FEAT_EVAL */