blob: 0dbd6514ecae4d9df9f8835b1eca78d21cf07825 [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 Moolenaarfe8ef982018-09-13 20:31:54 +0200445static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100446static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100447static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200448static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100449static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100450static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200451#ifdef FEAT_JOB_CHANNEL
452static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
453#endif
454static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
455#ifdef FEAT_JOB_CHANNEL
456static void f_test_null_job(typval_T *argvars, typval_T *rettv);
457#endif
458static void f_test_null_list(typval_T *argvars, typval_T *rettv);
459static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
460static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200461#ifdef FEAT_GUI
462static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
463#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200464#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200465static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200466#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200467static void f_test_settime(typval_T *argvars, typval_T *rettv);
468#ifdef FEAT_FLOAT
469static void f_tan(typval_T *argvars, typval_T *rettv);
470static void f_tanh(typval_T *argvars, typval_T *rettv);
471#endif
472#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200473static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200474static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200475static void f_timer_start(typval_T *argvars, typval_T *rettv);
476static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200477static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200478#endif
479static void f_tolower(typval_T *argvars, typval_T *rettv);
480static void f_toupper(typval_T *argvars, typval_T *rettv);
481static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100482static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200483#ifdef FEAT_FLOAT
484static void f_trunc(typval_T *argvars, typval_T *rettv);
485#endif
486static void f_type(typval_T *argvars, typval_T *rettv);
487static void f_undofile(typval_T *argvars, typval_T *rettv);
488static void f_undotree(typval_T *argvars, typval_T *rettv);
489static void f_uniq(typval_T *argvars, typval_T *rettv);
490static void f_values(typval_T *argvars, typval_T *rettv);
491static void f_virtcol(typval_T *argvars, typval_T *rettv);
492static void f_visualmode(typval_T *argvars, typval_T *rettv);
493static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
494static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
495static void f_win_getid(typval_T *argvars, typval_T *rettv);
496static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
497static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
498static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100499static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200500static void f_winbufnr(typval_T *argvars, typval_T *rettv);
501static void f_wincol(typval_T *argvars, typval_T *rettv);
502static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200503static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200504static void f_winline(typval_T *argvars, typval_T *rettv);
505static void f_winnr(typval_T *argvars, typval_T *rettv);
506static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
507static void f_winrestview(typval_T *argvars, typval_T *rettv);
508static void f_winsaveview(typval_T *argvars, typval_T *rettv);
509static void f_winwidth(typval_T *argvars, typval_T *rettv);
510static void f_writefile(typval_T *argvars, typval_T *rettv);
511static void f_wordcount(typval_T *argvars, typval_T *rettv);
512static void f_xor(typval_T *argvars, typval_T *rettv);
513
514/*
515 * Array with names and number of arguments of all internal functions
516 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
517 */
518static struct fst
519{
520 char *f_name; /* function name */
521 char f_min_argc; /* minimal number of arguments */
522 char f_max_argc; /* maximal number of arguments */
523 void (*f_func)(typval_T *args, typval_T *rvar);
524 /* implementation of function */
525} functions[] =
526{
527#ifdef FEAT_FLOAT
528 {"abs", 1, 1, f_abs},
529 {"acos", 1, 1, f_acos}, /* WJMc */
530#endif
531 {"add", 2, 2, f_add},
532 {"and", 2, 2, f_and},
533 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200534 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200535 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200536 {"argidx", 0, 0, f_argidx},
537 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200538 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200539#ifdef FEAT_FLOAT
540 {"asin", 1, 1, f_asin}, /* WJMc */
541#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100542 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200543 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100544 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200545 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200546 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200547 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100548 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200549 {"assert_match", 2, 3, f_assert_match},
550 {"assert_notequal", 2, 3, f_assert_notequal},
551 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100552 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200553 {"assert_true", 1, 2, f_assert_true},
554#ifdef FEAT_FLOAT
555 {"atan", 1, 1, f_atan},
556 {"atan2", 2, 2, f_atan2},
557#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100558#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200559 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100560 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100561# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100562 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100563# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100564#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200565 {"browse", 4, 4, f_browse},
566 {"browsedir", 2, 2, f_browsedir},
567 {"bufexists", 1, 1, f_bufexists},
568 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
569 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
570 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
571 {"buflisted", 1, 1, f_buflisted},
572 {"bufloaded", 1, 1, f_bufloaded},
573 {"bufname", 1, 1, f_bufname},
574 {"bufnr", 1, 2, f_bufnr},
575 {"bufwinid", 1, 1, f_bufwinid},
576 {"bufwinnr", 1, 1, f_bufwinnr},
577 {"byte2line", 1, 1, f_byte2line},
578 {"byteidx", 2, 2, f_byteidx},
579 {"byteidxcomp", 2, 2, f_byteidxcomp},
580 {"call", 2, 3, f_call},
581#ifdef FEAT_FLOAT
582 {"ceil", 1, 1, f_ceil},
583#endif
584#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100585 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200586 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200587 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200588 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
589 {"ch_evalraw", 2, 3, f_ch_evalraw},
590 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
591 {"ch_getjob", 1, 1, f_ch_getjob},
592 {"ch_info", 1, 1, f_ch_info},
593 {"ch_log", 1, 2, f_ch_log},
594 {"ch_logfile", 1, 2, f_ch_logfile},
595 {"ch_open", 1, 2, f_ch_open},
596 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100597 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200598 {"ch_readraw", 1, 2, f_ch_readraw},
599 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
600 {"ch_sendraw", 2, 3, f_ch_sendraw},
601 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200602 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200603#endif
604 {"changenr", 0, 0, f_changenr},
605 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200606 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200607 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100608 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200609 {"col", 1, 1, f_col},
610#if defined(FEAT_INS_EXPAND)
611 {"complete", 2, 2, f_complete},
612 {"complete_add", 1, 1, f_complete_add},
613 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100614 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200615#endif
616 {"confirm", 1, 4, f_confirm},
617 {"copy", 1, 1, f_copy},
618#ifdef FEAT_FLOAT
619 {"cos", 1, 1, f_cos},
620 {"cosh", 1, 1, f_cosh},
621#endif
622 {"count", 2, 4, f_count},
623 {"cscope_connection",0,3, f_cscope_connection},
624 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100625#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200626 {"debugbreak", 1, 1, f_debugbreak},
627#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200628 {"deepcopy", 1, 2, f_deepcopy},
629 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200630 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200631 {"did_filetype", 0, 0, f_did_filetype},
632 {"diff_filler", 1, 1, f_diff_filler},
633 {"diff_hlID", 2, 2, f_diff_hlID},
634 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200635 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636 {"escape", 2, 2, f_escape},
637 {"eval", 1, 1, f_eval},
638 {"eventhandler", 0, 0, f_eventhandler},
639 {"executable", 1, 1, f_executable},
640 {"execute", 1, 2, f_execute},
641 {"exepath", 1, 1, f_exepath},
642 {"exists", 1, 1, f_exists},
643#ifdef FEAT_FLOAT
644 {"exp", 1, 1, f_exp},
645#endif
646 {"expand", 1, 3, f_expand},
647 {"extend", 2, 3, f_extend},
648 {"feedkeys", 1, 2, f_feedkeys},
649 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
650 {"filereadable", 1, 1, f_filereadable},
651 {"filewritable", 1, 1, f_filewritable},
652 {"filter", 2, 2, f_filter},
653 {"finddir", 1, 3, f_finddir},
654 {"findfile", 1, 3, f_findfile},
655#ifdef FEAT_FLOAT
656 {"float2nr", 1, 1, f_float2nr},
657 {"floor", 1, 1, f_floor},
658 {"fmod", 2, 2, f_fmod},
659#endif
660 {"fnameescape", 1, 1, f_fnameescape},
661 {"fnamemodify", 2, 2, f_fnamemodify},
662 {"foldclosed", 1, 1, f_foldclosed},
663 {"foldclosedend", 1, 1, f_foldclosedend},
664 {"foldlevel", 1, 1, f_foldlevel},
665 {"foldtext", 0, 0, f_foldtext},
666 {"foldtextresult", 1, 1, f_foldtextresult},
667 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200668 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200669 {"function", 1, 3, f_function},
670 {"garbagecollect", 0, 1, f_garbagecollect},
671 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200672 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200673 {"getbufline", 2, 3, f_getbufline},
674 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100675 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200676 {"getchar", 0, 1, f_getchar},
677 {"getcharmod", 0, 0, f_getcharmod},
678 {"getcharsearch", 0, 0, f_getcharsearch},
679 {"getcmdline", 0, 0, f_getcmdline},
680 {"getcmdpos", 0, 0, f_getcmdpos},
681 {"getcmdtype", 0, 0, f_getcmdtype},
682 {"getcmdwintype", 0, 0, f_getcmdwintype},
683#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200684 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200685#endif
686 {"getcurpos", 0, 0, f_getcurpos},
687 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200688 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200689 {"getfontname", 0, 1, f_getfontname},
690 {"getfperm", 1, 1, f_getfperm},
691 {"getfsize", 1, 1, f_getfsize},
692 {"getftime", 1, 1, f_getftime},
693 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100694 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200695 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200696 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100697 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200698 {"getpid", 0, 0, f_getpid},
699 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200700 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200701 {"getreg", 0, 3, f_getreg},
702 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200703 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200704 {"gettabvar", 2, 3, f_gettabvar},
705 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100706 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200707 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100708 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200709 {"getwinposx", 0, 0, f_getwinposx},
710 {"getwinposy", 0, 0, f_getwinposy},
711 {"getwinvar", 2, 3, f_getwinvar},
712 {"glob", 1, 4, f_glob},
713 {"glob2regpat", 1, 1, f_glob2regpat},
714 {"globpath", 2, 5, f_globpath},
715 {"has", 1, 1, f_has},
716 {"has_key", 2, 2, f_has_key},
717 {"haslocaldir", 0, 2, f_haslocaldir},
718 {"hasmapto", 1, 3, f_hasmapto},
719 {"highlightID", 1, 1, f_hlID}, /* obsolete */
720 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
721 {"histadd", 2, 2, f_histadd},
722 {"histdel", 1, 2, f_histdel},
723 {"histget", 1, 2, f_histget},
724 {"histnr", 1, 1, f_histnr},
725 {"hlID", 1, 1, f_hlID},
726 {"hlexists", 1, 1, f_hlexists},
727 {"hostname", 0, 0, f_hostname},
728 {"iconv", 3, 3, f_iconv},
729 {"indent", 1, 1, f_indent},
730 {"index", 2, 4, f_index},
731 {"input", 1, 3, f_input},
732 {"inputdialog", 1, 3, f_inputdialog},
733 {"inputlist", 1, 1, f_inputlist},
734 {"inputrestore", 0, 0, f_inputrestore},
735 {"inputsave", 0, 0, f_inputsave},
736 {"inputsecret", 1, 2, f_inputsecret},
737 {"insert", 2, 3, f_insert},
738 {"invert", 1, 1, f_invert},
739 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200740#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
741 {"isinf", 1, 1, f_isinf},
742#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200743 {"islocked", 1, 1, f_islocked},
744#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
745 {"isnan", 1, 1, f_isnan},
746#endif
747 {"items", 1, 1, f_items},
748#ifdef FEAT_JOB_CHANNEL
749 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200750 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200751 {"job_setoptions", 2, 2, f_job_setoptions},
752 {"job_start", 1, 2, f_job_start},
753 {"job_status", 1, 1, f_job_status},
754 {"job_stop", 1, 2, f_job_stop},
755#endif
756 {"join", 1, 2, f_join},
757 {"js_decode", 1, 1, f_js_decode},
758 {"js_encode", 1, 1, f_js_encode},
759 {"json_decode", 1, 1, f_json_decode},
760 {"json_encode", 1, 1, f_json_encode},
761 {"keys", 1, 1, f_keys},
762 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
763 {"len", 1, 1, f_len},
764 {"libcall", 3, 3, f_libcall},
765 {"libcallnr", 3, 3, f_libcallnr},
766 {"line", 1, 1, f_line},
767 {"line2byte", 1, 1, f_line2byte},
768 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200769 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200770 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200771 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200772 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200773 {"localtime", 0, 0, f_localtime},
774#ifdef FEAT_FLOAT
775 {"log", 1, 1, f_log},
776 {"log10", 1, 1, f_log10},
777#endif
778#ifdef FEAT_LUA
779 {"luaeval", 1, 2, f_luaeval},
780#endif
781 {"map", 2, 2, f_map},
782 {"maparg", 1, 4, f_maparg},
783 {"mapcheck", 1, 3, f_mapcheck},
784 {"match", 2, 4, f_match},
785 {"matchadd", 2, 5, f_matchadd},
786 {"matchaddpos", 2, 5, f_matchaddpos},
787 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100788 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200789 {"matchend", 2, 4, f_matchend},
790 {"matchlist", 2, 4, f_matchlist},
791 {"matchstr", 2, 4, f_matchstr},
792 {"matchstrpos", 2, 4, f_matchstrpos},
793 {"max", 1, 1, f_max},
794 {"min", 1, 1, f_min},
795#ifdef vim_mkdir
796 {"mkdir", 1, 3, f_mkdir},
797#endif
798 {"mode", 0, 1, f_mode},
799#ifdef FEAT_MZSCHEME
800 {"mzeval", 1, 1, f_mzeval},
801#endif
802 {"nextnonblank", 1, 1, f_nextnonblank},
803 {"nr2char", 1, 2, f_nr2char},
804 {"or", 2, 2, f_or},
805 {"pathshorten", 1, 1, f_pathshorten},
806#ifdef FEAT_PERL
807 {"perleval", 1, 1, f_perleval},
808#endif
809#ifdef FEAT_FLOAT
810 {"pow", 2, 2, f_pow},
811#endif
812 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100813 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200814#ifdef FEAT_JOB_CHANNEL
815 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200816 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200817 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
818#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100819#ifdef FEAT_TEXT_PROP
820 {"prop_add", 3, 3, f_prop_add},
821 {"prop_clear", 1, 3, f_prop_clear},
822 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100823 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100824 {"prop_type_add", 2, 2, f_prop_type_add},
825 {"prop_type_change", 2, 2, f_prop_type_change},
826 {"prop_type_delete", 1, 2, f_prop_type_delete},
827 {"prop_type_get", 1, 2, f_prop_type_get},
828 {"prop_type_list", 0, 1, f_prop_type_list},
829#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200830 {"pumvisible", 0, 0, f_pumvisible},
831#ifdef FEAT_PYTHON3
832 {"py3eval", 1, 1, f_py3eval},
833#endif
834#ifdef FEAT_PYTHON
835 {"pyeval", 1, 1, f_pyeval},
836#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100837#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
838 {"pyxeval", 1, 1, f_pyxeval},
839#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200840 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200841 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200842 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200843 {"reg_executing", 0, 0, f_reg_executing},
844 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200845 {"reltime", 0, 2, f_reltime},
846#ifdef FEAT_FLOAT
847 {"reltimefloat", 1, 1, f_reltimefloat},
848#endif
849 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100850 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200851 {"remote_foreground", 1, 1, f_remote_foreground},
852 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100853 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200854 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100855 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200856 {"remove", 2, 3, f_remove},
857 {"rename", 2, 2, f_rename},
858 {"repeat", 2, 2, f_repeat},
859 {"resolve", 1, 1, f_resolve},
860 {"reverse", 1, 1, f_reverse},
861#ifdef FEAT_FLOAT
862 {"round", 1, 1, f_round},
863#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100864#ifdef FEAT_RUBY
865 {"rubyeval", 1, 1, f_rubyeval},
866#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200867 {"screenattr", 2, 2, f_screenattr},
868 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100869 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200870 {"screencol", 0, 0, f_screencol},
871 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100872 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200873 {"search", 1, 4, f_search},
874 {"searchdecl", 1, 3, f_searchdecl},
875 {"searchpair", 3, 7, f_searchpair},
876 {"searchpairpos", 3, 7, f_searchpairpos},
877 {"searchpos", 1, 4, f_searchpos},
878 {"server2client", 2, 2, f_server2client},
879 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200880 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200881 {"setbufvar", 3, 3, f_setbufvar},
882 {"setcharsearch", 1, 1, f_setcharsearch},
883 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200884 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200885 {"setfperm", 2, 2, f_setfperm},
886 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200887 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100888 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200889 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200890 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200891 {"setreg", 2, 3, f_setreg},
892 {"settabvar", 3, 3, f_settabvar},
893 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100894 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200895 {"setwinvar", 3, 3, f_setwinvar},
896#ifdef FEAT_CRYPT
897 {"sha256", 1, 1, f_sha256},
898#endif
899 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100900 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100901#ifdef FEAT_SIGNS
902 {"sign_define", 1, 2, f_sign_define},
903 {"sign_getdefined", 0, 1, f_sign_getdefined},
904 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100905 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100906 {"sign_place", 4, 5, f_sign_place},
907 {"sign_undefine", 0, 1, f_sign_undefine},
908 {"sign_unplace", 1, 2, f_sign_unplace},
909#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910 {"simplify", 1, 1, f_simplify},
911#ifdef FEAT_FLOAT
912 {"sin", 1, 1, f_sin},
913 {"sinh", 1, 1, f_sinh},
914#endif
915 {"sort", 1, 3, f_sort},
916 {"soundfold", 1, 1, f_soundfold},
917 {"spellbadword", 0, 1, f_spellbadword},
918 {"spellsuggest", 1, 3, f_spellsuggest},
919 {"split", 1, 3, f_split},
920#ifdef FEAT_FLOAT
921 {"sqrt", 1, 1, f_sqrt},
922 {"str2float", 1, 1, f_str2float},
923#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200924 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200925 {"str2nr", 1, 2, f_str2nr},
926 {"strcharpart", 2, 3, f_strcharpart},
927 {"strchars", 1, 2, f_strchars},
928 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
929#ifdef HAVE_STRFTIME
930 {"strftime", 1, 2, f_strftime},
931#endif
932 {"strgetchar", 2, 2, f_strgetchar},
933 {"stridx", 2, 3, f_stridx},
934 {"string", 1, 1, f_string},
935 {"strlen", 1, 1, f_strlen},
936 {"strpart", 2, 3, f_strpart},
937 {"strridx", 2, 3, f_strridx},
938 {"strtrans", 1, 1, f_strtrans},
939 {"strwidth", 1, 1, f_strwidth},
940 {"submatch", 1, 2, f_submatch},
941 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200942 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200943 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200944 {"synID", 3, 3, f_synID},
945 {"synIDattr", 2, 3, f_synIDattr},
946 {"synIDtrans", 1, 1, f_synIDtrans},
947 {"synconcealed", 2, 2, f_synconcealed},
948 {"synstack", 2, 2, f_synstack},
949 {"system", 1, 2, f_system},
950 {"systemlist", 1, 2, f_systemlist},
951 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
952 {"tabpagenr", 0, 1, f_tabpagenr},
953 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
954 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100955 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200956#ifdef FEAT_FLOAT
957 {"tan", 1, 1, f_tan},
958 {"tanh", 1, 1, f_tanh},
959#endif
960 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200961#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100962 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
963 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100964 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200965 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200966# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
967 {"term_getansicolors", 1, 1, f_term_getansicolors},
968# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200969 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200970 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200971 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200972 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200973 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200974 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200975 {"term_getstatus", 1, 1, f_term_getstatus},
976 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200977 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200978 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200979 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200980 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200981# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
982 {"term_setansicolors", 2, 2, f_term_setansicolors},
983# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100984 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100985 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200986 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200987 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200988 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200989#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200990 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
991 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200992 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200993 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100994 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100995 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200996#ifdef FEAT_JOB_CHANNEL
997 {"test_null_channel", 0, 0, f_test_null_channel},
998#endif
999 {"test_null_dict", 0, 0, f_test_null_dict},
1000#ifdef FEAT_JOB_CHANNEL
1001 {"test_null_job", 0, 0, f_test_null_job},
1002#endif
1003 {"test_null_list", 0, 0, f_test_null_list},
1004 {"test_null_partial", 0, 0, f_test_null_partial},
1005 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001006 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001007 {"test_override", 2, 2, f_test_override},
1008 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001009#ifdef FEAT_GUI
1010 {"test_scrollbar", 3, 3, f_test_scrollbar},
1011#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001012#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001013 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001014#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001015 {"test_settime", 1, 1, f_test_settime},
1016#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001017 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001018 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001019 {"timer_start", 2, 3, f_timer_start},
1020 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001021 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001022#endif
1023 {"tolower", 1, 1, f_tolower},
1024 {"toupper", 1, 1, f_toupper},
1025 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001026 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001027#ifdef FEAT_FLOAT
1028 {"trunc", 1, 1, f_trunc},
1029#endif
1030 {"type", 1, 1, f_type},
1031 {"undofile", 1, 1, f_undofile},
1032 {"undotree", 0, 0, f_undotree},
1033 {"uniq", 1, 3, f_uniq},
1034 {"values", 1, 1, f_values},
1035 {"virtcol", 1, 1, f_virtcol},
1036 {"visualmode", 0, 1, f_visualmode},
1037 {"wildmenumode", 0, 0, f_wildmenumode},
1038 {"win_findbuf", 1, 1, f_win_findbuf},
1039 {"win_getid", 0, 2, f_win_getid},
1040 {"win_gotoid", 1, 1, f_win_gotoid},
1041 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1042 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001043 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001044 {"winbufnr", 1, 1, f_winbufnr},
1045 {"wincol", 0, 0, f_wincol},
1046 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001047 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001048 {"winline", 0, 0, f_winline},
1049 {"winnr", 0, 1, f_winnr},
1050 {"winrestcmd", 0, 0, f_winrestcmd},
1051 {"winrestview", 1, 1, f_winrestview},
1052 {"winsaveview", 0, 0, f_winsaveview},
1053 {"winwidth", 1, 1, f_winwidth},
1054 {"wordcount", 0, 0, f_wordcount},
1055 {"writefile", 2, 3, f_writefile},
1056 {"xor", 2, 2, f_xor},
1057};
1058
1059#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1060
1061/*
1062 * Function given to ExpandGeneric() to obtain the list of internal
1063 * or user defined function names.
1064 */
1065 char_u *
1066get_function_name(expand_T *xp, int idx)
1067{
1068 static int intidx = -1;
1069 char_u *name;
1070
1071 if (idx == 0)
1072 intidx = -1;
1073 if (intidx < 0)
1074 {
1075 name = get_user_func_name(xp, idx);
1076 if (name != NULL)
1077 return name;
1078 }
1079 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1080 {
1081 STRCPY(IObuff, functions[intidx].f_name);
1082 STRCAT(IObuff, "(");
1083 if (functions[intidx].f_max_argc == 0)
1084 STRCAT(IObuff, ")");
1085 return IObuff;
1086 }
1087
1088 return NULL;
1089}
1090
1091/*
1092 * Function given to ExpandGeneric() to obtain the list of internal or
1093 * user defined variable or function names.
1094 */
1095 char_u *
1096get_expr_name(expand_T *xp, int idx)
1097{
1098 static int intidx = -1;
1099 char_u *name;
1100
1101 if (idx == 0)
1102 intidx = -1;
1103 if (intidx < 0)
1104 {
1105 name = get_function_name(xp, idx);
1106 if (name != NULL)
1107 return name;
1108 }
1109 return get_user_var_name(xp, ++intidx);
1110}
1111
1112#endif /* FEAT_CMDL_COMPL */
1113
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001114/*
1115 * Find internal function in table above.
1116 * Return index, or -1 if not found
1117 */
1118 int
1119find_internal_func(
1120 char_u *name) /* name of the function */
1121{
1122 int first = 0;
1123 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1124 int cmp;
1125 int x;
1126
1127 /*
1128 * Find the function name in the table. Binary search.
1129 */
1130 while (first <= last)
1131 {
1132 x = first + ((unsigned)(last - first) >> 1);
1133 cmp = STRCMP(name, functions[x].f_name);
1134 if (cmp < 0)
1135 last = x - 1;
1136 else if (cmp > 0)
1137 first = x + 1;
1138 else
1139 return x;
1140 }
1141 return -1;
1142}
1143
1144 int
1145call_internal_func(
1146 char_u *name,
1147 int argcount,
1148 typval_T *argvars,
1149 typval_T *rettv)
1150{
1151 int i;
1152
1153 i = find_internal_func(name);
1154 if (i < 0)
1155 return ERROR_UNKNOWN;
1156 if (argcount < functions[i].f_min_argc)
1157 return ERROR_TOOFEW;
1158 if (argcount > functions[i].f_max_argc)
1159 return ERROR_TOOMANY;
1160 argvars[argcount].v_type = VAR_UNKNOWN;
1161 functions[i].f_func(argvars, rettv);
1162 return ERROR_NONE;
1163}
1164
1165/*
1166 * Return TRUE for a non-zero Number and a non-empty String.
1167 */
1168 static int
1169non_zero_arg(typval_T *argvars)
1170{
1171 return ((argvars[0].v_type == VAR_NUMBER
1172 && argvars[0].vval.v_number != 0)
1173 || (argvars[0].v_type == VAR_SPECIAL
1174 && argvars[0].vval.v_number == VVAL_TRUE)
1175 || (argvars[0].v_type == VAR_STRING
1176 && argvars[0].vval.v_string != NULL
1177 && *argvars[0].vval.v_string != NUL));
1178}
1179
1180/*
1181 * Get the lnum from the first argument.
1182 * Also accepts ".", "$", etc., but that only works for the current buffer.
1183 * Returns -1 on error.
1184 */
1185 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001186tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001187{
1188 typval_T rettv;
1189 linenr_T lnum;
1190
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001191 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001192 if (lnum == 0) /* no valid number, try using line() */
1193 {
1194 rettv.v_type = VAR_NUMBER;
1195 f_line(argvars, &rettv);
1196 lnum = (linenr_T)rettv.vval.v_number;
1197 clear_tv(&rettv);
1198 }
1199 return lnum;
1200}
1201
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001202/*
1203 * Get the lnum from the first argument.
1204 * Also accepts "$", then "buf" is used.
1205 * Returns 0 on error.
1206 */
1207 static linenr_T
1208tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1209{
1210 if (argvars[0].v_type == VAR_STRING
1211 && argvars[0].vval.v_string != NULL
1212 && argvars[0].vval.v_string[0] == '$'
1213 && buf != NULL)
1214 return buf->b_ml.ml_line_count;
1215 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1216}
1217
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001218#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001219/*
1220 * Get the float value of "argvars[0]" into "f".
1221 * Returns FAIL when the argument is not a Number or Float.
1222 */
1223 static int
1224get_float_arg(typval_T *argvars, float_T *f)
1225{
1226 if (argvars[0].v_type == VAR_FLOAT)
1227 {
1228 *f = argvars[0].vval.v_float;
1229 return OK;
1230 }
1231 if (argvars[0].v_type == VAR_NUMBER)
1232 {
1233 *f = (float_T)argvars[0].vval.v_number;
1234 return OK;
1235 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001236 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001237 return FAIL;
1238}
1239
1240/*
1241 * "abs(expr)" function
1242 */
1243 static void
1244f_abs(typval_T *argvars, typval_T *rettv)
1245{
1246 if (argvars[0].v_type == VAR_FLOAT)
1247 {
1248 rettv->v_type = VAR_FLOAT;
1249 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1250 }
1251 else
1252 {
1253 varnumber_T n;
1254 int error = FALSE;
1255
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001256 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001257 if (error)
1258 rettv->vval.v_number = -1;
1259 else if (n > 0)
1260 rettv->vval.v_number = n;
1261 else
1262 rettv->vval.v_number = -n;
1263 }
1264}
1265
1266/*
1267 * "acos()" function
1268 */
1269 static void
1270f_acos(typval_T *argvars, typval_T *rettv)
1271{
1272 float_T f = 0.0;
1273
1274 rettv->v_type = VAR_FLOAT;
1275 if (get_float_arg(argvars, &f) == OK)
1276 rettv->vval.v_float = acos(f);
1277 else
1278 rettv->vval.v_float = 0.0;
1279}
1280#endif
1281
1282/*
1283 * "add(list, item)" function
1284 */
1285 static void
1286f_add(typval_T *argvars, typval_T *rettv)
1287{
1288 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001289 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001290
1291 rettv->vval.v_number = 1; /* Default: Failed */
1292 if (argvars[0].v_type == VAR_LIST)
1293 {
1294 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001295 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001296 (char_u *)N_("add() argument"), TRUE)
1297 && list_append_tv(l, &argvars[1]) == OK)
1298 copy_tv(&argvars[0], rettv);
1299 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001300 else if (argvars[0].v_type == VAR_BLOB)
1301 {
1302 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001303 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001304 (char_u *)N_("add() argument"), TRUE))
1305 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001306 int error = FALSE;
1307 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1308
1309 if (!error)
1310 {
1311 ga_append(&b->bv_ga, (int)n);
1312 copy_tv(&argvars[0], rettv);
1313 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001314 }
1315 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001316 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001317 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001318}
1319
1320/*
1321 * "and(expr, expr)" function
1322 */
1323 static void
1324f_and(typval_T *argvars, typval_T *rettv)
1325{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001326 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1327 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001328}
1329
1330/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001331 * If there is a window for "curbuf", make it the current window.
1332 */
1333 static void
1334find_win_for_curbuf(void)
1335{
1336 wininfo_T *wip;
1337
1338 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1339 {
1340 if (wip->wi_win != NULL)
1341 {
1342 curwin = wip->wi_win;
1343 break;
1344 }
1345 }
1346}
1347
1348/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001349 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001350 */
1351 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001352set_buffer_lines(
1353 buf_T *buf,
1354 linenr_T lnum_arg,
1355 int append,
1356 typval_T *lines,
1357 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358{
Bram Moolenaarca851592018-06-06 21:04:07 +02001359 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1360 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001361 list_T *l = NULL;
1362 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001363 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001364 linenr_T append_lnum;
1365 buf_T *curbuf_save = NULL;
1366 win_T *curwin_save = NULL;
1367 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001368
Bram Moolenaarca851592018-06-06 21:04:07 +02001369 /* When using the current buffer ml_mfp will be set if needed. Useful when
1370 * setline() is used on startup. For other buffers the buffer must be
1371 * loaded. */
1372 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001373 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001374 rettv->vval.v_number = 1; /* FAIL */
1375 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001376 }
1377
Bram Moolenaarca851592018-06-06 21:04:07 +02001378 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001379 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001380 curbuf_save = curbuf;
1381 curwin_save = curwin;
1382 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001383 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001384 }
1385
1386 if (append)
1387 // appendbufline() uses the line number below which we insert
1388 append_lnum = lnum - 1;
1389 else
1390 // setbufline() uses the line number above which we insert, we only
1391 // append if it's below the last line
1392 append_lnum = curbuf->b_ml.ml_line_count;
1393
1394 if (lines->v_type == VAR_LIST)
1395 {
1396 l = lines->vval.v_list;
1397 li = l->lv_first;
1398 }
1399 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001400 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001401
1402 /* default result is zero == OK */
1403 for (;;)
1404 {
1405 if (l != NULL)
1406 {
1407 /* list argument, get next string */
1408 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001409 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001410 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001411 li = li->li_next;
1412 }
1413
Bram Moolenaarca851592018-06-06 21:04:07 +02001414 rettv->vval.v_number = 1; /* FAIL */
1415 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1416 break;
1417
1418 /* When coming here from Insert mode, sync undo, so that this can be
1419 * undone separately from what was previously inserted. */
1420 if (u_sync_once == 2)
1421 {
1422 u_sync_once = 1; /* notify that u_sync() was called */
1423 u_sync(TRUE);
1424 }
1425
1426 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1427 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001428 // Existing line, replace it.
1429 // Removes any existing text properties.
1430 if (u_savesub(lnum) == OK && ml_replace_len(
1431 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001432 {
1433 changed_bytes(lnum, 0);
1434 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1435 check_cursor_col();
1436 rettv->vval.v_number = 0; /* OK */
1437 }
1438 }
1439 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1440 {
1441 /* append the line */
1442 ++added;
1443 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1444 rettv->vval.v_number = 0; /* OK */
1445 }
1446
1447 if (l == NULL) /* only one string argument */
1448 break;
1449 ++lnum;
1450 }
1451
1452 if (added > 0)
1453 {
1454 win_T *wp;
1455 tabpage_T *tp;
1456
1457 appended_lines_mark(append_lnum, added);
1458 FOR_ALL_TAB_WINDOWS(tp, wp)
1459 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1460 wp->w_cursor.lnum += added;
1461 check_cursor_col();
1462
Bram Moolenaarf2732452018-06-03 14:47:35 +02001463#ifdef FEAT_JOB_CHANNEL
1464 if (bt_prompt(curbuf) && (State & INSERT))
1465 // show the line with the prompt
1466 update_topline();
1467#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001468 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001469
1470 if (!is_curbuf)
1471 {
1472 curbuf = curbuf_save;
1473 curwin = curwin_save;
1474 }
1475}
1476
1477/*
1478 * "append(lnum, string/list)" function
1479 */
1480 static void
1481f_append(typval_T *argvars, typval_T *rettv)
1482{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001483 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001484
1485 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1486}
1487
1488/*
1489 * "appendbufline(buf, lnum, string/list)" function
1490 */
1491 static void
1492f_appendbufline(typval_T *argvars, typval_T *rettv)
1493{
1494 linenr_T lnum;
1495 buf_T *buf;
1496
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001497 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001498 if (buf == NULL)
1499 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001500 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001501 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001502 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001503 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1504 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001505}
1506
1507/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001508 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001509 */
1510 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001511f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001512{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001513 win_T *wp;
1514
1515 if (argvars[0].v_type == VAR_UNKNOWN)
1516 // use the current window
1517 rettv->vval.v_number = ARGCOUNT;
1518 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001519 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001520 // use the global argument list
1521 rettv->vval.v_number = GARGCOUNT;
1522 else
1523 {
1524 // use the argument list of the specified window
1525 wp = find_win_by_nr_or_id(&argvars[0]);
1526 if (wp != NULL)
1527 rettv->vval.v_number = WARGCOUNT(wp);
1528 else
1529 rettv->vval.v_number = -1;
1530 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001531}
1532
1533/*
1534 * "argidx()" function
1535 */
1536 static void
1537f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1538{
1539 rettv->vval.v_number = curwin->w_arg_idx;
1540}
1541
1542/*
1543 * "arglistid()" function
1544 */
1545 static void
1546f_arglistid(typval_T *argvars, typval_T *rettv)
1547{
1548 win_T *wp;
1549
1550 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001551 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001552 if (wp != NULL)
1553 rettv->vval.v_number = wp->w_alist->id;
1554}
1555
1556/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001557 * Get the argument list for a given window
1558 */
1559 static void
1560get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1561{
1562 int idx;
1563
1564 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1565 for (idx = 0; idx < argcount; ++idx)
1566 list_append_string(rettv->vval.v_list,
1567 alist_name(&arglist[idx]), -1);
1568}
1569
1570/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001571 * "argv(nr)" function
1572 */
1573 static void
1574f_argv(typval_T *argvars, typval_T *rettv)
1575{
1576 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001577 aentry_T *arglist = NULL;
1578 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001579
1580 if (argvars[0].v_type != VAR_UNKNOWN)
1581 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001582 if (argvars[1].v_type == VAR_UNKNOWN)
1583 {
1584 arglist = ARGLIST;
1585 argcount = ARGCOUNT;
1586 }
1587 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001588 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001589 {
1590 arglist = GARGLIST;
1591 argcount = GARGCOUNT;
1592 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001593 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001594 {
1595 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1596
1597 if (wp != NULL)
1598 {
1599 /* Use the argument list of the specified window */
1600 arglist = WARGLIST(wp);
1601 argcount = WARGCOUNT(wp);
1602 }
1603 }
1604
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001606 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001607 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001608 if (arglist != NULL && idx >= 0 && idx < argcount)
1609 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1610 else if (idx == -1)
1611 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001612 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001613 else
1614 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001615}
1616
1617/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001618 * "assert_beeps(cmd [, error])" function
1619 */
1620 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001621f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001622{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001623 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001624}
1625
1626/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001627 * "assert_equal(expected, actual[, msg])" function
1628 */
1629 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001630f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001631{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001632 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001633}
1634
1635/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001636 * "assert_equalfile(fname-one, fname-two)" function
1637 */
1638 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001639f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001640{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001641 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001642}
1643
1644/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001645 * "assert_notequal(expected, actual[, msg])" function
1646 */
1647 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001648f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001649{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001650 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001651}
1652
1653/*
1654 * "assert_exception(string[, msg])" function
1655 */
1656 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001657f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001658{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001659 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001660}
1661
1662/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001663 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001664 */
1665 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001666f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001667{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001668 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001669}
1670
1671/*
1672 * "assert_false(actual[, msg])" function
1673 */
1674 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001675f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001676{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001677 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001678}
1679
1680/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001681 * "assert_inrange(lower, upper[, msg])" function
1682 */
1683 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001684f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001685{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001686 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001687}
1688
1689/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001690 * "assert_match(pattern, actual[, msg])" function
1691 */
1692 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001693f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001694{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001695 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001696}
1697
1698/*
1699 * "assert_notmatch(pattern, actual[, msg])" function
1700 */
1701 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001702f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001703{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001704 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001705}
1706
1707/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001708 * "assert_report(msg)" function
1709 */
1710 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001711f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001712{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001713 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001714}
1715
1716/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001717 * "assert_true(actual[, msg])" function
1718 */
1719 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001720f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001721{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001722 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001723}
1724
1725#ifdef FEAT_FLOAT
1726/*
1727 * "asin()" function
1728 */
1729 static void
1730f_asin(typval_T *argvars, typval_T *rettv)
1731{
1732 float_T f = 0.0;
1733
1734 rettv->v_type = VAR_FLOAT;
1735 if (get_float_arg(argvars, &f) == OK)
1736 rettv->vval.v_float = asin(f);
1737 else
1738 rettv->vval.v_float = 0.0;
1739}
1740
1741/*
1742 * "atan()" function
1743 */
1744 static void
1745f_atan(typval_T *argvars, typval_T *rettv)
1746{
1747 float_T f = 0.0;
1748
1749 rettv->v_type = VAR_FLOAT;
1750 if (get_float_arg(argvars, &f) == OK)
1751 rettv->vval.v_float = atan(f);
1752 else
1753 rettv->vval.v_float = 0.0;
1754}
1755
1756/*
1757 * "atan2()" function
1758 */
1759 static void
1760f_atan2(typval_T *argvars, typval_T *rettv)
1761{
1762 float_T fx = 0.0, fy = 0.0;
1763
1764 rettv->v_type = VAR_FLOAT;
1765 if (get_float_arg(argvars, &fx) == OK
1766 && get_float_arg(&argvars[1], &fy) == OK)
1767 rettv->vval.v_float = atan2(fx, fy);
1768 else
1769 rettv->vval.v_float = 0.0;
1770}
1771#endif
1772
1773/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001774 * "balloon_show()" function
1775 */
1776#ifdef FEAT_BEVAL
1777 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001778f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1779{
1780 rettv->v_type = VAR_STRING;
1781 if (balloonEval != NULL)
1782 {
1783 if (balloonEval->msg == NULL)
1784 rettv->vval.v_string = NULL;
1785 else
1786 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1787 }
1788}
1789
1790 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001791f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1792{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001793 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001794 {
1795 if (argvars[0].v_type == VAR_LIST
1796# ifdef FEAT_GUI
1797 && !gui.in_use
1798# endif
1799 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001800 {
1801 list_T *l = argvars[0].vval.v_list;
1802
1803 // empty list removes the balloon
1804 post_balloon(balloonEval, NULL,
1805 l == NULL || l->lv_len == 0 ? NULL : l);
1806 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001807 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001808 {
1809 char_u *mesg = tv_get_string_chk(&argvars[0]);
1810
1811 if (mesg != NULL)
1812 // empty string removes the balloon
1813 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1814 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001815 }
1816}
1817
Bram Moolenaar669a8282017-11-19 20:13:05 +01001818# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001819 static void
1820f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1821{
1822 if (rettv_list_alloc(rettv) == OK)
1823 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001824 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001825
1826 if (msg != NULL)
1827 {
1828 pumitem_T *array;
1829 int size = split_message(msg, &array);
1830 int i;
1831
1832 /* Skip the first and last item, they are always empty. */
1833 for (i = 1; i < size - 1; ++i)
1834 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001835 while (size > 0)
1836 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001837 vim_free(array);
1838 }
1839 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001840}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001841# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001842#endif
1843
1844/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001845 * "browse(save, title, initdir, default)" function
1846 */
1847 static void
1848f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1849{
1850#ifdef FEAT_BROWSE
1851 int save;
1852 char_u *title;
1853 char_u *initdir;
1854 char_u *defname;
1855 char_u buf[NUMBUFLEN];
1856 char_u buf2[NUMBUFLEN];
1857 int error = FALSE;
1858
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001859 save = (int)tv_get_number_chk(&argvars[0], &error);
1860 title = tv_get_string_chk(&argvars[1]);
1861 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1862 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001863
1864 if (error || title == NULL || initdir == NULL || defname == NULL)
1865 rettv->vval.v_string = NULL;
1866 else
1867 rettv->vval.v_string =
1868 do_browse(save ? BROWSE_SAVE : 0,
1869 title, defname, NULL, initdir, NULL, curbuf);
1870#else
1871 rettv->vval.v_string = NULL;
1872#endif
1873 rettv->v_type = VAR_STRING;
1874}
1875
1876/*
1877 * "browsedir(title, initdir)" function
1878 */
1879 static void
1880f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1881{
1882#ifdef FEAT_BROWSE
1883 char_u *title;
1884 char_u *initdir;
1885 char_u buf[NUMBUFLEN];
1886
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001887 title = tv_get_string_chk(&argvars[0]);
1888 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001889
1890 if (title == NULL || initdir == NULL)
1891 rettv->vval.v_string = NULL;
1892 else
1893 rettv->vval.v_string = do_browse(BROWSE_DIR,
1894 title, NULL, NULL, initdir, NULL, curbuf);
1895#else
1896 rettv->vval.v_string = NULL;
1897#endif
1898 rettv->v_type = VAR_STRING;
1899}
1900
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001901/*
1902 * Find a buffer by number or exact name.
1903 */
1904 static buf_T *
1905find_buffer(typval_T *avar)
1906{
1907 buf_T *buf = NULL;
1908
1909 if (avar->v_type == VAR_NUMBER)
1910 buf = buflist_findnr((int)avar->vval.v_number);
1911 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1912 {
1913 buf = buflist_findname_exp(avar->vval.v_string);
1914 if (buf == NULL)
1915 {
1916 /* No full path name match, try a match with a URL or a "nofile"
1917 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001918 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001919 if (buf->b_fname != NULL
1920 && (path_with_url(buf->b_fname)
1921#ifdef FEAT_QUICKFIX
1922 || bt_nofile(buf)
1923#endif
1924 )
1925 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1926 break;
1927 }
1928 }
1929 return buf;
1930}
1931
1932/*
1933 * "bufexists(expr)" function
1934 */
1935 static void
1936f_bufexists(typval_T *argvars, typval_T *rettv)
1937{
1938 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1939}
1940
1941/*
1942 * "buflisted(expr)" function
1943 */
1944 static void
1945f_buflisted(typval_T *argvars, typval_T *rettv)
1946{
1947 buf_T *buf;
1948
1949 buf = find_buffer(&argvars[0]);
1950 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1951}
1952
1953/*
1954 * "bufloaded(expr)" function
1955 */
1956 static void
1957f_bufloaded(typval_T *argvars, typval_T *rettv)
1958{
1959 buf_T *buf;
1960
1961 buf = find_buffer(&argvars[0]);
1962 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1963}
1964
1965 buf_T *
1966buflist_find_by_name(char_u *name, int curtab_only)
1967{
1968 int save_magic;
1969 char_u *save_cpo;
1970 buf_T *buf;
1971
1972 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1973 save_magic = p_magic;
1974 p_magic = TRUE;
1975 save_cpo = p_cpo;
1976 p_cpo = (char_u *)"";
1977
1978 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1979 TRUE, FALSE, curtab_only));
1980
1981 p_magic = save_magic;
1982 p_cpo = save_cpo;
1983 return buf;
1984}
1985
1986/*
1987 * Get buffer by number or pattern.
1988 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001989 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001990tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001991{
1992 char_u *name = tv->vval.v_string;
1993 buf_T *buf;
1994
1995 if (tv->v_type == VAR_NUMBER)
1996 return buflist_findnr((int)tv->vval.v_number);
1997 if (tv->v_type != VAR_STRING)
1998 return NULL;
1999 if (name == NULL || *name == NUL)
2000 return curbuf;
2001 if (name[0] == '$' && name[1] == NUL)
2002 return lastbuf;
2003
2004 buf = buflist_find_by_name(name, curtab_only);
2005
2006 /* If not found, try expanding the name, like done for bufexists(). */
2007 if (buf == NULL)
2008 buf = find_buffer(tv);
2009
2010 return buf;
2011}
2012
2013/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002014 * Get the buffer from "arg" and give an error and return NULL if it is not
2015 * valid.
2016 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002017 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002018get_buf_arg(typval_T *arg)
2019{
2020 buf_T *buf;
2021
2022 ++emsg_off;
2023 buf = tv_get_buf(arg, FALSE);
2024 --emsg_off;
2025 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002026 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002027 return buf;
2028}
2029
2030/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002031 * "bufname(expr)" function
2032 */
2033 static void
2034f_bufname(typval_T *argvars, typval_T *rettv)
2035{
2036 buf_T *buf;
2037
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002038 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002039 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002040 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002041 rettv->v_type = VAR_STRING;
2042 if (buf != NULL && buf->b_fname != NULL)
2043 rettv->vval.v_string = vim_strsave(buf->b_fname);
2044 else
2045 rettv->vval.v_string = NULL;
2046 --emsg_off;
2047}
2048
2049/*
2050 * "bufnr(expr)" function
2051 */
2052 static void
2053f_bufnr(typval_T *argvars, typval_T *rettv)
2054{
2055 buf_T *buf;
2056 int error = FALSE;
2057 char_u *name;
2058
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002059 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002060 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002061 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002062 --emsg_off;
2063
2064 /* If the buffer isn't found and the second argument is not zero create a
2065 * new buffer. */
2066 if (buf == NULL
2067 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002068 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002069 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002070 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002071 && !error)
2072 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2073
2074 if (buf != NULL)
2075 rettv->vval.v_number = buf->b_fnum;
2076 else
2077 rettv->vval.v_number = -1;
2078}
2079
2080 static void
2081buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2082{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002083 win_T *wp;
2084 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002085 buf_T *buf;
2086
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002087 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002088 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002089 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002090 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002091 {
2092 ++winnr;
2093 if (wp->w_buffer == buf)
2094 break;
2095 }
2096 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002097 --emsg_off;
2098}
2099
2100/*
2101 * "bufwinid(nr)" function
2102 */
2103 static void
2104f_bufwinid(typval_T *argvars, typval_T *rettv)
2105{
2106 buf_win_common(argvars, rettv, FALSE);
2107}
2108
2109/*
2110 * "bufwinnr(nr)" function
2111 */
2112 static void
2113f_bufwinnr(typval_T *argvars, typval_T *rettv)
2114{
2115 buf_win_common(argvars, rettv, TRUE);
2116}
2117
2118/*
2119 * "byte2line(byte)" function
2120 */
2121 static void
2122f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2123{
2124#ifndef FEAT_BYTEOFF
2125 rettv->vval.v_number = -1;
2126#else
2127 long boff = 0;
2128
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002129 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002130 if (boff < 0)
2131 rettv->vval.v_number = -1;
2132 else
2133 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2134 (linenr_T)0, &boff);
2135#endif
2136}
2137
2138 static void
2139byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2140{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002141 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002142 char_u *str;
2143 varnumber_T idx;
2144
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002145 str = tv_get_string_chk(&argvars[0]);
2146 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002147 rettv->vval.v_number = -1;
2148 if (str == NULL || idx < 0)
2149 return;
2150
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002151 t = str;
2152 for ( ; idx > 0; idx--)
2153 {
2154 if (*t == NUL) /* EOL reached */
2155 return;
2156 if (enc_utf8 && comp)
2157 t += utf_ptr2len(t);
2158 else
2159 t += (*mb_ptr2len)(t);
2160 }
2161 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002162}
2163
2164/*
2165 * "byteidx()" function
2166 */
2167 static void
2168f_byteidx(typval_T *argvars, typval_T *rettv)
2169{
2170 byteidx(argvars, rettv, FALSE);
2171}
2172
2173/*
2174 * "byteidxcomp()" function
2175 */
2176 static void
2177f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2178{
2179 byteidx(argvars, rettv, TRUE);
2180}
2181
2182/*
2183 * "call(func, arglist [, dict])" function
2184 */
2185 static void
2186f_call(typval_T *argvars, typval_T *rettv)
2187{
2188 char_u *func;
2189 partial_T *partial = NULL;
2190 dict_T *selfdict = NULL;
2191
2192 if (argvars[1].v_type != VAR_LIST)
2193 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002194 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002195 return;
2196 }
2197 if (argvars[1].vval.v_list == NULL)
2198 return;
2199
2200 if (argvars[0].v_type == VAR_FUNC)
2201 func = argvars[0].vval.v_string;
2202 else if (argvars[0].v_type == VAR_PARTIAL)
2203 {
2204 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002205 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002206 }
2207 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002208 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002209 if (*func == NUL)
2210 return; /* type error or empty name */
2211
2212 if (argvars[2].v_type != VAR_UNKNOWN)
2213 {
2214 if (argvars[2].v_type != VAR_DICT)
2215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002216 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002217 return;
2218 }
2219 selfdict = argvars[2].vval.v_dict;
2220 }
2221
2222 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2223}
2224
2225#ifdef FEAT_FLOAT
2226/*
2227 * "ceil({float})" function
2228 */
2229 static void
2230f_ceil(typval_T *argvars, typval_T *rettv)
2231{
2232 float_T f = 0.0;
2233
2234 rettv->v_type = VAR_FLOAT;
2235 if (get_float_arg(argvars, &f) == OK)
2236 rettv->vval.v_float = ceil(f);
2237 else
2238 rettv->vval.v_float = 0.0;
2239}
2240#endif
2241
2242#ifdef FEAT_JOB_CHANNEL
2243/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002244 * "ch_canread()" function
2245 */
2246 static void
2247f_ch_canread(typval_T *argvars, typval_T *rettv)
2248{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002249 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002250
2251 rettv->vval.v_number = 0;
2252 if (channel != NULL)
2253 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2254 || channel_has_readahead(channel, PART_OUT)
2255 || channel_has_readahead(channel, PART_ERR);
2256}
2257
2258/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002259 * "ch_close()" function
2260 */
2261 static void
2262f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2263{
2264 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2265
2266 if (channel != NULL)
2267 {
2268 channel_close(channel, FALSE);
2269 channel_clear(channel);
2270 }
2271}
2272
2273/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002274 * "ch_close()" function
2275 */
2276 static void
2277f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2278{
2279 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2280
2281 if (channel != NULL)
2282 channel_close_in(channel);
2283}
2284
2285/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002286 * "ch_getbufnr()" function
2287 */
2288 static void
2289f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2290{
2291 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2292
2293 rettv->vval.v_number = -1;
2294 if (channel != NULL)
2295 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002296 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002297 int part;
2298
2299 if (STRCMP(what, "err") == 0)
2300 part = PART_ERR;
2301 else if (STRCMP(what, "out") == 0)
2302 part = PART_OUT;
2303 else if (STRCMP(what, "in") == 0)
2304 part = PART_IN;
2305 else
2306 part = PART_SOCK;
2307 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2308 rettv->vval.v_number =
2309 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2310 }
2311}
2312
2313/*
2314 * "ch_getjob()" function
2315 */
2316 static void
2317f_ch_getjob(typval_T *argvars, typval_T *rettv)
2318{
2319 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2320
2321 if (channel != NULL)
2322 {
2323 rettv->v_type = VAR_JOB;
2324 rettv->vval.v_job = channel->ch_job;
2325 if (channel->ch_job != NULL)
2326 ++channel->ch_job->jv_refcount;
2327 }
2328}
2329
2330/*
2331 * "ch_info()" function
2332 */
2333 static void
2334f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2335{
2336 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2337
2338 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2339 channel_info(channel, rettv->vval.v_dict);
2340}
2341
2342/*
2343 * "ch_log()" function
2344 */
2345 static void
2346f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2347{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002348 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002349 channel_T *channel = NULL;
2350
2351 if (argvars[1].v_type != VAR_UNKNOWN)
2352 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2353
Bram Moolenaard5359b22018-04-05 22:44:39 +02002354 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002355}
2356
2357/*
2358 * "ch_logfile()" function
2359 */
2360 static void
2361f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2362{
2363 char_u *fname;
2364 char_u *opt = (char_u *)"";
2365 char_u buf[NUMBUFLEN];
2366
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002367 /* Don't open a file in restricted mode. */
2368 if (check_restricted() || check_secure())
2369 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002370 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002371 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002372 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002373 ch_logfile(fname, opt);
2374}
2375
2376/*
2377 * "ch_open()" function
2378 */
2379 static void
2380f_ch_open(typval_T *argvars, typval_T *rettv)
2381{
2382 rettv->v_type = VAR_CHANNEL;
2383 if (check_restricted() || check_secure())
2384 return;
2385 rettv->vval.v_channel = channel_open_func(argvars);
2386}
2387
2388/*
2389 * "ch_read()" function
2390 */
2391 static void
2392f_ch_read(typval_T *argvars, typval_T *rettv)
2393{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002394 common_channel_read(argvars, rettv, FALSE, FALSE);
2395}
2396
2397/*
2398 * "ch_readblob()" function
2399 */
2400 static void
2401f_ch_readblob(typval_T *argvars, typval_T *rettv)
2402{
2403 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002404}
2405
2406/*
2407 * "ch_readraw()" function
2408 */
2409 static void
2410f_ch_readraw(typval_T *argvars, typval_T *rettv)
2411{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002412 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002413}
2414
2415/*
2416 * "ch_evalexpr()" function
2417 */
2418 static void
2419f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2420{
2421 ch_expr_common(argvars, rettv, TRUE);
2422}
2423
2424/*
2425 * "ch_sendexpr()" function
2426 */
2427 static void
2428f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2429{
2430 ch_expr_common(argvars, rettv, FALSE);
2431}
2432
2433/*
2434 * "ch_evalraw()" function
2435 */
2436 static void
2437f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2438{
2439 ch_raw_common(argvars, rettv, TRUE);
2440}
2441
2442/*
2443 * "ch_sendraw()" function
2444 */
2445 static void
2446f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2447{
2448 ch_raw_common(argvars, rettv, FALSE);
2449}
2450
2451/*
2452 * "ch_setoptions()" function
2453 */
2454 static void
2455f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2456{
2457 channel_T *channel;
2458 jobopt_T opt;
2459
2460 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2461 if (channel == NULL)
2462 return;
2463 clear_job_options(&opt);
2464 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002465 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002466 channel_set_options(channel, &opt);
2467 free_job_options(&opt);
2468}
2469
2470/*
2471 * "ch_status()" function
2472 */
2473 static void
2474f_ch_status(typval_T *argvars, typval_T *rettv)
2475{
2476 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002477 jobopt_T opt;
2478 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002479
2480 /* return an empty string by default */
2481 rettv->v_type = VAR_STRING;
2482 rettv->vval.v_string = NULL;
2483
2484 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002485
2486 if (argvars[1].v_type != VAR_UNKNOWN)
2487 {
2488 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002489 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002490 && (opt.jo_set & JO_PART))
2491 part = opt.jo_part;
2492 }
2493
2494 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002495}
2496#endif
2497
2498/*
2499 * "changenr()" function
2500 */
2501 static void
2502f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2503{
2504 rettv->vval.v_number = curbuf->b_u_seq_cur;
2505}
2506
2507/*
2508 * "char2nr(string)" function
2509 */
2510 static void
2511f_char2nr(typval_T *argvars, typval_T *rettv)
2512{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002513 if (has_mbyte)
2514 {
2515 int utf8 = 0;
2516
2517 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002518 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002519
2520 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002521 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002522 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002523 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002524 }
2525 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002526 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002527}
2528
2529/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002530 * "chdir(dir)" function
2531 */
2532 static void
2533f_chdir(typval_T *argvars, typval_T *rettv)
2534{
2535 char_u *cwd;
2536 cdscope_T scope = CDSCOPE_GLOBAL;
2537
2538 rettv->v_type = VAR_STRING;
2539 rettv->vval.v_string = NULL;
2540
2541 if (argvars[0].v_type != VAR_STRING)
2542 return;
2543
2544 // Return the current directory
2545 cwd = alloc(MAXPATHL);
2546 if (cwd != NULL)
2547 {
2548 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2549 {
2550#ifdef BACKSLASH_IN_FILENAME
2551 slash_adjust(cwd);
2552#endif
2553 rettv->vval.v_string = vim_strsave(cwd);
2554 }
2555 vim_free(cwd);
2556 }
2557
2558 if (curwin->w_localdir != NULL)
2559 scope = CDSCOPE_WINDOW;
2560 else if (curtab->tp_localdir != NULL)
2561 scope = CDSCOPE_TABPAGE;
2562
2563 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2564 // Directory change failed
2565 VIM_CLEAR(rettv->vval.v_string);
2566}
2567
2568/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002569 * "cindent(lnum)" function
2570 */
2571 static void
2572f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2573{
2574#ifdef FEAT_CINDENT
2575 pos_T pos;
2576 linenr_T lnum;
2577
2578 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002579 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002580 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2581 {
2582 curwin->w_cursor.lnum = lnum;
2583 rettv->vval.v_number = get_c_indent();
2584 curwin->w_cursor = pos;
2585 }
2586 else
2587#endif
2588 rettv->vval.v_number = -1;
2589}
2590
Bram Moolenaaraff74912019-03-30 18:11:49 +01002591 static win_T *
2592get_optional_window(typval_T *argvars, int idx)
2593{
2594 win_T *win = curwin;
2595
2596 if (argvars[idx].v_type != VAR_UNKNOWN)
2597 {
2598 win = find_win_by_nr_or_id(&argvars[idx]);
2599 if (win == NULL)
2600 {
2601 emsg(_(e_invalwindow));
2602 return NULL;
2603 }
2604 }
2605 return win;
2606}
2607
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002608/*
2609 * "clearmatches()" function
2610 */
2611 static void
2612f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2613{
2614#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002615 win_T *win = get_optional_window(argvars, 0);
2616
2617 if (win != NULL)
2618 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002619#endif
2620}
2621
2622/*
2623 * "col(string)" function
2624 */
2625 static void
2626f_col(typval_T *argvars, typval_T *rettv)
2627{
2628 colnr_T col = 0;
2629 pos_T *fp;
2630 int fnum = curbuf->b_fnum;
2631
2632 fp = var2fpos(&argvars[0], FALSE, &fnum);
2633 if (fp != NULL && fnum == curbuf->b_fnum)
2634 {
2635 if (fp->col == MAXCOL)
2636 {
2637 /* '> can be MAXCOL, get the length of the line then */
2638 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2639 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2640 else
2641 col = MAXCOL;
2642 }
2643 else
2644 {
2645 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002646 /* col(".") when the cursor is on the NUL at the end of the line
2647 * because of "coladd" can be seen as an extra column. */
2648 if (virtual_active() && fp == &curwin->w_cursor)
2649 {
2650 char_u *p = ml_get_cursor();
2651
2652 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2653 curwin->w_virtcol - curwin->w_cursor.coladd))
2654 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002655 int l;
2656
2657 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2658 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002659 }
2660 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002661 }
2662 }
2663 rettv->vval.v_number = col;
2664}
2665
2666#if defined(FEAT_INS_EXPAND)
2667/*
2668 * "complete()" function
2669 */
2670 static void
2671f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2672{
2673 int startcol;
2674
2675 if ((State & INSERT) == 0)
2676 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002677 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002678 return;
2679 }
2680
2681 /* Check for undo allowed here, because if something was already inserted
2682 * the line was already saved for undo and this check isn't done. */
2683 if (!undo_allowed())
2684 return;
2685
2686 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002688 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002689 return;
2690 }
2691
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002692 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002693 if (startcol <= 0)
2694 return;
2695
2696 set_completion(startcol - 1, argvars[1].vval.v_list);
2697}
2698
2699/*
2700 * "complete_add()" function
2701 */
2702 static void
2703f_complete_add(typval_T *argvars, typval_T *rettv)
2704{
2705 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2706}
2707
2708/*
2709 * "complete_check()" function
2710 */
2711 static void
2712f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2713{
2714 int saved = RedrawingDisabled;
2715
2716 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002717 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002718 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002719 RedrawingDisabled = saved;
2720}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002721
2722/*
2723 * "complete_info()" function
2724 */
2725 static void
2726f_complete_info(typval_T *argvars, typval_T *rettv)
2727{
2728 list_T *what_list = NULL;
2729
2730 if (rettv_dict_alloc(rettv) != OK)
2731 return;
2732
2733 if (argvars[0].v_type != VAR_UNKNOWN)
2734 {
2735 if (argvars[0].v_type != VAR_LIST)
2736 {
2737 emsg(_(e_listreq));
2738 return;
2739 }
2740 what_list = argvars[0].vval.v_list;
2741 }
2742 get_complete_info(what_list, rettv->vval.v_dict);
2743}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002744#endif
2745
2746/*
2747 * "confirm(message, buttons[, default [, type]])" function
2748 */
2749 static void
2750f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2751{
2752#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2753 char_u *message;
2754 char_u *buttons = NULL;
2755 char_u buf[NUMBUFLEN];
2756 char_u buf2[NUMBUFLEN];
2757 int def = 1;
2758 int type = VIM_GENERIC;
2759 char_u *typestr;
2760 int error = FALSE;
2761
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002762 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002763 if (message == NULL)
2764 error = TRUE;
2765 if (argvars[1].v_type != VAR_UNKNOWN)
2766 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002767 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002768 if (buttons == NULL)
2769 error = TRUE;
2770 if (argvars[2].v_type != VAR_UNKNOWN)
2771 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002772 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002773 if (argvars[3].v_type != VAR_UNKNOWN)
2774 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002775 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002776 if (typestr == NULL)
2777 error = TRUE;
2778 else
2779 {
2780 switch (TOUPPER_ASC(*typestr))
2781 {
2782 case 'E': type = VIM_ERROR; break;
2783 case 'Q': type = VIM_QUESTION; break;
2784 case 'I': type = VIM_INFO; break;
2785 case 'W': type = VIM_WARNING; break;
2786 case 'G': type = VIM_GENERIC; break;
2787 }
2788 }
2789 }
2790 }
2791 }
2792
2793 if (buttons == NULL || *buttons == NUL)
2794 buttons = (char_u *)_("&Ok");
2795
2796 if (!error)
2797 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2798 def, NULL, FALSE);
2799#endif
2800}
2801
2802/*
2803 * "copy()" function
2804 */
2805 static void
2806f_copy(typval_T *argvars, typval_T *rettv)
2807{
2808 item_copy(&argvars[0], rettv, FALSE, 0);
2809}
2810
2811#ifdef FEAT_FLOAT
2812/*
2813 * "cos()" function
2814 */
2815 static void
2816f_cos(typval_T *argvars, typval_T *rettv)
2817{
2818 float_T f = 0.0;
2819
2820 rettv->v_type = VAR_FLOAT;
2821 if (get_float_arg(argvars, &f) == OK)
2822 rettv->vval.v_float = cos(f);
2823 else
2824 rettv->vval.v_float = 0.0;
2825}
2826
2827/*
2828 * "cosh()" function
2829 */
2830 static void
2831f_cosh(typval_T *argvars, typval_T *rettv)
2832{
2833 float_T f = 0.0;
2834
2835 rettv->v_type = VAR_FLOAT;
2836 if (get_float_arg(argvars, &f) == OK)
2837 rettv->vval.v_float = cosh(f);
2838 else
2839 rettv->vval.v_float = 0.0;
2840}
2841#endif
2842
2843/*
2844 * "count()" function
2845 */
2846 static void
2847f_count(typval_T *argvars, typval_T *rettv)
2848{
2849 long n = 0;
2850 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002851 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002852
Bram Moolenaar9966b212017-07-28 16:46:57 +02002853 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002854 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002855
2856 if (argvars[0].v_type == VAR_STRING)
2857 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002858 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002859 char_u *p = argvars[0].vval.v_string;
2860 char_u *next;
2861
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002862 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002863 {
2864 if (ic)
2865 {
2866 size_t len = STRLEN(expr);
2867
2868 while (*p != NUL)
2869 {
2870 if (MB_STRNICMP(p, expr, len) == 0)
2871 {
2872 ++n;
2873 p += len;
2874 }
2875 else
2876 MB_PTR_ADV(p);
2877 }
2878 }
2879 else
2880 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2881 != NULL)
2882 {
2883 ++n;
2884 p = next + STRLEN(expr);
2885 }
2886 }
2887
2888 }
2889 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002890 {
2891 listitem_T *li;
2892 list_T *l;
2893 long idx;
2894
2895 if ((l = argvars[0].vval.v_list) != NULL)
2896 {
2897 li = l->lv_first;
2898 if (argvars[2].v_type != VAR_UNKNOWN)
2899 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002900 if (argvars[3].v_type != VAR_UNKNOWN)
2901 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002902 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002903 if (!error)
2904 {
2905 li = list_find(l, idx);
2906 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002907 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002908 }
2909 }
2910 if (error)
2911 li = NULL;
2912 }
2913
2914 for ( ; li != NULL; li = li->li_next)
2915 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2916 ++n;
2917 }
2918 }
2919 else if (argvars[0].v_type == VAR_DICT)
2920 {
2921 int todo;
2922 dict_T *d;
2923 hashitem_T *hi;
2924
2925 if ((d = argvars[0].vval.v_dict) != NULL)
2926 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002927 if (argvars[2].v_type != VAR_UNKNOWN)
2928 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002929 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002930 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002931 }
2932
2933 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2934 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2935 {
2936 if (!HASHITEM_EMPTY(hi))
2937 {
2938 --todo;
2939 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2940 ++n;
2941 }
2942 }
2943 }
2944 }
2945 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002946 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002947 rettv->vval.v_number = n;
2948}
2949
2950/*
2951 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2952 *
2953 * Checks the existence of a cscope connection.
2954 */
2955 static void
2956f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2957{
2958#ifdef FEAT_CSCOPE
2959 int num = 0;
2960 char_u *dbpath = NULL;
2961 char_u *prepend = NULL;
2962 char_u buf[NUMBUFLEN];
2963
2964 if (argvars[0].v_type != VAR_UNKNOWN
2965 && argvars[1].v_type != VAR_UNKNOWN)
2966 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002967 num = (int)tv_get_number(&argvars[0]);
2968 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002969 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002970 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 }
2972
2973 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2974#endif
2975}
2976
2977/*
2978 * "cursor(lnum, col)" function, or
2979 * "cursor(list)"
2980 *
2981 * Moves the cursor to the specified line and column.
2982 * Returns 0 when the position could be set, -1 otherwise.
2983 */
2984 static void
2985f_cursor(typval_T *argvars, typval_T *rettv)
2986{
2987 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002988 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002989 int set_curswant = TRUE;
2990
2991 rettv->vval.v_number = -1;
2992 if (argvars[1].v_type == VAR_UNKNOWN)
2993 {
2994 pos_T pos;
2995 colnr_T curswant = -1;
2996
2997 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2998 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002999 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003000 return;
3001 }
3002 line = pos.lnum;
3003 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003004 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003005 if (curswant >= 0)
3006 {
3007 curwin->w_curswant = curswant - 1;
3008 set_curswant = FALSE;
3009 }
3010 }
3011 else
3012 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003013 line = tv_get_lnum(argvars);
3014 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003015 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003016 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003017 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003018 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003019 return; /* type error; errmsg already given */
3020 if (line > 0)
3021 curwin->w_cursor.lnum = line;
3022 if (col > 0)
3023 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003024 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003025
3026 /* Make sure the cursor is in a valid position. */
3027 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003028 /* Correct cursor for multi-byte character. */
3029 if (has_mbyte)
3030 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031
3032 curwin->w_set_curswant = set_curswant;
3033 rettv->vval.v_number = 0;
3034}
3035
Bram Moolenaar4f974752019-02-17 17:44:42 +01003036#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003037/*
3038 * "debugbreak()" function
3039 */
3040 static void
3041f_debugbreak(typval_T *argvars, typval_T *rettv)
3042{
3043 int pid;
3044
3045 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003046 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003047 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003048 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003049 else
3050 {
3051 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3052
3053 if (hProcess != NULL)
3054 {
3055 DebugBreakProcess(hProcess);
3056 CloseHandle(hProcess);
3057 rettv->vval.v_number = OK;
3058 }
3059 }
3060}
3061#endif
3062
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003063/*
3064 * "deepcopy()" function
3065 */
3066 static void
3067f_deepcopy(typval_T *argvars, typval_T *rettv)
3068{
3069 int noref = 0;
3070 int copyID;
3071
3072 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003073 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003074 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003075 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003076 else
3077 {
3078 copyID = get_copyID();
3079 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3080 }
3081}
3082
3083/*
3084 * "delete()" function
3085 */
3086 static void
3087f_delete(typval_T *argvars, typval_T *rettv)
3088{
3089 char_u nbuf[NUMBUFLEN];
3090 char_u *name;
3091 char_u *flags;
3092
3093 rettv->vval.v_number = -1;
3094 if (check_restricted() || check_secure())
3095 return;
3096
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003097 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003098 if (name == NULL || *name == NUL)
3099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003100 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003101 return;
3102 }
3103
3104 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003105 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003106 else
3107 flags = (char_u *)"";
3108
3109 if (*flags == NUL)
3110 /* delete a file */
3111 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3112 else if (STRCMP(flags, "d") == 0)
3113 /* delete an empty directory */
3114 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3115 else if (STRCMP(flags, "rf") == 0)
3116 /* delete a directory recursively */
3117 rettv->vval.v_number = delete_recursive(name);
3118 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003119 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003120}
3121
3122/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003123 * "deletebufline()" function
3124 */
3125 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003126f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003127{
3128 buf_T *buf;
3129 linenr_T first, last;
3130 linenr_T lnum;
3131 long count;
3132 int is_curbuf;
3133 buf_T *curbuf_save = NULL;
3134 win_T *curwin_save = NULL;
3135 tabpage_T *tp;
3136 win_T *wp;
3137
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003138 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003139 if (buf == NULL)
3140 {
3141 rettv->vval.v_number = 1; /* FAIL */
3142 return;
3143 }
3144 is_curbuf = buf == curbuf;
3145
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003146 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003147 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003148 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003149 else
3150 last = first;
3151
3152 if (buf->b_ml.ml_mfp == NULL || first < 1
3153 || first > buf->b_ml.ml_line_count || last < first)
3154 {
3155 rettv->vval.v_number = 1; /* FAIL */
3156 return;
3157 }
3158
3159 if (!is_curbuf)
3160 {
3161 curbuf_save = curbuf;
3162 curwin_save = curwin;
3163 curbuf = buf;
3164 find_win_for_curbuf();
3165 }
3166 if (last > curbuf->b_ml.ml_line_count)
3167 last = curbuf->b_ml.ml_line_count;
3168 count = last - first + 1;
3169
3170 // When coming here from Insert mode, sync undo, so that this can be
3171 // undone separately from what was previously inserted.
3172 if (u_sync_once == 2)
3173 {
3174 u_sync_once = 1; // notify that u_sync() was called
3175 u_sync(TRUE);
3176 }
3177
3178 if (u_save(first - 1, last + 1) == FAIL)
3179 {
3180 rettv->vval.v_number = 1; /* FAIL */
3181 return;
3182 }
3183
3184 for (lnum = first; lnum <= last; ++lnum)
3185 ml_delete(first, TRUE);
3186
3187 FOR_ALL_TAB_WINDOWS(tp, wp)
3188 if (wp->w_buffer == buf)
3189 {
3190 if (wp->w_cursor.lnum > last)
3191 wp->w_cursor.lnum -= count;
3192 else if (wp->w_cursor.lnum> first)
3193 wp->w_cursor.lnum = first;
3194 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3195 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3196 }
3197 check_cursor_col();
3198 deleted_lines_mark(first, count);
3199
3200 if (!is_curbuf)
3201 {
3202 curbuf = curbuf_save;
3203 curwin = curwin_save;
3204 }
3205}
3206
3207/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003208 * "did_filetype()" function
3209 */
3210 static void
3211f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3212{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003213 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003214}
3215
3216/*
3217 * "diff_filler()" function
3218 */
3219 static void
3220f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3221{
3222#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003223 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003224#endif
3225}
3226
3227/*
3228 * "diff_hlID()" function
3229 */
3230 static void
3231f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3232{
3233#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003234 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003235 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003236 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003237 static int fnum = 0;
3238 static int change_start = 0;
3239 static int change_end = 0;
3240 static hlf_T hlID = (hlf_T)0;
3241 int filler_lines;
3242 int col;
3243
3244 if (lnum < 0) /* ignore type error in {lnum} arg */
3245 lnum = 0;
3246 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003247 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003248 || fnum != curbuf->b_fnum)
3249 {
3250 /* New line, buffer, change: need to get the values. */
3251 filler_lines = diff_check(curwin, lnum);
3252 if (filler_lines < 0)
3253 {
3254 if (filler_lines == -1)
3255 {
3256 change_start = MAXCOL;
3257 change_end = -1;
3258 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3259 hlID = HLF_ADD; /* added line */
3260 else
3261 hlID = HLF_CHD; /* changed line */
3262 }
3263 else
3264 hlID = HLF_ADD; /* added line */
3265 }
3266 else
3267 hlID = (hlf_T)0;
3268 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003269 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003270 fnum = curbuf->b_fnum;
3271 }
3272
3273 if (hlID == HLF_CHD || hlID == HLF_TXD)
3274 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003275 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003276 if (col >= change_start && col <= change_end)
3277 hlID = HLF_TXD; /* changed text */
3278 else
3279 hlID = HLF_CHD; /* changed line */
3280 }
3281 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3282#endif
3283}
3284
3285/*
3286 * "empty({expr})" function
3287 */
3288 static void
3289f_empty(typval_T *argvars, typval_T *rettv)
3290{
3291 int n = FALSE;
3292
3293 switch (argvars[0].v_type)
3294 {
3295 case VAR_STRING:
3296 case VAR_FUNC:
3297 n = argvars[0].vval.v_string == NULL
3298 || *argvars[0].vval.v_string == NUL;
3299 break;
3300 case VAR_PARTIAL:
3301 n = FALSE;
3302 break;
3303 case VAR_NUMBER:
3304 n = argvars[0].vval.v_number == 0;
3305 break;
3306 case VAR_FLOAT:
3307#ifdef FEAT_FLOAT
3308 n = argvars[0].vval.v_float == 0.0;
3309 break;
3310#endif
3311 case VAR_LIST:
3312 n = argvars[0].vval.v_list == NULL
3313 || argvars[0].vval.v_list->lv_first == NULL;
3314 break;
3315 case VAR_DICT:
3316 n = argvars[0].vval.v_dict == NULL
3317 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3318 break;
3319 case VAR_SPECIAL:
3320 n = argvars[0].vval.v_number != VVAL_TRUE;
3321 break;
3322
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003323 case VAR_BLOB:
3324 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003325 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3326 break;
3327
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003328 case VAR_JOB:
3329#ifdef FEAT_JOB_CHANNEL
3330 n = argvars[0].vval.v_job == NULL
3331 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3332 break;
3333#endif
3334 case VAR_CHANNEL:
3335#ifdef FEAT_JOB_CHANNEL
3336 n = argvars[0].vval.v_channel == NULL
3337 || !channel_is_open(argvars[0].vval.v_channel);
3338 break;
3339#endif
3340 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003341 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003342 n = TRUE;
3343 break;
3344 }
3345
3346 rettv->vval.v_number = n;
3347}
3348
3349/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003350 * "environ()" function
3351 */
3352 static void
3353f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3354{
3355#if !defined(AMIGA)
3356 int i = 0;
3357 char_u *entry, *value;
3358# ifdef MSWIN
3359 extern wchar_t **_wenviron;
3360# else
3361 extern char **environ;
3362# endif
3363
3364 if (rettv_dict_alloc(rettv) != OK)
3365 return;
3366
3367# ifdef MSWIN
3368 if (*_wenviron == NULL)
3369 return;
3370# else
3371 if (*environ == NULL)
3372 return;
3373# endif
3374
3375 for (i = 0; ; ++i)
3376 {
3377# ifdef MSWIN
3378 short_u *p;
3379
3380 if ((p = (short_u *)_wenviron[i]) == NULL)
3381 return;
3382 entry = utf16_to_enc(p, NULL);
3383# else
3384 if ((entry = (char_u *)environ[i]) == NULL)
3385 return;
3386 entry = vim_strsave(entry);
3387# endif
3388 if (entry == NULL) // out of memory
3389 return;
3390 if ((value = vim_strchr(entry, '=')) == NULL)
3391 {
3392 vim_free(entry);
3393 continue;
3394 }
3395 *value++ = NUL;
3396 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3397 vim_free(entry);
3398 }
3399#endif
3400}
3401
3402/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003403 * "escape({string}, {chars})" function
3404 */
3405 static void
3406f_escape(typval_T *argvars, typval_T *rettv)
3407{
3408 char_u buf[NUMBUFLEN];
3409
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003410 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3411 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003412 rettv->v_type = VAR_STRING;
3413}
3414
3415/*
3416 * "eval()" function
3417 */
3418 static void
3419f_eval(typval_T *argvars, typval_T *rettv)
3420{
3421 char_u *s, *p;
3422
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003423 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003424 if (s != NULL)
3425 s = skipwhite(s);
3426
3427 p = s;
3428 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3429 {
3430 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003431 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003432 need_clr_eos = FALSE;
3433 rettv->v_type = VAR_NUMBER;
3434 rettv->vval.v_number = 0;
3435 }
3436 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003437 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003438}
3439
3440/*
3441 * "eventhandler()" function
3442 */
3443 static void
3444f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3445{
3446 rettv->vval.v_number = vgetc_busy;
3447}
3448
3449/*
3450 * "executable()" function
3451 */
3452 static void
3453f_executable(typval_T *argvars, typval_T *rettv)
3454{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003455 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003456
3457 /* Check in $PATH and also check directly if there is a directory name. */
3458 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3459 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3460}
3461
3462static garray_T redir_execute_ga;
3463
3464/*
3465 * Append "value[value_len]" to the execute() output.
3466 */
3467 void
3468execute_redir_str(char_u *value, int value_len)
3469{
3470 int len;
3471
3472 if (value_len == -1)
3473 len = (int)STRLEN(value); /* Append the entire string */
3474 else
3475 len = value_len; /* Append only "value_len" characters */
3476 if (ga_grow(&redir_execute_ga, len) == OK)
3477 {
3478 mch_memmove((char *)redir_execute_ga.ga_data
3479 + redir_execute_ga.ga_len, value, len);
3480 redir_execute_ga.ga_len += len;
3481 }
3482}
3483
3484/*
3485 * Get next line from a list.
3486 * Called by do_cmdline() to get the next line.
3487 * Returns allocated string, or NULL for end of function.
3488 */
3489
3490 static char_u *
3491get_list_line(
3492 int c UNUSED,
3493 void *cookie,
3494 int indent UNUSED)
3495{
3496 listitem_T **p = (listitem_T **)cookie;
3497 listitem_T *item = *p;
3498 char_u buf[NUMBUFLEN];
3499 char_u *s;
3500
3501 if (item == NULL)
3502 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003503 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003504 *p = item->li_next;
3505 return s == NULL ? NULL : vim_strsave(s);
3506}
3507
3508/*
3509 * "execute()" function
3510 */
3511 static void
3512f_execute(typval_T *argvars, typval_T *rettv)
3513{
3514 char_u *cmd = NULL;
3515 list_T *list = NULL;
3516 int save_msg_silent = msg_silent;
3517 int save_emsg_silent = emsg_silent;
3518 int save_emsg_noredir = emsg_noredir;
3519 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003520 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003521 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003522 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003523 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003524
3525 rettv->vval.v_string = NULL;
3526 rettv->v_type = VAR_STRING;
3527
3528 if (argvars[0].v_type == VAR_LIST)
3529 {
3530 list = argvars[0].vval.v_list;
3531 if (list == NULL || list->lv_first == NULL)
3532 /* empty list, no commands, empty output */
3533 return;
3534 ++list->lv_refcount;
3535 }
3536 else
3537 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003538 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003539 if (cmd == NULL)
3540 return;
3541 }
3542
3543 if (argvars[1].v_type != VAR_UNKNOWN)
3544 {
3545 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003546 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547
3548 if (s == NULL)
3549 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003550 if (*s == NUL)
3551 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003552 if (STRNCMP(s, "silent", 6) == 0)
3553 ++msg_silent;
3554 if (STRCMP(s, "silent!") == 0)
3555 {
3556 emsg_silent = TRUE;
3557 emsg_noredir = TRUE;
3558 }
3559 }
3560 else
3561 ++msg_silent;
3562
3563 if (redir_execute)
3564 save_ga = redir_execute_ga;
3565 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3566 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003567 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003568 if (!echo_output)
3569 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003570
3571 if (cmd != NULL)
3572 do_cmdline_cmd(cmd);
3573 else
3574 {
3575 listitem_T *item = list->lv_first;
3576
3577 do_cmdline(NULL, get_list_line, (void *)&item,
3578 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3579 --list->lv_refcount;
3580 }
3581
Bram Moolenaard297f352017-01-29 20:31:21 +01003582 /* Need to append a NUL to the result. */
3583 if (ga_grow(&redir_execute_ga, 1) == OK)
3584 {
3585 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3586 rettv->vval.v_string = redir_execute_ga.ga_data;
3587 }
3588 else
3589 {
3590 ga_clear(&redir_execute_ga);
3591 rettv->vval.v_string = NULL;
3592 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003593 msg_silent = save_msg_silent;
3594 emsg_silent = save_emsg_silent;
3595 emsg_noredir = save_emsg_noredir;
3596
3597 redir_execute = save_redir_execute;
3598 if (redir_execute)
3599 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003600 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003601
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003602 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003603 if (echo_output)
3604 // When not working silently: put it in column zero. A following
3605 // "echon" will overwrite the message, unavoidably.
3606 msg_col = 0;
3607 else
3608 // When working silently: Put it back where it was, since nothing
3609 // should have been written.
3610 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003611}
3612
3613/*
3614 * "exepath()" function
3615 */
3616 static void
3617f_exepath(typval_T *argvars, typval_T *rettv)
3618{
3619 char_u *p = NULL;
3620
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003621 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003622 rettv->v_type = VAR_STRING;
3623 rettv->vval.v_string = p;
3624}
3625
3626/*
3627 * "exists()" function
3628 */
3629 static void
3630f_exists(typval_T *argvars, typval_T *rettv)
3631{
3632 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003633 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003634
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003635 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003636 if (*p == '$') /* environment variable */
3637 {
3638 /* first try "normal" environment variables (fast) */
3639 if (mch_getenv(p + 1) != NULL)
3640 n = TRUE;
3641 else
3642 {
3643 /* try expanding things like $VIM and ${HOME} */
3644 p = expand_env_save(p);
3645 if (p != NULL && *p != '$')
3646 n = TRUE;
3647 vim_free(p);
3648 }
3649 }
3650 else if (*p == '&' || *p == '+') /* option */
3651 {
3652 n = (get_option_tv(&p, NULL, TRUE) == OK);
3653 if (*skipwhite(p) != NUL)
3654 n = FALSE; /* trailing garbage */
3655 }
3656 else if (*p == '*') /* internal or user defined function */
3657 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003658 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003659 }
3660 else if (*p == ':')
3661 {
3662 n = cmd_exists(p + 1);
3663 }
3664 else if (*p == '#')
3665 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003666 if (p[1] == '#')
3667 n = autocmd_supported(p + 2);
3668 else
3669 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003670 }
3671 else /* internal variable */
3672 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003673 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003674 }
3675
3676 rettv->vval.v_number = n;
3677}
3678
3679#ifdef FEAT_FLOAT
3680/*
3681 * "exp()" function
3682 */
3683 static void
3684f_exp(typval_T *argvars, typval_T *rettv)
3685{
3686 float_T f = 0.0;
3687
3688 rettv->v_type = VAR_FLOAT;
3689 if (get_float_arg(argvars, &f) == OK)
3690 rettv->vval.v_float = exp(f);
3691 else
3692 rettv->vval.v_float = 0.0;
3693}
3694#endif
3695
3696/*
3697 * "expand()" function
3698 */
3699 static void
3700f_expand(typval_T *argvars, typval_T *rettv)
3701{
3702 char_u *s;
3703 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003704 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003705 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3706 expand_T xpc;
3707 int error = FALSE;
3708 char_u *result;
3709
3710 rettv->v_type = VAR_STRING;
3711 if (argvars[1].v_type != VAR_UNKNOWN
3712 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003713 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003714 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003715 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003716
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003717 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003718 if (*s == '%' || *s == '#' || *s == '<')
3719 {
3720 ++emsg_off;
3721 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3722 --emsg_off;
3723 if (rettv->v_type == VAR_LIST)
3724 {
3725 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3726 list_append_string(rettv->vval.v_list, result, -1);
3727 else
3728 vim_free(result);
3729 }
3730 else
3731 rettv->vval.v_string = result;
3732 }
3733 else
3734 {
3735 /* When the optional second argument is non-zero, don't remove matches
3736 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3737 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003738 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003739 options |= WILD_KEEP_ALL;
3740 if (!error)
3741 {
3742 ExpandInit(&xpc);
3743 xpc.xp_context = EXPAND_FILES;
3744 if (p_wic)
3745 options += WILD_ICASE;
3746 if (rettv->v_type == VAR_STRING)
3747 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3748 options, WILD_ALL);
3749 else if (rettv_list_alloc(rettv) != FAIL)
3750 {
3751 int i;
3752
3753 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3754 for (i = 0; i < xpc.xp_numfiles; i++)
3755 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3756 ExpandCleanup(&xpc);
3757 }
3758 }
3759 else
3760 rettv->vval.v_string = NULL;
3761 }
3762}
3763
3764/*
3765 * "extend(list, list [, idx])" function
3766 * "extend(dict, dict [, action])" function
3767 */
3768 static void
3769f_extend(typval_T *argvars, typval_T *rettv)
3770{
3771 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3772
3773 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3774 {
3775 list_T *l1, *l2;
3776 listitem_T *item;
3777 long before;
3778 int error = FALSE;
3779
3780 l1 = argvars[0].vval.v_list;
3781 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003782 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003783 && l2 != NULL)
3784 {
3785 if (argvars[2].v_type != VAR_UNKNOWN)
3786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003787 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003788 if (error)
3789 return; /* type error; errmsg already given */
3790
3791 if (before == l1->lv_len)
3792 item = NULL;
3793 else
3794 {
3795 item = list_find(l1, before);
3796 if (item == NULL)
3797 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003798 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003799 return;
3800 }
3801 }
3802 }
3803 else
3804 item = NULL;
3805 list_extend(l1, l2, item);
3806
3807 copy_tv(&argvars[0], rettv);
3808 }
3809 }
3810 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3811 {
3812 dict_T *d1, *d2;
3813 char_u *action;
3814 int i;
3815
3816 d1 = argvars[0].vval.v_dict;
3817 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003818 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003819 && d2 != NULL)
3820 {
3821 /* Check the third argument. */
3822 if (argvars[2].v_type != VAR_UNKNOWN)
3823 {
3824 static char *(av[]) = {"keep", "force", "error"};
3825
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003826 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003827 if (action == NULL)
3828 return; /* type error; errmsg already given */
3829 for (i = 0; i < 3; ++i)
3830 if (STRCMP(action, av[i]) == 0)
3831 break;
3832 if (i == 3)
3833 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003834 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003835 return;
3836 }
3837 }
3838 else
3839 action = (char_u *)"force";
3840
3841 dict_extend(d1, d2, action);
3842
3843 copy_tv(&argvars[0], rettv);
3844 }
3845 }
3846 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003847 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003848}
3849
3850/*
3851 * "feedkeys()" function
3852 */
3853 static void
3854f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3855{
3856 int remap = TRUE;
3857 int insert = FALSE;
3858 char_u *keys, *flags;
3859 char_u nbuf[NUMBUFLEN];
3860 int typed = FALSE;
3861 int execute = FALSE;
3862 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003863 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003864 char_u *keys_esc;
3865
3866 /* This is not allowed in the sandbox. If the commands would still be
3867 * executed in the sandbox it would be OK, but it probably happens later,
3868 * when "sandbox" is no longer set. */
3869 if (check_secure())
3870 return;
3871
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003872 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003873
3874 if (argvars[1].v_type != VAR_UNKNOWN)
3875 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003876 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003877 for ( ; *flags != NUL; ++flags)
3878 {
3879 switch (*flags)
3880 {
3881 case 'n': remap = FALSE; break;
3882 case 'm': remap = TRUE; break;
3883 case 't': typed = TRUE; break;
3884 case 'i': insert = TRUE; break;
3885 case 'x': execute = TRUE; break;
3886 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003887 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003888 }
3889 }
3890 }
3891
3892 if (*keys != NUL || execute)
3893 {
3894 /* Need to escape K_SPECIAL and CSI before putting the string in the
3895 * typeahead buffer. */
3896 keys_esc = vim_strsave_escape_csi(keys);
3897 if (keys_esc != NULL)
3898 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003899 if (lowlevel)
3900 {
3901#ifdef USE_INPUT_BUF
3902 add_to_input_buf(keys, (int)STRLEN(keys));
3903#else
3904 emsg(_("E980: lowlevel input not supported"));
3905#endif
3906 }
3907 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003908 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003909 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003910 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003911 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003912#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003913 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003914#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003915 )
3916 typebuf_was_filled = TRUE;
3917 }
3918 vim_free(keys_esc);
3919
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003920 if (execute)
3921 {
3922 int save_msg_scroll = msg_scroll;
3923
3924 /* Avoid a 1 second delay when the keys start Insert mode. */
3925 msg_scroll = FALSE;
3926
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003927 if (!dangerous)
3928 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003929 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003930 if (!dangerous)
3931 --ex_normal_busy;
3932
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003933 msg_scroll |= save_msg_scroll;
3934 }
3935 }
3936 }
3937}
3938
3939/*
3940 * "filereadable()" function
3941 */
3942 static void
3943f_filereadable(typval_T *argvars, typval_T *rettv)
3944{
3945 int fd;
3946 char_u *p;
3947 int n;
3948
3949#ifndef O_NONBLOCK
3950# define O_NONBLOCK 0
3951#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003952 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003953 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3954 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3955 {
3956 n = TRUE;
3957 close(fd);
3958 }
3959 else
3960 n = FALSE;
3961
3962 rettv->vval.v_number = n;
3963}
3964
3965/*
3966 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3967 * rights to write into.
3968 */
3969 static void
3970f_filewritable(typval_T *argvars, typval_T *rettv)
3971{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003972 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003973}
3974
3975 static void
3976findfilendir(
3977 typval_T *argvars UNUSED,
3978 typval_T *rettv,
3979 int find_what UNUSED)
3980{
3981#ifdef FEAT_SEARCHPATH
3982 char_u *fname;
3983 char_u *fresult = NULL;
3984 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3985 char_u *p;
3986 char_u pathbuf[NUMBUFLEN];
3987 int count = 1;
3988 int first = TRUE;
3989 int error = FALSE;
3990#endif
3991
3992 rettv->vval.v_string = NULL;
3993 rettv->v_type = VAR_STRING;
3994
3995#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003996 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003997
3998 if (argvars[1].v_type != VAR_UNKNOWN)
3999 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004000 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004001 if (p == NULL)
4002 error = TRUE;
4003 else
4004 {
4005 if (*p != NUL)
4006 path = p;
4007
4008 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004009 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004010 }
4011 }
4012
4013 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4014 error = TRUE;
4015
4016 if (*fname != NUL && !error)
4017 {
4018 do
4019 {
4020 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4021 vim_free(fresult);
4022 fresult = find_file_in_path_option(first ? fname : NULL,
4023 first ? (int)STRLEN(fname) : 0,
4024 0, first, path,
4025 find_what,
4026 curbuf->b_ffname,
4027 find_what == FINDFILE_DIR
4028 ? (char_u *)"" : curbuf->b_p_sua);
4029 first = FALSE;
4030
4031 if (fresult != NULL && rettv->v_type == VAR_LIST)
4032 list_append_string(rettv->vval.v_list, fresult, -1);
4033
4034 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4035 }
4036
4037 if (rettv->v_type == VAR_STRING)
4038 rettv->vval.v_string = fresult;
4039#endif
4040}
4041
4042/*
4043 * "filter()" function
4044 */
4045 static void
4046f_filter(typval_T *argvars, typval_T *rettv)
4047{
4048 filter_map(argvars, rettv, FALSE);
4049}
4050
4051/*
4052 * "finddir({fname}[, {path}[, {count}]])" function
4053 */
4054 static void
4055f_finddir(typval_T *argvars, typval_T *rettv)
4056{
4057 findfilendir(argvars, rettv, FINDFILE_DIR);
4058}
4059
4060/*
4061 * "findfile({fname}[, {path}[, {count}]])" function
4062 */
4063 static void
4064f_findfile(typval_T *argvars, typval_T *rettv)
4065{
4066 findfilendir(argvars, rettv, FINDFILE_FILE);
4067}
4068
4069#ifdef FEAT_FLOAT
4070/*
4071 * "float2nr({float})" function
4072 */
4073 static void
4074f_float2nr(typval_T *argvars, typval_T *rettv)
4075{
4076 float_T f = 0.0;
4077
4078 if (get_float_arg(argvars, &f) == OK)
4079 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004080 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004081 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004082 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004083 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004084 else
4085 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004086 }
4087}
4088
4089/*
4090 * "floor({float})" function
4091 */
4092 static void
4093f_floor(typval_T *argvars, typval_T *rettv)
4094{
4095 float_T f = 0.0;
4096
4097 rettv->v_type = VAR_FLOAT;
4098 if (get_float_arg(argvars, &f) == OK)
4099 rettv->vval.v_float = floor(f);
4100 else
4101 rettv->vval.v_float = 0.0;
4102}
4103
4104/*
4105 * "fmod()" function
4106 */
4107 static void
4108f_fmod(typval_T *argvars, typval_T *rettv)
4109{
4110 float_T fx = 0.0, fy = 0.0;
4111
4112 rettv->v_type = VAR_FLOAT;
4113 if (get_float_arg(argvars, &fx) == OK
4114 && get_float_arg(&argvars[1], &fy) == OK)
4115 rettv->vval.v_float = fmod(fx, fy);
4116 else
4117 rettv->vval.v_float = 0.0;
4118}
4119#endif
4120
4121/*
4122 * "fnameescape({string})" function
4123 */
4124 static void
4125f_fnameescape(typval_T *argvars, typval_T *rettv)
4126{
4127 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004128 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004129 rettv->v_type = VAR_STRING;
4130}
4131
4132/*
4133 * "fnamemodify({fname}, {mods})" function
4134 */
4135 static void
4136f_fnamemodify(typval_T *argvars, typval_T *rettv)
4137{
4138 char_u *fname;
4139 char_u *mods;
4140 int usedlen = 0;
4141 int len;
4142 char_u *fbuf = NULL;
4143 char_u buf[NUMBUFLEN];
4144
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004145 fname = tv_get_string_chk(&argvars[0]);
4146 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004147 if (fname == NULL || mods == NULL)
4148 fname = NULL;
4149 else
4150 {
4151 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004152 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004153 }
4154
4155 rettv->v_type = VAR_STRING;
4156 if (fname == NULL)
4157 rettv->vval.v_string = NULL;
4158 else
4159 rettv->vval.v_string = vim_strnsave(fname, len);
4160 vim_free(fbuf);
4161}
4162
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004163/*
4164 * "foldclosed()" function
4165 */
4166 static void
4167foldclosed_both(
4168 typval_T *argvars UNUSED,
4169 typval_T *rettv,
4170 int end UNUSED)
4171{
4172#ifdef FEAT_FOLDING
4173 linenr_T lnum;
4174 linenr_T first, last;
4175
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004176 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004177 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4178 {
4179 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4180 {
4181 if (end)
4182 rettv->vval.v_number = (varnumber_T)last;
4183 else
4184 rettv->vval.v_number = (varnumber_T)first;
4185 return;
4186 }
4187 }
4188#endif
4189 rettv->vval.v_number = -1;
4190}
4191
4192/*
4193 * "foldclosed()" function
4194 */
4195 static void
4196f_foldclosed(typval_T *argvars, typval_T *rettv)
4197{
4198 foldclosed_both(argvars, rettv, FALSE);
4199}
4200
4201/*
4202 * "foldclosedend()" function
4203 */
4204 static void
4205f_foldclosedend(typval_T *argvars, typval_T *rettv)
4206{
4207 foldclosed_both(argvars, rettv, TRUE);
4208}
4209
4210/*
4211 * "foldlevel()" function
4212 */
4213 static void
4214f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4215{
4216#ifdef FEAT_FOLDING
4217 linenr_T lnum;
4218
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004219 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004220 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4221 rettv->vval.v_number = foldLevel(lnum);
4222#endif
4223}
4224
4225/*
4226 * "foldtext()" function
4227 */
4228 static void
4229f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4230{
4231#ifdef FEAT_FOLDING
4232 linenr_T foldstart;
4233 linenr_T foldend;
4234 char_u *dashes;
4235 linenr_T lnum;
4236 char_u *s;
4237 char_u *r;
4238 int len;
4239 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004240 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004241#endif
4242
4243 rettv->v_type = VAR_STRING;
4244 rettv->vval.v_string = NULL;
4245#ifdef FEAT_FOLDING
4246 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4247 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4248 dashes = get_vim_var_str(VV_FOLDDASHES);
4249 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4250 && dashes != NULL)
4251 {
4252 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004253 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004254 if (!linewhite(lnum))
4255 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004256
4257 /* Find interesting text in this line. */
4258 s = skipwhite(ml_get(lnum));
4259 /* skip C comment-start */
4260 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4261 {
4262 s = skipwhite(s + 2);
4263 if (*skipwhite(s) == NUL
4264 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4265 {
4266 s = skipwhite(ml_get(lnum + 1));
4267 if (*s == '*')
4268 s = skipwhite(s + 1);
4269 }
4270 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004271 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004272 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004273 r = alloc((unsigned)(STRLEN(txt)
4274 + STRLEN(dashes) /* for %s */
4275 + 20 /* for %3ld */
4276 + STRLEN(s))); /* concatenated */
4277 if (r != NULL)
4278 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004279 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004280 len = (int)STRLEN(r);
4281 STRCAT(r, s);
4282 /* remove 'foldmarker' and 'commentstring' */
4283 foldtext_cleanup(r + len);
4284 rettv->vval.v_string = r;
4285 }
4286 }
4287#endif
4288}
4289
4290/*
4291 * "foldtextresult(lnum)" function
4292 */
4293 static void
4294f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4295{
4296#ifdef FEAT_FOLDING
4297 linenr_T lnum;
4298 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004299 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004300 foldinfo_T foldinfo;
4301 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004302 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004303#endif
4304
4305 rettv->v_type = VAR_STRING;
4306 rettv->vval.v_string = NULL;
4307#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004308 if (entered)
4309 return; /* reject recursive use */
4310 entered = TRUE;
4311
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004312 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004313 /* treat illegal types and illegal string values for {lnum} the same */
4314 if (lnum < 0)
4315 lnum = 0;
4316 fold_count = foldedCount(curwin, lnum, &foldinfo);
4317 if (fold_count > 0)
4318 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004319 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4320 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004321 if (text == buf)
4322 text = vim_strsave(text);
4323 rettv->vval.v_string = text;
4324 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004325
4326 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004327#endif
4328}
4329
4330/*
4331 * "foreground()" function
4332 */
4333 static void
4334f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4335{
4336#ifdef FEAT_GUI
4337 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004338 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004339 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004340 return;
4341 }
4342#endif
4343#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004344 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004345#endif
4346}
4347
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004348 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004349common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004350{
4351 char_u *s;
4352 char_u *name;
4353 int use_string = FALSE;
4354 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004355 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004356
4357 if (argvars[0].v_type == VAR_FUNC)
4358 {
4359 /* function(MyFunc, [arg], dict) */
4360 s = argvars[0].vval.v_string;
4361 }
4362 else if (argvars[0].v_type == VAR_PARTIAL
4363 && argvars[0].vval.v_partial != NULL)
4364 {
4365 /* function(dict.MyFunc, [arg]) */
4366 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004367 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004368 }
4369 else
4370 {
4371 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004372 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004373 use_string = TRUE;
4374 }
4375
Bram Moolenaar843b8842016-08-21 14:36:15 +02004376 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004377 {
4378 name = s;
4379 trans_name = trans_function_name(&name, FALSE,
4380 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4381 if (*name != NUL)
4382 s = NULL;
4383 }
4384
Bram Moolenaar843b8842016-08-21 14:36:15 +02004385 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4386 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004387 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004388 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004389 else if (trans_name != NULL && (is_funcref
4390 ? find_func(trans_name) == NULL
4391 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004392 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004393 else
4394 {
4395 int dict_idx = 0;
4396 int arg_idx = 0;
4397 list_T *list = NULL;
4398
4399 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4400 {
4401 char sid_buf[25];
4402 int off = *s == 's' ? 2 : 5;
4403
4404 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4405 * also be called from another script. Using trans_function_name()
4406 * would also work, but some plugins depend on the name being
4407 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004408 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004409 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4410 if (name != NULL)
4411 {
4412 STRCPY(name, sid_buf);
4413 STRCAT(name, s + off);
4414 }
4415 }
4416 else
4417 name = vim_strsave(s);
4418
4419 if (argvars[1].v_type != VAR_UNKNOWN)
4420 {
4421 if (argvars[2].v_type != VAR_UNKNOWN)
4422 {
4423 /* function(name, [args], dict) */
4424 arg_idx = 1;
4425 dict_idx = 2;
4426 }
4427 else if (argvars[1].v_type == VAR_DICT)
4428 /* function(name, dict) */
4429 dict_idx = 1;
4430 else
4431 /* function(name, [args]) */
4432 arg_idx = 1;
4433 if (dict_idx > 0)
4434 {
4435 if (argvars[dict_idx].v_type != VAR_DICT)
4436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004437 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004438 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004439 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004440 }
4441 if (argvars[dict_idx].vval.v_dict == NULL)
4442 dict_idx = 0;
4443 }
4444 if (arg_idx > 0)
4445 {
4446 if (argvars[arg_idx].v_type != VAR_LIST)
4447 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004448 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004449 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004450 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004451 }
4452 list = argvars[arg_idx].vval.v_list;
4453 if (list == NULL || list->lv_len == 0)
4454 arg_idx = 0;
4455 }
4456 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004457 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004458 {
4459 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4460
4461 /* result is a VAR_PARTIAL */
4462 if (pt == NULL)
4463 vim_free(name);
4464 else
4465 {
4466 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4467 {
4468 listitem_T *li;
4469 int i = 0;
4470 int arg_len = 0;
4471 int lv_len = 0;
4472
4473 if (arg_pt != NULL)
4474 arg_len = arg_pt->pt_argc;
4475 if (list != NULL)
4476 lv_len = list->lv_len;
4477 pt->pt_argc = arg_len + lv_len;
4478 pt->pt_argv = (typval_T *)alloc(
4479 sizeof(typval_T) * pt->pt_argc);
4480 if (pt->pt_argv == NULL)
4481 {
4482 vim_free(pt);
4483 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004484 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004485 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004486 for (i = 0; i < arg_len; i++)
4487 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4488 if (lv_len > 0)
4489 for (li = list->lv_first; li != NULL;
4490 li = li->li_next)
4491 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004492 }
4493
4494 /* For "function(dict.func, [], dict)" and "func" is a partial
4495 * use "dict". That is backwards compatible. */
4496 if (dict_idx > 0)
4497 {
4498 /* The dict is bound explicitly, pt_auto is FALSE. */
4499 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4500 ++pt->pt_dict->dv_refcount;
4501 }
4502 else if (arg_pt != NULL)
4503 {
4504 /* If the dict was bound automatically the result is also
4505 * bound automatically. */
4506 pt->pt_dict = arg_pt->pt_dict;
4507 pt->pt_auto = arg_pt->pt_auto;
4508 if (pt->pt_dict != NULL)
4509 ++pt->pt_dict->dv_refcount;
4510 }
4511
4512 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004513 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4514 {
4515 pt->pt_func = arg_pt->pt_func;
4516 func_ptr_ref(pt->pt_func);
4517 vim_free(name);
4518 }
4519 else if (is_funcref)
4520 {
4521 pt->pt_func = find_func(trans_name);
4522 func_ptr_ref(pt->pt_func);
4523 vim_free(name);
4524 }
4525 else
4526 {
4527 pt->pt_name = name;
4528 func_ref(name);
4529 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004530 }
4531 rettv->v_type = VAR_PARTIAL;
4532 rettv->vval.v_partial = pt;
4533 }
4534 else
4535 {
4536 /* result is a VAR_FUNC */
4537 rettv->v_type = VAR_FUNC;
4538 rettv->vval.v_string = name;
4539 func_ref(name);
4540 }
4541 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004542theend:
4543 vim_free(trans_name);
4544}
4545
4546/*
4547 * "funcref()" function
4548 */
4549 static void
4550f_funcref(typval_T *argvars, typval_T *rettv)
4551{
4552 common_function(argvars, rettv, TRUE);
4553}
4554
4555/*
4556 * "function()" function
4557 */
4558 static void
4559f_function(typval_T *argvars, typval_T *rettv)
4560{
4561 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004562}
4563
4564/*
4565 * "garbagecollect()" function
4566 */
4567 static void
4568f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4569{
4570 /* This is postponed until we are back at the toplevel, because we may be
4571 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4572 want_garbage_collect = TRUE;
4573
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004574 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004575 garbage_collect_at_exit = TRUE;
4576}
4577
4578/*
4579 * "get()" function
4580 */
4581 static void
4582f_get(typval_T *argvars, typval_T *rettv)
4583{
4584 listitem_T *li;
4585 list_T *l;
4586 dictitem_T *di;
4587 dict_T *d;
4588 typval_T *tv = NULL;
4589
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004590 if (argvars[0].v_type == VAR_BLOB)
4591 {
4592 int error = FALSE;
4593 int idx = tv_get_number_chk(&argvars[1], &error);
4594
4595 if (!error)
4596 {
4597 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004598 if (idx < 0)
4599 idx = blob_len(argvars[0].vval.v_blob) + idx;
4600 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4601 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004602 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004603 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004604 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004605 tv = rettv;
4606 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004607 }
4608 }
4609 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004610 {
4611 if ((l = argvars[0].vval.v_list) != NULL)
4612 {
4613 int error = FALSE;
4614
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004615 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004616 if (!error && li != NULL)
4617 tv = &li->li_tv;
4618 }
4619 }
4620 else if (argvars[0].v_type == VAR_DICT)
4621 {
4622 if ((d = argvars[0].vval.v_dict) != NULL)
4623 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004624 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004625 if (di != NULL)
4626 tv = &di->di_tv;
4627 }
4628 }
4629 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4630 {
4631 partial_T *pt;
4632 partial_T fref_pt;
4633
4634 if (argvars[0].v_type == VAR_PARTIAL)
4635 pt = argvars[0].vval.v_partial;
4636 else
4637 {
4638 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4639 fref_pt.pt_name = argvars[0].vval.v_string;
4640 pt = &fref_pt;
4641 }
4642
4643 if (pt != NULL)
4644 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004645 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004646 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004647
4648 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4649 {
4650 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004651 n = partial_name(pt);
4652 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004653 rettv->vval.v_string = NULL;
4654 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004655 {
4656 rettv->vval.v_string = vim_strsave(n);
4657 if (rettv->v_type == VAR_FUNC)
4658 func_ref(rettv->vval.v_string);
4659 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004660 }
4661 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004662 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004663 else if (STRCMP(what, "args") == 0)
4664 {
4665 rettv->v_type = VAR_LIST;
4666 if (rettv_list_alloc(rettv) == OK)
4667 {
4668 int i;
4669
4670 for (i = 0; i < pt->pt_argc; ++i)
4671 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4672 }
4673 }
4674 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004675 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004676 return;
4677 }
4678 }
4679 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004680 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004681
4682 if (tv == NULL)
4683 {
4684 if (argvars[2].v_type != VAR_UNKNOWN)
4685 copy_tv(&argvars[2], rettv);
4686 }
4687 else
4688 copy_tv(tv, rettv);
4689}
4690
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004691/*
4692 * Returns buffer options, variables and other attributes in a dictionary.
4693 */
4694 static dict_T *
4695get_buffer_info(buf_T *buf)
4696{
4697 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004698 tabpage_T *tp;
4699 win_T *wp;
4700 list_T *windows;
4701
4702 dict = dict_alloc();
4703 if (dict == NULL)
4704 return NULL;
4705
Bram Moolenaare0be1672018-07-08 16:50:37 +02004706 dict_add_number(dict, "bufnr", buf->b_fnum);
4707 dict_add_string(dict, "name", buf->b_ffname);
4708 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4709 : buflist_findlnum(buf));
4710 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4711 dict_add_number(dict, "listed", buf->b_p_bl);
4712 dict_add_number(dict, "changed", bufIsChanged(buf));
4713 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4714 dict_add_number(dict, "hidden",
4715 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004716
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004717 /* Get a reference to buffer variables */
4718 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004719
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004720 /* List of windows displaying this buffer */
4721 windows = list_alloc();
4722 if (windows != NULL)
4723 {
4724 FOR_ALL_TAB_WINDOWS(tp, wp)
4725 if (wp->w_buffer == buf)
4726 list_append_number(windows, (varnumber_T)wp->w_id);
4727 dict_add_list(dict, "windows", windows);
4728 }
4729
4730#ifdef FEAT_SIGNS
4731 if (buf->b_signlist != NULL)
4732 {
4733 /* List of signs placed in this buffer */
4734 list_T *signs = list_alloc();
4735 if (signs != NULL)
4736 {
4737 get_buffer_signs(buf, signs);
4738 dict_add_list(dict, "signs", signs);
4739 }
4740 }
4741#endif
4742
4743 return dict;
4744}
4745
4746/*
4747 * "getbufinfo()" function
4748 */
4749 static void
4750f_getbufinfo(typval_T *argvars, typval_T *rettv)
4751{
4752 buf_T *buf = NULL;
4753 buf_T *argbuf = NULL;
4754 dict_T *d;
4755 int filtered = FALSE;
4756 int sel_buflisted = FALSE;
4757 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004758 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004759
4760 if (rettv_list_alloc(rettv) != OK)
4761 return;
4762
4763 /* List of all the buffers or selected buffers */
4764 if (argvars[0].v_type == VAR_DICT)
4765 {
4766 dict_T *sel_d = argvars[0].vval.v_dict;
4767
4768 if (sel_d != NULL)
4769 {
4770 dictitem_T *di;
4771
4772 filtered = TRUE;
4773
4774 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004775 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004776 sel_buflisted = TRUE;
4777
4778 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004779 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004780 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004781
4782 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004783 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004784 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004785 }
4786 }
4787 else if (argvars[0].v_type != VAR_UNKNOWN)
4788 {
4789 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004790 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004791 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004792 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004793 --emsg_off;
4794 if (argbuf == NULL)
4795 return;
4796 }
4797
4798 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004799 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004800 {
4801 if (argbuf != NULL && argbuf != buf)
4802 continue;
4803 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004804 || (sel_buflisted && !buf->b_p_bl)
4805 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004806 continue;
4807
4808 d = get_buffer_info(buf);
4809 if (d != NULL)
4810 list_append_dict(rettv->vval.v_list, d);
4811 if (argbuf != NULL)
4812 return;
4813 }
4814}
4815
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004816/*
4817 * Get line or list of lines from buffer "buf" into "rettv".
4818 * Return a range (from start to end) of lines in rettv from the specified
4819 * buffer.
4820 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4821 */
4822 static void
4823get_buffer_lines(
4824 buf_T *buf,
4825 linenr_T start,
4826 linenr_T end,
4827 int retlist,
4828 typval_T *rettv)
4829{
4830 char_u *p;
4831
4832 rettv->v_type = VAR_STRING;
4833 rettv->vval.v_string = NULL;
4834 if (retlist && rettv_list_alloc(rettv) == FAIL)
4835 return;
4836
4837 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4838 return;
4839
4840 if (!retlist)
4841 {
4842 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4843 p = ml_get_buf(buf, start, FALSE);
4844 else
4845 p = (char_u *)"";
4846 rettv->vval.v_string = vim_strsave(p);
4847 }
4848 else
4849 {
4850 if (end < start)
4851 return;
4852
4853 if (start < 1)
4854 start = 1;
4855 if (end > buf->b_ml.ml_line_count)
4856 end = buf->b_ml.ml_line_count;
4857 while (start <= end)
4858 if (list_append_string(rettv->vval.v_list,
4859 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4860 break;
4861 }
4862}
4863
4864/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004865 * "getbufline()" function
4866 */
4867 static void
4868f_getbufline(typval_T *argvars, typval_T *rettv)
4869{
4870 linenr_T lnum;
4871 linenr_T end;
4872 buf_T *buf;
4873
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004874 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004875 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004876 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004877 --emsg_off;
4878
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004879 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004880 if (argvars[2].v_type == VAR_UNKNOWN)
4881 end = lnum;
4882 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004883 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004884
4885 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4886}
4887
4888/*
4889 * "getbufvar()" function
4890 */
4891 static void
4892f_getbufvar(typval_T *argvars, typval_T *rettv)
4893{
4894 buf_T *buf;
4895 buf_T *save_curbuf;
4896 char_u *varname;
4897 dictitem_T *v;
4898 int done = FALSE;
4899
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004900 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4901 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004902 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004903 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004904
4905 rettv->v_type = VAR_STRING;
4906 rettv->vval.v_string = NULL;
4907
4908 if (buf != NULL && varname != NULL)
4909 {
4910 /* set curbuf to be our buf, temporarily */
4911 save_curbuf = curbuf;
4912 curbuf = buf;
4913
Bram Moolenaar30567352016-08-27 21:25:44 +02004914 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004915 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004916 if (varname[1] == NUL)
4917 {
4918 /* get all buffer-local options in a dict */
4919 dict_T *opts = get_winbuf_options(TRUE);
4920
4921 if (opts != NULL)
4922 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004923 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004924 done = TRUE;
4925 }
4926 }
4927 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4928 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004929 done = TRUE;
4930 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004931 else
4932 {
4933 /* Look up the variable. */
4934 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4935 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4936 'b', varname, FALSE);
4937 if (v != NULL)
4938 {
4939 copy_tv(&v->di_tv, rettv);
4940 done = TRUE;
4941 }
4942 }
4943
4944 /* restore previous notion of curbuf */
4945 curbuf = save_curbuf;
4946 }
4947
4948 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4949 /* use the default value */
4950 copy_tv(&argvars[2], rettv);
4951
4952 --emsg_off;
4953}
4954
4955/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004956 * "getchangelist()" function
4957 */
4958 static void
4959f_getchangelist(typval_T *argvars, typval_T *rettv)
4960{
4961#ifdef FEAT_JUMPLIST
4962 buf_T *buf;
4963 int i;
4964 list_T *l;
4965 dict_T *d;
4966#endif
4967
4968 if (rettv_list_alloc(rettv) != OK)
4969 return;
4970
4971#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004972 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004973 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004974 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004975 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004976 if (buf == NULL)
4977 return;
4978
4979 l = list_alloc();
4980 if (l == NULL)
4981 return;
4982
4983 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4984 return;
4985 /*
4986 * The current window change list index tracks only the position in the
4987 * current buffer change list. For other buffers, use the change list
4988 * length as the current index.
4989 */
4990 list_append_number(rettv->vval.v_list,
4991 (varnumber_T)((buf == curwin->w_buffer)
4992 ? curwin->w_changelistidx : buf->b_changelistlen));
4993
4994 for (i = 0; i < buf->b_changelistlen; ++i)
4995 {
4996 if (buf->b_changelist[i].lnum == 0)
4997 continue;
4998 if ((d = dict_alloc()) == NULL)
4999 return;
5000 if (list_append_dict(l, d) == FAIL)
5001 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005002 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5003 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005004 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005005 }
5006#endif
5007}
5008/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005009 * "getchar()" function
5010 */
5011 static void
5012f_getchar(typval_T *argvars, typval_T *rettv)
5013{
5014 varnumber_T n;
5015 int error = FALSE;
5016
Bram Moolenaar84d93902018-09-11 20:10:20 +02005017#ifdef MESSAGE_QUEUE
5018 // vpeekc() used to check for messages, but that caused problems, invoking
5019 // a callback where it was not expected. Some plugins use getchar(1) in a
5020 // loop to await a message, therefore make sure we check for messages here.
5021 parse_queued_messages();
5022#endif
5023
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005024 /* Position the cursor. Needed after a message that ends in a space. */
5025 windgoto(msg_row, msg_col);
5026
5027 ++no_mapping;
5028 ++allow_keys;
5029 for (;;)
5030 {
5031 if (argvars[0].v_type == VAR_UNKNOWN)
5032 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005033 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005034 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005035 /* getchar(1): only check if char avail */
5036 n = vpeekc_any();
5037 else if (error || vpeekc_any() == NUL)
5038 /* illegal argument or getchar(0) and no char avail: return zero */
5039 n = 0;
5040 else
5041 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005042 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005043
5044 if (n == K_IGNORE)
5045 continue;
5046 break;
5047 }
5048 --no_mapping;
5049 --allow_keys;
5050
5051 set_vim_var_nr(VV_MOUSE_WIN, 0);
5052 set_vim_var_nr(VV_MOUSE_WINID, 0);
5053 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5054 set_vim_var_nr(VV_MOUSE_COL, 0);
5055
5056 rettv->vval.v_number = n;
5057 if (IS_SPECIAL(n) || mod_mask != 0)
5058 {
5059 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5060 int i = 0;
5061
5062 /* Turn a special key into three bytes, plus modifier. */
5063 if (mod_mask != 0)
5064 {
5065 temp[i++] = K_SPECIAL;
5066 temp[i++] = KS_MODIFIER;
5067 temp[i++] = mod_mask;
5068 }
5069 if (IS_SPECIAL(n))
5070 {
5071 temp[i++] = K_SPECIAL;
5072 temp[i++] = K_SECOND(n);
5073 temp[i++] = K_THIRD(n);
5074 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005075 else if (has_mbyte)
5076 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005077 else
5078 temp[i++] = n;
5079 temp[i++] = NUL;
5080 rettv->v_type = VAR_STRING;
5081 rettv->vval.v_string = vim_strsave(temp);
5082
5083#ifdef FEAT_MOUSE
5084 if (is_mouse_key(n))
5085 {
5086 int row = mouse_row;
5087 int col = mouse_col;
5088 win_T *win;
5089 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005090 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005091 int winnr = 1;
5092
5093 if (row >= 0 && col >= 0)
5094 {
5095 /* Find the window at the mouse coordinates and compute the
5096 * text position. */
5097 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005098 if (win == NULL)
5099 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005100 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005101 for (wp = firstwin; wp != win; wp = wp->w_next)
5102 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005103 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5104 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5105 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5106 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5107 }
5108 }
5109#endif
5110 }
5111}
5112
5113/*
5114 * "getcharmod()" function
5115 */
5116 static void
5117f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5118{
5119 rettv->vval.v_number = mod_mask;
5120}
5121
5122/*
5123 * "getcharsearch()" function
5124 */
5125 static void
5126f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5127{
5128 if (rettv_dict_alloc(rettv) != FAIL)
5129 {
5130 dict_T *dict = rettv->vval.v_dict;
5131
Bram Moolenaare0be1672018-07-08 16:50:37 +02005132 dict_add_string(dict, "char", last_csearch());
5133 dict_add_number(dict, "forward", last_csearch_forward());
5134 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005135 }
5136}
5137
5138/*
5139 * "getcmdline()" function
5140 */
5141 static void
5142f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5143{
5144 rettv->v_type = VAR_STRING;
5145 rettv->vval.v_string = get_cmdline_str();
5146}
5147
5148/*
5149 * "getcmdpos()" function
5150 */
5151 static void
5152f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5153{
5154 rettv->vval.v_number = get_cmdline_pos() + 1;
5155}
5156
5157/*
5158 * "getcmdtype()" function
5159 */
5160 static void
5161f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5162{
5163 rettv->v_type = VAR_STRING;
5164 rettv->vval.v_string = alloc(2);
5165 if (rettv->vval.v_string != NULL)
5166 {
5167 rettv->vval.v_string[0] = get_cmdline_type();
5168 rettv->vval.v_string[1] = NUL;
5169 }
5170}
5171
5172/*
5173 * "getcmdwintype()" function
5174 */
5175 static void
5176f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5177{
5178 rettv->v_type = VAR_STRING;
5179 rettv->vval.v_string = NULL;
5180#ifdef FEAT_CMDWIN
5181 rettv->vval.v_string = alloc(2);
5182 if (rettv->vval.v_string != NULL)
5183 {
5184 rettv->vval.v_string[0] = cmdwin_type;
5185 rettv->vval.v_string[1] = NUL;
5186 }
5187#endif
5188}
5189
5190#if defined(FEAT_CMDL_COMPL)
5191/*
5192 * "getcompletion()" function
5193 */
5194 static void
5195f_getcompletion(typval_T *argvars, typval_T *rettv)
5196{
5197 char_u *pat;
5198 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005199 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005200 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5201 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005202
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005203 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005204 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005205
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005206 if (p_wic)
5207 options |= WILD_ICASE;
5208
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005209 /* For filtered results, 'wildignore' is used */
5210 if (!filtered)
5211 options |= WILD_KEEP_ALL;
5212
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005213 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005214 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005215 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005216 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005217 if (xpc.xp_context == EXPAND_NOTHING)
5218 {
5219 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005220 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005221 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005222 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005223 return;
5224 }
5225
5226# if defined(FEAT_MENU)
5227 if (xpc.xp_context == EXPAND_MENUS)
5228 {
5229 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5230 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5231 }
5232# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005233#ifdef FEAT_CSCOPE
5234 if (xpc.xp_context == EXPAND_CSCOPE)
5235 {
5236 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5237 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5238 }
5239#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005240#ifdef FEAT_SIGNS
5241 if (xpc.xp_context == EXPAND_SIGN)
5242 {
5243 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5244 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5245 }
5246#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005247
5248 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5249 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5250 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005251 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005252
5253 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5254
5255 for (i = 0; i < xpc.xp_numfiles; i++)
5256 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5257 }
5258 vim_free(pat);
5259 ExpandCleanup(&xpc);
5260}
5261#endif
5262
5263/*
5264 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005265 *
5266 * Return the current working directory of a window in a tab page.
5267 * First optional argument 'winnr' is the window number or -1 and the second
5268 * optional argument 'tabnr' is the tab page number.
5269 *
5270 * If no arguments are supplied, then return the directory of the current
5271 * window.
5272 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5273 * the specified window.
5274 * If 'winnr' is 0 then return the directory of the current window.
5275 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5276 * directory of the specified tab page. Otherwise return the directory of the
5277 * specified window in the specified tab page.
5278 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005279 */
5280 static void
5281f_getcwd(typval_T *argvars, typval_T *rettv)
5282{
5283 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005284 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005285 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005286 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005287
5288 rettv->v_type = VAR_STRING;
5289 rettv->vval.v_string = NULL;
5290
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005291 if (argvars[0].v_type == VAR_NUMBER
5292 && argvars[0].vval.v_number == -1
5293 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005294 global = TRUE;
5295 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005296 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005297
5298 if (wp != NULL && wp->w_localdir != NULL)
5299 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005300 else if (tp != NULL && tp->tp_localdir != NULL)
5301 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5302 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005303 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005304 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005305 rettv->vval.v_string = vim_strsave(globaldir);
5306 else
5307 {
5308 cwd = alloc(MAXPATHL);
5309 if (cwd != NULL)
5310 {
5311 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5312 rettv->vval.v_string = vim_strsave(cwd);
5313 vim_free(cwd);
5314 }
5315 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005316 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005317#ifdef BACKSLASH_IN_FILENAME
5318 if (rettv->vval.v_string != NULL)
5319 slash_adjust(rettv->vval.v_string);
5320#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005321}
5322
5323/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005324 * "getenv()" function
5325 */
5326 static void
5327f_getenv(typval_T *argvars, typval_T *rettv)
5328{
5329 int mustfree = FALSE;
5330 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5331
5332 if (p == NULL)
5333 {
5334 rettv->v_type = VAR_SPECIAL;
5335 rettv->vval.v_number = VVAL_NULL;
5336 return;
5337 }
5338 if (!mustfree)
5339 p = vim_strsave(p);
5340 rettv->vval.v_string = p;
5341 rettv->v_type = VAR_STRING;
5342}
5343
5344/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005345 * "getfontname()" function
5346 */
5347 static void
5348f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5349{
5350 rettv->v_type = VAR_STRING;
5351 rettv->vval.v_string = NULL;
5352#ifdef FEAT_GUI
5353 if (gui.in_use)
5354 {
5355 GuiFont font;
5356 char_u *name = NULL;
5357
5358 if (argvars[0].v_type == VAR_UNKNOWN)
5359 {
5360 /* Get the "Normal" font. Either the name saved by
5361 * hl_set_font_name() or from the font ID. */
5362 font = gui.norm_font;
5363 name = hl_get_font_name();
5364 }
5365 else
5366 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005367 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005368 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5369 return;
5370 font = gui_mch_get_font(name, FALSE);
5371 if (font == NOFONT)
5372 return; /* Invalid font name, return empty string. */
5373 }
5374 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5375 if (argvars[0].v_type != VAR_UNKNOWN)
5376 gui_mch_free_font(font);
5377 }
5378#endif
5379}
5380
5381/*
5382 * "getfperm({fname})" function
5383 */
5384 static void
5385f_getfperm(typval_T *argvars, typval_T *rettv)
5386{
5387 char_u *fname;
5388 stat_T st;
5389 char_u *perm = NULL;
5390 char_u flags[] = "rwx";
5391 int i;
5392
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005393 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005394
5395 rettv->v_type = VAR_STRING;
5396 if (mch_stat((char *)fname, &st) >= 0)
5397 {
5398 perm = vim_strsave((char_u *)"---------");
5399 if (perm != NULL)
5400 {
5401 for (i = 0; i < 9; i++)
5402 {
5403 if (st.st_mode & (1 << (8 - i)))
5404 perm[i] = flags[i % 3];
5405 }
5406 }
5407 }
5408 rettv->vval.v_string = perm;
5409}
5410
5411/*
5412 * "getfsize({fname})" function
5413 */
5414 static void
5415f_getfsize(typval_T *argvars, typval_T *rettv)
5416{
5417 char_u *fname;
5418 stat_T st;
5419
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005420 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005421
5422 rettv->v_type = VAR_NUMBER;
5423
5424 if (mch_stat((char *)fname, &st) >= 0)
5425 {
5426 if (mch_isdir(fname))
5427 rettv->vval.v_number = 0;
5428 else
5429 {
5430 rettv->vval.v_number = (varnumber_T)st.st_size;
5431
5432 /* non-perfect check for overflow */
5433 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5434 rettv->vval.v_number = -2;
5435 }
5436 }
5437 else
5438 rettv->vval.v_number = -1;
5439}
5440
5441/*
5442 * "getftime({fname})" function
5443 */
5444 static void
5445f_getftime(typval_T *argvars, typval_T *rettv)
5446{
5447 char_u *fname;
5448 stat_T st;
5449
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005450 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005451
5452 if (mch_stat((char *)fname, &st) >= 0)
5453 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5454 else
5455 rettv->vval.v_number = -1;
5456}
5457
5458/*
5459 * "getftype({fname})" function
5460 */
5461 static void
5462f_getftype(typval_T *argvars, typval_T *rettv)
5463{
5464 char_u *fname;
5465 stat_T st;
5466 char_u *type = NULL;
5467 char *t;
5468
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005469 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005470
5471 rettv->v_type = VAR_STRING;
5472 if (mch_lstat((char *)fname, &st) >= 0)
5473 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005474 if (S_ISREG(st.st_mode))
5475 t = "file";
5476 else if (S_ISDIR(st.st_mode))
5477 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005478 else if (S_ISLNK(st.st_mode))
5479 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005480 else if (S_ISBLK(st.st_mode))
5481 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005482 else if (S_ISCHR(st.st_mode))
5483 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005484 else if (S_ISFIFO(st.st_mode))
5485 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005486 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005487 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005488 else
5489 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005490 type = vim_strsave((char_u *)t);
5491 }
5492 rettv->vval.v_string = type;
5493}
5494
5495/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005496 * "getjumplist()" function
5497 */
5498 static void
5499f_getjumplist(typval_T *argvars, typval_T *rettv)
5500{
5501#ifdef FEAT_JUMPLIST
5502 win_T *wp;
5503 int i;
5504 list_T *l;
5505 dict_T *d;
5506#endif
5507
5508 if (rettv_list_alloc(rettv) != OK)
5509 return;
5510
5511#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005512 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005513 if (wp == NULL)
5514 return;
5515
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005516 cleanup_jumplist(wp, TRUE);
5517
Bram Moolenaar4f505882018-02-10 21:06:32 +01005518 l = list_alloc();
5519 if (l == NULL)
5520 return;
5521
5522 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5523 return;
5524 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5525
5526 for (i = 0; i < wp->w_jumplistlen; ++i)
5527 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005528 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5529 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005530 if ((d = dict_alloc()) == NULL)
5531 return;
5532 if (list_append_dict(l, d) == FAIL)
5533 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005534 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5535 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005536 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005537 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005538 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005539 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005540 }
5541#endif
5542}
5543
5544/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005545 * "getline(lnum, [end])" function
5546 */
5547 static void
5548f_getline(typval_T *argvars, typval_T *rettv)
5549{
5550 linenr_T lnum;
5551 linenr_T end;
5552 int retlist;
5553
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005554 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005555 if (argvars[1].v_type == VAR_UNKNOWN)
5556 {
5557 end = 0;
5558 retlist = FALSE;
5559 }
5560 else
5561 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005562 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005563 retlist = TRUE;
5564 }
5565
5566 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5567}
5568
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005569#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005570 static void
5571get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5572{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005573 if (what_arg->v_type == VAR_UNKNOWN)
5574 {
5575 if (rettv_list_alloc(rettv) == OK)
5576 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005577 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005578 }
5579 else
5580 {
5581 if (rettv_dict_alloc(rettv) == OK)
5582 if (is_qf || (wp != NULL))
5583 {
5584 if (what_arg->v_type == VAR_DICT)
5585 {
5586 dict_T *d = what_arg->vval.v_dict;
5587
5588 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005589 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005590 }
5591 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005592 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005593 }
5594 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005595}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005596#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005597
5598/*
5599 * "getloclist()" function
5600 */
5601 static void
5602f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5603{
5604#ifdef FEAT_QUICKFIX
5605 win_T *wp;
5606
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005607 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005608 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5609#endif
5610}
5611
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005612/*
5613 * "getmatches()" function
5614 */
5615 static void
5616f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5617{
5618#ifdef FEAT_SEARCH_EXTRA
5619 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005620 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005621 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005622 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005623
Bram Moolenaaraff74912019-03-30 18:11:49 +01005624 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5625 return;
5626
5627 cur = win->w_match_head;
5628 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005629 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005630 dict = dict_alloc();
5631 if (dict == NULL)
5632 return;
5633 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005634 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005635 /* match added with matchaddpos() */
5636 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005637 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005638 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005639 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005640 list_T *l;
5641
5642 llpos = &cur->pos.pos[i];
5643 if (llpos->lnum == 0)
5644 break;
5645 l = list_alloc();
5646 if (l == NULL)
5647 break;
5648 list_append_number(l, (varnumber_T)llpos->lnum);
5649 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005650 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005651 list_append_number(l, (varnumber_T)llpos->col);
5652 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005653 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005654 sprintf(buf, "pos%d", i + 1);
5655 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005656 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005657 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005658 else
5659 {
5660 dict_add_string(dict, "pattern", cur->pattern);
5661 }
5662 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5663 dict_add_number(dict, "priority", (long)cur->priority);
5664 dict_add_number(dict, "id", (long)cur->id);
5665# if defined(FEAT_CONCEAL)
5666 if (cur->conceal_char)
5667 {
5668 char_u buf[MB_MAXBYTES + 1];
5669
5670 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5671 dict_add_string(dict, "conceal", (char_u *)&buf);
5672 }
5673# endif
5674 list_append_dict(rettv->vval.v_list, dict);
5675 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005676 }
5677#endif
5678}
5679
5680/*
5681 * "getpid()" function
5682 */
5683 static void
5684f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5685{
5686 rettv->vval.v_number = mch_get_pid();
5687}
5688
5689 static void
5690getpos_both(
5691 typval_T *argvars,
5692 typval_T *rettv,
5693 int getcurpos)
5694{
5695 pos_T *fp;
5696 list_T *l;
5697 int fnum = -1;
5698
5699 if (rettv_list_alloc(rettv) == OK)
5700 {
5701 l = rettv->vval.v_list;
5702 if (getcurpos)
5703 fp = &curwin->w_cursor;
5704 else
5705 fp = var2fpos(&argvars[0], TRUE, &fnum);
5706 if (fnum != -1)
5707 list_append_number(l, (varnumber_T)fnum);
5708 else
5709 list_append_number(l, (varnumber_T)0);
5710 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5711 : (varnumber_T)0);
5712 list_append_number(l, (fp != NULL)
5713 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5714 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005715 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005716 (varnumber_T)0);
5717 if (getcurpos)
5718 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005719 int save_set_curswant = curwin->w_set_curswant;
5720 colnr_T save_curswant = curwin->w_curswant;
5721 colnr_T save_virtcol = curwin->w_virtcol;
5722
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005723 update_curswant();
5724 list_append_number(l, curwin->w_curswant == MAXCOL ?
5725 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005726
5727 // Do not change "curswant", as it is unexpected that a get
5728 // function has a side effect.
5729 if (save_set_curswant)
5730 {
5731 curwin->w_set_curswant = save_set_curswant;
5732 curwin->w_curswant = save_curswant;
5733 curwin->w_virtcol = save_virtcol;
5734 curwin->w_valid &= ~VALID_VIRTCOL;
5735 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005736 }
5737 }
5738 else
5739 rettv->vval.v_number = FALSE;
5740}
5741
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005742/*
5743 * "getcurpos()" function
5744 */
5745 static void
5746f_getcurpos(typval_T *argvars, typval_T *rettv)
5747{
5748 getpos_both(argvars, rettv, TRUE);
5749}
5750
5751/*
5752 * "getpos(string)" function
5753 */
5754 static void
5755f_getpos(typval_T *argvars, typval_T *rettv)
5756{
5757 getpos_both(argvars, rettv, FALSE);
5758}
5759
5760/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005761 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005762 */
5763 static void
5764f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5765{
5766#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005767 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005768#endif
5769}
5770
5771/*
5772 * "getreg()" function
5773 */
5774 static void
5775f_getreg(typval_T *argvars, typval_T *rettv)
5776{
5777 char_u *strregname;
5778 int regname;
5779 int arg2 = FALSE;
5780 int return_list = FALSE;
5781 int error = FALSE;
5782
5783 if (argvars[0].v_type != VAR_UNKNOWN)
5784 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005785 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005786 error = strregname == NULL;
5787 if (argvars[1].v_type != VAR_UNKNOWN)
5788 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005789 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005790 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005791 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005792 }
5793 }
5794 else
5795 strregname = get_vim_var_str(VV_REG);
5796
5797 if (error)
5798 return;
5799
5800 regname = (strregname == NULL ? '"' : *strregname);
5801 if (regname == 0)
5802 regname = '"';
5803
5804 if (return_list)
5805 {
5806 rettv->v_type = VAR_LIST;
5807 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5808 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5809 if (rettv->vval.v_list == NULL)
5810 (void)rettv_list_alloc(rettv);
5811 else
5812 ++rettv->vval.v_list->lv_refcount;
5813 }
5814 else
5815 {
5816 rettv->v_type = VAR_STRING;
5817 rettv->vval.v_string = get_reg_contents(regname,
5818 arg2 ? GREG_EXPR_SRC : 0);
5819 }
5820}
5821
5822/*
5823 * "getregtype()" function
5824 */
5825 static void
5826f_getregtype(typval_T *argvars, typval_T *rettv)
5827{
5828 char_u *strregname;
5829 int regname;
5830 char_u buf[NUMBUFLEN + 2];
5831 long reglen = 0;
5832
5833 if (argvars[0].v_type != VAR_UNKNOWN)
5834 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005835 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005836 if (strregname == NULL) /* type error; errmsg already given */
5837 {
5838 rettv->v_type = VAR_STRING;
5839 rettv->vval.v_string = NULL;
5840 return;
5841 }
5842 }
5843 else
5844 /* Default to v:register */
5845 strregname = get_vim_var_str(VV_REG);
5846
5847 regname = (strregname == NULL ? '"' : *strregname);
5848 if (regname == 0)
5849 regname = '"';
5850
5851 buf[0] = NUL;
5852 buf[1] = NUL;
5853 switch (get_reg_type(regname, &reglen))
5854 {
5855 case MLINE: buf[0] = 'V'; break;
5856 case MCHAR: buf[0] = 'v'; break;
5857 case MBLOCK:
5858 buf[0] = Ctrl_V;
5859 sprintf((char *)buf + 1, "%ld", reglen + 1);
5860 break;
5861 }
5862 rettv->v_type = VAR_STRING;
5863 rettv->vval.v_string = vim_strsave(buf);
5864}
5865
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005866/*
5867 * Returns information (variables, options, etc.) about a tab page
5868 * as a dictionary.
5869 */
5870 static dict_T *
5871get_tabpage_info(tabpage_T *tp, int tp_idx)
5872{
5873 win_T *wp;
5874 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005875 list_T *l;
5876
5877 dict = dict_alloc();
5878 if (dict == NULL)
5879 return NULL;
5880
Bram Moolenaare0be1672018-07-08 16:50:37 +02005881 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005882
5883 l = list_alloc();
5884 if (l != NULL)
5885 {
5886 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5887 wp; wp = wp->w_next)
5888 list_append_number(l, (varnumber_T)wp->w_id);
5889 dict_add_list(dict, "windows", l);
5890 }
5891
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005892 /* Make a reference to tabpage variables */
5893 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005894
5895 return dict;
5896}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005897
5898/*
5899 * "gettabinfo()" function
5900 */
5901 static void
5902f_gettabinfo(typval_T *argvars, typval_T *rettv)
5903{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005904 tabpage_T *tp, *tparg = NULL;
5905 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005906 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005907
5908 if (rettv_list_alloc(rettv) != OK)
5909 return;
5910
5911 if (argvars[0].v_type != VAR_UNKNOWN)
5912 {
5913 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005914 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005915 if (tparg == NULL)
5916 return;
5917 }
5918
5919 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005920 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005921 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005922 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005923 if (tparg != NULL && tp != tparg)
5924 continue;
5925 d = get_tabpage_info(tp, tpnr);
5926 if (d != NULL)
5927 list_append_dict(rettv->vval.v_list, d);
5928 if (tparg != NULL)
5929 return;
5930 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005931}
5932
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005933/*
5934 * "gettabvar()" function
5935 */
5936 static void
5937f_gettabvar(typval_T *argvars, typval_T *rettv)
5938{
5939 win_T *oldcurwin;
5940 tabpage_T *tp, *oldtabpage;
5941 dictitem_T *v;
5942 char_u *varname;
5943 int done = FALSE;
5944
5945 rettv->v_type = VAR_STRING;
5946 rettv->vval.v_string = NULL;
5947
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005948 varname = tv_get_string_chk(&argvars[1]);
5949 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005950 if (tp != NULL && varname != NULL)
5951 {
5952 /* Set tp to be our tabpage, temporarily. Also set the window to the
5953 * first window in the tabpage, otherwise the window is not valid. */
5954 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005955 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5956 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005957 {
5958 /* look up the variable */
5959 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5960 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5961 if (v != NULL)
5962 {
5963 copy_tv(&v->di_tv, rettv);
5964 done = TRUE;
5965 }
5966 }
5967
5968 /* restore previous notion of curwin */
5969 restore_win(oldcurwin, oldtabpage, TRUE);
5970 }
5971
5972 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5973 copy_tv(&argvars[2], rettv);
5974}
5975
5976/*
5977 * "gettabwinvar()" function
5978 */
5979 static void
5980f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5981{
5982 getwinvar(argvars, rettv, 1);
5983}
5984
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005985/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005986 * "gettagstack()" function
5987 */
5988 static void
5989f_gettagstack(typval_T *argvars, typval_T *rettv)
5990{
5991 win_T *wp = curwin; // default is current window
5992
5993 if (rettv_dict_alloc(rettv) != OK)
5994 return;
5995
5996 if (argvars[0].v_type != VAR_UNKNOWN)
5997 {
5998 wp = find_win_by_nr_or_id(&argvars[0]);
5999 if (wp == NULL)
6000 return;
6001 }
6002
6003 get_tagstack(wp, rettv->vval.v_dict);
6004}
6005
6006/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006007 * Returns information about a window as a dictionary.
6008 */
6009 static dict_T *
6010get_win_info(win_T *wp, short tpnr, short winnr)
6011{
6012 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006013
6014 dict = dict_alloc();
6015 if (dict == NULL)
6016 return NULL;
6017
Bram Moolenaare0be1672018-07-08 16:50:37 +02006018 dict_add_number(dict, "tabnr", tpnr);
6019 dict_add_number(dict, "winnr", winnr);
6020 dict_add_number(dict, "winid", wp->w_id);
6021 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006022 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006023 dict_add_number(dict, "topline", wp->w_topline);
6024 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006025#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006026 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006027#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006028 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006029 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006030 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006031
Bram Moolenaar69905d12017-08-13 18:14:47 +02006032#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006033 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006034#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006035#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006036 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6037 dict_add_number(dict, "loclist",
6038 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006039#endif
6040
Bram Moolenaar30567352016-08-27 21:25:44 +02006041 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006042 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006043
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006044 return dict;
6045}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006046
6047/*
6048 * "getwininfo()" function
6049 */
6050 static void
6051f_getwininfo(typval_T *argvars, typval_T *rettv)
6052{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006053 tabpage_T *tp;
6054 win_T *wp = NULL, *wparg = NULL;
6055 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006056 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006057
6058 if (rettv_list_alloc(rettv) != OK)
6059 return;
6060
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006061 if (argvars[0].v_type != VAR_UNKNOWN)
6062 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006063 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006064 if (wparg == NULL)
6065 return;
6066 }
6067
6068 /* Collect information about either all the windows across all the tab
6069 * pages or one particular window.
6070 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006071 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006072 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006073 tabnr++;
6074 winnr = 0;
6075 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006076 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006077 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006078 if (wparg != NULL && wp != wparg)
6079 continue;
6080 d = get_win_info(wp, tabnr, winnr);
6081 if (d != NULL)
6082 list_append_dict(rettv->vval.v_list, d);
6083 if (wparg != NULL)
6084 /* found information about a specific window */
6085 return;
6086 }
6087 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006088}
6089
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006090/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006091 * "win_findbuf()" function
6092 */
6093 static void
6094f_win_findbuf(typval_T *argvars, typval_T *rettv)
6095{
6096 if (rettv_list_alloc(rettv) != FAIL)
6097 win_findbuf(argvars, rettv->vval.v_list);
6098}
6099
6100/*
6101 * "win_getid()" function
6102 */
6103 static void
6104f_win_getid(typval_T *argvars, typval_T *rettv)
6105{
6106 rettv->vval.v_number = win_getid(argvars);
6107}
6108
6109/*
6110 * "win_gotoid()" function
6111 */
6112 static void
6113f_win_gotoid(typval_T *argvars, typval_T *rettv)
6114{
6115 rettv->vval.v_number = win_gotoid(argvars);
6116}
6117
6118/*
6119 * "win_id2tabwin()" function
6120 */
6121 static void
6122f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6123{
6124 if (rettv_list_alloc(rettv) != FAIL)
6125 win_id2tabwin(argvars, rettv->vval.v_list);
6126}
6127
6128/*
6129 * "win_id2win()" function
6130 */
6131 static void
6132f_win_id2win(typval_T *argvars, typval_T *rettv)
6133{
6134 rettv->vval.v_number = win_id2win(argvars);
6135}
6136
6137/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006138 * "win_screenpos()" function
6139 */
6140 static void
6141f_win_screenpos(typval_T *argvars, typval_T *rettv)
6142{
6143 win_T *wp;
6144
6145 if (rettv_list_alloc(rettv) == FAIL)
6146 return;
6147
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006148 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006149 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6150 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6151}
6152
6153/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006154 * "getwinpos({timeout})" function
6155 */
6156 static void
6157f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6158{
6159 int x = -1;
6160 int y = -1;
6161
6162 if (rettv_list_alloc(rettv) == FAIL)
6163 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006164#if defined(FEAT_GUI) \
6165 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6166 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006167 {
6168 varnumber_T timeout = 100;
6169
6170 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006171 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006172
6173 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006174 }
6175#endif
6176 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6177 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6178}
6179
6180
6181/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006182 * "getwinposx()" function
6183 */
6184 static void
6185f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6186{
6187 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006188#if defined(FEAT_GUI) \
6189 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6190 || defined(MSWIN)
6191
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006192 {
6193 int x, y;
6194
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006195 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006196 rettv->vval.v_number = x;
6197 }
6198#endif
6199}
6200
6201/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006202 * "getwinposy()" function
6203 */
6204 static void
6205f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6206{
6207 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006208#if defined(FEAT_GUI) \
6209 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6210 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006211 {
6212 int x, y;
6213
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006214 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006215 rettv->vval.v_number = y;
6216 }
6217#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006218}
6219
6220/*
6221 * "getwinvar()" function
6222 */
6223 static void
6224f_getwinvar(typval_T *argvars, typval_T *rettv)
6225{
6226 getwinvar(argvars, rettv, 0);
6227}
6228
6229/*
6230 * "glob()" function
6231 */
6232 static void
6233f_glob(typval_T *argvars, typval_T *rettv)
6234{
6235 int options = WILD_SILENT|WILD_USE_NL;
6236 expand_T xpc;
6237 int error = FALSE;
6238
6239 /* When the optional second argument is non-zero, don't remove matches
6240 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6241 rettv->v_type = VAR_STRING;
6242 if (argvars[1].v_type != VAR_UNKNOWN)
6243 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006244 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006245 options |= WILD_KEEP_ALL;
6246 if (argvars[2].v_type != VAR_UNKNOWN)
6247 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006248 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006249 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006250 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006251 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006252 options |= WILD_ALLLINKS;
6253 }
6254 }
6255 if (!error)
6256 {
6257 ExpandInit(&xpc);
6258 xpc.xp_context = EXPAND_FILES;
6259 if (p_wic)
6260 options += WILD_ICASE;
6261 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006262 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006263 NULL, options, WILD_ALL);
6264 else if (rettv_list_alloc(rettv) != FAIL)
6265 {
6266 int i;
6267
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006268 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006269 NULL, options, WILD_ALL_KEEP);
6270 for (i = 0; i < xpc.xp_numfiles; i++)
6271 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6272
6273 ExpandCleanup(&xpc);
6274 }
6275 }
6276 else
6277 rettv->vval.v_string = NULL;
6278}
6279
6280/*
6281 * "globpath()" function
6282 */
6283 static void
6284f_globpath(typval_T *argvars, typval_T *rettv)
6285{
6286 int flags = 0;
6287 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006288 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006289 int error = FALSE;
6290 garray_T ga;
6291 int i;
6292
6293 /* When the optional second argument is non-zero, don't remove matches
6294 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6295 rettv->v_type = VAR_STRING;
6296 if (argvars[2].v_type != VAR_UNKNOWN)
6297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006298 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006299 flags |= WILD_KEEP_ALL;
6300 if (argvars[3].v_type != VAR_UNKNOWN)
6301 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006302 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006303 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006304 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006305 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006306 flags |= WILD_ALLLINKS;
6307 }
6308 }
6309 if (file != NULL && !error)
6310 {
6311 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006312 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006313 if (rettv->v_type == VAR_STRING)
6314 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6315 else if (rettv_list_alloc(rettv) != FAIL)
6316 for (i = 0; i < ga.ga_len; ++i)
6317 list_append_string(rettv->vval.v_list,
6318 ((char_u **)(ga.ga_data))[i], -1);
6319 ga_clear_strings(&ga);
6320 }
6321 else
6322 rettv->vval.v_string = NULL;
6323}
6324
6325/*
6326 * "glob2regpat()" function
6327 */
6328 static void
6329f_glob2regpat(typval_T *argvars, typval_T *rettv)
6330{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006331 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006332
6333 rettv->v_type = VAR_STRING;
6334 rettv->vval.v_string = (pat == NULL)
6335 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6336}
6337
6338/* for VIM_VERSION_ defines */
6339#include "version.h"
6340
6341/*
6342 * "has()" function
6343 */
6344 static void
6345f_has(typval_T *argvars, typval_T *rettv)
6346{
6347 int i;
6348 char_u *name;
6349 int n = FALSE;
6350 static char *(has_list[]) =
6351 {
6352#ifdef AMIGA
6353 "amiga",
6354# ifdef FEAT_ARP
6355 "arp",
6356# endif
6357#endif
6358#ifdef __BEOS__
6359 "beos",
6360#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006361#if defined(BSD) && !defined(MACOS_X)
6362 "bsd",
6363#endif
6364#ifdef hpux
6365 "hpux",
6366#endif
6367#ifdef __linux__
6368 "linux",
6369#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006370#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006371 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6372 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006373# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006374 "macunix", /* Mac OS X, with the darwin feature */
6375 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006376# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006377#endif
6378#ifdef __QNX__
6379 "qnx",
6380#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006381#ifdef SUN_SYSTEM
6382 "sun",
6383#else
6384 "moon",
6385#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006386#ifdef UNIX
6387 "unix",
6388#endif
6389#ifdef VMS
6390 "vms",
6391#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006392#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006393 "win32",
6394#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006395#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006396 "win32unix",
6397#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006398#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006399 "win64",
6400#endif
6401#ifdef EBCDIC
6402 "ebcdic",
6403#endif
6404#ifndef CASE_INSENSITIVE_FILENAME
6405 "fname_case",
6406#endif
6407#ifdef HAVE_ACL
6408 "acl",
6409#endif
6410#ifdef FEAT_ARABIC
6411 "arabic",
6412#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006413 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006414#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006415 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006416#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006417#ifdef FEAT_AUTOSERVERNAME
6418 "autoservername",
6419#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006420#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006421 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006422# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006423 "balloon_multiline",
6424# endif
6425#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006426#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006427 "balloon_eval_term",
6428#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006429#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6430 "builtin_terms",
6431# ifdef ALL_BUILTIN_TCAPS
6432 "all_builtin_terms",
6433# endif
6434#endif
6435#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006436 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006437 || defined(FEAT_GUI_MOTIF))
6438 "browsefilter",
6439#endif
6440#ifdef FEAT_BYTEOFF
6441 "byte_offset",
6442#endif
6443#ifdef FEAT_JOB_CHANNEL
6444 "channel",
6445#endif
6446#ifdef FEAT_CINDENT
6447 "cindent",
6448#endif
6449#ifdef FEAT_CLIENTSERVER
6450 "clientserver",
6451#endif
6452#ifdef FEAT_CLIPBOARD
6453 "clipboard",
6454#endif
6455#ifdef FEAT_CMDL_COMPL
6456 "cmdline_compl",
6457#endif
6458#ifdef FEAT_CMDHIST
6459 "cmdline_hist",
6460#endif
6461#ifdef FEAT_COMMENTS
6462 "comments",
6463#endif
6464#ifdef FEAT_CONCEAL
6465 "conceal",
6466#endif
6467#ifdef FEAT_CRYPT
6468 "cryptv",
6469 "crypt-blowfish",
6470 "crypt-blowfish2",
6471#endif
6472#ifdef FEAT_CSCOPE
6473 "cscope",
6474#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006475 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006476#ifdef CURSOR_SHAPE
6477 "cursorshape",
6478#endif
6479#ifdef DEBUG
6480 "debug",
6481#endif
6482#ifdef FEAT_CON_DIALOG
6483 "dialog_con",
6484#endif
6485#ifdef FEAT_GUI_DIALOG
6486 "dialog_gui",
6487#endif
6488#ifdef FEAT_DIFF
6489 "diff",
6490#endif
6491#ifdef FEAT_DIGRAPHS
6492 "digraphs",
6493#endif
6494#ifdef FEAT_DIRECTX
6495 "directx",
6496#endif
6497#ifdef FEAT_DND
6498 "dnd",
6499#endif
6500#ifdef FEAT_EMACS_TAGS
6501 "emacs_tags",
6502#endif
6503 "eval", /* always present, of course! */
6504 "ex_extra", /* graduated feature */
6505#ifdef FEAT_SEARCH_EXTRA
6506 "extra_search",
6507#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006508#ifdef FEAT_SEARCHPATH
6509 "file_in_path",
6510#endif
6511#ifdef FEAT_FILTERPIPE
6512 "filterpipe",
6513#endif
6514#ifdef FEAT_FIND_ID
6515 "find_in_path",
6516#endif
6517#ifdef FEAT_FLOAT
6518 "float",
6519#endif
6520#ifdef FEAT_FOLDING
6521 "folding",
6522#endif
6523#ifdef FEAT_FOOTER
6524 "footer",
6525#endif
6526#if !defined(USE_SYSTEM) && defined(UNIX)
6527 "fork",
6528#endif
6529#ifdef FEAT_GETTEXT
6530 "gettext",
6531#endif
6532#ifdef FEAT_GUI
6533 "gui",
6534#endif
6535#ifdef FEAT_GUI_ATHENA
6536# ifdef FEAT_GUI_NEXTAW
6537 "gui_neXtaw",
6538# else
6539 "gui_athena",
6540# endif
6541#endif
6542#ifdef FEAT_GUI_GTK
6543 "gui_gtk",
6544# ifdef USE_GTK3
6545 "gui_gtk3",
6546# else
6547 "gui_gtk2",
6548# endif
6549#endif
6550#ifdef FEAT_GUI_GNOME
6551 "gui_gnome",
6552#endif
6553#ifdef FEAT_GUI_MAC
6554 "gui_mac",
6555#endif
6556#ifdef FEAT_GUI_MOTIF
6557 "gui_motif",
6558#endif
6559#ifdef FEAT_GUI_PHOTON
6560 "gui_photon",
6561#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006562#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006563 "gui_win32",
6564#endif
6565#ifdef FEAT_HANGULIN
6566 "hangul_input",
6567#endif
6568#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6569 "iconv",
6570#endif
6571#ifdef FEAT_INS_EXPAND
6572 "insert_expand",
6573#endif
6574#ifdef FEAT_JOB_CHANNEL
6575 "job",
6576#endif
6577#ifdef FEAT_JUMPLIST
6578 "jumplist",
6579#endif
6580#ifdef FEAT_KEYMAP
6581 "keymap",
6582#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006583 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006584#ifdef FEAT_LANGMAP
6585 "langmap",
6586#endif
6587#ifdef FEAT_LIBCALL
6588 "libcall",
6589#endif
6590#ifdef FEAT_LINEBREAK
6591 "linebreak",
6592#endif
6593#ifdef FEAT_LISP
6594 "lispindent",
6595#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006596 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006597#ifdef FEAT_LOCALMAP
6598 "localmap",
6599#endif
6600#ifdef FEAT_LUA
6601# ifndef DYNAMIC_LUA
6602 "lua",
6603# endif
6604#endif
6605#ifdef FEAT_MENU
6606 "menu",
6607#endif
6608#ifdef FEAT_SESSION
6609 "mksession",
6610#endif
6611#ifdef FEAT_MODIFY_FNAME
6612 "modify_fname",
6613#endif
6614#ifdef FEAT_MOUSE
6615 "mouse",
6616#endif
6617#ifdef FEAT_MOUSESHAPE
6618 "mouseshape",
6619#endif
6620#if defined(UNIX) || defined(VMS)
6621# ifdef FEAT_MOUSE_DEC
6622 "mouse_dec",
6623# endif
6624# ifdef FEAT_MOUSE_GPM
6625 "mouse_gpm",
6626# endif
6627# ifdef FEAT_MOUSE_JSB
6628 "mouse_jsbterm",
6629# endif
6630# ifdef FEAT_MOUSE_NET
6631 "mouse_netterm",
6632# endif
6633# ifdef FEAT_MOUSE_PTERM
6634 "mouse_pterm",
6635# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006636# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006637 "mouse_sgr",
6638# endif
6639# ifdef FEAT_SYSMOUSE
6640 "mouse_sysmouse",
6641# endif
6642# ifdef FEAT_MOUSE_URXVT
6643 "mouse_urxvt",
6644# endif
6645# ifdef FEAT_MOUSE_XTERM
6646 "mouse_xterm",
6647# endif
6648#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006649 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650#ifdef FEAT_MBYTE_IME
6651 "multi_byte_ime",
6652#endif
6653#ifdef FEAT_MULTI_LANG
6654 "multi_lang",
6655#endif
6656#ifdef FEAT_MZSCHEME
6657#ifndef DYNAMIC_MZSCHEME
6658 "mzscheme",
6659#endif
6660#endif
6661#ifdef FEAT_NUM64
6662 "num64",
6663#endif
6664#ifdef FEAT_OLE
6665 "ole",
6666#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006667#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006668 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006669#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006670#ifdef FEAT_PATH_EXTRA
6671 "path_extra",
6672#endif
6673#ifdef FEAT_PERL
6674#ifndef DYNAMIC_PERL
6675 "perl",
6676#endif
6677#endif
6678#ifdef FEAT_PERSISTENT_UNDO
6679 "persistent_undo",
6680#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006681#if defined(FEAT_PYTHON)
6682 "python_compiled",
6683# if defined(DYNAMIC_PYTHON)
6684 "python_dynamic",
6685# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006686 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006687 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006688# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006689#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006690#if defined(FEAT_PYTHON3)
6691 "python3_compiled",
6692# if defined(DYNAMIC_PYTHON3)
6693 "python3_dynamic",
6694# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006696 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006697# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006698#endif
6699#ifdef FEAT_POSTSCRIPT
6700 "postscript",
6701#endif
6702#ifdef FEAT_PRINTER
6703 "printer",
6704#endif
6705#ifdef FEAT_PROFILE
6706 "profile",
6707#endif
6708#ifdef FEAT_RELTIME
6709 "reltime",
6710#endif
6711#ifdef FEAT_QUICKFIX
6712 "quickfix",
6713#endif
6714#ifdef FEAT_RIGHTLEFT
6715 "rightleft",
6716#endif
6717#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6718 "ruby",
6719#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006720 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006721#ifdef FEAT_CMDL_INFO
6722 "showcmd",
6723 "cmdline_info",
6724#endif
6725#ifdef FEAT_SIGNS
6726 "signs",
6727#endif
6728#ifdef FEAT_SMARTINDENT
6729 "smartindent",
6730#endif
6731#ifdef STARTUPTIME
6732 "startuptime",
6733#endif
6734#ifdef FEAT_STL_OPT
6735 "statusline",
6736#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006737#ifdef FEAT_NETBEANS_INTG
6738 "netbeans_intg",
6739#endif
6740#ifdef FEAT_SPELL
6741 "spell",
6742#endif
6743#ifdef FEAT_SYN_HL
6744 "syntax",
6745#endif
6746#if defined(USE_SYSTEM) || !defined(UNIX)
6747 "system",
6748#endif
6749#ifdef FEAT_TAG_BINS
6750 "tag_binary",
6751#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006752#ifdef FEAT_TCL
6753# ifndef DYNAMIC_TCL
6754 "tcl",
6755# endif
6756#endif
6757#ifdef FEAT_TERMGUICOLORS
6758 "termguicolors",
6759#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006760#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006761 "terminal",
6762#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006763#ifdef TERMINFO
6764 "terminfo",
6765#endif
6766#ifdef FEAT_TERMRESPONSE
6767 "termresponse",
6768#endif
6769#ifdef FEAT_TEXTOBJ
6770 "textobjects",
6771#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006772#ifdef FEAT_TEXT_PROP
6773 "textprop",
6774#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006775#ifdef HAVE_TGETENT
6776 "tgetent",
6777#endif
6778#ifdef FEAT_TIMERS
6779 "timers",
6780#endif
6781#ifdef FEAT_TITLE
6782 "title",
6783#endif
6784#ifdef FEAT_TOOLBAR
6785 "toolbar",
6786#endif
6787#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6788 "unnamedplus",
6789#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006790 "user-commands", /* was accidentally included in 5.4 */
6791 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006792#ifdef FEAT_VARTABS
6793 "vartabs",
6794#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006795 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006796#ifdef FEAT_VIMINFO
6797 "viminfo",
6798#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006799 "vimscript-1",
6800 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006801 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006802 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006803 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006804 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006805 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006806#ifdef FEAT_VTP
6807 "vtp",
6808#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006809#ifdef FEAT_WILDIGN
6810 "wildignore",
6811#endif
6812#ifdef FEAT_WILDMENU
6813 "wildmenu",
6814#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006815 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006816#ifdef FEAT_WAK
6817 "winaltkeys",
6818#endif
6819#ifdef FEAT_WRITEBACKUP
6820 "writebackup",
6821#endif
6822#ifdef FEAT_XIM
6823 "xim",
6824#endif
6825#ifdef FEAT_XFONTSET
6826 "xfontset",
6827#endif
6828#ifdef FEAT_XPM_W32
6829 "xpm",
6830 "xpm_w32", /* for backward compatibility */
6831#else
6832# if defined(HAVE_XPM)
6833 "xpm",
6834# endif
6835#endif
6836#ifdef USE_XSMP
6837 "xsmp",
6838#endif
6839#ifdef USE_XSMP_INTERACT
6840 "xsmp_interact",
6841#endif
6842#ifdef FEAT_XCLIPBOARD
6843 "xterm_clipboard",
6844#endif
6845#ifdef FEAT_XTERM_SAVE
6846 "xterm_save",
6847#endif
6848#if defined(UNIX) && defined(FEAT_X11)
6849 "X11",
6850#endif
6851 NULL
6852 };
6853
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006854 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006855 for (i = 0; has_list[i] != NULL; ++i)
6856 if (STRICMP(name, has_list[i]) == 0)
6857 {
6858 n = TRUE;
6859 break;
6860 }
6861
6862 if (n == FALSE)
6863 {
6864 if (STRNICMP(name, "patch", 5) == 0)
6865 {
6866 if (name[5] == '-'
6867 && STRLEN(name) >= 11
6868 && vim_isdigit(name[6])
6869 && vim_isdigit(name[8])
6870 && vim_isdigit(name[10]))
6871 {
6872 int major = atoi((char *)name + 6);
6873 int minor = atoi((char *)name + 8);
6874
6875 /* Expect "patch-9.9.01234". */
6876 n = (major < VIM_VERSION_MAJOR
6877 || (major == VIM_VERSION_MAJOR
6878 && (minor < VIM_VERSION_MINOR
6879 || (minor == VIM_VERSION_MINOR
6880 && has_patch(atoi((char *)name + 10))))));
6881 }
6882 else
6883 n = has_patch(atoi((char *)name + 5));
6884 }
6885 else if (STRICMP(name, "vim_starting") == 0)
6886 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006887 else if (STRICMP(name, "ttyin") == 0)
6888 n = mch_input_isatty();
6889 else if (STRICMP(name, "ttyout") == 0)
6890 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006891 else if (STRICMP(name, "multi_byte_encoding") == 0)
6892 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006893#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006894 else if (STRICMP(name, "balloon_multiline") == 0)
6895 n = multiline_balloon_available();
6896#endif
6897#ifdef DYNAMIC_TCL
6898 else if (STRICMP(name, "tcl") == 0)
6899 n = tcl_enabled(FALSE);
6900#endif
6901#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6902 else if (STRICMP(name, "iconv") == 0)
6903 n = iconv_enabled(FALSE);
6904#endif
6905#ifdef DYNAMIC_LUA
6906 else if (STRICMP(name, "lua") == 0)
6907 n = lua_enabled(FALSE);
6908#endif
6909#ifdef DYNAMIC_MZSCHEME
6910 else if (STRICMP(name, "mzscheme") == 0)
6911 n = mzscheme_enabled(FALSE);
6912#endif
6913#ifdef DYNAMIC_RUBY
6914 else if (STRICMP(name, "ruby") == 0)
6915 n = ruby_enabled(FALSE);
6916#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006917#ifdef DYNAMIC_PYTHON
6918 else if (STRICMP(name, "python") == 0)
6919 n = python_enabled(FALSE);
6920#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006921#ifdef DYNAMIC_PYTHON3
6922 else if (STRICMP(name, "python3") == 0)
6923 n = python3_enabled(FALSE);
6924#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006925#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6926 else if (STRICMP(name, "pythonx") == 0)
6927 {
6928# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6929 if (p_pyx == 0)
6930 n = python3_enabled(FALSE) || python_enabled(FALSE);
6931 else if (p_pyx == 3)
6932 n = python3_enabled(FALSE);
6933 else if (p_pyx == 2)
6934 n = python_enabled(FALSE);
6935# elif defined(DYNAMIC_PYTHON)
6936 n = python_enabled(FALSE);
6937# elif defined(DYNAMIC_PYTHON3)
6938 n = python3_enabled(FALSE);
6939# endif
6940 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006941#endif
6942#ifdef DYNAMIC_PERL
6943 else if (STRICMP(name, "perl") == 0)
6944 n = perl_enabled(FALSE);
6945#endif
6946#ifdef FEAT_GUI
6947 else if (STRICMP(name, "gui_running") == 0)
6948 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006949# ifdef FEAT_BROWSE
6950 else if (STRICMP(name, "browse") == 0)
6951 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6952# endif
6953#endif
6954#ifdef FEAT_SYN_HL
6955 else if (STRICMP(name, "syntax_items") == 0)
6956 n = syntax_present(curwin);
6957#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006958#ifdef FEAT_VTP
6959 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006960 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006961#endif
6962#ifdef FEAT_NETBEANS_INTG
6963 else if (STRICMP(name, "netbeans_enabled") == 0)
6964 n = netbeans_active();
6965#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006966#ifdef FEAT_MOUSE_GPM
6967 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6968 n = gpm_enabled();
6969#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006970#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006971 else if (STRICMP(name, "terminal") == 0)
6972 n = terminal_enabled();
6973#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006974#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006975 else if (STRICMP(name, "conpty") == 0)
6976 n = use_conpty();
6977#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006978 }
6979
6980 rettv->vval.v_number = n;
6981}
6982
6983/*
6984 * "has_key()" function
6985 */
6986 static void
6987f_has_key(typval_T *argvars, typval_T *rettv)
6988{
6989 if (argvars[0].v_type != VAR_DICT)
6990 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006991 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006992 return;
6993 }
6994 if (argvars[0].vval.v_dict == NULL)
6995 return;
6996
6997 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006998 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006999}
7000
7001/*
7002 * "haslocaldir()" function
7003 */
7004 static void
7005f_haslocaldir(typval_T *argvars, typval_T *rettv)
7006{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007007 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007008 win_T *wp = NULL;
7009
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007010 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7011
7012 // Check for window-local and tab-local directories
7013 if (wp != NULL && wp->w_localdir != NULL)
7014 rettv->vval.v_number = 1;
7015 else if (tp != NULL && tp->tp_localdir != NULL)
7016 rettv->vval.v_number = 2;
7017 else
7018 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007019}
7020
7021/*
7022 * "hasmapto()" function
7023 */
7024 static void
7025f_hasmapto(typval_T *argvars, typval_T *rettv)
7026{
7027 char_u *name;
7028 char_u *mode;
7029 char_u buf[NUMBUFLEN];
7030 int abbr = FALSE;
7031
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007032 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007033 if (argvars[1].v_type == VAR_UNKNOWN)
7034 mode = (char_u *)"nvo";
7035 else
7036 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007037 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007038 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007039 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007040 }
7041
7042 if (map_to_exists(name, mode, abbr))
7043 rettv->vval.v_number = TRUE;
7044 else
7045 rettv->vval.v_number = FALSE;
7046}
7047
7048/*
7049 * "histadd()" function
7050 */
7051 static void
7052f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7053{
7054#ifdef FEAT_CMDHIST
7055 int histype;
7056 char_u *str;
7057 char_u buf[NUMBUFLEN];
7058#endif
7059
7060 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007061 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007062 return;
7063#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007064 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007065 histype = str != NULL ? get_histtype(str) : -1;
7066 if (histype >= 0)
7067 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007068 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007069 if (*str != NUL)
7070 {
7071 init_history();
7072 add_to_history(histype, str, FALSE, NUL);
7073 rettv->vval.v_number = TRUE;
7074 return;
7075 }
7076 }
7077#endif
7078}
7079
7080/*
7081 * "histdel()" function
7082 */
7083 static void
7084f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7085{
7086#ifdef FEAT_CMDHIST
7087 int n;
7088 char_u buf[NUMBUFLEN];
7089 char_u *str;
7090
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007091 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007092 if (str == NULL)
7093 n = 0;
7094 else if (argvars[1].v_type == VAR_UNKNOWN)
7095 /* only one argument: clear entire history */
7096 n = clr_history(get_histtype(str));
7097 else if (argvars[1].v_type == VAR_NUMBER)
7098 /* index given: remove that entry */
7099 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007100 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007101 else
7102 /* string given: remove all matching entries */
7103 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007104 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007105 rettv->vval.v_number = n;
7106#endif
7107}
7108
7109/*
7110 * "histget()" function
7111 */
7112 static void
7113f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7114{
7115#ifdef FEAT_CMDHIST
7116 int type;
7117 int idx;
7118 char_u *str;
7119
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007120 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007121 if (str == NULL)
7122 rettv->vval.v_string = NULL;
7123 else
7124 {
7125 type = get_histtype(str);
7126 if (argvars[1].v_type == VAR_UNKNOWN)
7127 idx = get_history_idx(type);
7128 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007129 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007130 /* -1 on type error */
7131 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7132 }
7133#else
7134 rettv->vval.v_string = NULL;
7135#endif
7136 rettv->v_type = VAR_STRING;
7137}
7138
7139/*
7140 * "histnr()" function
7141 */
7142 static void
7143f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7144{
7145 int i;
7146
7147#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007148 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007149
7150 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7151 if (i >= HIST_CMD && i < HIST_COUNT)
7152 i = get_history_idx(i);
7153 else
7154#endif
7155 i = -1;
7156 rettv->vval.v_number = i;
7157}
7158
7159/*
7160 * "highlightID(name)" function
7161 */
7162 static void
7163f_hlID(typval_T *argvars, typval_T *rettv)
7164{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007165 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007166}
7167
7168/*
7169 * "highlight_exists()" function
7170 */
7171 static void
7172f_hlexists(typval_T *argvars, typval_T *rettv)
7173{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007174 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007175}
7176
7177/*
7178 * "hostname()" function
7179 */
7180 static void
7181f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7182{
7183 char_u hostname[256];
7184
7185 mch_get_host_name(hostname, 256);
7186 rettv->v_type = VAR_STRING;
7187 rettv->vval.v_string = vim_strsave(hostname);
7188}
7189
7190/*
7191 * iconv() function
7192 */
7193 static void
7194f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7195{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007196 char_u buf1[NUMBUFLEN];
7197 char_u buf2[NUMBUFLEN];
7198 char_u *from, *to, *str;
7199 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007200
7201 rettv->v_type = VAR_STRING;
7202 rettv->vval.v_string = NULL;
7203
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007204 str = tv_get_string(&argvars[0]);
7205 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7206 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007207 vimconv.vc_type = CONV_NONE;
7208 convert_setup(&vimconv, from, to);
7209
7210 /* If the encodings are equal, no conversion needed. */
7211 if (vimconv.vc_type == CONV_NONE)
7212 rettv->vval.v_string = vim_strsave(str);
7213 else
7214 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7215
7216 convert_setup(&vimconv, NULL, NULL);
7217 vim_free(from);
7218 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007219}
7220
7221/*
7222 * "indent()" function
7223 */
7224 static void
7225f_indent(typval_T *argvars, typval_T *rettv)
7226{
7227 linenr_T lnum;
7228
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007229 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007230 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7231 rettv->vval.v_number = get_indent_lnum(lnum);
7232 else
7233 rettv->vval.v_number = -1;
7234}
7235
7236/*
7237 * "index()" function
7238 */
7239 static void
7240f_index(typval_T *argvars, typval_T *rettv)
7241{
7242 list_T *l;
7243 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007244 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245 long idx = 0;
7246 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007247 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007248
7249 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007250 if (argvars[0].v_type == VAR_BLOB)
7251 {
7252 typval_T tv;
7253 int start = 0;
7254
7255 if (argvars[2].v_type != VAR_UNKNOWN)
7256 {
7257 start = tv_get_number_chk(&argvars[2], &error);
7258 if (error)
7259 return;
7260 }
7261 b = argvars[0].vval.v_blob;
7262 if (b == NULL)
7263 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007264 if (start < 0)
7265 {
7266 start = blob_len(b) + start;
7267 if (start < 0)
7268 start = 0;
7269 }
7270
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007271 for (idx = start; idx < blob_len(b); ++idx)
7272 {
7273 tv.v_type = VAR_NUMBER;
7274 tv.vval.v_number = blob_get(b, idx);
7275 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7276 {
7277 rettv->vval.v_number = idx;
7278 return;
7279 }
7280 }
7281 return;
7282 }
7283 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007284 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007285 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007286 return;
7287 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007288
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007289 l = argvars[0].vval.v_list;
7290 if (l != NULL)
7291 {
7292 item = l->lv_first;
7293 if (argvars[2].v_type != VAR_UNKNOWN)
7294 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007295 /* Start at specified item. Use the cached index that list_find()
7296 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007297 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007298 idx = l->lv_idx;
7299 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007300 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007301 if (error)
7302 item = NULL;
7303 }
7304
7305 for ( ; item != NULL; item = item->li_next, ++idx)
7306 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7307 {
7308 rettv->vval.v_number = idx;
7309 break;
7310 }
7311 }
7312}
7313
7314static int inputsecret_flag = 0;
7315
7316/*
7317 * "input()" function
7318 * Also handles inputsecret() when inputsecret is set.
7319 */
7320 static void
7321f_input(typval_T *argvars, typval_T *rettv)
7322{
7323 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7324}
7325
7326/*
7327 * "inputdialog()" function
7328 */
7329 static void
7330f_inputdialog(typval_T *argvars, typval_T *rettv)
7331{
7332#if defined(FEAT_GUI_TEXTDIALOG)
7333 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7334 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7335 {
7336 char_u *message;
7337 char_u buf[NUMBUFLEN];
7338 char_u *defstr = (char_u *)"";
7339
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007340 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007341 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007342 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007343 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7344 else
7345 IObuff[0] = NUL;
7346 if (message != NULL && defstr != NULL
7347 && do_dialog(VIM_QUESTION, NULL, message,
7348 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7349 rettv->vval.v_string = vim_strsave(IObuff);
7350 else
7351 {
7352 if (message != NULL && defstr != NULL
7353 && argvars[1].v_type != VAR_UNKNOWN
7354 && argvars[2].v_type != VAR_UNKNOWN)
7355 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007356 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007357 else
7358 rettv->vval.v_string = NULL;
7359 }
7360 rettv->v_type = VAR_STRING;
7361 }
7362 else
7363#endif
7364 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7365}
7366
7367/*
7368 * "inputlist()" function
7369 */
7370 static void
7371f_inputlist(typval_T *argvars, typval_T *rettv)
7372{
7373 listitem_T *li;
7374 int selected;
7375 int mouse_used;
7376
7377#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007378 /* While starting up, there is no place to enter text. When running tests
7379 * with --not-a-term we assume feedkeys() will be used. */
7380 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007381 return;
7382#endif
7383 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7384 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007385 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007386 return;
7387 }
7388
7389 msg_start();
7390 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7391 lines_left = Rows; /* avoid more prompt */
7392 msg_scroll = TRUE;
7393 msg_clr_eos();
7394
7395 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7396 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007397 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007398 msg_putchar('\n');
7399 }
7400
7401 /* Ask for choice. */
7402 selected = prompt_for_number(&mouse_used);
7403 if (mouse_used)
7404 selected -= lines_left;
7405
7406 rettv->vval.v_number = selected;
7407}
7408
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007409static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7410
7411/*
7412 * "inputrestore()" function
7413 */
7414 static void
7415f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7416{
7417 if (ga_userinput.ga_len > 0)
7418 {
7419 --ga_userinput.ga_len;
7420 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7421 + ga_userinput.ga_len);
7422 /* default return is zero == OK */
7423 }
7424 else if (p_verbose > 1)
7425 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007426 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007427 rettv->vval.v_number = 1; /* Failed */
7428 }
7429}
7430
7431/*
7432 * "inputsave()" function
7433 */
7434 static void
7435f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7436{
7437 /* Add an entry to the stack of typeahead storage. */
7438 if (ga_grow(&ga_userinput, 1) == OK)
7439 {
7440 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7441 + ga_userinput.ga_len);
7442 ++ga_userinput.ga_len;
7443 /* default return is zero == OK */
7444 }
7445 else
7446 rettv->vval.v_number = 1; /* Failed */
7447}
7448
7449/*
7450 * "inputsecret()" function
7451 */
7452 static void
7453f_inputsecret(typval_T *argvars, typval_T *rettv)
7454{
7455 ++cmdline_star;
7456 ++inputsecret_flag;
7457 f_input(argvars, rettv);
7458 --cmdline_star;
7459 --inputsecret_flag;
7460}
7461
7462/*
7463 * "insert()" function
7464 */
7465 static void
7466f_insert(typval_T *argvars, typval_T *rettv)
7467{
7468 long before = 0;
7469 listitem_T *item;
7470 list_T *l;
7471 int error = FALSE;
7472
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007473 if (argvars[0].v_type == VAR_BLOB)
7474 {
7475 int val, len;
7476 char_u *p;
7477
7478 len = blob_len(argvars[0].vval.v_blob);
7479 if (argvars[2].v_type != VAR_UNKNOWN)
7480 {
7481 before = (long)tv_get_number_chk(&argvars[2], &error);
7482 if (error)
7483 return; // type error; errmsg already given
7484 if (before < 0 || before > len)
7485 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007486 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007487 return;
7488 }
7489 }
7490 val = tv_get_number_chk(&argvars[1], &error);
7491 if (error)
7492 return;
7493 if (val < 0 || val > 255)
7494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007495 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007496 return;
7497 }
7498
7499 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7500 return;
7501 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7502 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7503 *(p + before) = val;
7504 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7505
7506 copy_tv(&argvars[0], rettv);
7507 }
7508 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007509 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007510 else if ((l = argvars[0].vval.v_list) != NULL
7511 && !var_check_lock(l->lv_lock,
7512 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007513 {
7514 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007515 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007516 if (error)
7517 return; /* type error; errmsg already given */
7518
7519 if (before == l->lv_len)
7520 item = NULL;
7521 else
7522 {
7523 item = list_find(l, before);
7524 if (item == NULL)
7525 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007526 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007527 l = NULL;
7528 }
7529 }
7530 if (l != NULL)
7531 {
7532 list_insert_tv(l, &argvars[1], item);
7533 copy_tv(&argvars[0], rettv);
7534 }
7535 }
7536}
7537
7538/*
7539 * "invert(expr)" function
7540 */
7541 static void
7542f_invert(typval_T *argvars, typval_T *rettv)
7543{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007544 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007545}
7546
7547/*
7548 * "isdirectory()" function
7549 */
7550 static void
7551f_isdirectory(typval_T *argvars, typval_T *rettv)
7552{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007553 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007554}
7555
7556/*
7557 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7558 * or it refers to a List or Dictionary that is locked.
7559 */
7560 static int
7561tv_islocked(typval_T *tv)
7562{
7563 return (tv->v_lock & VAR_LOCKED)
7564 || (tv->v_type == VAR_LIST
7565 && tv->vval.v_list != NULL
7566 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7567 || (tv->v_type == VAR_DICT
7568 && tv->vval.v_dict != NULL
7569 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7570}
7571
7572/*
7573 * "islocked()" function
7574 */
7575 static void
7576f_islocked(typval_T *argvars, typval_T *rettv)
7577{
7578 lval_T lv;
7579 char_u *end;
7580 dictitem_T *di;
7581
7582 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007583 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007584 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007585 if (end != NULL && lv.ll_name != NULL)
7586 {
7587 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007588 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007589 else
7590 {
7591 if (lv.ll_tv == NULL)
7592 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007593 di = find_var(lv.ll_name, NULL, TRUE);
7594 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007595 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007596 /* Consider a variable locked when:
7597 * 1. the variable itself is locked
7598 * 2. the value of the variable is locked.
7599 * 3. the List or Dict value is locked.
7600 */
7601 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7602 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603 }
7604 }
7605 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007606 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007607 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007608 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007609 else if (lv.ll_list != NULL)
7610 /* List item. */
7611 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7612 else
7613 /* Dictionary item. */
7614 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7615 }
7616 }
7617
7618 clear_lval(&lv);
7619}
7620
7621#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7622/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007623 * "isinf()" function
7624 */
7625 static void
7626f_isinf(typval_T *argvars, typval_T *rettv)
7627{
7628 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7629 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7630}
7631
7632/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007633 * "isnan()" function
7634 */
7635 static void
7636f_isnan(typval_T *argvars, typval_T *rettv)
7637{
7638 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7639 && isnan(argvars[0].vval.v_float);
7640}
7641#endif
7642
7643/*
7644 * "items(dict)" function
7645 */
7646 static void
7647f_items(typval_T *argvars, typval_T *rettv)
7648{
7649 dict_list(argvars, rettv, 2);
7650}
7651
7652#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7653/*
7654 * Get the job from the argument.
7655 * Returns NULL if the job is invalid.
7656 */
7657 static job_T *
7658get_job_arg(typval_T *tv)
7659{
7660 job_T *job;
7661
7662 if (tv->v_type != VAR_JOB)
7663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007664 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007665 return NULL;
7666 }
7667 job = tv->vval.v_job;
7668
7669 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007670 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007671 return job;
7672}
7673
7674/*
7675 * "job_getchannel()" function
7676 */
7677 static void
7678f_job_getchannel(typval_T *argvars, typval_T *rettv)
7679{
7680 job_T *job = get_job_arg(&argvars[0]);
7681
7682 if (job != NULL)
7683 {
7684 rettv->v_type = VAR_CHANNEL;
7685 rettv->vval.v_channel = job->jv_channel;
7686 if (job->jv_channel != NULL)
7687 ++job->jv_channel->ch_refcount;
7688 }
7689}
7690
7691/*
7692 * "job_info()" function
7693 */
7694 static void
7695f_job_info(typval_T *argvars, typval_T *rettv)
7696{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007697 if (argvars[0].v_type != VAR_UNKNOWN)
7698 {
7699 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007700
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007701 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7702 job_info(job, rettv->vval.v_dict);
7703 }
7704 else if (rettv_list_alloc(rettv) == OK)
7705 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007706}
7707
7708/*
7709 * "job_setoptions()" function
7710 */
7711 static void
7712f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7713{
7714 job_T *job = get_job_arg(&argvars[0]);
7715 jobopt_T opt;
7716
7717 if (job == NULL)
7718 return;
7719 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007720 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007721 job_set_options(job, &opt);
7722 free_job_options(&opt);
7723}
7724
7725/*
7726 * "job_start()" function
7727 */
7728 static void
7729f_job_start(typval_T *argvars, typval_T *rettv)
7730{
7731 rettv->v_type = VAR_JOB;
7732 if (check_restricted() || check_secure())
7733 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007734 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007735}
7736
7737/*
7738 * "job_status()" function
7739 */
7740 static void
7741f_job_status(typval_T *argvars, typval_T *rettv)
7742{
7743 job_T *job = get_job_arg(&argvars[0]);
7744
7745 if (job != NULL)
7746 {
7747 rettv->v_type = VAR_STRING;
7748 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7749 }
7750}
7751
7752/*
7753 * "job_stop()" function
7754 */
7755 static void
7756f_job_stop(typval_T *argvars, typval_T *rettv)
7757{
7758 job_T *job = get_job_arg(&argvars[0]);
7759
7760 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007761 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007762}
7763#endif
7764
7765/*
7766 * "join()" function
7767 */
7768 static void
7769f_join(typval_T *argvars, typval_T *rettv)
7770{
7771 garray_T ga;
7772 char_u *sep;
7773
7774 if (argvars[0].v_type != VAR_LIST)
7775 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007776 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007777 return;
7778 }
7779 if (argvars[0].vval.v_list == NULL)
7780 return;
7781 if (argvars[1].v_type == VAR_UNKNOWN)
7782 sep = (char_u *)" ";
7783 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007784 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007785
7786 rettv->v_type = VAR_STRING;
7787
7788 if (sep != NULL)
7789 {
7790 ga_init2(&ga, (int)sizeof(char), 80);
7791 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7792 ga_append(&ga, NUL);
7793 rettv->vval.v_string = (char_u *)ga.ga_data;
7794 }
7795 else
7796 rettv->vval.v_string = NULL;
7797}
7798
7799/*
7800 * "js_decode()" function
7801 */
7802 static void
7803f_js_decode(typval_T *argvars, typval_T *rettv)
7804{
7805 js_read_T reader;
7806
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007807 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007808 reader.js_fill = NULL;
7809 reader.js_used = 0;
7810 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007811 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007812}
7813
7814/*
7815 * "js_encode()" function
7816 */
7817 static void
7818f_js_encode(typval_T *argvars, typval_T *rettv)
7819{
7820 rettv->v_type = VAR_STRING;
7821 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7822}
7823
7824/*
7825 * "json_decode()" function
7826 */
7827 static void
7828f_json_decode(typval_T *argvars, typval_T *rettv)
7829{
7830 js_read_T reader;
7831
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007832 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007833 reader.js_fill = NULL;
7834 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007835 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007836}
7837
7838/*
7839 * "json_encode()" function
7840 */
7841 static void
7842f_json_encode(typval_T *argvars, typval_T *rettv)
7843{
7844 rettv->v_type = VAR_STRING;
7845 rettv->vval.v_string = json_encode(&argvars[0], 0);
7846}
7847
7848/*
7849 * "keys()" function
7850 */
7851 static void
7852f_keys(typval_T *argvars, typval_T *rettv)
7853{
7854 dict_list(argvars, rettv, 0);
7855}
7856
7857/*
7858 * "last_buffer_nr()" function.
7859 */
7860 static void
7861f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7862{
7863 int n = 0;
7864 buf_T *buf;
7865
Bram Moolenaar29323592016-07-24 22:04:11 +02007866 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007867 if (n < buf->b_fnum)
7868 n = buf->b_fnum;
7869
7870 rettv->vval.v_number = n;
7871}
7872
7873/*
7874 * "len()" function
7875 */
7876 static void
7877f_len(typval_T *argvars, typval_T *rettv)
7878{
7879 switch (argvars[0].v_type)
7880 {
7881 case VAR_STRING:
7882 case VAR_NUMBER:
7883 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007884 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007885 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007886 case VAR_BLOB:
7887 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7888 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007889 case VAR_LIST:
7890 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7891 break;
7892 case VAR_DICT:
7893 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7894 break;
7895 case VAR_UNKNOWN:
7896 case VAR_SPECIAL:
7897 case VAR_FLOAT:
7898 case VAR_FUNC:
7899 case VAR_PARTIAL:
7900 case VAR_JOB:
7901 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007902 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007903 break;
7904 }
7905}
7906
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007907 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007908libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007909{
7910#ifdef FEAT_LIBCALL
7911 char_u *string_in;
7912 char_u **string_result;
7913 int nr_result;
7914#endif
7915
7916 rettv->v_type = type;
7917 if (type != VAR_NUMBER)
7918 rettv->vval.v_string = NULL;
7919
7920 if (check_restricted() || check_secure())
7921 return;
7922
7923#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007924 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007925 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7926 {
7927 string_in = NULL;
7928 if (argvars[2].v_type == VAR_STRING)
7929 string_in = argvars[2].vval.v_string;
7930 if (type == VAR_NUMBER)
7931 string_result = NULL;
7932 else
7933 string_result = &rettv->vval.v_string;
7934 if (mch_libcall(argvars[0].vval.v_string,
7935 argvars[1].vval.v_string,
7936 string_in,
7937 argvars[2].vval.v_number,
7938 string_result,
7939 &nr_result) == OK
7940 && type == VAR_NUMBER)
7941 rettv->vval.v_number = nr_result;
7942 }
7943#endif
7944}
7945
7946/*
7947 * "libcall()" function
7948 */
7949 static void
7950f_libcall(typval_T *argvars, typval_T *rettv)
7951{
7952 libcall_common(argvars, rettv, VAR_STRING);
7953}
7954
7955/*
7956 * "libcallnr()" function
7957 */
7958 static void
7959f_libcallnr(typval_T *argvars, typval_T *rettv)
7960{
7961 libcall_common(argvars, rettv, VAR_NUMBER);
7962}
7963
7964/*
7965 * "line(string)" function
7966 */
7967 static void
7968f_line(typval_T *argvars, typval_T *rettv)
7969{
7970 linenr_T lnum = 0;
7971 pos_T *fp;
7972 int fnum;
7973
7974 fp = var2fpos(&argvars[0], TRUE, &fnum);
7975 if (fp != NULL)
7976 lnum = fp->lnum;
7977 rettv->vval.v_number = lnum;
7978}
7979
7980/*
7981 * "line2byte(lnum)" function
7982 */
7983 static void
7984f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7985{
7986#ifndef FEAT_BYTEOFF
7987 rettv->vval.v_number = -1;
7988#else
7989 linenr_T lnum;
7990
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007991 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7993 rettv->vval.v_number = -1;
7994 else
7995 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7996 if (rettv->vval.v_number >= 0)
7997 ++rettv->vval.v_number;
7998#endif
7999}
8000
8001/*
8002 * "lispindent(lnum)" function
8003 */
8004 static void
8005f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8006{
8007#ifdef FEAT_LISP
8008 pos_T pos;
8009 linenr_T lnum;
8010
8011 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008012 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008013 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8014 {
8015 curwin->w_cursor.lnum = lnum;
8016 rettv->vval.v_number = get_lisp_indent();
8017 curwin->w_cursor = pos;
8018 }
8019 else
8020#endif
8021 rettv->vval.v_number = -1;
8022}
8023
8024/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008025 * "list2str()" function
8026 */
8027 static void
8028f_list2str(typval_T *argvars, typval_T *rettv)
8029{
8030 list_T *l;
8031 listitem_T *li;
8032 garray_T ga;
8033 int utf8 = FALSE;
8034
8035 rettv->v_type = VAR_STRING;
8036 rettv->vval.v_string = NULL;
8037 if (argvars[0].v_type != VAR_LIST)
8038 {
8039 emsg(_(e_invarg));
8040 return;
8041 }
8042
8043 l = argvars[0].vval.v_list;
8044 if (l == NULL)
8045 return; // empty list results in empty string
8046
8047 if (argvars[1].v_type != VAR_UNKNOWN)
8048 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8049
8050 ga_init2(&ga, 1, 80);
8051 if (has_mbyte || utf8)
8052 {
8053 char_u buf[MB_MAXBYTES + 1];
8054 int (*char2bytes)(int, char_u *);
8055
8056 if (utf8 || enc_utf8)
8057 char2bytes = utf_char2bytes;
8058 else
8059 char2bytes = mb_char2bytes;
8060
8061 for (li = l->lv_first; li != NULL; li = li->li_next)
8062 {
8063 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8064 ga_concat(&ga, buf);
8065 }
8066 ga_append(&ga, NUL);
8067 }
8068 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8069 {
8070 for (li = l->lv_first; li != NULL; li = li->li_next)
8071 ga_append(&ga, tv_get_number(&li->li_tv));
8072 ga_append(&ga, NUL);
8073 }
8074
8075 rettv->v_type = VAR_STRING;
8076 rettv->vval.v_string = ga.ga_data;
8077}
8078
8079/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008080 * "localtime()" function
8081 */
8082 static void
8083f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8084{
8085 rettv->vval.v_number = (varnumber_T)time(NULL);
8086}
8087
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088 static void
8089get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8090{
8091 char_u *keys;
8092 char_u *which;
8093 char_u buf[NUMBUFLEN];
8094 char_u *keys_buf = NULL;
8095 char_u *rhs;
8096 int mode;
8097 int abbr = FALSE;
8098 int get_dict = FALSE;
8099 mapblock_T *mp;
8100 int buffer_local;
8101
8102 /* return empty string for failure */
8103 rettv->v_type = VAR_STRING;
8104 rettv->vval.v_string = NULL;
8105
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008106 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008107 if (*keys == NUL)
8108 return;
8109
8110 if (argvars[1].v_type != VAR_UNKNOWN)
8111 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008112 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008113 if (argvars[2].v_type != VAR_UNKNOWN)
8114 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008115 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008116 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008117 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 }
8119 }
8120 else
8121 which = (char_u *)"";
8122 if (which == NULL)
8123 return;
8124
8125 mode = get_map_mode(&which, 0);
8126
8127 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8128 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8129 vim_free(keys_buf);
8130
8131 if (!get_dict)
8132 {
8133 /* Return a string. */
8134 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008135 {
8136 if (*rhs == NUL)
8137 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8138 else
8139 rettv->vval.v_string = str2special_save(rhs, FALSE);
8140 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008141
8142 }
8143 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8144 {
8145 /* Return a dictionary. */
8146 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8147 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8148 dict_T *dict = rettv->vval.v_dict;
8149
Bram Moolenaare0be1672018-07-08 16:50:37 +02008150 dict_add_string(dict, "lhs", lhs);
8151 dict_add_string(dict, "rhs", mp->m_orig_str);
8152 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8153 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8154 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008155 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8156 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008157 dict_add_number(dict, "buffer", (long)buffer_local);
8158 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8159 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008160
8161 vim_free(lhs);
8162 vim_free(mapmode);
8163 }
8164}
8165
8166#ifdef FEAT_FLOAT
8167/*
8168 * "log()" function
8169 */
8170 static void
8171f_log(typval_T *argvars, typval_T *rettv)
8172{
8173 float_T f = 0.0;
8174
8175 rettv->v_type = VAR_FLOAT;
8176 if (get_float_arg(argvars, &f) == OK)
8177 rettv->vval.v_float = log(f);
8178 else
8179 rettv->vval.v_float = 0.0;
8180}
8181
8182/*
8183 * "log10()" function
8184 */
8185 static void
8186f_log10(typval_T *argvars, typval_T *rettv)
8187{
8188 float_T f = 0.0;
8189
8190 rettv->v_type = VAR_FLOAT;
8191 if (get_float_arg(argvars, &f) == OK)
8192 rettv->vval.v_float = log10(f);
8193 else
8194 rettv->vval.v_float = 0.0;
8195}
8196#endif
8197
8198#ifdef FEAT_LUA
8199/*
8200 * "luaeval()" function
8201 */
8202 static void
8203f_luaeval(typval_T *argvars, typval_T *rettv)
8204{
8205 char_u *str;
8206 char_u buf[NUMBUFLEN];
8207
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008208 if (check_restricted() || check_secure())
8209 return;
8210
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008211 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008212 do_luaeval(str, argvars + 1, rettv);
8213}
8214#endif
8215
8216/*
8217 * "map()" function
8218 */
8219 static void
8220f_map(typval_T *argvars, typval_T *rettv)
8221{
8222 filter_map(argvars, rettv, TRUE);
8223}
8224
8225/*
8226 * "maparg()" function
8227 */
8228 static void
8229f_maparg(typval_T *argvars, typval_T *rettv)
8230{
8231 get_maparg(argvars, rettv, TRUE);
8232}
8233
8234/*
8235 * "mapcheck()" function
8236 */
8237 static void
8238f_mapcheck(typval_T *argvars, typval_T *rettv)
8239{
8240 get_maparg(argvars, rettv, FALSE);
8241}
8242
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008243typedef enum
8244{
8245 MATCH_END, /* matchend() */
8246 MATCH_MATCH, /* match() */
8247 MATCH_STR, /* matchstr() */
8248 MATCH_LIST, /* matchlist() */
8249 MATCH_POS /* matchstrpos() */
8250} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008251
8252 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008253find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008254{
8255 char_u *str = NULL;
8256 long len = 0;
8257 char_u *expr = NULL;
8258 char_u *pat;
8259 regmatch_T regmatch;
8260 char_u patbuf[NUMBUFLEN];
8261 char_u strbuf[NUMBUFLEN];
8262 char_u *save_cpo;
8263 long start = 0;
8264 long nth = 1;
8265 colnr_T startcol = 0;
8266 int match = 0;
8267 list_T *l = NULL;
8268 listitem_T *li = NULL;
8269 long idx = 0;
8270 char_u *tofree = NULL;
8271
8272 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8273 save_cpo = p_cpo;
8274 p_cpo = (char_u *)"";
8275
8276 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008277 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008278 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008279 /* type MATCH_LIST: return empty list when there are no matches.
8280 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008281 if (rettv_list_alloc(rettv) == FAIL)
8282 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008283 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008284 && (list_append_string(rettv->vval.v_list,
8285 (char_u *)"", 0) == FAIL
8286 || list_append_number(rettv->vval.v_list,
8287 (varnumber_T)-1) == FAIL
8288 || list_append_number(rettv->vval.v_list,
8289 (varnumber_T)-1) == FAIL
8290 || list_append_number(rettv->vval.v_list,
8291 (varnumber_T)-1) == FAIL))
8292 {
8293 list_free(rettv->vval.v_list);
8294 rettv->vval.v_list = NULL;
8295 goto theend;
8296 }
8297 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008298 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299 {
8300 rettv->v_type = VAR_STRING;
8301 rettv->vval.v_string = NULL;
8302 }
8303
8304 if (argvars[0].v_type == VAR_LIST)
8305 {
8306 if ((l = argvars[0].vval.v_list) == NULL)
8307 goto theend;
8308 li = l->lv_first;
8309 }
8310 else
8311 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008312 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008313 len = (long)STRLEN(str);
8314 }
8315
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008316 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008317 if (pat == NULL)
8318 goto theend;
8319
8320 if (argvars[2].v_type != VAR_UNKNOWN)
8321 {
8322 int error = FALSE;
8323
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008324 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008325 if (error)
8326 goto theend;
8327 if (l != NULL)
8328 {
8329 li = list_find(l, start);
8330 if (li == NULL)
8331 goto theend;
8332 idx = l->lv_idx; /* use the cached index */
8333 }
8334 else
8335 {
8336 if (start < 0)
8337 start = 0;
8338 if (start > len)
8339 goto theend;
8340 /* When "count" argument is there ignore matches before "start",
8341 * otherwise skip part of the string. Differs when pattern is "^"
8342 * or "\<". */
8343 if (argvars[3].v_type != VAR_UNKNOWN)
8344 startcol = start;
8345 else
8346 {
8347 str += start;
8348 len -= start;
8349 }
8350 }
8351
8352 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008353 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008354 if (error)
8355 goto theend;
8356 }
8357
8358 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8359 if (regmatch.regprog != NULL)
8360 {
8361 regmatch.rm_ic = p_ic;
8362
8363 for (;;)
8364 {
8365 if (l != NULL)
8366 {
8367 if (li == NULL)
8368 {
8369 match = FALSE;
8370 break;
8371 }
8372 vim_free(tofree);
8373 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8374 if (str == NULL)
8375 break;
8376 }
8377
8378 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8379
8380 if (match && --nth <= 0)
8381 break;
8382 if (l == NULL && !match)
8383 break;
8384
8385 /* Advance to just after the match. */
8386 if (l != NULL)
8387 {
8388 li = li->li_next;
8389 ++idx;
8390 }
8391 else
8392 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008393 startcol = (colnr_T)(regmatch.startp[0]
8394 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008395 if (startcol > (colnr_T)len
8396 || str + startcol <= regmatch.startp[0])
8397 {
8398 match = FALSE;
8399 break;
8400 }
8401 }
8402 }
8403
8404 if (match)
8405 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008406 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407 {
8408 listitem_T *li1 = rettv->vval.v_list->lv_first;
8409 listitem_T *li2 = li1->li_next;
8410 listitem_T *li3 = li2->li_next;
8411 listitem_T *li4 = li3->li_next;
8412
8413 vim_free(li1->li_tv.vval.v_string);
8414 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8415 (int)(regmatch.endp[0] - regmatch.startp[0]));
8416 li3->li_tv.vval.v_number =
8417 (varnumber_T)(regmatch.startp[0] - expr);
8418 li4->li_tv.vval.v_number =
8419 (varnumber_T)(regmatch.endp[0] - expr);
8420 if (l != NULL)
8421 li2->li_tv.vval.v_number = (varnumber_T)idx;
8422 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008423 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008424 {
8425 int i;
8426
8427 /* return list with matched string and submatches */
8428 for (i = 0; i < NSUBEXP; ++i)
8429 {
8430 if (regmatch.endp[i] == NULL)
8431 {
8432 if (list_append_string(rettv->vval.v_list,
8433 (char_u *)"", 0) == FAIL)
8434 break;
8435 }
8436 else if (list_append_string(rettv->vval.v_list,
8437 regmatch.startp[i],
8438 (int)(regmatch.endp[i] - regmatch.startp[i]))
8439 == FAIL)
8440 break;
8441 }
8442 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008443 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008444 {
8445 /* return matched string */
8446 if (l != NULL)
8447 copy_tv(&li->li_tv, rettv);
8448 else
8449 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8450 (int)(regmatch.endp[0] - regmatch.startp[0]));
8451 }
8452 else if (l != NULL)
8453 rettv->vval.v_number = idx;
8454 else
8455 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008456 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008457 rettv->vval.v_number =
8458 (varnumber_T)(regmatch.startp[0] - str);
8459 else
8460 rettv->vval.v_number =
8461 (varnumber_T)(regmatch.endp[0] - str);
8462 rettv->vval.v_number += (varnumber_T)(str - expr);
8463 }
8464 }
8465 vim_regfree(regmatch.regprog);
8466 }
8467
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008468theend:
8469 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008470 /* matchstrpos() without a list: drop the second item. */
8471 listitem_remove(rettv->vval.v_list,
8472 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008473 vim_free(tofree);
8474 p_cpo = save_cpo;
8475}
8476
8477/*
8478 * "match()" function
8479 */
8480 static void
8481f_match(typval_T *argvars, typval_T *rettv)
8482{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008483 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008484}
8485
Bram Moolenaar95e51472018-07-28 16:55:56 +02008486#ifdef FEAT_SEARCH_EXTRA
8487 static int
8488matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8489{
8490 dictitem_T *di;
8491
8492 if (tv->v_type != VAR_DICT)
8493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008494 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008495 return FAIL;
8496 }
8497
8498 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008499 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008500 (char_u *)"conceal", FALSE);
8501
8502 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8503 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008504 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008505 if (*win == NULL)
8506 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008507 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008508 return FAIL;
8509 }
8510 }
8511
8512 return OK;
8513}
8514#endif
8515
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008516/*
8517 * "matchadd()" function
8518 */
8519 static void
8520f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8521{
8522#ifdef FEAT_SEARCH_EXTRA
8523 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008524 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8525 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008526 int prio = 10; /* default priority */
8527 int id = -1;
8528 int error = FALSE;
8529 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008530 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008531
8532 rettv->vval.v_number = -1;
8533
8534 if (grp == NULL || pat == NULL)
8535 return;
8536 if (argvars[2].v_type != VAR_UNKNOWN)
8537 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008538 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008539 if (argvars[3].v_type != VAR_UNKNOWN)
8540 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008541 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008542 if (argvars[4].v_type != VAR_UNKNOWN
8543 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8544 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008545 }
8546 }
8547 if (error == TRUE)
8548 return;
8549 if (id >= 1 && id <= 3)
8550 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008551 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008552 return;
8553 }
8554
Bram Moolenaar95e51472018-07-28 16:55:56 +02008555 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008556 conceal_char);
8557#endif
8558}
8559
8560/*
8561 * "matchaddpos()" function
8562 */
8563 static void
8564f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8565{
8566#ifdef FEAT_SEARCH_EXTRA
8567 char_u buf[NUMBUFLEN];
8568 char_u *group;
8569 int prio = 10;
8570 int id = -1;
8571 int error = FALSE;
8572 list_T *l;
8573 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008574 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008575
8576 rettv->vval.v_number = -1;
8577
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008578 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008579 if (group == NULL)
8580 return;
8581
8582 if (argvars[1].v_type != VAR_LIST)
8583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008584 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008585 return;
8586 }
8587 l = argvars[1].vval.v_list;
8588 if (l == NULL)
8589 return;
8590
8591 if (argvars[2].v_type != VAR_UNKNOWN)
8592 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008593 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008594 if (argvars[3].v_type != VAR_UNKNOWN)
8595 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008596 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008597
8598 if (argvars[4].v_type != VAR_UNKNOWN
8599 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8600 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008601 }
8602 }
8603 if (error == TRUE)
8604 return;
8605
8606 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8607 if (id == 1 || id == 2)
8608 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008609 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610 return;
8611 }
8612
Bram Moolenaar95e51472018-07-28 16:55:56 +02008613 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008614 conceal_char);
8615#endif
8616}
8617
8618/*
8619 * "matcharg()" function
8620 */
8621 static void
8622f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8623{
8624 if (rettv_list_alloc(rettv) == OK)
8625 {
8626#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008627 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008628 matchitem_T *m;
8629
8630 if (id >= 1 && id <= 3)
8631 {
8632 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8633 {
8634 list_append_string(rettv->vval.v_list,
8635 syn_id2name(m->hlg_id), -1);
8636 list_append_string(rettv->vval.v_list, m->pattern, -1);
8637 }
8638 else
8639 {
8640 list_append_string(rettv->vval.v_list, NULL, -1);
8641 list_append_string(rettv->vval.v_list, NULL, -1);
8642 }
8643 }
8644#endif
8645 }
8646}
8647
8648/*
8649 * "matchdelete()" function
8650 */
8651 static void
8652f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8653{
8654#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008655 win_T *win = get_optional_window(argvars, 1);
8656
8657 if (win == NULL)
8658 rettv->vval.v_number = -1;
8659 else
8660 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008661 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662#endif
8663}
8664
8665/*
8666 * "matchend()" function
8667 */
8668 static void
8669f_matchend(typval_T *argvars, typval_T *rettv)
8670{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008671 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672}
8673
8674/*
8675 * "matchlist()" function
8676 */
8677 static void
8678f_matchlist(typval_T *argvars, typval_T *rettv)
8679{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008680 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681}
8682
8683/*
8684 * "matchstr()" function
8685 */
8686 static void
8687f_matchstr(typval_T *argvars, typval_T *rettv)
8688{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008689 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690}
8691
8692/*
8693 * "matchstrpos()" function
8694 */
8695 static void
8696f_matchstrpos(typval_T *argvars, typval_T *rettv)
8697{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008698 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699}
8700
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701 static void
8702max_min(typval_T *argvars, typval_T *rettv, int domax)
8703{
8704 varnumber_T n = 0;
8705 varnumber_T i;
8706 int error = FALSE;
8707
8708 if (argvars[0].v_type == VAR_LIST)
8709 {
8710 list_T *l;
8711 listitem_T *li;
8712
8713 l = argvars[0].vval.v_list;
8714 if (l != NULL)
8715 {
8716 li = l->lv_first;
8717 if (li != NULL)
8718 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008719 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008720 for (;;)
8721 {
8722 li = li->li_next;
8723 if (li == NULL)
8724 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008725 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008726 if (domax ? i > n : i < n)
8727 n = i;
8728 }
8729 }
8730 }
8731 }
8732 else if (argvars[0].v_type == VAR_DICT)
8733 {
8734 dict_T *d;
8735 int first = TRUE;
8736 hashitem_T *hi;
8737 int todo;
8738
8739 d = argvars[0].vval.v_dict;
8740 if (d != NULL)
8741 {
8742 todo = (int)d->dv_hashtab.ht_used;
8743 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8744 {
8745 if (!HASHITEM_EMPTY(hi))
8746 {
8747 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008748 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008749 if (first)
8750 {
8751 n = i;
8752 first = FALSE;
8753 }
8754 else if (domax ? i > n : i < n)
8755 n = i;
8756 }
8757 }
8758 }
8759 }
8760 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008761 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008762 rettv->vval.v_number = error ? 0 : n;
8763}
8764
8765/*
8766 * "max()" function
8767 */
8768 static void
8769f_max(typval_T *argvars, typval_T *rettv)
8770{
8771 max_min(argvars, rettv, TRUE);
8772}
8773
8774/*
8775 * "min()" function
8776 */
8777 static void
8778f_min(typval_T *argvars, typval_T *rettv)
8779{
8780 max_min(argvars, rettv, FALSE);
8781}
8782
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008783/*
8784 * Create the directory in which "dir" is located, and higher levels when
8785 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008786 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008787 */
8788 static int
8789mkdir_recurse(char_u *dir, int prot)
8790{
8791 char_u *p;
8792 char_u *updir;
8793 int r = FAIL;
8794
8795 /* Get end of directory name in "dir".
8796 * We're done when it's "/" or "c:/". */
8797 p = gettail_sep(dir);
8798 if (p <= get_past_head(dir))
8799 return OK;
8800
8801 /* If the directory exists we're done. Otherwise: create it.*/
8802 updir = vim_strnsave(dir, (int)(p - dir));
8803 if (updir == NULL)
8804 return FAIL;
8805 if (mch_isdir(updir))
8806 r = OK;
8807 else if (mkdir_recurse(updir, prot) == OK)
8808 r = vim_mkdir_emsg(updir, prot);
8809 vim_free(updir);
8810 return r;
8811}
8812
8813#ifdef vim_mkdir
8814/*
8815 * "mkdir()" function
8816 */
8817 static void
8818f_mkdir(typval_T *argvars, typval_T *rettv)
8819{
8820 char_u *dir;
8821 char_u buf[NUMBUFLEN];
8822 int prot = 0755;
8823
8824 rettv->vval.v_number = FAIL;
8825 if (check_restricted() || check_secure())
8826 return;
8827
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008828 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008829 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008830 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008832 if (*gettail(dir) == NUL)
8833 /* remove trailing slashes */
8834 *gettail_sep(dir) = NUL;
8835
8836 if (argvars[1].v_type != VAR_UNKNOWN)
8837 {
8838 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008840 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008841 if (prot == -1)
8842 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008843 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008844 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008845 {
8846 if (mch_isdir(dir))
8847 {
8848 /* With the "p" flag it's OK if the dir already exists. */
8849 rettv->vval.v_number = OK;
8850 return;
8851 }
8852 mkdir_recurse(dir, prot);
8853 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008854 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008855 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008856}
8857#endif
8858
8859/*
8860 * "mode()" function
8861 */
8862 static void
8863f_mode(typval_T *argvars, typval_T *rettv)
8864{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008865 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008866
Bram Moolenaar612cc382018-07-29 15:34:26 +02008867 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008868
8869 if (time_for_testing == 93784)
8870 {
8871 /* Testing the two-character code. */
8872 buf[0] = 'x';
8873 buf[1] = '!';
8874 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008875#ifdef FEAT_TERMINAL
8876 else if (term_use_loop())
8877 buf[0] = 't';
8878#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008879 else if (VIsual_active)
8880 {
8881 if (VIsual_select)
8882 buf[0] = VIsual_mode + 's' - 'v';
8883 else
8884 buf[0] = VIsual_mode;
8885 }
8886 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8887 || State == CONFIRM)
8888 {
8889 buf[0] = 'r';
8890 if (State == ASKMORE)
8891 buf[1] = 'm';
8892 else if (State == CONFIRM)
8893 buf[1] = '?';
8894 }
8895 else if (State == EXTERNCMD)
8896 buf[0] = '!';
8897 else if (State & INSERT)
8898 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008899 if (State & VREPLACE_FLAG)
8900 {
8901 buf[0] = 'R';
8902 buf[1] = 'v';
8903 }
8904 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008905 {
8906 if (State & REPLACE_FLAG)
8907 buf[0] = 'R';
8908 else
8909 buf[0] = 'i';
8910#ifdef FEAT_INS_EXPAND
8911 if (ins_compl_active())
8912 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008913 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008914 buf[1] = 'x';
8915#endif
8916 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008918 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008919 {
8920 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008921 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008922 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008923 else if (exmode_active == EXMODE_NORMAL)
8924 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008925 }
8926 else
8927 {
8928 buf[0] = 'n';
8929 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008930 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008931 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008932 // to be able to detect force-linewise/blockwise/characterwise operations
8933 buf[2] = motion_force;
8934 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008935 else if (restart_edit == 'I' || restart_edit == 'R'
8936 || restart_edit == 'V')
8937 {
8938 buf[1] = 'i';
8939 buf[2] = restart_edit;
8940 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008941 }
8942
8943 /* Clear out the minor mode when the argument is not a non-zero number or
8944 * non-empty string. */
8945 if (!non_zero_arg(&argvars[0]))
8946 buf[1] = NUL;
8947
8948 rettv->vval.v_string = vim_strsave(buf);
8949 rettv->v_type = VAR_STRING;
8950}
8951
8952#if defined(FEAT_MZSCHEME) || defined(PROTO)
8953/*
8954 * "mzeval()" function
8955 */
8956 static void
8957f_mzeval(typval_T *argvars, typval_T *rettv)
8958{
8959 char_u *str;
8960 char_u buf[NUMBUFLEN];
8961
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008962 if (check_restricted() || check_secure())
8963 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008964 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008965 do_mzeval(str, rettv);
8966}
8967
8968 void
8969mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8970{
8971 typval_T argvars[3];
8972
8973 argvars[0].v_type = VAR_STRING;
8974 argvars[0].vval.v_string = name;
8975 copy_tv(args, &argvars[1]);
8976 argvars[2].v_type = VAR_UNKNOWN;
8977 f_call(argvars, rettv);
8978 clear_tv(&argvars[1]);
8979}
8980#endif
8981
8982/*
8983 * "nextnonblank()" function
8984 */
8985 static void
8986f_nextnonblank(typval_T *argvars, typval_T *rettv)
8987{
8988 linenr_T lnum;
8989
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008990 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008991 {
8992 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8993 {
8994 lnum = 0;
8995 break;
8996 }
8997 if (*skipwhite(ml_get(lnum)) != NUL)
8998 break;
8999 }
9000 rettv->vval.v_number = lnum;
9001}
9002
9003/*
9004 * "nr2char()" function
9005 */
9006 static void
9007f_nr2char(typval_T *argvars, typval_T *rettv)
9008{
9009 char_u buf[NUMBUFLEN];
9010
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009011 if (has_mbyte)
9012 {
9013 int utf8 = 0;
9014
9015 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009016 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009018 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009020 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009021 }
9022 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009024 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009025 buf[1] = NUL;
9026 }
9027 rettv->v_type = VAR_STRING;
9028 rettv->vval.v_string = vim_strsave(buf);
9029}
9030
9031/*
9032 * "or(expr, expr)" function
9033 */
9034 static void
9035f_or(typval_T *argvars, typval_T *rettv)
9036{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009037 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9038 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009039}
9040
9041/*
9042 * "pathshorten()" function
9043 */
9044 static void
9045f_pathshorten(typval_T *argvars, typval_T *rettv)
9046{
9047 char_u *p;
9048
9049 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009050 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009051 if (p == NULL)
9052 rettv->vval.v_string = NULL;
9053 else
9054 {
9055 p = vim_strsave(p);
9056 rettv->vval.v_string = p;
9057 if (p != NULL)
9058 shorten_dir(p);
9059 }
9060}
9061
9062#ifdef FEAT_PERL
9063/*
9064 * "perleval()" function
9065 */
9066 static void
9067f_perleval(typval_T *argvars, typval_T *rettv)
9068{
9069 char_u *str;
9070 char_u buf[NUMBUFLEN];
9071
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009072 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009073 do_perleval(str, rettv);
9074}
9075#endif
9076
9077#ifdef FEAT_FLOAT
9078/*
9079 * "pow()" function
9080 */
9081 static void
9082f_pow(typval_T *argvars, typval_T *rettv)
9083{
9084 float_T fx = 0.0, fy = 0.0;
9085
9086 rettv->v_type = VAR_FLOAT;
9087 if (get_float_arg(argvars, &fx) == OK
9088 && get_float_arg(&argvars[1], &fy) == OK)
9089 rettv->vval.v_float = pow(fx, fy);
9090 else
9091 rettv->vval.v_float = 0.0;
9092}
9093#endif
9094
9095/*
9096 * "prevnonblank()" function
9097 */
9098 static void
9099f_prevnonblank(typval_T *argvars, typval_T *rettv)
9100{
9101 linenr_T lnum;
9102
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009103 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009104 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9105 lnum = 0;
9106 else
9107 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9108 --lnum;
9109 rettv->vval.v_number = lnum;
9110}
9111
9112/* This dummy va_list is here because:
9113 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9114 * - locally in the function results in a "used before set" warning
9115 * - using va_start() to initialize it gives "function with fixed args" error */
9116static va_list ap;
9117
9118/*
9119 * "printf()" function
9120 */
9121 static void
9122f_printf(typval_T *argvars, typval_T *rettv)
9123{
9124 char_u buf[NUMBUFLEN];
9125 int len;
9126 char_u *s;
9127 int saved_did_emsg = did_emsg;
9128 char *fmt;
9129
9130 rettv->v_type = VAR_STRING;
9131 rettv->vval.v_string = NULL;
9132
9133 /* Get the required length, allocate the buffer and do it for real. */
9134 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009135 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009136 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009137 if (!did_emsg)
9138 {
9139 s = alloc(len + 1);
9140 if (s != NULL)
9141 {
9142 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009143 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9144 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145 }
9146 }
9147 did_emsg |= saved_did_emsg;
9148}
9149
Bram Moolenaarf2732452018-06-03 14:47:35 +02009150#ifdef FEAT_JOB_CHANNEL
9151/*
9152 * "prompt_setcallback({buffer}, {callback})" function
9153 */
9154 static void
9155f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9156{
9157 buf_T *buf;
9158 char_u *callback;
9159 partial_T *partial;
9160
9161 if (check_secure())
9162 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009163 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009164 if (buf == NULL)
9165 return;
9166
9167 callback = get_callback(&argvars[1], &partial);
9168 if (callback == NULL)
9169 return;
9170
9171 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
9172 if (partial == NULL)
9173 buf->b_prompt_callback = vim_strsave(callback);
9174 else
9175 /* pointer into the partial */
9176 buf->b_prompt_callback = callback;
9177 buf->b_prompt_partial = partial;
9178}
9179
9180/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009181 * "prompt_setinterrupt({buffer}, {callback})" function
9182 */
9183 static void
9184f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9185{
9186 buf_T *buf;
9187 char_u *callback;
9188 partial_T *partial;
9189
9190 if (check_secure())
9191 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009192 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009193 if (buf == NULL)
9194 return;
9195
9196 callback = get_callback(&argvars[1], &partial);
9197 if (callback == NULL)
9198 return;
9199
9200 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9201 if (partial == NULL)
9202 buf->b_prompt_interrupt = vim_strsave(callback);
9203 else
9204 /* pointer into the partial */
9205 buf->b_prompt_interrupt = callback;
9206 buf->b_prompt_int_partial = partial;
9207}
9208
9209/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009210 * "prompt_setprompt({buffer}, {text})" function
9211 */
9212 static void
9213f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9214{
9215 buf_T *buf;
9216 char_u *text;
9217
9218 if (check_secure())
9219 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009220 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009221 if (buf == NULL)
9222 return;
9223
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009224 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009225 vim_free(buf->b_prompt_text);
9226 buf->b_prompt_text = vim_strsave(text);
9227}
9228#endif
9229
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009230/*
9231 * "pumvisible()" function
9232 */
9233 static void
9234f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9235{
9236#ifdef FEAT_INS_EXPAND
9237 if (pum_visible())
9238 rettv->vval.v_number = 1;
9239#endif
9240}
9241
9242#ifdef FEAT_PYTHON3
9243/*
9244 * "py3eval()" function
9245 */
9246 static void
9247f_py3eval(typval_T *argvars, typval_T *rettv)
9248{
9249 char_u *str;
9250 char_u buf[NUMBUFLEN];
9251
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009252 if (check_restricted() || check_secure())
9253 return;
9254
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009255 if (p_pyx == 0)
9256 p_pyx = 3;
9257
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009258 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009259 do_py3eval(str, rettv);
9260}
9261#endif
9262
9263#ifdef FEAT_PYTHON
9264/*
9265 * "pyeval()" function
9266 */
9267 static void
9268f_pyeval(typval_T *argvars, typval_T *rettv)
9269{
9270 char_u *str;
9271 char_u buf[NUMBUFLEN];
9272
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009273 if (check_restricted() || check_secure())
9274 return;
9275
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009276 if (p_pyx == 0)
9277 p_pyx = 2;
9278
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009279 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009280 do_pyeval(str, rettv);
9281}
9282#endif
9283
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009284#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9285/*
9286 * "pyxeval()" function
9287 */
9288 static void
9289f_pyxeval(typval_T *argvars, typval_T *rettv)
9290{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009291 if (check_restricted() || check_secure())
9292 return;
9293
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009294# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9295 init_pyxversion();
9296 if (p_pyx == 2)
9297 f_pyeval(argvars, rettv);
9298 else
9299 f_py3eval(argvars, rettv);
9300# elif defined(FEAT_PYTHON)
9301 f_pyeval(argvars, rettv);
9302# elif defined(FEAT_PYTHON3)
9303 f_py3eval(argvars, rettv);
9304# endif
9305}
9306#endif
9307
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009308/*
9309 * "range()" function
9310 */
9311 static void
9312f_range(typval_T *argvars, typval_T *rettv)
9313{
9314 varnumber_T start;
9315 varnumber_T end;
9316 varnumber_T stride = 1;
9317 varnumber_T i;
9318 int error = FALSE;
9319
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009320 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009321 if (argvars[1].v_type == VAR_UNKNOWN)
9322 {
9323 end = start - 1;
9324 start = 0;
9325 }
9326 else
9327 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009328 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009329 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009330 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009331 }
9332
9333 if (error)
9334 return; /* type error; errmsg already given */
9335 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009336 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009337 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009338 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009339 else
9340 {
9341 if (rettv_list_alloc(rettv) == OK)
9342 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9343 if (list_append_number(rettv->vval.v_list,
9344 (varnumber_T)i) == FAIL)
9345 break;
9346 }
9347}
9348
9349/*
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009350 * Evaluate "expr" for readdir().
9351 */
9352 static int
9353readdir_checkitem(typval_T *expr, char_u *name)
9354{
9355 typval_T save_val;
9356 typval_T rettv;
9357 typval_T argv[2];
9358 int retval = 0;
9359 int error = FALSE;
9360
9361 prepare_vimvar(VV_VAL, &save_val);
9362 set_vim_var_string(VV_VAL, name, -1);
9363 argv[0].v_type = VAR_STRING;
9364 argv[0].vval.v_string = name;
9365
9366 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9367 goto theend;
9368
9369 retval = tv_get_number_chk(&rettv, &error);
9370 if (error)
9371 retval = -1;
9372 clear_tv(&rettv);
9373
9374theend:
9375 set_vim_var_string(VV_VAL, NULL, 0);
9376 restore_vimvar(VV_VAL, &save_val);
9377 return retval;
9378}
9379
9380/*
9381 * "readdir()" function
9382 */
9383 static void
9384f_readdir(typval_T *argvars, typval_T *rettv)
9385{
9386 typval_T *expr;
9387 int failed = FALSE;
9388 char_u *path;
9389 garray_T ga;
9390 int i;
9391#ifdef MSWIN
9392 char_u *buf, *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009393 int ok;
9394 HANDLE hFind = INVALID_HANDLE_VALUE;
9395 WIN32_FIND_DATAW wfb;
9396 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
9397#endif
9398
9399 if (rettv_list_alloc(rettv) == FAIL)
9400 return;
9401 path = tv_get_string(&argvars[0]);
9402 expr = &argvars[1];
9403 ga_init2(&ga, (int)sizeof(char *), 20);
9404
9405#ifdef MSWIN
9406 buf = alloc((int)MAXPATHL);
9407 if (buf == NULL)
9408 return;
9409 STRNCPY(buf, path, MAXPATHL-5);
9410 p = vim_strpbrk(path, (char_u *)"\\/");
9411 if (p != NULL)
9412 *p = NUL;
9413 STRCAT(buf, "\\*");
9414
9415 wn = enc_to_utf16(buf, NULL);
9416 if (wn != NULL)
9417 hFind = FindFirstFileW(wn, &wfb);
9418 ok = (hFind != INVALID_HANDLE_VALUE);
9419 if (!ok)
9420 smsg(_(e_notopen), path);
9421 else
9422 {
9423 while (ok)
9424 {
9425 int ignore;
9426
9427 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
9428 if (p == NULL)
9429 break; // out of memory
9430
9431 ignore = p[0] == '.' && (p[1] == NUL
9432 || (p[1] == '.' && p[2] == NUL));
9433 if (!ignore && expr->v_type != VAR_UNKNOWN)
9434 {
9435 int r = readdir_checkitem(expr, p);
9436
9437 if (r < 0)
9438 {
9439 vim_free(p);
9440 break;
9441 }
9442 if (r == 0)
9443 ignore = TRUE;
9444 }
9445
9446 if (!ignore)
9447 {
9448 if (ga_grow(&ga, 1) == OK)
9449 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9450 else
9451 {
9452 failed = TRUE;
9453 vim_free(p);
9454 break;
9455 }
9456 }
9457
9458 vim_free(p);
9459 ok = FindNextFileW(hFind, &wfb);
9460 }
9461 FindClose(hFind);
9462 }
9463
9464 vim_free(buf);
9465 vim_free(wn);
9466#else
9467 DIR *dirp;
9468 struct dirent *dp;
9469 char_u *p;
9470
9471 dirp = opendir((char *)path);
9472 if (dirp == NULL)
9473 smsg(_(e_notopen), path);
9474 else
9475 {
9476 for (;;)
9477 {
9478 int ignore;
9479
9480 dp = readdir(dirp);
9481 if (dp == NULL)
9482 break;
9483 p = (char_u *)dp->d_name;
9484
9485 ignore = p[0] == '.' &&
9486 (p[1] == NUL ||
9487 (p[1] == '.' && p[2] == NUL));
9488 if (!ignore && expr->v_type != VAR_UNKNOWN)
9489 {
9490 int r = readdir_checkitem(expr, p);
9491
9492 if (r < 0)
9493 break;
9494 if (r == 0)
9495 ignore = TRUE;
9496 }
9497
9498 if (!ignore)
9499 {
9500 if (ga_grow(&ga, 1) == OK)
9501 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9502 else
9503 {
9504 failed = TRUE;
9505 break;
9506 }
9507 }
9508 }
9509
9510 closedir(dirp);
9511 }
9512#endif
9513
Bram Moolenaar334ad412019-04-19 15:20:46 +02009514 if (!failed && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009515 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009516 sort_strings((char_u **)ga.ga_data, ga.ga_len);
9517 for (i = 0; i < ga.ga_len; i++)
9518 {
9519 p = ((char_u **)ga.ga_data)[i];
9520 list_append_string(rettv->vval.v_list, p, -1);
9521 }
9522 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009523 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009524}
9525
9526/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009527 * "readfile()" function
9528 */
9529 static void
9530f_readfile(typval_T *argvars, typval_T *rettv)
9531{
9532 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009533 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009534 int failed = FALSE;
9535 char_u *fname;
9536 FILE *fd;
9537 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9538 int io_size = sizeof(buf);
9539 int readlen; /* size of last fread() */
9540 char_u *prev = NULL; /* previously read bytes, if any */
9541 long prevlen = 0; /* length of data in prev */
9542 long prevsize = 0; /* size of prev buffer */
9543 long maxline = MAXLNUM;
9544 long cnt = 0;
9545 char_u *p; /* position in buf */
9546 char_u *start; /* start of current line */
9547
9548 if (argvars[1].v_type != VAR_UNKNOWN)
9549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009550 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009551 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009552 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9553 blob = TRUE;
9554
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009555 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009556 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009557 }
9558
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009559 if (blob)
9560 {
9561 if (rettv_blob_alloc(rettv) == FAIL)
9562 return;
9563 }
9564 else
9565 {
9566 if (rettv_list_alloc(rettv) == FAIL)
9567 return;
9568 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009569
9570 /* Always open the file in binary mode, library functions have a mind of
9571 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009572 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009573 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9574 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009575 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009576 return;
9577 }
9578
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009579 if (blob)
9580 {
9581 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9582 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009583 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009584 blob_free(rettv->vval.v_blob);
9585 }
9586 fclose(fd);
9587 return;
9588 }
9589
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009590 while (cnt < maxline || maxline < 0)
9591 {
9592 readlen = (int)fread(buf, 1, io_size, fd);
9593
9594 /* This for loop processes what was read, but is also entered at end
9595 * of file so that either:
9596 * - an incomplete line gets written
9597 * - a "binary" file gets an empty line at the end if it ends in a
9598 * newline. */
9599 for (p = buf, start = buf;
9600 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9601 ++p)
9602 {
9603 if (*p == '\n' || readlen <= 0)
9604 {
9605 listitem_T *li;
9606 char_u *s = NULL;
9607 long_u len = p - start;
9608
9609 /* Finished a line. Remove CRs before NL. */
9610 if (readlen > 0 && !binary)
9611 {
9612 while (len > 0 && start[len - 1] == '\r')
9613 --len;
9614 /* removal may cross back to the "prev" string */
9615 if (len == 0)
9616 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9617 --prevlen;
9618 }
9619 if (prevlen == 0)
9620 s = vim_strnsave(start, (int)len);
9621 else
9622 {
9623 /* Change "prev" buffer to be the right size. This way
9624 * the bytes are only copied once, and very long lines are
9625 * allocated only once. */
9626 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9627 {
9628 mch_memmove(s + prevlen, start, len);
9629 s[prevlen + len] = NUL;
9630 prev = NULL; /* the list will own the string */
9631 prevlen = prevsize = 0;
9632 }
9633 }
9634 if (s == NULL)
9635 {
9636 do_outofmem_msg((long_u) prevlen + len + 1);
9637 failed = TRUE;
9638 break;
9639 }
9640
9641 if ((li = listitem_alloc()) == NULL)
9642 {
9643 vim_free(s);
9644 failed = TRUE;
9645 break;
9646 }
9647 li->li_tv.v_type = VAR_STRING;
9648 li->li_tv.v_lock = 0;
9649 li->li_tv.vval.v_string = s;
9650 list_append(rettv->vval.v_list, li);
9651
9652 start = p + 1; /* step over newline */
9653 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9654 break;
9655 }
9656 else if (*p == NUL)
9657 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009658 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9659 * when finding the BF and check the previous two bytes. */
9660 else if (*p == 0xbf && enc_utf8 && !binary)
9661 {
9662 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9663 * + 1, these may be in the "prev" string. */
9664 char_u back1 = p >= buf + 1 ? p[-1]
9665 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9666 char_u back2 = p >= buf + 2 ? p[-2]
9667 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9668 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9669
9670 if (back2 == 0xef && back1 == 0xbb)
9671 {
9672 char_u *dest = p - 2;
9673
9674 /* Usually a BOM is at the beginning of a file, and so at
9675 * the beginning of a line; then we can just step over it.
9676 */
9677 if (start == dest)
9678 start = p + 1;
9679 else
9680 {
9681 /* have to shuffle buf to close gap */
9682 int adjust_prevlen = 0;
9683
9684 if (dest < buf)
9685 {
9686 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9687 dest = buf;
9688 }
9689 if (readlen > p - buf + 1)
9690 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9691 readlen -= 3 - adjust_prevlen;
9692 prevlen -= adjust_prevlen;
9693 p = dest - 1;
9694 }
9695 }
9696 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009697 } /* for */
9698
9699 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9700 break;
9701 if (start < p)
9702 {
9703 /* There's part of a line in buf, store it in "prev". */
9704 if (p - start + prevlen >= prevsize)
9705 {
9706 /* need bigger "prev" buffer */
9707 char_u *newprev;
9708
9709 /* A common use case is ordinary text files and "prev" gets a
9710 * fragment of a line, so the first allocation is made
9711 * small, to avoid repeatedly 'allocing' large and
9712 * 'reallocing' small. */
9713 if (prevsize == 0)
9714 prevsize = (long)(p - start);
9715 else
9716 {
9717 long grow50pc = (prevsize * 3) / 2;
9718 long growmin = (long)((p - start) * 2 + prevlen);
9719 prevsize = grow50pc > growmin ? grow50pc : growmin;
9720 }
9721 newprev = prev == NULL ? alloc(prevsize)
9722 : vim_realloc(prev, prevsize);
9723 if (newprev == NULL)
9724 {
9725 do_outofmem_msg((long_u)prevsize);
9726 failed = TRUE;
9727 break;
9728 }
9729 prev = newprev;
9730 }
9731 /* Add the line part to end of "prev". */
9732 mch_memmove(prev + prevlen, start, p - start);
9733 prevlen += (long)(p - start);
9734 }
9735 } /* while */
9736
9737 /*
9738 * For a negative line count use only the lines at the end of the file,
9739 * free the rest.
9740 */
9741 if (!failed && maxline < 0)
9742 while (cnt > -maxline)
9743 {
9744 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9745 --cnt;
9746 }
9747
9748 if (failed)
9749 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009750 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009751 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009752 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009753 }
9754
9755 vim_free(prev);
9756 fclose(fd);
9757}
9758
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009759 static void
9760return_register(int regname, typval_T *rettv)
9761{
9762 char_u buf[2] = {0, 0};
9763
9764 buf[0] = (char_u)regname;
9765 rettv->v_type = VAR_STRING;
9766 rettv->vval.v_string = vim_strsave(buf);
9767}
9768
9769/*
9770 * "reg_executing()" function
9771 */
9772 static void
9773f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9774{
9775 return_register(reg_executing, rettv);
9776}
9777
9778/*
9779 * "reg_recording()" function
9780 */
9781 static void
9782f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9783{
9784 return_register(reg_recording, rettv);
9785}
9786
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009787#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009788/*
9789 * Convert a List to proftime_T.
9790 * Return FAIL when there is something wrong.
9791 */
9792 static int
9793list2proftime(typval_T *arg, proftime_T *tm)
9794{
9795 long n1, n2;
9796 int error = FALSE;
9797
9798 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9799 || arg->vval.v_list->lv_len != 2)
9800 return FAIL;
9801 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9802 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009803# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009804 tm->HighPart = n1;
9805 tm->LowPart = n2;
9806# else
9807 tm->tv_sec = n1;
9808 tm->tv_usec = n2;
9809# endif
9810 return error ? FAIL : OK;
9811}
9812#endif /* FEAT_RELTIME */
9813
9814/*
9815 * "reltime()" function
9816 */
9817 static void
9818f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9819{
9820#ifdef FEAT_RELTIME
9821 proftime_T res;
9822 proftime_T start;
9823
9824 if (argvars[0].v_type == VAR_UNKNOWN)
9825 {
9826 /* No arguments: get current time. */
9827 profile_start(&res);
9828 }
9829 else if (argvars[1].v_type == VAR_UNKNOWN)
9830 {
9831 if (list2proftime(&argvars[0], &res) == FAIL)
9832 return;
9833 profile_end(&res);
9834 }
9835 else
9836 {
9837 /* Two arguments: compute the difference. */
9838 if (list2proftime(&argvars[0], &start) == FAIL
9839 || list2proftime(&argvars[1], &res) == FAIL)
9840 return;
9841 profile_sub(&res, &start);
9842 }
9843
9844 if (rettv_list_alloc(rettv) == OK)
9845 {
9846 long n1, n2;
9847
Bram Moolenaar4f974752019-02-17 17:44:42 +01009848# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009849 n1 = res.HighPart;
9850 n2 = res.LowPart;
9851# else
9852 n1 = res.tv_sec;
9853 n2 = res.tv_usec;
9854# endif
9855 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9856 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9857 }
9858#endif
9859}
9860
9861#ifdef FEAT_FLOAT
9862/*
9863 * "reltimefloat()" function
9864 */
9865 static void
9866f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9867{
9868# ifdef FEAT_RELTIME
9869 proftime_T tm;
9870# endif
9871
9872 rettv->v_type = VAR_FLOAT;
9873 rettv->vval.v_float = 0;
9874# ifdef FEAT_RELTIME
9875 if (list2proftime(&argvars[0], &tm) == OK)
9876 rettv->vval.v_float = profile_float(&tm);
9877# endif
9878}
9879#endif
9880
9881/*
9882 * "reltimestr()" function
9883 */
9884 static void
9885f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9886{
9887#ifdef FEAT_RELTIME
9888 proftime_T tm;
9889#endif
9890
9891 rettv->v_type = VAR_STRING;
9892 rettv->vval.v_string = NULL;
9893#ifdef FEAT_RELTIME
9894 if (list2proftime(&argvars[0], &tm) == OK)
9895 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9896#endif
9897}
9898
9899#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009900 static void
9901make_connection(void)
9902{
9903 if (X_DISPLAY == NULL
9904# ifdef FEAT_GUI
9905 && !gui.in_use
9906# endif
9907 )
9908 {
9909 x_force_connect = TRUE;
9910 setup_term_clip();
9911 x_force_connect = FALSE;
9912 }
9913}
9914
9915 static int
9916check_connection(void)
9917{
9918 make_connection();
9919 if (X_DISPLAY == NULL)
9920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009921 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009922 return FAIL;
9923 }
9924 return OK;
9925}
9926#endif
9927
9928#ifdef FEAT_CLIENTSERVER
9929 static void
9930remote_common(typval_T *argvars, typval_T *rettv, int expr)
9931{
9932 char_u *server_name;
9933 char_u *keys;
9934 char_u *r = NULL;
9935 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009936 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009937# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009938 HWND w;
9939# else
9940 Window w;
9941# endif
9942
9943 if (check_restricted() || check_secure())
9944 return;
9945
9946# ifdef FEAT_X11
9947 if (check_connection() == FAIL)
9948 return;
9949# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009950 if (argvars[2].v_type != VAR_UNKNOWN
9951 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009952 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009953
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009954 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009955 if (server_name == NULL)
9956 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009957 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009958# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009959 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009960# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009961 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9962 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009963# endif
9964 {
9965 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009967 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009968 vim_free(r);
9969 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009970 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009971 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009972 return;
9973 }
9974
9975 rettv->vval.v_string = r;
9976
9977 if (argvars[2].v_type != VAR_UNKNOWN)
9978 {
9979 dictitem_T v;
9980 char_u str[30];
9981 char_u *idvar;
9982
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009983 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009984 if (idvar != NULL && *idvar != NUL)
9985 {
9986 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9987 v.di_tv.v_type = VAR_STRING;
9988 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009989 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009990 vim_free(v.di_tv.vval.v_string);
9991 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009992 }
9993}
9994#endif
9995
9996/*
9997 * "remote_expr()" function
9998 */
9999 static void
10000f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
10001{
10002 rettv->v_type = VAR_STRING;
10003 rettv->vval.v_string = NULL;
10004#ifdef FEAT_CLIENTSERVER
10005 remote_common(argvars, rettv, TRUE);
10006#endif
10007}
10008
10009/*
10010 * "remote_foreground()" function
10011 */
10012 static void
10013f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10014{
10015#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010016# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010017 /* On Win32 it's done in this application. */
10018 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010019 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010020
10021 if (server_name != NULL)
10022 serverForeground(server_name);
10023 }
10024# else
10025 /* Send a foreground() expression to the server. */
10026 argvars[1].v_type = VAR_STRING;
10027 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
10028 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +020010029 rettv->v_type = VAR_STRING;
10030 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010031 remote_common(argvars, rettv, TRUE);
10032 vim_free(argvars[1].vval.v_string);
10033# endif
10034#endif
10035}
10036
10037 static void
10038f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
10039{
10040#ifdef FEAT_CLIENTSERVER
10041 dictitem_T v;
10042 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010043# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010044 long_u n = 0;
10045# endif
10046 char_u *serverid;
10047
10048 if (check_restricted() || check_secure())
10049 {
10050 rettv->vval.v_number = -1;
10051 return;
10052 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010053 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010054 if (serverid == NULL)
10055 {
10056 rettv->vval.v_number = -1;
10057 return; /* type error; errmsg already given */
10058 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010059# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010060 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
10061 if (n == 0)
10062 rettv->vval.v_number = -1;
10063 else
10064 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010065 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010066 rettv->vval.v_number = (s != NULL);
10067 }
10068# else
10069 if (check_connection() == FAIL)
10070 return;
10071
10072 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10073 serverStrToWin(serverid), &s);
10074# endif
10075
10076 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10077 {
10078 char_u *retvar;
10079
10080 v.di_tv.v_type = VAR_STRING;
10081 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010082 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010083 if (retvar != NULL)
10084 set_var(retvar, &v.di_tv, FALSE);
10085 vim_free(v.di_tv.vval.v_string);
10086 }
10087#else
10088 rettv->vval.v_number = -1;
10089#endif
10090}
10091
10092 static void
10093f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
10094{
10095 char_u *r = NULL;
10096
10097#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010098 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010099
10100 if (serverid != NULL && !check_restricted() && !check_secure())
10101 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010102 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010103# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010104 /* The server's HWND is encoded in the 'id' parameter */
10105 long_u n = 0;
10106# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010107
10108 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010109 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010110
Bram Moolenaar4f974752019-02-17 17:44:42 +010010111# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010112 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10113 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010114 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010115 if (r == NULL)
10116# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010117 if (check_connection() == FAIL
10118 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10119 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010120# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010121 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010122 }
10123#endif
10124 rettv->v_type = VAR_STRING;
10125 rettv->vval.v_string = r;
10126}
10127
10128/*
10129 * "remote_send()" function
10130 */
10131 static void
10132f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10133{
10134 rettv->v_type = VAR_STRING;
10135 rettv->vval.v_string = NULL;
10136#ifdef FEAT_CLIENTSERVER
10137 remote_common(argvars, rettv, FALSE);
10138#endif
10139}
10140
10141/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010142 * "remote_startserver()" function
10143 */
10144 static void
10145f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10146{
10147#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010148 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010149
10150 if (server == NULL)
10151 return; /* type error; errmsg already given */
10152 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010153 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010154 else
10155 {
10156# ifdef FEAT_X11
10157 if (check_connection() == OK)
10158 serverRegisterName(X_DISPLAY, server);
10159# else
10160 serverSetName(server);
10161# endif
10162 }
10163#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010164 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010165#endif
10166}
10167
10168/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010169 * "remove()" function
10170 */
10171 static void
10172f_remove(typval_T *argvars, typval_T *rettv)
10173{
10174 list_T *l;
10175 listitem_T *item, *item2;
10176 listitem_T *li;
10177 long idx;
10178 long end;
10179 char_u *key;
10180 dict_T *d;
10181 dictitem_T *di;
10182 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010183 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010184
10185 if (argvars[0].v_type == VAR_DICT)
10186 {
10187 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010188 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010189 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010190 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010191 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010192 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010193 if (key != NULL)
10194 {
10195 di = dict_find(d, key, -1);
10196 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010197 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010198 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10199 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10200 {
10201 *rettv = di->di_tv;
10202 init_tv(&di->di_tv);
10203 dictitem_remove(d, di);
10204 }
10205 }
10206 }
10207 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010208 else if (argvars[0].v_type == VAR_BLOB)
10209 {
10210 idx = (long)tv_get_number_chk(&argvars[1], &error);
10211 if (!error)
10212 {
10213 blob_T *b = argvars[0].vval.v_blob;
10214 int len = blob_len(b);
10215 char_u *p;
10216
10217 if (idx < 0)
10218 // count from the end
10219 idx = len + idx;
10220 if (idx < 0 || idx >= len)
10221 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010222 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010223 return;
10224 }
10225 if (argvars[2].v_type == VAR_UNKNOWN)
10226 {
10227 // Remove one item, return its value.
10228 p = (char_u *)b->bv_ga.ga_data;
10229 rettv->vval.v_number = (varnumber_T) *(p + idx);
10230 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10231 --b->bv_ga.ga_len;
10232 }
10233 else
10234 {
10235 blob_T *blob;
10236
10237 // Remove range of items, return list with values.
10238 end = (long)tv_get_number_chk(&argvars[2], &error);
10239 if (error)
10240 return;
10241 if (end < 0)
10242 // count from the end
10243 end = len + end;
10244 if (end >= len || idx > end)
10245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010246 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010247 return;
10248 }
10249 blob = blob_alloc();
10250 if (blob == NULL)
10251 return;
10252 blob->bv_ga.ga_len = end - idx + 1;
10253 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10254 {
10255 vim_free(blob);
10256 return;
10257 }
10258 p = (char_u *)b->bv_ga.ga_data;
10259 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10260 (size_t)(end - idx + 1));
10261 ++blob->bv_refcount;
10262 rettv->v_type = VAR_BLOB;
10263 rettv->vval.v_blob = blob;
10264
10265 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10266 b->bv_ga.ga_len -= end - idx + 1;
10267 }
10268 }
10269 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010270 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010271 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010272 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010273 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010274 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010275 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010276 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010277 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010278 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010279 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010280 else
10281 {
10282 if (argvars[2].v_type == VAR_UNKNOWN)
10283 {
10284 /* Remove one item, return its value. */
10285 vimlist_remove(l, item, item);
10286 *rettv = item->li_tv;
10287 vim_free(item);
10288 }
10289 else
10290 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010291 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010292 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010293 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010294 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010295 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010296 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010297 else
10298 {
10299 int cnt = 0;
10300
10301 for (li = item; li != NULL; li = li->li_next)
10302 {
10303 ++cnt;
10304 if (li == item2)
10305 break;
10306 }
10307 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010308 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010309 else
10310 {
10311 vimlist_remove(l, item, item2);
10312 if (rettv_list_alloc(rettv) == OK)
10313 {
10314 l = rettv->vval.v_list;
10315 l->lv_first = item;
10316 l->lv_last = item2;
10317 item->li_prev = NULL;
10318 item2->li_next = NULL;
10319 l->lv_len = cnt;
10320 }
10321 }
10322 }
10323 }
10324 }
10325 }
10326}
10327
10328/*
10329 * "rename({from}, {to})" function
10330 */
10331 static void
10332f_rename(typval_T *argvars, typval_T *rettv)
10333{
10334 char_u buf[NUMBUFLEN];
10335
10336 if (check_restricted() || check_secure())
10337 rettv->vval.v_number = -1;
10338 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010339 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10340 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010341}
10342
10343/*
10344 * "repeat()" function
10345 */
10346 static void
10347f_repeat(typval_T *argvars, typval_T *rettv)
10348{
10349 char_u *p;
10350 int n;
10351 int slen;
10352 int len;
10353 char_u *r;
10354 int i;
10355
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010356 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010357 if (argvars[0].v_type == VAR_LIST)
10358 {
10359 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10360 while (n-- > 0)
10361 if (list_extend(rettv->vval.v_list,
10362 argvars[0].vval.v_list, NULL) == FAIL)
10363 break;
10364 }
10365 else
10366 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010367 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010368 rettv->v_type = VAR_STRING;
10369 rettv->vval.v_string = NULL;
10370
10371 slen = (int)STRLEN(p);
10372 len = slen * n;
10373 if (len <= 0)
10374 return;
10375
10376 r = alloc(len + 1);
10377 if (r != NULL)
10378 {
10379 for (i = 0; i < n; i++)
10380 mch_memmove(r + i * slen, p, (size_t)slen);
10381 r[len] = NUL;
10382 }
10383
10384 rettv->vval.v_string = r;
10385 }
10386}
10387
10388/*
10389 * "resolve()" function
10390 */
10391 static void
10392f_resolve(typval_T *argvars, typval_T *rettv)
10393{
10394 char_u *p;
10395#ifdef HAVE_READLINK
10396 char_u *buf = NULL;
10397#endif
10398
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010399 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010400#ifdef FEAT_SHORTCUT
10401 {
10402 char_u *v = NULL;
10403
Bram Moolenaardce1e892019-02-10 23:18:53 +010010404 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010405 if (v != NULL)
10406 rettv->vval.v_string = v;
10407 else
10408 rettv->vval.v_string = vim_strsave(p);
10409 }
10410#else
10411# ifdef HAVE_READLINK
10412 {
10413 char_u *cpy;
10414 int len;
10415 char_u *remain = NULL;
10416 char_u *q;
10417 int is_relative_to_current = FALSE;
10418 int has_trailing_pathsep = FALSE;
10419 int limit = 100;
10420
10421 p = vim_strsave(p);
10422
10423 if (p[0] == '.' && (vim_ispathsep(p[1])
10424 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10425 is_relative_to_current = TRUE;
10426
10427 len = STRLEN(p);
10428 if (len > 0 && after_pathsep(p, p + len))
10429 {
10430 has_trailing_pathsep = TRUE;
10431 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10432 }
10433
10434 q = getnextcomp(p);
10435 if (*q != NUL)
10436 {
10437 /* Separate the first path component in "p", and keep the
10438 * remainder (beginning with the path separator). */
10439 remain = vim_strsave(q - 1);
10440 q[-1] = NUL;
10441 }
10442
10443 buf = alloc(MAXPATHL + 1);
10444 if (buf == NULL)
10445 goto fail;
10446
10447 for (;;)
10448 {
10449 for (;;)
10450 {
10451 len = readlink((char *)p, (char *)buf, MAXPATHL);
10452 if (len <= 0)
10453 break;
10454 buf[len] = NUL;
10455
10456 if (limit-- == 0)
10457 {
10458 vim_free(p);
10459 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010460 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010461 rettv->vval.v_string = NULL;
10462 goto fail;
10463 }
10464
10465 /* Ensure that the result will have a trailing path separator
10466 * if the argument has one. */
10467 if (remain == NULL && has_trailing_pathsep)
10468 add_pathsep(buf);
10469
10470 /* Separate the first path component in the link value and
10471 * concatenate the remainders. */
10472 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10473 if (*q != NUL)
10474 {
10475 if (remain == NULL)
10476 remain = vim_strsave(q - 1);
10477 else
10478 {
10479 cpy = concat_str(q - 1, remain);
10480 if (cpy != NULL)
10481 {
10482 vim_free(remain);
10483 remain = cpy;
10484 }
10485 }
10486 q[-1] = NUL;
10487 }
10488
10489 q = gettail(p);
10490 if (q > p && *q == NUL)
10491 {
10492 /* Ignore trailing path separator. */
10493 q[-1] = NUL;
10494 q = gettail(p);
10495 }
10496 if (q > p && !mch_isFullName(buf))
10497 {
10498 /* symlink is relative to directory of argument */
10499 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10500 if (cpy != NULL)
10501 {
10502 STRCPY(cpy, p);
10503 STRCPY(gettail(cpy), buf);
10504 vim_free(p);
10505 p = cpy;
10506 }
10507 }
10508 else
10509 {
10510 vim_free(p);
10511 p = vim_strsave(buf);
10512 }
10513 }
10514
10515 if (remain == NULL)
10516 break;
10517
10518 /* Append the first path component of "remain" to "p". */
10519 q = getnextcomp(remain + 1);
10520 len = q - remain - (*q != NUL);
10521 cpy = vim_strnsave(p, STRLEN(p) + len);
10522 if (cpy != NULL)
10523 {
10524 STRNCAT(cpy, remain, len);
10525 vim_free(p);
10526 p = cpy;
10527 }
10528 /* Shorten "remain". */
10529 if (*q != NUL)
10530 STRMOVE(remain, q - 1);
10531 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010532 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010533 }
10534
10535 /* If the result is a relative path name, make it explicitly relative to
10536 * the current directory if and only if the argument had this form. */
10537 if (!vim_ispathsep(*p))
10538 {
10539 if (is_relative_to_current
10540 && *p != NUL
10541 && !(p[0] == '.'
10542 && (p[1] == NUL
10543 || vim_ispathsep(p[1])
10544 || (p[1] == '.'
10545 && (p[2] == NUL
10546 || vim_ispathsep(p[2]))))))
10547 {
10548 /* Prepend "./". */
10549 cpy = concat_str((char_u *)"./", p);
10550 if (cpy != NULL)
10551 {
10552 vim_free(p);
10553 p = cpy;
10554 }
10555 }
10556 else if (!is_relative_to_current)
10557 {
10558 /* Strip leading "./". */
10559 q = p;
10560 while (q[0] == '.' && vim_ispathsep(q[1]))
10561 q += 2;
10562 if (q > p)
10563 STRMOVE(p, p + 2);
10564 }
10565 }
10566
10567 /* Ensure that the result will have no trailing path separator
10568 * if the argument had none. But keep "/" or "//". */
10569 if (!has_trailing_pathsep)
10570 {
10571 q = p + STRLEN(p);
10572 if (after_pathsep(p, q))
10573 *gettail_sep(p) = NUL;
10574 }
10575
10576 rettv->vval.v_string = p;
10577 }
10578# else
10579 rettv->vval.v_string = vim_strsave(p);
10580# endif
10581#endif
10582
10583 simplify_filename(rettv->vval.v_string);
10584
10585#ifdef HAVE_READLINK
10586fail:
10587 vim_free(buf);
10588#endif
10589 rettv->v_type = VAR_STRING;
10590}
10591
10592/*
10593 * "reverse({list})" function
10594 */
10595 static void
10596f_reverse(typval_T *argvars, typval_T *rettv)
10597{
10598 list_T *l;
10599 listitem_T *li, *ni;
10600
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010601 if (argvars[0].v_type == VAR_BLOB)
10602 {
10603 blob_T *b = argvars[0].vval.v_blob;
10604 int i, len = blob_len(b);
10605
10606 for (i = 0; i < len / 2; i++)
10607 {
10608 int tmp = blob_get(b, i);
10609
10610 blob_set(b, i, blob_get(b, len - i - 1));
10611 blob_set(b, len - i - 1, tmp);
10612 }
10613 rettv_blob_set(rettv, b);
10614 return;
10615 }
10616
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010617 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010618 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010620 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010621 (char_u *)N_("reverse() argument"), TRUE))
10622 {
10623 li = l->lv_last;
10624 l->lv_first = l->lv_last = NULL;
10625 l->lv_len = 0;
10626 while (li != NULL)
10627 {
10628 ni = li->li_prev;
10629 list_append(l, li);
10630 li = ni;
10631 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010632 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010633 l->lv_idx = l->lv_len - l->lv_idx - 1;
10634 }
10635}
10636
10637#define SP_NOMOVE 0x01 /* don't move cursor */
10638#define SP_REPEAT 0x02 /* repeat to find outer pair */
10639#define SP_RETCOUNT 0x04 /* return matchcount */
10640#define SP_SETPCMARK 0x08 /* set previous context mark */
10641#define SP_START 0x10 /* accept match at start position */
10642#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10643#define SP_END 0x40 /* leave cursor at end of match */
10644#define SP_COLUMN 0x80 /* start at cursor column */
10645
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010646/*
10647 * Get flags for a search function.
10648 * Possibly sets "p_ws".
10649 * Returns BACKWARD, FORWARD or zero (for an error).
10650 */
10651 static int
10652get_search_arg(typval_T *varp, int *flagsp)
10653{
10654 int dir = FORWARD;
10655 char_u *flags;
10656 char_u nbuf[NUMBUFLEN];
10657 int mask;
10658
10659 if (varp->v_type != VAR_UNKNOWN)
10660 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010661 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010662 if (flags == NULL)
10663 return 0; /* type error; errmsg already given */
10664 while (*flags != NUL)
10665 {
10666 switch (*flags)
10667 {
10668 case 'b': dir = BACKWARD; break;
10669 case 'w': p_ws = TRUE; break;
10670 case 'W': p_ws = FALSE; break;
10671 default: mask = 0;
10672 if (flagsp != NULL)
10673 switch (*flags)
10674 {
10675 case 'c': mask = SP_START; break;
10676 case 'e': mask = SP_END; break;
10677 case 'm': mask = SP_RETCOUNT; break;
10678 case 'n': mask = SP_NOMOVE; break;
10679 case 'p': mask = SP_SUBPAT; break;
10680 case 'r': mask = SP_REPEAT; break;
10681 case 's': mask = SP_SETPCMARK; break;
10682 case 'z': mask = SP_COLUMN; break;
10683 }
10684 if (mask == 0)
10685 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010686 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010687 dir = 0;
10688 }
10689 else
10690 *flagsp |= mask;
10691 }
10692 if (dir == 0)
10693 break;
10694 ++flags;
10695 }
10696 }
10697 return dir;
10698}
10699
10700/*
10701 * Shared by search() and searchpos() functions.
10702 */
10703 static int
10704search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10705{
10706 int flags;
10707 char_u *pat;
10708 pos_T pos;
10709 pos_T save_cursor;
10710 int save_p_ws = p_ws;
10711 int dir;
10712 int retval = 0; /* default: FAIL */
10713 long lnum_stop = 0;
10714 proftime_T tm;
10715#ifdef FEAT_RELTIME
10716 long time_limit = 0;
10717#endif
10718 int options = SEARCH_KEEP;
10719 int subpatnum;
10720
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010721 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010722 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10723 if (dir == 0)
10724 goto theend;
10725 flags = *flagsp;
10726 if (flags & SP_START)
10727 options |= SEARCH_START;
10728 if (flags & SP_END)
10729 options |= SEARCH_END;
10730 if (flags & SP_COLUMN)
10731 options |= SEARCH_COL;
10732
10733 /* Optional arguments: line number to stop searching and timeout. */
10734 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10735 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010736 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010737 if (lnum_stop < 0)
10738 goto theend;
10739#ifdef FEAT_RELTIME
10740 if (argvars[3].v_type != VAR_UNKNOWN)
10741 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010742 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010743 if (time_limit < 0)
10744 goto theend;
10745 }
10746#endif
10747 }
10748
10749#ifdef FEAT_RELTIME
10750 /* Set the time limit, if there is one. */
10751 profile_setlimit(time_limit, &tm);
10752#endif
10753
10754 /*
10755 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10756 * Check to make sure only those flags are set.
10757 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10758 * flags cannot be set. Check for that condition also.
10759 */
10760 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10761 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10762 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010763 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010764 goto theend;
10765 }
10766
10767 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010768 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010769 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010770 if (subpatnum != FAIL)
10771 {
10772 if (flags & SP_SUBPAT)
10773 retval = subpatnum;
10774 else
10775 retval = pos.lnum;
10776 if (flags & SP_SETPCMARK)
10777 setpcmark();
10778 curwin->w_cursor = pos;
10779 if (match_pos != NULL)
10780 {
10781 /* Store the match cursor position */
10782 match_pos->lnum = pos.lnum;
10783 match_pos->col = pos.col + 1;
10784 }
10785 /* "/$" will put the cursor after the end of the line, may need to
10786 * correct that here */
10787 check_cursor();
10788 }
10789
10790 /* If 'n' flag is used: restore cursor position. */
10791 if (flags & SP_NOMOVE)
10792 curwin->w_cursor = save_cursor;
10793 else
10794 curwin->w_set_curswant = TRUE;
10795theend:
10796 p_ws = save_p_ws;
10797
10798 return retval;
10799}
10800
10801#ifdef FEAT_FLOAT
10802
10803/*
10804 * round() is not in C90, use ceil() or floor() instead.
10805 */
10806 float_T
10807vim_round(float_T f)
10808{
10809 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10810}
10811
10812/*
10813 * "round({float})" function
10814 */
10815 static void
10816f_round(typval_T *argvars, typval_T *rettv)
10817{
10818 float_T f = 0.0;
10819
10820 rettv->v_type = VAR_FLOAT;
10821 if (get_float_arg(argvars, &f) == OK)
10822 rettv->vval.v_float = vim_round(f);
10823 else
10824 rettv->vval.v_float = 0.0;
10825}
10826#endif
10827
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010828#ifdef FEAT_RUBY
10829/*
10830 * "rubyeval()" function
10831 */
10832 static void
10833f_rubyeval(typval_T *argvars, typval_T *rettv)
10834{
10835 char_u *str;
10836 char_u buf[NUMBUFLEN];
10837
10838 str = tv_get_string_buf(&argvars[0], buf);
10839 do_rubyeval(str, rettv);
10840}
10841#endif
10842
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010843/*
10844 * "screenattr()" function
10845 */
10846 static void
10847f_screenattr(typval_T *argvars, typval_T *rettv)
10848{
10849 int row;
10850 int col;
10851 int c;
10852
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010853 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10854 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010855 if (row < 0 || row >= screen_Rows
10856 || col < 0 || col >= screen_Columns)
10857 c = -1;
10858 else
10859 c = ScreenAttrs[LineOffset[row] + col];
10860 rettv->vval.v_number = c;
10861}
10862
10863/*
10864 * "screenchar()" function
10865 */
10866 static void
10867f_screenchar(typval_T *argvars, typval_T *rettv)
10868{
10869 int row;
10870 int col;
10871 int off;
10872 int c;
10873
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010874 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10875 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010876 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010877 c = -1;
10878 else
10879 {
10880 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010881 if (enc_utf8 && ScreenLinesUC[off] != 0)
10882 c = ScreenLinesUC[off];
10883 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010884 c = ScreenLines[off];
10885 }
10886 rettv->vval.v_number = c;
10887}
10888
10889/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010890 * "screenchars()" function
10891 */
10892 static void
10893f_screenchars(typval_T *argvars, typval_T *rettv)
10894{
10895 int row;
10896 int col;
10897 int off;
10898 int c;
10899 int i;
10900
10901 if (rettv_list_alloc(rettv) == FAIL)
10902 return;
10903 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10904 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10905 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10906 return;
10907
10908 off = LineOffset[row] + col;
10909 if (enc_utf8 && ScreenLinesUC[off] != 0)
10910 c = ScreenLinesUC[off];
10911 else
10912 c = ScreenLines[off];
10913 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10914
10915 if (enc_utf8)
10916
10917 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10918 list_append_number(rettv->vval.v_list,
10919 (varnumber_T)ScreenLinesC[i][off]);
10920}
10921
10922/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010923 * "screencol()" function
10924 *
10925 * First column is 1 to be consistent with virtcol().
10926 */
10927 static void
10928f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10929{
10930 rettv->vval.v_number = screen_screencol() + 1;
10931}
10932
10933/*
10934 * "screenrow()" function
10935 */
10936 static void
10937f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10938{
10939 rettv->vval.v_number = screen_screenrow() + 1;
10940}
10941
10942/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010943 * "screenstring()" function
10944 */
10945 static void
10946f_screenstring(typval_T *argvars, typval_T *rettv)
10947{
10948 int row;
10949 int col;
10950 int off;
10951 int c;
10952 int i;
10953 char_u buf[MB_MAXBYTES + 1];
10954 int buflen = 0;
10955
10956 rettv->vval.v_string = NULL;
10957 rettv->v_type = VAR_STRING;
10958
10959 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10960 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10961 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10962 return;
10963
10964 off = LineOffset[row] + col;
10965 if (enc_utf8 && ScreenLinesUC[off] != 0)
10966 c = ScreenLinesUC[off];
10967 else
10968 c = ScreenLines[off];
10969 buflen += mb_char2bytes(c, buf);
10970
10971 if (enc_utf8)
10972 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10973 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10974
10975 buf[buflen] = NUL;
10976 rettv->vval.v_string = vim_strsave(buf);
10977}
10978
10979/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010980 * "search()" function
10981 */
10982 static void
10983f_search(typval_T *argvars, typval_T *rettv)
10984{
10985 int flags = 0;
10986
10987 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10988}
10989
10990/*
10991 * "searchdecl()" function
10992 */
10993 static void
10994f_searchdecl(typval_T *argvars, typval_T *rettv)
10995{
10996 int locally = 1;
10997 int thisblock = 0;
10998 int error = FALSE;
10999 char_u *name;
11000
11001 rettv->vval.v_number = 1; /* default: FAIL */
11002
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011003 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011004 if (argvars[1].v_type != VAR_UNKNOWN)
11005 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011006 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011007 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011008 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011009 }
11010 if (!error && name != NULL)
11011 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
11012 locally, thisblock, SEARCH_KEEP) == FAIL;
11013}
11014
11015/*
11016 * Used by searchpair() and searchpairpos()
11017 */
11018 static int
11019searchpair_cmn(typval_T *argvars, pos_T *match_pos)
11020{
11021 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010011022 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011023 int save_p_ws = p_ws;
11024 int dir;
11025 int flags = 0;
11026 char_u nbuf1[NUMBUFLEN];
11027 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011028 int retval = 0; /* default: FAIL */
11029 long lnum_stop = 0;
11030 long time_limit = 0;
11031
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011032 /* Get the three pattern arguments: start, middle, end. Will result in an
11033 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011034 spat = tv_get_string_chk(&argvars[0]);
11035 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
11036 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011037 if (spat == NULL || mpat == NULL || epat == NULL)
11038 goto theend; /* type error */
11039
11040 /* Handle the optional fourth argument: flags */
11041 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
11042 if (dir == 0)
11043 goto theend;
11044
11045 /* Don't accept SP_END or SP_SUBPAT.
11046 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
11047 */
11048 if ((flags & (SP_END | SP_SUBPAT)) != 0
11049 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
11050 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011051 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011052 goto theend;
11053 }
11054
11055 /* Using 'r' implies 'W', otherwise it doesn't work. */
11056 if (flags & SP_REPEAT)
11057 p_ws = FALSE;
11058
11059 /* Optional fifth argument: skip expression */
11060 if (argvars[3].v_type == VAR_UNKNOWN
11061 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010011062 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011063 else
11064 {
Bram Moolenaar48570482017-10-30 21:48:41 +010011065 skip = &argvars[4];
11066 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
11067 && skip->v_type != VAR_STRING)
11068 {
11069 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011070 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010011071 goto theend;
11072 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011073 if (argvars[5].v_type != VAR_UNKNOWN)
11074 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011075 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011076 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011077 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011078 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011079 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011080 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011081#ifdef FEAT_RELTIME
11082 if (argvars[6].v_type != VAR_UNKNOWN)
11083 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011084 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011085 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011086 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011087 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011088 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011089 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011090 }
11091#endif
11092 }
11093 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011094
11095 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
11096 match_pos, lnum_stop, time_limit);
11097
11098theend:
11099 p_ws = save_p_ws;
11100
11101 return retval;
11102}
11103
11104/*
11105 * "searchpair()" function
11106 */
11107 static void
11108f_searchpair(typval_T *argvars, typval_T *rettv)
11109{
11110 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11111}
11112
11113/*
11114 * "searchpairpos()" function
11115 */
11116 static void
11117f_searchpairpos(typval_T *argvars, typval_T *rettv)
11118{
11119 pos_T match_pos;
11120 int lnum = 0;
11121 int col = 0;
11122
11123 if (rettv_list_alloc(rettv) == FAIL)
11124 return;
11125
11126 if (searchpair_cmn(argvars, &match_pos) > 0)
11127 {
11128 lnum = match_pos.lnum;
11129 col = match_pos.col;
11130 }
11131
11132 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11133 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11134}
11135
11136/*
11137 * Search for a start/middle/end thing.
11138 * Used by searchpair(), see its documentation for the details.
11139 * Returns 0 or -1 for no match,
11140 */
11141 long
11142do_searchpair(
11143 char_u *spat, /* start pattern */
11144 char_u *mpat, /* middle pattern */
11145 char_u *epat, /* end pattern */
11146 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011147 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011148 int flags, /* SP_SETPCMARK and other SP_ values */
11149 pos_T *match_pos,
11150 linenr_T lnum_stop, /* stop at this line if not zero */
11151 long time_limit UNUSED) /* stop after this many msec */
11152{
11153 char_u *save_cpo;
11154 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11155 long retval = 0;
11156 pos_T pos;
11157 pos_T firstpos;
11158 pos_T foundpos;
11159 pos_T save_cursor;
11160 pos_T save_pos;
11161 int n;
11162 int r;
11163 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011164 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011165 int err;
11166 int options = SEARCH_KEEP;
11167 proftime_T tm;
11168
11169 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11170 save_cpo = p_cpo;
11171 p_cpo = empty_option;
11172
11173#ifdef FEAT_RELTIME
11174 /* Set the time limit, if there is one. */
11175 profile_setlimit(time_limit, &tm);
11176#endif
11177
11178 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11179 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011180 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
11181 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011182 if (pat2 == NULL || pat3 == NULL)
11183 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011184 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011185 if (*mpat == NUL)
11186 STRCPY(pat3, pat2);
11187 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011188 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011189 spat, epat, mpat);
11190 if (flags & SP_START)
11191 options |= SEARCH_START;
11192
Bram Moolenaar48570482017-10-30 21:48:41 +010011193 if (skip != NULL)
11194 {
11195 /* Empty string means to not use the skip expression. */
11196 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11197 use_skip = skip->vval.v_string != NULL
11198 && *skip->vval.v_string != NUL;
11199 }
11200
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011201 save_cursor = curwin->w_cursor;
11202 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011203 CLEAR_POS(&firstpos);
11204 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011205 pat = pat3;
11206 for (;;)
11207 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011208 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011209 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011210 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011211 /* didn't find it or found the first match again: FAIL */
11212 break;
11213
11214 if (firstpos.lnum == 0)
11215 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011216 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011217 {
11218 /* Found the same position again. Can happen with a pattern that
11219 * has "\zs" at the end and searching backwards. Advance one
11220 * character and try again. */
11221 if (dir == BACKWARD)
11222 decl(&pos);
11223 else
11224 incl(&pos);
11225 }
11226 foundpos = pos;
11227
11228 /* clear the start flag to avoid getting stuck here */
11229 options &= ~SEARCH_START;
11230
11231 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011232 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011233 {
11234 save_pos = curwin->w_cursor;
11235 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011236 err = FALSE;
11237 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011238 curwin->w_cursor = save_pos;
11239 if (err)
11240 {
11241 /* Evaluating {skip} caused an error, break here. */
11242 curwin->w_cursor = save_cursor;
11243 retval = -1;
11244 break;
11245 }
11246 if (r)
11247 continue;
11248 }
11249
11250 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11251 {
11252 /* Found end when searching backwards or start when searching
11253 * forward: nested pair. */
11254 ++nest;
11255 pat = pat2; /* nested, don't search for middle */
11256 }
11257 else
11258 {
11259 /* Found end when searching forward or start when searching
11260 * backward: end of (nested) pair; or found middle in outer pair. */
11261 if (--nest == 1)
11262 pat = pat3; /* outer level, search for middle */
11263 }
11264
11265 if (nest == 0)
11266 {
11267 /* Found the match: return matchcount or line number. */
11268 if (flags & SP_RETCOUNT)
11269 ++retval;
11270 else
11271 retval = pos.lnum;
11272 if (flags & SP_SETPCMARK)
11273 setpcmark();
11274 curwin->w_cursor = pos;
11275 if (!(flags & SP_REPEAT))
11276 break;
11277 nest = 1; /* search for next unmatched */
11278 }
11279 }
11280
11281 if (match_pos != NULL)
11282 {
11283 /* Store the match cursor position */
11284 match_pos->lnum = curwin->w_cursor.lnum;
11285 match_pos->col = curwin->w_cursor.col + 1;
11286 }
11287
11288 /* If 'n' flag is used or search failed: restore cursor position. */
11289 if ((flags & SP_NOMOVE) || retval == 0)
11290 curwin->w_cursor = save_cursor;
11291
11292theend:
11293 vim_free(pat2);
11294 vim_free(pat3);
11295 if (p_cpo == empty_option)
11296 p_cpo = save_cpo;
11297 else
11298 /* Darn, evaluating the {skip} expression changed the value. */
11299 free_string_option(save_cpo);
11300
11301 return retval;
11302}
11303
11304/*
11305 * "searchpos()" function
11306 */
11307 static void
11308f_searchpos(typval_T *argvars, typval_T *rettv)
11309{
11310 pos_T match_pos;
11311 int lnum = 0;
11312 int col = 0;
11313 int n;
11314 int flags = 0;
11315
11316 if (rettv_list_alloc(rettv) == FAIL)
11317 return;
11318
11319 n = search_cmn(argvars, &match_pos, &flags);
11320 if (n > 0)
11321 {
11322 lnum = match_pos.lnum;
11323 col = match_pos.col;
11324 }
11325
11326 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11327 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11328 if (flags & SP_SUBPAT)
11329 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11330}
11331
11332 static void
11333f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11334{
11335#ifdef FEAT_CLIENTSERVER
11336 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011337 char_u *server = tv_get_string_chk(&argvars[0]);
11338 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011339
11340 rettv->vval.v_number = -1;
11341 if (server == NULL || reply == NULL)
11342 return;
11343 if (check_restricted() || check_secure())
11344 return;
11345# ifdef FEAT_X11
11346 if (check_connection() == FAIL)
11347 return;
11348# endif
11349
11350 if (serverSendReply(server, reply) < 0)
11351 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011352 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011353 return;
11354 }
11355 rettv->vval.v_number = 0;
11356#else
11357 rettv->vval.v_number = -1;
11358#endif
11359}
11360
11361 static void
11362f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11363{
11364 char_u *r = NULL;
11365
11366#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011367# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011368 r = serverGetVimNames();
11369# else
11370 make_connection();
11371 if (X_DISPLAY != NULL)
11372 r = serverGetVimNames(X_DISPLAY);
11373# endif
11374#endif
11375 rettv->v_type = VAR_STRING;
11376 rettv->vval.v_string = r;
11377}
11378
11379/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011380 * "setbufline()" function
11381 */
11382 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011383f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011384{
11385 linenr_T lnum;
11386 buf_T *buf;
11387
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011388 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011389 if (buf == NULL)
11390 rettv->vval.v_number = 1; /* FAIL */
11391 else
11392 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011393 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011394 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011395 }
11396}
11397
11398/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011399 * "setbufvar()" function
11400 */
11401 static void
11402f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11403{
11404 buf_T *buf;
11405 char_u *varname, *bufvarname;
11406 typval_T *varp;
11407 char_u nbuf[NUMBUFLEN];
11408
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011409 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011410 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011411 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11412 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011413 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011414 varp = &argvars[2];
11415
11416 if (buf != NULL && varname != NULL && varp != NULL)
11417 {
11418 if (*varname == '&')
11419 {
11420 long numval;
11421 char_u *strval;
11422 int error = FALSE;
11423 aco_save_T aco;
11424
11425 /* set curbuf to be our buf, temporarily */
11426 aucmd_prepbuf(&aco, buf);
11427
11428 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011429 numval = (long)tv_get_number_chk(varp, &error);
11430 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011431 if (!error && strval != NULL)
11432 set_option_value(varname, numval, strval, OPT_LOCAL);
11433
11434 /* reset notion of buffer */
11435 aucmd_restbuf(&aco);
11436 }
11437 else
11438 {
11439 buf_T *save_curbuf = curbuf;
11440
11441 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11442 if (bufvarname != NULL)
11443 {
11444 curbuf = buf;
11445 STRCPY(bufvarname, "b:");
11446 STRCPY(bufvarname + 2, varname);
11447 set_var(bufvarname, varp, TRUE);
11448 vim_free(bufvarname);
11449 curbuf = save_curbuf;
11450 }
11451 }
11452 }
11453}
11454
11455 static void
11456f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11457{
11458 dict_T *d;
11459 dictitem_T *di;
11460 char_u *csearch;
11461
11462 if (argvars[0].v_type != VAR_DICT)
11463 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011464 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011465 return;
11466 }
11467
11468 if ((d = argvars[0].vval.v_dict) != NULL)
11469 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011470 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011471 if (csearch != NULL)
11472 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011473 if (enc_utf8)
11474 {
11475 int pcc[MAX_MCO];
11476 int c = utfc_ptr2char(csearch, pcc);
11477
11478 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11479 }
11480 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011481 set_last_csearch(PTR2CHAR(csearch),
11482 csearch, MB_PTR2LEN(csearch));
11483 }
11484
11485 di = dict_find(d, (char_u *)"forward", -1);
11486 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011487 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011488 ? FORWARD : BACKWARD);
11489
11490 di = dict_find(d, (char_u *)"until", -1);
11491 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011492 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011493 }
11494}
11495
11496/*
11497 * "setcmdpos()" function
11498 */
11499 static void
11500f_setcmdpos(typval_T *argvars, typval_T *rettv)
11501{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011502 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011503
11504 if (pos >= 0)
11505 rettv->vval.v_number = set_cmdline_pos(pos);
11506}
11507
11508/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011509 * "setenv()" function
11510 */
11511 static void
11512f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11513{
11514 char_u namebuf[NUMBUFLEN];
11515 char_u valbuf[NUMBUFLEN];
11516 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11517
11518 if (argvars[1].v_type == VAR_SPECIAL
11519 && argvars[1].vval.v_number == VVAL_NULL)
11520 vim_unsetenv(name);
11521 else
11522 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11523}
11524
11525/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011526 * "setfperm({fname}, {mode})" function
11527 */
11528 static void
11529f_setfperm(typval_T *argvars, typval_T *rettv)
11530{
11531 char_u *fname;
11532 char_u modebuf[NUMBUFLEN];
11533 char_u *mode_str;
11534 int i;
11535 int mask;
11536 int mode = 0;
11537
11538 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011539 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011540 if (fname == NULL)
11541 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011542 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011543 if (mode_str == NULL)
11544 return;
11545 if (STRLEN(mode_str) != 9)
11546 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011547 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011548 return;
11549 }
11550
11551 mask = 1;
11552 for (i = 8; i >= 0; --i)
11553 {
11554 if (mode_str[i] != '-')
11555 mode |= mask;
11556 mask = mask << 1;
11557 }
11558 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11559}
11560
11561/*
11562 * "setline()" function
11563 */
11564 static void
11565f_setline(typval_T *argvars, typval_T *rettv)
11566{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011567 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011568
Bram Moolenaarca851592018-06-06 21:04:07 +020011569 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011570}
11571
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011572/*
11573 * Used by "setqflist()" and "setloclist()" functions
11574 */
11575 static void
11576set_qf_ll_list(
11577 win_T *wp UNUSED,
11578 typval_T *list_arg UNUSED,
11579 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011580 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011581 typval_T *rettv)
11582{
11583#ifdef FEAT_QUICKFIX
11584 static char *e_invact = N_("E927: Invalid action: '%s'");
11585 char_u *act;
11586 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011587 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011588#endif
11589
11590 rettv->vval.v_number = -1;
11591
11592#ifdef FEAT_QUICKFIX
11593 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011594 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011595 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011596 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011597 else
11598 {
11599 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011600 dict_T *d = NULL;
11601 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011602
11603 if (action_arg->v_type == VAR_STRING)
11604 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011605 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011606 if (act == NULL)
11607 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011608 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11609 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011610 action = *act;
11611 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011612 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011613 }
11614 else if (action_arg->v_type == VAR_UNKNOWN)
11615 action = ' ';
11616 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011617 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011618
Bram Moolenaard823fa92016-08-12 16:29:27 +020011619 if (action_arg->v_type != VAR_UNKNOWN
11620 && what_arg->v_type != VAR_UNKNOWN)
11621 {
11622 if (what_arg->v_type == VAR_DICT)
11623 d = what_arg->vval.v_dict;
11624 else
11625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011626 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011627 valid_dict = FALSE;
11628 }
11629 }
11630
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011631 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011632 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011633 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11634 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011635 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011636 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011637 }
11638#endif
11639}
11640
11641/*
11642 * "setloclist()" function
11643 */
11644 static void
11645f_setloclist(typval_T *argvars, typval_T *rettv)
11646{
11647 win_T *win;
11648
11649 rettv->vval.v_number = -1;
11650
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011651 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011652 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011653 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011654}
11655
11656/*
11657 * "setmatches()" function
11658 */
11659 static void
11660f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11661{
11662#ifdef FEAT_SEARCH_EXTRA
11663 list_T *l;
11664 listitem_T *li;
11665 dict_T *d;
11666 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011667 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011668
11669 rettv->vval.v_number = -1;
11670 if (argvars[0].v_type != VAR_LIST)
11671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011672 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011673 return;
11674 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011675 if (win == NULL)
11676 return;
11677
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011678 if ((l = argvars[0].vval.v_list) != NULL)
11679 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011680 /* To some extent make sure that we are dealing with a list from
11681 * "getmatches()". */
11682 li = l->lv_first;
11683 while (li != NULL)
11684 {
11685 if (li->li_tv.v_type != VAR_DICT
11686 || (d = li->li_tv.vval.v_dict) == NULL)
11687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011688 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011689 return;
11690 }
11691 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11692 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11693 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11694 && dict_find(d, (char_u *)"priority", -1) != NULL
11695 && dict_find(d, (char_u *)"id", -1) != NULL))
11696 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011697 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011698 return;
11699 }
11700 li = li->li_next;
11701 }
11702
Bram Moolenaaraff74912019-03-30 18:11:49 +010011703 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011704 li = l->lv_first;
11705 while (li != NULL)
11706 {
11707 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011708 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011709 dictitem_T *di;
11710 char_u *group;
11711 int priority;
11712 int id;
11713 char_u *conceal;
11714
11715 d = li->li_tv.vval.v_dict;
11716 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11717 {
11718 if (s == NULL)
11719 {
11720 s = list_alloc();
11721 if (s == NULL)
11722 return;
11723 }
11724
11725 /* match from matchaddpos() */
11726 for (i = 1; i < 9; i++)
11727 {
11728 sprintf((char *)buf, (char *)"pos%d", i);
11729 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11730 {
11731 if (di->di_tv.v_type != VAR_LIST)
11732 return;
11733
11734 list_append_tv(s, &di->di_tv);
11735 s->lv_refcount++;
11736 }
11737 else
11738 break;
11739 }
11740 }
11741
Bram Moolenaar8f667172018-12-14 15:38:31 +010011742 group = dict_get_string(d, (char_u *)"group", TRUE);
11743 priority = (int)dict_get_number(d, (char_u *)"priority");
11744 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011745 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011746 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011747 : NULL;
11748 if (i == 0)
11749 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011750 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011751 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011752 priority, id, NULL, conceal);
11753 }
11754 else
11755 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011756 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011757 list_unref(s);
11758 s = NULL;
11759 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011760 vim_free(group);
11761 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011762
11763 li = li->li_next;
11764 }
11765 rettv->vval.v_number = 0;
11766 }
11767#endif
11768}
11769
11770/*
11771 * "setpos()" function
11772 */
11773 static void
11774f_setpos(typval_T *argvars, typval_T *rettv)
11775{
11776 pos_T pos;
11777 int fnum;
11778 char_u *name;
11779 colnr_T curswant = -1;
11780
11781 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011782 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011783 if (name != NULL)
11784 {
11785 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11786 {
11787 if (--pos.col < 0)
11788 pos.col = 0;
11789 if (name[0] == '.' && name[1] == NUL)
11790 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011791 /* set cursor; "fnum" is ignored */
11792 curwin->w_cursor = pos;
11793 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011794 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011795 curwin->w_curswant = curswant - 1;
11796 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011797 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011798 check_cursor();
11799 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011800 }
11801 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11802 {
11803 /* set mark */
11804 if (setmark_pos(name[1], &pos, fnum) == OK)
11805 rettv->vval.v_number = 0;
11806 }
11807 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011808 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011809 }
11810 }
11811}
11812
11813/*
11814 * "setqflist()" function
11815 */
11816 static void
11817f_setqflist(typval_T *argvars, typval_T *rettv)
11818{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011819 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011820}
11821
11822/*
11823 * "setreg()" function
11824 */
11825 static void
11826f_setreg(typval_T *argvars, typval_T *rettv)
11827{
11828 int regname;
11829 char_u *strregname;
11830 char_u *stropt;
11831 char_u *strval;
11832 int append;
11833 char_u yank_type;
11834 long block_len;
11835
11836 block_len = -1;
11837 yank_type = MAUTO;
11838 append = FALSE;
11839
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011840 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011841 rettv->vval.v_number = 1; /* FAIL is default */
11842
11843 if (strregname == NULL)
11844 return; /* type error; errmsg already given */
11845 regname = *strregname;
11846 if (regname == 0 || regname == '@')
11847 regname = '"';
11848
11849 if (argvars[2].v_type != VAR_UNKNOWN)
11850 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011851 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011852 if (stropt == NULL)
11853 return; /* type error */
11854 for (; *stropt != NUL; ++stropt)
11855 switch (*stropt)
11856 {
11857 case 'a': case 'A': /* append */
11858 append = TRUE;
11859 break;
11860 case 'v': case 'c': /* character-wise selection */
11861 yank_type = MCHAR;
11862 break;
11863 case 'V': case 'l': /* line-wise selection */
11864 yank_type = MLINE;
11865 break;
11866 case 'b': case Ctrl_V: /* block-wise selection */
11867 yank_type = MBLOCK;
11868 if (VIM_ISDIGIT(stropt[1]))
11869 {
11870 ++stropt;
11871 block_len = getdigits(&stropt) - 1;
11872 --stropt;
11873 }
11874 break;
11875 }
11876 }
11877
11878 if (argvars[1].v_type == VAR_LIST)
11879 {
11880 char_u **lstval;
11881 char_u **allocval;
11882 char_u buf[NUMBUFLEN];
11883 char_u **curval;
11884 char_u **curallocval;
11885 list_T *ll = argvars[1].vval.v_list;
11886 listitem_T *li;
11887 int len;
11888
11889 /* If the list is NULL handle like an empty list. */
11890 len = ll == NULL ? 0 : ll->lv_len;
11891
11892 /* First half: use for pointers to result lines; second half: use for
11893 * pointers to allocated copies. */
11894 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11895 if (lstval == NULL)
11896 return;
11897 curval = lstval;
11898 allocval = lstval + len + 2;
11899 curallocval = allocval;
11900
11901 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11902 li = li->li_next)
11903 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011904 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011905 if (strval == NULL)
11906 goto free_lstval;
11907 if (strval == buf)
11908 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011909 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011910 * overwrite the string. */
11911 strval = vim_strsave(buf);
11912 if (strval == NULL)
11913 goto free_lstval;
11914 *curallocval++ = strval;
11915 }
11916 *curval++ = strval;
11917 }
11918 *curval++ = NULL;
11919
11920 write_reg_contents_lst(regname, lstval, -1,
11921 append, yank_type, block_len);
11922free_lstval:
11923 while (curallocval > allocval)
11924 vim_free(*--curallocval);
11925 vim_free(lstval);
11926 }
11927 else
11928 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011929 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011930 if (strval == NULL)
11931 return;
11932 write_reg_contents_ex(regname, strval, -1,
11933 append, yank_type, block_len);
11934 }
11935 rettv->vval.v_number = 0;
11936}
11937
11938/*
11939 * "settabvar()" function
11940 */
11941 static void
11942f_settabvar(typval_T *argvars, typval_T *rettv)
11943{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011944 tabpage_T *save_curtab;
11945 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011946 char_u *varname, *tabvarname;
11947 typval_T *varp;
11948
11949 rettv->vval.v_number = 0;
11950
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011951 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011952 return;
11953
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011954 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11955 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011956 varp = &argvars[2];
11957
Bram Moolenaar4033c552017-09-16 20:54:51 +020011958 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011959 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011960 save_curtab = curtab;
11961 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011962
11963 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11964 if (tabvarname != NULL)
11965 {
11966 STRCPY(tabvarname, "t:");
11967 STRCPY(tabvarname + 2, varname);
11968 set_var(tabvarname, varp, TRUE);
11969 vim_free(tabvarname);
11970 }
11971
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011972 /* Restore current tabpage */
11973 if (valid_tabpage(save_curtab))
11974 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011975 }
11976}
11977
11978/*
11979 * "settabwinvar()" function
11980 */
11981 static void
11982f_settabwinvar(typval_T *argvars, typval_T *rettv)
11983{
11984 setwinvar(argvars, rettv, 1);
11985}
11986
11987/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011988 * "settagstack()" function
11989 */
11990 static void
11991f_settagstack(typval_T *argvars, typval_T *rettv)
11992{
11993 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11994 win_T *wp;
11995 dict_T *d;
11996 int action = 'r';
11997
11998 rettv->vval.v_number = -1;
11999
12000 // first argument: window number or id
12001 wp = find_win_by_nr_or_id(&argvars[0]);
12002 if (wp == NULL)
12003 return;
12004
12005 // second argument: dict with items to set in the tag stack
12006 if (argvars[1].v_type != VAR_DICT)
12007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012008 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012009 return;
12010 }
12011 d = argvars[1].vval.v_dict;
12012 if (d == NULL)
12013 return;
12014
12015 // third argument: action - 'a' for append and 'r' for replace.
12016 // default is to replace the stack.
12017 if (argvars[2].v_type == VAR_UNKNOWN)
12018 action = 'r';
12019 else if (argvars[2].v_type == VAR_STRING)
12020 {
12021 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012022 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012023 if (actstr == NULL)
12024 return;
12025 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
12026 action = *actstr;
12027 else
12028 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012029 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012030 return;
12031 }
12032 }
12033 else
12034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012035 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012036 return;
12037 }
12038
12039 if (set_tagstack(wp, d, action) == OK)
12040 rettv->vval.v_number = 0;
12041}
12042
12043/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012044 * "setwinvar()" function
12045 */
12046 static void
12047f_setwinvar(typval_T *argvars, typval_T *rettv)
12048{
12049 setwinvar(argvars, rettv, 0);
12050}
12051
12052#ifdef FEAT_CRYPT
12053/*
12054 * "sha256({string})" function
12055 */
12056 static void
12057f_sha256(typval_T *argvars, typval_T *rettv)
12058{
12059 char_u *p;
12060
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012061 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012062 rettv->vval.v_string = vim_strsave(
12063 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
12064 rettv->v_type = VAR_STRING;
12065}
12066#endif /* FEAT_CRYPT */
12067
12068/*
12069 * "shellescape({string})" function
12070 */
12071 static void
12072f_shellescape(typval_T *argvars, typval_T *rettv)
12073{
Bram Moolenaar20615522017-06-05 18:46:26 +020012074 int do_special = non_zero_arg(&argvars[1]);
12075
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012076 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012077 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012078 rettv->v_type = VAR_STRING;
12079}
12080
12081/*
12082 * shiftwidth() function
12083 */
12084 static void
12085f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
12086{
Bram Moolenaarf9514162018-11-22 03:08:29 +010012087 rettv->vval.v_number = 0;
12088
12089 if (argvars[0].v_type != VAR_UNKNOWN)
12090 {
12091 long col;
12092
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012093 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010012094 if (col < 0)
12095 return; // type error; errmsg already given
12096#ifdef FEAT_VARTABS
12097 rettv->vval.v_number = get_sw_value_col(curbuf, col);
12098 return;
12099#endif
12100 }
12101
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012102 rettv->vval.v_number = get_sw_value(curbuf);
12103}
12104
Bram Moolenaar162b7142018-12-21 15:17:36 +010012105#ifdef FEAT_SIGNS
12106/*
12107 * "sign_define()" function
12108 */
12109 static void
12110f_sign_define(typval_T *argvars, typval_T *rettv)
12111{
12112 char_u *name;
12113 dict_T *dict;
12114 char_u *icon = NULL;
12115 char_u *linehl = NULL;
12116 char_u *text = NULL;
12117 char_u *texthl = NULL;
12118
12119 rettv->vval.v_number = -1;
12120
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012121 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012122 if (name == NULL)
12123 return;
12124
12125 if (argvars[1].v_type != VAR_UNKNOWN)
12126 {
12127 if (argvars[1].v_type != VAR_DICT)
12128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012129 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012130 return;
12131 }
12132
12133 // sign attributes
12134 dict = argvars[1].vval.v_dict;
12135 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12136 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12137 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12138 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12139 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12140 text = dict_get_string(dict, (char_u *)"text", TRUE);
12141 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12142 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12143 }
12144
12145 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12146 rettv->vval.v_number = 0;
12147
12148 vim_free(icon);
12149 vim_free(linehl);
12150 vim_free(text);
12151 vim_free(texthl);
12152}
12153
12154/*
12155 * "sign_getdefined()" function
12156 */
12157 static void
12158f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12159{
12160 char_u *name = NULL;
12161
12162 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12163 return;
12164
12165 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012166 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012167
12168 sign_getlist(name, rettv->vval.v_list);
12169}
12170
12171/*
12172 * "sign_getplaced()" function
12173 */
12174 static void
12175f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12176{
12177 buf_T *buf = NULL;
12178 dict_T *dict;
12179 dictitem_T *di;
12180 linenr_T lnum = 0;
12181 int sign_id = 0;
12182 char_u *group = NULL;
12183 int notanum = FALSE;
12184
12185 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12186 return;
12187
12188 if (argvars[0].v_type != VAR_UNKNOWN)
12189 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012190 // get signs placed in the specified buffer
12191 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012192 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012193 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012194
12195 if (argvars[1].v_type != VAR_UNKNOWN)
12196 {
12197 if (argvars[1].v_type != VAR_DICT ||
12198 ((dict = argvars[1].vval.v_dict) == NULL))
12199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012200 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012201 return;
12202 }
12203 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12204 {
12205 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012206 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012207 if (notanum)
12208 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012209 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012210 }
12211 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12212 {
12213 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012214 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012215 if (notanum)
12216 return;
12217 }
12218 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12219 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012220 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012221 if (group == NULL)
12222 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012223 if (*group == '\0') // empty string means global group
12224 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012225 }
12226 }
12227 }
12228
12229 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12230}
12231
12232/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012233 * "sign_jump()" function
12234 */
12235 static void
12236f_sign_jump(typval_T *argvars, typval_T *rettv)
12237{
12238 int sign_id;
12239 char_u *sign_group = NULL;
12240 buf_T *buf;
12241 int notanum = FALSE;
12242
12243 rettv->vval.v_number = -1;
12244
Bram Moolenaarbdace832019-03-02 10:13:42 +010012245 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012246 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12247 if (notanum)
12248 return;
12249 if (sign_id <= 0)
12250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012251 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012252 return;
12253 }
12254
12255 // Sign group
12256 sign_group = tv_get_string_chk(&argvars[1]);
12257 if (sign_group == NULL)
12258 return;
12259 if (sign_group[0] == '\0')
12260 sign_group = NULL; // global sign group
12261 else
12262 {
12263 sign_group = vim_strsave(sign_group);
12264 if (sign_group == NULL)
12265 return;
12266 }
12267
12268 // Buffer to place the sign
12269 buf = get_buf_arg(&argvars[2]);
12270 if (buf == NULL)
12271 goto cleanup;
12272
12273 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12274
12275cleanup:
12276 vim_free(sign_group);
12277}
12278
12279/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012280 * "sign_place()" function
12281 */
12282 static void
12283f_sign_place(typval_T *argvars, typval_T *rettv)
12284{
12285 int sign_id;
12286 char_u *group = NULL;
12287 char_u *sign_name;
12288 buf_T *buf;
12289 dict_T *dict;
12290 dictitem_T *di;
12291 linenr_T lnum = 0;
12292 int prio = SIGN_DEF_PRIO;
12293 int notanum = FALSE;
12294
12295 rettv->vval.v_number = -1;
12296
Bram Moolenaarbdace832019-03-02 10:13:42 +010012297 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012298 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012299 if (notanum)
12300 return;
12301 if (sign_id < 0)
12302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012303 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012304 return;
12305 }
12306
12307 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012308 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012309 if (group == NULL)
12310 return;
12311 if (group[0] == '\0')
12312 group = NULL; // global sign group
12313 else
12314 {
12315 group = vim_strsave(group);
12316 if (group == NULL)
12317 return;
12318 }
12319
12320 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012321 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012322 if (sign_name == NULL)
12323 goto cleanup;
12324
12325 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012326 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012327 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012328 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012329
12330 if (argvars[4].v_type != VAR_UNKNOWN)
12331 {
12332 if (argvars[4].v_type != VAR_DICT ||
12333 ((dict = argvars[4].vval.v_dict) == NULL))
12334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012335 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012336 goto cleanup;
12337 }
12338
12339 // Line number where the sign is to be placed
12340 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12341 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012342 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012343 if (notanum)
12344 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012345 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012346 }
12347 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12348 {
12349 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012350 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012351 if (notanum)
12352 goto cleanup;
12353 }
12354 }
12355
12356 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12357 rettv->vval.v_number = sign_id;
12358
12359cleanup:
12360 vim_free(group);
12361}
12362
12363/*
12364 * "sign_undefine()" function
12365 */
12366 static void
12367f_sign_undefine(typval_T *argvars, typval_T *rettv)
12368{
12369 char_u *name;
12370
12371 rettv->vval.v_number = -1;
12372
12373 if (argvars[0].v_type == VAR_UNKNOWN)
12374 {
12375 // Free all the signs
12376 free_signs();
12377 rettv->vval.v_number = 0;
12378 }
12379 else
12380 {
12381 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012382 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012383 if (name == NULL)
12384 return;
12385
12386 if (sign_undefine_by_name(name) == OK)
12387 rettv->vval.v_number = 0;
12388 }
12389}
12390
12391/*
12392 * "sign_unplace()" function
12393 */
12394 static void
12395f_sign_unplace(typval_T *argvars, typval_T *rettv)
12396{
12397 dict_T *dict;
12398 dictitem_T *di;
12399 int sign_id = 0;
12400 buf_T *buf = NULL;
12401 char_u *group = NULL;
12402
12403 rettv->vval.v_number = -1;
12404
12405 if (argvars[0].v_type != VAR_STRING)
12406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012407 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012408 return;
12409 }
12410
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012411 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012412 if (group[0] == '\0')
12413 group = NULL; // global sign group
12414 else
12415 {
12416 group = vim_strsave(group);
12417 if (group == NULL)
12418 return;
12419 }
12420
12421 if (argvars[1].v_type != VAR_UNKNOWN)
12422 {
12423 if (argvars[1].v_type != VAR_DICT)
12424 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012425 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012426 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012427 }
12428 dict = argvars[1].vval.v_dict;
12429
12430 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12431 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012432 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012433 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012434 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012435 }
12436 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12437 sign_id = dict_get_number(dict, (char_u *)"id");
12438 }
12439
12440 if (buf == NULL)
12441 {
12442 // Delete the sign in all the buffers
12443 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012444 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012445 rettv->vval.v_number = 0;
12446 }
12447 else
12448 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012449 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012450 rettv->vval.v_number = 0;
12451 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012452
12453cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012454 vim_free(group);
12455}
12456#endif
12457
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012458/*
12459 * "simplify()" function
12460 */
12461 static void
12462f_simplify(typval_T *argvars, typval_T *rettv)
12463{
12464 char_u *p;
12465
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012466 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012467 rettv->vval.v_string = vim_strsave(p);
12468 simplify_filename(rettv->vval.v_string); /* simplify in place */
12469 rettv->v_type = VAR_STRING;
12470}
12471
12472#ifdef FEAT_FLOAT
12473/*
12474 * "sin()" function
12475 */
12476 static void
12477f_sin(typval_T *argvars, typval_T *rettv)
12478{
12479 float_T f = 0.0;
12480
12481 rettv->v_type = VAR_FLOAT;
12482 if (get_float_arg(argvars, &f) == OK)
12483 rettv->vval.v_float = sin(f);
12484 else
12485 rettv->vval.v_float = 0.0;
12486}
12487
12488/*
12489 * "sinh()" function
12490 */
12491 static void
12492f_sinh(typval_T *argvars, typval_T *rettv)
12493{
12494 float_T f = 0.0;
12495
12496 rettv->v_type = VAR_FLOAT;
12497 if (get_float_arg(argvars, &f) == OK)
12498 rettv->vval.v_float = sinh(f);
12499 else
12500 rettv->vval.v_float = 0.0;
12501}
12502#endif
12503
Bram Moolenaareae1b912019-05-09 15:12:55 +020012504static int item_compare(const void *s1, const void *s2);
12505static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012506
12507/* struct used in the array that's given to qsort() */
12508typedef struct
12509{
12510 listitem_T *item;
12511 int idx;
12512} sortItem_T;
12513
12514/* struct storing information about current sort */
12515typedef struct
12516{
12517 int item_compare_ic;
12518 int item_compare_numeric;
12519 int item_compare_numbers;
12520#ifdef FEAT_FLOAT
12521 int item_compare_float;
12522#endif
12523 char_u *item_compare_func;
12524 partial_T *item_compare_partial;
12525 dict_T *item_compare_selfdict;
12526 int item_compare_func_err;
12527 int item_compare_keep_zero;
12528} sortinfo_T;
12529static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012530#define ITEM_COMPARE_FAIL 999
12531
12532/*
12533 * Compare functions for f_sort() and f_uniq() below.
12534 */
12535 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012536item_compare(const void *s1, const void *s2)
12537{
12538 sortItem_T *si1, *si2;
12539 typval_T *tv1, *tv2;
12540 char_u *p1, *p2;
12541 char_u *tofree1 = NULL, *tofree2 = NULL;
12542 int res;
12543 char_u numbuf1[NUMBUFLEN];
12544 char_u numbuf2[NUMBUFLEN];
12545
12546 si1 = (sortItem_T *)s1;
12547 si2 = (sortItem_T *)s2;
12548 tv1 = &si1->item->li_tv;
12549 tv2 = &si2->item->li_tv;
12550
12551 if (sortinfo->item_compare_numbers)
12552 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012553 varnumber_T v1 = tv_get_number(tv1);
12554 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012555
12556 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12557 }
12558
12559#ifdef FEAT_FLOAT
12560 if (sortinfo->item_compare_float)
12561 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012562 float_T v1 = tv_get_float(tv1);
12563 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012564
12565 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12566 }
12567#endif
12568
12569 /* tv2string() puts quotes around a string and allocates memory. Don't do
12570 * that for string variables. Use a single quote when comparing with a
12571 * non-string to do what the docs promise. */
12572 if (tv1->v_type == VAR_STRING)
12573 {
12574 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12575 p1 = (char_u *)"'";
12576 else
12577 p1 = tv1->vval.v_string;
12578 }
12579 else
12580 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12581 if (tv2->v_type == VAR_STRING)
12582 {
12583 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12584 p2 = (char_u *)"'";
12585 else
12586 p2 = tv2->vval.v_string;
12587 }
12588 else
12589 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12590 if (p1 == NULL)
12591 p1 = (char_u *)"";
12592 if (p2 == NULL)
12593 p2 = (char_u *)"";
12594 if (!sortinfo->item_compare_numeric)
12595 {
12596 if (sortinfo->item_compare_ic)
12597 res = STRICMP(p1, p2);
12598 else
12599 res = STRCMP(p1, p2);
12600 }
12601 else
12602 {
12603 double n1, n2;
12604 n1 = strtod((char *)p1, (char **)&p1);
12605 n2 = strtod((char *)p2, (char **)&p2);
12606 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12607 }
12608
12609 /* When the result would be zero, compare the item indexes. Makes the
12610 * sort stable. */
12611 if (res == 0 && !sortinfo->item_compare_keep_zero)
12612 res = si1->idx > si2->idx ? 1 : -1;
12613
12614 vim_free(tofree1);
12615 vim_free(tofree2);
12616 return res;
12617}
12618
12619 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012620item_compare2(const void *s1, const void *s2)
12621{
12622 sortItem_T *si1, *si2;
12623 int res;
12624 typval_T rettv;
12625 typval_T argv[3];
12626 int dummy;
12627 char_u *func_name;
12628 partial_T *partial = sortinfo->item_compare_partial;
12629
12630 /* shortcut after failure in previous call; compare all items equal */
12631 if (sortinfo->item_compare_func_err)
12632 return 0;
12633
12634 si1 = (sortItem_T *)s1;
12635 si2 = (sortItem_T *)s2;
12636
12637 if (partial == NULL)
12638 func_name = sortinfo->item_compare_func;
12639 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012640 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012641
12642 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12643 * in the copy without changing the original list items. */
12644 copy_tv(&si1->item->li_tv, &argv[0]);
12645 copy_tv(&si2->item->li_tv, &argv[1]);
12646
12647 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012648 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012649 partial, sortinfo->item_compare_selfdict);
12650 clear_tv(&argv[0]);
12651 clear_tv(&argv[1]);
12652
12653 if (res == FAIL)
12654 res = ITEM_COMPARE_FAIL;
12655 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012656 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012657 if (sortinfo->item_compare_func_err)
12658 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12659 clear_tv(&rettv);
12660
12661 /* When the result would be zero, compare the pointers themselves. Makes
12662 * the sort stable. */
12663 if (res == 0 && !sortinfo->item_compare_keep_zero)
12664 res = si1->idx > si2->idx ? 1 : -1;
12665
12666 return res;
12667}
12668
12669/*
12670 * "sort({list})" function
12671 */
12672 static void
12673do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12674{
12675 list_T *l;
12676 listitem_T *li;
12677 sortItem_T *ptrs;
12678 sortinfo_T *old_sortinfo;
12679 sortinfo_T info;
12680 long len;
12681 long i;
12682
12683 /* Pointer to current info struct used in compare function. Save and
12684 * restore the current one for nested calls. */
12685 old_sortinfo = sortinfo;
12686 sortinfo = &info;
12687
12688 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012689 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012690 else
12691 {
12692 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012693 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012694 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12695 TRUE))
12696 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012697 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012698
12699 len = list_len(l);
12700 if (len <= 1)
12701 goto theend; /* short list sorts pretty quickly */
12702
12703 info.item_compare_ic = FALSE;
12704 info.item_compare_numeric = FALSE;
12705 info.item_compare_numbers = FALSE;
12706#ifdef FEAT_FLOAT
12707 info.item_compare_float = FALSE;
12708#endif
12709 info.item_compare_func = NULL;
12710 info.item_compare_partial = NULL;
12711 info.item_compare_selfdict = NULL;
12712 if (argvars[1].v_type != VAR_UNKNOWN)
12713 {
12714 /* optional second argument: {func} */
12715 if (argvars[1].v_type == VAR_FUNC)
12716 info.item_compare_func = argvars[1].vval.v_string;
12717 else if (argvars[1].v_type == VAR_PARTIAL)
12718 info.item_compare_partial = argvars[1].vval.v_partial;
12719 else
12720 {
12721 int error = FALSE;
12722
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012723 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012724 if (error)
12725 goto theend; /* type error; errmsg already given */
12726 if (i == 1)
12727 info.item_compare_ic = TRUE;
12728 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012729 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012730 else if (i != 0)
12731 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012732 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012733 goto theend;
12734 }
12735 if (info.item_compare_func != NULL)
12736 {
12737 if (*info.item_compare_func == NUL)
12738 {
12739 /* empty string means default sort */
12740 info.item_compare_func = NULL;
12741 }
12742 else if (STRCMP(info.item_compare_func, "n") == 0)
12743 {
12744 info.item_compare_func = NULL;
12745 info.item_compare_numeric = TRUE;
12746 }
12747 else if (STRCMP(info.item_compare_func, "N") == 0)
12748 {
12749 info.item_compare_func = NULL;
12750 info.item_compare_numbers = TRUE;
12751 }
12752#ifdef FEAT_FLOAT
12753 else if (STRCMP(info.item_compare_func, "f") == 0)
12754 {
12755 info.item_compare_func = NULL;
12756 info.item_compare_float = TRUE;
12757 }
12758#endif
12759 else if (STRCMP(info.item_compare_func, "i") == 0)
12760 {
12761 info.item_compare_func = NULL;
12762 info.item_compare_ic = TRUE;
12763 }
12764 }
12765 }
12766
12767 if (argvars[2].v_type != VAR_UNKNOWN)
12768 {
12769 /* optional third argument: {dict} */
12770 if (argvars[2].v_type != VAR_DICT)
12771 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012772 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012773 goto theend;
12774 }
12775 info.item_compare_selfdict = argvars[2].vval.v_dict;
12776 }
12777 }
12778
12779 /* Make an array with each entry pointing to an item in the List. */
12780 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12781 if (ptrs == NULL)
12782 goto theend;
12783
12784 i = 0;
12785 if (sort)
12786 {
12787 /* sort(): ptrs will be the list to sort */
12788 for (li = l->lv_first; li != NULL; li = li->li_next)
12789 {
12790 ptrs[i].item = li;
12791 ptrs[i].idx = i;
12792 ++i;
12793 }
12794
12795 info.item_compare_func_err = FALSE;
12796 info.item_compare_keep_zero = FALSE;
12797 /* test the compare function */
12798 if ((info.item_compare_func != NULL
12799 || info.item_compare_partial != NULL)
12800 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12801 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012802 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012803 else
12804 {
12805 /* Sort the array with item pointers. */
12806 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12807 info.item_compare_func == NULL
12808 && info.item_compare_partial == NULL
12809 ? item_compare : item_compare2);
12810
12811 if (!info.item_compare_func_err)
12812 {
12813 /* Clear the List and append the items in sorted order. */
12814 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12815 l->lv_len = 0;
12816 for (i = 0; i < len; ++i)
12817 list_append(l, ptrs[i].item);
12818 }
12819 }
12820 }
12821 else
12822 {
12823 int (*item_compare_func_ptr)(const void *, const void *);
12824
12825 /* f_uniq(): ptrs will be a stack of items to remove */
12826 info.item_compare_func_err = FALSE;
12827 info.item_compare_keep_zero = TRUE;
12828 item_compare_func_ptr = info.item_compare_func != NULL
12829 || info.item_compare_partial != NULL
12830 ? item_compare2 : item_compare;
12831
12832 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12833 li = li->li_next)
12834 {
12835 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12836 == 0)
12837 ptrs[i++].item = li;
12838 if (info.item_compare_func_err)
12839 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012840 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012841 break;
12842 }
12843 }
12844
12845 if (!info.item_compare_func_err)
12846 {
12847 while (--i >= 0)
12848 {
12849 li = ptrs[i].item->li_next;
12850 ptrs[i].item->li_next = li->li_next;
12851 if (li->li_next != NULL)
12852 li->li_next->li_prev = ptrs[i].item;
12853 else
12854 l->lv_last = ptrs[i].item;
12855 list_fix_watch(l, li);
12856 listitem_free(li);
12857 l->lv_len--;
12858 }
12859 }
12860 }
12861
12862 vim_free(ptrs);
12863 }
12864theend:
12865 sortinfo = old_sortinfo;
12866}
12867
12868/*
12869 * "sort({list})" function
12870 */
12871 static void
12872f_sort(typval_T *argvars, typval_T *rettv)
12873{
12874 do_sort_uniq(argvars, rettv, TRUE);
12875}
12876
12877/*
12878 * "uniq({list})" function
12879 */
12880 static void
12881f_uniq(typval_T *argvars, typval_T *rettv)
12882{
12883 do_sort_uniq(argvars, rettv, FALSE);
12884}
12885
12886/*
12887 * "soundfold({word})" function
12888 */
12889 static void
12890f_soundfold(typval_T *argvars, typval_T *rettv)
12891{
12892 char_u *s;
12893
12894 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012895 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012896#ifdef FEAT_SPELL
12897 rettv->vval.v_string = eval_soundfold(s);
12898#else
12899 rettv->vval.v_string = vim_strsave(s);
12900#endif
12901}
12902
12903/*
12904 * "spellbadword()" function
12905 */
12906 static void
12907f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12908{
12909 char_u *word = (char_u *)"";
12910 hlf_T attr = HLF_COUNT;
12911 int len = 0;
12912
12913 if (rettv_list_alloc(rettv) == FAIL)
12914 return;
12915
12916#ifdef FEAT_SPELL
12917 if (argvars[0].v_type == VAR_UNKNOWN)
12918 {
12919 /* Find the start and length of the badly spelled word. */
12920 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12921 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012922 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012923 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012924 curwin->w_set_curswant = TRUE;
12925 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012926 }
12927 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12928 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012929 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012930 int capcol = -1;
12931
12932 if (str != NULL)
12933 {
12934 /* Check the argument for spelling. */
12935 while (*str != NUL)
12936 {
12937 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12938 if (attr != HLF_COUNT)
12939 {
12940 word = str;
12941 break;
12942 }
12943 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012944 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012945 }
12946 }
12947 }
12948#endif
12949
12950 list_append_string(rettv->vval.v_list, word, len);
12951 list_append_string(rettv->vval.v_list, (char_u *)(
12952 attr == HLF_SPB ? "bad" :
12953 attr == HLF_SPR ? "rare" :
12954 attr == HLF_SPL ? "local" :
12955 attr == HLF_SPC ? "caps" :
12956 ""), -1);
12957}
12958
12959/*
12960 * "spellsuggest()" function
12961 */
12962 static void
12963f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12964{
12965#ifdef FEAT_SPELL
12966 char_u *str;
12967 int typeerr = FALSE;
12968 int maxcount;
12969 garray_T ga;
12970 int i;
12971 listitem_T *li;
12972 int need_capital = FALSE;
12973#endif
12974
12975 if (rettv_list_alloc(rettv) == FAIL)
12976 return;
12977
12978#ifdef FEAT_SPELL
12979 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12980 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012981 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012982 if (argvars[1].v_type != VAR_UNKNOWN)
12983 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012984 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012985 if (maxcount <= 0)
12986 return;
12987 if (argvars[2].v_type != VAR_UNKNOWN)
12988 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012989 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012990 if (typeerr)
12991 return;
12992 }
12993 }
12994 else
12995 maxcount = 25;
12996
12997 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12998
12999 for (i = 0; i < ga.ga_len; ++i)
13000 {
13001 str = ((char_u **)ga.ga_data)[i];
13002
13003 li = listitem_alloc();
13004 if (li == NULL)
13005 vim_free(str);
13006 else
13007 {
13008 li->li_tv.v_type = VAR_STRING;
13009 li->li_tv.v_lock = 0;
13010 li->li_tv.vval.v_string = str;
13011 list_append(rettv->vval.v_list, li);
13012 }
13013 }
13014 ga_clear(&ga);
13015 }
13016#endif
13017}
13018
13019 static void
13020f_split(typval_T *argvars, typval_T *rettv)
13021{
13022 char_u *str;
13023 char_u *end;
13024 char_u *pat = NULL;
13025 regmatch_T regmatch;
13026 char_u patbuf[NUMBUFLEN];
13027 char_u *save_cpo;
13028 int match;
13029 colnr_T col = 0;
13030 int keepempty = FALSE;
13031 int typeerr = FALSE;
13032
13033 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13034 save_cpo = p_cpo;
13035 p_cpo = (char_u *)"";
13036
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013037 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013038 if (argvars[1].v_type != VAR_UNKNOWN)
13039 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013040 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013041 if (pat == NULL)
13042 typeerr = TRUE;
13043 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013044 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013045 }
13046 if (pat == NULL || *pat == NUL)
13047 pat = (char_u *)"[\\x01- ]\\+";
13048
13049 if (rettv_list_alloc(rettv) == FAIL)
13050 return;
13051 if (typeerr)
13052 return;
13053
13054 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13055 if (regmatch.regprog != NULL)
13056 {
13057 regmatch.rm_ic = FALSE;
13058 while (*str != NUL || keepempty)
13059 {
13060 if (*str == NUL)
13061 match = FALSE; /* empty item at the end */
13062 else
13063 match = vim_regexec_nl(&regmatch, str, col);
13064 if (match)
13065 end = regmatch.startp[0];
13066 else
13067 end = str + STRLEN(str);
13068 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
13069 && *str != NUL && match && end < regmatch.endp[0]))
13070 {
13071 if (list_append_string(rettv->vval.v_list, str,
13072 (int)(end - str)) == FAIL)
13073 break;
13074 }
13075 if (!match)
13076 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010013077 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013078 if (regmatch.endp[0] > str)
13079 col = 0;
13080 else
Bram Moolenaar13505972019-01-24 15:04:48 +010013081 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013082 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013083 str = regmatch.endp[0];
13084 }
13085
13086 vim_regfree(regmatch.regprog);
13087 }
13088
13089 p_cpo = save_cpo;
13090}
13091
13092#ifdef FEAT_FLOAT
13093/*
13094 * "sqrt()" function
13095 */
13096 static void
13097f_sqrt(typval_T *argvars, typval_T *rettv)
13098{
13099 float_T f = 0.0;
13100
13101 rettv->v_type = VAR_FLOAT;
13102 if (get_float_arg(argvars, &f) == OK)
13103 rettv->vval.v_float = sqrt(f);
13104 else
13105 rettv->vval.v_float = 0.0;
13106}
13107
13108/*
13109 * "str2float()" function
13110 */
13111 static void
13112f_str2float(typval_T *argvars, typval_T *rettv)
13113{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013114 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013115 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013116
Bram Moolenaar08243d22017-01-10 16:12:29 +010013117 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013118 p = skipwhite(p + 1);
13119 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013120 if (isneg)
13121 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013122 rettv->v_type = VAR_FLOAT;
13123}
13124#endif
13125
13126/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013127 * "str2list()" function
13128 */
13129 static void
13130f_str2list(typval_T *argvars, typval_T *rettv)
13131{
13132 char_u *p;
13133 int utf8 = FALSE;
13134
13135 if (rettv_list_alloc(rettv) == FAIL)
13136 return;
13137
13138 if (argvars[1].v_type != VAR_UNKNOWN)
13139 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13140
13141 p = tv_get_string(&argvars[0]);
13142
13143 if (has_mbyte || utf8)
13144 {
13145 int (*ptr2len)(char_u *);
13146 int (*ptr2char)(char_u *);
13147
13148 if (utf8 || enc_utf8)
13149 {
13150 ptr2len = utf_ptr2len;
13151 ptr2char = utf_ptr2char;
13152 }
13153 else
13154 {
13155 ptr2len = mb_ptr2len;
13156 ptr2char = mb_ptr2char;
13157 }
13158
13159 for ( ; *p != NUL; p += (*ptr2len)(p))
13160 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13161 }
13162 else
13163 for ( ; *p != NUL; ++p)
13164 list_append_number(rettv->vval.v_list, *p);
13165}
13166
13167/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013168 * "str2nr()" function
13169 */
13170 static void
13171f_str2nr(typval_T *argvars, typval_T *rettv)
13172{
13173 int base = 10;
13174 char_u *p;
13175 varnumber_T n;
13176 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013177 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013178
13179 if (argvars[1].v_type != VAR_UNKNOWN)
13180 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013181 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013182 if (base != 2 && base != 8 && base != 10 && base != 16)
13183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013184 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013185 return;
13186 }
13187 }
13188
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013189 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013190 isneg = (*p == '-');
13191 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013192 p = skipwhite(p + 1);
13193 switch (base)
13194 {
13195 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13196 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13197 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13198 default: what = 0;
13199 }
13200 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013201 if (isneg)
13202 rettv->vval.v_number = -n;
13203 else
13204 rettv->vval.v_number = n;
13205
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013206}
13207
13208#ifdef HAVE_STRFTIME
13209/*
13210 * "strftime({format}[, {time}])" function
13211 */
13212 static void
13213f_strftime(typval_T *argvars, typval_T *rettv)
13214{
13215 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013216# ifdef HAVE_LOCALTIME_R
13217 struct tm tmval;
13218# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013219 struct tm *curtime;
13220 time_t seconds;
13221 char_u *p;
13222
13223 rettv->v_type = VAR_STRING;
13224
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013225 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013226 if (argvars[1].v_type == VAR_UNKNOWN)
13227 seconds = time(NULL);
13228 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013229 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013230# ifdef HAVE_LOCALTIME_R
13231 curtime = localtime_r(&seconds, &tmval);
13232# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013233 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013234# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013235 /* MSVC returns NULL for an invalid value of seconds. */
13236 if (curtime == NULL)
13237 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13238 else
13239 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013240 vimconv_T conv;
13241 char_u *enc;
13242
13243 conv.vc_type = CONV_NONE;
13244 enc = enc_locale();
13245 convert_setup(&conv, p_enc, enc);
13246 if (conv.vc_type != CONV_NONE)
13247 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013248 if (p != NULL)
13249 (void)strftime((char *)result_buf, sizeof(result_buf),
13250 (char *)p, curtime);
13251 else
13252 result_buf[0] = NUL;
13253
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013254 if (conv.vc_type != CONV_NONE)
13255 vim_free(p);
13256 convert_setup(&conv, enc, p_enc);
13257 if (conv.vc_type != CONV_NONE)
13258 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13259 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013260 rettv->vval.v_string = vim_strsave(result_buf);
13261
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013262 /* Release conversion descriptors */
13263 convert_setup(&conv, NULL, NULL);
13264 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013265 }
13266}
13267#endif
13268
13269/*
13270 * "strgetchar()" function
13271 */
13272 static void
13273f_strgetchar(typval_T *argvars, typval_T *rettv)
13274{
13275 char_u *str;
13276 int len;
13277 int error = FALSE;
13278 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013279 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013280
13281 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013282 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013283 if (str == NULL)
13284 return;
13285 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013286 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013287 if (error)
13288 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013289
Bram Moolenaar13505972019-01-24 15:04:48 +010013290 while (charidx >= 0 && byteidx < len)
13291 {
13292 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013293 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013294 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13295 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013296 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013297 --charidx;
13298 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013299 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013300}
13301
13302/*
13303 * "stridx()" function
13304 */
13305 static void
13306f_stridx(typval_T *argvars, typval_T *rettv)
13307{
13308 char_u buf[NUMBUFLEN];
13309 char_u *needle;
13310 char_u *haystack;
13311 char_u *save_haystack;
13312 char_u *pos;
13313 int start_idx;
13314
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013315 needle = tv_get_string_chk(&argvars[1]);
13316 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013317 rettv->vval.v_number = -1;
13318 if (needle == NULL || haystack == NULL)
13319 return; /* type error; errmsg already given */
13320
13321 if (argvars[2].v_type != VAR_UNKNOWN)
13322 {
13323 int error = FALSE;
13324
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013325 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013326 if (error || start_idx >= (int)STRLEN(haystack))
13327 return;
13328 if (start_idx >= 0)
13329 haystack += start_idx;
13330 }
13331
13332 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13333 if (pos != NULL)
13334 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13335}
13336
13337/*
13338 * "string()" function
13339 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013340 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013341f_string(typval_T *argvars, typval_T *rettv)
13342{
13343 char_u *tofree;
13344 char_u numbuf[NUMBUFLEN];
13345
13346 rettv->v_type = VAR_STRING;
13347 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13348 get_copyID());
13349 /* Make a copy if we have a value but it's not in allocated memory. */
13350 if (rettv->vval.v_string != NULL && tofree == NULL)
13351 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13352}
13353
13354/*
13355 * "strlen()" function
13356 */
13357 static void
13358f_strlen(typval_T *argvars, typval_T *rettv)
13359{
13360 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013361 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013362}
13363
13364/*
13365 * "strchars()" function
13366 */
13367 static void
13368f_strchars(typval_T *argvars, typval_T *rettv)
13369{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013370 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013371 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013372 varnumber_T len = 0;
13373 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013374
13375 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013376 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013377 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013378 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013379 else
13380 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013381 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13382 while (*s != NUL)
13383 {
13384 func_mb_ptr2char_adv(&s);
13385 ++len;
13386 }
13387 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013388 }
13389}
13390
13391/*
13392 * "strdisplaywidth()" function
13393 */
13394 static void
13395f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13396{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013397 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013398 int col = 0;
13399
13400 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013401 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013402
13403 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13404}
13405
13406/*
13407 * "strwidth()" function
13408 */
13409 static void
13410f_strwidth(typval_T *argvars, typval_T *rettv)
13411{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013412 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013413
Bram Moolenaar13505972019-01-24 15:04:48 +010013414 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013415}
13416
13417/*
13418 * "strcharpart()" function
13419 */
13420 static void
13421f_strcharpart(typval_T *argvars, typval_T *rettv)
13422{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013423 char_u *p;
13424 int nchar;
13425 int nbyte = 0;
13426 int charlen;
13427 int len = 0;
13428 int slen;
13429 int error = FALSE;
13430
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013431 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013432 slen = (int)STRLEN(p);
13433
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013434 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013435 if (!error)
13436 {
13437 if (nchar > 0)
13438 while (nchar > 0 && nbyte < slen)
13439 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013440 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013441 --nchar;
13442 }
13443 else
13444 nbyte = nchar;
13445 if (argvars[2].v_type != VAR_UNKNOWN)
13446 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013447 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013448 while (charlen > 0 && nbyte + len < slen)
13449 {
13450 int off = nbyte + len;
13451
13452 if (off < 0)
13453 len += 1;
13454 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013455 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013456 --charlen;
13457 }
13458 }
13459 else
13460 len = slen - nbyte; /* default: all bytes that are available. */
13461 }
13462
13463 /*
13464 * Only return the overlap between the specified part and the actual
13465 * string.
13466 */
13467 if (nbyte < 0)
13468 {
13469 len += nbyte;
13470 nbyte = 0;
13471 }
13472 else if (nbyte > slen)
13473 nbyte = slen;
13474 if (len < 0)
13475 len = 0;
13476 else if (nbyte + len > slen)
13477 len = slen - nbyte;
13478
13479 rettv->v_type = VAR_STRING;
13480 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013481}
13482
13483/*
13484 * "strpart()" function
13485 */
13486 static void
13487f_strpart(typval_T *argvars, typval_T *rettv)
13488{
13489 char_u *p;
13490 int n;
13491 int len;
13492 int slen;
13493 int error = FALSE;
13494
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013495 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013496 slen = (int)STRLEN(p);
13497
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013498 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013499 if (error)
13500 len = 0;
13501 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013502 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013503 else
13504 len = slen - n; /* default len: all bytes that are available. */
13505
13506 /*
13507 * Only return the overlap between the specified part and the actual
13508 * string.
13509 */
13510 if (n < 0)
13511 {
13512 len += n;
13513 n = 0;
13514 }
13515 else if (n > slen)
13516 n = slen;
13517 if (len < 0)
13518 len = 0;
13519 else if (n + len > slen)
13520 len = slen - n;
13521
13522 rettv->v_type = VAR_STRING;
13523 rettv->vval.v_string = vim_strnsave(p + n, len);
13524}
13525
13526/*
13527 * "strridx()" function
13528 */
13529 static void
13530f_strridx(typval_T *argvars, typval_T *rettv)
13531{
13532 char_u buf[NUMBUFLEN];
13533 char_u *needle;
13534 char_u *haystack;
13535 char_u *rest;
13536 char_u *lastmatch = NULL;
13537 int haystack_len, end_idx;
13538
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013539 needle = tv_get_string_chk(&argvars[1]);
13540 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013541
13542 rettv->vval.v_number = -1;
13543 if (needle == NULL || haystack == NULL)
13544 return; /* type error; errmsg already given */
13545
13546 haystack_len = (int)STRLEN(haystack);
13547 if (argvars[2].v_type != VAR_UNKNOWN)
13548 {
13549 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013550 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013551 if (end_idx < 0)
13552 return; /* can never find a match */
13553 }
13554 else
13555 end_idx = haystack_len;
13556
13557 if (*needle == NUL)
13558 {
13559 /* Empty string matches past the end. */
13560 lastmatch = haystack + end_idx;
13561 }
13562 else
13563 {
13564 for (rest = haystack; *rest != '\0'; ++rest)
13565 {
13566 rest = (char_u *)strstr((char *)rest, (char *)needle);
13567 if (rest == NULL || rest > haystack + end_idx)
13568 break;
13569 lastmatch = rest;
13570 }
13571 }
13572
13573 if (lastmatch == NULL)
13574 rettv->vval.v_number = -1;
13575 else
13576 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13577}
13578
13579/*
13580 * "strtrans()" function
13581 */
13582 static void
13583f_strtrans(typval_T *argvars, typval_T *rettv)
13584{
13585 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013586 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013587}
13588
13589/*
13590 * "submatch()" function
13591 */
13592 static void
13593f_submatch(typval_T *argvars, typval_T *rettv)
13594{
13595 int error = FALSE;
13596 int no;
13597 int retList = 0;
13598
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013599 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013600 if (error)
13601 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013602 if (no < 0 || no >= NSUBEXP)
13603 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013604 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013605 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013606 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013607 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013608 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013609 if (error)
13610 return;
13611
13612 if (retList == 0)
13613 {
13614 rettv->v_type = VAR_STRING;
13615 rettv->vval.v_string = reg_submatch(no);
13616 }
13617 else
13618 {
13619 rettv->v_type = VAR_LIST;
13620 rettv->vval.v_list = reg_submatch_list(no);
13621 }
13622}
13623
13624/*
13625 * "substitute()" function
13626 */
13627 static void
13628f_substitute(typval_T *argvars, typval_T *rettv)
13629{
13630 char_u patbuf[NUMBUFLEN];
13631 char_u subbuf[NUMBUFLEN];
13632 char_u flagsbuf[NUMBUFLEN];
13633
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013634 char_u *str = tv_get_string_chk(&argvars[0]);
13635 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013636 char_u *sub = NULL;
13637 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013638 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013639
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013640 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13641 expr = &argvars[2];
13642 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013643 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013644
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013645 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013646 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13647 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013648 rettv->vval.v_string = NULL;
13649 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013650 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013651}
13652
13653/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013654 * "swapinfo(swap_filename)" function
13655 */
13656 static void
13657f_swapinfo(typval_T *argvars, typval_T *rettv)
13658{
13659 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013660 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013661}
13662
13663/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013664 * "swapname(expr)" function
13665 */
13666 static void
13667f_swapname(typval_T *argvars, typval_T *rettv)
13668{
13669 buf_T *buf;
13670
13671 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013672 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013673 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13674 || buf->b_ml.ml_mfp->mf_fname == NULL)
13675 rettv->vval.v_string = NULL;
13676 else
13677 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13678}
13679
13680/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013681 * "synID(lnum, col, trans)" function
13682 */
13683 static void
13684f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13685{
13686 int id = 0;
13687#ifdef FEAT_SYN_HL
13688 linenr_T lnum;
13689 colnr_T col;
13690 int trans;
13691 int transerr = FALSE;
13692
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013693 lnum = tv_get_lnum(argvars); /* -1 on type error */
13694 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13695 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013696
13697 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13698 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13699 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13700#endif
13701
13702 rettv->vval.v_number = id;
13703}
13704
13705/*
13706 * "synIDattr(id, what [, mode])" function
13707 */
13708 static void
13709f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13710{
13711 char_u *p = NULL;
13712#ifdef FEAT_SYN_HL
13713 int id;
13714 char_u *what;
13715 char_u *mode;
13716 char_u modebuf[NUMBUFLEN];
13717 int modec;
13718
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013719 id = (int)tv_get_number(&argvars[0]);
13720 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013721 if (argvars[2].v_type != VAR_UNKNOWN)
13722 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013723 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013724 modec = TOLOWER_ASC(mode[0]);
13725 if (modec != 't' && modec != 'c' && modec != 'g')
13726 modec = 0; /* replace invalid with current */
13727 }
13728 else
13729 {
13730#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13731 if (USE_24BIT)
13732 modec = 'g';
13733 else
13734#endif
13735 if (t_colors > 1)
13736 modec = 'c';
13737 else
13738 modec = 't';
13739 }
13740
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013741 switch (TOLOWER_ASC(what[0]))
13742 {
13743 case 'b':
13744 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13745 p = highlight_color(id, what, modec);
13746 else /* bold */
13747 p = highlight_has_attr(id, HL_BOLD, modec);
13748 break;
13749
13750 case 'f': /* fg[#] or font */
13751 p = highlight_color(id, what, modec);
13752 break;
13753
13754 case 'i':
13755 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13756 p = highlight_has_attr(id, HL_INVERSE, modec);
13757 else /* italic */
13758 p = highlight_has_attr(id, HL_ITALIC, modec);
13759 break;
13760
13761 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013762 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013763 break;
13764
13765 case 'r': /* reverse */
13766 p = highlight_has_attr(id, HL_INVERSE, modec);
13767 break;
13768
13769 case 's':
13770 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13771 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013772 /* strikeout */
13773 else if (TOLOWER_ASC(what[1]) == 't' &&
13774 TOLOWER_ASC(what[2]) == 'r')
13775 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013776 else /* standout */
13777 p = highlight_has_attr(id, HL_STANDOUT, modec);
13778 break;
13779
13780 case 'u':
13781 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13782 /* underline */
13783 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13784 else
13785 /* undercurl */
13786 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13787 break;
13788 }
13789
13790 if (p != NULL)
13791 p = vim_strsave(p);
13792#endif
13793 rettv->v_type = VAR_STRING;
13794 rettv->vval.v_string = p;
13795}
13796
13797/*
13798 * "synIDtrans(id)" function
13799 */
13800 static void
13801f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13802{
13803 int id;
13804
13805#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013806 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013807
13808 if (id > 0)
13809 id = syn_get_final_id(id);
13810 else
13811#endif
13812 id = 0;
13813
13814 rettv->vval.v_number = id;
13815}
13816
13817/*
13818 * "synconcealed(lnum, col)" function
13819 */
13820 static void
13821f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13822{
13823#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13824 linenr_T lnum;
13825 colnr_T col;
13826 int syntax_flags = 0;
13827 int cchar;
13828 int matchid = 0;
13829 char_u str[NUMBUFLEN];
13830#endif
13831
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013832 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013833
13834#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013835 lnum = tv_get_lnum(argvars); /* -1 on type error */
13836 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013837
13838 vim_memset(str, NUL, sizeof(str));
13839
13840 if (rettv_list_alloc(rettv) != FAIL)
13841 {
13842 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13843 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13844 && curwin->w_p_cole > 0)
13845 {
13846 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13847 syntax_flags = get_syntax_info(&matchid);
13848
13849 /* get the conceal character */
13850 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13851 {
13852 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013853 if (cchar == NUL && curwin->w_p_cole == 1)
13854 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013855 if (cchar != NUL)
13856 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013857 if (has_mbyte)
13858 (*mb_char2bytes)(cchar, str);
13859 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013860 str[0] = cchar;
13861 }
13862 }
13863 }
13864
13865 list_append_number(rettv->vval.v_list,
13866 (syntax_flags & HL_CONCEAL) != 0);
13867 /* -1 to auto-determine strlen */
13868 list_append_string(rettv->vval.v_list, str, -1);
13869 list_append_number(rettv->vval.v_list, matchid);
13870 }
13871#endif
13872}
13873
13874/*
13875 * "synstack(lnum, col)" function
13876 */
13877 static void
13878f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13879{
13880#ifdef FEAT_SYN_HL
13881 linenr_T lnum;
13882 colnr_T col;
13883 int i;
13884 int id;
13885#endif
13886
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013887 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013888
13889#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013890 lnum = tv_get_lnum(argvars); /* -1 on type error */
13891 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013892
13893 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13894 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13895 && rettv_list_alloc(rettv) != FAIL)
13896 {
13897 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13898 for (i = 0; ; ++i)
13899 {
13900 id = syn_get_stack_item(i);
13901 if (id < 0)
13902 break;
13903 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13904 break;
13905 }
13906 }
13907#endif
13908}
13909
13910 static void
13911get_cmd_output_as_rettv(
13912 typval_T *argvars,
13913 typval_T *rettv,
13914 int retlist)
13915{
13916 char_u *res = NULL;
13917 char_u *p;
13918 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013919 int err = FALSE;
13920 FILE *fd;
13921 list_T *list = NULL;
13922 int flags = SHELL_SILENT;
13923
13924 rettv->v_type = VAR_STRING;
13925 rettv->vval.v_string = NULL;
13926 if (check_restricted() || check_secure())
13927 goto errret;
13928
13929 if (argvars[1].v_type != VAR_UNKNOWN)
13930 {
13931 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013932 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013933 * command.
13934 */
13935 if ((infile = vim_tempname('i', TRUE)) == NULL)
13936 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013937 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013938 goto errret;
13939 }
13940
13941 fd = mch_fopen((char *)infile, WRITEBIN);
13942 if (fd == NULL)
13943 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013944 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013945 goto errret;
13946 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013947 if (argvars[1].v_type == VAR_NUMBER)
13948 {
13949 linenr_T lnum;
13950 buf_T *buf;
13951
13952 buf = buflist_findnr(argvars[1].vval.v_number);
13953 if (buf == NULL)
13954 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013955 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013956 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013957 goto errret;
13958 }
13959
13960 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13961 {
13962 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13963 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13964 {
13965 err = TRUE;
13966 break;
13967 }
13968 if (putc(NL, fd) == EOF)
13969 {
13970 err = TRUE;
13971 break;
13972 }
13973 }
13974 }
13975 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013976 {
13977 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13978 err = TRUE;
13979 }
13980 else
13981 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013982 size_t len;
13983 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013984
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013985 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013986 if (p == NULL)
13987 {
13988 fclose(fd);
13989 goto errret; /* type error; errmsg already given */
13990 }
13991 len = STRLEN(p);
13992 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13993 err = TRUE;
13994 }
13995 if (fclose(fd) != 0)
13996 err = TRUE;
13997 if (err)
13998 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013999 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014000 goto errret;
14001 }
14002 }
14003
14004 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
14005 * echoes typeahead, that messes up the display. */
14006 if (!msg_silent)
14007 flags += SHELL_COOKED;
14008
14009 if (retlist)
14010 {
14011 int len;
14012 listitem_T *li;
14013 char_u *s = NULL;
14014 char_u *start;
14015 char_u *end;
14016 int i;
14017
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014018 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014019 if (res == NULL)
14020 goto errret;
14021
14022 list = list_alloc();
14023 if (list == NULL)
14024 goto errret;
14025
14026 for (i = 0; i < len; ++i)
14027 {
14028 start = res + i;
14029 while (i < len && res[i] != NL)
14030 ++i;
14031 end = res + i;
14032
14033 s = alloc((unsigned)(end - start + 1));
14034 if (s == NULL)
14035 goto errret;
14036
14037 for (p = s; start < end; ++p, ++start)
14038 *p = *start == NUL ? NL : *start;
14039 *p = NUL;
14040
14041 li = listitem_alloc();
14042 if (li == NULL)
14043 {
14044 vim_free(s);
14045 goto errret;
14046 }
14047 li->li_tv.v_type = VAR_STRING;
14048 li->li_tv.v_lock = 0;
14049 li->li_tv.vval.v_string = s;
14050 list_append(list, li);
14051 }
14052
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014053 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014054 list = NULL;
14055 }
14056 else
14057 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014058 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010014059#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014060 /* translate <CR><NL> into <NL> */
14061 if (res != NULL)
14062 {
14063 char_u *s, *d;
14064
14065 d = res;
14066 for (s = res; *s; ++s)
14067 {
14068 if (s[0] == CAR && s[1] == NL)
14069 ++s;
14070 *d++ = *s;
14071 }
14072 *d = NUL;
14073 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014074#endif
14075 rettv->vval.v_string = res;
14076 res = NULL;
14077 }
14078
14079errret:
14080 if (infile != NULL)
14081 {
14082 mch_remove(infile);
14083 vim_free(infile);
14084 }
14085 if (res != NULL)
14086 vim_free(res);
14087 if (list != NULL)
14088 list_free(list);
14089}
14090
14091/*
14092 * "system()" function
14093 */
14094 static void
14095f_system(typval_T *argvars, typval_T *rettv)
14096{
14097 get_cmd_output_as_rettv(argvars, rettv, FALSE);
14098}
14099
14100/*
14101 * "systemlist()" function
14102 */
14103 static void
14104f_systemlist(typval_T *argvars, typval_T *rettv)
14105{
14106 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14107}
14108
14109/*
14110 * "tabpagebuflist()" function
14111 */
14112 static void
14113f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14114{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014115 tabpage_T *tp;
14116 win_T *wp = NULL;
14117
14118 if (argvars[0].v_type == VAR_UNKNOWN)
14119 wp = firstwin;
14120 else
14121 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014122 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014123 if (tp != NULL)
14124 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14125 }
14126 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14127 {
14128 for (; wp != NULL; wp = wp->w_next)
14129 if (list_append_number(rettv->vval.v_list,
14130 wp->w_buffer->b_fnum) == FAIL)
14131 break;
14132 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014133}
14134
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014135/*
14136 * "tabpagenr()" function
14137 */
14138 static void
14139f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14140{
14141 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014142 char_u *arg;
14143
14144 if (argvars[0].v_type != VAR_UNKNOWN)
14145 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014146 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014147 nr = 0;
14148 if (arg != NULL)
14149 {
14150 if (STRCMP(arg, "$") == 0)
14151 nr = tabpage_index(NULL) - 1;
14152 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014153 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014154 }
14155 }
14156 else
14157 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014158 rettv->vval.v_number = nr;
14159}
14160
14161
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014162/*
14163 * Common code for tabpagewinnr() and winnr().
14164 */
14165 static int
14166get_winnr(tabpage_T *tp, typval_T *argvar)
14167{
14168 win_T *twin;
14169 int nr = 1;
14170 win_T *wp;
14171 char_u *arg;
14172
14173 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14174 if (argvar->v_type != VAR_UNKNOWN)
14175 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014176 int invalid_arg = FALSE;
14177
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014178 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014179 if (arg == NULL)
14180 nr = 0; /* type error; errmsg already given */
14181 else if (STRCMP(arg, "$") == 0)
14182 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14183 else if (STRCMP(arg, "#") == 0)
14184 {
14185 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14186 if (twin == NULL)
14187 nr = 0;
14188 }
14189 else
14190 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014191 long count;
14192 char_u *endp;
14193
14194 // Extract the window count (if specified). e.g. winnr('3j')
14195 count = strtol((char *)arg, (char **)&endp, 10);
14196 if (count <= 0)
14197 count = 1; // if count is not specified, default to 1
14198 if (endp != NULL && *endp != '\0')
14199 {
14200 if (STRCMP(endp, "j") == 0)
14201 twin = win_vert_neighbor(tp, twin, FALSE, count);
14202 else if (STRCMP(endp, "k") == 0)
14203 twin = win_vert_neighbor(tp, twin, TRUE, count);
14204 else if (STRCMP(endp, "h") == 0)
14205 twin = win_horz_neighbor(tp, twin, TRUE, count);
14206 else if (STRCMP(endp, "l") == 0)
14207 twin = win_horz_neighbor(tp, twin, FALSE, count);
14208 else
14209 invalid_arg = TRUE;
14210 }
14211 else
14212 invalid_arg = TRUE;
14213 }
14214
14215 if (invalid_arg)
14216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014217 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014218 nr = 0;
14219 }
14220 }
14221
14222 if (nr > 0)
14223 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14224 wp != twin; wp = wp->w_next)
14225 {
14226 if (wp == NULL)
14227 {
14228 /* didn't find it in this tabpage */
14229 nr = 0;
14230 break;
14231 }
14232 ++nr;
14233 }
14234 return nr;
14235}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014236
14237/*
14238 * "tabpagewinnr()" function
14239 */
14240 static void
14241f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14242{
14243 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014244 tabpage_T *tp;
14245
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014246 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014247 if (tp == NULL)
14248 nr = 0;
14249 else
14250 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014251 rettv->vval.v_number = nr;
14252}
14253
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014254/*
14255 * "tagfiles()" function
14256 */
14257 static void
14258f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14259{
14260 char_u *fname;
14261 tagname_T tn;
14262 int first;
14263
14264 if (rettv_list_alloc(rettv) == FAIL)
14265 return;
14266 fname = alloc(MAXPATHL);
14267 if (fname == NULL)
14268 return;
14269
14270 for (first = TRUE; ; first = FALSE)
14271 if (get_tagfname(&tn, first, fname) == FAIL
14272 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14273 break;
14274 tagname_free(&tn);
14275 vim_free(fname);
14276}
14277
14278/*
14279 * "taglist()" function
14280 */
14281 static void
14282f_taglist(typval_T *argvars, typval_T *rettv)
14283{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014284 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014285 char_u *tag_pattern;
14286
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014287 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014288
14289 rettv->vval.v_number = FALSE;
14290 if (*tag_pattern == NUL)
14291 return;
14292
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014293 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014294 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014295 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014296 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014297}
14298
14299/*
14300 * "tempname()" function
14301 */
14302 static void
14303f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14304{
14305 static int x = 'A';
14306
14307 rettv->v_type = VAR_STRING;
14308 rettv->vval.v_string = vim_tempname(x, FALSE);
14309
14310 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14311 * names. Skip 'I' and 'O', they are used for shell redirection. */
14312 do
14313 {
14314 if (x == 'Z')
14315 x = '0';
14316 else if (x == '9')
14317 x = 'A';
14318 else
14319 {
14320#ifdef EBCDIC
14321 if (x == 'I')
14322 x = 'J';
14323 else if (x == 'R')
14324 x = 'S';
14325 else
14326#endif
14327 ++x;
14328 }
14329 } while (x == 'I' || x == 'O');
14330}
14331
14332#ifdef FEAT_FLOAT
14333/*
14334 * "tan()" function
14335 */
14336 static void
14337f_tan(typval_T *argvars, typval_T *rettv)
14338{
14339 float_T f = 0.0;
14340
14341 rettv->v_type = VAR_FLOAT;
14342 if (get_float_arg(argvars, &f) == OK)
14343 rettv->vval.v_float = tan(f);
14344 else
14345 rettv->vval.v_float = 0.0;
14346}
14347
14348/*
14349 * "tanh()" function
14350 */
14351 static void
14352f_tanh(typval_T *argvars, typval_T *rettv)
14353{
14354 float_T f = 0.0;
14355
14356 rettv->v_type = VAR_FLOAT;
14357 if (get_float_arg(argvars, &f) == OK)
14358 rettv->vval.v_float = tanh(f);
14359 else
14360 rettv->vval.v_float = 0.0;
14361}
14362#endif
14363
14364/*
14365 * "test_alloc_fail(id, countdown, repeat)" function
14366 */
14367 static void
14368f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14369{
14370 if (argvars[0].v_type != VAR_NUMBER
14371 || argvars[0].vval.v_number <= 0
14372 || argvars[1].v_type != VAR_NUMBER
14373 || argvars[1].vval.v_number < 0
14374 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014375 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014376 else
14377 {
14378 alloc_fail_id = argvars[0].vval.v_number;
14379 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014380 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014381 alloc_fail_countdown = argvars[1].vval.v_number;
14382 alloc_fail_repeat = argvars[2].vval.v_number;
14383 did_outofmem_msg = FALSE;
14384 }
14385}
14386
14387/*
14388 * "test_autochdir()"
14389 */
14390 static void
14391f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14392{
14393#if defined(FEAT_AUTOCHDIR)
14394 test_autochdir = TRUE;
14395#endif
14396}
14397
14398/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014399 * "test_feedinput()"
14400 */
14401 static void
14402f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14403{
14404#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014405 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014406
14407 if (val != NULL)
14408 {
14409 trash_input_buf();
14410 add_to_input_buf_csi(val, (int)STRLEN(val));
14411 }
14412#endif
14413}
14414
14415/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014416 * "test_option_not_set({name})" function
14417 */
14418 static void
14419f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14420{
14421 char_u *name = (char_u *)"";
14422
14423 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014424 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014425 else
14426 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014427 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014428 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014429 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014430 }
14431}
14432
14433/*
14434 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014435 */
14436 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014437f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014438{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014439 char_u *name = (char_u *)"";
14440 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014441 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014442
14443 if (argvars[0].v_type != VAR_STRING
14444 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014445 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014446 else
14447 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014448 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014449 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014450
14451 if (STRCMP(name, (char_u *)"redraw") == 0)
14452 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014453 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14454 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014455 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14456 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014457 else if (STRCMP(name, (char_u *)"starting") == 0)
14458 {
14459 if (val)
14460 {
14461 if (save_starting < 0)
14462 save_starting = starting;
14463 starting = 0;
14464 }
14465 else
14466 {
14467 starting = save_starting;
14468 save_starting = -1;
14469 }
14470 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014471 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14472 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014473 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14474 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014475 else if (STRCMP(name, (char_u *)"ALL") == 0)
14476 {
14477 disable_char_avail_for_testing = FALSE;
14478 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014479 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014480 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014481 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014482 if (save_starting >= 0)
14483 {
14484 starting = save_starting;
14485 save_starting = -1;
14486 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014487 }
14488 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014489 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014490 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014491}
14492
14493/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014494 * "test_refcount({expr})" function
14495 */
14496 static void
14497f_test_refcount(typval_T *argvars, typval_T *rettv)
14498{
14499 int retval = -1;
14500
14501 switch (argvars[0].v_type)
14502 {
14503 case VAR_UNKNOWN:
14504 case VAR_NUMBER:
14505 case VAR_FLOAT:
14506 case VAR_SPECIAL:
14507 case VAR_STRING:
14508 break;
14509 case VAR_JOB:
14510#ifdef FEAT_JOB_CHANNEL
14511 if (argvars[0].vval.v_job != NULL)
14512 retval = argvars[0].vval.v_job->jv_refcount - 1;
14513#endif
14514 break;
14515 case VAR_CHANNEL:
14516#ifdef FEAT_JOB_CHANNEL
14517 if (argvars[0].vval.v_channel != NULL)
14518 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14519#endif
14520 break;
14521 case VAR_FUNC:
14522 if (argvars[0].vval.v_string != NULL)
14523 {
14524 ufunc_T *fp;
14525
14526 fp = find_func(argvars[0].vval.v_string);
14527 if (fp != NULL)
14528 retval = fp->uf_refcount;
14529 }
14530 break;
14531 case VAR_PARTIAL:
14532 if (argvars[0].vval.v_partial != NULL)
14533 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14534 break;
14535 case VAR_BLOB:
14536 if (argvars[0].vval.v_blob != NULL)
14537 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14538 break;
14539 case VAR_LIST:
14540 if (argvars[0].vval.v_list != NULL)
14541 retval = argvars[0].vval.v_list->lv_refcount - 1;
14542 break;
14543 case VAR_DICT:
14544 if (argvars[0].vval.v_dict != NULL)
14545 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14546 break;
14547 }
14548
14549 rettv->v_type = VAR_NUMBER;
14550 rettv->vval.v_number = retval;
14551
14552}
14553
14554/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014555 * "test_garbagecollect_now()" function
14556 */
14557 static void
14558f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14559{
14560 /* This is dangerous, any Lists and Dicts used internally may be freed
14561 * while still in use. */
14562 garbage_collect(TRUE);
14563}
14564
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014565/*
14566 * "test_ignore_error()" function
14567 */
14568 static void
14569f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14570{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014571 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014572}
14573
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014574 static void
14575f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14576{
14577 rettv->v_type = VAR_BLOB;
14578 rettv->vval.v_blob = NULL;
14579}
14580
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014581#ifdef FEAT_JOB_CHANNEL
14582 static void
14583f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14584{
14585 rettv->v_type = VAR_CHANNEL;
14586 rettv->vval.v_channel = NULL;
14587}
14588#endif
14589
14590 static void
14591f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14592{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014593 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014594}
14595
14596#ifdef FEAT_JOB_CHANNEL
14597 static void
14598f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14599{
14600 rettv->v_type = VAR_JOB;
14601 rettv->vval.v_job = NULL;
14602}
14603#endif
14604
14605 static void
14606f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14607{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014608 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014609}
14610
14611 static void
14612f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14613{
14614 rettv->v_type = VAR_PARTIAL;
14615 rettv->vval.v_partial = NULL;
14616}
14617
14618 static void
14619f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14620{
14621 rettv->v_type = VAR_STRING;
14622 rettv->vval.v_string = NULL;
14623}
14624
Bram Moolenaarab186732018-09-14 21:27:06 +020014625#ifdef FEAT_GUI
14626 static void
14627f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14628{
14629 char_u *which;
14630 long value;
14631 int dragging;
14632 scrollbar_T *sb = NULL;
14633
14634 if (argvars[0].v_type != VAR_STRING
14635 || (argvars[1].v_type) != VAR_NUMBER
14636 || (argvars[2].v_type) != VAR_NUMBER)
14637 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014638 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014639 return;
14640 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014641 which = tv_get_string(&argvars[0]);
14642 value = tv_get_number(&argvars[1]);
14643 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014644
14645 if (STRCMP(which, "left") == 0)
14646 sb = &curwin->w_scrollbars[SBAR_LEFT];
14647 else if (STRCMP(which, "right") == 0)
14648 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14649 else if (STRCMP(which, "hor") == 0)
14650 sb = &gui.bottom_sbar;
14651 if (sb == NULL)
14652 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014653 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014654 return;
14655 }
14656 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014657# ifndef USE_ON_FLY_SCROLL
14658 // need to loop through normal_cmd() to handle the scroll events
14659 exec_normal(FALSE, TRUE, FALSE);
14660# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014661}
14662#endif
14663
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014664#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014665 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014666f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14667{
14668 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14669 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14670}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014671#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014672
14673 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014674f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14675{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014676 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014677}
14678
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014679/*
14680 * Get a callback from "arg". It can be a Funcref or a function name.
14681 * When "arg" is zero return an empty string.
14682 * Return NULL for an invalid argument.
14683 */
14684 char_u *
14685get_callback(typval_T *arg, partial_T **pp)
14686{
14687 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14688 {
14689 *pp = arg->vval.v_partial;
14690 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014691 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014692 }
14693 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014694 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014695 {
14696 func_ref(arg->vval.v_string);
14697 return arg->vval.v_string;
14698 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014699 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14700 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014701 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014702 return NULL;
14703}
14704
14705/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014706 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014707 */
14708 void
14709free_callback(char_u *callback, partial_T *partial)
14710{
14711 if (partial != NULL)
14712 partial_unref(partial);
14713 else if (callback != NULL)
14714 {
14715 func_unref(callback);
14716 vim_free(callback);
14717 }
14718}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014719
14720#ifdef FEAT_TIMERS
14721/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014722 * "timer_info([timer])" function
14723 */
14724 static void
14725f_timer_info(typval_T *argvars, typval_T *rettv)
14726{
14727 timer_T *timer = NULL;
14728
14729 if (rettv_list_alloc(rettv) != OK)
14730 return;
14731 if (argvars[0].v_type != VAR_UNKNOWN)
14732 {
14733 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014734 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014735 else
14736 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014737 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014738 if (timer != NULL)
14739 add_timer_info(rettv, timer);
14740 }
14741 }
14742 else
14743 add_timer_info_all(rettv);
14744}
14745
14746/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014747 * "timer_pause(timer, paused)" function
14748 */
14749 static void
14750f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14751{
14752 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014753 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014754
14755 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014756 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014757 else
14758 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014759 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014760 if (timer != NULL)
14761 timer->tr_paused = paused;
14762 }
14763}
14764
14765/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014766 * "timer_start(time, callback [, options])" function
14767 */
14768 static void
14769f_timer_start(typval_T *argvars, typval_T *rettv)
14770{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014771 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014772 timer_T *timer;
14773 int repeat = 0;
14774 char_u *callback;
14775 dict_T *dict;
14776 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014777
Bram Moolenaar75537a92016-09-05 22:45:28 +020014778 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014779 if (check_secure())
14780 return;
14781 if (argvars[2].v_type != VAR_UNKNOWN)
14782 {
14783 if (argvars[2].v_type != VAR_DICT
14784 || (dict = argvars[2].vval.v_dict) == NULL)
14785 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014786 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014787 return;
14788 }
14789 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014790 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014791 }
14792
Bram Moolenaar75537a92016-09-05 22:45:28 +020014793 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014794 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014795 return;
14796
14797 timer = create_timer(msec, repeat);
14798 if (timer == NULL)
14799 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014800 else
14801 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014802 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014803 timer->tr_callback = vim_strsave(callback);
14804 else
14805 /* pointer into the partial */
14806 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014807 timer->tr_partial = partial;
14808 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014809 }
14810}
14811
14812/*
14813 * "timer_stop(timer)" function
14814 */
14815 static void
14816f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14817{
14818 timer_T *timer;
14819
14820 if (argvars[0].v_type != VAR_NUMBER)
14821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014822 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014823 return;
14824 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014825 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014826 if (timer != NULL)
14827 stop_timer(timer);
14828}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014829
14830/*
14831 * "timer_stopall()" function
14832 */
14833 static void
14834f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14835{
14836 stop_all_timers();
14837}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014838#endif
14839
14840/*
14841 * "tolower(string)" function
14842 */
14843 static void
14844f_tolower(typval_T *argvars, typval_T *rettv)
14845{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014846 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014847 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014848}
14849
14850/*
14851 * "toupper(string)" function
14852 */
14853 static void
14854f_toupper(typval_T *argvars, typval_T *rettv)
14855{
14856 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014857 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014858}
14859
14860/*
14861 * "tr(string, fromstr, tostr)" function
14862 */
14863 static void
14864f_tr(typval_T *argvars, typval_T *rettv)
14865{
14866 char_u *in_str;
14867 char_u *fromstr;
14868 char_u *tostr;
14869 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014870 int inlen;
14871 int fromlen;
14872 int tolen;
14873 int idx;
14874 char_u *cpstr;
14875 int cplen;
14876 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014877 char_u buf[NUMBUFLEN];
14878 char_u buf2[NUMBUFLEN];
14879 garray_T ga;
14880
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014881 in_str = tv_get_string(&argvars[0]);
14882 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14883 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014884
14885 /* Default return value: empty string. */
14886 rettv->v_type = VAR_STRING;
14887 rettv->vval.v_string = NULL;
14888 if (fromstr == NULL || tostr == NULL)
14889 return; /* type error; errmsg already given */
14890 ga_init2(&ga, (int)sizeof(char), 80);
14891
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014892 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014893 /* not multi-byte: fromstr and tostr must be the same length */
14894 if (STRLEN(fromstr) != STRLEN(tostr))
14895 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014896error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014897 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014898 ga_clear(&ga);
14899 return;
14900 }
14901
14902 /* fromstr and tostr have to contain the same number of chars */
14903 while (*in_str != NUL)
14904 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014905 if (has_mbyte)
14906 {
14907 inlen = (*mb_ptr2len)(in_str);
14908 cpstr = in_str;
14909 cplen = inlen;
14910 idx = 0;
14911 for (p = fromstr; *p != NUL; p += fromlen)
14912 {
14913 fromlen = (*mb_ptr2len)(p);
14914 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14915 {
14916 for (p = tostr; *p != NUL; p += tolen)
14917 {
14918 tolen = (*mb_ptr2len)(p);
14919 if (idx-- == 0)
14920 {
14921 cplen = tolen;
14922 cpstr = p;
14923 break;
14924 }
14925 }
14926 if (*p == NUL) /* tostr is shorter than fromstr */
14927 goto error;
14928 break;
14929 }
14930 ++idx;
14931 }
14932
14933 if (first && cpstr == in_str)
14934 {
14935 /* Check that fromstr and tostr have the same number of
14936 * (multi-byte) characters. Done only once when a character
14937 * of in_str doesn't appear in fromstr. */
14938 first = FALSE;
14939 for (p = tostr; *p != NUL; p += tolen)
14940 {
14941 tolen = (*mb_ptr2len)(p);
14942 --idx;
14943 }
14944 if (idx != 0)
14945 goto error;
14946 }
14947
14948 (void)ga_grow(&ga, cplen);
14949 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14950 ga.ga_len += cplen;
14951
14952 in_str += inlen;
14953 }
14954 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014955 {
14956 /* When not using multi-byte chars we can do it faster. */
14957 p = vim_strchr(fromstr, *in_str);
14958 if (p != NULL)
14959 ga_append(&ga, tostr[p - fromstr]);
14960 else
14961 ga_append(&ga, *in_str);
14962 ++in_str;
14963 }
14964 }
14965
14966 /* add a terminating NUL */
14967 (void)ga_grow(&ga, 1);
14968 ga_append(&ga, NUL);
14969
14970 rettv->vval.v_string = ga.ga_data;
14971}
14972
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014973/*
14974 * "trim({expr})" function
14975 */
14976 static void
14977f_trim(typval_T *argvars, typval_T *rettv)
14978{
14979 char_u buf1[NUMBUFLEN];
14980 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014981 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014982 char_u *mask = NULL;
14983 char_u *tail;
14984 char_u *prev;
14985 char_u *p;
14986 int c1;
14987
14988 rettv->v_type = VAR_STRING;
14989 if (head == NULL)
14990 {
14991 rettv->vval.v_string = NULL;
14992 return;
14993 }
14994
14995 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014996 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014997
14998 while (*head != NUL)
14999 {
15000 c1 = PTR2CHAR(head);
15001 if (mask == NULL)
15002 {
15003 if (c1 > ' ' && c1 != 0xa0)
15004 break;
15005 }
15006 else
15007 {
15008 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15009 if (c1 == PTR2CHAR(p))
15010 break;
15011 if (*p == NUL)
15012 break;
15013 }
15014 MB_PTR_ADV(head);
15015 }
15016
15017 for (tail = head + STRLEN(head); tail > head; tail = prev)
15018 {
15019 prev = tail;
15020 MB_PTR_BACK(head, prev);
15021 c1 = PTR2CHAR(prev);
15022 if (mask == NULL)
15023 {
15024 if (c1 > ' ' && c1 != 0xa0)
15025 break;
15026 }
15027 else
15028 {
15029 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15030 if (c1 == PTR2CHAR(p))
15031 break;
15032 if (*p == NUL)
15033 break;
15034 }
15035 }
15036 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
15037}
15038
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015039#ifdef FEAT_FLOAT
15040/*
15041 * "trunc({float})" function
15042 */
15043 static void
15044f_trunc(typval_T *argvars, typval_T *rettv)
15045{
15046 float_T f = 0.0;
15047
15048 rettv->v_type = VAR_FLOAT;
15049 if (get_float_arg(argvars, &f) == OK)
15050 /* trunc() is not in C90, use floor() or ceil() instead. */
15051 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
15052 else
15053 rettv->vval.v_float = 0.0;
15054}
15055#endif
15056
15057/*
15058 * "type(expr)" function
15059 */
15060 static void
15061f_type(typval_T *argvars, typval_T *rettv)
15062{
15063 int n = -1;
15064
15065 switch (argvars[0].v_type)
15066 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020015067 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
15068 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015069 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020015070 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
15071 case VAR_LIST: n = VAR_TYPE_LIST; break;
15072 case VAR_DICT: n = VAR_TYPE_DICT; break;
15073 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015074 case VAR_SPECIAL:
15075 if (argvars[0].vval.v_number == VVAL_FALSE
15076 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020015077 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015078 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020015079 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015080 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020015081 case VAR_JOB: n = VAR_TYPE_JOB; break;
15082 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015083 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015084 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015085 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015086 n = -1;
15087 break;
15088 }
15089 rettv->vval.v_number = n;
15090}
15091
15092/*
15093 * "undofile(name)" function
15094 */
15095 static void
15096f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15097{
15098 rettv->v_type = VAR_STRING;
15099#ifdef FEAT_PERSISTENT_UNDO
15100 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015101 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015102
15103 if (*fname == NUL)
15104 {
15105 /* If there is no file name there will be no undo file. */
15106 rettv->vval.v_string = NULL;
15107 }
15108 else
15109 {
15110 char_u *ffname = FullName_save(fname, FALSE);
15111
15112 if (ffname != NULL)
15113 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15114 vim_free(ffname);
15115 }
15116 }
15117#else
15118 rettv->vval.v_string = NULL;
15119#endif
15120}
15121
15122/*
15123 * "undotree()" function
15124 */
15125 static void
15126f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15127{
15128 if (rettv_dict_alloc(rettv) == OK)
15129 {
15130 dict_T *dict = rettv->vval.v_dict;
15131 list_T *list;
15132
Bram Moolenaare0be1672018-07-08 16:50:37 +020015133 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15134 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15135 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15136 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15137 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15138 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015139
15140 list = list_alloc();
15141 if (list != NULL)
15142 {
15143 u_eval_tree(curbuf->b_u_oldhead, list);
15144 dict_add_list(dict, "entries", list);
15145 }
15146 }
15147}
15148
15149/*
15150 * "values(dict)" function
15151 */
15152 static void
15153f_values(typval_T *argvars, typval_T *rettv)
15154{
15155 dict_list(argvars, rettv, 1);
15156}
15157
15158/*
15159 * "virtcol(string)" function
15160 */
15161 static void
15162f_virtcol(typval_T *argvars, typval_T *rettv)
15163{
15164 colnr_T vcol = 0;
15165 pos_T *fp;
15166 int fnum = curbuf->b_fnum;
15167
15168 fp = var2fpos(&argvars[0], FALSE, &fnum);
15169 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15170 && fnum == curbuf->b_fnum)
15171 {
15172 getvvcol(curwin, fp, NULL, NULL, &vcol);
15173 ++vcol;
15174 }
15175
15176 rettv->vval.v_number = vcol;
15177}
15178
15179/*
15180 * "visualmode()" function
15181 */
15182 static void
15183f_visualmode(typval_T *argvars, typval_T *rettv)
15184{
15185 char_u str[2];
15186
15187 rettv->v_type = VAR_STRING;
15188 str[0] = curbuf->b_visual_mode_eval;
15189 str[1] = NUL;
15190 rettv->vval.v_string = vim_strsave(str);
15191
15192 /* A non-zero number or non-empty string argument: reset mode. */
15193 if (non_zero_arg(&argvars[0]))
15194 curbuf->b_visual_mode_eval = NUL;
15195}
15196
15197/*
15198 * "wildmenumode()" function
15199 */
15200 static void
15201f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15202{
15203#ifdef FEAT_WILDMENU
15204 if (wild_menu_showing)
15205 rettv->vval.v_number = 1;
15206#endif
15207}
15208
15209/*
15210 * "winbufnr(nr)" function
15211 */
15212 static void
15213f_winbufnr(typval_T *argvars, typval_T *rettv)
15214{
15215 win_T *wp;
15216
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015217 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015218 if (wp == NULL)
15219 rettv->vval.v_number = -1;
15220 else
15221 rettv->vval.v_number = wp->w_buffer->b_fnum;
15222}
15223
15224/*
15225 * "wincol()" function
15226 */
15227 static void
15228f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15229{
15230 validate_cursor();
15231 rettv->vval.v_number = curwin->w_wcol + 1;
15232}
15233
15234/*
15235 * "winheight(nr)" function
15236 */
15237 static void
15238f_winheight(typval_T *argvars, typval_T *rettv)
15239{
15240 win_T *wp;
15241
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015242 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015243 if (wp == NULL)
15244 rettv->vval.v_number = -1;
15245 else
15246 rettv->vval.v_number = wp->w_height;
15247}
15248
15249/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015250 * "winlayout()" function
15251 */
15252 static void
15253f_winlayout(typval_T *argvars, typval_T *rettv)
15254{
15255 tabpage_T *tp;
15256
15257 if (rettv_list_alloc(rettv) != OK)
15258 return;
15259
15260 if (argvars[0].v_type == VAR_UNKNOWN)
15261 tp = curtab;
15262 else
15263 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015264 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015265 if (tp == NULL)
15266 return;
15267 }
15268
15269 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15270}
15271
15272/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015273 * "winline()" function
15274 */
15275 static void
15276f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15277{
15278 validate_cursor();
15279 rettv->vval.v_number = curwin->w_wrow + 1;
15280}
15281
15282/*
15283 * "winnr()" function
15284 */
15285 static void
15286f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15287{
15288 int nr = 1;
15289
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015290 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015291 rettv->vval.v_number = nr;
15292}
15293
15294/*
15295 * "winrestcmd()" function
15296 */
15297 static void
15298f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15299{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015300 win_T *wp;
15301 int winnr = 1;
15302 garray_T ga;
15303 char_u buf[50];
15304
15305 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015306 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015307 {
15308 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15309 ga_concat(&ga, buf);
15310 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15311 ga_concat(&ga, buf);
15312 ++winnr;
15313 }
15314 ga_append(&ga, NUL);
15315
15316 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015317 rettv->v_type = VAR_STRING;
15318}
15319
15320/*
15321 * "winrestview()" function
15322 */
15323 static void
15324f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15325{
15326 dict_T *dict;
15327
15328 if (argvars[0].v_type != VAR_DICT
15329 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015330 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015331 else
15332 {
15333 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015334 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015335 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015336 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015337 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015338 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015339 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15340 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015341 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015342 curwin->w_set_curswant = FALSE;
15343 }
15344
15345 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015346 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015347#ifdef FEAT_DIFF
15348 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015349 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015350#endif
15351 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015352 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015353 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015354 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015355
15356 check_cursor();
15357 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015358 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015359 changed_window_setting();
15360
15361 if (curwin->w_topline <= 0)
15362 curwin->w_topline = 1;
15363 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15364 curwin->w_topline = curbuf->b_ml.ml_line_count;
15365#ifdef FEAT_DIFF
15366 check_topfill(curwin, TRUE);
15367#endif
15368 }
15369}
15370
15371/*
15372 * "winsaveview()" function
15373 */
15374 static void
15375f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15376{
15377 dict_T *dict;
15378
15379 if (rettv_dict_alloc(rettv) == FAIL)
15380 return;
15381 dict = rettv->vval.v_dict;
15382
Bram Moolenaare0be1672018-07-08 16:50:37 +020015383 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15384 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015385 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015386 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015387 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015388
Bram Moolenaare0be1672018-07-08 16:50:37 +020015389 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015390#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015391 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015392#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015393 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15394 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015395}
15396
15397/*
15398 * "winwidth(nr)" function
15399 */
15400 static void
15401f_winwidth(typval_T *argvars, typval_T *rettv)
15402{
15403 win_T *wp;
15404
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015405 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015406 if (wp == NULL)
15407 rettv->vval.v_number = -1;
15408 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015409 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015410}
15411
15412/*
15413 * "wordcount()" function
15414 */
15415 static void
15416f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15417{
15418 if (rettv_dict_alloc(rettv) == FAIL)
15419 return;
15420 cursor_pos_info(rettv->vval.v_dict);
15421}
15422
15423/*
15424 * "writefile()" function
15425 */
15426 static void
15427f_writefile(typval_T *argvars, typval_T *rettv)
15428{
15429 int binary = FALSE;
15430 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015431#ifdef HAVE_FSYNC
15432 int do_fsync = p_fs;
15433#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015434 char_u *fname;
15435 FILE *fd;
15436 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015437 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015438 list_T *list = NULL;
15439 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015440
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015441 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015442 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015443 return;
15444
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015445 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015446 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015447 list = argvars[0].vval.v_list;
15448 if (list == NULL)
15449 return;
15450 for (li = list->lv_first; li != NULL; li = li->li_next)
15451 if (tv_get_string_chk(&li->li_tv) == NULL)
15452 return;
15453 }
15454 else if (argvars[0].v_type == VAR_BLOB)
15455 {
15456 blob = argvars[0].vval.v_blob;
15457 if (blob == NULL)
15458 return;
15459 }
15460 else
15461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015462 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015463 return;
15464 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015465
15466 if (argvars[2].v_type != VAR_UNKNOWN)
15467 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015468 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015469
15470 if (arg2 == NULL)
15471 return;
15472 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015473 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015474 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015475 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015476#ifdef HAVE_FSYNC
15477 if (vim_strchr(arg2, 's') != NULL)
15478 do_fsync = TRUE;
15479 else if (vim_strchr(arg2, 'S') != NULL)
15480 do_fsync = FALSE;
15481#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015482 }
15483
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015484 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015485 if (fname == NULL)
15486 return;
15487
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015488 /* Always open the file in binary mode, library functions have a mind of
15489 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015490 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15491 append ? APPENDBIN : WRITEBIN)) == NULL)
15492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015493 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015494 ret = -1;
15495 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015496 else if (blob)
15497 {
15498 if (write_blob(fd, blob) == FAIL)
15499 ret = -1;
15500#ifdef HAVE_FSYNC
15501 else if (do_fsync)
15502 // Ignore the error, the user wouldn't know what to do about it.
15503 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015504 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015505#endif
15506 fclose(fd);
15507 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015508 else
15509 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015510 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015511 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015512#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015513 else if (do_fsync)
15514 /* Ignore the error, the user wouldn't know what to do about it.
15515 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015516 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015517#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015518 fclose(fd);
15519 }
15520
15521 rettv->vval.v_number = ret;
15522}
15523
15524/*
15525 * "xor(expr, expr)" function
15526 */
15527 static void
15528f_xor(typval_T *argvars, typval_T *rettv)
15529{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015530 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15531 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015532}
15533
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015534#endif /* FEAT_EVAL */