blob: 846a914165c0e5eb600a7710c9b36ac1f613c270 [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
27#ifdef MACOS
28# 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);
43static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
47static void f_assert_equal(typval_T *argvars, typval_T *rettv);
48static void f_assert_exception(typval_T *argvars, typval_T *rettv);
49static void f_assert_fails(typval_T *argvars, typval_T *rettv);
50static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020051static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_assert_match(typval_T *argvars, typval_T *rettv);
53static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
54static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
55static void f_assert_true(typval_T *argvars, typval_T *rettv);
56#ifdef FEAT_FLOAT
57static void f_asin(typval_T *argvars, typval_T *rettv);
58static void f_atan(typval_T *argvars, typval_T *rettv);
59static void f_atan2(typval_T *argvars, typval_T *rettv);
60#endif
61static void f_browse(typval_T *argvars, typval_T *rettv);
62static void f_browsedir(typval_T *argvars, typval_T *rettv);
63static void f_bufexists(typval_T *argvars, typval_T *rettv);
64static void f_buflisted(typval_T *argvars, typval_T *rettv);
65static void f_bufloaded(typval_T *argvars, typval_T *rettv);
66static void f_bufname(typval_T *argvars, typval_T *rettv);
67static void f_bufnr(typval_T *argvars, typval_T *rettv);
68static void f_bufwinid(typval_T *argvars, typval_T *rettv);
69static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
70static void f_byte2line(typval_T *argvars, typval_T *rettv);
71static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
72static void f_byteidx(typval_T *argvars, typval_T *rettv);
73static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
74static void f_call(typval_T *argvars, typval_T *rettv);
75#ifdef FEAT_FLOAT
76static void f_ceil(typval_T *argvars, typval_T *rettv);
77#endif
78#ifdef FEAT_JOB_CHANNEL
79static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020080static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020081static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
82static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
83static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
84static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
85static void f_ch_info(typval_T *argvars, typval_T *rettv);
86static void f_ch_log(typval_T *argvars, typval_T *rettv);
87static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
88static void f_ch_open(typval_T *argvars, typval_T *rettv);
89static void f_ch_read(typval_T *argvars, typval_T *rettv);
90static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
91static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
92static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
93static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
94static void f_ch_status(typval_T *argvars, typval_T *rettv);
95#endif
96static void f_changenr(typval_T *argvars, typval_T *rettv);
97static void f_char2nr(typval_T *argvars, typval_T *rettv);
98static void f_cindent(typval_T *argvars, typval_T *rettv);
99static void f_clearmatches(typval_T *argvars, typval_T *rettv);
100static void f_col(typval_T *argvars, typval_T *rettv);
101#if defined(FEAT_INS_EXPAND)
102static void f_complete(typval_T *argvars, typval_T *rettv);
103static void f_complete_add(typval_T *argvars, typval_T *rettv);
104static void f_complete_check(typval_T *argvars, typval_T *rettv);
105#endif
106static void f_confirm(typval_T *argvars, typval_T *rettv);
107static void f_copy(typval_T *argvars, typval_T *rettv);
108#ifdef FEAT_FLOAT
109static void f_cos(typval_T *argvars, typval_T *rettv);
110static void f_cosh(typval_T *argvars, typval_T *rettv);
111#endif
112static void f_count(typval_T *argvars, typval_T *rettv);
113static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
114static void f_cursor(typval_T *argsvars, typval_T *rettv);
115static void f_deepcopy(typval_T *argvars, typval_T *rettv);
116static void f_delete(typval_T *argvars, typval_T *rettv);
117static void f_did_filetype(typval_T *argvars, typval_T *rettv);
118static void f_diff_filler(typval_T *argvars, typval_T *rettv);
119static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
120static void f_empty(typval_T *argvars, typval_T *rettv);
121static void f_escape(typval_T *argvars, typval_T *rettv);
122static void f_eval(typval_T *argvars, typval_T *rettv);
123static void f_eventhandler(typval_T *argvars, typval_T *rettv);
124static void f_executable(typval_T *argvars, typval_T *rettv);
125static void f_execute(typval_T *argvars, typval_T *rettv);
126static void f_exepath(typval_T *argvars, typval_T *rettv);
127static void f_exists(typval_T *argvars, typval_T *rettv);
128#ifdef FEAT_FLOAT
129static void f_exp(typval_T *argvars, typval_T *rettv);
130#endif
131static void f_expand(typval_T *argvars, typval_T *rettv);
132static void f_extend(typval_T *argvars, typval_T *rettv);
133static void f_feedkeys(typval_T *argvars, typval_T *rettv);
134static void f_filereadable(typval_T *argvars, typval_T *rettv);
135static void f_filewritable(typval_T *argvars, typval_T *rettv);
136static void f_filter(typval_T *argvars, typval_T *rettv);
137static void f_finddir(typval_T *argvars, typval_T *rettv);
138static void f_findfile(typval_T *argvars, typval_T *rettv);
139#ifdef FEAT_FLOAT
140static void f_float2nr(typval_T *argvars, typval_T *rettv);
141static void f_floor(typval_T *argvars, typval_T *rettv);
142static void f_fmod(typval_T *argvars, typval_T *rettv);
143#endif
144static void f_fnameescape(typval_T *argvars, typval_T *rettv);
145static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
146static void f_foldclosed(typval_T *argvars, typval_T *rettv);
147static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
148static void f_foldlevel(typval_T *argvars, typval_T *rettv);
149static void f_foldtext(typval_T *argvars, typval_T *rettv);
150static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
151static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200152static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153static void f_function(typval_T *argvars, typval_T *rettv);
154static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
155static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200156static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200157static void f_getbufline(typval_T *argvars, typval_T *rettv);
158static void f_getbufvar(typval_T *argvars, typval_T *rettv);
159static void f_getchar(typval_T *argvars, typval_T *rettv);
160static void f_getcharmod(typval_T *argvars, typval_T *rettv);
161static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
162static void f_getcmdline(typval_T *argvars, typval_T *rettv);
163#if defined(FEAT_CMDL_COMPL)
164static void f_getcompletion(typval_T *argvars, typval_T *rettv);
165#endif
166static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
167static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
168static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
169static void f_getcwd(typval_T *argvars, typval_T *rettv);
170static void f_getfontname(typval_T *argvars, typval_T *rettv);
171static void f_getfperm(typval_T *argvars, typval_T *rettv);
172static void f_getfsize(typval_T *argvars, typval_T *rettv);
173static void f_getftime(typval_T *argvars, typval_T *rettv);
174static void f_getftype(typval_T *argvars, typval_T *rettv);
175static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200176static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_getmatches(typval_T *argvars, typval_T *rettv);
178static void f_getpid(typval_T *argvars, typval_T *rettv);
179static void f_getcurpos(typval_T *argvars, typval_T *rettv);
180static void f_getpos(typval_T *argvars, typval_T *rettv);
181static void f_getqflist(typval_T *argvars, typval_T *rettv);
182static void f_getreg(typval_T *argvars, typval_T *rettv);
183static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200184static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200185static void f_gettabvar(typval_T *argvars, typval_T *rettv);
186static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200187static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200188static void f_getwinposx(typval_T *argvars, typval_T *rettv);
189static void f_getwinposy(typval_T *argvars, typval_T *rettv);
190static void f_getwinvar(typval_T *argvars, typval_T *rettv);
191static void f_glob(typval_T *argvars, typval_T *rettv);
192static void f_globpath(typval_T *argvars, typval_T *rettv);
193static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
194static void f_has(typval_T *argvars, typval_T *rettv);
195static void f_has_key(typval_T *argvars, typval_T *rettv);
196static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
197static void f_hasmapto(typval_T *argvars, typval_T *rettv);
198static void f_histadd(typval_T *argvars, typval_T *rettv);
199static void f_histdel(typval_T *argvars, typval_T *rettv);
200static void f_histget(typval_T *argvars, typval_T *rettv);
201static void f_histnr(typval_T *argvars, typval_T *rettv);
202static void f_hlID(typval_T *argvars, typval_T *rettv);
203static void f_hlexists(typval_T *argvars, typval_T *rettv);
204static void f_hostname(typval_T *argvars, typval_T *rettv);
205static void f_iconv(typval_T *argvars, typval_T *rettv);
206static void f_indent(typval_T *argvars, typval_T *rettv);
207static void f_index(typval_T *argvars, typval_T *rettv);
208static void f_input(typval_T *argvars, typval_T *rettv);
209static void f_inputdialog(typval_T *argvars, typval_T *rettv);
210static void f_inputlist(typval_T *argvars, typval_T *rettv);
211static void f_inputrestore(typval_T *argvars, typval_T *rettv);
212static void f_inputsave(typval_T *argvars, typval_T *rettv);
213static void f_inputsecret(typval_T *argvars, typval_T *rettv);
214static void f_insert(typval_T *argvars, typval_T *rettv);
215static void f_invert(typval_T *argvars, typval_T *rettv);
216static void f_isdirectory(typval_T *argvars, typval_T *rettv);
217static void f_islocked(typval_T *argvars, typval_T *rettv);
218#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
219static void f_isnan(typval_T *argvars, typval_T *rettv);
220#endif
221static void f_items(typval_T *argvars, typval_T *rettv);
222#ifdef FEAT_JOB_CHANNEL
223static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
224static void f_job_info(typval_T *argvars, typval_T *rettv);
225static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
226static void f_job_start(typval_T *argvars, typval_T *rettv);
227static void f_job_stop(typval_T *argvars, typval_T *rettv);
228static void f_job_status(typval_T *argvars, typval_T *rettv);
229#endif
230static void f_join(typval_T *argvars, typval_T *rettv);
231static void f_js_decode(typval_T *argvars, typval_T *rettv);
232static void f_js_encode(typval_T *argvars, typval_T *rettv);
233static void f_json_decode(typval_T *argvars, typval_T *rettv);
234static void f_json_encode(typval_T *argvars, typval_T *rettv);
235static void f_keys(typval_T *argvars, typval_T *rettv);
236static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
237static void f_len(typval_T *argvars, typval_T *rettv);
238static void f_libcall(typval_T *argvars, typval_T *rettv);
239static void f_libcallnr(typval_T *argvars, typval_T *rettv);
240static void f_line(typval_T *argvars, typval_T *rettv);
241static void f_line2byte(typval_T *argvars, typval_T *rettv);
242static void f_lispindent(typval_T *argvars, typval_T *rettv);
243static void f_localtime(typval_T *argvars, typval_T *rettv);
244#ifdef FEAT_FLOAT
245static void f_log(typval_T *argvars, typval_T *rettv);
246static void f_log10(typval_T *argvars, typval_T *rettv);
247#endif
248#ifdef FEAT_LUA
249static void f_luaeval(typval_T *argvars, typval_T *rettv);
250#endif
251static void f_map(typval_T *argvars, typval_T *rettv);
252static void f_maparg(typval_T *argvars, typval_T *rettv);
253static void f_mapcheck(typval_T *argvars, typval_T *rettv);
254static void f_match(typval_T *argvars, typval_T *rettv);
255static void f_matchadd(typval_T *argvars, typval_T *rettv);
256static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
257static void f_matcharg(typval_T *argvars, typval_T *rettv);
258static void f_matchdelete(typval_T *argvars, typval_T *rettv);
259static void f_matchend(typval_T *argvars, typval_T *rettv);
260static void f_matchlist(typval_T *argvars, typval_T *rettv);
261static void f_matchstr(typval_T *argvars, typval_T *rettv);
262static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
263static void f_max(typval_T *argvars, typval_T *rettv);
264static void f_min(typval_T *argvars, typval_T *rettv);
265#ifdef vim_mkdir
266static void f_mkdir(typval_T *argvars, typval_T *rettv);
267#endif
268static void f_mode(typval_T *argvars, typval_T *rettv);
269#ifdef FEAT_MZSCHEME
270static void f_mzeval(typval_T *argvars, typval_T *rettv);
271#endif
272static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
273static void f_nr2char(typval_T *argvars, typval_T *rettv);
274static void f_or(typval_T *argvars, typval_T *rettv);
275static void f_pathshorten(typval_T *argvars, typval_T *rettv);
276#ifdef FEAT_PERL
277static void f_perleval(typval_T *argvars, typval_T *rettv);
278#endif
279#ifdef FEAT_FLOAT
280static void f_pow(typval_T *argvars, typval_T *rettv);
281#endif
282static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
283static void f_printf(typval_T *argvars, typval_T *rettv);
284static void f_pumvisible(typval_T *argvars, typval_T *rettv);
285#ifdef FEAT_PYTHON3
286static void f_py3eval(typval_T *argvars, typval_T *rettv);
287#endif
288#ifdef FEAT_PYTHON
289static void f_pyeval(typval_T *argvars, typval_T *rettv);
290#endif
291static void f_range(typval_T *argvars, typval_T *rettv);
292static void f_readfile(typval_T *argvars, typval_T *rettv);
293static void f_reltime(typval_T *argvars, typval_T *rettv);
294#ifdef FEAT_FLOAT
295static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
296#endif
297static void f_reltimestr(typval_T *argvars, typval_T *rettv);
298static void f_remote_expr(typval_T *argvars, typval_T *rettv);
299static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
300static void f_remote_peek(typval_T *argvars, typval_T *rettv);
301static void f_remote_read(typval_T *argvars, typval_T *rettv);
302static void f_remote_send(typval_T *argvars, typval_T *rettv);
303static void f_remove(typval_T *argvars, typval_T *rettv);
304static void f_rename(typval_T *argvars, typval_T *rettv);
305static void f_repeat(typval_T *argvars, typval_T *rettv);
306static void f_resolve(typval_T *argvars, typval_T *rettv);
307static void f_reverse(typval_T *argvars, typval_T *rettv);
308#ifdef FEAT_FLOAT
309static void f_round(typval_T *argvars, typval_T *rettv);
310#endif
311static void f_screenattr(typval_T *argvars, typval_T *rettv);
312static void f_screenchar(typval_T *argvars, typval_T *rettv);
313static void f_screencol(typval_T *argvars, typval_T *rettv);
314static void f_screenrow(typval_T *argvars, typval_T *rettv);
315static void f_search(typval_T *argvars, typval_T *rettv);
316static void f_searchdecl(typval_T *argvars, typval_T *rettv);
317static void f_searchpair(typval_T *argvars, typval_T *rettv);
318static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
319static void f_searchpos(typval_T *argvars, typval_T *rettv);
320static void f_server2client(typval_T *argvars, typval_T *rettv);
321static void f_serverlist(typval_T *argvars, typval_T *rettv);
322static void f_setbufvar(typval_T *argvars, typval_T *rettv);
323static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
324static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
325static void f_setfperm(typval_T *argvars, typval_T *rettv);
326static void f_setline(typval_T *argvars, typval_T *rettv);
327static void f_setloclist(typval_T *argvars, typval_T *rettv);
328static void f_setmatches(typval_T *argvars, typval_T *rettv);
329static void f_setpos(typval_T *argvars, typval_T *rettv);
330static void f_setqflist(typval_T *argvars, typval_T *rettv);
331static void f_setreg(typval_T *argvars, typval_T *rettv);
332static void f_settabvar(typval_T *argvars, typval_T *rettv);
333static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
334static void f_setwinvar(typval_T *argvars, typval_T *rettv);
335#ifdef FEAT_CRYPT
336static void f_sha256(typval_T *argvars, typval_T *rettv);
337#endif /* FEAT_CRYPT */
338static void f_shellescape(typval_T *argvars, typval_T *rettv);
339static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
340static void f_simplify(typval_T *argvars, typval_T *rettv);
341#ifdef FEAT_FLOAT
342static void f_sin(typval_T *argvars, typval_T *rettv);
343static void f_sinh(typval_T *argvars, typval_T *rettv);
344#endif
345static void f_sort(typval_T *argvars, typval_T *rettv);
346static void f_soundfold(typval_T *argvars, typval_T *rettv);
347static void f_spellbadword(typval_T *argvars, typval_T *rettv);
348static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
349static void f_split(typval_T *argvars, typval_T *rettv);
350#ifdef FEAT_FLOAT
351static void f_sqrt(typval_T *argvars, typval_T *rettv);
352static void f_str2float(typval_T *argvars, typval_T *rettv);
353#endif
354static void f_str2nr(typval_T *argvars, typval_T *rettv);
355static void f_strchars(typval_T *argvars, typval_T *rettv);
356#ifdef HAVE_STRFTIME
357static void f_strftime(typval_T *argvars, typval_T *rettv);
358#endif
359static void f_strgetchar(typval_T *argvars, typval_T *rettv);
360static void f_stridx(typval_T *argvars, typval_T *rettv);
361static void f_string(typval_T *argvars, typval_T *rettv);
362static void f_strlen(typval_T *argvars, typval_T *rettv);
363static void f_strcharpart(typval_T *argvars, typval_T *rettv);
364static void f_strpart(typval_T *argvars, typval_T *rettv);
365static void f_strridx(typval_T *argvars, typval_T *rettv);
366static void f_strtrans(typval_T *argvars, typval_T *rettv);
367static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
368static void f_strwidth(typval_T *argvars, typval_T *rettv);
369static void f_submatch(typval_T *argvars, typval_T *rettv);
370static void f_substitute(typval_T *argvars, typval_T *rettv);
371static void f_synID(typval_T *argvars, typval_T *rettv);
372static void f_synIDattr(typval_T *argvars, typval_T *rettv);
373static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
374static void f_synstack(typval_T *argvars, typval_T *rettv);
375static void f_synconcealed(typval_T *argvars, typval_T *rettv);
376static void f_system(typval_T *argvars, typval_T *rettv);
377static void f_systemlist(typval_T *argvars, typval_T *rettv);
378static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
379static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
380static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
381static void f_taglist(typval_T *argvars, typval_T *rettv);
382static void f_tagfiles(typval_T *argvars, typval_T *rettv);
383static void f_tempname(typval_T *argvars, typval_T *rettv);
384static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
385static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
386static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
387static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
388#ifdef FEAT_JOB_CHANNEL
389static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
390#endif
391static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
392#ifdef FEAT_JOB_CHANNEL
393static void f_test_null_job(typval_T *argvars, typval_T *rettv);
394#endif
395static void f_test_null_list(typval_T *argvars, typval_T *rettv);
396static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
397static void f_test_null_string(typval_T *argvars, typval_T *rettv);
398static void f_test_settime(typval_T *argvars, typval_T *rettv);
399#ifdef FEAT_FLOAT
400static void f_tan(typval_T *argvars, typval_T *rettv);
401static void f_tanh(typval_T *argvars, typval_T *rettv);
402#endif
403#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200404static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200405static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200406static void f_timer_start(typval_T *argvars, typval_T *rettv);
407static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200408static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200409#endif
410static void f_tolower(typval_T *argvars, typval_T *rettv);
411static void f_toupper(typval_T *argvars, typval_T *rettv);
412static void f_tr(typval_T *argvars, typval_T *rettv);
413#ifdef FEAT_FLOAT
414static void f_trunc(typval_T *argvars, typval_T *rettv);
415#endif
416static void f_type(typval_T *argvars, typval_T *rettv);
417static void f_undofile(typval_T *argvars, typval_T *rettv);
418static void f_undotree(typval_T *argvars, typval_T *rettv);
419static void f_uniq(typval_T *argvars, typval_T *rettv);
420static void f_values(typval_T *argvars, typval_T *rettv);
421static void f_virtcol(typval_T *argvars, typval_T *rettv);
422static void f_visualmode(typval_T *argvars, typval_T *rettv);
423static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
424static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
425static void f_win_getid(typval_T *argvars, typval_T *rettv);
426static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
427static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
428static void f_win_id2win(typval_T *argvars, typval_T *rettv);
429static void f_winbufnr(typval_T *argvars, typval_T *rettv);
430static void f_wincol(typval_T *argvars, typval_T *rettv);
431static void f_winheight(typval_T *argvars, typval_T *rettv);
432static void f_winline(typval_T *argvars, typval_T *rettv);
433static void f_winnr(typval_T *argvars, typval_T *rettv);
434static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
435static void f_winrestview(typval_T *argvars, typval_T *rettv);
436static void f_winsaveview(typval_T *argvars, typval_T *rettv);
437static void f_winwidth(typval_T *argvars, typval_T *rettv);
438static void f_writefile(typval_T *argvars, typval_T *rettv);
439static void f_wordcount(typval_T *argvars, typval_T *rettv);
440static void f_xor(typval_T *argvars, typval_T *rettv);
441
442/*
443 * Array with names and number of arguments of all internal functions
444 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
445 */
446static struct fst
447{
448 char *f_name; /* function name */
449 char f_min_argc; /* minimal number of arguments */
450 char f_max_argc; /* maximal number of arguments */
451 void (*f_func)(typval_T *args, typval_T *rvar);
452 /* implementation of function */
453} functions[] =
454{
455#ifdef FEAT_FLOAT
456 {"abs", 1, 1, f_abs},
457 {"acos", 1, 1, f_acos}, /* WJMc */
458#endif
459 {"add", 2, 2, f_add},
460 {"and", 2, 2, f_and},
461 {"append", 2, 2, f_append},
462 {"argc", 0, 0, f_argc},
463 {"argidx", 0, 0, f_argidx},
464 {"arglistid", 0, 2, f_arglistid},
465 {"argv", 0, 1, f_argv},
466#ifdef FEAT_FLOAT
467 {"asin", 1, 1, f_asin}, /* WJMc */
468#endif
469 {"assert_equal", 2, 3, f_assert_equal},
470 {"assert_exception", 1, 2, f_assert_exception},
471 {"assert_fails", 1, 2, f_assert_fails},
472 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar61c04492016-07-23 15:35:35 +0200473 {"assert_inrange", 2, 3, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200474 {"assert_match", 2, 3, f_assert_match},
475 {"assert_notequal", 2, 3, f_assert_notequal},
476 {"assert_notmatch", 2, 3, f_assert_notmatch},
477 {"assert_true", 1, 2, f_assert_true},
478#ifdef FEAT_FLOAT
479 {"atan", 1, 1, f_atan},
480 {"atan2", 2, 2, f_atan2},
481#endif
482 {"browse", 4, 4, f_browse},
483 {"browsedir", 2, 2, f_browsedir},
484 {"bufexists", 1, 1, f_bufexists},
485 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
486 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
487 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
488 {"buflisted", 1, 1, f_buflisted},
489 {"bufloaded", 1, 1, f_bufloaded},
490 {"bufname", 1, 1, f_bufname},
491 {"bufnr", 1, 2, f_bufnr},
492 {"bufwinid", 1, 1, f_bufwinid},
493 {"bufwinnr", 1, 1, f_bufwinnr},
494 {"byte2line", 1, 1, f_byte2line},
495 {"byteidx", 2, 2, f_byteidx},
496 {"byteidxcomp", 2, 2, f_byteidxcomp},
497 {"call", 2, 3, f_call},
498#ifdef FEAT_FLOAT
499 {"ceil", 1, 1, f_ceil},
500#endif
501#ifdef FEAT_JOB_CHANNEL
502 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200503 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200504 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
505 {"ch_evalraw", 2, 3, f_ch_evalraw},
506 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
507 {"ch_getjob", 1, 1, f_ch_getjob},
508 {"ch_info", 1, 1, f_ch_info},
509 {"ch_log", 1, 2, f_ch_log},
510 {"ch_logfile", 1, 2, f_ch_logfile},
511 {"ch_open", 1, 2, f_ch_open},
512 {"ch_read", 1, 2, f_ch_read},
513 {"ch_readraw", 1, 2, f_ch_readraw},
514 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
515 {"ch_sendraw", 2, 3, f_ch_sendraw},
516 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200517 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200518#endif
519 {"changenr", 0, 0, f_changenr},
520 {"char2nr", 1, 2, f_char2nr},
521 {"cindent", 1, 1, f_cindent},
522 {"clearmatches", 0, 0, f_clearmatches},
523 {"col", 1, 1, f_col},
524#if defined(FEAT_INS_EXPAND)
525 {"complete", 2, 2, f_complete},
526 {"complete_add", 1, 1, f_complete_add},
527 {"complete_check", 0, 0, f_complete_check},
528#endif
529 {"confirm", 1, 4, f_confirm},
530 {"copy", 1, 1, f_copy},
531#ifdef FEAT_FLOAT
532 {"cos", 1, 1, f_cos},
533 {"cosh", 1, 1, f_cosh},
534#endif
535 {"count", 2, 4, f_count},
536 {"cscope_connection",0,3, f_cscope_connection},
537 {"cursor", 1, 3, f_cursor},
538 {"deepcopy", 1, 2, f_deepcopy},
539 {"delete", 1, 2, f_delete},
540 {"did_filetype", 0, 0, f_did_filetype},
541 {"diff_filler", 1, 1, f_diff_filler},
542 {"diff_hlID", 2, 2, f_diff_hlID},
543 {"empty", 1, 1, f_empty},
544 {"escape", 2, 2, f_escape},
545 {"eval", 1, 1, f_eval},
546 {"eventhandler", 0, 0, f_eventhandler},
547 {"executable", 1, 1, f_executable},
548 {"execute", 1, 2, f_execute},
549 {"exepath", 1, 1, f_exepath},
550 {"exists", 1, 1, f_exists},
551#ifdef FEAT_FLOAT
552 {"exp", 1, 1, f_exp},
553#endif
554 {"expand", 1, 3, f_expand},
555 {"extend", 2, 3, f_extend},
556 {"feedkeys", 1, 2, f_feedkeys},
557 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
558 {"filereadable", 1, 1, f_filereadable},
559 {"filewritable", 1, 1, f_filewritable},
560 {"filter", 2, 2, f_filter},
561 {"finddir", 1, 3, f_finddir},
562 {"findfile", 1, 3, f_findfile},
563#ifdef FEAT_FLOAT
564 {"float2nr", 1, 1, f_float2nr},
565 {"floor", 1, 1, f_floor},
566 {"fmod", 2, 2, f_fmod},
567#endif
568 {"fnameescape", 1, 1, f_fnameescape},
569 {"fnamemodify", 2, 2, f_fnamemodify},
570 {"foldclosed", 1, 1, f_foldclosed},
571 {"foldclosedend", 1, 1, f_foldclosedend},
572 {"foldlevel", 1, 1, f_foldlevel},
573 {"foldtext", 0, 0, f_foldtext},
574 {"foldtextresult", 1, 1, f_foldtextresult},
575 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200576 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200577 {"function", 1, 3, f_function},
578 {"garbagecollect", 0, 1, f_garbagecollect},
579 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200580 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200581 {"getbufline", 2, 3, f_getbufline},
582 {"getbufvar", 2, 3, f_getbufvar},
583 {"getchar", 0, 1, f_getchar},
584 {"getcharmod", 0, 0, f_getcharmod},
585 {"getcharsearch", 0, 0, f_getcharsearch},
586 {"getcmdline", 0, 0, f_getcmdline},
587 {"getcmdpos", 0, 0, f_getcmdpos},
588 {"getcmdtype", 0, 0, f_getcmdtype},
589 {"getcmdwintype", 0, 0, f_getcmdwintype},
590#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200591 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200592#endif
593 {"getcurpos", 0, 0, f_getcurpos},
594 {"getcwd", 0, 2, f_getcwd},
595 {"getfontname", 0, 1, f_getfontname},
596 {"getfperm", 1, 1, f_getfperm},
597 {"getfsize", 1, 1, f_getfsize},
598 {"getftime", 1, 1, f_getftime},
599 {"getftype", 1, 1, f_getftype},
600 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200601 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200602 {"getmatches", 0, 0, f_getmatches},
603 {"getpid", 0, 0, f_getpid},
604 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200605 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200606 {"getreg", 0, 3, f_getreg},
607 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200608 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200609 {"gettabvar", 2, 3, f_gettabvar},
610 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200611 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200612 {"getwinposx", 0, 0, f_getwinposx},
613 {"getwinposy", 0, 0, f_getwinposy},
614 {"getwinvar", 2, 3, f_getwinvar},
615 {"glob", 1, 4, f_glob},
616 {"glob2regpat", 1, 1, f_glob2regpat},
617 {"globpath", 2, 5, f_globpath},
618 {"has", 1, 1, f_has},
619 {"has_key", 2, 2, f_has_key},
620 {"haslocaldir", 0, 2, f_haslocaldir},
621 {"hasmapto", 1, 3, f_hasmapto},
622 {"highlightID", 1, 1, f_hlID}, /* obsolete */
623 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
624 {"histadd", 2, 2, f_histadd},
625 {"histdel", 1, 2, f_histdel},
626 {"histget", 1, 2, f_histget},
627 {"histnr", 1, 1, f_histnr},
628 {"hlID", 1, 1, f_hlID},
629 {"hlexists", 1, 1, f_hlexists},
630 {"hostname", 0, 0, f_hostname},
631 {"iconv", 3, 3, f_iconv},
632 {"indent", 1, 1, f_indent},
633 {"index", 2, 4, f_index},
634 {"input", 1, 3, f_input},
635 {"inputdialog", 1, 3, f_inputdialog},
636 {"inputlist", 1, 1, f_inputlist},
637 {"inputrestore", 0, 0, f_inputrestore},
638 {"inputsave", 0, 0, f_inputsave},
639 {"inputsecret", 1, 2, f_inputsecret},
640 {"insert", 2, 3, f_insert},
641 {"invert", 1, 1, f_invert},
642 {"isdirectory", 1, 1, f_isdirectory},
643 {"islocked", 1, 1, f_islocked},
644#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
645 {"isnan", 1, 1, f_isnan},
646#endif
647 {"items", 1, 1, f_items},
648#ifdef FEAT_JOB_CHANNEL
649 {"job_getchannel", 1, 1, f_job_getchannel},
650 {"job_info", 1, 1, f_job_info},
651 {"job_setoptions", 2, 2, f_job_setoptions},
652 {"job_start", 1, 2, f_job_start},
653 {"job_status", 1, 1, f_job_status},
654 {"job_stop", 1, 2, f_job_stop},
655#endif
656 {"join", 1, 2, f_join},
657 {"js_decode", 1, 1, f_js_decode},
658 {"js_encode", 1, 1, f_js_encode},
659 {"json_decode", 1, 1, f_json_decode},
660 {"json_encode", 1, 1, f_json_encode},
661 {"keys", 1, 1, f_keys},
662 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
663 {"len", 1, 1, f_len},
664 {"libcall", 3, 3, f_libcall},
665 {"libcallnr", 3, 3, f_libcallnr},
666 {"line", 1, 1, f_line},
667 {"line2byte", 1, 1, f_line2byte},
668 {"lispindent", 1, 1, f_lispindent},
669 {"localtime", 0, 0, f_localtime},
670#ifdef FEAT_FLOAT
671 {"log", 1, 1, f_log},
672 {"log10", 1, 1, f_log10},
673#endif
674#ifdef FEAT_LUA
675 {"luaeval", 1, 2, f_luaeval},
676#endif
677 {"map", 2, 2, f_map},
678 {"maparg", 1, 4, f_maparg},
679 {"mapcheck", 1, 3, f_mapcheck},
680 {"match", 2, 4, f_match},
681 {"matchadd", 2, 5, f_matchadd},
682 {"matchaddpos", 2, 5, f_matchaddpos},
683 {"matcharg", 1, 1, f_matcharg},
684 {"matchdelete", 1, 1, f_matchdelete},
685 {"matchend", 2, 4, f_matchend},
686 {"matchlist", 2, 4, f_matchlist},
687 {"matchstr", 2, 4, f_matchstr},
688 {"matchstrpos", 2, 4, f_matchstrpos},
689 {"max", 1, 1, f_max},
690 {"min", 1, 1, f_min},
691#ifdef vim_mkdir
692 {"mkdir", 1, 3, f_mkdir},
693#endif
694 {"mode", 0, 1, f_mode},
695#ifdef FEAT_MZSCHEME
696 {"mzeval", 1, 1, f_mzeval},
697#endif
698 {"nextnonblank", 1, 1, f_nextnonblank},
699 {"nr2char", 1, 2, f_nr2char},
700 {"or", 2, 2, f_or},
701 {"pathshorten", 1, 1, f_pathshorten},
702#ifdef FEAT_PERL
703 {"perleval", 1, 1, f_perleval},
704#endif
705#ifdef FEAT_FLOAT
706 {"pow", 2, 2, f_pow},
707#endif
708 {"prevnonblank", 1, 1, f_prevnonblank},
709 {"printf", 2, 19, f_printf},
710 {"pumvisible", 0, 0, f_pumvisible},
711#ifdef FEAT_PYTHON3
712 {"py3eval", 1, 1, f_py3eval},
713#endif
714#ifdef FEAT_PYTHON
715 {"pyeval", 1, 1, f_pyeval},
716#endif
717 {"range", 1, 3, f_range},
718 {"readfile", 1, 3, f_readfile},
719 {"reltime", 0, 2, f_reltime},
720#ifdef FEAT_FLOAT
721 {"reltimefloat", 1, 1, f_reltimefloat},
722#endif
723 {"reltimestr", 1, 1, f_reltimestr},
724 {"remote_expr", 2, 3, f_remote_expr},
725 {"remote_foreground", 1, 1, f_remote_foreground},
726 {"remote_peek", 1, 2, f_remote_peek},
727 {"remote_read", 1, 1, f_remote_read},
728 {"remote_send", 2, 3, f_remote_send},
729 {"remove", 2, 3, f_remove},
730 {"rename", 2, 2, f_rename},
731 {"repeat", 2, 2, f_repeat},
732 {"resolve", 1, 1, f_resolve},
733 {"reverse", 1, 1, f_reverse},
734#ifdef FEAT_FLOAT
735 {"round", 1, 1, f_round},
736#endif
737 {"screenattr", 2, 2, f_screenattr},
738 {"screenchar", 2, 2, f_screenchar},
739 {"screencol", 0, 0, f_screencol},
740 {"screenrow", 0, 0, f_screenrow},
741 {"search", 1, 4, f_search},
742 {"searchdecl", 1, 3, f_searchdecl},
743 {"searchpair", 3, 7, f_searchpair},
744 {"searchpairpos", 3, 7, f_searchpairpos},
745 {"searchpos", 1, 4, f_searchpos},
746 {"server2client", 2, 2, f_server2client},
747 {"serverlist", 0, 0, f_serverlist},
748 {"setbufvar", 3, 3, f_setbufvar},
749 {"setcharsearch", 1, 1, f_setcharsearch},
750 {"setcmdpos", 1, 1, f_setcmdpos},
751 {"setfperm", 2, 2, f_setfperm},
752 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200753 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200754 {"setmatches", 1, 1, f_setmatches},
755 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200756 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200757 {"setreg", 2, 3, f_setreg},
758 {"settabvar", 3, 3, f_settabvar},
759 {"settabwinvar", 4, 4, f_settabwinvar},
760 {"setwinvar", 3, 3, f_setwinvar},
761#ifdef FEAT_CRYPT
762 {"sha256", 1, 1, f_sha256},
763#endif
764 {"shellescape", 1, 2, f_shellescape},
765 {"shiftwidth", 0, 0, f_shiftwidth},
766 {"simplify", 1, 1, f_simplify},
767#ifdef FEAT_FLOAT
768 {"sin", 1, 1, f_sin},
769 {"sinh", 1, 1, f_sinh},
770#endif
771 {"sort", 1, 3, f_sort},
772 {"soundfold", 1, 1, f_soundfold},
773 {"spellbadword", 0, 1, f_spellbadword},
774 {"spellsuggest", 1, 3, f_spellsuggest},
775 {"split", 1, 3, f_split},
776#ifdef FEAT_FLOAT
777 {"sqrt", 1, 1, f_sqrt},
778 {"str2float", 1, 1, f_str2float},
779#endif
780 {"str2nr", 1, 2, f_str2nr},
781 {"strcharpart", 2, 3, f_strcharpart},
782 {"strchars", 1, 2, f_strchars},
783 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
784#ifdef HAVE_STRFTIME
785 {"strftime", 1, 2, f_strftime},
786#endif
787 {"strgetchar", 2, 2, f_strgetchar},
788 {"stridx", 2, 3, f_stridx},
789 {"string", 1, 1, f_string},
790 {"strlen", 1, 1, f_strlen},
791 {"strpart", 2, 3, f_strpart},
792 {"strridx", 2, 3, f_strridx},
793 {"strtrans", 1, 1, f_strtrans},
794 {"strwidth", 1, 1, f_strwidth},
795 {"submatch", 1, 2, f_submatch},
796 {"substitute", 4, 4, f_substitute},
797 {"synID", 3, 3, f_synID},
798 {"synIDattr", 2, 3, f_synIDattr},
799 {"synIDtrans", 1, 1, f_synIDtrans},
800 {"synconcealed", 2, 2, f_synconcealed},
801 {"synstack", 2, 2, f_synstack},
802 {"system", 1, 2, f_system},
803 {"systemlist", 1, 2, f_systemlist},
804 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
805 {"tabpagenr", 0, 1, f_tabpagenr},
806 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
807 {"tagfiles", 0, 0, f_tagfiles},
808 {"taglist", 1, 1, f_taglist},
809#ifdef FEAT_FLOAT
810 {"tan", 1, 1, f_tan},
811 {"tanh", 1, 1, f_tanh},
812#endif
813 {"tempname", 0, 0, f_tempname},
814 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
815 {"test_autochdir", 0, 0, f_test_autochdir},
816 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
817 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
818#ifdef FEAT_JOB_CHANNEL
819 {"test_null_channel", 0, 0, f_test_null_channel},
820#endif
821 {"test_null_dict", 0, 0, f_test_null_dict},
822#ifdef FEAT_JOB_CHANNEL
823 {"test_null_job", 0, 0, f_test_null_job},
824#endif
825 {"test_null_list", 0, 0, f_test_null_list},
826 {"test_null_partial", 0, 0, f_test_null_partial},
827 {"test_null_string", 0, 0, f_test_null_string},
828 {"test_settime", 1, 1, f_test_settime},
829#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200830 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200831 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200832 {"timer_start", 2, 3, f_timer_start},
833 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200834 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200835#endif
836 {"tolower", 1, 1, f_tolower},
837 {"toupper", 1, 1, f_toupper},
838 {"tr", 3, 3, f_tr},
839#ifdef FEAT_FLOAT
840 {"trunc", 1, 1, f_trunc},
841#endif
842 {"type", 1, 1, f_type},
843 {"undofile", 1, 1, f_undofile},
844 {"undotree", 0, 0, f_undotree},
845 {"uniq", 1, 3, f_uniq},
846 {"values", 1, 1, f_values},
847 {"virtcol", 1, 1, f_virtcol},
848 {"visualmode", 0, 1, f_visualmode},
849 {"wildmenumode", 0, 0, f_wildmenumode},
850 {"win_findbuf", 1, 1, f_win_findbuf},
851 {"win_getid", 0, 2, f_win_getid},
852 {"win_gotoid", 1, 1, f_win_gotoid},
853 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
854 {"win_id2win", 1, 1, f_win_id2win},
855 {"winbufnr", 1, 1, f_winbufnr},
856 {"wincol", 0, 0, f_wincol},
857 {"winheight", 1, 1, f_winheight},
858 {"winline", 0, 0, f_winline},
859 {"winnr", 0, 1, f_winnr},
860 {"winrestcmd", 0, 0, f_winrestcmd},
861 {"winrestview", 1, 1, f_winrestview},
862 {"winsaveview", 0, 0, f_winsaveview},
863 {"winwidth", 1, 1, f_winwidth},
864 {"wordcount", 0, 0, f_wordcount},
865 {"writefile", 2, 3, f_writefile},
866 {"xor", 2, 2, f_xor},
867};
868
869#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
870
871/*
872 * Function given to ExpandGeneric() to obtain the list of internal
873 * or user defined function names.
874 */
875 char_u *
876get_function_name(expand_T *xp, int idx)
877{
878 static int intidx = -1;
879 char_u *name;
880
881 if (idx == 0)
882 intidx = -1;
883 if (intidx < 0)
884 {
885 name = get_user_func_name(xp, idx);
886 if (name != NULL)
887 return name;
888 }
889 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
890 {
891 STRCPY(IObuff, functions[intidx].f_name);
892 STRCAT(IObuff, "(");
893 if (functions[intidx].f_max_argc == 0)
894 STRCAT(IObuff, ")");
895 return IObuff;
896 }
897
898 return NULL;
899}
900
901/*
902 * Function given to ExpandGeneric() to obtain the list of internal or
903 * user defined variable or function names.
904 */
905 char_u *
906get_expr_name(expand_T *xp, int idx)
907{
908 static int intidx = -1;
909 char_u *name;
910
911 if (idx == 0)
912 intidx = -1;
913 if (intidx < 0)
914 {
915 name = get_function_name(xp, idx);
916 if (name != NULL)
917 return name;
918 }
919 return get_user_var_name(xp, ++intidx);
920}
921
922#endif /* FEAT_CMDL_COMPL */
923
924#if defined(EBCDIC) || defined(PROTO)
925/*
926 * Compare struct fst by function name.
927 */
928 static int
929compare_func_name(const void *s1, const void *s2)
930{
931 struct fst *p1 = (struct fst *)s1;
932 struct fst *p2 = (struct fst *)s2;
933
934 return STRCMP(p1->f_name, p2->f_name);
935}
936
937/*
938 * Sort the function table by function name.
939 * The sorting of the table above is ASCII dependant.
940 * On machines using EBCDIC we have to sort it.
941 */
942 static void
943sortFunctions(void)
944{
945 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
946
947 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
948}
949#endif
950
951
952/*
953 * Find internal function in table above.
954 * Return index, or -1 if not found
955 */
956 int
957find_internal_func(
958 char_u *name) /* name of the function */
959{
960 int first = 0;
961 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
962 int cmp;
963 int x;
964
965 /*
966 * Find the function name in the table. Binary search.
967 */
968 while (first <= last)
969 {
970 x = first + ((unsigned)(last - first) >> 1);
971 cmp = STRCMP(name, functions[x].f_name);
972 if (cmp < 0)
973 last = x - 1;
974 else if (cmp > 0)
975 first = x + 1;
976 else
977 return x;
978 }
979 return -1;
980}
981
982 int
983call_internal_func(
984 char_u *name,
985 int argcount,
986 typval_T *argvars,
987 typval_T *rettv)
988{
989 int i;
990
991 i = find_internal_func(name);
992 if (i < 0)
993 return ERROR_UNKNOWN;
994 if (argcount < functions[i].f_min_argc)
995 return ERROR_TOOFEW;
996 if (argcount > functions[i].f_max_argc)
997 return ERROR_TOOMANY;
998 argvars[argcount].v_type = VAR_UNKNOWN;
999 functions[i].f_func(argvars, rettv);
1000 return ERROR_NONE;
1001}
1002
1003/*
1004 * Return TRUE for a non-zero Number and a non-empty String.
1005 */
1006 static int
1007non_zero_arg(typval_T *argvars)
1008{
1009 return ((argvars[0].v_type == VAR_NUMBER
1010 && argvars[0].vval.v_number != 0)
1011 || (argvars[0].v_type == VAR_SPECIAL
1012 && argvars[0].vval.v_number == VVAL_TRUE)
1013 || (argvars[0].v_type == VAR_STRING
1014 && argvars[0].vval.v_string != NULL
1015 && *argvars[0].vval.v_string != NUL));
1016}
1017
1018/*
1019 * Get the lnum from the first argument.
1020 * Also accepts ".", "$", etc., but that only works for the current buffer.
1021 * Returns -1 on error.
1022 */
1023 static linenr_T
1024get_tv_lnum(typval_T *argvars)
1025{
1026 typval_T rettv;
1027 linenr_T lnum;
1028
1029 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1030 if (lnum == 0) /* no valid number, try using line() */
1031 {
1032 rettv.v_type = VAR_NUMBER;
1033 f_line(argvars, &rettv);
1034 lnum = (linenr_T)rettv.vval.v_number;
1035 clear_tv(&rettv);
1036 }
1037 return lnum;
1038}
1039
1040#ifdef FEAT_FLOAT
1041static int get_float_arg(typval_T *argvars, float_T *f);
1042
1043/*
1044 * Get the float value of "argvars[0]" into "f".
1045 * Returns FAIL when the argument is not a Number or Float.
1046 */
1047 static int
1048get_float_arg(typval_T *argvars, float_T *f)
1049{
1050 if (argvars[0].v_type == VAR_FLOAT)
1051 {
1052 *f = argvars[0].vval.v_float;
1053 return OK;
1054 }
1055 if (argvars[0].v_type == VAR_NUMBER)
1056 {
1057 *f = (float_T)argvars[0].vval.v_number;
1058 return OK;
1059 }
1060 EMSG(_("E808: Number or Float required"));
1061 return FAIL;
1062}
1063
1064/*
1065 * "abs(expr)" function
1066 */
1067 static void
1068f_abs(typval_T *argvars, typval_T *rettv)
1069{
1070 if (argvars[0].v_type == VAR_FLOAT)
1071 {
1072 rettv->v_type = VAR_FLOAT;
1073 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1074 }
1075 else
1076 {
1077 varnumber_T n;
1078 int error = FALSE;
1079
1080 n = get_tv_number_chk(&argvars[0], &error);
1081 if (error)
1082 rettv->vval.v_number = -1;
1083 else if (n > 0)
1084 rettv->vval.v_number = n;
1085 else
1086 rettv->vval.v_number = -n;
1087 }
1088}
1089
1090/*
1091 * "acos()" function
1092 */
1093 static void
1094f_acos(typval_T *argvars, typval_T *rettv)
1095{
1096 float_T f = 0.0;
1097
1098 rettv->v_type = VAR_FLOAT;
1099 if (get_float_arg(argvars, &f) == OK)
1100 rettv->vval.v_float = acos(f);
1101 else
1102 rettv->vval.v_float = 0.0;
1103}
1104#endif
1105
1106/*
1107 * "add(list, item)" function
1108 */
1109 static void
1110f_add(typval_T *argvars, typval_T *rettv)
1111{
1112 list_T *l;
1113
1114 rettv->vval.v_number = 1; /* Default: Failed */
1115 if (argvars[0].v_type == VAR_LIST)
1116 {
1117 if ((l = argvars[0].vval.v_list) != NULL
1118 && !tv_check_lock(l->lv_lock,
1119 (char_u *)N_("add() argument"), TRUE)
1120 && list_append_tv(l, &argvars[1]) == OK)
1121 copy_tv(&argvars[0], rettv);
1122 }
1123 else
1124 EMSG(_(e_listreq));
1125}
1126
1127/*
1128 * "and(expr, expr)" function
1129 */
1130 static void
1131f_and(typval_T *argvars, typval_T *rettv)
1132{
1133 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1134 & get_tv_number_chk(&argvars[1], NULL);
1135}
1136
1137/*
1138 * "append(lnum, string/list)" function
1139 */
1140 static void
1141f_append(typval_T *argvars, typval_T *rettv)
1142{
1143 long lnum;
1144 char_u *line;
1145 list_T *l = NULL;
1146 listitem_T *li = NULL;
1147 typval_T *tv;
1148 long added = 0;
1149
1150 /* When coming here from Insert mode, sync undo, so that this can be
1151 * undone separately from what was previously inserted. */
1152 if (u_sync_once == 2)
1153 {
1154 u_sync_once = 1; /* notify that u_sync() was called */
1155 u_sync(TRUE);
1156 }
1157
1158 lnum = get_tv_lnum(argvars);
1159 if (lnum >= 0
1160 && lnum <= curbuf->b_ml.ml_line_count
1161 && u_save(lnum, lnum + 1) == OK)
1162 {
1163 if (argvars[1].v_type == VAR_LIST)
1164 {
1165 l = argvars[1].vval.v_list;
1166 if (l == NULL)
1167 return;
1168 li = l->lv_first;
1169 }
1170 for (;;)
1171 {
1172 if (l == NULL)
1173 tv = &argvars[1]; /* append a string */
1174 else if (li == NULL)
1175 break; /* end of list */
1176 else
1177 tv = &li->li_tv; /* append item from list */
1178 line = get_tv_string_chk(tv);
1179 if (line == NULL) /* type error */
1180 {
1181 rettv->vval.v_number = 1; /* Failed */
1182 break;
1183 }
1184 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1185 ++added;
1186 if (l == NULL)
1187 break;
1188 li = li->li_next;
1189 }
1190
1191 appended_lines_mark(lnum, added);
1192 if (curwin->w_cursor.lnum > lnum)
1193 curwin->w_cursor.lnum += added;
1194 }
1195 else
1196 rettv->vval.v_number = 1; /* Failed */
1197}
1198
1199/*
1200 * "argc()" function
1201 */
1202 static void
1203f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1204{
1205 rettv->vval.v_number = ARGCOUNT;
1206}
1207
1208/*
1209 * "argidx()" function
1210 */
1211 static void
1212f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1213{
1214 rettv->vval.v_number = curwin->w_arg_idx;
1215}
1216
1217/*
1218 * "arglistid()" function
1219 */
1220 static void
1221f_arglistid(typval_T *argvars, typval_T *rettv)
1222{
1223 win_T *wp;
1224
1225 rettv->vval.v_number = -1;
1226 wp = find_tabwin(&argvars[0], &argvars[1]);
1227 if (wp != NULL)
1228 rettv->vval.v_number = wp->w_alist->id;
1229}
1230
1231/*
1232 * "argv(nr)" function
1233 */
1234 static void
1235f_argv(typval_T *argvars, typval_T *rettv)
1236{
1237 int idx;
1238
1239 if (argvars[0].v_type != VAR_UNKNOWN)
1240 {
1241 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1242 if (idx >= 0 && idx < ARGCOUNT)
1243 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1244 else
1245 rettv->vval.v_string = NULL;
1246 rettv->v_type = VAR_STRING;
1247 }
1248 else if (rettv_list_alloc(rettv) == OK)
1249 for (idx = 0; idx < ARGCOUNT; ++idx)
1250 list_append_string(rettv->vval.v_list,
1251 alist_name(&ARGLIST[idx]), -1);
1252}
1253
1254/*
1255 * "assert_equal(expected, actual[, msg])" function
1256 */
1257 static void
1258f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1259{
1260 assert_equal_common(argvars, ASSERT_EQUAL);
1261}
1262
1263/*
1264 * "assert_notequal(expected, actual[, msg])" function
1265 */
1266 static void
1267f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1268{
1269 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1270}
1271
1272/*
1273 * "assert_exception(string[, msg])" function
1274 */
1275 static void
1276f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1277{
1278 assert_exception(argvars);
1279}
1280
1281/*
1282 * "assert_fails(cmd [, error])" function
1283 */
1284 static void
1285f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1286{
1287 assert_fails(argvars);
1288}
1289
1290/*
1291 * "assert_false(actual[, msg])" function
1292 */
1293 static void
1294f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1295{
1296 assert_bool(argvars, FALSE);
1297}
1298
1299/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001300 * "assert_inrange(lower, upper[, msg])" function
1301 */
1302 static void
1303f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1304{
1305 assert_inrange(argvars);
1306}
1307
1308/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001309 * "assert_match(pattern, actual[, msg])" function
1310 */
1311 static void
1312f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1313{
1314 assert_match_common(argvars, ASSERT_MATCH);
1315}
1316
1317/*
1318 * "assert_notmatch(pattern, actual[, msg])" function
1319 */
1320 static void
1321f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1322{
1323 assert_match_common(argvars, ASSERT_NOTMATCH);
1324}
1325
1326/*
1327 * "assert_true(actual[, msg])" function
1328 */
1329 static void
1330f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1331{
1332 assert_bool(argvars, TRUE);
1333}
1334
1335#ifdef FEAT_FLOAT
1336/*
1337 * "asin()" function
1338 */
1339 static void
1340f_asin(typval_T *argvars, typval_T *rettv)
1341{
1342 float_T f = 0.0;
1343
1344 rettv->v_type = VAR_FLOAT;
1345 if (get_float_arg(argvars, &f) == OK)
1346 rettv->vval.v_float = asin(f);
1347 else
1348 rettv->vval.v_float = 0.0;
1349}
1350
1351/*
1352 * "atan()" function
1353 */
1354 static void
1355f_atan(typval_T *argvars, typval_T *rettv)
1356{
1357 float_T f = 0.0;
1358
1359 rettv->v_type = VAR_FLOAT;
1360 if (get_float_arg(argvars, &f) == OK)
1361 rettv->vval.v_float = atan(f);
1362 else
1363 rettv->vval.v_float = 0.0;
1364}
1365
1366/*
1367 * "atan2()" function
1368 */
1369 static void
1370f_atan2(typval_T *argvars, typval_T *rettv)
1371{
1372 float_T fx = 0.0, fy = 0.0;
1373
1374 rettv->v_type = VAR_FLOAT;
1375 if (get_float_arg(argvars, &fx) == OK
1376 && get_float_arg(&argvars[1], &fy) == OK)
1377 rettv->vval.v_float = atan2(fx, fy);
1378 else
1379 rettv->vval.v_float = 0.0;
1380}
1381#endif
1382
1383/*
1384 * "browse(save, title, initdir, default)" function
1385 */
1386 static void
1387f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1388{
1389#ifdef FEAT_BROWSE
1390 int save;
1391 char_u *title;
1392 char_u *initdir;
1393 char_u *defname;
1394 char_u buf[NUMBUFLEN];
1395 char_u buf2[NUMBUFLEN];
1396 int error = FALSE;
1397
1398 save = (int)get_tv_number_chk(&argvars[0], &error);
1399 title = get_tv_string_chk(&argvars[1]);
1400 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1401 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1402
1403 if (error || title == NULL || initdir == NULL || defname == NULL)
1404 rettv->vval.v_string = NULL;
1405 else
1406 rettv->vval.v_string =
1407 do_browse(save ? BROWSE_SAVE : 0,
1408 title, defname, NULL, initdir, NULL, curbuf);
1409#else
1410 rettv->vval.v_string = NULL;
1411#endif
1412 rettv->v_type = VAR_STRING;
1413}
1414
1415/*
1416 * "browsedir(title, initdir)" function
1417 */
1418 static void
1419f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1420{
1421#ifdef FEAT_BROWSE
1422 char_u *title;
1423 char_u *initdir;
1424 char_u buf[NUMBUFLEN];
1425
1426 title = get_tv_string_chk(&argvars[0]);
1427 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1428
1429 if (title == NULL || initdir == NULL)
1430 rettv->vval.v_string = NULL;
1431 else
1432 rettv->vval.v_string = do_browse(BROWSE_DIR,
1433 title, NULL, NULL, initdir, NULL, curbuf);
1434#else
1435 rettv->vval.v_string = NULL;
1436#endif
1437 rettv->v_type = VAR_STRING;
1438}
1439
1440static buf_T *find_buffer(typval_T *avar);
1441
1442/*
1443 * Find a buffer by number or exact name.
1444 */
1445 static buf_T *
1446find_buffer(typval_T *avar)
1447{
1448 buf_T *buf = NULL;
1449
1450 if (avar->v_type == VAR_NUMBER)
1451 buf = buflist_findnr((int)avar->vval.v_number);
1452 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1453 {
1454 buf = buflist_findname_exp(avar->vval.v_string);
1455 if (buf == NULL)
1456 {
1457 /* No full path name match, try a match with a URL or a "nofile"
1458 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001459 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001460 if (buf->b_fname != NULL
1461 && (path_with_url(buf->b_fname)
1462#ifdef FEAT_QUICKFIX
1463 || bt_nofile(buf)
1464#endif
1465 )
1466 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1467 break;
1468 }
1469 }
1470 return buf;
1471}
1472
1473/*
1474 * "bufexists(expr)" function
1475 */
1476 static void
1477f_bufexists(typval_T *argvars, typval_T *rettv)
1478{
1479 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1480}
1481
1482/*
1483 * "buflisted(expr)" function
1484 */
1485 static void
1486f_buflisted(typval_T *argvars, typval_T *rettv)
1487{
1488 buf_T *buf;
1489
1490 buf = find_buffer(&argvars[0]);
1491 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1492}
1493
1494/*
1495 * "bufloaded(expr)" function
1496 */
1497 static void
1498f_bufloaded(typval_T *argvars, typval_T *rettv)
1499{
1500 buf_T *buf;
1501
1502 buf = find_buffer(&argvars[0]);
1503 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1504}
1505
1506 buf_T *
1507buflist_find_by_name(char_u *name, int curtab_only)
1508{
1509 int save_magic;
1510 char_u *save_cpo;
1511 buf_T *buf;
1512
1513 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1514 save_magic = p_magic;
1515 p_magic = TRUE;
1516 save_cpo = p_cpo;
1517 p_cpo = (char_u *)"";
1518
1519 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1520 TRUE, FALSE, curtab_only));
1521
1522 p_magic = save_magic;
1523 p_cpo = save_cpo;
1524 return buf;
1525}
1526
1527/*
1528 * Get buffer by number or pattern.
1529 */
1530 static buf_T *
1531get_buf_tv(typval_T *tv, int curtab_only)
1532{
1533 char_u *name = tv->vval.v_string;
1534 buf_T *buf;
1535
1536 if (tv->v_type == VAR_NUMBER)
1537 return buflist_findnr((int)tv->vval.v_number);
1538 if (tv->v_type != VAR_STRING)
1539 return NULL;
1540 if (name == NULL || *name == NUL)
1541 return curbuf;
1542 if (name[0] == '$' && name[1] == NUL)
1543 return lastbuf;
1544
1545 buf = buflist_find_by_name(name, curtab_only);
1546
1547 /* If not found, try expanding the name, like done for bufexists(). */
1548 if (buf == NULL)
1549 buf = find_buffer(tv);
1550
1551 return buf;
1552}
1553
1554/*
1555 * "bufname(expr)" function
1556 */
1557 static void
1558f_bufname(typval_T *argvars, typval_T *rettv)
1559{
1560 buf_T *buf;
1561
1562 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1563 ++emsg_off;
1564 buf = get_buf_tv(&argvars[0], FALSE);
1565 rettv->v_type = VAR_STRING;
1566 if (buf != NULL && buf->b_fname != NULL)
1567 rettv->vval.v_string = vim_strsave(buf->b_fname);
1568 else
1569 rettv->vval.v_string = NULL;
1570 --emsg_off;
1571}
1572
1573/*
1574 * "bufnr(expr)" function
1575 */
1576 static void
1577f_bufnr(typval_T *argvars, typval_T *rettv)
1578{
1579 buf_T *buf;
1580 int error = FALSE;
1581 char_u *name;
1582
1583 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1584 ++emsg_off;
1585 buf = get_buf_tv(&argvars[0], FALSE);
1586 --emsg_off;
1587
1588 /* If the buffer isn't found and the second argument is not zero create a
1589 * new buffer. */
1590 if (buf == NULL
1591 && argvars[1].v_type != VAR_UNKNOWN
1592 && get_tv_number_chk(&argvars[1], &error) != 0
1593 && !error
1594 && (name = get_tv_string_chk(&argvars[0])) != NULL
1595 && !error)
1596 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1597
1598 if (buf != NULL)
1599 rettv->vval.v_number = buf->b_fnum;
1600 else
1601 rettv->vval.v_number = -1;
1602}
1603
1604 static void
1605buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1606{
1607#ifdef FEAT_WINDOWS
1608 win_T *wp;
1609 int winnr = 0;
1610#endif
1611 buf_T *buf;
1612
1613 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1614 ++emsg_off;
1615 buf = get_buf_tv(&argvars[0], TRUE);
1616#ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001617 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001618 {
1619 ++winnr;
1620 if (wp->w_buffer == buf)
1621 break;
1622 }
1623 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
1624#else
1625 rettv->vval.v_number = (curwin->w_buffer == buf
1626 ? (get_nr ? 1 : curwin->w_id) : -1);
1627#endif
1628 --emsg_off;
1629}
1630
1631/*
1632 * "bufwinid(nr)" function
1633 */
1634 static void
1635f_bufwinid(typval_T *argvars, typval_T *rettv)
1636{
1637 buf_win_common(argvars, rettv, FALSE);
1638}
1639
1640/*
1641 * "bufwinnr(nr)" function
1642 */
1643 static void
1644f_bufwinnr(typval_T *argvars, typval_T *rettv)
1645{
1646 buf_win_common(argvars, rettv, TRUE);
1647}
1648
1649/*
1650 * "byte2line(byte)" function
1651 */
1652 static void
1653f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1654{
1655#ifndef FEAT_BYTEOFF
1656 rettv->vval.v_number = -1;
1657#else
1658 long boff = 0;
1659
1660 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1661 if (boff < 0)
1662 rettv->vval.v_number = -1;
1663 else
1664 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1665 (linenr_T)0, &boff);
1666#endif
1667}
1668
1669 static void
1670byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1671{
1672#ifdef FEAT_MBYTE
1673 char_u *t;
1674#endif
1675 char_u *str;
1676 varnumber_T idx;
1677
1678 str = get_tv_string_chk(&argvars[0]);
1679 idx = get_tv_number_chk(&argvars[1], NULL);
1680 rettv->vval.v_number = -1;
1681 if (str == NULL || idx < 0)
1682 return;
1683
1684#ifdef FEAT_MBYTE
1685 t = str;
1686 for ( ; idx > 0; idx--)
1687 {
1688 if (*t == NUL) /* EOL reached */
1689 return;
1690 if (enc_utf8 && comp)
1691 t += utf_ptr2len(t);
1692 else
1693 t += (*mb_ptr2len)(t);
1694 }
1695 rettv->vval.v_number = (varnumber_T)(t - str);
1696#else
1697 if ((size_t)idx <= STRLEN(str))
1698 rettv->vval.v_number = idx;
1699#endif
1700}
1701
1702/*
1703 * "byteidx()" function
1704 */
1705 static void
1706f_byteidx(typval_T *argvars, typval_T *rettv)
1707{
1708 byteidx(argvars, rettv, FALSE);
1709}
1710
1711/*
1712 * "byteidxcomp()" function
1713 */
1714 static void
1715f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1716{
1717 byteidx(argvars, rettv, TRUE);
1718}
1719
1720/*
1721 * "call(func, arglist [, dict])" function
1722 */
1723 static void
1724f_call(typval_T *argvars, typval_T *rettv)
1725{
1726 char_u *func;
1727 partial_T *partial = NULL;
1728 dict_T *selfdict = NULL;
1729
1730 if (argvars[1].v_type != VAR_LIST)
1731 {
1732 EMSG(_(e_listreq));
1733 return;
1734 }
1735 if (argvars[1].vval.v_list == NULL)
1736 return;
1737
1738 if (argvars[0].v_type == VAR_FUNC)
1739 func = argvars[0].vval.v_string;
1740 else if (argvars[0].v_type == VAR_PARTIAL)
1741 {
1742 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001743 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001744 }
1745 else
1746 func = get_tv_string(&argvars[0]);
1747 if (*func == NUL)
1748 return; /* type error or empty name */
1749
1750 if (argvars[2].v_type != VAR_UNKNOWN)
1751 {
1752 if (argvars[2].v_type != VAR_DICT)
1753 {
1754 EMSG(_(e_dictreq));
1755 return;
1756 }
1757 selfdict = argvars[2].vval.v_dict;
1758 }
1759
1760 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1761}
1762
1763#ifdef FEAT_FLOAT
1764/*
1765 * "ceil({float})" function
1766 */
1767 static void
1768f_ceil(typval_T *argvars, typval_T *rettv)
1769{
1770 float_T f = 0.0;
1771
1772 rettv->v_type = VAR_FLOAT;
1773 if (get_float_arg(argvars, &f) == OK)
1774 rettv->vval.v_float = ceil(f);
1775 else
1776 rettv->vval.v_float = 0.0;
1777}
1778#endif
1779
1780#ifdef FEAT_JOB_CHANNEL
1781/*
1782 * "ch_close()" function
1783 */
1784 static void
1785f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1786{
1787 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1788
1789 if (channel != NULL)
1790 {
1791 channel_close(channel, FALSE);
1792 channel_clear(channel);
1793 }
1794}
1795
1796/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001797 * "ch_close()" function
1798 */
1799 static void
1800f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1801{
1802 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1803
1804 if (channel != NULL)
1805 channel_close_in(channel);
1806}
1807
1808/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001809 * "ch_getbufnr()" function
1810 */
1811 static void
1812f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1813{
1814 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1815
1816 rettv->vval.v_number = -1;
1817 if (channel != NULL)
1818 {
1819 char_u *what = get_tv_string(&argvars[1]);
1820 int part;
1821
1822 if (STRCMP(what, "err") == 0)
1823 part = PART_ERR;
1824 else if (STRCMP(what, "out") == 0)
1825 part = PART_OUT;
1826 else if (STRCMP(what, "in") == 0)
1827 part = PART_IN;
1828 else
1829 part = PART_SOCK;
1830 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1831 rettv->vval.v_number =
1832 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1833 }
1834}
1835
1836/*
1837 * "ch_getjob()" function
1838 */
1839 static void
1840f_ch_getjob(typval_T *argvars, typval_T *rettv)
1841{
1842 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1843
1844 if (channel != NULL)
1845 {
1846 rettv->v_type = VAR_JOB;
1847 rettv->vval.v_job = channel->ch_job;
1848 if (channel->ch_job != NULL)
1849 ++channel->ch_job->jv_refcount;
1850 }
1851}
1852
1853/*
1854 * "ch_info()" function
1855 */
1856 static void
1857f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1858{
1859 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1860
1861 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1862 channel_info(channel, rettv->vval.v_dict);
1863}
1864
1865/*
1866 * "ch_log()" function
1867 */
1868 static void
1869f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1870{
1871 char_u *msg = get_tv_string(&argvars[0]);
1872 channel_T *channel = NULL;
1873
1874 if (argvars[1].v_type != VAR_UNKNOWN)
1875 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1876
1877 ch_log(channel, (char *)msg);
1878}
1879
1880/*
1881 * "ch_logfile()" function
1882 */
1883 static void
1884f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
1885{
1886 char_u *fname;
1887 char_u *opt = (char_u *)"";
1888 char_u buf[NUMBUFLEN];
1889
1890 fname = get_tv_string(&argvars[0]);
1891 if (argvars[1].v_type == VAR_STRING)
1892 opt = get_tv_string_buf(&argvars[1], buf);
1893 ch_logfile(fname, opt);
1894}
1895
1896/*
1897 * "ch_open()" function
1898 */
1899 static void
1900f_ch_open(typval_T *argvars, typval_T *rettv)
1901{
1902 rettv->v_type = VAR_CHANNEL;
1903 if (check_restricted() || check_secure())
1904 return;
1905 rettv->vval.v_channel = channel_open_func(argvars);
1906}
1907
1908/*
1909 * "ch_read()" function
1910 */
1911 static void
1912f_ch_read(typval_T *argvars, typval_T *rettv)
1913{
1914 common_channel_read(argvars, rettv, FALSE);
1915}
1916
1917/*
1918 * "ch_readraw()" function
1919 */
1920 static void
1921f_ch_readraw(typval_T *argvars, typval_T *rettv)
1922{
1923 common_channel_read(argvars, rettv, TRUE);
1924}
1925
1926/*
1927 * "ch_evalexpr()" function
1928 */
1929 static void
1930f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
1931{
1932 ch_expr_common(argvars, rettv, TRUE);
1933}
1934
1935/*
1936 * "ch_sendexpr()" function
1937 */
1938 static void
1939f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
1940{
1941 ch_expr_common(argvars, rettv, FALSE);
1942}
1943
1944/*
1945 * "ch_evalraw()" function
1946 */
1947 static void
1948f_ch_evalraw(typval_T *argvars, typval_T *rettv)
1949{
1950 ch_raw_common(argvars, rettv, TRUE);
1951}
1952
1953/*
1954 * "ch_sendraw()" function
1955 */
1956 static void
1957f_ch_sendraw(typval_T *argvars, typval_T *rettv)
1958{
1959 ch_raw_common(argvars, rettv, FALSE);
1960}
1961
1962/*
1963 * "ch_setoptions()" function
1964 */
1965 static void
1966f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1967{
1968 channel_T *channel;
1969 jobopt_T opt;
1970
1971 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1972 if (channel == NULL)
1973 return;
1974 clear_job_options(&opt);
1975 if (get_job_options(&argvars[1], &opt,
1976 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
1977 channel_set_options(channel, &opt);
1978 free_job_options(&opt);
1979}
1980
1981/*
1982 * "ch_status()" function
1983 */
1984 static void
1985f_ch_status(typval_T *argvars, typval_T *rettv)
1986{
1987 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02001988 jobopt_T opt;
1989 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001990
1991 /* return an empty string by default */
1992 rettv->v_type = VAR_STRING;
1993 rettv->vval.v_string = NULL;
1994
1995 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02001996
1997 if (argvars[1].v_type != VAR_UNKNOWN)
1998 {
1999 clear_job_options(&opt);
2000 if (get_job_options(&argvars[1], &opt, JO_PART) == OK
2001 && (opt.jo_set & JO_PART))
2002 part = opt.jo_part;
2003 }
2004
2005 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002006}
2007#endif
2008
2009/*
2010 * "changenr()" function
2011 */
2012 static void
2013f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2014{
2015 rettv->vval.v_number = curbuf->b_u_seq_cur;
2016}
2017
2018/*
2019 * "char2nr(string)" function
2020 */
2021 static void
2022f_char2nr(typval_T *argvars, typval_T *rettv)
2023{
2024#ifdef FEAT_MBYTE
2025 if (has_mbyte)
2026 {
2027 int utf8 = 0;
2028
2029 if (argvars[1].v_type != VAR_UNKNOWN)
2030 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2031
2032 if (utf8)
2033 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2034 else
2035 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2036 }
2037 else
2038#endif
2039 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2040}
2041
2042/*
2043 * "cindent(lnum)" function
2044 */
2045 static void
2046f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2047{
2048#ifdef FEAT_CINDENT
2049 pos_T pos;
2050 linenr_T lnum;
2051
2052 pos = curwin->w_cursor;
2053 lnum = get_tv_lnum(argvars);
2054 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2055 {
2056 curwin->w_cursor.lnum = lnum;
2057 rettv->vval.v_number = get_c_indent();
2058 curwin->w_cursor = pos;
2059 }
2060 else
2061#endif
2062 rettv->vval.v_number = -1;
2063}
2064
2065/*
2066 * "clearmatches()" function
2067 */
2068 static void
2069f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2070{
2071#ifdef FEAT_SEARCH_EXTRA
2072 clear_matches(curwin);
2073#endif
2074}
2075
2076/*
2077 * "col(string)" function
2078 */
2079 static void
2080f_col(typval_T *argvars, typval_T *rettv)
2081{
2082 colnr_T col = 0;
2083 pos_T *fp;
2084 int fnum = curbuf->b_fnum;
2085
2086 fp = var2fpos(&argvars[0], FALSE, &fnum);
2087 if (fp != NULL && fnum == curbuf->b_fnum)
2088 {
2089 if (fp->col == MAXCOL)
2090 {
2091 /* '> can be MAXCOL, get the length of the line then */
2092 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2093 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2094 else
2095 col = MAXCOL;
2096 }
2097 else
2098 {
2099 col = fp->col + 1;
2100#ifdef FEAT_VIRTUALEDIT
2101 /* col(".") when the cursor is on the NUL at the end of the line
2102 * because of "coladd" can be seen as an extra column. */
2103 if (virtual_active() && fp == &curwin->w_cursor)
2104 {
2105 char_u *p = ml_get_cursor();
2106
2107 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2108 curwin->w_virtcol - curwin->w_cursor.coladd))
2109 {
2110# ifdef FEAT_MBYTE
2111 int l;
2112
2113 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2114 col += l;
2115# else
2116 if (*p != NUL && p[1] == NUL)
2117 ++col;
2118# endif
2119 }
2120 }
2121#endif
2122 }
2123 }
2124 rettv->vval.v_number = col;
2125}
2126
2127#if defined(FEAT_INS_EXPAND)
2128/*
2129 * "complete()" function
2130 */
2131 static void
2132f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2133{
2134 int startcol;
2135
2136 if ((State & INSERT) == 0)
2137 {
2138 EMSG(_("E785: complete() can only be used in Insert mode"));
2139 return;
2140 }
2141
2142 /* Check for undo allowed here, because if something was already inserted
2143 * the line was already saved for undo and this check isn't done. */
2144 if (!undo_allowed())
2145 return;
2146
2147 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2148 {
2149 EMSG(_(e_invarg));
2150 return;
2151 }
2152
2153 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2154 if (startcol <= 0)
2155 return;
2156
2157 set_completion(startcol - 1, argvars[1].vval.v_list);
2158}
2159
2160/*
2161 * "complete_add()" function
2162 */
2163 static void
2164f_complete_add(typval_T *argvars, typval_T *rettv)
2165{
2166 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2167}
2168
2169/*
2170 * "complete_check()" function
2171 */
2172 static void
2173f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2174{
2175 int saved = RedrawingDisabled;
2176
2177 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002178 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002179 rettv->vval.v_number = compl_interrupted;
2180 RedrawingDisabled = saved;
2181}
2182#endif
2183
2184/*
2185 * "confirm(message, buttons[, default [, type]])" function
2186 */
2187 static void
2188f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2189{
2190#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2191 char_u *message;
2192 char_u *buttons = NULL;
2193 char_u buf[NUMBUFLEN];
2194 char_u buf2[NUMBUFLEN];
2195 int def = 1;
2196 int type = VIM_GENERIC;
2197 char_u *typestr;
2198 int error = FALSE;
2199
2200 message = get_tv_string_chk(&argvars[0]);
2201 if (message == NULL)
2202 error = TRUE;
2203 if (argvars[1].v_type != VAR_UNKNOWN)
2204 {
2205 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2206 if (buttons == NULL)
2207 error = TRUE;
2208 if (argvars[2].v_type != VAR_UNKNOWN)
2209 {
2210 def = (int)get_tv_number_chk(&argvars[2], &error);
2211 if (argvars[3].v_type != VAR_UNKNOWN)
2212 {
2213 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2214 if (typestr == NULL)
2215 error = TRUE;
2216 else
2217 {
2218 switch (TOUPPER_ASC(*typestr))
2219 {
2220 case 'E': type = VIM_ERROR; break;
2221 case 'Q': type = VIM_QUESTION; break;
2222 case 'I': type = VIM_INFO; break;
2223 case 'W': type = VIM_WARNING; break;
2224 case 'G': type = VIM_GENERIC; break;
2225 }
2226 }
2227 }
2228 }
2229 }
2230
2231 if (buttons == NULL || *buttons == NUL)
2232 buttons = (char_u *)_("&Ok");
2233
2234 if (!error)
2235 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2236 def, NULL, FALSE);
2237#endif
2238}
2239
2240/*
2241 * "copy()" function
2242 */
2243 static void
2244f_copy(typval_T *argvars, typval_T *rettv)
2245{
2246 item_copy(&argvars[0], rettv, FALSE, 0);
2247}
2248
2249#ifdef FEAT_FLOAT
2250/*
2251 * "cos()" function
2252 */
2253 static void
2254f_cos(typval_T *argvars, typval_T *rettv)
2255{
2256 float_T f = 0.0;
2257
2258 rettv->v_type = VAR_FLOAT;
2259 if (get_float_arg(argvars, &f) == OK)
2260 rettv->vval.v_float = cos(f);
2261 else
2262 rettv->vval.v_float = 0.0;
2263}
2264
2265/*
2266 * "cosh()" function
2267 */
2268 static void
2269f_cosh(typval_T *argvars, typval_T *rettv)
2270{
2271 float_T f = 0.0;
2272
2273 rettv->v_type = VAR_FLOAT;
2274 if (get_float_arg(argvars, &f) == OK)
2275 rettv->vval.v_float = cosh(f);
2276 else
2277 rettv->vval.v_float = 0.0;
2278}
2279#endif
2280
2281/*
2282 * "count()" function
2283 */
2284 static void
2285f_count(typval_T *argvars, typval_T *rettv)
2286{
2287 long n = 0;
2288 int ic = FALSE;
2289
2290 if (argvars[0].v_type == VAR_LIST)
2291 {
2292 listitem_T *li;
2293 list_T *l;
2294 long idx;
2295
2296 if ((l = argvars[0].vval.v_list) != NULL)
2297 {
2298 li = l->lv_first;
2299 if (argvars[2].v_type != VAR_UNKNOWN)
2300 {
2301 int error = FALSE;
2302
2303 ic = (int)get_tv_number_chk(&argvars[2], &error);
2304 if (argvars[3].v_type != VAR_UNKNOWN)
2305 {
2306 idx = (long)get_tv_number_chk(&argvars[3], &error);
2307 if (!error)
2308 {
2309 li = list_find(l, idx);
2310 if (li == NULL)
2311 EMSGN(_(e_listidx), idx);
2312 }
2313 }
2314 if (error)
2315 li = NULL;
2316 }
2317
2318 for ( ; li != NULL; li = li->li_next)
2319 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2320 ++n;
2321 }
2322 }
2323 else if (argvars[0].v_type == VAR_DICT)
2324 {
2325 int todo;
2326 dict_T *d;
2327 hashitem_T *hi;
2328
2329 if ((d = argvars[0].vval.v_dict) != NULL)
2330 {
2331 int error = FALSE;
2332
2333 if (argvars[2].v_type != VAR_UNKNOWN)
2334 {
2335 ic = (int)get_tv_number_chk(&argvars[2], &error);
2336 if (argvars[3].v_type != VAR_UNKNOWN)
2337 EMSG(_(e_invarg));
2338 }
2339
2340 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2341 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2342 {
2343 if (!HASHITEM_EMPTY(hi))
2344 {
2345 --todo;
2346 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2347 ++n;
2348 }
2349 }
2350 }
2351 }
2352 else
2353 EMSG2(_(e_listdictarg), "count()");
2354 rettv->vval.v_number = n;
2355}
2356
2357/*
2358 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2359 *
2360 * Checks the existence of a cscope connection.
2361 */
2362 static void
2363f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2364{
2365#ifdef FEAT_CSCOPE
2366 int num = 0;
2367 char_u *dbpath = NULL;
2368 char_u *prepend = NULL;
2369 char_u buf[NUMBUFLEN];
2370
2371 if (argvars[0].v_type != VAR_UNKNOWN
2372 && argvars[1].v_type != VAR_UNKNOWN)
2373 {
2374 num = (int)get_tv_number(&argvars[0]);
2375 dbpath = get_tv_string(&argvars[1]);
2376 if (argvars[2].v_type != VAR_UNKNOWN)
2377 prepend = get_tv_string_buf(&argvars[2], buf);
2378 }
2379
2380 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2381#endif
2382}
2383
2384/*
2385 * "cursor(lnum, col)" function, or
2386 * "cursor(list)"
2387 *
2388 * Moves the cursor to the specified line and column.
2389 * Returns 0 when the position could be set, -1 otherwise.
2390 */
2391 static void
2392f_cursor(typval_T *argvars, typval_T *rettv)
2393{
2394 long line, col;
2395#ifdef FEAT_VIRTUALEDIT
2396 long coladd = 0;
2397#endif
2398 int set_curswant = TRUE;
2399
2400 rettv->vval.v_number = -1;
2401 if (argvars[1].v_type == VAR_UNKNOWN)
2402 {
2403 pos_T pos;
2404 colnr_T curswant = -1;
2405
2406 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2407 {
2408 EMSG(_(e_invarg));
2409 return;
2410 }
2411 line = pos.lnum;
2412 col = pos.col;
2413#ifdef FEAT_VIRTUALEDIT
2414 coladd = pos.coladd;
2415#endif
2416 if (curswant >= 0)
2417 {
2418 curwin->w_curswant = curswant - 1;
2419 set_curswant = FALSE;
2420 }
2421 }
2422 else
2423 {
2424 line = get_tv_lnum(argvars);
2425 col = (long)get_tv_number_chk(&argvars[1], NULL);
2426#ifdef FEAT_VIRTUALEDIT
2427 if (argvars[2].v_type != VAR_UNKNOWN)
2428 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2429#endif
2430 }
2431 if (line < 0 || col < 0
2432#ifdef FEAT_VIRTUALEDIT
2433 || coladd < 0
2434#endif
2435 )
2436 return; /* type error; errmsg already given */
2437 if (line > 0)
2438 curwin->w_cursor.lnum = line;
2439 if (col > 0)
2440 curwin->w_cursor.col = col - 1;
2441#ifdef FEAT_VIRTUALEDIT
2442 curwin->w_cursor.coladd = coladd;
2443#endif
2444
2445 /* Make sure the cursor is in a valid position. */
2446 check_cursor();
2447#ifdef FEAT_MBYTE
2448 /* Correct cursor for multi-byte character. */
2449 if (has_mbyte)
2450 mb_adjust_cursor();
2451#endif
2452
2453 curwin->w_set_curswant = set_curswant;
2454 rettv->vval.v_number = 0;
2455}
2456
2457/*
2458 * "deepcopy()" function
2459 */
2460 static void
2461f_deepcopy(typval_T *argvars, typval_T *rettv)
2462{
2463 int noref = 0;
2464 int copyID;
2465
2466 if (argvars[1].v_type != VAR_UNKNOWN)
2467 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2468 if (noref < 0 || noref > 1)
2469 EMSG(_(e_invarg));
2470 else
2471 {
2472 copyID = get_copyID();
2473 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2474 }
2475}
2476
2477/*
2478 * "delete()" function
2479 */
2480 static void
2481f_delete(typval_T *argvars, typval_T *rettv)
2482{
2483 char_u nbuf[NUMBUFLEN];
2484 char_u *name;
2485 char_u *flags;
2486
2487 rettv->vval.v_number = -1;
2488 if (check_restricted() || check_secure())
2489 return;
2490
2491 name = get_tv_string(&argvars[0]);
2492 if (name == NULL || *name == NUL)
2493 {
2494 EMSG(_(e_invarg));
2495 return;
2496 }
2497
2498 if (argvars[1].v_type != VAR_UNKNOWN)
2499 flags = get_tv_string_buf(&argvars[1], nbuf);
2500 else
2501 flags = (char_u *)"";
2502
2503 if (*flags == NUL)
2504 /* delete a file */
2505 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2506 else if (STRCMP(flags, "d") == 0)
2507 /* delete an empty directory */
2508 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2509 else if (STRCMP(flags, "rf") == 0)
2510 /* delete a directory recursively */
2511 rettv->vval.v_number = delete_recursive(name);
2512 else
2513 EMSG2(_(e_invexpr2), flags);
2514}
2515
2516/*
2517 * "did_filetype()" function
2518 */
2519 static void
2520f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2521{
2522#ifdef FEAT_AUTOCMD
2523 rettv->vval.v_number = did_filetype;
2524#endif
2525}
2526
2527/*
2528 * "diff_filler()" function
2529 */
2530 static void
2531f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2532{
2533#ifdef FEAT_DIFF
2534 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2535#endif
2536}
2537
2538/*
2539 * "diff_hlID()" function
2540 */
2541 static void
2542f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2543{
2544#ifdef FEAT_DIFF
2545 linenr_T lnum = get_tv_lnum(argvars);
2546 static linenr_T prev_lnum = 0;
2547 static int changedtick = 0;
2548 static int fnum = 0;
2549 static int change_start = 0;
2550 static int change_end = 0;
2551 static hlf_T hlID = (hlf_T)0;
2552 int filler_lines;
2553 int col;
2554
2555 if (lnum < 0) /* ignore type error in {lnum} arg */
2556 lnum = 0;
2557 if (lnum != prev_lnum
2558 || changedtick != curbuf->b_changedtick
2559 || fnum != curbuf->b_fnum)
2560 {
2561 /* New line, buffer, change: need to get the values. */
2562 filler_lines = diff_check(curwin, lnum);
2563 if (filler_lines < 0)
2564 {
2565 if (filler_lines == -1)
2566 {
2567 change_start = MAXCOL;
2568 change_end = -1;
2569 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2570 hlID = HLF_ADD; /* added line */
2571 else
2572 hlID = HLF_CHD; /* changed line */
2573 }
2574 else
2575 hlID = HLF_ADD; /* added line */
2576 }
2577 else
2578 hlID = (hlf_T)0;
2579 prev_lnum = lnum;
2580 changedtick = curbuf->b_changedtick;
2581 fnum = curbuf->b_fnum;
2582 }
2583
2584 if (hlID == HLF_CHD || hlID == HLF_TXD)
2585 {
2586 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2587 if (col >= change_start && col <= change_end)
2588 hlID = HLF_TXD; /* changed text */
2589 else
2590 hlID = HLF_CHD; /* changed line */
2591 }
2592 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2593#endif
2594}
2595
2596/*
2597 * "empty({expr})" function
2598 */
2599 static void
2600f_empty(typval_T *argvars, typval_T *rettv)
2601{
2602 int n = FALSE;
2603
2604 switch (argvars[0].v_type)
2605 {
2606 case VAR_STRING:
2607 case VAR_FUNC:
2608 n = argvars[0].vval.v_string == NULL
2609 || *argvars[0].vval.v_string == NUL;
2610 break;
2611 case VAR_PARTIAL:
2612 n = FALSE;
2613 break;
2614 case VAR_NUMBER:
2615 n = argvars[0].vval.v_number == 0;
2616 break;
2617 case VAR_FLOAT:
2618#ifdef FEAT_FLOAT
2619 n = argvars[0].vval.v_float == 0.0;
2620 break;
2621#endif
2622 case VAR_LIST:
2623 n = argvars[0].vval.v_list == NULL
2624 || argvars[0].vval.v_list->lv_first == NULL;
2625 break;
2626 case VAR_DICT:
2627 n = argvars[0].vval.v_dict == NULL
2628 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2629 break;
2630 case VAR_SPECIAL:
2631 n = argvars[0].vval.v_number != VVAL_TRUE;
2632 break;
2633
2634 case VAR_JOB:
2635#ifdef FEAT_JOB_CHANNEL
2636 n = argvars[0].vval.v_job == NULL
2637 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2638 break;
2639#endif
2640 case VAR_CHANNEL:
2641#ifdef FEAT_JOB_CHANNEL
2642 n = argvars[0].vval.v_channel == NULL
2643 || !channel_is_open(argvars[0].vval.v_channel);
2644 break;
2645#endif
2646 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002647 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002648 n = TRUE;
2649 break;
2650 }
2651
2652 rettv->vval.v_number = n;
2653}
2654
2655/*
2656 * "escape({string}, {chars})" function
2657 */
2658 static void
2659f_escape(typval_T *argvars, typval_T *rettv)
2660{
2661 char_u buf[NUMBUFLEN];
2662
2663 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2664 get_tv_string_buf(&argvars[1], buf));
2665 rettv->v_type = VAR_STRING;
2666}
2667
2668/*
2669 * "eval()" function
2670 */
2671 static void
2672f_eval(typval_T *argvars, typval_T *rettv)
2673{
2674 char_u *s, *p;
2675
2676 s = get_tv_string_chk(&argvars[0]);
2677 if (s != NULL)
2678 s = skipwhite(s);
2679
2680 p = s;
2681 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2682 {
2683 if (p != NULL && !aborting())
2684 EMSG2(_(e_invexpr2), p);
2685 need_clr_eos = FALSE;
2686 rettv->v_type = VAR_NUMBER;
2687 rettv->vval.v_number = 0;
2688 }
2689 else if (*s != NUL)
2690 EMSG(_(e_trailing));
2691}
2692
2693/*
2694 * "eventhandler()" function
2695 */
2696 static void
2697f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2698{
2699 rettv->vval.v_number = vgetc_busy;
2700}
2701
2702/*
2703 * "executable()" function
2704 */
2705 static void
2706f_executable(typval_T *argvars, typval_T *rettv)
2707{
2708 char_u *name = get_tv_string(&argvars[0]);
2709
2710 /* Check in $PATH and also check directly if there is a directory name. */
2711 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2712 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2713}
2714
2715static garray_T redir_execute_ga;
2716
2717/*
2718 * Append "value[value_len]" to the execute() output.
2719 */
2720 void
2721execute_redir_str(char_u *value, int value_len)
2722{
2723 int len;
2724
2725 if (value_len == -1)
2726 len = (int)STRLEN(value); /* Append the entire string */
2727 else
2728 len = value_len; /* Append only "value_len" characters */
2729 if (ga_grow(&redir_execute_ga, len) == OK)
2730 {
2731 mch_memmove((char *)redir_execute_ga.ga_data
2732 + redir_execute_ga.ga_len, value, len);
2733 redir_execute_ga.ga_len += len;
2734 }
2735}
2736
2737/*
2738 * Get next line from a list.
2739 * Called by do_cmdline() to get the next line.
2740 * Returns allocated string, or NULL for end of function.
2741 */
2742
2743 static char_u *
2744get_list_line(
2745 int c UNUSED,
2746 void *cookie,
2747 int indent UNUSED)
2748{
2749 listitem_T **p = (listitem_T **)cookie;
2750 listitem_T *item = *p;
2751 char_u buf[NUMBUFLEN];
2752 char_u *s;
2753
2754 if (item == NULL)
2755 return NULL;
2756 s = get_tv_string_buf_chk(&item->li_tv, buf);
2757 *p = item->li_next;
2758 return s == NULL ? NULL : vim_strsave(s);
2759}
2760
2761/*
2762 * "execute()" function
2763 */
2764 static void
2765f_execute(typval_T *argvars, typval_T *rettv)
2766{
2767 char_u *cmd = NULL;
2768 list_T *list = NULL;
2769 int save_msg_silent = msg_silent;
2770 int save_emsg_silent = emsg_silent;
2771 int save_emsg_noredir = emsg_noredir;
2772 int save_redir_execute = redir_execute;
2773 garray_T save_ga;
2774
2775 rettv->vval.v_string = NULL;
2776 rettv->v_type = VAR_STRING;
2777
2778 if (argvars[0].v_type == VAR_LIST)
2779 {
2780 list = argvars[0].vval.v_list;
2781 if (list == NULL || list->lv_first == NULL)
2782 /* empty list, no commands, empty output */
2783 return;
2784 ++list->lv_refcount;
2785 }
2786 else
2787 {
2788 cmd = get_tv_string_chk(&argvars[0]);
2789 if (cmd == NULL)
2790 return;
2791 }
2792
2793 if (argvars[1].v_type != VAR_UNKNOWN)
2794 {
2795 char_u buf[NUMBUFLEN];
2796 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2797
2798 if (s == NULL)
2799 return;
2800 if (STRNCMP(s, "silent", 6) == 0)
2801 ++msg_silent;
2802 if (STRCMP(s, "silent!") == 0)
2803 {
2804 emsg_silent = TRUE;
2805 emsg_noredir = TRUE;
2806 }
2807 }
2808 else
2809 ++msg_silent;
2810
2811 if (redir_execute)
2812 save_ga = redir_execute_ga;
2813 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2814 redir_execute = TRUE;
2815
2816 if (cmd != NULL)
2817 do_cmdline_cmd(cmd);
2818 else
2819 {
2820 listitem_T *item = list->lv_first;
2821
2822 do_cmdline(NULL, get_list_line, (void *)&item,
2823 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2824 --list->lv_refcount;
2825 }
2826
2827 rettv->vval.v_string = redir_execute_ga.ga_data;
2828 msg_silent = save_msg_silent;
2829 emsg_silent = save_emsg_silent;
2830 emsg_noredir = save_emsg_noredir;
2831
2832 redir_execute = save_redir_execute;
2833 if (redir_execute)
2834 redir_execute_ga = save_ga;
2835
2836 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2837 * line. Put it back in the first column. */
2838 msg_col = 0;
2839}
2840
2841/*
2842 * "exepath()" function
2843 */
2844 static void
2845f_exepath(typval_T *argvars, typval_T *rettv)
2846{
2847 char_u *p = NULL;
2848
2849 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
2850 rettv->v_type = VAR_STRING;
2851 rettv->vval.v_string = p;
2852}
2853
2854/*
2855 * "exists()" function
2856 */
2857 static void
2858f_exists(typval_T *argvars, typval_T *rettv)
2859{
2860 char_u *p;
2861 char_u *name;
2862 int n = FALSE;
2863 int len = 0;
2864
2865 p = get_tv_string(&argvars[0]);
2866 if (*p == '$') /* environment variable */
2867 {
2868 /* first try "normal" environment variables (fast) */
2869 if (mch_getenv(p + 1) != NULL)
2870 n = TRUE;
2871 else
2872 {
2873 /* try expanding things like $VIM and ${HOME} */
2874 p = expand_env_save(p);
2875 if (p != NULL && *p != '$')
2876 n = TRUE;
2877 vim_free(p);
2878 }
2879 }
2880 else if (*p == '&' || *p == '+') /* option */
2881 {
2882 n = (get_option_tv(&p, NULL, TRUE) == OK);
2883 if (*skipwhite(p) != NUL)
2884 n = FALSE; /* trailing garbage */
2885 }
2886 else if (*p == '*') /* internal or user defined function */
2887 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002888 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002889 }
2890 else if (*p == ':')
2891 {
2892 n = cmd_exists(p + 1);
2893 }
2894 else if (*p == '#')
2895 {
2896#ifdef FEAT_AUTOCMD
2897 if (p[1] == '#')
2898 n = autocmd_supported(p + 2);
2899 else
2900 n = au_exists(p + 1);
2901#endif
2902 }
2903 else /* internal variable */
2904 {
2905 char_u *tofree;
2906 typval_T tv;
2907
2908 /* get_name_len() takes care of expanding curly braces */
2909 name = p;
2910 len = get_name_len(&p, &tofree, TRUE, FALSE);
2911 if (len > 0)
2912 {
2913 if (tofree != NULL)
2914 name = tofree;
2915 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
2916 if (n)
2917 {
2918 /* handle d.key, l[idx], f(expr) */
2919 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
2920 if (n)
2921 clear_tv(&tv);
2922 }
2923 }
2924 if (*p != NUL)
2925 n = FALSE;
2926
2927 vim_free(tofree);
2928 }
2929
2930 rettv->vval.v_number = n;
2931}
2932
2933#ifdef FEAT_FLOAT
2934/*
2935 * "exp()" function
2936 */
2937 static void
2938f_exp(typval_T *argvars, typval_T *rettv)
2939{
2940 float_T f = 0.0;
2941
2942 rettv->v_type = VAR_FLOAT;
2943 if (get_float_arg(argvars, &f) == OK)
2944 rettv->vval.v_float = exp(f);
2945 else
2946 rettv->vval.v_float = 0.0;
2947}
2948#endif
2949
2950/*
2951 * "expand()" function
2952 */
2953 static void
2954f_expand(typval_T *argvars, typval_T *rettv)
2955{
2956 char_u *s;
2957 int len;
2958 char_u *errormsg;
2959 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2960 expand_T xpc;
2961 int error = FALSE;
2962 char_u *result;
2963
2964 rettv->v_type = VAR_STRING;
2965 if (argvars[1].v_type != VAR_UNKNOWN
2966 && argvars[2].v_type != VAR_UNKNOWN
2967 && get_tv_number_chk(&argvars[2], &error)
2968 && !error)
2969 {
2970 rettv->v_type = VAR_LIST;
2971 rettv->vval.v_list = NULL;
2972 }
2973
2974 s = get_tv_string(&argvars[0]);
2975 if (*s == '%' || *s == '#' || *s == '<')
2976 {
2977 ++emsg_off;
2978 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2979 --emsg_off;
2980 if (rettv->v_type == VAR_LIST)
2981 {
2982 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2983 list_append_string(rettv->vval.v_list, result, -1);
2984 else
2985 vim_free(result);
2986 }
2987 else
2988 rettv->vval.v_string = result;
2989 }
2990 else
2991 {
2992 /* When the optional second argument is non-zero, don't remove matches
2993 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
2994 if (argvars[1].v_type != VAR_UNKNOWN
2995 && get_tv_number_chk(&argvars[1], &error))
2996 options |= WILD_KEEP_ALL;
2997 if (!error)
2998 {
2999 ExpandInit(&xpc);
3000 xpc.xp_context = EXPAND_FILES;
3001 if (p_wic)
3002 options += WILD_ICASE;
3003 if (rettv->v_type == VAR_STRING)
3004 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3005 options, WILD_ALL);
3006 else if (rettv_list_alloc(rettv) != FAIL)
3007 {
3008 int i;
3009
3010 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3011 for (i = 0; i < xpc.xp_numfiles; i++)
3012 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3013 ExpandCleanup(&xpc);
3014 }
3015 }
3016 else
3017 rettv->vval.v_string = NULL;
3018 }
3019}
3020
3021/*
3022 * "extend(list, list [, idx])" function
3023 * "extend(dict, dict [, action])" function
3024 */
3025 static void
3026f_extend(typval_T *argvars, typval_T *rettv)
3027{
3028 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3029
3030 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3031 {
3032 list_T *l1, *l2;
3033 listitem_T *item;
3034 long before;
3035 int error = FALSE;
3036
3037 l1 = argvars[0].vval.v_list;
3038 l2 = argvars[1].vval.v_list;
3039 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3040 && l2 != NULL)
3041 {
3042 if (argvars[2].v_type != VAR_UNKNOWN)
3043 {
3044 before = (long)get_tv_number_chk(&argvars[2], &error);
3045 if (error)
3046 return; /* type error; errmsg already given */
3047
3048 if (before == l1->lv_len)
3049 item = NULL;
3050 else
3051 {
3052 item = list_find(l1, before);
3053 if (item == NULL)
3054 {
3055 EMSGN(_(e_listidx), before);
3056 return;
3057 }
3058 }
3059 }
3060 else
3061 item = NULL;
3062 list_extend(l1, l2, item);
3063
3064 copy_tv(&argvars[0], rettv);
3065 }
3066 }
3067 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3068 {
3069 dict_T *d1, *d2;
3070 char_u *action;
3071 int i;
3072
3073 d1 = argvars[0].vval.v_dict;
3074 d2 = argvars[1].vval.v_dict;
3075 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3076 && d2 != NULL)
3077 {
3078 /* Check the third argument. */
3079 if (argvars[2].v_type != VAR_UNKNOWN)
3080 {
3081 static char *(av[]) = {"keep", "force", "error"};
3082
3083 action = get_tv_string_chk(&argvars[2]);
3084 if (action == NULL)
3085 return; /* type error; errmsg already given */
3086 for (i = 0; i < 3; ++i)
3087 if (STRCMP(action, av[i]) == 0)
3088 break;
3089 if (i == 3)
3090 {
3091 EMSG2(_(e_invarg2), action);
3092 return;
3093 }
3094 }
3095 else
3096 action = (char_u *)"force";
3097
3098 dict_extend(d1, d2, action);
3099
3100 copy_tv(&argvars[0], rettv);
3101 }
3102 }
3103 else
3104 EMSG2(_(e_listdictarg), "extend()");
3105}
3106
3107/*
3108 * "feedkeys()" function
3109 */
3110 static void
3111f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3112{
3113 int remap = TRUE;
3114 int insert = FALSE;
3115 char_u *keys, *flags;
3116 char_u nbuf[NUMBUFLEN];
3117 int typed = FALSE;
3118 int execute = FALSE;
3119 int dangerous = FALSE;
3120 char_u *keys_esc;
3121
3122 /* This is not allowed in the sandbox. If the commands would still be
3123 * executed in the sandbox it would be OK, but it probably happens later,
3124 * when "sandbox" is no longer set. */
3125 if (check_secure())
3126 return;
3127
3128 keys = get_tv_string(&argvars[0]);
3129
3130 if (argvars[1].v_type != VAR_UNKNOWN)
3131 {
3132 flags = get_tv_string_buf(&argvars[1], nbuf);
3133 for ( ; *flags != NUL; ++flags)
3134 {
3135 switch (*flags)
3136 {
3137 case 'n': remap = FALSE; break;
3138 case 'm': remap = TRUE; break;
3139 case 't': typed = TRUE; break;
3140 case 'i': insert = TRUE; break;
3141 case 'x': execute = TRUE; break;
3142 case '!': dangerous = TRUE; break;
3143 }
3144 }
3145 }
3146
3147 if (*keys != NUL || execute)
3148 {
3149 /* Need to escape K_SPECIAL and CSI before putting the string in the
3150 * typeahead buffer. */
3151 keys_esc = vim_strsave_escape_csi(keys);
3152 if (keys_esc != NULL)
3153 {
3154 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3155 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3156 vim_free(keys_esc);
3157 if (vgetc_busy)
3158 typebuf_was_filled = TRUE;
3159 if (execute)
3160 {
3161 int save_msg_scroll = msg_scroll;
3162
3163 /* Avoid a 1 second delay when the keys start Insert mode. */
3164 msg_scroll = FALSE;
3165
3166 if (!dangerous)
3167 ++ex_normal_busy;
3168 exec_normal(TRUE);
3169 if (!dangerous)
3170 --ex_normal_busy;
3171 msg_scroll |= save_msg_scroll;
3172 }
3173 }
3174 }
3175}
3176
3177/*
3178 * "filereadable()" function
3179 */
3180 static void
3181f_filereadable(typval_T *argvars, typval_T *rettv)
3182{
3183 int fd;
3184 char_u *p;
3185 int n;
3186
3187#ifndef O_NONBLOCK
3188# define O_NONBLOCK 0
3189#endif
3190 p = get_tv_string(&argvars[0]);
3191 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3192 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3193 {
3194 n = TRUE;
3195 close(fd);
3196 }
3197 else
3198 n = FALSE;
3199
3200 rettv->vval.v_number = n;
3201}
3202
3203/*
3204 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3205 * rights to write into.
3206 */
3207 static void
3208f_filewritable(typval_T *argvars, typval_T *rettv)
3209{
3210 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3211}
3212
3213 static void
3214findfilendir(
3215 typval_T *argvars UNUSED,
3216 typval_T *rettv,
3217 int find_what UNUSED)
3218{
3219#ifdef FEAT_SEARCHPATH
3220 char_u *fname;
3221 char_u *fresult = NULL;
3222 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3223 char_u *p;
3224 char_u pathbuf[NUMBUFLEN];
3225 int count = 1;
3226 int first = TRUE;
3227 int error = FALSE;
3228#endif
3229
3230 rettv->vval.v_string = NULL;
3231 rettv->v_type = VAR_STRING;
3232
3233#ifdef FEAT_SEARCHPATH
3234 fname = get_tv_string(&argvars[0]);
3235
3236 if (argvars[1].v_type != VAR_UNKNOWN)
3237 {
3238 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3239 if (p == NULL)
3240 error = TRUE;
3241 else
3242 {
3243 if (*p != NUL)
3244 path = p;
3245
3246 if (argvars[2].v_type != VAR_UNKNOWN)
3247 count = (int)get_tv_number_chk(&argvars[2], &error);
3248 }
3249 }
3250
3251 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3252 error = TRUE;
3253
3254 if (*fname != NUL && !error)
3255 {
3256 do
3257 {
3258 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3259 vim_free(fresult);
3260 fresult = find_file_in_path_option(first ? fname : NULL,
3261 first ? (int)STRLEN(fname) : 0,
3262 0, first, path,
3263 find_what,
3264 curbuf->b_ffname,
3265 find_what == FINDFILE_DIR
3266 ? (char_u *)"" : curbuf->b_p_sua);
3267 first = FALSE;
3268
3269 if (fresult != NULL && rettv->v_type == VAR_LIST)
3270 list_append_string(rettv->vval.v_list, fresult, -1);
3271
3272 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3273 }
3274
3275 if (rettv->v_type == VAR_STRING)
3276 rettv->vval.v_string = fresult;
3277#endif
3278}
3279
3280/*
3281 * "filter()" function
3282 */
3283 static void
3284f_filter(typval_T *argvars, typval_T *rettv)
3285{
3286 filter_map(argvars, rettv, FALSE);
3287}
3288
3289/*
3290 * "finddir({fname}[, {path}[, {count}]])" function
3291 */
3292 static void
3293f_finddir(typval_T *argvars, typval_T *rettv)
3294{
3295 findfilendir(argvars, rettv, FINDFILE_DIR);
3296}
3297
3298/*
3299 * "findfile({fname}[, {path}[, {count}]])" function
3300 */
3301 static void
3302f_findfile(typval_T *argvars, typval_T *rettv)
3303{
3304 findfilendir(argvars, rettv, FINDFILE_FILE);
3305}
3306
3307#ifdef FEAT_FLOAT
3308/*
3309 * "float2nr({float})" function
3310 */
3311 static void
3312f_float2nr(typval_T *argvars, typval_T *rettv)
3313{
3314 float_T f = 0.0;
3315
3316 if (get_float_arg(argvars, &f) == OK)
3317 {
3318# ifdef FEAT_NUM64
Bram Moolenaaraf9c4c92016-08-17 21:51:56 +02003319 if (f < -0x7fffffffffffffffLL)
3320 rettv->vval.v_number = -0x7fffffffffffffffLL;
3321 else if (f > 0x7fffffffffffffffLL)
3322 rettv->vval.v_number = 0x7fffffffffffffffLL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003323 else
3324 rettv->vval.v_number = (varnumber_T)f;
3325# else
3326 if (f < -0x7fffffff)
3327 rettv->vval.v_number = -0x7fffffff;
3328 else if (f > 0x7fffffff)
3329 rettv->vval.v_number = 0x7fffffff;
3330 else
3331 rettv->vval.v_number = (varnumber_T)f;
3332# endif
3333 }
3334}
3335
3336/*
3337 * "floor({float})" function
3338 */
3339 static void
3340f_floor(typval_T *argvars, typval_T *rettv)
3341{
3342 float_T f = 0.0;
3343
3344 rettv->v_type = VAR_FLOAT;
3345 if (get_float_arg(argvars, &f) == OK)
3346 rettv->vval.v_float = floor(f);
3347 else
3348 rettv->vval.v_float = 0.0;
3349}
3350
3351/*
3352 * "fmod()" function
3353 */
3354 static void
3355f_fmod(typval_T *argvars, typval_T *rettv)
3356{
3357 float_T fx = 0.0, fy = 0.0;
3358
3359 rettv->v_type = VAR_FLOAT;
3360 if (get_float_arg(argvars, &fx) == OK
3361 && get_float_arg(&argvars[1], &fy) == OK)
3362 rettv->vval.v_float = fmod(fx, fy);
3363 else
3364 rettv->vval.v_float = 0.0;
3365}
3366#endif
3367
3368/*
3369 * "fnameescape({string})" function
3370 */
3371 static void
3372f_fnameescape(typval_T *argvars, typval_T *rettv)
3373{
3374 rettv->vval.v_string = vim_strsave_fnameescape(
3375 get_tv_string(&argvars[0]), FALSE);
3376 rettv->v_type = VAR_STRING;
3377}
3378
3379/*
3380 * "fnamemodify({fname}, {mods})" function
3381 */
3382 static void
3383f_fnamemodify(typval_T *argvars, typval_T *rettv)
3384{
3385 char_u *fname;
3386 char_u *mods;
3387 int usedlen = 0;
3388 int len;
3389 char_u *fbuf = NULL;
3390 char_u buf[NUMBUFLEN];
3391
3392 fname = get_tv_string_chk(&argvars[0]);
3393 mods = get_tv_string_buf_chk(&argvars[1], buf);
3394 if (fname == NULL || mods == NULL)
3395 fname = NULL;
3396 else
3397 {
3398 len = (int)STRLEN(fname);
3399 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3400 }
3401
3402 rettv->v_type = VAR_STRING;
3403 if (fname == NULL)
3404 rettv->vval.v_string = NULL;
3405 else
3406 rettv->vval.v_string = vim_strnsave(fname, len);
3407 vim_free(fbuf);
3408}
3409
3410static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3411
3412/*
3413 * "foldclosed()" function
3414 */
3415 static void
3416foldclosed_both(
3417 typval_T *argvars UNUSED,
3418 typval_T *rettv,
3419 int end UNUSED)
3420{
3421#ifdef FEAT_FOLDING
3422 linenr_T lnum;
3423 linenr_T first, last;
3424
3425 lnum = get_tv_lnum(argvars);
3426 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3427 {
3428 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3429 {
3430 if (end)
3431 rettv->vval.v_number = (varnumber_T)last;
3432 else
3433 rettv->vval.v_number = (varnumber_T)first;
3434 return;
3435 }
3436 }
3437#endif
3438 rettv->vval.v_number = -1;
3439}
3440
3441/*
3442 * "foldclosed()" function
3443 */
3444 static void
3445f_foldclosed(typval_T *argvars, typval_T *rettv)
3446{
3447 foldclosed_both(argvars, rettv, FALSE);
3448}
3449
3450/*
3451 * "foldclosedend()" function
3452 */
3453 static void
3454f_foldclosedend(typval_T *argvars, typval_T *rettv)
3455{
3456 foldclosed_both(argvars, rettv, TRUE);
3457}
3458
3459/*
3460 * "foldlevel()" function
3461 */
3462 static void
3463f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3464{
3465#ifdef FEAT_FOLDING
3466 linenr_T lnum;
3467
3468 lnum = get_tv_lnum(argvars);
3469 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3470 rettv->vval.v_number = foldLevel(lnum);
3471#endif
3472}
3473
3474/*
3475 * "foldtext()" function
3476 */
3477 static void
3478f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3479{
3480#ifdef FEAT_FOLDING
3481 linenr_T foldstart;
3482 linenr_T foldend;
3483 char_u *dashes;
3484 linenr_T lnum;
3485 char_u *s;
3486 char_u *r;
3487 int len;
3488 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003489 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003490#endif
3491
3492 rettv->v_type = VAR_STRING;
3493 rettv->vval.v_string = NULL;
3494#ifdef FEAT_FOLDING
3495 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3496 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3497 dashes = get_vim_var_str(VV_FOLDDASHES);
3498 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3499 && dashes != NULL)
3500 {
3501 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003502 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003503 if (!linewhite(lnum))
3504 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003505
3506 /* Find interesting text in this line. */
3507 s = skipwhite(ml_get(lnum));
3508 /* skip C comment-start */
3509 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3510 {
3511 s = skipwhite(s + 2);
3512 if (*skipwhite(s) == NUL
3513 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3514 {
3515 s = skipwhite(ml_get(lnum + 1));
3516 if (*s == '*')
3517 s = skipwhite(s + 1);
3518 }
3519 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003520 count = (long)(foldend - foldstart + 1);
3521 txt = ngettext("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003522 r = alloc((unsigned)(STRLEN(txt)
3523 + STRLEN(dashes) /* for %s */
3524 + 20 /* for %3ld */
3525 + STRLEN(s))); /* concatenated */
3526 if (r != NULL)
3527 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003528 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003529 len = (int)STRLEN(r);
3530 STRCAT(r, s);
3531 /* remove 'foldmarker' and 'commentstring' */
3532 foldtext_cleanup(r + len);
3533 rettv->vval.v_string = r;
3534 }
3535 }
3536#endif
3537}
3538
3539/*
3540 * "foldtextresult(lnum)" function
3541 */
3542 static void
3543f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3544{
3545#ifdef FEAT_FOLDING
3546 linenr_T lnum;
3547 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003548 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003549 foldinfo_T foldinfo;
3550 int fold_count;
3551#endif
3552
3553 rettv->v_type = VAR_STRING;
3554 rettv->vval.v_string = NULL;
3555#ifdef FEAT_FOLDING
3556 lnum = get_tv_lnum(argvars);
3557 /* treat illegal types and illegal string values for {lnum} the same */
3558 if (lnum < 0)
3559 lnum = 0;
3560 fold_count = foldedCount(curwin, lnum, &foldinfo);
3561 if (fold_count > 0)
3562 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003563 text = get_foldtext(curwin, lnum, lnum + fold_count - 1, &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003564 if (text == buf)
3565 text = vim_strsave(text);
3566 rettv->vval.v_string = text;
3567 }
3568#endif
3569}
3570
3571/*
3572 * "foreground()" function
3573 */
3574 static void
3575f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3576{
3577#ifdef FEAT_GUI
3578 if (gui.in_use)
3579 gui_mch_set_foreground();
3580#else
3581# ifdef WIN32
3582 win32_set_foreground();
3583# endif
3584#endif
3585}
3586
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003587 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003588common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003589{
3590 char_u *s;
3591 char_u *name;
3592 int use_string = FALSE;
3593 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003594 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003595
3596 if (argvars[0].v_type == VAR_FUNC)
3597 {
3598 /* function(MyFunc, [arg], dict) */
3599 s = argvars[0].vval.v_string;
3600 }
3601 else if (argvars[0].v_type == VAR_PARTIAL
3602 && argvars[0].vval.v_partial != NULL)
3603 {
3604 /* function(dict.MyFunc, [arg]) */
3605 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003606 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003607 }
3608 else
3609 {
3610 /* function('MyFunc', [arg], dict) */
3611 s = get_tv_string(&argvars[0]);
3612 use_string = TRUE;
3613 }
3614
Bram Moolenaar843b8842016-08-21 14:36:15 +02003615 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003616 {
3617 name = s;
3618 trans_name = trans_function_name(&name, FALSE,
3619 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3620 if (*name != NUL)
3621 s = NULL;
3622 }
3623
Bram Moolenaar843b8842016-08-21 14:36:15 +02003624 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3625 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003626 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003627 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003628 else if (trans_name != NULL && (is_funcref
3629 ? find_func(trans_name) == NULL
3630 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003631 EMSG2(_("E700: Unknown function: %s"), s);
3632 else
3633 {
3634 int dict_idx = 0;
3635 int arg_idx = 0;
3636 list_T *list = NULL;
3637
3638 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3639 {
3640 char sid_buf[25];
3641 int off = *s == 's' ? 2 : 5;
3642
3643 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3644 * also be called from another script. Using trans_function_name()
3645 * would also work, but some plugins depend on the name being
3646 * printable text. */
3647 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3648 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3649 if (name != NULL)
3650 {
3651 STRCPY(name, sid_buf);
3652 STRCAT(name, s + off);
3653 }
3654 }
3655 else
3656 name = vim_strsave(s);
3657
3658 if (argvars[1].v_type != VAR_UNKNOWN)
3659 {
3660 if (argvars[2].v_type != VAR_UNKNOWN)
3661 {
3662 /* function(name, [args], dict) */
3663 arg_idx = 1;
3664 dict_idx = 2;
3665 }
3666 else if (argvars[1].v_type == VAR_DICT)
3667 /* function(name, dict) */
3668 dict_idx = 1;
3669 else
3670 /* function(name, [args]) */
3671 arg_idx = 1;
3672 if (dict_idx > 0)
3673 {
3674 if (argvars[dict_idx].v_type != VAR_DICT)
3675 {
3676 EMSG(_("E922: expected a dict"));
3677 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003678 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003679 }
3680 if (argvars[dict_idx].vval.v_dict == NULL)
3681 dict_idx = 0;
3682 }
3683 if (arg_idx > 0)
3684 {
3685 if (argvars[arg_idx].v_type != VAR_LIST)
3686 {
3687 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3688 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003689 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003690 }
3691 list = argvars[arg_idx].vval.v_list;
3692 if (list == NULL || list->lv_len == 0)
3693 arg_idx = 0;
3694 }
3695 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003696 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697 {
3698 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3699
3700 /* result is a VAR_PARTIAL */
3701 if (pt == NULL)
3702 vim_free(name);
3703 else
3704 {
3705 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3706 {
3707 listitem_T *li;
3708 int i = 0;
3709 int arg_len = 0;
3710 int lv_len = 0;
3711
3712 if (arg_pt != NULL)
3713 arg_len = arg_pt->pt_argc;
3714 if (list != NULL)
3715 lv_len = list->lv_len;
3716 pt->pt_argc = arg_len + lv_len;
3717 pt->pt_argv = (typval_T *)alloc(
3718 sizeof(typval_T) * pt->pt_argc);
3719 if (pt->pt_argv == NULL)
3720 {
3721 vim_free(pt);
3722 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003723 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003724 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003725 for (i = 0; i < arg_len; i++)
3726 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3727 if (lv_len > 0)
3728 for (li = list->lv_first; li != NULL;
3729 li = li->li_next)
3730 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003731 }
3732
3733 /* For "function(dict.func, [], dict)" and "func" is a partial
3734 * use "dict". That is backwards compatible. */
3735 if (dict_idx > 0)
3736 {
3737 /* The dict is bound explicitly, pt_auto is FALSE. */
3738 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3739 ++pt->pt_dict->dv_refcount;
3740 }
3741 else if (arg_pt != NULL)
3742 {
3743 /* If the dict was bound automatically the result is also
3744 * bound automatically. */
3745 pt->pt_dict = arg_pt->pt_dict;
3746 pt->pt_auto = arg_pt->pt_auto;
3747 if (pt->pt_dict != NULL)
3748 ++pt->pt_dict->dv_refcount;
3749 }
3750
3751 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003752 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3753 {
3754 pt->pt_func = arg_pt->pt_func;
3755 func_ptr_ref(pt->pt_func);
3756 vim_free(name);
3757 }
3758 else if (is_funcref)
3759 {
3760 pt->pt_func = find_func(trans_name);
3761 func_ptr_ref(pt->pt_func);
3762 vim_free(name);
3763 }
3764 else
3765 {
3766 pt->pt_name = name;
3767 func_ref(name);
3768 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003769 }
3770 rettv->v_type = VAR_PARTIAL;
3771 rettv->vval.v_partial = pt;
3772 }
3773 else
3774 {
3775 /* result is a VAR_FUNC */
3776 rettv->v_type = VAR_FUNC;
3777 rettv->vval.v_string = name;
3778 func_ref(name);
3779 }
3780 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003781theend:
3782 vim_free(trans_name);
3783}
3784
3785/*
3786 * "funcref()" function
3787 */
3788 static void
3789f_funcref(typval_T *argvars, typval_T *rettv)
3790{
3791 common_function(argvars, rettv, TRUE);
3792}
3793
3794/*
3795 * "function()" function
3796 */
3797 static void
3798f_function(typval_T *argvars, typval_T *rettv)
3799{
3800 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003801}
3802
3803/*
3804 * "garbagecollect()" function
3805 */
3806 static void
3807f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3808{
3809 /* This is postponed until we are back at the toplevel, because we may be
3810 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3811 want_garbage_collect = TRUE;
3812
3813 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3814 garbage_collect_at_exit = TRUE;
3815}
3816
3817/*
3818 * "get()" function
3819 */
3820 static void
3821f_get(typval_T *argvars, typval_T *rettv)
3822{
3823 listitem_T *li;
3824 list_T *l;
3825 dictitem_T *di;
3826 dict_T *d;
3827 typval_T *tv = NULL;
3828
3829 if (argvars[0].v_type == VAR_LIST)
3830 {
3831 if ((l = argvars[0].vval.v_list) != NULL)
3832 {
3833 int error = FALSE;
3834
3835 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3836 if (!error && li != NULL)
3837 tv = &li->li_tv;
3838 }
3839 }
3840 else if (argvars[0].v_type == VAR_DICT)
3841 {
3842 if ((d = argvars[0].vval.v_dict) != NULL)
3843 {
3844 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3845 if (di != NULL)
3846 tv = &di->di_tv;
3847 }
3848 }
3849 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3850 {
3851 partial_T *pt;
3852 partial_T fref_pt;
3853
3854 if (argvars[0].v_type == VAR_PARTIAL)
3855 pt = argvars[0].vval.v_partial;
3856 else
3857 {
3858 vim_memset(&fref_pt, 0, sizeof(fref_pt));
3859 fref_pt.pt_name = argvars[0].vval.v_string;
3860 pt = &fref_pt;
3861 }
3862
3863 if (pt != NULL)
3864 {
3865 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003866 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003867
3868 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3869 {
3870 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003871 n = partial_name(pt);
3872 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003873 rettv->vval.v_string = NULL;
3874 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003875 {
3876 rettv->vval.v_string = vim_strsave(n);
3877 if (rettv->v_type == VAR_FUNC)
3878 func_ref(rettv->vval.v_string);
3879 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003880 }
3881 else if (STRCMP(what, "dict") == 0)
3882 {
3883 rettv->v_type = VAR_DICT;
3884 rettv->vval.v_dict = pt->pt_dict;
3885 if (pt->pt_dict != NULL)
3886 ++pt->pt_dict->dv_refcount;
3887 }
3888 else if (STRCMP(what, "args") == 0)
3889 {
3890 rettv->v_type = VAR_LIST;
3891 if (rettv_list_alloc(rettv) == OK)
3892 {
3893 int i;
3894
3895 for (i = 0; i < pt->pt_argc; ++i)
3896 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3897 }
3898 }
3899 else
3900 EMSG2(_(e_invarg2), what);
3901 return;
3902 }
3903 }
3904 else
3905 EMSG2(_(e_listdictarg), "get()");
3906
3907 if (tv == NULL)
3908 {
3909 if (argvars[2].v_type != VAR_UNKNOWN)
3910 copy_tv(&argvars[2], rettv);
3911 }
3912 else
3913 copy_tv(tv, rettv);
3914}
3915
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003916#ifdef FEAT_SIGNS
3917/*
3918 * Returns information about signs placed in a buffer as list of dicts.
3919 */
3920 static void
3921get_buffer_signs(buf_T *buf, list_T *l)
3922{
3923 signlist_T *sign;
3924
3925 for (sign = buf->b_signlist; sign; sign = sign->next)
3926 {
3927 dict_T *d = dict_alloc();
3928
3929 if (d != NULL)
3930 {
3931 dict_add_nr_str(d, "id", sign->id, NULL);
3932 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02003933 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003934
3935 list_append_dict(l, d);
3936 }
3937 }
3938}
3939#endif
3940
3941/*
3942 * Returns buffer options, variables and other attributes in a dictionary.
3943 */
3944 static dict_T *
3945get_buffer_info(buf_T *buf)
3946{
3947 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003948 tabpage_T *tp;
3949 win_T *wp;
3950 list_T *windows;
3951
3952 dict = dict_alloc();
3953 if (dict == NULL)
3954 return NULL;
3955
Bram Moolenaar33928832016-08-18 21:22:04 +02003956 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003957 dict_add_nr_str(dict, "name", 0L,
3958 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
3959 dict_add_nr_str(dict, "lnum", buflist_findlnum(buf), NULL);
3960 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
3961 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
3962 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
3963 dict_add_nr_str(dict, "changedtick", buf->b_changedtick, NULL);
3964 dict_add_nr_str(dict, "hidden",
3965 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
3966 NULL);
3967
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02003968 /* Get a reference to buffer variables */
3969 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003970
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003971 /* List of windows displaying this buffer */
3972 windows = list_alloc();
3973 if (windows != NULL)
3974 {
3975 FOR_ALL_TAB_WINDOWS(tp, wp)
3976 if (wp->w_buffer == buf)
3977 list_append_number(windows, (varnumber_T)wp->w_id);
3978 dict_add_list(dict, "windows", windows);
3979 }
3980
3981#ifdef FEAT_SIGNS
3982 if (buf->b_signlist != NULL)
3983 {
3984 /* List of signs placed in this buffer */
3985 list_T *signs = list_alloc();
3986 if (signs != NULL)
3987 {
3988 get_buffer_signs(buf, signs);
3989 dict_add_list(dict, "signs", signs);
3990 }
3991 }
3992#endif
3993
3994 return dict;
3995}
3996
3997/*
3998 * "getbufinfo()" function
3999 */
4000 static void
4001f_getbufinfo(typval_T *argvars, typval_T *rettv)
4002{
4003 buf_T *buf = NULL;
4004 buf_T *argbuf = NULL;
4005 dict_T *d;
4006 int filtered = FALSE;
4007 int sel_buflisted = FALSE;
4008 int sel_bufloaded = FALSE;
4009
4010 if (rettv_list_alloc(rettv) != OK)
4011 return;
4012
4013 /* List of all the buffers or selected buffers */
4014 if (argvars[0].v_type == VAR_DICT)
4015 {
4016 dict_T *sel_d = argvars[0].vval.v_dict;
4017
4018 if (sel_d != NULL)
4019 {
4020 dictitem_T *di;
4021
4022 filtered = TRUE;
4023
4024 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4025 if (di != NULL && get_tv_number(&di->di_tv))
4026 sel_buflisted = TRUE;
4027
4028 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4029 if (di != NULL && get_tv_number(&di->di_tv))
4030 sel_bufloaded = TRUE;
4031 }
4032 }
4033 else if (argvars[0].v_type != VAR_UNKNOWN)
4034 {
4035 /* Information about one buffer. Argument specifies the buffer */
4036 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4037 ++emsg_off;
4038 argbuf = get_buf_tv(&argvars[0], FALSE);
4039 --emsg_off;
4040 if (argbuf == NULL)
4041 return;
4042 }
4043
4044 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004045 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004046 {
4047 if (argbuf != NULL && argbuf != buf)
4048 continue;
4049 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
4050 || (sel_buflisted && !buf->b_p_bl)))
4051 continue;
4052
4053 d = get_buffer_info(buf);
4054 if (d != NULL)
4055 list_append_dict(rettv->vval.v_list, d);
4056 if (argbuf != NULL)
4057 return;
4058 }
4059}
4060
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004061static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4062
4063/*
4064 * Get line or list of lines from buffer "buf" into "rettv".
4065 * Return a range (from start to end) of lines in rettv from the specified
4066 * buffer.
4067 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4068 */
4069 static void
4070get_buffer_lines(
4071 buf_T *buf,
4072 linenr_T start,
4073 linenr_T end,
4074 int retlist,
4075 typval_T *rettv)
4076{
4077 char_u *p;
4078
4079 rettv->v_type = VAR_STRING;
4080 rettv->vval.v_string = NULL;
4081 if (retlist && rettv_list_alloc(rettv) == FAIL)
4082 return;
4083
4084 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4085 return;
4086
4087 if (!retlist)
4088 {
4089 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4090 p = ml_get_buf(buf, start, FALSE);
4091 else
4092 p = (char_u *)"";
4093 rettv->vval.v_string = vim_strsave(p);
4094 }
4095 else
4096 {
4097 if (end < start)
4098 return;
4099
4100 if (start < 1)
4101 start = 1;
4102 if (end > buf->b_ml.ml_line_count)
4103 end = buf->b_ml.ml_line_count;
4104 while (start <= end)
4105 if (list_append_string(rettv->vval.v_list,
4106 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4107 break;
4108 }
4109}
4110
4111/*
4112 * Get the lnum from the first argument.
4113 * Also accepts "$", then "buf" is used.
4114 * Returns 0 on error.
4115 */
4116 static linenr_T
4117get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4118{
4119 if (argvars[0].v_type == VAR_STRING
4120 && argvars[0].vval.v_string != NULL
4121 && argvars[0].vval.v_string[0] == '$'
4122 && buf != NULL)
4123 return buf->b_ml.ml_line_count;
4124 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4125}
4126
4127/*
4128 * "getbufline()" function
4129 */
4130 static void
4131f_getbufline(typval_T *argvars, typval_T *rettv)
4132{
4133 linenr_T lnum;
4134 linenr_T end;
4135 buf_T *buf;
4136
4137 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4138 ++emsg_off;
4139 buf = get_buf_tv(&argvars[0], FALSE);
4140 --emsg_off;
4141
4142 lnum = get_tv_lnum_buf(&argvars[1], buf);
4143 if (argvars[2].v_type == VAR_UNKNOWN)
4144 end = lnum;
4145 else
4146 end = get_tv_lnum_buf(&argvars[2], buf);
4147
4148 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4149}
4150
4151/*
4152 * "getbufvar()" function
4153 */
4154 static void
4155f_getbufvar(typval_T *argvars, typval_T *rettv)
4156{
4157 buf_T *buf;
4158 buf_T *save_curbuf;
4159 char_u *varname;
4160 dictitem_T *v;
4161 int done = FALSE;
4162
4163 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4164 varname = get_tv_string_chk(&argvars[1]);
4165 ++emsg_off;
4166 buf = get_buf_tv(&argvars[0], FALSE);
4167
4168 rettv->v_type = VAR_STRING;
4169 rettv->vval.v_string = NULL;
4170
4171 if (buf != NULL && varname != NULL)
4172 {
4173 /* set curbuf to be our buf, temporarily */
4174 save_curbuf = curbuf;
4175 curbuf = buf;
4176
Bram Moolenaar30567352016-08-27 21:25:44 +02004177 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004178 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004179 if (varname[1] == NUL)
4180 {
4181 /* get all buffer-local options in a dict */
4182 dict_T *opts = get_winbuf_options(TRUE);
4183
4184 if (opts != NULL)
4185 {
4186 rettv->v_type = VAR_DICT;
4187 rettv->vval.v_dict = opts;
4188 ++opts->dv_refcount;
4189 done = TRUE;
4190 }
4191 }
4192 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4193 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004194 done = TRUE;
4195 }
4196 else if (STRCMP(varname, "changedtick") == 0)
4197 {
4198 rettv->v_type = VAR_NUMBER;
4199 rettv->vval.v_number = curbuf->b_changedtick;
4200 done = TRUE;
4201 }
4202 else
4203 {
4204 /* Look up the variable. */
4205 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4206 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4207 'b', varname, FALSE);
4208 if (v != NULL)
4209 {
4210 copy_tv(&v->di_tv, rettv);
4211 done = TRUE;
4212 }
4213 }
4214
4215 /* restore previous notion of curbuf */
4216 curbuf = save_curbuf;
4217 }
4218
4219 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4220 /* use the default value */
4221 copy_tv(&argvars[2], rettv);
4222
4223 --emsg_off;
4224}
4225
4226/*
4227 * "getchar()" function
4228 */
4229 static void
4230f_getchar(typval_T *argvars, typval_T *rettv)
4231{
4232 varnumber_T n;
4233 int error = FALSE;
4234
4235 /* Position the cursor. Needed after a message that ends in a space. */
4236 windgoto(msg_row, msg_col);
4237
4238 ++no_mapping;
4239 ++allow_keys;
4240 for (;;)
4241 {
4242 if (argvars[0].v_type == VAR_UNKNOWN)
4243 /* getchar(): blocking wait. */
4244 n = safe_vgetc();
4245 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4246 /* getchar(1): only check if char avail */
4247 n = vpeekc_any();
4248 else if (error || vpeekc_any() == NUL)
4249 /* illegal argument or getchar(0) and no char avail: return zero */
4250 n = 0;
4251 else
4252 /* getchar(0) and char avail: return char */
4253 n = safe_vgetc();
4254
4255 if (n == K_IGNORE)
4256 continue;
4257 break;
4258 }
4259 --no_mapping;
4260 --allow_keys;
4261
4262 set_vim_var_nr(VV_MOUSE_WIN, 0);
4263 set_vim_var_nr(VV_MOUSE_WINID, 0);
4264 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4265 set_vim_var_nr(VV_MOUSE_COL, 0);
4266
4267 rettv->vval.v_number = n;
4268 if (IS_SPECIAL(n) || mod_mask != 0)
4269 {
4270 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4271 int i = 0;
4272
4273 /* Turn a special key into three bytes, plus modifier. */
4274 if (mod_mask != 0)
4275 {
4276 temp[i++] = K_SPECIAL;
4277 temp[i++] = KS_MODIFIER;
4278 temp[i++] = mod_mask;
4279 }
4280 if (IS_SPECIAL(n))
4281 {
4282 temp[i++] = K_SPECIAL;
4283 temp[i++] = K_SECOND(n);
4284 temp[i++] = K_THIRD(n);
4285 }
4286#ifdef FEAT_MBYTE
4287 else if (has_mbyte)
4288 i += (*mb_char2bytes)(n, temp + i);
4289#endif
4290 else
4291 temp[i++] = n;
4292 temp[i++] = NUL;
4293 rettv->v_type = VAR_STRING;
4294 rettv->vval.v_string = vim_strsave(temp);
4295
4296#ifdef FEAT_MOUSE
4297 if (is_mouse_key(n))
4298 {
4299 int row = mouse_row;
4300 int col = mouse_col;
4301 win_T *win;
4302 linenr_T lnum;
4303# ifdef FEAT_WINDOWS
4304 win_T *wp;
4305# endif
4306 int winnr = 1;
4307
4308 if (row >= 0 && col >= 0)
4309 {
4310 /* Find the window at the mouse coordinates and compute the
4311 * text position. */
4312 win = mouse_find_win(&row, &col);
4313 (void)mouse_comp_pos(win, &row, &col, &lnum);
4314# ifdef FEAT_WINDOWS
4315 for (wp = firstwin; wp != win; wp = wp->w_next)
4316 ++winnr;
4317# endif
4318 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4319 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4320 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4321 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4322 }
4323 }
4324#endif
4325 }
4326}
4327
4328/*
4329 * "getcharmod()" function
4330 */
4331 static void
4332f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4333{
4334 rettv->vval.v_number = mod_mask;
4335}
4336
4337/*
4338 * "getcharsearch()" function
4339 */
4340 static void
4341f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4342{
4343 if (rettv_dict_alloc(rettv) != FAIL)
4344 {
4345 dict_T *dict = rettv->vval.v_dict;
4346
4347 dict_add_nr_str(dict, "char", 0L, last_csearch());
4348 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4349 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4350 }
4351}
4352
4353/*
4354 * "getcmdline()" function
4355 */
4356 static void
4357f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4358{
4359 rettv->v_type = VAR_STRING;
4360 rettv->vval.v_string = get_cmdline_str();
4361}
4362
4363/*
4364 * "getcmdpos()" function
4365 */
4366 static void
4367f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4368{
4369 rettv->vval.v_number = get_cmdline_pos() + 1;
4370}
4371
4372/*
4373 * "getcmdtype()" function
4374 */
4375 static void
4376f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4377{
4378 rettv->v_type = VAR_STRING;
4379 rettv->vval.v_string = alloc(2);
4380 if (rettv->vval.v_string != NULL)
4381 {
4382 rettv->vval.v_string[0] = get_cmdline_type();
4383 rettv->vval.v_string[1] = NUL;
4384 }
4385}
4386
4387/*
4388 * "getcmdwintype()" function
4389 */
4390 static void
4391f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4392{
4393 rettv->v_type = VAR_STRING;
4394 rettv->vval.v_string = NULL;
4395#ifdef FEAT_CMDWIN
4396 rettv->vval.v_string = alloc(2);
4397 if (rettv->vval.v_string != NULL)
4398 {
4399 rettv->vval.v_string[0] = cmdwin_type;
4400 rettv->vval.v_string[1] = NUL;
4401 }
4402#endif
4403}
4404
4405#if defined(FEAT_CMDL_COMPL)
4406/*
4407 * "getcompletion()" function
4408 */
4409 static void
4410f_getcompletion(typval_T *argvars, typval_T *rettv)
4411{
4412 char_u *pat;
4413 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004414 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004415 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4416 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004417
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004418 if (argvars[2].v_type != VAR_UNKNOWN)
4419 filtered = get_tv_number_chk(&argvars[2], NULL);
4420
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004421 if (p_wic)
4422 options |= WILD_ICASE;
4423
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004424 /* For filtered results, 'wildignore' is used */
4425 if (!filtered)
4426 options |= WILD_KEEP_ALL;
4427
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004428 ExpandInit(&xpc);
4429 xpc.xp_pattern = get_tv_string(&argvars[0]);
4430 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4431 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4432 if (xpc.xp_context == EXPAND_NOTHING)
4433 {
4434 if (argvars[1].v_type == VAR_STRING)
4435 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4436 else
4437 EMSG(_(e_invarg));
4438 return;
4439 }
4440
4441# if defined(FEAT_MENU)
4442 if (xpc.xp_context == EXPAND_MENUS)
4443 {
4444 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4445 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4446 }
4447# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004448#ifdef FEAT_CSCOPE
4449 if (xpc.xp_context == EXPAND_CSCOPE)
4450 {
4451 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4452 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4453 }
4454#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004455#ifdef FEAT_SIGNS
4456 if (xpc.xp_context == EXPAND_SIGN)
4457 {
4458 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4459 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4460 }
4461#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004462
4463 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4464 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4465 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004466 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004467
4468 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4469
4470 for (i = 0; i < xpc.xp_numfiles; i++)
4471 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4472 }
4473 vim_free(pat);
4474 ExpandCleanup(&xpc);
4475}
4476#endif
4477
4478/*
4479 * "getcwd()" function
4480 */
4481 static void
4482f_getcwd(typval_T *argvars, typval_T *rettv)
4483{
4484 win_T *wp = NULL;
4485 char_u *cwd;
4486
4487 rettv->v_type = VAR_STRING;
4488 rettv->vval.v_string = NULL;
4489
4490 wp = find_tabwin(&argvars[0], &argvars[1]);
4491 if (wp != NULL)
4492 {
4493 if (wp->w_localdir != NULL)
4494 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4495 else if (globaldir != NULL)
4496 rettv->vval.v_string = vim_strsave(globaldir);
4497 else
4498 {
4499 cwd = alloc(MAXPATHL);
4500 if (cwd != NULL)
4501 {
4502 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4503 rettv->vval.v_string = vim_strsave(cwd);
4504 vim_free(cwd);
4505 }
4506 }
4507#ifdef BACKSLASH_IN_FILENAME
4508 if (rettv->vval.v_string != NULL)
4509 slash_adjust(rettv->vval.v_string);
4510#endif
4511 }
4512}
4513
4514/*
4515 * "getfontname()" function
4516 */
4517 static void
4518f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4519{
4520 rettv->v_type = VAR_STRING;
4521 rettv->vval.v_string = NULL;
4522#ifdef FEAT_GUI
4523 if (gui.in_use)
4524 {
4525 GuiFont font;
4526 char_u *name = NULL;
4527
4528 if (argvars[0].v_type == VAR_UNKNOWN)
4529 {
4530 /* Get the "Normal" font. Either the name saved by
4531 * hl_set_font_name() or from the font ID. */
4532 font = gui.norm_font;
4533 name = hl_get_font_name();
4534 }
4535 else
4536 {
4537 name = get_tv_string(&argvars[0]);
4538 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4539 return;
4540 font = gui_mch_get_font(name, FALSE);
4541 if (font == NOFONT)
4542 return; /* Invalid font name, return empty string. */
4543 }
4544 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4545 if (argvars[0].v_type != VAR_UNKNOWN)
4546 gui_mch_free_font(font);
4547 }
4548#endif
4549}
4550
4551/*
4552 * "getfperm({fname})" function
4553 */
4554 static void
4555f_getfperm(typval_T *argvars, typval_T *rettv)
4556{
4557 char_u *fname;
4558 stat_T st;
4559 char_u *perm = NULL;
4560 char_u flags[] = "rwx";
4561 int i;
4562
4563 fname = get_tv_string(&argvars[0]);
4564
4565 rettv->v_type = VAR_STRING;
4566 if (mch_stat((char *)fname, &st) >= 0)
4567 {
4568 perm = vim_strsave((char_u *)"---------");
4569 if (perm != NULL)
4570 {
4571 for (i = 0; i < 9; i++)
4572 {
4573 if (st.st_mode & (1 << (8 - i)))
4574 perm[i] = flags[i % 3];
4575 }
4576 }
4577 }
4578 rettv->vval.v_string = perm;
4579}
4580
4581/*
4582 * "getfsize({fname})" function
4583 */
4584 static void
4585f_getfsize(typval_T *argvars, typval_T *rettv)
4586{
4587 char_u *fname;
4588 stat_T st;
4589
4590 fname = get_tv_string(&argvars[0]);
4591
4592 rettv->v_type = VAR_NUMBER;
4593
4594 if (mch_stat((char *)fname, &st) >= 0)
4595 {
4596 if (mch_isdir(fname))
4597 rettv->vval.v_number = 0;
4598 else
4599 {
4600 rettv->vval.v_number = (varnumber_T)st.st_size;
4601
4602 /* non-perfect check for overflow */
4603 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4604 rettv->vval.v_number = -2;
4605 }
4606 }
4607 else
4608 rettv->vval.v_number = -1;
4609}
4610
4611/*
4612 * "getftime({fname})" function
4613 */
4614 static void
4615f_getftime(typval_T *argvars, typval_T *rettv)
4616{
4617 char_u *fname;
4618 stat_T st;
4619
4620 fname = get_tv_string(&argvars[0]);
4621
4622 if (mch_stat((char *)fname, &st) >= 0)
4623 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4624 else
4625 rettv->vval.v_number = -1;
4626}
4627
4628/*
4629 * "getftype({fname})" function
4630 */
4631 static void
4632f_getftype(typval_T *argvars, typval_T *rettv)
4633{
4634 char_u *fname;
4635 stat_T st;
4636 char_u *type = NULL;
4637 char *t;
4638
4639 fname = get_tv_string(&argvars[0]);
4640
4641 rettv->v_type = VAR_STRING;
4642 if (mch_lstat((char *)fname, &st) >= 0)
4643 {
4644#ifdef S_ISREG
4645 if (S_ISREG(st.st_mode))
4646 t = "file";
4647 else if (S_ISDIR(st.st_mode))
4648 t = "dir";
4649# ifdef S_ISLNK
4650 else if (S_ISLNK(st.st_mode))
4651 t = "link";
4652# endif
4653# ifdef S_ISBLK
4654 else if (S_ISBLK(st.st_mode))
4655 t = "bdev";
4656# endif
4657# ifdef S_ISCHR
4658 else if (S_ISCHR(st.st_mode))
4659 t = "cdev";
4660# endif
4661# ifdef S_ISFIFO
4662 else if (S_ISFIFO(st.st_mode))
4663 t = "fifo";
4664# endif
4665# ifdef S_ISSOCK
4666 else if (S_ISSOCK(st.st_mode))
4667 t = "fifo";
4668# endif
4669 else
4670 t = "other";
4671#else
4672# ifdef S_IFMT
4673 switch (st.st_mode & S_IFMT)
4674 {
4675 case S_IFREG: t = "file"; break;
4676 case S_IFDIR: t = "dir"; break;
4677# ifdef S_IFLNK
4678 case S_IFLNK: t = "link"; break;
4679# endif
4680# ifdef S_IFBLK
4681 case S_IFBLK: t = "bdev"; break;
4682# endif
4683# ifdef S_IFCHR
4684 case S_IFCHR: t = "cdev"; break;
4685# endif
4686# ifdef S_IFIFO
4687 case S_IFIFO: t = "fifo"; break;
4688# endif
4689# ifdef S_IFSOCK
4690 case S_IFSOCK: t = "socket"; break;
4691# endif
4692 default: t = "other";
4693 }
4694# else
4695 if (mch_isdir(fname))
4696 t = "dir";
4697 else
4698 t = "file";
4699# endif
4700#endif
4701 type = vim_strsave((char_u *)t);
4702 }
4703 rettv->vval.v_string = type;
4704}
4705
4706/*
4707 * "getline(lnum, [end])" function
4708 */
4709 static void
4710f_getline(typval_T *argvars, typval_T *rettv)
4711{
4712 linenr_T lnum;
4713 linenr_T end;
4714 int retlist;
4715
4716 lnum = get_tv_lnum(argvars);
4717 if (argvars[1].v_type == VAR_UNKNOWN)
4718 {
4719 end = 0;
4720 retlist = FALSE;
4721 }
4722 else
4723 {
4724 end = get_tv_lnum(&argvars[1]);
4725 retlist = TRUE;
4726 }
4727
4728 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4729}
4730
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004731#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004732 static void
4733get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4734{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004735 if (what_arg->v_type == VAR_UNKNOWN)
4736 {
4737 if (rettv_list_alloc(rettv) == OK)
4738 if (is_qf || wp != NULL)
4739 (void)get_errorlist(wp, -1, rettv->vval.v_list);
4740 }
4741 else
4742 {
4743 if (rettv_dict_alloc(rettv) == OK)
4744 if (is_qf || (wp != NULL))
4745 {
4746 if (what_arg->v_type == VAR_DICT)
4747 {
4748 dict_T *d = what_arg->vval.v_dict;
4749
4750 if (d != NULL)
4751 get_errorlist_properties(wp, d, rettv->vval.v_dict);
4752 }
4753 else
4754 EMSG(_(e_dictreq));
4755 }
4756 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004757}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004758#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004759
4760/*
4761 * "getloclist()" function
4762 */
4763 static void
4764f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4765{
4766#ifdef FEAT_QUICKFIX
4767 win_T *wp;
4768
4769 wp = find_win_by_nr(&argvars[0], NULL);
4770 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4771#endif
4772}
4773
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004774/*
4775 * "getmatches()" function
4776 */
4777 static void
4778f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4779{
4780#ifdef FEAT_SEARCH_EXTRA
4781 dict_T *dict;
4782 matchitem_T *cur = curwin->w_match_head;
4783 int i;
4784
4785 if (rettv_list_alloc(rettv) == OK)
4786 {
4787 while (cur != NULL)
4788 {
4789 dict = dict_alloc();
4790 if (dict == NULL)
4791 return;
4792 if (cur->match.regprog == NULL)
4793 {
4794 /* match added with matchaddpos() */
4795 for (i = 0; i < MAXPOSMATCH; ++i)
4796 {
4797 llpos_T *llpos;
4798 char buf[6];
4799 list_T *l;
4800
4801 llpos = &cur->pos.pos[i];
4802 if (llpos->lnum == 0)
4803 break;
4804 l = list_alloc();
4805 if (l == NULL)
4806 break;
4807 list_append_number(l, (varnumber_T)llpos->lnum);
4808 if (llpos->col > 0)
4809 {
4810 list_append_number(l, (varnumber_T)llpos->col);
4811 list_append_number(l, (varnumber_T)llpos->len);
4812 }
4813 sprintf(buf, "pos%d", i + 1);
4814 dict_add_list(dict, buf, l);
4815 }
4816 }
4817 else
4818 {
4819 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4820 }
4821 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4822 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4823 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4824# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4825 if (cur->conceal_char)
4826 {
4827 char_u buf[MB_MAXBYTES + 1];
4828
4829 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
4830 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
4831 }
4832# endif
4833 list_append_dict(rettv->vval.v_list, dict);
4834 cur = cur->next;
4835 }
4836 }
4837#endif
4838}
4839
4840/*
4841 * "getpid()" function
4842 */
4843 static void
4844f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4845{
4846 rettv->vval.v_number = mch_get_pid();
4847}
4848
4849 static void
4850getpos_both(
4851 typval_T *argvars,
4852 typval_T *rettv,
4853 int getcurpos)
4854{
4855 pos_T *fp;
4856 list_T *l;
4857 int fnum = -1;
4858
4859 if (rettv_list_alloc(rettv) == OK)
4860 {
4861 l = rettv->vval.v_list;
4862 if (getcurpos)
4863 fp = &curwin->w_cursor;
4864 else
4865 fp = var2fpos(&argvars[0], TRUE, &fnum);
4866 if (fnum != -1)
4867 list_append_number(l, (varnumber_T)fnum);
4868 else
4869 list_append_number(l, (varnumber_T)0);
4870 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4871 : (varnumber_T)0);
4872 list_append_number(l, (fp != NULL)
4873 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4874 : (varnumber_T)0);
4875 list_append_number(l,
4876#ifdef FEAT_VIRTUALEDIT
4877 (fp != NULL) ? (varnumber_T)fp->coladd :
4878#endif
4879 (varnumber_T)0);
4880 if (getcurpos)
4881 {
4882 update_curswant();
4883 list_append_number(l, curwin->w_curswant == MAXCOL ?
4884 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
4885 }
4886 }
4887 else
4888 rettv->vval.v_number = FALSE;
4889}
4890
4891
4892/*
4893 * "getcurpos()" function
4894 */
4895 static void
4896f_getcurpos(typval_T *argvars, typval_T *rettv)
4897{
4898 getpos_both(argvars, rettv, TRUE);
4899}
4900
4901/*
4902 * "getpos(string)" function
4903 */
4904 static void
4905f_getpos(typval_T *argvars, typval_T *rettv)
4906{
4907 getpos_both(argvars, rettv, FALSE);
4908}
4909
4910/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004911 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004912 */
4913 static void
4914f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4915{
4916#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004917 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004918#endif
4919}
4920
4921/*
4922 * "getreg()" function
4923 */
4924 static void
4925f_getreg(typval_T *argvars, typval_T *rettv)
4926{
4927 char_u *strregname;
4928 int regname;
4929 int arg2 = FALSE;
4930 int return_list = FALSE;
4931 int error = FALSE;
4932
4933 if (argvars[0].v_type != VAR_UNKNOWN)
4934 {
4935 strregname = get_tv_string_chk(&argvars[0]);
4936 error = strregname == NULL;
4937 if (argvars[1].v_type != VAR_UNKNOWN)
4938 {
4939 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
4940 if (!error && argvars[2].v_type != VAR_UNKNOWN)
4941 return_list = (int)get_tv_number_chk(&argvars[2], &error);
4942 }
4943 }
4944 else
4945 strregname = get_vim_var_str(VV_REG);
4946
4947 if (error)
4948 return;
4949
4950 regname = (strregname == NULL ? '"' : *strregname);
4951 if (regname == 0)
4952 regname = '"';
4953
4954 if (return_list)
4955 {
4956 rettv->v_type = VAR_LIST;
4957 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4958 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4959 if (rettv->vval.v_list == NULL)
4960 (void)rettv_list_alloc(rettv);
4961 else
4962 ++rettv->vval.v_list->lv_refcount;
4963 }
4964 else
4965 {
4966 rettv->v_type = VAR_STRING;
4967 rettv->vval.v_string = get_reg_contents(regname,
4968 arg2 ? GREG_EXPR_SRC : 0);
4969 }
4970}
4971
4972/*
4973 * "getregtype()" function
4974 */
4975 static void
4976f_getregtype(typval_T *argvars, typval_T *rettv)
4977{
4978 char_u *strregname;
4979 int regname;
4980 char_u buf[NUMBUFLEN + 2];
4981 long reglen = 0;
4982
4983 if (argvars[0].v_type != VAR_UNKNOWN)
4984 {
4985 strregname = get_tv_string_chk(&argvars[0]);
4986 if (strregname == NULL) /* type error; errmsg already given */
4987 {
4988 rettv->v_type = VAR_STRING;
4989 rettv->vval.v_string = NULL;
4990 return;
4991 }
4992 }
4993 else
4994 /* Default to v:register */
4995 strregname = get_vim_var_str(VV_REG);
4996
4997 regname = (strregname == NULL ? '"' : *strregname);
4998 if (regname == 0)
4999 regname = '"';
5000
5001 buf[0] = NUL;
5002 buf[1] = NUL;
5003 switch (get_reg_type(regname, &reglen))
5004 {
5005 case MLINE: buf[0] = 'V'; break;
5006 case MCHAR: buf[0] = 'v'; break;
5007 case MBLOCK:
5008 buf[0] = Ctrl_V;
5009 sprintf((char *)buf + 1, "%ld", reglen + 1);
5010 break;
5011 }
5012 rettv->v_type = VAR_STRING;
5013 rettv->vval.v_string = vim_strsave(buf);
5014}
5015
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005016#ifdef FEAT_WINDOWS
5017/*
5018 * Returns information (variables, options, etc.) about a tab page
5019 * as a dictionary.
5020 */
5021 static dict_T *
5022get_tabpage_info(tabpage_T *tp, int tp_idx)
5023{
5024 win_T *wp;
5025 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005026 list_T *l;
5027
5028 dict = dict_alloc();
5029 if (dict == NULL)
5030 return NULL;
5031
Bram Moolenaar33928832016-08-18 21:22:04 +02005032 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005033
5034 l = list_alloc();
5035 if (l != NULL)
5036 {
5037 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5038 wp; wp = wp->w_next)
5039 list_append_number(l, (varnumber_T)wp->w_id);
5040 dict_add_list(dict, "windows", l);
5041 }
5042
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005043 /* Make a reference to tabpage variables */
5044 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005045
5046 return dict;
5047}
5048#endif
5049
5050/*
5051 * "gettabinfo()" function
5052 */
5053 static void
5054f_gettabinfo(typval_T *argvars, typval_T *rettv)
5055{
5056#ifdef FEAT_WINDOWS
5057 tabpage_T *tp, *tparg = NULL;
5058 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005059 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005060
5061 if (rettv_list_alloc(rettv) != OK)
5062 return;
5063
5064 if (argvars[0].v_type != VAR_UNKNOWN)
5065 {
5066 /* Information about one tab page */
5067 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5068 if (tparg == NULL)
5069 return;
5070 }
5071
5072 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005073 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005074 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005075 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005076 if (tparg != NULL && tp != tparg)
5077 continue;
5078 d = get_tabpage_info(tp, tpnr);
5079 if (d != NULL)
5080 list_append_dict(rettv->vval.v_list, d);
5081 if (tparg != NULL)
5082 return;
5083 }
5084#endif
5085}
5086
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005087/*
5088 * "gettabvar()" function
5089 */
5090 static void
5091f_gettabvar(typval_T *argvars, typval_T *rettv)
5092{
5093 win_T *oldcurwin;
5094 tabpage_T *tp, *oldtabpage;
5095 dictitem_T *v;
5096 char_u *varname;
5097 int done = FALSE;
5098
5099 rettv->v_type = VAR_STRING;
5100 rettv->vval.v_string = NULL;
5101
5102 varname = get_tv_string_chk(&argvars[1]);
5103 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5104 if (tp != NULL && varname != NULL)
5105 {
5106 /* Set tp to be our tabpage, temporarily. Also set the window to the
5107 * first window in the tabpage, otherwise the window is not valid. */
5108 if (switch_win(&oldcurwin, &oldtabpage,
5109 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
5110 == OK)
5111 {
5112 /* look up the variable */
5113 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5114 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5115 if (v != NULL)
5116 {
5117 copy_tv(&v->di_tv, rettv);
5118 done = TRUE;
5119 }
5120 }
5121
5122 /* restore previous notion of curwin */
5123 restore_win(oldcurwin, oldtabpage, TRUE);
5124 }
5125
5126 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5127 copy_tv(&argvars[2], rettv);
5128}
5129
5130/*
5131 * "gettabwinvar()" function
5132 */
5133 static void
5134f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5135{
5136 getwinvar(argvars, rettv, 1);
5137}
5138
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005139#ifdef FEAT_WINDOWS
5140/*
5141 * Returns information about a window as a dictionary.
5142 */
5143 static dict_T *
5144get_win_info(win_T *wp, short tpnr, short winnr)
5145{
5146 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005147
5148 dict = dict_alloc();
5149 if (dict == NULL)
5150 return NULL;
5151
Bram Moolenaar33928832016-08-18 21:22:04 +02005152 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5153 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005154 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5155 dict_add_nr_str(dict, "height", wp->w_height, NULL);
5156 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005157 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005158
Bram Moolenaar386600f2016-08-15 22:16:25 +02005159#ifdef FEAT_QUICKFIX
5160 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5161 dict_add_nr_str(dict, "loclist",
5162 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5163#endif
5164
Bram Moolenaar30567352016-08-27 21:25:44 +02005165 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005166 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005167
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005168 return dict;
5169}
5170#endif
5171
5172/*
5173 * "getwininfo()" function
5174 */
5175 static void
5176f_getwininfo(typval_T *argvars, typval_T *rettv)
5177{
5178#ifdef FEAT_WINDOWS
5179 tabpage_T *tp;
5180 win_T *wp = NULL, *wparg = NULL;
5181 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005182 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005183#endif
5184
5185 if (rettv_list_alloc(rettv) != OK)
5186 return;
5187
5188#ifdef FEAT_WINDOWS
5189 if (argvars[0].v_type != VAR_UNKNOWN)
5190 {
5191 wparg = win_id2wp(argvars);
5192 if (wparg == NULL)
5193 return;
5194 }
5195
5196 /* Collect information about either all the windows across all the tab
5197 * pages or one particular window.
5198 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005199 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005200 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005201 tabnr++;
5202 winnr = 0;
5203 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005204 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005205 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005206 if (wparg != NULL && wp != wparg)
5207 continue;
5208 d = get_win_info(wp, tabnr, winnr);
5209 if (d != NULL)
5210 list_append_dict(rettv->vval.v_list, d);
5211 if (wparg != NULL)
5212 /* found information about a specific window */
5213 return;
5214 }
5215 }
5216#endif
5217}
5218
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005219/*
5220 * "getwinposx()" function
5221 */
5222 static void
5223f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5224{
5225 rettv->vval.v_number = -1;
5226#ifdef FEAT_GUI
5227 if (gui.in_use)
5228 {
5229 int x, y;
5230
5231 if (gui_mch_get_winpos(&x, &y) == OK)
5232 rettv->vval.v_number = x;
5233 }
5234#endif
5235}
5236
5237/*
5238 * "win_findbuf()" function
5239 */
5240 static void
5241f_win_findbuf(typval_T *argvars, typval_T *rettv)
5242{
5243 if (rettv_list_alloc(rettv) != FAIL)
5244 win_findbuf(argvars, rettv->vval.v_list);
5245}
5246
5247/*
5248 * "win_getid()" function
5249 */
5250 static void
5251f_win_getid(typval_T *argvars, typval_T *rettv)
5252{
5253 rettv->vval.v_number = win_getid(argvars);
5254}
5255
5256/*
5257 * "win_gotoid()" function
5258 */
5259 static void
5260f_win_gotoid(typval_T *argvars, typval_T *rettv)
5261{
5262 rettv->vval.v_number = win_gotoid(argvars);
5263}
5264
5265/*
5266 * "win_id2tabwin()" function
5267 */
5268 static void
5269f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5270{
5271 if (rettv_list_alloc(rettv) != FAIL)
5272 win_id2tabwin(argvars, rettv->vval.v_list);
5273}
5274
5275/*
5276 * "win_id2win()" function
5277 */
5278 static void
5279f_win_id2win(typval_T *argvars, typval_T *rettv)
5280{
5281 rettv->vval.v_number = win_id2win(argvars);
5282}
5283
5284/*
5285 * "getwinposy()" function
5286 */
5287 static void
5288f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5289{
5290 rettv->vval.v_number = -1;
5291#ifdef FEAT_GUI
5292 if (gui.in_use)
5293 {
5294 int x, y;
5295
5296 if (gui_mch_get_winpos(&x, &y) == OK)
5297 rettv->vval.v_number = y;
5298 }
5299#endif
5300}
5301
5302/*
5303 * "getwinvar()" function
5304 */
5305 static void
5306f_getwinvar(typval_T *argvars, typval_T *rettv)
5307{
5308 getwinvar(argvars, rettv, 0);
5309}
5310
5311/*
5312 * "glob()" function
5313 */
5314 static void
5315f_glob(typval_T *argvars, typval_T *rettv)
5316{
5317 int options = WILD_SILENT|WILD_USE_NL;
5318 expand_T xpc;
5319 int error = FALSE;
5320
5321 /* When the optional second argument is non-zero, don't remove matches
5322 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5323 rettv->v_type = VAR_STRING;
5324 if (argvars[1].v_type != VAR_UNKNOWN)
5325 {
5326 if (get_tv_number_chk(&argvars[1], &error))
5327 options |= WILD_KEEP_ALL;
5328 if (argvars[2].v_type != VAR_UNKNOWN)
5329 {
5330 if (get_tv_number_chk(&argvars[2], &error))
5331 {
5332 rettv->v_type = VAR_LIST;
5333 rettv->vval.v_list = NULL;
5334 }
5335 if (argvars[3].v_type != VAR_UNKNOWN
5336 && get_tv_number_chk(&argvars[3], &error))
5337 options |= WILD_ALLLINKS;
5338 }
5339 }
5340 if (!error)
5341 {
5342 ExpandInit(&xpc);
5343 xpc.xp_context = EXPAND_FILES;
5344 if (p_wic)
5345 options += WILD_ICASE;
5346 if (rettv->v_type == VAR_STRING)
5347 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5348 NULL, options, WILD_ALL);
5349 else if (rettv_list_alloc(rettv) != FAIL)
5350 {
5351 int i;
5352
5353 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5354 NULL, options, WILD_ALL_KEEP);
5355 for (i = 0; i < xpc.xp_numfiles; i++)
5356 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5357
5358 ExpandCleanup(&xpc);
5359 }
5360 }
5361 else
5362 rettv->vval.v_string = NULL;
5363}
5364
5365/*
5366 * "globpath()" function
5367 */
5368 static void
5369f_globpath(typval_T *argvars, typval_T *rettv)
5370{
5371 int flags = 0;
5372 char_u buf1[NUMBUFLEN];
5373 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5374 int error = FALSE;
5375 garray_T ga;
5376 int i;
5377
5378 /* When the optional second argument is non-zero, don't remove matches
5379 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5380 rettv->v_type = VAR_STRING;
5381 if (argvars[2].v_type != VAR_UNKNOWN)
5382 {
5383 if (get_tv_number_chk(&argvars[2], &error))
5384 flags |= WILD_KEEP_ALL;
5385 if (argvars[3].v_type != VAR_UNKNOWN)
5386 {
5387 if (get_tv_number_chk(&argvars[3], &error))
5388 {
5389 rettv->v_type = VAR_LIST;
5390 rettv->vval.v_list = NULL;
5391 }
5392 if (argvars[4].v_type != VAR_UNKNOWN
5393 && get_tv_number_chk(&argvars[4], &error))
5394 flags |= WILD_ALLLINKS;
5395 }
5396 }
5397 if (file != NULL && !error)
5398 {
5399 ga_init2(&ga, (int)sizeof(char_u *), 10);
5400 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5401 if (rettv->v_type == VAR_STRING)
5402 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5403 else if (rettv_list_alloc(rettv) != FAIL)
5404 for (i = 0; i < ga.ga_len; ++i)
5405 list_append_string(rettv->vval.v_list,
5406 ((char_u **)(ga.ga_data))[i], -1);
5407 ga_clear_strings(&ga);
5408 }
5409 else
5410 rettv->vval.v_string = NULL;
5411}
5412
5413/*
5414 * "glob2regpat()" function
5415 */
5416 static void
5417f_glob2regpat(typval_T *argvars, typval_T *rettv)
5418{
5419 char_u *pat = get_tv_string_chk(&argvars[0]);
5420
5421 rettv->v_type = VAR_STRING;
5422 rettv->vval.v_string = (pat == NULL)
5423 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5424}
5425
5426/* for VIM_VERSION_ defines */
5427#include "version.h"
5428
5429/*
5430 * "has()" function
5431 */
5432 static void
5433f_has(typval_T *argvars, typval_T *rettv)
5434{
5435 int i;
5436 char_u *name;
5437 int n = FALSE;
5438 static char *(has_list[]) =
5439 {
5440#ifdef AMIGA
5441 "amiga",
5442# ifdef FEAT_ARP
5443 "arp",
5444# endif
5445#endif
5446#ifdef __BEOS__
5447 "beos",
5448#endif
5449#ifdef MACOS
5450 "mac",
5451#endif
5452#if defined(MACOS_X_UNIX)
5453 "macunix", /* built with 'darwin' enabled */
5454#endif
5455#if defined(__APPLE__) && __APPLE__ == 1
5456 "osx", /* built with or without 'darwin' enabled */
5457#endif
5458#ifdef __QNX__
5459 "qnx",
5460#endif
5461#ifdef UNIX
5462 "unix",
5463#endif
5464#ifdef VMS
5465 "vms",
5466#endif
5467#ifdef WIN32
5468 "win32",
5469#endif
5470#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5471 "win32unix",
5472#endif
5473#if defined(WIN64) || defined(_WIN64)
5474 "win64",
5475#endif
5476#ifdef EBCDIC
5477 "ebcdic",
5478#endif
5479#ifndef CASE_INSENSITIVE_FILENAME
5480 "fname_case",
5481#endif
5482#ifdef HAVE_ACL
5483 "acl",
5484#endif
5485#ifdef FEAT_ARABIC
5486 "arabic",
5487#endif
5488#ifdef FEAT_AUTOCMD
5489 "autocmd",
5490#endif
5491#ifdef FEAT_BEVAL
5492 "balloon_eval",
5493# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5494 "balloon_multiline",
5495# endif
5496#endif
5497#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5498 "builtin_terms",
5499# ifdef ALL_BUILTIN_TCAPS
5500 "all_builtin_terms",
5501# endif
5502#endif
5503#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5504 || defined(FEAT_GUI_W32) \
5505 || defined(FEAT_GUI_MOTIF))
5506 "browsefilter",
5507#endif
5508#ifdef FEAT_BYTEOFF
5509 "byte_offset",
5510#endif
5511#ifdef FEAT_JOB_CHANNEL
5512 "channel",
5513#endif
5514#ifdef FEAT_CINDENT
5515 "cindent",
5516#endif
5517#ifdef FEAT_CLIENTSERVER
5518 "clientserver",
5519#endif
5520#ifdef FEAT_CLIPBOARD
5521 "clipboard",
5522#endif
5523#ifdef FEAT_CMDL_COMPL
5524 "cmdline_compl",
5525#endif
5526#ifdef FEAT_CMDHIST
5527 "cmdline_hist",
5528#endif
5529#ifdef FEAT_COMMENTS
5530 "comments",
5531#endif
5532#ifdef FEAT_CONCEAL
5533 "conceal",
5534#endif
5535#ifdef FEAT_CRYPT
5536 "cryptv",
5537 "crypt-blowfish",
5538 "crypt-blowfish2",
5539#endif
5540#ifdef FEAT_CSCOPE
5541 "cscope",
5542#endif
5543#ifdef FEAT_CURSORBIND
5544 "cursorbind",
5545#endif
5546#ifdef CURSOR_SHAPE
5547 "cursorshape",
5548#endif
5549#ifdef DEBUG
5550 "debug",
5551#endif
5552#ifdef FEAT_CON_DIALOG
5553 "dialog_con",
5554#endif
5555#ifdef FEAT_GUI_DIALOG
5556 "dialog_gui",
5557#endif
5558#ifdef FEAT_DIFF
5559 "diff",
5560#endif
5561#ifdef FEAT_DIGRAPHS
5562 "digraphs",
5563#endif
5564#ifdef FEAT_DIRECTX
5565 "directx",
5566#endif
5567#ifdef FEAT_DND
5568 "dnd",
5569#endif
5570#ifdef FEAT_EMACS_TAGS
5571 "emacs_tags",
5572#endif
5573 "eval", /* always present, of course! */
5574 "ex_extra", /* graduated feature */
5575#ifdef FEAT_SEARCH_EXTRA
5576 "extra_search",
5577#endif
5578#ifdef FEAT_FKMAP
5579 "farsi",
5580#endif
5581#ifdef FEAT_SEARCHPATH
5582 "file_in_path",
5583#endif
5584#ifdef FEAT_FILTERPIPE
5585 "filterpipe",
5586#endif
5587#ifdef FEAT_FIND_ID
5588 "find_in_path",
5589#endif
5590#ifdef FEAT_FLOAT
5591 "float",
5592#endif
5593#ifdef FEAT_FOLDING
5594 "folding",
5595#endif
5596#ifdef FEAT_FOOTER
5597 "footer",
5598#endif
5599#if !defined(USE_SYSTEM) && defined(UNIX)
5600 "fork",
5601#endif
5602#ifdef FEAT_GETTEXT
5603 "gettext",
5604#endif
5605#ifdef FEAT_GUI
5606 "gui",
5607#endif
5608#ifdef FEAT_GUI_ATHENA
5609# ifdef FEAT_GUI_NEXTAW
5610 "gui_neXtaw",
5611# else
5612 "gui_athena",
5613# endif
5614#endif
5615#ifdef FEAT_GUI_GTK
5616 "gui_gtk",
5617# ifdef USE_GTK3
5618 "gui_gtk3",
5619# else
5620 "gui_gtk2",
5621# endif
5622#endif
5623#ifdef FEAT_GUI_GNOME
5624 "gui_gnome",
5625#endif
5626#ifdef FEAT_GUI_MAC
5627 "gui_mac",
5628#endif
5629#ifdef FEAT_GUI_MOTIF
5630 "gui_motif",
5631#endif
5632#ifdef FEAT_GUI_PHOTON
5633 "gui_photon",
5634#endif
5635#ifdef FEAT_GUI_W32
5636 "gui_win32",
5637#endif
5638#ifdef FEAT_HANGULIN
5639 "hangul_input",
5640#endif
5641#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5642 "iconv",
5643#endif
5644#ifdef FEAT_INS_EXPAND
5645 "insert_expand",
5646#endif
5647#ifdef FEAT_JOB_CHANNEL
5648 "job",
5649#endif
5650#ifdef FEAT_JUMPLIST
5651 "jumplist",
5652#endif
5653#ifdef FEAT_KEYMAP
5654 "keymap",
5655#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005656 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005657#ifdef FEAT_LANGMAP
5658 "langmap",
5659#endif
5660#ifdef FEAT_LIBCALL
5661 "libcall",
5662#endif
5663#ifdef FEAT_LINEBREAK
5664 "linebreak",
5665#endif
5666#ifdef FEAT_LISP
5667 "lispindent",
5668#endif
5669#ifdef FEAT_LISTCMDS
5670 "listcmds",
5671#endif
5672#ifdef FEAT_LOCALMAP
5673 "localmap",
5674#endif
5675#ifdef FEAT_LUA
5676# ifndef DYNAMIC_LUA
5677 "lua",
5678# endif
5679#endif
5680#ifdef FEAT_MENU
5681 "menu",
5682#endif
5683#ifdef FEAT_SESSION
5684 "mksession",
5685#endif
5686#ifdef FEAT_MODIFY_FNAME
5687 "modify_fname",
5688#endif
5689#ifdef FEAT_MOUSE
5690 "mouse",
5691#endif
5692#ifdef FEAT_MOUSESHAPE
5693 "mouseshape",
5694#endif
5695#if defined(UNIX) || defined(VMS)
5696# ifdef FEAT_MOUSE_DEC
5697 "mouse_dec",
5698# endif
5699# ifdef FEAT_MOUSE_GPM
5700 "mouse_gpm",
5701# endif
5702# ifdef FEAT_MOUSE_JSB
5703 "mouse_jsbterm",
5704# endif
5705# ifdef FEAT_MOUSE_NET
5706 "mouse_netterm",
5707# endif
5708# ifdef FEAT_MOUSE_PTERM
5709 "mouse_pterm",
5710# endif
5711# ifdef FEAT_MOUSE_SGR
5712 "mouse_sgr",
5713# endif
5714# ifdef FEAT_SYSMOUSE
5715 "mouse_sysmouse",
5716# endif
5717# ifdef FEAT_MOUSE_URXVT
5718 "mouse_urxvt",
5719# endif
5720# ifdef FEAT_MOUSE_XTERM
5721 "mouse_xterm",
5722# endif
5723#endif
5724#ifdef FEAT_MBYTE
5725 "multi_byte",
5726#endif
5727#ifdef FEAT_MBYTE_IME
5728 "multi_byte_ime",
5729#endif
5730#ifdef FEAT_MULTI_LANG
5731 "multi_lang",
5732#endif
5733#ifdef FEAT_MZSCHEME
5734#ifndef DYNAMIC_MZSCHEME
5735 "mzscheme",
5736#endif
5737#endif
5738#ifdef FEAT_NUM64
5739 "num64",
5740#endif
5741#ifdef FEAT_OLE
5742 "ole",
5743#endif
5744 "packages",
5745#ifdef FEAT_PATH_EXTRA
5746 "path_extra",
5747#endif
5748#ifdef FEAT_PERL
5749#ifndef DYNAMIC_PERL
5750 "perl",
5751#endif
5752#endif
5753#ifdef FEAT_PERSISTENT_UNDO
5754 "persistent_undo",
5755#endif
5756#ifdef FEAT_PYTHON
5757#ifndef DYNAMIC_PYTHON
5758 "python",
5759#endif
5760#endif
5761#ifdef FEAT_PYTHON3
5762#ifndef DYNAMIC_PYTHON3
5763 "python3",
5764#endif
5765#endif
5766#ifdef FEAT_POSTSCRIPT
5767 "postscript",
5768#endif
5769#ifdef FEAT_PRINTER
5770 "printer",
5771#endif
5772#ifdef FEAT_PROFILE
5773 "profile",
5774#endif
5775#ifdef FEAT_RELTIME
5776 "reltime",
5777#endif
5778#ifdef FEAT_QUICKFIX
5779 "quickfix",
5780#endif
5781#ifdef FEAT_RIGHTLEFT
5782 "rightleft",
5783#endif
5784#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5785 "ruby",
5786#endif
5787#ifdef FEAT_SCROLLBIND
5788 "scrollbind",
5789#endif
5790#ifdef FEAT_CMDL_INFO
5791 "showcmd",
5792 "cmdline_info",
5793#endif
5794#ifdef FEAT_SIGNS
5795 "signs",
5796#endif
5797#ifdef FEAT_SMARTINDENT
5798 "smartindent",
5799#endif
5800#ifdef STARTUPTIME
5801 "startuptime",
5802#endif
5803#ifdef FEAT_STL_OPT
5804 "statusline",
5805#endif
5806#ifdef FEAT_SUN_WORKSHOP
5807 "sun_workshop",
5808#endif
5809#ifdef FEAT_NETBEANS_INTG
5810 "netbeans_intg",
5811#endif
5812#ifdef FEAT_SPELL
5813 "spell",
5814#endif
5815#ifdef FEAT_SYN_HL
5816 "syntax",
5817#endif
5818#if defined(USE_SYSTEM) || !defined(UNIX)
5819 "system",
5820#endif
5821#ifdef FEAT_TAG_BINS
5822 "tag_binary",
5823#endif
5824#ifdef FEAT_TAG_OLDSTATIC
5825 "tag_old_static",
5826#endif
5827#ifdef FEAT_TAG_ANYWHITE
5828 "tag_any_white",
5829#endif
5830#ifdef FEAT_TCL
5831# ifndef DYNAMIC_TCL
5832 "tcl",
5833# endif
5834#endif
5835#ifdef FEAT_TERMGUICOLORS
5836 "termguicolors",
5837#endif
5838#ifdef TERMINFO
5839 "terminfo",
5840#endif
5841#ifdef FEAT_TERMRESPONSE
5842 "termresponse",
5843#endif
5844#ifdef FEAT_TEXTOBJ
5845 "textobjects",
5846#endif
5847#ifdef HAVE_TGETENT
5848 "tgetent",
5849#endif
5850#ifdef FEAT_TIMERS
5851 "timers",
5852#endif
5853#ifdef FEAT_TITLE
5854 "title",
5855#endif
5856#ifdef FEAT_TOOLBAR
5857 "toolbar",
5858#endif
5859#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5860 "unnamedplus",
5861#endif
5862#ifdef FEAT_USR_CMDS
5863 "user-commands", /* was accidentally included in 5.4 */
5864 "user_commands",
5865#endif
5866#ifdef FEAT_VIMINFO
5867 "viminfo",
5868#endif
5869#ifdef FEAT_WINDOWS
5870 "vertsplit",
5871#endif
5872#ifdef FEAT_VIRTUALEDIT
5873 "virtualedit",
5874#endif
5875 "visual",
5876#ifdef FEAT_VISUALEXTRA
5877 "visualextra",
5878#endif
5879#ifdef FEAT_VREPLACE
5880 "vreplace",
5881#endif
5882#ifdef FEAT_WILDIGN
5883 "wildignore",
5884#endif
5885#ifdef FEAT_WILDMENU
5886 "wildmenu",
5887#endif
5888#ifdef FEAT_WINDOWS
5889 "windows",
5890#endif
5891#ifdef FEAT_WAK
5892 "winaltkeys",
5893#endif
5894#ifdef FEAT_WRITEBACKUP
5895 "writebackup",
5896#endif
5897#ifdef FEAT_XIM
5898 "xim",
5899#endif
5900#ifdef FEAT_XFONTSET
5901 "xfontset",
5902#endif
5903#ifdef FEAT_XPM_W32
5904 "xpm",
5905 "xpm_w32", /* for backward compatibility */
5906#else
5907# if defined(HAVE_XPM)
5908 "xpm",
5909# endif
5910#endif
5911#ifdef USE_XSMP
5912 "xsmp",
5913#endif
5914#ifdef USE_XSMP_INTERACT
5915 "xsmp_interact",
5916#endif
5917#ifdef FEAT_XCLIPBOARD
5918 "xterm_clipboard",
5919#endif
5920#ifdef FEAT_XTERM_SAVE
5921 "xterm_save",
5922#endif
5923#if defined(UNIX) && defined(FEAT_X11)
5924 "X11",
5925#endif
5926 NULL
5927 };
5928
5929 name = get_tv_string(&argvars[0]);
5930 for (i = 0; has_list[i] != NULL; ++i)
5931 if (STRICMP(name, has_list[i]) == 0)
5932 {
5933 n = TRUE;
5934 break;
5935 }
5936
5937 if (n == FALSE)
5938 {
5939 if (STRNICMP(name, "patch", 5) == 0)
5940 {
5941 if (name[5] == '-'
5942 && STRLEN(name) >= 11
5943 && vim_isdigit(name[6])
5944 && vim_isdigit(name[8])
5945 && vim_isdigit(name[10]))
5946 {
5947 int major = atoi((char *)name + 6);
5948 int minor = atoi((char *)name + 8);
5949
5950 /* Expect "patch-9.9.01234". */
5951 n = (major < VIM_VERSION_MAJOR
5952 || (major == VIM_VERSION_MAJOR
5953 && (minor < VIM_VERSION_MINOR
5954 || (minor == VIM_VERSION_MINOR
5955 && has_patch(atoi((char *)name + 10))))));
5956 }
5957 else
5958 n = has_patch(atoi((char *)name + 5));
5959 }
5960 else if (STRICMP(name, "vim_starting") == 0)
5961 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01005962 else if (STRICMP(name, "ttyin") == 0)
5963 n = mch_input_isatty();
5964 else if (STRICMP(name, "ttyout") == 0)
5965 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005966#ifdef FEAT_MBYTE
5967 else if (STRICMP(name, "multi_byte_encoding") == 0)
5968 n = has_mbyte;
5969#endif
5970#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
5971 else if (STRICMP(name, "balloon_multiline") == 0)
5972 n = multiline_balloon_available();
5973#endif
5974#ifdef DYNAMIC_TCL
5975 else if (STRICMP(name, "tcl") == 0)
5976 n = tcl_enabled(FALSE);
5977#endif
5978#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5979 else if (STRICMP(name, "iconv") == 0)
5980 n = iconv_enabled(FALSE);
5981#endif
5982#ifdef DYNAMIC_LUA
5983 else if (STRICMP(name, "lua") == 0)
5984 n = lua_enabled(FALSE);
5985#endif
5986#ifdef DYNAMIC_MZSCHEME
5987 else if (STRICMP(name, "mzscheme") == 0)
5988 n = mzscheme_enabled(FALSE);
5989#endif
5990#ifdef DYNAMIC_RUBY
5991 else if (STRICMP(name, "ruby") == 0)
5992 n = ruby_enabled(FALSE);
5993#endif
5994#ifdef FEAT_PYTHON
5995#ifdef DYNAMIC_PYTHON
5996 else if (STRICMP(name, "python") == 0)
5997 n = python_enabled(FALSE);
5998#endif
5999#endif
6000#ifdef FEAT_PYTHON3
6001#ifdef DYNAMIC_PYTHON3
6002 else if (STRICMP(name, "python3") == 0)
6003 n = python3_enabled(FALSE);
6004#endif
6005#endif
6006#ifdef DYNAMIC_PERL
6007 else if (STRICMP(name, "perl") == 0)
6008 n = perl_enabled(FALSE);
6009#endif
6010#ifdef FEAT_GUI
6011 else if (STRICMP(name, "gui_running") == 0)
6012 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006013# ifdef FEAT_BROWSE
6014 else if (STRICMP(name, "browse") == 0)
6015 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6016# endif
6017#endif
6018#ifdef FEAT_SYN_HL
6019 else if (STRICMP(name, "syntax_items") == 0)
6020 n = syntax_present(curwin);
6021#endif
6022#if defined(WIN3264)
6023 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006024 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006025#endif
6026#ifdef FEAT_NETBEANS_INTG
6027 else if (STRICMP(name, "netbeans_enabled") == 0)
6028 n = netbeans_active();
6029#endif
6030 }
6031
6032 rettv->vval.v_number = n;
6033}
6034
6035/*
6036 * "has_key()" function
6037 */
6038 static void
6039f_has_key(typval_T *argvars, typval_T *rettv)
6040{
6041 if (argvars[0].v_type != VAR_DICT)
6042 {
6043 EMSG(_(e_dictreq));
6044 return;
6045 }
6046 if (argvars[0].vval.v_dict == NULL)
6047 return;
6048
6049 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6050 get_tv_string(&argvars[1]), -1) != NULL;
6051}
6052
6053/*
6054 * "haslocaldir()" function
6055 */
6056 static void
6057f_haslocaldir(typval_T *argvars, typval_T *rettv)
6058{
6059 win_T *wp = NULL;
6060
6061 wp = find_tabwin(&argvars[0], &argvars[1]);
6062 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6063}
6064
6065/*
6066 * "hasmapto()" function
6067 */
6068 static void
6069f_hasmapto(typval_T *argvars, typval_T *rettv)
6070{
6071 char_u *name;
6072 char_u *mode;
6073 char_u buf[NUMBUFLEN];
6074 int abbr = FALSE;
6075
6076 name = get_tv_string(&argvars[0]);
6077 if (argvars[1].v_type == VAR_UNKNOWN)
6078 mode = (char_u *)"nvo";
6079 else
6080 {
6081 mode = get_tv_string_buf(&argvars[1], buf);
6082 if (argvars[2].v_type != VAR_UNKNOWN)
6083 abbr = (int)get_tv_number(&argvars[2]);
6084 }
6085
6086 if (map_to_exists(name, mode, abbr))
6087 rettv->vval.v_number = TRUE;
6088 else
6089 rettv->vval.v_number = FALSE;
6090}
6091
6092/*
6093 * "histadd()" function
6094 */
6095 static void
6096f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6097{
6098#ifdef FEAT_CMDHIST
6099 int histype;
6100 char_u *str;
6101 char_u buf[NUMBUFLEN];
6102#endif
6103
6104 rettv->vval.v_number = FALSE;
6105 if (check_restricted() || check_secure())
6106 return;
6107#ifdef FEAT_CMDHIST
6108 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6109 histype = str != NULL ? get_histtype(str) : -1;
6110 if (histype >= 0)
6111 {
6112 str = get_tv_string_buf(&argvars[1], buf);
6113 if (*str != NUL)
6114 {
6115 init_history();
6116 add_to_history(histype, str, FALSE, NUL);
6117 rettv->vval.v_number = TRUE;
6118 return;
6119 }
6120 }
6121#endif
6122}
6123
6124/*
6125 * "histdel()" function
6126 */
6127 static void
6128f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6129{
6130#ifdef FEAT_CMDHIST
6131 int n;
6132 char_u buf[NUMBUFLEN];
6133 char_u *str;
6134
6135 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6136 if (str == NULL)
6137 n = 0;
6138 else if (argvars[1].v_type == VAR_UNKNOWN)
6139 /* only one argument: clear entire history */
6140 n = clr_history(get_histtype(str));
6141 else if (argvars[1].v_type == VAR_NUMBER)
6142 /* index given: remove that entry */
6143 n = del_history_idx(get_histtype(str),
6144 (int)get_tv_number(&argvars[1]));
6145 else
6146 /* string given: remove all matching entries */
6147 n = del_history_entry(get_histtype(str),
6148 get_tv_string_buf(&argvars[1], buf));
6149 rettv->vval.v_number = n;
6150#endif
6151}
6152
6153/*
6154 * "histget()" function
6155 */
6156 static void
6157f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6158{
6159#ifdef FEAT_CMDHIST
6160 int type;
6161 int idx;
6162 char_u *str;
6163
6164 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6165 if (str == NULL)
6166 rettv->vval.v_string = NULL;
6167 else
6168 {
6169 type = get_histtype(str);
6170 if (argvars[1].v_type == VAR_UNKNOWN)
6171 idx = get_history_idx(type);
6172 else
6173 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6174 /* -1 on type error */
6175 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6176 }
6177#else
6178 rettv->vval.v_string = NULL;
6179#endif
6180 rettv->v_type = VAR_STRING;
6181}
6182
6183/*
6184 * "histnr()" function
6185 */
6186 static void
6187f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6188{
6189 int i;
6190
6191#ifdef FEAT_CMDHIST
6192 char_u *history = get_tv_string_chk(&argvars[0]);
6193
6194 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6195 if (i >= HIST_CMD && i < HIST_COUNT)
6196 i = get_history_idx(i);
6197 else
6198#endif
6199 i = -1;
6200 rettv->vval.v_number = i;
6201}
6202
6203/*
6204 * "highlightID(name)" function
6205 */
6206 static void
6207f_hlID(typval_T *argvars, typval_T *rettv)
6208{
6209 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6210}
6211
6212/*
6213 * "highlight_exists()" function
6214 */
6215 static void
6216f_hlexists(typval_T *argvars, typval_T *rettv)
6217{
6218 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6219}
6220
6221/*
6222 * "hostname()" function
6223 */
6224 static void
6225f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6226{
6227 char_u hostname[256];
6228
6229 mch_get_host_name(hostname, 256);
6230 rettv->v_type = VAR_STRING;
6231 rettv->vval.v_string = vim_strsave(hostname);
6232}
6233
6234/*
6235 * iconv() function
6236 */
6237 static void
6238f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6239{
6240#ifdef FEAT_MBYTE
6241 char_u buf1[NUMBUFLEN];
6242 char_u buf2[NUMBUFLEN];
6243 char_u *from, *to, *str;
6244 vimconv_T vimconv;
6245#endif
6246
6247 rettv->v_type = VAR_STRING;
6248 rettv->vval.v_string = NULL;
6249
6250#ifdef FEAT_MBYTE
6251 str = get_tv_string(&argvars[0]);
6252 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6253 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6254 vimconv.vc_type = CONV_NONE;
6255 convert_setup(&vimconv, from, to);
6256
6257 /* If the encodings are equal, no conversion needed. */
6258 if (vimconv.vc_type == CONV_NONE)
6259 rettv->vval.v_string = vim_strsave(str);
6260 else
6261 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6262
6263 convert_setup(&vimconv, NULL, NULL);
6264 vim_free(from);
6265 vim_free(to);
6266#endif
6267}
6268
6269/*
6270 * "indent()" function
6271 */
6272 static void
6273f_indent(typval_T *argvars, typval_T *rettv)
6274{
6275 linenr_T lnum;
6276
6277 lnum = get_tv_lnum(argvars);
6278 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6279 rettv->vval.v_number = get_indent_lnum(lnum);
6280 else
6281 rettv->vval.v_number = -1;
6282}
6283
6284/*
6285 * "index()" function
6286 */
6287 static void
6288f_index(typval_T *argvars, typval_T *rettv)
6289{
6290 list_T *l;
6291 listitem_T *item;
6292 long idx = 0;
6293 int ic = FALSE;
6294
6295 rettv->vval.v_number = -1;
6296 if (argvars[0].v_type != VAR_LIST)
6297 {
6298 EMSG(_(e_listreq));
6299 return;
6300 }
6301 l = argvars[0].vval.v_list;
6302 if (l != NULL)
6303 {
6304 item = l->lv_first;
6305 if (argvars[2].v_type != VAR_UNKNOWN)
6306 {
6307 int error = FALSE;
6308
6309 /* Start at specified item. Use the cached index that list_find()
6310 * sets, so that a negative number also works. */
6311 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6312 idx = l->lv_idx;
6313 if (argvars[3].v_type != VAR_UNKNOWN)
6314 ic = (int)get_tv_number_chk(&argvars[3], &error);
6315 if (error)
6316 item = NULL;
6317 }
6318
6319 for ( ; item != NULL; item = item->li_next, ++idx)
6320 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6321 {
6322 rettv->vval.v_number = idx;
6323 break;
6324 }
6325 }
6326}
6327
6328static int inputsecret_flag = 0;
6329
6330/*
6331 * "input()" function
6332 * Also handles inputsecret() when inputsecret is set.
6333 */
6334 static void
6335f_input(typval_T *argvars, typval_T *rettv)
6336{
6337 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6338}
6339
6340/*
6341 * "inputdialog()" function
6342 */
6343 static void
6344f_inputdialog(typval_T *argvars, typval_T *rettv)
6345{
6346#if defined(FEAT_GUI_TEXTDIALOG)
6347 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6348 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6349 {
6350 char_u *message;
6351 char_u buf[NUMBUFLEN];
6352 char_u *defstr = (char_u *)"";
6353
6354 message = get_tv_string_chk(&argvars[0]);
6355 if (argvars[1].v_type != VAR_UNKNOWN
6356 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6357 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6358 else
6359 IObuff[0] = NUL;
6360 if (message != NULL && defstr != NULL
6361 && do_dialog(VIM_QUESTION, NULL, message,
6362 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6363 rettv->vval.v_string = vim_strsave(IObuff);
6364 else
6365 {
6366 if (message != NULL && defstr != NULL
6367 && argvars[1].v_type != VAR_UNKNOWN
6368 && argvars[2].v_type != VAR_UNKNOWN)
6369 rettv->vval.v_string = vim_strsave(
6370 get_tv_string_buf(&argvars[2], buf));
6371 else
6372 rettv->vval.v_string = NULL;
6373 }
6374 rettv->v_type = VAR_STRING;
6375 }
6376 else
6377#endif
6378 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6379}
6380
6381/*
6382 * "inputlist()" function
6383 */
6384 static void
6385f_inputlist(typval_T *argvars, typval_T *rettv)
6386{
6387 listitem_T *li;
6388 int selected;
6389 int mouse_used;
6390
6391#ifdef NO_CONSOLE_INPUT
6392 /* While starting up, there is no place to enter text. */
6393 if (no_console_input())
6394 return;
6395#endif
6396 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6397 {
6398 EMSG2(_(e_listarg), "inputlist()");
6399 return;
6400 }
6401
6402 msg_start();
6403 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6404 lines_left = Rows; /* avoid more prompt */
6405 msg_scroll = TRUE;
6406 msg_clr_eos();
6407
6408 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6409 {
6410 msg_puts(get_tv_string(&li->li_tv));
6411 msg_putchar('\n');
6412 }
6413
6414 /* Ask for choice. */
6415 selected = prompt_for_number(&mouse_used);
6416 if (mouse_used)
6417 selected -= lines_left;
6418
6419 rettv->vval.v_number = selected;
6420}
6421
6422
6423static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6424
6425/*
6426 * "inputrestore()" function
6427 */
6428 static void
6429f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6430{
6431 if (ga_userinput.ga_len > 0)
6432 {
6433 --ga_userinput.ga_len;
6434 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6435 + ga_userinput.ga_len);
6436 /* default return is zero == OK */
6437 }
6438 else if (p_verbose > 1)
6439 {
6440 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6441 rettv->vval.v_number = 1; /* Failed */
6442 }
6443}
6444
6445/*
6446 * "inputsave()" function
6447 */
6448 static void
6449f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6450{
6451 /* Add an entry to the stack of typeahead storage. */
6452 if (ga_grow(&ga_userinput, 1) == OK)
6453 {
6454 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6455 + ga_userinput.ga_len);
6456 ++ga_userinput.ga_len;
6457 /* default return is zero == OK */
6458 }
6459 else
6460 rettv->vval.v_number = 1; /* Failed */
6461}
6462
6463/*
6464 * "inputsecret()" function
6465 */
6466 static void
6467f_inputsecret(typval_T *argvars, typval_T *rettv)
6468{
6469 ++cmdline_star;
6470 ++inputsecret_flag;
6471 f_input(argvars, rettv);
6472 --cmdline_star;
6473 --inputsecret_flag;
6474}
6475
6476/*
6477 * "insert()" function
6478 */
6479 static void
6480f_insert(typval_T *argvars, typval_T *rettv)
6481{
6482 long before = 0;
6483 listitem_T *item;
6484 list_T *l;
6485 int error = FALSE;
6486
6487 if (argvars[0].v_type != VAR_LIST)
6488 EMSG2(_(e_listarg), "insert()");
6489 else if ((l = argvars[0].vval.v_list) != NULL
6490 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6491 {
6492 if (argvars[2].v_type != VAR_UNKNOWN)
6493 before = (long)get_tv_number_chk(&argvars[2], &error);
6494 if (error)
6495 return; /* type error; errmsg already given */
6496
6497 if (before == l->lv_len)
6498 item = NULL;
6499 else
6500 {
6501 item = list_find(l, before);
6502 if (item == NULL)
6503 {
6504 EMSGN(_(e_listidx), before);
6505 l = NULL;
6506 }
6507 }
6508 if (l != NULL)
6509 {
6510 list_insert_tv(l, &argvars[1], item);
6511 copy_tv(&argvars[0], rettv);
6512 }
6513 }
6514}
6515
6516/*
6517 * "invert(expr)" function
6518 */
6519 static void
6520f_invert(typval_T *argvars, typval_T *rettv)
6521{
6522 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6523}
6524
6525/*
6526 * "isdirectory()" function
6527 */
6528 static void
6529f_isdirectory(typval_T *argvars, typval_T *rettv)
6530{
6531 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6532}
6533
6534/*
6535 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6536 * or it refers to a List or Dictionary that is locked.
6537 */
6538 static int
6539tv_islocked(typval_T *tv)
6540{
6541 return (tv->v_lock & VAR_LOCKED)
6542 || (tv->v_type == VAR_LIST
6543 && tv->vval.v_list != NULL
6544 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6545 || (tv->v_type == VAR_DICT
6546 && tv->vval.v_dict != NULL
6547 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6548}
6549
6550/*
6551 * "islocked()" function
6552 */
6553 static void
6554f_islocked(typval_T *argvars, typval_T *rettv)
6555{
6556 lval_T lv;
6557 char_u *end;
6558 dictitem_T *di;
6559
6560 rettv->vval.v_number = -1;
6561 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
6562 GLV_NO_AUTOLOAD, FNE_CHECK_START);
6563 if (end != NULL && lv.ll_name != NULL)
6564 {
6565 if (*end != NUL)
6566 EMSG(_(e_trailing));
6567 else
6568 {
6569 if (lv.ll_tv == NULL)
6570 {
6571 if (check_changedtick(lv.ll_name))
6572 rettv->vval.v_number = 1; /* always locked */
6573 else
6574 {
6575 di = find_var(lv.ll_name, NULL, TRUE);
6576 if (di != NULL)
6577 {
6578 /* Consider a variable locked when:
6579 * 1. the variable itself is locked
6580 * 2. the value of the variable is locked.
6581 * 3. the List or Dict value is locked.
6582 */
6583 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6584 || tv_islocked(&di->di_tv));
6585 }
6586 }
6587 }
6588 else if (lv.ll_range)
6589 EMSG(_("E786: Range not allowed"));
6590 else if (lv.ll_newkey != NULL)
6591 EMSG2(_(e_dictkey), lv.ll_newkey);
6592 else if (lv.ll_list != NULL)
6593 /* List item. */
6594 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6595 else
6596 /* Dictionary item. */
6597 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6598 }
6599 }
6600
6601 clear_lval(&lv);
6602}
6603
6604#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6605/*
6606 * "isnan()" function
6607 */
6608 static void
6609f_isnan(typval_T *argvars, typval_T *rettv)
6610{
6611 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6612 && isnan(argvars[0].vval.v_float);
6613}
6614#endif
6615
6616/*
6617 * "items(dict)" function
6618 */
6619 static void
6620f_items(typval_T *argvars, typval_T *rettv)
6621{
6622 dict_list(argvars, rettv, 2);
6623}
6624
6625#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6626/*
6627 * Get the job from the argument.
6628 * Returns NULL if the job is invalid.
6629 */
6630 static job_T *
6631get_job_arg(typval_T *tv)
6632{
6633 job_T *job;
6634
6635 if (tv->v_type != VAR_JOB)
6636 {
6637 EMSG2(_(e_invarg2), get_tv_string(tv));
6638 return NULL;
6639 }
6640 job = tv->vval.v_job;
6641
6642 if (job == NULL)
6643 EMSG(_("E916: not a valid job"));
6644 return job;
6645}
6646
6647/*
6648 * "job_getchannel()" function
6649 */
6650 static void
6651f_job_getchannel(typval_T *argvars, typval_T *rettv)
6652{
6653 job_T *job = get_job_arg(&argvars[0]);
6654
6655 if (job != NULL)
6656 {
6657 rettv->v_type = VAR_CHANNEL;
6658 rettv->vval.v_channel = job->jv_channel;
6659 if (job->jv_channel != NULL)
6660 ++job->jv_channel->ch_refcount;
6661 }
6662}
6663
6664/*
6665 * "job_info()" function
6666 */
6667 static void
6668f_job_info(typval_T *argvars, typval_T *rettv)
6669{
6670 job_T *job = get_job_arg(&argvars[0]);
6671
6672 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6673 job_info(job, rettv->vval.v_dict);
6674}
6675
6676/*
6677 * "job_setoptions()" function
6678 */
6679 static void
6680f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6681{
6682 job_T *job = get_job_arg(&argvars[0]);
6683 jobopt_T opt;
6684
6685 if (job == NULL)
6686 return;
6687 clear_job_options(&opt);
6688 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
6689 job_set_options(job, &opt);
6690 free_job_options(&opt);
6691}
6692
6693/*
6694 * "job_start()" function
6695 */
6696 static void
6697f_job_start(typval_T *argvars, typval_T *rettv)
6698{
6699 rettv->v_type = VAR_JOB;
6700 if (check_restricted() || check_secure())
6701 return;
6702 rettv->vval.v_job = job_start(argvars);
6703}
6704
6705/*
6706 * "job_status()" function
6707 */
6708 static void
6709f_job_status(typval_T *argvars, typval_T *rettv)
6710{
6711 job_T *job = get_job_arg(&argvars[0]);
6712
6713 if (job != NULL)
6714 {
6715 rettv->v_type = VAR_STRING;
6716 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6717 }
6718}
6719
6720/*
6721 * "job_stop()" function
6722 */
6723 static void
6724f_job_stop(typval_T *argvars, typval_T *rettv)
6725{
6726 job_T *job = get_job_arg(&argvars[0]);
6727
6728 if (job != NULL)
6729 rettv->vval.v_number = job_stop(job, argvars);
6730}
6731#endif
6732
6733/*
6734 * "join()" function
6735 */
6736 static void
6737f_join(typval_T *argvars, typval_T *rettv)
6738{
6739 garray_T ga;
6740 char_u *sep;
6741
6742 if (argvars[0].v_type != VAR_LIST)
6743 {
6744 EMSG(_(e_listreq));
6745 return;
6746 }
6747 if (argvars[0].vval.v_list == NULL)
6748 return;
6749 if (argvars[1].v_type == VAR_UNKNOWN)
6750 sep = (char_u *)" ";
6751 else
6752 sep = get_tv_string_chk(&argvars[1]);
6753
6754 rettv->v_type = VAR_STRING;
6755
6756 if (sep != NULL)
6757 {
6758 ga_init2(&ga, (int)sizeof(char), 80);
6759 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6760 ga_append(&ga, NUL);
6761 rettv->vval.v_string = (char_u *)ga.ga_data;
6762 }
6763 else
6764 rettv->vval.v_string = NULL;
6765}
6766
6767/*
6768 * "js_decode()" function
6769 */
6770 static void
6771f_js_decode(typval_T *argvars, typval_T *rettv)
6772{
6773 js_read_T reader;
6774
6775 reader.js_buf = get_tv_string(&argvars[0]);
6776 reader.js_fill = NULL;
6777 reader.js_used = 0;
6778 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
6779 EMSG(_(e_invarg));
6780}
6781
6782/*
6783 * "js_encode()" function
6784 */
6785 static void
6786f_js_encode(typval_T *argvars, typval_T *rettv)
6787{
6788 rettv->v_type = VAR_STRING;
6789 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
6790}
6791
6792/*
6793 * "json_decode()" function
6794 */
6795 static void
6796f_json_decode(typval_T *argvars, typval_T *rettv)
6797{
6798 js_read_T reader;
6799
6800 reader.js_buf = get_tv_string(&argvars[0]);
6801 reader.js_fill = NULL;
6802 reader.js_used = 0;
6803 if (json_decode_all(&reader, rettv, 0) != OK)
6804 EMSG(_(e_invarg));
6805}
6806
6807/*
6808 * "json_encode()" function
6809 */
6810 static void
6811f_json_encode(typval_T *argvars, typval_T *rettv)
6812{
6813 rettv->v_type = VAR_STRING;
6814 rettv->vval.v_string = json_encode(&argvars[0], 0);
6815}
6816
6817/*
6818 * "keys()" function
6819 */
6820 static void
6821f_keys(typval_T *argvars, typval_T *rettv)
6822{
6823 dict_list(argvars, rettv, 0);
6824}
6825
6826/*
6827 * "last_buffer_nr()" function.
6828 */
6829 static void
6830f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6831{
6832 int n = 0;
6833 buf_T *buf;
6834
Bram Moolenaar29323592016-07-24 22:04:11 +02006835 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006836 if (n < buf->b_fnum)
6837 n = buf->b_fnum;
6838
6839 rettv->vval.v_number = n;
6840}
6841
6842/*
6843 * "len()" function
6844 */
6845 static void
6846f_len(typval_T *argvars, typval_T *rettv)
6847{
6848 switch (argvars[0].v_type)
6849 {
6850 case VAR_STRING:
6851 case VAR_NUMBER:
6852 rettv->vval.v_number = (varnumber_T)STRLEN(
6853 get_tv_string(&argvars[0]));
6854 break;
6855 case VAR_LIST:
6856 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6857 break;
6858 case VAR_DICT:
6859 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6860 break;
6861 case VAR_UNKNOWN:
6862 case VAR_SPECIAL:
6863 case VAR_FLOAT:
6864 case VAR_FUNC:
6865 case VAR_PARTIAL:
6866 case VAR_JOB:
6867 case VAR_CHANNEL:
6868 EMSG(_("E701: Invalid type for len()"));
6869 break;
6870 }
6871}
6872
6873static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
6874
6875 static void
6876libcall_common(typval_T *argvars, typval_T *rettv, int type)
6877{
6878#ifdef FEAT_LIBCALL
6879 char_u *string_in;
6880 char_u **string_result;
6881 int nr_result;
6882#endif
6883
6884 rettv->v_type = type;
6885 if (type != VAR_NUMBER)
6886 rettv->vval.v_string = NULL;
6887
6888 if (check_restricted() || check_secure())
6889 return;
6890
6891#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02006892 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006893 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6894 {
6895 string_in = NULL;
6896 if (argvars[2].v_type == VAR_STRING)
6897 string_in = argvars[2].vval.v_string;
6898 if (type == VAR_NUMBER)
6899 string_result = NULL;
6900 else
6901 string_result = &rettv->vval.v_string;
6902 if (mch_libcall(argvars[0].vval.v_string,
6903 argvars[1].vval.v_string,
6904 string_in,
6905 argvars[2].vval.v_number,
6906 string_result,
6907 &nr_result) == OK
6908 && type == VAR_NUMBER)
6909 rettv->vval.v_number = nr_result;
6910 }
6911#endif
6912}
6913
6914/*
6915 * "libcall()" function
6916 */
6917 static void
6918f_libcall(typval_T *argvars, typval_T *rettv)
6919{
6920 libcall_common(argvars, rettv, VAR_STRING);
6921}
6922
6923/*
6924 * "libcallnr()" function
6925 */
6926 static void
6927f_libcallnr(typval_T *argvars, typval_T *rettv)
6928{
6929 libcall_common(argvars, rettv, VAR_NUMBER);
6930}
6931
6932/*
6933 * "line(string)" function
6934 */
6935 static void
6936f_line(typval_T *argvars, typval_T *rettv)
6937{
6938 linenr_T lnum = 0;
6939 pos_T *fp;
6940 int fnum;
6941
6942 fp = var2fpos(&argvars[0], TRUE, &fnum);
6943 if (fp != NULL)
6944 lnum = fp->lnum;
6945 rettv->vval.v_number = lnum;
6946}
6947
6948/*
6949 * "line2byte(lnum)" function
6950 */
6951 static void
6952f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6953{
6954#ifndef FEAT_BYTEOFF
6955 rettv->vval.v_number = -1;
6956#else
6957 linenr_T lnum;
6958
6959 lnum = get_tv_lnum(argvars);
6960 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6961 rettv->vval.v_number = -1;
6962 else
6963 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6964 if (rettv->vval.v_number >= 0)
6965 ++rettv->vval.v_number;
6966#endif
6967}
6968
6969/*
6970 * "lispindent(lnum)" function
6971 */
6972 static void
6973f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
6974{
6975#ifdef FEAT_LISP
6976 pos_T pos;
6977 linenr_T lnum;
6978
6979 pos = curwin->w_cursor;
6980 lnum = get_tv_lnum(argvars);
6981 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6982 {
6983 curwin->w_cursor.lnum = lnum;
6984 rettv->vval.v_number = get_lisp_indent();
6985 curwin->w_cursor = pos;
6986 }
6987 else
6988#endif
6989 rettv->vval.v_number = -1;
6990}
6991
6992/*
6993 * "localtime()" function
6994 */
6995 static void
6996f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
6997{
6998 rettv->vval.v_number = (varnumber_T)time(NULL);
6999}
7000
7001static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7002
7003 static void
7004get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7005{
7006 char_u *keys;
7007 char_u *which;
7008 char_u buf[NUMBUFLEN];
7009 char_u *keys_buf = NULL;
7010 char_u *rhs;
7011 int mode;
7012 int abbr = FALSE;
7013 int get_dict = FALSE;
7014 mapblock_T *mp;
7015 int buffer_local;
7016
7017 /* return empty string for failure */
7018 rettv->v_type = VAR_STRING;
7019 rettv->vval.v_string = NULL;
7020
7021 keys = get_tv_string(&argvars[0]);
7022 if (*keys == NUL)
7023 return;
7024
7025 if (argvars[1].v_type != VAR_UNKNOWN)
7026 {
7027 which = get_tv_string_buf_chk(&argvars[1], buf);
7028 if (argvars[2].v_type != VAR_UNKNOWN)
7029 {
7030 abbr = (int)get_tv_number(&argvars[2]);
7031 if (argvars[3].v_type != VAR_UNKNOWN)
7032 get_dict = (int)get_tv_number(&argvars[3]);
7033 }
7034 }
7035 else
7036 which = (char_u *)"";
7037 if (which == NULL)
7038 return;
7039
7040 mode = get_map_mode(&which, 0);
7041
7042 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7043 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7044 vim_free(keys_buf);
7045
7046 if (!get_dict)
7047 {
7048 /* Return a string. */
7049 if (rhs != NULL)
7050 rettv->vval.v_string = str2special_save(rhs, FALSE);
7051
7052 }
7053 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7054 {
7055 /* Return a dictionary. */
7056 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7057 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7058 dict_T *dict = rettv->vval.v_dict;
7059
7060 dict_add_nr_str(dict, "lhs", 0L, lhs);
7061 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7062 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7063 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7064 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7065 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7066 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7067 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7068 dict_add_nr_str(dict, "mode", 0L, mapmode);
7069
7070 vim_free(lhs);
7071 vim_free(mapmode);
7072 }
7073}
7074
7075#ifdef FEAT_FLOAT
7076/*
7077 * "log()" function
7078 */
7079 static void
7080f_log(typval_T *argvars, typval_T *rettv)
7081{
7082 float_T f = 0.0;
7083
7084 rettv->v_type = VAR_FLOAT;
7085 if (get_float_arg(argvars, &f) == OK)
7086 rettv->vval.v_float = log(f);
7087 else
7088 rettv->vval.v_float = 0.0;
7089}
7090
7091/*
7092 * "log10()" function
7093 */
7094 static void
7095f_log10(typval_T *argvars, typval_T *rettv)
7096{
7097 float_T f = 0.0;
7098
7099 rettv->v_type = VAR_FLOAT;
7100 if (get_float_arg(argvars, &f) == OK)
7101 rettv->vval.v_float = log10(f);
7102 else
7103 rettv->vval.v_float = 0.0;
7104}
7105#endif
7106
7107#ifdef FEAT_LUA
7108/*
7109 * "luaeval()" function
7110 */
7111 static void
7112f_luaeval(typval_T *argvars, typval_T *rettv)
7113{
7114 char_u *str;
7115 char_u buf[NUMBUFLEN];
7116
7117 str = get_tv_string_buf(&argvars[0], buf);
7118 do_luaeval(str, argvars + 1, rettv);
7119}
7120#endif
7121
7122/*
7123 * "map()" function
7124 */
7125 static void
7126f_map(typval_T *argvars, typval_T *rettv)
7127{
7128 filter_map(argvars, rettv, TRUE);
7129}
7130
7131/*
7132 * "maparg()" function
7133 */
7134 static void
7135f_maparg(typval_T *argvars, typval_T *rettv)
7136{
7137 get_maparg(argvars, rettv, TRUE);
7138}
7139
7140/*
7141 * "mapcheck()" function
7142 */
7143 static void
7144f_mapcheck(typval_T *argvars, typval_T *rettv)
7145{
7146 get_maparg(argvars, rettv, FALSE);
7147}
7148
7149static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
7150
7151 static void
7152find_some_match(typval_T *argvars, typval_T *rettv, int type)
7153{
7154 char_u *str = NULL;
7155 long len = 0;
7156 char_u *expr = NULL;
7157 char_u *pat;
7158 regmatch_T regmatch;
7159 char_u patbuf[NUMBUFLEN];
7160 char_u strbuf[NUMBUFLEN];
7161 char_u *save_cpo;
7162 long start = 0;
7163 long nth = 1;
7164 colnr_T startcol = 0;
7165 int match = 0;
7166 list_T *l = NULL;
7167 listitem_T *li = NULL;
7168 long idx = 0;
7169 char_u *tofree = NULL;
7170
7171 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7172 save_cpo = p_cpo;
7173 p_cpo = (char_u *)"";
7174
7175 rettv->vval.v_number = -1;
7176 if (type == 3 || type == 4)
7177 {
7178 /* type 3: return empty list when there are no matches.
7179 * type 4: return ["", -1, -1, -1] */
7180 if (rettv_list_alloc(rettv) == FAIL)
7181 goto theend;
7182 if (type == 4
7183 && (list_append_string(rettv->vval.v_list,
7184 (char_u *)"", 0) == FAIL
7185 || list_append_number(rettv->vval.v_list,
7186 (varnumber_T)-1) == FAIL
7187 || list_append_number(rettv->vval.v_list,
7188 (varnumber_T)-1) == FAIL
7189 || list_append_number(rettv->vval.v_list,
7190 (varnumber_T)-1) == FAIL))
7191 {
7192 list_free(rettv->vval.v_list);
7193 rettv->vval.v_list = NULL;
7194 goto theend;
7195 }
7196 }
7197 else if (type == 2)
7198 {
7199 rettv->v_type = VAR_STRING;
7200 rettv->vval.v_string = NULL;
7201 }
7202
7203 if (argvars[0].v_type == VAR_LIST)
7204 {
7205 if ((l = argvars[0].vval.v_list) == NULL)
7206 goto theend;
7207 li = l->lv_first;
7208 }
7209 else
7210 {
7211 expr = str = get_tv_string(&argvars[0]);
7212 len = (long)STRLEN(str);
7213 }
7214
7215 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7216 if (pat == NULL)
7217 goto theend;
7218
7219 if (argvars[2].v_type != VAR_UNKNOWN)
7220 {
7221 int error = FALSE;
7222
7223 start = (long)get_tv_number_chk(&argvars[2], &error);
7224 if (error)
7225 goto theend;
7226 if (l != NULL)
7227 {
7228 li = list_find(l, start);
7229 if (li == NULL)
7230 goto theend;
7231 idx = l->lv_idx; /* use the cached index */
7232 }
7233 else
7234 {
7235 if (start < 0)
7236 start = 0;
7237 if (start > len)
7238 goto theend;
7239 /* When "count" argument is there ignore matches before "start",
7240 * otherwise skip part of the string. Differs when pattern is "^"
7241 * or "\<". */
7242 if (argvars[3].v_type != VAR_UNKNOWN)
7243 startcol = start;
7244 else
7245 {
7246 str += start;
7247 len -= start;
7248 }
7249 }
7250
7251 if (argvars[3].v_type != VAR_UNKNOWN)
7252 nth = (long)get_tv_number_chk(&argvars[3], &error);
7253 if (error)
7254 goto theend;
7255 }
7256
7257 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7258 if (regmatch.regprog != NULL)
7259 {
7260 regmatch.rm_ic = p_ic;
7261
7262 for (;;)
7263 {
7264 if (l != NULL)
7265 {
7266 if (li == NULL)
7267 {
7268 match = FALSE;
7269 break;
7270 }
7271 vim_free(tofree);
7272 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7273 if (str == NULL)
7274 break;
7275 }
7276
7277 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7278
7279 if (match && --nth <= 0)
7280 break;
7281 if (l == NULL && !match)
7282 break;
7283
7284 /* Advance to just after the match. */
7285 if (l != NULL)
7286 {
7287 li = li->li_next;
7288 ++idx;
7289 }
7290 else
7291 {
7292#ifdef FEAT_MBYTE
7293 startcol = (colnr_T)(regmatch.startp[0]
7294 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7295#else
7296 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7297#endif
7298 if (startcol > (colnr_T)len
7299 || str + startcol <= regmatch.startp[0])
7300 {
7301 match = FALSE;
7302 break;
7303 }
7304 }
7305 }
7306
7307 if (match)
7308 {
7309 if (type == 4)
7310 {
7311 listitem_T *li1 = rettv->vval.v_list->lv_first;
7312 listitem_T *li2 = li1->li_next;
7313 listitem_T *li3 = li2->li_next;
7314 listitem_T *li4 = li3->li_next;
7315
7316 vim_free(li1->li_tv.vval.v_string);
7317 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7318 (int)(regmatch.endp[0] - regmatch.startp[0]));
7319 li3->li_tv.vval.v_number =
7320 (varnumber_T)(regmatch.startp[0] - expr);
7321 li4->li_tv.vval.v_number =
7322 (varnumber_T)(regmatch.endp[0] - expr);
7323 if (l != NULL)
7324 li2->li_tv.vval.v_number = (varnumber_T)idx;
7325 }
7326 else if (type == 3)
7327 {
7328 int i;
7329
7330 /* return list with matched string and submatches */
7331 for (i = 0; i < NSUBEXP; ++i)
7332 {
7333 if (regmatch.endp[i] == NULL)
7334 {
7335 if (list_append_string(rettv->vval.v_list,
7336 (char_u *)"", 0) == FAIL)
7337 break;
7338 }
7339 else if (list_append_string(rettv->vval.v_list,
7340 regmatch.startp[i],
7341 (int)(regmatch.endp[i] - regmatch.startp[i]))
7342 == FAIL)
7343 break;
7344 }
7345 }
7346 else if (type == 2)
7347 {
7348 /* return matched string */
7349 if (l != NULL)
7350 copy_tv(&li->li_tv, rettv);
7351 else
7352 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7353 (int)(regmatch.endp[0] - regmatch.startp[0]));
7354 }
7355 else if (l != NULL)
7356 rettv->vval.v_number = idx;
7357 else
7358 {
7359 if (type != 0)
7360 rettv->vval.v_number =
7361 (varnumber_T)(regmatch.startp[0] - str);
7362 else
7363 rettv->vval.v_number =
7364 (varnumber_T)(regmatch.endp[0] - str);
7365 rettv->vval.v_number += (varnumber_T)(str - expr);
7366 }
7367 }
7368 vim_regfree(regmatch.regprog);
7369 }
7370
7371 if (type == 4 && l == NULL)
7372 /* matchstrpos() without a list: drop the second item. */
7373 listitem_remove(rettv->vval.v_list,
7374 rettv->vval.v_list->lv_first->li_next);
7375
7376theend:
7377 vim_free(tofree);
7378 p_cpo = save_cpo;
7379}
7380
7381/*
7382 * "match()" function
7383 */
7384 static void
7385f_match(typval_T *argvars, typval_T *rettv)
7386{
7387 find_some_match(argvars, rettv, 1);
7388}
7389
7390/*
7391 * "matchadd()" function
7392 */
7393 static void
7394f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7395{
7396#ifdef FEAT_SEARCH_EXTRA
7397 char_u buf[NUMBUFLEN];
7398 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7399 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7400 int prio = 10; /* default priority */
7401 int id = -1;
7402 int error = FALSE;
7403 char_u *conceal_char = NULL;
7404
7405 rettv->vval.v_number = -1;
7406
7407 if (grp == NULL || pat == NULL)
7408 return;
7409 if (argvars[2].v_type != VAR_UNKNOWN)
7410 {
7411 prio = (int)get_tv_number_chk(&argvars[2], &error);
7412 if (argvars[3].v_type != VAR_UNKNOWN)
7413 {
7414 id = (int)get_tv_number_chk(&argvars[3], &error);
7415 if (argvars[4].v_type != VAR_UNKNOWN)
7416 {
7417 if (argvars[4].v_type != VAR_DICT)
7418 {
7419 EMSG(_(e_dictreq));
7420 return;
7421 }
7422 if (dict_find(argvars[4].vval.v_dict,
7423 (char_u *)"conceal", -1) != NULL)
7424 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7425 (char_u *)"conceal", FALSE);
7426 }
7427 }
7428 }
7429 if (error == TRUE)
7430 return;
7431 if (id >= 1 && id <= 3)
7432 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007433 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007434 return;
7435 }
7436
7437 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7438 conceal_char);
7439#endif
7440}
7441
7442/*
7443 * "matchaddpos()" function
7444 */
7445 static void
7446f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7447{
7448#ifdef FEAT_SEARCH_EXTRA
7449 char_u buf[NUMBUFLEN];
7450 char_u *group;
7451 int prio = 10;
7452 int id = -1;
7453 int error = FALSE;
7454 list_T *l;
7455 char_u *conceal_char = NULL;
7456
7457 rettv->vval.v_number = -1;
7458
7459 group = get_tv_string_buf_chk(&argvars[0], buf);
7460 if (group == NULL)
7461 return;
7462
7463 if (argvars[1].v_type != VAR_LIST)
7464 {
7465 EMSG2(_(e_listarg), "matchaddpos()");
7466 return;
7467 }
7468 l = argvars[1].vval.v_list;
7469 if (l == NULL)
7470 return;
7471
7472 if (argvars[2].v_type != VAR_UNKNOWN)
7473 {
7474 prio = (int)get_tv_number_chk(&argvars[2], &error);
7475 if (argvars[3].v_type != VAR_UNKNOWN)
7476 {
7477 id = (int)get_tv_number_chk(&argvars[3], &error);
7478 if (argvars[4].v_type != VAR_UNKNOWN)
7479 {
7480 if (argvars[4].v_type != VAR_DICT)
7481 {
7482 EMSG(_(e_dictreq));
7483 return;
7484 }
7485 if (dict_find(argvars[4].vval.v_dict,
7486 (char_u *)"conceal", -1) != NULL)
7487 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7488 (char_u *)"conceal", FALSE);
7489 }
7490 }
7491 }
7492 if (error == TRUE)
7493 return;
7494
7495 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7496 if (id == 1 || id == 2)
7497 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007498 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007499 return;
7500 }
7501
7502 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7503 conceal_char);
7504#endif
7505}
7506
7507/*
7508 * "matcharg()" function
7509 */
7510 static void
7511f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7512{
7513 if (rettv_list_alloc(rettv) == OK)
7514 {
7515#ifdef FEAT_SEARCH_EXTRA
7516 int id = (int)get_tv_number(&argvars[0]);
7517 matchitem_T *m;
7518
7519 if (id >= 1 && id <= 3)
7520 {
7521 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7522 {
7523 list_append_string(rettv->vval.v_list,
7524 syn_id2name(m->hlg_id), -1);
7525 list_append_string(rettv->vval.v_list, m->pattern, -1);
7526 }
7527 else
7528 {
7529 list_append_string(rettv->vval.v_list, NULL, -1);
7530 list_append_string(rettv->vval.v_list, NULL, -1);
7531 }
7532 }
7533#endif
7534 }
7535}
7536
7537/*
7538 * "matchdelete()" function
7539 */
7540 static void
7541f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7542{
7543#ifdef FEAT_SEARCH_EXTRA
7544 rettv->vval.v_number = match_delete(curwin,
7545 (int)get_tv_number(&argvars[0]), TRUE);
7546#endif
7547}
7548
7549/*
7550 * "matchend()" function
7551 */
7552 static void
7553f_matchend(typval_T *argvars, typval_T *rettv)
7554{
7555 find_some_match(argvars, rettv, 0);
7556}
7557
7558/*
7559 * "matchlist()" function
7560 */
7561 static void
7562f_matchlist(typval_T *argvars, typval_T *rettv)
7563{
7564 find_some_match(argvars, rettv, 3);
7565}
7566
7567/*
7568 * "matchstr()" function
7569 */
7570 static void
7571f_matchstr(typval_T *argvars, typval_T *rettv)
7572{
7573 find_some_match(argvars, rettv, 2);
7574}
7575
7576/*
7577 * "matchstrpos()" function
7578 */
7579 static void
7580f_matchstrpos(typval_T *argvars, typval_T *rettv)
7581{
7582 find_some_match(argvars, rettv, 4);
7583}
7584
7585static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7586
7587 static void
7588max_min(typval_T *argvars, typval_T *rettv, int domax)
7589{
7590 varnumber_T n = 0;
7591 varnumber_T i;
7592 int error = FALSE;
7593
7594 if (argvars[0].v_type == VAR_LIST)
7595 {
7596 list_T *l;
7597 listitem_T *li;
7598
7599 l = argvars[0].vval.v_list;
7600 if (l != NULL)
7601 {
7602 li = l->lv_first;
7603 if (li != NULL)
7604 {
7605 n = get_tv_number_chk(&li->li_tv, &error);
7606 for (;;)
7607 {
7608 li = li->li_next;
7609 if (li == NULL)
7610 break;
7611 i = get_tv_number_chk(&li->li_tv, &error);
7612 if (domax ? i > n : i < n)
7613 n = i;
7614 }
7615 }
7616 }
7617 }
7618 else if (argvars[0].v_type == VAR_DICT)
7619 {
7620 dict_T *d;
7621 int first = TRUE;
7622 hashitem_T *hi;
7623 int todo;
7624
7625 d = argvars[0].vval.v_dict;
7626 if (d != NULL)
7627 {
7628 todo = (int)d->dv_hashtab.ht_used;
7629 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7630 {
7631 if (!HASHITEM_EMPTY(hi))
7632 {
7633 --todo;
7634 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7635 if (first)
7636 {
7637 n = i;
7638 first = FALSE;
7639 }
7640 else if (domax ? i > n : i < n)
7641 n = i;
7642 }
7643 }
7644 }
7645 }
7646 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007647 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007648 rettv->vval.v_number = error ? 0 : n;
7649}
7650
7651/*
7652 * "max()" function
7653 */
7654 static void
7655f_max(typval_T *argvars, typval_T *rettv)
7656{
7657 max_min(argvars, rettv, TRUE);
7658}
7659
7660/*
7661 * "min()" function
7662 */
7663 static void
7664f_min(typval_T *argvars, typval_T *rettv)
7665{
7666 max_min(argvars, rettv, FALSE);
7667}
7668
7669static int mkdir_recurse(char_u *dir, int prot);
7670
7671/*
7672 * Create the directory in which "dir" is located, and higher levels when
7673 * needed.
7674 */
7675 static int
7676mkdir_recurse(char_u *dir, int prot)
7677{
7678 char_u *p;
7679 char_u *updir;
7680 int r = FAIL;
7681
7682 /* Get end of directory name in "dir".
7683 * We're done when it's "/" or "c:/". */
7684 p = gettail_sep(dir);
7685 if (p <= get_past_head(dir))
7686 return OK;
7687
7688 /* If the directory exists we're done. Otherwise: create it.*/
7689 updir = vim_strnsave(dir, (int)(p - dir));
7690 if (updir == NULL)
7691 return FAIL;
7692 if (mch_isdir(updir))
7693 r = OK;
7694 else if (mkdir_recurse(updir, prot) == OK)
7695 r = vim_mkdir_emsg(updir, prot);
7696 vim_free(updir);
7697 return r;
7698}
7699
7700#ifdef vim_mkdir
7701/*
7702 * "mkdir()" function
7703 */
7704 static void
7705f_mkdir(typval_T *argvars, typval_T *rettv)
7706{
7707 char_u *dir;
7708 char_u buf[NUMBUFLEN];
7709 int prot = 0755;
7710
7711 rettv->vval.v_number = FAIL;
7712 if (check_restricted() || check_secure())
7713 return;
7714
7715 dir = get_tv_string_buf(&argvars[0], buf);
7716 if (*dir == NUL)
7717 rettv->vval.v_number = FAIL;
7718 else
7719 {
7720 if (*gettail(dir) == NUL)
7721 /* remove trailing slashes */
7722 *gettail_sep(dir) = NUL;
7723
7724 if (argvars[1].v_type != VAR_UNKNOWN)
7725 {
7726 if (argvars[2].v_type != VAR_UNKNOWN)
7727 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7728 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7729 mkdir_recurse(dir, prot);
7730 }
7731 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7732 }
7733}
7734#endif
7735
7736/*
7737 * "mode()" function
7738 */
7739 static void
7740f_mode(typval_T *argvars, typval_T *rettv)
7741{
7742 char_u buf[3];
7743
7744 buf[1] = NUL;
7745 buf[2] = NUL;
7746
7747 if (time_for_testing == 93784)
7748 {
7749 /* Testing the two-character code. */
7750 buf[0] = 'x';
7751 buf[1] = '!';
7752 }
7753 else if (VIsual_active)
7754 {
7755 if (VIsual_select)
7756 buf[0] = VIsual_mode + 's' - 'v';
7757 else
7758 buf[0] = VIsual_mode;
7759 }
7760 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7761 || State == CONFIRM)
7762 {
7763 buf[0] = 'r';
7764 if (State == ASKMORE)
7765 buf[1] = 'm';
7766 else if (State == CONFIRM)
7767 buf[1] = '?';
7768 }
7769 else if (State == EXTERNCMD)
7770 buf[0] = '!';
7771 else if (State & INSERT)
7772 {
7773#ifdef FEAT_VREPLACE
7774 if (State & VREPLACE_FLAG)
7775 {
7776 buf[0] = 'R';
7777 buf[1] = 'v';
7778 }
7779 else
7780#endif
7781 if (State & REPLACE_FLAG)
7782 buf[0] = 'R';
7783 else
7784 buf[0] = 'i';
7785 }
7786 else if (State & CMDLINE)
7787 {
7788 buf[0] = 'c';
7789 if (exmode_active)
7790 buf[1] = 'v';
7791 }
7792 else if (exmode_active)
7793 {
7794 buf[0] = 'c';
7795 buf[1] = 'e';
7796 }
7797 else
7798 {
7799 buf[0] = 'n';
7800 if (finish_op)
7801 buf[1] = 'o';
7802 }
7803
7804 /* Clear out the minor mode when the argument is not a non-zero number or
7805 * non-empty string. */
7806 if (!non_zero_arg(&argvars[0]))
7807 buf[1] = NUL;
7808
7809 rettv->vval.v_string = vim_strsave(buf);
7810 rettv->v_type = VAR_STRING;
7811}
7812
7813#if defined(FEAT_MZSCHEME) || defined(PROTO)
7814/*
7815 * "mzeval()" function
7816 */
7817 static void
7818f_mzeval(typval_T *argvars, typval_T *rettv)
7819{
7820 char_u *str;
7821 char_u buf[NUMBUFLEN];
7822
7823 str = get_tv_string_buf(&argvars[0], buf);
7824 do_mzeval(str, rettv);
7825}
7826
7827 void
7828mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
7829{
7830 typval_T argvars[3];
7831
7832 argvars[0].v_type = VAR_STRING;
7833 argvars[0].vval.v_string = name;
7834 copy_tv(args, &argvars[1]);
7835 argvars[2].v_type = VAR_UNKNOWN;
7836 f_call(argvars, rettv);
7837 clear_tv(&argvars[1]);
7838}
7839#endif
7840
7841/*
7842 * "nextnonblank()" function
7843 */
7844 static void
7845f_nextnonblank(typval_T *argvars, typval_T *rettv)
7846{
7847 linenr_T lnum;
7848
7849 for (lnum = get_tv_lnum(argvars); ; ++lnum)
7850 {
7851 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
7852 {
7853 lnum = 0;
7854 break;
7855 }
7856 if (*skipwhite(ml_get(lnum)) != NUL)
7857 break;
7858 }
7859 rettv->vval.v_number = lnum;
7860}
7861
7862/*
7863 * "nr2char()" function
7864 */
7865 static void
7866f_nr2char(typval_T *argvars, typval_T *rettv)
7867{
7868 char_u buf[NUMBUFLEN];
7869
7870#ifdef FEAT_MBYTE
7871 if (has_mbyte)
7872 {
7873 int utf8 = 0;
7874
7875 if (argvars[1].v_type != VAR_UNKNOWN)
7876 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
7877 if (utf8)
7878 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7879 else
7880 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7881 }
7882 else
7883#endif
7884 {
7885 buf[0] = (char_u)get_tv_number(&argvars[0]);
7886 buf[1] = NUL;
7887 }
7888 rettv->v_type = VAR_STRING;
7889 rettv->vval.v_string = vim_strsave(buf);
7890}
7891
7892/*
7893 * "or(expr, expr)" function
7894 */
7895 static void
7896f_or(typval_T *argvars, typval_T *rettv)
7897{
7898 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
7899 | get_tv_number_chk(&argvars[1], NULL);
7900}
7901
7902/*
7903 * "pathshorten()" function
7904 */
7905 static void
7906f_pathshorten(typval_T *argvars, typval_T *rettv)
7907{
7908 char_u *p;
7909
7910 rettv->v_type = VAR_STRING;
7911 p = get_tv_string_chk(&argvars[0]);
7912 if (p == NULL)
7913 rettv->vval.v_string = NULL;
7914 else
7915 {
7916 p = vim_strsave(p);
7917 rettv->vval.v_string = p;
7918 if (p != NULL)
7919 shorten_dir(p);
7920 }
7921}
7922
7923#ifdef FEAT_PERL
7924/*
7925 * "perleval()" function
7926 */
7927 static void
7928f_perleval(typval_T *argvars, typval_T *rettv)
7929{
7930 char_u *str;
7931 char_u buf[NUMBUFLEN];
7932
7933 str = get_tv_string_buf(&argvars[0], buf);
7934 do_perleval(str, rettv);
7935}
7936#endif
7937
7938#ifdef FEAT_FLOAT
7939/*
7940 * "pow()" function
7941 */
7942 static void
7943f_pow(typval_T *argvars, typval_T *rettv)
7944{
7945 float_T fx = 0.0, fy = 0.0;
7946
7947 rettv->v_type = VAR_FLOAT;
7948 if (get_float_arg(argvars, &fx) == OK
7949 && get_float_arg(&argvars[1], &fy) == OK)
7950 rettv->vval.v_float = pow(fx, fy);
7951 else
7952 rettv->vval.v_float = 0.0;
7953}
7954#endif
7955
7956/*
7957 * "prevnonblank()" function
7958 */
7959 static void
7960f_prevnonblank(typval_T *argvars, typval_T *rettv)
7961{
7962 linenr_T lnum;
7963
7964 lnum = get_tv_lnum(argvars);
7965 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7966 lnum = 0;
7967 else
7968 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7969 --lnum;
7970 rettv->vval.v_number = lnum;
7971}
7972
7973/* This dummy va_list is here because:
7974 * - passing a NULL pointer doesn't work when va_list isn't a pointer
7975 * - locally in the function results in a "used before set" warning
7976 * - using va_start() to initialize it gives "function with fixed args" error */
7977static va_list ap;
7978
7979/*
7980 * "printf()" function
7981 */
7982 static void
7983f_printf(typval_T *argvars, typval_T *rettv)
7984{
7985 char_u buf[NUMBUFLEN];
7986 int len;
7987 char_u *s;
7988 int saved_did_emsg = did_emsg;
7989 char *fmt;
7990
7991 rettv->v_type = VAR_STRING;
7992 rettv->vval.v_string = NULL;
7993
7994 /* Get the required length, allocate the buffer and do it for real. */
7995 did_emsg = FALSE;
7996 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
7997 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
7998 if (!did_emsg)
7999 {
8000 s = alloc(len + 1);
8001 if (s != NULL)
8002 {
8003 rettv->vval.v_string = s;
8004 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
8005 }
8006 }
8007 did_emsg |= saved_did_emsg;
8008}
8009
8010/*
8011 * "pumvisible()" function
8012 */
8013 static void
8014f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8015{
8016#ifdef FEAT_INS_EXPAND
8017 if (pum_visible())
8018 rettv->vval.v_number = 1;
8019#endif
8020}
8021
8022#ifdef FEAT_PYTHON3
8023/*
8024 * "py3eval()" function
8025 */
8026 static void
8027f_py3eval(typval_T *argvars, typval_T *rettv)
8028{
8029 char_u *str;
8030 char_u buf[NUMBUFLEN];
8031
8032 str = get_tv_string_buf(&argvars[0], buf);
8033 do_py3eval(str, rettv);
8034}
8035#endif
8036
8037#ifdef FEAT_PYTHON
8038/*
8039 * "pyeval()" function
8040 */
8041 static void
8042f_pyeval(typval_T *argvars, typval_T *rettv)
8043{
8044 char_u *str;
8045 char_u buf[NUMBUFLEN];
8046
8047 str = get_tv_string_buf(&argvars[0], buf);
8048 do_pyeval(str, rettv);
8049}
8050#endif
8051
8052/*
8053 * "range()" function
8054 */
8055 static void
8056f_range(typval_T *argvars, typval_T *rettv)
8057{
8058 varnumber_T start;
8059 varnumber_T end;
8060 varnumber_T stride = 1;
8061 varnumber_T i;
8062 int error = FALSE;
8063
8064 start = get_tv_number_chk(&argvars[0], &error);
8065 if (argvars[1].v_type == VAR_UNKNOWN)
8066 {
8067 end = start - 1;
8068 start = 0;
8069 }
8070 else
8071 {
8072 end = get_tv_number_chk(&argvars[1], &error);
8073 if (argvars[2].v_type != VAR_UNKNOWN)
8074 stride = get_tv_number_chk(&argvars[2], &error);
8075 }
8076
8077 if (error)
8078 return; /* type error; errmsg already given */
8079 if (stride == 0)
8080 EMSG(_("E726: Stride is zero"));
8081 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8082 EMSG(_("E727: Start past end"));
8083 else
8084 {
8085 if (rettv_list_alloc(rettv) == OK)
8086 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8087 if (list_append_number(rettv->vval.v_list,
8088 (varnumber_T)i) == FAIL)
8089 break;
8090 }
8091}
8092
8093/*
8094 * "readfile()" function
8095 */
8096 static void
8097f_readfile(typval_T *argvars, typval_T *rettv)
8098{
8099 int binary = FALSE;
8100 int failed = FALSE;
8101 char_u *fname;
8102 FILE *fd;
8103 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8104 int io_size = sizeof(buf);
8105 int readlen; /* size of last fread() */
8106 char_u *prev = NULL; /* previously read bytes, if any */
8107 long prevlen = 0; /* length of data in prev */
8108 long prevsize = 0; /* size of prev buffer */
8109 long maxline = MAXLNUM;
8110 long cnt = 0;
8111 char_u *p; /* position in buf */
8112 char_u *start; /* start of current line */
8113
8114 if (argvars[1].v_type != VAR_UNKNOWN)
8115 {
8116 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8117 binary = TRUE;
8118 if (argvars[2].v_type != VAR_UNKNOWN)
8119 maxline = (long)get_tv_number(&argvars[2]);
8120 }
8121
8122 if (rettv_list_alloc(rettv) == FAIL)
8123 return;
8124
8125 /* Always open the file in binary mode, library functions have a mind of
8126 * their own about CR-LF conversion. */
8127 fname = get_tv_string(&argvars[0]);
8128 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8129 {
8130 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8131 return;
8132 }
8133
8134 while (cnt < maxline || maxline < 0)
8135 {
8136 readlen = (int)fread(buf, 1, io_size, fd);
8137
8138 /* This for loop processes what was read, but is also entered at end
8139 * of file so that either:
8140 * - an incomplete line gets written
8141 * - a "binary" file gets an empty line at the end if it ends in a
8142 * newline. */
8143 for (p = buf, start = buf;
8144 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8145 ++p)
8146 {
8147 if (*p == '\n' || readlen <= 0)
8148 {
8149 listitem_T *li;
8150 char_u *s = NULL;
8151 long_u len = p - start;
8152
8153 /* Finished a line. Remove CRs before NL. */
8154 if (readlen > 0 && !binary)
8155 {
8156 while (len > 0 && start[len - 1] == '\r')
8157 --len;
8158 /* removal may cross back to the "prev" string */
8159 if (len == 0)
8160 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8161 --prevlen;
8162 }
8163 if (prevlen == 0)
8164 s = vim_strnsave(start, (int)len);
8165 else
8166 {
8167 /* Change "prev" buffer to be the right size. This way
8168 * the bytes are only copied once, and very long lines are
8169 * allocated only once. */
8170 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8171 {
8172 mch_memmove(s + prevlen, start, len);
8173 s[prevlen + len] = NUL;
8174 prev = NULL; /* the list will own the string */
8175 prevlen = prevsize = 0;
8176 }
8177 }
8178 if (s == NULL)
8179 {
8180 do_outofmem_msg((long_u) prevlen + len + 1);
8181 failed = TRUE;
8182 break;
8183 }
8184
8185 if ((li = listitem_alloc()) == NULL)
8186 {
8187 vim_free(s);
8188 failed = TRUE;
8189 break;
8190 }
8191 li->li_tv.v_type = VAR_STRING;
8192 li->li_tv.v_lock = 0;
8193 li->li_tv.vval.v_string = s;
8194 list_append(rettv->vval.v_list, li);
8195
8196 start = p + 1; /* step over newline */
8197 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8198 break;
8199 }
8200 else if (*p == NUL)
8201 *p = '\n';
8202#ifdef FEAT_MBYTE
8203 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8204 * when finding the BF and check the previous two bytes. */
8205 else if (*p == 0xbf && enc_utf8 && !binary)
8206 {
8207 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8208 * + 1, these may be in the "prev" string. */
8209 char_u back1 = p >= buf + 1 ? p[-1]
8210 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8211 char_u back2 = p >= buf + 2 ? p[-2]
8212 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8213 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8214
8215 if (back2 == 0xef && back1 == 0xbb)
8216 {
8217 char_u *dest = p - 2;
8218
8219 /* Usually a BOM is at the beginning of a file, and so at
8220 * the beginning of a line; then we can just step over it.
8221 */
8222 if (start == dest)
8223 start = p + 1;
8224 else
8225 {
8226 /* have to shuffle buf to close gap */
8227 int adjust_prevlen = 0;
8228
8229 if (dest < buf)
8230 {
8231 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8232 dest = buf;
8233 }
8234 if (readlen > p - buf + 1)
8235 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8236 readlen -= 3 - adjust_prevlen;
8237 prevlen -= adjust_prevlen;
8238 p = dest - 1;
8239 }
8240 }
8241 }
8242#endif
8243 } /* for */
8244
8245 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8246 break;
8247 if (start < p)
8248 {
8249 /* There's part of a line in buf, store it in "prev". */
8250 if (p - start + prevlen >= prevsize)
8251 {
8252 /* need bigger "prev" buffer */
8253 char_u *newprev;
8254
8255 /* A common use case is ordinary text files and "prev" gets a
8256 * fragment of a line, so the first allocation is made
8257 * small, to avoid repeatedly 'allocing' large and
8258 * 'reallocing' small. */
8259 if (prevsize == 0)
8260 prevsize = (long)(p - start);
8261 else
8262 {
8263 long grow50pc = (prevsize * 3) / 2;
8264 long growmin = (long)((p - start) * 2 + prevlen);
8265 prevsize = grow50pc > growmin ? grow50pc : growmin;
8266 }
8267 newprev = prev == NULL ? alloc(prevsize)
8268 : vim_realloc(prev, prevsize);
8269 if (newprev == NULL)
8270 {
8271 do_outofmem_msg((long_u)prevsize);
8272 failed = TRUE;
8273 break;
8274 }
8275 prev = newprev;
8276 }
8277 /* Add the line part to end of "prev". */
8278 mch_memmove(prev + prevlen, start, p - start);
8279 prevlen += (long)(p - start);
8280 }
8281 } /* while */
8282
8283 /*
8284 * For a negative line count use only the lines at the end of the file,
8285 * free the rest.
8286 */
8287 if (!failed && maxline < 0)
8288 while (cnt > -maxline)
8289 {
8290 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8291 --cnt;
8292 }
8293
8294 if (failed)
8295 {
8296 list_free(rettv->vval.v_list);
8297 /* readfile doc says an empty list is returned on error */
8298 rettv->vval.v_list = list_alloc();
8299 }
8300
8301 vim_free(prev);
8302 fclose(fd);
8303}
8304
8305#if defined(FEAT_RELTIME)
8306static int list2proftime(typval_T *arg, proftime_T *tm);
8307
8308/*
8309 * Convert a List to proftime_T.
8310 * Return FAIL when there is something wrong.
8311 */
8312 static int
8313list2proftime(typval_T *arg, proftime_T *tm)
8314{
8315 long n1, n2;
8316 int error = FALSE;
8317
8318 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8319 || arg->vval.v_list->lv_len != 2)
8320 return FAIL;
8321 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8322 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8323# ifdef WIN3264
8324 tm->HighPart = n1;
8325 tm->LowPart = n2;
8326# else
8327 tm->tv_sec = n1;
8328 tm->tv_usec = n2;
8329# endif
8330 return error ? FAIL : OK;
8331}
8332#endif /* FEAT_RELTIME */
8333
8334/*
8335 * "reltime()" function
8336 */
8337 static void
8338f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8339{
8340#ifdef FEAT_RELTIME
8341 proftime_T res;
8342 proftime_T start;
8343
8344 if (argvars[0].v_type == VAR_UNKNOWN)
8345 {
8346 /* No arguments: get current time. */
8347 profile_start(&res);
8348 }
8349 else if (argvars[1].v_type == VAR_UNKNOWN)
8350 {
8351 if (list2proftime(&argvars[0], &res) == FAIL)
8352 return;
8353 profile_end(&res);
8354 }
8355 else
8356 {
8357 /* Two arguments: compute the difference. */
8358 if (list2proftime(&argvars[0], &start) == FAIL
8359 || list2proftime(&argvars[1], &res) == FAIL)
8360 return;
8361 profile_sub(&res, &start);
8362 }
8363
8364 if (rettv_list_alloc(rettv) == OK)
8365 {
8366 long n1, n2;
8367
8368# ifdef WIN3264
8369 n1 = res.HighPart;
8370 n2 = res.LowPart;
8371# else
8372 n1 = res.tv_sec;
8373 n2 = res.tv_usec;
8374# endif
8375 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8376 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8377 }
8378#endif
8379}
8380
8381#ifdef FEAT_FLOAT
8382/*
8383 * "reltimefloat()" function
8384 */
8385 static void
8386f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8387{
8388# ifdef FEAT_RELTIME
8389 proftime_T tm;
8390# endif
8391
8392 rettv->v_type = VAR_FLOAT;
8393 rettv->vval.v_float = 0;
8394# ifdef FEAT_RELTIME
8395 if (list2proftime(&argvars[0], &tm) == OK)
8396 rettv->vval.v_float = profile_float(&tm);
8397# endif
8398}
8399#endif
8400
8401/*
8402 * "reltimestr()" function
8403 */
8404 static void
8405f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8406{
8407#ifdef FEAT_RELTIME
8408 proftime_T tm;
8409#endif
8410
8411 rettv->v_type = VAR_STRING;
8412 rettv->vval.v_string = NULL;
8413#ifdef FEAT_RELTIME
8414 if (list2proftime(&argvars[0], &tm) == OK)
8415 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8416#endif
8417}
8418
8419#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8420static void make_connection(void);
8421static int check_connection(void);
8422
8423 static void
8424make_connection(void)
8425{
8426 if (X_DISPLAY == NULL
8427# ifdef FEAT_GUI
8428 && !gui.in_use
8429# endif
8430 )
8431 {
8432 x_force_connect = TRUE;
8433 setup_term_clip();
8434 x_force_connect = FALSE;
8435 }
8436}
8437
8438 static int
8439check_connection(void)
8440{
8441 make_connection();
8442 if (X_DISPLAY == NULL)
8443 {
8444 EMSG(_("E240: No connection to Vim server"));
8445 return FAIL;
8446 }
8447 return OK;
8448}
8449#endif
8450
8451#ifdef FEAT_CLIENTSERVER
8452 static void
8453remote_common(typval_T *argvars, typval_T *rettv, int expr)
8454{
8455 char_u *server_name;
8456 char_u *keys;
8457 char_u *r = NULL;
8458 char_u buf[NUMBUFLEN];
8459# ifdef WIN32
8460 HWND w;
8461# else
8462 Window w;
8463# endif
8464
8465 if (check_restricted() || check_secure())
8466 return;
8467
8468# ifdef FEAT_X11
8469 if (check_connection() == FAIL)
8470 return;
8471# endif
8472
8473 server_name = get_tv_string_chk(&argvars[0]);
8474 if (server_name == NULL)
8475 return; /* type error; errmsg already given */
8476 keys = get_tv_string_buf(&argvars[1], buf);
8477# ifdef WIN32
8478 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
8479# else
8480 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
8481 < 0)
8482# endif
8483 {
8484 if (r != NULL)
8485 EMSG(r); /* sending worked but evaluation failed */
8486 else
8487 EMSG2(_("E241: Unable to send to %s"), server_name);
8488 return;
8489 }
8490
8491 rettv->vval.v_string = r;
8492
8493 if (argvars[2].v_type != VAR_UNKNOWN)
8494 {
8495 dictitem_T v;
8496 char_u str[30];
8497 char_u *idvar;
8498
8499 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8500 v.di_tv.v_type = VAR_STRING;
8501 v.di_tv.vval.v_string = vim_strsave(str);
8502 idvar = get_tv_string_chk(&argvars[2]);
8503 if (idvar != NULL)
8504 set_var(idvar, &v.di_tv, FALSE);
8505 vim_free(v.di_tv.vval.v_string);
8506 }
8507}
8508#endif
8509
8510/*
8511 * "remote_expr()" function
8512 */
8513 static void
8514f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8515{
8516 rettv->v_type = VAR_STRING;
8517 rettv->vval.v_string = NULL;
8518#ifdef FEAT_CLIENTSERVER
8519 remote_common(argvars, rettv, TRUE);
8520#endif
8521}
8522
8523/*
8524 * "remote_foreground()" function
8525 */
8526 static void
8527f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8528{
8529#ifdef FEAT_CLIENTSERVER
8530# ifdef WIN32
8531 /* On Win32 it's done in this application. */
8532 {
8533 char_u *server_name = get_tv_string_chk(&argvars[0]);
8534
8535 if (server_name != NULL)
8536 serverForeground(server_name);
8537 }
8538# else
8539 /* Send a foreground() expression to the server. */
8540 argvars[1].v_type = VAR_STRING;
8541 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8542 argvars[2].v_type = VAR_UNKNOWN;
8543 remote_common(argvars, rettv, TRUE);
8544 vim_free(argvars[1].vval.v_string);
8545# endif
8546#endif
8547}
8548
8549 static void
8550f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8551{
8552#ifdef FEAT_CLIENTSERVER
8553 dictitem_T v;
8554 char_u *s = NULL;
8555# ifdef WIN32
8556 long_u n = 0;
8557# endif
8558 char_u *serverid;
8559
8560 if (check_restricted() || check_secure())
8561 {
8562 rettv->vval.v_number = -1;
8563 return;
8564 }
8565 serverid = get_tv_string_chk(&argvars[0]);
8566 if (serverid == NULL)
8567 {
8568 rettv->vval.v_number = -1;
8569 return; /* type error; errmsg already given */
8570 }
8571# ifdef WIN32
8572 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8573 if (n == 0)
8574 rettv->vval.v_number = -1;
8575 else
8576 {
8577 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
8578 rettv->vval.v_number = (s != NULL);
8579 }
8580# else
8581 if (check_connection() == FAIL)
8582 return;
8583
8584 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8585 serverStrToWin(serverid), &s);
8586# endif
8587
8588 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8589 {
8590 char_u *retvar;
8591
8592 v.di_tv.v_type = VAR_STRING;
8593 v.di_tv.vval.v_string = vim_strsave(s);
8594 retvar = get_tv_string_chk(&argvars[1]);
8595 if (retvar != NULL)
8596 set_var(retvar, &v.di_tv, FALSE);
8597 vim_free(v.di_tv.vval.v_string);
8598 }
8599#else
8600 rettv->vval.v_number = -1;
8601#endif
8602}
8603
8604 static void
8605f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8606{
8607 char_u *r = NULL;
8608
8609#ifdef FEAT_CLIENTSERVER
8610 char_u *serverid = get_tv_string_chk(&argvars[0]);
8611
8612 if (serverid != NULL && !check_restricted() && !check_secure())
8613 {
8614# ifdef WIN32
8615 /* The server's HWND is encoded in the 'id' parameter */
8616 long_u n = 0;
8617
8618 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8619 if (n != 0)
8620 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
8621 if (r == NULL)
8622# else
8623 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
8624 serverStrToWin(serverid), &r, FALSE) < 0)
8625# endif
8626 EMSG(_("E277: Unable to read a server reply"));
8627 }
8628#endif
8629 rettv->v_type = VAR_STRING;
8630 rettv->vval.v_string = r;
8631}
8632
8633/*
8634 * "remote_send()" function
8635 */
8636 static void
8637f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8638{
8639 rettv->v_type = VAR_STRING;
8640 rettv->vval.v_string = NULL;
8641#ifdef FEAT_CLIENTSERVER
8642 remote_common(argvars, rettv, FALSE);
8643#endif
8644}
8645
8646/*
8647 * "remove()" function
8648 */
8649 static void
8650f_remove(typval_T *argvars, typval_T *rettv)
8651{
8652 list_T *l;
8653 listitem_T *item, *item2;
8654 listitem_T *li;
8655 long idx;
8656 long end;
8657 char_u *key;
8658 dict_T *d;
8659 dictitem_T *di;
8660 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8661
8662 if (argvars[0].v_type == VAR_DICT)
8663 {
8664 if (argvars[2].v_type != VAR_UNKNOWN)
8665 EMSG2(_(e_toomanyarg), "remove()");
8666 else if ((d = argvars[0].vval.v_dict) != NULL
8667 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8668 {
8669 key = get_tv_string_chk(&argvars[1]);
8670 if (key != NULL)
8671 {
8672 di = dict_find(d, key, -1);
8673 if (di == NULL)
8674 EMSG2(_(e_dictkey), key);
8675 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8676 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8677 {
8678 *rettv = di->di_tv;
8679 init_tv(&di->di_tv);
8680 dictitem_remove(d, di);
8681 }
8682 }
8683 }
8684 }
8685 else if (argvars[0].v_type != VAR_LIST)
8686 EMSG2(_(e_listdictarg), "remove()");
8687 else if ((l = argvars[0].vval.v_list) != NULL
8688 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
8689 {
8690 int error = FALSE;
8691
8692 idx = (long)get_tv_number_chk(&argvars[1], &error);
8693 if (error)
8694 ; /* type error: do nothing, errmsg already given */
8695 else if ((item = list_find(l, idx)) == NULL)
8696 EMSGN(_(e_listidx), idx);
8697 else
8698 {
8699 if (argvars[2].v_type == VAR_UNKNOWN)
8700 {
8701 /* Remove one item, return its value. */
8702 vimlist_remove(l, item, item);
8703 *rettv = item->li_tv;
8704 vim_free(item);
8705 }
8706 else
8707 {
8708 /* Remove range of items, return list with values. */
8709 end = (long)get_tv_number_chk(&argvars[2], &error);
8710 if (error)
8711 ; /* type error: do nothing */
8712 else if ((item2 = list_find(l, end)) == NULL)
8713 EMSGN(_(e_listidx), end);
8714 else
8715 {
8716 int cnt = 0;
8717
8718 for (li = item; li != NULL; li = li->li_next)
8719 {
8720 ++cnt;
8721 if (li == item2)
8722 break;
8723 }
8724 if (li == NULL) /* didn't find "item2" after "item" */
8725 EMSG(_(e_invrange));
8726 else
8727 {
8728 vimlist_remove(l, item, item2);
8729 if (rettv_list_alloc(rettv) == OK)
8730 {
8731 l = rettv->vval.v_list;
8732 l->lv_first = item;
8733 l->lv_last = item2;
8734 item->li_prev = NULL;
8735 item2->li_next = NULL;
8736 l->lv_len = cnt;
8737 }
8738 }
8739 }
8740 }
8741 }
8742 }
8743}
8744
8745/*
8746 * "rename({from}, {to})" function
8747 */
8748 static void
8749f_rename(typval_T *argvars, typval_T *rettv)
8750{
8751 char_u buf[NUMBUFLEN];
8752
8753 if (check_restricted() || check_secure())
8754 rettv->vval.v_number = -1;
8755 else
8756 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
8757 get_tv_string_buf(&argvars[1], buf));
8758}
8759
8760/*
8761 * "repeat()" function
8762 */
8763 static void
8764f_repeat(typval_T *argvars, typval_T *rettv)
8765{
8766 char_u *p;
8767 int n;
8768 int slen;
8769 int len;
8770 char_u *r;
8771 int i;
8772
8773 n = (int)get_tv_number(&argvars[1]);
8774 if (argvars[0].v_type == VAR_LIST)
8775 {
8776 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
8777 while (n-- > 0)
8778 if (list_extend(rettv->vval.v_list,
8779 argvars[0].vval.v_list, NULL) == FAIL)
8780 break;
8781 }
8782 else
8783 {
8784 p = get_tv_string(&argvars[0]);
8785 rettv->v_type = VAR_STRING;
8786 rettv->vval.v_string = NULL;
8787
8788 slen = (int)STRLEN(p);
8789 len = slen * n;
8790 if (len <= 0)
8791 return;
8792
8793 r = alloc(len + 1);
8794 if (r != NULL)
8795 {
8796 for (i = 0; i < n; i++)
8797 mch_memmove(r + i * slen, p, (size_t)slen);
8798 r[len] = NUL;
8799 }
8800
8801 rettv->vval.v_string = r;
8802 }
8803}
8804
8805/*
8806 * "resolve()" function
8807 */
8808 static void
8809f_resolve(typval_T *argvars, typval_T *rettv)
8810{
8811 char_u *p;
8812#ifdef HAVE_READLINK
8813 char_u *buf = NULL;
8814#endif
8815
8816 p = get_tv_string(&argvars[0]);
8817#ifdef FEAT_SHORTCUT
8818 {
8819 char_u *v = NULL;
8820
8821 v = mch_resolve_shortcut(p);
8822 if (v != NULL)
8823 rettv->vval.v_string = v;
8824 else
8825 rettv->vval.v_string = vim_strsave(p);
8826 }
8827#else
8828# ifdef HAVE_READLINK
8829 {
8830 char_u *cpy;
8831 int len;
8832 char_u *remain = NULL;
8833 char_u *q;
8834 int is_relative_to_current = FALSE;
8835 int has_trailing_pathsep = FALSE;
8836 int limit = 100;
8837
8838 p = vim_strsave(p);
8839
8840 if (p[0] == '.' && (vim_ispathsep(p[1])
8841 || (p[1] == '.' && (vim_ispathsep(p[2])))))
8842 is_relative_to_current = TRUE;
8843
8844 len = STRLEN(p);
8845 if (len > 0 && after_pathsep(p, p + len))
8846 {
8847 has_trailing_pathsep = TRUE;
8848 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
8849 }
8850
8851 q = getnextcomp(p);
8852 if (*q != NUL)
8853 {
8854 /* Separate the first path component in "p", and keep the
8855 * remainder (beginning with the path separator). */
8856 remain = vim_strsave(q - 1);
8857 q[-1] = NUL;
8858 }
8859
8860 buf = alloc(MAXPATHL + 1);
8861 if (buf == NULL)
8862 goto fail;
8863
8864 for (;;)
8865 {
8866 for (;;)
8867 {
8868 len = readlink((char *)p, (char *)buf, MAXPATHL);
8869 if (len <= 0)
8870 break;
8871 buf[len] = NUL;
8872
8873 if (limit-- == 0)
8874 {
8875 vim_free(p);
8876 vim_free(remain);
8877 EMSG(_("E655: Too many symbolic links (cycle?)"));
8878 rettv->vval.v_string = NULL;
8879 goto fail;
8880 }
8881
8882 /* Ensure that the result will have a trailing path separator
8883 * if the argument has one. */
8884 if (remain == NULL && has_trailing_pathsep)
8885 add_pathsep(buf);
8886
8887 /* Separate the first path component in the link value and
8888 * concatenate the remainders. */
8889 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
8890 if (*q != NUL)
8891 {
8892 if (remain == NULL)
8893 remain = vim_strsave(q - 1);
8894 else
8895 {
8896 cpy = concat_str(q - 1, remain);
8897 if (cpy != NULL)
8898 {
8899 vim_free(remain);
8900 remain = cpy;
8901 }
8902 }
8903 q[-1] = NUL;
8904 }
8905
8906 q = gettail(p);
8907 if (q > p && *q == NUL)
8908 {
8909 /* Ignore trailing path separator. */
8910 q[-1] = NUL;
8911 q = gettail(p);
8912 }
8913 if (q > p && !mch_isFullName(buf))
8914 {
8915 /* symlink is relative to directory of argument */
8916 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
8917 if (cpy != NULL)
8918 {
8919 STRCPY(cpy, p);
8920 STRCPY(gettail(cpy), buf);
8921 vim_free(p);
8922 p = cpy;
8923 }
8924 }
8925 else
8926 {
8927 vim_free(p);
8928 p = vim_strsave(buf);
8929 }
8930 }
8931
8932 if (remain == NULL)
8933 break;
8934
8935 /* Append the first path component of "remain" to "p". */
8936 q = getnextcomp(remain + 1);
8937 len = q - remain - (*q != NUL);
8938 cpy = vim_strnsave(p, STRLEN(p) + len);
8939 if (cpy != NULL)
8940 {
8941 STRNCAT(cpy, remain, len);
8942 vim_free(p);
8943 p = cpy;
8944 }
8945 /* Shorten "remain". */
8946 if (*q != NUL)
8947 STRMOVE(remain, q - 1);
8948 else
8949 {
8950 vim_free(remain);
8951 remain = NULL;
8952 }
8953 }
8954
8955 /* If the result is a relative path name, make it explicitly relative to
8956 * the current directory if and only if the argument had this form. */
8957 if (!vim_ispathsep(*p))
8958 {
8959 if (is_relative_to_current
8960 && *p != NUL
8961 && !(p[0] == '.'
8962 && (p[1] == NUL
8963 || vim_ispathsep(p[1])
8964 || (p[1] == '.'
8965 && (p[2] == NUL
8966 || vim_ispathsep(p[2]))))))
8967 {
8968 /* Prepend "./". */
8969 cpy = concat_str((char_u *)"./", p);
8970 if (cpy != NULL)
8971 {
8972 vim_free(p);
8973 p = cpy;
8974 }
8975 }
8976 else if (!is_relative_to_current)
8977 {
8978 /* Strip leading "./". */
8979 q = p;
8980 while (q[0] == '.' && vim_ispathsep(q[1]))
8981 q += 2;
8982 if (q > p)
8983 STRMOVE(p, p + 2);
8984 }
8985 }
8986
8987 /* Ensure that the result will have no trailing path separator
8988 * if the argument had none. But keep "/" or "//". */
8989 if (!has_trailing_pathsep)
8990 {
8991 q = p + STRLEN(p);
8992 if (after_pathsep(p, q))
8993 *gettail_sep(p) = NUL;
8994 }
8995
8996 rettv->vval.v_string = p;
8997 }
8998# else
8999 rettv->vval.v_string = vim_strsave(p);
9000# endif
9001#endif
9002
9003 simplify_filename(rettv->vval.v_string);
9004
9005#ifdef HAVE_READLINK
9006fail:
9007 vim_free(buf);
9008#endif
9009 rettv->v_type = VAR_STRING;
9010}
9011
9012/*
9013 * "reverse({list})" function
9014 */
9015 static void
9016f_reverse(typval_T *argvars, typval_T *rettv)
9017{
9018 list_T *l;
9019 listitem_T *li, *ni;
9020
9021 if (argvars[0].v_type != VAR_LIST)
9022 EMSG2(_(e_listarg), "reverse()");
9023 else if ((l = argvars[0].vval.v_list) != NULL
9024 && !tv_check_lock(l->lv_lock,
9025 (char_u *)N_("reverse() argument"), TRUE))
9026 {
9027 li = l->lv_last;
9028 l->lv_first = l->lv_last = NULL;
9029 l->lv_len = 0;
9030 while (li != NULL)
9031 {
9032 ni = li->li_prev;
9033 list_append(l, li);
9034 li = ni;
9035 }
9036 rettv->vval.v_list = l;
9037 rettv->v_type = VAR_LIST;
9038 ++l->lv_refcount;
9039 l->lv_idx = l->lv_len - l->lv_idx - 1;
9040 }
9041}
9042
9043#define SP_NOMOVE 0x01 /* don't move cursor */
9044#define SP_REPEAT 0x02 /* repeat to find outer pair */
9045#define SP_RETCOUNT 0x04 /* return matchcount */
9046#define SP_SETPCMARK 0x08 /* set previous context mark */
9047#define SP_START 0x10 /* accept match at start position */
9048#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9049#define SP_END 0x40 /* leave cursor at end of match */
9050#define SP_COLUMN 0x80 /* start at cursor column */
9051
9052static int get_search_arg(typval_T *varp, int *flagsp);
9053
9054/*
9055 * Get flags for a search function.
9056 * Possibly sets "p_ws".
9057 * Returns BACKWARD, FORWARD or zero (for an error).
9058 */
9059 static int
9060get_search_arg(typval_T *varp, int *flagsp)
9061{
9062 int dir = FORWARD;
9063 char_u *flags;
9064 char_u nbuf[NUMBUFLEN];
9065 int mask;
9066
9067 if (varp->v_type != VAR_UNKNOWN)
9068 {
9069 flags = get_tv_string_buf_chk(varp, nbuf);
9070 if (flags == NULL)
9071 return 0; /* type error; errmsg already given */
9072 while (*flags != NUL)
9073 {
9074 switch (*flags)
9075 {
9076 case 'b': dir = BACKWARD; break;
9077 case 'w': p_ws = TRUE; break;
9078 case 'W': p_ws = FALSE; break;
9079 default: mask = 0;
9080 if (flagsp != NULL)
9081 switch (*flags)
9082 {
9083 case 'c': mask = SP_START; break;
9084 case 'e': mask = SP_END; break;
9085 case 'm': mask = SP_RETCOUNT; break;
9086 case 'n': mask = SP_NOMOVE; break;
9087 case 'p': mask = SP_SUBPAT; break;
9088 case 'r': mask = SP_REPEAT; break;
9089 case 's': mask = SP_SETPCMARK; break;
9090 case 'z': mask = SP_COLUMN; break;
9091 }
9092 if (mask == 0)
9093 {
9094 EMSG2(_(e_invarg2), flags);
9095 dir = 0;
9096 }
9097 else
9098 *flagsp |= mask;
9099 }
9100 if (dir == 0)
9101 break;
9102 ++flags;
9103 }
9104 }
9105 return dir;
9106}
9107
9108/*
9109 * Shared by search() and searchpos() functions.
9110 */
9111 static int
9112search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9113{
9114 int flags;
9115 char_u *pat;
9116 pos_T pos;
9117 pos_T save_cursor;
9118 int save_p_ws = p_ws;
9119 int dir;
9120 int retval = 0; /* default: FAIL */
9121 long lnum_stop = 0;
9122 proftime_T tm;
9123#ifdef FEAT_RELTIME
9124 long time_limit = 0;
9125#endif
9126 int options = SEARCH_KEEP;
9127 int subpatnum;
9128
9129 pat = get_tv_string(&argvars[0]);
9130 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9131 if (dir == 0)
9132 goto theend;
9133 flags = *flagsp;
9134 if (flags & SP_START)
9135 options |= SEARCH_START;
9136 if (flags & SP_END)
9137 options |= SEARCH_END;
9138 if (flags & SP_COLUMN)
9139 options |= SEARCH_COL;
9140
9141 /* Optional arguments: line number to stop searching and timeout. */
9142 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9143 {
9144 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9145 if (lnum_stop < 0)
9146 goto theend;
9147#ifdef FEAT_RELTIME
9148 if (argvars[3].v_type != VAR_UNKNOWN)
9149 {
9150 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9151 if (time_limit < 0)
9152 goto theend;
9153 }
9154#endif
9155 }
9156
9157#ifdef FEAT_RELTIME
9158 /* Set the time limit, if there is one. */
9159 profile_setlimit(time_limit, &tm);
9160#endif
9161
9162 /*
9163 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9164 * Check to make sure only those flags are set.
9165 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9166 * flags cannot be set. Check for that condition also.
9167 */
9168 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9169 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9170 {
9171 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9172 goto theend;
9173 }
9174
9175 pos = save_cursor = curwin->w_cursor;
9176 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9177 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
9178 if (subpatnum != FAIL)
9179 {
9180 if (flags & SP_SUBPAT)
9181 retval = subpatnum;
9182 else
9183 retval = pos.lnum;
9184 if (flags & SP_SETPCMARK)
9185 setpcmark();
9186 curwin->w_cursor = pos;
9187 if (match_pos != NULL)
9188 {
9189 /* Store the match cursor position */
9190 match_pos->lnum = pos.lnum;
9191 match_pos->col = pos.col + 1;
9192 }
9193 /* "/$" will put the cursor after the end of the line, may need to
9194 * correct that here */
9195 check_cursor();
9196 }
9197
9198 /* If 'n' flag is used: restore cursor position. */
9199 if (flags & SP_NOMOVE)
9200 curwin->w_cursor = save_cursor;
9201 else
9202 curwin->w_set_curswant = TRUE;
9203theend:
9204 p_ws = save_p_ws;
9205
9206 return retval;
9207}
9208
9209#ifdef FEAT_FLOAT
9210
9211/*
9212 * round() is not in C90, use ceil() or floor() instead.
9213 */
9214 float_T
9215vim_round(float_T f)
9216{
9217 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9218}
9219
9220/*
9221 * "round({float})" function
9222 */
9223 static void
9224f_round(typval_T *argvars, typval_T *rettv)
9225{
9226 float_T f = 0.0;
9227
9228 rettv->v_type = VAR_FLOAT;
9229 if (get_float_arg(argvars, &f) == OK)
9230 rettv->vval.v_float = vim_round(f);
9231 else
9232 rettv->vval.v_float = 0.0;
9233}
9234#endif
9235
9236/*
9237 * "screenattr()" function
9238 */
9239 static void
9240f_screenattr(typval_T *argvars, typval_T *rettv)
9241{
9242 int row;
9243 int col;
9244 int c;
9245
9246 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9247 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9248 if (row < 0 || row >= screen_Rows
9249 || col < 0 || col >= screen_Columns)
9250 c = -1;
9251 else
9252 c = ScreenAttrs[LineOffset[row] + col];
9253 rettv->vval.v_number = c;
9254}
9255
9256/*
9257 * "screenchar()" function
9258 */
9259 static void
9260f_screenchar(typval_T *argvars, typval_T *rettv)
9261{
9262 int row;
9263 int col;
9264 int off;
9265 int c;
9266
9267 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9268 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9269 if (row < 0 || row >= screen_Rows
9270 || col < 0 || col >= screen_Columns)
9271 c = -1;
9272 else
9273 {
9274 off = LineOffset[row] + col;
9275#ifdef FEAT_MBYTE
9276 if (enc_utf8 && ScreenLinesUC[off] != 0)
9277 c = ScreenLinesUC[off];
9278 else
9279#endif
9280 c = ScreenLines[off];
9281 }
9282 rettv->vval.v_number = c;
9283}
9284
9285/*
9286 * "screencol()" function
9287 *
9288 * First column is 1 to be consistent with virtcol().
9289 */
9290 static void
9291f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9292{
9293 rettv->vval.v_number = screen_screencol() + 1;
9294}
9295
9296/*
9297 * "screenrow()" function
9298 */
9299 static void
9300f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9301{
9302 rettv->vval.v_number = screen_screenrow() + 1;
9303}
9304
9305/*
9306 * "search()" function
9307 */
9308 static void
9309f_search(typval_T *argvars, typval_T *rettv)
9310{
9311 int flags = 0;
9312
9313 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9314}
9315
9316/*
9317 * "searchdecl()" function
9318 */
9319 static void
9320f_searchdecl(typval_T *argvars, typval_T *rettv)
9321{
9322 int locally = 1;
9323 int thisblock = 0;
9324 int error = FALSE;
9325 char_u *name;
9326
9327 rettv->vval.v_number = 1; /* default: FAIL */
9328
9329 name = get_tv_string_chk(&argvars[0]);
9330 if (argvars[1].v_type != VAR_UNKNOWN)
9331 {
9332 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9333 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9334 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9335 }
9336 if (!error && name != NULL)
9337 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9338 locally, thisblock, SEARCH_KEEP) == FAIL;
9339}
9340
9341/*
9342 * Used by searchpair() and searchpairpos()
9343 */
9344 static int
9345searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9346{
9347 char_u *spat, *mpat, *epat;
9348 char_u *skip;
9349 int save_p_ws = p_ws;
9350 int dir;
9351 int flags = 0;
9352 char_u nbuf1[NUMBUFLEN];
9353 char_u nbuf2[NUMBUFLEN];
9354 char_u nbuf3[NUMBUFLEN];
9355 int retval = 0; /* default: FAIL */
9356 long lnum_stop = 0;
9357 long time_limit = 0;
9358
9359 /* Get the three pattern arguments: start, middle, end. */
9360 spat = get_tv_string_chk(&argvars[0]);
9361 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9362 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9363 if (spat == NULL || mpat == NULL || epat == NULL)
9364 goto theend; /* type error */
9365
9366 /* Handle the optional fourth argument: flags */
9367 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9368 if (dir == 0)
9369 goto theend;
9370
9371 /* Don't accept SP_END or SP_SUBPAT.
9372 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9373 */
9374 if ((flags & (SP_END | SP_SUBPAT)) != 0
9375 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9376 {
9377 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9378 goto theend;
9379 }
9380
9381 /* Using 'r' implies 'W', otherwise it doesn't work. */
9382 if (flags & SP_REPEAT)
9383 p_ws = FALSE;
9384
9385 /* Optional fifth argument: skip expression */
9386 if (argvars[3].v_type == VAR_UNKNOWN
9387 || argvars[4].v_type == VAR_UNKNOWN)
9388 skip = (char_u *)"";
9389 else
9390 {
9391 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
9392 if (argvars[5].v_type != VAR_UNKNOWN)
9393 {
9394 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9395 if (lnum_stop < 0)
9396 goto theend;
9397#ifdef FEAT_RELTIME
9398 if (argvars[6].v_type != VAR_UNKNOWN)
9399 {
9400 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9401 if (time_limit < 0)
9402 goto theend;
9403 }
9404#endif
9405 }
9406 }
9407 if (skip == NULL)
9408 goto theend; /* type error */
9409
9410 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9411 match_pos, lnum_stop, time_limit);
9412
9413theend:
9414 p_ws = save_p_ws;
9415
9416 return retval;
9417}
9418
9419/*
9420 * "searchpair()" function
9421 */
9422 static void
9423f_searchpair(typval_T *argvars, typval_T *rettv)
9424{
9425 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9426}
9427
9428/*
9429 * "searchpairpos()" function
9430 */
9431 static void
9432f_searchpairpos(typval_T *argvars, typval_T *rettv)
9433{
9434 pos_T match_pos;
9435 int lnum = 0;
9436 int col = 0;
9437
9438 if (rettv_list_alloc(rettv) == FAIL)
9439 return;
9440
9441 if (searchpair_cmn(argvars, &match_pos) > 0)
9442 {
9443 lnum = match_pos.lnum;
9444 col = match_pos.col;
9445 }
9446
9447 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9448 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9449}
9450
9451/*
9452 * Search for a start/middle/end thing.
9453 * Used by searchpair(), see its documentation for the details.
9454 * Returns 0 or -1 for no match,
9455 */
9456 long
9457do_searchpair(
9458 char_u *spat, /* start pattern */
9459 char_u *mpat, /* middle pattern */
9460 char_u *epat, /* end pattern */
9461 int dir, /* BACKWARD or FORWARD */
9462 char_u *skip, /* skip expression */
9463 int flags, /* SP_SETPCMARK and other SP_ values */
9464 pos_T *match_pos,
9465 linenr_T lnum_stop, /* stop at this line if not zero */
9466 long time_limit UNUSED) /* stop after this many msec */
9467{
9468 char_u *save_cpo;
9469 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9470 long retval = 0;
9471 pos_T pos;
9472 pos_T firstpos;
9473 pos_T foundpos;
9474 pos_T save_cursor;
9475 pos_T save_pos;
9476 int n;
9477 int r;
9478 int nest = 1;
9479 int err;
9480 int options = SEARCH_KEEP;
9481 proftime_T tm;
9482
9483 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9484 save_cpo = p_cpo;
9485 p_cpo = empty_option;
9486
9487#ifdef FEAT_RELTIME
9488 /* Set the time limit, if there is one. */
9489 profile_setlimit(time_limit, &tm);
9490#endif
9491
9492 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9493 * start/middle/end (pat3, for the top pair). */
9494 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
9495 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
9496 if (pat2 == NULL || pat3 == NULL)
9497 goto theend;
9498 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
9499 if (*mpat == NUL)
9500 STRCPY(pat3, pat2);
9501 else
9502 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
9503 spat, epat, mpat);
9504 if (flags & SP_START)
9505 options |= SEARCH_START;
9506
9507 save_cursor = curwin->w_cursor;
9508 pos = curwin->w_cursor;
9509 clearpos(&firstpos);
9510 clearpos(&foundpos);
9511 pat = pat3;
9512 for (;;)
9513 {
9514 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9515 options, RE_SEARCH, lnum_stop, &tm);
9516 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
9517 /* didn't find it or found the first match again: FAIL */
9518 break;
9519
9520 if (firstpos.lnum == 0)
9521 firstpos = pos;
9522 if (equalpos(pos, foundpos))
9523 {
9524 /* Found the same position again. Can happen with a pattern that
9525 * has "\zs" at the end and searching backwards. Advance one
9526 * character and try again. */
9527 if (dir == BACKWARD)
9528 decl(&pos);
9529 else
9530 incl(&pos);
9531 }
9532 foundpos = pos;
9533
9534 /* clear the start flag to avoid getting stuck here */
9535 options &= ~SEARCH_START;
9536
9537 /* If the skip pattern matches, ignore this match. */
9538 if (*skip != NUL)
9539 {
9540 save_pos = curwin->w_cursor;
9541 curwin->w_cursor = pos;
9542 r = eval_to_bool(skip, &err, NULL, FALSE);
9543 curwin->w_cursor = save_pos;
9544 if (err)
9545 {
9546 /* Evaluating {skip} caused an error, break here. */
9547 curwin->w_cursor = save_cursor;
9548 retval = -1;
9549 break;
9550 }
9551 if (r)
9552 continue;
9553 }
9554
9555 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9556 {
9557 /* Found end when searching backwards or start when searching
9558 * forward: nested pair. */
9559 ++nest;
9560 pat = pat2; /* nested, don't search for middle */
9561 }
9562 else
9563 {
9564 /* Found end when searching forward or start when searching
9565 * backward: end of (nested) pair; or found middle in outer pair. */
9566 if (--nest == 1)
9567 pat = pat3; /* outer level, search for middle */
9568 }
9569
9570 if (nest == 0)
9571 {
9572 /* Found the match: return matchcount or line number. */
9573 if (flags & SP_RETCOUNT)
9574 ++retval;
9575 else
9576 retval = pos.lnum;
9577 if (flags & SP_SETPCMARK)
9578 setpcmark();
9579 curwin->w_cursor = pos;
9580 if (!(flags & SP_REPEAT))
9581 break;
9582 nest = 1; /* search for next unmatched */
9583 }
9584 }
9585
9586 if (match_pos != NULL)
9587 {
9588 /* Store the match cursor position */
9589 match_pos->lnum = curwin->w_cursor.lnum;
9590 match_pos->col = curwin->w_cursor.col + 1;
9591 }
9592
9593 /* If 'n' flag is used or search failed: restore cursor position. */
9594 if ((flags & SP_NOMOVE) || retval == 0)
9595 curwin->w_cursor = save_cursor;
9596
9597theend:
9598 vim_free(pat2);
9599 vim_free(pat3);
9600 if (p_cpo == empty_option)
9601 p_cpo = save_cpo;
9602 else
9603 /* Darn, evaluating the {skip} expression changed the value. */
9604 free_string_option(save_cpo);
9605
9606 return retval;
9607}
9608
9609/*
9610 * "searchpos()" function
9611 */
9612 static void
9613f_searchpos(typval_T *argvars, typval_T *rettv)
9614{
9615 pos_T match_pos;
9616 int lnum = 0;
9617 int col = 0;
9618 int n;
9619 int flags = 0;
9620
9621 if (rettv_list_alloc(rettv) == FAIL)
9622 return;
9623
9624 n = search_cmn(argvars, &match_pos, &flags);
9625 if (n > 0)
9626 {
9627 lnum = match_pos.lnum;
9628 col = match_pos.col;
9629 }
9630
9631 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9632 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9633 if (flags & SP_SUBPAT)
9634 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9635}
9636
9637 static void
9638f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9639{
9640#ifdef FEAT_CLIENTSERVER
9641 char_u buf[NUMBUFLEN];
9642 char_u *server = get_tv_string_chk(&argvars[0]);
9643 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9644
9645 rettv->vval.v_number = -1;
9646 if (server == NULL || reply == NULL)
9647 return;
9648 if (check_restricted() || check_secure())
9649 return;
9650# ifdef FEAT_X11
9651 if (check_connection() == FAIL)
9652 return;
9653# endif
9654
9655 if (serverSendReply(server, reply) < 0)
9656 {
9657 EMSG(_("E258: Unable to send to client"));
9658 return;
9659 }
9660 rettv->vval.v_number = 0;
9661#else
9662 rettv->vval.v_number = -1;
9663#endif
9664}
9665
9666 static void
9667f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9668{
9669 char_u *r = NULL;
9670
9671#ifdef FEAT_CLIENTSERVER
9672# ifdef WIN32
9673 r = serverGetVimNames();
9674# else
9675 make_connection();
9676 if (X_DISPLAY != NULL)
9677 r = serverGetVimNames(X_DISPLAY);
9678# endif
9679#endif
9680 rettv->v_type = VAR_STRING;
9681 rettv->vval.v_string = r;
9682}
9683
9684/*
9685 * "setbufvar()" function
9686 */
9687 static void
9688f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
9689{
9690 buf_T *buf;
9691 char_u *varname, *bufvarname;
9692 typval_T *varp;
9693 char_u nbuf[NUMBUFLEN];
9694
9695 if (check_restricted() || check_secure())
9696 return;
9697 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9698 varname = get_tv_string_chk(&argvars[1]);
9699 buf = get_buf_tv(&argvars[0], FALSE);
9700 varp = &argvars[2];
9701
9702 if (buf != NULL && varname != NULL && varp != NULL)
9703 {
9704 if (*varname == '&')
9705 {
9706 long numval;
9707 char_u *strval;
9708 int error = FALSE;
9709 aco_save_T aco;
9710
9711 /* set curbuf to be our buf, temporarily */
9712 aucmd_prepbuf(&aco, buf);
9713
9714 ++varname;
9715 numval = (long)get_tv_number_chk(varp, &error);
9716 strval = get_tv_string_buf_chk(varp, nbuf);
9717 if (!error && strval != NULL)
9718 set_option_value(varname, numval, strval, OPT_LOCAL);
9719
9720 /* reset notion of buffer */
9721 aucmd_restbuf(&aco);
9722 }
9723 else
9724 {
9725 buf_T *save_curbuf = curbuf;
9726
9727 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
9728 if (bufvarname != NULL)
9729 {
9730 curbuf = buf;
9731 STRCPY(bufvarname, "b:");
9732 STRCPY(bufvarname + 2, varname);
9733 set_var(bufvarname, varp, TRUE);
9734 vim_free(bufvarname);
9735 curbuf = save_curbuf;
9736 }
9737 }
9738 }
9739}
9740
9741 static void
9742f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
9743{
9744 dict_T *d;
9745 dictitem_T *di;
9746 char_u *csearch;
9747
9748 if (argvars[0].v_type != VAR_DICT)
9749 {
9750 EMSG(_(e_dictreq));
9751 return;
9752 }
9753
9754 if ((d = argvars[0].vval.v_dict) != NULL)
9755 {
9756 csearch = get_dict_string(d, (char_u *)"char", FALSE);
9757 if (csearch != NULL)
9758 {
9759#ifdef FEAT_MBYTE
9760 if (enc_utf8)
9761 {
9762 int pcc[MAX_MCO];
9763 int c = utfc_ptr2char(csearch, pcc);
9764
9765 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
9766 }
9767 else
9768#endif
9769 set_last_csearch(PTR2CHAR(csearch),
9770 csearch, MB_PTR2LEN(csearch));
9771 }
9772
9773 di = dict_find(d, (char_u *)"forward", -1);
9774 if (di != NULL)
9775 set_csearch_direction((int)get_tv_number(&di->di_tv)
9776 ? FORWARD : BACKWARD);
9777
9778 di = dict_find(d, (char_u *)"until", -1);
9779 if (di != NULL)
9780 set_csearch_until(!!get_tv_number(&di->di_tv));
9781 }
9782}
9783
9784/*
9785 * "setcmdpos()" function
9786 */
9787 static void
9788f_setcmdpos(typval_T *argvars, typval_T *rettv)
9789{
9790 int pos = (int)get_tv_number(&argvars[0]) - 1;
9791
9792 if (pos >= 0)
9793 rettv->vval.v_number = set_cmdline_pos(pos);
9794}
9795
9796/*
9797 * "setfperm({fname}, {mode})" function
9798 */
9799 static void
9800f_setfperm(typval_T *argvars, typval_T *rettv)
9801{
9802 char_u *fname;
9803 char_u modebuf[NUMBUFLEN];
9804 char_u *mode_str;
9805 int i;
9806 int mask;
9807 int mode = 0;
9808
9809 rettv->vval.v_number = 0;
9810 fname = get_tv_string_chk(&argvars[0]);
9811 if (fname == NULL)
9812 return;
9813 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
9814 if (mode_str == NULL)
9815 return;
9816 if (STRLEN(mode_str) != 9)
9817 {
9818 EMSG2(_(e_invarg2), mode_str);
9819 return;
9820 }
9821
9822 mask = 1;
9823 for (i = 8; i >= 0; --i)
9824 {
9825 if (mode_str[i] != '-')
9826 mode |= mask;
9827 mask = mask << 1;
9828 }
9829 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
9830}
9831
9832/*
9833 * "setline()" function
9834 */
9835 static void
9836f_setline(typval_T *argvars, typval_T *rettv)
9837{
9838 linenr_T lnum;
9839 char_u *line = NULL;
9840 list_T *l = NULL;
9841 listitem_T *li = NULL;
9842 long added = 0;
9843 linenr_T lcount = curbuf->b_ml.ml_line_count;
9844
9845 lnum = get_tv_lnum(&argvars[0]);
9846 if (argvars[1].v_type == VAR_LIST)
9847 {
9848 l = argvars[1].vval.v_list;
9849 li = l->lv_first;
9850 }
9851 else
9852 line = get_tv_string_chk(&argvars[1]);
9853
9854 /* default result is zero == OK */
9855 for (;;)
9856 {
9857 if (l != NULL)
9858 {
9859 /* list argument, get next string */
9860 if (li == NULL)
9861 break;
9862 line = get_tv_string_chk(&li->li_tv);
9863 li = li->li_next;
9864 }
9865
9866 rettv->vval.v_number = 1; /* FAIL */
9867 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
9868 break;
9869
9870 /* When coming here from Insert mode, sync undo, so that this can be
9871 * undone separately from what was previously inserted. */
9872 if (u_sync_once == 2)
9873 {
9874 u_sync_once = 1; /* notify that u_sync() was called */
9875 u_sync(TRUE);
9876 }
9877
9878 if (lnum <= curbuf->b_ml.ml_line_count)
9879 {
9880 /* existing line, replace it */
9881 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
9882 {
9883 changed_bytes(lnum, 0);
9884 if (lnum == curwin->w_cursor.lnum)
9885 check_cursor_col();
9886 rettv->vval.v_number = 0; /* OK */
9887 }
9888 }
9889 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
9890 {
9891 /* lnum is one past the last line, append the line */
9892 ++added;
9893 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
9894 rettv->vval.v_number = 0; /* OK */
9895 }
9896
9897 if (l == NULL) /* only one string argument */
9898 break;
9899 ++lnum;
9900 }
9901
9902 if (added > 0)
9903 appended_lines_mark(lcount, added);
9904}
9905
Bram Moolenaard823fa92016-08-12 16:29:27 +02009906static 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 +02009907
9908/*
9909 * Used by "setqflist()" and "setloclist()" functions
9910 */
9911 static void
9912set_qf_ll_list(
9913 win_T *wp UNUSED,
9914 typval_T *list_arg UNUSED,
9915 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +02009916 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009917 typval_T *rettv)
9918{
9919#ifdef FEAT_QUICKFIX
9920 static char *e_invact = N_("E927: Invalid action: '%s'");
9921 char_u *act;
9922 int action = 0;
9923#endif
9924
9925 rettv->vval.v_number = -1;
9926
9927#ifdef FEAT_QUICKFIX
9928 if (list_arg->v_type != VAR_LIST)
9929 EMSG(_(e_listreq));
9930 else
9931 {
9932 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +02009933 dict_T *d = NULL;
9934 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009935
9936 if (action_arg->v_type == VAR_STRING)
9937 {
9938 act = get_tv_string_chk(action_arg);
9939 if (act == NULL)
9940 return; /* type error; errmsg already given */
9941 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
9942 action = *act;
9943 else
9944 EMSG2(_(e_invact), act);
9945 }
9946 else if (action_arg->v_type == VAR_UNKNOWN)
9947 action = ' ';
9948 else
9949 EMSG(_(e_stringreq));
9950
Bram Moolenaard823fa92016-08-12 16:29:27 +02009951 if (action_arg->v_type != VAR_UNKNOWN
9952 && what_arg->v_type != VAR_UNKNOWN)
9953 {
9954 if (what_arg->v_type == VAR_DICT)
9955 d = what_arg->vval.v_dict;
9956 else
9957 {
9958 EMSG(_(e_dictreq));
9959 valid_dict = FALSE;
9960 }
9961 }
9962
9963 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
9964 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009965 rettv->vval.v_number = 0;
9966 }
9967#endif
9968}
9969
9970/*
9971 * "setloclist()" function
9972 */
9973 static void
9974f_setloclist(typval_T *argvars, typval_T *rettv)
9975{
9976 win_T *win;
9977
9978 rettv->vval.v_number = -1;
9979
9980 win = find_win_by_nr(&argvars[0], NULL);
9981 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02009982 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009983}
9984
9985/*
9986 * "setmatches()" function
9987 */
9988 static void
9989f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9990{
9991#ifdef FEAT_SEARCH_EXTRA
9992 list_T *l;
9993 listitem_T *li;
9994 dict_T *d;
9995 list_T *s = NULL;
9996
9997 rettv->vval.v_number = -1;
9998 if (argvars[0].v_type != VAR_LIST)
9999 {
10000 EMSG(_(e_listreq));
10001 return;
10002 }
10003 if ((l = argvars[0].vval.v_list) != NULL)
10004 {
10005
10006 /* To some extent make sure that we are dealing with a list from
10007 * "getmatches()". */
10008 li = l->lv_first;
10009 while (li != NULL)
10010 {
10011 if (li->li_tv.v_type != VAR_DICT
10012 || (d = li->li_tv.vval.v_dict) == NULL)
10013 {
10014 EMSG(_(e_invarg));
10015 return;
10016 }
10017 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10018 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10019 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10020 && dict_find(d, (char_u *)"priority", -1) != NULL
10021 && dict_find(d, (char_u *)"id", -1) != NULL))
10022 {
10023 EMSG(_(e_invarg));
10024 return;
10025 }
10026 li = li->li_next;
10027 }
10028
10029 clear_matches(curwin);
10030 li = l->lv_first;
10031 while (li != NULL)
10032 {
10033 int i = 0;
10034 char_u buf[5];
10035 dictitem_T *di;
10036 char_u *group;
10037 int priority;
10038 int id;
10039 char_u *conceal;
10040
10041 d = li->li_tv.vval.v_dict;
10042 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10043 {
10044 if (s == NULL)
10045 {
10046 s = list_alloc();
10047 if (s == NULL)
10048 return;
10049 }
10050
10051 /* match from matchaddpos() */
10052 for (i = 1; i < 9; i++)
10053 {
10054 sprintf((char *)buf, (char *)"pos%d", i);
10055 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10056 {
10057 if (di->di_tv.v_type != VAR_LIST)
10058 return;
10059
10060 list_append_tv(s, &di->di_tv);
10061 s->lv_refcount++;
10062 }
10063 else
10064 break;
10065 }
10066 }
10067
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010068 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010069 priority = (int)get_dict_number(d, (char_u *)"priority");
10070 id = (int)get_dict_number(d, (char_u *)"id");
10071 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010072 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010073 : NULL;
10074 if (i == 0)
10075 {
10076 match_add(curwin, group,
10077 get_dict_string(d, (char_u *)"pattern", FALSE),
10078 priority, id, NULL, conceal);
10079 }
10080 else
10081 {
10082 match_add(curwin, group, NULL, priority, id, s, conceal);
10083 list_unref(s);
10084 s = NULL;
10085 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010086 vim_free(group);
10087 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010088
10089 li = li->li_next;
10090 }
10091 rettv->vval.v_number = 0;
10092 }
10093#endif
10094}
10095
10096/*
10097 * "setpos()" function
10098 */
10099 static void
10100f_setpos(typval_T *argvars, typval_T *rettv)
10101{
10102 pos_T pos;
10103 int fnum;
10104 char_u *name;
10105 colnr_T curswant = -1;
10106
10107 rettv->vval.v_number = -1;
10108 name = get_tv_string_chk(argvars);
10109 if (name != NULL)
10110 {
10111 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10112 {
10113 if (--pos.col < 0)
10114 pos.col = 0;
10115 if (name[0] == '.' && name[1] == NUL)
10116 {
10117 /* set cursor */
10118 if (fnum == curbuf->b_fnum)
10119 {
10120 curwin->w_cursor = pos;
10121 if (curswant >= 0)
10122 {
10123 curwin->w_curswant = curswant - 1;
10124 curwin->w_set_curswant = FALSE;
10125 }
10126 check_cursor();
10127 rettv->vval.v_number = 0;
10128 }
10129 else
10130 EMSG(_(e_invarg));
10131 }
10132 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10133 {
10134 /* set mark */
10135 if (setmark_pos(name[1], &pos, fnum) == OK)
10136 rettv->vval.v_number = 0;
10137 }
10138 else
10139 EMSG(_(e_invarg));
10140 }
10141 }
10142}
10143
10144/*
10145 * "setqflist()" function
10146 */
10147 static void
10148f_setqflist(typval_T *argvars, typval_T *rettv)
10149{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010150 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010151}
10152
10153/*
10154 * "setreg()" function
10155 */
10156 static void
10157f_setreg(typval_T *argvars, typval_T *rettv)
10158{
10159 int regname;
10160 char_u *strregname;
10161 char_u *stropt;
10162 char_u *strval;
10163 int append;
10164 char_u yank_type;
10165 long block_len;
10166
10167 block_len = -1;
10168 yank_type = MAUTO;
10169 append = FALSE;
10170
10171 strregname = get_tv_string_chk(argvars);
10172 rettv->vval.v_number = 1; /* FAIL is default */
10173
10174 if (strregname == NULL)
10175 return; /* type error; errmsg already given */
10176 regname = *strregname;
10177 if (regname == 0 || regname == '@')
10178 regname = '"';
10179
10180 if (argvars[2].v_type != VAR_UNKNOWN)
10181 {
10182 stropt = get_tv_string_chk(&argvars[2]);
10183 if (stropt == NULL)
10184 return; /* type error */
10185 for (; *stropt != NUL; ++stropt)
10186 switch (*stropt)
10187 {
10188 case 'a': case 'A': /* append */
10189 append = TRUE;
10190 break;
10191 case 'v': case 'c': /* character-wise selection */
10192 yank_type = MCHAR;
10193 break;
10194 case 'V': case 'l': /* line-wise selection */
10195 yank_type = MLINE;
10196 break;
10197 case 'b': case Ctrl_V: /* block-wise selection */
10198 yank_type = MBLOCK;
10199 if (VIM_ISDIGIT(stropt[1]))
10200 {
10201 ++stropt;
10202 block_len = getdigits(&stropt) - 1;
10203 --stropt;
10204 }
10205 break;
10206 }
10207 }
10208
10209 if (argvars[1].v_type == VAR_LIST)
10210 {
10211 char_u **lstval;
10212 char_u **allocval;
10213 char_u buf[NUMBUFLEN];
10214 char_u **curval;
10215 char_u **curallocval;
10216 list_T *ll = argvars[1].vval.v_list;
10217 listitem_T *li;
10218 int len;
10219
10220 /* If the list is NULL handle like an empty list. */
10221 len = ll == NULL ? 0 : ll->lv_len;
10222
10223 /* First half: use for pointers to result lines; second half: use for
10224 * pointers to allocated copies. */
10225 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10226 if (lstval == NULL)
10227 return;
10228 curval = lstval;
10229 allocval = lstval + len + 2;
10230 curallocval = allocval;
10231
10232 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10233 li = li->li_next)
10234 {
10235 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10236 if (strval == NULL)
10237 goto free_lstval;
10238 if (strval == buf)
10239 {
10240 /* Need to make a copy, next get_tv_string_buf_chk() will
10241 * overwrite the string. */
10242 strval = vim_strsave(buf);
10243 if (strval == NULL)
10244 goto free_lstval;
10245 *curallocval++ = strval;
10246 }
10247 *curval++ = strval;
10248 }
10249 *curval++ = NULL;
10250
10251 write_reg_contents_lst(regname, lstval, -1,
10252 append, yank_type, block_len);
10253free_lstval:
10254 while (curallocval > allocval)
10255 vim_free(*--curallocval);
10256 vim_free(lstval);
10257 }
10258 else
10259 {
10260 strval = get_tv_string_chk(&argvars[1]);
10261 if (strval == NULL)
10262 return;
10263 write_reg_contents_ex(regname, strval, -1,
10264 append, yank_type, block_len);
10265 }
10266 rettv->vval.v_number = 0;
10267}
10268
10269/*
10270 * "settabvar()" function
10271 */
10272 static void
10273f_settabvar(typval_T *argvars, typval_T *rettv)
10274{
10275#ifdef FEAT_WINDOWS
10276 tabpage_T *save_curtab;
10277 tabpage_T *tp;
10278#endif
10279 char_u *varname, *tabvarname;
10280 typval_T *varp;
10281
10282 rettv->vval.v_number = 0;
10283
10284 if (check_restricted() || check_secure())
10285 return;
10286
10287#ifdef FEAT_WINDOWS
10288 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10289#endif
10290 varname = get_tv_string_chk(&argvars[1]);
10291 varp = &argvars[2];
10292
10293 if (varname != NULL && varp != NULL
10294#ifdef FEAT_WINDOWS
10295 && tp != NULL
10296#endif
10297 )
10298 {
10299#ifdef FEAT_WINDOWS
10300 save_curtab = curtab;
10301 goto_tabpage_tp(tp, FALSE, FALSE);
10302#endif
10303
10304 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10305 if (tabvarname != NULL)
10306 {
10307 STRCPY(tabvarname, "t:");
10308 STRCPY(tabvarname + 2, varname);
10309 set_var(tabvarname, varp, TRUE);
10310 vim_free(tabvarname);
10311 }
10312
10313#ifdef FEAT_WINDOWS
10314 /* Restore current tabpage */
10315 if (valid_tabpage(save_curtab))
10316 goto_tabpage_tp(save_curtab, FALSE, FALSE);
10317#endif
10318 }
10319}
10320
10321/*
10322 * "settabwinvar()" function
10323 */
10324 static void
10325f_settabwinvar(typval_T *argvars, typval_T *rettv)
10326{
10327 setwinvar(argvars, rettv, 1);
10328}
10329
10330/*
10331 * "setwinvar()" function
10332 */
10333 static void
10334f_setwinvar(typval_T *argvars, typval_T *rettv)
10335{
10336 setwinvar(argvars, rettv, 0);
10337}
10338
10339#ifdef FEAT_CRYPT
10340/*
10341 * "sha256({string})" function
10342 */
10343 static void
10344f_sha256(typval_T *argvars, typval_T *rettv)
10345{
10346 char_u *p;
10347
10348 p = get_tv_string(&argvars[0]);
10349 rettv->vval.v_string = vim_strsave(
10350 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10351 rettv->v_type = VAR_STRING;
10352}
10353#endif /* FEAT_CRYPT */
10354
10355/*
10356 * "shellescape({string})" function
10357 */
10358 static void
10359f_shellescape(typval_T *argvars, typval_T *rettv)
10360{
10361 rettv->vval.v_string = vim_strsave_shellescape(
10362 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
10363 rettv->v_type = VAR_STRING;
10364}
10365
10366/*
10367 * shiftwidth() function
10368 */
10369 static void
10370f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10371{
10372 rettv->vval.v_number = get_sw_value(curbuf);
10373}
10374
10375/*
10376 * "simplify()" function
10377 */
10378 static void
10379f_simplify(typval_T *argvars, typval_T *rettv)
10380{
10381 char_u *p;
10382
10383 p = get_tv_string(&argvars[0]);
10384 rettv->vval.v_string = vim_strsave(p);
10385 simplify_filename(rettv->vval.v_string); /* simplify in place */
10386 rettv->v_type = VAR_STRING;
10387}
10388
10389#ifdef FEAT_FLOAT
10390/*
10391 * "sin()" function
10392 */
10393 static void
10394f_sin(typval_T *argvars, typval_T *rettv)
10395{
10396 float_T f = 0.0;
10397
10398 rettv->v_type = VAR_FLOAT;
10399 if (get_float_arg(argvars, &f) == OK)
10400 rettv->vval.v_float = sin(f);
10401 else
10402 rettv->vval.v_float = 0.0;
10403}
10404
10405/*
10406 * "sinh()" function
10407 */
10408 static void
10409f_sinh(typval_T *argvars, typval_T *rettv)
10410{
10411 float_T f = 0.0;
10412
10413 rettv->v_type = VAR_FLOAT;
10414 if (get_float_arg(argvars, &f) == OK)
10415 rettv->vval.v_float = sinh(f);
10416 else
10417 rettv->vval.v_float = 0.0;
10418}
10419#endif
10420
10421static int
10422#ifdef __BORLANDC__
10423 _RTLENTRYF
10424#endif
10425 item_compare(const void *s1, const void *s2);
10426static int
10427#ifdef __BORLANDC__
10428 _RTLENTRYF
10429#endif
10430 item_compare2(const void *s1, const void *s2);
10431
10432/* struct used in the array that's given to qsort() */
10433typedef struct
10434{
10435 listitem_T *item;
10436 int idx;
10437} sortItem_T;
10438
10439/* struct storing information about current sort */
10440typedef struct
10441{
10442 int item_compare_ic;
10443 int item_compare_numeric;
10444 int item_compare_numbers;
10445#ifdef FEAT_FLOAT
10446 int item_compare_float;
10447#endif
10448 char_u *item_compare_func;
10449 partial_T *item_compare_partial;
10450 dict_T *item_compare_selfdict;
10451 int item_compare_func_err;
10452 int item_compare_keep_zero;
10453} sortinfo_T;
10454static sortinfo_T *sortinfo = NULL;
10455static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10456#define ITEM_COMPARE_FAIL 999
10457
10458/*
10459 * Compare functions for f_sort() and f_uniq() below.
10460 */
10461 static int
10462#ifdef __BORLANDC__
10463_RTLENTRYF
10464#endif
10465item_compare(const void *s1, const void *s2)
10466{
10467 sortItem_T *si1, *si2;
10468 typval_T *tv1, *tv2;
10469 char_u *p1, *p2;
10470 char_u *tofree1 = NULL, *tofree2 = NULL;
10471 int res;
10472 char_u numbuf1[NUMBUFLEN];
10473 char_u numbuf2[NUMBUFLEN];
10474
10475 si1 = (sortItem_T *)s1;
10476 si2 = (sortItem_T *)s2;
10477 tv1 = &si1->item->li_tv;
10478 tv2 = &si2->item->li_tv;
10479
10480 if (sortinfo->item_compare_numbers)
10481 {
10482 varnumber_T v1 = get_tv_number(tv1);
10483 varnumber_T v2 = get_tv_number(tv2);
10484
10485 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10486 }
10487
10488#ifdef FEAT_FLOAT
10489 if (sortinfo->item_compare_float)
10490 {
10491 float_T v1 = get_tv_float(tv1);
10492 float_T v2 = get_tv_float(tv2);
10493
10494 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10495 }
10496#endif
10497
10498 /* tv2string() puts quotes around a string and allocates memory. Don't do
10499 * that for string variables. Use a single quote when comparing with a
10500 * non-string to do what the docs promise. */
10501 if (tv1->v_type == VAR_STRING)
10502 {
10503 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10504 p1 = (char_u *)"'";
10505 else
10506 p1 = tv1->vval.v_string;
10507 }
10508 else
10509 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10510 if (tv2->v_type == VAR_STRING)
10511 {
10512 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10513 p2 = (char_u *)"'";
10514 else
10515 p2 = tv2->vval.v_string;
10516 }
10517 else
10518 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10519 if (p1 == NULL)
10520 p1 = (char_u *)"";
10521 if (p2 == NULL)
10522 p2 = (char_u *)"";
10523 if (!sortinfo->item_compare_numeric)
10524 {
10525 if (sortinfo->item_compare_ic)
10526 res = STRICMP(p1, p2);
10527 else
10528 res = STRCMP(p1, p2);
10529 }
10530 else
10531 {
10532 double n1, n2;
10533 n1 = strtod((char *)p1, (char **)&p1);
10534 n2 = strtod((char *)p2, (char **)&p2);
10535 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10536 }
10537
10538 /* When the result would be zero, compare the item indexes. Makes the
10539 * sort stable. */
10540 if (res == 0 && !sortinfo->item_compare_keep_zero)
10541 res = si1->idx > si2->idx ? 1 : -1;
10542
10543 vim_free(tofree1);
10544 vim_free(tofree2);
10545 return res;
10546}
10547
10548 static int
10549#ifdef __BORLANDC__
10550_RTLENTRYF
10551#endif
10552item_compare2(const void *s1, const void *s2)
10553{
10554 sortItem_T *si1, *si2;
10555 int res;
10556 typval_T rettv;
10557 typval_T argv[3];
10558 int dummy;
10559 char_u *func_name;
10560 partial_T *partial = sortinfo->item_compare_partial;
10561
10562 /* shortcut after failure in previous call; compare all items equal */
10563 if (sortinfo->item_compare_func_err)
10564 return 0;
10565
10566 si1 = (sortItem_T *)s1;
10567 si2 = (sortItem_T *)s2;
10568
10569 if (partial == NULL)
10570 func_name = sortinfo->item_compare_func;
10571 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010572 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010573
10574 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10575 * in the copy without changing the original list items. */
10576 copy_tv(&si1->item->li_tv, &argv[0]);
10577 copy_tv(&si2->item->li_tv, &argv[1]);
10578
10579 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10580 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010581 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010582 partial, sortinfo->item_compare_selfdict);
10583 clear_tv(&argv[0]);
10584 clear_tv(&argv[1]);
10585
10586 if (res == FAIL)
10587 res = ITEM_COMPARE_FAIL;
10588 else
10589 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10590 if (sortinfo->item_compare_func_err)
10591 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10592 clear_tv(&rettv);
10593
10594 /* When the result would be zero, compare the pointers themselves. Makes
10595 * the sort stable. */
10596 if (res == 0 && !sortinfo->item_compare_keep_zero)
10597 res = si1->idx > si2->idx ? 1 : -1;
10598
10599 return res;
10600}
10601
10602/*
10603 * "sort({list})" function
10604 */
10605 static void
10606do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10607{
10608 list_T *l;
10609 listitem_T *li;
10610 sortItem_T *ptrs;
10611 sortinfo_T *old_sortinfo;
10612 sortinfo_T info;
10613 long len;
10614 long i;
10615
10616 /* Pointer to current info struct used in compare function. Save and
10617 * restore the current one for nested calls. */
10618 old_sortinfo = sortinfo;
10619 sortinfo = &info;
10620
10621 if (argvars[0].v_type != VAR_LIST)
10622 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
10623 else
10624 {
10625 l = argvars[0].vval.v_list;
10626 if (l == NULL || tv_check_lock(l->lv_lock,
10627 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
10628 TRUE))
10629 goto theend;
10630 rettv->vval.v_list = l;
10631 rettv->v_type = VAR_LIST;
10632 ++l->lv_refcount;
10633
10634 len = list_len(l);
10635 if (len <= 1)
10636 goto theend; /* short list sorts pretty quickly */
10637
10638 info.item_compare_ic = FALSE;
10639 info.item_compare_numeric = FALSE;
10640 info.item_compare_numbers = FALSE;
10641#ifdef FEAT_FLOAT
10642 info.item_compare_float = FALSE;
10643#endif
10644 info.item_compare_func = NULL;
10645 info.item_compare_partial = NULL;
10646 info.item_compare_selfdict = NULL;
10647 if (argvars[1].v_type != VAR_UNKNOWN)
10648 {
10649 /* optional second argument: {func} */
10650 if (argvars[1].v_type == VAR_FUNC)
10651 info.item_compare_func = argvars[1].vval.v_string;
10652 else if (argvars[1].v_type == VAR_PARTIAL)
10653 info.item_compare_partial = argvars[1].vval.v_partial;
10654 else
10655 {
10656 int error = FALSE;
10657
10658 i = (long)get_tv_number_chk(&argvars[1], &error);
10659 if (error)
10660 goto theend; /* type error; errmsg already given */
10661 if (i == 1)
10662 info.item_compare_ic = TRUE;
10663 else if (argvars[1].v_type != VAR_NUMBER)
10664 info.item_compare_func = get_tv_string(&argvars[1]);
10665 else if (i != 0)
10666 {
10667 EMSG(_(e_invarg));
10668 goto theend;
10669 }
10670 if (info.item_compare_func != NULL)
10671 {
10672 if (*info.item_compare_func == NUL)
10673 {
10674 /* empty string means default sort */
10675 info.item_compare_func = NULL;
10676 }
10677 else if (STRCMP(info.item_compare_func, "n") == 0)
10678 {
10679 info.item_compare_func = NULL;
10680 info.item_compare_numeric = TRUE;
10681 }
10682 else if (STRCMP(info.item_compare_func, "N") == 0)
10683 {
10684 info.item_compare_func = NULL;
10685 info.item_compare_numbers = TRUE;
10686 }
10687#ifdef FEAT_FLOAT
10688 else if (STRCMP(info.item_compare_func, "f") == 0)
10689 {
10690 info.item_compare_func = NULL;
10691 info.item_compare_float = TRUE;
10692 }
10693#endif
10694 else if (STRCMP(info.item_compare_func, "i") == 0)
10695 {
10696 info.item_compare_func = NULL;
10697 info.item_compare_ic = TRUE;
10698 }
10699 }
10700 }
10701
10702 if (argvars[2].v_type != VAR_UNKNOWN)
10703 {
10704 /* optional third argument: {dict} */
10705 if (argvars[2].v_type != VAR_DICT)
10706 {
10707 EMSG(_(e_dictreq));
10708 goto theend;
10709 }
10710 info.item_compare_selfdict = argvars[2].vval.v_dict;
10711 }
10712 }
10713
10714 /* Make an array with each entry pointing to an item in the List. */
10715 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
10716 if (ptrs == NULL)
10717 goto theend;
10718
10719 i = 0;
10720 if (sort)
10721 {
10722 /* sort(): ptrs will be the list to sort */
10723 for (li = l->lv_first; li != NULL; li = li->li_next)
10724 {
10725 ptrs[i].item = li;
10726 ptrs[i].idx = i;
10727 ++i;
10728 }
10729
10730 info.item_compare_func_err = FALSE;
10731 info.item_compare_keep_zero = FALSE;
10732 /* test the compare function */
10733 if ((info.item_compare_func != NULL
10734 || info.item_compare_partial != NULL)
10735 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
10736 == ITEM_COMPARE_FAIL)
10737 EMSG(_("E702: Sort compare function failed"));
10738 else
10739 {
10740 /* Sort the array with item pointers. */
10741 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
10742 info.item_compare_func == NULL
10743 && info.item_compare_partial == NULL
10744 ? item_compare : item_compare2);
10745
10746 if (!info.item_compare_func_err)
10747 {
10748 /* Clear the List and append the items in sorted order. */
10749 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
10750 l->lv_len = 0;
10751 for (i = 0; i < len; ++i)
10752 list_append(l, ptrs[i].item);
10753 }
10754 }
10755 }
10756 else
10757 {
10758 int (*item_compare_func_ptr)(const void *, const void *);
10759
10760 /* f_uniq(): ptrs will be a stack of items to remove */
10761 info.item_compare_func_err = FALSE;
10762 info.item_compare_keep_zero = TRUE;
10763 item_compare_func_ptr = info.item_compare_func != NULL
10764 || info.item_compare_partial != NULL
10765 ? item_compare2 : item_compare;
10766
10767 for (li = l->lv_first; li != NULL && li->li_next != NULL;
10768 li = li->li_next)
10769 {
10770 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
10771 == 0)
10772 ptrs[i++].item = li;
10773 if (info.item_compare_func_err)
10774 {
10775 EMSG(_("E882: Uniq compare function failed"));
10776 break;
10777 }
10778 }
10779
10780 if (!info.item_compare_func_err)
10781 {
10782 while (--i >= 0)
10783 {
10784 li = ptrs[i].item->li_next;
10785 ptrs[i].item->li_next = li->li_next;
10786 if (li->li_next != NULL)
10787 li->li_next->li_prev = ptrs[i].item;
10788 else
10789 l->lv_last = ptrs[i].item;
10790 list_fix_watch(l, li);
10791 listitem_free(li);
10792 l->lv_len--;
10793 }
10794 }
10795 }
10796
10797 vim_free(ptrs);
10798 }
10799theend:
10800 sortinfo = old_sortinfo;
10801}
10802
10803/*
10804 * "sort({list})" function
10805 */
10806 static void
10807f_sort(typval_T *argvars, typval_T *rettv)
10808{
10809 do_sort_uniq(argvars, rettv, TRUE);
10810}
10811
10812/*
10813 * "uniq({list})" function
10814 */
10815 static void
10816f_uniq(typval_T *argvars, typval_T *rettv)
10817{
10818 do_sort_uniq(argvars, rettv, FALSE);
10819}
10820
10821/*
10822 * "soundfold({word})" function
10823 */
10824 static void
10825f_soundfold(typval_T *argvars, typval_T *rettv)
10826{
10827 char_u *s;
10828
10829 rettv->v_type = VAR_STRING;
10830 s = get_tv_string(&argvars[0]);
10831#ifdef FEAT_SPELL
10832 rettv->vval.v_string = eval_soundfold(s);
10833#else
10834 rettv->vval.v_string = vim_strsave(s);
10835#endif
10836}
10837
10838/*
10839 * "spellbadword()" function
10840 */
10841 static void
10842f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10843{
10844 char_u *word = (char_u *)"";
10845 hlf_T attr = HLF_COUNT;
10846 int len = 0;
10847
10848 if (rettv_list_alloc(rettv) == FAIL)
10849 return;
10850
10851#ifdef FEAT_SPELL
10852 if (argvars[0].v_type == VAR_UNKNOWN)
10853 {
10854 /* Find the start and length of the badly spelled word. */
10855 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10856 if (len != 0)
10857 word = ml_get_cursor();
10858 }
10859 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10860 {
10861 char_u *str = get_tv_string_chk(&argvars[0]);
10862 int capcol = -1;
10863
10864 if (str != NULL)
10865 {
10866 /* Check the argument for spelling. */
10867 while (*str != NUL)
10868 {
10869 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10870 if (attr != HLF_COUNT)
10871 {
10872 word = str;
10873 break;
10874 }
10875 str += len;
10876 }
10877 }
10878 }
10879#endif
10880
10881 list_append_string(rettv->vval.v_list, word, len);
10882 list_append_string(rettv->vval.v_list, (char_u *)(
10883 attr == HLF_SPB ? "bad" :
10884 attr == HLF_SPR ? "rare" :
10885 attr == HLF_SPL ? "local" :
10886 attr == HLF_SPC ? "caps" :
10887 ""), -1);
10888}
10889
10890/*
10891 * "spellsuggest()" function
10892 */
10893 static void
10894f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
10895{
10896#ifdef FEAT_SPELL
10897 char_u *str;
10898 int typeerr = FALSE;
10899 int maxcount;
10900 garray_T ga;
10901 int i;
10902 listitem_T *li;
10903 int need_capital = FALSE;
10904#endif
10905
10906 if (rettv_list_alloc(rettv) == FAIL)
10907 return;
10908
10909#ifdef FEAT_SPELL
10910 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
10911 {
10912 str = get_tv_string(&argvars[0]);
10913 if (argvars[1].v_type != VAR_UNKNOWN)
10914 {
10915 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
10916 if (maxcount <= 0)
10917 return;
10918 if (argvars[2].v_type != VAR_UNKNOWN)
10919 {
10920 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
10921 if (typeerr)
10922 return;
10923 }
10924 }
10925 else
10926 maxcount = 25;
10927
10928 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
10929
10930 for (i = 0; i < ga.ga_len; ++i)
10931 {
10932 str = ((char_u **)ga.ga_data)[i];
10933
10934 li = listitem_alloc();
10935 if (li == NULL)
10936 vim_free(str);
10937 else
10938 {
10939 li->li_tv.v_type = VAR_STRING;
10940 li->li_tv.v_lock = 0;
10941 li->li_tv.vval.v_string = str;
10942 list_append(rettv->vval.v_list, li);
10943 }
10944 }
10945 ga_clear(&ga);
10946 }
10947#endif
10948}
10949
10950 static void
10951f_split(typval_T *argvars, typval_T *rettv)
10952{
10953 char_u *str;
10954 char_u *end;
10955 char_u *pat = NULL;
10956 regmatch_T regmatch;
10957 char_u patbuf[NUMBUFLEN];
10958 char_u *save_cpo;
10959 int match;
10960 colnr_T col = 0;
10961 int keepempty = FALSE;
10962 int typeerr = FALSE;
10963
10964 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10965 save_cpo = p_cpo;
10966 p_cpo = (char_u *)"";
10967
10968 str = get_tv_string(&argvars[0]);
10969 if (argvars[1].v_type != VAR_UNKNOWN)
10970 {
10971 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
10972 if (pat == NULL)
10973 typeerr = TRUE;
10974 if (argvars[2].v_type != VAR_UNKNOWN)
10975 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
10976 }
10977 if (pat == NULL || *pat == NUL)
10978 pat = (char_u *)"[\\x01- ]\\+";
10979
10980 if (rettv_list_alloc(rettv) == FAIL)
10981 return;
10982 if (typeerr)
10983 return;
10984
10985 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10986 if (regmatch.regprog != NULL)
10987 {
10988 regmatch.rm_ic = FALSE;
10989 while (*str != NUL || keepempty)
10990 {
10991 if (*str == NUL)
10992 match = FALSE; /* empty item at the end */
10993 else
10994 match = vim_regexec_nl(&regmatch, str, col);
10995 if (match)
10996 end = regmatch.startp[0];
10997 else
10998 end = str + STRLEN(str);
10999 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11000 && *str != NUL && match && end < regmatch.endp[0]))
11001 {
11002 if (list_append_string(rettv->vval.v_list, str,
11003 (int)(end - str)) == FAIL)
11004 break;
11005 }
11006 if (!match)
11007 break;
11008 /* Advance to just after the match. */
11009 if (regmatch.endp[0] > str)
11010 col = 0;
11011 else
11012 {
11013 /* Don't get stuck at the same match. */
11014#ifdef FEAT_MBYTE
11015 col = (*mb_ptr2len)(regmatch.endp[0]);
11016#else
11017 col = 1;
11018#endif
11019 }
11020 str = regmatch.endp[0];
11021 }
11022
11023 vim_regfree(regmatch.regprog);
11024 }
11025
11026 p_cpo = save_cpo;
11027}
11028
11029#ifdef FEAT_FLOAT
11030/*
11031 * "sqrt()" function
11032 */
11033 static void
11034f_sqrt(typval_T *argvars, typval_T *rettv)
11035{
11036 float_T f = 0.0;
11037
11038 rettv->v_type = VAR_FLOAT;
11039 if (get_float_arg(argvars, &f) == OK)
11040 rettv->vval.v_float = sqrt(f);
11041 else
11042 rettv->vval.v_float = 0.0;
11043}
11044
11045/*
11046 * "str2float()" function
11047 */
11048 static void
11049f_str2float(typval_T *argvars, typval_T *rettv)
11050{
11051 char_u *p = skipwhite(get_tv_string(&argvars[0]));
11052
11053 if (*p == '+')
11054 p = skipwhite(p + 1);
11055 (void)string2float(p, &rettv->vval.v_float);
11056 rettv->v_type = VAR_FLOAT;
11057}
11058#endif
11059
11060/*
11061 * "str2nr()" function
11062 */
11063 static void
11064f_str2nr(typval_T *argvars, typval_T *rettv)
11065{
11066 int base = 10;
11067 char_u *p;
11068 varnumber_T n;
11069 int what;
11070
11071 if (argvars[1].v_type != VAR_UNKNOWN)
11072 {
11073 base = (int)get_tv_number(&argvars[1]);
11074 if (base != 2 && base != 8 && base != 10 && base != 16)
11075 {
11076 EMSG(_(e_invarg));
11077 return;
11078 }
11079 }
11080
11081 p = skipwhite(get_tv_string(&argvars[0]));
11082 if (*p == '+')
11083 p = skipwhite(p + 1);
11084 switch (base)
11085 {
11086 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11087 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11088 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11089 default: what = 0;
11090 }
11091 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
11092 rettv->vval.v_number = n;
11093}
11094
11095#ifdef HAVE_STRFTIME
11096/*
11097 * "strftime({format}[, {time}])" function
11098 */
11099 static void
11100f_strftime(typval_T *argvars, typval_T *rettv)
11101{
11102 char_u result_buf[256];
11103 struct tm *curtime;
11104 time_t seconds;
11105 char_u *p;
11106
11107 rettv->v_type = VAR_STRING;
11108
11109 p = get_tv_string(&argvars[0]);
11110 if (argvars[1].v_type == VAR_UNKNOWN)
11111 seconds = time(NULL);
11112 else
11113 seconds = (time_t)get_tv_number(&argvars[1]);
11114 curtime = localtime(&seconds);
11115 /* MSVC returns NULL for an invalid value of seconds. */
11116 if (curtime == NULL)
11117 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11118 else
11119 {
11120# ifdef FEAT_MBYTE
11121 vimconv_T conv;
11122 char_u *enc;
11123
11124 conv.vc_type = CONV_NONE;
11125 enc = enc_locale();
11126 convert_setup(&conv, p_enc, enc);
11127 if (conv.vc_type != CONV_NONE)
11128 p = string_convert(&conv, p, NULL);
11129# endif
11130 if (p != NULL)
11131 (void)strftime((char *)result_buf, sizeof(result_buf),
11132 (char *)p, curtime);
11133 else
11134 result_buf[0] = NUL;
11135
11136# ifdef FEAT_MBYTE
11137 if (conv.vc_type != CONV_NONE)
11138 vim_free(p);
11139 convert_setup(&conv, enc, p_enc);
11140 if (conv.vc_type != CONV_NONE)
11141 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11142 else
11143# endif
11144 rettv->vval.v_string = vim_strsave(result_buf);
11145
11146# ifdef FEAT_MBYTE
11147 /* Release conversion descriptors */
11148 convert_setup(&conv, NULL, NULL);
11149 vim_free(enc);
11150# endif
11151 }
11152}
11153#endif
11154
11155/*
11156 * "strgetchar()" function
11157 */
11158 static void
11159f_strgetchar(typval_T *argvars, typval_T *rettv)
11160{
11161 char_u *str;
11162 int len;
11163 int error = FALSE;
11164 int charidx;
11165
11166 rettv->vval.v_number = -1;
11167 str = get_tv_string_chk(&argvars[0]);
11168 if (str == NULL)
11169 return;
11170 len = (int)STRLEN(str);
11171 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11172 if (error)
11173 return;
11174#ifdef FEAT_MBYTE
11175 {
11176 int byteidx = 0;
11177
11178 while (charidx >= 0 && byteidx < len)
11179 {
11180 if (charidx == 0)
11181 {
11182 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11183 break;
11184 }
11185 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011186 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011187 }
11188 }
11189#else
11190 if (charidx < len)
11191 rettv->vval.v_number = str[charidx];
11192#endif
11193}
11194
11195/*
11196 * "stridx()" function
11197 */
11198 static void
11199f_stridx(typval_T *argvars, typval_T *rettv)
11200{
11201 char_u buf[NUMBUFLEN];
11202 char_u *needle;
11203 char_u *haystack;
11204 char_u *save_haystack;
11205 char_u *pos;
11206 int start_idx;
11207
11208 needle = get_tv_string_chk(&argvars[1]);
11209 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11210 rettv->vval.v_number = -1;
11211 if (needle == NULL || haystack == NULL)
11212 return; /* type error; errmsg already given */
11213
11214 if (argvars[2].v_type != VAR_UNKNOWN)
11215 {
11216 int error = FALSE;
11217
11218 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11219 if (error || start_idx >= (int)STRLEN(haystack))
11220 return;
11221 if (start_idx >= 0)
11222 haystack += start_idx;
11223 }
11224
11225 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11226 if (pos != NULL)
11227 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11228}
11229
11230/*
11231 * "string()" function
11232 */
11233 static void
11234f_string(typval_T *argvars, typval_T *rettv)
11235{
11236 char_u *tofree;
11237 char_u numbuf[NUMBUFLEN];
11238
11239 rettv->v_type = VAR_STRING;
11240 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11241 get_copyID());
11242 /* Make a copy if we have a value but it's not in allocated memory. */
11243 if (rettv->vval.v_string != NULL && tofree == NULL)
11244 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11245}
11246
11247/*
11248 * "strlen()" function
11249 */
11250 static void
11251f_strlen(typval_T *argvars, typval_T *rettv)
11252{
11253 rettv->vval.v_number = (varnumber_T)(STRLEN(
11254 get_tv_string(&argvars[0])));
11255}
11256
11257/*
11258 * "strchars()" function
11259 */
11260 static void
11261f_strchars(typval_T *argvars, typval_T *rettv)
11262{
11263 char_u *s = get_tv_string(&argvars[0]);
11264 int skipcc = 0;
11265#ifdef FEAT_MBYTE
11266 varnumber_T len = 0;
11267 int (*func_mb_ptr2char_adv)(char_u **pp);
11268#endif
11269
11270 if (argvars[1].v_type != VAR_UNKNOWN)
11271 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11272 if (skipcc < 0 || skipcc > 1)
11273 EMSG(_(e_invarg));
11274 else
11275 {
11276#ifdef FEAT_MBYTE
11277 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11278 while (*s != NUL)
11279 {
11280 func_mb_ptr2char_adv(&s);
11281 ++len;
11282 }
11283 rettv->vval.v_number = len;
11284#else
11285 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11286#endif
11287 }
11288}
11289
11290/*
11291 * "strdisplaywidth()" function
11292 */
11293 static void
11294f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11295{
11296 char_u *s = get_tv_string(&argvars[0]);
11297 int col = 0;
11298
11299 if (argvars[1].v_type != VAR_UNKNOWN)
11300 col = (int)get_tv_number(&argvars[1]);
11301
11302 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11303}
11304
11305/*
11306 * "strwidth()" function
11307 */
11308 static void
11309f_strwidth(typval_T *argvars, typval_T *rettv)
11310{
11311 char_u *s = get_tv_string(&argvars[0]);
11312
11313 rettv->vval.v_number = (varnumber_T)(
11314#ifdef FEAT_MBYTE
11315 mb_string2cells(s, -1)
11316#else
11317 STRLEN(s)
11318#endif
11319 );
11320}
11321
11322/*
11323 * "strcharpart()" function
11324 */
11325 static void
11326f_strcharpart(typval_T *argvars, typval_T *rettv)
11327{
11328#ifdef FEAT_MBYTE
11329 char_u *p;
11330 int nchar;
11331 int nbyte = 0;
11332 int charlen;
11333 int len = 0;
11334 int slen;
11335 int error = FALSE;
11336
11337 p = get_tv_string(&argvars[0]);
11338 slen = (int)STRLEN(p);
11339
11340 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11341 if (!error)
11342 {
11343 if (nchar > 0)
11344 while (nchar > 0 && nbyte < slen)
11345 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011346 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011347 --nchar;
11348 }
11349 else
11350 nbyte = nchar;
11351 if (argvars[2].v_type != VAR_UNKNOWN)
11352 {
11353 charlen = (int)get_tv_number(&argvars[2]);
11354 while (charlen > 0 && nbyte + len < slen)
11355 {
11356 int off = nbyte + len;
11357
11358 if (off < 0)
11359 len += 1;
11360 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011361 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011362 --charlen;
11363 }
11364 }
11365 else
11366 len = slen - nbyte; /* default: all bytes that are available. */
11367 }
11368
11369 /*
11370 * Only return the overlap between the specified part and the actual
11371 * string.
11372 */
11373 if (nbyte < 0)
11374 {
11375 len += nbyte;
11376 nbyte = 0;
11377 }
11378 else if (nbyte > slen)
11379 nbyte = slen;
11380 if (len < 0)
11381 len = 0;
11382 else if (nbyte + len > slen)
11383 len = slen - nbyte;
11384
11385 rettv->v_type = VAR_STRING;
11386 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11387#else
11388 f_strpart(argvars, rettv);
11389#endif
11390}
11391
11392/*
11393 * "strpart()" function
11394 */
11395 static void
11396f_strpart(typval_T *argvars, typval_T *rettv)
11397{
11398 char_u *p;
11399 int n;
11400 int len;
11401 int slen;
11402 int error = FALSE;
11403
11404 p = get_tv_string(&argvars[0]);
11405 slen = (int)STRLEN(p);
11406
11407 n = (int)get_tv_number_chk(&argvars[1], &error);
11408 if (error)
11409 len = 0;
11410 else if (argvars[2].v_type != VAR_UNKNOWN)
11411 len = (int)get_tv_number(&argvars[2]);
11412 else
11413 len = slen - n; /* default len: all bytes that are available. */
11414
11415 /*
11416 * Only return the overlap between the specified part and the actual
11417 * string.
11418 */
11419 if (n < 0)
11420 {
11421 len += n;
11422 n = 0;
11423 }
11424 else if (n > slen)
11425 n = slen;
11426 if (len < 0)
11427 len = 0;
11428 else if (n + len > slen)
11429 len = slen - n;
11430
11431 rettv->v_type = VAR_STRING;
11432 rettv->vval.v_string = vim_strnsave(p + n, len);
11433}
11434
11435/*
11436 * "strridx()" function
11437 */
11438 static void
11439f_strridx(typval_T *argvars, typval_T *rettv)
11440{
11441 char_u buf[NUMBUFLEN];
11442 char_u *needle;
11443 char_u *haystack;
11444 char_u *rest;
11445 char_u *lastmatch = NULL;
11446 int haystack_len, end_idx;
11447
11448 needle = get_tv_string_chk(&argvars[1]);
11449 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11450
11451 rettv->vval.v_number = -1;
11452 if (needle == NULL || haystack == NULL)
11453 return; /* type error; errmsg already given */
11454
11455 haystack_len = (int)STRLEN(haystack);
11456 if (argvars[2].v_type != VAR_UNKNOWN)
11457 {
11458 /* Third argument: upper limit for index */
11459 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11460 if (end_idx < 0)
11461 return; /* can never find a match */
11462 }
11463 else
11464 end_idx = haystack_len;
11465
11466 if (*needle == NUL)
11467 {
11468 /* Empty string matches past the end. */
11469 lastmatch = haystack + end_idx;
11470 }
11471 else
11472 {
11473 for (rest = haystack; *rest != '\0'; ++rest)
11474 {
11475 rest = (char_u *)strstr((char *)rest, (char *)needle);
11476 if (rest == NULL || rest > haystack + end_idx)
11477 break;
11478 lastmatch = rest;
11479 }
11480 }
11481
11482 if (lastmatch == NULL)
11483 rettv->vval.v_number = -1;
11484 else
11485 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11486}
11487
11488/*
11489 * "strtrans()" function
11490 */
11491 static void
11492f_strtrans(typval_T *argvars, typval_T *rettv)
11493{
11494 rettv->v_type = VAR_STRING;
11495 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11496}
11497
11498/*
11499 * "submatch()" function
11500 */
11501 static void
11502f_submatch(typval_T *argvars, typval_T *rettv)
11503{
11504 int error = FALSE;
11505 int no;
11506 int retList = 0;
11507
11508 no = (int)get_tv_number_chk(&argvars[0], &error);
11509 if (error)
11510 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011511 if (no < 0 || no >= NSUBEXP)
11512 {
11513 EMSGN(_("E935: invalid submatch number: %d"), no);
11514 return;
11515 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011516 if (argvars[1].v_type != VAR_UNKNOWN)
11517 retList = (int)get_tv_number_chk(&argvars[1], &error);
11518 if (error)
11519 return;
11520
11521 if (retList == 0)
11522 {
11523 rettv->v_type = VAR_STRING;
11524 rettv->vval.v_string = reg_submatch(no);
11525 }
11526 else
11527 {
11528 rettv->v_type = VAR_LIST;
11529 rettv->vval.v_list = reg_submatch_list(no);
11530 }
11531}
11532
11533/*
11534 * "substitute()" function
11535 */
11536 static void
11537f_substitute(typval_T *argvars, typval_T *rettv)
11538{
11539 char_u patbuf[NUMBUFLEN];
11540 char_u subbuf[NUMBUFLEN];
11541 char_u flagsbuf[NUMBUFLEN];
11542
11543 char_u *str = get_tv_string_chk(&argvars[0]);
11544 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011545 char_u *sub = NULL;
11546 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011547 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11548
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011549 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11550 expr = &argvars[2];
11551 else
11552 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11553
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011554 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011555 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11556 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011557 rettv->vval.v_string = NULL;
11558 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011559 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011560}
11561
11562/*
11563 * "synID(lnum, col, trans)" function
11564 */
11565 static void
11566f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11567{
11568 int id = 0;
11569#ifdef FEAT_SYN_HL
11570 linenr_T lnum;
11571 colnr_T col;
11572 int trans;
11573 int transerr = FALSE;
11574
11575 lnum = get_tv_lnum(argvars); /* -1 on type error */
11576 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11577 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11578
11579 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11580 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11581 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11582#endif
11583
11584 rettv->vval.v_number = id;
11585}
11586
11587/*
11588 * "synIDattr(id, what [, mode])" function
11589 */
11590 static void
11591f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11592{
11593 char_u *p = NULL;
11594#ifdef FEAT_SYN_HL
11595 int id;
11596 char_u *what;
11597 char_u *mode;
11598 char_u modebuf[NUMBUFLEN];
11599 int modec;
11600
11601 id = (int)get_tv_number(&argvars[0]);
11602 what = get_tv_string(&argvars[1]);
11603 if (argvars[2].v_type != VAR_UNKNOWN)
11604 {
11605 mode = get_tv_string_buf(&argvars[2], modebuf);
11606 modec = TOLOWER_ASC(mode[0]);
11607 if (modec != 't' && modec != 'c' && modec != 'g')
11608 modec = 0; /* replace invalid with current */
11609 }
11610 else
11611 {
11612#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11613 if (USE_24BIT)
11614 modec = 'g';
11615 else
11616#endif
11617 if (t_colors > 1)
11618 modec = 'c';
11619 else
11620 modec = 't';
11621 }
11622
11623
11624 switch (TOLOWER_ASC(what[0]))
11625 {
11626 case 'b':
11627 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11628 p = highlight_color(id, what, modec);
11629 else /* bold */
11630 p = highlight_has_attr(id, HL_BOLD, modec);
11631 break;
11632
11633 case 'f': /* fg[#] or font */
11634 p = highlight_color(id, what, modec);
11635 break;
11636
11637 case 'i':
11638 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11639 p = highlight_has_attr(id, HL_INVERSE, modec);
11640 else /* italic */
11641 p = highlight_has_attr(id, HL_ITALIC, modec);
11642 break;
11643
11644 case 'n': /* name */
11645 p = get_highlight_name(NULL, id - 1);
11646 break;
11647
11648 case 'r': /* reverse */
11649 p = highlight_has_attr(id, HL_INVERSE, modec);
11650 break;
11651
11652 case 's':
11653 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11654 p = highlight_color(id, what, modec);
11655 else /* standout */
11656 p = highlight_has_attr(id, HL_STANDOUT, modec);
11657 break;
11658
11659 case 'u':
11660 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11661 /* underline */
11662 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11663 else
11664 /* undercurl */
11665 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11666 break;
11667 }
11668
11669 if (p != NULL)
11670 p = vim_strsave(p);
11671#endif
11672 rettv->v_type = VAR_STRING;
11673 rettv->vval.v_string = p;
11674}
11675
11676/*
11677 * "synIDtrans(id)" function
11678 */
11679 static void
11680f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11681{
11682 int id;
11683
11684#ifdef FEAT_SYN_HL
11685 id = (int)get_tv_number(&argvars[0]);
11686
11687 if (id > 0)
11688 id = syn_get_final_id(id);
11689 else
11690#endif
11691 id = 0;
11692
11693 rettv->vval.v_number = id;
11694}
11695
11696/*
11697 * "synconcealed(lnum, col)" function
11698 */
11699 static void
11700f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11701{
11702#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11703 linenr_T lnum;
11704 colnr_T col;
11705 int syntax_flags = 0;
11706 int cchar;
11707 int matchid = 0;
11708 char_u str[NUMBUFLEN];
11709#endif
11710
11711 rettv->v_type = VAR_LIST;
11712 rettv->vval.v_list = NULL;
11713
11714#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11715 lnum = get_tv_lnum(argvars); /* -1 on type error */
11716 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11717
11718 vim_memset(str, NUL, sizeof(str));
11719
11720 if (rettv_list_alloc(rettv) != FAIL)
11721 {
11722 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11723 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11724 && curwin->w_p_cole > 0)
11725 {
11726 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11727 syntax_flags = get_syntax_info(&matchid);
11728
11729 /* get the conceal character */
11730 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11731 {
11732 cchar = syn_get_sub_char();
11733 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
11734 cchar = lcs_conceal;
11735 if (cchar != NUL)
11736 {
11737# ifdef FEAT_MBYTE
11738 if (has_mbyte)
11739 (*mb_char2bytes)(cchar, str);
11740 else
11741# endif
11742 str[0] = cchar;
11743 }
11744 }
11745 }
11746
11747 list_append_number(rettv->vval.v_list,
11748 (syntax_flags & HL_CONCEAL) != 0);
11749 /* -1 to auto-determine strlen */
11750 list_append_string(rettv->vval.v_list, str, -1);
11751 list_append_number(rettv->vval.v_list, matchid);
11752 }
11753#endif
11754}
11755
11756/*
11757 * "synstack(lnum, col)" function
11758 */
11759 static void
11760f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11761{
11762#ifdef FEAT_SYN_HL
11763 linenr_T lnum;
11764 colnr_T col;
11765 int i;
11766 int id;
11767#endif
11768
11769 rettv->v_type = VAR_LIST;
11770 rettv->vval.v_list = NULL;
11771
11772#ifdef FEAT_SYN_HL
11773 lnum = get_tv_lnum(argvars); /* -1 on type error */
11774 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11775
11776 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11777 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11778 && rettv_list_alloc(rettv) != FAIL)
11779 {
11780 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11781 for (i = 0; ; ++i)
11782 {
11783 id = syn_get_stack_item(i);
11784 if (id < 0)
11785 break;
11786 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11787 break;
11788 }
11789 }
11790#endif
11791}
11792
11793 static void
11794get_cmd_output_as_rettv(
11795 typval_T *argvars,
11796 typval_T *rettv,
11797 int retlist)
11798{
11799 char_u *res = NULL;
11800 char_u *p;
11801 char_u *infile = NULL;
11802 char_u buf[NUMBUFLEN];
11803 int err = FALSE;
11804 FILE *fd;
11805 list_T *list = NULL;
11806 int flags = SHELL_SILENT;
11807
11808 rettv->v_type = VAR_STRING;
11809 rettv->vval.v_string = NULL;
11810 if (check_restricted() || check_secure())
11811 goto errret;
11812
11813 if (argvars[1].v_type != VAR_UNKNOWN)
11814 {
11815 /*
11816 * Write the string to a temp file, to be used for input of the shell
11817 * command.
11818 */
11819 if ((infile = vim_tempname('i', TRUE)) == NULL)
11820 {
11821 EMSG(_(e_notmp));
11822 goto errret;
11823 }
11824
11825 fd = mch_fopen((char *)infile, WRITEBIN);
11826 if (fd == NULL)
11827 {
11828 EMSG2(_(e_notopen), infile);
11829 goto errret;
11830 }
11831 if (argvars[1].v_type == VAR_LIST)
11832 {
11833 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11834 err = TRUE;
11835 }
11836 else
11837 {
11838 size_t len;
11839
11840 p = get_tv_string_buf_chk(&argvars[1], buf);
11841 if (p == NULL)
11842 {
11843 fclose(fd);
11844 goto errret; /* type error; errmsg already given */
11845 }
11846 len = STRLEN(p);
11847 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11848 err = TRUE;
11849 }
11850 if (fclose(fd) != 0)
11851 err = TRUE;
11852 if (err)
11853 {
11854 EMSG(_("E677: Error writing temp file"));
11855 goto errret;
11856 }
11857 }
11858
11859 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
11860 * echoes typeahead, that messes up the display. */
11861 if (!msg_silent)
11862 flags += SHELL_COOKED;
11863
11864 if (retlist)
11865 {
11866 int len;
11867 listitem_T *li;
11868 char_u *s = NULL;
11869 char_u *start;
11870 char_u *end;
11871 int i;
11872
11873 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
11874 if (res == NULL)
11875 goto errret;
11876
11877 list = list_alloc();
11878 if (list == NULL)
11879 goto errret;
11880
11881 for (i = 0; i < len; ++i)
11882 {
11883 start = res + i;
11884 while (i < len && res[i] != NL)
11885 ++i;
11886 end = res + i;
11887
11888 s = alloc((unsigned)(end - start + 1));
11889 if (s == NULL)
11890 goto errret;
11891
11892 for (p = s; start < end; ++p, ++start)
11893 *p = *start == NUL ? NL : *start;
11894 *p = NUL;
11895
11896 li = listitem_alloc();
11897 if (li == NULL)
11898 {
11899 vim_free(s);
11900 goto errret;
11901 }
11902 li->li_tv.v_type = VAR_STRING;
11903 li->li_tv.v_lock = 0;
11904 li->li_tv.vval.v_string = s;
11905 list_append(list, li);
11906 }
11907
11908 ++list->lv_refcount;
11909 rettv->v_type = VAR_LIST;
11910 rettv->vval.v_list = list;
11911 list = NULL;
11912 }
11913 else
11914 {
11915 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
11916#ifdef USE_CR
11917 /* translate <CR> into <NL> */
11918 if (res != NULL)
11919 {
11920 char_u *s;
11921
11922 for (s = res; *s; ++s)
11923 {
11924 if (*s == CAR)
11925 *s = NL;
11926 }
11927 }
11928#else
11929# ifdef USE_CRNL
11930 /* translate <CR><NL> into <NL> */
11931 if (res != NULL)
11932 {
11933 char_u *s, *d;
11934
11935 d = res;
11936 for (s = res; *s; ++s)
11937 {
11938 if (s[0] == CAR && s[1] == NL)
11939 ++s;
11940 *d++ = *s;
11941 }
11942 *d = NUL;
11943 }
11944# endif
11945#endif
11946 rettv->vval.v_string = res;
11947 res = NULL;
11948 }
11949
11950errret:
11951 if (infile != NULL)
11952 {
11953 mch_remove(infile);
11954 vim_free(infile);
11955 }
11956 if (res != NULL)
11957 vim_free(res);
11958 if (list != NULL)
11959 list_free(list);
11960}
11961
11962/*
11963 * "system()" function
11964 */
11965 static void
11966f_system(typval_T *argvars, typval_T *rettv)
11967{
11968 get_cmd_output_as_rettv(argvars, rettv, FALSE);
11969}
11970
11971/*
11972 * "systemlist()" function
11973 */
11974 static void
11975f_systemlist(typval_T *argvars, typval_T *rettv)
11976{
11977 get_cmd_output_as_rettv(argvars, rettv, TRUE);
11978}
11979
11980/*
11981 * "tabpagebuflist()" function
11982 */
11983 static void
11984f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11985{
11986#ifdef FEAT_WINDOWS
11987 tabpage_T *tp;
11988 win_T *wp = NULL;
11989
11990 if (argvars[0].v_type == VAR_UNKNOWN)
11991 wp = firstwin;
11992 else
11993 {
11994 tp = find_tabpage((int)get_tv_number(&argvars[0]));
11995 if (tp != NULL)
11996 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
11997 }
11998 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
11999 {
12000 for (; wp != NULL; wp = wp->w_next)
12001 if (list_append_number(rettv->vval.v_list,
12002 wp->w_buffer->b_fnum) == FAIL)
12003 break;
12004 }
12005#endif
12006}
12007
12008
12009/*
12010 * "tabpagenr()" function
12011 */
12012 static void
12013f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12014{
12015 int nr = 1;
12016#ifdef FEAT_WINDOWS
12017 char_u *arg;
12018
12019 if (argvars[0].v_type != VAR_UNKNOWN)
12020 {
12021 arg = get_tv_string_chk(&argvars[0]);
12022 nr = 0;
12023 if (arg != NULL)
12024 {
12025 if (STRCMP(arg, "$") == 0)
12026 nr = tabpage_index(NULL) - 1;
12027 else
12028 EMSG2(_(e_invexpr2), arg);
12029 }
12030 }
12031 else
12032 nr = tabpage_index(curtab);
12033#endif
12034 rettv->vval.v_number = nr;
12035}
12036
12037
12038#ifdef FEAT_WINDOWS
12039static int get_winnr(tabpage_T *tp, typval_T *argvar);
12040
12041/*
12042 * Common code for tabpagewinnr() and winnr().
12043 */
12044 static int
12045get_winnr(tabpage_T *tp, typval_T *argvar)
12046{
12047 win_T *twin;
12048 int nr = 1;
12049 win_T *wp;
12050 char_u *arg;
12051
12052 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12053 if (argvar->v_type != VAR_UNKNOWN)
12054 {
12055 arg = get_tv_string_chk(argvar);
12056 if (arg == NULL)
12057 nr = 0; /* type error; errmsg already given */
12058 else if (STRCMP(arg, "$") == 0)
12059 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12060 else if (STRCMP(arg, "#") == 0)
12061 {
12062 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12063 if (twin == NULL)
12064 nr = 0;
12065 }
12066 else
12067 {
12068 EMSG2(_(e_invexpr2), arg);
12069 nr = 0;
12070 }
12071 }
12072
12073 if (nr > 0)
12074 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12075 wp != twin; wp = wp->w_next)
12076 {
12077 if (wp == NULL)
12078 {
12079 /* didn't find it in this tabpage */
12080 nr = 0;
12081 break;
12082 }
12083 ++nr;
12084 }
12085 return nr;
12086}
12087#endif
12088
12089/*
12090 * "tabpagewinnr()" function
12091 */
12092 static void
12093f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12094{
12095 int nr = 1;
12096#ifdef FEAT_WINDOWS
12097 tabpage_T *tp;
12098
12099 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12100 if (tp == NULL)
12101 nr = 0;
12102 else
12103 nr = get_winnr(tp, &argvars[1]);
12104#endif
12105 rettv->vval.v_number = nr;
12106}
12107
12108
12109/*
12110 * "tagfiles()" function
12111 */
12112 static void
12113f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12114{
12115 char_u *fname;
12116 tagname_T tn;
12117 int first;
12118
12119 if (rettv_list_alloc(rettv) == FAIL)
12120 return;
12121 fname = alloc(MAXPATHL);
12122 if (fname == NULL)
12123 return;
12124
12125 for (first = TRUE; ; first = FALSE)
12126 if (get_tagfname(&tn, first, fname) == FAIL
12127 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12128 break;
12129 tagname_free(&tn);
12130 vim_free(fname);
12131}
12132
12133/*
12134 * "taglist()" function
12135 */
12136 static void
12137f_taglist(typval_T *argvars, typval_T *rettv)
12138{
12139 char_u *tag_pattern;
12140
12141 tag_pattern = get_tv_string(&argvars[0]);
12142
12143 rettv->vval.v_number = FALSE;
12144 if (*tag_pattern == NUL)
12145 return;
12146
12147 if (rettv_list_alloc(rettv) == OK)
12148 (void)get_tags(rettv->vval.v_list, tag_pattern);
12149}
12150
12151/*
12152 * "tempname()" function
12153 */
12154 static void
12155f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12156{
12157 static int x = 'A';
12158
12159 rettv->v_type = VAR_STRING;
12160 rettv->vval.v_string = vim_tempname(x, FALSE);
12161
12162 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12163 * names. Skip 'I' and 'O', they are used for shell redirection. */
12164 do
12165 {
12166 if (x == 'Z')
12167 x = '0';
12168 else if (x == '9')
12169 x = 'A';
12170 else
12171 {
12172#ifdef EBCDIC
12173 if (x == 'I')
12174 x = 'J';
12175 else if (x == 'R')
12176 x = 'S';
12177 else
12178#endif
12179 ++x;
12180 }
12181 } while (x == 'I' || x == 'O');
12182}
12183
12184#ifdef FEAT_FLOAT
12185/*
12186 * "tan()" function
12187 */
12188 static void
12189f_tan(typval_T *argvars, typval_T *rettv)
12190{
12191 float_T f = 0.0;
12192
12193 rettv->v_type = VAR_FLOAT;
12194 if (get_float_arg(argvars, &f) == OK)
12195 rettv->vval.v_float = tan(f);
12196 else
12197 rettv->vval.v_float = 0.0;
12198}
12199
12200/*
12201 * "tanh()" function
12202 */
12203 static void
12204f_tanh(typval_T *argvars, typval_T *rettv)
12205{
12206 float_T f = 0.0;
12207
12208 rettv->v_type = VAR_FLOAT;
12209 if (get_float_arg(argvars, &f) == OK)
12210 rettv->vval.v_float = tanh(f);
12211 else
12212 rettv->vval.v_float = 0.0;
12213}
12214#endif
12215
12216/*
12217 * "test_alloc_fail(id, countdown, repeat)" function
12218 */
12219 static void
12220f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12221{
12222 if (argvars[0].v_type != VAR_NUMBER
12223 || argvars[0].vval.v_number <= 0
12224 || argvars[1].v_type != VAR_NUMBER
12225 || argvars[1].vval.v_number < 0
12226 || argvars[2].v_type != VAR_NUMBER)
12227 EMSG(_(e_invarg));
12228 else
12229 {
12230 alloc_fail_id = argvars[0].vval.v_number;
12231 if (alloc_fail_id >= aid_last)
12232 EMSG(_(e_invarg));
12233 alloc_fail_countdown = argvars[1].vval.v_number;
12234 alloc_fail_repeat = argvars[2].vval.v_number;
12235 did_outofmem_msg = FALSE;
12236 }
12237}
12238
12239/*
12240 * "test_autochdir()"
12241 */
12242 static void
12243f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12244{
12245#if defined(FEAT_AUTOCHDIR)
12246 test_autochdir = TRUE;
12247#endif
12248}
12249
12250/*
12251 * "test_disable_char_avail({expr})" function
12252 */
12253 static void
12254f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
12255{
12256 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
12257}
12258
12259/*
12260 * "test_garbagecollect_now()" function
12261 */
12262 static void
12263f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12264{
12265 /* This is dangerous, any Lists and Dicts used internally may be freed
12266 * while still in use. */
12267 garbage_collect(TRUE);
12268}
12269
12270#ifdef FEAT_JOB_CHANNEL
12271 static void
12272f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12273{
12274 rettv->v_type = VAR_CHANNEL;
12275 rettv->vval.v_channel = NULL;
12276}
12277#endif
12278
12279 static void
12280f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12281{
12282 rettv->v_type = VAR_DICT;
12283 rettv->vval.v_dict = NULL;
12284}
12285
12286#ifdef FEAT_JOB_CHANNEL
12287 static void
12288f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12289{
12290 rettv->v_type = VAR_JOB;
12291 rettv->vval.v_job = NULL;
12292}
12293#endif
12294
12295 static void
12296f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12297{
12298 rettv->v_type = VAR_LIST;
12299 rettv->vval.v_list = NULL;
12300}
12301
12302 static void
12303f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12304{
12305 rettv->v_type = VAR_PARTIAL;
12306 rettv->vval.v_partial = NULL;
12307}
12308
12309 static void
12310f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12311{
12312 rettv->v_type = VAR_STRING;
12313 rettv->vval.v_string = NULL;
12314}
12315
12316 static void
12317f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12318{
12319 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12320}
12321
12322#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12323/*
12324 * Get a callback from "arg". It can be a Funcref or a function name.
12325 * When "arg" is zero return an empty string.
12326 * Return NULL for an invalid argument.
12327 */
12328 char_u *
12329get_callback(typval_T *arg, partial_T **pp)
12330{
12331 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12332 {
12333 *pp = arg->vval.v_partial;
12334 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012335 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012336 }
12337 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012338 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012339 {
12340 func_ref(arg->vval.v_string);
12341 return arg->vval.v_string;
12342 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012343 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12344 return (char_u *)"";
12345 EMSG(_("E921: Invalid callback argument"));
12346 return NULL;
12347}
12348
12349/*
12350 * Unref/free "callback" and "partial" retured by get_callback().
12351 */
12352 void
12353free_callback(char_u *callback, partial_T *partial)
12354{
12355 if (partial != NULL)
12356 partial_unref(partial);
12357 else if (callback != NULL)
12358 {
12359 func_unref(callback);
12360 vim_free(callback);
12361 }
12362}
12363#endif
12364
12365#ifdef FEAT_TIMERS
12366/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012367 * "timer_info([timer])" function
12368 */
12369 static void
12370f_timer_info(typval_T *argvars, typval_T *rettv)
12371{
12372 timer_T *timer = NULL;
12373
12374 if (rettv_list_alloc(rettv) != OK)
12375 return;
12376 if (argvars[0].v_type != VAR_UNKNOWN)
12377 {
12378 if (argvars[0].v_type != VAR_NUMBER)
12379 EMSG(_(e_number_exp));
12380 else
12381 {
12382 timer = find_timer((int)get_tv_number(&argvars[0]));
12383 if (timer != NULL)
12384 add_timer_info(rettv, timer);
12385 }
12386 }
12387 else
12388 add_timer_info_all(rettv);
12389}
12390
12391/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012392 * "timer_pause(timer, paused)" function
12393 */
12394 static void
12395f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12396{
12397 timer_T *timer = NULL;
12398 int paused = (int)get_tv_number(&argvars[1]);
12399
12400 if (argvars[0].v_type != VAR_NUMBER)
12401 EMSG(_(e_number_exp));
12402 else
12403 {
12404 timer = find_timer((int)get_tv_number(&argvars[0]));
12405 if (timer != NULL)
12406 timer->tr_paused = paused;
12407 }
12408}
12409
12410/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012411 * "timer_start(time, callback [, options])" function
12412 */
12413 static void
12414f_timer_start(typval_T *argvars, typval_T *rettv)
12415{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012416 long msec = (long)get_tv_number(&argvars[0]);
12417 timer_T *timer;
12418 int repeat = 0;
12419 char_u *callback;
12420 dict_T *dict;
12421 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012422
Bram Moolenaar75537a92016-09-05 22:45:28 +020012423 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012424 if (check_secure())
12425 return;
12426 if (argvars[2].v_type != VAR_UNKNOWN)
12427 {
12428 if (argvars[2].v_type != VAR_DICT
12429 || (dict = argvars[2].vval.v_dict) == NULL)
12430 {
12431 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12432 return;
12433 }
12434 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12435 repeat = get_dict_number(dict, (char_u *)"repeat");
12436 }
12437
Bram Moolenaar75537a92016-09-05 22:45:28 +020012438 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012439 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012440 return;
12441
12442 timer = create_timer(msec, repeat);
12443 if (timer == NULL)
12444 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012445 else
12446 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012447 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012448 timer->tr_callback = vim_strsave(callback);
12449 else
12450 /* pointer into the partial */
12451 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012452 timer->tr_partial = partial;
12453 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012454 }
12455}
12456
12457/*
12458 * "timer_stop(timer)" function
12459 */
12460 static void
12461f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12462{
12463 timer_T *timer;
12464
12465 if (argvars[0].v_type != VAR_NUMBER)
12466 {
12467 EMSG(_(e_number_exp));
12468 return;
12469 }
12470 timer = find_timer((int)get_tv_number(&argvars[0]));
12471 if (timer != NULL)
12472 stop_timer(timer);
12473}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012474
12475/*
12476 * "timer_stopall()" function
12477 */
12478 static void
12479f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12480{
12481 stop_all_timers();
12482}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012483#endif
12484
12485/*
12486 * "tolower(string)" function
12487 */
12488 static void
12489f_tolower(typval_T *argvars, typval_T *rettv)
12490{
12491 char_u *p;
12492
12493 p = vim_strsave(get_tv_string(&argvars[0]));
12494 rettv->v_type = VAR_STRING;
12495 rettv->vval.v_string = p;
12496
12497 if (p != NULL)
12498 while (*p != NUL)
12499 {
12500#ifdef FEAT_MBYTE
12501 int l;
12502
12503 if (enc_utf8)
12504 {
12505 int c, lc;
12506
12507 c = utf_ptr2char(p);
12508 lc = utf_tolower(c);
12509 l = utf_ptr2len(p);
12510 /* TODO: reallocate string when byte count changes. */
12511 if (utf_char2len(lc) == l)
12512 utf_char2bytes(lc, p);
12513 p += l;
12514 }
12515 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
12516 p += l; /* skip multi-byte character */
12517 else
12518#endif
12519 {
12520 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
12521 ++p;
12522 }
12523 }
12524}
12525
12526/*
12527 * "toupper(string)" function
12528 */
12529 static void
12530f_toupper(typval_T *argvars, typval_T *rettv)
12531{
12532 rettv->v_type = VAR_STRING;
12533 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12534}
12535
12536/*
12537 * "tr(string, fromstr, tostr)" function
12538 */
12539 static void
12540f_tr(typval_T *argvars, typval_T *rettv)
12541{
12542 char_u *in_str;
12543 char_u *fromstr;
12544 char_u *tostr;
12545 char_u *p;
12546#ifdef FEAT_MBYTE
12547 int inlen;
12548 int fromlen;
12549 int tolen;
12550 int idx;
12551 char_u *cpstr;
12552 int cplen;
12553 int first = TRUE;
12554#endif
12555 char_u buf[NUMBUFLEN];
12556 char_u buf2[NUMBUFLEN];
12557 garray_T ga;
12558
12559 in_str = get_tv_string(&argvars[0]);
12560 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
12561 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
12562
12563 /* Default return value: empty string. */
12564 rettv->v_type = VAR_STRING;
12565 rettv->vval.v_string = NULL;
12566 if (fromstr == NULL || tostr == NULL)
12567 return; /* type error; errmsg already given */
12568 ga_init2(&ga, (int)sizeof(char), 80);
12569
12570#ifdef FEAT_MBYTE
12571 if (!has_mbyte)
12572#endif
12573 /* not multi-byte: fromstr and tostr must be the same length */
12574 if (STRLEN(fromstr) != STRLEN(tostr))
12575 {
12576#ifdef FEAT_MBYTE
12577error:
12578#endif
12579 EMSG2(_(e_invarg2), fromstr);
12580 ga_clear(&ga);
12581 return;
12582 }
12583
12584 /* fromstr and tostr have to contain the same number of chars */
12585 while (*in_str != NUL)
12586 {
12587#ifdef FEAT_MBYTE
12588 if (has_mbyte)
12589 {
12590 inlen = (*mb_ptr2len)(in_str);
12591 cpstr = in_str;
12592 cplen = inlen;
12593 idx = 0;
12594 for (p = fromstr; *p != NUL; p += fromlen)
12595 {
12596 fromlen = (*mb_ptr2len)(p);
12597 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12598 {
12599 for (p = tostr; *p != NUL; p += tolen)
12600 {
12601 tolen = (*mb_ptr2len)(p);
12602 if (idx-- == 0)
12603 {
12604 cplen = tolen;
12605 cpstr = p;
12606 break;
12607 }
12608 }
12609 if (*p == NUL) /* tostr is shorter than fromstr */
12610 goto error;
12611 break;
12612 }
12613 ++idx;
12614 }
12615
12616 if (first && cpstr == in_str)
12617 {
12618 /* Check that fromstr and tostr have the same number of
12619 * (multi-byte) characters. Done only once when a character
12620 * of in_str doesn't appear in fromstr. */
12621 first = FALSE;
12622 for (p = tostr; *p != NUL; p += tolen)
12623 {
12624 tolen = (*mb_ptr2len)(p);
12625 --idx;
12626 }
12627 if (idx != 0)
12628 goto error;
12629 }
12630
12631 (void)ga_grow(&ga, cplen);
12632 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12633 ga.ga_len += cplen;
12634
12635 in_str += inlen;
12636 }
12637 else
12638#endif
12639 {
12640 /* When not using multi-byte chars we can do it faster. */
12641 p = vim_strchr(fromstr, *in_str);
12642 if (p != NULL)
12643 ga_append(&ga, tostr[p - fromstr]);
12644 else
12645 ga_append(&ga, *in_str);
12646 ++in_str;
12647 }
12648 }
12649
12650 /* add a terminating NUL */
12651 (void)ga_grow(&ga, 1);
12652 ga_append(&ga, NUL);
12653
12654 rettv->vval.v_string = ga.ga_data;
12655}
12656
12657#ifdef FEAT_FLOAT
12658/*
12659 * "trunc({float})" function
12660 */
12661 static void
12662f_trunc(typval_T *argvars, typval_T *rettv)
12663{
12664 float_T f = 0.0;
12665
12666 rettv->v_type = VAR_FLOAT;
12667 if (get_float_arg(argvars, &f) == OK)
12668 /* trunc() is not in C90, use floor() or ceil() instead. */
12669 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12670 else
12671 rettv->vval.v_float = 0.0;
12672}
12673#endif
12674
12675/*
12676 * "type(expr)" function
12677 */
12678 static void
12679f_type(typval_T *argvars, typval_T *rettv)
12680{
12681 int n = -1;
12682
12683 switch (argvars[0].v_type)
12684 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012685 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12686 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012687 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012688 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12689 case VAR_LIST: n = VAR_TYPE_LIST; break;
12690 case VAR_DICT: n = VAR_TYPE_DICT; break;
12691 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012692 case VAR_SPECIAL:
12693 if (argvars[0].vval.v_number == VVAL_FALSE
12694 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012695 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012696 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012697 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012698 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012699 case VAR_JOB: n = VAR_TYPE_JOB; break;
12700 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012701 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012702 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012703 n = -1;
12704 break;
12705 }
12706 rettv->vval.v_number = n;
12707}
12708
12709/*
12710 * "undofile(name)" function
12711 */
12712 static void
12713f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12714{
12715 rettv->v_type = VAR_STRING;
12716#ifdef FEAT_PERSISTENT_UNDO
12717 {
12718 char_u *fname = get_tv_string(&argvars[0]);
12719
12720 if (*fname == NUL)
12721 {
12722 /* If there is no file name there will be no undo file. */
12723 rettv->vval.v_string = NULL;
12724 }
12725 else
12726 {
12727 char_u *ffname = FullName_save(fname, FALSE);
12728
12729 if (ffname != NULL)
12730 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12731 vim_free(ffname);
12732 }
12733 }
12734#else
12735 rettv->vval.v_string = NULL;
12736#endif
12737}
12738
12739/*
12740 * "undotree()" function
12741 */
12742 static void
12743f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12744{
12745 if (rettv_dict_alloc(rettv) == OK)
12746 {
12747 dict_T *dict = rettv->vval.v_dict;
12748 list_T *list;
12749
12750 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
12751 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
12752 dict_add_nr_str(dict, "save_last",
12753 (long)curbuf->b_u_save_nr_last, NULL);
12754 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
12755 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
12756 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
12757
12758 list = list_alloc();
12759 if (list != NULL)
12760 {
12761 u_eval_tree(curbuf->b_u_oldhead, list);
12762 dict_add_list(dict, "entries", list);
12763 }
12764 }
12765}
12766
12767/*
12768 * "values(dict)" function
12769 */
12770 static void
12771f_values(typval_T *argvars, typval_T *rettv)
12772{
12773 dict_list(argvars, rettv, 1);
12774}
12775
12776/*
12777 * "virtcol(string)" function
12778 */
12779 static void
12780f_virtcol(typval_T *argvars, typval_T *rettv)
12781{
12782 colnr_T vcol = 0;
12783 pos_T *fp;
12784 int fnum = curbuf->b_fnum;
12785
12786 fp = var2fpos(&argvars[0], FALSE, &fnum);
12787 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12788 && fnum == curbuf->b_fnum)
12789 {
12790 getvvcol(curwin, fp, NULL, NULL, &vcol);
12791 ++vcol;
12792 }
12793
12794 rettv->vval.v_number = vcol;
12795}
12796
12797/*
12798 * "visualmode()" function
12799 */
12800 static void
12801f_visualmode(typval_T *argvars, typval_T *rettv)
12802{
12803 char_u str[2];
12804
12805 rettv->v_type = VAR_STRING;
12806 str[0] = curbuf->b_visual_mode_eval;
12807 str[1] = NUL;
12808 rettv->vval.v_string = vim_strsave(str);
12809
12810 /* A non-zero number or non-empty string argument: reset mode. */
12811 if (non_zero_arg(&argvars[0]))
12812 curbuf->b_visual_mode_eval = NUL;
12813}
12814
12815/*
12816 * "wildmenumode()" function
12817 */
12818 static void
12819f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12820{
12821#ifdef FEAT_WILDMENU
12822 if (wild_menu_showing)
12823 rettv->vval.v_number = 1;
12824#endif
12825}
12826
12827/*
12828 * "winbufnr(nr)" function
12829 */
12830 static void
12831f_winbufnr(typval_T *argvars, typval_T *rettv)
12832{
12833 win_T *wp;
12834
12835 wp = find_win_by_nr(&argvars[0], NULL);
12836 if (wp == NULL)
12837 rettv->vval.v_number = -1;
12838 else
12839 rettv->vval.v_number = wp->w_buffer->b_fnum;
12840}
12841
12842/*
12843 * "wincol()" function
12844 */
12845 static void
12846f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12847{
12848 validate_cursor();
12849 rettv->vval.v_number = curwin->w_wcol + 1;
12850}
12851
12852/*
12853 * "winheight(nr)" function
12854 */
12855 static void
12856f_winheight(typval_T *argvars, typval_T *rettv)
12857{
12858 win_T *wp;
12859
12860 wp = find_win_by_nr(&argvars[0], NULL);
12861 if (wp == NULL)
12862 rettv->vval.v_number = -1;
12863 else
12864 rettv->vval.v_number = wp->w_height;
12865}
12866
12867/*
12868 * "winline()" function
12869 */
12870 static void
12871f_winline(typval_T *argvars UNUSED, typval_T *rettv)
12872{
12873 validate_cursor();
12874 rettv->vval.v_number = curwin->w_wrow + 1;
12875}
12876
12877/*
12878 * "winnr()" function
12879 */
12880 static void
12881f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
12882{
12883 int nr = 1;
12884
12885#ifdef FEAT_WINDOWS
12886 nr = get_winnr(curtab, &argvars[0]);
12887#endif
12888 rettv->vval.v_number = nr;
12889}
12890
12891/*
12892 * "winrestcmd()" function
12893 */
12894 static void
12895f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
12896{
12897#ifdef FEAT_WINDOWS
12898 win_T *wp;
12899 int winnr = 1;
12900 garray_T ga;
12901 char_u buf[50];
12902
12903 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020012904 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012905 {
12906 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12907 ga_concat(&ga, buf);
12908 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12909 ga_concat(&ga, buf);
12910 ++winnr;
12911 }
12912 ga_append(&ga, NUL);
12913
12914 rettv->vval.v_string = ga.ga_data;
12915#else
12916 rettv->vval.v_string = NULL;
12917#endif
12918 rettv->v_type = VAR_STRING;
12919}
12920
12921/*
12922 * "winrestview()" function
12923 */
12924 static void
12925f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
12926{
12927 dict_T *dict;
12928
12929 if (argvars[0].v_type != VAR_DICT
12930 || (dict = argvars[0].vval.v_dict) == NULL)
12931 EMSG(_(e_invarg));
12932 else
12933 {
12934 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
12935 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
12936 if (dict_find(dict, (char_u *)"col", -1) != NULL)
12937 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
12938#ifdef FEAT_VIRTUALEDIT
12939 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
12940 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
12941#endif
12942 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
12943 {
12944 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
12945 curwin->w_set_curswant = FALSE;
12946 }
12947
12948 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
12949 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
12950#ifdef FEAT_DIFF
12951 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
12952 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
12953#endif
12954 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
12955 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
12956 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
12957 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
12958
12959 check_cursor();
12960 win_new_height(curwin, curwin->w_height);
12961# ifdef FEAT_WINDOWS
12962 win_new_width(curwin, W_WIDTH(curwin));
12963# endif
12964 changed_window_setting();
12965
12966 if (curwin->w_topline <= 0)
12967 curwin->w_topline = 1;
12968 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
12969 curwin->w_topline = curbuf->b_ml.ml_line_count;
12970#ifdef FEAT_DIFF
12971 check_topfill(curwin, TRUE);
12972#endif
12973 }
12974}
12975
12976/*
12977 * "winsaveview()" function
12978 */
12979 static void
12980f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
12981{
12982 dict_T *dict;
12983
12984 if (rettv_dict_alloc(rettv) == FAIL)
12985 return;
12986 dict = rettv->vval.v_dict;
12987
12988 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
12989 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
12990#ifdef FEAT_VIRTUALEDIT
12991 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
12992#endif
12993 update_curswant();
12994 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
12995
12996 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
12997#ifdef FEAT_DIFF
12998 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
12999#endif
13000 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13001 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13002}
13003
13004/*
13005 * "winwidth(nr)" function
13006 */
13007 static void
13008f_winwidth(typval_T *argvars, typval_T *rettv)
13009{
13010 win_T *wp;
13011
13012 wp = find_win_by_nr(&argvars[0], NULL);
13013 if (wp == NULL)
13014 rettv->vval.v_number = -1;
13015 else
13016#ifdef FEAT_WINDOWS
13017 rettv->vval.v_number = wp->w_width;
13018#else
13019 rettv->vval.v_number = Columns;
13020#endif
13021}
13022
13023/*
13024 * "wordcount()" function
13025 */
13026 static void
13027f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13028{
13029 if (rettv_dict_alloc(rettv) == FAIL)
13030 return;
13031 cursor_pos_info(rettv->vval.v_dict);
13032}
13033
13034/*
13035 * "writefile()" function
13036 */
13037 static void
13038f_writefile(typval_T *argvars, typval_T *rettv)
13039{
13040 int binary = FALSE;
13041 int append = FALSE;
13042 char_u *fname;
13043 FILE *fd;
13044 int ret = 0;
13045
13046 if (check_restricted() || check_secure())
13047 return;
13048
13049 if (argvars[0].v_type != VAR_LIST)
13050 {
13051 EMSG2(_(e_listarg), "writefile()");
13052 return;
13053 }
13054 if (argvars[0].vval.v_list == NULL)
13055 return;
13056
13057 if (argvars[2].v_type != VAR_UNKNOWN)
13058 {
13059 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
13060 binary = TRUE;
13061 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
13062 append = TRUE;
13063 }
13064
13065 /* Always open the file in binary mode, library functions have a mind of
13066 * their own about CR-LF conversion. */
13067 fname = get_tv_string(&argvars[1]);
13068 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13069 append ? APPENDBIN : WRITEBIN)) == NULL)
13070 {
13071 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13072 ret = -1;
13073 }
13074 else
13075 {
13076 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
13077 ret = -1;
13078 fclose(fd);
13079 }
13080
13081 rettv->vval.v_number = ret;
13082}
13083
13084/*
13085 * "xor(expr, expr)" function
13086 */
13087 static void
13088f_xor(typval_T *argvars, typval_T *rettv)
13089{
13090 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13091 ^ get_tv_number_chk(&argvars[1], NULL);
13092}
13093
13094
13095#endif /* FEAT_EVAL */