blob: a6703d18b50cca348974e378a5ab03f834d064a7 [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);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200495static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200496static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
497static void f_win_getid(typval_T *argvars, typval_T *rettv);
498static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
499static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
500static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100501static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200502static void f_winbufnr(typval_T *argvars, typval_T *rettv);
503static void f_wincol(typval_T *argvars, typval_T *rettv);
504static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200505static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200506static void f_winline(typval_T *argvars, typval_T *rettv);
507static void f_winnr(typval_T *argvars, typval_T *rettv);
508static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
509static void f_winrestview(typval_T *argvars, typval_T *rettv);
510static void f_winsaveview(typval_T *argvars, typval_T *rettv);
511static void f_winwidth(typval_T *argvars, typval_T *rettv);
512static void f_writefile(typval_T *argvars, typval_T *rettv);
513static void f_wordcount(typval_T *argvars, typval_T *rettv);
514static void f_xor(typval_T *argvars, typval_T *rettv);
515
516/*
517 * Array with names and number of arguments of all internal functions
518 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
519 */
520static struct fst
521{
522 char *f_name; /* function name */
523 char f_min_argc; /* minimal number of arguments */
524 char f_max_argc; /* maximal number of arguments */
525 void (*f_func)(typval_T *args, typval_T *rvar);
526 /* implementation of function */
527} functions[] =
528{
529#ifdef FEAT_FLOAT
530 {"abs", 1, 1, f_abs},
531 {"acos", 1, 1, f_acos}, /* WJMc */
532#endif
533 {"add", 2, 2, f_add},
534 {"and", 2, 2, f_and},
535 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200536 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200537 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200538 {"argidx", 0, 0, f_argidx},
539 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200540 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200541#ifdef FEAT_FLOAT
542 {"asin", 1, 1, f_asin}, /* WJMc */
543#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100544 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200545 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100546 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200547 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200548 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200549 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100550 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200551 {"assert_match", 2, 3, f_assert_match},
552 {"assert_notequal", 2, 3, f_assert_notequal},
553 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100554 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200555 {"assert_true", 1, 2, f_assert_true},
556#ifdef FEAT_FLOAT
557 {"atan", 1, 1, f_atan},
558 {"atan2", 2, 2, f_atan2},
559#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100560#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200561 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100562 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100563# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100564 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100565# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100566#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200567 {"browse", 4, 4, f_browse},
568 {"browsedir", 2, 2, f_browsedir},
569 {"bufexists", 1, 1, f_bufexists},
570 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
571 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
572 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
573 {"buflisted", 1, 1, f_buflisted},
574 {"bufloaded", 1, 1, f_bufloaded},
575 {"bufname", 1, 1, f_bufname},
576 {"bufnr", 1, 2, f_bufnr},
577 {"bufwinid", 1, 1, f_bufwinid},
578 {"bufwinnr", 1, 1, f_bufwinnr},
579 {"byte2line", 1, 1, f_byte2line},
580 {"byteidx", 2, 2, f_byteidx},
581 {"byteidxcomp", 2, 2, f_byteidxcomp},
582 {"call", 2, 3, f_call},
583#ifdef FEAT_FLOAT
584 {"ceil", 1, 1, f_ceil},
585#endif
586#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100587 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200588 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200589 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200590 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
591 {"ch_evalraw", 2, 3, f_ch_evalraw},
592 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
593 {"ch_getjob", 1, 1, f_ch_getjob},
594 {"ch_info", 1, 1, f_ch_info},
595 {"ch_log", 1, 2, f_ch_log},
596 {"ch_logfile", 1, 2, f_ch_logfile},
597 {"ch_open", 1, 2, f_ch_open},
598 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100599 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200600 {"ch_readraw", 1, 2, f_ch_readraw},
601 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
602 {"ch_sendraw", 2, 3, f_ch_sendraw},
603 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200604 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200605#endif
606 {"changenr", 0, 0, f_changenr},
607 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200608 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200609 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100610 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200611 {"col", 1, 1, f_col},
612#if defined(FEAT_INS_EXPAND)
613 {"complete", 2, 2, f_complete},
614 {"complete_add", 1, 1, f_complete_add},
615 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100616 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200617#endif
618 {"confirm", 1, 4, f_confirm},
619 {"copy", 1, 1, f_copy},
620#ifdef FEAT_FLOAT
621 {"cos", 1, 1, f_cos},
622 {"cosh", 1, 1, f_cosh},
623#endif
624 {"count", 2, 4, f_count},
625 {"cscope_connection",0,3, f_cscope_connection},
626 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100627#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200628 {"debugbreak", 1, 1, f_debugbreak},
629#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200630 {"deepcopy", 1, 2, f_deepcopy},
631 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200632 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"did_filetype", 0, 0, f_did_filetype},
634 {"diff_filler", 1, 1, f_diff_filler},
635 {"diff_hlID", 2, 2, f_diff_hlID},
636 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200637 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200638 {"escape", 2, 2, f_escape},
639 {"eval", 1, 1, f_eval},
640 {"eventhandler", 0, 0, f_eventhandler},
641 {"executable", 1, 1, f_executable},
642 {"execute", 1, 2, f_execute},
643 {"exepath", 1, 1, f_exepath},
644 {"exists", 1, 1, f_exists},
645#ifdef FEAT_FLOAT
646 {"exp", 1, 1, f_exp},
647#endif
648 {"expand", 1, 3, f_expand},
649 {"extend", 2, 3, f_extend},
650 {"feedkeys", 1, 2, f_feedkeys},
651 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
652 {"filereadable", 1, 1, f_filereadable},
653 {"filewritable", 1, 1, f_filewritable},
654 {"filter", 2, 2, f_filter},
655 {"finddir", 1, 3, f_finddir},
656 {"findfile", 1, 3, f_findfile},
657#ifdef FEAT_FLOAT
658 {"float2nr", 1, 1, f_float2nr},
659 {"floor", 1, 1, f_floor},
660 {"fmod", 2, 2, f_fmod},
661#endif
662 {"fnameescape", 1, 1, f_fnameescape},
663 {"fnamemodify", 2, 2, f_fnamemodify},
664 {"foldclosed", 1, 1, f_foldclosed},
665 {"foldclosedend", 1, 1, f_foldclosedend},
666 {"foldlevel", 1, 1, f_foldlevel},
667 {"foldtext", 0, 0, f_foldtext},
668 {"foldtextresult", 1, 1, f_foldtextresult},
669 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200670 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200671 {"function", 1, 3, f_function},
672 {"garbagecollect", 0, 1, f_garbagecollect},
673 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200674 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200675 {"getbufline", 2, 3, f_getbufline},
676 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100677 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200678 {"getchar", 0, 1, f_getchar},
679 {"getcharmod", 0, 0, f_getcharmod},
680 {"getcharsearch", 0, 0, f_getcharsearch},
681 {"getcmdline", 0, 0, f_getcmdline},
682 {"getcmdpos", 0, 0, f_getcmdpos},
683 {"getcmdtype", 0, 0, f_getcmdtype},
684 {"getcmdwintype", 0, 0, f_getcmdwintype},
685#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200686 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200687#endif
688 {"getcurpos", 0, 0, f_getcurpos},
689 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200690 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200691 {"getfontname", 0, 1, f_getfontname},
692 {"getfperm", 1, 1, f_getfperm},
693 {"getfsize", 1, 1, f_getfsize},
694 {"getftime", 1, 1, f_getftime},
695 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100696 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200697 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200698 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100699 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200700 {"getpid", 0, 0, f_getpid},
701 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200702 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200703 {"getreg", 0, 3, f_getreg},
704 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200705 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200706 {"gettabvar", 2, 3, f_gettabvar},
707 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100708 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200709 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100710 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200711 {"getwinposx", 0, 0, f_getwinposx},
712 {"getwinposy", 0, 0, f_getwinposy},
713 {"getwinvar", 2, 3, f_getwinvar},
714 {"glob", 1, 4, f_glob},
715 {"glob2regpat", 1, 1, f_glob2regpat},
716 {"globpath", 2, 5, f_globpath},
717 {"has", 1, 1, f_has},
718 {"has_key", 2, 2, f_has_key},
719 {"haslocaldir", 0, 2, f_haslocaldir},
720 {"hasmapto", 1, 3, f_hasmapto},
721 {"highlightID", 1, 1, f_hlID}, /* obsolete */
722 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
723 {"histadd", 2, 2, f_histadd},
724 {"histdel", 1, 2, f_histdel},
725 {"histget", 1, 2, f_histget},
726 {"histnr", 1, 1, f_histnr},
727 {"hlID", 1, 1, f_hlID},
728 {"hlexists", 1, 1, f_hlexists},
729 {"hostname", 0, 0, f_hostname},
730 {"iconv", 3, 3, f_iconv},
731 {"indent", 1, 1, f_indent},
732 {"index", 2, 4, f_index},
733 {"input", 1, 3, f_input},
734 {"inputdialog", 1, 3, f_inputdialog},
735 {"inputlist", 1, 1, f_inputlist},
736 {"inputrestore", 0, 0, f_inputrestore},
737 {"inputsave", 0, 0, f_inputsave},
738 {"inputsecret", 1, 2, f_inputsecret},
739 {"insert", 2, 3, f_insert},
740 {"invert", 1, 1, f_invert},
741 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200742#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
743 {"isinf", 1, 1, f_isinf},
744#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200745 {"islocked", 1, 1, f_islocked},
746#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
747 {"isnan", 1, 1, f_isnan},
748#endif
749 {"items", 1, 1, f_items},
750#ifdef FEAT_JOB_CHANNEL
751 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200752 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200753 {"job_setoptions", 2, 2, f_job_setoptions},
754 {"job_start", 1, 2, f_job_start},
755 {"job_status", 1, 1, f_job_status},
756 {"job_stop", 1, 2, f_job_stop},
757#endif
758 {"join", 1, 2, f_join},
759 {"js_decode", 1, 1, f_js_decode},
760 {"js_encode", 1, 1, f_js_encode},
761 {"json_decode", 1, 1, f_json_decode},
762 {"json_encode", 1, 1, f_json_encode},
763 {"keys", 1, 1, f_keys},
764 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
765 {"len", 1, 1, f_len},
766 {"libcall", 3, 3, f_libcall},
767 {"libcallnr", 3, 3, f_libcallnr},
768 {"line", 1, 1, f_line},
769 {"line2byte", 1, 1, f_line2byte},
770 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200771 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200772 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200773 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200774 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200775 {"localtime", 0, 0, f_localtime},
776#ifdef FEAT_FLOAT
777 {"log", 1, 1, f_log},
778 {"log10", 1, 1, f_log10},
779#endif
780#ifdef FEAT_LUA
781 {"luaeval", 1, 2, f_luaeval},
782#endif
783 {"map", 2, 2, f_map},
784 {"maparg", 1, 4, f_maparg},
785 {"mapcheck", 1, 3, f_mapcheck},
786 {"match", 2, 4, f_match},
787 {"matchadd", 2, 5, f_matchadd},
788 {"matchaddpos", 2, 5, f_matchaddpos},
789 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100790 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200791 {"matchend", 2, 4, f_matchend},
792 {"matchlist", 2, 4, f_matchlist},
793 {"matchstr", 2, 4, f_matchstr},
794 {"matchstrpos", 2, 4, f_matchstrpos},
795 {"max", 1, 1, f_max},
796 {"min", 1, 1, f_min},
797#ifdef vim_mkdir
798 {"mkdir", 1, 3, f_mkdir},
799#endif
800 {"mode", 0, 1, f_mode},
801#ifdef FEAT_MZSCHEME
802 {"mzeval", 1, 1, f_mzeval},
803#endif
804 {"nextnonblank", 1, 1, f_nextnonblank},
805 {"nr2char", 1, 2, f_nr2char},
806 {"or", 2, 2, f_or},
807 {"pathshorten", 1, 1, f_pathshorten},
808#ifdef FEAT_PERL
809 {"perleval", 1, 1, f_perleval},
810#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200811#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200812 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200813 {"popup_close", 1, 1, f_popup_close},
814 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200815 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarbc133542019-05-29 20:26:46 +0200816 {"popup_getposition", 1, 1, f_popup_getposition},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200817 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200818 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200819 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200820#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200821#ifdef FEAT_FLOAT
822 {"pow", 2, 2, f_pow},
823#endif
824 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100825 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200826#ifdef FEAT_JOB_CHANNEL
827 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200828 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200829 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
830#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100831#ifdef FEAT_TEXT_PROP
832 {"prop_add", 3, 3, f_prop_add},
833 {"prop_clear", 1, 3, f_prop_clear},
834 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100835 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100836 {"prop_type_add", 2, 2, f_prop_type_add},
837 {"prop_type_change", 2, 2, f_prop_type_change},
838 {"prop_type_delete", 1, 2, f_prop_type_delete},
839 {"prop_type_get", 1, 2, f_prop_type_get},
840 {"prop_type_list", 0, 1, f_prop_type_list},
841#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200842 {"pumvisible", 0, 0, f_pumvisible},
843#ifdef FEAT_PYTHON3
844 {"py3eval", 1, 1, f_py3eval},
845#endif
846#ifdef FEAT_PYTHON
847 {"pyeval", 1, 1, f_pyeval},
848#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100849#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
850 {"pyxeval", 1, 1, f_pyxeval},
851#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200853 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200854 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200855 {"reg_executing", 0, 0, f_reg_executing},
856 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200857 {"reltime", 0, 2, f_reltime},
858#ifdef FEAT_FLOAT
859 {"reltimefloat", 1, 1, f_reltimefloat},
860#endif
861 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100862 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200863 {"remote_foreground", 1, 1, f_remote_foreground},
864 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100865 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200866 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100867 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200868 {"remove", 2, 3, f_remove},
869 {"rename", 2, 2, f_rename},
870 {"repeat", 2, 2, f_repeat},
871 {"resolve", 1, 1, f_resolve},
872 {"reverse", 1, 1, f_reverse},
873#ifdef FEAT_FLOAT
874 {"round", 1, 1, f_round},
875#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100876#ifdef FEAT_RUBY
877 {"rubyeval", 1, 1, f_rubyeval},
878#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200879 {"screenattr", 2, 2, f_screenattr},
880 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100881 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882 {"screencol", 0, 0, f_screencol},
883 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100884 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200885 {"search", 1, 4, f_search},
886 {"searchdecl", 1, 3, f_searchdecl},
887 {"searchpair", 3, 7, f_searchpair},
888 {"searchpairpos", 3, 7, f_searchpairpos},
889 {"searchpos", 1, 4, f_searchpos},
890 {"server2client", 2, 2, f_server2client},
891 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200892 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200893 {"setbufvar", 3, 3, f_setbufvar},
894 {"setcharsearch", 1, 1, f_setcharsearch},
895 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200896 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200897 {"setfperm", 2, 2, f_setfperm},
898 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200899 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100900 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200901 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200902 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200903 {"setreg", 2, 3, f_setreg},
904 {"settabvar", 3, 3, f_settabvar},
905 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100906 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200907 {"setwinvar", 3, 3, f_setwinvar},
908#ifdef FEAT_CRYPT
909 {"sha256", 1, 1, f_sha256},
910#endif
911 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100912 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100913#ifdef FEAT_SIGNS
914 {"sign_define", 1, 2, f_sign_define},
915 {"sign_getdefined", 0, 1, f_sign_getdefined},
916 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100917 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100918 {"sign_place", 4, 5, f_sign_place},
919 {"sign_undefine", 0, 1, f_sign_undefine},
920 {"sign_unplace", 1, 2, f_sign_unplace},
921#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200922 {"simplify", 1, 1, f_simplify},
923#ifdef FEAT_FLOAT
924 {"sin", 1, 1, f_sin},
925 {"sinh", 1, 1, f_sinh},
926#endif
927 {"sort", 1, 3, f_sort},
928 {"soundfold", 1, 1, f_soundfold},
929 {"spellbadword", 0, 1, f_spellbadword},
930 {"spellsuggest", 1, 3, f_spellsuggest},
931 {"split", 1, 3, f_split},
932#ifdef FEAT_FLOAT
933 {"sqrt", 1, 1, f_sqrt},
934 {"str2float", 1, 1, f_str2float},
935#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200936 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200937 {"str2nr", 1, 2, f_str2nr},
938 {"strcharpart", 2, 3, f_strcharpart},
939 {"strchars", 1, 2, f_strchars},
940 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
941#ifdef HAVE_STRFTIME
942 {"strftime", 1, 2, f_strftime},
943#endif
944 {"strgetchar", 2, 2, f_strgetchar},
945 {"stridx", 2, 3, f_stridx},
946 {"string", 1, 1, f_string},
947 {"strlen", 1, 1, f_strlen},
948 {"strpart", 2, 3, f_strpart},
949 {"strridx", 2, 3, f_strridx},
950 {"strtrans", 1, 1, f_strtrans},
951 {"strwidth", 1, 1, f_strwidth},
952 {"submatch", 1, 2, f_submatch},
953 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200954 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200955 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200956 {"synID", 3, 3, f_synID},
957 {"synIDattr", 2, 3, f_synIDattr},
958 {"synIDtrans", 1, 1, f_synIDtrans},
959 {"synconcealed", 2, 2, f_synconcealed},
960 {"synstack", 2, 2, f_synstack},
961 {"system", 1, 2, f_system},
962 {"systemlist", 1, 2, f_systemlist},
963 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
964 {"tabpagenr", 0, 1, f_tabpagenr},
965 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
966 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100967 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200968#ifdef FEAT_FLOAT
969 {"tan", 1, 1, f_tan},
970 {"tanh", 1, 1, f_tanh},
971#endif
972 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200973#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100974 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
975 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100976 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200977 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200978# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
979 {"term_getansicolors", 1, 1, f_term_getansicolors},
980# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200981 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200982 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200983 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200984 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200985 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200986 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200987 {"term_getstatus", 1, 1, f_term_getstatus},
988 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200989 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200990 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200991 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200992 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200993# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
994 {"term_setansicolors", 2, 2, f_term_setansicolors},
995# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100996 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100997 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200998 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200999 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +02001000 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001001#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001002 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
1003 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +02001004 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001005 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +02001006 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +01001007 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001008 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001009#ifdef FEAT_JOB_CHANNEL
1010 {"test_null_channel", 0, 0, f_test_null_channel},
1011#endif
1012 {"test_null_dict", 0, 0, f_test_null_dict},
1013#ifdef FEAT_JOB_CHANNEL
1014 {"test_null_job", 0, 0, f_test_null_job},
1015#endif
1016 {"test_null_list", 0, 0, f_test_null_list},
1017 {"test_null_partial", 0, 0, f_test_null_partial},
1018 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001019 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001020 {"test_override", 2, 2, f_test_override},
1021 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001022#ifdef FEAT_GUI
1023 {"test_scrollbar", 3, 3, f_test_scrollbar},
1024#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001025#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001026 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001027#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001028 {"test_settime", 1, 1, f_test_settime},
1029#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001030 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001031 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001032 {"timer_start", 2, 3, f_timer_start},
1033 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001034 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001035#endif
1036 {"tolower", 1, 1, f_tolower},
1037 {"toupper", 1, 1, f_toupper},
1038 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001039 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001040#ifdef FEAT_FLOAT
1041 {"trunc", 1, 1, f_trunc},
1042#endif
1043 {"type", 1, 1, f_type},
1044 {"undofile", 1, 1, f_undofile},
1045 {"undotree", 0, 0, f_undotree},
1046 {"uniq", 1, 3, f_uniq},
1047 {"values", 1, 1, f_values},
1048 {"virtcol", 1, 1, f_virtcol},
1049 {"visualmode", 0, 1, f_visualmode},
1050 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001051 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001052 {"win_findbuf", 1, 1, f_win_findbuf},
1053 {"win_getid", 0, 2, f_win_getid},
1054 {"win_gotoid", 1, 1, f_win_gotoid},
1055 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1056 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001057 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001058 {"winbufnr", 1, 1, f_winbufnr},
1059 {"wincol", 0, 0, f_wincol},
1060 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001061 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001062 {"winline", 0, 0, f_winline},
1063 {"winnr", 0, 1, f_winnr},
1064 {"winrestcmd", 0, 0, f_winrestcmd},
1065 {"winrestview", 1, 1, f_winrestview},
1066 {"winsaveview", 0, 0, f_winsaveview},
1067 {"winwidth", 1, 1, f_winwidth},
1068 {"wordcount", 0, 0, f_wordcount},
1069 {"writefile", 2, 3, f_writefile},
1070 {"xor", 2, 2, f_xor},
1071};
1072
1073#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1074
1075/*
1076 * Function given to ExpandGeneric() to obtain the list of internal
1077 * or user defined function names.
1078 */
1079 char_u *
1080get_function_name(expand_T *xp, int idx)
1081{
1082 static int intidx = -1;
1083 char_u *name;
1084
1085 if (idx == 0)
1086 intidx = -1;
1087 if (intidx < 0)
1088 {
1089 name = get_user_func_name(xp, idx);
1090 if (name != NULL)
1091 return name;
1092 }
1093 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1094 {
1095 STRCPY(IObuff, functions[intidx].f_name);
1096 STRCAT(IObuff, "(");
1097 if (functions[intidx].f_max_argc == 0)
1098 STRCAT(IObuff, ")");
1099 return IObuff;
1100 }
1101
1102 return NULL;
1103}
1104
1105/*
1106 * Function given to ExpandGeneric() to obtain the list of internal or
1107 * user defined variable or function names.
1108 */
1109 char_u *
1110get_expr_name(expand_T *xp, int idx)
1111{
1112 static int intidx = -1;
1113 char_u *name;
1114
1115 if (idx == 0)
1116 intidx = -1;
1117 if (intidx < 0)
1118 {
1119 name = get_function_name(xp, idx);
1120 if (name != NULL)
1121 return name;
1122 }
1123 return get_user_var_name(xp, ++intidx);
1124}
1125
1126#endif /* FEAT_CMDL_COMPL */
1127
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001128/*
1129 * Find internal function in table above.
1130 * Return index, or -1 if not found
1131 */
1132 int
1133find_internal_func(
1134 char_u *name) /* name of the function */
1135{
1136 int first = 0;
1137 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1138 int cmp;
1139 int x;
1140
1141 /*
1142 * Find the function name in the table. Binary search.
1143 */
1144 while (first <= last)
1145 {
1146 x = first + ((unsigned)(last - first) >> 1);
1147 cmp = STRCMP(name, functions[x].f_name);
1148 if (cmp < 0)
1149 last = x - 1;
1150 else if (cmp > 0)
1151 first = x + 1;
1152 else
1153 return x;
1154 }
1155 return -1;
1156}
1157
1158 int
1159call_internal_func(
1160 char_u *name,
1161 int argcount,
1162 typval_T *argvars,
1163 typval_T *rettv)
1164{
1165 int i;
1166
1167 i = find_internal_func(name);
1168 if (i < 0)
1169 return ERROR_UNKNOWN;
1170 if (argcount < functions[i].f_min_argc)
1171 return ERROR_TOOFEW;
1172 if (argcount > functions[i].f_max_argc)
1173 return ERROR_TOOMANY;
1174 argvars[argcount].v_type = VAR_UNKNOWN;
1175 functions[i].f_func(argvars, rettv);
1176 return ERROR_NONE;
1177}
1178
1179/*
1180 * Return TRUE for a non-zero Number and a non-empty String.
1181 */
1182 static int
1183non_zero_arg(typval_T *argvars)
1184{
1185 return ((argvars[0].v_type == VAR_NUMBER
1186 && argvars[0].vval.v_number != 0)
1187 || (argvars[0].v_type == VAR_SPECIAL
1188 && argvars[0].vval.v_number == VVAL_TRUE)
1189 || (argvars[0].v_type == VAR_STRING
1190 && argvars[0].vval.v_string != NULL
1191 && *argvars[0].vval.v_string != NUL));
1192}
1193
1194/*
1195 * Get the lnum from the first argument.
1196 * Also accepts ".", "$", etc., but that only works for the current buffer.
1197 * Returns -1 on error.
1198 */
1199 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001200tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001201{
1202 typval_T rettv;
1203 linenr_T lnum;
1204
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001205 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001206 if (lnum == 0) /* no valid number, try using line() */
1207 {
1208 rettv.v_type = VAR_NUMBER;
1209 f_line(argvars, &rettv);
1210 lnum = (linenr_T)rettv.vval.v_number;
1211 clear_tv(&rettv);
1212 }
1213 return lnum;
1214}
1215
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001216/*
1217 * Get the lnum from the first argument.
1218 * Also accepts "$", then "buf" is used.
1219 * Returns 0 on error.
1220 */
1221 static linenr_T
1222tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1223{
1224 if (argvars[0].v_type == VAR_STRING
1225 && argvars[0].vval.v_string != NULL
1226 && argvars[0].vval.v_string[0] == '$'
1227 && buf != NULL)
1228 return buf->b_ml.ml_line_count;
1229 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1230}
1231
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001232#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001233/*
1234 * Get the float value of "argvars[0]" into "f".
1235 * Returns FAIL when the argument is not a Number or Float.
1236 */
1237 static int
1238get_float_arg(typval_T *argvars, float_T *f)
1239{
1240 if (argvars[0].v_type == VAR_FLOAT)
1241 {
1242 *f = argvars[0].vval.v_float;
1243 return OK;
1244 }
1245 if (argvars[0].v_type == VAR_NUMBER)
1246 {
1247 *f = (float_T)argvars[0].vval.v_number;
1248 return OK;
1249 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001250 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001251 return FAIL;
1252}
1253
1254/*
1255 * "abs(expr)" function
1256 */
1257 static void
1258f_abs(typval_T *argvars, typval_T *rettv)
1259{
1260 if (argvars[0].v_type == VAR_FLOAT)
1261 {
1262 rettv->v_type = VAR_FLOAT;
1263 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1264 }
1265 else
1266 {
1267 varnumber_T n;
1268 int error = FALSE;
1269
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001270 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001271 if (error)
1272 rettv->vval.v_number = -1;
1273 else if (n > 0)
1274 rettv->vval.v_number = n;
1275 else
1276 rettv->vval.v_number = -n;
1277 }
1278}
1279
1280/*
1281 * "acos()" function
1282 */
1283 static void
1284f_acos(typval_T *argvars, typval_T *rettv)
1285{
1286 float_T f = 0.0;
1287
1288 rettv->v_type = VAR_FLOAT;
1289 if (get_float_arg(argvars, &f) == OK)
1290 rettv->vval.v_float = acos(f);
1291 else
1292 rettv->vval.v_float = 0.0;
1293}
1294#endif
1295
1296/*
1297 * "add(list, item)" function
1298 */
1299 static void
1300f_add(typval_T *argvars, typval_T *rettv)
1301{
1302 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001303 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001304
1305 rettv->vval.v_number = 1; /* Default: Failed */
1306 if (argvars[0].v_type == VAR_LIST)
1307 {
1308 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001309 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001310 (char_u *)N_("add() argument"), TRUE)
1311 && list_append_tv(l, &argvars[1]) == OK)
1312 copy_tv(&argvars[0], rettv);
1313 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001314 else if (argvars[0].v_type == VAR_BLOB)
1315 {
1316 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001317 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001318 (char_u *)N_("add() argument"), TRUE))
1319 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001320 int error = FALSE;
1321 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1322
1323 if (!error)
1324 {
1325 ga_append(&b->bv_ga, (int)n);
1326 copy_tv(&argvars[0], rettv);
1327 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001328 }
1329 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001330 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001331 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001332}
1333
1334/*
1335 * "and(expr, expr)" function
1336 */
1337 static void
1338f_and(typval_T *argvars, typval_T *rettv)
1339{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001340 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1341 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001342}
1343
1344/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001345 * If there is a window for "curbuf", make it the current window.
1346 */
1347 static void
1348find_win_for_curbuf(void)
1349{
1350 wininfo_T *wip;
1351
1352 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1353 {
1354 if (wip->wi_win != NULL)
1355 {
1356 curwin = wip->wi_win;
1357 break;
1358 }
1359 }
1360}
1361
1362/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001363 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001364 */
1365 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001366set_buffer_lines(
1367 buf_T *buf,
1368 linenr_T lnum_arg,
1369 int append,
1370 typval_T *lines,
1371 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372{
Bram Moolenaarca851592018-06-06 21:04:07 +02001373 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1374 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001375 list_T *l = NULL;
1376 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001377 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001378 linenr_T append_lnum;
1379 buf_T *curbuf_save = NULL;
1380 win_T *curwin_save = NULL;
1381 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001382
Bram Moolenaarca851592018-06-06 21:04:07 +02001383 /* When using the current buffer ml_mfp will be set if needed. Useful when
1384 * setline() is used on startup. For other buffers the buffer must be
1385 * loaded. */
1386 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001387 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001388 rettv->vval.v_number = 1; /* FAIL */
1389 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001390 }
1391
Bram Moolenaarca851592018-06-06 21:04:07 +02001392 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001393 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001394 curbuf_save = curbuf;
1395 curwin_save = curwin;
1396 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001397 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001398 }
1399
1400 if (append)
1401 // appendbufline() uses the line number below which we insert
1402 append_lnum = lnum - 1;
1403 else
1404 // setbufline() uses the line number above which we insert, we only
1405 // append if it's below the last line
1406 append_lnum = curbuf->b_ml.ml_line_count;
1407
1408 if (lines->v_type == VAR_LIST)
1409 {
1410 l = lines->vval.v_list;
1411 li = l->lv_first;
1412 }
1413 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001414 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001415
1416 /* default result is zero == OK */
1417 for (;;)
1418 {
1419 if (l != NULL)
1420 {
1421 /* list argument, get next string */
1422 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001423 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001424 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001425 li = li->li_next;
1426 }
1427
Bram Moolenaarca851592018-06-06 21:04:07 +02001428 rettv->vval.v_number = 1; /* FAIL */
1429 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1430 break;
1431
1432 /* When coming here from Insert mode, sync undo, so that this can be
1433 * undone separately from what was previously inserted. */
1434 if (u_sync_once == 2)
1435 {
1436 u_sync_once = 1; /* notify that u_sync() was called */
1437 u_sync(TRUE);
1438 }
1439
1440 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1441 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001442 // Existing line, replace it.
1443 // Removes any existing text properties.
1444 if (u_savesub(lnum) == OK && ml_replace_len(
1445 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001446 {
1447 changed_bytes(lnum, 0);
1448 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1449 check_cursor_col();
1450 rettv->vval.v_number = 0; /* OK */
1451 }
1452 }
1453 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1454 {
1455 /* append the line */
1456 ++added;
1457 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1458 rettv->vval.v_number = 0; /* OK */
1459 }
1460
1461 if (l == NULL) /* only one string argument */
1462 break;
1463 ++lnum;
1464 }
1465
1466 if (added > 0)
1467 {
1468 win_T *wp;
1469 tabpage_T *tp;
1470
1471 appended_lines_mark(append_lnum, added);
1472 FOR_ALL_TAB_WINDOWS(tp, wp)
1473 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1474 wp->w_cursor.lnum += added;
1475 check_cursor_col();
1476
Bram Moolenaarf2732452018-06-03 14:47:35 +02001477#ifdef FEAT_JOB_CHANNEL
1478 if (bt_prompt(curbuf) && (State & INSERT))
1479 // show the line with the prompt
1480 update_topline();
1481#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001482 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001483
1484 if (!is_curbuf)
1485 {
1486 curbuf = curbuf_save;
1487 curwin = curwin_save;
1488 }
1489}
1490
1491/*
1492 * "append(lnum, string/list)" function
1493 */
1494 static void
1495f_append(typval_T *argvars, typval_T *rettv)
1496{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001497 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001498
1499 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1500}
1501
1502/*
1503 * "appendbufline(buf, lnum, string/list)" function
1504 */
1505 static void
1506f_appendbufline(typval_T *argvars, typval_T *rettv)
1507{
1508 linenr_T lnum;
1509 buf_T *buf;
1510
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001511 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001512 if (buf == NULL)
1513 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001514 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001515 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001516 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001517 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1518 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001519}
1520
1521/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001522 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001523 */
1524 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001525f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001526{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001527 win_T *wp;
1528
1529 if (argvars[0].v_type == VAR_UNKNOWN)
1530 // use the current window
1531 rettv->vval.v_number = ARGCOUNT;
1532 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001533 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001534 // use the global argument list
1535 rettv->vval.v_number = GARGCOUNT;
1536 else
1537 {
1538 // use the argument list of the specified window
1539 wp = find_win_by_nr_or_id(&argvars[0]);
1540 if (wp != NULL)
1541 rettv->vval.v_number = WARGCOUNT(wp);
1542 else
1543 rettv->vval.v_number = -1;
1544 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001545}
1546
1547/*
1548 * "argidx()" function
1549 */
1550 static void
1551f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1552{
1553 rettv->vval.v_number = curwin->w_arg_idx;
1554}
1555
1556/*
1557 * "arglistid()" function
1558 */
1559 static void
1560f_arglistid(typval_T *argvars, typval_T *rettv)
1561{
1562 win_T *wp;
1563
1564 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001565 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001566 if (wp != NULL)
1567 rettv->vval.v_number = wp->w_alist->id;
1568}
1569
1570/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001571 * Get the argument list for a given window
1572 */
1573 static void
1574get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1575{
1576 int idx;
1577
1578 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1579 for (idx = 0; idx < argcount; ++idx)
1580 list_append_string(rettv->vval.v_list,
1581 alist_name(&arglist[idx]), -1);
1582}
1583
1584/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001585 * "argv(nr)" function
1586 */
1587 static void
1588f_argv(typval_T *argvars, typval_T *rettv)
1589{
1590 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001591 aentry_T *arglist = NULL;
1592 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001593
1594 if (argvars[0].v_type != VAR_UNKNOWN)
1595 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001596 if (argvars[1].v_type == VAR_UNKNOWN)
1597 {
1598 arglist = ARGLIST;
1599 argcount = ARGCOUNT;
1600 }
1601 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001602 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001603 {
1604 arglist = GARGLIST;
1605 argcount = GARGCOUNT;
1606 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001608 {
1609 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1610
1611 if (wp != NULL)
1612 {
1613 /* Use the argument list of the specified window */
1614 arglist = WARGLIST(wp);
1615 argcount = WARGCOUNT(wp);
1616 }
1617 }
1618
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001619 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001620 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001621 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001622 if (arglist != NULL && idx >= 0 && idx < argcount)
1623 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1624 else if (idx == -1)
1625 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001626 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001627 else
1628 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001629}
1630
1631/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001632 * "assert_beeps(cmd [, error])" function
1633 */
1634 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001635f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001636{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001637 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001638}
1639
1640/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641 * "assert_equal(expected, actual[, msg])" function
1642 */
1643 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001644f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001645{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001646 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001647}
1648
1649/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001650 * "assert_equalfile(fname-one, fname-two)" function
1651 */
1652 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001653f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001654{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001655 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001656}
1657
1658/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659 * "assert_notequal(expected, actual[, msg])" function
1660 */
1661 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001662f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001663{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001664 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001665}
1666
1667/*
1668 * "assert_exception(string[, msg])" function
1669 */
1670 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001671f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001672{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001673 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001674}
1675
1676/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001677 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001678 */
1679 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001680f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001681{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001682 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001683}
1684
1685/*
1686 * "assert_false(actual[, msg])" function
1687 */
1688 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001689f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001690{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001691 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001692}
1693
1694/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001695 * "assert_inrange(lower, upper[, msg])" function
1696 */
1697 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001698f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001699{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001700 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001701}
1702
1703/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001704 * "assert_match(pattern, actual[, msg])" function
1705 */
1706 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001707f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001708{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001709 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001710}
1711
1712/*
1713 * "assert_notmatch(pattern, actual[, msg])" function
1714 */
1715 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001716f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001717{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001718 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001719}
1720
1721/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001722 * "assert_report(msg)" function
1723 */
1724 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001725f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001726{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001727 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001728}
1729
1730/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001731 * "assert_true(actual[, msg])" function
1732 */
1733 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001734f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001735{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001736 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001737}
1738
1739#ifdef FEAT_FLOAT
1740/*
1741 * "asin()" function
1742 */
1743 static void
1744f_asin(typval_T *argvars, typval_T *rettv)
1745{
1746 float_T f = 0.0;
1747
1748 rettv->v_type = VAR_FLOAT;
1749 if (get_float_arg(argvars, &f) == OK)
1750 rettv->vval.v_float = asin(f);
1751 else
1752 rettv->vval.v_float = 0.0;
1753}
1754
1755/*
1756 * "atan()" function
1757 */
1758 static void
1759f_atan(typval_T *argvars, typval_T *rettv)
1760{
1761 float_T f = 0.0;
1762
1763 rettv->v_type = VAR_FLOAT;
1764 if (get_float_arg(argvars, &f) == OK)
1765 rettv->vval.v_float = atan(f);
1766 else
1767 rettv->vval.v_float = 0.0;
1768}
1769
1770/*
1771 * "atan2()" function
1772 */
1773 static void
1774f_atan2(typval_T *argvars, typval_T *rettv)
1775{
1776 float_T fx = 0.0, fy = 0.0;
1777
1778 rettv->v_type = VAR_FLOAT;
1779 if (get_float_arg(argvars, &fx) == OK
1780 && get_float_arg(&argvars[1], &fy) == OK)
1781 rettv->vval.v_float = atan2(fx, fy);
1782 else
1783 rettv->vval.v_float = 0.0;
1784}
1785#endif
1786
1787/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001788 * "balloon_show()" function
1789 */
1790#ifdef FEAT_BEVAL
1791 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001792f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1793{
1794 rettv->v_type = VAR_STRING;
1795 if (balloonEval != NULL)
1796 {
1797 if (balloonEval->msg == NULL)
1798 rettv->vval.v_string = NULL;
1799 else
1800 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1801 }
1802}
1803
1804 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001805f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1806{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001807 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001808 {
1809 if (argvars[0].v_type == VAR_LIST
1810# ifdef FEAT_GUI
1811 && !gui.in_use
1812# endif
1813 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001814 {
1815 list_T *l = argvars[0].vval.v_list;
1816
1817 // empty list removes the balloon
1818 post_balloon(balloonEval, NULL,
1819 l == NULL || l->lv_len == 0 ? NULL : l);
1820 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001821 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001822 {
1823 char_u *mesg = tv_get_string_chk(&argvars[0]);
1824
1825 if (mesg != NULL)
1826 // empty string removes the balloon
1827 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1828 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001829 }
1830}
1831
Bram Moolenaar669a8282017-11-19 20:13:05 +01001832# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001833 static void
1834f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1835{
1836 if (rettv_list_alloc(rettv) == OK)
1837 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001838 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001839
1840 if (msg != NULL)
1841 {
1842 pumitem_T *array;
1843 int size = split_message(msg, &array);
1844 int i;
1845
1846 /* Skip the first and last item, they are always empty. */
1847 for (i = 1; i < size - 1; ++i)
1848 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001849 while (size > 0)
1850 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001851 vim_free(array);
1852 }
1853 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001854}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001855# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001856#endif
1857
1858/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001859 * "browse(save, title, initdir, default)" function
1860 */
1861 static void
1862f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1863{
1864#ifdef FEAT_BROWSE
1865 int save;
1866 char_u *title;
1867 char_u *initdir;
1868 char_u *defname;
1869 char_u buf[NUMBUFLEN];
1870 char_u buf2[NUMBUFLEN];
1871 int error = FALSE;
1872
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001873 save = (int)tv_get_number_chk(&argvars[0], &error);
1874 title = tv_get_string_chk(&argvars[1]);
1875 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1876 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001877
1878 if (error || title == NULL || initdir == NULL || defname == NULL)
1879 rettv->vval.v_string = NULL;
1880 else
1881 rettv->vval.v_string =
1882 do_browse(save ? BROWSE_SAVE : 0,
1883 title, defname, NULL, initdir, NULL, curbuf);
1884#else
1885 rettv->vval.v_string = NULL;
1886#endif
1887 rettv->v_type = VAR_STRING;
1888}
1889
1890/*
1891 * "browsedir(title, initdir)" function
1892 */
1893 static void
1894f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1895{
1896#ifdef FEAT_BROWSE
1897 char_u *title;
1898 char_u *initdir;
1899 char_u buf[NUMBUFLEN];
1900
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001901 title = tv_get_string_chk(&argvars[0]);
1902 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001903
1904 if (title == NULL || initdir == NULL)
1905 rettv->vval.v_string = NULL;
1906 else
1907 rettv->vval.v_string = do_browse(BROWSE_DIR,
1908 title, NULL, NULL, initdir, NULL, curbuf);
1909#else
1910 rettv->vval.v_string = NULL;
1911#endif
1912 rettv->v_type = VAR_STRING;
1913}
1914
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915/*
1916 * Find a buffer by number or exact name.
1917 */
1918 static buf_T *
1919find_buffer(typval_T *avar)
1920{
1921 buf_T *buf = NULL;
1922
1923 if (avar->v_type == VAR_NUMBER)
1924 buf = buflist_findnr((int)avar->vval.v_number);
1925 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1926 {
1927 buf = buflist_findname_exp(avar->vval.v_string);
1928 if (buf == NULL)
1929 {
1930 /* No full path name match, try a match with a URL or a "nofile"
1931 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001932 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001933 if (buf->b_fname != NULL
1934 && (path_with_url(buf->b_fname)
1935#ifdef FEAT_QUICKFIX
1936 || bt_nofile(buf)
1937#endif
1938 )
1939 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1940 break;
1941 }
1942 }
1943 return buf;
1944}
1945
1946/*
1947 * "bufexists(expr)" function
1948 */
1949 static void
1950f_bufexists(typval_T *argvars, typval_T *rettv)
1951{
1952 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1953}
1954
1955/*
1956 * "buflisted(expr)" function
1957 */
1958 static void
1959f_buflisted(typval_T *argvars, typval_T *rettv)
1960{
1961 buf_T *buf;
1962
1963 buf = find_buffer(&argvars[0]);
1964 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1965}
1966
1967/*
1968 * "bufloaded(expr)" function
1969 */
1970 static void
1971f_bufloaded(typval_T *argvars, typval_T *rettv)
1972{
1973 buf_T *buf;
1974
1975 buf = find_buffer(&argvars[0]);
1976 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1977}
1978
1979 buf_T *
1980buflist_find_by_name(char_u *name, int curtab_only)
1981{
1982 int save_magic;
1983 char_u *save_cpo;
1984 buf_T *buf;
1985
1986 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1987 save_magic = p_magic;
1988 p_magic = TRUE;
1989 save_cpo = p_cpo;
1990 p_cpo = (char_u *)"";
1991
1992 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1993 TRUE, FALSE, curtab_only));
1994
1995 p_magic = save_magic;
1996 p_cpo = save_cpo;
1997 return buf;
1998}
1999
2000/*
2001 * Get buffer by number or pattern.
2002 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002003 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002004tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002005{
2006 char_u *name = tv->vval.v_string;
2007 buf_T *buf;
2008
2009 if (tv->v_type == VAR_NUMBER)
2010 return buflist_findnr((int)tv->vval.v_number);
2011 if (tv->v_type != VAR_STRING)
2012 return NULL;
2013 if (name == NULL || *name == NUL)
2014 return curbuf;
2015 if (name[0] == '$' && name[1] == NUL)
2016 return lastbuf;
2017
2018 buf = buflist_find_by_name(name, curtab_only);
2019
2020 /* If not found, try expanding the name, like done for bufexists(). */
2021 if (buf == NULL)
2022 buf = find_buffer(tv);
2023
2024 return buf;
2025}
2026
2027/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002028 * Get the buffer from "arg" and give an error and return NULL if it is not
2029 * valid.
2030 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002031 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002032get_buf_arg(typval_T *arg)
2033{
2034 buf_T *buf;
2035
2036 ++emsg_off;
2037 buf = tv_get_buf(arg, FALSE);
2038 --emsg_off;
2039 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002040 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002041 return buf;
2042}
2043
2044/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002045 * "bufname(expr)" function
2046 */
2047 static void
2048f_bufname(typval_T *argvars, typval_T *rettv)
2049{
2050 buf_T *buf;
2051
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002052 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002053 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002054 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002055 rettv->v_type = VAR_STRING;
2056 if (buf != NULL && buf->b_fname != NULL)
2057 rettv->vval.v_string = vim_strsave(buf->b_fname);
2058 else
2059 rettv->vval.v_string = NULL;
2060 --emsg_off;
2061}
2062
2063/*
2064 * "bufnr(expr)" function
2065 */
2066 static void
2067f_bufnr(typval_T *argvars, typval_T *rettv)
2068{
2069 buf_T *buf;
2070 int error = FALSE;
2071 char_u *name;
2072
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002073 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002074 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002075 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002076 --emsg_off;
2077
2078 /* If the buffer isn't found and the second argument is not zero create a
2079 * new buffer. */
2080 if (buf == NULL
2081 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002082 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002083 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002084 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002085 && !error)
2086 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2087
2088 if (buf != NULL)
2089 rettv->vval.v_number = buf->b_fnum;
2090 else
2091 rettv->vval.v_number = -1;
2092}
2093
2094 static void
2095buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2096{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002097 win_T *wp;
2098 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002099 buf_T *buf;
2100
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002101 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002102 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002103 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002104 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002105 {
2106 ++winnr;
2107 if (wp->w_buffer == buf)
2108 break;
2109 }
2110 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002111 --emsg_off;
2112}
2113
2114/*
2115 * "bufwinid(nr)" function
2116 */
2117 static void
2118f_bufwinid(typval_T *argvars, typval_T *rettv)
2119{
2120 buf_win_common(argvars, rettv, FALSE);
2121}
2122
2123/*
2124 * "bufwinnr(nr)" function
2125 */
2126 static void
2127f_bufwinnr(typval_T *argvars, typval_T *rettv)
2128{
2129 buf_win_common(argvars, rettv, TRUE);
2130}
2131
2132/*
2133 * "byte2line(byte)" function
2134 */
2135 static void
2136f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2137{
2138#ifndef FEAT_BYTEOFF
2139 rettv->vval.v_number = -1;
2140#else
2141 long boff = 0;
2142
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002143 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002144 if (boff < 0)
2145 rettv->vval.v_number = -1;
2146 else
2147 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2148 (linenr_T)0, &boff);
2149#endif
2150}
2151
2152 static void
2153byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2154{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002155 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002156 char_u *str;
2157 varnumber_T idx;
2158
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002159 str = tv_get_string_chk(&argvars[0]);
2160 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002161 rettv->vval.v_number = -1;
2162 if (str == NULL || idx < 0)
2163 return;
2164
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002165 t = str;
2166 for ( ; idx > 0; idx--)
2167 {
2168 if (*t == NUL) /* EOL reached */
2169 return;
2170 if (enc_utf8 && comp)
2171 t += utf_ptr2len(t);
2172 else
2173 t += (*mb_ptr2len)(t);
2174 }
2175 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002176}
2177
2178/*
2179 * "byteidx()" function
2180 */
2181 static void
2182f_byteidx(typval_T *argvars, typval_T *rettv)
2183{
2184 byteidx(argvars, rettv, FALSE);
2185}
2186
2187/*
2188 * "byteidxcomp()" function
2189 */
2190 static void
2191f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2192{
2193 byteidx(argvars, rettv, TRUE);
2194}
2195
2196/*
2197 * "call(func, arglist [, dict])" function
2198 */
2199 static void
2200f_call(typval_T *argvars, typval_T *rettv)
2201{
2202 char_u *func;
2203 partial_T *partial = NULL;
2204 dict_T *selfdict = NULL;
2205
2206 if (argvars[1].v_type != VAR_LIST)
2207 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002208 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002209 return;
2210 }
2211 if (argvars[1].vval.v_list == NULL)
2212 return;
2213
2214 if (argvars[0].v_type == VAR_FUNC)
2215 func = argvars[0].vval.v_string;
2216 else if (argvars[0].v_type == VAR_PARTIAL)
2217 {
2218 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002219 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002220 }
2221 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002222 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002223 if (*func == NUL)
2224 return; /* type error or empty name */
2225
2226 if (argvars[2].v_type != VAR_UNKNOWN)
2227 {
2228 if (argvars[2].v_type != VAR_DICT)
2229 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002230 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002231 return;
2232 }
2233 selfdict = argvars[2].vval.v_dict;
2234 }
2235
2236 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2237}
2238
2239#ifdef FEAT_FLOAT
2240/*
2241 * "ceil({float})" function
2242 */
2243 static void
2244f_ceil(typval_T *argvars, typval_T *rettv)
2245{
2246 float_T f = 0.0;
2247
2248 rettv->v_type = VAR_FLOAT;
2249 if (get_float_arg(argvars, &f) == OK)
2250 rettv->vval.v_float = ceil(f);
2251 else
2252 rettv->vval.v_float = 0.0;
2253}
2254#endif
2255
2256#ifdef FEAT_JOB_CHANNEL
2257/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002258 * "ch_canread()" function
2259 */
2260 static void
2261f_ch_canread(typval_T *argvars, typval_T *rettv)
2262{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002263 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002264
2265 rettv->vval.v_number = 0;
2266 if (channel != NULL)
2267 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2268 || channel_has_readahead(channel, PART_OUT)
2269 || channel_has_readahead(channel, PART_ERR);
2270}
2271
2272/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002273 * "ch_close()" function
2274 */
2275 static void
2276f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2277{
2278 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2279
2280 if (channel != NULL)
2281 {
2282 channel_close(channel, FALSE);
2283 channel_clear(channel);
2284 }
2285}
2286
2287/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002288 * "ch_close()" function
2289 */
2290 static void
2291f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2292{
2293 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2294
2295 if (channel != NULL)
2296 channel_close_in(channel);
2297}
2298
2299/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002300 * "ch_getbufnr()" function
2301 */
2302 static void
2303f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2304{
2305 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2306
2307 rettv->vval.v_number = -1;
2308 if (channel != NULL)
2309 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002310 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002311 int part;
2312
2313 if (STRCMP(what, "err") == 0)
2314 part = PART_ERR;
2315 else if (STRCMP(what, "out") == 0)
2316 part = PART_OUT;
2317 else if (STRCMP(what, "in") == 0)
2318 part = PART_IN;
2319 else
2320 part = PART_SOCK;
2321 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2322 rettv->vval.v_number =
2323 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2324 }
2325}
2326
2327/*
2328 * "ch_getjob()" function
2329 */
2330 static void
2331f_ch_getjob(typval_T *argvars, typval_T *rettv)
2332{
2333 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2334
2335 if (channel != NULL)
2336 {
2337 rettv->v_type = VAR_JOB;
2338 rettv->vval.v_job = channel->ch_job;
2339 if (channel->ch_job != NULL)
2340 ++channel->ch_job->jv_refcount;
2341 }
2342}
2343
2344/*
2345 * "ch_info()" function
2346 */
2347 static void
2348f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2349{
2350 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2351
2352 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2353 channel_info(channel, rettv->vval.v_dict);
2354}
2355
2356/*
2357 * "ch_log()" function
2358 */
2359 static void
2360f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2361{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002362 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002363 channel_T *channel = NULL;
2364
2365 if (argvars[1].v_type != VAR_UNKNOWN)
2366 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2367
Bram Moolenaard5359b22018-04-05 22:44:39 +02002368 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002369}
2370
2371/*
2372 * "ch_logfile()" function
2373 */
2374 static void
2375f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2376{
2377 char_u *fname;
2378 char_u *opt = (char_u *)"";
2379 char_u buf[NUMBUFLEN];
2380
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002381 /* Don't open a file in restricted mode. */
2382 if (check_restricted() || check_secure())
2383 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002384 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002385 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002386 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002387 ch_logfile(fname, opt);
2388}
2389
2390/*
2391 * "ch_open()" function
2392 */
2393 static void
2394f_ch_open(typval_T *argvars, typval_T *rettv)
2395{
2396 rettv->v_type = VAR_CHANNEL;
2397 if (check_restricted() || check_secure())
2398 return;
2399 rettv->vval.v_channel = channel_open_func(argvars);
2400}
2401
2402/*
2403 * "ch_read()" function
2404 */
2405 static void
2406f_ch_read(typval_T *argvars, typval_T *rettv)
2407{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002408 common_channel_read(argvars, rettv, FALSE, FALSE);
2409}
2410
2411/*
2412 * "ch_readblob()" function
2413 */
2414 static void
2415f_ch_readblob(typval_T *argvars, typval_T *rettv)
2416{
2417 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002418}
2419
2420/*
2421 * "ch_readraw()" function
2422 */
2423 static void
2424f_ch_readraw(typval_T *argvars, typval_T *rettv)
2425{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002426 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002427}
2428
2429/*
2430 * "ch_evalexpr()" function
2431 */
2432 static void
2433f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2434{
2435 ch_expr_common(argvars, rettv, TRUE);
2436}
2437
2438/*
2439 * "ch_sendexpr()" function
2440 */
2441 static void
2442f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2443{
2444 ch_expr_common(argvars, rettv, FALSE);
2445}
2446
2447/*
2448 * "ch_evalraw()" function
2449 */
2450 static void
2451f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2452{
2453 ch_raw_common(argvars, rettv, TRUE);
2454}
2455
2456/*
2457 * "ch_sendraw()" function
2458 */
2459 static void
2460f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2461{
2462 ch_raw_common(argvars, rettv, FALSE);
2463}
2464
2465/*
2466 * "ch_setoptions()" function
2467 */
2468 static void
2469f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2470{
2471 channel_T *channel;
2472 jobopt_T opt;
2473
2474 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2475 if (channel == NULL)
2476 return;
2477 clear_job_options(&opt);
2478 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002479 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002480 channel_set_options(channel, &opt);
2481 free_job_options(&opt);
2482}
2483
2484/*
2485 * "ch_status()" function
2486 */
2487 static void
2488f_ch_status(typval_T *argvars, typval_T *rettv)
2489{
2490 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002491 jobopt_T opt;
2492 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002493
2494 /* return an empty string by default */
2495 rettv->v_type = VAR_STRING;
2496 rettv->vval.v_string = NULL;
2497
2498 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002499
2500 if (argvars[1].v_type != VAR_UNKNOWN)
2501 {
2502 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002503 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002504 && (opt.jo_set & JO_PART))
2505 part = opt.jo_part;
2506 }
2507
2508 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002509}
2510#endif
2511
2512/*
2513 * "changenr()" function
2514 */
2515 static void
2516f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2517{
2518 rettv->vval.v_number = curbuf->b_u_seq_cur;
2519}
2520
2521/*
2522 * "char2nr(string)" function
2523 */
2524 static void
2525f_char2nr(typval_T *argvars, typval_T *rettv)
2526{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002527 if (has_mbyte)
2528 {
2529 int utf8 = 0;
2530
2531 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002532 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002533
2534 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002535 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002536 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002537 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002538 }
2539 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002540 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541}
2542
2543/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002544 * "chdir(dir)" function
2545 */
2546 static void
2547f_chdir(typval_T *argvars, typval_T *rettv)
2548{
2549 char_u *cwd;
2550 cdscope_T scope = CDSCOPE_GLOBAL;
2551
2552 rettv->v_type = VAR_STRING;
2553 rettv->vval.v_string = NULL;
2554
2555 if (argvars[0].v_type != VAR_STRING)
2556 return;
2557
2558 // Return the current directory
2559 cwd = alloc(MAXPATHL);
2560 if (cwd != NULL)
2561 {
2562 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2563 {
2564#ifdef BACKSLASH_IN_FILENAME
2565 slash_adjust(cwd);
2566#endif
2567 rettv->vval.v_string = vim_strsave(cwd);
2568 }
2569 vim_free(cwd);
2570 }
2571
2572 if (curwin->w_localdir != NULL)
2573 scope = CDSCOPE_WINDOW;
2574 else if (curtab->tp_localdir != NULL)
2575 scope = CDSCOPE_TABPAGE;
2576
2577 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2578 // Directory change failed
2579 VIM_CLEAR(rettv->vval.v_string);
2580}
2581
2582/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002583 * "cindent(lnum)" function
2584 */
2585 static void
2586f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2587{
2588#ifdef FEAT_CINDENT
2589 pos_T pos;
2590 linenr_T lnum;
2591
2592 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002593 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002594 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2595 {
2596 curwin->w_cursor.lnum = lnum;
2597 rettv->vval.v_number = get_c_indent();
2598 curwin->w_cursor = pos;
2599 }
2600 else
2601#endif
2602 rettv->vval.v_number = -1;
2603}
2604
Bram Moolenaaraff74912019-03-30 18:11:49 +01002605 static win_T *
2606get_optional_window(typval_T *argvars, int idx)
2607{
2608 win_T *win = curwin;
2609
2610 if (argvars[idx].v_type != VAR_UNKNOWN)
2611 {
2612 win = find_win_by_nr_or_id(&argvars[idx]);
2613 if (win == NULL)
2614 {
2615 emsg(_(e_invalwindow));
2616 return NULL;
2617 }
2618 }
2619 return win;
2620}
2621
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002622/*
2623 * "clearmatches()" function
2624 */
2625 static void
2626f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2627{
2628#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002629 win_T *win = get_optional_window(argvars, 0);
2630
2631 if (win != NULL)
2632 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002633#endif
2634}
2635
2636/*
2637 * "col(string)" function
2638 */
2639 static void
2640f_col(typval_T *argvars, typval_T *rettv)
2641{
2642 colnr_T col = 0;
2643 pos_T *fp;
2644 int fnum = curbuf->b_fnum;
2645
2646 fp = var2fpos(&argvars[0], FALSE, &fnum);
2647 if (fp != NULL && fnum == curbuf->b_fnum)
2648 {
2649 if (fp->col == MAXCOL)
2650 {
2651 /* '> can be MAXCOL, get the length of the line then */
2652 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2653 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2654 else
2655 col = MAXCOL;
2656 }
2657 else
2658 {
2659 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660 /* col(".") when the cursor is on the NUL at the end of the line
2661 * because of "coladd" can be seen as an extra column. */
2662 if (virtual_active() && fp == &curwin->w_cursor)
2663 {
2664 char_u *p = ml_get_cursor();
2665
2666 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2667 curwin->w_virtcol - curwin->w_cursor.coladd))
2668 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002669 int l;
2670
2671 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2672 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002673 }
2674 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 }
2676 }
2677 rettv->vval.v_number = col;
2678}
2679
2680#if defined(FEAT_INS_EXPAND)
2681/*
2682 * "complete()" function
2683 */
2684 static void
2685f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2686{
2687 int startcol;
2688
2689 if ((State & INSERT) == 0)
2690 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002691 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002692 return;
2693 }
2694
2695 /* Check for undo allowed here, because if something was already inserted
2696 * the line was already saved for undo and this check isn't done. */
2697 if (!undo_allowed())
2698 return;
2699
2700 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2701 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002702 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002703 return;
2704 }
2705
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002706 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002707 if (startcol <= 0)
2708 return;
2709
2710 set_completion(startcol - 1, argvars[1].vval.v_list);
2711}
2712
2713/*
2714 * "complete_add()" function
2715 */
2716 static void
2717f_complete_add(typval_T *argvars, typval_T *rettv)
2718{
2719 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2720}
2721
2722/*
2723 * "complete_check()" function
2724 */
2725 static void
2726f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2727{
2728 int saved = RedrawingDisabled;
2729
2730 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002731 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002732 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002733 RedrawingDisabled = saved;
2734}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002735
2736/*
2737 * "complete_info()" function
2738 */
2739 static void
2740f_complete_info(typval_T *argvars, typval_T *rettv)
2741{
2742 list_T *what_list = NULL;
2743
2744 if (rettv_dict_alloc(rettv) != OK)
2745 return;
2746
2747 if (argvars[0].v_type != VAR_UNKNOWN)
2748 {
2749 if (argvars[0].v_type != VAR_LIST)
2750 {
2751 emsg(_(e_listreq));
2752 return;
2753 }
2754 what_list = argvars[0].vval.v_list;
2755 }
2756 get_complete_info(what_list, rettv->vval.v_dict);
2757}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002758#endif
2759
2760/*
2761 * "confirm(message, buttons[, default [, type]])" function
2762 */
2763 static void
2764f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2765{
2766#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2767 char_u *message;
2768 char_u *buttons = NULL;
2769 char_u buf[NUMBUFLEN];
2770 char_u buf2[NUMBUFLEN];
2771 int def = 1;
2772 int type = VIM_GENERIC;
2773 char_u *typestr;
2774 int error = FALSE;
2775
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002776 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002777 if (message == NULL)
2778 error = TRUE;
2779 if (argvars[1].v_type != VAR_UNKNOWN)
2780 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002781 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 if (buttons == NULL)
2783 error = TRUE;
2784 if (argvars[2].v_type != VAR_UNKNOWN)
2785 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002786 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002787 if (argvars[3].v_type != VAR_UNKNOWN)
2788 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002789 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 if (typestr == NULL)
2791 error = TRUE;
2792 else
2793 {
2794 switch (TOUPPER_ASC(*typestr))
2795 {
2796 case 'E': type = VIM_ERROR; break;
2797 case 'Q': type = VIM_QUESTION; break;
2798 case 'I': type = VIM_INFO; break;
2799 case 'W': type = VIM_WARNING; break;
2800 case 'G': type = VIM_GENERIC; break;
2801 }
2802 }
2803 }
2804 }
2805 }
2806
2807 if (buttons == NULL || *buttons == NUL)
2808 buttons = (char_u *)_("&Ok");
2809
2810 if (!error)
2811 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2812 def, NULL, FALSE);
2813#endif
2814}
2815
2816/*
2817 * "copy()" function
2818 */
2819 static void
2820f_copy(typval_T *argvars, typval_T *rettv)
2821{
2822 item_copy(&argvars[0], rettv, FALSE, 0);
2823}
2824
2825#ifdef FEAT_FLOAT
2826/*
2827 * "cos()" function
2828 */
2829 static void
2830f_cos(typval_T *argvars, typval_T *rettv)
2831{
2832 float_T f = 0.0;
2833
2834 rettv->v_type = VAR_FLOAT;
2835 if (get_float_arg(argvars, &f) == OK)
2836 rettv->vval.v_float = cos(f);
2837 else
2838 rettv->vval.v_float = 0.0;
2839}
2840
2841/*
2842 * "cosh()" function
2843 */
2844 static void
2845f_cosh(typval_T *argvars, typval_T *rettv)
2846{
2847 float_T f = 0.0;
2848
2849 rettv->v_type = VAR_FLOAT;
2850 if (get_float_arg(argvars, &f) == OK)
2851 rettv->vval.v_float = cosh(f);
2852 else
2853 rettv->vval.v_float = 0.0;
2854}
2855#endif
2856
2857/*
2858 * "count()" function
2859 */
2860 static void
2861f_count(typval_T *argvars, typval_T *rettv)
2862{
2863 long n = 0;
2864 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002865 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866
Bram Moolenaar9966b212017-07-28 16:46:57 +02002867 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002868 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002869
2870 if (argvars[0].v_type == VAR_STRING)
2871 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002872 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002873 char_u *p = argvars[0].vval.v_string;
2874 char_u *next;
2875
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002876 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002877 {
2878 if (ic)
2879 {
2880 size_t len = STRLEN(expr);
2881
2882 while (*p != NUL)
2883 {
2884 if (MB_STRNICMP(p, expr, len) == 0)
2885 {
2886 ++n;
2887 p += len;
2888 }
2889 else
2890 MB_PTR_ADV(p);
2891 }
2892 }
2893 else
2894 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2895 != NULL)
2896 {
2897 ++n;
2898 p = next + STRLEN(expr);
2899 }
2900 }
2901
2902 }
2903 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002904 {
2905 listitem_T *li;
2906 list_T *l;
2907 long idx;
2908
2909 if ((l = argvars[0].vval.v_list) != NULL)
2910 {
2911 li = l->lv_first;
2912 if (argvars[2].v_type != VAR_UNKNOWN)
2913 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002914 if (argvars[3].v_type != VAR_UNKNOWN)
2915 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002916 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002917 if (!error)
2918 {
2919 li = list_find(l, idx);
2920 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002921 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002922 }
2923 }
2924 if (error)
2925 li = NULL;
2926 }
2927
2928 for ( ; li != NULL; li = li->li_next)
2929 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2930 ++n;
2931 }
2932 }
2933 else if (argvars[0].v_type == VAR_DICT)
2934 {
2935 int todo;
2936 dict_T *d;
2937 hashitem_T *hi;
2938
2939 if ((d = argvars[0].vval.v_dict) != NULL)
2940 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002941 if (argvars[2].v_type != VAR_UNKNOWN)
2942 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002943 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002944 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002945 }
2946
2947 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2948 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2949 {
2950 if (!HASHITEM_EMPTY(hi))
2951 {
2952 --todo;
2953 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2954 ++n;
2955 }
2956 }
2957 }
2958 }
2959 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002960 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002961 rettv->vval.v_number = n;
2962}
2963
2964/*
2965 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2966 *
2967 * Checks the existence of a cscope connection.
2968 */
2969 static void
2970f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2971{
2972#ifdef FEAT_CSCOPE
2973 int num = 0;
2974 char_u *dbpath = NULL;
2975 char_u *prepend = NULL;
2976 char_u buf[NUMBUFLEN];
2977
2978 if (argvars[0].v_type != VAR_UNKNOWN
2979 && argvars[1].v_type != VAR_UNKNOWN)
2980 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002981 num = (int)tv_get_number(&argvars[0]);
2982 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002983 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002984 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002985 }
2986
2987 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2988#endif
2989}
2990
2991/*
2992 * "cursor(lnum, col)" function, or
2993 * "cursor(list)"
2994 *
2995 * Moves the cursor to the specified line and column.
2996 * Returns 0 when the position could be set, -1 otherwise.
2997 */
2998 static void
2999f_cursor(typval_T *argvars, typval_T *rettv)
3000{
3001 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003002 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003003 int set_curswant = TRUE;
3004
3005 rettv->vval.v_number = -1;
3006 if (argvars[1].v_type == VAR_UNKNOWN)
3007 {
3008 pos_T pos;
3009 colnr_T curswant = -1;
3010
3011 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3012 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003013 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003014 return;
3015 }
3016 line = pos.lnum;
3017 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003018 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003019 if (curswant >= 0)
3020 {
3021 curwin->w_curswant = curswant - 1;
3022 set_curswant = FALSE;
3023 }
3024 }
3025 else
3026 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003027 line = tv_get_lnum(argvars);
3028 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003029 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003030 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003032 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033 return; /* type error; errmsg already given */
3034 if (line > 0)
3035 curwin->w_cursor.lnum = line;
3036 if (col > 0)
3037 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003039
3040 /* Make sure the cursor is in a valid position. */
3041 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003042 /* Correct cursor for multi-byte character. */
3043 if (has_mbyte)
3044 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003045
3046 curwin->w_set_curswant = set_curswant;
3047 rettv->vval.v_number = 0;
3048}
3049
Bram Moolenaar4f974752019-02-17 17:44:42 +01003050#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003051/*
3052 * "debugbreak()" function
3053 */
3054 static void
3055f_debugbreak(typval_T *argvars, typval_T *rettv)
3056{
3057 int pid;
3058
3059 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003060 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003061 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003062 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003063 else
3064 {
3065 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3066
3067 if (hProcess != NULL)
3068 {
3069 DebugBreakProcess(hProcess);
3070 CloseHandle(hProcess);
3071 rettv->vval.v_number = OK;
3072 }
3073 }
3074}
3075#endif
3076
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003077/*
3078 * "deepcopy()" function
3079 */
3080 static void
3081f_deepcopy(typval_T *argvars, typval_T *rettv)
3082{
3083 int noref = 0;
3084 int copyID;
3085
3086 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003087 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003088 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003089 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003090 else
3091 {
3092 copyID = get_copyID();
3093 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3094 }
3095}
3096
3097/*
3098 * "delete()" function
3099 */
3100 static void
3101f_delete(typval_T *argvars, typval_T *rettv)
3102{
3103 char_u nbuf[NUMBUFLEN];
3104 char_u *name;
3105 char_u *flags;
3106
3107 rettv->vval.v_number = -1;
3108 if (check_restricted() || check_secure())
3109 return;
3110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003111 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003112 if (name == NULL || *name == NUL)
3113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003114 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 return;
3116 }
3117
3118 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003119 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003120 else
3121 flags = (char_u *)"";
3122
3123 if (*flags == NUL)
3124 /* delete a file */
3125 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3126 else if (STRCMP(flags, "d") == 0)
3127 /* delete an empty directory */
3128 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3129 else if (STRCMP(flags, "rf") == 0)
3130 /* delete a directory recursively */
3131 rettv->vval.v_number = delete_recursive(name);
3132 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003133 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003134}
3135
3136/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003137 * "deletebufline()" function
3138 */
3139 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003140f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003141{
3142 buf_T *buf;
3143 linenr_T first, last;
3144 linenr_T lnum;
3145 long count;
3146 int is_curbuf;
3147 buf_T *curbuf_save = NULL;
3148 win_T *curwin_save = NULL;
3149 tabpage_T *tp;
3150 win_T *wp;
3151
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003152 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003153 if (buf == NULL)
3154 {
3155 rettv->vval.v_number = 1; /* FAIL */
3156 return;
3157 }
3158 is_curbuf = buf == curbuf;
3159
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003160 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003161 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003162 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003163 else
3164 last = first;
3165
3166 if (buf->b_ml.ml_mfp == NULL || first < 1
3167 || first > buf->b_ml.ml_line_count || last < first)
3168 {
3169 rettv->vval.v_number = 1; /* FAIL */
3170 return;
3171 }
3172
3173 if (!is_curbuf)
3174 {
3175 curbuf_save = curbuf;
3176 curwin_save = curwin;
3177 curbuf = buf;
3178 find_win_for_curbuf();
3179 }
3180 if (last > curbuf->b_ml.ml_line_count)
3181 last = curbuf->b_ml.ml_line_count;
3182 count = last - first + 1;
3183
3184 // When coming here from Insert mode, sync undo, so that this can be
3185 // undone separately from what was previously inserted.
3186 if (u_sync_once == 2)
3187 {
3188 u_sync_once = 1; // notify that u_sync() was called
3189 u_sync(TRUE);
3190 }
3191
3192 if (u_save(first - 1, last + 1) == FAIL)
3193 {
3194 rettv->vval.v_number = 1; /* FAIL */
3195 return;
3196 }
3197
3198 for (lnum = first; lnum <= last; ++lnum)
3199 ml_delete(first, TRUE);
3200
3201 FOR_ALL_TAB_WINDOWS(tp, wp)
3202 if (wp->w_buffer == buf)
3203 {
3204 if (wp->w_cursor.lnum > last)
3205 wp->w_cursor.lnum -= count;
3206 else if (wp->w_cursor.lnum> first)
3207 wp->w_cursor.lnum = first;
3208 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3209 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3210 }
3211 check_cursor_col();
3212 deleted_lines_mark(first, count);
3213
3214 if (!is_curbuf)
3215 {
3216 curbuf = curbuf_save;
3217 curwin = curwin_save;
3218 }
3219}
3220
3221/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003222 * "did_filetype()" function
3223 */
3224 static void
3225f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3226{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003227 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003228}
3229
3230/*
3231 * "diff_filler()" function
3232 */
3233 static void
3234f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3235{
3236#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003237 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003238#endif
3239}
3240
3241/*
3242 * "diff_hlID()" function
3243 */
3244 static void
3245f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3246{
3247#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003248 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003249 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003250 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003251 static int fnum = 0;
3252 static int change_start = 0;
3253 static int change_end = 0;
3254 static hlf_T hlID = (hlf_T)0;
3255 int filler_lines;
3256 int col;
3257
3258 if (lnum < 0) /* ignore type error in {lnum} arg */
3259 lnum = 0;
3260 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003261 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003262 || fnum != curbuf->b_fnum)
3263 {
3264 /* New line, buffer, change: need to get the values. */
3265 filler_lines = diff_check(curwin, lnum);
3266 if (filler_lines < 0)
3267 {
3268 if (filler_lines == -1)
3269 {
3270 change_start = MAXCOL;
3271 change_end = -1;
3272 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3273 hlID = HLF_ADD; /* added line */
3274 else
3275 hlID = HLF_CHD; /* changed line */
3276 }
3277 else
3278 hlID = HLF_ADD; /* added line */
3279 }
3280 else
3281 hlID = (hlf_T)0;
3282 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003283 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003284 fnum = curbuf->b_fnum;
3285 }
3286
3287 if (hlID == HLF_CHD || hlID == HLF_TXD)
3288 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003289 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 if (col >= change_start && col <= change_end)
3291 hlID = HLF_TXD; /* changed text */
3292 else
3293 hlID = HLF_CHD; /* changed line */
3294 }
3295 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3296#endif
3297}
3298
3299/*
3300 * "empty({expr})" function
3301 */
3302 static void
3303f_empty(typval_T *argvars, typval_T *rettv)
3304{
3305 int n = FALSE;
3306
3307 switch (argvars[0].v_type)
3308 {
3309 case VAR_STRING:
3310 case VAR_FUNC:
3311 n = argvars[0].vval.v_string == NULL
3312 || *argvars[0].vval.v_string == NUL;
3313 break;
3314 case VAR_PARTIAL:
3315 n = FALSE;
3316 break;
3317 case VAR_NUMBER:
3318 n = argvars[0].vval.v_number == 0;
3319 break;
3320 case VAR_FLOAT:
3321#ifdef FEAT_FLOAT
3322 n = argvars[0].vval.v_float == 0.0;
3323 break;
3324#endif
3325 case VAR_LIST:
3326 n = argvars[0].vval.v_list == NULL
3327 || argvars[0].vval.v_list->lv_first == NULL;
3328 break;
3329 case VAR_DICT:
3330 n = argvars[0].vval.v_dict == NULL
3331 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3332 break;
3333 case VAR_SPECIAL:
3334 n = argvars[0].vval.v_number != VVAL_TRUE;
3335 break;
3336
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003337 case VAR_BLOB:
3338 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003339 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3340 break;
3341
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003342 case VAR_JOB:
3343#ifdef FEAT_JOB_CHANNEL
3344 n = argvars[0].vval.v_job == NULL
3345 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3346 break;
3347#endif
3348 case VAR_CHANNEL:
3349#ifdef FEAT_JOB_CHANNEL
3350 n = argvars[0].vval.v_channel == NULL
3351 || !channel_is_open(argvars[0].vval.v_channel);
3352 break;
3353#endif
3354 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003355 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003356 n = TRUE;
3357 break;
3358 }
3359
3360 rettv->vval.v_number = n;
3361}
3362
3363/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003364 * "environ()" function
3365 */
3366 static void
3367f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3368{
3369#if !defined(AMIGA)
3370 int i = 0;
3371 char_u *entry, *value;
3372# ifdef MSWIN
3373 extern wchar_t **_wenviron;
3374# else
3375 extern char **environ;
3376# endif
3377
3378 if (rettv_dict_alloc(rettv) != OK)
3379 return;
3380
3381# ifdef MSWIN
3382 if (*_wenviron == NULL)
3383 return;
3384# else
3385 if (*environ == NULL)
3386 return;
3387# endif
3388
3389 for (i = 0; ; ++i)
3390 {
3391# ifdef MSWIN
3392 short_u *p;
3393
3394 if ((p = (short_u *)_wenviron[i]) == NULL)
3395 return;
3396 entry = utf16_to_enc(p, NULL);
3397# else
3398 if ((entry = (char_u *)environ[i]) == NULL)
3399 return;
3400 entry = vim_strsave(entry);
3401# endif
3402 if (entry == NULL) // out of memory
3403 return;
3404 if ((value = vim_strchr(entry, '=')) == NULL)
3405 {
3406 vim_free(entry);
3407 continue;
3408 }
3409 *value++ = NUL;
3410 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3411 vim_free(entry);
3412 }
3413#endif
3414}
3415
3416/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003417 * "escape({string}, {chars})" function
3418 */
3419 static void
3420f_escape(typval_T *argvars, typval_T *rettv)
3421{
3422 char_u buf[NUMBUFLEN];
3423
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003424 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3425 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003426 rettv->v_type = VAR_STRING;
3427}
3428
3429/*
3430 * "eval()" function
3431 */
3432 static void
3433f_eval(typval_T *argvars, typval_T *rettv)
3434{
3435 char_u *s, *p;
3436
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003437 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003438 if (s != NULL)
3439 s = skipwhite(s);
3440
3441 p = s;
3442 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3443 {
3444 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003445 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003446 need_clr_eos = FALSE;
3447 rettv->v_type = VAR_NUMBER;
3448 rettv->vval.v_number = 0;
3449 }
3450 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003451 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003452}
3453
3454/*
3455 * "eventhandler()" function
3456 */
3457 static void
3458f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3459{
3460 rettv->vval.v_number = vgetc_busy;
3461}
3462
3463/*
3464 * "executable()" function
3465 */
3466 static void
3467f_executable(typval_T *argvars, typval_T *rettv)
3468{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003469 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003470
3471 /* Check in $PATH and also check directly if there is a directory name. */
3472 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3473 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3474}
3475
3476static garray_T redir_execute_ga;
3477
3478/*
3479 * Append "value[value_len]" to the execute() output.
3480 */
3481 void
3482execute_redir_str(char_u *value, int value_len)
3483{
3484 int len;
3485
3486 if (value_len == -1)
3487 len = (int)STRLEN(value); /* Append the entire string */
3488 else
3489 len = value_len; /* Append only "value_len" characters */
3490 if (ga_grow(&redir_execute_ga, len) == OK)
3491 {
3492 mch_memmove((char *)redir_execute_ga.ga_data
3493 + redir_execute_ga.ga_len, value, len);
3494 redir_execute_ga.ga_len += len;
3495 }
3496}
3497
3498/*
3499 * Get next line from a list.
3500 * Called by do_cmdline() to get the next line.
3501 * Returns allocated string, or NULL for end of function.
3502 */
3503
3504 static char_u *
3505get_list_line(
3506 int c UNUSED,
3507 void *cookie,
3508 int indent UNUSED)
3509{
3510 listitem_T **p = (listitem_T **)cookie;
3511 listitem_T *item = *p;
3512 char_u buf[NUMBUFLEN];
3513 char_u *s;
3514
3515 if (item == NULL)
3516 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003517 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003518 *p = item->li_next;
3519 return s == NULL ? NULL : vim_strsave(s);
3520}
3521
3522/*
3523 * "execute()" function
3524 */
3525 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003526execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003527{
3528 char_u *cmd = NULL;
3529 list_T *list = NULL;
3530 int save_msg_silent = msg_silent;
3531 int save_emsg_silent = emsg_silent;
3532 int save_emsg_noredir = emsg_noredir;
3533 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003534 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003535 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003536 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003537 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003538
3539 rettv->vval.v_string = NULL;
3540 rettv->v_type = VAR_STRING;
3541
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003542 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003543 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003544 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003545 if (list == NULL || list->lv_first == NULL)
3546 /* empty list, no commands, empty output */
3547 return;
3548 ++list->lv_refcount;
3549 }
3550 else
3551 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003552 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003553 if (cmd == NULL)
3554 return;
3555 }
3556
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003557 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003558 {
3559 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003560 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561
3562 if (s == NULL)
3563 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003564 if (*s == NUL)
3565 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003566 if (STRNCMP(s, "silent", 6) == 0)
3567 ++msg_silent;
3568 if (STRCMP(s, "silent!") == 0)
3569 {
3570 emsg_silent = TRUE;
3571 emsg_noredir = TRUE;
3572 }
3573 }
3574 else
3575 ++msg_silent;
3576
3577 if (redir_execute)
3578 save_ga = redir_execute_ga;
3579 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3580 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003581 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003582 if (!echo_output)
3583 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003584
3585 if (cmd != NULL)
3586 do_cmdline_cmd(cmd);
3587 else
3588 {
3589 listitem_T *item = list->lv_first;
3590
3591 do_cmdline(NULL, get_list_line, (void *)&item,
3592 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3593 --list->lv_refcount;
3594 }
3595
Bram Moolenaard297f352017-01-29 20:31:21 +01003596 /* Need to append a NUL to the result. */
3597 if (ga_grow(&redir_execute_ga, 1) == OK)
3598 {
3599 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3600 rettv->vval.v_string = redir_execute_ga.ga_data;
3601 }
3602 else
3603 {
3604 ga_clear(&redir_execute_ga);
3605 rettv->vval.v_string = NULL;
3606 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003607 msg_silent = save_msg_silent;
3608 emsg_silent = save_emsg_silent;
3609 emsg_noredir = save_emsg_noredir;
3610
3611 redir_execute = save_redir_execute;
3612 if (redir_execute)
3613 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003614 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003615
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003616 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003617 if (echo_output)
3618 // When not working silently: put it in column zero. A following
3619 // "echon" will overwrite the message, unavoidably.
3620 msg_col = 0;
3621 else
3622 // When working silently: Put it back where it was, since nothing
3623 // should have been written.
3624 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003625}
3626
3627/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003628 * "execute()" function
3629 */
3630 static void
3631f_execute(typval_T *argvars, typval_T *rettv)
3632{
3633 execute_common(argvars, rettv, 0);
3634}
3635
3636/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003637 * "exepath()" function
3638 */
3639 static void
3640f_exepath(typval_T *argvars, typval_T *rettv)
3641{
3642 char_u *p = NULL;
3643
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003644 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003645 rettv->v_type = VAR_STRING;
3646 rettv->vval.v_string = p;
3647}
3648
3649/*
3650 * "exists()" function
3651 */
3652 static void
3653f_exists(typval_T *argvars, typval_T *rettv)
3654{
3655 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003656 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003657
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003658 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003659 if (*p == '$') /* environment variable */
3660 {
3661 /* first try "normal" environment variables (fast) */
3662 if (mch_getenv(p + 1) != NULL)
3663 n = TRUE;
3664 else
3665 {
3666 /* try expanding things like $VIM and ${HOME} */
3667 p = expand_env_save(p);
3668 if (p != NULL && *p != '$')
3669 n = TRUE;
3670 vim_free(p);
3671 }
3672 }
3673 else if (*p == '&' || *p == '+') /* option */
3674 {
3675 n = (get_option_tv(&p, NULL, TRUE) == OK);
3676 if (*skipwhite(p) != NUL)
3677 n = FALSE; /* trailing garbage */
3678 }
3679 else if (*p == '*') /* internal or user defined function */
3680 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003681 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003682 }
3683 else if (*p == ':')
3684 {
3685 n = cmd_exists(p + 1);
3686 }
3687 else if (*p == '#')
3688 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003689 if (p[1] == '#')
3690 n = autocmd_supported(p + 2);
3691 else
3692 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003693 }
3694 else /* internal variable */
3695 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003696 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697 }
3698
3699 rettv->vval.v_number = n;
3700}
3701
3702#ifdef FEAT_FLOAT
3703/*
3704 * "exp()" function
3705 */
3706 static void
3707f_exp(typval_T *argvars, typval_T *rettv)
3708{
3709 float_T f = 0.0;
3710
3711 rettv->v_type = VAR_FLOAT;
3712 if (get_float_arg(argvars, &f) == OK)
3713 rettv->vval.v_float = exp(f);
3714 else
3715 rettv->vval.v_float = 0.0;
3716}
3717#endif
3718
3719/*
3720 * "expand()" function
3721 */
3722 static void
3723f_expand(typval_T *argvars, typval_T *rettv)
3724{
3725 char_u *s;
3726 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003727 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003728 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3729 expand_T xpc;
3730 int error = FALSE;
3731 char_u *result;
3732
3733 rettv->v_type = VAR_STRING;
3734 if (argvars[1].v_type != VAR_UNKNOWN
3735 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003736 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003737 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003738 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003739
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003740 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003741 if (*s == '%' || *s == '#' || *s == '<')
3742 {
3743 ++emsg_off;
3744 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3745 --emsg_off;
3746 if (rettv->v_type == VAR_LIST)
3747 {
3748 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3749 list_append_string(rettv->vval.v_list, result, -1);
3750 else
3751 vim_free(result);
3752 }
3753 else
3754 rettv->vval.v_string = result;
3755 }
3756 else
3757 {
3758 /* When the optional second argument is non-zero, don't remove matches
3759 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3760 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003761 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003762 options |= WILD_KEEP_ALL;
3763 if (!error)
3764 {
3765 ExpandInit(&xpc);
3766 xpc.xp_context = EXPAND_FILES;
3767 if (p_wic)
3768 options += WILD_ICASE;
3769 if (rettv->v_type == VAR_STRING)
3770 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3771 options, WILD_ALL);
3772 else if (rettv_list_alloc(rettv) != FAIL)
3773 {
3774 int i;
3775
3776 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3777 for (i = 0; i < xpc.xp_numfiles; i++)
3778 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3779 ExpandCleanup(&xpc);
3780 }
3781 }
3782 else
3783 rettv->vval.v_string = NULL;
3784 }
3785}
3786
3787/*
3788 * "extend(list, list [, idx])" function
3789 * "extend(dict, dict [, action])" function
3790 */
3791 static void
3792f_extend(typval_T *argvars, typval_T *rettv)
3793{
3794 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3795
3796 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3797 {
3798 list_T *l1, *l2;
3799 listitem_T *item;
3800 long before;
3801 int error = FALSE;
3802
3803 l1 = argvars[0].vval.v_list;
3804 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003805 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003806 && l2 != NULL)
3807 {
3808 if (argvars[2].v_type != VAR_UNKNOWN)
3809 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003810 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003811 if (error)
3812 return; /* type error; errmsg already given */
3813
3814 if (before == l1->lv_len)
3815 item = NULL;
3816 else
3817 {
3818 item = list_find(l1, before);
3819 if (item == NULL)
3820 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003821 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003822 return;
3823 }
3824 }
3825 }
3826 else
3827 item = NULL;
3828 list_extend(l1, l2, item);
3829
3830 copy_tv(&argvars[0], rettv);
3831 }
3832 }
3833 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3834 {
3835 dict_T *d1, *d2;
3836 char_u *action;
3837 int i;
3838
3839 d1 = argvars[0].vval.v_dict;
3840 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003841 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003842 && d2 != NULL)
3843 {
3844 /* Check the third argument. */
3845 if (argvars[2].v_type != VAR_UNKNOWN)
3846 {
3847 static char *(av[]) = {"keep", "force", "error"};
3848
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003849 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003850 if (action == NULL)
3851 return; /* type error; errmsg already given */
3852 for (i = 0; i < 3; ++i)
3853 if (STRCMP(action, av[i]) == 0)
3854 break;
3855 if (i == 3)
3856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003857 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003858 return;
3859 }
3860 }
3861 else
3862 action = (char_u *)"force";
3863
3864 dict_extend(d1, d2, action);
3865
3866 copy_tv(&argvars[0], rettv);
3867 }
3868 }
3869 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003870 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003871}
3872
3873/*
3874 * "feedkeys()" function
3875 */
3876 static void
3877f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3878{
3879 int remap = TRUE;
3880 int insert = FALSE;
3881 char_u *keys, *flags;
3882 char_u nbuf[NUMBUFLEN];
3883 int typed = FALSE;
3884 int execute = FALSE;
3885 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003886 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003887 char_u *keys_esc;
3888
3889 /* This is not allowed in the sandbox. If the commands would still be
3890 * executed in the sandbox it would be OK, but it probably happens later,
3891 * when "sandbox" is no longer set. */
3892 if (check_secure())
3893 return;
3894
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003895 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003896
3897 if (argvars[1].v_type != VAR_UNKNOWN)
3898 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003899 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003900 for ( ; *flags != NUL; ++flags)
3901 {
3902 switch (*flags)
3903 {
3904 case 'n': remap = FALSE; break;
3905 case 'm': remap = TRUE; break;
3906 case 't': typed = TRUE; break;
3907 case 'i': insert = TRUE; break;
3908 case 'x': execute = TRUE; break;
3909 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003910 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003911 }
3912 }
3913 }
3914
3915 if (*keys != NUL || execute)
3916 {
3917 /* Need to escape K_SPECIAL and CSI before putting the string in the
3918 * typeahead buffer. */
3919 keys_esc = vim_strsave_escape_csi(keys);
3920 if (keys_esc != NULL)
3921 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003922 if (lowlevel)
3923 {
3924#ifdef USE_INPUT_BUF
3925 add_to_input_buf(keys, (int)STRLEN(keys));
3926#else
3927 emsg(_("E980: lowlevel input not supported"));
3928#endif
3929 }
3930 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003931 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003932 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003933 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003934 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003935#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003936 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003937#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003938 )
3939 typebuf_was_filled = TRUE;
3940 }
3941 vim_free(keys_esc);
3942
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003943 if (execute)
3944 {
3945 int save_msg_scroll = msg_scroll;
3946
3947 /* Avoid a 1 second delay when the keys start Insert mode. */
3948 msg_scroll = FALSE;
3949
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003950 if (!dangerous)
3951 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003952 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003953 if (!dangerous)
3954 --ex_normal_busy;
3955
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003956 msg_scroll |= save_msg_scroll;
3957 }
3958 }
3959 }
3960}
3961
3962/*
3963 * "filereadable()" function
3964 */
3965 static void
3966f_filereadable(typval_T *argvars, typval_T *rettv)
3967{
3968 int fd;
3969 char_u *p;
3970 int n;
3971
3972#ifndef O_NONBLOCK
3973# define O_NONBLOCK 0
3974#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003975 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003976 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3977 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3978 {
3979 n = TRUE;
3980 close(fd);
3981 }
3982 else
3983 n = FALSE;
3984
3985 rettv->vval.v_number = n;
3986}
3987
3988/*
3989 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3990 * rights to write into.
3991 */
3992 static void
3993f_filewritable(typval_T *argvars, typval_T *rettv)
3994{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003995 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003996}
3997
3998 static void
3999findfilendir(
4000 typval_T *argvars UNUSED,
4001 typval_T *rettv,
4002 int find_what UNUSED)
4003{
4004#ifdef FEAT_SEARCHPATH
4005 char_u *fname;
4006 char_u *fresult = NULL;
4007 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
4008 char_u *p;
4009 char_u pathbuf[NUMBUFLEN];
4010 int count = 1;
4011 int first = TRUE;
4012 int error = FALSE;
4013#endif
4014
4015 rettv->vval.v_string = NULL;
4016 rettv->v_type = VAR_STRING;
4017
4018#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004019 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004020
4021 if (argvars[1].v_type != VAR_UNKNOWN)
4022 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004023 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004024 if (p == NULL)
4025 error = TRUE;
4026 else
4027 {
4028 if (*p != NUL)
4029 path = p;
4030
4031 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004032 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004033 }
4034 }
4035
4036 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4037 error = TRUE;
4038
4039 if (*fname != NUL && !error)
4040 {
4041 do
4042 {
4043 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4044 vim_free(fresult);
4045 fresult = find_file_in_path_option(first ? fname : NULL,
4046 first ? (int)STRLEN(fname) : 0,
4047 0, first, path,
4048 find_what,
4049 curbuf->b_ffname,
4050 find_what == FINDFILE_DIR
4051 ? (char_u *)"" : curbuf->b_p_sua);
4052 first = FALSE;
4053
4054 if (fresult != NULL && rettv->v_type == VAR_LIST)
4055 list_append_string(rettv->vval.v_list, fresult, -1);
4056
4057 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4058 }
4059
4060 if (rettv->v_type == VAR_STRING)
4061 rettv->vval.v_string = fresult;
4062#endif
4063}
4064
4065/*
4066 * "filter()" function
4067 */
4068 static void
4069f_filter(typval_T *argvars, typval_T *rettv)
4070{
4071 filter_map(argvars, rettv, FALSE);
4072}
4073
4074/*
4075 * "finddir({fname}[, {path}[, {count}]])" function
4076 */
4077 static void
4078f_finddir(typval_T *argvars, typval_T *rettv)
4079{
4080 findfilendir(argvars, rettv, FINDFILE_DIR);
4081}
4082
4083/*
4084 * "findfile({fname}[, {path}[, {count}]])" function
4085 */
4086 static void
4087f_findfile(typval_T *argvars, typval_T *rettv)
4088{
4089 findfilendir(argvars, rettv, FINDFILE_FILE);
4090}
4091
4092#ifdef FEAT_FLOAT
4093/*
4094 * "float2nr({float})" function
4095 */
4096 static void
4097f_float2nr(typval_T *argvars, typval_T *rettv)
4098{
4099 float_T f = 0.0;
4100
4101 if (get_float_arg(argvars, &f) == OK)
4102 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004103 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004104 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004105 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004106 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004107 else
4108 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004109 }
4110}
4111
4112/*
4113 * "floor({float})" function
4114 */
4115 static void
4116f_floor(typval_T *argvars, typval_T *rettv)
4117{
4118 float_T f = 0.0;
4119
4120 rettv->v_type = VAR_FLOAT;
4121 if (get_float_arg(argvars, &f) == OK)
4122 rettv->vval.v_float = floor(f);
4123 else
4124 rettv->vval.v_float = 0.0;
4125}
4126
4127/*
4128 * "fmod()" function
4129 */
4130 static void
4131f_fmod(typval_T *argvars, typval_T *rettv)
4132{
4133 float_T fx = 0.0, fy = 0.0;
4134
4135 rettv->v_type = VAR_FLOAT;
4136 if (get_float_arg(argvars, &fx) == OK
4137 && get_float_arg(&argvars[1], &fy) == OK)
4138 rettv->vval.v_float = fmod(fx, fy);
4139 else
4140 rettv->vval.v_float = 0.0;
4141}
4142#endif
4143
4144/*
4145 * "fnameescape({string})" function
4146 */
4147 static void
4148f_fnameescape(typval_T *argvars, typval_T *rettv)
4149{
4150 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004151 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004152 rettv->v_type = VAR_STRING;
4153}
4154
4155/*
4156 * "fnamemodify({fname}, {mods})" function
4157 */
4158 static void
4159f_fnamemodify(typval_T *argvars, typval_T *rettv)
4160{
4161 char_u *fname;
4162 char_u *mods;
4163 int usedlen = 0;
4164 int len;
4165 char_u *fbuf = NULL;
4166 char_u buf[NUMBUFLEN];
4167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004168 fname = tv_get_string_chk(&argvars[0]);
4169 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004170 if (fname == NULL || mods == NULL)
4171 fname = NULL;
4172 else
4173 {
4174 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004175 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004176 }
4177
4178 rettv->v_type = VAR_STRING;
4179 if (fname == NULL)
4180 rettv->vval.v_string = NULL;
4181 else
4182 rettv->vval.v_string = vim_strnsave(fname, len);
4183 vim_free(fbuf);
4184}
4185
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004186/*
4187 * "foldclosed()" function
4188 */
4189 static void
4190foldclosed_both(
4191 typval_T *argvars UNUSED,
4192 typval_T *rettv,
4193 int end UNUSED)
4194{
4195#ifdef FEAT_FOLDING
4196 linenr_T lnum;
4197 linenr_T first, last;
4198
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004199 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004200 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4201 {
4202 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4203 {
4204 if (end)
4205 rettv->vval.v_number = (varnumber_T)last;
4206 else
4207 rettv->vval.v_number = (varnumber_T)first;
4208 return;
4209 }
4210 }
4211#endif
4212 rettv->vval.v_number = -1;
4213}
4214
4215/*
4216 * "foldclosed()" function
4217 */
4218 static void
4219f_foldclosed(typval_T *argvars, typval_T *rettv)
4220{
4221 foldclosed_both(argvars, rettv, FALSE);
4222}
4223
4224/*
4225 * "foldclosedend()" function
4226 */
4227 static void
4228f_foldclosedend(typval_T *argvars, typval_T *rettv)
4229{
4230 foldclosed_both(argvars, rettv, TRUE);
4231}
4232
4233/*
4234 * "foldlevel()" function
4235 */
4236 static void
4237f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4238{
4239#ifdef FEAT_FOLDING
4240 linenr_T lnum;
4241
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004242 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004243 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4244 rettv->vval.v_number = foldLevel(lnum);
4245#endif
4246}
4247
4248/*
4249 * "foldtext()" function
4250 */
4251 static void
4252f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4253{
4254#ifdef FEAT_FOLDING
4255 linenr_T foldstart;
4256 linenr_T foldend;
4257 char_u *dashes;
4258 linenr_T lnum;
4259 char_u *s;
4260 char_u *r;
4261 int len;
4262 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004263 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004264#endif
4265
4266 rettv->v_type = VAR_STRING;
4267 rettv->vval.v_string = NULL;
4268#ifdef FEAT_FOLDING
4269 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4270 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4271 dashes = get_vim_var_str(VV_FOLDDASHES);
4272 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4273 && dashes != NULL)
4274 {
4275 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004276 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004277 if (!linewhite(lnum))
4278 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004279
4280 /* Find interesting text in this line. */
4281 s = skipwhite(ml_get(lnum));
4282 /* skip C comment-start */
4283 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4284 {
4285 s = skipwhite(s + 2);
4286 if (*skipwhite(s) == NUL
4287 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4288 {
4289 s = skipwhite(ml_get(lnum + 1));
4290 if (*s == '*')
4291 s = skipwhite(s + 1);
4292 }
4293 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004294 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004295 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004296 r = alloc(STRLEN(txt)
4297 + STRLEN(dashes) // for %s
4298 + 20 // for %3ld
4299 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004300 if (r != NULL)
4301 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004302 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004303 len = (int)STRLEN(r);
4304 STRCAT(r, s);
4305 /* remove 'foldmarker' and 'commentstring' */
4306 foldtext_cleanup(r + len);
4307 rettv->vval.v_string = r;
4308 }
4309 }
4310#endif
4311}
4312
4313/*
4314 * "foldtextresult(lnum)" function
4315 */
4316 static void
4317f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4318{
4319#ifdef FEAT_FOLDING
4320 linenr_T lnum;
4321 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004322 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004323 foldinfo_T foldinfo;
4324 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004325 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004326#endif
4327
4328 rettv->v_type = VAR_STRING;
4329 rettv->vval.v_string = NULL;
4330#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004331 if (entered)
4332 return; /* reject recursive use */
4333 entered = TRUE;
4334
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004335 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004336 /* treat illegal types and illegal string values for {lnum} the same */
4337 if (lnum < 0)
4338 lnum = 0;
4339 fold_count = foldedCount(curwin, lnum, &foldinfo);
4340 if (fold_count > 0)
4341 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004342 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4343 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004344 if (text == buf)
4345 text = vim_strsave(text);
4346 rettv->vval.v_string = text;
4347 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004348
4349 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004350#endif
4351}
4352
4353/*
4354 * "foreground()" function
4355 */
4356 static void
4357f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4358{
4359#ifdef FEAT_GUI
4360 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004361 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004362 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004363 return;
4364 }
4365#endif
4366#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004367 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004368#endif
4369}
4370
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004371 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004372common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004373{
4374 char_u *s;
4375 char_u *name;
4376 int use_string = FALSE;
4377 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004378 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004379
4380 if (argvars[0].v_type == VAR_FUNC)
4381 {
4382 /* function(MyFunc, [arg], dict) */
4383 s = argvars[0].vval.v_string;
4384 }
4385 else if (argvars[0].v_type == VAR_PARTIAL
4386 && argvars[0].vval.v_partial != NULL)
4387 {
4388 /* function(dict.MyFunc, [arg]) */
4389 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004390 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004391 }
4392 else
4393 {
4394 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004395 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004396 use_string = TRUE;
4397 }
4398
Bram Moolenaar843b8842016-08-21 14:36:15 +02004399 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004400 {
4401 name = s;
4402 trans_name = trans_function_name(&name, FALSE,
4403 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4404 if (*name != NUL)
4405 s = NULL;
4406 }
4407
Bram Moolenaar843b8842016-08-21 14:36:15 +02004408 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4409 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004410 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004411 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004412 else if (trans_name != NULL && (is_funcref
4413 ? find_func(trans_name) == NULL
4414 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004415 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004416 else
4417 {
4418 int dict_idx = 0;
4419 int arg_idx = 0;
4420 list_T *list = NULL;
4421
4422 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4423 {
4424 char sid_buf[25];
4425 int off = *s == 's' ? 2 : 5;
4426
4427 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4428 * also be called from another script. Using trans_function_name()
4429 * would also work, but some plugins depend on the name being
4430 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004431 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004432 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004433 if (name != NULL)
4434 {
4435 STRCPY(name, sid_buf);
4436 STRCAT(name, s + off);
4437 }
4438 }
4439 else
4440 name = vim_strsave(s);
4441
4442 if (argvars[1].v_type != VAR_UNKNOWN)
4443 {
4444 if (argvars[2].v_type != VAR_UNKNOWN)
4445 {
4446 /* function(name, [args], dict) */
4447 arg_idx = 1;
4448 dict_idx = 2;
4449 }
4450 else if (argvars[1].v_type == VAR_DICT)
4451 /* function(name, dict) */
4452 dict_idx = 1;
4453 else
4454 /* function(name, [args]) */
4455 arg_idx = 1;
4456 if (dict_idx > 0)
4457 {
4458 if (argvars[dict_idx].v_type != VAR_DICT)
4459 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004460 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004461 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004462 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004463 }
4464 if (argvars[dict_idx].vval.v_dict == NULL)
4465 dict_idx = 0;
4466 }
4467 if (arg_idx > 0)
4468 {
4469 if (argvars[arg_idx].v_type != VAR_LIST)
4470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004471 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004472 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004473 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004474 }
4475 list = argvars[arg_idx].vval.v_list;
4476 if (list == NULL || list->lv_len == 0)
4477 arg_idx = 0;
4478 }
4479 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004480 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004481 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004482 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004483
4484 /* result is a VAR_PARTIAL */
4485 if (pt == NULL)
4486 vim_free(name);
4487 else
4488 {
4489 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4490 {
4491 listitem_T *li;
4492 int i = 0;
4493 int arg_len = 0;
4494 int lv_len = 0;
4495
4496 if (arg_pt != NULL)
4497 arg_len = arg_pt->pt_argc;
4498 if (list != NULL)
4499 lv_len = list->lv_len;
4500 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004501 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004502 if (pt->pt_argv == NULL)
4503 {
4504 vim_free(pt);
4505 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004506 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004507 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004508 for (i = 0; i < arg_len; i++)
4509 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4510 if (lv_len > 0)
4511 for (li = list->lv_first; li != NULL;
4512 li = li->li_next)
4513 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004514 }
4515
4516 /* For "function(dict.func, [], dict)" and "func" is a partial
4517 * use "dict". That is backwards compatible. */
4518 if (dict_idx > 0)
4519 {
4520 /* The dict is bound explicitly, pt_auto is FALSE. */
4521 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4522 ++pt->pt_dict->dv_refcount;
4523 }
4524 else if (arg_pt != NULL)
4525 {
4526 /* If the dict was bound automatically the result is also
4527 * bound automatically. */
4528 pt->pt_dict = arg_pt->pt_dict;
4529 pt->pt_auto = arg_pt->pt_auto;
4530 if (pt->pt_dict != NULL)
4531 ++pt->pt_dict->dv_refcount;
4532 }
4533
4534 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004535 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4536 {
4537 pt->pt_func = arg_pt->pt_func;
4538 func_ptr_ref(pt->pt_func);
4539 vim_free(name);
4540 }
4541 else if (is_funcref)
4542 {
4543 pt->pt_func = find_func(trans_name);
4544 func_ptr_ref(pt->pt_func);
4545 vim_free(name);
4546 }
4547 else
4548 {
4549 pt->pt_name = name;
4550 func_ref(name);
4551 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004552 }
4553 rettv->v_type = VAR_PARTIAL;
4554 rettv->vval.v_partial = pt;
4555 }
4556 else
4557 {
4558 /* result is a VAR_FUNC */
4559 rettv->v_type = VAR_FUNC;
4560 rettv->vval.v_string = name;
4561 func_ref(name);
4562 }
4563 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004564theend:
4565 vim_free(trans_name);
4566}
4567
4568/*
4569 * "funcref()" function
4570 */
4571 static void
4572f_funcref(typval_T *argvars, typval_T *rettv)
4573{
4574 common_function(argvars, rettv, TRUE);
4575}
4576
4577/*
4578 * "function()" function
4579 */
4580 static void
4581f_function(typval_T *argvars, typval_T *rettv)
4582{
4583 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004584}
4585
4586/*
4587 * "garbagecollect()" function
4588 */
4589 static void
4590f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4591{
4592 /* This is postponed until we are back at the toplevel, because we may be
4593 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4594 want_garbage_collect = TRUE;
4595
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004596 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004597 garbage_collect_at_exit = TRUE;
4598}
4599
4600/*
4601 * "get()" function
4602 */
4603 static void
4604f_get(typval_T *argvars, typval_T *rettv)
4605{
4606 listitem_T *li;
4607 list_T *l;
4608 dictitem_T *di;
4609 dict_T *d;
4610 typval_T *tv = NULL;
4611
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004612 if (argvars[0].v_type == VAR_BLOB)
4613 {
4614 int error = FALSE;
4615 int idx = tv_get_number_chk(&argvars[1], &error);
4616
4617 if (!error)
4618 {
4619 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004620 if (idx < 0)
4621 idx = blob_len(argvars[0].vval.v_blob) + idx;
4622 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4623 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004624 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004625 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004626 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004627 tv = rettv;
4628 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004629 }
4630 }
4631 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004632 {
4633 if ((l = argvars[0].vval.v_list) != NULL)
4634 {
4635 int error = FALSE;
4636
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004637 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004638 if (!error && li != NULL)
4639 tv = &li->li_tv;
4640 }
4641 }
4642 else if (argvars[0].v_type == VAR_DICT)
4643 {
4644 if ((d = argvars[0].vval.v_dict) != NULL)
4645 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004646 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004647 if (di != NULL)
4648 tv = &di->di_tv;
4649 }
4650 }
4651 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4652 {
4653 partial_T *pt;
4654 partial_T fref_pt;
4655
4656 if (argvars[0].v_type == VAR_PARTIAL)
4657 pt = argvars[0].vval.v_partial;
4658 else
4659 {
4660 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4661 fref_pt.pt_name = argvars[0].vval.v_string;
4662 pt = &fref_pt;
4663 }
4664
4665 if (pt != NULL)
4666 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004667 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004668 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004669
4670 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4671 {
4672 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004673 n = partial_name(pt);
4674 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004675 rettv->vval.v_string = NULL;
4676 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004677 {
4678 rettv->vval.v_string = vim_strsave(n);
4679 if (rettv->v_type == VAR_FUNC)
4680 func_ref(rettv->vval.v_string);
4681 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004682 }
4683 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004684 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004685 else if (STRCMP(what, "args") == 0)
4686 {
4687 rettv->v_type = VAR_LIST;
4688 if (rettv_list_alloc(rettv) == OK)
4689 {
4690 int i;
4691
4692 for (i = 0; i < pt->pt_argc; ++i)
4693 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4694 }
4695 }
4696 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004697 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004698 return;
4699 }
4700 }
4701 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004702 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004703
4704 if (tv == NULL)
4705 {
4706 if (argvars[2].v_type != VAR_UNKNOWN)
4707 copy_tv(&argvars[2], rettv);
4708 }
4709 else
4710 copy_tv(tv, rettv);
4711}
4712
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004713/*
4714 * Returns buffer options, variables and other attributes in a dictionary.
4715 */
4716 static dict_T *
4717get_buffer_info(buf_T *buf)
4718{
4719 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004720 tabpage_T *tp;
4721 win_T *wp;
4722 list_T *windows;
4723
4724 dict = dict_alloc();
4725 if (dict == NULL)
4726 return NULL;
4727
Bram Moolenaare0be1672018-07-08 16:50:37 +02004728 dict_add_number(dict, "bufnr", buf->b_fnum);
4729 dict_add_string(dict, "name", buf->b_ffname);
4730 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4731 : buflist_findlnum(buf));
4732 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4733 dict_add_number(dict, "listed", buf->b_p_bl);
4734 dict_add_number(dict, "changed", bufIsChanged(buf));
4735 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4736 dict_add_number(dict, "hidden",
4737 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004738
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004739 /* Get a reference to buffer variables */
4740 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004741
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004742 /* List of windows displaying this buffer */
4743 windows = list_alloc();
4744 if (windows != NULL)
4745 {
4746 FOR_ALL_TAB_WINDOWS(tp, wp)
4747 if (wp->w_buffer == buf)
4748 list_append_number(windows, (varnumber_T)wp->w_id);
4749 dict_add_list(dict, "windows", windows);
4750 }
4751
4752#ifdef FEAT_SIGNS
4753 if (buf->b_signlist != NULL)
4754 {
4755 /* List of signs placed in this buffer */
4756 list_T *signs = list_alloc();
4757 if (signs != NULL)
4758 {
4759 get_buffer_signs(buf, signs);
4760 dict_add_list(dict, "signs", signs);
4761 }
4762 }
4763#endif
4764
4765 return dict;
4766}
4767
4768/*
4769 * "getbufinfo()" function
4770 */
4771 static void
4772f_getbufinfo(typval_T *argvars, typval_T *rettv)
4773{
4774 buf_T *buf = NULL;
4775 buf_T *argbuf = NULL;
4776 dict_T *d;
4777 int filtered = FALSE;
4778 int sel_buflisted = FALSE;
4779 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004780 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004781
4782 if (rettv_list_alloc(rettv) != OK)
4783 return;
4784
4785 /* List of all the buffers or selected buffers */
4786 if (argvars[0].v_type == VAR_DICT)
4787 {
4788 dict_T *sel_d = argvars[0].vval.v_dict;
4789
4790 if (sel_d != NULL)
4791 {
4792 dictitem_T *di;
4793
4794 filtered = TRUE;
4795
4796 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004797 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004798 sel_buflisted = TRUE;
4799
4800 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004801 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004802 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004803
4804 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004805 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004806 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004807 }
4808 }
4809 else if (argvars[0].v_type != VAR_UNKNOWN)
4810 {
4811 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004812 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004813 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004814 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004815 --emsg_off;
4816 if (argbuf == NULL)
4817 return;
4818 }
4819
4820 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004821 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004822 {
4823 if (argbuf != NULL && argbuf != buf)
4824 continue;
4825 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004826 || (sel_buflisted && !buf->b_p_bl)
4827 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004828 continue;
4829
4830 d = get_buffer_info(buf);
4831 if (d != NULL)
4832 list_append_dict(rettv->vval.v_list, d);
4833 if (argbuf != NULL)
4834 return;
4835 }
4836}
4837
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004838/*
4839 * Get line or list of lines from buffer "buf" into "rettv".
4840 * Return a range (from start to end) of lines in rettv from the specified
4841 * buffer.
4842 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4843 */
4844 static void
4845get_buffer_lines(
4846 buf_T *buf,
4847 linenr_T start,
4848 linenr_T end,
4849 int retlist,
4850 typval_T *rettv)
4851{
4852 char_u *p;
4853
4854 rettv->v_type = VAR_STRING;
4855 rettv->vval.v_string = NULL;
4856 if (retlist && rettv_list_alloc(rettv) == FAIL)
4857 return;
4858
4859 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4860 return;
4861
4862 if (!retlist)
4863 {
4864 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4865 p = ml_get_buf(buf, start, FALSE);
4866 else
4867 p = (char_u *)"";
4868 rettv->vval.v_string = vim_strsave(p);
4869 }
4870 else
4871 {
4872 if (end < start)
4873 return;
4874
4875 if (start < 1)
4876 start = 1;
4877 if (end > buf->b_ml.ml_line_count)
4878 end = buf->b_ml.ml_line_count;
4879 while (start <= end)
4880 if (list_append_string(rettv->vval.v_list,
4881 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4882 break;
4883 }
4884}
4885
4886/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004887 * "getbufline()" function
4888 */
4889 static void
4890f_getbufline(typval_T *argvars, typval_T *rettv)
4891{
4892 linenr_T lnum;
4893 linenr_T end;
4894 buf_T *buf;
4895
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004896 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004897 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004898 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004899 --emsg_off;
4900
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004901 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004902 if (argvars[2].v_type == VAR_UNKNOWN)
4903 end = lnum;
4904 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004905 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004906
4907 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4908}
4909
4910/*
4911 * "getbufvar()" function
4912 */
4913 static void
4914f_getbufvar(typval_T *argvars, typval_T *rettv)
4915{
4916 buf_T *buf;
4917 buf_T *save_curbuf;
4918 char_u *varname;
4919 dictitem_T *v;
4920 int done = FALSE;
4921
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004922 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4923 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004924 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004925 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004926
4927 rettv->v_type = VAR_STRING;
4928 rettv->vval.v_string = NULL;
4929
4930 if (buf != NULL && varname != NULL)
4931 {
4932 /* set curbuf to be our buf, temporarily */
4933 save_curbuf = curbuf;
4934 curbuf = buf;
4935
Bram Moolenaar30567352016-08-27 21:25:44 +02004936 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004937 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004938 if (varname[1] == NUL)
4939 {
4940 /* get all buffer-local options in a dict */
4941 dict_T *opts = get_winbuf_options(TRUE);
4942
4943 if (opts != NULL)
4944 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004945 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004946 done = TRUE;
4947 }
4948 }
4949 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4950 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004951 done = TRUE;
4952 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004953 else
4954 {
4955 /* Look up the variable. */
4956 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4957 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4958 'b', varname, FALSE);
4959 if (v != NULL)
4960 {
4961 copy_tv(&v->di_tv, rettv);
4962 done = TRUE;
4963 }
4964 }
4965
4966 /* restore previous notion of curbuf */
4967 curbuf = save_curbuf;
4968 }
4969
4970 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4971 /* use the default value */
4972 copy_tv(&argvars[2], rettv);
4973
4974 --emsg_off;
4975}
4976
4977/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004978 * "getchangelist()" function
4979 */
4980 static void
4981f_getchangelist(typval_T *argvars, typval_T *rettv)
4982{
4983#ifdef FEAT_JUMPLIST
4984 buf_T *buf;
4985 int i;
4986 list_T *l;
4987 dict_T *d;
4988#endif
4989
4990 if (rettv_list_alloc(rettv) != OK)
4991 return;
4992
4993#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004994 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004995 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004996 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004997 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004998 if (buf == NULL)
4999 return;
5000
5001 l = list_alloc();
5002 if (l == NULL)
5003 return;
5004
5005 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5006 return;
5007 /*
5008 * The current window change list index tracks only the position in the
5009 * current buffer change list. For other buffers, use the change list
5010 * length as the current index.
5011 */
5012 list_append_number(rettv->vval.v_list,
5013 (varnumber_T)((buf == curwin->w_buffer)
5014 ? curwin->w_changelistidx : buf->b_changelistlen));
5015
5016 for (i = 0; i < buf->b_changelistlen; ++i)
5017 {
5018 if (buf->b_changelist[i].lnum == 0)
5019 continue;
5020 if ((d = dict_alloc()) == NULL)
5021 return;
5022 if (list_append_dict(l, d) == FAIL)
5023 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005024 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5025 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005026 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005027 }
5028#endif
5029}
5030/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005031 * "getchar()" function
5032 */
5033 static void
5034f_getchar(typval_T *argvars, typval_T *rettv)
5035{
5036 varnumber_T n;
5037 int error = FALSE;
5038
Bram Moolenaar84d93902018-09-11 20:10:20 +02005039#ifdef MESSAGE_QUEUE
5040 // vpeekc() used to check for messages, but that caused problems, invoking
5041 // a callback where it was not expected. Some plugins use getchar(1) in a
5042 // loop to await a message, therefore make sure we check for messages here.
5043 parse_queued_messages();
5044#endif
5045
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005046 /* Position the cursor. Needed after a message that ends in a space. */
5047 windgoto(msg_row, msg_col);
5048
5049 ++no_mapping;
5050 ++allow_keys;
5051 for (;;)
5052 {
5053 if (argvars[0].v_type == VAR_UNKNOWN)
5054 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005055 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005056 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005057 /* getchar(1): only check if char avail */
5058 n = vpeekc_any();
5059 else if (error || vpeekc_any() == NUL)
5060 /* illegal argument or getchar(0) and no char avail: return zero */
5061 n = 0;
5062 else
5063 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005064 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005065
5066 if (n == K_IGNORE)
5067 continue;
5068 break;
5069 }
5070 --no_mapping;
5071 --allow_keys;
5072
5073 set_vim_var_nr(VV_MOUSE_WIN, 0);
5074 set_vim_var_nr(VV_MOUSE_WINID, 0);
5075 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5076 set_vim_var_nr(VV_MOUSE_COL, 0);
5077
5078 rettv->vval.v_number = n;
5079 if (IS_SPECIAL(n) || mod_mask != 0)
5080 {
5081 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5082 int i = 0;
5083
5084 /* Turn a special key into three bytes, plus modifier. */
5085 if (mod_mask != 0)
5086 {
5087 temp[i++] = K_SPECIAL;
5088 temp[i++] = KS_MODIFIER;
5089 temp[i++] = mod_mask;
5090 }
5091 if (IS_SPECIAL(n))
5092 {
5093 temp[i++] = K_SPECIAL;
5094 temp[i++] = K_SECOND(n);
5095 temp[i++] = K_THIRD(n);
5096 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005097 else if (has_mbyte)
5098 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099 else
5100 temp[i++] = n;
5101 temp[i++] = NUL;
5102 rettv->v_type = VAR_STRING;
5103 rettv->vval.v_string = vim_strsave(temp);
5104
5105#ifdef FEAT_MOUSE
5106 if (is_mouse_key(n))
5107 {
5108 int row = mouse_row;
5109 int col = mouse_col;
5110 win_T *win;
5111 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005113 int winnr = 1;
5114
5115 if (row >= 0 && col >= 0)
5116 {
5117 /* Find the window at the mouse coordinates and compute the
5118 * text position. */
5119 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005120 if (win == NULL)
5121 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005122 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005123 for (wp = firstwin; wp != win; wp = wp->w_next)
5124 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005125 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5126 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5127 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5128 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5129 }
5130 }
5131#endif
5132 }
5133}
5134
5135/*
5136 * "getcharmod()" function
5137 */
5138 static void
5139f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5140{
5141 rettv->vval.v_number = mod_mask;
5142}
5143
5144/*
5145 * "getcharsearch()" function
5146 */
5147 static void
5148f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5149{
5150 if (rettv_dict_alloc(rettv) != FAIL)
5151 {
5152 dict_T *dict = rettv->vval.v_dict;
5153
Bram Moolenaare0be1672018-07-08 16:50:37 +02005154 dict_add_string(dict, "char", last_csearch());
5155 dict_add_number(dict, "forward", last_csearch_forward());
5156 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005157 }
5158}
5159
5160/*
5161 * "getcmdline()" function
5162 */
5163 static void
5164f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5165{
5166 rettv->v_type = VAR_STRING;
5167 rettv->vval.v_string = get_cmdline_str();
5168}
5169
5170/*
5171 * "getcmdpos()" function
5172 */
5173 static void
5174f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5175{
5176 rettv->vval.v_number = get_cmdline_pos() + 1;
5177}
5178
5179/*
5180 * "getcmdtype()" function
5181 */
5182 static void
5183f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5184{
5185 rettv->v_type = VAR_STRING;
5186 rettv->vval.v_string = alloc(2);
5187 if (rettv->vval.v_string != NULL)
5188 {
5189 rettv->vval.v_string[0] = get_cmdline_type();
5190 rettv->vval.v_string[1] = NUL;
5191 }
5192}
5193
5194/*
5195 * "getcmdwintype()" function
5196 */
5197 static void
5198f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5199{
5200 rettv->v_type = VAR_STRING;
5201 rettv->vval.v_string = NULL;
5202#ifdef FEAT_CMDWIN
5203 rettv->vval.v_string = alloc(2);
5204 if (rettv->vval.v_string != NULL)
5205 {
5206 rettv->vval.v_string[0] = cmdwin_type;
5207 rettv->vval.v_string[1] = NUL;
5208 }
5209#endif
5210}
5211
5212#if defined(FEAT_CMDL_COMPL)
5213/*
5214 * "getcompletion()" function
5215 */
5216 static void
5217f_getcompletion(typval_T *argvars, typval_T *rettv)
5218{
5219 char_u *pat;
5220 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005221 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005222 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5223 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005224
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005225 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005226 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005227
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005228 if (p_wic)
5229 options |= WILD_ICASE;
5230
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005231 /* For filtered results, 'wildignore' is used */
5232 if (!filtered)
5233 options |= WILD_KEEP_ALL;
5234
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005235 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005236 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005237 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005238 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005239 if (xpc.xp_context == EXPAND_NOTHING)
5240 {
5241 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005242 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005243 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005244 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005245 return;
5246 }
5247
5248# if defined(FEAT_MENU)
5249 if (xpc.xp_context == EXPAND_MENUS)
5250 {
5251 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5252 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5253 }
5254# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005255#ifdef FEAT_CSCOPE
5256 if (xpc.xp_context == EXPAND_CSCOPE)
5257 {
5258 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5259 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5260 }
5261#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005262#ifdef FEAT_SIGNS
5263 if (xpc.xp_context == EXPAND_SIGN)
5264 {
5265 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5266 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5267 }
5268#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005269
5270 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5271 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5272 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005273 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005274
5275 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5276
5277 for (i = 0; i < xpc.xp_numfiles; i++)
5278 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5279 }
5280 vim_free(pat);
5281 ExpandCleanup(&xpc);
5282}
5283#endif
5284
5285/*
5286 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005287 *
5288 * Return the current working directory of a window in a tab page.
5289 * First optional argument 'winnr' is the window number or -1 and the second
5290 * optional argument 'tabnr' is the tab page number.
5291 *
5292 * If no arguments are supplied, then return the directory of the current
5293 * window.
5294 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5295 * the specified window.
5296 * If 'winnr' is 0 then return the directory of the current window.
5297 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5298 * directory of the specified tab page. Otherwise return the directory of the
5299 * specified window in the specified tab page.
5300 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005301 */
5302 static void
5303f_getcwd(typval_T *argvars, typval_T *rettv)
5304{
5305 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005306 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005307 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005308 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005309
5310 rettv->v_type = VAR_STRING;
5311 rettv->vval.v_string = NULL;
5312
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005313 if (argvars[0].v_type == VAR_NUMBER
5314 && argvars[0].vval.v_number == -1
5315 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005316 global = TRUE;
5317 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005318 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005319
5320 if (wp != NULL && wp->w_localdir != NULL)
5321 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005322 else if (tp != NULL && tp->tp_localdir != NULL)
5323 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5324 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005326 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005327 rettv->vval.v_string = vim_strsave(globaldir);
5328 else
5329 {
5330 cwd = alloc(MAXPATHL);
5331 if (cwd != NULL)
5332 {
5333 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5334 rettv->vval.v_string = vim_strsave(cwd);
5335 vim_free(cwd);
5336 }
5337 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005338 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005339#ifdef BACKSLASH_IN_FILENAME
5340 if (rettv->vval.v_string != NULL)
5341 slash_adjust(rettv->vval.v_string);
5342#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005343}
5344
5345/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005346 * "getenv()" function
5347 */
5348 static void
5349f_getenv(typval_T *argvars, typval_T *rettv)
5350{
5351 int mustfree = FALSE;
5352 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5353
5354 if (p == NULL)
5355 {
5356 rettv->v_type = VAR_SPECIAL;
5357 rettv->vval.v_number = VVAL_NULL;
5358 return;
5359 }
5360 if (!mustfree)
5361 p = vim_strsave(p);
5362 rettv->vval.v_string = p;
5363 rettv->v_type = VAR_STRING;
5364}
5365
5366/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005367 * "getfontname()" function
5368 */
5369 static void
5370f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5371{
5372 rettv->v_type = VAR_STRING;
5373 rettv->vval.v_string = NULL;
5374#ifdef FEAT_GUI
5375 if (gui.in_use)
5376 {
5377 GuiFont font;
5378 char_u *name = NULL;
5379
5380 if (argvars[0].v_type == VAR_UNKNOWN)
5381 {
5382 /* Get the "Normal" font. Either the name saved by
5383 * hl_set_font_name() or from the font ID. */
5384 font = gui.norm_font;
5385 name = hl_get_font_name();
5386 }
5387 else
5388 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005389 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005390 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5391 return;
5392 font = gui_mch_get_font(name, FALSE);
5393 if (font == NOFONT)
5394 return; /* Invalid font name, return empty string. */
5395 }
5396 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5397 if (argvars[0].v_type != VAR_UNKNOWN)
5398 gui_mch_free_font(font);
5399 }
5400#endif
5401}
5402
5403/*
5404 * "getfperm({fname})" function
5405 */
5406 static void
5407f_getfperm(typval_T *argvars, typval_T *rettv)
5408{
5409 char_u *fname;
5410 stat_T st;
5411 char_u *perm = NULL;
5412 char_u flags[] = "rwx";
5413 int i;
5414
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005415 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005416
5417 rettv->v_type = VAR_STRING;
5418 if (mch_stat((char *)fname, &st) >= 0)
5419 {
5420 perm = vim_strsave((char_u *)"---------");
5421 if (perm != NULL)
5422 {
5423 for (i = 0; i < 9; i++)
5424 {
5425 if (st.st_mode & (1 << (8 - i)))
5426 perm[i] = flags[i % 3];
5427 }
5428 }
5429 }
5430 rettv->vval.v_string = perm;
5431}
5432
5433/*
5434 * "getfsize({fname})" function
5435 */
5436 static void
5437f_getfsize(typval_T *argvars, typval_T *rettv)
5438{
5439 char_u *fname;
5440 stat_T st;
5441
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005442 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005443
5444 rettv->v_type = VAR_NUMBER;
5445
5446 if (mch_stat((char *)fname, &st) >= 0)
5447 {
5448 if (mch_isdir(fname))
5449 rettv->vval.v_number = 0;
5450 else
5451 {
5452 rettv->vval.v_number = (varnumber_T)st.st_size;
5453
5454 /* non-perfect check for overflow */
5455 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5456 rettv->vval.v_number = -2;
5457 }
5458 }
5459 else
5460 rettv->vval.v_number = -1;
5461}
5462
5463/*
5464 * "getftime({fname})" function
5465 */
5466 static void
5467f_getftime(typval_T *argvars, typval_T *rettv)
5468{
5469 char_u *fname;
5470 stat_T st;
5471
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005472 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005473
5474 if (mch_stat((char *)fname, &st) >= 0)
5475 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5476 else
5477 rettv->vval.v_number = -1;
5478}
5479
5480/*
5481 * "getftype({fname})" function
5482 */
5483 static void
5484f_getftype(typval_T *argvars, typval_T *rettv)
5485{
5486 char_u *fname;
5487 stat_T st;
5488 char_u *type = NULL;
5489 char *t;
5490
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005491 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005492
5493 rettv->v_type = VAR_STRING;
5494 if (mch_lstat((char *)fname, &st) >= 0)
5495 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005496 if (S_ISREG(st.st_mode))
5497 t = "file";
5498 else if (S_ISDIR(st.st_mode))
5499 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005500 else if (S_ISLNK(st.st_mode))
5501 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005502 else if (S_ISBLK(st.st_mode))
5503 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005504 else if (S_ISCHR(st.st_mode))
5505 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005506 else if (S_ISFIFO(st.st_mode))
5507 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005508 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005509 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005510 else
5511 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005512 type = vim_strsave((char_u *)t);
5513 }
5514 rettv->vval.v_string = type;
5515}
5516
5517/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005518 * "getjumplist()" function
5519 */
5520 static void
5521f_getjumplist(typval_T *argvars, typval_T *rettv)
5522{
5523#ifdef FEAT_JUMPLIST
5524 win_T *wp;
5525 int i;
5526 list_T *l;
5527 dict_T *d;
5528#endif
5529
5530 if (rettv_list_alloc(rettv) != OK)
5531 return;
5532
5533#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005534 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005535 if (wp == NULL)
5536 return;
5537
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005538 cleanup_jumplist(wp, TRUE);
5539
Bram Moolenaar4f505882018-02-10 21:06:32 +01005540 l = list_alloc();
5541 if (l == NULL)
5542 return;
5543
5544 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5545 return;
5546 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5547
5548 for (i = 0; i < wp->w_jumplistlen; ++i)
5549 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005550 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5551 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005552 if ((d = dict_alloc()) == NULL)
5553 return;
5554 if (list_append_dict(l, d) == FAIL)
5555 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005556 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5557 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005558 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005559 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005560 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005561 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005562 }
5563#endif
5564}
5565
5566/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005567 * "getline(lnum, [end])" function
5568 */
5569 static void
5570f_getline(typval_T *argvars, typval_T *rettv)
5571{
5572 linenr_T lnum;
5573 linenr_T end;
5574 int retlist;
5575
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005576 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005577 if (argvars[1].v_type == VAR_UNKNOWN)
5578 {
5579 end = 0;
5580 retlist = FALSE;
5581 }
5582 else
5583 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005584 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005585 retlist = TRUE;
5586 }
5587
5588 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5589}
5590
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005591#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005592 static void
5593get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5594{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005595 if (what_arg->v_type == VAR_UNKNOWN)
5596 {
5597 if (rettv_list_alloc(rettv) == OK)
5598 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005599 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005600 }
5601 else
5602 {
5603 if (rettv_dict_alloc(rettv) == OK)
5604 if (is_qf || (wp != NULL))
5605 {
5606 if (what_arg->v_type == VAR_DICT)
5607 {
5608 dict_T *d = what_arg->vval.v_dict;
5609
5610 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005611 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005612 }
5613 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005614 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005615 }
5616 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005617}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005618#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005619
5620/*
5621 * "getloclist()" function
5622 */
5623 static void
5624f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5625{
5626#ifdef FEAT_QUICKFIX
5627 win_T *wp;
5628
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005629 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005630 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5631#endif
5632}
5633
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005634/*
5635 * "getmatches()" function
5636 */
5637 static void
5638f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5639{
5640#ifdef FEAT_SEARCH_EXTRA
5641 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005642 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005643 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005644 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005645
Bram Moolenaaraff74912019-03-30 18:11:49 +01005646 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5647 return;
5648
5649 cur = win->w_match_head;
5650 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005651 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005652 dict = dict_alloc();
5653 if (dict == NULL)
5654 return;
5655 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005656 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005657 /* match added with matchaddpos() */
5658 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005659 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005660 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005661 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005662 list_T *l;
5663
5664 llpos = &cur->pos.pos[i];
5665 if (llpos->lnum == 0)
5666 break;
5667 l = list_alloc();
5668 if (l == NULL)
5669 break;
5670 list_append_number(l, (varnumber_T)llpos->lnum);
5671 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005672 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005673 list_append_number(l, (varnumber_T)llpos->col);
5674 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005675 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005676 sprintf(buf, "pos%d", i + 1);
5677 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005678 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005679 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005680 else
5681 {
5682 dict_add_string(dict, "pattern", cur->pattern);
5683 }
5684 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5685 dict_add_number(dict, "priority", (long)cur->priority);
5686 dict_add_number(dict, "id", (long)cur->id);
5687# if defined(FEAT_CONCEAL)
5688 if (cur->conceal_char)
5689 {
5690 char_u buf[MB_MAXBYTES + 1];
5691
5692 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5693 dict_add_string(dict, "conceal", (char_u *)&buf);
5694 }
5695# endif
5696 list_append_dict(rettv->vval.v_list, dict);
5697 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005698 }
5699#endif
5700}
5701
5702/*
5703 * "getpid()" function
5704 */
5705 static void
5706f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5707{
5708 rettv->vval.v_number = mch_get_pid();
5709}
5710
5711 static void
5712getpos_both(
5713 typval_T *argvars,
5714 typval_T *rettv,
5715 int getcurpos)
5716{
5717 pos_T *fp;
5718 list_T *l;
5719 int fnum = -1;
5720
5721 if (rettv_list_alloc(rettv) == OK)
5722 {
5723 l = rettv->vval.v_list;
5724 if (getcurpos)
5725 fp = &curwin->w_cursor;
5726 else
5727 fp = var2fpos(&argvars[0], TRUE, &fnum);
5728 if (fnum != -1)
5729 list_append_number(l, (varnumber_T)fnum);
5730 else
5731 list_append_number(l, (varnumber_T)0);
5732 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5733 : (varnumber_T)0);
5734 list_append_number(l, (fp != NULL)
5735 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5736 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005737 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005738 (varnumber_T)0);
5739 if (getcurpos)
5740 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005741 int save_set_curswant = curwin->w_set_curswant;
5742 colnr_T save_curswant = curwin->w_curswant;
5743 colnr_T save_virtcol = curwin->w_virtcol;
5744
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005745 update_curswant();
5746 list_append_number(l, curwin->w_curswant == MAXCOL ?
5747 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005748
5749 // Do not change "curswant", as it is unexpected that a get
5750 // function has a side effect.
5751 if (save_set_curswant)
5752 {
5753 curwin->w_set_curswant = save_set_curswant;
5754 curwin->w_curswant = save_curswant;
5755 curwin->w_virtcol = save_virtcol;
5756 curwin->w_valid &= ~VALID_VIRTCOL;
5757 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005758 }
5759 }
5760 else
5761 rettv->vval.v_number = FALSE;
5762}
5763
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005764/*
5765 * "getcurpos()" function
5766 */
5767 static void
5768f_getcurpos(typval_T *argvars, typval_T *rettv)
5769{
5770 getpos_both(argvars, rettv, TRUE);
5771}
5772
5773/*
5774 * "getpos(string)" function
5775 */
5776 static void
5777f_getpos(typval_T *argvars, typval_T *rettv)
5778{
5779 getpos_both(argvars, rettv, FALSE);
5780}
5781
5782/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005783 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005784 */
5785 static void
5786f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5787{
5788#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005789 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005790#endif
5791}
5792
5793/*
5794 * "getreg()" function
5795 */
5796 static void
5797f_getreg(typval_T *argvars, typval_T *rettv)
5798{
5799 char_u *strregname;
5800 int regname;
5801 int arg2 = FALSE;
5802 int return_list = FALSE;
5803 int error = FALSE;
5804
5805 if (argvars[0].v_type != VAR_UNKNOWN)
5806 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005807 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005808 error = strregname == NULL;
5809 if (argvars[1].v_type != VAR_UNKNOWN)
5810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005811 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005812 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005813 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005814 }
5815 }
5816 else
5817 strregname = get_vim_var_str(VV_REG);
5818
5819 if (error)
5820 return;
5821
5822 regname = (strregname == NULL ? '"' : *strregname);
5823 if (regname == 0)
5824 regname = '"';
5825
5826 if (return_list)
5827 {
5828 rettv->v_type = VAR_LIST;
5829 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5830 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5831 if (rettv->vval.v_list == NULL)
5832 (void)rettv_list_alloc(rettv);
5833 else
5834 ++rettv->vval.v_list->lv_refcount;
5835 }
5836 else
5837 {
5838 rettv->v_type = VAR_STRING;
5839 rettv->vval.v_string = get_reg_contents(regname,
5840 arg2 ? GREG_EXPR_SRC : 0);
5841 }
5842}
5843
5844/*
5845 * "getregtype()" function
5846 */
5847 static void
5848f_getregtype(typval_T *argvars, typval_T *rettv)
5849{
5850 char_u *strregname;
5851 int regname;
5852 char_u buf[NUMBUFLEN + 2];
5853 long reglen = 0;
5854
5855 if (argvars[0].v_type != VAR_UNKNOWN)
5856 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005857 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858 if (strregname == NULL) /* type error; errmsg already given */
5859 {
5860 rettv->v_type = VAR_STRING;
5861 rettv->vval.v_string = NULL;
5862 return;
5863 }
5864 }
5865 else
5866 /* Default to v:register */
5867 strregname = get_vim_var_str(VV_REG);
5868
5869 regname = (strregname == NULL ? '"' : *strregname);
5870 if (regname == 0)
5871 regname = '"';
5872
5873 buf[0] = NUL;
5874 buf[1] = NUL;
5875 switch (get_reg_type(regname, &reglen))
5876 {
5877 case MLINE: buf[0] = 'V'; break;
5878 case MCHAR: buf[0] = 'v'; break;
5879 case MBLOCK:
5880 buf[0] = Ctrl_V;
5881 sprintf((char *)buf + 1, "%ld", reglen + 1);
5882 break;
5883 }
5884 rettv->v_type = VAR_STRING;
5885 rettv->vval.v_string = vim_strsave(buf);
5886}
5887
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005888/*
5889 * Returns information (variables, options, etc.) about a tab page
5890 * as a dictionary.
5891 */
5892 static dict_T *
5893get_tabpage_info(tabpage_T *tp, int tp_idx)
5894{
5895 win_T *wp;
5896 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005897 list_T *l;
5898
5899 dict = dict_alloc();
5900 if (dict == NULL)
5901 return NULL;
5902
Bram Moolenaare0be1672018-07-08 16:50:37 +02005903 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005904
5905 l = list_alloc();
5906 if (l != NULL)
5907 {
5908 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5909 wp; wp = wp->w_next)
5910 list_append_number(l, (varnumber_T)wp->w_id);
5911 dict_add_list(dict, "windows", l);
5912 }
5913
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005914 /* Make a reference to tabpage variables */
5915 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005916
5917 return dict;
5918}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005919
5920/*
5921 * "gettabinfo()" function
5922 */
5923 static void
5924f_gettabinfo(typval_T *argvars, typval_T *rettv)
5925{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005926 tabpage_T *tp, *tparg = NULL;
5927 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005928 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005929
5930 if (rettv_list_alloc(rettv) != OK)
5931 return;
5932
5933 if (argvars[0].v_type != VAR_UNKNOWN)
5934 {
5935 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005936 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005937 if (tparg == NULL)
5938 return;
5939 }
5940
5941 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005942 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005943 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005944 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005945 if (tparg != NULL && tp != tparg)
5946 continue;
5947 d = get_tabpage_info(tp, tpnr);
5948 if (d != NULL)
5949 list_append_dict(rettv->vval.v_list, d);
5950 if (tparg != NULL)
5951 return;
5952 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005953}
5954
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005955/*
5956 * "gettabvar()" function
5957 */
5958 static void
5959f_gettabvar(typval_T *argvars, typval_T *rettv)
5960{
5961 win_T *oldcurwin;
5962 tabpage_T *tp, *oldtabpage;
5963 dictitem_T *v;
5964 char_u *varname;
5965 int done = FALSE;
5966
5967 rettv->v_type = VAR_STRING;
5968 rettv->vval.v_string = NULL;
5969
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005970 varname = tv_get_string_chk(&argvars[1]);
5971 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005972 if (tp != NULL && varname != NULL)
5973 {
5974 /* Set tp to be our tabpage, temporarily. Also set the window to the
5975 * first window in the tabpage, otherwise the window is not valid. */
5976 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005977 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5978 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005979 {
5980 /* look up the variable */
5981 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5982 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5983 if (v != NULL)
5984 {
5985 copy_tv(&v->di_tv, rettv);
5986 done = TRUE;
5987 }
5988 }
5989
5990 /* restore previous notion of curwin */
5991 restore_win(oldcurwin, oldtabpage, TRUE);
5992 }
5993
5994 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5995 copy_tv(&argvars[2], rettv);
5996}
5997
5998/*
5999 * "gettabwinvar()" function
6000 */
6001 static void
6002f_gettabwinvar(typval_T *argvars, typval_T *rettv)
6003{
6004 getwinvar(argvars, rettv, 1);
6005}
6006
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006007/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01006008 * "gettagstack()" function
6009 */
6010 static void
6011f_gettagstack(typval_T *argvars, typval_T *rettv)
6012{
6013 win_T *wp = curwin; // default is current window
6014
6015 if (rettv_dict_alloc(rettv) != OK)
6016 return;
6017
6018 if (argvars[0].v_type != VAR_UNKNOWN)
6019 {
6020 wp = find_win_by_nr_or_id(&argvars[0]);
6021 if (wp == NULL)
6022 return;
6023 }
6024
6025 get_tagstack(wp, rettv->vval.v_dict);
6026}
6027
6028/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006029 * Returns information about a window as a dictionary.
6030 */
6031 static dict_T *
6032get_win_info(win_T *wp, short tpnr, short winnr)
6033{
6034 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006035
6036 dict = dict_alloc();
6037 if (dict == NULL)
6038 return NULL;
6039
Bram Moolenaare0be1672018-07-08 16:50:37 +02006040 dict_add_number(dict, "tabnr", tpnr);
6041 dict_add_number(dict, "winnr", winnr);
6042 dict_add_number(dict, "winid", wp->w_id);
6043 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006044 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006045 dict_add_number(dict, "topline", wp->w_topline);
6046 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006047#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006048 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006049#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006050 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006051 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006052 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006053
Bram Moolenaar69905d12017-08-13 18:14:47 +02006054#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006055 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006056#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006057#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006058 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6059 dict_add_number(dict, "loclist",
6060 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006061#endif
6062
Bram Moolenaar30567352016-08-27 21:25:44 +02006063 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006064 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006065
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006066 return dict;
6067}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006068
6069/*
6070 * "getwininfo()" function
6071 */
6072 static void
6073f_getwininfo(typval_T *argvars, typval_T *rettv)
6074{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006075 tabpage_T *tp;
6076 win_T *wp = NULL, *wparg = NULL;
6077 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006078 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006079
6080 if (rettv_list_alloc(rettv) != OK)
6081 return;
6082
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006083 if (argvars[0].v_type != VAR_UNKNOWN)
6084 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006085 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006086 if (wparg == NULL)
6087 return;
6088 }
6089
6090 /* Collect information about either all the windows across all the tab
6091 * pages or one particular window.
6092 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006093 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006094 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006095 tabnr++;
6096 winnr = 0;
6097 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006098 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006099 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006100 if (wparg != NULL && wp != wparg)
6101 continue;
6102 d = get_win_info(wp, tabnr, winnr);
6103 if (d != NULL)
6104 list_append_dict(rettv->vval.v_list, d);
6105 if (wparg != NULL)
6106 /* found information about a specific window */
6107 return;
6108 }
6109 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006110}
6111
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006112/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006113 * "win_execute()" function
6114 */
6115 static void
6116f_win_execute(typval_T *argvars, typval_T *rettv)
6117{
6118 int id = (int)tv_get_number(argvars);
6119 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006120 win_T *save_curwin;
6121 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006122
6123 if (wp != NULL)
6124 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006125 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
6126 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006127 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006128 check_cursor();
6129 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006130 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006131 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006132 }
6133}
6134
6135/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006136 * "win_findbuf()" function
6137 */
6138 static void
6139f_win_findbuf(typval_T *argvars, typval_T *rettv)
6140{
6141 if (rettv_list_alloc(rettv) != FAIL)
6142 win_findbuf(argvars, rettv->vval.v_list);
6143}
6144
6145/*
6146 * "win_getid()" function
6147 */
6148 static void
6149f_win_getid(typval_T *argvars, typval_T *rettv)
6150{
6151 rettv->vval.v_number = win_getid(argvars);
6152}
6153
6154/*
6155 * "win_gotoid()" function
6156 */
6157 static void
6158f_win_gotoid(typval_T *argvars, typval_T *rettv)
6159{
6160 rettv->vval.v_number = win_gotoid(argvars);
6161}
6162
6163/*
6164 * "win_id2tabwin()" function
6165 */
6166 static void
6167f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6168{
6169 if (rettv_list_alloc(rettv) != FAIL)
6170 win_id2tabwin(argvars, rettv->vval.v_list);
6171}
6172
6173/*
6174 * "win_id2win()" function
6175 */
6176 static void
6177f_win_id2win(typval_T *argvars, typval_T *rettv)
6178{
6179 rettv->vval.v_number = win_id2win(argvars);
6180}
6181
6182/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006183 * "win_screenpos()" function
6184 */
6185 static void
6186f_win_screenpos(typval_T *argvars, typval_T *rettv)
6187{
6188 win_T *wp;
6189
6190 if (rettv_list_alloc(rettv) == FAIL)
6191 return;
6192
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006193 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006194 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6195 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6196}
6197
6198/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006199 * "getwinpos({timeout})" function
6200 */
6201 static void
6202f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6203{
6204 int x = -1;
6205 int y = -1;
6206
6207 if (rettv_list_alloc(rettv) == FAIL)
6208 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006209#if defined(FEAT_GUI) \
6210 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6211 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006212 {
6213 varnumber_T timeout = 100;
6214
6215 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006216 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006217
6218 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006219 }
6220#endif
6221 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6222 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6223}
6224
6225
6226/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006227 * "getwinposx()" function
6228 */
6229 static void
6230f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6231{
6232 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006233#if defined(FEAT_GUI) \
6234 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6235 || defined(MSWIN)
6236
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006237 {
6238 int x, y;
6239
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006240 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006241 rettv->vval.v_number = x;
6242 }
6243#endif
6244}
6245
6246/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247 * "getwinposy()" function
6248 */
6249 static void
6250f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6251{
6252 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006253#if defined(FEAT_GUI) \
6254 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6255 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006256 {
6257 int x, y;
6258
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006259 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006260 rettv->vval.v_number = y;
6261 }
6262#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006263}
6264
6265/*
6266 * "getwinvar()" function
6267 */
6268 static void
6269f_getwinvar(typval_T *argvars, typval_T *rettv)
6270{
6271 getwinvar(argvars, rettv, 0);
6272}
6273
6274/*
6275 * "glob()" function
6276 */
6277 static void
6278f_glob(typval_T *argvars, typval_T *rettv)
6279{
6280 int options = WILD_SILENT|WILD_USE_NL;
6281 expand_T xpc;
6282 int error = FALSE;
6283
6284 /* When the optional second argument is non-zero, don't remove matches
6285 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6286 rettv->v_type = VAR_STRING;
6287 if (argvars[1].v_type != VAR_UNKNOWN)
6288 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006289 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006290 options |= WILD_KEEP_ALL;
6291 if (argvars[2].v_type != VAR_UNKNOWN)
6292 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006293 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006294 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006295 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006296 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297 options |= WILD_ALLLINKS;
6298 }
6299 }
6300 if (!error)
6301 {
6302 ExpandInit(&xpc);
6303 xpc.xp_context = EXPAND_FILES;
6304 if (p_wic)
6305 options += WILD_ICASE;
6306 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006307 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308 NULL, options, WILD_ALL);
6309 else if (rettv_list_alloc(rettv) != FAIL)
6310 {
6311 int i;
6312
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006313 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314 NULL, options, WILD_ALL_KEEP);
6315 for (i = 0; i < xpc.xp_numfiles; i++)
6316 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6317
6318 ExpandCleanup(&xpc);
6319 }
6320 }
6321 else
6322 rettv->vval.v_string = NULL;
6323}
6324
6325/*
6326 * "globpath()" function
6327 */
6328 static void
6329f_globpath(typval_T *argvars, typval_T *rettv)
6330{
6331 int flags = 0;
6332 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006333 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006334 int error = FALSE;
6335 garray_T ga;
6336 int i;
6337
6338 /* When the optional second argument is non-zero, don't remove matches
6339 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6340 rettv->v_type = VAR_STRING;
6341 if (argvars[2].v_type != VAR_UNKNOWN)
6342 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006343 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006344 flags |= WILD_KEEP_ALL;
6345 if (argvars[3].v_type != VAR_UNKNOWN)
6346 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006347 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006348 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006349 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006350 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006351 flags |= WILD_ALLLINKS;
6352 }
6353 }
6354 if (file != NULL && !error)
6355 {
6356 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006357 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006358 if (rettv->v_type == VAR_STRING)
6359 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6360 else if (rettv_list_alloc(rettv) != FAIL)
6361 for (i = 0; i < ga.ga_len; ++i)
6362 list_append_string(rettv->vval.v_list,
6363 ((char_u **)(ga.ga_data))[i], -1);
6364 ga_clear_strings(&ga);
6365 }
6366 else
6367 rettv->vval.v_string = NULL;
6368}
6369
6370/*
6371 * "glob2regpat()" function
6372 */
6373 static void
6374f_glob2regpat(typval_T *argvars, typval_T *rettv)
6375{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006376 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006377
6378 rettv->v_type = VAR_STRING;
6379 rettv->vval.v_string = (pat == NULL)
6380 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6381}
6382
6383/* for VIM_VERSION_ defines */
6384#include "version.h"
6385
6386/*
6387 * "has()" function
6388 */
6389 static void
6390f_has(typval_T *argvars, typval_T *rettv)
6391{
6392 int i;
6393 char_u *name;
6394 int n = FALSE;
6395 static char *(has_list[]) =
6396 {
6397#ifdef AMIGA
6398 "amiga",
6399# ifdef FEAT_ARP
6400 "arp",
6401# endif
6402#endif
6403#ifdef __BEOS__
6404 "beos",
6405#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006406#if defined(BSD) && !defined(MACOS_X)
6407 "bsd",
6408#endif
6409#ifdef hpux
6410 "hpux",
6411#endif
6412#ifdef __linux__
6413 "linux",
6414#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006415#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006416 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6417 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006418# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006419 "macunix", /* Mac OS X, with the darwin feature */
6420 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006421# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006422#endif
6423#ifdef __QNX__
6424 "qnx",
6425#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006426#ifdef SUN_SYSTEM
6427 "sun",
6428#else
6429 "moon",
6430#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006431#ifdef UNIX
6432 "unix",
6433#endif
6434#ifdef VMS
6435 "vms",
6436#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006437#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006438 "win32",
6439#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006440#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006441 "win32unix",
6442#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006443#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006444 "win64",
6445#endif
6446#ifdef EBCDIC
6447 "ebcdic",
6448#endif
6449#ifndef CASE_INSENSITIVE_FILENAME
6450 "fname_case",
6451#endif
6452#ifdef HAVE_ACL
6453 "acl",
6454#endif
6455#ifdef FEAT_ARABIC
6456 "arabic",
6457#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006458 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006459#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006460 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006461#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006462#ifdef FEAT_AUTOSERVERNAME
6463 "autoservername",
6464#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006465#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006466 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006467# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006468 "balloon_multiline",
6469# endif
6470#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006471#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006472 "balloon_eval_term",
6473#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006474#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6475 "builtin_terms",
6476# ifdef ALL_BUILTIN_TCAPS
6477 "all_builtin_terms",
6478# endif
6479#endif
6480#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006481 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006482 || defined(FEAT_GUI_MOTIF))
6483 "browsefilter",
6484#endif
6485#ifdef FEAT_BYTEOFF
6486 "byte_offset",
6487#endif
6488#ifdef FEAT_JOB_CHANNEL
6489 "channel",
6490#endif
6491#ifdef FEAT_CINDENT
6492 "cindent",
6493#endif
6494#ifdef FEAT_CLIENTSERVER
6495 "clientserver",
6496#endif
6497#ifdef FEAT_CLIPBOARD
6498 "clipboard",
6499#endif
6500#ifdef FEAT_CMDL_COMPL
6501 "cmdline_compl",
6502#endif
6503#ifdef FEAT_CMDHIST
6504 "cmdline_hist",
6505#endif
6506#ifdef FEAT_COMMENTS
6507 "comments",
6508#endif
6509#ifdef FEAT_CONCEAL
6510 "conceal",
6511#endif
6512#ifdef FEAT_CRYPT
6513 "cryptv",
6514 "crypt-blowfish",
6515 "crypt-blowfish2",
6516#endif
6517#ifdef FEAT_CSCOPE
6518 "cscope",
6519#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006520 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006521#ifdef CURSOR_SHAPE
6522 "cursorshape",
6523#endif
6524#ifdef DEBUG
6525 "debug",
6526#endif
6527#ifdef FEAT_CON_DIALOG
6528 "dialog_con",
6529#endif
6530#ifdef FEAT_GUI_DIALOG
6531 "dialog_gui",
6532#endif
6533#ifdef FEAT_DIFF
6534 "diff",
6535#endif
6536#ifdef FEAT_DIGRAPHS
6537 "digraphs",
6538#endif
6539#ifdef FEAT_DIRECTX
6540 "directx",
6541#endif
6542#ifdef FEAT_DND
6543 "dnd",
6544#endif
6545#ifdef FEAT_EMACS_TAGS
6546 "emacs_tags",
6547#endif
6548 "eval", /* always present, of course! */
6549 "ex_extra", /* graduated feature */
6550#ifdef FEAT_SEARCH_EXTRA
6551 "extra_search",
6552#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006553#ifdef FEAT_SEARCHPATH
6554 "file_in_path",
6555#endif
6556#ifdef FEAT_FILTERPIPE
6557 "filterpipe",
6558#endif
6559#ifdef FEAT_FIND_ID
6560 "find_in_path",
6561#endif
6562#ifdef FEAT_FLOAT
6563 "float",
6564#endif
6565#ifdef FEAT_FOLDING
6566 "folding",
6567#endif
6568#ifdef FEAT_FOOTER
6569 "footer",
6570#endif
6571#if !defined(USE_SYSTEM) && defined(UNIX)
6572 "fork",
6573#endif
6574#ifdef FEAT_GETTEXT
6575 "gettext",
6576#endif
6577#ifdef FEAT_GUI
6578 "gui",
6579#endif
6580#ifdef FEAT_GUI_ATHENA
6581# ifdef FEAT_GUI_NEXTAW
6582 "gui_neXtaw",
6583# else
6584 "gui_athena",
6585# endif
6586#endif
6587#ifdef FEAT_GUI_GTK
6588 "gui_gtk",
6589# ifdef USE_GTK3
6590 "gui_gtk3",
6591# else
6592 "gui_gtk2",
6593# endif
6594#endif
6595#ifdef FEAT_GUI_GNOME
6596 "gui_gnome",
6597#endif
6598#ifdef FEAT_GUI_MAC
6599 "gui_mac",
6600#endif
6601#ifdef FEAT_GUI_MOTIF
6602 "gui_motif",
6603#endif
6604#ifdef FEAT_GUI_PHOTON
6605 "gui_photon",
6606#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006607#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006608 "gui_win32",
6609#endif
6610#ifdef FEAT_HANGULIN
6611 "hangul_input",
6612#endif
6613#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6614 "iconv",
6615#endif
6616#ifdef FEAT_INS_EXPAND
6617 "insert_expand",
6618#endif
6619#ifdef FEAT_JOB_CHANNEL
6620 "job",
6621#endif
6622#ifdef FEAT_JUMPLIST
6623 "jumplist",
6624#endif
6625#ifdef FEAT_KEYMAP
6626 "keymap",
6627#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006628 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006629#ifdef FEAT_LANGMAP
6630 "langmap",
6631#endif
6632#ifdef FEAT_LIBCALL
6633 "libcall",
6634#endif
6635#ifdef FEAT_LINEBREAK
6636 "linebreak",
6637#endif
6638#ifdef FEAT_LISP
6639 "lispindent",
6640#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642#ifdef FEAT_LOCALMAP
6643 "localmap",
6644#endif
6645#ifdef FEAT_LUA
6646# ifndef DYNAMIC_LUA
6647 "lua",
6648# endif
6649#endif
6650#ifdef FEAT_MENU
6651 "menu",
6652#endif
6653#ifdef FEAT_SESSION
6654 "mksession",
6655#endif
6656#ifdef FEAT_MODIFY_FNAME
6657 "modify_fname",
6658#endif
6659#ifdef FEAT_MOUSE
6660 "mouse",
6661#endif
6662#ifdef FEAT_MOUSESHAPE
6663 "mouseshape",
6664#endif
6665#if defined(UNIX) || defined(VMS)
6666# ifdef FEAT_MOUSE_DEC
6667 "mouse_dec",
6668# endif
6669# ifdef FEAT_MOUSE_GPM
6670 "mouse_gpm",
6671# endif
6672# ifdef FEAT_MOUSE_JSB
6673 "mouse_jsbterm",
6674# endif
6675# ifdef FEAT_MOUSE_NET
6676 "mouse_netterm",
6677# endif
6678# ifdef FEAT_MOUSE_PTERM
6679 "mouse_pterm",
6680# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006681# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006682 "mouse_sgr",
6683# endif
6684# ifdef FEAT_SYSMOUSE
6685 "mouse_sysmouse",
6686# endif
6687# ifdef FEAT_MOUSE_URXVT
6688 "mouse_urxvt",
6689# endif
6690# ifdef FEAT_MOUSE_XTERM
6691 "mouse_xterm",
6692# endif
6693#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695#ifdef FEAT_MBYTE_IME
6696 "multi_byte_ime",
6697#endif
6698#ifdef FEAT_MULTI_LANG
6699 "multi_lang",
6700#endif
6701#ifdef FEAT_MZSCHEME
6702#ifndef DYNAMIC_MZSCHEME
6703 "mzscheme",
6704#endif
6705#endif
6706#ifdef FEAT_NUM64
6707 "num64",
6708#endif
6709#ifdef FEAT_OLE
6710 "ole",
6711#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006712#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006713 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006714#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006715#ifdef FEAT_PATH_EXTRA
6716 "path_extra",
6717#endif
6718#ifdef FEAT_PERL
6719#ifndef DYNAMIC_PERL
6720 "perl",
6721#endif
6722#endif
6723#ifdef FEAT_PERSISTENT_UNDO
6724 "persistent_undo",
6725#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006726#if defined(FEAT_PYTHON)
6727 "python_compiled",
6728# if defined(DYNAMIC_PYTHON)
6729 "python_dynamic",
6730# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006731 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006732 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006733# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006734#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006735#if defined(FEAT_PYTHON3)
6736 "python3_compiled",
6737# if defined(DYNAMIC_PYTHON3)
6738 "python3_dynamic",
6739# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006740 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006741 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006742# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006743#endif
6744#ifdef FEAT_POSTSCRIPT
6745 "postscript",
6746#endif
6747#ifdef FEAT_PRINTER
6748 "printer",
6749#endif
6750#ifdef FEAT_PROFILE
6751 "profile",
6752#endif
6753#ifdef FEAT_RELTIME
6754 "reltime",
6755#endif
6756#ifdef FEAT_QUICKFIX
6757 "quickfix",
6758#endif
6759#ifdef FEAT_RIGHTLEFT
6760 "rightleft",
6761#endif
6762#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6763 "ruby",
6764#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006765 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006766#ifdef FEAT_CMDL_INFO
6767 "showcmd",
6768 "cmdline_info",
6769#endif
6770#ifdef FEAT_SIGNS
6771 "signs",
6772#endif
6773#ifdef FEAT_SMARTINDENT
6774 "smartindent",
6775#endif
6776#ifdef STARTUPTIME
6777 "startuptime",
6778#endif
6779#ifdef FEAT_STL_OPT
6780 "statusline",
6781#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006782#ifdef FEAT_NETBEANS_INTG
6783 "netbeans_intg",
6784#endif
6785#ifdef FEAT_SPELL
6786 "spell",
6787#endif
6788#ifdef FEAT_SYN_HL
6789 "syntax",
6790#endif
6791#if defined(USE_SYSTEM) || !defined(UNIX)
6792 "system",
6793#endif
6794#ifdef FEAT_TAG_BINS
6795 "tag_binary",
6796#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006797#ifdef FEAT_TCL
6798# ifndef DYNAMIC_TCL
6799 "tcl",
6800# endif
6801#endif
6802#ifdef FEAT_TERMGUICOLORS
6803 "termguicolors",
6804#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006805#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006806 "terminal",
6807#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006808#ifdef TERMINFO
6809 "terminfo",
6810#endif
6811#ifdef FEAT_TERMRESPONSE
6812 "termresponse",
6813#endif
6814#ifdef FEAT_TEXTOBJ
6815 "textobjects",
6816#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006817#ifdef FEAT_TEXT_PROP
6818 "textprop",
6819#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006820#ifdef HAVE_TGETENT
6821 "tgetent",
6822#endif
6823#ifdef FEAT_TIMERS
6824 "timers",
6825#endif
6826#ifdef FEAT_TITLE
6827 "title",
6828#endif
6829#ifdef FEAT_TOOLBAR
6830 "toolbar",
6831#endif
6832#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6833 "unnamedplus",
6834#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006835 "user-commands", /* was accidentally included in 5.4 */
6836 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006837#ifdef FEAT_VARTABS
6838 "vartabs",
6839#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006840 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006841#ifdef FEAT_VIMINFO
6842 "viminfo",
6843#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006844 "vimscript-1",
6845 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006846 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006847 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006848 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006849 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006850 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006851#ifdef FEAT_VTP
6852 "vtp",
6853#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006854#ifdef FEAT_WILDIGN
6855 "wildignore",
6856#endif
6857#ifdef FEAT_WILDMENU
6858 "wildmenu",
6859#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006860 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006861#ifdef FEAT_WAK
6862 "winaltkeys",
6863#endif
6864#ifdef FEAT_WRITEBACKUP
6865 "writebackup",
6866#endif
6867#ifdef FEAT_XIM
6868 "xim",
6869#endif
6870#ifdef FEAT_XFONTSET
6871 "xfontset",
6872#endif
6873#ifdef FEAT_XPM_W32
6874 "xpm",
6875 "xpm_w32", /* for backward compatibility */
6876#else
6877# if defined(HAVE_XPM)
6878 "xpm",
6879# endif
6880#endif
6881#ifdef USE_XSMP
6882 "xsmp",
6883#endif
6884#ifdef USE_XSMP_INTERACT
6885 "xsmp_interact",
6886#endif
6887#ifdef FEAT_XCLIPBOARD
6888 "xterm_clipboard",
6889#endif
6890#ifdef FEAT_XTERM_SAVE
6891 "xterm_save",
6892#endif
6893#if defined(UNIX) && defined(FEAT_X11)
6894 "X11",
6895#endif
6896 NULL
6897 };
6898
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006899 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 for (i = 0; has_list[i] != NULL; ++i)
6901 if (STRICMP(name, has_list[i]) == 0)
6902 {
6903 n = TRUE;
6904 break;
6905 }
6906
6907 if (n == FALSE)
6908 {
6909 if (STRNICMP(name, "patch", 5) == 0)
6910 {
6911 if (name[5] == '-'
6912 && STRLEN(name) >= 11
6913 && vim_isdigit(name[6])
6914 && vim_isdigit(name[8])
6915 && vim_isdigit(name[10]))
6916 {
6917 int major = atoi((char *)name + 6);
6918 int minor = atoi((char *)name + 8);
6919
6920 /* Expect "patch-9.9.01234". */
6921 n = (major < VIM_VERSION_MAJOR
6922 || (major == VIM_VERSION_MAJOR
6923 && (minor < VIM_VERSION_MINOR
6924 || (minor == VIM_VERSION_MINOR
6925 && has_patch(atoi((char *)name + 10))))));
6926 }
6927 else
6928 n = has_patch(atoi((char *)name + 5));
6929 }
6930 else if (STRICMP(name, "vim_starting") == 0)
6931 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006932 else if (STRICMP(name, "ttyin") == 0)
6933 n = mch_input_isatty();
6934 else if (STRICMP(name, "ttyout") == 0)
6935 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006936 else if (STRICMP(name, "multi_byte_encoding") == 0)
6937 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006938#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006939 else if (STRICMP(name, "balloon_multiline") == 0)
6940 n = multiline_balloon_available();
6941#endif
6942#ifdef DYNAMIC_TCL
6943 else if (STRICMP(name, "tcl") == 0)
6944 n = tcl_enabled(FALSE);
6945#endif
6946#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6947 else if (STRICMP(name, "iconv") == 0)
6948 n = iconv_enabled(FALSE);
6949#endif
6950#ifdef DYNAMIC_LUA
6951 else if (STRICMP(name, "lua") == 0)
6952 n = lua_enabled(FALSE);
6953#endif
6954#ifdef DYNAMIC_MZSCHEME
6955 else if (STRICMP(name, "mzscheme") == 0)
6956 n = mzscheme_enabled(FALSE);
6957#endif
6958#ifdef DYNAMIC_RUBY
6959 else if (STRICMP(name, "ruby") == 0)
6960 n = ruby_enabled(FALSE);
6961#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006962#ifdef DYNAMIC_PYTHON
6963 else if (STRICMP(name, "python") == 0)
6964 n = python_enabled(FALSE);
6965#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006966#ifdef DYNAMIC_PYTHON3
6967 else if (STRICMP(name, "python3") == 0)
6968 n = python3_enabled(FALSE);
6969#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006970#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6971 else if (STRICMP(name, "pythonx") == 0)
6972 {
6973# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6974 if (p_pyx == 0)
6975 n = python3_enabled(FALSE) || python_enabled(FALSE);
6976 else if (p_pyx == 3)
6977 n = python3_enabled(FALSE);
6978 else if (p_pyx == 2)
6979 n = python_enabled(FALSE);
6980# elif defined(DYNAMIC_PYTHON)
6981 n = python_enabled(FALSE);
6982# elif defined(DYNAMIC_PYTHON3)
6983 n = python3_enabled(FALSE);
6984# endif
6985 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006986#endif
6987#ifdef DYNAMIC_PERL
6988 else if (STRICMP(name, "perl") == 0)
6989 n = perl_enabled(FALSE);
6990#endif
6991#ifdef FEAT_GUI
6992 else if (STRICMP(name, "gui_running") == 0)
6993 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006994# ifdef FEAT_BROWSE
6995 else if (STRICMP(name, "browse") == 0)
6996 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6997# endif
6998#endif
6999#ifdef FEAT_SYN_HL
7000 else if (STRICMP(name, "syntax_items") == 0)
7001 n = syntax_present(curwin);
7002#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007003#ifdef FEAT_VTP
7004 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02007005 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007006#endif
7007#ifdef FEAT_NETBEANS_INTG
7008 else if (STRICMP(name, "netbeans_enabled") == 0)
7009 n = netbeans_active();
7010#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007011#ifdef FEAT_MOUSE_GPM
7012 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
7013 n = gpm_enabled();
7014#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007015#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02007016 else if (STRICMP(name, "terminal") == 0)
7017 n = terminal_enabled();
7018#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007019#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007020 else if (STRICMP(name, "conpty") == 0)
7021 n = use_conpty();
7022#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023 }
7024
7025 rettv->vval.v_number = n;
7026}
7027
7028/*
7029 * "has_key()" function
7030 */
7031 static void
7032f_has_key(typval_T *argvars, typval_T *rettv)
7033{
7034 if (argvars[0].v_type != VAR_DICT)
7035 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007036 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007037 return;
7038 }
7039 if (argvars[0].vval.v_dict == NULL)
7040 return;
7041
7042 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007043 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044}
7045
7046/*
7047 * "haslocaldir()" function
7048 */
7049 static void
7050f_haslocaldir(typval_T *argvars, typval_T *rettv)
7051{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007052 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007053 win_T *wp = NULL;
7054
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007055 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7056
7057 // Check for window-local and tab-local directories
7058 if (wp != NULL && wp->w_localdir != NULL)
7059 rettv->vval.v_number = 1;
7060 else if (tp != NULL && tp->tp_localdir != NULL)
7061 rettv->vval.v_number = 2;
7062 else
7063 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007064}
7065
7066/*
7067 * "hasmapto()" function
7068 */
7069 static void
7070f_hasmapto(typval_T *argvars, typval_T *rettv)
7071{
7072 char_u *name;
7073 char_u *mode;
7074 char_u buf[NUMBUFLEN];
7075 int abbr = FALSE;
7076
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007077 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007078 if (argvars[1].v_type == VAR_UNKNOWN)
7079 mode = (char_u *)"nvo";
7080 else
7081 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007082 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007083 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007084 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007085 }
7086
7087 if (map_to_exists(name, mode, abbr))
7088 rettv->vval.v_number = TRUE;
7089 else
7090 rettv->vval.v_number = FALSE;
7091}
7092
7093/*
7094 * "histadd()" function
7095 */
7096 static void
7097f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7098{
7099#ifdef FEAT_CMDHIST
7100 int histype;
7101 char_u *str;
7102 char_u buf[NUMBUFLEN];
7103#endif
7104
7105 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007106 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007107 return;
7108#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007109 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007110 histype = str != NULL ? get_histtype(str) : -1;
7111 if (histype >= 0)
7112 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007113 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007114 if (*str != NUL)
7115 {
7116 init_history();
7117 add_to_history(histype, str, FALSE, NUL);
7118 rettv->vval.v_number = TRUE;
7119 return;
7120 }
7121 }
7122#endif
7123}
7124
7125/*
7126 * "histdel()" function
7127 */
7128 static void
7129f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7130{
7131#ifdef FEAT_CMDHIST
7132 int n;
7133 char_u buf[NUMBUFLEN];
7134 char_u *str;
7135
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007136 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007137 if (str == NULL)
7138 n = 0;
7139 else if (argvars[1].v_type == VAR_UNKNOWN)
7140 /* only one argument: clear entire history */
7141 n = clr_history(get_histtype(str));
7142 else if (argvars[1].v_type == VAR_NUMBER)
7143 /* index given: remove that entry */
7144 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007145 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007146 else
7147 /* string given: remove all matching entries */
7148 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007149 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007150 rettv->vval.v_number = n;
7151#endif
7152}
7153
7154/*
7155 * "histget()" function
7156 */
7157 static void
7158f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7159{
7160#ifdef FEAT_CMDHIST
7161 int type;
7162 int idx;
7163 char_u *str;
7164
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007165 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007166 if (str == NULL)
7167 rettv->vval.v_string = NULL;
7168 else
7169 {
7170 type = get_histtype(str);
7171 if (argvars[1].v_type == VAR_UNKNOWN)
7172 idx = get_history_idx(type);
7173 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007174 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007175 /* -1 on type error */
7176 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7177 }
7178#else
7179 rettv->vval.v_string = NULL;
7180#endif
7181 rettv->v_type = VAR_STRING;
7182}
7183
7184/*
7185 * "histnr()" function
7186 */
7187 static void
7188f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7189{
7190 int i;
7191
7192#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007193 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007194
7195 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7196 if (i >= HIST_CMD && i < HIST_COUNT)
7197 i = get_history_idx(i);
7198 else
7199#endif
7200 i = -1;
7201 rettv->vval.v_number = i;
7202}
7203
7204/*
7205 * "highlightID(name)" function
7206 */
7207 static void
7208f_hlID(typval_T *argvars, typval_T *rettv)
7209{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007210 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007211}
7212
7213/*
7214 * "highlight_exists()" function
7215 */
7216 static void
7217f_hlexists(typval_T *argvars, typval_T *rettv)
7218{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007219 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007220}
7221
7222/*
7223 * "hostname()" function
7224 */
7225 static void
7226f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7227{
7228 char_u hostname[256];
7229
7230 mch_get_host_name(hostname, 256);
7231 rettv->v_type = VAR_STRING;
7232 rettv->vval.v_string = vim_strsave(hostname);
7233}
7234
7235/*
7236 * iconv() function
7237 */
7238 static void
7239f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7240{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007241 char_u buf1[NUMBUFLEN];
7242 char_u buf2[NUMBUFLEN];
7243 char_u *from, *to, *str;
7244 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245
7246 rettv->v_type = VAR_STRING;
7247 rettv->vval.v_string = NULL;
7248
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007249 str = tv_get_string(&argvars[0]);
7250 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7251 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007252 vimconv.vc_type = CONV_NONE;
7253 convert_setup(&vimconv, from, to);
7254
7255 /* If the encodings are equal, no conversion needed. */
7256 if (vimconv.vc_type == CONV_NONE)
7257 rettv->vval.v_string = vim_strsave(str);
7258 else
7259 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7260
7261 convert_setup(&vimconv, NULL, NULL);
7262 vim_free(from);
7263 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007264}
7265
7266/*
7267 * "indent()" function
7268 */
7269 static void
7270f_indent(typval_T *argvars, typval_T *rettv)
7271{
7272 linenr_T lnum;
7273
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007274 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007275 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7276 rettv->vval.v_number = get_indent_lnum(lnum);
7277 else
7278 rettv->vval.v_number = -1;
7279}
7280
7281/*
7282 * "index()" function
7283 */
7284 static void
7285f_index(typval_T *argvars, typval_T *rettv)
7286{
7287 list_T *l;
7288 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007289 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007290 long idx = 0;
7291 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007292 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293
7294 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007295 if (argvars[0].v_type == VAR_BLOB)
7296 {
7297 typval_T tv;
7298 int start = 0;
7299
7300 if (argvars[2].v_type != VAR_UNKNOWN)
7301 {
7302 start = tv_get_number_chk(&argvars[2], &error);
7303 if (error)
7304 return;
7305 }
7306 b = argvars[0].vval.v_blob;
7307 if (b == NULL)
7308 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007309 if (start < 0)
7310 {
7311 start = blob_len(b) + start;
7312 if (start < 0)
7313 start = 0;
7314 }
7315
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007316 for (idx = start; idx < blob_len(b); ++idx)
7317 {
7318 tv.v_type = VAR_NUMBER;
7319 tv.vval.v_number = blob_get(b, idx);
7320 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7321 {
7322 rettv->vval.v_number = idx;
7323 return;
7324 }
7325 }
7326 return;
7327 }
7328 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007329 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007330 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007331 return;
7332 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007333
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007334 l = argvars[0].vval.v_list;
7335 if (l != NULL)
7336 {
7337 item = l->lv_first;
7338 if (argvars[2].v_type != VAR_UNKNOWN)
7339 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007340 /* Start at specified item. Use the cached index that list_find()
7341 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007342 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007343 idx = l->lv_idx;
7344 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007345 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007346 if (error)
7347 item = NULL;
7348 }
7349
7350 for ( ; item != NULL; item = item->li_next, ++idx)
7351 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7352 {
7353 rettv->vval.v_number = idx;
7354 break;
7355 }
7356 }
7357}
7358
7359static int inputsecret_flag = 0;
7360
7361/*
7362 * "input()" function
7363 * Also handles inputsecret() when inputsecret is set.
7364 */
7365 static void
7366f_input(typval_T *argvars, typval_T *rettv)
7367{
7368 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7369}
7370
7371/*
7372 * "inputdialog()" function
7373 */
7374 static void
7375f_inputdialog(typval_T *argvars, typval_T *rettv)
7376{
7377#if defined(FEAT_GUI_TEXTDIALOG)
7378 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7379 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7380 {
7381 char_u *message;
7382 char_u buf[NUMBUFLEN];
7383 char_u *defstr = (char_u *)"";
7384
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007385 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007386 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007387 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007388 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7389 else
7390 IObuff[0] = NUL;
7391 if (message != NULL && defstr != NULL
7392 && do_dialog(VIM_QUESTION, NULL, message,
7393 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7394 rettv->vval.v_string = vim_strsave(IObuff);
7395 else
7396 {
7397 if (message != NULL && defstr != NULL
7398 && argvars[1].v_type != VAR_UNKNOWN
7399 && argvars[2].v_type != VAR_UNKNOWN)
7400 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007401 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007402 else
7403 rettv->vval.v_string = NULL;
7404 }
7405 rettv->v_type = VAR_STRING;
7406 }
7407 else
7408#endif
7409 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7410}
7411
7412/*
7413 * "inputlist()" function
7414 */
7415 static void
7416f_inputlist(typval_T *argvars, typval_T *rettv)
7417{
7418 listitem_T *li;
7419 int selected;
7420 int mouse_used;
7421
7422#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007423 /* While starting up, there is no place to enter text. When running tests
7424 * with --not-a-term we assume feedkeys() will be used. */
7425 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007426 return;
7427#endif
7428 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7429 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007430 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007431 return;
7432 }
7433
7434 msg_start();
7435 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7436 lines_left = Rows; /* avoid more prompt */
7437 msg_scroll = TRUE;
7438 msg_clr_eos();
7439
7440 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7441 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007442 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007443 msg_putchar('\n');
7444 }
7445
7446 /* Ask for choice. */
7447 selected = prompt_for_number(&mouse_used);
7448 if (mouse_used)
7449 selected -= lines_left;
7450
7451 rettv->vval.v_number = selected;
7452}
7453
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007454static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7455
7456/*
7457 * "inputrestore()" function
7458 */
7459 static void
7460f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7461{
7462 if (ga_userinput.ga_len > 0)
7463 {
7464 --ga_userinput.ga_len;
7465 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7466 + ga_userinput.ga_len);
7467 /* default return is zero == OK */
7468 }
7469 else if (p_verbose > 1)
7470 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007471 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007472 rettv->vval.v_number = 1; /* Failed */
7473 }
7474}
7475
7476/*
7477 * "inputsave()" function
7478 */
7479 static void
7480f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7481{
7482 /* Add an entry to the stack of typeahead storage. */
7483 if (ga_grow(&ga_userinput, 1) == OK)
7484 {
7485 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7486 + ga_userinput.ga_len);
7487 ++ga_userinput.ga_len;
7488 /* default return is zero == OK */
7489 }
7490 else
7491 rettv->vval.v_number = 1; /* Failed */
7492}
7493
7494/*
7495 * "inputsecret()" function
7496 */
7497 static void
7498f_inputsecret(typval_T *argvars, typval_T *rettv)
7499{
7500 ++cmdline_star;
7501 ++inputsecret_flag;
7502 f_input(argvars, rettv);
7503 --cmdline_star;
7504 --inputsecret_flag;
7505}
7506
7507/*
7508 * "insert()" function
7509 */
7510 static void
7511f_insert(typval_T *argvars, typval_T *rettv)
7512{
7513 long before = 0;
7514 listitem_T *item;
7515 list_T *l;
7516 int error = FALSE;
7517
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007518 if (argvars[0].v_type == VAR_BLOB)
7519 {
7520 int val, len;
7521 char_u *p;
7522
7523 len = blob_len(argvars[0].vval.v_blob);
7524 if (argvars[2].v_type != VAR_UNKNOWN)
7525 {
7526 before = (long)tv_get_number_chk(&argvars[2], &error);
7527 if (error)
7528 return; // type error; errmsg already given
7529 if (before < 0 || before > len)
7530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007531 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007532 return;
7533 }
7534 }
7535 val = tv_get_number_chk(&argvars[1], &error);
7536 if (error)
7537 return;
7538 if (val < 0 || val > 255)
7539 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007540 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007541 return;
7542 }
7543
7544 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7545 return;
7546 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7547 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7548 *(p + before) = val;
7549 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7550
7551 copy_tv(&argvars[0], rettv);
7552 }
7553 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007554 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007555 else if ((l = argvars[0].vval.v_list) != NULL
7556 && !var_check_lock(l->lv_lock,
7557 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007558 {
7559 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007560 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007561 if (error)
7562 return; /* type error; errmsg already given */
7563
7564 if (before == l->lv_len)
7565 item = NULL;
7566 else
7567 {
7568 item = list_find(l, before);
7569 if (item == NULL)
7570 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007571 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007572 l = NULL;
7573 }
7574 }
7575 if (l != NULL)
7576 {
7577 list_insert_tv(l, &argvars[1], item);
7578 copy_tv(&argvars[0], rettv);
7579 }
7580 }
7581}
7582
7583/*
7584 * "invert(expr)" function
7585 */
7586 static void
7587f_invert(typval_T *argvars, typval_T *rettv)
7588{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007589 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007590}
7591
7592/*
7593 * "isdirectory()" function
7594 */
7595 static void
7596f_isdirectory(typval_T *argvars, typval_T *rettv)
7597{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007598 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007599}
7600
7601/*
7602 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7603 * or it refers to a List or Dictionary that is locked.
7604 */
7605 static int
7606tv_islocked(typval_T *tv)
7607{
7608 return (tv->v_lock & VAR_LOCKED)
7609 || (tv->v_type == VAR_LIST
7610 && tv->vval.v_list != NULL
7611 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7612 || (tv->v_type == VAR_DICT
7613 && tv->vval.v_dict != NULL
7614 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7615}
7616
7617/*
7618 * "islocked()" function
7619 */
7620 static void
7621f_islocked(typval_T *argvars, typval_T *rettv)
7622{
7623 lval_T lv;
7624 char_u *end;
7625 dictitem_T *di;
7626
7627 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007628 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007629 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007630 if (end != NULL && lv.ll_name != NULL)
7631 {
7632 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007633 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007634 else
7635 {
7636 if (lv.ll_tv == NULL)
7637 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007638 di = find_var(lv.ll_name, NULL, TRUE);
7639 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007640 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007641 /* Consider a variable locked when:
7642 * 1. the variable itself is locked
7643 * 2. the value of the variable is locked.
7644 * 3. the List or Dict value is locked.
7645 */
7646 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7647 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007648 }
7649 }
7650 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007651 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007652 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007653 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007654 else if (lv.ll_list != NULL)
7655 /* List item. */
7656 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7657 else
7658 /* Dictionary item. */
7659 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7660 }
7661 }
7662
7663 clear_lval(&lv);
7664}
7665
7666#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7667/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007668 * "isinf()" function
7669 */
7670 static void
7671f_isinf(typval_T *argvars, typval_T *rettv)
7672{
7673 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7674 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7675}
7676
7677/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007678 * "isnan()" function
7679 */
7680 static void
7681f_isnan(typval_T *argvars, typval_T *rettv)
7682{
7683 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7684 && isnan(argvars[0].vval.v_float);
7685}
7686#endif
7687
7688/*
7689 * "items(dict)" function
7690 */
7691 static void
7692f_items(typval_T *argvars, typval_T *rettv)
7693{
7694 dict_list(argvars, rettv, 2);
7695}
7696
7697#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7698/*
7699 * Get the job from the argument.
7700 * Returns NULL if the job is invalid.
7701 */
7702 static job_T *
7703get_job_arg(typval_T *tv)
7704{
7705 job_T *job;
7706
7707 if (tv->v_type != VAR_JOB)
7708 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007709 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007710 return NULL;
7711 }
7712 job = tv->vval.v_job;
7713
7714 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007715 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007716 return job;
7717}
7718
7719/*
7720 * "job_getchannel()" function
7721 */
7722 static void
7723f_job_getchannel(typval_T *argvars, typval_T *rettv)
7724{
7725 job_T *job = get_job_arg(&argvars[0]);
7726
7727 if (job != NULL)
7728 {
7729 rettv->v_type = VAR_CHANNEL;
7730 rettv->vval.v_channel = job->jv_channel;
7731 if (job->jv_channel != NULL)
7732 ++job->jv_channel->ch_refcount;
7733 }
7734}
7735
7736/*
7737 * "job_info()" function
7738 */
7739 static void
7740f_job_info(typval_T *argvars, typval_T *rettv)
7741{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007742 if (argvars[0].v_type != VAR_UNKNOWN)
7743 {
7744 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007745
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007746 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7747 job_info(job, rettv->vval.v_dict);
7748 }
7749 else if (rettv_list_alloc(rettv) == OK)
7750 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007751}
7752
7753/*
7754 * "job_setoptions()" function
7755 */
7756 static void
7757f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7758{
7759 job_T *job = get_job_arg(&argvars[0]);
7760 jobopt_T opt;
7761
7762 if (job == NULL)
7763 return;
7764 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007765 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007766 job_set_options(job, &opt);
7767 free_job_options(&opt);
7768}
7769
7770/*
7771 * "job_start()" function
7772 */
7773 static void
7774f_job_start(typval_T *argvars, typval_T *rettv)
7775{
7776 rettv->v_type = VAR_JOB;
7777 if (check_restricted() || check_secure())
7778 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007779 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007780}
7781
7782/*
7783 * "job_status()" function
7784 */
7785 static void
7786f_job_status(typval_T *argvars, typval_T *rettv)
7787{
7788 job_T *job = get_job_arg(&argvars[0]);
7789
7790 if (job != NULL)
7791 {
7792 rettv->v_type = VAR_STRING;
7793 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7794 }
7795}
7796
7797/*
7798 * "job_stop()" function
7799 */
7800 static void
7801f_job_stop(typval_T *argvars, typval_T *rettv)
7802{
7803 job_T *job = get_job_arg(&argvars[0]);
7804
7805 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007806 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007807}
7808#endif
7809
7810/*
7811 * "join()" function
7812 */
7813 static void
7814f_join(typval_T *argvars, typval_T *rettv)
7815{
7816 garray_T ga;
7817 char_u *sep;
7818
7819 if (argvars[0].v_type != VAR_LIST)
7820 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007821 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007822 return;
7823 }
7824 if (argvars[0].vval.v_list == NULL)
7825 return;
7826 if (argvars[1].v_type == VAR_UNKNOWN)
7827 sep = (char_u *)" ";
7828 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007829 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007830
7831 rettv->v_type = VAR_STRING;
7832
7833 if (sep != NULL)
7834 {
7835 ga_init2(&ga, (int)sizeof(char), 80);
7836 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7837 ga_append(&ga, NUL);
7838 rettv->vval.v_string = (char_u *)ga.ga_data;
7839 }
7840 else
7841 rettv->vval.v_string = NULL;
7842}
7843
7844/*
7845 * "js_decode()" function
7846 */
7847 static void
7848f_js_decode(typval_T *argvars, typval_T *rettv)
7849{
7850 js_read_T reader;
7851
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007852 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007853 reader.js_fill = NULL;
7854 reader.js_used = 0;
7855 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007856 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007857}
7858
7859/*
7860 * "js_encode()" function
7861 */
7862 static void
7863f_js_encode(typval_T *argvars, typval_T *rettv)
7864{
7865 rettv->v_type = VAR_STRING;
7866 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7867}
7868
7869/*
7870 * "json_decode()" function
7871 */
7872 static void
7873f_json_decode(typval_T *argvars, typval_T *rettv)
7874{
7875 js_read_T reader;
7876
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007877 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007878 reader.js_fill = NULL;
7879 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007880 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881}
7882
7883/*
7884 * "json_encode()" function
7885 */
7886 static void
7887f_json_encode(typval_T *argvars, typval_T *rettv)
7888{
7889 rettv->v_type = VAR_STRING;
7890 rettv->vval.v_string = json_encode(&argvars[0], 0);
7891}
7892
7893/*
7894 * "keys()" function
7895 */
7896 static void
7897f_keys(typval_T *argvars, typval_T *rettv)
7898{
7899 dict_list(argvars, rettv, 0);
7900}
7901
7902/*
7903 * "last_buffer_nr()" function.
7904 */
7905 static void
7906f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7907{
7908 int n = 0;
7909 buf_T *buf;
7910
Bram Moolenaar29323592016-07-24 22:04:11 +02007911 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912 if (n < buf->b_fnum)
7913 n = buf->b_fnum;
7914
7915 rettv->vval.v_number = n;
7916}
7917
7918/*
7919 * "len()" function
7920 */
7921 static void
7922f_len(typval_T *argvars, typval_T *rettv)
7923{
7924 switch (argvars[0].v_type)
7925 {
7926 case VAR_STRING:
7927 case VAR_NUMBER:
7928 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007929 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007930 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007931 case VAR_BLOB:
7932 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7933 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934 case VAR_LIST:
7935 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7936 break;
7937 case VAR_DICT:
7938 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7939 break;
7940 case VAR_UNKNOWN:
7941 case VAR_SPECIAL:
7942 case VAR_FLOAT:
7943 case VAR_FUNC:
7944 case VAR_PARTIAL:
7945 case VAR_JOB:
7946 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007947 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007948 break;
7949 }
7950}
7951
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007952 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007953libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007954{
7955#ifdef FEAT_LIBCALL
7956 char_u *string_in;
7957 char_u **string_result;
7958 int nr_result;
7959#endif
7960
7961 rettv->v_type = type;
7962 if (type != VAR_NUMBER)
7963 rettv->vval.v_string = NULL;
7964
7965 if (check_restricted() || check_secure())
7966 return;
7967
7968#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007969 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007970 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7971 {
7972 string_in = NULL;
7973 if (argvars[2].v_type == VAR_STRING)
7974 string_in = argvars[2].vval.v_string;
7975 if (type == VAR_NUMBER)
7976 string_result = NULL;
7977 else
7978 string_result = &rettv->vval.v_string;
7979 if (mch_libcall(argvars[0].vval.v_string,
7980 argvars[1].vval.v_string,
7981 string_in,
7982 argvars[2].vval.v_number,
7983 string_result,
7984 &nr_result) == OK
7985 && type == VAR_NUMBER)
7986 rettv->vval.v_number = nr_result;
7987 }
7988#endif
7989}
7990
7991/*
7992 * "libcall()" function
7993 */
7994 static void
7995f_libcall(typval_T *argvars, typval_T *rettv)
7996{
7997 libcall_common(argvars, rettv, VAR_STRING);
7998}
7999
8000/*
8001 * "libcallnr()" function
8002 */
8003 static void
8004f_libcallnr(typval_T *argvars, typval_T *rettv)
8005{
8006 libcall_common(argvars, rettv, VAR_NUMBER);
8007}
8008
8009/*
8010 * "line(string)" function
8011 */
8012 static void
8013f_line(typval_T *argvars, typval_T *rettv)
8014{
8015 linenr_T lnum = 0;
8016 pos_T *fp;
8017 int fnum;
8018
8019 fp = var2fpos(&argvars[0], TRUE, &fnum);
8020 if (fp != NULL)
8021 lnum = fp->lnum;
8022 rettv->vval.v_number = lnum;
8023}
8024
8025/*
8026 * "line2byte(lnum)" function
8027 */
8028 static void
8029f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
8030{
8031#ifndef FEAT_BYTEOFF
8032 rettv->vval.v_number = -1;
8033#else
8034 linenr_T lnum;
8035
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008036 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008037 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
8038 rettv->vval.v_number = -1;
8039 else
8040 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8041 if (rettv->vval.v_number >= 0)
8042 ++rettv->vval.v_number;
8043#endif
8044}
8045
8046/*
8047 * "lispindent(lnum)" function
8048 */
8049 static void
8050f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8051{
8052#ifdef FEAT_LISP
8053 pos_T pos;
8054 linenr_T lnum;
8055
8056 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008057 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008058 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8059 {
8060 curwin->w_cursor.lnum = lnum;
8061 rettv->vval.v_number = get_lisp_indent();
8062 curwin->w_cursor = pos;
8063 }
8064 else
8065#endif
8066 rettv->vval.v_number = -1;
8067}
8068
8069/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008070 * "list2str()" function
8071 */
8072 static void
8073f_list2str(typval_T *argvars, typval_T *rettv)
8074{
8075 list_T *l;
8076 listitem_T *li;
8077 garray_T ga;
8078 int utf8 = FALSE;
8079
8080 rettv->v_type = VAR_STRING;
8081 rettv->vval.v_string = NULL;
8082 if (argvars[0].v_type != VAR_LIST)
8083 {
8084 emsg(_(e_invarg));
8085 return;
8086 }
8087
8088 l = argvars[0].vval.v_list;
8089 if (l == NULL)
8090 return; // empty list results in empty string
8091
8092 if (argvars[1].v_type != VAR_UNKNOWN)
8093 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8094
8095 ga_init2(&ga, 1, 80);
8096 if (has_mbyte || utf8)
8097 {
8098 char_u buf[MB_MAXBYTES + 1];
8099 int (*char2bytes)(int, char_u *);
8100
8101 if (utf8 || enc_utf8)
8102 char2bytes = utf_char2bytes;
8103 else
8104 char2bytes = mb_char2bytes;
8105
8106 for (li = l->lv_first; li != NULL; li = li->li_next)
8107 {
8108 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8109 ga_concat(&ga, buf);
8110 }
8111 ga_append(&ga, NUL);
8112 }
8113 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8114 {
8115 for (li = l->lv_first; li != NULL; li = li->li_next)
8116 ga_append(&ga, tv_get_number(&li->li_tv));
8117 ga_append(&ga, NUL);
8118 }
8119
8120 rettv->v_type = VAR_STRING;
8121 rettv->vval.v_string = ga.ga_data;
8122}
8123
8124/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125 * "localtime()" function
8126 */
8127 static void
8128f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8129{
8130 rettv->vval.v_number = (varnumber_T)time(NULL);
8131}
8132
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008133 static void
8134get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8135{
8136 char_u *keys;
8137 char_u *which;
8138 char_u buf[NUMBUFLEN];
8139 char_u *keys_buf = NULL;
8140 char_u *rhs;
8141 int mode;
8142 int abbr = FALSE;
8143 int get_dict = FALSE;
8144 mapblock_T *mp;
8145 int buffer_local;
8146
8147 /* return empty string for failure */
8148 rettv->v_type = VAR_STRING;
8149 rettv->vval.v_string = NULL;
8150
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008151 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008152 if (*keys == NUL)
8153 return;
8154
8155 if (argvars[1].v_type != VAR_UNKNOWN)
8156 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008157 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158 if (argvars[2].v_type != VAR_UNKNOWN)
8159 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008160 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008161 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008162 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163 }
8164 }
8165 else
8166 which = (char_u *)"";
8167 if (which == NULL)
8168 return;
8169
8170 mode = get_map_mode(&which, 0);
8171
8172 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8173 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8174 vim_free(keys_buf);
8175
8176 if (!get_dict)
8177 {
8178 /* Return a string. */
8179 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008180 {
8181 if (*rhs == NUL)
8182 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8183 else
8184 rettv->vval.v_string = str2special_save(rhs, FALSE);
8185 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008186
8187 }
8188 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8189 {
8190 /* Return a dictionary. */
8191 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8192 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8193 dict_T *dict = rettv->vval.v_dict;
8194
Bram Moolenaare0be1672018-07-08 16:50:37 +02008195 dict_add_string(dict, "lhs", lhs);
8196 dict_add_string(dict, "rhs", mp->m_orig_str);
8197 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8198 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8199 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008200 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8201 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008202 dict_add_number(dict, "buffer", (long)buffer_local);
8203 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8204 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008205
8206 vim_free(lhs);
8207 vim_free(mapmode);
8208 }
8209}
8210
8211#ifdef FEAT_FLOAT
8212/*
8213 * "log()" function
8214 */
8215 static void
8216f_log(typval_T *argvars, typval_T *rettv)
8217{
8218 float_T f = 0.0;
8219
8220 rettv->v_type = VAR_FLOAT;
8221 if (get_float_arg(argvars, &f) == OK)
8222 rettv->vval.v_float = log(f);
8223 else
8224 rettv->vval.v_float = 0.0;
8225}
8226
8227/*
8228 * "log10()" function
8229 */
8230 static void
8231f_log10(typval_T *argvars, typval_T *rettv)
8232{
8233 float_T f = 0.0;
8234
8235 rettv->v_type = VAR_FLOAT;
8236 if (get_float_arg(argvars, &f) == OK)
8237 rettv->vval.v_float = log10(f);
8238 else
8239 rettv->vval.v_float = 0.0;
8240}
8241#endif
8242
8243#ifdef FEAT_LUA
8244/*
8245 * "luaeval()" function
8246 */
8247 static void
8248f_luaeval(typval_T *argvars, typval_T *rettv)
8249{
8250 char_u *str;
8251 char_u buf[NUMBUFLEN];
8252
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008253 if (check_restricted() || check_secure())
8254 return;
8255
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008256 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008257 do_luaeval(str, argvars + 1, rettv);
8258}
8259#endif
8260
8261/*
8262 * "map()" function
8263 */
8264 static void
8265f_map(typval_T *argvars, typval_T *rettv)
8266{
8267 filter_map(argvars, rettv, TRUE);
8268}
8269
8270/*
8271 * "maparg()" function
8272 */
8273 static void
8274f_maparg(typval_T *argvars, typval_T *rettv)
8275{
8276 get_maparg(argvars, rettv, TRUE);
8277}
8278
8279/*
8280 * "mapcheck()" function
8281 */
8282 static void
8283f_mapcheck(typval_T *argvars, typval_T *rettv)
8284{
8285 get_maparg(argvars, rettv, FALSE);
8286}
8287
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008288typedef enum
8289{
8290 MATCH_END, /* matchend() */
8291 MATCH_MATCH, /* match() */
8292 MATCH_STR, /* matchstr() */
8293 MATCH_LIST, /* matchlist() */
8294 MATCH_POS /* matchstrpos() */
8295} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296
8297 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008298find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299{
8300 char_u *str = NULL;
8301 long len = 0;
8302 char_u *expr = NULL;
8303 char_u *pat;
8304 regmatch_T regmatch;
8305 char_u patbuf[NUMBUFLEN];
8306 char_u strbuf[NUMBUFLEN];
8307 char_u *save_cpo;
8308 long start = 0;
8309 long nth = 1;
8310 colnr_T startcol = 0;
8311 int match = 0;
8312 list_T *l = NULL;
8313 listitem_T *li = NULL;
8314 long idx = 0;
8315 char_u *tofree = NULL;
8316
8317 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8318 save_cpo = p_cpo;
8319 p_cpo = (char_u *)"";
8320
8321 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008322 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008323 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008324 /* type MATCH_LIST: return empty list when there are no matches.
8325 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326 if (rettv_list_alloc(rettv) == FAIL)
8327 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008328 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008329 && (list_append_string(rettv->vval.v_list,
8330 (char_u *)"", 0) == FAIL
8331 || list_append_number(rettv->vval.v_list,
8332 (varnumber_T)-1) == FAIL
8333 || list_append_number(rettv->vval.v_list,
8334 (varnumber_T)-1) == FAIL
8335 || list_append_number(rettv->vval.v_list,
8336 (varnumber_T)-1) == FAIL))
8337 {
8338 list_free(rettv->vval.v_list);
8339 rettv->vval.v_list = NULL;
8340 goto theend;
8341 }
8342 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008343 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008344 {
8345 rettv->v_type = VAR_STRING;
8346 rettv->vval.v_string = NULL;
8347 }
8348
8349 if (argvars[0].v_type == VAR_LIST)
8350 {
8351 if ((l = argvars[0].vval.v_list) == NULL)
8352 goto theend;
8353 li = l->lv_first;
8354 }
8355 else
8356 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008357 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008358 len = (long)STRLEN(str);
8359 }
8360
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008361 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 if (pat == NULL)
8363 goto theend;
8364
8365 if (argvars[2].v_type != VAR_UNKNOWN)
8366 {
8367 int error = FALSE;
8368
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008369 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008370 if (error)
8371 goto theend;
8372 if (l != NULL)
8373 {
8374 li = list_find(l, start);
8375 if (li == NULL)
8376 goto theend;
8377 idx = l->lv_idx; /* use the cached index */
8378 }
8379 else
8380 {
8381 if (start < 0)
8382 start = 0;
8383 if (start > len)
8384 goto theend;
8385 /* When "count" argument is there ignore matches before "start",
8386 * otherwise skip part of the string. Differs when pattern is "^"
8387 * or "\<". */
8388 if (argvars[3].v_type != VAR_UNKNOWN)
8389 startcol = start;
8390 else
8391 {
8392 str += start;
8393 len -= start;
8394 }
8395 }
8396
8397 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008398 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008399 if (error)
8400 goto theend;
8401 }
8402
8403 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8404 if (regmatch.regprog != NULL)
8405 {
8406 regmatch.rm_ic = p_ic;
8407
8408 for (;;)
8409 {
8410 if (l != NULL)
8411 {
8412 if (li == NULL)
8413 {
8414 match = FALSE;
8415 break;
8416 }
8417 vim_free(tofree);
8418 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8419 if (str == NULL)
8420 break;
8421 }
8422
8423 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8424
8425 if (match && --nth <= 0)
8426 break;
8427 if (l == NULL && !match)
8428 break;
8429
8430 /* Advance to just after the match. */
8431 if (l != NULL)
8432 {
8433 li = li->li_next;
8434 ++idx;
8435 }
8436 else
8437 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438 startcol = (colnr_T)(regmatch.startp[0]
8439 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008440 if (startcol > (colnr_T)len
8441 || str + startcol <= regmatch.startp[0])
8442 {
8443 match = FALSE;
8444 break;
8445 }
8446 }
8447 }
8448
8449 if (match)
8450 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008451 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008452 {
8453 listitem_T *li1 = rettv->vval.v_list->lv_first;
8454 listitem_T *li2 = li1->li_next;
8455 listitem_T *li3 = li2->li_next;
8456 listitem_T *li4 = li3->li_next;
8457
8458 vim_free(li1->li_tv.vval.v_string);
8459 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8460 (int)(regmatch.endp[0] - regmatch.startp[0]));
8461 li3->li_tv.vval.v_number =
8462 (varnumber_T)(regmatch.startp[0] - expr);
8463 li4->li_tv.vval.v_number =
8464 (varnumber_T)(regmatch.endp[0] - expr);
8465 if (l != NULL)
8466 li2->li_tv.vval.v_number = (varnumber_T)idx;
8467 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008468 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008469 {
8470 int i;
8471
8472 /* return list with matched string and submatches */
8473 for (i = 0; i < NSUBEXP; ++i)
8474 {
8475 if (regmatch.endp[i] == NULL)
8476 {
8477 if (list_append_string(rettv->vval.v_list,
8478 (char_u *)"", 0) == FAIL)
8479 break;
8480 }
8481 else if (list_append_string(rettv->vval.v_list,
8482 regmatch.startp[i],
8483 (int)(regmatch.endp[i] - regmatch.startp[i]))
8484 == FAIL)
8485 break;
8486 }
8487 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008488 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489 {
8490 /* return matched string */
8491 if (l != NULL)
8492 copy_tv(&li->li_tv, rettv);
8493 else
8494 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8495 (int)(regmatch.endp[0] - regmatch.startp[0]));
8496 }
8497 else if (l != NULL)
8498 rettv->vval.v_number = idx;
8499 else
8500 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008501 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008502 rettv->vval.v_number =
8503 (varnumber_T)(regmatch.startp[0] - str);
8504 else
8505 rettv->vval.v_number =
8506 (varnumber_T)(regmatch.endp[0] - str);
8507 rettv->vval.v_number += (varnumber_T)(str - expr);
8508 }
8509 }
8510 vim_regfree(regmatch.regprog);
8511 }
8512
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008513theend:
8514 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008515 /* matchstrpos() without a list: drop the second item. */
8516 listitem_remove(rettv->vval.v_list,
8517 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008518 vim_free(tofree);
8519 p_cpo = save_cpo;
8520}
8521
8522/*
8523 * "match()" function
8524 */
8525 static void
8526f_match(typval_T *argvars, typval_T *rettv)
8527{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008528 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008529}
8530
Bram Moolenaar95e51472018-07-28 16:55:56 +02008531#ifdef FEAT_SEARCH_EXTRA
8532 static int
8533matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8534{
8535 dictitem_T *di;
8536
8537 if (tv->v_type != VAR_DICT)
8538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008539 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008540 return FAIL;
8541 }
8542
8543 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008544 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008545 (char_u *)"conceal", FALSE);
8546
8547 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8548 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008549 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008550 if (*win == NULL)
8551 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008552 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008553 return FAIL;
8554 }
8555 }
8556
8557 return OK;
8558}
8559#endif
8560
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561/*
8562 * "matchadd()" function
8563 */
8564 static void
8565f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8566{
8567#ifdef FEAT_SEARCH_EXTRA
8568 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008569 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8570 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008571 int prio = 10; /* default priority */
8572 int id = -1;
8573 int error = FALSE;
8574 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008575 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576
8577 rettv->vval.v_number = -1;
8578
8579 if (grp == NULL || pat == NULL)
8580 return;
8581 if (argvars[2].v_type != VAR_UNKNOWN)
8582 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008583 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008584 if (argvars[3].v_type != VAR_UNKNOWN)
8585 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008586 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008587 if (argvars[4].v_type != VAR_UNKNOWN
8588 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8589 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008590 }
8591 }
8592 if (error == TRUE)
8593 return;
8594 if (id >= 1 && id <= 3)
8595 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008596 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008597 return;
8598 }
8599
Bram Moolenaar95e51472018-07-28 16:55:56 +02008600 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008601 conceal_char);
8602#endif
8603}
8604
8605/*
8606 * "matchaddpos()" function
8607 */
8608 static void
8609f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8610{
8611#ifdef FEAT_SEARCH_EXTRA
8612 char_u buf[NUMBUFLEN];
8613 char_u *group;
8614 int prio = 10;
8615 int id = -1;
8616 int error = FALSE;
8617 list_T *l;
8618 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008619 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008620
8621 rettv->vval.v_number = -1;
8622
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008623 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624 if (group == NULL)
8625 return;
8626
8627 if (argvars[1].v_type != VAR_LIST)
8628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008629 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630 return;
8631 }
8632 l = argvars[1].vval.v_list;
8633 if (l == NULL)
8634 return;
8635
8636 if (argvars[2].v_type != VAR_UNKNOWN)
8637 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008638 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008639 if (argvars[3].v_type != VAR_UNKNOWN)
8640 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008641 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008642
8643 if (argvars[4].v_type != VAR_UNKNOWN
8644 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8645 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008646 }
8647 }
8648 if (error == TRUE)
8649 return;
8650
8651 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8652 if (id == 1 || id == 2)
8653 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008654 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 return;
8656 }
8657
Bram Moolenaar95e51472018-07-28 16:55:56 +02008658 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659 conceal_char);
8660#endif
8661}
8662
8663/*
8664 * "matcharg()" function
8665 */
8666 static void
8667f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8668{
8669 if (rettv_list_alloc(rettv) == OK)
8670 {
8671#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008672 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008673 matchitem_T *m;
8674
8675 if (id >= 1 && id <= 3)
8676 {
8677 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8678 {
8679 list_append_string(rettv->vval.v_list,
8680 syn_id2name(m->hlg_id), -1);
8681 list_append_string(rettv->vval.v_list, m->pattern, -1);
8682 }
8683 else
8684 {
8685 list_append_string(rettv->vval.v_list, NULL, -1);
8686 list_append_string(rettv->vval.v_list, NULL, -1);
8687 }
8688 }
8689#endif
8690 }
8691}
8692
8693/*
8694 * "matchdelete()" function
8695 */
8696 static void
8697f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8698{
8699#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008700 win_T *win = get_optional_window(argvars, 1);
8701
8702 if (win == NULL)
8703 rettv->vval.v_number = -1;
8704 else
8705 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008706 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008707#endif
8708}
8709
8710/*
8711 * "matchend()" function
8712 */
8713 static void
8714f_matchend(typval_T *argvars, typval_T *rettv)
8715{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008716 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008717}
8718
8719/*
8720 * "matchlist()" function
8721 */
8722 static void
8723f_matchlist(typval_T *argvars, typval_T *rettv)
8724{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008725 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008726}
8727
8728/*
8729 * "matchstr()" function
8730 */
8731 static void
8732f_matchstr(typval_T *argvars, typval_T *rettv)
8733{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008734 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735}
8736
8737/*
8738 * "matchstrpos()" function
8739 */
8740 static void
8741f_matchstrpos(typval_T *argvars, typval_T *rettv)
8742{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008743 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008744}
8745
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008746 static void
8747max_min(typval_T *argvars, typval_T *rettv, int domax)
8748{
8749 varnumber_T n = 0;
8750 varnumber_T i;
8751 int error = FALSE;
8752
8753 if (argvars[0].v_type == VAR_LIST)
8754 {
8755 list_T *l;
8756 listitem_T *li;
8757
8758 l = argvars[0].vval.v_list;
8759 if (l != NULL)
8760 {
8761 li = l->lv_first;
8762 if (li != NULL)
8763 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008764 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008765 for (;;)
8766 {
8767 li = li->li_next;
8768 if (li == NULL)
8769 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008770 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008771 if (domax ? i > n : i < n)
8772 n = i;
8773 }
8774 }
8775 }
8776 }
8777 else if (argvars[0].v_type == VAR_DICT)
8778 {
8779 dict_T *d;
8780 int first = TRUE;
8781 hashitem_T *hi;
8782 int todo;
8783
8784 d = argvars[0].vval.v_dict;
8785 if (d != NULL)
8786 {
8787 todo = (int)d->dv_hashtab.ht_used;
8788 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8789 {
8790 if (!HASHITEM_EMPTY(hi))
8791 {
8792 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008793 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 if (first)
8795 {
8796 n = i;
8797 first = FALSE;
8798 }
8799 else if (domax ? i > n : i < n)
8800 n = i;
8801 }
8802 }
8803 }
8804 }
8805 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008806 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008807 rettv->vval.v_number = error ? 0 : n;
8808}
8809
8810/*
8811 * "max()" function
8812 */
8813 static void
8814f_max(typval_T *argvars, typval_T *rettv)
8815{
8816 max_min(argvars, rettv, TRUE);
8817}
8818
8819/*
8820 * "min()" function
8821 */
8822 static void
8823f_min(typval_T *argvars, typval_T *rettv)
8824{
8825 max_min(argvars, rettv, FALSE);
8826}
8827
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008828/*
8829 * Create the directory in which "dir" is located, and higher levels when
8830 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008831 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008832 */
8833 static int
8834mkdir_recurse(char_u *dir, int prot)
8835{
8836 char_u *p;
8837 char_u *updir;
8838 int r = FAIL;
8839
8840 /* Get end of directory name in "dir".
8841 * We're done when it's "/" or "c:/". */
8842 p = gettail_sep(dir);
8843 if (p <= get_past_head(dir))
8844 return OK;
8845
8846 /* If the directory exists we're done. Otherwise: create it.*/
8847 updir = vim_strnsave(dir, (int)(p - dir));
8848 if (updir == NULL)
8849 return FAIL;
8850 if (mch_isdir(updir))
8851 r = OK;
8852 else if (mkdir_recurse(updir, prot) == OK)
8853 r = vim_mkdir_emsg(updir, prot);
8854 vim_free(updir);
8855 return r;
8856}
8857
8858#ifdef vim_mkdir
8859/*
8860 * "mkdir()" function
8861 */
8862 static void
8863f_mkdir(typval_T *argvars, typval_T *rettv)
8864{
8865 char_u *dir;
8866 char_u buf[NUMBUFLEN];
8867 int prot = 0755;
8868
8869 rettv->vval.v_number = FAIL;
8870 if (check_restricted() || check_secure())
8871 return;
8872
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008873 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008875 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008877 if (*gettail(dir) == NUL)
8878 /* remove trailing slashes */
8879 *gettail_sep(dir) = NUL;
8880
8881 if (argvars[1].v_type != VAR_UNKNOWN)
8882 {
8883 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008884 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008885 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008886 if (prot == -1)
8887 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008889 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008890 {
8891 if (mch_isdir(dir))
8892 {
8893 /* With the "p" flag it's OK if the dir already exists. */
8894 rettv->vval.v_number = OK;
8895 return;
8896 }
8897 mkdir_recurse(dir, prot);
8898 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008899 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008900 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008901}
8902#endif
8903
8904/*
8905 * "mode()" function
8906 */
8907 static void
8908f_mode(typval_T *argvars, typval_T *rettv)
8909{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008910 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008911
Bram Moolenaar612cc382018-07-29 15:34:26 +02008912 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008913
8914 if (time_for_testing == 93784)
8915 {
8916 /* Testing the two-character code. */
8917 buf[0] = 'x';
8918 buf[1] = '!';
8919 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008920#ifdef FEAT_TERMINAL
8921 else if (term_use_loop())
8922 buf[0] = 't';
8923#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 else if (VIsual_active)
8925 {
8926 if (VIsual_select)
8927 buf[0] = VIsual_mode + 's' - 'v';
8928 else
8929 buf[0] = VIsual_mode;
8930 }
8931 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8932 || State == CONFIRM)
8933 {
8934 buf[0] = 'r';
8935 if (State == ASKMORE)
8936 buf[1] = 'm';
8937 else if (State == CONFIRM)
8938 buf[1] = '?';
8939 }
8940 else if (State == EXTERNCMD)
8941 buf[0] = '!';
8942 else if (State & INSERT)
8943 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008944 if (State & VREPLACE_FLAG)
8945 {
8946 buf[0] = 'R';
8947 buf[1] = 'v';
8948 }
8949 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008950 {
8951 if (State & REPLACE_FLAG)
8952 buf[0] = 'R';
8953 else
8954 buf[0] = 'i';
8955#ifdef FEAT_INS_EXPAND
8956 if (ins_compl_active())
8957 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008958 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008959 buf[1] = 'x';
8960#endif
8961 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008962 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008963 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964 {
8965 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008966 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008967 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008968 else if (exmode_active == EXMODE_NORMAL)
8969 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008970 }
8971 else
8972 {
8973 buf[0] = 'n';
8974 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008975 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008976 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008977 // to be able to detect force-linewise/blockwise/characterwise operations
8978 buf[2] = motion_force;
8979 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008980 else if (restart_edit == 'I' || restart_edit == 'R'
8981 || restart_edit == 'V')
8982 {
8983 buf[1] = 'i';
8984 buf[2] = restart_edit;
8985 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008986 }
8987
8988 /* Clear out the minor mode when the argument is not a non-zero number or
8989 * non-empty string. */
8990 if (!non_zero_arg(&argvars[0]))
8991 buf[1] = NUL;
8992
8993 rettv->vval.v_string = vim_strsave(buf);
8994 rettv->v_type = VAR_STRING;
8995}
8996
8997#if defined(FEAT_MZSCHEME) || defined(PROTO)
8998/*
8999 * "mzeval()" function
9000 */
9001 static void
9002f_mzeval(typval_T *argvars, typval_T *rettv)
9003{
9004 char_u *str;
9005 char_u buf[NUMBUFLEN];
9006
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009007 if (check_restricted() || check_secure())
9008 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009009 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009010 do_mzeval(str, rettv);
9011}
9012
9013 void
9014mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
9015{
9016 typval_T argvars[3];
9017
9018 argvars[0].v_type = VAR_STRING;
9019 argvars[0].vval.v_string = name;
9020 copy_tv(args, &argvars[1]);
9021 argvars[2].v_type = VAR_UNKNOWN;
9022 f_call(argvars, rettv);
9023 clear_tv(&argvars[1]);
9024}
9025#endif
9026
9027/*
9028 * "nextnonblank()" function
9029 */
9030 static void
9031f_nextnonblank(typval_T *argvars, typval_T *rettv)
9032{
9033 linenr_T lnum;
9034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009035 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036 {
9037 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
9038 {
9039 lnum = 0;
9040 break;
9041 }
9042 if (*skipwhite(ml_get(lnum)) != NUL)
9043 break;
9044 }
9045 rettv->vval.v_number = lnum;
9046}
9047
9048/*
9049 * "nr2char()" function
9050 */
9051 static void
9052f_nr2char(typval_T *argvars, typval_T *rettv)
9053{
9054 char_u buf[NUMBUFLEN];
9055
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 if (has_mbyte)
9057 {
9058 int utf8 = 0;
9059
9060 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009061 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009062 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009063 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009064 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009065 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009066 }
9067 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009068 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009069 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009070 buf[1] = NUL;
9071 }
9072 rettv->v_type = VAR_STRING;
9073 rettv->vval.v_string = vim_strsave(buf);
9074}
9075
9076/*
9077 * "or(expr, expr)" function
9078 */
9079 static void
9080f_or(typval_T *argvars, typval_T *rettv)
9081{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009082 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9083 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009084}
9085
9086/*
9087 * "pathshorten()" function
9088 */
9089 static void
9090f_pathshorten(typval_T *argvars, typval_T *rettv)
9091{
9092 char_u *p;
9093
9094 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009095 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009096 if (p == NULL)
9097 rettv->vval.v_string = NULL;
9098 else
9099 {
9100 p = vim_strsave(p);
9101 rettv->vval.v_string = p;
9102 if (p != NULL)
9103 shorten_dir(p);
9104 }
9105}
9106
9107#ifdef FEAT_PERL
9108/*
9109 * "perleval()" function
9110 */
9111 static void
9112f_perleval(typval_T *argvars, typval_T *rettv)
9113{
9114 char_u *str;
9115 char_u buf[NUMBUFLEN];
9116
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009117 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009118 do_perleval(str, rettv);
9119}
9120#endif
9121
9122#ifdef FEAT_FLOAT
9123/*
9124 * "pow()" function
9125 */
9126 static void
9127f_pow(typval_T *argvars, typval_T *rettv)
9128{
9129 float_T fx = 0.0, fy = 0.0;
9130
9131 rettv->v_type = VAR_FLOAT;
9132 if (get_float_arg(argvars, &fx) == OK
9133 && get_float_arg(&argvars[1], &fy) == OK)
9134 rettv->vval.v_float = pow(fx, fy);
9135 else
9136 rettv->vval.v_float = 0.0;
9137}
9138#endif
9139
9140/*
9141 * "prevnonblank()" function
9142 */
9143 static void
9144f_prevnonblank(typval_T *argvars, typval_T *rettv)
9145{
9146 linenr_T lnum;
9147
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009148 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009149 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9150 lnum = 0;
9151 else
9152 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9153 --lnum;
9154 rettv->vval.v_number = lnum;
9155}
9156
9157/* This dummy va_list is here because:
9158 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9159 * - locally in the function results in a "used before set" warning
9160 * - using va_start() to initialize it gives "function with fixed args" error */
9161static va_list ap;
9162
9163/*
9164 * "printf()" function
9165 */
9166 static void
9167f_printf(typval_T *argvars, typval_T *rettv)
9168{
9169 char_u buf[NUMBUFLEN];
9170 int len;
9171 char_u *s;
9172 int saved_did_emsg = did_emsg;
9173 char *fmt;
9174
9175 rettv->v_type = VAR_STRING;
9176 rettv->vval.v_string = NULL;
9177
9178 /* Get the required length, allocate the buffer and do it for real. */
9179 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009180 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009181 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009182 if (!did_emsg)
9183 {
9184 s = alloc(len + 1);
9185 if (s != NULL)
9186 {
9187 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009188 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9189 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009190 }
9191 }
9192 did_emsg |= saved_did_emsg;
9193}
9194
Bram Moolenaarf2732452018-06-03 14:47:35 +02009195#ifdef FEAT_JOB_CHANNEL
9196/*
9197 * "prompt_setcallback({buffer}, {callback})" function
9198 */
9199 static void
9200f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9201{
9202 buf_T *buf;
9203 char_u *callback;
9204 partial_T *partial;
9205
9206 if (check_secure())
9207 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009208 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009209 if (buf == NULL)
9210 return;
9211
9212 callback = get_callback(&argvars[1], &partial);
9213 if (callback == NULL)
9214 return;
9215
9216 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
9217 if (partial == NULL)
9218 buf->b_prompt_callback = vim_strsave(callback);
9219 else
9220 /* pointer into the partial */
9221 buf->b_prompt_callback = callback;
9222 buf->b_prompt_partial = partial;
9223}
9224
9225/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009226 * "prompt_setinterrupt({buffer}, {callback})" function
9227 */
9228 static void
9229f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9230{
9231 buf_T *buf;
9232 char_u *callback;
9233 partial_T *partial;
9234
9235 if (check_secure())
9236 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009237 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009238 if (buf == NULL)
9239 return;
9240
9241 callback = get_callback(&argvars[1], &partial);
9242 if (callback == NULL)
9243 return;
9244
9245 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9246 if (partial == NULL)
9247 buf->b_prompt_interrupt = vim_strsave(callback);
9248 else
9249 /* pointer into the partial */
9250 buf->b_prompt_interrupt = callback;
9251 buf->b_prompt_int_partial = partial;
9252}
9253
9254/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009255 * "prompt_setprompt({buffer}, {text})" function
9256 */
9257 static void
9258f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9259{
9260 buf_T *buf;
9261 char_u *text;
9262
9263 if (check_secure())
9264 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009265 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009266 if (buf == NULL)
9267 return;
9268
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009269 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009270 vim_free(buf->b_prompt_text);
9271 buf->b_prompt_text = vim_strsave(text);
9272}
9273#endif
9274
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009275/*
9276 * "pumvisible()" function
9277 */
9278 static void
9279f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9280{
9281#ifdef FEAT_INS_EXPAND
9282 if (pum_visible())
9283 rettv->vval.v_number = 1;
9284#endif
9285}
9286
9287#ifdef FEAT_PYTHON3
9288/*
9289 * "py3eval()" function
9290 */
9291 static void
9292f_py3eval(typval_T *argvars, typval_T *rettv)
9293{
9294 char_u *str;
9295 char_u buf[NUMBUFLEN];
9296
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009297 if (check_restricted() || check_secure())
9298 return;
9299
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009300 if (p_pyx == 0)
9301 p_pyx = 3;
9302
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009303 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009304 do_py3eval(str, rettv);
9305}
9306#endif
9307
9308#ifdef FEAT_PYTHON
9309/*
9310 * "pyeval()" function
9311 */
9312 static void
9313f_pyeval(typval_T *argvars, typval_T *rettv)
9314{
9315 char_u *str;
9316 char_u buf[NUMBUFLEN];
9317
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009318 if (check_restricted() || check_secure())
9319 return;
9320
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009321 if (p_pyx == 0)
9322 p_pyx = 2;
9323
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009324 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325 do_pyeval(str, rettv);
9326}
9327#endif
9328
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009329#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9330/*
9331 * "pyxeval()" function
9332 */
9333 static void
9334f_pyxeval(typval_T *argvars, typval_T *rettv)
9335{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009336 if (check_restricted() || check_secure())
9337 return;
9338
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009339# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9340 init_pyxversion();
9341 if (p_pyx == 2)
9342 f_pyeval(argvars, rettv);
9343 else
9344 f_py3eval(argvars, rettv);
9345# elif defined(FEAT_PYTHON)
9346 f_pyeval(argvars, rettv);
9347# elif defined(FEAT_PYTHON3)
9348 f_py3eval(argvars, rettv);
9349# endif
9350}
9351#endif
9352
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009353/*
9354 * "range()" function
9355 */
9356 static void
9357f_range(typval_T *argvars, typval_T *rettv)
9358{
9359 varnumber_T start;
9360 varnumber_T end;
9361 varnumber_T stride = 1;
9362 varnumber_T i;
9363 int error = FALSE;
9364
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009365 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009366 if (argvars[1].v_type == VAR_UNKNOWN)
9367 {
9368 end = start - 1;
9369 start = 0;
9370 }
9371 else
9372 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009373 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009374 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009375 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009376 }
9377
9378 if (error)
9379 return; /* type error; errmsg already given */
9380 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009381 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009382 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009383 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009384 else
9385 {
9386 if (rettv_list_alloc(rettv) == OK)
9387 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9388 if (list_append_number(rettv->vval.v_list,
9389 (varnumber_T)i) == FAIL)
9390 break;
9391 }
9392}
9393
9394/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009395 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009396 */
9397 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009398readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009399{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009400 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009401 typval_T save_val;
9402 typval_T rettv;
9403 typval_T argv[2];
9404 int retval = 0;
9405 int error = FALSE;
9406
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009407 if (expr->v_type == VAR_UNKNOWN)
9408 return 1;
9409
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009410 prepare_vimvar(VV_VAL, &save_val);
9411 set_vim_var_string(VV_VAL, name, -1);
9412 argv[0].v_type = VAR_STRING;
9413 argv[0].vval.v_string = name;
9414
9415 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9416 goto theend;
9417
9418 retval = tv_get_number_chk(&rettv, &error);
9419 if (error)
9420 retval = -1;
9421 clear_tv(&rettv);
9422
9423theend:
9424 set_vim_var_string(VV_VAL, NULL, 0);
9425 restore_vimvar(VV_VAL, &save_val);
9426 return retval;
9427}
9428
9429/*
9430 * "readdir()" function
9431 */
9432 static void
9433f_readdir(typval_T *argvars, typval_T *rettv)
9434{
9435 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009436 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009437 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009438 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009439 garray_T ga;
9440 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009441
9442 if (rettv_list_alloc(rettv) == FAIL)
9443 return;
9444 path = tv_get_string(&argvars[0]);
9445 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009446
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009447 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9448 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009449 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009450 for (i = 0; i < ga.ga_len; i++)
9451 {
9452 p = ((char_u **)ga.ga_data)[i];
9453 list_append_string(rettv->vval.v_list, p, -1);
9454 }
9455 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009456 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009457}
9458
9459/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009460 * "readfile()" function
9461 */
9462 static void
9463f_readfile(typval_T *argvars, typval_T *rettv)
9464{
9465 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009466 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009467 int failed = FALSE;
9468 char_u *fname;
9469 FILE *fd;
9470 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9471 int io_size = sizeof(buf);
9472 int readlen; /* size of last fread() */
9473 char_u *prev = NULL; /* previously read bytes, if any */
9474 long prevlen = 0; /* length of data in prev */
9475 long prevsize = 0; /* size of prev buffer */
9476 long maxline = MAXLNUM;
9477 long cnt = 0;
9478 char_u *p; /* position in buf */
9479 char_u *start; /* start of current line */
9480
9481 if (argvars[1].v_type != VAR_UNKNOWN)
9482 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009483 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009484 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009485 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9486 blob = TRUE;
9487
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009488 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009489 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009490 }
9491
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009492 if (blob)
9493 {
9494 if (rettv_blob_alloc(rettv) == FAIL)
9495 return;
9496 }
9497 else
9498 {
9499 if (rettv_list_alloc(rettv) == FAIL)
9500 return;
9501 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009502
9503 /* Always open the file in binary mode, library functions have a mind of
9504 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009505 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009506 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009508 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009509 return;
9510 }
9511
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009512 if (blob)
9513 {
9514 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9515 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009516 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009517 blob_free(rettv->vval.v_blob);
9518 }
9519 fclose(fd);
9520 return;
9521 }
9522
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009523 while (cnt < maxline || maxline < 0)
9524 {
9525 readlen = (int)fread(buf, 1, io_size, fd);
9526
9527 /* This for loop processes what was read, but is also entered at end
9528 * of file so that either:
9529 * - an incomplete line gets written
9530 * - a "binary" file gets an empty line at the end if it ends in a
9531 * newline. */
9532 for (p = buf, start = buf;
9533 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9534 ++p)
9535 {
9536 if (*p == '\n' || readlen <= 0)
9537 {
9538 listitem_T *li;
9539 char_u *s = NULL;
9540 long_u len = p - start;
9541
9542 /* Finished a line. Remove CRs before NL. */
9543 if (readlen > 0 && !binary)
9544 {
9545 while (len > 0 && start[len - 1] == '\r')
9546 --len;
9547 /* removal may cross back to the "prev" string */
9548 if (len == 0)
9549 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9550 --prevlen;
9551 }
9552 if (prevlen == 0)
9553 s = vim_strnsave(start, (int)len);
9554 else
9555 {
9556 /* Change "prev" buffer to be the right size. This way
9557 * the bytes are only copied once, and very long lines are
9558 * allocated only once. */
9559 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9560 {
9561 mch_memmove(s + prevlen, start, len);
9562 s[prevlen + len] = NUL;
9563 prev = NULL; /* the list will own the string */
9564 prevlen = prevsize = 0;
9565 }
9566 }
9567 if (s == NULL)
9568 {
9569 do_outofmem_msg((long_u) prevlen + len + 1);
9570 failed = TRUE;
9571 break;
9572 }
9573
9574 if ((li = listitem_alloc()) == NULL)
9575 {
9576 vim_free(s);
9577 failed = TRUE;
9578 break;
9579 }
9580 li->li_tv.v_type = VAR_STRING;
9581 li->li_tv.v_lock = 0;
9582 li->li_tv.vval.v_string = s;
9583 list_append(rettv->vval.v_list, li);
9584
9585 start = p + 1; /* step over newline */
9586 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9587 break;
9588 }
9589 else if (*p == NUL)
9590 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009591 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9592 * when finding the BF and check the previous two bytes. */
9593 else if (*p == 0xbf && enc_utf8 && !binary)
9594 {
9595 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9596 * + 1, these may be in the "prev" string. */
9597 char_u back1 = p >= buf + 1 ? p[-1]
9598 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9599 char_u back2 = p >= buf + 2 ? p[-2]
9600 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9601 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9602
9603 if (back2 == 0xef && back1 == 0xbb)
9604 {
9605 char_u *dest = p - 2;
9606
9607 /* Usually a BOM is at the beginning of a file, and so at
9608 * the beginning of a line; then we can just step over it.
9609 */
9610 if (start == dest)
9611 start = p + 1;
9612 else
9613 {
9614 /* have to shuffle buf to close gap */
9615 int adjust_prevlen = 0;
9616
9617 if (dest < buf)
9618 {
9619 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9620 dest = buf;
9621 }
9622 if (readlen > p - buf + 1)
9623 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9624 readlen -= 3 - adjust_prevlen;
9625 prevlen -= adjust_prevlen;
9626 p = dest - 1;
9627 }
9628 }
9629 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009630 } /* for */
9631
9632 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9633 break;
9634 if (start < p)
9635 {
9636 /* There's part of a line in buf, store it in "prev". */
9637 if (p - start + prevlen >= prevsize)
9638 {
9639 /* need bigger "prev" buffer */
9640 char_u *newprev;
9641
9642 /* A common use case is ordinary text files and "prev" gets a
9643 * fragment of a line, so the first allocation is made
9644 * small, to avoid repeatedly 'allocing' large and
9645 * 'reallocing' small. */
9646 if (prevsize == 0)
9647 prevsize = (long)(p - start);
9648 else
9649 {
9650 long grow50pc = (prevsize * 3) / 2;
9651 long growmin = (long)((p - start) * 2 + prevlen);
9652 prevsize = grow50pc > growmin ? grow50pc : growmin;
9653 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009654 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009655 if (newprev == NULL)
9656 {
9657 do_outofmem_msg((long_u)prevsize);
9658 failed = TRUE;
9659 break;
9660 }
9661 prev = newprev;
9662 }
9663 /* Add the line part to end of "prev". */
9664 mch_memmove(prev + prevlen, start, p - start);
9665 prevlen += (long)(p - start);
9666 }
9667 } /* while */
9668
9669 /*
9670 * For a negative line count use only the lines at the end of the file,
9671 * free the rest.
9672 */
9673 if (!failed && maxline < 0)
9674 while (cnt > -maxline)
9675 {
9676 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9677 --cnt;
9678 }
9679
9680 if (failed)
9681 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009682 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009684 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009685 }
9686
9687 vim_free(prev);
9688 fclose(fd);
9689}
9690
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009691 static void
9692return_register(int regname, typval_T *rettv)
9693{
9694 char_u buf[2] = {0, 0};
9695
9696 buf[0] = (char_u)regname;
9697 rettv->v_type = VAR_STRING;
9698 rettv->vval.v_string = vim_strsave(buf);
9699}
9700
9701/*
9702 * "reg_executing()" function
9703 */
9704 static void
9705f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9706{
9707 return_register(reg_executing, rettv);
9708}
9709
9710/*
9711 * "reg_recording()" function
9712 */
9713 static void
9714f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9715{
9716 return_register(reg_recording, rettv);
9717}
9718
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009719#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009720/*
9721 * Convert a List to proftime_T.
9722 * Return FAIL when there is something wrong.
9723 */
9724 static int
9725list2proftime(typval_T *arg, proftime_T *tm)
9726{
9727 long n1, n2;
9728 int error = FALSE;
9729
9730 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9731 || arg->vval.v_list->lv_len != 2)
9732 return FAIL;
9733 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9734 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009735# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009736 tm->HighPart = n1;
9737 tm->LowPart = n2;
9738# else
9739 tm->tv_sec = n1;
9740 tm->tv_usec = n2;
9741# endif
9742 return error ? FAIL : OK;
9743}
9744#endif /* FEAT_RELTIME */
9745
9746/*
9747 * "reltime()" function
9748 */
9749 static void
9750f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9751{
9752#ifdef FEAT_RELTIME
9753 proftime_T res;
9754 proftime_T start;
9755
9756 if (argvars[0].v_type == VAR_UNKNOWN)
9757 {
9758 /* No arguments: get current time. */
9759 profile_start(&res);
9760 }
9761 else if (argvars[1].v_type == VAR_UNKNOWN)
9762 {
9763 if (list2proftime(&argvars[0], &res) == FAIL)
9764 return;
9765 profile_end(&res);
9766 }
9767 else
9768 {
9769 /* Two arguments: compute the difference. */
9770 if (list2proftime(&argvars[0], &start) == FAIL
9771 || list2proftime(&argvars[1], &res) == FAIL)
9772 return;
9773 profile_sub(&res, &start);
9774 }
9775
9776 if (rettv_list_alloc(rettv) == OK)
9777 {
9778 long n1, n2;
9779
Bram Moolenaar4f974752019-02-17 17:44:42 +01009780# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009781 n1 = res.HighPart;
9782 n2 = res.LowPart;
9783# else
9784 n1 = res.tv_sec;
9785 n2 = res.tv_usec;
9786# endif
9787 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9788 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9789 }
9790#endif
9791}
9792
9793#ifdef FEAT_FLOAT
9794/*
9795 * "reltimefloat()" function
9796 */
9797 static void
9798f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9799{
9800# ifdef FEAT_RELTIME
9801 proftime_T tm;
9802# endif
9803
9804 rettv->v_type = VAR_FLOAT;
9805 rettv->vval.v_float = 0;
9806# ifdef FEAT_RELTIME
9807 if (list2proftime(&argvars[0], &tm) == OK)
9808 rettv->vval.v_float = profile_float(&tm);
9809# endif
9810}
9811#endif
9812
9813/*
9814 * "reltimestr()" function
9815 */
9816 static void
9817f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9818{
9819#ifdef FEAT_RELTIME
9820 proftime_T tm;
9821#endif
9822
9823 rettv->v_type = VAR_STRING;
9824 rettv->vval.v_string = NULL;
9825#ifdef FEAT_RELTIME
9826 if (list2proftime(&argvars[0], &tm) == OK)
9827 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9828#endif
9829}
9830
9831#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009832 static void
9833make_connection(void)
9834{
9835 if (X_DISPLAY == NULL
9836# ifdef FEAT_GUI
9837 && !gui.in_use
9838# endif
9839 )
9840 {
9841 x_force_connect = TRUE;
9842 setup_term_clip();
9843 x_force_connect = FALSE;
9844 }
9845}
9846
9847 static int
9848check_connection(void)
9849{
9850 make_connection();
9851 if (X_DISPLAY == NULL)
9852 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009853 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009854 return FAIL;
9855 }
9856 return OK;
9857}
9858#endif
9859
9860#ifdef FEAT_CLIENTSERVER
9861 static void
9862remote_common(typval_T *argvars, typval_T *rettv, int expr)
9863{
9864 char_u *server_name;
9865 char_u *keys;
9866 char_u *r = NULL;
9867 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009868 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009869# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009870 HWND w;
9871# else
9872 Window w;
9873# endif
9874
9875 if (check_restricted() || check_secure())
9876 return;
9877
9878# ifdef FEAT_X11
9879 if (check_connection() == FAIL)
9880 return;
9881# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009882 if (argvars[2].v_type != VAR_UNKNOWN
9883 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009884 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009885
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009886 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009887 if (server_name == NULL)
9888 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009889 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009890# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009891 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009892# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009893 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9894 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009895# endif
9896 {
9897 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009899 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009900 vim_free(r);
9901 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009902 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009903 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009904 return;
9905 }
9906
9907 rettv->vval.v_string = r;
9908
9909 if (argvars[2].v_type != VAR_UNKNOWN)
9910 {
9911 dictitem_T v;
9912 char_u str[30];
9913 char_u *idvar;
9914
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009915 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009916 if (idvar != NULL && *idvar != NUL)
9917 {
9918 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9919 v.di_tv.v_type = VAR_STRING;
9920 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009922 vim_free(v.di_tv.vval.v_string);
9923 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009924 }
9925}
9926#endif
9927
9928/*
9929 * "remote_expr()" function
9930 */
9931 static void
9932f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9933{
9934 rettv->v_type = VAR_STRING;
9935 rettv->vval.v_string = NULL;
9936#ifdef FEAT_CLIENTSERVER
9937 remote_common(argvars, rettv, TRUE);
9938#endif
9939}
9940
9941/*
9942 * "remote_foreground()" function
9943 */
9944 static void
9945f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9946{
9947#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009948# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949 /* On Win32 it's done in this application. */
9950 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009951 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009952
9953 if (server_name != NULL)
9954 serverForeground(server_name);
9955 }
9956# else
9957 /* Send a foreground() expression to the server. */
9958 argvars[1].v_type = VAR_STRING;
9959 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9960 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009961 rettv->v_type = VAR_STRING;
9962 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009963 remote_common(argvars, rettv, TRUE);
9964 vim_free(argvars[1].vval.v_string);
9965# endif
9966#endif
9967}
9968
9969 static void
9970f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9971{
9972#ifdef FEAT_CLIENTSERVER
9973 dictitem_T v;
9974 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009975# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009976 long_u n = 0;
9977# endif
9978 char_u *serverid;
9979
9980 if (check_restricted() || check_secure())
9981 {
9982 rettv->vval.v_number = -1;
9983 return;
9984 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009985 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009986 if (serverid == NULL)
9987 {
9988 rettv->vval.v_number = -1;
9989 return; /* type error; errmsg already given */
9990 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009991# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009992 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9993 if (n == 0)
9994 rettv->vval.v_number = -1;
9995 else
9996 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009997 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009998 rettv->vval.v_number = (s != NULL);
9999 }
10000# else
10001 if (check_connection() == FAIL)
10002 return;
10003
10004 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10005 serverStrToWin(serverid), &s);
10006# endif
10007
10008 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10009 {
10010 char_u *retvar;
10011
10012 v.di_tv.v_type = VAR_STRING;
10013 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010014 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010015 if (retvar != NULL)
10016 set_var(retvar, &v.di_tv, FALSE);
10017 vim_free(v.di_tv.vval.v_string);
10018 }
10019#else
10020 rettv->vval.v_number = -1;
10021#endif
10022}
10023
10024 static void
10025f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
10026{
10027 char_u *r = NULL;
10028
10029#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010030 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010031
10032 if (serverid != NULL && !check_restricted() && !check_secure())
10033 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010034 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010035# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010036 /* The server's HWND is encoded in the 'id' parameter */
10037 long_u n = 0;
10038# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010039
10040 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010041 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010042
Bram Moolenaar4f974752019-02-17 17:44:42 +010010043# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010044 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10045 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010046 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010047 if (r == NULL)
10048# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010049 if (check_connection() == FAIL
10050 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10051 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010052# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010053 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010054 }
10055#endif
10056 rettv->v_type = VAR_STRING;
10057 rettv->vval.v_string = r;
10058}
10059
10060/*
10061 * "remote_send()" function
10062 */
10063 static void
10064f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10065{
10066 rettv->v_type = VAR_STRING;
10067 rettv->vval.v_string = NULL;
10068#ifdef FEAT_CLIENTSERVER
10069 remote_common(argvars, rettv, FALSE);
10070#endif
10071}
10072
10073/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010074 * "remote_startserver()" function
10075 */
10076 static void
10077f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10078{
10079#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010080 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010081
10082 if (server == NULL)
10083 return; /* type error; errmsg already given */
10084 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010085 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010086 else
10087 {
10088# ifdef FEAT_X11
10089 if (check_connection() == OK)
10090 serverRegisterName(X_DISPLAY, server);
10091# else
10092 serverSetName(server);
10093# endif
10094 }
10095#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010096 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010097#endif
10098}
10099
10100/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010101 * "remove()" function
10102 */
10103 static void
10104f_remove(typval_T *argvars, typval_T *rettv)
10105{
10106 list_T *l;
10107 listitem_T *item, *item2;
10108 listitem_T *li;
10109 long idx;
10110 long end;
10111 char_u *key;
10112 dict_T *d;
10113 dictitem_T *di;
10114 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010115 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010116
10117 if (argvars[0].v_type == VAR_DICT)
10118 {
10119 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010120 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010121 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010122 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010123 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010124 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010125 if (key != NULL)
10126 {
10127 di = dict_find(d, key, -1);
10128 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010129 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010130 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10131 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10132 {
10133 *rettv = di->di_tv;
10134 init_tv(&di->di_tv);
10135 dictitem_remove(d, di);
10136 }
10137 }
10138 }
10139 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010140 else if (argvars[0].v_type == VAR_BLOB)
10141 {
10142 idx = (long)tv_get_number_chk(&argvars[1], &error);
10143 if (!error)
10144 {
10145 blob_T *b = argvars[0].vval.v_blob;
10146 int len = blob_len(b);
10147 char_u *p;
10148
10149 if (idx < 0)
10150 // count from the end
10151 idx = len + idx;
10152 if (idx < 0 || idx >= len)
10153 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010154 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010155 return;
10156 }
10157 if (argvars[2].v_type == VAR_UNKNOWN)
10158 {
10159 // Remove one item, return its value.
10160 p = (char_u *)b->bv_ga.ga_data;
10161 rettv->vval.v_number = (varnumber_T) *(p + idx);
10162 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10163 --b->bv_ga.ga_len;
10164 }
10165 else
10166 {
10167 blob_T *blob;
10168
10169 // Remove range of items, return list with values.
10170 end = (long)tv_get_number_chk(&argvars[2], &error);
10171 if (error)
10172 return;
10173 if (end < 0)
10174 // count from the end
10175 end = len + end;
10176 if (end >= len || idx > end)
10177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010178 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010179 return;
10180 }
10181 blob = blob_alloc();
10182 if (blob == NULL)
10183 return;
10184 blob->bv_ga.ga_len = end - idx + 1;
10185 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10186 {
10187 vim_free(blob);
10188 return;
10189 }
10190 p = (char_u *)b->bv_ga.ga_data;
10191 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10192 (size_t)(end - idx + 1));
10193 ++blob->bv_refcount;
10194 rettv->v_type = VAR_BLOB;
10195 rettv->vval.v_blob = blob;
10196
10197 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10198 b->bv_ga.ga_len -= end - idx + 1;
10199 }
10200 }
10201 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010202 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010203 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010204 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010205 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010206 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010207 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010208 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010209 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010210 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010211 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010212 else
10213 {
10214 if (argvars[2].v_type == VAR_UNKNOWN)
10215 {
10216 /* Remove one item, return its value. */
10217 vimlist_remove(l, item, item);
10218 *rettv = item->li_tv;
10219 vim_free(item);
10220 }
10221 else
10222 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010223 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010224 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010225 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010226 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010227 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010228 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010229 else
10230 {
10231 int cnt = 0;
10232
10233 for (li = item; li != NULL; li = li->li_next)
10234 {
10235 ++cnt;
10236 if (li == item2)
10237 break;
10238 }
10239 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010240 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010241 else
10242 {
10243 vimlist_remove(l, item, item2);
10244 if (rettv_list_alloc(rettv) == OK)
10245 {
10246 l = rettv->vval.v_list;
10247 l->lv_first = item;
10248 l->lv_last = item2;
10249 item->li_prev = NULL;
10250 item2->li_next = NULL;
10251 l->lv_len = cnt;
10252 }
10253 }
10254 }
10255 }
10256 }
10257 }
10258}
10259
10260/*
10261 * "rename({from}, {to})" function
10262 */
10263 static void
10264f_rename(typval_T *argvars, typval_T *rettv)
10265{
10266 char_u buf[NUMBUFLEN];
10267
10268 if (check_restricted() || check_secure())
10269 rettv->vval.v_number = -1;
10270 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010271 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10272 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010273}
10274
10275/*
10276 * "repeat()" function
10277 */
10278 static void
10279f_repeat(typval_T *argvars, typval_T *rettv)
10280{
10281 char_u *p;
10282 int n;
10283 int slen;
10284 int len;
10285 char_u *r;
10286 int i;
10287
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010288 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010289 if (argvars[0].v_type == VAR_LIST)
10290 {
10291 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10292 while (n-- > 0)
10293 if (list_extend(rettv->vval.v_list,
10294 argvars[0].vval.v_list, NULL) == FAIL)
10295 break;
10296 }
10297 else
10298 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010299 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010300 rettv->v_type = VAR_STRING;
10301 rettv->vval.v_string = NULL;
10302
10303 slen = (int)STRLEN(p);
10304 len = slen * n;
10305 if (len <= 0)
10306 return;
10307
10308 r = alloc(len + 1);
10309 if (r != NULL)
10310 {
10311 for (i = 0; i < n; i++)
10312 mch_memmove(r + i * slen, p, (size_t)slen);
10313 r[len] = NUL;
10314 }
10315
10316 rettv->vval.v_string = r;
10317 }
10318}
10319
10320/*
10321 * "resolve()" function
10322 */
10323 static void
10324f_resolve(typval_T *argvars, typval_T *rettv)
10325{
10326 char_u *p;
10327#ifdef HAVE_READLINK
10328 char_u *buf = NULL;
10329#endif
10330
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010331 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010332#ifdef FEAT_SHORTCUT
10333 {
10334 char_u *v = NULL;
10335
Bram Moolenaardce1e892019-02-10 23:18:53 +010010336 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010337 if (v != NULL)
10338 rettv->vval.v_string = v;
10339 else
10340 rettv->vval.v_string = vim_strsave(p);
10341 }
10342#else
10343# ifdef HAVE_READLINK
10344 {
10345 char_u *cpy;
10346 int len;
10347 char_u *remain = NULL;
10348 char_u *q;
10349 int is_relative_to_current = FALSE;
10350 int has_trailing_pathsep = FALSE;
10351 int limit = 100;
10352
10353 p = vim_strsave(p);
10354
10355 if (p[0] == '.' && (vim_ispathsep(p[1])
10356 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10357 is_relative_to_current = TRUE;
10358
10359 len = STRLEN(p);
10360 if (len > 0 && after_pathsep(p, p + len))
10361 {
10362 has_trailing_pathsep = TRUE;
10363 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10364 }
10365
10366 q = getnextcomp(p);
10367 if (*q != NUL)
10368 {
10369 /* Separate the first path component in "p", and keep the
10370 * remainder (beginning with the path separator). */
10371 remain = vim_strsave(q - 1);
10372 q[-1] = NUL;
10373 }
10374
10375 buf = alloc(MAXPATHL + 1);
10376 if (buf == NULL)
10377 goto fail;
10378
10379 for (;;)
10380 {
10381 for (;;)
10382 {
10383 len = readlink((char *)p, (char *)buf, MAXPATHL);
10384 if (len <= 0)
10385 break;
10386 buf[len] = NUL;
10387
10388 if (limit-- == 0)
10389 {
10390 vim_free(p);
10391 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010392 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010393 rettv->vval.v_string = NULL;
10394 goto fail;
10395 }
10396
10397 /* Ensure that the result will have a trailing path separator
10398 * if the argument has one. */
10399 if (remain == NULL && has_trailing_pathsep)
10400 add_pathsep(buf);
10401
10402 /* Separate the first path component in the link value and
10403 * concatenate the remainders. */
10404 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10405 if (*q != NUL)
10406 {
10407 if (remain == NULL)
10408 remain = vim_strsave(q - 1);
10409 else
10410 {
10411 cpy = concat_str(q - 1, remain);
10412 if (cpy != NULL)
10413 {
10414 vim_free(remain);
10415 remain = cpy;
10416 }
10417 }
10418 q[-1] = NUL;
10419 }
10420
10421 q = gettail(p);
10422 if (q > p && *q == NUL)
10423 {
10424 /* Ignore trailing path separator. */
10425 q[-1] = NUL;
10426 q = gettail(p);
10427 }
10428 if (q > p && !mch_isFullName(buf))
10429 {
10430 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010431 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010432 if (cpy != NULL)
10433 {
10434 STRCPY(cpy, p);
10435 STRCPY(gettail(cpy), buf);
10436 vim_free(p);
10437 p = cpy;
10438 }
10439 }
10440 else
10441 {
10442 vim_free(p);
10443 p = vim_strsave(buf);
10444 }
10445 }
10446
10447 if (remain == NULL)
10448 break;
10449
10450 /* Append the first path component of "remain" to "p". */
10451 q = getnextcomp(remain + 1);
10452 len = q - remain - (*q != NUL);
10453 cpy = vim_strnsave(p, STRLEN(p) + len);
10454 if (cpy != NULL)
10455 {
10456 STRNCAT(cpy, remain, len);
10457 vim_free(p);
10458 p = cpy;
10459 }
10460 /* Shorten "remain". */
10461 if (*q != NUL)
10462 STRMOVE(remain, q - 1);
10463 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010464 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010465 }
10466
10467 /* If the result is a relative path name, make it explicitly relative to
10468 * the current directory if and only if the argument had this form. */
10469 if (!vim_ispathsep(*p))
10470 {
10471 if (is_relative_to_current
10472 && *p != NUL
10473 && !(p[0] == '.'
10474 && (p[1] == NUL
10475 || vim_ispathsep(p[1])
10476 || (p[1] == '.'
10477 && (p[2] == NUL
10478 || vim_ispathsep(p[2]))))))
10479 {
10480 /* Prepend "./". */
10481 cpy = concat_str((char_u *)"./", p);
10482 if (cpy != NULL)
10483 {
10484 vim_free(p);
10485 p = cpy;
10486 }
10487 }
10488 else if (!is_relative_to_current)
10489 {
10490 /* Strip leading "./". */
10491 q = p;
10492 while (q[0] == '.' && vim_ispathsep(q[1]))
10493 q += 2;
10494 if (q > p)
10495 STRMOVE(p, p + 2);
10496 }
10497 }
10498
10499 /* Ensure that the result will have no trailing path separator
10500 * if the argument had none. But keep "/" or "//". */
10501 if (!has_trailing_pathsep)
10502 {
10503 q = p + STRLEN(p);
10504 if (after_pathsep(p, q))
10505 *gettail_sep(p) = NUL;
10506 }
10507
10508 rettv->vval.v_string = p;
10509 }
10510# else
10511 rettv->vval.v_string = vim_strsave(p);
10512# endif
10513#endif
10514
10515 simplify_filename(rettv->vval.v_string);
10516
10517#ifdef HAVE_READLINK
10518fail:
10519 vim_free(buf);
10520#endif
10521 rettv->v_type = VAR_STRING;
10522}
10523
10524/*
10525 * "reverse({list})" function
10526 */
10527 static void
10528f_reverse(typval_T *argvars, typval_T *rettv)
10529{
10530 list_T *l;
10531 listitem_T *li, *ni;
10532
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010533 if (argvars[0].v_type == VAR_BLOB)
10534 {
10535 blob_T *b = argvars[0].vval.v_blob;
10536 int i, len = blob_len(b);
10537
10538 for (i = 0; i < len / 2; i++)
10539 {
10540 int tmp = blob_get(b, i);
10541
10542 blob_set(b, i, blob_get(b, len - i - 1));
10543 blob_set(b, len - i - 1, tmp);
10544 }
10545 rettv_blob_set(rettv, b);
10546 return;
10547 }
10548
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010549 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010550 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010551 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010552 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010553 (char_u *)N_("reverse() argument"), TRUE))
10554 {
10555 li = l->lv_last;
10556 l->lv_first = l->lv_last = NULL;
10557 l->lv_len = 0;
10558 while (li != NULL)
10559 {
10560 ni = li->li_prev;
10561 list_append(l, li);
10562 li = ni;
10563 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010564 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010565 l->lv_idx = l->lv_len - l->lv_idx - 1;
10566 }
10567}
10568
10569#define SP_NOMOVE 0x01 /* don't move cursor */
10570#define SP_REPEAT 0x02 /* repeat to find outer pair */
10571#define SP_RETCOUNT 0x04 /* return matchcount */
10572#define SP_SETPCMARK 0x08 /* set previous context mark */
10573#define SP_START 0x10 /* accept match at start position */
10574#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10575#define SP_END 0x40 /* leave cursor at end of match */
10576#define SP_COLUMN 0x80 /* start at cursor column */
10577
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010578/*
10579 * Get flags for a search function.
10580 * Possibly sets "p_ws".
10581 * Returns BACKWARD, FORWARD or zero (for an error).
10582 */
10583 static int
10584get_search_arg(typval_T *varp, int *flagsp)
10585{
10586 int dir = FORWARD;
10587 char_u *flags;
10588 char_u nbuf[NUMBUFLEN];
10589 int mask;
10590
10591 if (varp->v_type != VAR_UNKNOWN)
10592 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010593 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010594 if (flags == NULL)
10595 return 0; /* type error; errmsg already given */
10596 while (*flags != NUL)
10597 {
10598 switch (*flags)
10599 {
10600 case 'b': dir = BACKWARD; break;
10601 case 'w': p_ws = TRUE; break;
10602 case 'W': p_ws = FALSE; break;
10603 default: mask = 0;
10604 if (flagsp != NULL)
10605 switch (*flags)
10606 {
10607 case 'c': mask = SP_START; break;
10608 case 'e': mask = SP_END; break;
10609 case 'm': mask = SP_RETCOUNT; break;
10610 case 'n': mask = SP_NOMOVE; break;
10611 case 'p': mask = SP_SUBPAT; break;
10612 case 'r': mask = SP_REPEAT; break;
10613 case 's': mask = SP_SETPCMARK; break;
10614 case 'z': mask = SP_COLUMN; break;
10615 }
10616 if (mask == 0)
10617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010618 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619 dir = 0;
10620 }
10621 else
10622 *flagsp |= mask;
10623 }
10624 if (dir == 0)
10625 break;
10626 ++flags;
10627 }
10628 }
10629 return dir;
10630}
10631
10632/*
10633 * Shared by search() and searchpos() functions.
10634 */
10635 static int
10636search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10637{
10638 int flags;
10639 char_u *pat;
10640 pos_T pos;
10641 pos_T save_cursor;
10642 int save_p_ws = p_ws;
10643 int dir;
10644 int retval = 0; /* default: FAIL */
10645 long lnum_stop = 0;
10646 proftime_T tm;
10647#ifdef FEAT_RELTIME
10648 long time_limit = 0;
10649#endif
10650 int options = SEARCH_KEEP;
10651 int subpatnum;
10652
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010653 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010654 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10655 if (dir == 0)
10656 goto theend;
10657 flags = *flagsp;
10658 if (flags & SP_START)
10659 options |= SEARCH_START;
10660 if (flags & SP_END)
10661 options |= SEARCH_END;
10662 if (flags & SP_COLUMN)
10663 options |= SEARCH_COL;
10664
10665 /* Optional arguments: line number to stop searching and timeout. */
10666 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10667 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010668 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010669 if (lnum_stop < 0)
10670 goto theend;
10671#ifdef FEAT_RELTIME
10672 if (argvars[3].v_type != VAR_UNKNOWN)
10673 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010674 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010675 if (time_limit < 0)
10676 goto theend;
10677 }
10678#endif
10679 }
10680
10681#ifdef FEAT_RELTIME
10682 /* Set the time limit, if there is one. */
10683 profile_setlimit(time_limit, &tm);
10684#endif
10685
10686 /*
10687 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10688 * Check to make sure only those flags are set.
10689 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10690 * flags cannot be set. Check for that condition also.
10691 */
10692 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10693 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10694 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010695 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010696 goto theend;
10697 }
10698
10699 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010700 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010701 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010702 if (subpatnum != FAIL)
10703 {
10704 if (flags & SP_SUBPAT)
10705 retval = subpatnum;
10706 else
10707 retval = pos.lnum;
10708 if (flags & SP_SETPCMARK)
10709 setpcmark();
10710 curwin->w_cursor = pos;
10711 if (match_pos != NULL)
10712 {
10713 /* Store the match cursor position */
10714 match_pos->lnum = pos.lnum;
10715 match_pos->col = pos.col + 1;
10716 }
10717 /* "/$" will put the cursor after the end of the line, may need to
10718 * correct that here */
10719 check_cursor();
10720 }
10721
10722 /* If 'n' flag is used: restore cursor position. */
10723 if (flags & SP_NOMOVE)
10724 curwin->w_cursor = save_cursor;
10725 else
10726 curwin->w_set_curswant = TRUE;
10727theend:
10728 p_ws = save_p_ws;
10729
10730 return retval;
10731}
10732
10733#ifdef FEAT_FLOAT
10734
10735/*
10736 * round() is not in C90, use ceil() or floor() instead.
10737 */
10738 float_T
10739vim_round(float_T f)
10740{
10741 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10742}
10743
10744/*
10745 * "round({float})" function
10746 */
10747 static void
10748f_round(typval_T *argvars, typval_T *rettv)
10749{
10750 float_T f = 0.0;
10751
10752 rettv->v_type = VAR_FLOAT;
10753 if (get_float_arg(argvars, &f) == OK)
10754 rettv->vval.v_float = vim_round(f);
10755 else
10756 rettv->vval.v_float = 0.0;
10757}
10758#endif
10759
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010760#ifdef FEAT_RUBY
10761/*
10762 * "rubyeval()" function
10763 */
10764 static void
10765f_rubyeval(typval_T *argvars, typval_T *rettv)
10766{
10767 char_u *str;
10768 char_u buf[NUMBUFLEN];
10769
10770 str = tv_get_string_buf(&argvars[0], buf);
10771 do_rubyeval(str, rettv);
10772}
10773#endif
10774
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010775/*
10776 * "screenattr()" function
10777 */
10778 static void
10779f_screenattr(typval_T *argvars, typval_T *rettv)
10780{
10781 int row;
10782 int col;
10783 int c;
10784
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010785 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10786 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010787 if (row < 0 || row >= screen_Rows
10788 || col < 0 || col >= screen_Columns)
10789 c = -1;
10790 else
10791 c = ScreenAttrs[LineOffset[row] + col];
10792 rettv->vval.v_number = c;
10793}
10794
10795/*
10796 * "screenchar()" function
10797 */
10798 static void
10799f_screenchar(typval_T *argvars, typval_T *rettv)
10800{
10801 int row;
10802 int col;
10803 int off;
10804 int c;
10805
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010806 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10807 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010808 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010809 c = -1;
10810 else
10811 {
10812 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010813 if (enc_utf8 && ScreenLinesUC[off] != 0)
10814 c = ScreenLinesUC[off];
10815 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010816 c = ScreenLines[off];
10817 }
10818 rettv->vval.v_number = c;
10819}
10820
10821/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010822 * "screenchars()" function
10823 */
10824 static void
10825f_screenchars(typval_T *argvars, typval_T *rettv)
10826{
10827 int row;
10828 int col;
10829 int off;
10830 int c;
10831 int i;
10832
10833 if (rettv_list_alloc(rettv) == FAIL)
10834 return;
10835 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10836 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10837 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10838 return;
10839
10840 off = LineOffset[row] + col;
10841 if (enc_utf8 && ScreenLinesUC[off] != 0)
10842 c = ScreenLinesUC[off];
10843 else
10844 c = ScreenLines[off];
10845 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10846
10847 if (enc_utf8)
10848
10849 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10850 list_append_number(rettv->vval.v_list,
10851 (varnumber_T)ScreenLinesC[i][off]);
10852}
10853
10854/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010855 * "screencol()" function
10856 *
10857 * First column is 1 to be consistent with virtcol().
10858 */
10859 static void
10860f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10861{
10862 rettv->vval.v_number = screen_screencol() + 1;
10863}
10864
10865/*
10866 * "screenrow()" function
10867 */
10868 static void
10869f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10870{
10871 rettv->vval.v_number = screen_screenrow() + 1;
10872}
10873
10874/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010875 * "screenstring()" function
10876 */
10877 static void
10878f_screenstring(typval_T *argvars, typval_T *rettv)
10879{
10880 int row;
10881 int col;
10882 int off;
10883 int c;
10884 int i;
10885 char_u buf[MB_MAXBYTES + 1];
10886 int buflen = 0;
10887
10888 rettv->vval.v_string = NULL;
10889 rettv->v_type = VAR_STRING;
10890
10891 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10892 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10893 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10894 return;
10895
10896 off = LineOffset[row] + col;
10897 if (enc_utf8 && ScreenLinesUC[off] != 0)
10898 c = ScreenLinesUC[off];
10899 else
10900 c = ScreenLines[off];
10901 buflen += mb_char2bytes(c, buf);
10902
10903 if (enc_utf8)
10904 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10905 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10906
10907 buf[buflen] = NUL;
10908 rettv->vval.v_string = vim_strsave(buf);
10909}
10910
10911/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010912 * "search()" function
10913 */
10914 static void
10915f_search(typval_T *argvars, typval_T *rettv)
10916{
10917 int flags = 0;
10918
10919 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10920}
10921
10922/*
10923 * "searchdecl()" function
10924 */
10925 static void
10926f_searchdecl(typval_T *argvars, typval_T *rettv)
10927{
10928 int locally = 1;
10929 int thisblock = 0;
10930 int error = FALSE;
10931 char_u *name;
10932
10933 rettv->vval.v_number = 1; /* default: FAIL */
10934
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010935 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010936 if (argvars[1].v_type != VAR_UNKNOWN)
10937 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010938 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010939 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010940 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010941 }
10942 if (!error && name != NULL)
10943 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10944 locally, thisblock, SEARCH_KEEP) == FAIL;
10945}
10946
10947/*
10948 * Used by searchpair() and searchpairpos()
10949 */
10950 static int
10951searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10952{
10953 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010954 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010955 int save_p_ws = p_ws;
10956 int dir;
10957 int flags = 0;
10958 char_u nbuf1[NUMBUFLEN];
10959 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010960 int retval = 0; /* default: FAIL */
10961 long lnum_stop = 0;
10962 long time_limit = 0;
10963
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010964 /* Get the three pattern arguments: start, middle, end. Will result in an
10965 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010966 spat = tv_get_string_chk(&argvars[0]);
10967 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10968 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010969 if (spat == NULL || mpat == NULL || epat == NULL)
10970 goto theend; /* type error */
10971
10972 /* Handle the optional fourth argument: flags */
10973 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10974 if (dir == 0)
10975 goto theend;
10976
10977 /* Don't accept SP_END or SP_SUBPAT.
10978 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10979 */
10980 if ((flags & (SP_END | SP_SUBPAT)) != 0
10981 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010983 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010984 goto theend;
10985 }
10986
10987 /* Using 'r' implies 'W', otherwise it doesn't work. */
10988 if (flags & SP_REPEAT)
10989 p_ws = FALSE;
10990
10991 /* Optional fifth argument: skip expression */
10992 if (argvars[3].v_type == VAR_UNKNOWN
10993 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010994 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010995 else
10996 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010997 skip = &argvars[4];
10998 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10999 && skip->v_type != VAR_STRING)
11000 {
11001 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011002 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010011003 goto theend;
11004 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011005 if (argvars[5].v_type != VAR_UNKNOWN)
11006 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011007 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011008 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011009 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011010 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011011 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011012 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011013#ifdef FEAT_RELTIME
11014 if (argvars[6].v_type != VAR_UNKNOWN)
11015 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011016 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011017 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011018 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011019 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011020 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011021 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011022 }
11023#endif
11024 }
11025 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011026
11027 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
11028 match_pos, lnum_stop, time_limit);
11029
11030theend:
11031 p_ws = save_p_ws;
11032
11033 return retval;
11034}
11035
11036/*
11037 * "searchpair()" function
11038 */
11039 static void
11040f_searchpair(typval_T *argvars, typval_T *rettv)
11041{
11042 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11043}
11044
11045/*
11046 * "searchpairpos()" function
11047 */
11048 static void
11049f_searchpairpos(typval_T *argvars, typval_T *rettv)
11050{
11051 pos_T match_pos;
11052 int lnum = 0;
11053 int col = 0;
11054
11055 if (rettv_list_alloc(rettv) == FAIL)
11056 return;
11057
11058 if (searchpair_cmn(argvars, &match_pos) > 0)
11059 {
11060 lnum = match_pos.lnum;
11061 col = match_pos.col;
11062 }
11063
11064 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11065 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11066}
11067
11068/*
11069 * Search for a start/middle/end thing.
11070 * Used by searchpair(), see its documentation for the details.
11071 * Returns 0 or -1 for no match,
11072 */
11073 long
11074do_searchpair(
11075 char_u *spat, /* start pattern */
11076 char_u *mpat, /* middle pattern */
11077 char_u *epat, /* end pattern */
11078 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011079 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011080 int flags, /* SP_SETPCMARK and other SP_ values */
11081 pos_T *match_pos,
11082 linenr_T lnum_stop, /* stop at this line if not zero */
11083 long time_limit UNUSED) /* stop after this many msec */
11084{
11085 char_u *save_cpo;
11086 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11087 long retval = 0;
11088 pos_T pos;
11089 pos_T firstpos;
11090 pos_T foundpos;
11091 pos_T save_cursor;
11092 pos_T save_pos;
11093 int n;
11094 int r;
11095 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011096 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011097 int err;
11098 int options = SEARCH_KEEP;
11099 proftime_T tm;
11100
11101 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11102 save_cpo = p_cpo;
11103 p_cpo = empty_option;
11104
11105#ifdef FEAT_RELTIME
11106 /* Set the time limit, if there is one. */
11107 profile_setlimit(time_limit, &tm);
11108#endif
11109
11110 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11111 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011112 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11113 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011114 if (pat2 == NULL || pat3 == NULL)
11115 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011116 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011117 if (*mpat == NUL)
11118 STRCPY(pat3, pat2);
11119 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011120 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011121 spat, epat, mpat);
11122 if (flags & SP_START)
11123 options |= SEARCH_START;
11124
Bram Moolenaar48570482017-10-30 21:48:41 +010011125 if (skip != NULL)
11126 {
11127 /* Empty string means to not use the skip expression. */
11128 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11129 use_skip = skip->vval.v_string != NULL
11130 && *skip->vval.v_string != NUL;
11131 }
11132
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011133 save_cursor = curwin->w_cursor;
11134 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011135 CLEAR_POS(&firstpos);
11136 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011137 pat = pat3;
11138 for (;;)
11139 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011140 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011141 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011142 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011143 /* didn't find it or found the first match again: FAIL */
11144 break;
11145
11146 if (firstpos.lnum == 0)
11147 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011148 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011149 {
11150 /* Found the same position again. Can happen with a pattern that
11151 * has "\zs" at the end and searching backwards. Advance one
11152 * character and try again. */
11153 if (dir == BACKWARD)
11154 decl(&pos);
11155 else
11156 incl(&pos);
11157 }
11158 foundpos = pos;
11159
11160 /* clear the start flag to avoid getting stuck here */
11161 options &= ~SEARCH_START;
11162
11163 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011164 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011165 {
11166 save_pos = curwin->w_cursor;
11167 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011168 err = FALSE;
11169 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011170 curwin->w_cursor = save_pos;
11171 if (err)
11172 {
11173 /* Evaluating {skip} caused an error, break here. */
11174 curwin->w_cursor = save_cursor;
11175 retval = -1;
11176 break;
11177 }
11178 if (r)
11179 continue;
11180 }
11181
11182 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11183 {
11184 /* Found end when searching backwards or start when searching
11185 * forward: nested pair. */
11186 ++nest;
11187 pat = pat2; /* nested, don't search for middle */
11188 }
11189 else
11190 {
11191 /* Found end when searching forward or start when searching
11192 * backward: end of (nested) pair; or found middle in outer pair. */
11193 if (--nest == 1)
11194 pat = pat3; /* outer level, search for middle */
11195 }
11196
11197 if (nest == 0)
11198 {
11199 /* Found the match: return matchcount or line number. */
11200 if (flags & SP_RETCOUNT)
11201 ++retval;
11202 else
11203 retval = pos.lnum;
11204 if (flags & SP_SETPCMARK)
11205 setpcmark();
11206 curwin->w_cursor = pos;
11207 if (!(flags & SP_REPEAT))
11208 break;
11209 nest = 1; /* search for next unmatched */
11210 }
11211 }
11212
11213 if (match_pos != NULL)
11214 {
11215 /* Store the match cursor position */
11216 match_pos->lnum = curwin->w_cursor.lnum;
11217 match_pos->col = curwin->w_cursor.col + 1;
11218 }
11219
11220 /* If 'n' flag is used or search failed: restore cursor position. */
11221 if ((flags & SP_NOMOVE) || retval == 0)
11222 curwin->w_cursor = save_cursor;
11223
11224theend:
11225 vim_free(pat2);
11226 vim_free(pat3);
11227 if (p_cpo == empty_option)
11228 p_cpo = save_cpo;
11229 else
11230 /* Darn, evaluating the {skip} expression changed the value. */
11231 free_string_option(save_cpo);
11232
11233 return retval;
11234}
11235
11236/*
11237 * "searchpos()" function
11238 */
11239 static void
11240f_searchpos(typval_T *argvars, typval_T *rettv)
11241{
11242 pos_T match_pos;
11243 int lnum = 0;
11244 int col = 0;
11245 int n;
11246 int flags = 0;
11247
11248 if (rettv_list_alloc(rettv) == FAIL)
11249 return;
11250
11251 n = search_cmn(argvars, &match_pos, &flags);
11252 if (n > 0)
11253 {
11254 lnum = match_pos.lnum;
11255 col = match_pos.col;
11256 }
11257
11258 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11259 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11260 if (flags & SP_SUBPAT)
11261 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11262}
11263
11264 static void
11265f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11266{
11267#ifdef FEAT_CLIENTSERVER
11268 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011269 char_u *server = tv_get_string_chk(&argvars[0]);
11270 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011271
11272 rettv->vval.v_number = -1;
11273 if (server == NULL || reply == NULL)
11274 return;
11275 if (check_restricted() || check_secure())
11276 return;
11277# ifdef FEAT_X11
11278 if (check_connection() == FAIL)
11279 return;
11280# endif
11281
11282 if (serverSendReply(server, reply) < 0)
11283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011284 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011285 return;
11286 }
11287 rettv->vval.v_number = 0;
11288#else
11289 rettv->vval.v_number = -1;
11290#endif
11291}
11292
11293 static void
11294f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11295{
11296 char_u *r = NULL;
11297
11298#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011299# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011300 r = serverGetVimNames();
11301# else
11302 make_connection();
11303 if (X_DISPLAY != NULL)
11304 r = serverGetVimNames(X_DISPLAY);
11305# endif
11306#endif
11307 rettv->v_type = VAR_STRING;
11308 rettv->vval.v_string = r;
11309}
11310
11311/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011312 * "setbufline()" function
11313 */
11314 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011315f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011316{
11317 linenr_T lnum;
11318 buf_T *buf;
11319
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011320 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011321 if (buf == NULL)
11322 rettv->vval.v_number = 1; /* FAIL */
11323 else
11324 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011325 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011326 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011327 }
11328}
11329
11330/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011331 * "setbufvar()" function
11332 */
11333 static void
11334f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11335{
11336 buf_T *buf;
11337 char_u *varname, *bufvarname;
11338 typval_T *varp;
11339 char_u nbuf[NUMBUFLEN];
11340
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011341 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011342 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011343 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11344 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011345 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011346 varp = &argvars[2];
11347
11348 if (buf != NULL && varname != NULL && varp != NULL)
11349 {
11350 if (*varname == '&')
11351 {
11352 long numval;
11353 char_u *strval;
11354 int error = FALSE;
11355 aco_save_T aco;
11356
11357 /* set curbuf to be our buf, temporarily */
11358 aucmd_prepbuf(&aco, buf);
11359
11360 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011361 numval = (long)tv_get_number_chk(varp, &error);
11362 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011363 if (!error && strval != NULL)
11364 set_option_value(varname, numval, strval, OPT_LOCAL);
11365
11366 /* reset notion of buffer */
11367 aucmd_restbuf(&aco);
11368 }
11369 else
11370 {
11371 buf_T *save_curbuf = curbuf;
11372
Bram Moolenaar964b3742019-05-24 18:54:09 +020011373 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011374 if (bufvarname != NULL)
11375 {
11376 curbuf = buf;
11377 STRCPY(bufvarname, "b:");
11378 STRCPY(bufvarname + 2, varname);
11379 set_var(bufvarname, varp, TRUE);
11380 vim_free(bufvarname);
11381 curbuf = save_curbuf;
11382 }
11383 }
11384 }
11385}
11386
11387 static void
11388f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11389{
11390 dict_T *d;
11391 dictitem_T *di;
11392 char_u *csearch;
11393
11394 if (argvars[0].v_type != VAR_DICT)
11395 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011396 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011397 return;
11398 }
11399
11400 if ((d = argvars[0].vval.v_dict) != NULL)
11401 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011402 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011403 if (csearch != NULL)
11404 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011405 if (enc_utf8)
11406 {
11407 int pcc[MAX_MCO];
11408 int c = utfc_ptr2char(csearch, pcc);
11409
11410 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11411 }
11412 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011413 set_last_csearch(PTR2CHAR(csearch),
11414 csearch, MB_PTR2LEN(csearch));
11415 }
11416
11417 di = dict_find(d, (char_u *)"forward", -1);
11418 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011419 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011420 ? FORWARD : BACKWARD);
11421
11422 di = dict_find(d, (char_u *)"until", -1);
11423 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011424 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011425 }
11426}
11427
11428/*
11429 * "setcmdpos()" function
11430 */
11431 static void
11432f_setcmdpos(typval_T *argvars, typval_T *rettv)
11433{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011434 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011435
11436 if (pos >= 0)
11437 rettv->vval.v_number = set_cmdline_pos(pos);
11438}
11439
11440/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011441 * "setenv()" function
11442 */
11443 static void
11444f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11445{
11446 char_u namebuf[NUMBUFLEN];
11447 char_u valbuf[NUMBUFLEN];
11448 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11449
11450 if (argvars[1].v_type == VAR_SPECIAL
11451 && argvars[1].vval.v_number == VVAL_NULL)
11452 vim_unsetenv(name);
11453 else
11454 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11455}
11456
11457/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011458 * "setfperm({fname}, {mode})" function
11459 */
11460 static void
11461f_setfperm(typval_T *argvars, typval_T *rettv)
11462{
11463 char_u *fname;
11464 char_u modebuf[NUMBUFLEN];
11465 char_u *mode_str;
11466 int i;
11467 int mask;
11468 int mode = 0;
11469
11470 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011471 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011472 if (fname == NULL)
11473 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011474 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011475 if (mode_str == NULL)
11476 return;
11477 if (STRLEN(mode_str) != 9)
11478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011479 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011480 return;
11481 }
11482
11483 mask = 1;
11484 for (i = 8; i >= 0; --i)
11485 {
11486 if (mode_str[i] != '-')
11487 mode |= mask;
11488 mask = mask << 1;
11489 }
11490 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11491}
11492
11493/*
11494 * "setline()" function
11495 */
11496 static void
11497f_setline(typval_T *argvars, typval_T *rettv)
11498{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011499 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011500
Bram Moolenaarca851592018-06-06 21:04:07 +020011501 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011502}
11503
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011504/*
11505 * Used by "setqflist()" and "setloclist()" functions
11506 */
11507 static void
11508set_qf_ll_list(
11509 win_T *wp UNUSED,
11510 typval_T *list_arg UNUSED,
11511 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011512 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011513 typval_T *rettv)
11514{
11515#ifdef FEAT_QUICKFIX
11516 static char *e_invact = N_("E927: Invalid action: '%s'");
11517 char_u *act;
11518 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011519 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011520#endif
11521
11522 rettv->vval.v_number = -1;
11523
11524#ifdef FEAT_QUICKFIX
11525 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011526 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011527 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011528 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011529 else
11530 {
11531 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011532 dict_T *d = NULL;
11533 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011534
11535 if (action_arg->v_type == VAR_STRING)
11536 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011537 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011538 if (act == NULL)
11539 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011540 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11541 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011542 action = *act;
11543 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011544 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011545 }
11546 else if (action_arg->v_type == VAR_UNKNOWN)
11547 action = ' ';
11548 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011549 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011550
Bram Moolenaard823fa92016-08-12 16:29:27 +020011551 if (action_arg->v_type != VAR_UNKNOWN
11552 && what_arg->v_type != VAR_UNKNOWN)
11553 {
11554 if (what_arg->v_type == VAR_DICT)
11555 d = what_arg->vval.v_dict;
11556 else
11557 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011558 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011559 valid_dict = FALSE;
11560 }
11561 }
11562
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011563 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011564 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011565 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11566 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011567 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011568 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011569 }
11570#endif
11571}
11572
11573/*
11574 * "setloclist()" function
11575 */
11576 static void
11577f_setloclist(typval_T *argvars, typval_T *rettv)
11578{
11579 win_T *win;
11580
11581 rettv->vval.v_number = -1;
11582
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011583 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011584 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011585 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011586}
11587
11588/*
11589 * "setmatches()" function
11590 */
11591 static void
11592f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11593{
11594#ifdef FEAT_SEARCH_EXTRA
11595 list_T *l;
11596 listitem_T *li;
11597 dict_T *d;
11598 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011599 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011600
11601 rettv->vval.v_number = -1;
11602 if (argvars[0].v_type != VAR_LIST)
11603 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011604 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011605 return;
11606 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011607 if (win == NULL)
11608 return;
11609
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011610 if ((l = argvars[0].vval.v_list) != NULL)
11611 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011612 /* To some extent make sure that we are dealing with a list from
11613 * "getmatches()". */
11614 li = l->lv_first;
11615 while (li != NULL)
11616 {
11617 if (li->li_tv.v_type != VAR_DICT
11618 || (d = li->li_tv.vval.v_dict) == NULL)
11619 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011620 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011621 return;
11622 }
11623 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11624 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11625 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11626 && dict_find(d, (char_u *)"priority", -1) != NULL
11627 && dict_find(d, (char_u *)"id", -1) != NULL))
11628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011629 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011630 return;
11631 }
11632 li = li->li_next;
11633 }
11634
Bram Moolenaaraff74912019-03-30 18:11:49 +010011635 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011636 li = l->lv_first;
11637 while (li != NULL)
11638 {
11639 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011640 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011641 dictitem_T *di;
11642 char_u *group;
11643 int priority;
11644 int id;
11645 char_u *conceal;
11646
11647 d = li->li_tv.vval.v_dict;
11648 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11649 {
11650 if (s == NULL)
11651 {
11652 s = list_alloc();
11653 if (s == NULL)
11654 return;
11655 }
11656
11657 /* match from matchaddpos() */
11658 for (i = 1; i < 9; i++)
11659 {
11660 sprintf((char *)buf, (char *)"pos%d", i);
11661 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11662 {
11663 if (di->di_tv.v_type != VAR_LIST)
11664 return;
11665
11666 list_append_tv(s, &di->di_tv);
11667 s->lv_refcount++;
11668 }
11669 else
11670 break;
11671 }
11672 }
11673
Bram Moolenaar8f667172018-12-14 15:38:31 +010011674 group = dict_get_string(d, (char_u *)"group", TRUE);
11675 priority = (int)dict_get_number(d, (char_u *)"priority");
11676 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011677 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011678 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011679 : NULL;
11680 if (i == 0)
11681 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011682 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011683 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011684 priority, id, NULL, conceal);
11685 }
11686 else
11687 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011688 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011689 list_unref(s);
11690 s = NULL;
11691 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011692 vim_free(group);
11693 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011694
11695 li = li->li_next;
11696 }
11697 rettv->vval.v_number = 0;
11698 }
11699#endif
11700}
11701
11702/*
11703 * "setpos()" function
11704 */
11705 static void
11706f_setpos(typval_T *argvars, typval_T *rettv)
11707{
11708 pos_T pos;
11709 int fnum;
11710 char_u *name;
11711 colnr_T curswant = -1;
11712
11713 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011714 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011715 if (name != NULL)
11716 {
11717 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11718 {
11719 if (--pos.col < 0)
11720 pos.col = 0;
11721 if (name[0] == '.' && name[1] == NUL)
11722 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011723 /* set cursor; "fnum" is ignored */
11724 curwin->w_cursor = pos;
11725 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011726 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011727 curwin->w_curswant = curswant - 1;
11728 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011729 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011730 check_cursor();
11731 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011732 }
11733 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11734 {
11735 /* set mark */
11736 if (setmark_pos(name[1], &pos, fnum) == OK)
11737 rettv->vval.v_number = 0;
11738 }
11739 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011740 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011741 }
11742 }
11743}
11744
11745/*
11746 * "setqflist()" function
11747 */
11748 static void
11749f_setqflist(typval_T *argvars, typval_T *rettv)
11750{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011751 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011752}
11753
11754/*
11755 * "setreg()" function
11756 */
11757 static void
11758f_setreg(typval_T *argvars, typval_T *rettv)
11759{
11760 int regname;
11761 char_u *strregname;
11762 char_u *stropt;
11763 char_u *strval;
11764 int append;
11765 char_u yank_type;
11766 long block_len;
11767
11768 block_len = -1;
11769 yank_type = MAUTO;
11770 append = FALSE;
11771
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011772 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011773 rettv->vval.v_number = 1; /* FAIL is default */
11774
11775 if (strregname == NULL)
11776 return; /* type error; errmsg already given */
11777 regname = *strregname;
11778 if (regname == 0 || regname == '@')
11779 regname = '"';
11780
11781 if (argvars[2].v_type != VAR_UNKNOWN)
11782 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011783 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011784 if (stropt == NULL)
11785 return; /* type error */
11786 for (; *stropt != NUL; ++stropt)
11787 switch (*stropt)
11788 {
11789 case 'a': case 'A': /* append */
11790 append = TRUE;
11791 break;
11792 case 'v': case 'c': /* character-wise selection */
11793 yank_type = MCHAR;
11794 break;
11795 case 'V': case 'l': /* line-wise selection */
11796 yank_type = MLINE;
11797 break;
11798 case 'b': case Ctrl_V: /* block-wise selection */
11799 yank_type = MBLOCK;
11800 if (VIM_ISDIGIT(stropt[1]))
11801 {
11802 ++stropt;
11803 block_len = getdigits(&stropt) - 1;
11804 --stropt;
11805 }
11806 break;
11807 }
11808 }
11809
11810 if (argvars[1].v_type == VAR_LIST)
11811 {
11812 char_u **lstval;
11813 char_u **allocval;
11814 char_u buf[NUMBUFLEN];
11815 char_u **curval;
11816 char_u **curallocval;
11817 list_T *ll = argvars[1].vval.v_list;
11818 listitem_T *li;
11819 int len;
11820
11821 /* If the list is NULL handle like an empty list. */
11822 len = ll == NULL ? 0 : ll->lv_len;
11823
11824 /* First half: use for pointers to result lines; second half: use for
11825 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011826 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011827 if (lstval == NULL)
11828 return;
11829 curval = lstval;
11830 allocval = lstval + len + 2;
11831 curallocval = allocval;
11832
11833 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11834 li = li->li_next)
11835 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011836 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011837 if (strval == NULL)
11838 goto free_lstval;
11839 if (strval == buf)
11840 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011841 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011842 * overwrite the string. */
11843 strval = vim_strsave(buf);
11844 if (strval == NULL)
11845 goto free_lstval;
11846 *curallocval++ = strval;
11847 }
11848 *curval++ = strval;
11849 }
11850 *curval++ = NULL;
11851
11852 write_reg_contents_lst(regname, lstval, -1,
11853 append, yank_type, block_len);
11854free_lstval:
11855 while (curallocval > allocval)
11856 vim_free(*--curallocval);
11857 vim_free(lstval);
11858 }
11859 else
11860 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011861 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011862 if (strval == NULL)
11863 return;
11864 write_reg_contents_ex(regname, strval, -1,
11865 append, yank_type, block_len);
11866 }
11867 rettv->vval.v_number = 0;
11868}
11869
11870/*
11871 * "settabvar()" function
11872 */
11873 static void
11874f_settabvar(typval_T *argvars, typval_T *rettv)
11875{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011876 tabpage_T *save_curtab;
11877 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011878 char_u *varname, *tabvarname;
11879 typval_T *varp;
11880
11881 rettv->vval.v_number = 0;
11882
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011883 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011884 return;
11885
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011886 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11887 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011888 varp = &argvars[2];
11889
Bram Moolenaar4033c552017-09-16 20:54:51 +020011890 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011891 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011892 save_curtab = curtab;
11893 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011894
Bram Moolenaar964b3742019-05-24 18:54:09 +020011895 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011896 if (tabvarname != NULL)
11897 {
11898 STRCPY(tabvarname, "t:");
11899 STRCPY(tabvarname + 2, varname);
11900 set_var(tabvarname, varp, TRUE);
11901 vim_free(tabvarname);
11902 }
11903
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011904 /* Restore current tabpage */
11905 if (valid_tabpage(save_curtab))
11906 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011907 }
11908}
11909
11910/*
11911 * "settabwinvar()" function
11912 */
11913 static void
11914f_settabwinvar(typval_T *argvars, typval_T *rettv)
11915{
11916 setwinvar(argvars, rettv, 1);
11917}
11918
11919/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011920 * "settagstack()" function
11921 */
11922 static void
11923f_settagstack(typval_T *argvars, typval_T *rettv)
11924{
11925 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11926 win_T *wp;
11927 dict_T *d;
11928 int action = 'r';
11929
11930 rettv->vval.v_number = -1;
11931
11932 // first argument: window number or id
11933 wp = find_win_by_nr_or_id(&argvars[0]);
11934 if (wp == NULL)
11935 return;
11936
11937 // second argument: dict with items to set in the tag stack
11938 if (argvars[1].v_type != VAR_DICT)
11939 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011940 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011941 return;
11942 }
11943 d = argvars[1].vval.v_dict;
11944 if (d == NULL)
11945 return;
11946
11947 // third argument: action - 'a' for append and 'r' for replace.
11948 // default is to replace the stack.
11949 if (argvars[2].v_type == VAR_UNKNOWN)
11950 action = 'r';
11951 else if (argvars[2].v_type == VAR_STRING)
11952 {
11953 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011954 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011955 if (actstr == NULL)
11956 return;
11957 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11958 action = *actstr;
11959 else
11960 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011961 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011962 return;
11963 }
11964 }
11965 else
11966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011967 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011968 return;
11969 }
11970
11971 if (set_tagstack(wp, d, action) == OK)
11972 rettv->vval.v_number = 0;
11973}
11974
11975/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011976 * "setwinvar()" function
11977 */
11978 static void
11979f_setwinvar(typval_T *argvars, typval_T *rettv)
11980{
11981 setwinvar(argvars, rettv, 0);
11982}
11983
11984#ifdef FEAT_CRYPT
11985/*
11986 * "sha256({string})" function
11987 */
11988 static void
11989f_sha256(typval_T *argvars, typval_T *rettv)
11990{
11991 char_u *p;
11992
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011993 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011994 rettv->vval.v_string = vim_strsave(
11995 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11996 rettv->v_type = VAR_STRING;
11997}
11998#endif /* FEAT_CRYPT */
11999
12000/*
12001 * "shellescape({string})" function
12002 */
12003 static void
12004f_shellescape(typval_T *argvars, typval_T *rettv)
12005{
Bram Moolenaar20615522017-06-05 18:46:26 +020012006 int do_special = non_zero_arg(&argvars[1]);
12007
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012008 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012009 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012010 rettv->v_type = VAR_STRING;
12011}
12012
12013/*
12014 * shiftwidth() function
12015 */
12016 static void
12017f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
12018{
Bram Moolenaarf9514162018-11-22 03:08:29 +010012019 rettv->vval.v_number = 0;
12020
12021 if (argvars[0].v_type != VAR_UNKNOWN)
12022 {
12023 long col;
12024
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012025 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010012026 if (col < 0)
12027 return; // type error; errmsg already given
12028#ifdef FEAT_VARTABS
12029 rettv->vval.v_number = get_sw_value_col(curbuf, col);
12030 return;
12031#endif
12032 }
12033
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012034 rettv->vval.v_number = get_sw_value(curbuf);
12035}
12036
Bram Moolenaar162b7142018-12-21 15:17:36 +010012037#ifdef FEAT_SIGNS
12038/*
12039 * "sign_define()" function
12040 */
12041 static void
12042f_sign_define(typval_T *argvars, typval_T *rettv)
12043{
12044 char_u *name;
12045 dict_T *dict;
12046 char_u *icon = NULL;
12047 char_u *linehl = NULL;
12048 char_u *text = NULL;
12049 char_u *texthl = NULL;
12050
12051 rettv->vval.v_number = -1;
12052
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012053 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012054 if (name == NULL)
12055 return;
12056
12057 if (argvars[1].v_type != VAR_UNKNOWN)
12058 {
12059 if (argvars[1].v_type != VAR_DICT)
12060 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012061 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012062 return;
12063 }
12064
12065 // sign attributes
12066 dict = argvars[1].vval.v_dict;
12067 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12068 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12069 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12070 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12071 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12072 text = dict_get_string(dict, (char_u *)"text", TRUE);
12073 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12074 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12075 }
12076
12077 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12078 rettv->vval.v_number = 0;
12079
12080 vim_free(icon);
12081 vim_free(linehl);
12082 vim_free(text);
12083 vim_free(texthl);
12084}
12085
12086/*
12087 * "sign_getdefined()" function
12088 */
12089 static void
12090f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12091{
12092 char_u *name = NULL;
12093
12094 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12095 return;
12096
12097 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012098 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012099
12100 sign_getlist(name, rettv->vval.v_list);
12101}
12102
12103/*
12104 * "sign_getplaced()" function
12105 */
12106 static void
12107f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12108{
12109 buf_T *buf = NULL;
12110 dict_T *dict;
12111 dictitem_T *di;
12112 linenr_T lnum = 0;
12113 int sign_id = 0;
12114 char_u *group = NULL;
12115 int notanum = FALSE;
12116
12117 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12118 return;
12119
12120 if (argvars[0].v_type != VAR_UNKNOWN)
12121 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012122 // get signs placed in the specified buffer
12123 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012124 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012125 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012126
12127 if (argvars[1].v_type != VAR_UNKNOWN)
12128 {
12129 if (argvars[1].v_type != VAR_DICT ||
12130 ((dict = argvars[1].vval.v_dict) == NULL))
12131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012132 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012133 return;
12134 }
12135 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12136 {
12137 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012138 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012139 if (notanum)
12140 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012141 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012142 }
12143 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12144 {
12145 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012146 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012147 if (notanum)
12148 return;
12149 }
12150 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12151 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012152 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012153 if (group == NULL)
12154 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012155 if (*group == '\0') // empty string means global group
12156 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012157 }
12158 }
12159 }
12160
12161 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12162}
12163
12164/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012165 * "sign_jump()" function
12166 */
12167 static void
12168f_sign_jump(typval_T *argvars, typval_T *rettv)
12169{
12170 int sign_id;
12171 char_u *sign_group = NULL;
12172 buf_T *buf;
12173 int notanum = FALSE;
12174
12175 rettv->vval.v_number = -1;
12176
Bram Moolenaarbdace832019-03-02 10:13:42 +010012177 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012178 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12179 if (notanum)
12180 return;
12181 if (sign_id <= 0)
12182 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012183 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012184 return;
12185 }
12186
12187 // Sign group
12188 sign_group = tv_get_string_chk(&argvars[1]);
12189 if (sign_group == NULL)
12190 return;
12191 if (sign_group[0] == '\0')
12192 sign_group = NULL; // global sign group
12193 else
12194 {
12195 sign_group = vim_strsave(sign_group);
12196 if (sign_group == NULL)
12197 return;
12198 }
12199
12200 // Buffer to place the sign
12201 buf = get_buf_arg(&argvars[2]);
12202 if (buf == NULL)
12203 goto cleanup;
12204
12205 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12206
12207cleanup:
12208 vim_free(sign_group);
12209}
12210
12211/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012212 * "sign_place()" function
12213 */
12214 static void
12215f_sign_place(typval_T *argvars, typval_T *rettv)
12216{
12217 int sign_id;
12218 char_u *group = NULL;
12219 char_u *sign_name;
12220 buf_T *buf;
12221 dict_T *dict;
12222 dictitem_T *di;
12223 linenr_T lnum = 0;
12224 int prio = SIGN_DEF_PRIO;
12225 int notanum = FALSE;
12226
12227 rettv->vval.v_number = -1;
12228
Bram Moolenaarbdace832019-03-02 10:13:42 +010012229 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012230 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012231 if (notanum)
12232 return;
12233 if (sign_id < 0)
12234 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012235 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012236 return;
12237 }
12238
12239 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012240 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012241 if (group == NULL)
12242 return;
12243 if (group[0] == '\0')
12244 group = NULL; // global sign group
12245 else
12246 {
12247 group = vim_strsave(group);
12248 if (group == NULL)
12249 return;
12250 }
12251
12252 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012253 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012254 if (sign_name == NULL)
12255 goto cleanup;
12256
12257 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012258 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012259 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012260 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012261
12262 if (argvars[4].v_type != VAR_UNKNOWN)
12263 {
12264 if (argvars[4].v_type != VAR_DICT ||
12265 ((dict = argvars[4].vval.v_dict) == NULL))
12266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012267 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012268 goto cleanup;
12269 }
12270
12271 // Line number where the sign is to be placed
12272 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12273 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012274 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012275 if (notanum)
12276 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012277 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012278 }
12279 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12280 {
12281 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012282 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012283 if (notanum)
12284 goto cleanup;
12285 }
12286 }
12287
12288 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12289 rettv->vval.v_number = sign_id;
12290
12291cleanup:
12292 vim_free(group);
12293}
12294
12295/*
12296 * "sign_undefine()" function
12297 */
12298 static void
12299f_sign_undefine(typval_T *argvars, typval_T *rettv)
12300{
12301 char_u *name;
12302
12303 rettv->vval.v_number = -1;
12304
12305 if (argvars[0].v_type == VAR_UNKNOWN)
12306 {
12307 // Free all the signs
12308 free_signs();
12309 rettv->vval.v_number = 0;
12310 }
12311 else
12312 {
12313 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012314 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012315 if (name == NULL)
12316 return;
12317
12318 if (sign_undefine_by_name(name) == OK)
12319 rettv->vval.v_number = 0;
12320 }
12321}
12322
12323/*
12324 * "sign_unplace()" function
12325 */
12326 static void
12327f_sign_unplace(typval_T *argvars, typval_T *rettv)
12328{
12329 dict_T *dict;
12330 dictitem_T *di;
12331 int sign_id = 0;
12332 buf_T *buf = NULL;
12333 char_u *group = NULL;
12334
12335 rettv->vval.v_number = -1;
12336
12337 if (argvars[0].v_type != VAR_STRING)
12338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012339 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012340 return;
12341 }
12342
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012343 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012344 if (group[0] == '\0')
12345 group = NULL; // global sign group
12346 else
12347 {
12348 group = vim_strsave(group);
12349 if (group == NULL)
12350 return;
12351 }
12352
12353 if (argvars[1].v_type != VAR_UNKNOWN)
12354 {
12355 if (argvars[1].v_type != VAR_DICT)
12356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012357 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012358 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012359 }
12360 dict = argvars[1].vval.v_dict;
12361
12362 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12363 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012364 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012365 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012366 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012367 }
12368 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12369 sign_id = dict_get_number(dict, (char_u *)"id");
12370 }
12371
12372 if (buf == NULL)
12373 {
12374 // Delete the sign in all the buffers
12375 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012376 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012377 rettv->vval.v_number = 0;
12378 }
12379 else
12380 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012381 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012382 rettv->vval.v_number = 0;
12383 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012384
12385cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012386 vim_free(group);
12387}
12388#endif
12389
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012390/*
12391 * "simplify()" function
12392 */
12393 static void
12394f_simplify(typval_T *argvars, typval_T *rettv)
12395{
12396 char_u *p;
12397
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012398 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012399 rettv->vval.v_string = vim_strsave(p);
12400 simplify_filename(rettv->vval.v_string); /* simplify in place */
12401 rettv->v_type = VAR_STRING;
12402}
12403
12404#ifdef FEAT_FLOAT
12405/*
12406 * "sin()" function
12407 */
12408 static void
12409f_sin(typval_T *argvars, typval_T *rettv)
12410{
12411 float_T f = 0.0;
12412
12413 rettv->v_type = VAR_FLOAT;
12414 if (get_float_arg(argvars, &f) == OK)
12415 rettv->vval.v_float = sin(f);
12416 else
12417 rettv->vval.v_float = 0.0;
12418}
12419
12420/*
12421 * "sinh()" function
12422 */
12423 static void
12424f_sinh(typval_T *argvars, typval_T *rettv)
12425{
12426 float_T f = 0.0;
12427
12428 rettv->v_type = VAR_FLOAT;
12429 if (get_float_arg(argvars, &f) == OK)
12430 rettv->vval.v_float = sinh(f);
12431 else
12432 rettv->vval.v_float = 0.0;
12433}
12434#endif
12435
Bram Moolenaareae1b912019-05-09 15:12:55 +020012436static int item_compare(const void *s1, const void *s2);
12437static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012438
12439/* struct used in the array that's given to qsort() */
12440typedef struct
12441{
12442 listitem_T *item;
12443 int idx;
12444} sortItem_T;
12445
12446/* struct storing information about current sort */
12447typedef struct
12448{
12449 int item_compare_ic;
12450 int item_compare_numeric;
12451 int item_compare_numbers;
12452#ifdef FEAT_FLOAT
12453 int item_compare_float;
12454#endif
12455 char_u *item_compare_func;
12456 partial_T *item_compare_partial;
12457 dict_T *item_compare_selfdict;
12458 int item_compare_func_err;
12459 int item_compare_keep_zero;
12460} sortinfo_T;
12461static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012462#define ITEM_COMPARE_FAIL 999
12463
12464/*
12465 * Compare functions for f_sort() and f_uniq() below.
12466 */
12467 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012468item_compare(const void *s1, const void *s2)
12469{
12470 sortItem_T *si1, *si2;
12471 typval_T *tv1, *tv2;
12472 char_u *p1, *p2;
12473 char_u *tofree1 = NULL, *tofree2 = NULL;
12474 int res;
12475 char_u numbuf1[NUMBUFLEN];
12476 char_u numbuf2[NUMBUFLEN];
12477
12478 si1 = (sortItem_T *)s1;
12479 si2 = (sortItem_T *)s2;
12480 tv1 = &si1->item->li_tv;
12481 tv2 = &si2->item->li_tv;
12482
12483 if (sortinfo->item_compare_numbers)
12484 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012485 varnumber_T v1 = tv_get_number(tv1);
12486 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012487
12488 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12489 }
12490
12491#ifdef FEAT_FLOAT
12492 if (sortinfo->item_compare_float)
12493 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012494 float_T v1 = tv_get_float(tv1);
12495 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012496
12497 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12498 }
12499#endif
12500
12501 /* tv2string() puts quotes around a string and allocates memory. Don't do
12502 * that for string variables. Use a single quote when comparing with a
12503 * non-string to do what the docs promise. */
12504 if (tv1->v_type == VAR_STRING)
12505 {
12506 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12507 p1 = (char_u *)"'";
12508 else
12509 p1 = tv1->vval.v_string;
12510 }
12511 else
12512 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12513 if (tv2->v_type == VAR_STRING)
12514 {
12515 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12516 p2 = (char_u *)"'";
12517 else
12518 p2 = tv2->vval.v_string;
12519 }
12520 else
12521 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12522 if (p1 == NULL)
12523 p1 = (char_u *)"";
12524 if (p2 == NULL)
12525 p2 = (char_u *)"";
12526 if (!sortinfo->item_compare_numeric)
12527 {
12528 if (sortinfo->item_compare_ic)
12529 res = STRICMP(p1, p2);
12530 else
12531 res = STRCMP(p1, p2);
12532 }
12533 else
12534 {
12535 double n1, n2;
12536 n1 = strtod((char *)p1, (char **)&p1);
12537 n2 = strtod((char *)p2, (char **)&p2);
12538 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12539 }
12540
12541 /* When the result would be zero, compare the item indexes. Makes the
12542 * sort stable. */
12543 if (res == 0 && !sortinfo->item_compare_keep_zero)
12544 res = si1->idx > si2->idx ? 1 : -1;
12545
12546 vim_free(tofree1);
12547 vim_free(tofree2);
12548 return res;
12549}
12550
12551 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012552item_compare2(const void *s1, const void *s2)
12553{
12554 sortItem_T *si1, *si2;
12555 int res;
12556 typval_T rettv;
12557 typval_T argv[3];
12558 int dummy;
12559 char_u *func_name;
12560 partial_T *partial = sortinfo->item_compare_partial;
12561
12562 /* shortcut after failure in previous call; compare all items equal */
12563 if (sortinfo->item_compare_func_err)
12564 return 0;
12565
12566 si1 = (sortItem_T *)s1;
12567 si2 = (sortItem_T *)s2;
12568
12569 if (partial == NULL)
12570 func_name = sortinfo->item_compare_func;
12571 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012572 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012573
12574 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12575 * in the copy without changing the original list items. */
12576 copy_tv(&si1->item->li_tv, &argv[0]);
12577 copy_tv(&si2->item->li_tv, &argv[1]);
12578
12579 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012580 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012581 partial, sortinfo->item_compare_selfdict);
12582 clear_tv(&argv[0]);
12583 clear_tv(&argv[1]);
12584
12585 if (res == FAIL)
12586 res = ITEM_COMPARE_FAIL;
12587 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012588 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012589 if (sortinfo->item_compare_func_err)
12590 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12591 clear_tv(&rettv);
12592
12593 /* When the result would be zero, compare the pointers themselves. Makes
12594 * the sort stable. */
12595 if (res == 0 && !sortinfo->item_compare_keep_zero)
12596 res = si1->idx > si2->idx ? 1 : -1;
12597
12598 return res;
12599}
12600
12601/*
12602 * "sort({list})" function
12603 */
12604 static void
12605do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12606{
12607 list_T *l;
12608 listitem_T *li;
12609 sortItem_T *ptrs;
12610 sortinfo_T *old_sortinfo;
12611 sortinfo_T info;
12612 long len;
12613 long i;
12614
12615 /* Pointer to current info struct used in compare function. Save and
12616 * restore the current one for nested calls. */
12617 old_sortinfo = sortinfo;
12618 sortinfo = &info;
12619
12620 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012621 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012622 else
12623 {
12624 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012625 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012626 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12627 TRUE))
12628 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012629 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012630
12631 len = list_len(l);
12632 if (len <= 1)
12633 goto theend; /* short list sorts pretty quickly */
12634
12635 info.item_compare_ic = FALSE;
12636 info.item_compare_numeric = FALSE;
12637 info.item_compare_numbers = FALSE;
12638#ifdef FEAT_FLOAT
12639 info.item_compare_float = FALSE;
12640#endif
12641 info.item_compare_func = NULL;
12642 info.item_compare_partial = NULL;
12643 info.item_compare_selfdict = NULL;
12644 if (argvars[1].v_type != VAR_UNKNOWN)
12645 {
12646 /* optional second argument: {func} */
12647 if (argvars[1].v_type == VAR_FUNC)
12648 info.item_compare_func = argvars[1].vval.v_string;
12649 else if (argvars[1].v_type == VAR_PARTIAL)
12650 info.item_compare_partial = argvars[1].vval.v_partial;
12651 else
12652 {
12653 int error = FALSE;
12654
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012655 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012656 if (error)
12657 goto theend; /* type error; errmsg already given */
12658 if (i == 1)
12659 info.item_compare_ic = TRUE;
12660 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012661 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012662 else if (i != 0)
12663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012664 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012665 goto theend;
12666 }
12667 if (info.item_compare_func != NULL)
12668 {
12669 if (*info.item_compare_func == NUL)
12670 {
12671 /* empty string means default sort */
12672 info.item_compare_func = NULL;
12673 }
12674 else if (STRCMP(info.item_compare_func, "n") == 0)
12675 {
12676 info.item_compare_func = NULL;
12677 info.item_compare_numeric = TRUE;
12678 }
12679 else if (STRCMP(info.item_compare_func, "N") == 0)
12680 {
12681 info.item_compare_func = NULL;
12682 info.item_compare_numbers = TRUE;
12683 }
12684#ifdef FEAT_FLOAT
12685 else if (STRCMP(info.item_compare_func, "f") == 0)
12686 {
12687 info.item_compare_func = NULL;
12688 info.item_compare_float = TRUE;
12689 }
12690#endif
12691 else if (STRCMP(info.item_compare_func, "i") == 0)
12692 {
12693 info.item_compare_func = NULL;
12694 info.item_compare_ic = TRUE;
12695 }
12696 }
12697 }
12698
12699 if (argvars[2].v_type != VAR_UNKNOWN)
12700 {
12701 /* optional third argument: {dict} */
12702 if (argvars[2].v_type != VAR_DICT)
12703 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012704 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012705 goto theend;
12706 }
12707 info.item_compare_selfdict = argvars[2].vval.v_dict;
12708 }
12709 }
12710
12711 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020012712 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012713 if (ptrs == NULL)
12714 goto theend;
12715
12716 i = 0;
12717 if (sort)
12718 {
12719 /* sort(): ptrs will be the list to sort */
12720 for (li = l->lv_first; li != NULL; li = li->li_next)
12721 {
12722 ptrs[i].item = li;
12723 ptrs[i].idx = i;
12724 ++i;
12725 }
12726
12727 info.item_compare_func_err = FALSE;
12728 info.item_compare_keep_zero = FALSE;
12729 /* test the compare function */
12730 if ((info.item_compare_func != NULL
12731 || info.item_compare_partial != NULL)
12732 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12733 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012734 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012735 else
12736 {
12737 /* Sort the array with item pointers. */
12738 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12739 info.item_compare_func == NULL
12740 && info.item_compare_partial == NULL
12741 ? item_compare : item_compare2);
12742
12743 if (!info.item_compare_func_err)
12744 {
12745 /* Clear the List and append the items in sorted order. */
12746 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12747 l->lv_len = 0;
12748 for (i = 0; i < len; ++i)
12749 list_append(l, ptrs[i].item);
12750 }
12751 }
12752 }
12753 else
12754 {
12755 int (*item_compare_func_ptr)(const void *, const void *);
12756
12757 /* f_uniq(): ptrs will be a stack of items to remove */
12758 info.item_compare_func_err = FALSE;
12759 info.item_compare_keep_zero = TRUE;
12760 item_compare_func_ptr = info.item_compare_func != NULL
12761 || info.item_compare_partial != NULL
12762 ? item_compare2 : item_compare;
12763
12764 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12765 li = li->li_next)
12766 {
12767 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12768 == 0)
12769 ptrs[i++].item = li;
12770 if (info.item_compare_func_err)
12771 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012772 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012773 break;
12774 }
12775 }
12776
12777 if (!info.item_compare_func_err)
12778 {
12779 while (--i >= 0)
12780 {
12781 li = ptrs[i].item->li_next;
12782 ptrs[i].item->li_next = li->li_next;
12783 if (li->li_next != NULL)
12784 li->li_next->li_prev = ptrs[i].item;
12785 else
12786 l->lv_last = ptrs[i].item;
12787 list_fix_watch(l, li);
12788 listitem_free(li);
12789 l->lv_len--;
12790 }
12791 }
12792 }
12793
12794 vim_free(ptrs);
12795 }
12796theend:
12797 sortinfo = old_sortinfo;
12798}
12799
12800/*
12801 * "sort({list})" function
12802 */
12803 static void
12804f_sort(typval_T *argvars, typval_T *rettv)
12805{
12806 do_sort_uniq(argvars, rettv, TRUE);
12807}
12808
12809/*
12810 * "uniq({list})" function
12811 */
12812 static void
12813f_uniq(typval_T *argvars, typval_T *rettv)
12814{
12815 do_sort_uniq(argvars, rettv, FALSE);
12816}
12817
12818/*
12819 * "soundfold({word})" function
12820 */
12821 static void
12822f_soundfold(typval_T *argvars, typval_T *rettv)
12823{
12824 char_u *s;
12825
12826 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012827 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012828#ifdef FEAT_SPELL
12829 rettv->vval.v_string = eval_soundfold(s);
12830#else
12831 rettv->vval.v_string = vim_strsave(s);
12832#endif
12833}
12834
12835/*
12836 * "spellbadword()" function
12837 */
12838 static void
12839f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12840{
12841 char_u *word = (char_u *)"";
12842 hlf_T attr = HLF_COUNT;
12843 int len = 0;
12844
12845 if (rettv_list_alloc(rettv) == FAIL)
12846 return;
12847
12848#ifdef FEAT_SPELL
12849 if (argvars[0].v_type == VAR_UNKNOWN)
12850 {
12851 /* Find the start and length of the badly spelled word. */
12852 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12853 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012854 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012855 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012856 curwin->w_set_curswant = TRUE;
12857 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012858 }
12859 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12860 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012861 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012862 int capcol = -1;
12863
12864 if (str != NULL)
12865 {
12866 /* Check the argument for spelling. */
12867 while (*str != NUL)
12868 {
12869 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12870 if (attr != HLF_COUNT)
12871 {
12872 word = str;
12873 break;
12874 }
12875 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012876 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012877 }
12878 }
12879 }
12880#endif
12881
12882 list_append_string(rettv->vval.v_list, word, len);
12883 list_append_string(rettv->vval.v_list, (char_u *)(
12884 attr == HLF_SPB ? "bad" :
12885 attr == HLF_SPR ? "rare" :
12886 attr == HLF_SPL ? "local" :
12887 attr == HLF_SPC ? "caps" :
12888 ""), -1);
12889}
12890
12891/*
12892 * "spellsuggest()" function
12893 */
12894 static void
12895f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12896{
12897#ifdef FEAT_SPELL
12898 char_u *str;
12899 int typeerr = FALSE;
12900 int maxcount;
12901 garray_T ga;
12902 int i;
12903 listitem_T *li;
12904 int need_capital = FALSE;
12905#endif
12906
12907 if (rettv_list_alloc(rettv) == FAIL)
12908 return;
12909
12910#ifdef FEAT_SPELL
12911 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12912 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012913 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012914 if (argvars[1].v_type != VAR_UNKNOWN)
12915 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012916 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012917 if (maxcount <= 0)
12918 return;
12919 if (argvars[2].v_type != VAR_UNKNOWN)
12920 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012921 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012922 if (typeerr)
12923 return;
12924 }
12925 }
12926 else
12927 maxcount = 25;
12928
12929 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12930
12931 for (i = 0; i < ga.ga_len; ++i)
12932 {
12933 str = ((char_u **)ga.ga_data)[i];
12934
12935 li = listitem_alloc();
12936 if (li == NULL)
12937 vim_free(str);
12938 else
12939 {
12940 li->li_tv.v_type = VAR_STRING;
12941 li->li_tv.v_lock = 0;
12942 li->li_tv.vval.v_string = str;
12943 list_append(rettv->vval.v_list, li);
12944 }
12945 }
12946 ga_clear(&ga);
12947 }
12948#endif
12949}
12950
12951 static void
12952f_split(typval_T *argvars, typval_T *rettv)
12953{
12954 char_u *str;
12955 char_u *end;
12956 char_u *pat = NULL;
12957 regmatch_T regmatch;
12958 char_u patbuf[NUMBUFLEN];
12959 char_u *save_cpo;
12960 int match;
12961 colnr_T col = 0;
12962 int keepempty = FALSE;
12963 int typeerr = FALSE;
12964
12965 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12966 save_cpo = p_cpo;
12967 p_cpo = (char_u *)"";
12968
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012969 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012970 if (argvars[1].v_type != VAR_UNKNOWN)
12971 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012972 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012973 if (pat == NULL)
12974 typeerr = TRUE;
12975 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012976 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012977 }
12978 if (pat == NULL || *pat == NUL)
12979 pat = (char_u *)"[\\x01- ]\\+";
12980
12981 if (rettv_list_alloc(rettv) == FAIL)
12982 return;
12983 if (typeerr)
12984 return;
12985
12986 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12987 if (regmatch.regprog != NULL)
12988 {
12989 regmatch.rm_ic = FALSE;
12990 while (*str != NUL || keepempty)
12991 {
12992 if (*str == NUL)
12993 match = FALSE; /* empty item at the end */
12994 else
12995 match = vim_regexec_nl(&regmatch, str, col);
12996 if (match)
12997 end = regmatch.startp[0];
12998 else
12999 end = str + STRLEN(str);
13000 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
13001 && *str != NUL && match && end < regmatch.endp[0]))
13002 {
13003 if (list_append_string(rettv->vval.v_list, str,
13004 (int)(end - str)) == FAIL)
13005 break;
13006 }
13007 if (!match)
13008 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010013009 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013010 if (regmatch.endp[0] > str)
13011 col = 0;
13012 else
Bram Moolenaar13505972019-01-24 15:04:48 +010013013 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013014 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013015 str = regmatch.endp[0];
13016 }
13017
13018 vim_regfree(regmatch.regprog);
13019 }
13020
13021 p_cpo = save_cpo;
13022}
13023
13024#ifdef FEAT_FLOAT
13025/*
13026 * "sqrt()" function
13027 */
13028 static void
13029f_sqrt(typval_T *argvars, typval_T *rettv)
13030{
13031 float_T f = 0.0;
13032
13033 rettv->v_type = VAR_FLOAT;
13034 if (get_float_arg(argvars, &f) == OK)
13035 rettv->vval.v_float = sqrt(f);
13036 else
13037 rettv->vval.v_float = 0.0;
13038}
13039
13040/*
13041 * "str2float()" function
13042 */
13043 static void
13044f_str2float(typval_T *argvars, typval_T *rettv)
13045{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013046 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013047 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013048
Bram Moolenaar08243d22017-01-10 16:12:29 +010013049 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013050 p = skipwhite(p + 1);
13051 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013052 if (isneg)
13053 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013054 rettv->v_type = VAR_FLOAT;
13055}
13056#endif
13057
13058/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013059 * "str2list()" function
13060 */
13061 static void
13062f_str2list(typval_T *argvars, typval_T *rettv)
13063{
13064 char_u *p;
13065 int utf8 = FALSE;
13066
13067 if (rettv_list_alloc(rettv) == FAIL)
13068 return;
13069
13070 if (argvars[1].v_type != VAR_UNKNOWN)
13071 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13072
13073 p = tv_get_string(&argvars[0]);
13074
13075 if (has_mbyte || utf8)
13076 {
13077 int (*ptr2len)(char_u *);
13078 int (*ptr2char)(char_u *);
13079
13080 if (utf8 || enc_utf8)
13081 {
13082 ptr2len = utf_ptr2len;
13083 ptr2char = utf_ptr2char;
13084 }
13085 else
13086 {
13087 ptr2len = mb_ptr2len;
13088 ptr2char = mb_ptr2char;
13089 }
13090
13091 for ( ; *p != NUL; p += (*ptr2len)(p))
13092 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13093 }
13094 else
13095 for ( ; *p != NUL; ++p)
13096 list_append_number(rettv->vval.v_list, *p);
13097}
13098
13099/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013100 * "str2nr()" function
13101 */
13102 static void
13103f_str2nr(typval_T *argvars, typval_T *rettv)
13104{
13105 int base = 10;
13106 char_u *p;
13107 varnumber_T n;
13108 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013109 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013110
13111 if (argvars[1].v_type != VAR_UNKNOWN)
13112 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013113 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013114 if (base != 2 && base != 8 && base != 10 && base != 16)
13115 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013116 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013117 return;
13118 }
13119 }
13120
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013121 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013122 isneg = (*p == '-');
13123 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013124 p = skipwhite(p + 1);
13125 switch (base)
13126 {
13127 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13128 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13129 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13130 default: what = 0;
13131 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013132 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13133 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013134 if (isneg)
13135 rettv->vval.v_number = -n;
13136 else
13137 rettv->vval.v_number = n;
13138
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013139}
13140
13141#ifdef HAVE_STRFTIME
13142/*
13143 * "strftime({format}[, {time}])" function
13144 */
13145 static void
13146f_strftime(typval_T *argvars, typval_T *rettv)
13147{
13148 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013149# ifdef HAVE_LOCALTIME_R
13150 struct tm tmval;
13151# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013152 struct tm *curtime;
13153 time_t seconds;
13154 char_u *p;
13155
13156 rettv->v_type = VAR_STRING;
13157
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013158 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013159 if (argvars[1].v_type == VAR_UNKNOWN)
13160 seconds = time(NULL);
13161 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013162 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013163# ifdef HAVE_LOCALTIME_R
13164 curtime = localtime_r(&seconds, &tmval);
13165# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013166 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013167# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013168 /* MSVC returns NULL for an invalid value of seconds. */
13169 if (curtime == NULL)
13170 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13171 else
13172 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013173 vimconv_T conv;
13174 char_u *enc;
13175
13176 conv.vc_type = CONV_NONE;
13177 enc = enc_locale();
13178 convert_setup(&conv, p_enc, enc);
13179 if (conv.vc_type != CONV_NONE)
13180 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013181 if (p != NULL)
13182 (void)strftime((char *)result_buf, sizeof(result_buf),
13183 (char *)p, curtime);
13184 else
13185 result_buf[0] = NUL;
13186
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013187 if (conv.vc_type != CONV_NONE)
13188 vim_free(p);
13189 convert_setup(&conv, enc, p_enc);
13190 if (conv.vc_type != CONV_NONE)
13191 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13192 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013193 rettv->vval.v_string = vim_strsave(result_buf);
13194
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013195 /* Release conversion descriptors */
13196 convert_setup(&conv, NULL, NULL);
13197 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013198 }
13199}
13200#endif
13201
13202/*
13203 * "strgetchar()" function
13204 */
13205 static void
13206f_strgetchar(typval_T *argvars, typval_T *rettv)
13207{
13208 char_u *str;
13209 int len;
13210 int error = FALSE;
13211 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013212 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013213
13214 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013215 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013216 if (str == NULL)
13217 return;
13218 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013219 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013220 if (error)
13221 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013222
Bram Moolenaar13505972019-01-24 15:04:48 +010013223 while (charidx >= 0 && byteidx < len)
13224 {
13225 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013226 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013227 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13228 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013229 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013230 --charidx;
13231 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013232 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013233}
13234
13235/*
13236 * "stridx()" function
13237 */
13238 static void
13239f_stridx(typval_T *argvars, typval_T *rettv)
13240{
13241 char_u buf[NUMBUFLEN];
13242 char_u *needle;
13243 char_u *haystack;
13244 char_u *save_haystack;
13245 char_u *pos;
13246 int start_idx;
13247
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013248 needle = tv_get_string_chk(&argvars[1]);
13249 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013250 rettv->vval.v_number = -1;
13251 if (needle == NULL || haystack == NULL)
13252 return; /* type error; errmsg already given */
13253
13254 if (argvars[2].v_type != VAR_UNKNOWN)
13255 {
13256 int error = FALSE;
13257
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013258 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013259 if (error || start_idx >= (int)STRLEN(haystack))
13260 return;
13261 if (start_idx >= 0)
13262 haystack += start_idx;
13263 }
13264
13265 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13266 if (pos != NULL)
13267 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13268}
13269
13270/*
13271 * "string()" function
13272 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013273 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013274f_string(typval_T *argvars, typval_T *rettv)
13275{
13276 char_u *tofree;
13277 char_u numbuf[NUMBUFLEN];
13278
13279 rettv->v_type = VAR_STRING;
13280 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13281 get_copyID());
13282 /* Make a copy if we have a value but it's not in allocated memory. */
13283 if (rettv->vval.v_string != NULL && tofree == NULL)
13284 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13285}
13286
13287/*
13288 * "strlen()" function
13289 */
13290 static void
13291f_strlen(typval_T *argvars, typval_T *rettv)
13292{
13293 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013294 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013295}
13296
13297/*
13298 * "strchars()" function
13299 */
13300 static void
13301f_strchars(typval_T *argvars, typval_T *rettv)
13302{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013303 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013304 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013305 varnumber_T len = 0;
13306 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013307
13308 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013309 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013310 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013311 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013312 else
13313 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013314 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13315 while (*s != NUL)
13316 {
13317 func_mb_ptr2char_adv(&s);
13318 ++len;
13319 }
13320 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013321 }
13322}
13323
13324/*
13325 * "strdisplaywidth()" function
13326 */
13327 static void
13328f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13329{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013330 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013331 int col = 0;
13332
13333 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013334 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013335
13336 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13337}
13338
13339/*
13340 * "strwidth()" function
13341 */
13342 static void
13343f_strwidth(typval_T *argvars, typval_T *rettv)
13344{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013345 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013346
Bram Moolenaar13505972019-01-24 15:04:48 +010013347 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013348}
13349
13350/*
13351 * "strcharpart()" function
13352 */
13353 static void
13354f_strcharpart(typval_T *argvars, typval_T *rettv)
13355{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013356 char_u *p;
13357 int nchar;
13358 int nbyte = 0;
13359 int charlen;
13360 int len = 0;
13361 int slen;
13362 int error = FALSE;
13363
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013364 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013365 slen = (int)STRLEN(p);
13366
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013367 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013368 if (!error)
13369 {
13370 if (nchar > 0)
13371 while (nchar > 0 && nbyte < slen)
13372 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013373 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013374 --nchar;
13375 }
13376 else
13377 nbyte = nchar;
13378 if (argvars[2].v_type != VAR_UNKNOWN)
13379 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013380 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013381 while (charlen > 0 && nbyte + len < slen)
13382 {
13383 int off = nbyte + len;
13384
13385 if (off < 0)
13386 len += 1;
13387 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013388 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013389 --charlen;
13390 }
13391 }
13392 else
13393 len = slen - nbyte; /* default: all bytes that are available. */
13394 }
13395
13396 /*
13397 * Only return the overlap between the specified part and the actual
13398 * string.
13399 */
13400 if (nbyte < 0)
13401 {
13402 len += nbyte;
13403 nbyte = 0;
13404 }
13405 else if (nbyte > slen)
13406 nbyte = slen;
13407 if (len < 0)
13408 len = 0;
13409 else if (nbyte + len > slen)
13410 len = slen - nbyte;
13411
13412 rettv->v_type = VAR_STRING;
13413 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013414}
13415
13416/*
13417 * "strpart()" function
13418 */
13419 static void
13420f_strpart(typval_T *argvars, typval_T *rettv)
13421{
13422 char_u *p;
13423 int n;
13424 int len;
13425 int slen;
13426 int error = FALSE;
13427
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013428 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013429 slen = (int)STRLEN(p);
13430
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013431 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013432 if (error)
13433 len = 0;
13434 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013435 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013436 else
13437 len = slen - n; /* default len: all bytes that are available. */
13438
13439 /*
13440 * Only return the overlap between the specified part and the actual
13441 * string.
13442 */
13443 if (n < 0)
13444 {
13445 len += n;
13446 n = 0;
13447 }
13448 else if (n > slen)
13449 n = slen;
13450 if (len < 0)
13451 len = 0;
13452 else if (n + len > slen)
13453 len = slen - n;
13454
13455 rettv->v_type = VAR_STRING;
13456 rettv->vval.v_string = vim_strnsave(p + n, len);
13457}
13458
13459/*
13460 * "strridx()" function
13461 */
13462 static void
13463f_strridx(typval_T *argvars, typval_T *rettv)
13464{
13465 char_u buf[NUMBUFLEN];
13466 char_u *needle;
13467 char_u *haystack;
13468 char_u *rest;
13469 char_u *lastmatch = NULL;
13470 int haystack_len, end_idx;
13471
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013472 needle = tv_get_string_chk(&argvars[1]);
13473 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013474
13475 rettv->vval.v_number = -1;
13476 if (needle == NULL || haystack == NULL)
13477 return; /* type error; errmsg already given */
13478
13479 haystack_len = (int)STRLEN(haystack);
13480 if (argvars[2].v_type != VAR_UNKNOWN)
13481 {
13482 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013483 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013484 if (end_idx < 0)
13485 return; /* can never find a match */
13486 }
13487 else
13488 end_idx = haystack_len;
13489
13490 if (*needle == NUL)
13491 {
13492 /* Empty string matches past the end. */
13493 lastmatch = haystack + end_idx;
13494 }
13495 else
13496 {
13497 for (rest = haystack; *rest != '\0'; ++rest)
13498 {
13499 rest = (char_u *)strstr((char *)rest, (char *)needle);
13500 if (rest == NULL || rest > haystack + end_idx)
13501 break;
13502 lastmatch = rest;
13503 }
13504 }
13505
13506 if (lastmatch == NULL)
13507 rettv->vval.v_number = -1;
13508 else
13509 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13510}
13511
13512/*
13513 * "strtrans()" function
13514 */
13515 static void
13516f_strtrans(typval_T *argvars, typval_T *rettv)
13517{
13518 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013519 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013520}
13521
13522/*
13523 * "submatch()" function
13524 */
13525 static void
13526f_submatch(typval_T *argvars, typval_T *rettv)
13527{
13528 int error = FALSE;
13529 int no;
13530 int retList = 0;
13531
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013532 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013533 if (error)
13534 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013535 if (no < 0 || no >= NSUBEXP)
13536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013537 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013538 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013539 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013540 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013541 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013542 if (error)
13543 return;
13544
13545 if (retList == 0)
13546 {
13547 rettv->v_type = VAR_STRING;
13548 rettv->vval.v_string = reg_submatch(no);
13549 }
13550 else
13551 {
13552 rettv->v_type = VAR_LIST;
13553 rettv->vval.v_list = reg_submatch_list(no);
13554 }
13555}
13556
13557/*
13558 * "substitute()" function
13559 */
13560 static void
13561f_substitute(typval_T *argvars, typval_T *rettv)
13562{
13563 char_u patbuf[NUMBUFLEN];
13564 char_u subbuf[NUMBUFLEN];
13565 char_u flagsbuf[NUMBUFLEN];
13566
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013567 char_u *str = tv_get_string_chk(&argvars[0]);
13568 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013569 char_u *sub = NULL;
13570 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013571 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013572
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013573 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13574 expr = &argvars[2];
13575 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013576 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013577
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013578 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013579 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13580 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013581 rettv->vval.v_string = NULL;
13582 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013583 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013584}
13585
13586/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013587 * "swapinfo(swap_filename)" function
13588 */
13589 static void
13590f_swapinfo(typval_T *argvars, typval_T *rettv)
13591{
13592 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013593 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013594}
13595
13596/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013597 * "swapname(expr)" function
13598 */
13599 static void
13600f_swapname(typval_T *argvars, typval_T *rettv)
13601{
13602 buf_T *buf;
13603
13604 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013605 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013606 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13607 || buf->b_ml.ml_mfp->mf_fname == NULL)
13608 rettv->vval.v_string = NULL;
13609 else
13610 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13611}
13612
13613/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013614 * "synID(lnum, col, trans)" function
13615 */
13616 static void
13617f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13618{
13619 int id = 0;
13620#ifdef FEAT_SYN_HL
13621 linenr_T lnum;
13622 colnr_T col;
13623 int trans;
13624 int transerr = FALSE;
13625
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013626 lnum = tv_get_lnum(argvars); /* -1 on type error */
13627 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13628 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013629
13630 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13631 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13632 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13633#endif
13634
13635 rettv->vval.v_number = id;
13636}
13637
13638/*
13639 * "synIDattr(id, what [, mode])" function
13640 */
13641 static void
13642f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13643{
13644 char_u *p = NULL;
13645#ifdef FEAT_SYN_HL
13646 int id;
13647 char_u *what;
13648 char_u *mode;
13649 char_u modebuf[NUMBUFLEN];
13650 int modec;
13651
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013652 id = (int)tv_get_number(&argvars[0]);
13653 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013654 if (argvars[2].v_type != VAR_UNKNOWN)
13655 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013656 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013657 modec = TOLOWER_ASC(mode[0]);
13658 if (modec != 't' && modec != 'c' && modec != 'g')
13659 modec = 0; /* replace invalid with current */
13660 }
13661 else
13662 {
13663#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13664 if (USE_24BIT)
13665 modec = 'g';
13666 else
13667#endif
13668 if (t_colors > 1)
13669 modec = 'c';
13670 else
13671 modec = 't';
13672 }
13673
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013674 switch (TOLOWER_ASC(what[0]))
13675 {
13676 case 'b':
13677 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13678 p = highlight_color(id, what, modec);
13679 else /* bold */
13680 p = highlight_has_attr(id, HL_BOLD, modec);
13681 break;
13682
13683 case 'f': /* fg[#] or font */
13684 p = highlight_color(id, what, modec);
13685 break;
13686
13687 case 'i':
13688 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13689 p = highlight_has_attr(id, HL_INVERSE, modec);
13690 else /* italic */
13691 p = highlight_has_attr(id, HL_ITALIC, modec);
13692 break;
13693
13694 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013695 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013696 break;
13697
13698 case 'r': /* reverse */
13699 p = highlight_has_attr(id, HL_INVERSE, modec);
13700 break;
13701
13702 case 's':
13703 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13704 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013705 /* strikeout */
13706 else if (TOLOWER_ASC(what[1]) == 't' &&
13707 TOLOWER_ASC(what[2]) == 'r')
13708 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013709 else /* standout */
13710 p = highlight_has_attr(id, HL_STANDOUT, modec);
13711 break;
13712
13713 case 'u':
13714 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13715 /* underline */
13716 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13717 else
13718 /* undercurl */
13719 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13720 break;
13721 }
13722
13723 if (p != NULL)
13724 p = vim_strsave(p);
13725#endif
13726 rettv->v_type = VAR_STRING;
13727 rettv->vval.v_string = p;
13728}
13729
13730/*
13731 * "synIDtrans(id)" function
13732 */
13733 static void
13734f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13735{
13736 int id;
13737
13738#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013739 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013740
13741 if (id > 0)
13742 id = syn_get_final_id(id);
13743 else
13744#endif
13745 id = 0;
13746
13747 rettv->vval.v_number = id;
13748}
13749
13750/*
13751 * "synconcealed(lnum, col)" function
13752 */
13753 static void
13754f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13755{
13756#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13757 linenr_T lnum;
13758 colnr_T col;
13759 int syntax_flags = 0;
13760 int cchar;
13761 int matchid = 0;
13762 char_u str[NUMBUFLEN];
13763#endif
13764
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013765 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013766
13767#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013768 lnum = tv_get_lnum(argvars); /* -1 on type error */
13769 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013770
13771 vim_memset(str, NUL, sizeof(str));
13772
13773 if (rettv_list_alloc(rettv) != FAIL)
13774 {
13775 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13776 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13777 && curwin->w_p_cole > 0)
13778 {
13779 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13780 syntax_flags = get_syntax_info(&matchid);
13781
13782 /* get the conceal character */
13783 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13784 {
13785 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013786 if (cchar == NUL && curwin->w_p_cole == 1)
13787 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013788 if (cchar != NUL)
13789 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013790 if (has_mbyte)
13791 (*mb_char2bytes)(cchar, str);
13792 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013793 str[0] = cchar;
13794 }
13795 }
13796 }
13797
13798 list_append_number(rettv->vval.v_list,
13799 (syntax_flags & HL_CONCEAL) != 0);
13800 /* -1 to auto-determine strlen */
13801 list_append_string(rettv->vval.v_list, str, -1);
13802 list_append_number(rettv->vval.v_list, matchid);
13803 }
13804#endif
13805}
13806
13807/*
13808 * "synstack(lnum, col)" function
13809 */
13810 static void
13811f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13812{
13813#ifdef FEAT_SYN_HL
13814 linenr_T lnum;
13815 colnr_T col;
13816 int i;
13817 int id;
13818#endif
13819
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013820 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013821
13822#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013823 lnum = tv_get_lnum(argvars); /* -1 on type error */
13824 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013825
13826 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13827 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13828 && rettv_list_alloc(rettv) != FAIL)
13829 {
13830 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13831 for (i = 0; ; ++i)
13832 {
13833 id = syn_get_stack_item(i);
13834 if (id < 0)
13835 break;
13836 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13837 break;
13838 }
13839 }
13840#endif
13841}
13842
13843 static void
13844get_cmd_output_as_rettv(
13845 typval_T *argvars,
13846 typval_T *rettv,
13847 int retlist)
13848{
13849 char_u *res = NULL;
13850 char_u *p;
13851 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013852 int err = FALSE;
13853 FILE *fd;
13854 list_T *list = NULL;
13855 int flags = SHELL_SILENT;
13856
13857 rettv->v_type = VAR_STRING;
13858 rettv->vval.v_string = NULL;
13859 if (check_restricted() || check_secure())
13860 goto errret;
13861
13862 if (argvars[1].v_type != VAR_UNKNOWN)
13863 {
13864 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013865 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013866 * command.
13867 */
13868 if ((infile = vim_tempname('i', TRUE)) == NULL)
13869 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013870 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013871 goto errret;
13872 }
13873
13874 fd = mch_fopen((char *)infile, WRITEBIN);
13875 if (fd == NULL)
13876 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013877 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013878 goto errret;
13879 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013880 if (argvars[1].v_type == VAR_NUMBER)
13881 {
13882 linenr_T lnum;
13883 buf_T *buf;
13884
13885 buf = buflist_findnr(argvars[1].vval.v_number);
13886 if (buf == NULL)
13887 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013888 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013889 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013890 goto errret;
13891 }
13892
13893 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13894 {
13895 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13896 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13897 {
13898 err = TRUE;
13899 break;
13900 }
13901 if (putc(NL, fd) == EOF)
13902 {
13903 err = TRUE;
13904 break;
13905 }
13906 }
13907 }
13908 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013909 {
13910 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13911 err = TRUE;
13912 }
13913 else
13914 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013915 size_t len;
13916 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013917
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013918 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013919 if (p == NULL)
13920 {
13921 fclose(fd);
13922 goto errret; /* type error; errmsg already given */
13923 }
13924 len = STRLEN(p);
13925 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13926 err = TRUE;
13927 }
13928 if (fclose(fd) != 0)
13929 err = TRUE;
13930 if (err)
13931 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013932 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013933 goto errret;
13934 }
13935 }
13936
13937 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13938 * echoes typeahead, that messes up the display. */
13939 if (!msg_silent)
13940 flags += SHELL_COOKED;
13941
13942 if (retlist)
13943 {
13944 int len;
13945 listitem_T *li;
13946 char_u *s = NULL;
13947 char_u *start;
13948 char_u *end;
13949 int i;
13950
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013951 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013952 if (res == NULL)
13953 goto errret;
13954
13955 list = list_alloc();
13956 if (list == NULL)
13957 goto errret;
13958
13959 for (i = 0; i < len; ++i)
13960 {
13961 start = res + i;
13962 while (i < len && res[i] != NL)
13963 ++i;
13964 end = res + i;
13965
Bram Moolenaar964b3742019-05-24 18:54:09 +020013966 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013967 if (s == NULL)
13968 goto errret;
13969
13970 for (p = s; start < end; ++p, ++start)
13971 *p = *start == NUL ? NL : *start;
13972 *p = NUL;
13973
13974 li = listitem_alloc();
13975 if (li == NULL)
13976 {
13977 vim_free(s);
13978 goto errret;
13979 }
13980 li->li_tv.v_type = VAR_STRING;
13981 li->li_tv.v_lock = 0;
13982 li->li_tv.vval.v_string = s;
13983 list_append(list, li);
13984 }
13985
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013986 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013987 list = NULL;
13988 }
13989 else
13990 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013991 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013992#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013993 /* translate <CR><NL> into <NL> */
13994 if (res != NULL)
13995 {
13996 char_u *s, *d;
13997
13998 d = res;
13999 for (s = res; *s; ++s)
14000 {
14001 if (s[0] == CAR && s[1] == NL)
14002 ++s;
14003 *d++ = *s;
14004 }
14005 *d = NUL;
14006 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014007#endif
14008 rettv->vval.v_string = res;
14009 res = NULL;
14010 }
14011
14012errret:
14013 if (infile != NULL)
14014 {
14015 mch_remove(infile);
14016 vim_free(infile);
14017 }
14018 if (res != NULL)
14019 vim_free(res);
14020 if (list != NULL)
14021 list_free(list);
14022}
14023
14024/*
14025 * "system()" function
14026 */
14027 static void
14028f_system(typval_T *argvars, typval_T *rettv)
14029{
14030 get_cmd_output_as_rettv(argvars, rettv, FALSE);
14031}
14032
14033/*
14034 * "systemlist()" function
14035 */
14036 static void
14037f_systemlist(typval_T *argvars, typval_T *rettv)
14038{
14039 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14040}
14041
14042/*
14043 * "tabpagebuflist()" function
14044 */
14045 static void
14046f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14047{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014048 tabpage_T *tp;
14049 win_T *wp = NULL;
14050
14051 if (argvars[0].v_type == VAR_UNKNOWN)
14052 wp = firstwin;
14053 else
14054 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014055 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014056 if (tp != NULL)
14057 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14058 }
14059 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14060 {
14061 for (; wp != NULL; wp = wp->w_next)
14062 if (list_append_number(rettv->vval.v_list,
14063 wp->w_buffer->b_fnum) == FAIL)
14064 break;
14065 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014066}
14067
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014068/*
14069 * "tabpagenr()" function
14070 */
14071 static void
14072f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14073{
14074 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014075 char_u *arg;
14076
14077 if (argvars[0].v_type != VAR_UNKNOWN)
14078 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014079 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014080 nr = 0;
14081 if (arg != NULL)
14082 {
14083 if (STRCMP(arg, "$") == 0)
14084 nr = tabpage_index(NULL) - 1;
14085 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014086 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014087 }
14088 }
14089 else
14090 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014091 rettv->vval.v_number = nr;
14092}
14093
14094
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014095/*
14096 * Common code for tabpagewinnr() and winnr().
14097 */
14098 static int
14099get_winnr(tabpage_T *tp, typval_T *argvar)
14100{
14101 win_T *twin;
14102 int nr = 1;
14103 win_T *wp;
14104 char_u *arg;
14105
14106 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14107 if (argvar->v_type != VAR_UNKNOWN)
14108 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014109 int invalid_arg = FALSE;
14110
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014111 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014112 if (arg == NULL)
14113 nr = 0; /* type error; errmsg already given */
14114 else if (STRCMP(arg, "$") == 0)
14115 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14116 else if (STRCMP(arg, "#") == 0)
14117 {
14118 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14119 if (twin == NULL)
14120 nr = 0;
14121 }
14122 else
14123 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014124 long count;
14125 char_u *endp;
14126
14127 // Extract the window count (if specified). e.g. winnr('3j')
14128 count = strtol((char *)arg, (char **)&endp, 10);
14129 if (count <= 0)
14130 count = 1; // if count is not specified, default to 1
14131 if (endp != NULL && *endp != '\0')
14132 {
14133 if (STRCMP(endp, "j") == 0)
14134 twin = win_vert_neighbor(tp, twin, FALSE, count);
14135 else if (STRCMP(endp, "k") == 0)
14136 twin = win_vert_neighbor(tp, twin, TRUE, count);
14137 else if (STRCMP(endp, "h") == 0)
14138 twin = win_horz_neighbor(tp, twin, TRUE, count);
14139 else if (STRCMP(endp, "l") == 0)
14140 twin = win_horz_neighbor(tp, twin, FALSE, count);
14141 else
14142 invalid_arg = TRUE;
14143 }
14144 else
14145 invalid_arg = TRUE;
14146 }
14147
14148 if (invalid_arg)
14149 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014150 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014151 nr = 0;
14152 }
14153 }
14154
14155 if (nr > 0)
14156 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14157 wp != twin; wp = wp->w_next)
14158 {
14159 if (wp == NULL)
14160 {
14161 /* didn't find it in this tabpage */
14162 nr = 0;
14163 break;
14164 }
14165 ++nr;
14166 }
14167 return nr;
14168}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014169
14170/*
14171 * "tabpagewinnr()" function
14172 */
14173 static void
14174f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14175{
14176 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014177 tabpage_T *tp;
14178
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014179 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014180 if (tp == NULL)
14181 nr = 0;
14182 else
14183 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014184 rettv->vval.v_number = nr;
14185}
14186
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014187/*
14188 * "tagfiles()" function
14189 */
14190 static void
14191f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14192{
14193 char_u *fname;
14194 tagname_T tn;
14195 int first;
14196
14197 if (rettv_list_alloc(rettv) == FAIL)
14198 return;
14199 fname = alloc(MAXPATHL);
14200 if (fname == NULL)
14201 return;
14202
14203 for (first = TRUE; ; first = FALSE)
14204 if (get_tagfname(&tn, first, fname) == FAIL
14205 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14206 break;
14207 tagname_free(&tn);
14208 vim_free(fname);
14209}
14210
14211/*
14212 * "taglist()" function
14213 */
14214 static void
14215f_taglist(typval_T *argvars, typval_T *rettv)
14216{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014217 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014218 char_u *tag_pattern;
14219
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014220 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014221
14222 rettv->vval.v_number = FALSE;
14223 if (*tag_pattern == NUL)
14224 return;
14225
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014226 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014227 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014228 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014229 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014230}
14231
14232/*
14233 * "tempname()" function
14234 */
14235 static void
14236f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14237{
14238 static int x = 'A';
14239
14240 rettv->v_type = VAR_STRING;
14241 rettv->vval.v_string = vim_tempname(x, FALSE);
14242
14243 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14244 * names. Skip 'I' and 'O', they are used for shell redirection. */
14245 do
14246 {
14247 if (x == 'Z')
14248 x = '0';
14249 else if (x == '9')
14250 x = 'A';
14251 else
14252 {
14253#ifdef EBCDIC
14254 if (x == 'I')
14255 x = 'J';
14256 else if (x == 'R')
14257 x = 'S';
14258 else
14259#endif
14260 ++x;
14261 }
14262 } while (x == 'I' || x == 'O');
14263}
14264
14265#ifdef FEAT_FLOAT
14266/*
14267 * "tan()" function
14268 */
14269 static void
14270f_tan(typval_T *argvars, typval_T *rettv)
14271{
14272 float_T f = 0.0;
14273
14274 rettv->v_type = VAR_FLOAT;
14275 if (get_float_arg(argvars, &f) == OK)
14276 rettv->vval.v_float = tan(f);
14277 else
14278 rettv->vval.v_float = 0.0;
14279}
14280
14281/*
14282 * "tanh()" function
14283 */
14284 static void
14285f_tanh(typval_T *argvars, typval_T *rettv)
14286{
14287 float_T f = 0.0;
14288
14289 rettv->v_type = VAR_FLOAT;
14290 if (get_float_arg(argvars, &f) == OK)
14291 rettv->vval.v_float = tanh(f);
14292 else
14293 rettv->vval.v_float = 0.0;
14294}
14295#endif
14296
14297/*
14298 * "test_alloc_fail(id, countdown, repeat)" function
14299 */
14300 static void
14301f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14302{
14303 if (argvars[0].v_type != VAR_NUMBER
14304 || argvars[0].vval.v_number <= 0
14305 || argvars[1].v_type != VAR_NUMBER
14306 || argvars[1].vval.v_number < 0
14307 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014308 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014309 else
14310 {
14311 alloc_fail_id = argvars[0].vval.v_number;
14312 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014313 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014314 alloc_fail_countdown = argvars[1].vval.v_number;
14315 alloc_fail_repeat = argvars[2].vval.v_number;
14316 did_outofmem_msg = FALSE;
14317 }
14318}
14319
14320/*
14321 * "test_autochdir()"
14322 */
14323 static void
14324f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14325{
14326#if defined(FEAT_AUTOCHDIR)
14327 test_autochdir = TRUE;
14328#endif
14329}
14330
14331/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014332 * "test_feedinput()"
14333 */
14334 static void
14335f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14336{
14337#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014338 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014339
14340 if (val != NULL)
14341 {
14342 trash_input_buf();
14343 add_to_input_buf_csi(val, (int)STRLEN(val));
14344 }
14345#endif
14346}
14347
14348/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014349 * "test_getvalue({name})" function
14350 */
14351 static void
14352f_test_getvalue(typval_T *argvars, typval_T *rettv)
14353{
14354 if (argvars[0].v_type != VAR_STRING)
14355 emsg(_(e_invarg));
14356 else
14357 {
14358 char_u *name = tv_get_string(&argvars[0]);
14359
14360 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14361 rettv->vval.v_number = need_fileinfo;
14362 else
14363 semsg(_(e_invarg2), name);
14364 }
14365}
14366
14367/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014368 * "test_option_not_set({name})" function
14369 */
14370 static void
14371f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14372{
14373 char_u *name = (char_u *)"";
14374
14375 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014376 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014377 else
14378 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014379 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014380 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014381 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014382 }
14383}
14384
14385/*
14386 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014387 */
14388 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014389f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014390{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014391 char_u *name = (char_u *)"";
14392 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014393 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014394
14395 if (argvars[0].v_type != VAR_STRING
14396 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014397 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014398 else
14399 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014400 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014401 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014402
14403 if (STRCMP(name, (char_u *)"redraw") == 0)
14404 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014405 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14406 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014407 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14408 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014409 else if (STRCMP(name, (char_u *)"starting") == 0)
14410 {
14411 if (val)
14412 {
14413 if (save_starting < 0)
14414 save_starting = starting;
14415 starting = 0;
14416 }
14417 else
14418 {
14419 starting = save_starting;
14420 save_starting = -1;
14421 }
14422 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014423 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14424 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014425 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14426 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014427 else if (STRCMP(name, (char_u *)"ALL") == 0)
14428 {
14429 disable_char_avail_for_testing = FALSE;
14430 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014431 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014432 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014433 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014434 if (save_starting >= 0)
14435 {
14436 starting = save_starting;
14437 save_starting = -1;
14438 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014439 }
14440 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014441 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014442 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014443}
14444
14445/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014446 * "test_refcount({expr})" function
14447 */
14448 static void
14449f_test_refcount(typval_T *argvars, typval_T *rettv)
14450{
14451 int retval = -1;
14452
14453 switch (argvars[0].v_type)
14454 {
14455 case VAR_UNKNOWN:
14456 case VAR_NUMBER:
14457 case VAR_FLOAT:
14458 case VAR_SPECIAL:
14459 case VAR_STRING:
14460 break;
14461 case VAR_JOB:
14462#ifdef FEAT_JOB_CHANNEL
14463 if (argvars[0].vval.v_job != NULL)
14464 retval = argvars[0].vval.v_job->jv_refcount - 1;
14465#endif
14466 break;
14467 case VAR_CHANNEL:
14468#ifdef FEAT_JOB_CHANNEL
14469 if (argvars[0].vval.v_channel != NULL)
14470 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14471#endif
14472 break;
14473 case VAR_FUNC:
14474 if (argvars[0].vval.v_string != NULL)
14475 {
14476 ufunc_T *fp;
14477
14478 fp = find_func(argvars[0].vval.v_string);
14479 if (fp != NULL)
14480 retval = fp->uf_refcount;
14481 }
14482 break;
14483 case VAR_PARTIAL:
14484 if (argvars[0].vval.v_partial != NULL)
14485 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14486 break;
14487 case VAR_BLOB:
14488 if (argvars[0].vval.v_blob != NULL)
14489 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14490 break;
14491 case VAR_LIST:
14492 if (argvars[0].vval.v_list != NULL)
14493 retval = argvars[0].vval.v_list->lv_refcount - 1;
14494 break;
14495 case VAR_DICT:
14496 if (argvars[0].vval.v_dict != NULL)
14497 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14498 break;
14499 }
14500
14501 rettv->v_type = VAR_NUMBER;
14502 rettv->vval.v_number = retval;
14503
14504}
14505
14506/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014507 * "test_garbagecollect_now()" function
14508 */
14509 static void
14510f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14511{
14512 /* This is dangerous, any Lists and Dicts used internally may be freed
14513 * while still in use. */
14514 garbage_collect(TRUE);
14515}
14516
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014517/*
14518 * "test_ignore_error()" function
14519 */
14520 static void
14521f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14522{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014523 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014524}
14525
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014526 static void
14527f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14528{
14529 rettv->v_type = VAR_BLOB;
14530 rettv->vval.v_blob = NULL;
14531}
14532
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014533#ifdef FEAT_JOB_CHANNEL
14534 static void
14535f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14536{
14537 rettv->v_type = VAR_CHANNEL;
14538 rettv->vval.v_channel = NULL;
14539}
14540#endif
14541
14542 static void
14543f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14544{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014545 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014546}
14547
14548#ifdef FEAT_JOB_CHANNEL
14549 static void
14550f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14551{
14552 rettv->v_type = VAR_JOB;
14553 rettv->vval.v_job = NULL;
14554}
14555#endif
14556
14557 static void
14558f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14559{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014560 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014561}
14562
14563 static void
14564f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14565{
14566 rettv->v_type = VAR_PARTIAL;
14567 rettv->vval.v_partial = NULL;
14568}
14569
14570 static void
14571f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14572{
14573 rettv->v_type = VAR_STRING;
14574 rettv->vval.v_string = NULL;
14575}
14576
Bram Moolenaarab186732018-09-14 21:27:06 +020014577#ifdef FEAT_GUI
14578 static void
14579f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14580{
14581 char_u *which;
14582 long value;
14583 int dragging;
14584 scrollbar_T *sb = NULL;
14585
14586 if (argvars[0].v_type != VAR_STRING
14587 || (argvars[1].v_type) != VAR_NUMBER
14588 || (argvars[2].v_type) != VAR_NUMBER)
14589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014590 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014591 return;
14592 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014593 which = tv_get_string(&argvars[0]);
14594 value = tv_get_number(&argvars[1]);
14595 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014596
14597 if (STRCMP(which, "left") == 0)
14598 sb = &curwin->w_scrollbars[SBAR_LEFT];
14599 else if (STRCMP(which, "right") == 0)
14600 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14601 else if (STRCMP(which, "hor") == 0)
14602 sb = &gui.bottom_sbar;
14603 if (sb == NULL)
14604 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014605 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014606 return;
14607 }
14608 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014609# ifndef USE_ON_FLY_SCROLL
14610 // need to loop through normal_cmd() to handle the scroll events
14611 exec_normal(FALSE, TRUE, FALSE);
14612# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014613}
14614#endif
14615
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014616#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014617 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014618f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14619{
14620 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14621 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14622}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014623#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014624
14625 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014626f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14627{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014628 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014629}
14630
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014631/*
14632 * Get a callback from "arg". It can be a Funcref or a function name.
14633 * When "arg" is zero return an empty string.
14634 * Return NULL for an invalid argument.
14635 */
14636 char_u *
14637get_callback(typval_T *arg, partial_T **pp)
14638{
14639 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14640 {
14641 *pp = arg->vval.v_partial;
14642 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014643 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014644 }
14645 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014646 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014647 {
14648 func_ref(arg->vval.v_string);
14649 return arg->vval.v_string;
14650 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014651 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14652 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014653 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014654 return NULL;
14655}
14656
14657/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014658 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014659 */
14660 void
14661free_callback(char_u *callback, partial_T *partial)
14662{
14663 if (partial != NULL)
14664 partial_unref(partial);
14665 else if (callback != NULL)
14666 {
14667 func_unref(callback);
14668 vim_free(callback);
14669 }
14670}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014671
14672#ifdef FEAT_TIMERS
14673/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014674 * "timer_info([timer])" function
14675 */
14676 static void
14677f_timer_info(typval_T *argvars, typval_T *rettv)
14678{
14679 timer_T *timer = NULL;
14680
14681 if (rettv_list_alloc(rettv) != OK)
14682 return;
14683 if (argvars[0].v_type != VAR_UNKNOWN)
14684 {
14685 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014686 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014687 else
14688 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014689 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014690 if (timer != NULL)
14691 add_timer_info(rettv, timer);
14692 }
14693 }
14694 else
14695 add_timer_info_all(rettv);
14696}
14697
14698/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014699 * "timer_pause(timer, paused)" function
14700 */
14701 static void
14702f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14703{
14704 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014705 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014706
14707 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014708 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014709 else
14710 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014711 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014712 if (timer != NULL)
14713 timer->tr_paused = paused;
14714 }
14715}
14716
14717/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014718 * "timer_start(time, callback [, options])" function
14719 */
14720 static void
14721f_timer_start(typval_T *argvars, typval_T *rettv)
14722{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014723 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014724 timer_T *timer;
14725 int repeat = 0;
14726 char_u *callback;
14727 dict_T *dict;
14728 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014729
Bram Moolenaar75537a92016-09-05 22:45:28 +020014730 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014731 if (check_secure())
14732 return;
14733 if (argvars[2].v_type != VAR_UNKNOWN)
14734 {
14735 if (argvars[2].v_type != VAR_DICT
14736 || (dict = argvars[2].vval.v_dict) == NULL)
14737 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014738 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014739 return;
14740 }
14741 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014742 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014743 }
14744
Bram Moolenaar75537a92016-09-05 22:45:28 +020014745 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014746 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014747 return;
14748
14749 timer = create_timer(msec, repeat);
14750 if (timer == NULL)
14751 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014752 else
14753 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014754 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014755 timer->tr_callback = vim_strsave(callback);
14756 else
14757 /* pointer into the partial */
14758 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014759 timer->tr_partial = partial;
14760 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014761 }
14762}
14763
14764/*
14765 * "timer_stop(timer)" function
14766 */
14767 static void
14768f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14769{
14770 timer_T *timer;
14771
14772 if (argvars[0].v_type != VAR_NUMBER)
14773 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014774 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014775 return;
14776 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014777 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014778 if (timer != NULL)
14779 stop_timer(timer);
14780}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014781
14782/*
14783 * "timer_stopall()" function
14784 */
14785 static void
14786f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14787{
14788 stop_all_timers();
14789}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014790#endif
14791
14792/*
14793 * "tolower(string)" function
14794 */
14795 static void
14796f_tolower(typval_T *argvars, typval_T *rettv)
14797{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014798 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014799 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014800}
14801
14802/*
14803 * "toupper(string)" function
14804 */
14805 static void
14806f_toupper(typval_T *argvars, typval_T *rettv)
14807{
14808 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014809 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014810}
14811
14812/*
14813 * "tr(string, fromstr, tostr)" function
14814 */
14815 static void
14816f_tr(typval_T *argvars, typval_T *rettv)
14817{
14818 char_u *in_str;
14819 char_u *fromstr;
14820 char_u *tostr;
14821 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014822 int inlen;
14823 int fromlen;
14824 int tolen;
14825 int idx;
14826 char_u *cpstr;
14827 int cplen;
14828 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014829 char_u buf[NUMBUFLEN];
14830 char_u buf2[NUMBUFLEN];
14831 garray_T ga;
14832
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014833 in_str = tv_get_string(&argvars[0]);
14834 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14835 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014836
14837 /* Default return value: empty string. */
14838 rettv->v_type = VAR_STRING;
14839 rettv->vval.v_string = NULL;
14840 if (fromstr == NULL || tostr == NULL)
14841 return; /* type error; errmsg already given */
14842 ga_init2(&ga, (int)sizeof(char), 80);
14843
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014844 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014845 /* not multi-byte: fromstr and tostr must be the same length */
14846 if (STRLEN(fromstr) != STRLEN(tostr))
14847 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014848error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014849 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014850 ga_clear(&ga);
14851 return;
14852 }
14853
14854 /* fromstr and tostr have to contain the same number of chars */
14855 while (*in_str != NUL)
14856 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014857 if (has_mbyte)
14858 {
14859 inlen = (*mb_ptr2len)(in_str);
14860 cpstr = in_str;
14861 cplen = inlen;
14862 idx = 0;
14863 for (p = fromstr; *p != NUL; p += fromlen)
14864 {
14865 fromlen = (*mb_ptr2len)(p);
14866 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14867 {
14868 for (p = tostr; *p != NUL; p += tolen)
14869 {
14870 tolen = (*mb_ptr2len)(p);
14871 if (idx-- == 0)
14872 {
14873 cplen = tolen;
14874 cpstr = p;
14875 break;
14876 }
14877 }
14878 if (*p == NUL) /* tostr is shorter than fromstr */
14879 goto error;
14880 break;
14881 }
14882 ++idx;
14883 }
14884
14885 if (first && cpstr == in_str)
14886 {
14887 /* Check that fromstr and tostr have the same number of
14888 * (multi-byte) characters. Done only once when a character
14889 * of in_str doesn't appear in fromstr. */
14890 first = FALSE;
14891 for (p = tostr; *p != NUL; p += tolen)
14892 {
14893 tolen = (*mb_ptr2len)(p);
14894 --idx;
14895 }
14896 if (idx != 0)
14897 goto error;
14898 }
14899
14900 (void)ga_grow(&ga, cplen);
14901 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14902 ga.ga_len += cplen;
14903
14904 in_str += inlen;
14905 }
14906 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014907 {
14908 /* When not using multi-byte chars we can do it faster. */
14909 p = vim_strchr(fromstr, *in_str);
14910 if (p != NULL)
14911 ga_append(&ga, tostr[p - fromstr]);
14912 else
14913 ga_append(&ga, *in_str);
14914 ++in_str;
14915 }
14916 }
14917
14918 /* add a terminating NUL */
14919 (void)ga_grow(&ga, 1);
14920 ga_append(&ga, NUL);
14921
14922 rettv->vval.v_string = ga.ga_data;
14923}
14924
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014925/*
14926 * "trim({expr})" function
14927 */
14928 static void
14929f_trim(typval_T *argvars, typval_T *rettv)
14930{
14931 char_u buf1[NUMBUFLEN];
14932 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014933 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014934 char_u *mask = NULL;
14935 char_u *tail;
14936 char_u *prev;
14937 char_u *p;
14938 int c1;
14939
14940 rettv->v_type = VAR_STRING;
14941 if (head == NULL)
14942 {
14943 rettv->vval.v_string = NULL;
14944 return;
14945 }
14946
14947 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014948 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014949
14950 while (*head != NUL)
14951 {
14952 c1 = PTR2CHAR(head);
14953 if (mask == NULL)
14954 {
14955 if (c1 > ' ' && c1 != 0xa0)
14956 break;
14957 }
14958 else
14959 {
14960 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14961 if (c1 == PTR2CHAR(p))
14962 break;
14963 if (*p == NUL)
14964 break;
14965 }
14966 MB_PTR_ADV(head);
14967 }
14968
14969 for (tail = head + STRLEN(head); tail > head; tail = prev)
14970 {
14971 prev = tail;
14972 MB_PTR_BACK(head, prev);
14973 c1 = PTR2CHAR(prev);
14974 if (mask == NULL)
14975 {
14976 if (c1 > ' ' && c1 != 0xa0)
14977 break;
14978 }
14979 else
14980 {
14981 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14982 if (c1 == PTR2CHAR(p))
14983 break;
14984 if (*p == NUL)
14985 break;
14986 }
14987 }
14988 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14989}
14990
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014991#ifdef FEAT_FLOAT
14992/*
14993 * "trunc({float})" function
14994 */
14995 static void
14996f_trunc(typval_T *argvars, typval_T *rettv)
14997{
14998 float_T f = 0.0;
14999
15000 rettv->v_type = VAR_FLOAT;
15001 if (get_float_arg(argvars, &f) == OK)
15002 /* trunc() is not in C90, use floor() or ceil() instead. */
15003 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
15004 else
15005 rettv->vval.v_float = 0.0;
15006}
15007#endif
15008
15009/*
15010 * "type(expr)" function
15011 */
15012 static void
15013f_type(typval_T *argvars, typval_T *rettv)
15014{
15015 int n = -1;
15016
15017 switch (argvars[0].v_type)
15018 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020015019 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
15020 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015021 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020015022 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
15023 case VAR_LIST: n = VAR_TYPE_LIST; break;
15024 case VAR_DICT: n = VAR_TYPE_DICT; break;
15025 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015026 case VAR_SPECIAL:
15027 if (argvars[0].vval.v_number == VVAL_FALSE
15028 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020015029 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015030 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020015031 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015032 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020015033 case VAR_JOB: n = VAR_TYPE_JOB; break;
15034 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015035 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015036 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015037 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015038 n = -1;
15039 break;
15040 }
15041 rettv->vval.v_number = n;
15042}
15043
15044/*
15045 * "undofile(name)" function
15046 */
15047 static void
15048f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15049{
15050 rettv->v_type = VAR_STRING;
15051#ifdef FEAT_PERSISTENT_UNDO
15052 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015053 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015054
15055 if (*fname == NUL)
15056 {
15057 /* If there is no file name there will be no undo file. */
15058 rettv->vval.v_string = NULL;
15059 }
15060 else
15061 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015062 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015063
15064 if (ffname != NULL)
15065 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15066 vim_free(ffname);
15067 }
15068 }
15069#else
15070 rettv->vval.v_string = NULL;
15071#endif
15072}
15073
15074/*
15075 * "undotree()" function
15076 */
15077 static void
15078f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15079{
15080 if (rettv_dict_alloc(rettv) == OK)
15081 {
15082 dict_T *dict = rettv->vval.v_dict;
15083 list_T *list;
15084
Bram Moolenaare0be1672018-07-08 16:50:37 +020015085 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15086 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15087 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15088 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15089 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15090 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015091
15092 list = list_alloc();
15093 if (list != NULL)
15094 {
15095 u_eval_tree(curbuf->b_u_oldhead, list);
15096 dict_add_list(dict, "entries", list);
15097 }
15098 }
15099}
15100
15101/*
15102 * "values(dict)" function
15103 */
15104 static void
15105f_values(typval_T *argvars, typval_T *rettv)
15106{
15107 dict_list(argvars, rettv, 1);
15108}
15109
15110/*
15111 * "virtcol(string)" function
15112 */
15113 static void
15114f_virtcol(typval_T *argvars, typval_T *rettv)
15115{
15116 colnr_T vcol = 0;
15117 pos_T *fp;
15118 int fnum = curbuf->b_fnum;
15119
15120 fp = var2fpos(&argvars[0], FALSE, &fnum);
15121 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15122 && fnum == curbuf->b_fnum)
15123 {
15124 getvvcol(curwin, fp, NULL, NULL, &vcol);
15125 ++vcol;
15126 }
15127
15128 rettv->vval.v_number = vcol;
15129}
15130
15131/*
15132 * "visualmode()" function
15133 */
15134 static void
15135f_visualmode(typval_T *argvars, typval_T *rettv)
15136{
15137 char_u str[2];
15138
15139 rettv->v_type = VAR_STRING;
15140 str[0] = curbuf->b_visual_mode_eval;
15141 str[1] = NUL;
15142 rettv->vval.v_string = vim_strsave(str);
15143
15144 /* A non-zero number or non-empty string argument: reset mode. */
15145 if (non_zero_arg(&argvars[0]))
15146 curbuf->b_visual_mode_eval = NUL;
15147}
15148
15149/*
15150 * "wildmenumode()" function
15151 */
15152 static void
15153f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15154{
15155#ifdef FEAT_WILDMENU
15156 if (wild_menu_showing)
15157 rettv->vval.v_number = 1;
15158#endif
15159}
15160
15161/*
15162 * "winbufnr(nr)" function
15163 */
15164 static void
15165f_winbufnr(typval_T *argvars, typval_T *rettv)
15166{
15167 win_T *wp;
15168
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015169 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015170 if (wp == NULL)
15171 rettv->vval.v_number = -1;
15172 else
15173 rettv->vval.v_number = wp->w_buffer->b_fnum;
15174}
15175
15176/*
15177 * "wincol()" function
15178 */
15179 static void
15180f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15181{
15182 validate_cursor();
15183 rettv->vval.v_number = curwin->w_wcol + 1;
15184}
15185
15186/*
15187 * "winheight(nr)" function
15188 */
15189 static void
15190f_winheight(typval_T *argvars, typval_T *rettv)
15191{
15192 win_T *wp;
15193
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015194 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015195 if (wp == NULL)
15196 rettv->vval.v_number = -1;
15197 else
15198 rettv->vval.v_number = wp->w_height;
15199}
15200
15201/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015202 * "winlayout()" function
15203 */
15204 static void
15205f_winlayout(typval_T *argvars, typval_T *rettv)
15206{
15207 tabpage_T *tp;
15208
15209 if (rettv_list_alloc(rettv) != OK)
15210 return;
15211
15212 if (argvars[0].v_type == VAR_UNKNOWN)
15213 tp = curtab;
15214 else
15215 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015216 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015217 if (tp == NULL)
15218 return;
15219 }
15220
15221 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15222}
15223
15224/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015225 * "winline()" function
15226 */
15227 static void
15228f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15229{
15230 validate_cursor();
15231 rettv->vval.v_number = curwin->w_wrow + 1;
15232}
15233
15234/*
15235 * "winnr()" function
15236 */
15237 static void
15238f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15239{
15240 int nr = 1;
15241
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015242 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015243 rettv->vval.v_number = nr;
15244}
15245
15246/*
15247 * "winrestcmd()" function
15248 */
15249 static void
15250f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15251{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015252 win_T *wp;
15253 int winnr = 1;
15254 garray_T ga;
15255 char_u buf[50];
15256
15257 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015258 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015259 {
15260 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15261 ga_concat(&ga, buf);
15262 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15263 ga_concat(&ga, buf);
15264 ++winnr;
15265 }
15266 ga_append(&ga, NUL);
15267
15268 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015269 rettv->v_type = VAR_STRING;
15270}
15271
15272/*
15273 * "winrestview()" function
15274 */
15275 static void
15276f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15277{
15278 dict_T *dict;
15279
15280 if (argvars[0].v_type != VAR_DICT
15281 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015282 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015283 else
15284 {
15285 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015286 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015287 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015288 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015289 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015290 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015291 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15292 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015293 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015294 curwin->w_set_curswant = FALSE;
15295 }
15296
15297 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015298 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015299#ifdef FEAT_DIFF
15300 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015301 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015302#endif
15303 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015304 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015305 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015306 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015307
15308 check_cursor();
15309 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015310 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015311 changed_window_setting();
15312
15313 if (curwin->w_topline <= 0)
15314 curwin->w_topline = 1;
15315 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15316 curwin->w_topline = curbuf->b_ml.ml_line_count;
15317#ifdef FEAT_DIFF
15318 check_topfill(curwin, TRUE);
15319#endif
15320 }
15321}
15322
15323/*
15324 * "winsaveview()" function
15325 */
15326 static void
15327f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15328{
15329 dict_T *dict;
15330
15331 if (rettv_dict_alloc(rettv) == FAIL)
15332 return;
15333 dict = rettv->vval.v_dict;
15334
Bram Moolenaare0be1672018-07-08 16:50:37 +020015335 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15336 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015337 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015338 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015339 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015340
Bram Moolenaare0be1672018-07-08 16:50:37 +020015341 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015342#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015343 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015344#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015345 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15346 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015347}
15348
15349/*
15350 * "winwidth(nr)" function
15351 */
15352 static void
15353f_winwidth(typval_T *argvars, typval_T *rettv)
15354{
15355 win_T *wp;
15356
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015357 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015358 if (wp == NULL)
15359 rettv->vval.v_number = -1;
15360 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015361 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015362}
15363
15364/*
15365 * "wordcount()" function
15366 */
15367 static void
15368f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15369{
15370 if (rettv_dict_alloc(rettv) == FAIL)
15371 return;
15372 cursor_pos_info(rettv->vval.v_dict);
15373}
15374
15375/*
15376 * "writefile()" function
15377 */
15378 static void
15379f_writefile(typval_T *argvars, typval_T *rettv)
15380{
15381 int binary = FALSE;
15382 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015383#ifdef HAVE_FSYNC
15384 int do_fsync = p_fs;
15385#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015386 char_u *fname;
15387 FILE *fd;
15388 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015389 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015390 list_T *list = NULL;
15391 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015392
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015393 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015394 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015395 return;
15396
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015397 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015398 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015399 list = argvars[0].vval.v_list;
15400 if (list == NULL)
15401 return;
15402 for (li = list->lv_first; li != NULL; li = li->li_next)
15403 if (tv_get_string_chk(&li->li_tv) == NULL)
15404 return;
15405 }
15406 else if (argvars[0].v_type == VAR_BLOB)
15407 {
15408 blob = argvars[0].vval.v_blob;
15409 if (blob == NULL)
15410 return;
15411 }
15412 else
15413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015414 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015415 return;
15416 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015417
15418 if (argvars[2].v_type != VAR_UNKNOWN)
15419 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015420 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015421
15422 if (arg2 == NULL)
15423 return;
15424 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015425 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015426 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015427 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015428#ifdef HAVE_FSYNC
15429 if (vim_strchr(arg2, 's') != NULL)
15430 do_fsync = TRUE;
15431 else if (vim_strchr(arg2, 'S') != NULL)
15432 do_fsync = FALSE;
15433#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015434 }
15435
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015436 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015437 if (fname == NULL)
15438 return;
15439
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015440 /* Always open the file in binary mode, library functions have a mind of
15441 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015442 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15443 append ? APPENDBIN : WRITEBIN)) == NULL)
15444 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015445 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015446 ret = -1;
15447 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015448 else if (blob)
15449 {
15450 if (write_blob(fd, blob) == FAIL)
15451 ret = -1;
15452#ifdef HAVE_FSYNC
15453 else if (do_fsync)
15454 // Ignore the error, the user wouldn't know what to do about it.
15455 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015456 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015457#endif
15458 fclose(fd);
15459 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015460 else
15461 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015462 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015463 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015464#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015465 else if (do_fsync)
15466 /* Ignore the error, the user wouldn't know what to do about it.
15467 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015468 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015469#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015470 fclose(fd);
15471 }
15472
15473 rettv->vval.v_number = ret;
15474}
15475
15476/*
15477 * "xor(expr, expr)" function
15478 */
15479 static void
15480f_xor(typval_T *argvars, typval_T *rettv)
15481{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015482 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15483 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015484}
15485
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015486#endif /* FEAT_EVAL */