blob: 3cb66f3bbc2534087ba2ad105d36a46307abfb2c [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");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
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
66static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010067# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010068static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010069# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010070#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_browse(typval_T *argvars, typval_T *rettv);
72static void f_browsedir(typval_T *argvars, typval_T *rettv);
73static void f_bufexists(typval_T *argvars, typval_T *rettv);
74static void f_buflisted(typval_T *argvars, typval_T *rettv);
75static void f_bufloaded(typval_T *argvars, typval_T *rettv);
76static void f_bufname(typval_T *argvars, typval_T *rettv);
77static void f_bufnr(typval_T *argvars, typval_T *rettv);
78static void f_bufwinid(typval_T *argvars, typval_T *rettv);
79static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
80static void f_byte2line(typval_T *argvars, typval_T *rettv);
81static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
82static void f_byteidx(typval_T *argvars, typval_T *rettv);
83static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
84static void f_call(typval_T *argvars, typval_T *rettv);
85#ifdef FEAT_FLOAT
86static void f_ceil(typval_T *argvars, typval_T *rettv);
87#endif
88#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010089static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020091static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
93static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
94static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
95static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
96static void f_ch_info(typval_T *argvars, typval_T *rettv);
97static void f_ch_log(typval_T *argvars, typval_T *rettv);
98static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
99static void f_ch_open(typval_T *argvars, typval_T *rettv);
100static void f_ch_read(typval_T *argvars, typval_T *rettv);
101static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
102static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
103static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
105static void f_ch_status(typval_T *argvars, typval_T *rettv);
106#endif
107static void f_changenr(typval_T *argvars, typval_T *rettv);
108static void f_char2nr(typval_T *argvars, typval_T *rettv);
109static void f_cindent(typval_T *argvars, typval_T *rettv);
110static void f_clearmatches(typval_T *argvars, typval_T *rettv);
111static void f_col(typval_T *argvars, typval_T *rettv);
112#if defined(FEAT_INS_EXPAND)
113static void f_complete(typval_T *argvars, typval_T *rettv);
114static void f_complete_add(typval_T *argvars, typval_T *rettv);
115static void f_complete_check(typval_T *argvars, typval_T *rettv);
116#endif
117static void f_confirm(typval_T *argvars, typval_T *rettv);
118static void f_copy(typval_T *argvars, typval_T *rettv);
119#ifdef FEAT_FLOAT
120static void f_cos(typval_T *argvars, typval_T *rettv);
121static void f_cosh(typval_T *argvars, typval_T *rettv);
122#endif
123static void f_count(typval_T *argvars, typval_T *rettv);
124static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
125static void f_cursor(typval_T *argsvars, typval_T *rettv);
126static void f_deepcopy(typval_T *argvars, typval_T *rettv);
127static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200128static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200129static void f_did_filetype(typval_T *argvars, typval_T *rettv);
130static void f_diff_filler(typval_T *argvars, typval_T *rettv);
131static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
132static void f_empty(typval_T *argvars, typval_T *rettv);
133static void f_escape(typval_T *argvars, typval_T *rettv);
134static void f_eval(typval_T *argvars, typval_T *rettv);
135static void f_eventhandler(typval_T *argvars, typval_T *rettv);
136static void f_executable(typval_T *argvars, typval_T *rettv);
137static void f_execute(typval_T *argvars, typval_T *rettv);
138static void f_exepath(typval_T *argvars, typval_T *rettv);
139static void f_exists(typval_T *argvars, typval_T *rettv);
140#ifdef FEAT_FLOAT
141static void f_exp(typval_T *argvars, typval_T *rettv);
142#endif
143static void f_expand(typval_T *argvars, typval_T *rettv);
144static void f_extend(typval_T *argvars, typval_T *rettv);
145static void f_feedkeys(typval_T *argvars, typval_T *rettv);
146static void f_filereadable(typval_T *argvars, typval_T *rettv);
147static void f_filewritable(typval_T *argvars, typval_T *rettv);
148static void f_filter(typval_T *argvars, typval_T *rettv);
149static void f_finddir(typval_T *argvars, typval_T *rettv);
150static void f_findfile(typval_T *argvars, typval_T *rettv);
151#ifdef FEAT_FLOAT
152static void f_float2nr(typval_T *argvars, typval_T *rettv);
153static void f_floor(typval_T *argvars, typval_T *rettv);
154static void f_fmod(typval_T *argvars, typval_T *rettv);
155#endif
156static void f_fnameescape(typval_T *argvars, typval_T *rettv);
157static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
158static void f_foldclosed(typval_T *argvars, typval_T *rettv);
159static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
160static void f_foldlevel(typval_T *argvars, typval_T *rettv);
161static void f_foldtext(typval_T *argvars, typval_T *rettv);
162static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
163static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200164static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200165static void f_function(typval_T *argvars, typval_T *rettv);
166static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
167static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200168static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200169static void f_getbufline(typval_T *argvars, typval_T *rettv);
170static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100171static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_getchar(typval_T *argvars, typval_T *rettv);
173static void f_getcharmod(typval_T *argvars, typval_T *rettv);
174static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
175static void f_getcmdline(typval_T *argvars, typval_T *rettv);
176#if defined(FEAT_CMDL_COMPL)
177static void f_getcompletion(typval_T *argvars, typval_T *rettv);
178#endif
179static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
180static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
181static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
182static void f_getcwd(typval_T *argvars, typval_T *rettv);
183static void f_getfontname(typval_T *argvars, typval_T *rettv);
184static void f_getfperm(typval_T *argvars, typval_T *rettv);
185static void f_getfsize(typval_T *argvars, typval_T *rettv);
186static void f_getftime(typval_T *argvars, typval_T *rettv);
187static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100188static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200189static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200190static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_getmatches(typval_T *argvars, typval_T *rettv);
192static void f_getpid(typval_T *argvars, typval_T *rettv);
193static void f_getcurpos(typval_T *argvars, typval_T *rettv);
194static void f_getpos(typval_T *argvars, typval_T *rettv);
195static void f_getqflist(typval_T *argvars, typval_T *rettv);
196static void f_getreg(typval_T *argvars, typval_T *rettv);
197static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200198static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200199static void f_gettabvar(typval_T *argvars, typval_T *rettv);
200static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200201static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100202static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200203static void f_getwinposx(typval_T *argvars, typval_T *rettv);
204static void f_getwinposy(typval_T *argvars, typval_T *rettv);
205static void f_getwinvar(typval_T *argvars, typval_T *rettv);
206static void f_glob(typval_T *argvars, typval_T *rettv);
207static void f_globpath(typval_T *argvars, typval_T *rettv);
208static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
209static void f_has(typval_T *argvars, typval_T *rettv);
210static void f_has_key(typval_T *argvars, typval_T *rettv);
211static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
212static void f_hasmapto(typval_T *argvars, typval_T *rettv);
213static void f_histadd(typval_T *argvars, typval_T *rettv);
214static void f_histdel(typval_T *argvars, typval_T *rettv);
215static void f_histget(typval_T *argvars, typval_T *rettv);
216static void f_histnr(typval_T *argvars, typval_T *rettv);
217static void f_hlID(typval_T *argvars, typval_T *rettv);
218static void f_hlexists(typval_T *argvars, typval_T *rettv);
219static void f_hostname(typval_T *argvars, typval_T *rettv);
220static void f_iconv(typval_T *argvars, typval_T *rettv);
221static void f_indent(typval_T *argvars, typval_T *rettv);
222static void f_index(typval_T *argvars, typval_T *rettv);
223static void f_input(typval_T *argvars, typval_T *rettv);
224static void f_inputdialog(typval_T *argvars, typval_T *rettv);
225static void f_inputlist(typval_T *argvars, typval_T *rettv);
226static void f_inputrestore(typval_T *argvars, typval_T *rettv);
227static void f_inputsave(typval_T *argvars, typval_T *rettv);
228static void f_inputsecret(typval_T *argvars, typval_T *rettv);
229static void f_insert(typval_T *argvars, typval_T *rettv);
230static void f_invert(typval_T *argvars, typval_T *rettv);
231static void f_isdirectory(typval_T *argvars, typval_T *rettv);
232static void f_islocked(typval_T *argvars, typval_T *rettv);
233#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
234static void f_isnan(typval_T *argvars, typval_T *rettv);
235#endif
236static void f_items(typval_T *argvars, typval_T *rettv);
237#ifdef FEAT_JOB_CHANNEL
238static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
239static void f_job_info(typval_T *argvars, typval_T *rettv);
240static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
241static void f_job_start(typval_T *argvars, typval_T *rettv);
242static void f_job_stop(typval_T *argvars, typval_T *rettv);
243static void f_job_status(typval_T *argvars, typval_T *rettv);
244#endif
245static void f_join(typval_T *argvars, typval_T *rettv);
246static void f_js_decode(typval_T *argvars, typval_T *rettv);
247static void f_js_encode(typval_T *argvars, typval_T *rettv);
248static void f_json_decode(typval_T *argvars, typval_T *rettv);
249static void f_json_encode(typval_T *argvars, typval_T *rettv);
250static void f_keys(typval_T *argvars, typval_T *rettv);
251static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
252static void f_len(typval_T *argvars, typval_T *rettv);
253static void f_libcall(typval_T *argvars, typval_T *rettv);
254static void f_libcallnr(typval_T *argvars, typval_T *rettv);
255static void f_line(typval_T *argvars, typval_T *rettv);
256static void f_line2byte(typval_T *argvars, typval_T *rettv);
257static void f_lispindent(typval_T *argvars, typval_T *rettv);
258static void f_localtime(typval_T *argvars, typval_T *rettv);
259#ifdef FEAT_FLOAT
260static void f_log(typval_T *argvars, typval_T *rettv);
261static void f_log10(typval_T *argvars, typval_T *rettv);
262#endif
263#ifdef FEAT_LUA
264static void f_luaeval(typval_T *argvars, typval_T *rettv);
265#endif
266static void f_map(typval_T *argvars, typval_T *rettv);
267static void f_maparg(typval_T *argvars, typval_T *rettv);
268static void f_mapcheck(typval_T *argvars, typval_T *rettv);
269static void f_match(typval_T *argvars, typval_T *rettv);
270static void f_matchadd(typval_T *argvars, typval_T *rettv);
271static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
272static void f_matcharg(typval_T *argvars, typval_T *rettv);
273static void f_matchdelete(typval_T *argvars, typval_T *rettv);
274static void f_matchend(typval_T *argvars, typval_T *rettv);
275static void f_matchlist(typval_T *argvars, typval_T *rettv);
276static void f_matchstr(typval_T *argvars, typval_T *rettv);
277static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
278static void f_max(typval_T *argvars, typval_T *rettv);
279static void f_min(typval_T *argvars, typval_T *rettv);
280#ifdef vim_mkdir
281static void f_mkdir(typval_T *argvars, typval_T *rettv);
282#endif
283static void f_mode(typval_T *argvars, typval_T *rettv);
284#ifdef FEAT_MZSCHEME
285static void f_mzeval(typval_T *argvars, typval_T *rettv);
286#endif
287static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
288static void f_nr2char(typval_T *argvars, typval_T *rettv);
289static void f_or(typval_T *argvars, typval_T *rettv);
290static void f_pathshorten(typval_T *argvars, typval_T *rettv);
291#ifdef FEAT_PERL
292static void f_perleval(typval_T *argvars, typval_T *rettv);
293#endif
294#ifdef FEAT_FLOAT
295static void f_pow(typval_T *argvars, typval_T *rettv);
296#endif
297static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
298static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200299#ifdef FEAT_JOB_CHANNEL
300static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200301static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200302static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
303#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200304static void f_pumvisible(typval_T *argvars, typval_T *rettv);
305#ifdef FEAT_PYTHON3
306static void f_py3eval(typval_T *argvars, typval_T *rettv);
307#endif
308#ifdef FEAT_PYTHON
309static void f_pyeval(typval_T *argvars, typval_T *rettv);
310#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100311#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
312static void f_pyxeval(typval_T *argvars, typval_T *rettv);
313#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200314static void f_range(typval_T *argvars, typval_T *rettv);
315static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200316static void f_reg_executing(typval_T *argvars, typval_T *rettv);
317static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200318static void f_reltime(typval_T *argvars, typval_T *rettv);
319#ifdef FEAT_FLOAT
320static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
321#endif
322static void f_reltimestr(typval_T *argvars, typval_T *rettv);
323static void f_remote_expr(typval_T *argvars, typval_T *rettv);
324static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
325static void f_remote_peek(typval_T *argvars, typval_T *rettv);
326static void f_remote_read(typval_T *argvars, typval_T *rettv);
327static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100328static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200329static void f_remove(typval_T *argvars, typval_T *rettv);
330static void f_rename(typval_T *argvars, typval_T *rettv);
331static void f_repeat(typval_T *argvars, typval_T *rettv);
332static void f_resolve(typval_T *argvars, typval_T *rettv);
333static void f_reverse(typval_T *argvars, typval_T *rettv);
334#ifdef FEAT_FLOAT
335static void f_round(typval_T *argvars, typval_T *rettv);
336#endif
337static void f_screenattr(typval_T *argvars, typval_T *rettv);
338static void f_screenchar(typval_T *argvars, typval_T *rettv);
339static void f_screencol(typval_T *argvars, typval_T *rettv);
340static void f_screenrow(typval_T *argvars, typval_T *rettv);
341static void f_search(typval_T *argvars, typval_T *rettv);
342static void f_searchdecl(typval_T *argvars, typval_T *rettv);
343static void f_searchpair(typval_T *argvars, typval_T *rettv);
344static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
345static void f_searchpos(typval_T *argvars, typval_T *rettv);
346static void f_server2client(typval_T *argvars, typval_T *rettv);
347static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200348static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200349static void f_setbufvar(typval_T *argvars, typval_T *rettv);
350static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
351static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
352static void f_setfperm(typval_T *argvars, typval_T *rettv);
353static void f_setline(typval_T *argvars, typval_T *rettv);
354static void f_setloclist(typval_T *argvars, typval_T *rettv);
355static void f_setmatches(typval_T *argvars, typval_T *rettv);
356static void f_setpos(typval_T *argvars, typval_T *rettv);
357static void f_setqflist(typval_T *argvars, typval_T *rettv);
358static void f_setreg(typval_T *argvars, typval_T *rettv);
359static void f_settabvar(typval_T *argvars, typval_T *rettv);
360static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
361static void f_setwinvar(typval_T *argvars, typval_T *rettv);
362#ifdef FEAT_CRYPT
363static void f_sha256(typval_T *argvars, typval_T *rettv);
364#endif /* FEAT_CRYPT */
365static void f_shellescape(typval_T *argvars, typval_T *rettv);
366static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
367static void f_simplify(typval_T *argvars, typval_T *rettv);
368#ifdef FEAT_FLOAT
369static void f_sin(typval_T *argvars, typval_T *rettv);
370static void f_sinh(typval_T *argvars, typval_T *rettv);
371#endif
372static void f_sort(typval_T *argvars, typval_T *rettv);
373static void f_soundfold(typval_T *argvars, typval_T *rettv);
374static void f_spellbadword(typval_T *argvars, typval_T *rettv);
375static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
376static void f_split(typval_T *argvars, typval_T *rettv);
377#ifdef FEAT_FLOAT
378static void f_sqrt(typval_T *argvars, typval_T *rettv);
379static void f_str2float(typval_T *argvars, typval_T *rettv);
380#endif
381static void f_str2nr(typval_T *argvars, typval_T *rettv);
382static void f_strchars(typval_T *argvars, typval_T *rettv);
383#ifdef HAVE_STRFTIME
384static void f_strftime(typval_T *argvars, typval_T *rettv);
385#endif
386static void f_strgetchar(typval_T *argvars, typval_T *rettv);
387static void f_stridx(typval_T *argvars, typval_T *rettv);
388static void f_string(typval_T *argvars, typval_T *rettv);
389static void f_strlen(typval_T *argvars, typval_T *rettv);
390static void f_strcharpart(typval_T *argvars, typval_T *rettv);
391static void f_strpart(typval_T *argvars, typval_T *rettv);
392static void f_strridx(typval_T *argvars, typval_T *rettv);
393static void f_strtrans(typval_T *argvars, typval_T *rettv);
394static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
395static void f_strwidth(typval_T *argvars, typval_T *rettv);
396static void f_submatch(typval_T *argvars, typval_T *rettv);
397static void f_substitute(typval_T *argvars, typval_T *rettv);
398static void f_synID(typval_T *argvars, typval_T *rettv);
399static void f_synIDattr(typval_T *argvars, typval_T *rettv);
400static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
401static void f_synstack(typval_T *argvars, typval_T *rettv);
402static void f_synconcealed(typval_T *argvars, typval_T *rettv);
403static void f_system(typval_T *argvars, typval_T *rettv);
404static void f_systemlist(typval_T *argvars, typval_T *rettv);
405static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
406static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
407static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
408static void f_taglist(typval_T *argvars, typval_T *rettv);
409static void f_tagfiles(typval_T *argvars, typval_T *rettv);
410static void f_tempname(typval_T *argvars, typval_T *rettv);
411static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
412static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200413static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100414static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200415static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100416static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200417#ifdef FEAT_JOB_CHANNEL
418static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
419#endif
420static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
421#ifdef FEAT_JOB_CHANNEL
422static void f_test_null_job(typval_T *argvars, typval_T *rettv);
423#endif
424static void f_test_null_list(typval_T *argvars, typval_T *rettv);
425static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
426static void f_test_null_string(typval_T *argvars, typval_T *rettv);
427static void f_test_settime(typval_T *argvars, typval_T *rettv);
428#ifdef FEAT_FLOAT
429static void f_tan(typval_T *argvars, typval_T *rettv);
430static void f_tanh(typval_T *argvars, typval_T *rettv);
431#endif
432#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200433static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200434static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200435static void f_timer_start(typval_T *argvars, typval_T *rettv);
436static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200437static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200438#endif
439static void f_tolower(typval_T *argvars, typval_T *rettv);
440static void f_toupper(typval_T *argvars, typval_T *rettv);
441static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100442static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200443#ifdef FEAT_FLOAT
444static void f_trunc(typval_T *argvars, typval_T *rettv);
445#endif
446static void f_type(typval_T *argvars, typval_T *rettv);
447static void f_undofile(typval_T *argvars, typval_T *rettv);
448static void f_undotree(typval_T *argvars, typval_T *rettv);
449static void f_uniq(typval_T *argvars, typval_T *rettv);
450static void f_values(typval_T *argvars, typval_T *rettv);
451static void f_virtcol(typval_T *argvars, typval_T *rettv);
452static void f_visualmode(typval_T *argvars, typval_T *rettv);
453static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
454static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
455static void f_win_getid(typval_T *argvars, typval_T *rettv);
456static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
457static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
458static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100459static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200460static void f_winbufnr(typval_T *argvars, typval_T *rettv);
461static void f_wincol(typval_T *argvars, typval_T *rettv);
462static void f_winheight(typval_T *argvars, typval_T *rettv);
463static void f_winline(typval_T *argvars, typval_T *rettv);
464static void f_winnr(typval_T *argvars, typval_T *rettv);
465static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
466static void f_winrestview(typval_T *argvars, typval_T *rettv);
467static void f_winsaveview(typval_T *argvars, typval_T *rettv);
468static void f_winwidth(typval_T *argvars, typval_T *rettv);
469static void f_writefile(typval_T *argvars, typval_T *rettv);
470static void f_wordcount(typval_T *argvars, typval_T *rettv);
471static void f_xor(typval_T *argvars, typval_T *rettv);
472
473/*
474 * Array with names and number of arguments of all internal functions
475 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
476 */
477static struct fst
478{
479 char *f_name; /* function name */
480 char f_min_argc; /* minimal number of arguments */
481 char f_max_argc; /* maximal number of arguments */
482 void (*f_func)(typval_T *args, typval_T *rvar);
483 /* implementation of function */
484} functions[] =
485{
486#ifdef FEAT_FLOAT
487 {"abs", 1, 1, f_abs},
488 {"acos", 1, 1, f_acos}, /* WJMc */
489#endif
490 {"add", 2, 2, f_add},
491 {"and", 2, 2, f_and},
492 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200493 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200494 {"argc", 0, 0, f_argc},
495 {"argidx", 0, 0, f_argidx},
496 {"arglistid", 0, 2, f_arglistid},
497 {"argv", 0, 1, f_argv},
498#ifdef FEAT_FLOAT
499 {"asin", 1, 1, f_asin}, /* WJMc */
500#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100501 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200502 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100503 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200504 {"assert_exception", 1, 2, f_assert_exception},
505 {"assert_fails", 1, 2, f_assert_fails},
506 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100507 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200508 {"assert_match", 2, 3, f_assert_match},
509 {"assert_notequal", 2, 3, f_assert_notequal},
510 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100511 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200512 {"assert_true", 1, 2, f_assert_true},
513#ifdef FEAT_FLOAT
514 {"atan", 1, 1, f_atan},
515 {"atan2", 2, 2, f_atan2},
516#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100517#ifdef FEAT_BEVAL
518 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100519# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100520 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100521# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100522#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200523 {"browse", 4, 4, f_browse},
524 {"browsedir", 2, 2, f_browsedir},
525 {"bufexists", 1, 1, f_bufexists},
526 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
527 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
528 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
529 {"buflisted", 1, 1, f_buflisted},
530 {"bufloaded", 1, 1, f_bufloaded},
531 {"bufname", 1, 1, f_bufname},
532 {"bufnr", 1, 2, f_bufnr},
533 {"bufwinid", 1, 1, f_bufwinid},
534 {"bufwinnr", 1, 1, f_bufwinnr},
535 {"byte2line", 1, 1, f_byte2line},
536 {"byteidx", 2, 2, f_byteidx},
537 {"byteidxcomp", 2, 2, f_byteidxcomp},
538 {"call", 2, 3, f_call},
539#ifdef FEAT_FLOAT
540 {"ceil", 1, 1, f_ceil},
541#endif
542#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100543 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200544 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200545 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200546 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
547 {"ch_evalraw", 2, 3, f_ch_evalraw},
548 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
549 {"ch_getjob", 1, 1, f_ch_getjob},
550 {"ch_info", 1, 1, f_ch_info},
551 {"ch_log", 1, 2, f_ch_log},
552 {"ch_logfile", 1, 2, f_ch_logfile},
553 {"ch_open", 1, 2, f_ch_open},
554 {"ch_read", 1, 2, f_ch_read},
555 {"ch_readraw", 1, 2, f_ch_readraw},
556 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
557 {"ch_sendraw", 2, 3, f_ch_sendraw},
558 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200559 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200560#endif
561 {"changenr", 0, 0, f_changenr},
562 {"char2nr", 1, 2, f_char2nr},
563 {"cindent", 1, 1, f_cindent},
564 {"clearmatches", 0, 0, f_clearmatches},
565 {"col", 1, 1, f_col},
566#if defined(FEAT_INS_EXPAND)
567 {"complete", 2, 2, f_complete},
568 {"complete_add", 1, 1, f_complete_add},
569 {"complete_check", 0, 0, f_complete_check},
570#endif
571 {"confirm", 1, 4, f_confirm},
572 {"copy", 1, 1, f_copy},
573#ifdef FEAT_FLOAT
574 {"cos", 1, 1, f_cos},
575 {"cosh", 1, 1, f_cosh},
576#endif
577 {"count", 2, 4, f_count},
578 {"cscope_connection",0,3, f_cscope_connection},
579 {"cursor", 1, 3, f_cursor},
580 {"deepcopy", 1, 2, f_deepcopy},
581 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200582 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200583 {"did_filetype", 0, 0, f_did_filetype},
584 {"diff_filler", 1, 1, f_diff_filler},
585 {"diff_hlID", 2, 2, f_diff_hlID},
586 {"empty", 1, 1, f_empty},
587 {"escape", 2, 2, f_escape},
588 {"eval", 1, 1, f_eval},
589 {"eventhandler", 0, 0, f_eventhandler},
590 {"executable", 1, 1, f_executable},
591 {"execute", 1, 2, f_execute},
592 {"exepath", 1, 1, f_exepath},
593 {"exists", 1, 1, f_exists},
594#ifdef FEAT_FLOAT
595 {"exp", 1, 1, f_exp},
596#endif
597 {"expand", 1, 3, f_expand},
598 {"extend", 2, 3, f_extend},
599 {"feedkeys", 1, 2, f_feedkeys},
600 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
601 {"filereadable", 1, 1, f_filereadable},
602 {"filewritable", 1, 1, f_filewritable},
603 {"filter", 2, 2, f_filter},
604 {"finddir", 1, 3, f_finddir},
605 {"findfile", 1, 3, f_findfile},
606#ifdef FEAT_FLOAT
607 {"float2nr", 1, 1, f_float2nr},
608 {"floor", 1, 1, f_floor},
609 {"fmod", 2, 2, f_fmod},
610#endif
611 {"fnameescape", 1, 1, f_fnameescape},
612 {"fnamemodify", 2, 2, f_fnamemodify},
613 {"foldclosed", 1, 1, f_foldclosed},
614 {"foldclosedend", 1, 1, f_foldclosedend},
615 {"foldlevel", 1, 1, f_foldlevel},
616 {"foldtext", 0, 0, f_foldtext},
617 {"foldtextresult", 1, 1, f_foldtextresult},
618 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200619 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200620 {"function", 1, 3, f_function},
621 {"garbagecollect", 0, 1, f_garbagecollect},
622 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200623 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200624 {"getbufline", 2, 3, f_getbufline},
625 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100626 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200627 {"getchar", 0, 1, f_getchar},
628 {"getcharmod", 0, 0, f_getcharmod},
629 {"getcharsearch", 0, 0, f_getcharsearch},
630 {"getcmdline", 0, 0, f_getcmdline},
631 {"getcmdpos", 0, 0, f_getcmdpos},
632 {"getcmdtype", 0, 0, f_getcmdtype},
633 {"getcmdwintype", 0, 0, f_getcmdwintype},
634#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200635 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636#endif
637 {"getcurpos", 0, 0, f_getcurpos},
638 {"getcwd", 0, 2, f_getcwd},
639 {"getfontname", 0, 1, f_getfontname},
640 {"getfperm", 1, 1, f_getfperm},
641 {"getfsize", 1, 1, f_getfsize},
642 {"getftime", 1, 1, f_getftime},
643 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100644 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200645 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200646 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200647 {"getmatches", 0, 0, f_getmatches},
648 {"getpid", 0, 0, f_getpid},
649 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200650 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200651 {"getreg", 0, 3, f_getreg},
652 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200653 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200654 {"gettabvar", 2, 3, f_gettabvar},
655 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200656 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100657 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200658 {"getwinposx", 0, 0, f_getwinposx},
659 {"getwinposy", 0, 0, f_getwinposy},
660 {"getwinvar", 2, 3, f_getwinvar},
661 {"glob", 1, 4, f_glob},
662 {"glob2regpat", 1, 1, f_glob2regpat},
663 {"globpath", 2, 5, f_globpath},
664 {"has", 1, 1, f_has},
665 {"has_key", 2, 2, f_has_key},
666 {"haslocaldir", 0, 2, f_haslocaldir},
667 {"hasmapto", 1, 3, f_hasmapto},
668 {"highlightID", 1, 1, f_hlID}, /* obsolete */
669 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
670 {"histadd", 2, 2, f_histadd},
671 {"histdel", 1, 2, f_histdel},
672 {"histget", 1, 2, f_histget},
673 {"histnr", 1, 1, f_histnr},
674 {"hlID", 1, 1, f_hlID},
675 {"hlexists", 1, 1, f_hlexists},
676 {"hostname", 0, 0, f_hostname},
677 {"iconv", 3, 3, f_iconv},
678 {"indent", 1, 1, f_indent},
679 {"index", 2, 4, f_index},
680 {"input", 1, 3, f_input},
681 {"inputdialog", 1, 3, f_inputdialog},
682 {"inputlist", 1, 1, f_inputlist},
683 {"inputrestore", 0, 0, f_inputrestore},
684 {"inputsave", 0, 0, f_inputsave},
685 {"inputsecret", 1, 2, f_inputsecret},
686 {"insert", 2, 3, f_insert},
687 {"invert", 1, 1, f_invert},
688 {"isdirectory", 1, 1, f_isdirectory},
689 {"islocked", 1, 1, f_islocked},
690#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
691 {"isnan", 1, 1, f_isnan},
692#endif
693 {"items", 1, 1, f_items},
694#ifdef FEAT_JOB_CHANNEL
695 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200696 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200697 {"job_setoptions", 2, 2, f_job_setoptions},
698 {"job_start", 1, 2, f_job_start},
699 {"job_status", 1, 1, f_job_status},
700 {"job_stop", 1, 2, f_job_stop},
701#endif
702 {"join", 1, 2, f_join},
703 {"js_decode", 1, 1, f_js_decode},
704 {"js_encode", 1, 1, f_js_encode},
705 {"json_decode", 1, 1, f_json_decode},
706 {"json_encode", 1, 1, f_json_encode},
707 {"keys", 1, 1, f_keys},
708 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
709 {"len", 1, 1, f_len},
710 {"libcall", 3, 3, f_libcall},
711 {"libcallnr", 3, 3, f_libcallnr},
712 {"line", 1, 1, f_line},
713 {"line2byte", 1, 1, f_line2byte},
714 {"lispindent", 1, 1, f_lispindent},
715 {"localtime", 0, 0, f_localtime},
716#ifdef FEAT_FLOAT
717 {"log", 1, 1, f_log},
718 {"log10", 1, 1, f_log10},
719#endif
720#ifdef FEAT_LUA
721 {"luaeval", 1, 2, f_luaeval},
722#endif
723 {"map", 2, 2, f_map},
724 {"maparg", 1, 4, f_maparg},
725 {"mapcheck", 1, 3, f_mapcheck},
726 {"match", 2, 4, f_match},
727 {"matchadd", 2, 5, f_matchadd},
728 {"matchaddpos", 2, 5, f_matchaddpos},
729 {"matcharg", 1, 1, f_matcharg},
730 {"matchdelete", 1, 1, f_matchdelete},
731 {"matchend", 2, 4, f_matchend},
732 {"matchlist", 2, 4, f_matchlist},
733 {"matchstr", 2, 4, f_matchstr},
734 {"matchstrpos", 2, 4, f_matchstrpos},
735 {"max", 1, 1, f_max},
736 {"min", 1, 1, f_min},
737#ifdef vim_mkdir
738 {"mkdir", 1, 3, f_mkdir},
739#endif
740 {"mode", 0, 1, f_mode},
741#ifdef FEAT_MZSCHEME
742 {"mzeval", 1, 1, f_mzeval},
743#endif
744 {"nextnonblank", 1, 1, f_nextnonblank},
745 {"nr2char", 1, 2, f_nr2char},
746 {"or", 2, 2, f_or},
747 {"pathshorten", 1, 1, f_pathshorten},
748#ifdef FEAT_PERL
749 {"perleval", 1, 1, f_perleval},
750#endif
751#ifdef FEAT_FLOAT
752 {"pow", 2, 2, f_pow},
753#endif
754 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100755 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200756#ifdef FEAT_JOB_CHANNEL
757 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200758 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200759 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
760#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200761 {"pumvisible", 0, 0, f_pumvisible},
762#ifdef FEAT_PYTHON3
763 {"py3eval", 1, 1, f_py3eval},
764#endif
765#ifdef FEAT_PYTHON
766 {"pyeval", 1, 1, f_pyeval},
767#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100768#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
769 {"pyxeval", 1, 1, f_pyxeval},
770#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200771 {"range", 1, 3, f_range},
772 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200773 {"reg_executing", 0, 0, f_reg_executing},
774 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200775 {"reltime", 0, 2, f_reltime},
776#ifdef FEAT_FLOAT
777 {"reltimefloat", 1, 1, f_reltimefloat},
778#endif
779 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100780 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200781 {"remote_foreground", 1, 1, f_remote_foreground},
782 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100783 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200784 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100785 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200786 {"remove", 2, 3, f_remove},
787 {"rename", 2, 2, f_rename},
788 {"repeat", 2, 2, f_repeat},
789 {"resolve", 1, 1, f_resolve},
790 {"reverse", 1, 1, f_reverse},
791#ifdef FEAT_FLOAT
792 {"round", 1, 1, f_round},
793#endif
794 {"screenattr", 2, 2, f_screenattr},
795 {"screenchar", 2, 2, f_screenchar},
796 {"screencol", 0, 0, f_screencol},
797 {"screenrow", 0, 0, f_screenrow},
798 {"search", 1, 4, f_search},
799 {"searchdecl", 1, 3, f_searchdecl},
800 {"searchpair", 3, 7, f_searchpair},
801 {"searchpairpos", 3, 7, f_searchpairpos},
802 {"searchpos", 1, 4, f_searchpos},
803 {"server2client", 2, 2, f_server2client},
804 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200805 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200806 {"setbufvar", 3, 3, f_setbufvar},
807 {"setcharsearch", 1, 1, f_setcharsearch},
808 {"setcmdpos", 1, 1, f_setcmdpos},
809 {"setfperm", 2, 2, f_setfperm},
810 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200811 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200812 {"setmatches", 1, 1, f_setmatches},
813 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200814 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200815 {"setreg", 2, 3, f_setreg},
816 {"settabvar", 3, 3, f_settabvar},
817 {"settabwinvar", 4, 4, f_settabwinvar},
818 {"setwinvar", 3, 3, f_setwinvar},
819#ifdef FEAT_CRYPT
820 {"sha256", 1, 1, f_sha256},
821#endif
822 {"shellescape", 1, 2, f_shellescape},
823 {"shiftwidth", 0, 0, f_shiftwidth},
824 {"simplify", 1, 1, f_simplify},
825#ifdef FEAT_FLOAT
826 {"sin", 1, 1, f_sin},
827 {"sinh", 1, 1, f_sinh},
828#endif
829 {"sort", 1, 3, f_sort},
830 {"soundfold", 1, 1, f_soundfold},
831 {"spellbadword", 0, 1, f_spellbadword},
832 {"spellsuggest", 1, 3, f_spellsuggest},
833 {"split", 1, 3, f_split},
834#ifdef FEAT_FLOAT
835 {"sqrt", 1, 1, f_sqrt},
836 {"str2float", 1, 1, f_str2float},
837#endif
838 {"str2nr", 1, 2, f_str2nr},
839 {"strcharpart", 2, 3, f_strcharpart},
840 {"strchars", 1, 2, f_strchars},
841 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
842#ifdef HAVE_STRFTIME
843 {"strftime", 1, 2, f_strftime},
844#endif
845 {"strgetchar", 2, 2, f_strgetchar},
846 {"stridx", 2, 3, f_stridx},
847 {"string", 1, 1, f_string},
848 {"strlen", 1, 1, f_strlen},
849 {"strpart", 2, 3, f_strpart},
850 {"strridx", 2, 3, f_strridx},
851 {"strtrans", 1, 1, f_strtrans},
852 {"strwidth", 1, 1, f_strwidth},
853 {"submatch", 1, 2, f_submatch},
854 {"substitute", 4, 4, f_substitute},
855 {"synID", 3, 3, f_synID},
856 {"synIDattr", 2, 3, f_synIDattr},
857 {"synIDtrans", 1, 1, f_synIDtrans},
858 {"synconcealed", 2, 2, f_synconcealed},
859 {"synstack", 2, 2, f_synstack},
860 {"system", 1, 2, f_system},
861 {"systemlist", 1, 2, f_systemlist},
862 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
863 {"tabpagenr", 0, 1, f_tabpagenr},
864 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
865 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100866 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200867#ifdef FEAT_FLOAT
868 {"tan", 1, 1, f_tan},
869 {"tanh", 1, 1, f_tanh},
870#endif
871 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200872#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100873 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
874 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100875 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200876 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200877# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
878 {"term_getansicolors", 1, 1, f_term_getansicolors},
879# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200880 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200881 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200882 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200883 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200884 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200885 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200886 {"term_getstatus", 1, 1, f_term_getstatus},
887 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200888 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200889 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200890 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200891 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200892# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
893 {"term_setansicolors", 2, 2, f_term_setansicolors},
894# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100895 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100896 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200897 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200898 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200899 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200900#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200901 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
902 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200903 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200904 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100905 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200906#ifdef FEAT_JOB_CHANNEL
907 {"test_null_channel", 0, 0, f_test_null_channel},
908#endif
909 {"test_null_dict", 0, 0, f_test_null_dict},
910#ifdef FEAT_JOB_CHANNEL
911 {"test_null_job", 0, 0, f_test_null_job},
912#endif
913 {"test_null_list", 0, 0, f_test_null_list},
914 {"test_null_partial", 0, 0, f_test_null_partial},
915 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100916 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200917 {"test_settime", 1, 1, f_test_settime},
918#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200919 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200920 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200921 {"timer_start", 2, 3, f_timer_start},
922 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200923 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200924#endif
925 {"tolower", 1, 1, f_tolower},
926 {"toupper", 1, 1, f_toupper},
927 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100928 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200929#ifdef FEAT_FLOAT
930 {"trunc", 1, 1, f_trunc},
931#endif
932 {"type", 1, 1, f_type},
933 {"undofile", 1, 1, f_undofile},
934 {"undotree", 0, 0, f_undotree},
935 {"uniq", 1, 3, f_uniq},
936 {"values", 1, 1, f_values},
937 {"virtcol", 1, 1, f_virtcol},
938 {"visualmode", 0, 1, f_visualmode},
939 {"wildmenumode", 0, 0, f_wildmenumode},
940 {"win_findbuf", 1, 1, f_win_findbuf},
941 {"win_getid", 0, 2, f_win_getid},
942 {"win_gotoid", 1, 1, f_win_gotoid},
943 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
944 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100945 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200946 {"winbufnr", 1, 1, f_winbufnr},
947 {"wincol", 0, 0, f_wincol},
948 {"winheight", 1, 1, f_winheight},
949 {"winline", 0, 0, f_winline},
950 {"winnr", 0, 1, f_winnr},
951 {"winrestcmd", 0, 0, f_winrestcmd},
952 {"winrestview", 1, 1, f_winrestview},
953 {"winsaveview", 0, 0, f_winsaveview},
954 {"winwidth", 1, 1, f_winwidth},
955 {"wordcount", 0, 0, f_wordcount},
956 {"writefile", 2, 3, f_writefile},
957 {"xor", 2, 2, f_xor},
958};
959
960#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
961
962/*
963 * Function given to ExpandGeneric() to obtain the list of internal
964 * or user defined function names.
965 */
966 char_u *
967get_function_name(expand_T *xp, int idx)
968{
969 static int intidx = -1;
970 char_u *name;
971
972 if (idx == 0)
973 intidx = -1;
974 if (intidx < 0)
975 {
976 name = get_user_func_name(xp, idx);
977 if (name != NULL)
978 return name;
979 }
980 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
981 {
982 STRCPY(IObuff, functions[intidx].f_name);
983 STRCAT(IObuff, "(");
984 if (functions[intidx].f_max_argc == 0)
985 STRCAT(IObuff, ")");
986 return IObuff;
987 }
988
989 return NULL;
990}
991
992/*
993 * Function given to ExpandGeneric() to obtain the list of internal or
994 * user defined variable or function names.
995 */
996 char_u *
997get_expr_name(expand_T *xp, int idx)
998{
999 static int intidx = -1;
1000 char_u *name;
1001
1002 if (idx == 0)
1003 intidx = -1;
1004 if (intidx < 0)
1005 {
1006 name = get_function_name(xp, idx);
1007 if (name != NULL)
1008 return name;
1009 }
1010 return get_user_var_name(xp, ++intidx);
1011}
1012
1013#endif /* FEAT_CMDL_COMPL */
1014
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001015/*
1016 * Find internal function in table above.
1017 * Return index, or -1 if not found
1018 */
1019 int
1020find_internal_func(
1021 char_u *name) /* name of the function */
1022{
1023 int first = 0;
1024 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1025 int cmp;
1026 int x;
1027
1028 /*
1029 * Find the function name in the table. Binary search.
1030 */
1031 while (first <= last)
1032 {
1033 x = first + ((unsigned)(last - first) >> 1);
1034 cmp = STRCMP(name, functions[x].f_name);
1035 if (cmp < 0)
1036 last = x - 1;
1037 else if (cmp > 0)
1038 first = x + 1;
1039 else
1040 return x;
1041 }
1042 return -1;
1043}
1044
1045 int
1046call_internal_func(
1047 char_u *name,
1048 int argcount,
1049 typval_T *argvars,
1050 typval_T *rettv)
1051{
1052 int i;
1053
1054 i = find_internal_func(name);
1055 if (i < 0)
1056 return ERROR_UNKNOWN;
1057 if (argcount < functions[i].f_min_argc)
1058 return ERROR_TOOFEW;
1059 if (argcount > functions[i].f_max_argc)
1060 return ERROR_TOOMANY;
1061 argvars[argcount].v_type = VAR_UNKNOWN;
1062 functions[i].f_func(argvars, rettv);
1063 return ERROR_NONE;
1064}
1065
1066/*
1067 * Return TRUE for a non-zero Number and a non-empty String.
1068 */
1069 static int
1070non_zero_arg(typval_T *argvars)
1071{
1072 return ((argvars[0].v_type == VAR_NUMBER
1073 && argvars[0].vval.v_number != 0)
1074 || (argvars[0].v_type == VAR_SPECIAL
1075 && argvars[0].vval.v_number == VVAL_TRUE)
1076 || (argvars[0].v_type == VAR_STRING
1077 && argvars[0].vval.v_string != NULL
1078 && *argvars[0].vval.v_string != NUL));
1079}
1080
1081/*
1082 * Get the lnum from the first argument.
1083 * Also accepts ".", "$", etc., but that only works for the current buffer.
1084 * Returns -1 on error.
1085 */
1086 static linenr_T
1087get_tv_lnum(typval_T *argvars)
1088{
1089 typval_T rettv;
1090 linenr_T lnum;
1091
1092 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1093 if (lnum == 0) /* no valid number, try using line() */
1094 {
1095 rettv.v_type = VAR_NUMBER;
1096 f_line(argvars, &rettv);
1097 lnum = (linenr_T)rettv.vval.v_number;
1098 clear_tv(&rettv);
1099 }
1100 return lnum;
1101}
1102
1103#ifdef FEAT_FLOAT
1104static int get_float_arg(typval_T *argvars, float_T *f);
1105
1106/*
1107 * Get the float value of "argvars[0]" into "f".
1108 * Returns FAIL when the argument is not a Number or Float.
1109 */
1110 static int
1111get_float_arg(typval_T *argvars, float_T *f)
1112{
1113 if (argvars[0].v_type == VAR_FLOAT)
1114 {
1115 *f = argvars[0].vval.v_float;
1116 return OK;
1117 }
1118 if (argvars[0].v_type == VAR_NUMBER)
1119 {
1120 *f = (float_T)argvars[0].vval.v_number;
1121 return OK;
1122 }
1123 EMSG(_("E808: Number or Float required"));
1124 return FAIL;
1125}
1126
1127/*
1128 * "abs(expr)" function
1129 */
1130 static void
1131f_abs(typval_T *argvars, typval_T *rettv)
1132{
1133 if (argvars[0].v_type == VAR_FLOAT)
1134 {
1135 rettv->v_type = VAR_FLOAT;
1136 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1137 }
1138 else
1139 {
1140 varnumber_T n;
1141 int error = FALSE;
1142
1143 n = get_tv_number_chk(&argvars[0], &error);
1144 if (error)
1145 rettv->vval.v_number = -1;
1146 else if (n > 0)
1147 rettv->vval.v_number = n;
1148 else
1149 rettv->vval.v_number = -n;
1150 }
1151}
1152
1153/*
1154 * "acos()" function
1155 */
1156 static void
1157f_acos(typval_T *argvars, typval_T *rettv)
1158{
1159 float_T f = 0.0;
1160
1161 rettv->v_type = VAR_FLOAT;
1162 if (get_float_arg(argvars, &f) == OK)
1163 rettv->vval.v_float = acos(f);
1164 else
1165 rettv->vval.v_float = 0.0;
1166}
1167#endif
1168
1169/*
1170 * "add(list, item)" function
1171 */
1172 static void
1173f_add(typval_T *argvars, typval_T *rettv)
1174{
1175 list_T *l;
1176
1177 rettv->vval.v_number = 1; /* Default: Failed */
1178 if (argvars[0].v_type == VAR_LIST)
1179 {
1180 if ((l = argvars[0].vval.v_list) != NULL
1181 && !tv_check_lock(l->lv_lock,
1182 (char_u *)N_("add() argument"), TRUE)
1183 && list_append_tv(l, &argvars[1]) == OK)
1184 copy_tv(&argvars[0], rettv);
1185 }
1186 else
1187 EMSG(_(e_listreq));
1188}
1189
1190/*
1191 * "and(expr, expr)" function
1192 */
1193 static void
1194f_and(typval_T *argvars, typval_T *rettv)
1195{
1196 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1197 & get_tv_number_chk(&argvars[1], NULL);
1198}
1199
1200/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001201 * Get the lnum from the first argument.
1202 * Also accepts "$", then "buf" is used.
1203 * Returns 0 on error.
1204 */
1205 static linenr_T
1206get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
1207{
1208 if (argvars[0].v_type == VAR_STRING
1209 && argvars[0].vval.v_string != NULL
1210 && argvars[0].vval.v_string[0] == '$'
1211 && buf != NULL)
1212 return buf->b_ml.ml_line_count;
1213 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1214}
1215
1216/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001217 * If there is a window for "curbuf", make it the current window.
1218 */
1219 static void
1220find_win_for_curbuf(void)
1221{
1222 wininfo_T *wip;
1223
1224 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1225 {
1226 if (wip->wi_win != NULL)
1227 {
1228 curwin = wip->wi_win;
1229 break;
1230 }
1231 }
1232}
1233
1234/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001235 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001236 */
1237 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001238set_buffer_lines(
1239 buf_T *buf,
1240 linenr_T lnum_arg,
1241 int append,
1242 typval_T *lines,
1243 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001244{
Bram Moolenaarca851592018-06-06 21:04:07 +02001245 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1246 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001247 list_T *l = NULL;
1248 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001249 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001250 linenr_T append_lnum;
1251 buf_T *curbuf_save = NULL;
1252 win_T *curwin_save = NULL;
1253 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001254
Bram Moolenaarca851592018-06-06 21:04:07 +02001255 /* When using the current buffer ml_mfp will be set if needed. Useful when
1256 * setline() is used on startup. For other buffers the buffer must be
1257 * loaded. */
1258 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001259 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001260 rettv->vval.v_number = 1; /* FAIL */
1261 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001262 }
1263
Bram Moolenaarca851592018-06-06 21:04:07 +02001264 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001265 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001266 curbuf_save = curbuf;
1267 curwin_save = curwin;
1268 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001269 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001270 }
1271
1272 if (append)
1273 // appendbufline() uses the line number below which we insert
1274 append_lnum = lnum - 1;
1275 else
1276 // setbufline() uses the line number above which we insert, we only
1277 // append if it's below the last line
1278 append_lnum = curbuf->b_ml.ml_line_count;
1279
1280 if (lines->v_type == VAR_LIST)
1281 {
1282 l = lines->vval.v_list;
1283 li = l->lv_first;
1284 }
1285 else
1286 line = get_tv_string_chk(lines);
1287
1288 /* default result is zero == OK */
1289 for (;;)
1290 {
1291 if (l != NULL)
1292 {
1293 /* list argument, get next string */
1294 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001295 break;
Bram Moolenaarca851592018-06-06 21:04:07 +02001296 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001297 li = li->li_next;
1298 }
1299
Bram Moolenaarca851592018-06-06 21:04:07 +02001300 rettv->vval.v_number = 1; /* FAIL */
1301 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1302 break;
1303
1304 /* When coming here from Insert mode, sync undo, so that this can be
1305 * undone separately from what was previously inserted. */
1306 if (u_sync_once == 2)
1307 {
1308 u_sync_once = 1; /* notify that u_sync() was called */
1309 u_sync(TRUE);
1310 }
1311
1312 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1313 {
1314 /* existing line, replace it */
1315 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
1316 {
1317 changed_bytes(lnum, 0);
1318 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1319 check_cursor_col();
1320 rettv->vval.v_number = 0; /* OK */
1321 }
1322 }
1323 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1324 {
1325 /* append the line */
1326 ++added;
1327 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1328 rettv->vval.v_number = 0; /* OK */
1329 }
1330
1331 if (l == NULL) /* only one string argument */
1332 break;
1333 ++lnum;
1334 }
1335
1336 if (added > 0)
1337 {
1338 win_T *wp;
1339 tabpage_T *tp;
1340
1341 appended_lines_mark(append_lnum, added);
1342 FOR_ALL_TAB_WINDOWS(tp, wp)
1343 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1344 wp->w_cursor.lnum += added;
1345 check_cursor_col();
1346
Bram Moolenaarf2732452018-06-03 14:47:35 +02001347#ifdef FEAT_JOB_CHANNEL
1348 if (bt_prompt(curbuf) && (State & INSERT))
1349 // show the line with the prompt
1350 update_topline();
1351#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001352 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001353
1354 if (!is_curbuf)
1355 {
1356 curbuf = curbuf_save;
1357 curwin = curwin_save;
1358 }
1359}
1360
1361/*
1362 * "append(lnum, string/list)" function
1363 */
1364 static void
1365f_append(typval_T *argvars, typval_T *rettv)
1366{
1367 linenr_T lnum = get_tv_lnum(&argvars[0]);
1368
1369 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1370}
1371
1372/*
1373 * "appendbufline(buf, lnum, string/list)" function
1374 */
1375 static void
1376f_appendbufline(typval_T *argvars, typval_T *rettv)
1377{
1378 linenr_T lnum;
1379 buf_T *buf;
1380
1381 buf = get_buf_tv(&argvars[0], FALSE);
1382 if (buf == NULL)
1383 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001384 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001385 {
1386 lnum = get_tv_lnum_buf(&argvars[1], buf);
1387 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1388 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001389}
1390
1391/*
1392 * "argc()" function
1393 */
1394 static void
1395f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1396{
1397 rettv->vval.v_number = ARGCOUNT;
1398}
1399
1400/*
1401 * "argidx()" function
1402 */
1403 static void
1404f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1405{
1406 rettv->vval.v_number = curwin->w_arg_idx;
1407}
1408
1409/*
1410 * "arglistid()" function
1411 */
1412 static void
1413f_arglistid(typval_T *argvars, typval_T *rettv)
1414{
1415 win_T *wp;
1416
1417 rettv->vval.v_number = -1;
1418 wp = find_tabwin(&argvars[0], &argvars[1]);
1419 if (wp != NULL)
1420 rettv->vval.v_number = wp->w_alist->id;
1421}
1422
1423/*
1424 * "argv(nr)" function
1425 */
1426 static void
1427f_argv(typval_T *argvars, typval_T *rettv)
1428{
1429 int idx;
1430
1431 if (argvars[0].v_type != VAR_UNKNOWN)
1432 {
1433 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1434 if (idx >= 0 && idx < ARGCOUNT)
1435 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1436 else
1437 rettv->vval.v_string = NULL;
1438 rettv->v_type = VAR_STRING;
1439 }
1440 else if (rettv_list_alloc(rettv) == OK)
1441 for (idx = 0; idx < ARGCOUNT; ++idx)
1442 list_append_string(rettv->vval.v_list,
1443 alist_name(&ARGLIST[idx]), -1);
1444}
1445
1446/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001447 * "assert_beeps(cmd [, error])" function
1448 */
1449 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001450f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001451{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001452 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001453}
1454
1455/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001456 * "assert_equal(expected, actual[, msg])" function
1457 */
1458 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001459f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001460{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001461 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001462}
1463
1464/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001465 * "assert_equalfile(fname-one, fname-two)" function
1466 */
1467 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001468f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001469{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001470 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001471}
1472
1473/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001474 * "assert_notequal(expected, actual[, msg])" function
1475 */
1476 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001477f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001478{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001479 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001480}
1481
1482/*
1483 * "assert_exception(string[, msg])" function
1484 */
1485 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001486f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001487{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001488 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001489}
1490
1491/*
1492 * "assert_fails(cmd [, error])" function
1493 */
1494 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001495f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001496{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001497 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001498}
1499
1500/*
1501 * "assert_false(actual[, msg])" function
1502 */
1503 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001504f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001505{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001506 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001507}
1508
1509/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001510 * "assert_inrange(lower, upper[, msg])" function
1511 */
1512 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001513f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001514{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001515 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001516}
1517
1518/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001519 * "assert_match(pattern, actual[, msg])" function
1520 */
1521 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001522f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001523{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001524 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001525}
1526
1527/*
1528 * "assert_notmatch(pattern, actual[, msg])" function
1529 */
1530 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001531f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001532{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001533 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001534}
1535
1536/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001537 * "assert_report(msg)" function
1538 */
1539 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001540f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001541{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001542 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001543}
1544
1545/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001546 * "assert_true(actual[, msg])" function
1547 */
1548 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001549f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001550{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001551 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001552}
1553
1554#ifdef FEAT_FLOAT
1555/*
1556 * "asin()" function
1557 */
1558 static void
1559f_asin(typval_T *argvars, typval_T *rettv)
1560{
1561 float_T f = 0.0;
1562
1563 rettv->v_type = VAR_FLOAT;
1564 if (get_float_arg(argvars, &f) == OK)
1565 rettv->vval.v_float = asin(f);
1566 else
1567 rettv->vval.v_float = 0.0;
1568}
1569
1570/*
1571 * "atan()" function
1572 */
1573 static void
1574f_atan(typval_T *argvars, typval_T *rettv)
1575{
1576 float_T f = 0.0;
1577
1578 rettv->v_type = VAR_FLOAT;
1579 if (get_float_arg(argvars, &f) == OK)
1580 rettv->vval.v_float = atan(f);
1581 else
1582 rettv->vval.v_float = 0.0;
1583}
1584
1585/*
1586 * "atan2()" function
1587 */
1588 static void
1589f_atan2(typval_T *argvars, typval_T *rettv)
1590{
1591 float_T fx = 0.0, fy = 0.0;
1592
1593 rettv->v_type = VAR_FLOAT;
1594 if (get_float_arg(argvars, &fx) == OK
1595 && get_float_arg(&argvars[1], &fy) == OK)
1596 rettv->vval.v_float = atan2(fx, fy);
1597 else
1598 rettv->vval.v_float = 0.0;
1599}
1600#endif
1601
1602/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001603 * "balloon_show()" function
1604 */
1605#ifdef FEAT_BEVAL
1606 static void
1607f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1608{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001609 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001610 {
1611 if (argvars[0].v_type == VAR_LIST
1612# ifdef FEAT_GUI
1613 && !gui.in_use
1614# endif
1615 )
1616 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1617 else
1618 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1619 }
1620}
1621
Bram Moolenaar669a8282017-11-19 20:13:05 +01001622# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001623 static void
1624f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1625{
1626 if (rettv_list_alloc(rettv) == OK)
1627 {
1628 char_u *msg = get_tv_string_chk(&argvars[0]);
1629
1630 if (msg != NULL)
1631 {
1632 pumitem_T *array;
1633 int size = split_message(msg, &array);
1634 int i;
1635
1636 /* Skip the first and last item, they are always empty. */
1637 for (i = 1; i < size - 1; ++i)
1638 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001639 while (size > 0)
1640 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001641 vim_free(array);
1642 }
1643 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001644}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001645# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001646#endif
1647
1648/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001649 * "browse(save, title, initdir, default)" function
1650 */
1651 static void
1652f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1653{
1654#ifdef FEAT_BROWSE
1655 int save;
1656 char_u *title;
1657 char_u *initdir;
1658 char_u *defname;
1659 char_u buf[NUMBUFLEN];
1660 char_u buf2[NUMBUFLEN];
1661 int error = FALSE;
1662
1663 save = (int)get_tv_number_chk(&argvars[0], &error);
1664 title = get_tv_string_chk(&argvars[1]);
1665 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1666 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1667
1668 if (error || title == NULL || initdir == NULL || defname == NULL)
1669 rettv->vval.v_string = NULL;
1670 else
1671 rettv->vval.v_string =
1672 do_browse(save ? BROWSE_SAVE : 0,
1673 title, defname, NULL, initdir, NULL, curbuf);
1674#else
1675 rettv->vval.v_string = NULL;
1676#endif
1677 rettv->v_type = VAR_STRING;
1678}
1679
1680/*
1681 * "browsedir(title, initdir)" function
1682 */
1683 static void
1684f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1685{
1686#ifdef FEAT_BROWSE
1687 char_u *title;
1688 char_u *initdir;
1689 char_u buf[NUMBUFLEN];
1690
1691 title = get_tv_string_chk(&argvars[0]);
1692 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1693
1694 if (title == NULL || initdir == NULL)
1695 rettv->vval.v_string = NULL;
1696 else
1697 rettv->vval.v_string = do_browse(BROWSE_DIR,
1698 title, NULL, NULL, initdir, NULL, curbuf);
1699#else
1700 rettv->vval.v_string = NULL;
1701#endif
1702 rettv->v_type = VAR_STRING;
1703}
1704
1705static buf_T *find_buffer(typval_T *avar);
1706
1707/*
1708 * Find a buffer by number or exact name.
1709 */
1710 static buf_T *
1711find_buffer(typval_T *avar)
1712{
1713 buf_T *buf = NULL;
1714
1715 if (avar->v_type == VAR_NUMBER)
1716 buf = buflist_findnr((int)avar->vval.v_number);
1717 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1718 {
1719 buf = buflist_findname_exp(avar->vval.v_string);
1720 if (buf == NULL)
1721 {
1722 /* No full path name match, try a match with a URL or a "nofile"
1723 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001724 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001725 if (buf->b_fname != NULL
1726 && (path_with_url(buf->b_fname)
1727#ifdef FEAT_QUICKFIX
1728 || bt_nofile(buf)
1729#endif
1730 )
1731 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1732 break;
1733 }
1734 }
1735 return buf;
1736}
1737
1738/*
1739 * "bufexists(expr)" function
1740 */
1741 static void
1742f_bufexists(typval_T *argvars, typval_T *rettv)
1743{
1744 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1745}
1746
1747/*
1748 * "buflisted(expr)" function
1749 */
1750 static void
1751f_buflisted(typval_T *argvars, typval_T *rettv)
1752{
1753 buf_T *buf;
1754
1755 buf = find_buffer(&argvars[0]);
1756 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1757}
1758
1759/*
1760 * "bufloaded(expr)" function
1761 */
1762 static void
1763f_bufloaded(typval_T *argvars, typval_T *rettv)
1764{
1765 buf_T *buf;
1766
1767 buf = find_buffer(&argvars[0]);
1768 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1769}
1770
1771 buf_T *
1772buflist_find_by_name(char_u *name, int curtab_only)
1773{
1774 int save_magic;
1775 char_u *save_cpo;
1776 buf_T *buf;
1777
1778 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1779 save_magic = p_magic;
1780 p_magic = TRUE;
1781 save_cpo = p_cpo;
1782 p_cpo = (char_u *)"";
1783
1784 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1785 TRUE, FALSE, curtab_only));
1786
1787 p_magic = save_magic;
1788 p_cpo = save_cpo;
1789 return buf;
1790}
1791
1792/*
1793 * Get buffer by number or pattern.
1794 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001795 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001796get_buf_tv(typval_T *tv, int curtab_only)
1797{
1798 char_u *name = tv->vval.v_string;
1799 buf_T *buf;
1800
1801 if (tv->v_type == VAR_NUMBER)
1802 return buflist_findnr((int)tv->vval.v_number);
1803 if (tv->v_type != VAR_STRING)
1804 return NULL;
1805 if (name == NULL || *name == NUL)
1806 return curbuf;
1807 if (name[0] == '$' && name[1] == NUL)
1808 return lastbuf;
1809
1810 buf = buflist_find_by_name(name, curtab_only);
1811
1812 /* If not found, try expanding the name, like done for bufexists(). */
1813 if (buf == NULL)
1814 buf = find_buffer(tv);
1815
1816 return buf;
1817}
1818
1819/*
1820 * "bufname(expr)" function
1821 */
1822 static void
1823f_bufname(typval_T *argvars, typval_T *rettv)
1824{
1825 buf_T *buf;
1826
1827 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1828 ++emsg_off;
1829 buf = get_buf_tv(&argvars[0], FALSE);
1830 rettv->v_type = VAR_STRING;
1831 if (buf != NULL && buf->b_fname != NULL)
1832 rettv->vval.v_string = vim_strsave(buf->b_fname);
1833 else
1834 rettv->vval.v_string = NULL;
1835 --emsg_off;
1836}
1837
1838/*
1839 * "bufnr(expr)" function
1840 */
1841 static void
1842f_bufnr(typval_T *argvars, typval_T *rettv)
1843{
1844 buf_T *buf;
1845 int error = FALSE;
1846 char_u *name;
1847
1848 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1849 ++emsg_off;
1850 buf = get_buf_tv(&argvars[0], FALSE);
1851 --emsg_off;
1852
1853 /* If the buffer isn't found and the second argument is not zero create a
1854 * new buffer. */
1855 if (buf == NULL
1856 && argvars[1].v_type != VAR_UNKNOWN
1857 && get_tv_number_chk(&argvars[1], &error) != 0
1858 && !error
1859 && (name = get_tv_string_chk(&argvars[0])) != NULL
1860 && !error)
1861 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1862
1863 if (buf != NULL)
1864 rettv->vval.v_number = buf->b_fnum;
1865 else
1866 rettv->vval.v_number = -1;
1867}
1868
1869 static void
1870buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1871{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001872 win_T *wp;
1873 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001874 buf_T *buf;
1875
1876 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1877 ++emsg_off;
1878 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001879 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001880 {
1881 ++winnr;
1882 if (wp->w_buffer == buf)
1883 break;
1884 }
1885 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001886 --emsg_off;
1887}
1888
1889/*
1890 * "bufwinid(nr)" function
1891 */
1892 static void
1893f_bufwinid(typval_T *argvars, typval_T *rettv)
1894{
1895 buf_win_common(argvars, rettv, FALSE);
1896}
1897
1898/*
1899 * "bufwinnr(nr)" function
1900 */
1901 static void
1902f_bufwinnr(typval_T *argvars, typval_T *rettv)
1903{
1904 buf_win_common(argvars, rettv, TRUE);
1905}
1906
1907/*
1908 * "byte2line(byte)" function
1909 */
1910 static void
1911f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1912{
1913#ifndef FEAT_BYTEOFF
1914 rettv->vval.v_number = -1;
1915#else
1916 long boff = 0;
1917
1918 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1919 if (boff < 0)
1920 rettv->vval.v_number = -1;
1921 else
1922 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1923 (linenr_T)0, &boff);
1924#endif
1925}
1926
1927 static void
1928byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1929{
1930#ifdef FEAT_MBYTE
1931 char_u *t;
1932#endif
1933 char_u *str;
1934 varnumber_T idx;
1935
1936 str = get_tv_string_chk(&argvars[0]);
1937 idx = get_tv_number_chk(&argvars[1], NULL);
1938 rettv->vval.v_number = -1;
1939 if (str == NULL || idx < 0)
1940 return;
1941
1942#ifdef FEAT_MBYTE
1943 t = str;
1944 for ( ; idx > 0; idx--)
1945 {
1946 if (*t == NUL) /* EOL reached */
1947 return;
1948 if (enc_utf8 && comp)
1949 t += utf_ptr2len(t);
1950 else
1951 t += (*mb_ptr2len)(t);
1952 }
1953 rettv->vval.v_number = (varnumber_T)(t - str);
1954#else
1955 if ((size_t)idx <= STRLEN(str))
1956 rettv->vval.v_number = idx;
1957#endif
1958}
1959
1960/*
1961 * "byteidx()" function
1962 */
1963 static void
1964f_byteidx(typval_T *argvars, typval_T *rettv)
1965{
1966 byteidx(argvars, rettv, FALSE);
1967}
1968
1969/*
1970 * "byteidxcomp()" function
1971 */
1972 static void
1973f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1974{
1975 byteidx(argvars, rettv, TRUE);
1976}
1977
1978/*
1979 * "call(func, arglist [, dict])" function
1980 */
1981 static void
1982f_call(typval_T *argvars, typval_T *rettv)
1983{
1984 char_u *func;
1985 partial_T *partial = NULL;
1986 dict_T *selfdict = NULL;
1987
1988 if (argvars[1].v_type != VAR_LIST)
1989 {
1990 EMSG(_(e_listreq));
1991 return;
1992 }
1993 if (argvars[1].vval.v_list == NULL)
1994 return;
1995
1996 if (argvars[0].v_type == VAR_FUNC)
1997 func = argvars[0].vval.v_string;
1998 else if (argvars[0].v_type == VAR_PARTIAL)
1999 {
2000 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002001 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002002 }
2003 else
2004 func = get_tv_string(&argvars[0]);
2005 if (*func == NUL)
2006 return; /* type error or empty name */
2007
2008 if (argvars[2].v_type != VAR_UNKNOWN)
2009 {
2010 if (argvars[2].v_type != VAR_DICT)
2011 {
2012 EMSG(_(e_dictreq));
2013 return;
2014 }
2015 selfdict = argvars[2].vval.v_dict;
2016 }
2017
2018 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2019}
2020
2021#ifdef FEAT_FLOAT
2022/*
2023 * "ceil({float})" function
2024 */
2025 static void
2026f_ceil(typval_T *argvars, typval_T *rettv)
2027{
2028 float_T f = 0.0;
2029
2030 rettv->v_type = VAR_FLOAT;
2031 if (get_float_arg(argvars, &f) == OK)
2032 rettv->vval.v_float = ceil(f);
2033 else
2034 rettv->vval.v_float = 0.0;
2035}
2036#endif
2037
2038#ifdef FEAT_JOB_CHANNEL
2039/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002040 * "ch_canread()" function
2041 */
2042 static void
2043f_ch_canread(typval_T *argvars, typval_T *rettv)
2044{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002045 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002046
2047 rettv->vval.v_number = 0;
2048 if (channel != NULL)
2049 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2050 || channel_has_readahead(channel, PART_OUT)
2051 || channel_has_readahead(channel, PART_ERR);
2052}
2053
2054/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002055 * "ch_close()" function
2056 */
2057 static void
2058f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2059{
2060 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2061
2062 if (channel != NULL)
2063 {
2064 channel_close(channel, FALSE);
2065 channel_clear(channel);
2066 }
2067}
2068
2069/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002070 * "ch_close()" function
2071 */
2072 static void
2073f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2074{
2075 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2076
2077 if (channel != NULL)
2078 channel_close_in(channel);
2079}
2080
2081/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002082 * "ch_getbufnr()" function
2083 */
2084 static void
2085f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2086{
2087 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2088
2089 rettv->vval.v_number = -1;
2090 if (channel != NULL)
2091 {
2092 char_u *what = get_tv_string(&argvars[1]);
2093 int part;
2094
2095 if (STRCMP(what, "err") == 0)
2096 part = PART_ERR;
2097 else if (STRCMP(what, "out") == 0)
2098 part = PART_OUT;
2099 else if (STRCMP(what, "in") == 0)
2100 part = PART_IN;
2101 else
2102 part = PART_SOCK;
2103 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2104 rettv->vval.v_number =
2105 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2106 }
2107}
2108
2109/*
2110 * "ch_getjob()" function
2111 */
2112 static void
2113f_ch_getjob(typval_T *argvars, typval_T *rettv)
2114{
2115 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2116
2117 if (channel != NULL)
2118 {
2119 rettv->v_type = VAR_JOB;
2120 rettv->vval.v_job = channel->ch_job;
2121 if (channel->ch_job != NULL)
2122 ++channel->ch_job->jv_refcount;
2123 }
2124}
2125
2126/*
2127 * "ch_info()" function
2128 */
2129 static void
2130f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2131{
2132 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2133
2134 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2135 channel_info(channel, rettv->vval.v_dict);
2136}
2137
2138/*
2139 * "ch_log()" function
2140 */
2141 static void
2142f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2143{
2144 char_u *msg = get_tv_string(&argvars[0]);
2145 channel_T *channel = NULL;
2146
2147 if (argvars[1].v_type != VAR_UNKNOWN)
2148 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2149
Bram Moolenaard5359b22018-04-05 22:44:39 +02002150 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002151}
2152
2153/*
2154 * "ch_logfile()" function
2155 */
2156 static void
2157f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2158{
2159 char_u *fname;
2160 char_u *opt = (char_u *)"";
2161 char_u buf[NUMBUFLEN];
2162
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002163 /* Don't open a file in restricted mode. */
2164 if (check_restricted() || check_secure())
2165 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002166 fname = get_tv_string(&argvars[0]);
2167 if (argvars[1].v_type == VAR_STRING)
2168 opt = get_tv_string_buf(&argvars[1], buf);
2169 ch_logfile(fname, opt);
2170}
2171
2172/*
2173 * "ch_open()" function
2174 */
2175 static void
2176f_ch_open(typval_T *argvars, typval_T *rettv)
2177{
2178 rettv->v_type = VAR_CHANNEL;
2179 if (check_restricted() || check_secure())
2180 return;
2181 rettv->vval.v_channel = channel_open_func(argvars);
2182}
2183
2184/*
2185 * "ch_read()" function
2186 */
2187 static void
2188f_ch_read(typval_T *argvars, typval_T *rettv)
2189{
2190 common_channel_read(argvars, rettv, FALSE);
2191}
2192
2193/*
2194 * "ch_readraw()" function
2195 */
2196 static void
2197f_ch_readraw(typval_T *argvars, typval_T *rettv)
2198{
2199 common_channel_read(argvars, rettv, TRUE);
2200}
2201
2202/*
2203 * "ch_evalexpr()" function
2204 */
2205 static void
2206f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2207{
2208 ch_expr_common(argvars, rettv, TRUE);
2209}
2210
2211/*
2212 * "ch_sendexpr()" function
2213 */
2214 static void
2215f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2216{
2217 ch_expr_common(argvars, rettv, FALSE);
2218}
2219
2220/*
2221 * "ch_evalraw()" function
2222 */
2223 static void
2224f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2225{
2226 ch_raw_common(argvars, rettv, TRUE);
2227}
2228
2229/*
2230 * "ch_sendraw()" function
2231 */
2232 static void
2233f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2234{
2235 ch_raw_common(argvars, rettv, FALSE);
2236}
2237
2238/*
2239 * "ch_setoptions()" function
2240 */
2241 static void
2242f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2243{
2244 channel_T *channel;
2245 jobopt_T opt;
2246
2247 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2248 if (channel == NULL)
2249 return;
2250 clear_job_options(&opt);
2251 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002252 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002253 channel_set_options(channel, &opt);
2254 free_job_options(&opt);
2255}
2256
2257/*
2258 * "ch_status()" function
2259 */
2260 static void
2261f_ch_status(typval_T *argvars, typval_T *rettv)
2262{
2263 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002264 jobopt_T opt;
2265 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002266
2267 /* return an empty string by default */
2268 rettv->v_type = VAR_STRING;
2269 rettv->vval.v_string = NULL;
2270
2271 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002272
2273 if (argvars[1].v_type != VAR_UNKNOWN)
2274 {
2275 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002276 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002277 && (opt.jo_set & JO_PART))
2278 part = opt.jo_part;
2279 }
2280
2281 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002282}
2283#endif
2284
2285/*
2286 * "changenr()" function
2287 */
2288 static void
2289f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2290{
2291 rettv->vval.v_number = curbuf->b_u_seq_cur;
2292}
2293
2294/*
2295 * "char2nr(string)" function
2296 */
2297 static void
2298f_char2nr(typval_T *argvars, typval_T *rettv)
2299{
2300#ifdef FEAT_MBYTE
2301 if (has_mbyte)
2302 {
2303 int utf8 = 0;
2304
2305 if (argvars[1].v_type != VAR_UNKNOWN)
2306 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2307
2308 if (utf8)
2309 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2310 else
2311 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2312 }
2313 else
2314#endif
2315 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2316}
2317
2318/*
2319 * "cindent(lnum)" function
2320 */
2321 static void
2322f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2323{
2324#ifdef FEAT_CINDENT
2325 pos_T pos;
2326 linenr_T lnum;
2327
2328 pos = curwin->w_cursor;
2329 lnum = get_tv_lnum(argvars);
2330 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2331 {
2332 curwin->w_cursor.lnum = lnum;
2333 rettv->vval.v_number = get_c_indent();
2334 curwin->w_cursor = pos;
2335 }
2336 else
2337#endif
2338 rettv->vval.v_number = -1;
2339}
2340
2341/*
2342 * "clearmatches()" function
2343 */
2344 static void
2345f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2346{
2347#ifdef FEAT_SEARCH_EXTRA
2348 clear_matches(curwin);
2349#endif
2350}
2351
2352/*
2353 * "col(string)" function
2354 */
2355 static void
2356f_col(typval_T *argvars, typval_T *rettv)
2357{
2358 colnr_T col = 0;
2359 pos_T *fp;
2360 int fnum = curbuf->b_fnum;
2361
2362 fp = var2fpos(&argvars[0], FALSE, &fnum);
2363 if (fp != NULL && fnum == curbuf->b_fnum)
2364 {
2365 if (fp->col == MAXCOL)
2366 {
2367 /* '> can be MAXCOL, get the length of the line then */
2368 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2369 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2370 else
2371 col = MAXCOL;
2372 }
2373 else
2374 {
2375 col = fp->col + 1;
2376#ifdef FEAT_VIRTUALEDIT
2377 /* col(".") when the cursor is on the NUL at the end of the line
2378 * because of "coladd" can be seen as an extra column. */
2379 if (virtual_active() && fp == &curwin->w_cursor)
2380 {
2381 char_u *p = ml_get_cursor();
2382
2383 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2384 curwin->w_virtcol - curwin->w_cursor.coladd))
2385 {
2386# ifdef FEAT_MBYTE
2387 int l;
2388
2389 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2390 col += l;
2391# else
2392 if (*p != NUL && p[1] == NUL)
2393 ++col;
2394# endif
2395 }
2396 }
2397#endif
2398 }
2399 }
2400 rettv->vval.v_number = col;
2401}
2402
2403#if defined(FEAT_INS_EXPAND)
2404/*
2405 * "complete()" function
2406 */
2407 static void
2408f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2409{
2410 int startcol;
2411
2412 if ((State & INSERT) == 0)
2413 {
2414 EMSG(_("E785: complete() can only be used in Insert mode"));
2415 return;
2416 }
2417
2418 /* Check for undo allowed here, because if something was already inserted
2419 * the line was already saved for undo and this check isn't done. */
2420 if (!undo_allowed())
2421 return;
2422
2423 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2424 {
2425 EMSG(_(e_invarg));
2426 return;
2427 }
2428
2429 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2430 if (startcol <= 0)
2431 return;
2432
2433 set_completion(startcol - 1, argvars[1].vval.v_list);
2434}
2435
2436/*
2437 * "complete_add()" function
2438 */
2439 static void
2440f_complete_add(typval_T *argvars, typval_T *rettv)
2441{
2442 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2443}
2444
2445/*
2446 * "complete_check()" function
2447 */
2448 static void
2449f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2450{
2451 int saved = RedrawingDisabled;
2452
2453 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002454 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002455 rettv->vval.v_number = compl_interrupted;
2456 RedrawingDisabled = saved;
2457}
2458#endif
2459
2460/*
2461 * "confirm(message, buttons[, default [, type]])" function
2462 */
2463 static void
2464f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2465{
2466#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2467 char_u *message;
2468 char_u *buttons = NULL;
2469 char_u buf[NUMBUFLEN];
2470 char_u buf2[NUMBUFLEN];
2471 int def = 1;
2472 int type = VIM_GENERIC;
2473 char_u *typestr;
2474 int error = FALSE;
2475
2476 message = get_tv_string_chk(&argvars[0]);
2477 if (message == NULL)
2478 error = TRUE;
2479 if (argvars[1].v_type != VAR_UNKNOWN)
2480 {
2481 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2482 if (buttons == NULL)
2483 error = TRUE;
2484 if (argvars[2].v_type != VAR_UNKNOWN)
2485 {
2486 def = (int)get_tv_number_chk(&argvars[2], &error);
2487 if (argvars[3].v_type != VAR_UNKNOWN)
2488 {
2489 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2490 if (typestr == NULL)
2491 error = TRUE;
2492 else
2493 {
2494 switch (TOUPPER_ASC(*typestr))
2495 {
2496 case 'E': type = VIM_ERROR; break;
2497 case 'Q': type = VIM_QUESTION; break;
2498 case 'I': type = VIM_INFO; break;
2499 case 'W': type = VIM_WARNING; break;
2500 case 'G': type = VIM_GENERIC; break;
2501 }
2502 }
2503 }
2504 }
2505 }
2506
2507 if (buttons == NULL || *buttons == NUL)
2508 buttons = (char_u *)_("&Ok");
2509
2510 if (!error)
2511 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2512 def, NULL, FALSE);
2513#endif
2514}
2515
2516/*
2517 * "copy()" function
2518 */
2519 static void
2520f_copy(typval_T *argvars, typval_T *rettv)
2521{
2522 item_copy(&argvars[0], rettv, FALSE, 0);
2523}
2524
2525#ifdef FEAT_FLOAT
2526/*
2527 * "cos()" function
2528 */
2529 static void
2530f_cos(typval_T *argvars, typval_T *rettv)
2531{
2532 float_T f = 0.0;
2533
2534 rettv->v_type = VAR_FLOAT;
2535 if (get_float_arg(argvars, &f) == OK)
2536 rettv->vval.v_float = cos(f);
2537 else
2538 rettv->vval.v_float = 0.0;
2539}
2540
2541/*
2542 * "cosh()" function
2543 */
2544 static void
2545f_cosh(typval_T *argvars, typval_T *rettv)
2546{
2547 float_T f = 0.0;
2548
2549 rettv->v_type = VAR_FLOAT;
2550 if (get_float_arg(argvars, &f) == OK)
2551 rettv->vval.v_float = cosh(f);
2552 else
2553 rettv->vval.v_float = 0.0;
2554}
2555#endif
2556
2557/*
2558 * "count()" function
2559 */
2560 static void
2561f_count(typval_T *argvars, typval_T *rettv)
2562{
2563 long n = 0;
2564 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002565 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002566
Bram Moolenaar9966b212017-07-28 16:46:57 +02002567 if (argvars[2].v_type != VAR_UNKNOWN)
2568 ic = (int)get_tv_number_chk(&argvars[2], &error);
2569
2570 if (argvars[0].v_type == VAR_STRING)
2571 {
2572 char_u *expr = get_tv_string_chk(&argvars[1]);
2573 char_u *p = argvars[0].vval.v_string;
2574 char_u *next;
2575
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002576 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002577 {
2578 if (ic)
2579 {
2580 size_t len = STRLEN(expr);
2581
2582 while (*p != NUL)
2583 {
2584 if (MB_STRNICMP(p, expr, len) == 0)
2585 {
2586 ++n;
2587 p += len;
2588 }
2589 else
2590 MB_PTR_ADV(p);
2591 }
2592 }
2593 else
2594 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2595 != NULL)
2596 {
2597 ++n;
2598 p = next + STRLEN(expr);
2599 }
2600 }
2601
2602 }
2603 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002604 {
2605 listitem_T *li;
2606 list_T *l;
2607 long idx;
2608
2609 if ((l = argvars[0].vval.v_list) != NULL)
2610 {
2611 li = l->lv_first;
2612 if (argvars[2].v_type != VAR_UNKNOWN)
2613 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002614 if (argvars[3].v_type != VAR_UNKNOWN)
2615 {
2616 idx = (long)get_tv_number_chk(&argvars[3], &error);
2617 if (!error)
2618 {
2619 li = list_find(l, idx);
2620 if (li == NULL)
2621 EMSGN(_(e_listidx), idx);
2622 }
2623 }
2624 if (error)
2625 li = NULL;
2626 }
2627
2628 for ( ; li != NULL; li = li->li_next)
2629 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2630 ++n;
2631 }
2632 }
2633 else if (argvars[0].v_type == VAR_DICT)
2634 {
2635 int todo;
2636 dict_T *d;
2637 hashitem_T *hi;
2638
2639 if ((d = argvars[0].vval.v_dict) != NULL)
2640 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002641 if (argvars[2].v_type != VAR_UNKNOWN)
2642 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002643 if (argvars[3].v_type != VAR_UNKNOWN)
2644 EMSG(_(e_invarg));
2645 }
2646
2647 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2648 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2649 {
2650 if (!HASHITEM_EMPTY(hi))
2651 {
2652 --todo;
2653 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2654 ++n;
2655 }
2656 }
2657 }
2658 }
2659 else
2660 EMSG2(_(e_listdictarg), "count()");
2661 rettv->vval.v_number = n;
2662}
2663
2664/*
2665 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2666 *
2667 * Checks the existence of a cscope connection.
2668 */
2669 static void
2670f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2671{
2672#ifdef FEAT_CSCOPE
2673 int num = 0;
2674 char_u *dbpath = NULL;
2675 char_u *prepend = NULL;
2676 char_u buf[NUMBUFLEN];
2677
2678 if (argvars[0].v_type != VAR_UNKNOWN
2679 && argvars[1].v_type != VAR_UNKNOWN)
2680 {
2681 num = (int)get_tv_number(&argvars[0]);
2682 dbpath = get_tv_string(&argvars[1]);
2683 if (argvars[2].v_type != VAR_UNKNOWN)
2684 prepend = get_tv_string_buf(&argvars[2], buf);
2685 }
2686
2687 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2688#endif
2689}
2690
2691/*
2692 * "cursor(lnum, col)" function, or
2693 * "cursor(list)"
2694 *
2695 * Moves the cursor to the specified line and column.
2696 * Returns 0 when the position could be set, -1 otherwise.
2697 */
2698 static void
2699f_cursor(typval_T *argvars, typval_T *rettv)
2700{
2701 long line, col;
2702#ifdef FEAT_VIRTUALEDIT
2703 long coladd = 0;
2704#endif
2705 int set_curswant = TRUE;
2706
2707 rettv->vval.v_number = -1;
2708 if (argvars[1].v_type == VAR_UNKNOWN)
2709 {
2710 pos_T pos;
2711 colnr_T curswant = -1;
2712
2713 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2714 {
2715 EMSG(_(e_invarg));
2716 return;
2717 }
2718 line = pos.lnum;
2719 col = pos.col;
2720#ifdef FEAT_VIRTUALEDIT
2721 coladd = pos.coladd;
2722#endif
2723 if (curswant >= 0)
2724 {
2725 curwin->w_curswant = curswant - 1;
2726 set_curswant = FALSE;
2727 }
2728 }
2729 else
2730 {
2731 line = get_tv_lnum(argvars);
2732 col = (long)get_tv_number_chk(&argvars[1], NULL);
2733#ifdef FEAT_VIRTUALEDIT
2734 if (argvars[2].v_type != VAR_UNKNOWN)
2735 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2736#endif
2737 }
2738 if (line < 0 || col < 0
2739#ifdef FEAT_VIRTUALEDIT
2740 || coladd < 0
2741#endif
2742 )
2743 return; /* type error; errmsg already given */
2744 if (line > 0)
2745 curwin->w_cursor.lnum = line;
2746 if (col > 0)
2747 curwin->w_cursor.col = col - 1;
2748#ifdef FEAT_VIRTUALEDIT
2749 curwin->w_cursor.coladd = coladd;
2750#endif
2751
2752 /* Make sure the cursor is in a valid position. */
2753 check_cursor();
2754#ifdef FEAT_MBYTE
2755 /* Correct cursor for multi-byte character. */
2756 if (has_mbyte)
2757 mb_adjust_cursor();
2758#endif
2759
2760 curwin->w_set_curswant = set_curswant;
2761 rettv->vval.v_number = 0;
2762}
2763
2764/*
2765 * "deepcopy()" function
2766 */
2767 static void
2768f_deepcopy(typval_T *argvars, typval_T *rettv)
2769{
2770 int noref = 0;
2771 int copyID;
2772
2773 if (argvars[1].v_type != VAR_UNKNOWN)
2774 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2775 if (noref < 0 || noref > 1)
2776 EMSG(_(e_invarg));
2777 else
2778 {
2779 copyID = get_copyID();
2780 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2781 }
2782}
2783
2784/*
2785 * "delete()" function
2786 */
2787 static void
2788f_delete(typval_T *argvars, typval_T *rettv)
2789{
2790 char_u nbuf[NUMBUFLEN];
2791 char_u *name;
2792 char_u *flags;
2793
2794 rettv->vval.v_number = -1;
2795 if (check_restricted() || check_secure())
2796 return;
2797
2798 name = get_tv_string(&argvars[0]);
2799 if (name == NULL || *name == NUL)
2800 {
2801 EMSG(_(e_invarg));
2802 return;
2803 }
2804
2805 if (argvars[1].v_type != VAR_UNKNOWN)
2806 flags = get_tv_string_buf(&argvars[1], nbuf);
2807 else
2808 flags = (char_u *)"";
2809
2810 if (*flags == NUL)
2811 /* delete a file */
2812 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2813 else if (STRCMP(flags, "d") == 0)
2814 /* delete an empty directory */
2815 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2816 else if (STRCMP(flags, "rf") == 0)
2817 /* delete a directory recursively */
2818 rettv->vval.v_number = delete_recursive(name);
2819 else
2820 EMSG2(_(e_invexpr2), flags);
2821}
2822
2823/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002824 * "deletebufline()" function
2825 */
2826 static void
2827f_deletebufline(argvars, rettv)
2828 typval_T *argvars;
2829 typval_T *rettv;
2830{
2831 buf_T *buf;
2832 linenr_T first, last;
2833 linenr_T lnum;
2834 long count;
2835 int is_curbuf;
2836 buf_T *curbuf_save = NULL;
2837 win_T *curwin_save = NULL;
2838 tabpage_T *tp;
2839 win_T *wp;
2840
2841 buf = get_buf_tv(&argvars[0], FALSE);
2842 if (buf == NULL)
2843 {
2844 rettv->vval.v_number = 1; /* FAIL */
2845 return;
2846 }
2847 is_curbuf = buf == curbuf;
2848
2849 first = get_tv_lnum_buf(&argvars[1], buf);
2850 if (argvars[2].v_type != VAR_UNKNOWN)
2851 last = get_tv_lnum_buf(&argvars[2], buf);
2852 else
2853 last = first;
2854
2855 if (buf->b_ml.ml_mfp == NULL || first < 1
2856 || first > buf->b_ml.ml_line_count || last < first)
2857 {
2858 rettv->vval.v_number = 1; /* FAIL */
2859 return;
2860 }
2861
2862 if (!is_curbuf)
2863 {
2864 curbuf_save = curbuf;
2865 curwin_save = curwin;
2866 curbuf = buf;
2867 find_win_for_curbuf();
2868 }
2869 if (last > curbuf->b_ml.ml_line_count)
2870 last = curbuf->b_ml.ml_line_count;
2871 count = last - first + 1;
2872
2873 // When coming here from Insert mode, sync undo, so that this can be
2874 // undone separately from what was previously inserted.
2875 if (u_sync_once == 2)
2876 {
2877 u_sync_once = 1; // notify that u_sync() was called
2878 u_sync(TRUE);
2879 }
2880
2881 if (u_save(first - 1, last + 1) == FAIL)
2882 {
2883 rettv->vval.v_number = 1; /* FAIL */
2884 return;
2885 }
2886
2887 for (lnum = first; lnum <= last; ++lnum)
2888 ml_delete(first, TRUE);
2889
2890 FOR_ALL_TAB_WINDOWS(tp, wp)
2891 if (wp->w_buffer == buf)
2892 {
2893 if (wp->w_cursor.lnum > last)
2894 wp->w_cursor.lnum -= count;
2895 else if (wp->w_cursor.lnum> first)
2896 wp->w_cursor.lnum = first;
2897 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2898 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2899 }
2900 check_cursor_col();
2901 deleted_lines_mark(first, count);
2902
2903 if (!is_curbuf)
2904 {
2905 curbuf = curbuf_save;
2906 curwin = curwin_save;
2907 }
2908}
2909
2910/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002911 * "did_filetype()" function
2912 */
2913 static void
2914f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2915{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002916 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002917}
2918
2919/*
2920 * "diff_filler()" function
2921 */
2922 static void
2923f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2924{
2925#ifdef FEAT_DIFF
2926 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2927#endif
2928}
2929
2930/*
2931 * "diff_hlID()" function
2932 */
2933 static void
2934f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2935{
2936#ifdef FEAT_DIFF
2937 linenr_T lnum = get_tv_lnum(argvars);
2938 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002939 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002940 static int fnum = 0;
2941 static int change_start = 0;
2942 static int change_end = 0;
2943 static hlf_T hlID = (hlf_T)0;
2944 int filler_lines;
2945 int col;
2946
2947 if (lnum < 0) /* ignore type error in {lnum} arg */
2948 lnum = 0;
2949 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002950 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002951 || fnum != curbuf->b_fnum)
2952 {
2953 /* New line, buffer, change: need to get the values. */
2954 filler_lines = diff_check(curwin, lnum);
2955 if (filler_lines < 0)
2956 {
2957 if (filler_lines == -1)
2958 {
2959 change_start = MAXCOL;
2960 change_end = -1;
2961 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2962 hlID = HLF_ADD; /* added line */
2963 else
2964 hlID = HLF_CHD; /* changed line */
2965 }
2966 else
2967 hlID = HLF_ADD; /* added line */
2968 }
2969 else
2970 hlID = (hlf_T)0;
2971 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002972 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002973 fnum = curbuf->b_fnum;
2974 }
2975
2976 if (hlID == HLF_CHD || hlID == HLF_TXD)
2977 {
2978 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2979 if (col >= change_start && col <= change_end)
2980 hlID = HLF_TXD; /* changed text */
2981 else
2982 hlID = HLF_CHD; /* changed line */
2983 }
2984 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2985#endif
2986}
2987
2988/*
2989 * "empty({expr})" function
2990 */
2991 static void
2992f_empty(typval_T *argvars, typval_T *rettv)
2993{
2994 int n = FALSE;
2995
2996 switch (argvars[0].v_type)
2997 {
2998 case VAR_STRING:
2999 case VAR_FUNC:
3000 n = argvars[0].vval.v_string == NULL
3001 || *argvars[0].vval.v_string == NUL;
3002 break;
3003 case VAR_PARTIAL:
3004 n = FALSE;
3005 break;
3006 case VAR_NUMBER:
3007 n = argvars[0].vval.v_number == 0;
3008 break;
3009 case VAR_FLOAT:
3010#ifdef FEAT_FLOAT
3011 n = argvars[0].vval.v_float == 0.0;
3012 break;
3013#endif
3014 case VAR_LIST:
3015 n = argvars[0].vval.v_list == NULL
3016 || argvars[0].vval.v_list->lv_first == NULL;
3017 break;
3018 case VAR_DICT:
3019 n = argvars[0].vval.v_dict == NULL
3020 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3021 break;
3022 case VAR_SPECIAL:
3023 n = argvars[0].vval.v_number != VVAL_TRUE;
3024 break;
3025
3026 case VAR_JOB:
3027#ifdef FEAT_JOB_CHANNEL
3028 n = argvars[0].vval.v_job == NULL
3029 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3030 break;
3031#endif
3032 case VAR_CHANNEL:
3033#ifdef FEAT_JOB_CHANNEL
3034 n = argvars[0].vval.v_channel == NULL
3035 || !channel_is_open(argvars[0].vval.v_channel);
3036 break;
3037#endif
3038 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003039 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003040 n = TRUE;
3041 break;
3042 }
3043
3044 rettv->vval.v_number = n;
3045}
3046
3047/*
3048 * "escape({string}, {chars})" function
3049 */
3050 static void
3051f_escape(typval_T *argvars, typval_T *rettv)
3052{
3053 char_u buf[NUMBUFLEN];
3054
3055 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
3056 get_tv_string_buf(&argvars[1], buf));
3057 rettv->v_type = VAR_STRING;
3058}
3059
3060/*
3061 * "eval()" function
3062 */
3063 static void
3064f_eval(typval_T *argvars, typval_T *rettv)
3065{
3066 char_u *s, *p;
3067
3068 s = get_tv_string_chk(&argvars[0]);
3069 if (s != NULL)
3070 s = skipwhite(s);
3071
3072 p = s;
3073 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3074 {
3075 if (p != NULL && !aborting())
3076 EMSG2(_(e_invexpr2), p);
3077 need_clr_eos = FALSE;
3078 rettv->v_type = VAR_NUMBER;
3079 rettv->vval.v_number = 0;
3080 }
3081 else if (*s != NUL)
3082 EMSG(_(e_trailing));
3083}
3084
3085/*
3086 * "eventhandler()" function
3087 */
3088 static void
3089f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3090{
3091 rettv->vval.v_number = vgetc_busy;
3092}
3093
3094/*
3095 * "executable()" function
3096 */
3097 static void
3098f_executable(typval_T *argvars, typval_T *rettv)
3099{
3100 char_u *name = get_tv_string(&argvars[0]);
3101
3102 /* Check in $PATH and also check directly if there is a directory name. */
3103 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3104 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3105}
3106
3107static garray_T redir_execute_ga;
3108
3109/*
3110 * Append "value[value_len]" to the execute() output.
3111 */
3112 void
3113execute_redir_str(char_u *value, int value_len)
3114{
3115 int len;
3116
3117 if (value_len == -1)
3118 len = (int)STRLEN(value); /* Append the entire string */
3119 else
3120 len = value_len; /* Append only "value_len" characters */
3121 if (ga_grow(&redir_execute_ga, len) == OK)
3122 {
3123 mch_memmove((char *)redir_execute_ga.ga_data
3124 + redir_execute_ga.ga_len, value, len);
3125 redir_execute_ga.ga_len += len;
3126 }
3127}
3128
3129/*
3130 * Get next line from a list.
3131 * Called by do_cmdline() to get the next line.
3132 * Returns allocated string, or NULL for end of function.
3133 */
3134
3135 static char_u *
3136get_list_line(
3137 int c UNUSED,
3138 void *cookie,
3139 int indent UNUSED)
3140{
3141 listitem_T **p = (listitem_T **)cookie;
3142 listitem_T *item = *p;
3143 char_u buf[NUMBUFLEN];
3144 char_u *s;
3145
3146 if (item == NULL)
3147 return NULL;
3148 s = get_tv_string_buf_chk(&item->li_tv, buf);
3149 *p = item->li_next;
3150 return s == NULL ? NULL : vim_strsave(s);
3151}
3152
3153/*
3154 * "execute()" function
3155 */
3156 static void
3157f_execute(typval_T *argvars, typval_T *rettv)
3158{
3159 char_u *cmd = NULL;
3160 list_T *list = NULL;
3161 int save_msg_silent = msg_silent;
3162 int save_emsg_silent = emsg_silent;
3163 int save_emsg_noredir = emsg_noredir;
3164 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003165 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003166 garray_T save_ga;
3167
3168 rettv->vval.v_string = NULL;
3169 rettv->v_type = VAR_STRING;
3170
3171 if (argvars[0].v_type == VAR_LIST)
3172 {
3173 list = argvars[0].vval.v_list;
3174 if (list == NULL || list->lv_first == NULL)
3175 /* empty list, no commands, empty output */
3176 return;
3177 ++list->lv_refcount;
3178 }
3179 else
3180 {
3181 cmd = get_tv_string_chk(&argvars[0]);
3182 if (cmd == NULL)
3183 return;
3184 }
3185
3186 if (argvars[1].v_type != VAR_UNKNOWN)
3187 {
3188 char_u buf[NUMBUFLEN];
3189 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
3190
3191 if (s == NULL)
3192 return;
3193 if (STRNCMP(s, "silent", 6) == 0)
3194 ++msg_silent;
3195 if (STRCMP(s, "silent!") == 0)
3196 {
3197 emsg_silent = TRUE;
3198 emsg_noredir = TRUE;
3199 }
3200 }
3201 else
3202 ++msg_silent;
3203
3204 if (redir_execute)
3205 save_ga = redir_execute_ga;
3206 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3207 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003208 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003209
3210 if (cmd != NULL)
3211 do_cmdline_cmd(cmd);
3212 else
3213 {
3214 listitem_T *item = list->lv_first;
3215
3216 do_cmdline(NULL, get_list_line, (void *)&item,
3217 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3218 --list->lv_refcount;
3219 }
3220
Bram Moolenaard297f352017-01-29 20:31:21 +01003221 /* Need to append a NUL to the result. */
3222 if (ga_grow(&redir_execute_ga, 1) == OK)
3223 {
3224 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3225 rettv->vval.v_string = redir_execute_ga.ga_data;
3226 }
3227 else
3228 {
3229 ga_clear(&redir_execute_ga);
3230 rettv->vval.v_string = NULL;
3231 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003232 msg_silent = save_msg_silent;
3233 emsg_silent = save_emsg_silent;
3234 emsg_noredir = save_emsg_noredir;
3235
3236 redir_execute = save_redir_execute;
3237 if (redir_execute)
3238 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003239 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003240
3241 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3242 * line. Put it back in the first column. */
3243 msg_col = 0;
3244}
3245
3246/*
3247 * "exepath()" function
3248 */
3249 static void
3250f_exepath(typval_T *argvars, typval_T *rettv)
3251{
3252 char_u *p = NULL;
3253
3254 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3255 rettv->v_type = VAR_STRING;
3256 rettv->vval.v_string = p;
3257}
3258
3259/*
3260 * "exists()" function
3261 */
3262 static void
3263f_exists(typval_T *argvars, typval_T *rettv)
3264{
3265 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003266 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003267
3268 p = get_tv_string(&argvars[0]);
3269 if (*p == '$') /* environment variable */
3270 {
3271 /* first try "normal" environment variables (fast) */
3272 if (mch_getenv(p + 1) != NULL)
3273 n = TRUE;
3274 else
3275 {
3276 /* try expanding things like $VIM and ${HOME} */
3277 p = expand_env_save(p);
3278 if (p != NULL && *p != '$')
3279 n = TRUE;
3280 vim_free(p);
3281 }
3282 }
3283 else if (*p == '&' || *p == '+') /* option */
3284 {
3285 n = (get_option_tv(&p, NULL, TRUE) == OK);
3286 if (*skipwhite(p) != NUL)
3287 n = FALSE; /* trailing garbage */
3288 }
3289 else if (*p == '*') /* internal or user defined function */
3290 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003291 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003292 }
3293 else if (*p == ':')
3294 {
3295 n = cmd_exists(p + 1);
3296 }
3297 else if (*p == '#')
3298 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 if (p[1] == '#')
3300 n = autocmd_supported(p + 2);
3301 else
3302 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003303 }
3304 else /* internal variable */
3305 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003306 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003307 }
3308
3309 rettv->vval.v_number = n;
3310}
3311
3312#ifdef FEAT_FLOAT
3313/*
3314 * "exp()" function
3315 */
3316 static void
3317f_exp(typval_T *argvars, typval_T *rettv)
3318{
3319 float_T f = 0.0;
3320
3321 rettv->v_type = VAR_FLOAT;
3322 if (get_float_arg(argvars, &f) == OK)
3323 rettv->vval.v_float = exp(f);
3324 else
3325 rettv->vval.v_float = 0.0;
3326}
3327#endif
3328
3329/*
3330 * "expand()" function
3331 */
3332 static void
3333f_expand(typval_T *argvars, typval_T *rettv)
3334{
3335 char_u *s;
3336 int len;
3337 char_u *errormsg;
3338 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3339 expand_T xpc;
3340 int error = FALSE;
3341 char_u *result;
3342
3343 rettv->v_type = VAR_STRING;
3344 if (argvars[1].v_type != VAR_UNKNOWN
3345 && argvars[2].v_type != VAR_UNKNOWN
3346 && get_tv_number_chk(&argvars[2], &error)
3347 && !error)
3348 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003349 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003350 }
3351
3352 s = get_tv_string(&argvars[0]);
3353 if (*s == '%' || *s == '#' || *s == '<')
3354 {
3355 ++emsg_off;
3356 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3357 --emsg_off;
3358 if (rettv->v_type == VAR_LIST)
3359 {
3360 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3361 list_append_string(rettv->vval.v_list, result, -1);
3362 else
3363 vim_free(result);
3364 }
3365 else
3366 rettv->vval.v_string = result;
3367 }
3368 else
3369 {
3370 /* When the optional second argument is non-zero, don't remove matches
3371 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3372 if (argvars[1].v_type != VAR_UNKNOWN
3373 && get_tv_number_chk(&argvars[1], &error))
3374 options |= WILD_KEEP_ALL;
3375 if (!error)
3376 {
3377 ExpandInit(&xpc);
3378 xpc.xp_context = EXPAND_FILES;
3379 if (p_wic)
3380 options += WILD_ICASE;
3381 if (rettv->v_type == VAR_STRING)
3382 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3383 options, WILD_ALL);
3384 else if (rettv_list_alloc(rettv) != FAIL)
3385 {
3386 int i;
3387
3388 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3389 for (i = 0; i < xpc.xp_numfiles; i++)
3390 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3391 ExpandCleanup(&xpc);
3392 }
3393 }
3394 else
3395 rettv->vval.v_string = NULL;
3396 }
3397}
3398
3399/*
3400 * "extend(list, list [, idx])" function
3401 * "extend(dict, dict [, action])" function
3402 */
3403 static void
3404f_extend(typval_T *argvars, typval_T *rettv)
3405{
3406 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3407
3408 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3409 {
3410 list_T *l1, *l2;
3411 listitem_T *item;
3412 long before;
3413 int error = FALSE;
3414
3415 l1 = argvars[0].vval.v_list;
3416 l2 = argvars[1].vval.v_list;
3417 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3418 && l2 != NULL)
3419 {
3420 if (argvars[2].v_type != VAR_UNKNOWN)
3421 {
3422 before = (long)get_tv_number_chk(&argvars[2], &error);
3423 if (error)
3424 return; /* type error; errmsg already given */
3425
3426 if (before == l1->lv_len)
3427 item = NULL;
3428 else
3429 {
3430 item = list_find(l1, before);
3431 if (item == NULL)
3432 {
3433 EMSGN(_(e_listidx), before);
3434 return;
3435 }
3436 }
3437 }
3438 else
3439 item = NULL;
3440 list_extend(l1, l2, item);
3441
3442 copy_tv(&argvars[0], rettv);
3443 }
3444 }
3445 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3446 {
3447 dict_T *d1, *d2;
3448 char_u *action;
3449 int i;
3450
3451 d1 = argvars[0].vval.v_dict;
3452 d2 = argvars[1].vval.v_dict;
3453 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3454 && d2 != NULL)
3455 {
3456 /* Check the third argument. */
3457 if (argvars[2].v_type != VAR_UNKNOWN)
3458 {
3459 static char *(av[]) = {"keep", "force", "error"};
3460
3461 action = get_tv_string_chk(&argvars[2]);
3462 if (action == NULL)
3463 return; /* type error; errmsg already given */
3464 for (i = 0; i < 3; ++i)
3465 if (STRCMP(action, av[i]) == 0)
3466 break;
3467 if (i == 3)
3468 {
3469 EMSG2(_(e_invarg2), action);
3470 return;
3471 }
3472 }
3473 else
3474 action = (char_u *)"force";
3475
3476 dict_extend(d1, d2, action);
3477
3478 copy_tv(&argvars[0], rettv);
3479 }
3480 }
3481 else
3482 EMSG2(_(e_listdictarg), "extend()");
3483}
3484
3485/*
3486 * "feedkeys()" function
3487 */
3488 static void
3489f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3490{
3491 int remap = TRUE;
3492 int insert = FALSE;
3493 char_u *keys, *flags;
3494 char_u nbuf[NUMBUFLEN];
3495 int typed = FALSE;
3496 int execute = FALSE;
3497 int dangerous = FALSE;
3498 char_u *keys_esc;
3499
3500 /* This is not allowed in the sandbox. If the commands would still be
3501 * executed in the sandbox it would be OK, but it probably happens later,
3502 * when "sandbox" is no longer set. */
3503 if (check_secure())
3504 return;
3505
3506 keys = get_tv_string(&argvars[0]);
3507
3508 if (argvars[1].v_type != VAR_UNKNOWN)
3509 {
3510 flags = get_tv_string_buf(&argvars[1], nbuf);
3511 for ( ; *flags != NUL; ++flags)
3512 {
3513 switch (*flags)
3514 {
3515 case 'n': remap = FALSE; break;
3516 case 'm': remap = TRUE; break;
3517 case 't': typed = TRUE; break;
3518 case 'i': insert = TRUE; break;
3519 case 'x': execute = TRUE; break;
3520 case '!': dangerous = TRUE; break;
3521 }
3522 }
3523 }
3524
3525 if (*keys != NUL || execute)
3526 {
3527 /* Need to escape K_SPECIAL and CSI before putting the string in the
3528 * typeahead buffer. */
3529 keys_esc = vim_strsave_escape_csi(keys);
3530 if (keys_esc != NULL)
3531 {
3532 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3533 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3534 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003535 if (vgetc_busy
3536#ifdef FEAT_TIMERS
3537 || timer_busy
3538#endif
3539 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003540 typebuf_was_filled = TRUE;
3541 if (execute)
3542 {
3543 int save_msg_scroll = msg_scroll;
3544
3545 /* Avoid a 1 second delay when the keys start Insert mode. */
3546 msg_scroll = FALSE;
3547
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003548 if (!dangerous)
3549 ++ex_normal_busy;
Bram Moolenaar655a82a2018-05-08 22:01:07 +02003550 exec_normal(TRUE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003551 if (!dangerous)
3552 --ex_normal_busy;
3553
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003554 msg_scroll |= save_msg_scroll;
3555 }
3556 }
3557 }
3558}
3559
3560/*
3561 * "filereadable()" function
3562 */
3563 static void
3564f_filereadable(typval_T *argvars, typval_T *rettv)
3565{
3566 int fd;
3567 char_u *p;
3568 int n;
3569
3570#ifndef O_NONBLOCK
3571# define O_NONBLOCK 0
3572#endif
3573 p = get_tv_string(&argvars[0]);
3574 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3575 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3576 {
3577 n = TRUE;
3578 close(fd);
3579 }
3580 else
3581 n = FALSE;
3582
3583 rettv->vval.v_number = n;
3584}
3585
3586/*
3587 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3588 * rights to write into.
3589 */
3590 static void
3591f_filewritable(typval_T *argvars, typval_T *rettv)
3592{
3593 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3594}
3595
3596 static void
3597findfilendir(
3598 typval_T *argvars UNUSED,
3599 typval_T *rettv,
3600 int find_what UNUSED)
3601{
3602#ifdef FEAT_SEARCHPATH
3603 char_u *fname;
3604 char_u *fresult = NULL;
3605 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3606 char_u *p;
3607 char_u pathbuf[NUMBUFLEN];
3608 int count = 1;
3609 int first = TRUE;
3610 int error = FALSE;
3611#endif
3612
3613 rettv->vval.v_string = NULL;
3614 rettv->v_type = VAR_STRING;
3615
3616#ifdef FEAT_SEARCHPATH
3617 fname = get_tv_string(&argvars[0]);
3618
3619 if (argvars[1].v_type != VAR_UNKNOWN)
3620 {
3621 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3622 if (p == NULL)
3623 error = TRUE;
3624 else
3625 {
3626 if (*p != NUL)
3627 path = p;
3628
3629 if (argvars[2].v_type != VAR_UNKNOWN)
3630 count = (int)get_tv_number_chk(&argvars[2], &error);
3631 }
3632 }
3633
3634 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3635 error = TRUE;
3636
3637 if (*fname != NUL && !error)
3638 {
3639 do
3640 {
3641 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3642 vim_free(fresult);
3643 fresult = find_file_in_path_option(first ? fname : NULL,
3644 first ? (int)STRLEN(fname) : 0,
3645 0, first, path,
3646 find_what,
3647 curbuf->b_ffname,
3648 find_what == FINDFILE_DIR
3649 ? (char_u *)"" : curbuf->b_p_sua);
3650 first = FALSE;
3651
3652 if (fresult != NULL && rettv->v_type == VAR_LIST)
3653 list_append_string(rettv->vval.v_list, fresult, -1);
3654
3655 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3656 }
3657
3658 if (rettv->v_type == VAR_STRING)
3659 rettv->vval.v_string = fresult;
3660#endif
3661}
3662
3663/*
3664 * "filter()" function
3665 */
3666 static void
3667f_filter(typval_T *argvars, typval_T *rettv)
3668{
3669 filter_map(argvars, rettv, FALSE);
3670}
3671
3672/*
3673 * "finddir({fname}[, {path}[, {count}]])" function
3674 */
3675 static void
3676f_finddir(typval_T *argvars, typval_T *rettv)
3677{
3678 findfilendir(argvars, rettv, FINDFILE_DIR);
3679}
3680
3681/*
3682 * "findfile({fname}[, {path}[, {count}]])" function
3683 */
3684 static void
3685f_findfile(typval_T *argvars, typval_T *rettv)
3686{
3687 findfilendir(argvars, rettv, FINDFILE_FILE);
3688}
3689
3690#ifdef FEAT_FLOAT
3691/*
3692 * "float2nr({float})" function
3693 */
3694 static void
3695f_float2nr(typval_T *argvars, typval_T *rettv)
3696{
3697 float_T f = 0.0;
3698
3699 if (get_float_arg(argvars, &f) == OK)
3700 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003701 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003702 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003703 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003704 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003705 else
3706 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003707 }
3708}
3709
3710/*
3711 * "floor({float})" function
3712 */
3713 static void
3714f_floor(typval_T *argvars, typval_T *rettv)
3715{
3716 float_T f = 0.0;
3717
3718 rettv->v_type = VAR_FLOAT;
3719 if (get_float_arg(argvars, &f) == OK)
3720 rettv->vval.v_float = floor(f);
3721 else
3722 rettv->vval.v_float = 0.0;
3723}
3724
3725/*
3726 * "fmod()" function
3727 */
3728 static void
3729f_fmod(typval_T *argvars, typval_T *rettv)
3730{
3731 float_T fx = 0.0, fy = 0.0;
3732
3733 rettv->v_type = VAR_FLOAT;
3734 if (get_float_arg(argvars, &fx) == OK
3735 && get_float_arg(&argvars[1], &fy) == OK)
3736 rettv->vval.v_float = fmod(fx, fy);
3737 else
3738 rettv->vval.v_float = 0.0;
3739}
3740#endif
3741
3742/*
3743 * "fnameescape({string})" function
3744 */
3745 static void
3746f_fnameescape(typval_T *argvars, typval_T *rettv)
3747{
3748 rettv->vval.v_string = vim_strsave_fnameescape(
3749 get_tv_string(&argvars[0]), FALSE);
3750 rettv->v_type = VAR_STRING;
3751}
3752
3753/*
3754 * "fnamemodify({fname}, {mods})" function
3755 */
3756 static void
3757f_fnamemodify(typval_T *argvars, typval_T *rettv)
3758{
3759 char_u *fname;
3760 char_u *mods;
3761 int usedlen = 0;
3762 int len;
3763 char_u *fbuf = NULL;
3764 char_u buf[NUMBUFLEN];
3765
3766 fname = get_tv_string_chk(&argvars[0]);
3767 mods = get_tv_string_buf_chk(&argvars[1], buf);
3768 if (fname == NULL || mods == NULL)
3769 fname = NULL;
3770 else
3771 {
3772 len = (int)STRLEN(fname);
3773 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3774 }
3775
3776 rettv->v_type = VAR_STRING;
3777 if (fname == NULL)
3778 rettv->vval.v_string = NULL;
3779 else
3780 rettv->vval.v_string = vim_strnsave(fname, len);
3781 vim_free(fbuf);
3782}
3783
3784static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3785
3786/*
3787 * "foldclosed()" function
3788 */
3789 static void
3790foldclosed_both(
3791 typval_T *argvars UNUSED,
3792 typval_T *rettv,
3793 int end UNUSED)
3794{
3795#ifdef FEAT_FOLDING
3796 linenr_T lnum;
3797 linenr_T first, last;
3798
3799 lnum = get_tv_lnum(argvars);
3800 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3801 {
3802 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3803 {
3804 if (end)
3805 rettv->vval.v_number = (varnumber_T)last;
3806 else
3807 rettv->vval.v_number = (varnumber_T)first;
3808 return;
3809 }
3810 }
3811#endif
3812 rettv->vval.v_number = -1;
3813}
3814
3815/*
3816 * "foldclosed()" function
3817 */
3818 static void
3819f_foldclosed(typval_T *argvars, typval_T *rettv)
3820{
3821 foldclosed_both(argvars, rettv, FALSE);
3822}
3823
3824/*
3825 * "foldclosedend()" function
3826 */
3827 static void
3828f_foldclosedend(typval_T *argvars, typval_T *rettv)
3829{
3830 foldclosed_both(argvars, rettv, TRUE);
3831}
3832
3833/*
3834 * "foldlevel()" function
3835 */
3836 static void
3837f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3838{
3839#ifdef FEAT_FOLDING
3840 linenr_T lnum;
3841
3842 lnum = get_tv_lnum(argvars);
3843 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3844 rettv->vval.v_number = foldLevel(lnum);
3845#endif
3846}
3847
3848/*
3849 * "foldtext()" function
3850 */
3851 static void
3852f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3853{
3854#ifdef FEAT_FOLDING
3855 linenr_T foldstart;
3856 linenr_T foldend;
3857 char_u *dashes;
3858 linenr_T lnum;
3859 char_u *s;
3860 char_u *r;
3861 int len;
3862 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003863 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003864#endif
3865
3866 rettv->v_type = VAR_STRING;
3867 rettv->vval.v_string = NULL;
3868#ifdef FEAT_FOLDING
3869 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3870 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3871 dashes = get_vim_var_str(VV_FOLDDASHES);
3872 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3873 && dashes != NULL)
3874 {
3875 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003876 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003877 if (!linewhite(lnum))
3878 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003879
3880 /* Find interesting text in this line. */
3881 s = skipwhite(ml_get(lnum));
3882 /* skip C comment-start */
3883 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3884 {
3885 s = skipwhite(s + 2);
3886 if (*skipwhite(s) == NUL
3887 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3888 {
3889 s = skipwhite(ml_get(lnum + 1));
3890 if (*s == '*')
3891 s = skipwhite(s + 1);
3892 }
3893 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003894 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003895 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003896 r = alloc((unsigned)(STRLEN(txt)
3897 + STRLEN(dashes) /* for %s */
3898 + 20 /* for %3ld */
3899 + STRLEN(s))); /* concatenated */
3900 if (r != NULL)
3901 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003902 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003903 len = (int)STRLEN(r);
3904 STRCAT(r, s);
3905 /* remove 'foldmarker' and 'commentstring' */
3906 foldtext_cleanup(r + len);
3907 rettv->vval.v_string = r;
3908 }
3909 }
3910#endif
3911}
3912
3913/*
3914 * "foldtextresult(lnum)" function
3915 */
3916 static void
3917f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3918{
3919#ifdef FEAT_FOLDING
3920 linenr_T lnum;
3921 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003922 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003923 foldinfo_T foldinfo;
3924 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003925 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003926#endif
3927
3928 rettv->v_type = VAR_STRING;
3929 rettv->vval.v_string = NULL;
3930#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003931 if (entered)
3932 return; /* reject recursive use */
3933 entered = TRUE;
3934
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003935 lnum = get_tv_lnum(argvars);
3936 /* treat illegal types and illegal string values for {lnum} the same */
3937 if (lnum < 0)
3938 lnum = 0;
3939 fold_count = foldedCount(curwin, lnum, &foldinfo);
3940 if (fold_count > 0)
3941 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003942 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3943 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003944 if (text == buf)
3945 text = vim_strsave(text);
3946 rettv->vval.v_string = text;
3947 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003948
3949 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003950#endif
3951}
3952
3953/*
3954 * "foreground()" function
3955 */
3956 static void
3957f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3958{
3959#ifdef FEAT_GUI
3960 if (gui.in_use)
3961 gui_mch_set_foreground();
3962#else
3963# ifdef WIN32
3964 win32_set_foreground();
3965# endif
3966#endif
3967}
3968
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003969 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003970common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003971{
3972 char_u *s;
3973 char_u *name;
3974 int use_string = FALSE;
3975 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003976 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003977
3978 if (argvars[0].v_type == VAR_FUNC)
3979 {
3980 /* function(MyFunc, [arg], dict) */
3981 s = argvars[0].vval.v_string;
3982 }
3983 else if (argvars[0].v_type == VAR_PARTIAL
3984 && argvars[0].vval.v_partial != NULL)
3985 {
3986 /* function(dict.MyFunc, [arg]) */
3987 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003988 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003989 }
3990 else
3991 {
3992 /* function('MyFunc', [arg], dict) */
3993 s = get_tv_string(&argvars[0]);
3994 use_string = TRUE;
3995 }
3996
Bram Moolenaar843b8842016-08-21 14:36:15 +02003997 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003998 {
3999 name = s;
4000 trans_name = trans_function_name(&name, FALSE,
4001 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4002 if (*name != NUL)
4003 s = NULL;
4004 }
4005
Bram Moolenaar843b8842016-08-21 14:36:15 +02004006 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4007 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02004008 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004009 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004010 else if (trans_name != NULL && (is_funcref
4011 ? find_func(trans_name) == NULL
4012 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004013 EMSG2(_("E700: Unknown function: %s"), s);
4014 else
4015 {
4016 int dict_idx = 0;
4017 int arg_idx = 0;
4018 list_T *list = NULL;
4019
4020 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4021 {
4022 char sid_buf[25];
4023 int off = *s == 's' ? 2 : 5;
4024
4025 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4026 * also be called from another script. Using trans_function_name()
4027 * would also work, but some plugins depend on the name being
4028 * printable text. */
4029 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
4030 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4031 if (name != NULL)
4032 {
4033 STRCPY(name, sid_buf);
4034 STRCAT(name, s + off);
4035 }
4036 }
4037 else
4038 name = vim_strsave(s);
4039
4040 if (argvars[1].v_type != VAR_UNKNOWN)
4041 {
4042 if (argvars[2].v_type != VAR_UNKNOWN)
4043 {
4044 /* function(name, [args], dict) */
4045 arg_idx = 1;
4046 dict_idx = 2;
4047 }
4048 else if (argvars[1].v_type == VAR_DICT)
4049 /* function(name, dict) */
4050 dict_idx = 1;
4051 else
4052 /* function(name, [args]) */
4053 arg_idx = 1;
4054 if (dict_idx > 0)
4055 {
4056 if (argvars[dict_idx].v_type != VAR_DICT)
4057 {
4058 EMSG(_("E922: expected a dict"));
4059 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004060 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004061 }
4062 if (argvars[dict_idx].vval.v_dict == NULL)
4063 dict_idx = 0;
4064 }
4065 if (arg_idx > 0)
4066 {
4067 if (argvars[arg_idx].v_type != VAR_LIST)
4068 {
4069 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4070 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004071 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004072 }
4073 list = argvars[arg_idx].vval.v_list;
4074 if (list == NULL || list->lv_len == 0)
4075 arg_idx = 0;
4076 }
4077 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004078 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004079 {
4080 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4081
4082 /* result is a VAR_PARTIAL */
4083 if (pt == NULL)
4084 vim_free(name);
4085 else
4086 {
4087 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4088 {
4089 listitem_T *li;
4090 int i = 0;
4091 int arg_len = 0;
4092 int lv_len = 0;
4093
4094 if (arg_pt != NULL)
4095 arg_len = arg_pt->pt_argc;
4096 if (list != NULL)
4097 lv_len = list->lv_len;
4098 pt->pt_argc = arg_len + lv_len;
4099 pt->pt_argv = (typval_T *)alloc(
4100 sizeof(typval_T) * pt->pt_argc);
4101 if (pt->pt_argv == NULL)
4102 {
4103 vim_free(pt);
4104 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004105 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004106 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004107 for (i = 0; i < arg_len; i++)
4108 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4109 if (lv_len > 0)
4110 for (li = list->lv_first; li != NULL;
4111 li = li->li_next)
4112 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004113 }
4114
4115 /* For "function(dict.func, [], dict)" and "func" is a partial
4116 * use "dict". That is backwards compatible. */
4117 if (dict_idx > 0)
4118 {
4119 /* The dict is bound explicitly, pt_auto is FALSE. */
4120 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4121 ++pt->pt_dict->dv_refcount;
4122 }
4123 else if (arg_pt != NULL)
4124 {
4125 /* If the dict was bound automatically the result is also
4126 * bound automatically. */
4127 pt->pt_dict = arg_pt->pt_dict;
4128 pt->pt_auto = arg_pt->pt_auto;
4129 if (pt->pt_dict != NULL)
4130 ++pt->pt_dict->dv_refcount;
4131 }
4132
4133 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004134 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4135 {
4136 pt->pt_func = arg_pt->pt_func;
4137 func_ptr_ref(pt->pt_func);
4138 vim_free(name);
4139 }
4140 else if (is_funcref)
4141 {
4142 pt->pt_func = find_func(trans_name);
4143 func_ptr_ref(pt->pt_func);
4144 vim_free(name);
4145 }
4146 else
4147 {
4148 pt->pt_name = name;
4149 func_ref(name);
4150 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004151 }
4152 rettv->v_type = VAR_PARTIAL;
4153 rettv->vval.v_partial = pt;
4154 }
4155 else
4156 {
4157 /* result is a VAR_FUNC */
4158 rettv->v_type = VAR_FUNC;
4159 rettv->vval.v_string = name;
4160 func_ref(name);
4161 }
4162 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004163theend:
4164 vim_free(trans_name);
4165}
4166
4167/*
4168 * "funcref()" function
4169 */
4170 static void
4171f_funcref(typval_T *argvars, typval_T *rettv)
4172{
4173 common_function(argvars, rettv, TRUE);
4174}
4175
4176/*
4177 * "function()" function
4178 */
4179 static void
4180f_function(typval_T *argvars, typval_T *rettv)
4181{
4182 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004183}
4184
4185/*
4186 * "garbagecollect()" function
4187 */
4188 static void
4189f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4190{
4191 /* This is postponed until we are back at the toplevel, because we may be
4192 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4193 want_garbage_collect = TRUE;
4194
4195 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
4196 garbage_collect_at_exit = TRUE;
4197}
4198
4199/*
4200 * "get()" function
4201 */
4202 static void
4203f_get(typval_T *argvars, typval_T *rettv)
4204{
4205 listitem_T *li;
4206 list_T *l;
4207 dictitem_T *di;
4208 dict_T *d;
4209 typval_T *tv = NULL;
4210
4211 if (argvars[0].v_type == VAR_LIST)
4212 {
4213 if ((l = argvars[0].vval.v_list) != NULL)
4214 {
4215 int error = FALSE;
4216
4217 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
4218 if (!error && li != NULL)
4219 tv = &li->li_tv;
4220 }
4221 }
4222 else if (argvars[0].v_type == VAR_DICT)
4223 {
4224 if ((d = argvars[0].vval.v_dict) != NULL)
4225 {
4226 di = dict_find(d, get_tv_string(&argvars[1]), -1);
4227 if (di != NULL)
4228 tv = &di->di_tv;
4229 }
4230 }
4231 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4232 {
4233 partial_T *pt;
4234 partial_T fref_pt;
4235
4236 if (argvars[0].v_type == VAR_PARTIAL)
4237 pt = argvars[0].vval.v_partial;
4238 else
4239 {
4240 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4241 fref_pt.pt_name = argvars[0].vval.v_string;
4242 pt = &fref_pt;
4243 }
4244
4245 if (pt != NULL)
4246 {
4247 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004248 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004249
4250 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4251 {
4252 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004253 n = partial_name(pt);
4254 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004255 rettv->vval.v_string = NULL;
4256 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004257 {
4258 rettv->vval.v_string = vim_strsave(n);
4259 if (rettv->v_type == VAR_FUNC)
4260 func_ref(rettv->vval.v_string);
4261 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004262 }
4263 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004264 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004265 else if (STRCMP(what, "args") == 0)
4266 {
4267 rettv->v_type = VAR_LIST;
4268 if (rettv_list_alloc(rettv) == OK)
4269 {
4270 int i;
4271
4272 for (i = 0; i < pt->pt_argc; ++i)
4273 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4274 }
4275 }
4276 else
4277 EMSG2(_(e_invarg2), what);
4278 return;
4279 }
4280 }
4281 else
4282 EMSG2(_(e_listdictarg), "get()");
4283
4284 if (tv == NULL)
4285 {
4286 if (argvars[2].v_type != VAR_UNKNOWN)
4287 copy_tv(&argvars[2], rettv);
4288 }
4289 else
4290 copy_tv(tv, rettv);
4291}
4292
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004293#ifdef FEAT_SIGNS
4294/*
4295 * Returns information about signs placed in a buffer as list of dicts.
4296 */
4297 static void
4298get_buffer_signs(buf_T *buf, list_T *l)
4299{
4300 signlist_T *sign;
4301
4302 for (sign = buf->b_signlist; sign; sign = sign->next)
4303 {
4304 dict_T *d = dict_alloc();
4305
4306 if (d != NULL)
4307 {
4308 dict_add_nr_str(d, "id", sign->id, NULL);
4309 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004310 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004311
4312 list_append_dict(l, d);
4313 }
4314 }
4315}
4316#endif
4317
4318/*
4319 * Returns buffer options, variables and other attributes in a dictionary.
4320 */
4321 static dict_T *
4322get_buffer_info(buf_T *buf)
4323{
4324 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004325 tabpage_T *tp;
4326 win_T *wp;
4327 list_T *windows;
4328
4329 dict = dict_alloc();
4330 if (dict == NULL)
4331 return NULL;
4332
Bram Moolenaar33928832016-08-18 21:22:04 +02004333 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004334 dict_add_nr_str(dict, "name", 0L,
4335 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004336 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4337 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004338 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4339 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4340 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004341 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004342 dict_add_nr_str(dict, "hidden",
4343 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4344 NULL);
4345
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004346 /* Get a reference to buffer variables */
4347 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004348
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004349 /* List of windows displaying this buffer */
4350 windows = list_alloc();
4351 if (windows != NULL)
4352 {
4353 FOR_ALL_TAB_WINDOWS(tp, wp)
4354 if (wp->w_buffer == buf)
4355 list_append_number(windows, (varnumber_T)wp->w_id);
4356 dict_add_list(dict, "windows", windows);
4357 }
4358
4359#ifdef FEAT_SIGNS
4360 if (buf->b_signlist != NULL)
4361 {
4362 /* List of signs placed in this buffer */
4363 list_T *signs = list_alloc();
4364 if (signs != NULL)
4365 {
4366 get_buffer_signs(buf, signs);
4367 dict_add_list(dict, "signs", signs);
4368 }
4369 }
4370#endif
4371
4372 return dict;
4373}
4374
4375/*
4376 * "getbufinfo()" function
4377 */
4378 static void
4379f_getbufinfo(typval_T *argvars, typval_T *rettv)
4380{
4381 buf_T *buf = NULL;
4382 buf_T *argbuf = NULL;
4383 dict_T *d;
4384 int filtered = FALSE;
4385 int sel_buflisted = FALSE;
4386 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004387 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004388
4389 if (rettv_list_alloc(rettv) != OK)
4390 return;
4391
4392 /* List of all the buffers or selected buffers */
4393 if (argvars[0].v_type == VAR_DICT)
4394 {
4395 dict_T *sel_d = argvars[0].vval.v_dict;
4396
4397 if (sel_d != NULL)
4398 {
4399 dictitem_T *di;
4400
4401 filtered = TRUE;
4402
4403 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4404 if (di != NULL && get_tv_number(&di->di_tv))
4405 sel_buflisted = TRUE;
4406
4407 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4408 if (di != NULL && get_tv_number(&di->di_tv))
4409 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004410
4411 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4412 if (di != NULL && get_tv_number(&di->di_tv))
4413 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004414 }
4415 }
4416 else if (argvars[0].v_type != VAR_UNKNOWN)
4417 {
4418 /* Information about one buffer. Argument specifies the buffer */
4419 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4420 ++emsg_off;
4421 argbuf = get_buf_tv(&argvars[0], FALSE);
4422 --emsg_off;
4423 if (argbuf == NULL)
4424 return;
4425 }
4426
4427 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004428 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004429 {
4430 if (argbuf != NULL && argbuf != buf)
4431 continue;
4432 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004433 || (sel_buflisted && !buf->b_p_bl)
4434 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004435 continue;
4436
4437 d = get_buffer_info(buf);
4438 if (d != NULL)
4439 list_append_dict(rettv->vval.v_list, d);
4440 if (argbuf != NULL)
4441 return;
4442 }
4443}
4444
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004445static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4446
4447/*
4448 * Get line or list of lines from buffer "buf" into "rettv".
4449 * Return a range (from start to end) of lines in rettv from the specified
4450 * buffer.
4451 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4452 */
4453 static void
4454get_buffer_lines(
4455 buf_T *buf,
4456 linenr_T start,
4457 linenr_T end,
4458 int retlist,
4459 typval_T *rettv)
4460{
4461 char_u *p;
4462
4463 rettv->v_type = VAR_STRING;
4464 rettv->vval.v_string = NULL;
4465 if (retlist && rettv_list_alloc(rettv) == FAIL)
4466 return;
4467
4468 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4469 return;
4470
4471 if (!retlist)
4472 {
4473 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4474 p = ml_get_buf(buf, start, FALSE);
4475 else
4476 p = (char_u *)"";
4477 rettv->vval.v_string = vim_strsave(p);
4478 }
4479 else
4480 {
4481 if (end < start)
4482 return;
4483
4484 if (start < 1)
4485 start = 1;
4486 if (end > buf->b_ml.ml_line_count)
4487 end = buf->b_ml.ml_line_count;
4488 while (start <= end)
4489 if (list_append_string(rettv->vval.v_list,
4490 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4491 break;
4492 }
4493}
4494
4495/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004496 * "getbufline()" function
4497 */
4498 static void
4499f_getbufline(typval_T *argvars, typval_T *rettv)
4500{
4501 linenr_T lnum;
4502 linenr_T end;
4503 buf_T *buf;
4504
4505 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4506 ++emsg_off;
4507 buf = get_buf_tv(&argvars[0], FALSE);
4508 --emsg_off;
4509
4510 lnum = get_tv_lnum_buf(&argvars[1], buf);
4511 if (argvars[2].v_type == VAR_UNKNOWN)
4512 end = lnum;
4513 else
4514 end = get_tv_lnum_buf(&argvars[2], buf);
4515
4516 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4517}
4518
4519/*
4520 * "getbufvar()" function
4521 */
4522 static void
4523f_getbufvar(typval_T *argvars, typval_T *rettv)
4524{
4525 buf_T *buf;
4526 buf_T *save_curbuf;
4527 char_u *varname;
4528 dictitem_T *v;
4529 int done = FALSE;
4530
4531 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4532 varname = get_tv_string_chk(&argvars[1]);
4533 ++emsg_off;
4534 buf = get_buf_tv(&argvars[0], FALSE);
4535
4536 rettv->v_type = VAR_STRING;
4537 rettv->vval.v_string = NULL;
4538
4539 if (buf != NULL && varname != NULL)
4540 {
4541 /* set curbuf to be our buf, temporarily */
4542 save_curbuf = curbuf;
4543 curbuf = buf;
4544
Bram Moolenaar30567352016-08-27 21:25:44 +02004545 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004546 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004547 if (varname[1] == NUL)
4548 {
4549 /* get all buffer-local options in a dict */
4550 dict_T *opts = get_winbuf_options(TRUE);
4551
4552 if (opts != NULL)
4553 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004554 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004555 done = TRUE;
4556 }
4557 }
4558 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4559 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004560 done = TRUE;
4561 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004562 else
4563 {
4564 /* Look up the variable. */
4565 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4566 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4567 'b', varname, FALSE);
4568 if (v != NULL)
4569 {
4570 copy_tv(&v->di_tv, rettv);
4571 done = TRUE;
4572 }
4573 }
4574
4575 /* restore previous notion of curbuf */
4576 curbuf = save_curbuf;
4577 }
4578
4579 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4580 /* use the default value */
4581 copy_tv(&argvars[2], rettv);
4582
4583 --emsg_off;
4584}
4585
4586/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004587 * "getchangelist()" function
4588 */
4589 static void
4590f_getchangelist(typval_T *argvars, typval_T *rettv)
4591{
4592#ifdef FEAT_JUMPLIST
4593 buf_T *buf;
4594 int i;
4595 list_T *l;
4596 dict_T *d;
4597#endif
4598
4599 if (rettv_list_alloc(rettv) != OK)
4600 return;
4601
4602#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004603 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4604 ++emsg_off;
4605 buf = get_buf_tv(&argvars[0], FALSE);
4606 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004607 if (buf == NULL)
4608 return;
4609
4610 l = list_alloc();
4611 if (l == NULL)
4612 return;
4613
4614 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4615 return;
4616 /*
4617 * The current window change list index tracks only the position in the
4618 * current buffer change list. For other buffers, use the change list
4619 * length as the current index.
4620 */
4621 list_append_number(rettv->vval.v_list,
4622 (varnumber_T)((buf == curwin->w_buffer)
4623 ? curwin->w_changelistidx : buf->b_changelistlen));
4624
4625 for (i = 0; i < buf->b_changelistlen; ++i)
4626 {
4627 if (buf->b_changelist[i].lnum == 0)
4628 continue;
4629 if ((d = dict_alloc()) == NULL)
4630 return;
4631 if (list_append_dict(l, d) == FAIL)
4632 return;
4633 dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
4634 dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
4635# ifdef FEAT_VIRTUALEDIT
4636 dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
4637# endif
4638 }
4639#endif
4640}
4641/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004642 * "getchar()" function
4643 */
4644 static void
4645f_getchar(typval_T *argvars, typval_T *rettv)
4646{
4647 varnumber_T n;
4648 int error = FALSE;
4649
4650 /* Position the cursor. Needed after a message that ends in a space. */
4651 windgoto(msg_row, msg_col);
4652
4653 ++no_mapping;
4654 ++allow_keys;
4655 for (;;)
4656 {
4657 if (argvars[0].v_type == VAR_UNKNOWN)
4658 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004659 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004660 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4661 /* getchar(1): only check if char avail */
4662 n = vpeekc_any();
4663 else if (error || vpeekc_any() == NUL)
4664 /* illegal argument or getchar(0) and no char avail: return zero */
4665 n = 0;
4666 else
4667 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004668 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004669
4670 if (n == K_IGNORE)
4671 continue;
4672 break;
4673 }
4674 --no_mapping;
4675 --allow_keys;
4676
4677 set_vim_var_nr(VV_MOUSE_WIN, 0);
4678 set_vim_var_nr(VV_MOUSE_WINID, 0);
4679 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4680 set_vim_var_nr(VV_MOUSE_COL, 0);
4681
4682 rettv->vval.v_number = n;
4683 if (IS_SPECIAL(n) || mod_mask != 0)
4684 {
4685 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4686 int i = 0;
4687
4688 /* Turn a special key into three bytes, plus modifier. */
4689 if (mod_mask != 0)
4690 {
4691 temp[i++] = K_SPECIAL;
4692 temp[i++] = KS_MODIFIER;
4693 temp[i++] = mod_mask;
4694 }
4695 if (IS_SPECIAL(n))
4696 {
4697 temp[i++] = K_SPECIAL;
4698 temp[i++] = K_SECOND(n);
4699 temp[i++] = K_THIRD(n);
4700 }
4701#ifdef FEAT_MBYTE
4702 else if (has_mbyte)
4703 i += (*mb_char2bytes)(n, temp + i);
4704#endif
4705 else
4706 temp[i++] = n;
4707 temp[i++] = NUL;
4708 rettv->v_type = VAR_STRING;
4709 rettv->vval.v_string = vim_strsave(temp);
4710
4711#ifdef FEAT_MOUSE
4712 if (is_mouse_key(n))
4713 {
4714 int row = mouse_row;
4715 int col = mouse_col;
4716 win_T *win;
4717 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004718 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004719 int winnr = 1;
4720
4721 if (row >= 0 && col >= 0)
4722 {
4723 /* Find the window at the mouse coordinates and compute the
4724 * text position. */
4725 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004726 if (win == NULL)
4727 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004728 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004729 for (wp = firstwin; wp != win; wp = wp->w_next)
4730 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004731 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4732 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4733 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4734 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4735 }
4736 }
4737#endif
4738 }
4739}
4740
4741/*
4742 * "getcharmod()" function
4743 */
4744 static void
4745f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4746{
4747 rettv->vval.v_number = mod_mask;
4748}
4749
4750/*
4751 * "getcharsearch()" function
4752 */
4753 static void
4754f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4755{
4756 if (rettv_dict_alloc(rettv) != FAIL)
4757 {
4758 dict_T *dict = rettv->vval.v_dict;
4759
4760 dict_add_nr_str(dict, "char", 0L, last_csearch());
4761 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4762 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4763 }
4764}
4765
4766/*
4767 * "getcmdline()" function
4768 */
4769 static void
4770f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4771{
4772 rettv->v_type = VAR_STRING;
4773 rettv->vval.v_string = get_cmdline_str();
4774}
4775
4776/*
4777 * "getcmdpos()" function
4778 */
4779 static void
4780f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4781{
4782 rettv->vval.v_number = get_cmdline_pos() + 1;
4783}
4784
4785/*
4786 * "getcmdtype()" function
4787 */
4788 static void
4789f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4790{
4791 rettv->v_type = VAR_STRING;
4792 rettv->vval.v_string = alloc(2);
4793 if (rettv->vval.v_string != NULL)
4794 {
4795 rettv->vval.v_string[0] = get_cmdline_type();
4796 rettv->vval.v_string[1] = NUL;
4797 }
4798}
4799
4800/*
4801 * "getcmdwintype()" function
4802 */
4803 static void
4804f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4805{
4806 rettv->v_type = VAR_STRING;
4807 rettv->vval.v_string = NULL;
4808#ifdef FEAT_CMDWIN
4809 rettv->vval.v_string = alloc(2);
4810 if (rettv->vval.v_string != NULL)
4811 {
4812 rettv->vval.v_string[0] = cmdwin_type;
4813 rettv->vval.v_string[1] = NUL;
4814 }
4815#endif
4816}
4817
4818#if defined(FEAT_CMDL_COMPL)
4819/*
4820 * "getcompletion()" function
4821 */
4822 static void
4823f_getcompletion(typval_T *argvars, typval_T *rettv)
4824{
4825 char_u *pat;
4826 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004827 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004828 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4829 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004830
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004831 if (argvars[2].v_type != VAR_UNKNOWN)
4832 filtered = get_tv_number_chk(&argvars[2], NULL);
4833
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004834 if (p_wic)
4835 options |= WILD_ICASE;
4836
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004837 /* For filtered results, 'wildignore' is used */
4838 if (!filtered)
4839 options |= WILD_KEEP_ALL;
4840
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004841 ExpandInit(&xpc);
4842 xpc.xp_pattern = get_tv_string(&argvars[0]);
4843 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4844 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4845 if (xpc.xp_context == EXPAND_NOTHING)
4846 {
4847 if (argvars[1].v_type == VAR_STRING)
4848 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4849 else
4850 EMSG(_(e_invarg));
4851 return;
4852 }
4853
4854# if defined(FEAT_MENU)
4855 if (xpc.xp_context == EXPAND_MENUS)
4856 {
4857 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4858 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4859 }
4860# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004861#ifdef FEAT_CSCOPE
4862 if (xpc.xp_context == EXPAND_CSCOPE)
4863 {
4864 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4865 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4866 }
4867#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004868#ifdef FEAT_SIGNS
4869 if (xpc.xp_context == EXPAND_SIGN)
4870 {
4871 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4872 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4873 }
4874#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004875
4876 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4877 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4878 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004879 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004880
4881 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4882
4883 for (i = 0; i < xpc.xp_numfiles; i++)
4884 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4885 }
4886 vim_free(pat);
4887 ExpandCleanup(&xpc);
4888}
4889#endif
4890
4891/*
4892 * "getcwd()" function
4893 */
4894 static void
4895f_getcwd(typval_T *argvars, typval_T *rettv)
4896{
4897 win_T *wp = NULL;
4898 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004899 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004900
4901 rettv->v_type = VAR_STRING;
4902 rettv->vval.v_string = NULL;
4903
Bram Moolenaar54591292018-02-09 20:53:59 +01004904 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4905 global = TRUE;
4906 else
4907 wp = find_tabwin(&argvars[0], &argvars[1]);
4908
4909 if (wp != NULL && wp->w_localdir != NULL)
4910 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4911 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004912 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004913 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004914 rettv->vval.v_string = vim_strsave(globaldir);
4915 else
4916 {
4917 cwd = alloc(MAXPATHL);
4918 if (cwd != NULL)
4919 {
4920 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4921 rettv->vval.v_string = vim_strsave(cwd);
4922 vim_free(cwd);
4923 }
4924 }
4925#ifdef BACKSLASH_IN_FILENAME
4926 if (rettv->vval.v_string != NULL)
4927 slash_adjust(rettv->vval.v_string);
4928#endif
4929 }
4930}
4931
4932/*
4933 * "getfontname()" function
4934 */
4935 static void
4936f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4937{
4938 rettv->v_type = VAR_STRING;
4939 rettv->vval.v_string = NULL;
4940#ifdef FEAT_GUI
4941 if (gui.in_use)
4942 {
4943 GuiFont font;
4944 char_u *name = NULL;
4945
4946 if (argvars[0].v_type == VAR_UNKNOWN)
4947 {
4948 /* Get the "Normal" font. Either the name saved by
4949 * hl_set_font_name() or from the font ID. */
4950 font = gui.norm_font;
4951 name = hl_get_font_name();
4952 }
4953 else
4954 {
4955 name = get_tv_string(&argvars[0]);
4956 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4957 return;
4958 font = gui_mch_get_font(name, FALSE);
4959 if (font == NOFONT)
4960 return; /* Invalid font name, return empty string. */
4961 }
4962 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4963 if (argvars[0].v_type != VAR_UNKNOWN)
4964 gui_mch_free_font(font);
4965 }
4966#endif
4967}
4968
4969/*
4970 * "getfperm({fname})" function
4971 */
4972 static void
4973f_getfperm(typval_T *argvars, typval_T *rettv)
4974{
4975 char_u *fname;
4976 stat_T st;
4977 char_u *perm = NULL;
4978 char_u flags[] = "rwx";
4979 int i;
4980
4981 fname = get_tv_string(&argvars[0]);
4982
4983 rettv->v_type = VAR_STRING;
4984 if (mch_stat((char *)fname, &st) >= 0)
4985 {
4986 perm = vim_strsave((char_u *)"---------");
4987 if (perm != NULL)
4988 {
4989 for (i = 0; i < 9; i++)
4990 {
4991 if (st.st_mode & (1 << (8 - i)))
4992 perm[i] = flags[i % 3];
4993 }
4994 }
4995 }
4996 rettv->vval.v_string = perm;
4997}
4998
4999/*
5000 * "getfsize({fname})" function
5001 */
5002 static void
5003f_getfsize(typval_T *argvars, typval_T *rettv)
5004{
5005 char_u *fname;
5006 stat_T st;
5007
5008 fname = get_tv_string(&argvars[0]);
5009
5010 rettv->v_type = VAR_NUMBER;
5011
5012 if (mch_stat((char *)fname, &st) >= 0)
5013 {
5014 if (mch_isdir(fname))
5015 rettv->vval.v_number = 0;
5016 else
5017 {
5018 rettv->vval.v_number = (varnumber_T)st.st_size;
5019
5020 /* non-perfect check for overflow */
5021 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5022 rettv->vval.v_number = -2;
5023 }
5024 }
5025 else
5026 rettv->vval.v_number = -1;
5027}
5028
5029/*
5030 * "getftime({fname})" function
5031 */
5032 static void
5033f_getftime(typval_T *argvars, typval_T *rettv)
5034{
5035 char_u *fname;
5036 stat_T st;
5037
5038 fname = get_tv_string(&argvars[0]);
5039
5040 if (mch_stat((char *)fname, &st) >= 0)
5041 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5042 else
5043 rettv->vval.v_number = -1;
5044}
5045
5046/*
5047 * "getftype({fname})" function
5048 */
5049 static void
5050f_getftype(typval_T *argvars, typval_T *rettv)
5051{
5052 char_u *fname;
5053 stat_T st;
5054 char_u *type = NULL;
5055 char *t;
5056
5057 fname = get_tv_string(&argvars[0]);
5058
5059 rettv->v_type = VAR_STRING;
5060 if (mch_lstat((char *)fname, &st) >= 0)
5061 {
5062#ifdef S_ISREG
5063 if (S_ISREG(st.st_mode))
5064 t = "file";
5065 else if (S_ISDIR(st.st_mode))
5066 t = "dir";
5067# ifdef S_ISLNK
5068 else if (S_ISLNK(st.st_mode))
5069 t = "link";
5070# endif
5071# ifdef S_ISBLK
5072 else if (S_ISBLK(st.st_mode))
5073 t = "bdev";
5074# endif
5075# ifdef S_ISCHR
5076 else if (S_ISCHR(st.st_mode))
5077 t = "cdev";
5078# endif
5079# ifdef S_ISFIFO
5080 else if (S_ISFIFO(st.st_mode))
5081 t = "fifo";
5082# endif
5083# ifdef S_ISSOCK
5084 else if (S_ISSOCK(st.st_mode))
5085 t = "fifo";
5086# endif
5087 else
5088 t = "other";
5089#else
5090# ifdef S_IFMT
5091 switch (st.st_mode & S_IFMT)
5092 {
5093 case S_IFREG: t = "file"; break;
5094 case S_IFDIR: t = "dir"; break;
5095# ifdef S_IFLNK
5096 case S_IFLNK: t = "link"; break;
5097# endif
5098# ifdef S_IFBLK
5099 case S_IFBLK: t = "bdev"; break;
5100# endif
5101# ifdef S_IFCHR
5102 case S_IFCHR: t = "cdev"; break;
5103# endif
5104# ifdef S_IFIFO
5105 case S_IFIFO: t = "fifo"; break;
5106# endif
5107# ifdef S_IFSOCK
5108 case S_IFSOCK: t = "socket"; break;
5109# endif
5110 default: t = "other";
5111 }
5112# else
5113 if (mch_isdir(fname))
5114 t = "dir";
5115 else
5116 t = "file";
5117# endif
5118#endif
5119 type = vim_strsave((char_u *)t);
5120 }
5121 rettv->vval.v_string = type;
5122}
5123
5124/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005125 * "getjumplist()" function
5126 */
5127 static void
5128f_getjumplist(typval_T *argvars, typval_T *rettv)
5129{
5130#ifdef FEAT_JUMPLIST
5131 win_T *wp;
5132 int i;
5133 list_T *l;
5134 dict_T *d;
5135#endif
5136
5137 if (rettv_list_alloc(rettv) != OK)
5138 return;
5139
5140#ifdef FEAT_JUMPLIST
5141 wp = find_tabwin(&argvars[0], &argvars[1]);
5142 if (wp == NULL)
5143 return;
5144
5145 l = list_alloc();
5146 if (l == NULL)
5147 return;
5148
5149 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5150 return;
5151 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5152
Bram Moolenaar48679742018-02-13 13:33:29 +01005153 cleanup_jumplist(wp, TRUE);
5154
Bram Moolenaar4f505882018-02-10 21:06:32 +01005155 for (i = 0; i < wp->w_jumplistlen; ++i)
5156 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005157 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5158 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005159 if ((d = dict_alloc()) == NULL)
5160 return;
5161 if (list_append_dict(l, d) == FAIL)
5162 return;
5163 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
5164 NULL);
5165 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
5166 NULL);
5167# ifdef FEAT_VIRTUALEDIT
5168 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
5169 NULL);
5170# endif
5171 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005172 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01005173 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
5174 }
5175#endif
5176}
5177
5178/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005179 * "getline(lnum, [end])" function
5180 */
5181 static void
5182f_getline(typval_T *argvars, typval_T *rettv)
5183{
5184 linenr_T lnum;
5185 linenr_T end;
5186 int retlist;
5187
5188 lnum = get_tv_lnum(argvars);
5189 if (argvars[1].v_type == VAR_UNKNOWN)
5190 {
5191 end = 0;
5192 retlist = FALSE;
5193 }
5194 else
5195 {
5196 end = get_tv_lnum(&argvars[1]);
5197 retlist = TRUE;
5198 }
5199
5200 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5201}
5202
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005203#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005204 static void
5205get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5206{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005207 if (what_arg->v_type == VAR_UNKNOWN)
5208 {
5209 if (rettv_list_alloc(rettv) == OK)
5210 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005211 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005212 }
5213 else
5214 {
5215 if (rettv_dict_alloc(rettv) == OK)
5216 if (is_qf || (wp != NULL))
5217 {
5218 if (what_arg->v_type == VAR_DICT)
5219 {
5220 dict_T *d = what_arg->vval.v_dict;
5221
5222 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005223 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005224 }
5225 else
5226 EMSG(_(e_dictreq));
5227 }
5228 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005229}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005230#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005231
5232/*
5233 * "getloclist()" function
5234 */
5235 static void
5236f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5237{
5238#ifdef FEAT_QUICKFIX
5239 win_T *wp;
5240
5241 wp = find_win_by_nr(&argvars[0], NULL);
5242 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5243#endif
5244}
5245
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005246/*
5247 * "getmatches()" function
5248 */
5249 static void
5250f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5251{
5252#ifdef FEAT_SEARCH_EXTRA
5253 dict_T *dict;
5254 matchitem_T *cur = curwin->w_match_head;
5255 int i;
5256
5257 if (rettv_list_alloc(rettv) == OK)
5258 {
5259 while (cur != NULL)
5260 {
5261 dict = dict_alloc();
5262 if (dict == NULL)
5263 return;
5264 if (cur->match.regprog == NULL)
5265 {
5266 /* match added with matchaddpos() */
5267 for (i = 0; i < MAXPOSMATCH; ++i)
5268 {
5269 llpos_T *llpos;
5270 char buf[6];
5271 list_T *l;
5272
5273 llpos = &cur->pos.pos[i];
5274 if (llpos->lnum == 0)
5275 break;
5276 l = list_alloc();
5277 if (l == NULL)
5278 break;
5279 list_append_number(l, (varnumber_T)llpos->lnum);
5280 if (llpos->col > 0)
5281 {
5282 list_append_number(l, (varnumber_T)llpos->col);
5283 list_append_number(l, (varnumber_T)llpos->len);
5284 }
5285 sprintf(buf, "pos%d", i + 1);
5286 dict_add_list(dict, buf, l);
5287 }
5288 }
5289 else
5290 {
5291 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5292 }
5293 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5294 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5295 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5296# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5297 if (cur->conceal_char)
5298 {
5299 char_u buf[MB_MAXBYTES + 1];
5300
5301 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5302 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5303 }
5304# endif
5305 list_append_dict(rettv->vval.v_list, dict);
5306 cur = cur->next;
5307 }
5308 }
5309#endif
5310}
5311
5312/*
5313 * "getpid()" function
5314 */
5315 static void
5316f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5317{
5318 rettv->vval.v_number = mch_get_pid();
5319}
5320
5321 static void
5322getpos_both(
5323 typval_T *argvars,
5324 typval_T *rettv,
5325 int getcurpos)
5326{
5327 pos_T *fp;
5328 list_T *l;
5329 int fnum = -1;
5330
5331 if (rettv_list_alloc(rettv) == OK)
5332 {
5333 l = rettv->vval.v_list;
5334 if (getcurpos)
5335 fp = &curwin->w_cursor;
5336 else
5337 fp = var2fpos(&argvars[0], TRUE, &fnum);
5338 if (fnum != -1)
5339 list_append_number(l, (varnumber_T)fnum);
5340 else
5341 list_append_number(l, (varnumber_T)0);
5342 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5343 : (varnumber_T)0);
5344 list_append_number(l, (fp != NULL)
5345 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5346 : (varnumber_T)0);
5347 list_append_number(l,
5348#ifdef FEAT_VIRTUALEDIT
5349 (fp != NULL) ? (varnumber_T)fp->coladd :
5350#endif
5351 (varnumber_T)0);
5352 if (getcurpos)
5353 {
5354 update_curswant();
5355 list_append_number(l, curwin->w_curswant == MAXCOL ?
5356 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5357 }
5358 }
5359 else
5360 rettv->vval.v_number = FALSE;
5361}
5362
5363
5364/*
5365 * "getcurpos()" function
5366 */
5367 static void
5368f_getcurpos(typval_T *argvars, typval_T *rettv)
5369{
5370 getpos_both(argvars, rettv, TRUE);
5371}
5372
5373/*
5374 * "getpos(string)" function
5375 */
5376 static void
5377f_getpos(typval_T *argvars, typval_T *rettv)
5378{
5379 getpos_both(argvars, rettv, FALSE);
5380}
5381
5382/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005383 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005384 */
5385 static void
5386f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5387{
5388#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005389 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005390#endif
5391}
5392
5393/*
5394 * "getreg()" function
5395 */
5396 static void
5397f_getreg(typval_T *argvars, typval_T *rettv)
5398{
5399 char_u *strregname;
5400 int regname;
5401 int arg2 = FALSE;
5402 int return_list = FALSE;
5403 int error = FALSE;
5404
5405 if (argvars[0].v_type != VAR_UNKNOWN)
5406 {
5407 strregname = get_tv_string_chk(&argvars[0]);
5408 error = strregname == NULL;
5409 if (argvars[1].v_type != VAR_UNKNOWN)
5410 {
5411 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5412 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5413 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5414 }
5415 }
5416 else
5417 strregname = get_vim_var_str(VV_REG);
5418
5419 if (error)
5420 return;
5421
5422 regname = (strregname == NULL ? '"' : *strregname);
5423 if (regname == 0)
5424 regname = '"';
5425
5426 if (return_list)
5427 {
5428 rettv->v_type = VAR_LIST;
5429 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5430 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5431 if (rettv->vval.v_list == NULL)
5432 (void)rettv_list_alloc(rettv);
5433 else
5434 ++rettv->vval.v_list->lv_refcount;
5435 }
5436 else
5437 {
5438 rettv->v_type = VAR_STRING;
5439 rettv->vval.v_string = get_reg_contents(regname,
5440 arg2 ? GREG_EXPR_SRC : 0);
5441 }
5442}
5443
5444/*
5445 * "getregtype()" function
5446 */
5447 static void
5448f_getregtype(typval_T *argvars, typval_T *rettv)
5449{
5450 char_u *strregname;
5451 int regname;
5452 char_u buf[NUMBUFLEN + 2];
5453 long reglen = 0;
5454
5455 if (argvars[0].v_type != VAR_UNKNOWN)
5456 {
5457 strregname = get_tv_string_chk(&argvars[0]);
5458 if (strregname == NULL) /* type error; errmsg already given */
5459 {
5460 rettv->v_type = VAR_STRING;
5461 rettv->vval.v_string = NULL;
5462 return;
5463 }
5464 }
5465 else
5466 /* Default to v:register */
5467 strregname = get_vim_var_str(VV_REG);
5468
5469 regname = (strregname == NULL ? '"' : *strregname);
5470 if (regname == 0)
5471 regname = '"';
5472
5473 buf[0] = NUL;
5474 buf[1] = NUL;
5475 switch (get_reg_type(regname, &reglen))
5476 {
5477 case MLINE: buf[0] = 'V'; break;
5478 case MCHAR: buf[0] = 'v'; break;
5479 case MBLOCK:
5480 buf[0] = Ctrl_V;
5481 sprintf((char *)buf + 1, "%ld", reglen + 1);
5482 break;
5483 }
5484 rettv->v_type = VAR_STRING;
5485 rettv->vval.v_string = vim_strsave(buf);
5486}
5487
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005488/*
5489 * Returns information (variables, options, etc.) about a tab page
5490 * as a dictionary.
5491 */
5492 static dict_T *
5493get_tabpage_info(tabpage_T *tp, int tp_idx)
5494{
5495 win_T *wp;
5496 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005497 list_T *l;
5498
5499 dict = dict_alloc();
5500 if (dict == NULL)
5501 return NULL;
5502
Bram Moolenaar33928832016-08-18 21:22:04 +02005503 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005504
5505 l = list_alloc();
5506 if (l != NULL)
5507 {
5508 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5509 wp; wp = wp->w_next)
5510 list_append_number(l, (varnumber_T)wp->w_id);
5511 dict_add_list(dict, "windows", l);
5512 }
5513
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005514 /* Make a reference to tabpage variables */
5515 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005516
5517 return dict;
5518}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005519
5520/*
5521 * "gettabinfo()" function
5522 */
5523 static void
5524f_gettabinfo(typval_T *argvars, typval_T *rettv)
5525{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005526 tabpage_T *tp, *tparg = NULL;
5527 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005528 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005529
5530 if (rettv_list_alloc(rettv) != OK)
5531 return;
5532
5533 if (argvars[0].v_type != VAR_UNKNOWN)
5534 {
5535 /* Information about one tab page */
5536 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5537 if (tparg == NULL)
5538 return;
5539 }
5540
5541 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005542 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005543 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005544 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005545 if (tparg != NULL && tp != tparg)
5546 continue;
5547 d = get_tabpage_info(tp, tpnr);
5548 if (d != NULL)
5549 list_append_dict(rettv->vval.v_list, d);
5550 if (tparg != NULL)
5551 return;
5552 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005553}
5554
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005555/*
5556 * "gettabvar()" function
5557 */
5558 static void
5559f_gettabvar(typval_T *argvars, typval_T *rettv)
5560{
5561 win_T *oldcurwin;
5562 tabpage_T *tp, *oldtabpage;
5563 dictitem_T *v;
5564 char_u *varname;
5565 int done = FALSE;
5566
5567 rettv->v_type = VAR_STRING;
5568 rettv->vval.v_string = NULL;
5569
5570 varname = get_tv_string_chk(&argvars[1]);
5571 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5572 if (tp != NULL && varname != NULL)
5573 {
5574 /* Set tp to be our tabpage, temporarily. Also set the window to the
5575 * first window in the tabpage, otherwise the window is not valid. */
5576 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005577 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5578 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005579 {
5580 /* look up the variable */
5581 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5582 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5583 if (v != NULL)
5584 {
5585 copy_tv(&v->di_tv, rettv);
5586 done = TRUE;
5587 }
5588 }
5589
5590 /* restore previous notion of curwin */
5591 restore_win(oldcurwin, oldtabpage, TRUE);
5592 }
5593
5594 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5595 copy_tv(&argvars[2], rettv);
5596}
5597
5598/*
5599 * "gettabwinvar()" function
5600 */
5601 static void
5602f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5603{
5604 getwinvar(argvars, rettv, 1);
5605}
5606
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005607/*
5608 * Returns information about a window as a dictionary.
5609 */
5610 static dict_T *
5611get_win_info(win_T *wp, short tpnr, short winnr)
5612{
5613 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005614
5615 dict = dict_alloc();
5616 if (dict == NULL)
5617 return NULL;
5618
Bram Moolenaar33928832016-08-18 21:22:04 +02005619 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5620 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005621 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5622 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005623#ifdef FEAT_MENU
5624 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5625#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005626 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005627 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005628
Bram Moolenaar69905d12017-08-13 18:14:47 +02005629#ifdef FEAT_TERMINAL
5630 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5631#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005632#ifdef FEAT_QUICKFIX
5633 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5634 dict_add_nr_str(dict, "loclist",
5635 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5636#endif
5637
Bram Moolenaar30567352016-08-27 21:25:44 +02005638 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005639 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005640
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005641 return dict;
5642}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005643
5644/*
5645 * "getwininfo()" function
5646 */
5647 static void
5648f_getwininfo(typval_T *argvars, typval_T *rettv)
5649{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005650 tabpage_T *tp;
5651 win_T *wp = NULL, *wparg = NULL;
5652 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005653 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005654
5655 if (rettv_list_alloc(rettv) != OK)
5656 return;
5657
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005658 if (argvars[0].v_type != VAR_UNKNOWN)
5659 {
5660 wparg = win_id2wp(argvars);
5661 if (wparg == NULL)
5662 return;
5663 }
5664
5665 /* Collect information about either all the windows across all the tab
5666 * pages or one particular window.
5667 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005668 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005669 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005670 tabnr++;
5671 winnr = 0;
5672 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005673 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005674 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005675 if (wparg != NULL && wp != wparg)
5676 continue;
5677 d = get_win_info(wp, tabnr, winnr);
5678 if (d != NULL)
5679 list_append_dict(rettv->vval.v_list, d);
5680 if (wparg != NULL)
5681 /* found information about a specific window */
5682 return;
5683 }
5684 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005685}
5686
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005687/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005688 * "win_findbuf()" function
5689 */
5690 static void
5691f_win_findbuf(typval_T *argvars, typval_T *rettv)
5692{
5693 if (rettv_list_alloc(rettv) != FAIL)
5694 win_findbuf(argvars, rettv->vval.v_list);
5695}
5696
5697/*
5698 * "win_getid()" function
5699 */
5700 static void
5701f_win_getid(typval_T *argvars, typval_T *rettv)
5702{
5703 rettv->vval.v_number = win_getid(argvars);
5704}
5705
5706/*
5707 * "win_gotoid()" function
5708 */
5709 static void
5710f_win_gotoid(typval_T *argvars, typval_T *rettv)
5711{
5712 rettv->vval.v_number = win_gotoid(argvars);
5713}
5714
5715/*
5716 * "win_id2tabwin()" function
5717 */
5718 static void
5719f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5720{
5721 if (rettv_list_alloc(rettv) != FAIL)
5722 win_id2tabwin(argvars, rettv->vval.v_list);
5723}
5724
5725/*
5726 * "win_id2win()" function
5727 */
5728 static void
5729f_win_id2win(typval_T *argvars, typval_T *rettv)
5730{
5731 rettv->vval.v_number = win_id2win(argvars);
5732}
5733
5734/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005735 * "win_screenpos()" function
5736 */
5737 static void
5738f_win_screenpos(typval_T *argvars, typval_T *rettv)
5739{
5740 win_T *wp;
5741
5742 if (rettv_list_alloc(rettv) == FAIL)
5743 return;
5744
5745 wp = find_win_by_nr(&argvars[0], NULL);
5746 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5747 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5748}
5749
5750/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005751 * "getwinpos({timeout})" function
5752 */
5753 static void
5754f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5755{
5756 int x = -1;
5757 int y = -1;
5758
5759 if (rettv_list_alloc(rettv) == FAIL)
5760 return;
5761#ifdef FEAT_GUI
5762 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005763 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005764# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5765 else
5766# endif
5767#endif
5768#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5769 {
5770 varnumber_T timeout = 100;
5771
5772 if (argvars[0].v_type != VAR_UNKNOWN)
5773 timeout = get_tv_number(&argvars[0]);
5774 term_get_winpos(&x, &y, timeout);
5775 }
5776#endif
5777 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5778 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5779}
5780
5781
5782/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005783 * "getwinposx()" function
5784 */
5785 static void
5786f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5787{
5788 rettv->vval.v_number = -1;
5789#ifdef FEAT_GUI
5790 if (gui.in_use)
5791 {
5792 int x, y;
5793
5794 if (gui_mch_get_winpos(&x, &y) == OK)
5795 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005796 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005797 }
5798#endif
5799#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5800 {
5801 int x, y;
5802
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005803 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005804 rettv->vval.v_number = x;
5805 }
5806#endif
5807}
5808
5809/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005810 * "getwinposy()" function
5811 */
5812 static void
5813f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5814{
5815 rettv->vval.v_number = -1;
5816#ifdef FEAT_GUI
5817 if (gui.in_use)
5818 {
5819 int x, y;
5820
5821 if (gui_mch_get_winpos(&x, &y) == OK)
5822 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005823 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005824 }
5825#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005826#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5827 {
5828 int x, y;
5829
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005830 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005831 rettv->vval.v_number = y;
5832 }
5833#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005834}
5835
5836/*
5837 * "getwinvar()" function
5838 */
5839 static void
5840f_getwinvar(typval_T *argvars, typval_T *rettv)
5841{
5842 getwinvar(argvars, rettv, 0);
5843}
5844
5845/*
5846 * "glob()" function
5847 */
5848 static void
5849f_glob(typval_T *argvars, typval_T *rettv)
5850{
5851 int options = WILD_SILENT|WILD_USE_NL;
5852 expand_T xpc;
5853 int error = FALSE;
5854
5855 /* When the optional second argument is non-zero, don't remove matches
5856 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5857 rettv->v_type = VAR_STRING;
5858 if (argvars[1].v_type != VAR_UNKNOWN)
5859 {
5860 if (get_tv_number_chk(&argvars[1], &error))
5861 options |= WILD_KEEP_ALL;
5862 if (argvars[2].v_type != VAR_UNKNOWN)
5863 {
5864 if (get_tv_number_chk(&argvars[2], &error))
5865 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005866 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005867 }
5868 if (argvars[3].v_type != VAR_UNKNOWN
5869 && get_tv_number_chk(&argvars[3], &error))
5870 options |= WILD_ALLLINKS;
5871 }
5872 }
5873 if (!error)
5874 {
5875 ExpandInit(&xpc);
5876 xpc.xp_context = EXPAND_FILES;
5877 if (p_wic)
5878 options += WILD_ICASE;
5879 if (rettv->v_type == VAR_STRING)
5880 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5881 NULL, options, WILD_ALL);
5882 else if (rettv_list_alloc(rettv) != FAIL)
5883 {
5884 int i;
5885
5886 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5887 NULL, options, WILD_ALL_KEEP);
5888 for (i = 0; i < xpc.xp_numfiles; i++)
5889 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5890
5891 ExpandCleanup(&xpc);
5892 }
5893 }
5894 else
5895 rettv->vval.v_string = NULL;
5896}
5897
5898/*
5899 * "globpath()" function
5900 */
5901 static void
5902f_globpath(typval_T *argvars, typval_T *rettv)
5903{
5904 int flags = 0;
5905 char_u buf1[NUMBUFLEN];
5906 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5907 int error = FALSE;
5908 garray_T ga;
5909 int i;
5910
5911 /* When the optional second argument is non-zero, don't remove matches
5912 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5913 rettv->v_type = VAR_STRING;
5914 if (argvars[2].v_type != VAR_UNKNOWN)
5915 {
5916 if (get_tv_number_chk(&argvars[2], &error))
5917 flags |= WILD_KEEP_ALL;
5918 if (argvars[3].v_type != VAR_UNKNOWN)
5919 {
5920 if (get_tv_number_chk(&argvars[3], &error))
5921 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005922 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005923 }
5924 if (argvars[4].v_type != VAR_UNKNOWN
5925 && get_tv_number_chk(&argvars[4], &error))
5926 flags |= WILD_ALLLINKS;
5927 }
5928 }
5929 if (file != NULL && !error)
5930 {
5931 ga_init2(&ga, (int)sizeof(char_u *), 10);
5932 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5933 if (rettv->v_type == VAR_STRING)
5934 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5935 else if (rettv_list_alloc(rettv) != FAIL)
5936 for (i = 0; i < ga.ga_len; ++i)
5937 list_append_string(rettv->vval.v_list,
5938 ((char_u **)(ga.ga_data))[i], -1);
5939 ga_clear_strings(&ga);
5940 }
5941 else
5942 rettv->vval.v_string = NULL;
5943}
5944
5945/*
5946 * "glob2regpat()" function
5947 */
5948 static void
5949f_glob2regpat(typval_T *argvars, typval_T *rettv)
5950{
5951 char_u *pat = get_tv_string_chk(&argvars[0]);
5952
5953 rettv->v_type = VAR_STRING;
5954 rettv->vval.v_string = (pat == NULL)
5955 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5956}
5957
5958/* for VIM_VERSION_ defines */
5959#include "version.h"
5960
5961/*
5962 * "has()" function
5963 */
5964 static void
5965f_has(typval_T *argvars, typval_T *rettv)
5966{
5967 int i;
5968 char_u *name;
5969 int n = FALSE;
5970 static char *(has_list[]) =
5971 {
5972#ifdef AMIGA
5973 "amiga",
5974# ifdef FEAT_ARP
5975 "arp",
5976# endif
5977#endif
5978#ifdef __BEOS__
5979 "beos",
5980#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005981#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005982 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5983 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005984# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005985 "macunix", /* Mac OS X, with the darwin feature */
5986 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005987# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005988#endif
5989#ifdef __QNX__
5990 "qnx",
5991#endif
5992#ifdef UNIX
5993 "unix",
5994#endif
5995#ifdef VMS
5996 "vms",
5997#endif
5998#ifdef WIN32
5999 "win32",
6000#endif
6001#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6002 "win32unix",
6003#endif
6004#if defined(WIN64) || defined(_WIN64)
6005 "win64",
6006#endif
6007#ifdef EBCDIC
6008 "ebcdic",
6009#endif
6010#ifndef CASE_INSENSITIVE_FILENAME
6011 "fname_case",
6012#endif
6013#ifdef HAVE_ACL
6014 "acl",
6015#endif
6016#ifdef FEAT_ARABIC
6017 "arabic",
6018#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006019 "autocmd",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006020#ifdef FEAT_AUTOSERVERNAME
6021 "autoservername",
6022#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006023#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006024 "balloon_eval",
6025# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6026 "balloon_multiline",
6027# endif
6028#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006029#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006030 "balloon_eval_term",
6031#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006032#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6033 "builtin_terms",
6034# ifdef ALL_BUILTIN_TCAPS
6035 "all_builtin_terms",
6036# endif
6037#endif
6038#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6039 || defined(FEAT_GUI_W32) \
6040 || defined(FEAT_GUI_MOTIF))
6041 "browsefilter",
6042#endif
6043#ifdef FEAT_BYTEOFF
6044 "byte_offset",
6045#endif
6046#ifdef FEAT_JOB_CHANNEL
6047 "channel",
6048#endif
6049#ifdef FEAT_CINDENT
6050 "cindent",
6051#endif
6052#ifdef FEAT_CLIENTSERVER
6053 "clientserver",
6054#endif
6055#ifdef FEAT_CLIPBOARD
6056 "clipboard",
6057#endif
6058#ifdef FEAT_CMDL_COMPL
6059 "cmdline_compl",
6060#endif
6061#ifdef FEAT_CMDHIST
6062 "cmdline_hist",
6063#endif
6064#ifdef FEAT_COMMENTS
6065 "comments",
6066#endif
6067#ifdef FEAT_CONCEAL
6068 "conceal",
6069#endif
6070#ifdef FEAT_CRYPT
6071 "cryptv",
6072 "crypt-blowfish",
6073 "crypt-blowfish2",
6074#endif
6075#ifdef FEAT_CSCOPE
6076 "cscope",
6077#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006078 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006079#ifdef CURSOR_SHAPE
6080 "cursorshape",
6081#endif
6082#ifdef DEBUG
6083 "debug",
6084#endif
6085#ifdef FEAT_CON_DIALOG
6086 "dialog_con",
6087#endif
6088#ifdef FEAT_GUI_DIALOG
6089 "dialog_gui",
6090#endif
6091#ifdef FEAT_DIFF
6092 "diff",
6093#endif
6094#ifdef FEAT_DIGRAPHS
6095 "digraphs",
6096#endif
6097#ifdef FEAT_DIRECTX
6098 "directx",
6099#endif
6100#ifdef FEAT_DND
6101 "dnd",
6102#endif
6103#ifdef FEAT_EMACS_TAGS
6104 "emacs_tags",
6105#endif
6106 "eval", /* always present, of course! */
6107 "ex_extra", /* graduated feature */
6108#ifdef FEAT_SEARCH_EXTRA
6109 "extra_search",
6110#endif
6111#ifdef FEAT_FKMAP
6112 "farsi",
6113#endif
6114#ifdef FEAT_SEARCHPATH
6115 "file_in_path",
6116#endif
6117#ifdef FEAT_FILTERPIPE
6118 "filterpipe",
6119#endif
6120#ifdef FEAT_FIND_ID
6121 "find_in_path",
6122#endif
6123#ifdef FEAT_FLOAT
6124 "float",
6125#endif
6126#ifdef FEAT_FOLDING
6127 "folding",
6128#endif
6129#ifdef FEAT_FOOTER
6130 "footer",
6131#endif
6132#if !defined(USE_SYSTEM) && defined(UNIX)
6133 "fork",
6134#endif
6135#ifdef FEAT_GETTEXT
6136 "gettext",
6137#endif
6138#ifdef FEAT_GUI
6139 "gui",
6140#endif
6141#ifdef FEAT_GUI_ATHENA
6142# ifdef FEAT_GUI_NEXTAW
6143 "gui_neXtaw",
6144# else
6145 "gui_athena",
6146# endif
6147#endif
6148#ifdef FEAT_GUI_GTK
6149 "gui_gtk",
6150# ifdef USE_GTK3
6151 "gui_gtk3",
6152# else
6153 "gui_gtk2",
6154# endif
6155#endif
6156#ifdef FEAT_GUI_GNOME
6157 "gui_gnome",
6158#endif
6159#ifdef FEAT_GUI_MAC
6160 "gui_mac",
6161#endif
6162#ifdef FEAT_GUI_MOTIF
6163 "gui_motif",
6164#endif
6165#ifdef FEAT_GUI_PHOTON
6166 "gui_photon",
6167#endif
6168#ifdef FEAT_GUI_W32
6169 "gui_win32",
6170#endif
6171#ifdef FEAT_HANGULIN
6172 "hangul_input",
6173#endif
6174#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6175 "iconv",
6176#endif
6177#ifdef FEAT_INS_EXPAND
6178 "insert_expand",
6179#endif
6180#ifdef FEAT_JOB_CHANNEL
6181 "job",
6182#endif
6183#ifdef FEAT_JUMPLIST
6184 "jumplist",
6185#endif
6186#ifdef FEAT_KEYMAP
6187 "keymap",
6188#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006189 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190#ifdef FEAT_LANGMAP
6191 "langmap",
6192#endif
6193#ifdef FEAT_LIBCALL
6194 "libcall",
6195#endif
6196#ifdef FEAT_LINEBREAK
6197 "linebreak",
6198#endif
6199#ifdef FEAT_LISP
6200 "lispindent",
6201#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006202 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006203#ifdef FEAT_LOCALMAP
6204 "localmap",
6205#endif
6206#ifdef FEAT_LUA
6207# ifndef DYNAMIC_LUA
6208 "lua",
6209# endif
6210#endif
6211#ifdef FEAT_MENU
6212 "menu",
6213#endif
6214#ifdef FEAT_SESSION
6215 "mksession",
6216#endif
6217#ifdef FEAT_MODIFY_FNAME
6218 "modify_fname",
6219#endif
6220#ifdef FEAT_MOUSE
6221 "mouse",
6222#endif
6223#ifdef FEAT_MOUSESHAPE
6224 "mouseshape",
6225#endif
6226#if defined(UNIX) || defined(VMS)
6227# ifdef FEAT_MOUSE_DEC
6228 "mouse_dec",
6229# endif
6230# ifdef FEAT_MOUSE_GPM
6231 "mouse_gpm",
6232# endif
6233# ifdef FEAT_MOUSE_JSB
6234 "mouse_jsbterm",
6235# endif
6236# ifdef FEAT_MOUSE_NET
6237 "mouse_netterm",
6238# endif
6239# ifdef FEAT_MOUSE_PTERM
6240 "mouse_pterm",
6241# endif
6242# ifdef FEAT_MOUSE_SGR
6243 "mouse_sgr",
6244# endif
6245# ifdef FEAT_SYSMOUSE
6246 "mouse_sysmouse",
6247# endif
6248# ifdef FEAT_MOUSE_URXVT
6249 "mouse_urxvt",
6250# endif
6251# ifdef FEAT_MOUSE_XTERM
6252 "mouse_xterm",
6253# endif
6254#endif
6255#ifdef FEAT_MBYTE
6256 "multi_byte",
6257#endif
6258#ifdef FEAT_MBYTE_IME
6259 "multi_byte_ime",
6260#endif
6261#ifdef FEAT_MULTI_LANG
6262 "multi_lang",
6263#endif
6264#ifdef FEAT_MZSCHEME
6265#ifndef DYNAMIC_MZSCHEME
6266 "mzscheme",
6267#endif
6268#endif
6269#ifdef FEAT_NUM64
6270 "num64",
6271#endif
6272#ifdef FEAT_OLE
6273 "ole",
6274#endif
6275 "packages",
6276#ifdef FEAT_PATH_EXTRA
6277 "path_extra",
6278#endif
6279#ifdef FEAT_PERL
6280#ifndef DYNAMIC_PERL
6281 "perl",
6282#endif
6283#endif
6284#ifdef FEAT_PERSISTENT_UNDO
6285 "persistent_undo",
6286#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006287#if defined(FEAT_PYTHON)
6288 "python_compiled",
6289# if defined(DYNAMIC_PYTHON)
6290 "python_dynamic",
6291# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006292 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006293 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006294# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006295#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006296#if defined(FEAT_PYTHON3)
6297 "python3_compiled",
6298# if defined(DYNAMIC_PYTHON3)
6299 "python3_dynamic",
6300# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006301 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006302 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006303# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006304#endif
6305#ifdef FEAT_POSTSCRIPT
6306 "postscript",
6307#endif
6308#ifdef FEAT_PRINTER
6309 "printer",
6310#endif
6311#ifdef FEAT_PROFILE
6312 "profile",
6313#endif
6314#ifdef FEAT_RELTIME
6315 "reltime",
6316#endif
6317#ifdef FEAT_QUICKFIX
6318 "quickfix",
6319#endif
6320#ifdef FEAT_RIGHTLEFT
6321 "rightleft",
6322#endif
6323#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6324 "ruby",
6325#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006326 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006327#ifdef FEAT_CMDL_INFO
6328 "showcmd",
6329 "cmdline_info",
6330#endif
6331#ifdef FEAT_SIGNS
6332 "signs",
6333#endif
6334#ifdef FEAT_SMARTINDENT
6335 "smartindent",
6336#endif
6337#ifdef STARTUPTIME
6338 "startuptime",
6339#endif
6340#ifdef FEAT_STL_OPT
6341 "statusline",
6342#endif
6343#ifdef FEAT_SUN_WORKSHOP
6344 "sun_workshop",
6345#endif
6346#ifdef FEAT_NETBEANS_INTG
6347 "netbeans_intg",
6348#endif
6349#ifdef FEAT_SPELL
6350 "spell",
6351#endif
6352#ifdef FEAT_SYN_HL
6353 "syntax",
6354#endif
6355#if defined(USE_SYSTEM) || !defined(UNIX)
6356 "system",
6357#endif
6358#ifdef FEAT_TAG_BINS
6359 "tag_binary",
6360#endif
6361#ifdef FEAT_TAG_OLDSTATIC
6362 "tag_old_static",
6363#endif
6364#ifdef FEAT_TAG_ANYWHITE
6365 "tag_any_white",
6366#endif
6367#ifdef FEAT_TCL
6368# ifndef DYNAMIC_TCL
6369 "tcl",
6370# endif
6371#endif
6372#ifdef FEAT_TERMGUICOLORS
6373 "termguicolors",
6374#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006375#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006376 "terminal",
6377#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006378#ifdef TERMINFO
6379 "terminfo",
6380#endif
6381#ifdef FEAT_TERMRESPONSE
6382 "termresponse",
6383#endif
6384#ifdef FEAT_TEXTOBJ
6385 "textobjects",
6386#endif
6387#ifdef HAVE_TGETENT
6388 "tgetent",
6389#endif
6390#ifdef FEAT_TIMERS
6391 "timers",
6392#endif
6393#ifdef FEAT_TITLE
6394 "title",
6395#endif
6396#ifdef FEAT_TOOLBAR
6397 "toolbar",
6398#endif
6399#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6400 "unnamedplus",
6401#endif
6402#ifdef FEAT_USR_CMDS
6403 "user-commands", /* was accidentally included in 5.4 */
6404 "user_commands",
6405#endif
6406#ifdef FEAT_VIMINFO
6407 "viminfo",
6408#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006409 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006410#ifdef FEAT_VIRTUALEDIT
6411 "virtualedit",
6412#endif
6413 "visual",
6414#ifdef FEAT_VISUALEXTRA
6415 "visualextra",
6416#endif
6417#ifdef FEAT_VREPLACE
6418 "vreplace",
6419#endif
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006420#ifdef FEAT_VTP
6421 "vtp",
6422#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006423#ifdef FEAT_WILDIGN
6424 "wildignore",
6425#endif
6426#ifdef FEAT_WILDMENU
6427 "wildmenu",
6428#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006429 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006430#ifdef FEAT_WAK
6431 "winaltkeys",
6432#endif
6433#ifdef FEAT_WRITEBACKUP
6434 "writebackup",
6435#endif
6436#ifdef FEAT_XIM
6437 "xim",
6438#endif
6439#ifdef FEAT_XFONTSET
6440 "xfontset",
6441#endif
6442#ifdef FEAT_XPM_W32
6443 "xpm",
6444 "xpm_w32", /* for backward compatibility */
6445#else
6446# if defined(HAVE_XPM)
6447 "xpm",
6448# endif
6449#endif
6450#ifdef USE_XSMP
6451 "xsmp",
6452#endif
6453#ifdef USE_XSMP_INTERACT
6454 "xsmp_interact",
6455#endif
6456#ifdef FEAT_XCLIPBOARD
6457 "xterm_clipboard",
6458#endif
6459#ifdef FEAT_XTERM_SAVE
6460 "xterm_save",
6461#endif
6462#if defined(UNIX) && defined(FEAT_X11)
6463 "X11",
6464#endif
6465 NULL
6466 };
6467
6468 name = get_tv_string(&argvars[0]);
6469 for (i = 0; has_list[i] != NULL; ++i)
6470 if (STRICMP(name, has_list[i]) == 0)
6471 {
6472 n = TRUE;
6473 break;
6474 }
6475
6476 if (n == FALSE)
6477 {
6478 if (STRNICMP(name, "patch", 5) == 0)
6479 {
6480 if (name[5] == '-'
6481 && STRLEN(name) >= 11
6482 && vim_isdigit(name[6])
6483 && vim_isdigit(name[8])
6484 && vim_isdigit(name[10]))
6485 {
6486 int major = atoi((char *)name + 6);
6487 int minor = atoi((char *)name + 8);
6488
6489 /* Expect "patch-9.9.01234". */
6490 n = (major < VIM_VERSION_MAJOR
6491 || (major == VIM_VERSION_MAJOR
6492 && (minor < VIM_VERSION_MINOR
6493 || (minor == VIM_VERSION_MINOR
6494 && has_patch(atoi((char *)name + 10))))));
6495 }
6496 else
6497 n = has_patch(atoi((char *)name + 5));
6498 }
6499 else if (STRICMP(name, "vim_starting") == 0)
6500 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006501 else if (STRICMP(name, "ttyin") == 0)
6502 n = mch_input_isatty();
6503 else if (STRICMP(name, "ttyout") == 0)
6504 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006505#ifdef FEAT_MBYTE
6506 else if (STRICMP(name, "multi_byte_encoding") == 0)
6507 n = has_mbyte;
6508#endif
6509#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6510 else if (STRICMP(name, "balloon_multiline") == 0)
6511 n = multiline_balloon_available();
6512#endif
6513#ifdef DYNAMIC_TCL
6514 else if (STRICMP(name, "tcl") == 0)
6515 n = tcl_enabled(FALSE);
6516#endif
6517#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6518 else if (STRICMP(name, "iconv") == 0)
6519 n = iconv_enabled(FALSE);
6520#endif
6521#ifdef DYNAMIC_LUA
6522 else if (STRICMP(name, "lua") == 0)
6523 n = lua_enabled(FALSE);
6524#endif
6525#ifdef DYNAMIC_MZSCHEME
6526 else if (STRICMP(name, "mzscheme") == 0)
6527 n = mzscheme_enabled(FALSE);
6528#endif
6529#ifdef DYNAMIC_RUBY
6530 else if (STRICMP(name, "ruby") == 0)
6531 n = ruby_enabled(FALSE);
6532#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006533#ifdef DYNAMIC_PYTHON
6534 else if (STRICMP(name, "python") == 0)
6535 n = python_enabled(FALSE);
6536#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006537#ifdef DYNAMIC_PYTHON3
6538 else if (STRICMP(name, "python3") == 0)
6539 n = python3_enabled(FALSE);
6540#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006541#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6542 else if (STRICMP(name, "pythonx") == 0)
6543 {
6544# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6545 if (p_pyx == 0)
6546 n = python3_enabled(FALSE) || python_enabled(FALSE);
6547 else if (p_pyx == 3)
6548 n = python3_enabled(FALSE);
6549 else if (p_pyx == 2)
6550 n = python_enabled(FALSE);
6551# elif defined(DYNAMIC_PYTHON)
6552 n = python_enabled(FALSE);
6553# elif defined(DYNAMIC_PYTHON3)
6554 n = python3_enabled(FALSE);
6555# endif
6556 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006557#endif
6558#ifdef DYNAMIC_PERL
6559 else if (STRICMP(name, "perl") == 0)
6560 n = perl_enabled(FALSE);
6561#endif
6562#ifdef FEAT_GUI
6563 else if (STRICMP(name, "gui_running") == 0)
6564 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565# ifdef FEAT_BROWSE
6566 else if (STRICMP(name, "browse") == 0)
6567 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6568# endif
6569#endif
6570#ifdef FEAT_SYN_HL
6571 else if (STRICMP(name, "syntax_items") == 0)
6572 n = syntax_present(curwin);
6573#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006574#ifdef FEAT_VTP
6575 else if (STRICMP(name, "vcon") == 0)
6576 n = has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006577#endif
6578#ifdef FEAT_NETBEANS_INTG
6579 else if (STRICMP(name, "netbeans_enabled") == 0)
6580 n = netbeans_active();
6581#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006582#if defined(FEAT_TERMINAL) && defined(WIN3264)
6583 else if (STRICMP(name, "terminal") == 0)
6584 n = terminal_enabled();
6585#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586 }
6587
6588 rettv->vval.v_number = n;
6589}
6590
6591/*
6592 * "has_key()" function
6593 */
6594 static void
6595f_has_key(typval_T *argvars, typval_T *rettv)
6596{
6597 if (argvars[0].v_type != VAR_DICT)
6598 {
6599 EMSG(_(e_dictreq));
6600 return;
6601 }
6602 if (argvars[0].vval.v_dict == NULL)
6603 return;
6604
6605 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6606 get_tv_string(&argvars[1]), -1) != NULL;
6607}
6608
6609/*
6610 * "haslocaldir()" function
6611 */
6612 static void
6613f_haslocaldir(typval_T *argvars, typval_T *rettv)
6614{
6615 win_T *wp = NULL;
6616
6617 wp = find_tabwin(&argvars[0], &argvars[1]);
6618 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6619}
6620
6621/*
6622 * "hasmapto()" function
6623 */
6624 static void
6625f_hasmapto(typval_T *argvars, typval_T *rettv)
6626{
6627 char_u *name;
6628 char_u *mode;
6629 char_u buf[NUMBUFLEN];
6630 int abbr = FALSE;
6631
6632 name = get_tv_string(&argvars[0]);
6633 if (argvars[1].v_type == VAR_UNKNOWN)
6634 mode = (char_u *)"nvo";
6635 else
6636 {
6637 mode = get_tv_string_buf(&argvars[1], buf);
6638 if (argvars[2].v_type != VAR_UNKNOWN)
6639 abbr = (int)get_tv_number(&argvars[2]);
6640 }
6641
6642 if (map_to_exists(name, mode, abbr))
6643 rettv->vval.v_number = TRUE;
6644 else
6645 rettv->vval.v_number = FALSE;
6646}
6647
6648/*
6649 * "histadd()" function
6650 */
6651 static void
6652f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6653{
6654#ifdef FEAT_CMDHIST
6655 int histype;
6656 char_u *str;
6657 char_u buf[NUMBUFLEN];
6658#endif
6659
6660 rettv->vval.v_number = FALSE;
6661 if (check_restricted() || check_secure())
6662 return;
6663#ifdef FEAT_CMDHIST
6664 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6665 histype = str != NULL ? get_histtype(str) : -1;
6666 if (histype >= 0)
6667 {
6668 str = get_tv_string_buf(&argvars[1], buf);
6669 if (*str != NUL)
6670 {
6671 init_history();
6672 add_to_history(histype, str, FALSE, NUL);
6673 rettv->vval.v_number = TRUE;
6674 return;
6675 }
6676 }
6677#endif
6678}
6679
6680/*
6681 * "histdel()" function
6682 */
6683 static void
6684f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6685{
6686#ifdef FEAT_CMDHIST
6687 int n;
6688 char_u buf[NUMBUFLEN];
6689 char_u *str;
6690
6691 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6692 if (str == NULL)
6693 n = 0;
6694 else if (argvars[1].v_type == VAR_UNKNOWN)
6695 /* only one argument: clear entire history */
6696 n = clr_history(get_histtype(str));
6697 else if (argvars[1].v_type == VAR_NUMBER)
6698 /* index given: remove that entry */
6699 n = del_history_idx(get_histtype(str),
6700 (int)get_tv_number(&argvars[1]));
6701 else
6702 /* string given: remove all matching entries */
6703 n = del_history_entry(get_histtype(str),
6704 get_tv_string_buf(&argvars[1], buf));
6705 rettv->vval.v_number = n;
6706#endif
6707}
6708
6709/*
6710 * "histget()" function
6711 */
6712 static void
6713f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6714{
6715#ifdef FEAT_CMDHIST
6716 int type;
6717 int idx;
6718 char_u *str;
6719
6720 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6721 if (str == NULL)
6722 rettv->vval.v_string = NULL;
6723 else
6724 {
6725 type = get_histtype(str);
6726 if (argvars[1].v_type == VAR_UNKNOWN)
6727 idx = get_history_idx(type);
6728 else
6729 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6730 /* -1 on type error */
6731 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6732 }
6733#else
6734 rettv->vval.v_string = NULL;
6735#endif
6736 rettv->v_type = VAR_STRING;
6737}
6738
6739/*
6740 * "histnr()" function
6741 */
6742 static void
6743f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6744{
6745 int i;
6746
6747#ifdef FEAT_CMDHIST
6748 char_u *history = get_tv_string_chk(&argvars[0]);
6749
6750 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6751 if (i >= HIST_CMD && i < HIST_COUNT)
6752 i = get_history_idx(i);
6753 else
6754#endif
6755 i = -1;
6756 rettv->vval.v_number = i;
6757}
6758
6759/*
6760 * "highlightID(name)" function
6761 */
6762 static void
6763f_hlID(typval_T *argvars, typval_T *rettv)
6764{
6765 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6766}
6767
6768/*
6769 * "highlight_exists()" function
6770 */
6771 static void
6772f_hlexists(typval_T *argvars, typval_T *rettv)
6773{
6774 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6775}
6776
6777/*
6778 * "hostname()" function
6779 */
6780 static void
6781f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6782{
6783 char_u hostname[256];
6784
6785 mch_get_host_name(hostname, 256);
6786 rettv->v_type = VAR_STRING;
6787 rettv->vval.v_string = vim_strsave(hostname);
6788}
6789
6790/*
6791 * iconv() function
6792 */
6793 static void
6794f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6795{
6796#ifdef FEAT_MBYTE
6797 char_u buf1[NUMBUFLEN];
6798 char_u buf2[NUMBUFLEN];
6799 char_u *from, *to, *str;
6800 vimconv_T vimconv;
6801#endif
6802
6803 rettv->v_type = VAR_STRING;
6804 rettv->vval.v_string = NULL;
6805
6806#ifdef FEAT_MBYTE
6807 str = get_tv_string(&argvars[0]);
6808 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6809 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6810 vimconv.vc_type = CONV_NONE;
6811 convert_setup(&vimconv, from, to);
6812
6813 /* If the encodings are equal, no conversion needed. */
6814 if (vimconv.vc_type == CONV_NONE)
6815 rettv->vval.v_string = vim_strsave(str);
6816 else
6817 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6818
6819 convert_setup(&vimconv, NULL, NULL);
6820 vim_free(from);
6821 vim_free(to);
6822#endif
6823}
6824
6825/*
6826 * "indent()" function
6827 */
6828 static void
6829f_indent(typval_T *argvars, typval_T *rettv)
6830{
6831 linenr_T lnum;
6832
6833 lnum = get_tv_lnum(argvars);
6834 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6835 rettv->vval.v_number = get_indent_lnum(lnum);
6836 else
6837 rettv->vval.v_number = -1;
6838}
6839
6840/*
6841 * "index()" function
6842 */
6843 static void
6844f_index(typval_T *argvars, typval_T *rettv)
6845{
6846 list_T *l;
6847 listitem_T *item;
6848 long idx = 0;
6849 int ic = FALSE;
6850
6851 rettv->vval.v_number = -1;
6852 if (argvars[0].v_type != VAR_LIST)
6853 {
6854 EMSG(_(e_listreq));
6855 return;
6856 }
6857 l = argvars[0].vval.v_list;
6858 if (l != NULL)
6859 {
6860 item = l->lv_first;
6861 if (argvars[2].v_type != VAR_UNKNOWN)
6862 {
6863 int error = FALSE;
6864
6865 /* Start at specified item. Use the cached index that list_find()
6866 * sets, so that a negative number also works. */
6867 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6868 idx = l->lv_idx;
6869 if (argvars[3].v_type != VAR_UNKNOWN)
6870 ic = (int)get_tv_number_chk(&argvars[3], &error);
6871 if (error)
6872 item = NULL;
6873 }
6874
6875 for ( ; item != NULL; item = item->li_next, ++idx)
6876 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6877 {
6878 rettv->vval.v_number = idx;
6879 break;
6880 }
6881 }
6882}
6883
6884static int inputsecret_flag = 0;
6885
6886/*
6887 * "input()" function
6888 * Also handles inputsecret() when inputsecret is set.
6889 */
6890 static void
6891f_input(typval_T *argvars, typval_T *rettv)
6892{
6893 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6894}
6895
6896/*
6897 * "inputdialog()" function
6898 */
6899 static void
6900f_inputdialog(typval_T *argvars, typval_T *rettv)
6901{
6902#if defined(FEAT_GUI_TEXTDIALOG)
6903 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6904 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6905 {
6906 char_u *message;
6907 char_u buf[NUMBUFLEN];
6908 char_u *defstr = (char_u *)"";
6909
6910 message = get_tv_string_chk(&argvars[0]);
6911 if (argvars[1].v_type != VAR_UNKNOWN
6912 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6913 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6914 else
6915 IObuff[0] = NUL;
6916 if (message != NULL && defstr != NULL
6917 && do_dialog(VIM_QUESTION, NULL, message,
6918 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6919 rettv->vval.v_string = vim_strsave(IObuff);
6920 else
6921 {
6922 if (message != NULL && defstr != NULL
6923 && argvars[1].v_type != VAR_UNKNOWN
6924 && argvars[2].v_type != VAR_UNKNOWN)
6925 rettv->vval.v_string = vim_strsave(
6926 get_tv_string_buf(&argvars[2], buf));
6927 else
6928 rettv->vval.v_string = NULL;
6929 }
6930 rettv->v_type = VAR_STRING;
6931 }
6932 else
6933#endif
6934 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6935}
6936
6937/*
6938 * "inputlist()" function
6939 */
6940 static void
6941f_inputlist(typval_T *argvars, typval_T *rettv)
6942{
6943 listitem_T *li;
6944 int selected;
6945 int mouse_used;
6946
6947#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006948 /* While starting up, there is no place to enter text. When running tests
6949 * with --not-a-term we assume feedkeys() will be used. */
6950 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006951 return;
6952#endif
6953 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6954 {
6955 EMSG2(_(e_listarg), "inputlist()");
6956 return;
6957 }
6958
6959 msg_start();
6960 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6961 lines_left = Rows; /* avoid more prompt */
6962 msg_scroll = TRUE;
6963 msg_clr_eos();
6964
6965 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6966 {
6967 msg_puts(get_tv_string(&li->li_tv));
6968 msg_putchar('\n');
6969 }
6970
6971 /* Ask for choice. */
6972 selected = prompt_for_number(&mouse_used);
6973 if (mouse_used)
6974 selected -= lines_left;
6975
6976 rettv->vval.v_number = selected;
6977}
6978
6979
6980static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6981
6982/*
6983 * "inputrestore()" function
6984 */
6985 static void
6986f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6987{
6988 if (ga_userinput.ga_len > 0)
6989 {
6990 --ga_userinput.ga_len;
6991 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6992 + ga_userinput.ga_len);
6993 /* default return is zero == OK */
6994 }
6995 else if (p_verbose > 1)
6996 {
6997 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6998 rettv->vval.v_number = 1; /* Failed */
6999 }
7000}
7001
7002/*
7003 * "inputsave()" function
7004 */
7005 static void
7006f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7007{
7008 /* Add an entry to the stack of typeahead storage. */
7009 if (ga_grow(&ga_userinput, 1) == OK)
7010 {
7011 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7012 + ga_userinput.ga_len);
7013 ++ga_userinput.ga_len;
7014 /* default return is zero == OK */
7015 }
7016 else
7017 rettv->vval.v_number = 1; /* Failed */
7018}
7019
7020/*
7021 * "inputsecret()" function
7022 */
7023 static void
7024f_inputsecret(typval_T *argvars, typval_T *rettv)
7025{
7026 ++cmdline_star;
7027 ++inputsecret_flag;
7028 f_input(argvars, rettv);
7029 --cmdline_star;
7030 --inputsecret_flag;
7031}
7032
7033/*
7034 * "insert()" function
7035 */
7036 static void
7037f_insert(typval_T *argvars, typval_T *rettv)
7038{
7039 long before = 0;
7040 listitem_T *item;
7041 list_T *l;
7042 int error = FALSE;
7043
7044 if (argvars[0].v_type != VAR_LIST)
7045 EMSG2(_(e_listarg), "insert()");
7046 else if ((l = argvars[0].vval.v_list) != NULL
7047 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7048 {
7049 if (argvars[2].v_type != VAR_UNKNOWN)
7050 before = (long)get_tv_number_chk(&argvars[2], &error);
7051 if (error)
7052 return; /* type error; errmsg already given */
7053
7054 if (before == l->lv_len)
7055 item = NULL;
7056 else
7057 {
7058 item = list_find(l, before);
7059 if (item == NULL)
7060 {
7061 EMSGN(_(e_listidx), before);
7062 l = NULL;
7063 }
7064 }
7065 if (l != NULL)
7066 {
7067 list_insert_tv(l, &argvars[1], item);
7068 copy_tv(&argvars[0], rettv);
7069 }
7070 }
7071}
7072
7073/*
7074 * "invert(expr)" function
7075 */
7076 static void
7077f_invert(typval_T *argvars, typval_T *rettv)
7078{
7079 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
7080}
7081
7082/*
7083 * "isdirectory()" function
7084 */
7085 static void
7086f_isdirectory(typval_T *argvars, typval_T *rettv)
7087{
7088 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
7089}
7090
7091/*
7092 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7093 * or it refers to a List or Dictionary that is locked.
7094 */
7095 static int
7096tv_islocked(typval_T *tv)
7097{
7098 return (tv->v_lock & VAR_LOCKED)
7099 || (tv->v_type == VAR_LIST
7100 && tv->vval.v_list != NULL
7101 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7102 || (tv->v_type == VAR_DICT
7103 && tv->vval.v_dict != NULL
7104 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7105}
7106
7107/*
7108 * "islocked()" function
7109 */
7110 static void
7111f_islocked(typval_T *argvars, typval_T *rettv)
7112{
7113 lval_T lv;
7114 char_u *end;
7115 dictitem_T *di;
7116
7117 rettv->vval.v_number = -1;
7118 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007119 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007120 if (end != NULL && lv.ll_name != NULL)
7121 {
7122 if (*end != NUL)
7123 EMSG(_(e_trailing));
7124 else
7125 {
7126 if (lv.ll_tv == NULL)
7127 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007128 di = find_var(lv.ll_name, NULL, TRUE);
7129 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007130 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007131 /* Consider a variable locked when:
7132 * 1. the variable itself is locked
7133 * 2. the value of the variable is locked.
7134 * 3. the List or Dict value is locked.
7135 */
7136 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7137 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007138 }
7139 }
7140 else if (lv.ll_range)
7141 EMSG(_("E786: Range not allowed"));
7142 else if (lv.ll_newkey != NULL)
7143 EMSG2(_(e_dictkey), lv.ll_newkey);
7144 else if (lv.ll_list != NULL)
7145 /* List item. */
7146 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7147 else
7148 /* Dictionary item. */
7149 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7150 }
7151 }
7152
7153 clear_lval(&lv);
7154}
7155
7156#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7157/*
7158 * "isnan()" function
7159 */
7160 static void
7161f_isnan(typval_T *argvars, typval_T *rettv)
7162{
7163 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7164 && isnan(argvars[0].vval.v_float);
7165}
7166#endif
7167
7168/*
7169 * "items(dict)" function
7170 */
7171 static void
7172f_items(typval_T *argvars, typval_T *rettv)
7173{
7174 dict_list(argvars, rettv, 2);
7175}
7176
7177#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7178/*
7179 * Get the job from the argument.
7180 * Returns NULL if the job is invalid.
7181 */
7182 static job_T *
7183get_job_arg(typval_T *tv)
7184{
7185 job_T *job;
7186
7187 if (tv->v_type != VAR_JOB)
7188 {
7189 EMSG2(_(e_invarg2), get_tv_string(tv));
7190 return NULL;
7191 }
7192 job = tv->vval.v_job;
7193
7194 if (job == NULL)
7195 EMSG(_("E916: not a valid job"));
7196 return job;
7197}
7198
7199/*
7200 * "job_getchannel()" function
7201 */
7202 static void
7203f_job_getchannel(typval_T *argvars, typval_T *rettv)
7204{
7205 job_T *job = get_job_arg(&argvars[0]);
7206
7207 if (job != NULL)
7208 {
7209 rettv->v_type = VAR_CHANNEL;
7210 rettv->vval.v_channel = job->jv_channel;
7211 if (job->jv_channel != NULL)
7212 ++job->jv_channel->ch_refcount;
7213 }
7214}
7215
7216/*
7217 * "job_info()" function
7218 */
7219 static void
7220f_job_info(typval_T *argvars, typval_T *rettv)
7221{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007222 if (argvars[0].v_type != VAR_UNKNOWN)
7223 {
7224 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007225
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007226 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7227 job_info(job, rettv->vval.v_dict);
7228 }
7229 else if (rettv_list_alloc(rettv) == OK)
7230 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007231}
7232
7233/*
7234 * "job_setoptions()" function
7235 */
7236 static void
7237f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7238{
7239 job_T *job = get_job_arg(&argvars[0]);
7240 jobopt_T opt;
7241
7242 if (job == NULL)
7243 return;
7244 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007245 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007246 job_set_options(job, &opt);
7247 free_job_options(&opt);
7248}
7249
7250/*
7251 * "job_start()" function
7252 */
7253 static void
7254f_job_start(typval_T *argvars, typval_T *rettv)
7255{
7256 rettv->v_type = VAR_JOB;
7257 if (check_restricted() || check_secure())
7258 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007259 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007260}
7261
7262/*
7263 * "job_status()" function
7264 */
7265 static void
7266f_job_status(typval_T *argvars, typval_T *rettv)
7267{
7268 job_T *job = get_job_arg(&argvars[0]);
7269
7270 if (job != NULL)
7271 {
7272 rettv->v_type = VAR_STRING;
7273 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7274 }
7275}
7276
7277/*
7278 * "job_stop()" function
7279 */
7280 static void
7281f_job_stop(typval_T *argvars, typval_T *rettv)
7282{
7283 job_T *job = get_job_arg(&argvars[0]);
7284
7285 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007286 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007287}
7288#endif
7289
7290/*
7291 * "join()" function
7292 */
7293 static void
7294f_join(typval_T *argvars, typval_T *rettv)
7295{
7296 garray_T ga;
7297 char_u *sep;
7298
7299 if (argvars[0].v_type != VAR_LIST)
7300 {
7301 EMSG(_(e_listreq));
7302 return;
7303 }
7304 if (argvars[0].vval.v_list == NULL)
7305 return;
7306 if (argvars[1].v_type == VAR_UNKNOWN)
7307 sep = (char_u *)" ";
7308 else
7309 sep = get_tv_string_chk(&argvars[1]);
7310
7311 rettv->v_type = VAR_STRING;
7312
7313 if (sep != NULL)
7314 {
7315 ga_init2(&ga, (int)sizeof(char), 80);
7316 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7317 ga_append(&ga, NUL);
7318 rettv->vval.v_string = (char_u *)ga.ga_data;
7319 }
7320 else
7321 rettv->vval.v_string = NULL;
7322}
7323
7324/*
7325 * "js_decode()" function
7326 */
7327 static void
7328f_js_decode(typval_T *argvars, typval_T *rettv)
7329{
7330 js_read_T reader;
7331
7332 reader.js_buf = get_tv_string(&argvars[0]);
7333 reader.js_fill = NULL;
7334 reader.js_used = 0;
7335 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7336 EMSG(_(e_invarg));
7337}
7338
7339/*
7340 * "js_encode()" function
7341 */
7342 static void
7343f_js_encode(typval_T *argvars, typval_T *rettv)
7344{
7345 rettv->v_type = VAR_STRING;
7346 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7347}
7348
7349/*
7350 * "json_decode()" function
7351 */
7352 static void
7353f_json_decode(typval_T *argvars, typval_T *rettv)
7354{
7355 js_read_T reader;
7356
7357 reader.js_buf = get_tv_string(&argvars[0]);
7358 reader.js_fill = NULL;
7359 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007360 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007361}
7362
7363/*
7364 * "json_encode()" function
7365 */
7366 static void
7367f_json_encode(typval_T *argvars, typval_T *rettv)
7368{
7369 rettv->v_type = VAR_STRING;
7370 rettv->vval.v_string = json_encode(&argvars[0], 0);
7371}
7372
7373/*
7374 * "keys()" function
7375 */
7376 static void
7377f_keys(typval_T *argvars, typval_T *rettv)
7378{
7379 dict_list(argvars, rettv, 0);
7380}
7381
7382/*
7383 * "last_buffer_nr()" function.
7384 */
7385 static void
7386f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7387{
7388 int n = 0;
7389 buf_T *buf;
7390
Bram Moolenaar29323592016-07-24 22:04:11 +02007391 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007392 if (n < buf->b_fnum)
7393 n = buf->b_fnum;
7394
7395 rettv->vval.v_number = n;
7396}
7397
7398/*
7399 * "len()" function
7400 */
7401 static void
7402f_len(typval_T *argvars, typval_T *rettv)
7403{
7404 switch (argvars[0].v_type)
7405 {
7406 case VAR_STRING:
7407 case VAR_NUMBER:
7408 rettv->vval.v_number = (varnumber_T)STRLEN(
7409 get_tv_string(&argvars[0]));
7410 break;
7411 case VAR_LIST:
7412 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7413 break;
7414 case VAR_DICT:
7415 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7416 break;
7417 case VAR_UNKNOWN:
7418 case VAR_SPECIAL:
7419 case VAR_FLOAT:
7420 case VAR_FUNC:
7421 case VAR_PARTIAL:
7422 case VAR_JOB:
7423 case VAR_CHANNEL:
7424 EMSG(_("E701: Invalid type for len()"));
7425 break;
7426 }
7427}
7428
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007429 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007430libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007431{
7432#ifdef FEAT_LIBCALL
7433 char_u *string_in;
7434 char_u **string_result;
7435 int nr_result;
7436#endif
7437
7438 rettv->v_type = type;
7439 if (type != VAR_NUMBER)
7440 rettv->vval.v_string = NULL;
7441
7442 if (check_restricted() || check_secure())
7443 return;
7444
7445#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007446 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007447 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7448 {
7449 string_in = NULL;
7450 if (argvars[2].v_type == VAR_STRING)
7451 string_in = argvars[2].vval.v_string;
7452 if (type == VAR_NUMBER)
7453 string_result = NULL;
7454 else
7455 string_result = &rettv->vval.v_string;
7456 if (mch_libcall(argvars[0].vval.v_string,
7457 argvars[1].vval.v_string,
7458 string_in,
7459 argvars[2].vval.v_number,
7460 string_result,
7461 &nr_result) == OK
7462 && type == VAR_NUMBER)
7463 rettv->vval.v_number = nr_result;
7464 }
7465#endif
7466}
7467
7468/*
7469 * "libcall()" function
7470 */
7471 static void
7472f_libcall(typval_T *argvars, typval_T *rettv)
7473{
7474 libcall_common(argvars, rettv, VAR_STRING);
7475}
7476
7477/*
7478 * "libcallnr()" function
7479 */
7480 static void
7481f_libcallnr(typval_T *argvars, typval_T *rettv)
7482{
7483 libcall_common(argvars, rettv, VAR_NUMBER);
7484}
7485
7486/*
7487 * "line(string)" function
7488 */
7489 static void
7490f_line(typval_T *argvars, typval_T *rettv)
7491{
7492 linenr_T lnum = 0;
7493 pos_T *fp;
7494 int fnum;
7495
7496 fp = var2fpos(&argvars[0], TRUE, &fnum);
7497 if (fp != NULL)
7498 lnum = fp->lnum;
7499 rettv->vval.v_number = lnum;
7500}
7501
7502/*
7503 * "line2byte(lnum)" function
7504 */
7505 static void
7506f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7507{
7508#ifndef FEAT_BYTEOFF
7509 rettv->vval.v_number = -1;
7510#else
7511 linenr_T lnum;
7512
7513 lnum = get_tv_lnum(argvars);
7514 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7515 rettv->vval.v_number = -1;
7516 else
7517 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7518 if (rettv->vval.v_number >= 0)
7519 ++rettv->vval.v_number;
7520#endif
7521}
7522
7523/*
7524 * "lispindent(lnum)" function
7525 */
7526 static void
7527f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7528{
7529#ifdef FEAT_LISP
7530 pos_T pos;
7531 linenr_T lnum;
7532
7533 pos = curwin->w_cursor;
7534 lnum = get_tv_lnum(argvars);
7535 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7536 {
7537 curwin->w_cursor.lnum = lnum;
7538 rettv->vval.v_number = get_lisp_indent();
7539 curwin->w_cursor = pos;
7540 }
7541 else
7542#endif
7543 rettv->vval.v_number = -1;
7544}
7545
7546/*
7547 * "localtime()" function
7548 */
7549 static void
7550f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7551{
7552 rettv->vval.v_number = (varnumber_T)time(NULL);
7553}
7554
7555static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7556
7557 static void
7558get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7559{
7560 char_u *keys;
7561 char_u *which;
7562 char_u buf[NUMBUFLEN];
7563 char_u *keys_buf = NULL;
7564 char_u *rhs;
7565 int mode;
7566 int abbr = FALSE;
7567 int get_dict = FALSE;
7568 mapblock_T *mp;
7569 int buffer_local;
7570
7571 /* return empty string for failure */
7572 rettv->v_type = VAR_STRING;
7573 rettv->vval.v_string = NULL;
7574
7575 keys = get_tv_string(&argvars[0]);
7576 if (*keys == NUL)
7577 return;
7578
7579 if (argvars[1].v_type != VAR_UNKNOWN)
7580 {
7581 which = get_tv_string_buf_chk(&argvars[1], buf);
7582 if (argvars[2].v_type != VAR_UNKNOWN)
7583 {
7584 abbr = (int)get_tv_number(&argvars[2]);
7585 if (argvars[3].v_type != VAR_UNKNOWN)
7586 get_dict = (int)get_tv_number(&argvars[3]);
7587 }
7588 }
7589 else
7590 which = (char_u *)"";
7591 if (which == NULL)
7592 return;
7593
7594 mode = get_map_mode(&which, 0);
7595
7596 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7597 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7598 vim_free(keys_buf);
7599
7600 if (!get_dict)
7601 {
7602 /* Return a string. */
7603 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007604 {
7605 if (*rhs == NUL)
7606 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7607 else
7608 rettv->vval.v_string = str2special_save(rhs, FALSE);
7609 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007610
7611 }
7612 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7613 {
7614 /* Return a dictionary. */
7615 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7616 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7617 dict_T *dict = rettv->vval.v_dict;
7618
7619 dict_add_nr_str(dict, "lhs", 0L, lhs);
7620 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7621 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7622 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7623 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7624 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7625 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7626 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7627 dict_add_nr_str(dict, "mode", 0L, mapmode);
7628
7629 vim_free(lhs);
7630 vim_free(mapmode);
7631 }
7632}
7633
7634#ifdef FEAT_FLOAT
7635/*
7636 * "log()" function
7637 */
7638 static void
7639f_log(typval_T *argvars, typval_T *rettv)
7640{
7641 float_T f = 0.0;
7642
7643 rettv->v_type = VAR_FLOAT;
7644 if (get_float_arg(argvars, &f) == OK)
7645 rettv->vval.v_float = log(f);
7646 else
7647 rettv->vval.v_float = 0.0;
7648}
7649
7650/*
7651 * "log10()" function
7652 */
7653 static void
7654f_log10(typval_T *argvars, typval_T *rettv)
7655{
7656 float_T f = 0.0;
7657
7658 rettv->v_type = VAR_FLOAT;
7659 if (get_float_arg(argvars, &f) == OK)
7660 rettv->vval.v_float = log10(f);
7661 else
7662 rettv->vval.v_float = 0.0;
7663}
7664#endif
7665
7666#ifdef FEAT_LUA
7667/*
7668 * "luaeval()" function
7669 */
7670 static void
7671f_luaeval(typval_T *argvars, typval_T *rettv)
7672{
7673 char_u *str;
7674 char_u buf[NUMBUFLEN];
7675
7676 str = get_tv_string_buf(&argvars[0], buf);
7677 do_luaeval(str, argvars + 1, rettv);
7678}
7679#endif
7680
7681/*
7682 * "map()" function
7683 */
7684 static void
7685f_map(typval_T *argvars, typval_T *rettv)
7686{
7687 filter_map(argvars, rettv, TRUE);
7688}
7689
7690/*
7691 * "maparg()" function
7692 */
7693 static void
7694f_maparg(typval_T *argvars, typval_T *rettv)
7695{
7696 get_maparg(argvars, rettv, TRUE);
7697}
7698
7699/*
7700 * "mapcheck()" function
7701 */
7702 static void
7703f_mapcheck(typval_T *argvars, typval_T *rettv)
7704{
7705 get_maparg(argvars, rettv, FALSE);
7706}
7707
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007708typedef enum
7709{
7710 MATCH_END, /* matchend() */
7711 MATCH_MATCH, /* match() */
7712 MATCH_STR, /* matchstr() */
7713 MATCH_LIST, /* matchlist() */
7714 MATCH_POS /* matchstrpos() */
7715} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007716
7717 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007718find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007719{
7720 char_u *str = NULL;
7721 long len = 0;
7722 char_u *expr = NULL;
7723 char_u *pat;
7724 regmatch_T regmatch;
7725 char_u patbuf[NUMBUFLEN];
7726 char_u strbuf[NUMBUFLEN];
7727 char_u *save_cpo;
7728 long start = 0;
7729 long nth = 1;
7730 colnr_T startcol = 0;
7731 int match = 0;
7732 list_T *l = NULL;
7733 listitem_T *li = NULL;
7734 long idx = 0;
7735 char_u *tofree = NULL;
7736
7737 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7738 save_cpo = p_cpo;
7739 p_cpo = (char_u *)"";
7740
7741 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007742 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007743 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007744 /* type MATCH_LIST: return empty list when there are no matches.
7745 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007746 if (rettv_list_alloc(rettv) == FAIL)
7747 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007748 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007749 && (list_append_string(rettv->vval.v_list,
7750 (char_u *)"", 0) == FAIL
7751 || list_append_number(rettv->vval.v_list,
7752 (varnumber_T)-1) == FAIL
7753 || list_append_number(rettv->vval.v_list,
7754 (varnumber_T)-1) == FAIL
7755 || list_append_number(rettv->vval.v_list,
7756 (varnumber_T)-1) == FAIL))
7757 {
7758 list_free(rettv->vval.v_list);
7759 rettv->vval.v_list = NULL;
7760 goto theend;
7761 }
7762 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007763 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007764 {
7765 rettv->v_type = VAR_STRING;
7766 rettv->vval.v_string = NULL;
7767 }
7768
7769 if (argvars[0].v_type == VAR_LIST)
7770 {
7771 if ((l = argvars[0].vval.v_list) == NULL)
7772 goto theend;
7773 li = l->lv_first;
7774 }
7775 else
7776 {
7777 expr = str = get_tv_string(&argvars[0]);
7778 len = (long)STRLEN(str);
7779 }
7780
7781 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7782 if (pat == NULL)
7783 goto theend;
7784
7785 if (argvars[2].v_type != VAR_UNKNOWN)
7786 {
7787 int error = FALSE;
7788
7789 start = (long)get_tv_number_chk(&argvars[2], &error);
7790 if (error)
7791 goto theend;
7792 if (l != NULL)
7793 {
7794 li = list_find(l, start);
7795 if (li == NULL)
7796 goto theend;
7797 idx = l->lv_idx; /* use the cached index */
7798 }
7799 else
7800 {
7801 if (start < 0)
7802 start = 0;
7803 if (start > len)
7804 goto theend;
7805 /* When "count" argument is there ignore matches before "start",
7806 * otherwise skip part of the string. Differs when pattern is "^"
7807 * or "\<". */
7808 if (argvars[3].v_type != VAR_UNKNOWN)
7809 startcol = start;
7810 else
7811 {
7812 str += start;
7813 len -= start;
7814 }
7815 }
7816
7817 if (argvars[3].v_type != VAR_UNKNOWN)
7818 nth = (long)get_tv_number_chk(&argvars[3], &error);
7819 if (error)
7820 goto theend;
7821 }
7822
7823 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7824 if (regmatch.regprog != NULL)
7825 {
7826 regmatch.rm_ic = p_ic;
7827
7828 for (;;)
7829 {
7830 if (l != NULL)
7831 {
7832 if (li == NULL)
7833 {
7834 match = FALSE;
7835 break;
7836 }
7837 vim_free(tofree);
7838 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7839 if (str == NULL)
7840 break;
7841 }
7842
7843 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7844
7845 if (match && --nth <= 0)
7846 break;
7847 if (l == NULL && !match)
7848 break;
7849
7850 /* Advance to just after the match. */
7851 if (l != NULL)
7852 {
7853 li = li->li_next;
7854 ++idx;
7855 }
7856 else
7857 {
7858#ifdef FEAT_MBYTE
7859 startcol = (colnr_T)(regmatch.startp[0]
7860 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7861#else
7862 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7863#endif
7864 if (startcol > (colnr_T)len
7865 || str + startcol <= regmatch.startp[0])
7866 {
7867 match = FALSE;
7868 break;
7869 }
7870 }
7871 }
7872
7873 if (match)
7874 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007875 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007876 {
7877 listitem_T *li1 = rettv->vval.v_list->lv_first;
7878 listitem_T *li2 = li1->li_next;
7879 listitem_T *li3 = li2->li_next;
7880 listitem_T *li4 = li3->li_next;
7881
7882 vim_free(li1->li_tv.vval.v_string);
7883 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7884 (int)(regmatch.endp[0] - regmatch.startp[0]));
7885 li3->li_tv.vval.v_number =
7886 (varnumber_T)(regmatch.startp[0] - expr);
7887 li4->li_tv.vval.v_number =
7888 (varnumber_T)(regmatch.endp[0] - expr);
7889 if (l != NULL)
7890 li2->li_tv.vval.v_number = (varnumber_T)idx;
7891 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007892 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007893 {
7894 int i;
7895
7896 /* return list with matched string and submatches */
7897 for (i = 0; i < NSUBEXP; ++i)
7898 {
7899 if (regmatch.endp[i] == NULL)
7900 {
7901 if (list_append_string(rettv->vval.v_list,
7902 (char_u *)"", 0) == FAIL)
7903 break;
7904 }
7905 else if (list_append_string(rettv->vval.v_list,
7906 regmatch.startp[i],
7907 (int)(regmatch.endp[i] - regmatch.startp[i]))
7908 == FAIL)
7909 break;
7910 }
7911 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007912 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007913 {
7914 /* return matched string */
7915 if (l != NULL)
7916 copy_tv(&li->li_tv, rettv);
7917 else
7918 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7919 (int)(regmatch.endp[0] - regmatch.startp[0]));
7920 }
7921 else if (l != NULL)
7922 rettv->vval.v_number = idx;
7923 else
7924 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007925 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007926 rettv->vval.v_number =
7927 (varnumber_T)(regmatch.startp[0] - str);
7928 else
7929 rettv->vval.v_number =
7930 (varnumber_T)(regmatch.endp[0] - str);
7931 rettv->vval.v_number += (varnumber_T)(str - expr);
7932 }
7933 }
7934 vim_regfree(regmatch.regprog);
7935 }
7936
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007937theend:
7938 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007939 /* matchstrpos() without a list: drop the second item. */
7940 listitem_remove(rettv->vval.v_list,
7941 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007942 vim_free(tofree);
7943 p_cpo = save_cpo;
7944}
7945
7946/*
7947 * "match()" function
7948 */
7949 static void
7950f_match(typval_T *argvars, typval_T *rettv)
7951{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007952 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007953}
7954
7955/*
7956 * "matchadd()" function
7957 */
7958 static void
7959f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7960{
7961#ifdef FEAT_SEARCH_EXTRA
7962 char_u buf[NUMBUFLEN];
7963 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7964 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7965 int prio = 10; /* default priority */
7966 int id = -1;
7967 int error = FALSE;
7968 char_u *conceal_char = NULL;
7969
7970 rettv->vval.v_number = -1;
7971
7972 if (grp == NULL || pat == NULL)
7973 return;
7974 if (argvars[2].v_type != VAR_UNKNOWN)
7975 {
7976 prio = (int)get_tv_number_chk(&argvars[2], &error);
7977 if (argvars[3].v_type != VAR_UNKNOWN)
7978 {
7979 id = (int)get_tv_number_chk(&argvars[3], &error);
7980 if (argvars[4].v_type != VAR_UNKNOWN)
7981 {
7982 if (argvars[4].v_type != VAR_DICT)
7983 {
7984 EMSG(_(e_dictreq));
7985 return;
7986 }
7987 if (dict_find(argvars[4].vval.v_dict,
7988 (char_u *)"conceal", -1) != NULL)
7989 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7990 (char_u *)"conceal", FALSE);
7991 }
7992 }
7993 }
7994 if (error == TRUE)
7995 return;
7996 if (id >= 1 && id <= 3)
7997 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007998 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007999 return;
8000 }
8001
8002 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
8003 conceal_char);
8004#endif
8005}
8006
8007/*
8008 * "matchaddpos()" function
8009 */
8010 static void
8011f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8012{
8013#ifdef FEAT_SEARCH_EXTRA
8014 char_u buf[NUMBUFLEN];
8015 char_u *group;
8016 int prio = 10;
8017 int id = -1;
8018 int error = FALSE;
8019 list_T *l;
8020 char_u *conceal_char = NULL;
8021
8022 rettv->vval.v_number = -1;
8023
8024 group = get_tv_string_buf_chk(&argvars[0], buf);
8025 if (group == NULL)
8026 return;
8027
8028 if (argvars[1].v_type != VAR_LIST)
8029 {
8030 EMSG2(_(e_listarg), "matchaddpos()");
8031 return;
8032 }
8033 l = argvars[1].vval.v_list;
8034 if (l == NULL)
8035 return;
8036
8037 if (argvars[2].v_type != VAR_UNKNOWN)
8038 {
8039 prio = (int)get_tv_number_chk(&argvars[2], &error);
8040 if (argvars[3].v_type != VAR_UNKNOWN)
8041 {
8042 id = (int)get_tv_number_chk(&argvars[3], &error);
8043 if (argvars[4].v_type != VAR_UNKNOWN)
8044 {
8045 if (argvars[4].v_type != VAR_DICT)
8046 {
8047 EMSG(_(e_dictreq));
8048 return;
8049 }
8050 if (dict_find(argvars[4].vval.v_dict,
8051 (char_u *)"conceal", -1) != NULL)
8052 conceal_char = get_dict_string(argvars[4].vval.v_dict,
8053 (char_u *)"conceal", FALSE);
8054 }
8055 }
8056 }
8057 if (error == TRUE)
8058 return;
8059
8060 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8061 if (id == 1 || id == 2)
8062 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008063 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008064 return;
8065 }
8066
8067 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
8068 conceal_char);
8069#endif
8070}
8071
8072/*
8073 * "matcharg()" function
8074 */
8075 static void
8076f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8077{
8078 if (rettv_list_alloc(rettv) == OK)
8079 {
8080#ifdef FEAT_SEARCH_EXTRA
8081 int id = (int)get_tv_number(&argvars[0]);
8082 matchitem_T *m;
8083
8084 if (id >= 1 && id <= 3)
8085 {
8086 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8087 {
8088 list_append_string(rettv->vval.v_list,
8089 syn_id2name(m->hlg_id), -1);
8090 list_append_string(rettv->vval.v_list, m->pattern, -1);
8091 }
8092 else
8093 {
8094 list_append_string(rettv->vval.v_list, NULL, -1);
8095 list_append_string(rettv->vval.v_list, NULL, -1);
8096 }
8097 }
8098#endif
8099 }
8100}
8101
8102/*
8103 * "matchdelete()" function
8104 */
8105 static void
8106f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8107{
8108#ifdef FEAT_SEARCH_EXTRA
8109 rettv->vval.v_number = match_delete(curwin,
8110 (int)get_tv_number(&argvars[0]), TRUE);
8111#endif
8112}
8113
8114/*
8115 * "matchend()" function
8116 */
8117 static void
8118f_matchend(typval_T *argvars, typval_T *rettv)
8119{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008120 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008121}
8122
8123/*
8124 * "matchlist()" function
8125 */
8126 static void
8127f_matchlist(typval_T *argvars, typval_T *rettv)
8128{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008129 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130}
8131
8132/*
8133 * "matchstr()" function
8134 */
8135 static void
8136f_matchstr(typval_T *argvars, typval_T *rettv)
8137{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008138 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008139}
8140
8141/*
8142 * "matchstrpos()" function
8143 */
8144 static void
8145f_matchstrpos(typval_T *argvars, typval_T *rettv)
8146{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008147 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008148}
8149
8150static void max_min(typval_T *argvars, typval_T *rettv, int domax);
8151
8152 static void
8153max_min(typval_T *argvars, typval_T *rettv, int domax)
8154{
8155 varnumber_T n = 0;
8156 varnumber_T i;
8157 int error = FALSE;
8158
8159 if (argvars[0].v_type == VAR_LIST)
8160 {
8161 list_T *l;
8162 listitem_T *li;
8163
8164 l = argvars[0].vval.v_list;
8165 if (l != NULL)
8166 {
8167 li = l->lv_first;
8168 if (li != NULL)
8169 {
8170 n = get_tv_number_chk(&li->li_tv, &error);
8171 for (;;)
8172 {
8173 li = li->li_next;
8174 if (li == NULL)
8175 break;
8176 i = get_tv_number_chk(&li->li_tv, &error);
8177 if (domax ? i > n : i < n)
8178 n = i;
8179 }
8180 }
8181 }
8182 }
8183 else if (argvars[0].v_type == VAR_DICT)
8184 {
8185 dict_T *d;
8186 int first = TRUE;
8187 hashitem_T *hi;
8188 int todo;
8189
8190 d = argvars[0].vval.v_dict;
8191 if (d != NULL)
8192 {
8193 todo = (int)d->dv_hashtab.ht_used;
8194 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8195 {
8196 if (!HASHITEM_EMPTY(hi))
8197 {
8198 --todo;
8199 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
8200 if (first)
8201 {
8202 n = i;
8203 first = FALSE;
8204 }
8205 else if (domax ? i > n : i < n)
8206 n = i;
8207 }
8208 }
8209 }
8210 }
8211 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008212 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008213 rettv->vval.v_number = error ? 0 : n;
8214}
8215
8216/*
8217 * "max()" function
8218 */
8219 static void
8220f_max(typval_T *argvars, typval_T *rettv)
8221{
8222 max_min(argvars, rettv, TRUE);
8223}
8224
8225/*
8226 * "min()" function
8227 */
8228 static void
8229f_min(typval_T *argvars, typval_T *rettv)
8230{
8231 max_min(argvars, rettv, FALSE);
8232}
8233
8234static int mkdir_recurse(char_u *dir, int prot);
8235
8236/*
8237 * Create the directory in which "dir" is located, and higher levels when
8238 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008239 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008240 */
8241 static int
8242mkdir_recurse(char_u *dir, int prot)
8243{
8244 char_u *p;
8245 char_u *updir;
8246 int r = FAIL;
8247
8248 /* Get end of directory name in "dir".
8249 * We're done when it's "/" or "c:/". */
8250 p = gettail_sep(dir);
8251 if (p <= get_past_head(dir))
8252 return OK;
8253
8254 /* If the directory exists we're done. Otherwise: create it.*/
8255 updir = vim_strnsave(dir, (int)(p - dir));
8256 if (updir == NULL)
8257 return FAIL;
8258 if (mch_isdir(updir))
8259 r = OK;
8260 else if (mkdir_recurse(updir, prot) == OK)
8261 r = vim_mkdir_emsg(updir, prot);
8262 vim_free(updir);
8263 return r;
8264}
8265
8266#ifdef vim_mkdir
8267/*
8268 * "mkdir()" function
8269 */
8270 static void
8271f_mkdir(typval_T *argvars, typval_T *rettv)
8272{
8273 char_u *dir;
8274 char_u buf[NUMBUFLEN];
8275 int prot = 0755;
8276
8277 rettv->vval.v_number = FAIL;
8278 if (check_restricted() || check_secure())
8279 return;
8280
8281 dir = get_tv_string_buf(&argvars[0], buf);
8282 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008283 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008284
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008285 if (*gettail(dir) == NUL)
8286 /* remove trailing slashes */
8287 *gettail_sep(dir) = NUL;
8288
8289 if (argvars[1].v_type != VAR_UNKNOWN)
8290 {
8291 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008292 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008293 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8294 if (prot == -1)
8295 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008297 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8298 {
8299 if (mch_isdir(dir))
8300 {
8301 /* With the "p" flag it's OK if the dir already exists. */
8302 rettv->vval.v_number = OK;
8303 return;
8304 }
8305 mkdir_recurse(dir, prot);
8306 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008307 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008308 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309}
8310#endif
8311
8312/*
8313 * "mode()" function
8314 */
8315 static void
8316f_mode(typval_T *argvars, typval_T *rettv)
8317{
8318 char_u buf[3];
8319
8320 buf[1] = NUL;
8321 buf[2] = NUL;
8322
8323 if (time_for_testing == 93784)
8324 {
8325 /* Testing the two-character code. */
8326 buf[0] = 'x';
8327 buf[1] = '!';
8328 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008329#ifdef FEAT_TERMINAL
8330 else if (term_use_loop())
8331 buf[0] = 't';
8332#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008333 else if (VIsual_active)
8334 {
8335 if (VIsual_select)
8336 buf[0] = VIsual_mode + 's' - 'v';
8337 else
8338 buf[0] = VIsual_mode;
8339 }
8340 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8341 || State == CONFIRM)
8342 {
8343 buf[0] = 'r';
8344 if (State == ASKMORE)
8345 buf[1] = 'm';
8346 else if (State == CONFIRM)
8347 buf[1] = '?';
8348 }
8349 else if (State == EXTERNCMD)
8350 buf[0] = '!';
8351 else if (State & INSERT)
8352 {
8353#ifdef FEAT_VREPLACE
8354 if (State & VREPLACE_FLAG)
8355 {
8356 buf[0] = 'R';
8357 buf[1] = 'v';
8358 }
8359 else
8360#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008361 {
8362 if (State & REPLACE_FLAG)
8363 buf[0] = 'R';
8364 else
8365 buf[0] = 'i';
8366#ifdef FEAT_INS_EXPAND
8367 if (ins_compl_active())
8368 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008369 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008370 buf[1] = 'x';
8371#endif
8372 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008373 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008374 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008375 {
8376 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008377 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008378 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008379 else if (exmode_active == EXMODE_NORMAL)
8380 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008381 }
8382 else
8383 {
8384 buf[0] = 'n';
8385 if (finish_op)
8386 buf[1] = 'o';
8387 }
8388
8389 /* Clear out the minor mode when the argument is not a non-zero number or
8390 * non-empty string. */
8391 if (!non_zero_arg(&argvars[0]))
8392 buf[1] = NUL;
8393
8394 rettv->vval.v_string = vim_strsave(buf);
8395 rettv->v_type = VAR_STRING;
8396}
8397
8398#if defined(FEAT_MZSCHEME) || defined(PROTO)
8399/*
8400 * "mzeval()" function
8401 */
8402 static void
8403f_mzeval(typval_T *argvars, typval_T *rettv)
8404{
8405 char_u *str;
8406 char_u buf[NUMBUFLEN];
8407
8408 str = get_tv_string_buf(&argvars[0], buf);
8409 do_mzeval(str, rettv);
8410}
8411
8412 void
8413mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8414{
8415 typval_T argvars[3];
8416
8417 argvars[0].v_type = VAR_STRING;
8418 argvars[0].vval.v_string = name;
8419 copy_tv(args, &argvars[1]);
8420 argvars[2].v_type = VAR_UNKNOWN;
8421 f_call(argvars, rettv);
8422 clear_tv(&argvars[1]);
8423}
8424#endif
8425
8426/*
8427 * "nextnonblank()" function
8428 */
8429 static void
8430f_nextnonblank(typval_T *argvars, typval_T *rettv)
8431{
8432 linenr_T lnum;
8433
8434 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8435 {
8436 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8437 {
8438 lnum = 0;
8439 break;
8440 }
8441 if (*skipwhite(ml_get(lnum)) != NUL)
8442 break;
8443 }
8444 rettv->vval.v_number = lnum;
8445}
8446
8447/*
8448 * "nr2char()" function
8449 */
8450 static void
8451f_nr2char(typval_T *argvars, typval_T *rettv)
8452{
8453 char_u buf[NUMBUFLEN];
8454
8455#ifdef FEAT_MBYTE
8456 if (has_mbyte)
8457 {
8458 int utf8 = 0;
8459
8460 if (argvars[1].v_type != VAR_UNKNOWN)
8461 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8462 if (utf8)
8463 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8464 else
8465 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8466 }
8467 else
8468#endif
8469 {
8470 buf[0] = (char_u)get_tv_number(&argvars[0]);
8471 buf[1] = NUL;
8472 }
8473 rettv->v_type = VAR_STRING;
8474 rettv->vval.v_string = vim_strsave(buf);
8475}
8476
8477/*
8478 * "or(expr, expr)" function
8479 */
8480 static void
8481f_or(typval_T *argvars, typval_T *rettv)
8482{
8483 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8484 | get_tv_number_chk(&argvars[1], NULL);
8485}
8486
8487/*
8488 * "pathshorten()" function
8489 */
8490 static void
8491f_pathshorten(typval_T *argvars, typval_T *rettv)
8492{
8493 char_u *p;
8494
8495 rettv->v_type = VAR_STRING;
8496 p = get_tv_string_chk(&argvars[0]);
8497 if (p == NULL)
8498 rettv->vval.v_string = NULL;
8499 else
8500 {
8501 p = vim_strsave(p);
8502 rettv->vval.v_string = p;
8503 if (p != NULL)
8504 shorten_dir(p);
8505 }
8506}
8507
8508#ifdef FEAT_PERL
8509/*
8510 * "perleval()" function
8511 */
8512 static void
8513f_perleval(typval_T *argvars, typval_T *rettv)
8514{
8515 char_u *str;
8516 char_u buf[NUMBUFLEN];
8517
8518 str = get_tv_string_buf(&argvars[0], buf);
8519 do_perleval(str, rettv);
8520}
8521#endif
8522
8523#ifdef FEAT_FLOAT
8524/*
8525 * "pow()" function
8526 */
8527 static void
8528f_pow(typval_T *argvars, typval_T *rettv)
8529{
8530 float_T fx = 0.0, fy = 0.0;
8531
8532 rettv->v_type = VAR_FLOAT;
8533 if (get_float_arg(argvars, &fx) == OK
8534 && get_float_arg(&argvars[1], &fy) == OK)
8535 rettv->vval.v_float = pow(fx, fy);
8536 else
8537 rettv->vval.v_float = 0.0;
8538}
8539#endif
8540
8541/*
8542 * "prevnonblank()" function
8543 */
8544 static void
8545f_prevnonblank(typval_T *argvars, typval_T *rettv)
8546{
8547 linenr_T lnum;
8548
8549 lnum = get_tv_lnum(argvars);
8550 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8551 lnum = 0;
8552 else
8553 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8554 --lnum;
8555 rettv->vval.v_number = lnum;
8556}
8557
8558/* This dummy va_list is here because:
8559 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8560 * - locally in the function results in a "used before set" warning
8561 * - using va_start() to initialize it gives "function with fixed args" error */
8562static va_list ap;
8563
8564/*
8565 * "printf()" function
8566 */
8567 static void
8568f_printf(typval_T *argvars, typval_T *rettv)
8569{
8570 char_u buf[NUMBUFLEN];
8571 int len;
8572 char_u *s;
8573 int saved_did_emsg = did_emsg;
8574 char *fmt;
8575
8576 rettv->v_type = VAR_STRING;
8577 rettv->vval.v_string = NULL;
8578
8579 /* Get the required length, allocate the buffer and do it for real. */
8580 did_emsg = FALSE;
8581 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008582 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583 if (!did_emsg)
8584 {
8585 s = alloc(len + 1);
8586 if (s != NULL)
8587 {
8588 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008589 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8590 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008591 }
8592 }
8593 did_emsg |= saved_did_emsg;
8594}
8595
Bram Moolenaarf2732452018-06-03 14:47:35 +02008596#ifdef FEAT_JOB_CHANNEL
8597/*
8598 * "prompt_setcallback({buffer}, {callback})" function
8599 */
8600 static void
8601f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8602{
8603 buf_T *buf;
8604 char_u *callback;
8605 partial_T *partial;
8606
8607 if (check_secure())
8608 return;
8609 buf = get_buf_tv(&argvars[0], FALSE);
8610 if (buf == NULL)
8611 return;
8612
8613 callback = get_callback(&argvars[1], &partial);
8614 if (callback == NULL)
8615 return;
8616
8617 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8618 if (partial == NULL)
8619 buf->b_prompt_callback = vim_strsave(callback);
8620 else
8621 /* pointer into the partial */
8622 buf->b_prompt_callback = callback;
8623 buf->b_prompt_partial = partial;
8624}
8625
8626/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008627 * "prompt_setinterrupt({buffer}, {callback})" function
8628 */
8629 static void
8630f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8631{
8632 buf_T *buf;
8633 char_u *callback;
8634 partial_T *partial;
8635
8636 if (check_secure())
8637 return;
8638 buf = get_buf_tv(&argvars[0], FALSE);
8639 if (buf == NULL)
8640 return;
8641
8642 callback = get_callback(&argvars[1], &partial);
8643 if (callback == NULL)
8644 return;
8645
8646 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8647 if (partial == NULL)
8648 buf->b_prompt_interrupt = vim_strsave(callback);
8649 else
8650 /* pointer into the partial */
8651 buf->b_prompt_interrupt = callback;
8652 buf->b_prompt_int_partial = partial;
8653}
8654
8655/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008656 * "prompt_setprompt({buffer}, {text})" function
8657 */
8658 static void
8659f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8660{
8661 buf_T *buf;
8662 char_u *text;
8663
8664 if (check_secure())
8665 return;
8666 buf = get_buf_tv(&argvars[0], FALSE);
8667 if (buf == NULL)
8668 return;
8669
8670 text = get_tv_string(&argvars[1]);
8671 vim_free(buf->b_prompt_text);
8672 buf->b_prompt_text = vim_strsave(text);
8673}
8674#endif
8675
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008676/*
8677 * "pumvisible()" function
8678 */
8679 static void
8680f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8681{
8682#ifdef FEAT_INS_EXPAND
8683 if (pum_visible())
8684 rettv->vval.v_number = 1;
8685#endif
8686}
8687
8688#ifdef FEAT_PYTHON3
8689/*
8690 * "py3eval()" function
8691 */
8692 static void
8693f_py3eval(typval_T *argvars, typval_T *rettv)
8694{
8695 char_u *str;
8696 char_u buf[NUMBUFLEN];
8697
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008698 if (p_pyx == 0)
8699 p_pyx = 3;
8700
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701 str = get_tv_string_buf(&argvars[0], buf);
8702 do_py3eval(str, rettv);
8703}
8704#endif
8705
8706#ifdef FEAT_PYTHON
8707/*
8708 * "pyeval()" function
8709 */
8710 static void
8711f_pyeval(typval_T *argvars, typval_T *rettv)
8712{
8713 char_u *str;
8714 char_u buf[NUMBUFLEN];
8715
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008716 if (p_pyx == 0)
8717 p_pyx = 2;
8718
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719 str = get_tv_string_buf(&argvars[0], buf);
8720 do_pyeval(str, rettv);
8721}
8722#endif
8723
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008724#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8725/*
8726 * "pyxeval()" function
8727 */
8728 static void
8729f_pyxeval(typval_T *argvars, typval_T *rettv)
8730{
8731# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8732 init_pyxversion();
8733 if (p_pyx == 2)
8734 f_pyeval(argvars, rettv);
8735 else
8736 f_py3eval(argvars, rettv);
8737# elif defined(FEAT_PYTHON)
8738 f_pyeval(argvars, rettv);
8739# elif defined(FEAT_PYTHON3)
8740 f_py3eval(argvars, rettv);
8741# endif
8742}
8743#endif
8744
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745/*
8746 * "range()" function
8747 */
8748 static void
8749f_range(typval_T *argvars, typval_T *rettv)
8750{
8751 varnumber_T start;
8752 varnumber_T end;
8753 varnumber_T stride = 1;
8754 varnumber_T i;
8755 int error = FALSE;
8756
8757 start = get_tv_number_chk(&argvars[0], &error);
8758 if (argvars[1].v_type == VAR_UNKNOWN)
8759 {
8760 end = start - 1;
8761 start = 0;
8762 }
8763 else
8764 {
8765 end = get_tv_number_chk(&argvars[1], &error);
8766 if (argvars[2].v_type != VAR_UNKNOWN)
8767 stride = get_tv_number_chk(&argvars[2], &error);
8768 }
8769
8770 if (error)
8771 return; /* type error; errmsg already given */
8772 if (stride == 0)
8773 EMSG(_("E726: Stride is zero"));
8774 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8775 EMSG(_("E727: Start past end"));
8776 else
8777 {
8778 if (rettv_list_alloc(rettv) == OK)
8779 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8780 if (list_append_number(rettv->vval.v_list,
8781 (varnumber_T)i) == FAIL)
8782 break;
8783 }
8784}
8785
8786/*
8787 * "readfile()" function
8788 */
8789 static void
8790f_readfile(typval_T *argvars, typval_T *rettv)
8791{
8792 int binary = FALSE;
8793 int failed = FALSE;
8794 char_u *fname;
8795 FILE *fd;
8796 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8797 int io_size = sizeof(buf);
8798 int readlen; /* size of last fread() */
8799 char_u *prev = NULL; /* previously read bytes, if any */
8800 long prevlen = 0; /* length of data in prev */
8801 long prevsize = 0; /* size of prev buffer */
8802 long maxline = MAXLNUM;
8803 long cnt = 0;
8804 char_u *p; /* position in buf */
8805 char_u *start; /* start of current line */
8806
8807 if (argvars[1].v_type != VAR_UNKNOWN)
8808 {
8809 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8810 binary = TRUE;
8811 if (argvars[2].v_type != VAR_UNKNOWN)
8812 maxline = (long)get_tv_number(&argvars[2]);
8813 }
8814
8815 if (rettv_list_alloc(rettv) == FAIL)
8816 return;
8817
8818 /* Always open the file in binary mode, library functions have a mind of
8819 * their own about CR-LF conversion. */
8820 fname = get_tv_string(&argvars[0]);
8821 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8822 {
8823 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8824 return;
8825 }
8826
8827 while (cnt < maxline || maxline < 0)
8828 {
8829 readlen = (int)fread(buf, 1, io_size, fd);
8830
8831 /* This for loop processes what was read, but is also entered at end
8832 * of file so that either:
8833 * - an incomplete line gets written
8834 * - a "binary" file gets an empty line at the end if it ends in a
8835 * newline. */
8836 for (p = buf, start = buf;
8837 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8838 ++p)
8839 {
8840 if (*p == '\n' || readlen <= 0)
8841 {
8842 listitem_T *li;
8843 char_u *s = NULL;
8844 long_u len = p - start;
8845
8846 /* Finished a line. Remove CRs before NL. */
8847 if (readlen > 0 && !binary)
8848 {
8849 while (len > 0 && start[len - 1] == '\r')
8850 --len;
8851 /* removal may cross back to the "prev" string */
8852 if (len == 0)
8853 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8854 --prevlen;
8855 }
8856 if (prevlen == 0)
8857 s = vim_strnsave(start, (int)len);
8858 else
8859 {
8860 /* Change "prev" buffer to be the right size. This way
8861 * the bytes are only copied once, and very long lines are
8862 * allocated only once. */
8863 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8864 {
8865 mch_memmove(s + prevlen, start, len);
8866 s[prevlen + len] = NUL;
8867 prev = NULL; /* the list will own the string */
8868 prevlen = prevsize = 0;
8869 }
8870 }
8871 if (s == NULL)
8872 {
8873 do_outofmem_msg((long_u) prevlen + len + 1);
8874 failed = TRUE;
8875 break;
8876 }
8877
8878 if ((li = listitem_alloc()) == NULL)
8879 {
8880 vim_free(s);
8881 failed = TRUE;
8882 break;
8883 }
8884 li->li_tv.v_type = VAR_STRING;
8885 li->li_tv.v_lock = 0;
8886 li->li_tv.vval.v_string = s;
8887 list_append(rettv->vval.v_list, li);
8888
8889 start = p + 1; /* step over newline */
8890 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8891 break;
8892 }
8893 else if (*p == NUL)
8894 *p = '\n';
8895#ifdef FEAT_MBYTE
8896 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8897 * when finding the BF and check the previous two bytes. */
8898 else if (*p == 0xbf && enc_utf8 && !binary)
8899 {
8900 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8901 * + 1, these may be in the "prev" string. */
8902 char_u back1 = p >= buf + 1 ? p[-1]
8903 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8904 char_u back2 = p >= buf + 2 ? p[-2]
8905 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8906 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8907
8908 if (back2 == 0xef && back1 == 0xbb)
8909 {
8910 char_u *dest = p - 2;
8911
8912 /* Usually a BOM is at the beginning of a file, and so at
8913 * the beginning of a line; then we can just step over it.
8914 */
8915 if (start == dest)
8916 start = p + 1;
8917 else
8918 {
8919 /* have to shuffle buf to close gap */
8920 int adjust_prevlen = 0;
8921
8922 if (dest < buf)
8923 {
8924 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8925 dest = buf;
8926 }
8927 if (readlen > p - buf + 1)
8928 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8929 readlen -= 3 - adjust_prevlen;
8930 prevlen -= adjust_prevlen;
8931 p = dest - 1;
8932 }
8933 }
8934 }
8935#endif
8936 } /* for */
8937
8938 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8939 break;
8940 if (start < p)
8941 {
8942 /* There's part of a line in buf, store it in "prev". */
8943 if (p - start + prevlen >= prevsize)
8944 {
8945 /* need bigger "prev" buffer */
8946 char_u *newprev;
8947
8948 /* A common use case is ordinary text files and "prev" gets a
8949 * fragment of a line, so the first allocation is made
8950 * small, to avoid repeatedly 'allocing' large and
8951 * 'reallocing' small. */
8952 if (prevsize == 0)
8953 prevsize = (long)(p - start);
8954 else
8955 {
8956 long grow50pc = (prevsize * 3) / 2;
8957 long growmin = (long)((p - start) * 2 + prevlen);
8958 prevsize = grow50pc > growmin ? grow50pc : growmin;
8959 }
8960 newprev = prev == NULL ? alloc(prevsize)
8961 : vim_realloc(prev, prevsize);
8962 if (newprev == NULL)
8963 {
8964 do_outofmem_msg((long_u)prevsize);
8965 failed = TRUE;
8966 break;
8967 }
8968 prev = newprev;
8969 }
8970 /* Add the line part to end of "prev". */
8971 mch_memmove(prev + prevlen, start, p - start);
8972 prevlen += (long)(p - start);
8973 }
8974 } /* while */
8975
8976 /*
8977 * For a negative line count use only the lines at the end of the file,
8978 * free the rest.
8979 */
8980 if (!failed && maxline < 0)
8981 while (cnt > -maxline)
8982 {
8983 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8984 --cnt;
8985 }
8986
8987 if (failed)
8988 {
8989 list_free(rettv->vval.v_list);
8990 /* readfile doc says an empty list is returned on error */
8991 rettv->vval.v_list = list_alloc();
8992 }
8993
8994 vim_free(prev);
8995 fclose(fd);
8996}
8997
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008998 static void
8999return_register(int regname, typval_T *rettv)
9000{
9001 char_u buf[2] = {0, 0};
9002
9003 buf[0] = (char_u)regname;
9004 rettv->v_type = VAR_STRING;
9005 rettv->vval.v_string = vim_strsave(buf);
9006}
9007
9008/*
9009 * "reg_executing()" function
9010 */
9011 static void
9012f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9013{
9014 return_register(reg_executing, rettv);
9015}
9016
9017/*
9018 * "reg_recording()" function
9019 */
9020 static void
9021f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9022{
9023 return_register(reg_recording, rettv);
9024}
9025
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026#if defined(FEAT_RELTIME)
9027static int list2proftime(typval_T *arg, proftime_T *tm);
9028
9029/*
9030 * Convert a List to proftime_T.
9031 * Return FAIL when there is something wrong.
9032 */
9033 static int
9034list2proftime(typval_T *arg, proftime_T *tm)
9035{
9036 long n1, n2;
9037 int error = FALSE;
9038
9039 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9040 || arg->vval.v_list->lv_len != 2)
9041 return FAIL;
9042 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9043 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9044# ifdef WIN3264
9045 tm->HighPart = n1;
9046 tm->LowPart = n2;
9047# else
9048 tm->tv_sec = n1;
9049 tm->tv_usec = n2;
9050# endif
9051 return error ? FAIL : OK;
9052}
9053#endif /* FEAT_RELTIME */
9054
9055/*
9056 * "reltime()" function
9057 */
9058 static void
9059f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9060{
9061#ifdef FEAT_RELTIME
9062 proftime_T res;
9063 proftime_T start;
9064
9065 if (argvars[0].v_type == VAR_UNKNOWN)
9066 {
9067 /* No arguments: get current time. */
9068 profile_start(&res);
9069 }
9070 else if (argvars[1].v_type == VAR_UNKNOWN)
9071 {
9072 if (list2proftime(&argvars[0], &res) == FAIL)
9073 return;
9074 profile_end(&res);
9075 }
9076 else
9077 {
9078 /* Two arguments: compute the difference. */
9079 if (list2proftime(&argvars[0], &start) == FAIL
9080 || list2proftime(&argvars[1], &res) == FAIL)
9081 return;
9082 profile_sub(&res, &start);
9083 }
9084
9085 if (rettv_list_alloc(rettv) == OK)
9086 {
9087 long n1, n2;
9088
9089# ifdef WIN3264
9090 n1 = res.HighPart;
9091 n2 = res.LowPart;
9092# else
9093 n1 = res.tv_sec;
9094 n2 = res.tv_usec;
9095# endif
9096 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9097 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9098 }
9099#endif
9100}
9101
9102#ifdef FEAT_FLOAT
9103/*
9104 * "reltimefloat()" function
9105 */
9106 static void
9107f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9108{
9109# ifdef FEAT_RELTIME
9110 proftime_T tm;
9111# endif
9112
9113 rettv->v_type = VAR_FLOAT;
9114 rettv->vval.v_float = 0;
9115# ifdef FEAT_RELTIME
9116 if (list2proftime(&argvars[0], &tm) == OK)
9117 rettv->vval.v_float = profile_float(&tm);
9118# endif
9119}
9120#endif
9121
9122/*
9123 * "reltimestr()" function
9124 */
9125 static void
9126f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9127{
9128#ifdef FEAT_RELTIME
9129 proftime_T tm;
9130#endif
9131
9132 rettv->v_type = VAR_STRING;
9133 rettv->vval.v_string = NULL;
9134#ifdef FEAT_RELTIME
9135 if (list2proftime(&argvars[0], &tm) == OK)
9136 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9137#endif
9138}
9139
9140#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
9141static void make_connection(void);
9142static int check_connection(void);
9143
9144 static void
9145make_connection(void)
9146{
9147 if (X_DISPLAY == NULL
9148# ifdef FEAT_GUI
9149 && !gui.in_use
9150# endif
9151 )
9152 {
9153 x_force_connect = TRUE;
9154 setup_term_clip();
9155 x_force_connect = FALSE;
9156 }
9157}
9158
9159 static int
9160check_connection(void)
9161{
9162 make_connection();
9163 if (X_DISPLAY == NULL)
9164 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009165 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009166 return FAIL;
9167 }
9168 return OK;
9169}
9170#endif
9171
9172#ifdef FEAT_CLIENTSERVER
9173 static void
9174remote_common(typval_T *argvars, typval_T *rettv, int expr)
9175{
9176 char_u *server_name;
9177 char_u *keys;
9178 char_u *r = NULL;
9179 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009180 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009181# ifdef WIN32
9182 HWND w;
9183# else
9184 Window w;
9185# endif
9186
9187 if (check_restricted() || check_secure())
9188 return;
9189
9190# ifdef FEAT_X11
9191 if (check_connection() == FAIL)
9192 return;
9193# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009194 if (argvars[2].v_type != VAR_UNKNOWN
9195 && argvars[3].v_type != VAR_UNKNOWN)
9196 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009197
9198 server_name = get_tv_string_chk(&argvars[0]);
9199 if (server_name == NULL)
9200 return; /* type error; errmsg already given */
9201 keys = get_tv_string_buf(&argvars[1], buf);
9202# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009203 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009204# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009205 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9206 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009207# endif
9208 {
9209 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009210 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009211 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009212 vim_free(r);
9213 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009214 else
9215 EMSG2(_("E241: Unable to send to %s"), server_name);
9216 return;
9217 }
9218
9219 rettv->vval.v_string = r;
9220
9221 if (argvars[2].v_type != VAR_UNKNOWN)
9222 {
9223 dictitem_T v;
9224 char_u str[30];
9225 char_u *idvar;
9226
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009227 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009228 if (idvar != NULL && *idvar != NUL)
9229 {
9230 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9231 v.di_tv.v_type = VAR_STRING;
9232 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009233 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009234 vim_free(v.di_tv.vval.v_string);
9235 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009236 }
9237}
9238#endif
9239
9240/*
9241 * "remote_expr()" function
9242 */
9243 static void
9244f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9245{
9246 rettv->v_type = VAR_STRING;
9247 rettv->vval.v_string = NULL;
9248#ifdef FEAT_CLIENTSERVER
9249 remote_common(argvars, rettv, TRUE);
9250#endif
9251}
9252
9253/*
9254 * "remote_foreground()" function
9255 */
9256 static void
9257f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9258{
9259#ifdef FEAT_CLIENTSERVER
9260# ifdef WIN32
9261 /* On Win32 it's done in this application. */
9262 {
9263 char_u *server_name = get_tv_string_chk(&argvars[0]);
9264
9265 if (server_name != NULL)
9266 serverForeground(server_name);
9267 }
9268# else
9269 /* Send a foreground() expression to the server. */
9270 argvars[1].v_type = VAR_STRING;
9271 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9272 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009273 rettv->v_type = VAR_STRING;
9274 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009275 remote_common(argvars, rettv, TRUE);
9276 vim_free(argvars[1].vval.v_string);
9277# endif
9278#endif
9279}
9280
9281 static void
9282f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9283{
9284#ifdef FEAT_CLIENTSERVER
9285 dictitem_T v;
9286 char_u *s = NULL;
9287# ifdef WIN32
9288 long_u n = 0;
9289# endif
9290 char_u *serverid;
9291
9292 if (check_restricted() || check_secure())
9293 {
9294 rettv->vval.v_number = -1;
9295 return;
9296 }
9297 serverid = get_tv_string_chk(&argvars[0]);
9298 if (serverid == NULL)
9299 {
9300 rettv->vval.v_number = -1;
9301 return; /* type error; errmsg already given */
9302 }
9303# ifdef WIN32
9304 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9305 if (n == 0)
9306 rettv->vval.v_number = -1;
9307 else
9308 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009309 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009310 rettv->vval.v_number = (s != NULL);
9311 }
9312# else
9313 if (check_connection() == FAIL)
9314 return;
9315
9316 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9317 serverStrToWin(serverid), &s);
9318# endif
9319
9320 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9321 {
9322 char_u *retvar;
9323
9324 v.di_tv.v_type = VAR_STRING;
9325 v.di_tv.vval.v_string = vim_strsave(s);
9326 retvar = get_tv_string_chk(&argvars[1]);
9327 if (retvar != NULL)
9328 set_var(retvar, &v.di_tv, FALSE);
9329 vim_free(v.di_tv.vval.v_string);
9330 }
9331#else
9332 rettv->vval.v_number = -1;
9333#endif
9334}
9335
9336 static void
9337f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9338{
9339 char_u *r = NULL;
9340
9341#ifdef FEAT_CLIENTSERVER
9342 char_u *serverid = get_tv_string_chk(&argvars[0]);
9343
9344 if (serverid != NULL && !check_restricted() && !check_secure())
9345 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009346 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009347# ifdef WIN32
9348 /* The server's HWND is encoded in the 'id' parameter */
9349 long_u n = 0;
9350# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009351
9352 if (argvars[1].v_type != VAR_UNKNOWN)
9353 timeout = get_tv_number(&argvars[1]);
9354
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009355# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009356 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9357 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009358 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009359 if (r == NULL)
9360# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009361 if (check_connection() == FAIL
9362 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9363 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009364# endif
9365 EMSG(_("E277: Unable to read a server reply"));
9366 }
9367#endif
9368 rettv->v_type = VAR_STRING;
9369 rettv->vval.v_string = r;
9370}
9371
9372/*
9373 * "remote_send()" function
9374 */
9375 static void
9376f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9377{
9378 rettv->v_type = VAR_STRING;
9379 rettv->vval.v_string = NULL;
9380#ifdef FEAT_CLIENTSERVER
9381 remote_common(argvars, rettv, FALSE);
9382#endif
9383}
9384
9385/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009386 * "remote_startserver()" function
9387 */
9388 static void
9389f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9390{
9391#ifdef FEAT_CLIENTSERVER
9392 char_u *server = get_tv_string_chk(&argvars[0]);
9393
9394 if (server == NULL)
9395 return; /* type error; errmsg already given */
9396 if (serverName != NULL)
9397 EMSG(_("E941: already started a server"));
9398 else
9399 {
9400# ifdef FEAT_X11
9401 if (check_connection() == OK)
9402 serverRegisterName(X_DISPLAY, server);
9403# else
9404 serverSetName(server);
9405# endif
9406 }
9407#else
9408 EMSG(_("E942: +clientserver feature not available"));
9409#endif
9410}
9411
9412/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009413 * "remove()" function
9414 */
9415 static void
9416f_remove(typval_T *argvars, typval_T *rettv)
9417{
9418 list_T *l;
9419 listitem_T *item, *item2;
9420 listitem_T *li;
9421 long idx;
9422 long end;
9423 char_u *key;
9424 dict_T *d;
9425 dictitem_T *di;
9426 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9427
9428 if (argvars[0].v_type == VAR_DICT)
9429 {
9430 if (argvars[2].v_type != VAR_UNKNOWN)
9431 EMSG2(_(e_toomanyarg), "remove()");
9432 else if ((d = argvars[0].vval.v_dict) != NULL
9433 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9434 {
9435 key = get_tv_string_chk(&argvars[1]);
9436 if (key != NULL)
9437 {
9438 di = dict_find(d, key, -1);
9439 if (di == NULL)
9440 EMSG2(_(e_dictkey), key);
9441 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9442 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9443 {
9444 *rettv = di->di_tv;
9445 init_tv(&di->di_tv);
9446 dictitem_remove(d, di);
9447 }
9448 }
9449 }
9450 }
9451 else if (argvars[0].v_type != VAR_LIST)
9452 EMSG2(_(e_listdictarg), "remove()");
9453 else if ((l = argvars[0].vval.v_list) != NULL
9454 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9455 {
9456 int error = FALSE;
9457
9458 idx = (long)get_tv_number_chk(&argvars[1], &error);
9459 if (error)
9460 ; /* type error: do nothing, errmsg already given */
9461 else if ((item = list_find(l, idx)) == NULL)
9462 EMSGN(_(e_listidx), idx);
9463 else
9464 {
9465 if (argvars[2].v_type == VAR_UNKNOWN)
9466 {
9467 /* Remove one item, return its value. */
9468 vimlist_remove(l, item, item);
9469 *rettv = item->li_tv;
9470 vim_free(item);
9471 }
9472 else
9473 {
9474 /* Remove range of items, return list with values. */
9475 end = (long)get_tv_number_chk(&argvars[2], &error);
9476 if (error)
9477 ; /* type error: do nothing */
9478 else if ((item2 = list_find(l, end)) == NULL)
9479 EMSGN(_(e_listidx), end);
9480 else
9481 {
9482 int cnt = 0;
9483
9484 for (li = item; li != NULL; li = li->li_next)
9485 {
9486 ++cnt;
9487 if (li == item2)
9488 break;
9489 }
9490 if (li == NULL) /* didn't find "item2" after "item" */
9491 EMSG(_(e_invrange));
9492 else
9493 {
9494 vimlist_remove(l, item, item2);
9495 if (rettv_list_alloc(rettv) == OK)
9496 {
9497 l = rettv->vval.v_list;
9498 l->lv_first = item;
9499 l->lv_last = item2;
9500 item->li_prev = NULL;
9501 item2->li_next = NULL;
9502 l->lv_len = cnt;
9503 }
9504 }
9505 }
9506 }
9507 }
9508 }
9509}
9510
9511/*
9512 * "rename({from}, {to})" function
9513 */
9514 static void
9515f_rename(typval_T *argvars, typval_T *rettv)
9516{
9517 char_u buf[NUMBUFLEN];
9518
9519 if (check_restricted() || check_secure())
9520 rettv->vval.v_number = -1;
9521 else
9522 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9523 get_tv_string_buf(&argvars[1], buf));
9524}
9525
9526/*
9527 * "repeat()" function
9528 */
9529 static void
9530f_repeat(typval_T *argvars, typval_T *rettv)
9531{
9532 char_u *p;
9533 int n;
9534 int slen;
9535 int len;
9536 char_u *r;
9537 int i;
9538
9539 n = (int)get_tv_number(&argvars[1]);
9540 if (argvars[0].v_type == VAR_LIST)
9541 {
9542 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9543 while (n-- > 0)
9544 if (list_extend(rettv->vval.v_list,
9545 argvars[0].vval.v_list, NULL) == FAIL)
9546 break;
9547 }
9548 else
9549 {
9550 p = get_tv_string(&argvars[0]);
9551 rettv->v_type = VAR_STRING;
9552 rettv->vval.v_string = NULL;
9553
9554 slen = (int)STRLEN(p);
9555 len = slen * n;
9556 if (len <= 0)
9557 return;
9558
9559 r = alloc(len + 1);
9560 if (r != NULL)
9561 {
9562 for (i = 0; i < n; i++)
9563 mch_memmove(r + i * slen, p, (size_t)slen);
9564 r[len] = NUL;
9565 }
9566
9567 rettv->vval.v_string = r;
9568 }
9569}
9570
9571/*
9572 * "resolve()" function
9573 */
9574 static void
9575f_resolve(typval_T *argvars, typval_T *rettv)
9576{
9577 char_u *p;
9578#ifdef HAVE_READLINK
9579 char_u *buf = NULL;
9580#endif
9581
9582 p = get_tv_string(&argvars[0]);
9583#ifdef FEAT_SHORTCUT
9584 {
9585 char_u *v = NULL;
9586
9587 v = mch_resolve_shortcut(p);
9588 if (v != NULL)
9589 rettv->vval.v_string = v;
9590 else
9591 rettv->vval.v_string = vim_strsave(p);
9592 }
9593#else
9594# ifdef HAVE_READLINK
9595 {
9596 char_u *cpy;
9597 int len;
9598 char_u *remain = NULL;
9599 char_u *q;
9600 int is_relative_to_current = FALSE;
9601 int has_trailing_pathsep = FALSE;
9602 int limit = 100;
9603
9604 p = vim_strsave(p);
9605
9606 if (p[0] == '.' && (vim_ispathsep(p[1])
9607 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9608 is_relative_to_current = TRUE;
9609
9610 len = STRLEN(p);
9611 if (len > 0 && after_pathsep(p, p + len))
9612 {
9613 has_trailing_pathsep = TRUE;
9614 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9615 }
9616
9617 q = getnextcomp(p);
9618 if (*q != NUL)
9619 {
9620 /* Separate the first path component in "p", and keep the
9621 * remainder (beginning with the path separator). */
9622 remain = vim_strsave(q - 1);
9623 q[-1] = NUL;
9624 }
9625
9626 buf = alloc(MAXPATHL + 1);
9627 if (buf == NULL)
9628 goto fail;
9629
9630 for (;;)
9631 {
9632 for (;;)
9633 {
9634 len = readlink((char *)p, (char *)buf, MAXPATHL);
9635 if (len <= 0)
9636 break;
9637 buf[len] = NUL;
9638
9639 if (limit-- == 0)
9640 {
9641 vim_free(p);
9642 vim_free(remain);
9643 EMSG(_("E655: Too many symbolic links (cycle?)"));
9644 rettv->vval.v_string = NULL;
9645 goto fail;
9646 }
9647
9648 /* Ensure that the result will have a trailing path separator
9649 * if the argument has one. */
9650 if (remain == NULL && has_trailing_pathsep)
9651 add_pathsep(buf);
9652
9653 /* Separate the first path component in the link value and
9654 * concatenate the remainders. */
9655 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9656 if (*q != NUL)
9657 {
9658 if (remain == NULL)
9659 remain = vim_strsave(q - 1);
9660 else
9661 {
9662 cpy = concat_str(q - 1, remain);
9663 if (cpy != NULL)
9664 {
9665 vim_free(remain);
9666 remain = cpy;
9667 }
9668 }
9669 q[-1] = NUL;
9670 }
9671
9672 q = gettail(p);
9673 if (q > p && *q == NUL)
9674 {
9675 /* Ignore trailing path separator. */
9676 q[-1] = NUL;
9677 q = gettail(p);
9678 }
9679 if (q > p && !mch_isFullName(buf))
9680 {
9681 /* symlink is relative to directory of argument */
9682 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9683 if (cpy != NULL)
9684 {
9685 STRCPY(cpy, p);
9686 STRCPY(gettail(cpy), buf);
9687 vim_free(p);
9688 p = cpy;
9689 }
9690 }
9691 else
9692 {
9693 vim_free(p);
9694 p = vim_strsave(buf);
9695 }
9696 }
9697
9698 if (remain == NULL)
9699 break;
9700
9701 /* Append the first path component of "remain" to "p". */
9702 q = getnextcomp(remain + 1);
9703 len = q - remain - (*q != NUL);
9704 cpy = vim_strnsave(p, STRLEN(p) + len);
9705 if (cpy != NULL)
9706 {
9707 STRNCAT(cpy, remain, len);
9708 vim_free(p);
9709 p = cpy;
9710 }
9711 /* Shorten "remain". */
9712 if (*q != NUL)
9713 STRMOVE(remain, q - 1);
9714 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009715 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009716 }
9717
9718 /* If the result is a relative path name, make it explicitly relative to
9719 * the current directory if and only if the argument had this form. */
9720 if (!vim_ispathsep(*p))
9721 {
9722 if (is_relative_to_current
9723 && *p != NUL
9724 && !(p[0] == '.'
9725 && (p[1] == NUL
9726 || vim_ispathsep(p[1])
9727 || (p[1] == '.'
9728 && (p[2] == NUL
9729 || vim_ispathsep(p[2]))))))
9730 {
9731 /* Prepend "./". */
9732 cpy = concat_str((char_u *)"./", p);
9733 if (cpy != NULL)
9734 {
9735 vim_free(p);
9736 p = cpy;
9737 }
9738 }
9739 else if (!is_relative_to_current)
9740 {
9741 /* Strip leading "./". */
9742 q = p;
9743 while (q[0] == '.' && vim_ispathsep(q[1]))
9744 q += 2;
9745 if (q > p)
9746 STRMOVE(p, p + 2);
9747 }
9748 }
9749
9750 /* Ensure that the result will have no trailing path separator
9751 * if the argument had none. But keep "/" or "//". */
9752 if (!has_trailing_pathsep)
9753 {
9754 q = p + STRLEN(p);
9755 if (after_pathsep(p, q))
9756 *gettail_sep(p) = NUL;
9757 }
9758
9759 rettv->vval.v_string = p;
9760 }
9761# else
9762 rettv->vval.v_string = vim_strsave(p);
9763# endif
9764#endif
9765
9766 simplify_filename(rettv->vval.v_string);
9767
9768#ifdef HAVE_READLINK
9769fail:
9770 vim_free(buf);
9771#endif
9772 rettv->v_type = VAR_STRING;
9773}
9774
9775/*
9776 * "reverse({list})" function
9777 */
9778 static void
9779f_reverse(typval_T *argvars, typval_T *rettv)
9780{
9781 list_T *l;
9782 listitem_T *li, *ni;
9783
9784 if (argvars[0].v_type != VAR_LIST)
9785 EMSG2(_(e_listarg), "reverse()");
9786 else if ((l = argvars[0].vval.v_list) != NULL
9787 && !tv_check_lock(l->lv_lock,
9788 (char_u *)N_("reverse() argument"), TRUE))
9789 {
9790 li = l->lv_last;
9791 l->lv_first = l->lv_last = NULL;
9792 l->lv_len = 0;
9793 while (li != NULL)
9794 {
9795 ni = li->li_prev;
9796 list_append(l, li);
9797 li = ni;
9798 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009799 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009800 l->lv_idx = l->lv_len - l->lv_idx - 1;
9801 }
9802}
9803
9804#define SP_NOMOVE 0x01 /* don't move cursor */
9805#define SP_REPEAT 0x02 /* repeat to find outer pair */
9806#define SP_RETCOUNT 0x04 /* return matchcount */
9807#define SP_SETPCMARK 0x08 /* set previous context mark */
9808#define SP_START 0x10 /* accept match at start position */
9809#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9810#define SP_END 0x40 /* leave cursor at end of match */
9811#define SP_COLUMN 0x80 /* start at cursor column */
9812
9813static int get_search_arg(typval_T *varp, int *flagsp);
9814
9815/*
9816 * Get flags for a search function.
9817 * Possibly sets "p_ws".
9818 * Returns BACKWARD, FORWARD or zero (for an error).
9819 */
9820 static int
9821get_search_arg(typval_T *varp, int *flagsp)
9822{
9823 int dir = FORWARD;
9824 char_u *flags;
9825 char_u nbuf[NUMBUFLEN];
9826 int mask;
9827
9828 if (varp->v_type != VAR_UNKNOWN)
9829 {
9830 flags = get_tv_string_buf_chk(varp, nbuf);
9831 if (flags == NULL)
9832 return 0; /* type error; errmsg already given */
9833 while (*flags != NUL)
9834 {
9835 switch (*flags)
9836 {
9837 case 'b': dir = BACKWARD; break;
9838 case 'w': p_ws = TRUE; break;
9839 case 'W': p_ws = FALSE; break;
9840 default: mask = 0;
9841 if (flagsp != NULL)
9842 switch (*flags)
9843 {
9844 case 'c': mask = SP_START; break;
9845 case 'e': mask = SP_END; break;
9846 case 'm': mask = SP_RETCOUNT; break;
9847 case 'n': mask = SP_NOMOVE; break;
9848 case 'p': mask = SP_SUBPAT; break;
9849 case 'r': mask = SP_REPEAT; break;
9850 case 's': mask = SP_SETPCMARK; break;
9851 case 'z': mask = SP_COLUMN; break;
9852 }
9853 if (mask == 0)
9854 {
9855 EMSG2(_(e_invarg2), flags);
9856 dir = 0;
9857 }
9858 else
9859 *flagsp |= mask;
9860 }
9861 if (dir == 0)
9862 break;
9863 ++flags;
9864 }
9865 }
9866 return dir;
9867}
9868
9869/*
9870 * Shared by search() and searchpos() functions.
9871 */
9872 static int
9873search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9874{
9875 int flags;
9876 char_u *pat;
9877 pos_T pos;
9878 pos_T save_cursor;
9879 int save_p_ws = p_ws;
9880 int dir;
9881 int retval = 0; /* default: FAIL */
9882 long lnum_stop = 0;
9883 proftime_T tm;
9884#ifdef FEAT_RELTIME
9885 long time_limit = 0;
9886#endif
9887 int options = SEARCH_KEEP;
9888 int subpatnum;
9889
9890 pat = get_tv_string(&argvars[0]);
9891 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9892 if (dir == 0)
9893 goto theend;
9894 flags = *flagsp;
9895 if (flags & SP_START)
9896 options |= SEARCH_START;
9897 if (flags & SP_END)
9898 options |= SEARCH_END;
9899 if (flags & SP_COLUMN)
9900 options |= SEARCH_COL;
9901
9902 /* Optional arguments: line number to stop searching and timeout. */
9903 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9904 {
9905 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9906 if (lnum_stop < 0)
9907 goto theend;
9908#ifdef FEAT_RELTIME
9909 if (argvars[3].v_type != VAR_UNKNOWN)
9910 {
9911 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9912 if (time_limit < 0)
9913 goto theend;
9914 }
9915#endif
9916 }
9917
9918#ifdef FEAT_RELTIME
9919 /* Set the time limit, if there is one. */
9920 profile_setlimit(time_limit, &tm);
9921#endif
9922
9923 /*
9924 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9925 * Check to make sure only those flags are set.
9926 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9927 * flags cannot be set. Check for that condition also.
9928 */
9929 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9930 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9931 {
9932 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9933 goto theend;
9934 }
9935
9936 pos = save_cursor = curwin->w_cursor;
9937 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009938 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009939 if (subpatnum != FAIL)
9940 {
9941 if (flags & SP_SUBPAT)
9942 retval = subpatnum;
9943 else
9944 retval = pos.lnum;
9945 if (flags & SP_SETPCMARK)
9946 setpcmark();
9947 curwin->w_cursor = pos;
9948 if (match_pos != NULL)
9949 {
9950 /* Store the match cursor position */
9951 match_pos->lnum = pos.lnum;
9952 match_pos->col = pos.col + 1;
9953 }
9954 /* "/$" will put the cursor after the end of the line, may need to
9955 * correct that here */
9956 check_cursor();
9957 }
9958
9959 /* If 'n' flag is used: restore cursor position. */
9960 if (flags & SP_NOMOVE)
9961 curwin->w_cursor = save_cursor;
9962 else
9963 curwin->w_set_curswant = TRUE;
9964theend:
9965 p_ws = save_p_ws;
9966
9967 return retval;
9968}
9969
9970#ifdef FEAT_FLOAT
9971
9972/*
9973 * round() is not in C90, use ceil() or floor() instead.
9974 */
9975 float_T
9976vim_round(float_T f)
9977{
9978 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9979}
9980
9981/*
9982 * "round({float})" function
9983 */
9984 static void
9985f_round(typval_T *argvars, typval_T *rettv)
9986{
9987 float_T f = 0.0;
9988
9989 rettv->v_type = VAR_FLOAT;
9990 if (get_float_arg(argvars, &f) == OK)
9991 rettv->vval.v_float = vim_round(f);
9992 else
9993 rettv->vval.v_float = 0.0;
9994}
9995#endif
9996
9997/*
9998 * "screenattr()" function
9999 */
10000 static void
10001f_screenattr(typval_T *argvars, typval_T *rettv)
10002{
10003 int row;
10004 int col;
10005 int c;
10006
10007 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10008 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10009 if (row < 0 || row >= screen_Rows
10010 || col < 0 || col >= screen_Columns)
10011 c = -1;
10012 else
10013 c = ScreenAttrs[LineOffset[row] + col];
10014 rettv->vval.v_number = c;
10015}
10016
10017/*
10018 * "screenchar()" function
10019 */
10020 static void
10021f_screenchar(typval_T *argvars, typval_T *rettv)
10022{
10023 int row;
10024 int col;
10025 int off;
10026 int c;
10027
10028 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10029 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10030 if (row < 0 || row >= screen_Rows
10031 || col < 0 || col >= screen_Columns)
10032 c = -1;
10033 else
10034 {
10035 off = LineOffset[row] + col;
10036#ifdef FEAT_MBYTE
10037 if (enc_utf8 && ScreenLinesUC[off] != 0)
10038 c = ScreenLinesUC[off];
10039 else
10040#endif
10041 c = ScreenLines[off];
10042 }
10043 rettv->vval.v_number = c;
10044}
10045
10046/*
10047 * "screencol()" function
10048 *
10049 * First column is 1 to be consistent with virtcol().
10050 */
10051 static void
10052f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10053{
10054 rettv->vval.v_number = screen_screencol() + 1;
10055}
10056
10057/*
10058 * "screenrow()" function
10059 */
10060 static void
10061f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10062{
10063 rettv->vval.v_number = screen_screenrow() + 1;
10064}
10065
10066/*
10067 * "search()" function
10068 */
10069 static void
10070f_search(typval_T *argvars, typval_T *rettv)
10071{
10072 int flags = 0;
10073
10074 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10075}
10076
10077/*
10078 * "searchdecl()" function
10079 */
10080 static void
10081f_searchdecl(typval_T *argvars, typval_T *rettv)
10082{
10083 int locally = 1;
10084 int thisblock = 0;
10085 int error = FALSE;
10086 char_u *name;
10087
10088 rettv->vval.v_number = 1; /* default: FAIL */
10089
10090 name = get_tv_string_chk(&argvars[0]);
10091 if (argvars[1].v_type != VAR_UNKNOWN)
10092 {
10093 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
10094 if (!error && argvars[2].v_type != VAR_UNKNOWN)
10095 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
10096 }
10097 if (!error && name != NULL)
10098 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10099 locally, thisblock, SEARCH_KEEP) == FAIL;
10100}
10101
10102/*
10103 * Used by searchpair() and searchpairpos()
10104 */
10105 static int
10106searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10107{
10108 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010109 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010110 int save_p_ws = p_ws;
10111 int dir;
10112 int flags = 0;
10113 char_u nbuf1[NUMBUFLEN];
10114 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010115 int retval = 0; /* default: FAIL */
10116 long lnum_stop = 0;
10117 long time_limit = 0;
10118
10119 /* Get the three pattern arguments: start, middle, end. */
10120 spat = get_tv_string_chk(&argvars[0]);
10121 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
10122 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
10123 if (spat == NULL || mpat == NULL || epat == NULL)
10124 goto theend; /* type error */
10125
10126 /* Handle the optional fourth argument: flags */
10127 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10128 if (dir == 0)
10129 goto theend;
10130
10131 /* Don't accept SP_END or SP_SUBPAT.
10132 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10133 */
10134 if ((flags & (SP_END | SP_SUBPAT)) != 0
10135 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10136 {
10137 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
10138 goto theend;
10139 }
10140
10141 /* Using 'r' implies 'W', otherwise it doesn't work. */
10142 if (flags & SP_REPEAT)
10143 p_ws = FALSE;
10144
10145 /* Optional fifth argument: skip expression */
10146 if (argvars[3].v_type == VAR_UNKNOWN
10147 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010148 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010149 else
10150 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010151 skip = &argvars[4];
10152 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10153 && skip->v_type != VAR_STRING)
10154 {
10155 /* Type error */
10156 goto theend;
10157 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010158 if (argvars[5].v_type != VAR_UNKNOWN)
10159 {
10160 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
10161 if (lnum_stop < 0)
10162 goto theend;
10163#ifdef FEAT_RELTIME
10164 if (argvars[6].v_type != VAR_UNKNOWN)
10165 {
10166 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
10167 if (time_limit < 0)
10168 goto theend;
10169 }
10170#endif
10171 }
10172 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010173
10174 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10175 match_pos, lnum_stop, time_limit);
10176
10177theend:
10178 p_ws = save_p_ws;
10179
10180 return retval;
10181}
10182
10183/*
10184 * "searchpair()" function
10185 */
10186 static void
10187f_searchpair(typval_T *argvars, typval_T *rettv)
10188{
10189 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10190}
10191
10192/*
10193 * "searchpairpos()" function
10194 */
10195 static void
10196f_searchpairpos(typval_T *argvars, typval_T *rettv)
10197{
10198 pos_T match_pos;
10199 int lnum = 0;
10200 int col = 0;
10201
10202 if (rettv_list_alloc(rettv) == FAIL)
10203 return;
10204
10205 if (searchpair_cmn(argvars, &match_pos) > 0)
10206 {
10207 lnum = match_pos.lnum;
10208 col = match_pos.col;
10209 }
10210
10211 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10212 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10213}
10214
10215/*
10216 * Search for a start/middle/end thing.
10217 * Used by searchpair(), see its documentation for the details.
10218 * Returns 0 or -1 for no match,
10219 */
10220 long
10221do_searchpair(
10222 char_u *spat, /* start pattern */
10223 char_u *mpat, /* middle pattern */
10224 char_u *epat, /* end pattern */
10225 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010226 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010227 int flags, /* SP_SETPCMARK and other SP_ values */
10228 pos_T *match_pos,
10229 linenr_T lnum_stop, /* stop at this line if not zero */
10230 long time_limit UNUSED) /* stop after this many msec */
10231{
10232 char_u *save_cpo;
10233 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10234 long retval = 0;
10235 pos_T pos;
10236 pos_T firstpos;
10237 pos_T foundpos;
10238 pos_T save_cursor;
10239 pos_T save_pos;
10240 int n;
10241 int r;
10242 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010243 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010244 int err;
10245 int options = SEARCH_KEEP;
10246 proftime_T tm;
10247
10248 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10249 save_cpo = p_cpo;
10250 p_cpo = empty_option;
10251
10252#ifdef FEAT_RELTIME
10253 /* Set the time limit, if there is one. */
10254 profile_setlimit(time_limit, &tm);
10255#endif
10256
10257 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10258 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010259 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10260 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010261 if (pat2 == NULL || pat3 == NULL)
10262 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010263 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010264 if (*mpat == NUL)
10265 STRCPY(pat3, pat2);
10266 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010267 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010268 spat, epat, mpat);
10269 if (flags & SP_START)
10270 options |= SEARCH_START;
10271
Bram Moolenaar48570482017-10-30 21:48:41 +010010272 if (skip != NULL)
10273 {
10274 /* Empty string means to not use the skip expression. */
10275 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10276 use_skip = skip->vval.v_string != NULL
10277 && *skip->vval.v_string != NUL;
10278 }
10279
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010280 save_cursor = curwin->w_cursor;
10281 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010282 CLEAR_POS(&firstpos);
10283 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010284 pat = pat3;
10285 for (;;)
10286 {
10287 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010288 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010289 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010290 /* didn't find it or found the first match again: FAIL */
10291 break;
10292
10293 if (firstpos.lnum == 0)
10294 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010295 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010296 {
10297 /* Found the same position again. Can happen with a pattern that
10298 * has "\zs" at the end and searching backwards. Advance one
10299 * character and try again. */
10300 if (dir == BACKWARD)
10301 decl(&pos);
10302 else
10303 incl(&pos);
10304 }
10305 foundpos = pos;
10306
10307 /* clear the start flag to avoid getting stuck here */
10308 options &= ~SEARCH_START;
10309
10310 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010311 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010312 {
10313 save_pos = curwin->w_cursor;
10314 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010315 err = FALSE;
10316 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010317 curwin->w_cursor = save_pos;
10318 if (err)
10319 {
10320 /* Evaluating {skip} caused an error, break here. */
10321 curwin->w_cursor = save_cursor;
10322 retval = -1;
10323 break;
10324 }
10325 if (r)
10326 continue;
10327 }
10328
10329 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10330 {
10331 /* Found end when searching backwards or start when searching
10332 * forward: nested pair. */
10333 ++nest;
10334 pat = pat2; /* nested, don't search for middle */
10335 }
10336 else
10337 {
10338 /* Found end when searching forward or start when searching
10339 * backward: end of (nested) pair; or found middle in outer pair. */
10340 if (--nest == 1)
10341 pat = pat3; /* outer level, search for middle */
10342 }
10343
10344 if (nest == 0)
10345 {
10346 /* Found the match: return matchcount or line number. */
10347 if (flags & SP_RETCOUNT)
10348 ++retval;
10349 else
10350 retval = pos.lnum;
10351 if (flags & SP_SETPCMARK)
10352 setpcmark();
10353 curwin->w_cursor = pos;
10354 if (!(flags & SP_REPEAT))
10355 break;
10356 nest = 1; /* search for next unmatched */
10357 }
10358 }
10359
10360 if (match_pos != NULL)
10361 {
10362 /* Store the match cursor position */
10363 match_pos->lnum = curwin->w_cursor.lnum;
10364 match_pos->col = curwin->w_cursor.col + 1;
10365 }
10366
10367 /* If 'n' flag is used or search failed: restore cursor position. */
10368 if ((flags & SP_NOMOVE) || retval == 0)
10369 curwin->w_cursor = save_cursor;
10370
10371theend:
10372 vim_free(pat2);
10373 vim_free(pat3);
10374 if (p_cpo == empty_option)
10375 p_cpo = save_cpo;
10376 else
10377 /* Darn, evaluating the {skip} expression changed the value. */
10378 free_string_option(save_cpo);
10379
10380 return retval;
10381}
10382
10383/*
10384 * "searchpos()" function
10385 */
10386 static void
10387f_searchpos(typval_T *argvars, typval_T *rettv)
10388{
10389 pos_T match_pos;
10390 int lnum = 0;
10391 int col = 0;
10392 int n;
10393 int flags = 0;
10394
10395 if (rettv_list_alloc(rettv) == FAIL)
10396 return;
10397
10398 n = search_cmn(argvars, &match_pos, &flags);
10399 if (n > 0)
10400 {
10401 lnum = match_pos.lnum;
10402 col = match_pos.col;
10403 }
10404
10405 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10406 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10407 if (flags & SP_SUBPAT)
10408 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10409}
10410
10411 static void
10412f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10413{
10414#ifdef FEAT_CLIENTSERVER
10415 char_u buf[NUMBUFLEN];
10416 char_u *server = get_tv_string_chk(&argvars[0]);
10417 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10418
10419 rettv->vval.v_number = -1;
10420 if (server == NULL || reply == NULL)
10421 return;
10422 if (check_restricted() || check_secure())
10423 return;
10424# ifdef FEAT_X11
10425 if (check_connection() == FAIL)
10426 return;
10427# endif
10428
10429 if (serverSendReply(server, reply) < 0)
10430 {
10431 EMSG(_("E258: Unable to send to client"));
10432 return;
10433 }
10434 rettv->vval.v_number = 0;
10435#else
10436 rettv->vval.v_number = -1;
10437#endif
10438}
10439
10440 static void
10441f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10442{
10443 char_u *r = NULL;
10444
10445#ifdef FEAT_CLIENTSERVER
10446# ifdef WIN32
10447 r = serverGetVimNames();
10448# else
10449 make_connection();
10450 if (X_DISPLAY != NULL)
10451 r = serverGetVimNames(X_DISPLAY);
10452# endif
10453#endif
10454 rettv->v_type = VAR_STRING;
10455 rettv->vval.v_string = r;
10456}
10457
10458/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010459 * "setbufline()" function
10460 */
10461 static void
10462f_setbufline(argvars, rettv)
10463 typval_T *argvars;
10464 typval_T *rettv;
10465{
10466 linenr_T lnum;
10467 buf_T *buf;
10468
10469 buf = get_buf_tv(&argvars[0], FALSE);
10470 if (buf == NULL)
10471 rettv->vval.v_number = 1; /* FAIL */
10472 else
10473 {
10474 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010475 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010476 }
10477}
10478
10479/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010480 * "setbufvar()" function
10481 */
10482 static void
10483f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10484{
10485 buf_T *buf;
10486 char_u *varname, *bufvarname;
10487 typval_T *varp;
10488 char_u nbuf[NUMBUFLEN];
10489
10490 if (check_restricted() || check_secure())
10491 return;
10492 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10493 varname = get_tv_string_chk(&argvars[1]);
10494 buf = get_buf_tv(&argvars[0], FALSE);
10495 varp = &argvars[2];
10496
10497 if (buf != NULL && varname != NULL && varp != NULL)
10498 {
10499 if (*varname == '&')
10500 {
10501 long numval;
10502 char_u *strval;
10503 int error = FALSE;
10504 aco_save_T aco;
10505
10506 /* set curbuf to be our buf, temporarily */
10507 aucmd_prepbuf(&aco, buf);
10508
10509 ++varname;
10510 numval = (long)get_tv_number_chk(varp, &error);
10511 strval = get_tv_string_buf_chk(varp, nbuf);
10512 if (!error && strval != NULL)
10513 set_option_value(varname, numval, strval, OPT_LOCAL);
10514
10515 /* reset notion of buffer */
10516 aucmd_restbuf(&aco);
10517 }
10518 else
10519 {
10520 buf_T *save_curbuf = curbuf;
10521
10522 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10523 if (bufvarname != NULL)
10524 {
10525 curbuf = buf;
10526 STRCPY(bufvarname, "b:");
10527 STRCPY(bufvarname + 2, varname);
10528 set_var(bufvarname, varp, TRUE);
10529 vim_free(bufvarname);
10530 curbuf = save_curbuf;
10531 }
10532 }
10533 }
10534}
10535
10536 static void
10537f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10538{
10539 dict_T *d;
10540 dictitem_T *di;
10541 char_u *csearch;
10542
10543 if (argvars[0].v_type != VAR_DICT)
10544 {
10545 EMSG(_(e_dictreq));
10546 return;
10547 }
10548
10549 if ((d = argvars[0].vval.v_dict) != NULL)
10550 {
10551 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10552 if (csearch != NULL)
10553 {
10554#ifdef FEAT_MBYTE
10555 if (enc_utf8)
10556 {
10557 int pcc[MAX_MCO];
10558 int c = utfc_ptr2char(csearch, pcc);
10559
10560 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10561 }
10562 else
10563#endif
10564 set_last_csearch(PTR2CHAR(csearch),
10565 csearch, MB_PTR2LEN(csearch));
10566 }
10567
10568 di = dict_find(d, (char_u *)"forward", -1);
10569 if (di != NULL)
10570 set_csearch_direction((int)get_tv_number(&di->di_tv)
10571 ? FORWARD : BACKWARD);
10572
10573 di = dict_find(d, (char_u *)"until", -1);
10574 if (di != NULL)
10575 set_csearch_until(!!get_tv_number(&di->di_tv));
10576 }
10577}
10578
10579/*
10580 * "setcmdpos()" function
10581 */
10582 static void
10583f_setcmdpos(typval_T *argvars, typval_T *rettv)
10584{
10585 int pos = (int)get_tv_number(&argvars[0]) - 1;
10586
10587 if (pos >= 0)
10588 rettv->vval.v_number = set_cmdline_pos(pos);
10589}
10590
10591/*
10592 * "setfperm({fname}, {mode})" function
10593 */
10594 static void
10595f_setfperm(typval_T *argvars, typval_T *rettv)
10596{
10597 char_u *fname;
10598 char_u modebuf[NUMBUFLEN];
10599 char_u *mode_str;
10600 int i;
10601 int mask;
10602 int mode = 0;
10603
10604 rettv->vval.v_number = 0;
10605 fname = get_tv_string_chk(&argvars[0]);
10606 if (fname == NULL)
10607 return;
10608 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10609 if (mode_str == NULL)
10610 return;
10611 if (STRLEN(mode_str) != 9)
10612 {
10613 EMSG2(_(e_invarg2), mode_str);
10614 return;
10615 }
10616
10617 mask = 1;
10618 for (i = 8; i >= 0; --i)
10619 {
10620 if (mode_str[i] != '-')
10621 mode |= mask;
10622 mask = mask << 1;
10623 }
10624 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10625}
10626
10627/*
10628 * "setline()" function
10629 */
10630 static void
10631f_setline(typval_T *argvars, typval_T *rettv)
10632{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010633 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010634
Bram Moolenaarca851592018-06-06 21:04:07 +020010635 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010636}
10637
Bram Moolenaard823fa92016-08-12 16:29:27 +020010638static void set_qf_ll_list(win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *what_arg, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010639
10640/*
10641 * Used by "setqflist()" and "setloclist()" functions
10642 */
10643 static void
10644set_qf_ll_list(
10645 win_T *wp UNUSED,
10646 typval_T *list_arg UNUSED,
10647 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010648 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010649 typval_T *rettv)
10650{
10651#ifdef FEAT_QUICKFIX
10652 static char *e_invact = N_("E927: Invalid action: '%s'");
10653 char_u *act;
10654 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010655 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010656#endif
10657
10658 rettv->vval.v_number = -1;
10659
10660#ifdef FEAT_QUICKFIX
10661 if (list_arg->v_type != VAR_LIST)
10662 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010663 else if (recursive != 0)
10664 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010665 else
10666 {
10667 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010668 dict_T *d = NULL;
10669 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010670
10671 if (action_arg->v_type == VAR_STRING)
10672 {
10673 act = get_tv_string_chk(action_arg);
10674 if (act == NULL)
10675 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010676 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10677 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010678 action = *act;
10679 else
10680 EMSG2(_(e_invact), act);
10681 }
10682 else if (action_arg->v_type == VAR_UNKNOWN)
10683 action = ' ';
10684 else
10685 EMSG(_(e_stringreq));
10686
Bram Moolenaard823fa92016-08-12 16:29:27 +020010687 if (action_arg->v_type != VAR_UNKNOWN
10688 && what_arg->v_type != VAR_UNKNOWN)
10689 {
10690 if (what_arg->v_type == VAR_DICT)
10691 d = what_arg->vval.v_dict;
10692 else
10693 {
10694 EMSG(_(e_dictreq));
10695 valid_dict = FALSE;
10696 }
10697 }
10698
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010699 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010700 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010701 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10702 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010703 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010704 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010705 }
10706#endif
10707}
10708
10709/*
10710 * "setloclist()" function
10711 */
10712 static void
10713f_setloclist(typval_T *argvars, typval_T *rettv)
10714{
10715 win_T *win;
10716
10717 rettv->vval.v_number = -1;
10718
10719 win = find_win_by_nr(&argvars[0], NULL);
10720 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010721 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010722}
10723
10724/*
10725 * "setmatches()" function
10726 */
10727 static void
10728f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10729{
10730#ifdef FEAT_SEARCH_EXTRA
10731 list_T *l;
10732 listitem_T *li;
10733 dict_T *d;
10734 list_T *s = NULL;
10735
10736 rettv->vval.v_number = -1;
10737 if (argvars[0].v_type != VAR_LIST)
10738 {
10739 EMSG(_(e_listreq));
10740 return;
10741 }
10742 if ((l = argvars[0].vval.v_list) != NULL)
10743 {
10744
10745 /* To some extent make sure that we are dealing with a list from
10746 * "getmatches()". */
10747 li = l->lv_first;
10748 while (li != NULL)
10749 {
10750 if (li->li_tv.v_type != VAR_DICT
10751 || (d = li->li_tv.vval.v_dict) == NULL)
10752 {
10753 EMSG(_(e_invarg));
10754 return;
10755 }
10756 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10757 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10758 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10759 && dict_find(d, (char_u *)"priority", -1) != NULL
10760 && dict_find(d, (char_u *)"id", -1) != NULL))
10761 {
10762 EMSG(_(e_invarg));
10763 return;
10764 }
10765 li = li->li_next;
10766 }
10767
10768 clear_matches(curwin);
10769 li = l->lv_first;
10770 while (li != NULL)
10771 {
10772 int i = 0;
10773 char_u buf[5];
10774 dictitem_T *di;
10775 char_u *group;
10776 int priority;
10777 int id;
10778 char_u *conceal;
10779
10780 d = li->li_tv.vval.v_dict;
10781 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10782 {
10783 if (s == NULL)
10784 {
10785 s = list_alloc();
10786 if (s == NULL)
10787 return;
10788 }
10789
10790 /* match from matchaddpos() */
10791 for (i = 1; i < 9; i++)
10792 {
10793 sprintf((char *)buf, (char *)"pos%d", i);
10794 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10795 {
10796 if (di->di_tv.v_type != VAR_LIST)
10797 return;
10798
10799 list_append_tv(s, &di->di_tv);
10800 s->lv_refcount++;
10801 }
10802 else
10803 break;
10804 }
10805 }
10806
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010807 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010808 priority = (int)get_dict_number(d, (char_u *)"priority");
10809 id = (int)get_dict_number(d, (char_u *)"id");
10810 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010811 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010812 : NULL;
10813 if (i == 0)
10814 {
10815 match_add(curwin, group,
10816 get_dict_string(d, (char_u *)"pattern", FALSE),
10817 priority, id, NULL, conceal);
10818 }
10819 else
10820 {
10821 match_add(curwin, group, NULL, priority, id, s, conceal);
10822 list_unref(s);
10823 s = NULL;
10824 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010825 vim_free(group);
10826 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010827
10828 li = li->li_next;
10829 }
10830 rettv->vval.v_number = 0;
10831 }
10832#endif
10833}
10834
10835/*
10836 * "setpos()" function
10837 */
10838 static void
10839f_setpos(typval_T *argvars, typval_T *rettv)
10840{
10841 pos_T pos;
10842 int fnum;
10843 char_u *name;
10844 colnr_T curswant = -1;
10845
10846 rettv->vval.v_number = -1;
10847 name = get_tv_string_chk(argvars);
10848 if (name != NULL)
10849 {
10850 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10851 {
10852 if (--pos.col < 0)
10853 pos.col = 0;
10854 if (name[0] == '.' && name[1] == NUL)
10855 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010856 /* set cursor; "fnum" is ignored */
10857 curwin->w_cursor = pos;
10858 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010859 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010860 curwin->w_curswant = curswant - 1;
10861 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010862 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010863 check_cursor();
10864 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010865 }
10866 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10867 {
10868 /* set mark */
10869 if (setmark_pos(name[1], &pos, fnum) == OK)
10870 rettv->vval.v_number = 0;
10871 }
10872 else
10873 EMSG(_(e_invarg));
10874 }
10875 }
10876}
10877
10878/*
10879 * "setqflist()" function
10880 */
10881 static void
10882f_setqflist(typval_T *argvars, typval_T *rettv)
10883{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010884 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010885}
10886
10887/*
10888 * "setreg()" function
10889 */
10890 static void
10891f_setreg(typval_T *argvars, typval_T *rettv)
10892{
10893 int regname;
10894 char_u *strregname;
10895 char_u *stropt;
10896 char_u *strval;
10897 int append;
10898 char_u yank_type;
10899 long block_len;
10900
10901 block_len = -1;
10902 yank_type = MAUTO;
10903 append = FALSE;
10904
10905 strregname = get_tv_string_chk(argvars);
10906 rettv->vval.v_number = 1; /* FAIL is default */
10907
10908 if (strregname == NULL)
10909 return; /* type error; errmsg already given */
10910 regname = *strregname;
10911 if (regname == 0 || regname == '@')
10912 regname = '"';
10913
10914 if (argvars[2].v_type != VAR_UNKNOWN)
10915 {
10916 stropt = get_tv_string_chk(&argvars[2]);
10917 if (stropt == NULL)
10918 return; /* type error */
10919 for (; *stropt != NUL; ++stropt)
10920 switch (*stropt)
10921 {
10922 case 'a': case 'A': /* append */
10923 append = TRUE;
10924 break;
10925 case 'v': case 'c': /* character-wise selection */
10926 yank_type = MCHAR;
10927 break;
10928 case 'V': case 'l': /* line-wise selection */
10929 yank_type = MLINE;
10930 break;
10931 case 'b': case Ctrl_V: /* block-wise selection */
10932 yank_type = MBLOCK;
10933 if (VIM_ISDIGIT(stropt[1]))
10934 {
10935 ++stropt;
10936 block_len = getdigits(&stropt) - 1;
10937 --stropt;
10938 }
10939 break;
10940 }
10941 }
10942
10943 if (argvars[1].v_type == VAR_LIST)
10944 {
10945 char_u **lstval;
10946 char_u **allocval;
10947 char_u buf[NUMBUFLEN];
10948 char_u **curval;
10949 char_u **curallocval;
10950 list_T *ll = argvars[1].vval.v_list;
10951 listitem_T *li;
10952 int len;
10953
10954 /* If the list is NULL handle like an empty list. */
10955 len = ll == NULL ? 0 : ll->lv_len;
10956
10957 /* First half: use for pointers to result lines; second half: use for
10958 * pointers to allocated copies. */
10959 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10960 if (lstval == NULL)
10961 return;
10962 curval = lstval;
10963 allocval = lstval + len + 2;
10964 curallocval = allocval;
10965
10966 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10967 li = li->li_next)
10968 {
10969 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10970 if (strval == NULL)
10971 goto free_lstval;
10972 if (strval == buf)
10973 {
10974 /* Need to make a copy, next get_tv_string_buf_chk() will
10975 * overwrite the string. */
10976 strval = vim_strsave(buf);
10977 if (strval == NULL)
10978 goto free_lstval;
10979 *curallocval++ = strval;
10980 }
10981 *curval++ = strval;
10982 }
10983 *curval++ = NULL;
10984
10985 write_reg_contents_lst(regname, lstval, -1,
10986 append, yank_type, block_len);
10987free_lstval:
10988 while (curallocval > allocval)
10989 vim_free(*--curallocval);
10990 vim_free(lstval);
10991 }
10992 else
10993 {
10994 strval = get_tv_string_chk(&argvars[1]);
10995 if (strval == NULL)
10996 return;
10997 write_reg_contents_ex(regname, strval, -1,
10998 append, yank_type, block_len);
10999 }
11000 rettv->vval.v_number = 0;
11001}
11002
11003/*
11004 * "settabvar()" function
11005 */
11006 static void
11007f_settabvar(typval_T *argvars, typval_T *rettv)
11008{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011009 tabpage_T *save_curtab;
11010 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011011 char_u *varname, *tabvarname;
11012 typval_T *varp;
11013
11014 rettv->vval.v_number = 0;
11015
11016 if (check_restricted() || check_secure())
11017 return;
11018
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011019 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011020 varname = get_tv_string_chk(&argvars[1]);
11021 varp = &argvars[2];
11022
Bram Moolenaar4033c552017-09-16 20:54:51 +020011023 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011024 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011025 save_curtab = curtab;
11026 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011027
11028 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11029 if (tabvarname != NULL)
11030 {
11031 STRCPY(tabvarname, "t:");
11032 STRCPY(tabvarname + 2, varname);
11033 set_var(tabvarname, varp, TRUE);
11034 vim_free(tabvarname);
11035 }
11036
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011037 /* Restore current tabpage */
11038 if (valid_tabpage(save_curtab))
11039 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011040 }
11041}
11042
11043/*
11044 * "settabwinvar()" function
11045 */
11046 static void
11047f_settabwinvar(typval_T *argvars, typval_T *rettv)
11048{
11049 setwinvar(argvars, rettv, 1);
11050}
11051
11052/*
11053 * "setwinvar()" function
11054 */
11055 static void
11056f_setwinvar(typval_T *argvars, typval_T *rettv)
11057{
11058 setwinvar(argvars, rettv, 0);
11059}
11060
11061#ifdef FEAT_CRYPT
11062/*
11063 * "sha256({string})" function
11064 */
11065 static void
11066f_sha256(typval_T *argvars, typval_T *rettv)
11067{
11068 char_u *p;
11069
11070 p = get_tv_string(&argvars[0]);
11071 rettv->vval.v_string = vim_strsave(
11072 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11073 rettv->v_type = VAR_STRING;
11074}
11075#endif /* FEAT_CRYPT */
11076
11077/*
11078 * "shellescape({string})" function
11079 */
11080 static void
11081f_shellescape(typval_T *argvars, typval_T *rettv)
11082{
Bram Moolenaar20615522017-06-05 18:46:26 +020011083 int do_special = non_zero_arg(&argvars[1]);
11084
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011085 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020011086 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011087 rettv->v_type = VAR_STRING;
11088}
11089
11090/*
11091 * shiftwidth() function
11092 */
11093 static void
11094f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11095{
11096 rettv->vval.v_number = get_sw_value(curbuf);
11097}
11098
11099/*
11100 * "simplify()" function
11101 */
11102 static void
11103f_simplify(typval_T *argvars, typval_T *rettv)
11104{
11105 char_u *p;
11106
11107 p = get_tv_string(&argvars[0]);
11108 rettv->vval.v_string = vim_strsave(p);
11109 simplify_filename(rettv->vval.v_string); /* simplify in place */
11110 rettv->v_type = VAR_STRING;
11111}
11112
11113#ifdef FEAT_FLOAT
11114/*
11115 * "sin()" function
11116 */
11117 static void
11118f_sin(typval_T *argvars, typval_T *rettv)
11119{
11120 float_T f = 0.0;
11121
11122 rettv->v_type = VAR_FLOAT;
11123 if (get_float_arg(argvars, &f) == OK)
11124 rettv->vval.v_float = sin(f);
11125 else
11126 rettv->vval.v_float = 0.0;
11127}
11128
11129/*
11130 * "sinh()" function
11131 */
11132 static void
11133f_sinh(typval_T *argvars, typval_T *rettv)
11134{
11135 float_T f = 0.0;
11136
11137 rettv->v_type = VAR_FLOAT;
11138 if (get_float_arg(argvars, &f) == OK)
11139 rettv->vval.v_float = sinh(f);
11140 else
11141 rettv->vval.v_float = 0.0;
11142}
11143#endif
11144
11145static int
11146#ifdef __BORLANDC__
11147 _RTLENTRYF
11148#endif
11149 item_compare(const void *s1, const void *s2);
11150static int
11151#ifdef __BORLANDC__
11152 _RTLENTRYF
11153#endif
11154 item_compare2(const void *s1, const void *s2);
11155
11156/* struct used in the array that's given to qsort() */
11157typedef struct
11158{
11159 listitem_T *item;
11160 int idx;
11161} sortItem_T;
11162
11163/* struct storing information about current sort */
11164typedef struct
11165{
11166 int item_compare_ic;
11167 int item_compare_numeric;
11168 int item_compare_numbers;
11169#ifdef FEAT_FLOAT
11170 int item_compare_float;
11171#endif
11172 char_u *item_compare_func;
11173 partial_T *item_compare_partial;
11174 dict_T *item_compare_selfdict;
11175 int item_compare_func_err;
11176 int item_compare_keep_zero;
11177} sortinfo_T;
11178static sortinfo_T *sortinfo = NULL;
11179static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
11180#define ITEM_COMPARE_FAIL 999
11181
11182/*
11183 * Compare functions for f_sort() and f_uniq() below.
11184 */
11185 static int
11186#ifdef __BORLANDC__
11187_RTLENTRYF
11188#endif
11189item_compare(const void *s1, const void *s2)
11190{
11191 sortItem_T *si1, *si2;
11192 typval_T *tv1, *tv2;
11193 char_u *p1, *p2;
11194 char_u *tofree1 = NULL, *tofree2 = NULL;
11195 int res;
11196 char_u numbuf1[NUMBUFLEN];
11197 char_u numbuf2[NUMBUFLEN];
11198
11199 si1 = (sortItem_T *)s1;
11200 si2 = (sortItem_T *)s2;
11201 tv1 = &si1->item->li_tv;
11202 tv2 = &si2->item->li_tv;
11203
11204 if (sortinfo->item_compare_numbers)
11205 {
11206 varnumber_T v1 = get_tv_number(tv1);
11207 varnumber_T v2 = get_tv_number(tv2);
11208
11209 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11210 }
11211
11212#ifdef FEAT_FLOAT
11213 if (sortinfo->item_compare_float)
11214 {
11215 float_T v1 = get_tv_float(tv1);
11216 float_T v2 = get_tv_float(tv2);
11217
11218 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11219 }
11220#endif
11221
11222 /* tv2string() puts quotes around a string and allocates memory. Don't do
11223 * that for string variables. Use a single quote when comparing with a
11224 * non-string to do what the docs promise. */
11225 if (tv1->v_type == VAR_STRING)
11226 {
11227 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11228 p1 = (char_u *)"'";
11229 else
11230 p1 = tv1->vval.v_string;
11231 }
11232 else
11233 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11234 if (tv2->v_type == VAR_STRING)
11235 {
11236 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11237 p2 = (char_u *)"'";
11238 else
11239 p2 = tv2->vval.v_string;
11240 }
11241 else
11242 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11243 if (p1 == NULL)
11244 p1 = (char_u *)"";
11245 if (p2 == NULL)
11246 p2 = (char_u *)"";
11247 if (!sortinfo->item_compare_numeric)
11248 {
11249 if (sortinfo->item_compare_ic)
11250 res = STRICMP(p1, p2);
11251 else
11252 res = STRCMP(p1, p2);
11253 }
11254 else
11255 {
11256 double n1, n2;
11257 n1 = strtod((char *)p1, (char **)&p1);
11258 n2 = strtod((char *)p2, (char **)&p2);
11259 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11260 }
11261
11262 /* When the result would be zero, compare the item indexes. Makes the
11263 * sort stable. */
11264 if (res == 0 && !sortinfo->item_compare_keep_zero)
11265 res = si1->idx > si2->idx ? 1 : -1;
11266
11267 vim_free(tofree1);
11268 vim_free(tofree2);
11269 return res;
11270}
11271
11272 static int
11273#ifdef __BORLANDC__
11274_RTLENTRYF
11275#endif
11276item_compare2(const void *s1, const void *s2)
11277{
11278 sortItem_T *si1, *si2;
11279 int res;
11280 typval_T rettv;
11281 typval_T argv[3];
11282 int dummy;
11283 char_u *func_name;
11284 partial_T *partial = sortinfo->item_compare_partial;
11285
11286 /* shortcut after failure in previous call; compare all items equal */
11287 if (sortinfo->item_compare_func_err)
11288 return 0;
11289
11290 si1 = (sortItem_T *)s1;
11291 si2 = (sortItem_T *)s2;
11292
11293 if (partial == NULL)
11294 func_name = sortinfo->item_compare_func;
11295 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011296 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011297
11298 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11299 * in the copy without changing the original list items. */
11300 copy_tv(&si1->item->li_tv, &argv[0]);
11301 copy_tv(&si2->item->li_tv, &argv[1]);
11302
11303 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11304 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011305 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011306 partial, sortinfo->item_compare_selfdict);
11307 clear_tv(&argv[0]);
11308 clear_tv(&argv[1]);
11309
11310 if (res == FAIL)
11311 res = ITEM_COMPARE_FAIL;
11312 else
11313 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11314 if (sortinfo->item_compare_func_err)
11315 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11316 clear_tv(&rettv);
11317
11318 /* When the result would be zero, compare the pointers themselves. Makes
11319 * the sort stable. */
11320 if (res == 0 && !sortinfo->item_compare_keep_zero)
11321 res = si1->idx > si2->idx ? 1 : -1;
11322
11323 return res;
11324}
11325
11326/*
11327 * "sort({list})" function
11328 */
11329 static void
11330do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11331{
11332 list_T *l;
11333 listitem_T *li;
11334 sortItem_T *ptrs;
11335 sortinfo_T *old_sortinfo;
11336 sortinfo_T info;
11337 long len;
11338 long i;
11339
11340 /* Pointer to current info struct used in compare function. Save and
11341 * restore the current one for nested calls. */
11342 old_sortinfo = sortinfo;
11343 sortinfo = &info;
11344
11345 if (argvars[0].v_type != VAR_LIST)
11346 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11347 else
11348 {
11349 l = argvars[0].vval.v_list;
11350 if (l == NULL || tv_check_lock(l->lv_lock,
11351 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11352 TRUE))
11353 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011354 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011355
11356 len = list_len(l);
11357 if (len <= 1)
11358 goto theend; /* short list sorts pretty quickly */
11359
11360 info.item_compare_ic = FALSE;
11361 info.item_compare_numeric = FALSE;
11362 info.item_compare_numbers = FALSE;
11363#ifdef FEAT_FLOAT
11364 info.item_compare_float = FALSE;
11365#endif
11366 info.item_compare_func = NULL;
11367 info.item_compare_partial = NULL;
11368 info.item_compare_selfdict = NULL;
11369 if (argvars[1].v_type != VAR_UNKNOWN)
11370 {
11371 /* optional second argument: {func} */
11372 if (argvars[1].v_type == VAR_FUNC)
11373 info.item_compare_func = argvars[1].vval.v_string;
11374 else if (argvars[1].v_type == VAR_PARTIAL)
11375 info.item_compare_partial = argvars[1].vval.v_partial;
11376 else
11377 {
11378 int error = FALSE;
11379
11380 i = (long)get_tv_number_chk(&argvars[1], &error);
11381 if (error)
11382 goto theend; /* type error; errmsg already given */
11383 if (i == 1)
11384 info.item_compare_ic = TRUE;
11385 else if (argvars[1].v_type != VAR_NUMBER)
11386 info.item_compare_func = get_tv_string(&argvars[1]);
11387 else if (i != 0)
11388 {
11389 EMSG(_(e_invarg));
11390 goto theend;
11391 }
11392 if (info.item_compare_func != NULL)
11393 {
11394 if (*info.item_compare_func == NUL)
11395 {
11396 /* empty string means default sort */
11397 info.item_compare_func = NULL;
11398 }
11399 else if (STRCMP(info.item_compare_func, "n") == 0)
11400 {
11401 info.item_compare_func = NULL;
11402 info.item_compare_numeric = TRUE;
11403 }
11404 else if (STRCMP(info.item_compare_func, "N") == 0)
11405 {
11406 info.item_compare_func = NULL;
11407 info.item_compare_numbers = TRUE;
11408 }
11409#ifdef FEAT_FLOAT
11410 else if (STRCMP(info.item_compare_func, "f") == 0)
11411 {
11412 info.item_compare_func = NULL;
11413 info.item_compare_float = TRUE;
11414 }
11415#endif
11416 else if (STRCMP(info.item_compare_func, "i") == 0)
11417 {
11418 info.item_compare_func = NULL;
11419 info.item_compare_ic = TRUE;
11420 }
11421 }
11422 }
11423
11424 if (argvars[2].v_type != VAR_UNKNOWN)
11425 {
11426 /* optional third argument: {dict} */
11427 if (argvars[2].v_type != VAR_DICT)
11428 {
11429 EMSG(_(e_dictreq));
11430 goto theend;
11431 }
11432 info.item_compare_selfdict = argvars[2].vval.v_dict;
11433 }
11434 }
11435
11436 /* Make an array with each entry pointing to an item in the List. */
11437 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11438 if (ptrs == NULL)
11439 goto theend;
11440
11441 i = 0;
11442 if (sort)
11443 {
11444 /* sort(): ptrs will be the list to sort */
11445 for (li = l->lv_first; li != NULL; li = li->li_next)
11446 {
11447 ptrs[i].item = li;
11448 ptrs[i].idx = i;
11449 ++i;
11450 }
11451
11452 info.item_compare_func_err = FALSE;
11453 info.item_compare_keep_zero = FALSE;
11454 /* test the compare function */
11455 if ((info.item_compare_func != NULL
11456 || info.item_compare_partial != NULL)
11457 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11458 == ITEM_COMPARE_FAIL)
11459 EMSG(_("E702: Sort compare function failed"));
11460 else
11461 {
11462 /* Sort the array with item pointers. */
11463 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11464 info.item_compare_func == NULL
11465 && info.item_compare_partial == NULL
11466 ? item_compare : item_compare2);
11467
11468 if (!info.item_compare_func_err)
11469 {
11470 /* Clear the List and append the items in sorted order. */
11471 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11472 l->lv_len = 0;
11473 for (i = 0; i < len; ++i)
11474 list_append(l, ptrs[i].item);
11475 }
11476 }
11477 }
11478 else
11479 {
11480 int (*item_compare_func_ptr)(const void *, const void *);
11481
11482 /* f_uniq(): ptrs will be a stack of items to remove */
11483 info.item_compare_func_err = FALSE;
11484 info.item_compare_keep_zero = TRUE;
11485 item_compare_func_ptr = info.item_compare_func != NULL
11486 || info.item_compare_partial != NULL
11487 ? item_compare2 : item_compare;
11488
11489 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11490 li = li->li_next)
11491 {
11492 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11493 == 0)
11494 ptrs[i++].item = li;
11495 if (info.item_compare_func_err)
11496 {
11497 EMSG(_("E882: Uniq compare function failed"));
11498 break;
11499 }
11500 }
11501
11502 if (!info.item_compare_func_err)
11503 {
11504 while (--i >= 0)
11505 {
11506 li = ptrs[i].item->li_next;
11507 ptrs[i].item->li_next = li->li_next;
11508 if (li->li_next != NULL)
11509 li->li_next->li_prev = ptrs[i].item;
11510 else
11511 l->lv_last = ptrs[i].item;
11512 list_fix_watch(l, li);
11513 listitem_free(li);
11514 l->lv_len--;
11515 }
11516 }
11517 }
11518
11519 vim_free(ptrs);
11520 }
11521theend:
11522 sortinfo = old_sortinfo;
11523}
11524
11525/*
11526 * "sort({list})" function
11527 */
11528 static void
11529f_sort(typval_T *argvars, typval_T *rettv)
11530{
11531 do_sort_uniq(argvars, rettv, TRUE);
11532}
11533
11534/*
11535 * "uniq({list})" function
11536 */
11537 static void
11538f_uniq(typval_T *argvars, typval_T *rettv)
11539{
11540 do_sort_uniq(argvars, rettv, FALSE);
11541}
11542
11543/*
11544 * "soundfold({word})" function
11545 */
11546 static void
11547f_soundfold(typval_T *argvars, typval_T *rettv)
11548{
11549 char_u *s;
11550
11551 rettv->v_type = VAR_STRING;
11552 s = get_tv_string(&argvars[0]);
11553#ifdef FEAT_SPELL
11554 rettv->vval.v_string = eval_soundfold(s);
11555#else
11556 rettv->vval.v_string = vim_strsave(s);
11557#endif
11558}
11559
11560/*
11561 * "spellbadword()" function
11562 */
11563 static void
11564f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11565{
11566 char_u *word = (char_u *)"";
11567 hlf_T attr = HLF_COUNT;
11568 int len = 0;
11569
11570 if (rettv_list_alloc(rettv) == FAIL)
11571 return;
11572
11573#ifdef FEAT_SPELL
11574 if (argvars[0].v_type == VAR_UNKNOWN)
11575 {
11576 /* Find the start and length of the badly spelled word. */
11577 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11578 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011579 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011580 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011581 curwin->w_set_curswant = TRUE;
11582 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011583 }
11584 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11585 {
11586 char_u *str = get_tv_string_chk(&argvars[0]);
11587 int capcol = -1;
11588
11589 if (str != NULL)
11590 {
11591 /* Check the argument for spelling. */
11592 while (*str != NUL)
11593 {
11594 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11595 if (attr != HLF_COUNT)
11596 {
11597 word = str;
11598 break;
11599 }
11600 str += len;
11601 }
11602 }
11603 }
11604#endif
11605
11606 list_append_string(rettv->vval.v_list, word, len);
11607 list_append_string(rettv->vval.v_list, (char_u *)(
11608 attr == HLF_SPB ? "bad" :
11609 attr == HLF_SPR ? "rare" :
11610 attr == HLF_SPL ? "local" :
11611 attr == HLF_SPC ? "caps" :
11612 ""), -1);
11613}
11614
11615/*
11616 * "spellsuggest()" function
11617 */
11618 static void
11619f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11620{
11621#ifdef FEAT_SPELL
11622 char_u *str;
11623 int typeerr = FALSE;
11624 int maxcount;
11625 garray_T ga;
11626 int i;
11627 listitem_T *li;
11628 int need_capital = FALSE;
11629#endif
11630
11631 if (rettv_list_alloc(rettv) == FAIL)
11632 return;
11633
11634#ifdef FEAT_SPELL
11635 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11636 {
11637 str = get_tv_string(&argvars[0]);
11638 if (argvars[1].v_type != VAR_UNKNOWN)
11639 {
11640 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11641 if (maxcount <= 0)
11642 return;
11643 if (argvars[2].v_type != VAR_UNKNOWN)
11644 {
11645 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11646 if (typeerr)
11647 return;
11648 }
11649 }
11650 else
11651 maxcount = 25;
11652
11653 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11654
11655 for (i = 0; i < ga.ga_len; ++i)
11656 {
11657 str = ((char_u **)ga.ga_data)[i];
11658
11659 li = listitem_alloc();
11660 if (li == NULL)
11661 vim_free(str);
11662 else
11663 {
11664 li->li_tv.v_type = VAR_STRING;
11665 li->li_tv.v_lock = 0;
11666 li->li_tv.vval.v_string = str;
11667 list_append(rettv->vval.v_list, li);
11668 }
11669 }
11670 ga_clear(&ga);
11671 }
11672#endif
11673}
11674
11675 static void
11676f_split(typval_T *argvars, typval_T *rettv)
11677{
11678 char_u *str;
11679 char_u *end;
11680 char_u *pat = NULL;
11681 regmatch_T regmatch;
11682 char_u patbuf[NUMBUFLEN];
11683 char_u *save_cpo;
11684 int match;
11685 colnr_T col = 0;
11686 int keepempty = FALSE;
11687 int typeerr = FALSE;
11688
11689 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11690 save_cpo = p_cpo;
11691 p_cpo = (char_u *)"";
11692
11693 str = get_tv_string(&argvars[0]);
11694 if (argvars[1].v_type != VAR_UNKNOWN)
11695 {
11696 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11697 if (pat == NULL)
11698 typeerr = TRUE;
11699 if (argvars[2].v_type != VAR_UNKNOWN)
11700 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11701 }
11702 if (pat == NULL || *pat == NUL)
11703 pat = (char_u *)"[\\x01- ]\\+";
11704
11705 if (rettv_list_alloc(rettv) == FAIL)
11706 return;
11707 if (typeerr)
11708 return;
11709
11710 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11711 if (regmatch.regprog != NULL)
11712 {
11713 regmatch.rm_ic = FALSE;
11714 while (*str != NUL || keepempty)
11715 {
11716 if (*str == NUL)
11717 match = FALSE; /* empty item at the end */
11718 else
11719 match = vim_regexec_nl(&regmatch, str, col);
11720 if (match)
11721 end = regmatch.startp[0];
11722 else
11723 end = str + STRLEN(str);
11724 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11725 && *str != NUL && match && end < regmatch.endp[0]))
11726 {
11727 if (list_append_string(rettv->vval.v_list, str,
11728 (int)(end - str)) == FAIL)
11729 break;
11730 }
11731 if (!match)
11732 break;
11733 /* Advance to just after the match. */
11734 if (regmatch.endp[0] > str)
11735 col = 0;
11736 else
11737 {
11738 /* Don't get stuck at the same match. */
11739#ifdef FEAT_MBYTE
11740 col = (*mb_ptr2len)(regmatch.endp[0]);
11741#else
11742 col = 1;
11743#endif
11744 }
11745 str = regmatch.endp[0];
11746 }
11747
11748 vim_regfree(regmatch.regprog);
11749 }
11750
11751 p_cpo = save_cpo;
11752}
11753
11754#ifdef FEAT_FLOAT
11755/*
11756 * "sqrt()" function
11757 */
11758 static void
11759f_sqrt(typval_T *argvars, typval_T *rettv)
11760{
11761 float_T f = 0.0;
11762
11763 rettv->v_type = VAR_FLOAT;
11764 if (get_float_arg(argvars, &f) == OK)
11765 rettv->vval.v_float = sqrt(f);
11766 else
11767 rettv->vval.v_float = 0.0;
11768}
11769
11770/*
11771 * "str2float()" function
11772 */
11773 static void
11774f_str2float(typval_T *argvars, typval_T *rettv)
11775{
11776 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011777 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011778
Bram Moolenaar08243d22017-01-10 16:12:29 +010011779 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011780 p = skipwhite(p + 1);
11781 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011782 if (isneg)
11783 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011784 rettv->v_type = VAR_FLOAT;
11785}
11786#endif
11787
11788/*
11789 * "str2nr()" function
11790 */
11791 static void
11792f_str2nr(typval_T *argvars, typval_T *rettv)
11793{
11794 int base = 10;
11795 char_u *p;
11796 varnumber_T n;
11797 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011798 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011799
11800 if (argvars[1].v_type != VAR_UNKNOWN)
11801 {
11802 base = (int)get_tv_number(&argvars[1]);
11803 if (base != 2 && base != 8 && base != 10 && base != 16)
11804 {
11805 EMSG(_(e_invarg));
11806 return;
11807 }
11808 }
11809
11810 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011811 isneg = (*p == '-');
11812 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011813 p = skipwhite(p + 1);
11814 switch (base)
11815 {
11816 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11817 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11818 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11819 default: what = 0;
11820 }
11821 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011822 if (isneg)
11823 rettv->vval.v_number = -n;
11824 else
11825 rettv->vval.v_number = n;
11826
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011827}
11828
11829#ifdef HAVE_STRFTIME
11830/*
11831 * "strftime({format}[, {time}])" function
11832 */
11833 static void
11834f_strftime(typval_T *argvars, typval_T *rettv)
11835{
11836 char_u result_buf[256];
11837 struct tm *curtime;
11838 time_t seconds;
11839 char_u *p;
11840
11841 rettv->v_type = VAR_STRING;
11842
11843 p = get_tv_string(&argvars[0]);
11844 if (argvars[1].v_type == VAR_UNKNOWN)
11845 seconds = time(NULL);
11846 else
11847 seconds = (time_t)get_tv_number(&argvars[1]);
11848 curtime = localtime(&seconds);
11849 /* MSVC returns NULL for an invalid value of seconds. */
11850 if (curtime == NULL)
11851 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11852 else
11853 {
11854# ifdef FEAT_MBYTE
11855 vimconv_T conv;
11856 char_u *enc;
11857
11858 conv.vc_type = CONV_NONE;
11859 enc = enc_locale();
11860 convert_setup(&conv, p_enc, enc);
11861 if (conv.vc_type != CONV_NONE)
11862 p = string_convert(&conv, p, NULL);
11863# endif
11864 if (p != NULL)
11865 (void)strftime((char *)result_buf, sizeof(result_buf),
11866 (char *)p, curtime);
11867 else
11868 result_buf[0] = NUL;
11869
11870# ifdef FEAT_MBYTE
11871 if (conv.vc_type != CONV_NONE)
11872 vim_free(p);
11873 convert_setup(&conv, enc, p_enc);
11874 if (conv.vc_type != CONV_NONE)
11875 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11876 else
11877# endif
11878 rettv->vval.v_string = vim_strsave(result_buf);
11879
11880# ifdef FEAT_MBYTE
11881 /* Release conversion descriptors */
11882 convert_setup(&conv, NULL, NULL);
11883 vim_free(enc);
11884# endif
11885 }
11886}
11887#endif
11888
11889/*
11890 * "strgetchar()" function
11891 */
11892 static void
11893f_strgetchar(typval_T *argvars, typval_T *rettv)
11894{
11895 char_u *str;
11896 int len;
11897 int error = FALSE;
11898 int charidx;
11899
11900 rettv->vval.v_number = -1;
11901 str = get_tv_string_chk(&argvars[0]);
11902 if (str == NULL)
11903 return;
11904 len = (int)STRLEN(str);
11905 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11906 if (error)
11907 return;
11908#ifdef FEAT_MBYTE
11909 {
11910 int byteidx = 0;
11911
11912 while (charidx >= 0 && byteidx < len)
11913 {
11914 if (charidx == 0)
11915 {
11916 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11917 break;
11918 }
11919 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011920 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011921 }
11922 }
11923#else
11924 if (charidx < len)
11925 rettv->vval.v_number = str[charidx];
11926#endif
11927}
11928
11929/*
11930 * "stridx()" function
11931 */
11932 static void
11933f_stridx(typval_T *argvars, typval_T *rettv)
11934{
11935 char_u buf[NUMBUFLEN];
11936 char_u *needle;
11937 char_u *haystack;
11938 char_u *save_haystack;
11939 char_u *pos;
11940 int start_idx;
11941
11942 needle = get_tv_string_chk(&argvars[1]);
11943 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11944 rettv->vval.v_number = -1;
11945 if (needle == NULL || haystack == NULL)
11946 return; /* type error; errmsg already given */
11947
11948 if (argvars[2].v_type != VAR_UNKNOWN)
11949 {
11950 int error = FALSE;
11951
11952 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11953 if (error || start_idx >= (int)STRLEN(haystack))
11954 return;
11955 if (start_idx >= 0)
11956 haystack += start_idx;
11957 }
11958
11959 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11960 if (pos != NULL)
11961 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11962}
11963
11964/*
11965 * "string()" function
11966 */
11967 static void
11968f_string(typval_T *argvars, typval_T *rettv)
11969{
11970 char_u *tofree;
11971 char_u numbuf[NUMBUFLEN];
11972
11973 rettv->v_type = VAR_STRING;
11974 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11975 get_copyID());
11976 /* Make a copy if we have a value but it's not in allocated memory. */
11977 if (rettv->vval.v_string != NULL && tofree == NULL)
11978 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11979}
11980
11981/*
11982 * "strlen()" function
11983 */
11984 static void
11985f_strlen(typval_T *argvars, typval_T *rettv)
11986{
11987 rettv->vval.v_number = (varnumber_T)(STRLEN(
11988 get_tv_string(&argvars[0])));
11989}
11990
11991/*
11992 * "strchars()" function
11993 */
11994 static void
11995f_strchars(typval_T *argvars, typval_T *rettv)
11996{
11997 char_u *s = get_tv_string(&argvars[0]);
11998 int skipcc = 0;
11999#ifdef FEAT_MBYTE
12000 varnumber_T len = 0;
12001 int (*func_mb_ptr2char_adv)(char_u **pp);
12002#endif
12003
12004 if (argvars[1].v_type != VAR_UNKNOWN)
12005 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
12006 if (skipcc < 0 || skipcc > 1)
12007 EMSG(_(e_invarg));
12008 else
12009 {
12010#ifdef FEAT_MBYTE
12011 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12012 while (*s != NUL)
12013 {
12014 func_mb_ptr2char_adv(&s);
12015 ++len;
12016 }
12017 rettv->vval.v_number = len;
12018#else
12019 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12020#endif
12021 }
12022}
12023
12024/*
12025 * "strdisplaywidth()" function
12026 */
12027 static void
12028f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12029{
12030 char_u *s = get_tv_string(&argvars[0]);
12031 int col = 0;
12032
12033 if (argvars[1].v_type != VAR_UNKNOWN)
12034 col = (int)get_tv_number(&argvars[1]);
12035
12036 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12037}
12038
12039/*
12040 * "strwidth()" function
12041 */
12042 static void
12043f_strwidth(typval_T *argvars, typval_T *rettv)
12044{
12045 char_u *s = get_tv_string(&argvars[0]);
12046
12047 rettv->vval.v_number = (varnumber_T)(
12048#ifdef FEAT_MBYTE
12049 mb_string2cells(s, -1)
12050#else
12051 STRLEN(s)
12052#endif
12053 );
12054}
12055
12056/*
12057 * "strcharpart()" function
12058 */
12059 static void
12060f_strcharpart(typval_T *argvars, typval_T *rettv)
12061{
12062#ifdef FEAT_MBYTE
12063 char_u *p;
12064 int nchar;
12065 int nbyte = 0;
12066 int charlen;
12067 int len = 0;
12068 int slen;
12069 int error = FALSE;
12070
12071 p = get_tv_string(&argvars[0]);
12072 slen = (int)STRLEN(p);
12073
12074 nchar = (int)get_tv_number_chk(&argvars[1], &error);
12075 if (!error)
12076 {
12077 if (nchar > 0)
12078 while (nchar > 0 && nbyte < slen)
12079 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012080 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012081 --nchar;
12082 }
12083 else
12084 nbyte = nchar;
12085 if (argvars[2].v_type != VAR_UNKNOWN)
12086 {
12087 charlen = (int)get_tv_number(&argvars[2]);
12088 while (charlen > 0 && nbyte + len < slen)
12089 {
12090 int off = nbyte + len;
12091
12092 if (off < 0)
12093 len += 1;
12094 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012095 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012096 --charlen;
12097 }
12098 }
12099 else
12100 len = slen - nbyte; /* default: all bytes that are available. */
12101 }
12102
12103 /*
12104 * Only return the overlap between the specified part and the actual
12105 * string.
12106 */
12107 if (nbyte < 0)
12108 {
12109 len += nbyte;
12110 nbyte = 0;
12111 }
12112 else if (nbyte > slen)
12113 nbyte = slen;
12114 if (len < 0)
12115 len = 0;
12116 else if (nbyte + len > slen)
12117 len = slen - nbyte;
12118
12119 rettv->v_type = VAR_STRING;
12120 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12121#else
12122 f_strpart(argvars, rettv);
12123#endif
12124}
12125
12126/*
12127 * "strpart()" function
12128 */
12129 static void
12130f_strpart(typval_T *argvars, typval_T *rettv)
12131{
12132 char_u *p;
12133 int n;
12134 int len;
12135 int slen;
12136 int error = FALSE;
12137
12138 p = get_tv_string(&argvars[0]);
12139 slen = (int)STRLEN(p);
12140
12141 n = (int)get_tv_number_chk(&argvars[1], &error);
12142 if (error)
12143 len = 0;
12144 else if (argvars[2].v_type != VAR_UNKNOWN)
12145 len = (int)get_tv_number(&argvars[2]);
12146 else
12147 len = slen - n; /* default len: all bytes that are available. */
12148
12149 /*
12150 * Only return the overlap between the specified part and the actual
12151 * string.
12152 */
12153 if (n < 0)
12154 {
12155 len += n;
12156 n = 0;
12157 }
12158 else if (n > slen)
12159 n = slen;
12160 if (len < 0)
12161 len = 0;
12162 else if (n + len > slen)
12163 len = slen - n;
12164
12165 rettv->v_type = VAR_STRING;
12166 rettv->vval.v_string = vim_strnsave(p + n, len);
12167}
12168
12169/*
12170 * "strridx()" function
12171 */
12172 static void
12173f_strridx(typval_T *argvars, typval_T *rettv)
12174{
12175 char_u buf[NUMBUFLEN];
12176 char_u *needle;
12177 char_u *haystack;
12178 char_u *rest;
12179 char_u *lastmatch = NULL;
12180 int haystack_len, end_idx;
12181
12182 needle = get_tv_string_chk(&argvars[1]);
12183 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12184
12185 rettv->vval.v_number = -1;
12186 if (needle == NULL || haystack == NULL)
12187 return; /* type error; errmsg already given */
12188
12189 haystack_len = (int)STRLEN(haystack);
12190 if (argvars[2].v_type != VAR_UNKNOWN)
12191 {
12192 /* Third argument: upper limit for index */
12193 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12194 if (end_idx < 0)
12195 return; /* can never find a match */
12196 }
12197 else
12198 end_idx = haystack_len;
12199
12200 if (*needle == NUL)
12201 {
12202 /* Empty string matches past the end. */
12203 lastmatch = haystack + end_idx;
12204 }
12205 else
12206 {
12207 for (rest = haystack; *rest != '\0'; ++rest)
12208 {
12209 rest = (char_u *)strstr((char *)rest, (char *)needle);
12210 if (rest == NULL || rest > haystack + end_idx)
12211 break;
12212 lastmatch = rest;
12213 }
12214 }
12215
12216 if (lastmatch == NULL)
12217 rettv->vval.v_number = -1;
12218 else
12219 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12220}
12221
12222/*
12223 * "strtrans()" function
12224 */
12225 static void
12226f_strtrans(typval_T *argvars, typval_T *rettv)
12227{
12228 rettv->v_type = VAR_STRING;
12229 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12230}
12231
12232/*
12233 * "submatch()" function
12234 */
12235 static void
12236f_submatch(typval_T *argvars, typval_T *rettv)
12237{
12238 int error = FALSE;
12239 int no;
12240 int retList = 0;
12241
12242 no = (int)get_tv_number_chk(&argvars[0], &error);
12243 if (error)
12244 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012245 if (no < 0 || no >= NSUBEXP)
12246 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012247 EMSGN(_("E935: invalid submatch number: %d"), no);
12248 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012249 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012250 if (argvars[1].v_type != VAR_UNKNOWN)
12251 retList = (int)get_tv_number_chk(&argvars[1], &error);
12252 if (error)
12253 return;
12254
12255 if (retList == 0)
12256 {
12257 rettv->v_type = VAR_STRING;
12258 rettv->vval.v_string = reg_submatch(no);
12259 }
12260 else
12261 {
12262 rettv->v_type = VAR_LIST;
12263 rettv->vval.v_list = reg_submatch_list(no);
12264 }
12265}
12266
12267/*
12268 * "substitute()" function
12269 */
12270 static void
12271f_substitute(typval_T *argvars, typval_T *rettv)
12272{
12273 char_u patbuf[NUMBUFLEN];
12274 char_u subbuf[NUMBUFLEN];
12275 char_u flagsbuf[NUMBUFLEN];
12276
12277 char_u *str = get_tv_string_chk(&argvars[0]);
12278 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012279 char_u *sub = NULL;
12280 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012281 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12282
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012283 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12284 expr = &argvars[2];
12285 else
12286 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12287
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012288 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012289 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12290 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012291 rettv->vval.v_string = NULL;
12292 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012293 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012294}
12295
12296/*
12297 * "synID(lnum, col, trans)" function
12298 */
12299 static void
12300f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12301{
12302 int id = 0;
12303#ifdef FEAT_SYN_HL
12304 linenr_T lnum;
12305 colnr_T col;
12306 int trans;
12307 int transerr = FALSE;
12308
12309 lnum = get_tv_lnum(argvars); /* -1 on type error */
12310 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12311 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12312
12313 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12314 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12315 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12316#endif
12317
12318 rettv->vval.v_number = id;
12319}
12320
12321/*
12322 * "synIDattr(id, what [, mode])" function
12323 */
12324 static void
12325f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12326{
12327 char_u *p = NULL;
12328#ifdef FEAT_SYN_HL
12329 int id;
12330 char_u *what;
12331 char_u *mode;
12332 char_u modebuf[NUMBUFLEN];
12333 int modec;
12334
12335 id = (int)get_tv_number(&argvars[0]);
12336 what = get_tv_string(&argvars[1]);
12337 if (argvars[2].v_type != VAR_UNKNOWN)
12338 {
12339 mode = get_tv_string_buf(&argvars[2], modebuf);
12340 modec = TOLOWER_ASC(mode[0]);
12341 if (modec != 't' && modec != 'c' && modec != 'g')
12342 modec = 0; /* replace invalid with current */
12343 }
12344 else
12345 {
12346#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12347 if (USE_24BIT)
12348 modec = 'g';
12349 else
12350#endif
12351 if (t_colors > 1)
12352 modec = 'c';
12353 else
12354 modec = 't';
12355 }
12356
12357
12358 switch (TOLOWER_ASC(what[0]))
12359 {
12360 case 'b':
12361 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12362 p = highlight_color(id, what, modec);
12363 else /* bold */
12364 p = highlight_has_attr(id, HL_BOLD, modec);
12365 break;
12366
12367 case 'f': /* fg[#] or font */
12368 p = highlight_color(id, what, modec);
12369 break;
12370
12371 case 'i':
12372 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12373 p = highlight_has_attr(id, HL_INVERSE, modec);
12374 else /* italic */
12375 p = highlight_has_attr(id, HL_ITALIC, modec);
12376 break;
12377
12378 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012379 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012380 break;
12381
12382 case 'r': /* reverse */
12383 p = highlight_has_attr(id, HL_INVERSE, modec);
12384 break;
12385
12386 case 's':
12387 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12388 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012389 /* strikeout */
12390 else if (TOLOWER_ASC(what[1]) == 't' &&
12391 TOLOWER_ASC(what[2]) == 'r')
12392 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012393 else /* standout */
12394 p = highlight_has_attr(id, HL_STANDOUT, modec);
12395 break;
12396
12397 case 'u':
12398 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12399 /* underline */
12400 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12401 else
12402 /* undercurl */
12403 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12404 break;
12405 }
12406
12407 if (p != NULL)
12408 p = vim_strsave(p);
12409#endif
12410 rettv->v_type = VAR_STRING;
12411 rettv->vval.v_string = p;
12412}
12413
12414/*
12415 * "synIDtrans(id)" function
12416 */
12417 static void
12418f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12419{
12420 int id;
12421
12422#ifdef FEAT_SYN_HL
12423 id = (int)get_tv_number(&argvars[0]);
12424
12425 if (id > 0)
12426 id = syn_get_final_id(id);
12427 else
12428#endif
12429 id = 0;
12430
12431 rettv->vval.v_number = id;
12432}
12433
12434/*
12435 * "synconcealed(lnum, col)" function
12436 */
12437 static void
12438f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12439{
12440#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12441 linenr_T lnum;
12442 colnr_T col;
12443 int syntax_flags = 0;
12444 int cchar;
12445 int matchid = 0;
12446 char_u str[NUMBUFLEN];
12447#endif
12448
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012449 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450
12451#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12452 lnum = get_tv_lnum(argvars); /* -1 on type error */
12453 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12454
12455 vim_memset(str, NUL, sizeof(str));
12456
12457 if (rettv_list_alloc(rettv) != FAIL)
12458 {
12459 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12460 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12461 && curwin->w_p_cole > 0)
12462 {
12463 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12464 syntax_flags = get_syntax_info(&matchid);
12465
12466 /* get the conceal character */
12467 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12468 {
12469 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012470 if (cchar == NUL && curwin->w_p_cole == 1)
12471 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012472 if (cchar != NUL)
12473 {
12474# ifdef FEAT_MBYTE
12475 if (has_mbyte)
12476 (*mb_char2bytes)(cchar, str);
12477 else
12478# endif
12479 str[0] = cchar;
12480 }
12481 }
12482 }
12483
12484 list_append_number(rettv->vval.v_list,
12485 (syntax_flags & HL_CONCEAL) != 0);
12486 /* -1 to auto-determine strlen */
12487 list_append_string(rettv->vval.v_list, str, -1);
12488 list_append_number(rettv->vval.v_list, matchid);
12489 }
12490#endif
12491}
12492
12493/*
12494 * "synstack(lnum, col)" function
12495 */
12496 static void
12497f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12498{
12499#ifdef FEAT_SYN_HL
12500 linenr_T lnum;
12501 colnr_T col;
12502 int i;
12503 int id;
12504#endif
12505
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012506 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012507
12508#ifdef FEAT_SYN_HL
12509 lnum = get_tv_lnum(argvars); /* -1 on type error */
12510 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12511
12512 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12513 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12514 && rettv_list_alloc(rettv) != FAIL)
12515 {
12516 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12517 for (i = 0; ; ++i)
12518 {
12519 id = syn_get_stack_item(i);
12520 if (id < 0)
12521 break;
12522 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12523 break;
12524 }
12525 }
12526#endif
12527}
12528
12529 static void
12530get_cmd_output_as_rettv(
12531 typval_T *argvars,
12532 typval_T *rettv,
12533 int retlist)
12534{
12535 char_u *res = NULL;
12536 char_u *p;
12537 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012538 int err = FALSE;
12539 FILE *fd;
12540 list_T *list = NULL;
12541 int flags = SHELL_SILENT;
12542
12543 rettv->v_type = VAR_STRING;
12544 rettv->vval.v_string = NULL;
12545 if (check_restricted() || check_secure())
12546 goto errret;
12547
12548 if (argvars[1].v_type != VAR_UNKNOWN)
12549 {
12550 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012551 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012552 * command.
12553 */
12554 if ((infile = vim_tempname('i', TRUE)) == NULL)
12555 {
12556 EMSG(_(e_notmp));
12557 goto errret;
12558 }
12559
12560 fd = mch_fopen((char *)infile, WRITEBIN);
12561 if (fd == NULL)
12562 {
12563 EMSG2(_(e_notopen), infile);
12564 goto errret;
12565 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012566 if (argvars[1].v_type == VAR_NUMBER)
12567 {
12568 linenr_T lnum;
12569 buf_T *buf;
12570
12571 buf = buflist_findnr(argvars[1].vval.v_number);
12572 if (buf == NULL)
12573 {
12574 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012575 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012576 goto errret;
12577 }
12578
12579 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12580 {
12581 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12582 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12583 {
12584 err = TRUE;
12585 break;
12586 }
12587 if (putc(NL, fd) == EOF)
12588 {
12589 err = TRUE;
12590 break;
12591 }
12592 }
12593 }
12594 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012595 {
12596 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12597 err = TRUE;
12598 }
12599 else
12600 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012601 size_t len;
12602 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012603
12604 p = get_tv_string_buf_chk(&argvars[1], buf);
12605 if (p == NULL)
12606 {
12607 fclose(fd);
12608 goto errret; /* type error; errmsg already given */
12609 }
12610 len = STRLEN(p);
12611 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12612 err = TRUE;
12613 }
12614 if (fclose(fd) != 0)
12615 err = TRUE;
12616 if (err)
12617 {
12618 EMSG(_("E677: Error writing temp file"));
12619 goto errret;
12620 }
12621 }
12622
12623 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12624 * echoes typeahead, that messes up the display. */
12625 if (!msg_silent)
12626 flags += SHELL_COOKED;
12627
12628 if (retlist)
12629 {
12630 int len;
12631 listitem_T *li;
12632 char_u *s = NULL;
12633 char_u *start;
12634 char_u *end;
12635 int i;
12636
12637 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12638 if (res == NULL)
12639 goto errret;
12640
12641 list = list_alloc();
12642 if (list == NULL)
12643 goto errret;
12644
12645 for (i = 0; i < len; ++i)
12646 {
12647 start = res + i;
12648 while (i < len && res[i] != NL)
12649 ++i;
12650 end = res + i;
12651
12652 s = alloc((unsigned)(end - start + 1));
12653 if (s == NULL)
12654 goto errret;
12655
12656 for (p = s; start < end; ++p, ++start)
12657 *p = *start == NUL ? NL : *start;
12658 *p = NUL;
12659
12660 li = listitem_alloc();
12661 if (li == NULL)
12662 {
12663 vim_free(s);
12664 goto errret;
12665 }
12666 li->li_tv.v_type = VAR_STRING;
12667 li->li_tv.v_lock = 0;
12668 li->li_tv.vval.v_string = s;
12669 list_append(list, li);
12670 }
12671
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012672 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012673 list = NULL;
12674 }
12675 else
12676 {
12677 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12678#ifdef USE_CR
12679 /* translate <CR> into <NL> */
12680 if (res != NULL)
12681 {
12682 char_u *s;
12683
12684 for (s = res; *s; ++s)
12685 {
12686 if (*s == CAR)
12687 *s = NL;
12688 }
12689 }
12690#else
12691# ifdef USE_CRNL
12692 /* translate <CR><NL> into <NL> */
12693 if (res != NULL)
12694 {
12695 char_u *s, *d;
12696
12697 d = res;
12698 for (s = res; *s; ++s)
12699 {
12700 if (s[0] == CAR && s[1] == NL)
12701 ++s;
12702 *d++ = *s;
12703 }
12704 *d = NUL;
12705 }
12706# endif
12707#endif
12708 rettv->vval.v_string = res;
12709 res = NULL;
12710 }
12711
12712errret:
12713 if (infile != NULL)
12714 {
12715 mch_remove(infile);
12716 vim_free(infile);
12717 }
12718 if (res != NULL)
12719 vim_free(res);
12720 if (list != NULL)
12721 list_free(list);
12722}
12723
12724/*
12725 * "system()" function
12726 */
12727 static void
12728f_system(typval_T *argvars, typval_T *rettv)
12729{
12730 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12731}
12732
12733/*
12734 * "systemlist()" function
12735 */
12736 static void
12737f_systemlist(typval_T *argvars, typval_T *rettv)
12738{
12739 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12740}
12741
12742/*
12743 * "tabpagebuflist()" function
12744 */
12745 static void
12746f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12747{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012748 tabpage_T *tp;
12749 win_T *wp = NULL;
12750
12751 if (argvars[0].v_type == VAR_UNKNOWN)
12752 wp = firstwin;
12753 else
12754 {
12755 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12756 if (tp != NULL)
12757 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12758 }
12759 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12760 {
12761 for (; wp != NULL; wp = wp->w_next)
12762 if (list_append_number(rettv->vval.v_list,
12763 wp->w_buffer->b_fnum) == FAIL)
12764 break;
12765 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012766}
12767
12768
12769/*
12770 * "tabpagenr()" function
12771 */
12772 static void
12773f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12774{
12775 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012776 char_u *arg;
12777
12778 if (argvars[0].v_type != VAR_UNKNOWN)
12779 {
12780 arg = get_tv_string_chk(&argvars[0]);
12781 nr = 0;
12782 if (arg != NULL)
12783 {
12784 if (STRCMP(arg, "$") == 0)
12785 nr = tabpage_index(NULL) - 1;
12786 else
12787 EMSG2(_(e_invexpr2), arg);
12788 }
12789 }
12790 else
12791 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012792 rettv->vval.v_number = nr;
12793}
12794
12795
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012796static int get_winnr(tabpage_T *tp, typval_T *argvar);
12797
12798/*
12799 * Common code for tabpagewinnr() and winnr().
12800 */
12801 static int
12802get_winnr(tabpage_T *tp, typval_T *argvar)
12803{
12804 win_T *twin;
12805 int nr = 1;
12806 win_T *wp;
12807 char_u *arg;
12808
12809 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12810 if (argvar->v_type != VAR_UNKNOWN)
12811 {
12812 arg = get_tv_string_chk(argvar);
12813 if (arg == NULL)
12814 nr = 0; /* type error; errmsg already given */
12815 else if (STRCMP(arg, "$") == 0)
12816 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12817 else if (STRCMP(arg, "#") == 0)
12818 {
12819 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12820 if (twin == NULL)
12821 nr = 0;
12822 }
12823 else
12824 {
12825 EMSG2(_(e_invexpr2), arg);
12826 nr = 0;
12827 }
12828 }
12829
12830 if (nr > 0)
12831 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12832 wp != twin; wp = wp->w_next)
12833 {
12834 if (wp == NULL)
12835 {
12836 /* didn't find it in this tabpage */
12837 nr = 0;
12838 break;
12839 }
12840 ++nr;
12841 }
12842 return nr;
12843}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012844
12845/*
12846 * "tabpagewinnr()" function
12847 */
12848 static void
12849f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12850{
12851 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012852 tabpage_T *tp;
12853
12854 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12855 if (tp == NULL)
12856 nr = 0;
12857 else
12858 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012859 rettv->vval.v_number = nr;
12860}
12861
12862
12863/*
12864 * "tagfiles()" function
12865 */
12866 static void
12867f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12868{
12869 char_u *fname;
12870 tagname_T tn;
12871 int first;
12872
12873 if (rettv_list_alloc(rettv) == FAIL)
12874 return;
12875 fname = alloc(MAXPATHL);
12876 if (fname == NULL)
12877 return;
12878
12879 for (first = TRUE; ; first = FALSE)
12880 if (get_tagfname(&tn, first, fname) == FAIL
12881 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12882 break;
12883 tagname_free(&tn);
12884 vim_free(fname);
12885}
12886
12887/*
12888 * "taglist()" function
12889 */
12890 static void
12891f_taglist(typval_T *argvars, typval_T *rettv)
12892{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012893 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012894 char_u *tag_pattern;
12895
12896 tag_pattern = get_tv_string(&argvars[0]);
12897
12898 rettv->vval.v_number = FALSE;
12899 if (*tag_pattern == NUL)
12900 return;
12901
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012902 if (argvars[1].v_type != VAR_UNKNOWN)
12903 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012904 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012905 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012906}
12907
12908/*
12909 * "tempname()" function
12910 */
12911 static void
12912f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12913{
12914 static int x = 'A';
12915
12916 rettv->v_type = VAR_STRING;
12917 rettv->vval.v_string = vim_tempname(x, FALSE);
12918
12919 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12920 * names. Skip 'I' and 'O', they are used for shell redirection. */
12921 do
12922 {
12923 if (x == 'Z')
12924 x = '0';
12925 else if (x == '9')
12926 x = 'A';
12927 else
12928 {
12929#ifdef EBCDIC
12930 if (x == 'I')
12931 x = 'J';
12932 else if (x == 'R')
12933 x = 'S';
12934 else
12935#endif
12936 ++x;
12937 }
12938 } while (x == 'I' || x == 'O');
12939}
12940
12941#ifdef FEAT_FLOAT
12942/*
12943 * "tan()" function
12944 */
12945 static void
12946f_tan(typval_T *argvars, typval_T *rettv)
12947{
12948 float_T f = 0.0;
12949
12950 rettv->v_type = VAR_FLOAT;
12951 if (get_float_arg(argvars, &f) == OK)
12952 rettv->vval.v_float = tan(f);
12953 else
12954 rettv->vval.v_float = 0.0;
12955}
12956
12957/*
12958 * "tanh()" function
12959 */
12960 static void
12961f_tanh(typval_T *argvars, typval_T *rettv)
12962{
12963 float_T f = 0.0;
12964
12965 rettv->v_type = VAR_FLOAT;
12966 if (get_float_arg(argvars, &f) == OK)
12967 rettv->vval.v_float = tanh(f);
12968 else
12969 rettv->vval.v_float = 0.0;
12970}
12971#endif
12972
12973/*
12974 * "test_alloc_fail(id, countdown, repeat)" function
12975 */
12976 static void
12977f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12978{
12979 if (argvars[0].v_type != VAR_NUMBER
12980 || argvars[0].vval.v_number <= 0
12981 || argvars[1].v_type != VAR_NUMBER
12982 || argvars[1].vval.v_number < 0
12983 || argvars[2].v_type != VAR_NUMBER)
12984 EMSG(_(e_invarg));
12985 else
12986 {
12987 alloc_fail_id = argvars[0].vval.v_number;
12988 if (alloc_fail_id >= aid_last)
12989 EMSG(_(e_invarg));
12990 alloc_fail_countdown = argvars[1].vval.v_number;
12991 alloc_fail_repeat = argvars[2].vval.v_number;
12992 did_outofmem_msg = FALSE;
12993 }
12994}
12995
12996/*
12997 * "test_autochdir()"
12998 */
12999 static void
13000f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13001{
13002#if defined(FEAT_AUTOCHDIR)
13003 test_autochdir = TRUE;
13004#endif
13005}
13006
13007/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013008 * "test_feedinput()"
13009 */
13010 static void
13011f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13012{
13013#ifdef USE_INPUT_BUF
13014 char_u *val = get_tv_string_chk(&argvars[0]);
13015
13016 if (val != NULL)
13017 {
13018 trash_input_buf();
13019 add_to_input_buf_csi(val, (int)STRLEN(val));
13020 }
13021#endif
13022}
13023
13024/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013025 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013026 */
13027 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013028f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013029{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013030 char_u *name = (char_u *)"";
13031 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013032 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013033
13034 if (argvars[0].v_type != VAR_STRING
13035 || (argvars[1].v_type) != VAR_NUMBER)
13036 EMSG(_(e_invarg));
13037 else
13038 {
13039 name = get_tv_string_chk(&argvars[0]);
13040 val = (int)get_tv_number(&argvars[1]);
13041
13042 if (STRCMP(name, (char_u *)"redraw") == 0)
13043 disable_redraw_for_testing = val;
13044 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13045 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013046 else if (STRCMP(name, (char_u *)"starting") == 0)
13047 {
13048 if (val)
13049 {
13050 if (save_starting < 0)
13051 save_starting = starting;
13052 starting = 0;
13053 }
13054 else
13055 {
13056 starting = save_starting;
13057 save_starting = -1;
13058 }
13059 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013060 else if (STRCMP(name, (char_u *)"ALL") == 0)
13061 {
13062 disable_char_avail_for_testing = FALSE;
13063 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013064 if (save_starting >= 0)
13065 {
13066 starting = save_starting;
13067 save_starting = -1;
13068 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013069 }
13070 else
13071 EMSG2(_(e_invarg2), name);
13072 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013073}
13074
13075/*
13076 * "test_garbagecollect_now()" function
13077 */
13078 static void
13079f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13080{
13081 /* This is dangerous, any Lists and Dicts used internally may be freed
13082 * while still in use. */
13083 garbage_collect(TRUE);
13084}
13085
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013086/*
13087 * "test_ignore_error()" function
13088 */
13089 static void
13090f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13091{
13092 ignore_error_for_testing(get_tv_string(&argvars[0]));
13093}
13094
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013095#ifdef FEAT_JOB_CHANNEL
13096 static void
13097f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13098{
13099 rettv->v_type = VAR_CHANNEL;
13100 rettv->vval.v_channel = NULL;
13101}
13102#endif
13103
13104 static void
13105f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13106{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013107 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013108}
13109
13110#ifdef FEAT_JOB_CHANNEL
13111 static void
13112f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13113{
13114 rettv->v_type = VAR_JOB;
13115 rettv->vval.v_job = NULL;
13116}
13117#endif
13118
13119 static void
13120f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13121{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013122 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013123}
13124
13125 static void
13126f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13127{
13128 rettv->v_type = VAR_PARTIAL;
13129 rettv->vval.v_partial = NULL;
13130}
13131
13132 static void
13133f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13134{
13135 rettv->v_type = VAR_STRING;
13136 rettv->vval.v_string = NULL;
13137}
13138
13139 static void
13140f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13141{
13142 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13143}
13144
13145#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13146/*
13147 * Get a callback from "arg". It can be a Funcref or a function name.
13148 * When "arg" is zero return an empty string.
13149 * Return NULL for an invalid argument.
13150 */
13151 char_u *
13152get_callback(typval_T *arg, partial_T **pp)
13153{
13154 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13155 {
13156 *pp = arg->vval.v_partial;
13157 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013158 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013159 }
13160 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013161 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013162 {
13163 func_ref(arg->vval.v_string);
13164 return arg->vval.v_string;
13165 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013166 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13167 return (char_u *)"";
13168 EMSG(_("E921: Invalid callback argument"));
13169 return NULL;
13170}
13171
13172/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013173 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013174 */
13175 void
13176free_callback(char_u *callback, partial_T *partial)
13177{
13178 if (partial != NULL)
13179 partial_unref(partial);
13180 else if (callback != NULL)
13181 {
13182 func_unref(callback);
13183 vim_free(callback);
13184 }
13185}
13186#endif
13187
13188#ifdef FEAT_TIMERS
13189/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013190 * "timer_info([timer])" function
13191 */
13192 static void
13193f_timer_info(typval_T *argvars, typval_T *rettv)
13194{
13195 timer_T *timer = NULL;
13196
13197 if (rettv_list_alloc(rettv) != OK)
13198 return;
13199 if (argvars[0].v_type != VAR_UNKNOWN)
13200 {
13201 if (argvars[0].v_type != VAR_NUMBER)
13202 EMSG(_(e_number_exp));
13203 else
13204 {
13205 timer = find_timer((int)get_tv_number(&argvars[0]));
13206 if (timer != NULL)
13207 add_timer_info(rettv, timer);
13208 }
13209 }
13210 else
13211 add_timer_info_all(rettv);
13212}
13213
13214/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013215 * "timer_pause(timer, paused)" function
13216 */
13217 static void
13218f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13219{
13220 timer_T *timer = NULL;
13221 int paused = (int)get_tv_number(&argvars[1]);
13222
13223 if (argvars[0].v_type != VAR_NUMBER)
13224 EMSG(_(e_number_exp));
13225 else
13226 {
13227 timer = find_timer((int)get_tv_number(&argvars[0]));
13228 if (timer != NULL)
13229 timer->tr_paused = paused;
13230 }
13231}
13232
13233/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013234 * "timer_start(time, callback [, options])" function
13235 */
13236 static void
13237f_timer_start(typval_T *argvars, typval_T *rettv)
13238{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013239 long msec = (long)get_tv_number(&argvars[0]);
13240 timer_T *timer;
13241 int repeat = 0;
13242 char_u *callback;
13243 dict_T *dict;
13244 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013245
Bram Moolenaar75537a92016-09-05 22:45:28 +020013246 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013247 if (check_secure())
13248 return;
13249 if (argvars[2].v_type != VAR_UNKNOWN)
13250 {
13251 if (argvars[2].v_type != VAR_DICT
13252 || (dict = argvars[2].vval.v_dict) == NULL)
13253 {
13254 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13255 return;
13256 }
13257 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13258 repeat = get_dict_number(dict, (char_u *)"repeat");
13259 }
13260
Bram Moolenaar75537a92016-09-05 22:45:28 +020013261 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013262 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013263 return;
13264
13265 timer = create_timer(msec, repeat);
13266 if (timer == NULL)
13267 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013268 else
13269 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013270 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013271 timer->tr_callback = vim_strsave(callback);
13272 else
13273 /* pointer into the partial */
13274 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013275 timer->tr_partial = partial;
13276 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013277 }
13278}
13279
13280/*
13281 * "timer_stop(timer)" function
13282 */
13283 static void
13284f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13285{
13286 timer_T *timer;
13287
13288 if (argvars[0].v_type != VAR_NUMBER)
13289 {
13290 EMSG(_(e_number_exp));
13291 return;
13292 }
13293 timer = find_timer((int)get_tv_number(&argvars[0]));
13294 if (timer != NULL)
13295 stop_timer(timer);
13296}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013297
13298/*
13299 * "timer_stopall()" function
13300 */
13301 static void
13302f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13303{
13304 stop_all_timers();
13305}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013306#endif
13307
13308/*
13309 * "tolower(string)" function
13310 */
13311 static void
13312f_tolower(typval_T *argvars, typval_T *rettv)
13313{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013314 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013315 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013316}
13317
13318/*
13319 * "toupper(string)" function
13320 */
13321 static void
13322f_toupper(typval_T *argvars, typval_T *rettv)
13323{
13324 rettv->v_type = VAR_STRING;
13325 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13326}
13327
13328/*
13329 * "tr(string, fromstr, tostr)" function
13330 */
13331 static void
13332f_tr(typval_T *argvars, typval_T *rettv)
13333{
13334 char_u *in_str;
13335 char_u *fromstr;
13336 char_u *tostr;
13337 char_u *p;
13338#ifdef FEAT_MBYTE
13339 int inlen;
13340 int fromlen;
13341 int tolen;
13342 int idx;
13343 char_u *cpstr;
13344 int cplen;
13345 int first = TRUE;
13346#endif
13347 char_u buf[NUMBUFLEN];
13348 char_u buf2[NUMBUFLEN];
13349 garray_T ga;
13350
13351 in_str = get_tv_string(&argvars[0]);
13352 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13353 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13354
13355 /* Default return value: empty string. */
13356 rettv->v_type = VAR_STRING;
13357 rettv->vval.v_string = NULL;
13358 if (fromstr == NULL || tostr == NULL)
13359 return; /* type error; errmsg already given */
13360 ga_init2(&ga, (int)sizeof(char), 80);
13361
13362#ifdef FEAT_MBYTE
13363 if (!has_mbyte)
13364#endif
13365 /* not multi-byte: fromstr and tostr must be the same length */
13366 if (STRLEN(fromstr) != STRLEN(tostr))
13367 {
13368#ifdef FEAT_MBYTE
13369error:
13370#endif
13371 EMSG2(_(e_invarg2), fromstr);
13372 ga_clear(&ga);
13373 return;
13374 }
13375
13376 /* fromstr and tostr have to contain the same number of chars */
13377 while (*in_str != NUL)
13378 {
13379#ifdef FEAT_MBYTE
13380 if (has_mbyte)
13381 {
13382 inlen = (*mb_ptr2len)(in_str);
13383 cpstr = in_str;
13384 cplen = inlen;
13385 idx = 0;
13386 for (p = fromstr; *p != NUL; p += fromlen)
13387 {
13388 fromlen = (*mb_ptr2len)(p);
13389 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13390 {
13391 for (p = tostr; *p != NUL; p += tolen)
13392 {
13393 tolen = (*mb_ptr2len)(p);
13394 if (idx-- == 0)
13395 {
13396 cplen = tolen;
13397 cpstr = p;
13398 break;
13399 }
13400 }
13401 if (*p == NUL) /* tostr is shorter than fromstr */
13402 goto error;
13403 break;
13404 }
13405 ++idx;
13406 }
13407
13408 if (first && cpstr == in_str)
13409 {
13410 /* Check that fromstr and tostr have the same number of
13411 * (multi-byte) characters. Done only once when a character
13412 * of in_str doesn't appear in fromstr. */
13413 first = FALSE;
13414 for (p = tostr; *p != NUL; p += tolen)
13415 {
13416 tolen = (*mb_ptr2len)(p);
13417 --idx;
13418 }
13419 if (idx != 0)
13420 goto error;
13421 }
13422
13423 (void)ga_grow(&ga, cplen);
13424 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13425 ga.ga_len += cplen;
13426
13427 in_str += inlen;
13428 }
13429 else
13430#endif
13431 {
13432 /* When not using multi-byte chars we can do it faster. */
13433 p = vim_strchr(fromstr, *in_str);
13434 if (p != NULL)
13435 ga_append(&ga, tostr[p - fromstr]);
13436 else
13437 ga_append(&ga, *in_str);
13438 ++in_str;
13439 }
13440 }
13441
13442 /* add a terminating NUL */
13443 (void)ga_grow(&ga, 1);
13444 ga_append(&ga, NUL);
13445
13446 rettv->vval.v_string = ga.ga_data;
13447}
13448
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013449/*
13450 * "trim({expr})" function
13451 */
13452 static void
13453f_trim(typval_T *argvars, typval_T *rettv)
13454{
13455 char_u buf1[NUMBUFLEN];
13456 char_u buf2[NUMBUFLEN];
13457 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13458 char_u *mask = NULL;
13459 char_u *tail;
13460 char_u *prev;
13461 char_u *p;
13462 int c1;
13463
13464 rettv->v_type = VAR_STRING;
13465 if (head == NULL)
13466 {
13467 rettv->vval.v_string = NULL;
13468 return;
13469 }
13470
13471 if (argvars[1].v_type == VAR_STRING)
13472 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13473
13474 while (*head != NUL)
13475 {
13476 c1 = PTR2CHAR(head);
13477 if (mask == NULL)
13478 {
13479 if (c1 > ' ' && c1 != 0xa0)
13480 break;
13481 }
13482 else
13483 {
13484 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13485 if (c1 == PTR2CHAR(p))
13486 break;
13487 if (*p == NUL)
13488 break;
13489 }
13490 MB_PTR_ADV(head);
13491 }
13492
13493 for (tail = head + STRLEN(head); tail > head; tail = prev)
13494 {
13495 prev = tail;
13496 MB_PTR_BACK(head, prev);
13497 c1 = PTR2CHAR(prev);
13498 if (mask == NULL)
13499 {
13500 if (c1 > ' ' && c1 != 0xa0)
13501 break;
13502 }
13503 else
13504 {
13505 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13506 if (c1 == PTR2CHAR(p))
13507 break;
13508 if (*p == NUL)
13509 break;
13510 }
13511 }
13512 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13513}
13514
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013515#ifdef FEAT_FLOAT
13516/*
13517 * "trunc({float})" function
13518 */
13519 static void
13520f_trunc(typval_T *argvars, typval_T *rettv)
13521{
13522 float_T f = 0.0;
13523
13524 rettv->v_type = VAR_FLOAT;
13525 if (get_float_arg(argvars, &f) == OK)
13526 /* trunc() is not in C90, use floor() or ceil() instead. */
13527 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13528 else
13529 rettv->vval.v_float = 0.0;
13530}
13531#endif
13532
13533/*
13534 * "type(expr)" function
13535 */
13536 static void
13537f_type(typval_T *argvars, typval_T *rettv)
13538{
13539 int n = -1;
13540
13541 switch (argvars[0].v_type)
13542 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013543 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13544 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013545 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013546 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13547 case VAR_LIST: n = VAR_TYPE_LIST; break;
13548 case VAR_DICT: n = VAR_TYPE_DICT; break;
13549 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013550 case VAR_SPECIAL:
13551 if (argvars[0].vval.v_number == VVAL_FALSE
13552 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013553 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013554 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013555 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013556 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013557 case VAR_JOB: n = VAR_TYPE_JOB; break;
13558 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013559 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013560 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013561 n = -1;
13562 break;
13563 }
13564 rettv->vval.v_number = n;
13565}
13566
13567/*
13568 * "undofile(name)" function
13569 */
13570 static void
13571f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13572{
13573 rettv->v_type = VAR_STRING;
13574#ifdef FEAT_PERSISTENT_UNDO
13575 {
13576 char_u *fname = get_tv_string(&argvars[0]);
13577
13578 if (*fname == NUL)
13579 {
13580 /* If there is no file name there will be no undo file. */
13581 rettv->vval.v_string = NULL;
13582 }
13583 else
13584 {
13585 char_u *ffname = FullName_save(fname, FALSE);
13586
13587 if (ffname != NULL)
13588 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13589 vim_free(ffname);
13590 }
13591 }
13592#else
13593 rettv->vval.v_string = NULL;
13594#endif
13595}
13596
13597/*
13598 * "undotree()" function
13599 */
13600 static void
13601f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13602{
13603 if (rettv_dict_alloc(rettv) == OK)
13604 {
13605 dict_T *dict = rettv->vval.v_dict;
13606 list_T *list;
13607
13608 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13609 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13610 dict_add_nr_str(dict, "save_last",
13611 (long)curbuf->b_u_save_nr_last, NULL);
13612 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13613 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13614 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13615
13616 list = list_alloc();
13617 if (list != NULL)
13618 {
13619 u_eval_tree(curbuf->b_u_oldhead, list);
13620 dict_add_list(dict, "entries", list);
13621 }
13622 }
13623}
13624
13625/*
13626 * "values(dict)" function
13627 */
13628 static void
13629f_values(typval_T *argvars, typval_T *rettv)
13630{
13631 dict_list(argvars, rettv, 1);
13632}
13633
13634/*
13635 * "virtcol(string)" function
13636 */
13637 static void
13638f_virtcol(typval_T *argvars, typval_T *rettv)
13639{
13640 colnr_T vcol = 0;
13641 pos_T *fp;
13642 int fnum = curbuf->b_fnum;
13643
13644 fp = var2fpos(&argvars[0], FALSE, &fnum);
13645 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13646 && fnum == curbuf->b_fnum)
13647 {
13648 getvvcol(curwin, fp, NULL, NULL, &vcol);
13649 ++vcol;
13650 }
13651
13652 rettv->vval.v_number = vcol;
13653}
13654
13655/*
13656 * "visualmode()" function
13657 */
13658 static void
13659f_visualmode(typval_T *argvars, typval_T *rettv)
13660{
13661 char_u str[2];
13662
13663 rettv->v_type = VAR_STRING;
13664 str[0] = curbuf->b_visual_mode_eval;
13665 str[1] = NUL;
13666 rettv->vval.v_string = vim_strsave(str);
13667
13668 /* A non-zero number or non-empty string argument: reset mode. */
13669 if (non_zero_arg(&argvars[0]))
13670 curbuf->b_visual_mode_eval = NUL;
13671}
13672
13673/*
13674 * "wildmenumode()" function
13675 */
13676 static void
13677f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13678{
13679#ifdef FEAT_WILDMENU
13680 if (wild_menu_showing)
13681 rettv->vval.v_number = 1;
13682#endif
13683}
13684
13685/*
13686 * "winbufnr(nr)" function
13687 */
13688 static void
13689f_winbufnr(typval_T *argvars, typval_T *rettv)
13690{
13691 win_T *wp;
13692
13693 wp = find_win_by_nr(&argvars[0], NULL);
13694 if (wp == NULL)
13695 rettv->vval.v_number = -1;
13696 else
13697 rettv->vval.v_number = wp->w_buffer->b_fnum;
13698}
13699
13700/*
13701 * "wincol()" function
13702 */
13703 static void
13704f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13705{
13706 validate_cursor();
13707 rettv->vval.v_number = curwin->w_wcol + 1;
13708}
13709
13710/*
13711 * "winheight(nr)" function
13712 */
13713 static void
13714f_winheight(typval_T *argvars, typval_T *rettv)
13715{
13716 win_T *wp;
13717
13718 wp = find_win_by_nr(&argvars[0], NULL);
13719 if (wp == NULL)
13720 rettv->vval.v_number = -1;
13721 else
13722 rettv->vval.v_number = wp->w_height;
13723}
13724
13725/*
13726 * "winline()" function
13727 */
13728 static void
13729f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13730{
13731 validate_cursor();
13732 rettv->vval.v_number = curwin->w_wrow + 1;
13733}
13734
13735/*
13736 * "winnr()" function
13737 */
13738 static void
13739f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13740{
13741 int nr = 1;
13742
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013743 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013744 rettv->vval.v_number = nr;
13745}
13746
13747/*
13748 * "winrestcmd()" function
13749 */
13750 static void
13751f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13752{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013753 win_T *wp;
13754 int winnr = 1;
13755 garray_T ga;
13756 char_u buf[50];
13757
13758 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013759 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013760 {
13761 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13762 ga_concat(&ga, buf);
13763 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13764 ga_concat(&ga, buf);
13765 ++winnr;
13766 }
13767 ga_append(&ga, NUL);
13768
13769 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013770 rettv->v_type = VAR_STRING;
13771}
13772
13773/*
13774 * "winrestview()" function
13775 */
13776 static void
13777f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13778{
13779 dict_T *dict;
13780
13781 if (argvars[0].v_type != VAR_DICT
13782 || (dict = argvars[0].vval.v_dict) == NULL)
13783 EMSG(_(e_invarg));
13784 else
13785 {
13786 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13787 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13788 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13789 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13790#ifdef FEAT_VIRTUALEDIT
13791 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13792 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13793#endif
13794 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13795 {
13796 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13797 curwin->w_set_curswant = FALSE;
13798 }
13799
13800 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13801 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13802#ifdef FEAT_DIFF
13803 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13804 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13805#endif
13806 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13807 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13808 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13809 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13810
13811 check_cursor();
13812 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013813 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013814 changed_window_setting();
13815
13816 if (curwin->w_topline <= 0)
13817 curwin->w_topline = 1;
13818 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13819 curwin->w_topline = curbuf->b_ml.ml_line_count;
13820#ifdef FEAT_DIFF
13821 check_topfill(curwin, TRUE);
13822#endif
13823 }
13824}
13825
13826/*
13827 * "winsaveview()" function
13828 */
13829 static void
13830f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13831{
13832 dict_T *dict;
13833
13834 if (rettv_dict_alloc(rettv) == FAIL)
13835 return;
13836 dict = rettv->vval.v_dict;
13837
13838 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13839 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13840#ifdef FEAT_VIRTUALEDIT
13841 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13842#endif
13843 update_curswant();
13844 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13845
13846 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13847#ifdef FEAT_DIFF
13848 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13849#endif
13850 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13851 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13852}
13853
13854/*
13855 * "winwidth(nr)" function
13856 */
13857 static void
13858f_winwidth(typval_T *argvars, typval_T *rettv)
13859{
13860 win_T *wp;
13861
13862 wp = find_win_by_nr(&argvars[0], NULL);
13863 if (wp == NULL)
13864 rettv->vval.v_number = -1;
13865 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013866 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013867}
13868
13869/*
13870 * "wordcount()" function
13871 */
13872 static void
13873f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13874{
13875 if (rettv_dict_alloc(rettv) == FAIL)
13876 return;
13877 cursor_pos_info(rettv->vval.v_dict);
13878}
13879
13880/*
13881 * "writefile()" function
13882 */
13883 static void
13884f_writefile(typval_T *argvars, typval_T *rettv)
13885{
13886 int binary = FALSE;
13887 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013888#ifdef HAVE_FSYNC
13889 int do_fsync = p_fs;
13890#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013891 char_u *fname;
13892 FILE *fd;
13893 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013894 listitem_T *li;
13895 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013896
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013897 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013898 if (check_restricted() || check_secure())
13899 return;
13900
13901 if (argvars[0].v_type != VAR_LIST)
13902 {
13903 EMSG2(_(e_listarg), "writefile()");
13904 return;
13905 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013906 list = argvars[0].vval.v_list;
13907 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013908 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013909 for (li = list->lv_first; li != NULL; li = li->li_next)
13910 if (get_tv_string_chk(&li->li_tv) == NULL)
13911 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013912
13913 if (argvars[2].v_type != VAR_UNKNOWN)
13914 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013915 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13916
13917 if (arg2 == NULL)
13918 return;
13919 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013920 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013921 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013922 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013923#ifdef HAVE_FSYNC
13924 if (vim_strchr(arg2, 's') != NULL)
13925 do_fsync = TRUE;
13926 else if (vim_strchr(arg2, 'S') != NULL)
13927 do_fsync = FALSE;
13928#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013929 }
13930
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013931 fname = get_tv_string_chk(&argvars[1]);
13932 if (fname == NULL)
13933 return;
13934
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013935 /* Always open the file in binary mode, library functions have a mind of
13936 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013937 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13938 append ? APPENDBIN : WRITEBIN)) == NULL)
13939 {
13940 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13941 ret = -1;
13942 }
13943 else
13944 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013945 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013946 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013947#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013948 else if (do_fsync)
13949 /* Ignore the error, the user wouldn't know what to do about it.
13950 * May happen for a device. */
13951 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013952#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013953 fclose(fd);
13954 }
13955
13956 rettv->vval.v_number = ret;
13957}
13958
13959/*
13960 * "xor(expr, expr)" function
13961 */
13962 static void
13963f_xor(typval_T *argvars, typval_T *rettv)
13964{
13965 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13966 ^ get_tv_number_chk(&argvars[1], NULL);
13967}
13968
13969
13970#endif /* FEAT_EVAL */