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