blob: ba6f0f597f578672307952d5c9bc8b3e38dc5929 [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
3294 if (f < -0x7fffffffffffffff)
3295 rettv->vval.v_number = -0x7fffffffffffffff;
3296 else if (f > 0x7fffffffffffffff)
3297 rettv->vval.v_number = 0x7fffffffffffffff;
3298 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 Moolenaar437bafe2016-08-01 15:40:54 +02003590 if (((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL)
3591 || is_funcref))
3592 {
3593 name = s;
3594 trans_name = trans_function_name(&name, FALSE,
3595 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3596 if (*name != NUL)
3597 s = NULL;
3598 }
3599
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003600 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
3601 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;
3924 dict_T *opts;
3925 dict_T *vars;
3926 tabpage_T *tp;
3927 win_T *wp;
3928 list_T *windows;
3929
3930 dict = dict_alloc();
3931 if (dict == NULL)
3932 return NULL;
3933
3934 dict_add_nr_str(dict, "nr", buf->b_fnum, NULL);
3935 dict_add_nr_str(dict, "name", 0L,
3936 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
3937 dict_add_nr_str(dict, "lnum", buflist_findlnum(buf), NULL);
3938 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
3939 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
3940 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
3941 dict_add_nr_str(dict, "changedtick", buf->b_changedtick, NULL);
3942 dict_add_nr_str(dict, "hidden",
3943 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
3944 NULL);
3945
3946 /* Copy buffer variables */
3947 vars = dict_copy(buf->b_vars, TRUE, 0);
3948 if (vars != NULL)
3949 dict_add_dict(dict, "variables", vars);
3950
3951 /* Copy buffer options */
3952 opts = get_winbuf_options(TRUE);
3953 if (opts != NULL)
3954 dict_add_dict(dict, "options", opts);
3955
3956 /* List of windows displaying this buffer */
3957 windows = list_alloc();
3958 if (windows != NULL)
3959 {
3960 FOR_ALL_TAB_WINDOWS(tp, wp)
3961 if (wp->w_buffer == buf)
3962 list_append_number(windows, (varnumber_T)wp->w_id);
3963 dict_add_list(dict, "windows", windows);
3964 }
3965
3966#ifdef FEAT_SIGNS
3967 if (buf->b_signlist != NULL)
3968 {
3969 /* List of signs placed in this buffer */
3970 list_T *signs = list_alloc();
3971 if (signs != NULL)
3972 {
3973 get_buffer_signs(buf, signs);
3974 dict_add_list(dict, "signs", signs);
3975 }
3976 }
3977#endif
3978
3979 return dict;
3980}
3981
3982/*
3983 * "getbufinfo()" function
3984 */
3985 static void
3986f_getbufinfo(typval_T *argvars, typval_T *rettv)
3987{
3988 buf_T *buf = NULL;
3989 buf_T *argbuf = NULL;
3990 dict_T *d;
3991 int filtered = FALSE;
3992 int sel_buflisted = FALSE;
3993 int sel_bufloaded = FALSE;
3994
3995 if (rettv_list_alloc(rettv) != OK)
3996 return;
3997
3998 /* List of all the buffers or selected buffers */
3999 if (argvars[0].v_type == VAR_DICT)
4000 {
4001 dict_T *sel_d = argvars[0].vval.v_dict;
4002
4003 if (sel_d != NULL)
4004 {
4005 dictitem_T *di;
4006
4007 filtered = TRUE;
4008
4009 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4010 if (di != NULL && get_tv_number(&di->di_tv))
4011 sel_buflisted = TRUE;
4012
4013 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4014 if (di != NULL && get_tv_number(&di->di_tv))
4015 sel_bufloaded = TRUE;
4016 }
4017 }
4018 else if (argvars[0].v_type != VAR_UNKNOWN)
4019 {
4020 /* Information about one buffer. Argument specifies the buffer */
4021 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4022 ++emsg_off;
4023 argbuf = get_buf_tv(&argvars[0], FALSE);
4024 --emsg_off;
4025 if (argbuf == NULL)
4026 return;
4027 }
4028
4029 /* Return information about all the buffers or a specified buffer */
4030 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4031 {
4032 if (argbuf != NULL && argbuf != buf)
4033 continue;
4034 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
4035 || (sel_buflisted && !buf->b_p_bl)))
4036 continue;
4037
4038 d = get_buffer_info(buf);
4039 if (d != NULL)
4040 list_append_dict(rettv->vval.v_list, d);
4041 if (argbuf != NULL)
4042 return;
4043 }
4044}
4045
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004046static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4047
4048/*
4049 * Get line or list of lines from buffer "buf" into "rettv".
4050 * Return a range (from start to end) of lines in rettv from the specified
4051 * buffer.
4052 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4053 */
4054 static void
4055get_buffer_lines(
4056 buf_T *buf,
4057 linenr_T start,
4058 linenr_T end,
4059 int retlist,
4060 typval_T *rettv)
4061{
4062 char_u *p;
4063
4064 rettv->v_type = VAR_STRING;
4065 rettv->vval.v_string = NULL;
4066 if (retlist && rettv_list_alloc(rettv) == FAIL)
4067 return;
4068
4069 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4070 return;
4071
4072 if (!retlist)
4073 {
4074 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4075 p = ml_get_buf(buf, start, FALSE);
4076 else
4077 p = (char_u *)"";
4078 rettv->vval.v_string = vim_strsave(p);
4079 }
4080 else
4081 {
4082 if (end < start)
4083 return;
4084
4085 if (start < 1)
4086 start = 1;
4087 if (end > buf->b_ml.ml_line_count)
4088 end = buf->b_ml.ml_line_count;
4089 while (start <= end)
4090 if (list_append_string(rettv->vval.v_list,
4091 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4092 break;
4093 }
4094}
4095
4096/*
4097 * Get the lnum from the first argument.
4098 * Also accepts "$", then "buf" is used.
4099 * Returns 0 on error.
4100 */
4101 static linenr_T
4102get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4103{
4104 if (argvars[0].v_type == VAR_STRING
4105 && argvars[0].vval.v_string != NULL
4106 && argvars[0].vval.v_string[0] == '$'
4107 && buf != NULL)
4108 return buf->b_ml.ml_line_count;
4109 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4110}
4111
4112/*
4113 * "getbufline()" function
4114 */
4115 static void
4116f_getbufline(typval_T *argvars, typval_T *rettv)
4117{
4118 linenr_T lnum;
4119 linenr_T end;
4120 buf_T *buf;
4121
4122 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4123 ++emsg_off;
4124 buf = get_buf_tv(&argvars[0], FALSE);
4125 --emsg_off;
4126
4127 lnum = get_tv_lnum_buf(&argvars[1], buf);
4128 if (argvars[2].v_type == VAR_UNKNOWN)
4129 end = lnum;
4130 else
4131 end = get_tv_lnum_buf(&argvars[2], buf);
4132
4133 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4134}
4135
4136/*
4137 * "getbufvar()" function
4138 */
4139 static void
4140f_getbufvar(typval_T *argvars, typval_T *rettv)
4141{
4142 buf_T *buf;
4143 buf_T *save_curbuf;
4144 char_u *varname;
4145 dictitem_T *v;
4146 int done = FALSE;
4147
4148 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4149 varname = get_tv_string_chk(&argvars[1]);
4150 ++emsg_off;
4151 buf = get_buf_tv(&argvars[0], FALSE);
4152
4153 rettv->v_type = VAR_STRING;
4154 rettv->vval.v_string = NULL;
4155
4156 if (buf != NULL && varname != NULL)
4157 {
4158 /* set curbuf to be our buf, temporarily */
4159 save_curbuf = curbuf;
4160 curbuf = buf;
4161
4162 if (*varname == '&') /* buffer-local-option */
4163 {
4164 if (get_option_tv(&varname, rettv, TRUE) == OK)
4165 done = TRUE;
4166 }
4167 else if (STRCMP(varname, "changedtick") == 0)
4168 {
4169 rettv->v_type = VAR_NUMBER;
4170 rettv->vval.v_number = curbuf->b_changedtick;
4171 done = TRUE;
4172 }
4173 else
4174 {
4175 /* Look up the variable. */
4176 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4177 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4178 'b', varname, FALSE);
4179 if (v != NULL)
4180 {
4181 copy_tv(&v->di_tv, rettv);
4182 done = TRUE;
4183 }
4184 }
4185
4186 /* restore previous notion of curbuf */
4187 curbuf = save_curbuf;
4188 }
4189
4190 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4191 /* use the default value */
4192 copy_tv(&argvars[2], rettv);
4193
4194 --emsg_off;
4195}
4196
4197/*
4198 * "getchar()" function
4199 */
4200 static void
4201f_getchar(typval_T *argvars, typval_T *rettv)
4202{
4203 varnumber_T n;
4204 int error = FALSE;
4205
4206 /* Position the cursor. Needed after a message that ends in a space. */
4207 windgoto(msg_row, msg_col);
4208
4209 ++no_mapping;
4210 ++allow_keys;
4211 for (;;)
4212 {
4213 if (argvars[0].v_type == VAR_UNKNOWN)
4214 /* getchar(): blocking wait. */
4215 n = safe_vgetc();
4216 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4217 /* getchar(1): only check if char avail */
4218 n = vpeekc_any();
4219 else if (error || vpeekc_any() == NUL)
4220 /* illegal argument or getchar(0) and no char avail: return zero */
4221 n = 0;
4222 else
4223 /* getchar(0) and char avail: return char */
4224 n = safe_vgetc();
4225
4226 if (n == K_IGNORE)
4227 continue;
4228 break;
4229 }
4230 --no_mapping;
4231 --allow_keys;
4232
4233 set_vim_var_nr(VV_MOUSE_WIN, 0);
4234 set_vim_var_nr(VV_MOUSE_WINID, 0);
4235 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4236 set_vim_var_nr(VV_MOUSE_COL, 0);
4237
4238 rettv->vval.v_number = n;
4239 if (IS_SPECIAL(n) || mod_mask != 0)
4240 {
4241 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4242 int i = 0;
4243
4244 /* Turn a special key into three bytes, plus modifier. */
4245 if (mod_mask != 0)
4246 {
4247 temp[i++] = K_SPECIAL;
4248 temp[i++] = KS_MODIFIER;
4249 temp[i++] = mod_mask;
4250 }
4251 if (IS_SPECIAL(n))
4252 {
4253 temp[i++] = K_SPECIAL;
4254 temp[i++] = K_SECOND(n);
4255 temp[i++] = K_THIRD(n);
4256 }
4257#ifdef FEAT_MBYTE
4258 else if (has_mbyte)
4259 i += (*mb_char2bytes)(n, temp + i);
4260#endif
4261 else
4262 temp[i++] = n;
4263 temp[i++] = NUL;
4264 rettv->v_type = VAR_STRING;
4265 rettv->vval.v_string = vim_strsave(temp);
4266
4267#ifdef FEAT_MOUSE
4268 if (is_mouse_key(n))
4269 {
4270 int row = mouse_row;
4271 int col = mouse_col;
4272 win_T *win;
4273 linenr_T lnum;
4274# ifdef FEAT_WINDOWS
4275 win_T *wp;
4276# endif
4277 int winnr = 1;
4278
4279 if (row >= 0 && col >= 0)
4280 {
4281 /* Find the window at the mouse coordinates and compute the
4282 * text position. */
4283 win = mouse_find_win(&row, &col);
4284 (void)mouse_comp_pos(win, &row, &col, &lnum);
4285# ifdef FEAT_WINDOWS
4286 for (wp = firstwin; wp != win; wp = wp->w_next)
4287 ++winnr;
4288# endif
4289 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4290 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4291 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4292 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4293 }
4294 }
4295#endif
4296 }
4297}
4298
4299/*
4300 * "getcharmod()" function
4301 */
4302 static void
4303f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4304{
4305 rettv->vval.v_number = mod_mask;
4306}
4307
4308/*
4309 * "getcharsearch()" function
4310 */
4311 static void
4312f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4313{
4314 if (rettv_dict_alloc(rettv) != FAIL)
4315 {
4316 dict_T *dict = rettv->vval.v_dict;
4317
4318 dict_add_nr_str(dict, "char", 0L, last_csearch());
4319 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4320 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4321 }
4322}
4323
4324/*
4325 * "getcmdline()" function
4326 */
4327 static void
4328f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4329{
4330 rettv->v_type = VAR_STRING;
4331 rettv->vval.v_string = get_cmdline_str();
4332}
4333
4334/*
4335 * "getcmdpos()" function
4336 */
4337 static void
4338f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4339{
4340 rettv->vval.v_number = get_cmdline_pos() + 1;
4341}
4342
4343/*
4344 * "getcmdtype()" function
4345 */
4346 static void
4347f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4348{
4349 rettv->v_type = VAR_STRING;
4350 rettv->vval.v_string = alloc(2);
4351 if (rettv->vval.v_string != NULL)
4352 {
4353 rettv->vval.v_string[0] = get_cmdline_type();
4354 rettv->vval.v_string[1] = NUL;
4355 }
4356}
4357
4358/*
4359 * "getcmdwintype()" function
4360 */
4361 static void
4362f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4363{
4364 rettv->v_type = VAR_STRING;
4365 rettv->vval.v_string = NULL;
4366#ifdef FEAT_CMDWIN
4367 rettv->vval.v_string = alloc(2);
4368 if (rettv->vval.v_string != NULL)
4369 {
4370 rettv->vval.v_string[0] = cmdwin_type;
4371 rettv->vval.v_string[1] = NUL;
4372 }
4373#endif
4374}
4375
4376#if defined(FEAT_CMDL_COMPL)
4377/*
4378 * "getcompletion()" function
4379 */
4380 static void
4381f_getcompletion(typval_T *argvars, typval_T *rettv)
4382{
4383 char_u *pat;
4384 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004385 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004386 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4387 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004388
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004389 if (argvars[2].v_type != VAR_UNKNOWN)
4390 filtered = get_tv_number_chk(&argvars[2], NULL);
4391
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004392 if (p_wic)
4393 options |= WILD_ICASE;
4394
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004395 /* For filtered results, 'wildignore' is used */
4396 if (!filtered)
4397 options |= WILD_KEEP_ALL;
4398
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004399 ExpandInit(&xpc);
4400 xpc.xp_pattern = get_tv_string(&argvars[0]);
4401 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4402 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4403 if (xpc.xp_context == EXPAND_NOTHING)
4404 {
4405 if (argvars[1].v_type == VAR_STRING)
4406 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4407 else
4408 EMSG(_(e_invarg));
4409 return;
4410 }
4411
4412# if defined(FEAT_MENU)
4413 if (xpc.xp_context == EXPAND_MENUS)
4414 {
4415 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4416 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4417 }
4418# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004419#ifdef FEAT_CSCOPE
4420 if (xpc.xp_context == EXPAND_CSCOPE)
4421 {
4422 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4423 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4424 }
4425#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004426#ifdef FEAT_SIGNS
4427 if (xpc.xp_context == EXPAND_SIGN)
4428 {
4429 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4430 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4431 }
4432#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004433
4434 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4435 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4436 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004437 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004438
4439 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4440
4441 for (i = 0; i < xpc.xp_numfiles; i++)
4442 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4443 }
4444 vim_free(pat);
4445 ExpandCleanup(&xpc);
4446}
4447#endif
4448
4449/*
4450 * "getcwd()" function
4451 */
4452 static void
4453f_getcwd(typval_T *argvars, typval_T *rettv)
4454{
4455 win_T *wp = NULL;
4456 char_u *cwd;
4457
4458 rettv->v_type = VAR_STRING;
4459 rettv->vval.v_string = NULL;
4460
4461 wp = find_tabwin(&argvars[0], &argvars[1]);
4462 if (wp != NULL)
4463 {
4464 if (wp->w_localdir != NULL)
4465 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4466 else if (globaldir != NULL)
4467 rettv->vval.v_string = vim_strsave(globaldir);
4468 else
4469 {
4470 cwd = alloc(MAXPATHL);
4471 if (cwd != NULL)
4472 {
4473 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4474 rettv->vval.v_string = vim_strsave(cwd);
4475 vim_free(cwd);
4476 }
4477 }
4478#ifdef BACKSLASH_IN_FILENAME
4479 if (rettv->vval.v_string != NULL)
4480 slash_adjust(rettv->vval.v_string);
4481#endif
4482 }
4483}
4484
4485/*
4486 * "getfontname()" function
4487 */
4488 static void
4489f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4490{
4491 rettv->v_type = VAR_STRING;
4492 rettv->vval.v_string = NULL;
4493#ifdef FEAT_GUI
4494 if (gui.in_use)
4495 {
4496 GuiFont font;
4497 char_u *name = NULL;
4498
4499 if (argvars[0].v_type == VAR_UNKNOWN)
4500 {
4501 /* Get the "Normal" font. Either the name saved by
4502 * hl_set_font_name() or from the font ID. */
4503 font = gui.norm_font;
4504 name = hl_get_font_name();
4505 }
4506 else
4507 {
4508 name = get_tv_string(&argvars[0]);
4509 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4510 return;
4511 font = gui_mch_get_font(name, FALSE);
4512 if (font == NOFONT)
4513 return; /* Invalid font name, return empty string. */
4514 }
4515 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4516 if (argvars[0].v_type != VAR_UNKNOWN)
4517 gui_mch_free_font(font);
4518 }
4519#endif
4520}
4521
4522/*
4523 * "getfperm({fname})" function
4524 */
4525 static void
4526f_getfperm(typval_T *argvars, typval_T *rettv)
4527{
4528 char_u *fname;
4529 stat_T st;
4530 char_u *perm = NULL;
4531 char_u flags[] = "rwx";
4532 int i;
4533
4534 fname = get_tv_string(&argvars[0]);
4535
4536 rettv->v_type = VAR_STRING;
4537 if (mch_stat((char *)fname, &st) >= 0)
4538 {
4539 perm = vim_strsave((char_u *)"---------");
4540 if (perm != NULL)
4541 {
4542 for (i = 0; i < 9; i++)
4543 {
4544 if (st.st_mode & (1 << (8 - i)))
4545 perm[i] = flags[i % 3];
4546 }
4547 }
4548 }
4549 rettv->vval.v_string = perm;
4550}
4551
4552/*
4553 * "getfsize({fname})" function
4554 */
4555 static void
4556f_getfsize(typval_T *argvars, typval_T *rettv)
4557{
4558 char_u *fname;
4559 stat_T st;
4560
4561 fname = get_tv_string(&argvars[0]);
4562
4563 rettv->v_type = VAR_NUMBER;
4564
4565 if (mch_stat((char *)fname, &st) >= 0)
4566 {
4567 if (mch_isdir(fname))
4568 rettv->vval.v_number = 0;
4569 else
4570 {
4571 rettv->vval.v_number = (varnumber_T)st.st_size;
4572
4573 /* non-perfect check for overflow */
4574 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4575 rettv->vval.v_number = -2;
4576 }
4577 }
4578 else
4579 rettv->vval.v_number = -1;
4580}
4581
4582/*
4583 * "getftime({fname})" function
4584 */
4585 static void
4586f_getftime(typval_T *argvars, typval_T *rettv)
4587{
4588 char_u *fname;
4589 stat_T st;
4590
4591 fname = get_tv_string(&argvars[0]);
4592
4593 if (mch_stat((char *)fname, &st) >= 0)
4594 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4595 else
4596 rettv->vval.v_number = -1;
4597}
4598
4599/*
4600 * "getftype({fname})" function
4601 */
4602 static void
4603f_getftype(typval_T *argvars, typval_T *rettv)
4604{
4605 char_u *fname;
4606 stat_T st;
4607 char_u *type = NULL;
4608 char *t;
4609
4610 fname = get_tv_string(&argvars[0]);
4611
4612 rettv->v_type = VAR_STRING;
4613 if (mch_lstat((char *)fname, &st) >= 0)
4614 {
4615#ifdef S_ISREG
4616 if (S_ISREG(st.st_mode))
4617 t = "file";
4618 else if (S_ISDIR(st.st_mode))
4619 t = "dir";
4620# ifdef S_ISLNK
4621 else if (S_ISLNK(st.st_mode))
4622 t = "link";
4623# endif
4624# ifdef S_ISBLK
4625 else if (S_ISBLK(st.st_mode))
4626 t = "bdev";
4627# endif
4628# ifdef S_ISCHR
4629 else if (S_ISCHR(st.st_mode))
4630 t = "cdev";
4631# endif
4632# ifdef S_ISFIFO
4633 else if (S_ISFIFO(st.st_mode))
4634 t = "fifo";
4635# endif
4636# ifdef S_ISSOCK
4637 else if (S_ISSOCK(st.st_mode))
4638 t = "fifo";
4639# endif
4640 else
4641 t = "other";
4642#else
4643# ifdef S_IFMT
4644 switch (st.st_mode & S_IFMT)
4645 {
4646 case S_IFREG: t = "file"; break;
4647 case S_IFDIR: t = "dir"; break;
4648# ifdef S_IFLNK
4649 case S_IFLNK: t = "link"; break;
4650# endif
4651# ifdef S_IFBLK
4652 case S_IFBLK: t = "bdev"; break;
4653# endif
4654# ifdef S_IFCHR
4655 case S_IFCHR: t = "cdev"; break;
4656# endif
4657# ifdef S_IFIFO
4658 case S_IFIFO: t = "fifo"; break;
4659# endif
4660# ifdef S_IFSOCK
4661 case S_IFSOCK: t = "socket"; break;
4662# endif
4663 default: t = "other";
4664 }
4665# else
4666 if (mch_isdir(fname))
4667 t = "dir";
4668 else
4669 t = "file";
4670# endif
4671#endif
4672 type = vim_strsave((char_u *)t);
4673 }
4674 rettv->vval.v_string = type;
4675}
4676
4677/*
4678 * "getline(lnum, [end])" function
4679 */
4680 static void
4681f_getline(typval_T *argvars, typval_T *rettv)
4682{
4683 linenr_T lnum;
4684 linenr_T end;
4685 int retlist;
4686
4687 lnum = get_tv_lnum(argvars);
4688 if (argvars[1].v_type == VAR_UNKNOWN)
4689 {
4690 end = 0;
4691 retlist = FALSE;
4692 }
4693 else
4694 {
4695 end = get_tv_lnum(&argvars[1]);
4696 retlist = TRUE;
4697 }
4698
4699 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4700}
4701
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004702#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004703 static void
4704get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4705{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004706 if (what_arg->v_type == VAR_UNKNOWN)
4707 {
4708 if (rettv_list_alloc(rettv) == OK)
4709 if (is_qf || wp != NULL)
4710 (void)get_errorlist(wp, -1, rettv->vval.v_list);
4711 }
4712 else
4713 {
4714 if (rettv_dict_alloc(rettv) == OK)
4715 if (is_qf || (wp != NULL))
4716 {
4717 if (what_arg->v_type == VAR_DICT)
4718 {
4719 dict_T *d = what_arg->vval.v_dict;
4720
4721 if (d != NULL)
4722 get_errorlist_properties(wp, d, rettv->vval.v_dict);
4723 }
4724 else
4725 EMSG(_(e_dictreq));
4726 }
4727 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004728}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004729#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004730
4731/*
4732 * "getloclist()" function
4733 */
4734 static void
4735f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4736{
4737#ifdef FEAT_QUICKFIX
4738 win_T *wp;
4739
4740 wp = find_win_by_nr(&argvars[0], NULL);
4741 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4742#endif
4743}
4744
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004745/*
4746 * "getmatches()" function
4747 */
4748 static void
4749f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4750{
4751#ifdef FEAT_SEARCH_EXTRA
4752 dict_T *dict;
4753 matchitem_T *cur = curwin->w_match_head;
4754 int i;
4755
4756 if (rettv_list_alloc(rettv) == OK)
4757 {
4758 while (cur != NULL)
4759 {
4760 dict = dict_alloc();
4761 if (dict == NULL)
4762 return;
4763 if (cur->match.regprog == NULL)
4764 {
4765 /* match added with matchaddpos() */
4766 for (i = 0; i < MAXPOSMATCH; ++i)
4767 {
4768 llpos_T *llpos;
4769 char buf[6];
4770 list_T *l;
4771
4772 llpos = &cur->pos.pos[i];
4773 if (llpos->lnum == 0)
4774 break;
4775 l = list_alloc();
4776 if (l == NULL)
4777 break;
4778 list_append_number(l, (varnumber_T)llpos->lnum);
4779 if (llpos->col > 0)
4780 {
4781 list_append_number(l, (varnumber_T)llpos->col);
4782 list_append_number(l, (varnumber_T)llpos->len);
4783 }
4784 sprintf(buf, "pos%d", i + 1);
4785 dict_add_list(dict, buf, l);
4786 }
4787 }
4788 else
4789 {
4790 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4791 }
4792 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4793 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4794 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4795# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4796 if (cur->conceal_char)
4797 {
4798 char_u buf[MB_MAXBYTES + 1];
4799
4800 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
4801 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
4802 }
4803# endif
4804 list_append_dict(rettv->vval.v_list, dict);
4805 cur = cur->next;
4806 }
4807 }
4808#endif
4809}
4810
4811/*
4812 * "getpid()" function
4813 */
4814 static void
4815f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4816{
4817 rettv->vval.v_number = mch_get_pid();
4818}
4819
4820 static void
4821getpos_both(
4822 typval_T *argvars,
4823 typval_T *rettv,
4824 int getcurpos)
4825{
4826 pos_T *fp;
4827 list_T *l;
4828 int fnum = -1;
4829
4830 if (rettv_list_alloc(rettv) == OK)
4831 {
4832 l = rettv->vval.v_list;
4833 if (getcurpos)
4834 fp = &curwin->w_cursor;
4835 else
4836 fp = var2fpos(&argvars[0], TRUE, &fnum);
4837 if (fnum != -1)
4838 list_append_number(l, (varnumber_T)fnum);
4839 else
4840 list_append_number(l, (varnumber_T)0);
4841 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4842 : (varnumber_T)0);
4843 list_append_number(l, (fp != NULL)
4844 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4845 : (varnumber_T)0);
4846 list_append_number(l,
4847#ifdef FEAT_VIRTUALEDIT
4848 (fp != NULL) ? (varnumber_T)fp->coladd :
4849#endif
4850 (varnumber_T)0);
4851 if (getcurpos)
4852 {
4853 update_curswant();
4854 list_append_number(l, curwin->w_curswant == MAXCOL ?
4855 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
4856 }
4857 }
4858 else
4859 rettv->vval.v_number = FALSE;
4860}
4861
4862
4863/*
4864 * "getcurpos()" function
4865 */
4866 static void
4867f_getcurpos(typval_T *argvars, typval_T *rettv)
4868{
4869 getpos_both(argvars, rettv, TRUE);
4870}
4871
4872/*
4873 * "getpos(string)" function
4874 */
4875 static void
4876f_getpos(typval_T *argvars, typval_T *rettv)
4877{
4878 getpos_both(argvars, rettv, FALSE);
4879}
4880
4881/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004882 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004883 */
4884 static void
4885f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4886{
4887#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004888 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004889#endif
4890}
4891
4892/*
4893 * "getreg()" function
4894 */
4895 static void
4896f_getreg(typval_T *argvars, typval_T *rettv)
4897{
4898 char_u *strregname;
4899 int regname;
4900 int arg2 = FALSE;
4901 int return_list = FALSE;
4902 int error = FALSE;
4903
4904 if (argvars[0].v_type != VAR_UNKNOWN)
4905 {
4906 strregname = get_tv_string_chk(&argvars[0]);
4907 error = strregname == NULL;
4908 if (argvars[1].v_type != VAR_UNKNOWN)
4909 {
4910 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
4911 if (!error && argvars[2].v_type != VAR_UNKNOWN)
4912 return_list = (int)get_tv_number_chk(&argvars[2], &error);
4913 }
4914 }
4915 else
4916 strregname = get_vim_var_str(VV_REG);
4917
4918 if (error)
4919 return;
4920
4921 regname = (strregname == NULL ? '"' : *strregname);
4922 if (regname == 0)
4923 regname = '"';
4924
4925 if (return_list)
4926 {
4927 rettv->v_type = VAR_LIST;
4928 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4929 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4930 if (rettv->vval.v_list == NULL)
4931 (void)rettv_list_alloc(rettv);
4932 else
4933 ++rettv->vval.v_list->lv_refcount;
4934 }
4935 else
4936 {
4937 rettv->v_type = VAR_STRING;
4938 rettv->vval.v_string = get_reg_contents(regname,
4939 arg2 ? GREG_EXPR_SRC : 0);
4940 }
4941}
4942
4943/*
4944 * "getregtype()" function
4945 */
4946 static void
4947f_getregtype(typval_T *argvars, typval_T *rettv)
4948{
4949 char_u *strregname;
4950 int regname;
4951 char_u buf[NUMBUFLEN + 2];
4952 long reglen = 0;
4953
4954 if (argvars[0].v_type != VAR_UNKNOWN)
4955 {
4956 strregname = get_tv_string_chk(&argvars[0]);
4957 if (strregname == NULL) /* type error; errmsg already given */
4958 {
4959 rettv->v_type = VAR_STRING;
4960 rettv->vval.v_string = NULL;
4961 return;
4962 }
4963 }
4964 else
4965 /* Default to v:register */
4966 strregname = get_vim_var_str(VV_REG);
4967
4968 regname = (strregname == NULL ? '"' : *strregname);
4969 if (regname == 0)
4970 regname = '"';
4971
4972 buf[0] = NUL;
4973 buf[1] = NUL;
4974 switch (get_reg_type(regname, &reglen))
4975 {
4976 case MLINE: buf[0] = 'V'; break;
4977 case MCHAR: buf[0] = 'v'; break;
4978 case MBLOCK:
4979 buf[0] = Ctrl_V;
4980 sprintf((char *)buf + 1, "%ld", reglen + 1);
4981 break;
4982 }
4983 rettv->v_type = VAR_STRING;
4984 rettv->vval.v_string = vim_strsave(buf);
4985}
4986
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004987#ifdef FEAT_WINDOWS
4988/*
4989 * Returns information (variables, options, etc.) about a tab page
4990 * as a dictionary.
4991 */
4992 static dict_T *
4993get_tabpage_info(tabpage_T *tp, int tp_idx)
4994{
4995 win_T *wp;
4996 dict_T *dict;
4997 dict_T *vars;
4998 list_T *l;
4999
5000 dict = dict_alloc();
5001 if (dict == NULL)
5002 return NULL;
5003
5004 dict_add_nr_str(dict, "nr", tp_idx, NULL);
5005
5006 l = list_alloc();
5007 if (l != NULL)
5008 {
5009 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5010 wp; wp = wp->w_next)
5011 list_append_number(l, (varnumber_T)wp->w_id);
5012 dict_add_list(dict, "windows", l);
5013 }
5014
5015 /* Copy tabpage variables */
5016 vars = dict_copy(tp->tp_vars, TRUE, 0);
5017 if (vars != NULL)
5018 dict_add_dict(dict, "variables", vars);
5019
5020 return dict;
5021}
5022#endif
5023
5024/*
5025 * "gettabinfo()" function
5026 */
5027 static void
5028f_gettabinfo(typval_T *argvars, typval_T *rettv)
5029{
5030#ifdef FEAT_WINDOWS
5031 tabpage_T *tp, *tparg = NULL;
5032 dict_T *d;
5033 int tpnr = 1;
5034
5035 if (rettv_list_alloc(rettv) != OK)
5036 return;
5037
5038 if (argvars[0].v_type != VAR_UNKNOWN)
5039 {
5040 /* Information about one tab page */
5041 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5042 if (tparg == NULL)
5043 return;
5044 }
5045
5046 /* Get information about a specific tab page or all tab pages */
5047 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, tpnr++)
5048 {
5049 if (tparg != NULL && tp != tparg)
5050 continue;
5051 d = get_tabpage_info(tp, tpnr);
5052 if (d != NULL)
5053 list_append_dict(rettv->vval.v_list, d);
5054 if (tparg != NULL)
5055 return;
5056 }
5057#endif
5058}
5059
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005060/*
5061 * "gettabvar()" function
5062 */
5063 static void
5064f_gettabvar(typval_T *argvars, typval_T *rettv)
5065{
5066 win_T *oldcurwin;
5067 tabpage_T *tp, *oldtabpage;
5068 dictitem_T *v;
5069 char_u *varname;
5070 int done = FALSE;
5071
5072 rettv->v_type = VAR_STRING;
5073 rettv->vval.v_string = NULL;
5074
5075 varname = get_tv_string_chk(&argvars[1]);
5076 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5077 if (tp != NULL && varname != NULL)
5078 {
5079 /* Set tp to be our tabpage, temporarily. Also set the window to the
5080 * first window in the tabpage, otherwise the window is not valid. */
5081 if (switch_win(&oldcurwin, &oldtabpage,
5082 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
5083 == OK)
5084 {
5085 /* look up the variable */
5086 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5087 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5088 if (v != NULL)
5089 {
5090 copy_tv(&v->di_tv, rettv);
5091 done = TRUE;
5092 }
5093 }
5094
5095 /* restore previous notion of curwin */
5096 restore_win(oldcurwin, oldtabpage, TRUE);
5097 }
5098
5099 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5100 copy_tv(&argvars[2], rettv);
5101}
5102
5103/*
5104 * "gettabwinvar()" function
5105 */
5106 static void
5107f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5108{
5109 getwinvar(argvars, rettv, 1);
5110}
5111
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005112#ifdef FEAT_WINDOWS
5113/*
5114 * Returns information about a window as a dictionary.
5115 */
5116 static dict_T *
5117get_win_info(win_T *wp, short tpnr, short winnr)
5118{
5119 dict_T *dict;
5120 dict_T *vars;
5121 dict_T *opts;
5122
5123 dict = dict_alloc();
5124 if (dict == NULL)
5125 return NULL;
5126
5127 dict_add_nr_str(dict, "tpnr", tpnr, NULL);
5128 dict_add_nr_str(dict, "nr", winnr, NULL);
5129 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5130 dict_add_nr_str(dict, "height", wp->w_height, NULL);
5131 dict_add_nr_str(dict, "width", wp->w_width, NULL);
5132 dict_add_nr_str(dict, "bufnum", wp->w_buffer->b_fnum, NULL);
5133
5134 /* Copy window variables */
5135 vars = dict_copy(wp->w_vars, TRUE, 0);
5136 if (vars != NULL)
5137 dict_add_dict(dict, "variables", vars);
5138
5139 /* Copy window options */
5140 opts = get_winbuf_options(FALSE);
5141 if (opts != NULL)
5142 dict_add_dict(dict, "options", opts);
5143
5144 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;
5158 short tabnr, winnr;
5159#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 */
5175 tabnr = 1;
5176 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, tabnr++)
5177 {
5178 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5179 winnr = 1;
5180 for (; wp; wp = wp->w_next, winnr++)
5181 {
5182 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);
5985# ifdef FEAT_GUI_W32
5986 else if (STRICMP(name, "gui_win32s") == 0)
5987 n = gui_is_win32s();
5988# endif
5989# ifdef FEAT_BROWSE
5990 else if (STRICMP(name, "browse") == 0)
5991 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
5992# endif
5993#endif
5994#ifdef FEAT_SYN_HL
5995 else if (STRICMP(name, "syntax_items") == 0)
5996 n = syntax_present(curwin);
5997#endif
5998#if defined(WIN3264)
5999 else if (STRICMP(name, "win95") == 0)
6000 n = mch_windows95();
6001#endif
6002#ifdef FEAT_NETBEANS_INTG
6003 else if (STRICMP(name, "netbeans_enabled") == 0)
6004 n = netbeans_active();
6005#endif
6006 }
6007
6008 rettv->vval.v_number = n;
6009}
6010
6011/*
6012 * "has_key()" function
6013 */
6014 static void
6015f_has_key(typval_T *argvars, typval_T *rettv)
6016{
6017 if (argvars[0].v_type != VAR_DICT)
6018 {
6019 EMSG(_(e_dictreq));
6020 return;
6021 }
6022 if (argvars[0].vval.v_dict == NULL)
6023 return;
6024
6025 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6026 get_tv_string(&argvars[1]), -1) != NULL;
6027}
6028
6029/*
6030 * "haslocaldir()" function
6031 */
6032 static void
6033f_haslocaldir(typval_T *argvars, typval_T *rettv)
6034{
6035 win_T *wp = NULL;
6036
6037 wp = find_tabwin(&argvars[0], &argvars[1]);
6038 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6039}
6040
6041/*
6042 * "hasmapto()" function
6043 */
6044 static void
6045f_hasmapto(typval_T *argvars, typval_T *rettv)
6046{
6047 char_u *name;
6048 char_u *mode;
6049 char_u buf[NUMBUFLEN];
6050 int abbr = FALSE;
6051
6052 name = get_tv_string(&argvars[0]);
6053 if (argvars[1].v_type == VAR_UNKNOWN)
6054 mode = (char_u *)"nvo";
6055 else
6056 {
6057 mode = get_tv_string_buf(&argvars[1], buf);
6058 if (argvars[2].v_type != VAR_UNKNOWN)
6059 abbr = (int)get_tv_number(&argvars[2]);
6060 }
6061
6062 if (map_to_exists(name, mode, abbr))
6063 rettv->vval.v_number = TRUE;
6064 else
6065 rettv->vval.v_number = FALSE;
6066}
6067
6068/*
6069 * "histadd()" function
6070 */
6071 static void
6072f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6073{
6074#ifdef FEAT_CMDHIST
6075 int histype;
6076 char_u *str;
6077 char_u buf[NUMBUFLEN];
6078#endif
6079
6080 rettv->vval.v_number = FALSE;
6081 if (check_restricted() || check_secure())
6082 return;
6083#ifdef FEAT_CMDHIST
6084 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6085 histype = str != NULL ? get_histtype(str) : -1;
6086 if (histype >= 0)
6087 {
6088 str = get_tv_string_buf(&argvars[1], buf);
6089 if (*str != NUL)
6090 {
6091 init_history();
6092 add_to_history(histype, str, FALSE, NUL);
6093 rettv->vval.v_number = TRUE;
6094 return;
6095 }
6096 }
6097#endif
6098}
6099
6100/*
6101 * "histdel()" function
6102 */
6103 static void
6104f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6105{
6106#ifdef FEAT_CMDHIST
6107 int n;
6108 char_u buf[NUMBUFLEN];
6109 char_u *str;
6110
6111 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6112 if (str == NULL)
6113 n = 0;
6114 else if (argvars[1].v_type == VAR_UNKNOWN)
6115 /* only one argument: clear entire history */
6116 n = clr_history(get_histtype(str));
6117 else if (argvars[1].v_type == VAR_NUMBER)
6118 /* index given: remove that entry */
6119 n = del_history_idx(get_histtype(str),
6120 (int)get_tv_number(&argvars[1]));
6121 else
6122 /* string given: remove all matching entries */
6123 n = del_history_entry(get_histtype(str),
6124 get_tv_string_buf(&argvars[1], buf));
6125 rettv->vval.v_number = n;
6126#endif
6127}
6128
6129/*
6130 * "histget()" function
6131 */
6132 static void
6133f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6134{
6135#ifdef FEAT_CMDHIST
6136 int type;
6137 int idx;
6138 char_u *str;
6139
6140 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6141 if (str == NULL)
6142 rettv->vval.v_string = NULL;
6143 else
6144 {
6145 type = get_histtype(str);
6146 if (argvars[1].v_type == VAR_UNKNOWN)
6147 idx = get_history_idx(type);
6148 else
6149 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6150 /* -1 on type error */
6151 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6152 }
6153#else
6154 rettv->vval.v_string = NULL;
6155#endif
6156 rettv->v_type = VAR_STRING;
6157}
6158
6159/*
6160 * "histnr()" function
6161 */
6162 static void
6163f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6164{
6165 int i;
6166
6167#ifdef FEAT_CMDHIST
6168 char_u *history = get_tv_string_chk(&argvars[0]);
6169
6170 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6171 if (i >= HIST_CMD && i < HIST_COUNT)
6172 i = get_history_idx(i);
6173 else
6174#endif
6175 i = -1;
6176 rettv->vval.v_number = i;
6177}
6178
6179/*
6180 * "highlightID(name)" function
6181 */
6182 static void
6183f_hlID(typval_T *argvars, typval_T *rettv)
6184{
6185 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6186}
6187
6188/*
6189 * "highlight_exists()" function
6190 */
6191 static void
6192f_hlexists(typval_T *argvars, typval_T *rettv)
6193{
6194 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6195}
6196
6197/*
6198 * "hostname()" function
6199 */
6200 static void
6201f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6202{
6203 char_u hostname[256];
6204
6205 mch_get_host_name(hostname, 256);
6206 rettv->v_type = VAR_STRING;
6207 rettv->vval.v_string = vim_strsave(hostname);
6208}
6209
6210/*
6211 * iconv() function
6212 */
6213 static void
6214f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6215{
6216#ifdef FEAT_MBYTE
6217 char_u buf1[NUMBUFLEN];
6218 char_u buf2[NUMBUFLEN];
6219 char_u *from, *to, *str;
6220 vimconv_T vimconv;
6221#endif
6222
6223 rettv->v_type = VAR_STRING;
6224 rettv->vval.v_string = NULL;
6225
6226#ifdef FEAT_MBYTE
6227 str = get_tv_string(&argvars[0]);
6228 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6229 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6230 vimconv.vc_type = CONV_NONE;
6231 convert_setup(&vimconv, from, to);
6232
6233 /* If the encodings are equal, no conversion needed. */
6234 if (vimconv.vc_type == CONV_NONE)
6235 rettv->vval.v_string = vim_strsave(str);
6236 else
6237 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6238
6239 convert_setup(&vimconv, NULL, NULL);
6240 vim_free(from);
6241 vim_free(to);
6242#endif
6243}
6244
6245/*
6246 * "indent()" function
6247 */
6248 static void
6249f_indent(typval_T *argvars, typval_T *rettv)
6250{
6251 linenr_T lnum;
6252
6253 lnum = get_tv_lnum(argvars);
6254 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6255 rettv->vval.v_number = get_indent_lnum(lnum);
6256 else
6257 rettv->vval.v_number = -1;
6258}
6259
6260/*
6261 * "index()" function
6262 */
6263 static void
6264f_index(typval_T *argvars, typval_T *rettv)
6265{
6266 list_T *l;
6267 listitem_T *item;
6268 long idx = 0;
6269 int ic = FALSE;
6270
6271 rettv->vval.v_number = -1;
6272 if (argvars[0].v_type != VAR_LIST)
6273 {
6274 EMSG(_(e_listreq));
6275 return;
6276 }
6277 l = argvars[0].vval.v_list;
6278 if (l != NULL)
6279 {
6280 item = l->lv_first;
6281 if (argvars[2].v_type != VAR_UNKNOWN)
6282 {
6283 int error = FALSE;
6284
6285 /* Start at specified item. Use the cached index that list_find()
6286 * sets, so that a negative number also works. */
6287 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6288 idx = l->lv_idx;
6289 if (argvars[3].v_type != VAR_UNKNOWN)
6290 ic = (int)get_tv_number_chk(&argvars[3], &error);
6291 if (error)
6292 item = NULL;
6293 }
6294
6295 for ( ; item != NULL; item = item->li_next, ++idx)
6296 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6297 {
6298 rettv->vval.v_number = idx;
6299 break;
6300 }
6301 }
6302}
6303
6304static int inputsecret_flag = 0;
6305
6306/*
6307 * "input()" function
6308 * Also handles inputsecret() when inputsecret is set.
6309 */
6310 static void
6311f_input(typval_T *argvars, typval_T *rettv)
6312{
6313 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6314}
6315
6316/*
6317 * "inputdialog()" function
6318 */
6319 static void
6320f_inputdialog(typval_T *argvars, typval_T *rettv)
6321{
6322#if defined(FEAT_GUI_TEXTDIALOG)
6323 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6324 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6325 {
6326 char_u *message;
6327 char_u buf[NUMBUFLEN];
6328 char_u *defstr = (char_u *)"";
6329
6330 message = get_tv_string_chk(&argvars[0]);
6331 if (argvars[1].v_type != VAR_UNKNOWN
6332 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6333 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6334 else
6335 IObuff[0] = NUL;
6336 if (message != NULL && defstr != NULL
6337 && do_dialog(VIM_QUESTION, NULL, message,
6338 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6339 rettv->vval.v_string = vim_strsave(IObuff);
6340 else
6341 {
6342 if (message != NULL && defstr != NULL
6343 && argvars[1].v_type != VAR_UNKNOWN
6344 && argvars[2].v_type != VAR_UNKNOWN)
6345 rettv->vval.v_string = vim_strsave(
6346 get_tv_string_buf(&argvars[2], buf));
6347 else
6348 rettv->vval.v_string = NULL;
6349 }
6350 rettv->v_type = VAR_STRING;
6351 }
6352 else
6353#endif
6354 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6355}
6356
6357/*
6358 * "inputlist()" function
6359 */
6360 static void
6361f_inputlist(typval_T *argvars, typval_T *rettv)
6362{
6363 listitem_T *li;
6364 int selected;
6365 int mouse_used;
6366
6367#ifdef NO_CONSOLE_INPUT
6368 /* While starting up, there is no place to enter text. */
6369 if (no_console_input())
6370 return;
6371#endif
6372 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6373 {
6374 EMSG2(_(e_listarg), "inputlist()");
6375 return;
6376 }
6377
6378 msg_start();
6379 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6380 lines_left = Rows; /* avoid more prompt */
6381 msg_scroll = TRUE;
6382 msg_clr_eos();
6383
6384 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6385 {
6386 msg_puts(get_tv_string(&li->li_tv));
6387 msg_putchar('\n');
6388 }
6389
6390 /* Ask for choice. */
6391 selected = prompt_for_number(&mouse_used);
6392 if (mouse_used)
6393 selected -= lines_left;
6394
6395 rettv->vval.v_number = selected;
6396}
6397
6398
6399static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6400
6401/*
6402 * "inputrestore()" function
6403 */
6404 static void
6405f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6406{
6407 if (ga_userinput.ga_len > 0)
6408 {
6409 --ga_userinput.ga_len;
6410 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6411 + ga_userinput.ga_len);
6412 /* default return is zero == OK */
6413 }
6414 else if (p_verbose > 1)
6415 {
6416 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6417 rettv->vval.v_number = 1; /* Failed */
6418 }
6419}
6420
6421/*
6422 * "inputsave()" function
6423 */
6424 static void
6425f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6426{
6427 /* Add an entry to the stack of typeahead storage. */
6428 if (ga_grow(&ga_userinput, 1) == OK)
6429 {
6430 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6431 + ga_userinput.ga_len);
6432 ++ga_userinput.ga_len;
6433 /* default return is zero == OK */
6434 }
6435 else
6436 rettv->vval.v_number = 1; /* Failed */
6437}
6438
6439/*
6440 * "inputsecret()" function
6441 */
6442 static void
6443f_inputsecret(typval_T *argvars, typval_T *rettv)
6444{
6445 ++cmdline_star;
6446 ++inputsecret_flag;
6447 f_input(argvars, rettv);
6448 --cmdline_star;
6449 --inputsecret_flag;
6450}
6451
6452/*
6453 * "insert()" function
6454 */
6455 static void
6456f_insert(typval_T *argvars, typval_T *rettv)
6457{
6458 long before = 0;
6459 listitem_T *item;
6460 list_T *l;
6461 int error = FALSE;
6462
6463 if (argvars[0].v_type != VAR_LIST)
6464 EMSG2(_(e_listarg), "insert()");
6465 else if ((l = argvars[0].vval.v_list) != NULL
6466 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6467 {
6468 if (argvars[2].v_type != VAR_UNKNOWN)
6469 before = (long)get_tv_number_chk(&argvars[2], &error);
6470 if (error)
6471 return; /* type error; errmsg already given */
6472
6473 if (before == l->lv_len)
6474 item = NULL;
6475 else
6476 {
6477 item = list_find(l, before);
6478 if (item == NULL)
6479 {
6480 EMSGN(_(e_listidx), before);
6481 l = NULL;
6482 }
6483 }
6484 if (l != NULL)
6485 {
6486 list_insert_tv(l, &argvars[1], item);
6487 copy_tv(&argvars[0], rettv);
6488 }
6489 }
6490}
6491
6492/*
6493 * "invert(expr)" function
6494 */
6495 static void
6496f_invert(typval_T *argvars, typval_T *rettv)
6497{
6498 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6499}
6500
6501/*
6502 * "isdirectory()" function
6503 */
6504 static void
6505f_isdirectory(typval_T *argvars, typval_T *rettv)
6506{
6507 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6508}
6509
6510/*
6511 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6512 * or it refers to a List or Dictionary that is locked.
6513 */
6514 static int
6515tv_islocked(typval_T *tv)
6516{
6517 return (tv->v_lock & VAR_LOCKED)
6518 || (tv->v_type == VAR_LIST
6519 && tv->vval.v_list != NULL
6520 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6521 || (tv->v_type == VAR_DICT
6522 && tv->vval.v_dict != NULL
6523 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6524}
6525
6526/*
6527 * "islocked()" function
6528 */
6529 static void
6530f_islocked(typval_T *argvars, typval_T *rettv)
6531{
6532 lval_T lv;
6533 char_u *end;
6534 dictitem_T *di;
6535
6536 rettv->vval.v_number = -1;
6537 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
6538 GLV_NO_AUTOLOAD, FNE_CHECK_START);
6539 if (end != NULL && lv.ll_name != NULL)
6540 {
6541 if (*end != NUL)
6542 EMSG(_(e_trailing));
6543 else
6544 {
6545 if (lv.ll_tv == NULL)
6546 {
6547 if (check_changedtick(lv.ll_name))
6548 rettv->vval.v_number = 1; /* always locked */
6549 else
6550 {
6551 di = find_var(lv.ll_name, NULL, TRUE);
6552 if (di != NULL)
6553 {
6554 /* Consider a variable locked when:
6555 * 1. the variable itself is locked
6556 * 2. the value of the variable is locked.
6557 * 3. the List or Dict value is locked.
6558 */
6559 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6560 || tv_islocked(&di->di_tv));
6561 }
6562 }
6563 }
6564 else if (lv.ll_range)
6565 EMSG(_("E786: Range not allowed"));
6566 else if (lv.ll_newkey != NULL)
6567 EMSG2(_(e_dictkey), lv.ll_newkey);
6568 else if (lv.ll_list != NULL)
6569 /* List item. */
6570 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6571 else
6572 /* Dictionary item. */
6573 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6574 }
6575 }
6576
6577 clear_lval(&lv);
6578}
6579
6580#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6581/*
6582 * "isnan()" function
6583 */
6584 static void
6585f_isnan(typval_T *argvars, typval_T *rettv)
6586{
6587 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6588 && isnan(argvars[0].vval.v_float);
6589}
6590#endif
6591
6592/*
6593 * "items(dict)" function
6594 */
6595 static void
6596f_items(typval_T *argvars, typval_T *rettv)
6597{
6598 dict_list(argvars, rettv, 2);
6599}
6600
6601#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6602/*
6603 * Get the job from the argument.
6604 * Returns NULL if the job is invalid.
6605 */
6606 static job_T *
6607get_job_arg(typval_T *tv)
6608{
6609 job_T *job;
6610
6611 if (tv->v_type != VAR_JOB)
6612 {
6613 EMSG2(_(e_invarg2), get_tv_string(tv));
6614 return NULL;
6615 }
6616 job = tv->vval.v_job;
6617
6618 if (job == NULL)
6619 EMSG(_("E916: not a valid job"));
6620 return job;
6621}
6622
6623/*
6624 * "job_getchannel()" function
6625 */
6626 static void
6627f_job_getchannel(typval_T *argvars, typval_T *rettv)
6628{
6629 job_T *job = get_job_arg(&argvars[0]);
6630
6631 if (job != NULL)
6632 {
6633 rettv->v_type = VAR_CHANNEL;
6634 rettv->vval.v_channel = job->jv_channel;
6635 if (job->jv_channel != NULL)
6636 ++job->jv_channel->ch_refcount;
6637 }
6638}
6639
6640/*
6641 * "job_info()" function
6642 */
6643 static void
6644f_job_info(typval_T *argvars, typval_T *rettv)
6645{
6646 job_T *job = get_job_arg(&argvars[0]);
6647
6648 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6649 job_info(job, rettv->vval.v_dict);
6650}
6651
6652/*
6653 * "job_setoptions()" function
6654 */
6655 static void
6656f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6657{
6658 job_T *job = get_job_arg(&argvars[0]);
6659 jobopt_T opt;
6660
6661 if (job == NULL)
6662 return;
6663 clear_job_options(&opt);
6664 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
6665 job_set_options(job, &opt);
6666 free_job_options(&opt);
6667}
6668
6669/*
6670 * "job_start()" function
6671 */
6672 static void
6673f_job_start(typval_T *argvars, typval_T *rettv)
6674{
6675 rettv->v_type = VAR_JOB;
6676 if (check_restricted() || check_secure())
6677 return;
6678 rettv->vval.v_job = job_start(argvars);
6679}
6680
6681/*
6682 * "job_status()" function
6683 */
6684 static void
6685f_job_status(typval_T *argvars, typval_T *rettv)
6686{
6687 job_T *job = get_job_arg(&argvars[0]);
6688
6689 if (job != NULL)
6690 {
6691 rettv->v_type = VAR_STRING;
6692 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6693 }
6694}
6695
6696/*
6697 * "job_stop()" function
6698 */
6699 static void
6700f_job_stop(typval_T *argvars, typval_T *rettv)
6701{
6702 job_T *job = get_job_arg(&argvars[0]);
6703
6704 if (job != NULL)
6705 rettv->vval.v_number = job_stop(job, argvars);
6706}
6707#endif
6708
6709/*
6710 * "join()" function
6711 */
6712 static void
6713f_join(typval_T *argvars, typval_T *rettv)
6714{
6715 garray_T ga;
6716 char_u *sep;
6717
6718 if (argvars[0].v_type != VAR_LIST)
6719 {
6720 EMSG(_(e_listreq));
6721 return;
6722 }
6723 if (argvars[0].vval.v_list == NULL)
6724 return;
6725 if (argvars[1].v_type == VAR_UNKNOWN)
6726 sep = (char_u *)" ";
6727 else
6728 sep = get_tv_string_chk(&argvars[1]);
6729
6730 rettv->v_type = VAR_STRING;
6731
6732 if (sep != NULL)
6733 {
6734 ga_init2(&ga, (int)sizeof(char), 80);
6735 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6736 ga_append(&ga, NUL);
6737 rettv->vval.v_string = (char_u *)ga.ga_data;
6738 }
6739 else
6740 rettv->vval.v_string = NULL;
6741}
6742
6743/*
6744 * "js_decode()" function
6745 */
6746 static void
6747f_js_decode(typval_T *argvars, typval_T *rettv)
6748{
6749 js_read_T reader;
6750
6751 reader.js_buf = get_tv_string(&argvars[0]);
6752 reader.js_fill = NULL;
6753 reader.js_used = 0;
6754 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
6755 EMSG(_(e_invarg));
6756}
6757
6758/*
6759 * "js_encode()" function
6760 */
6761 static void
6762f_js_encode(typval_T *argvars, typval_T *rettv)
6763{
6764 rettv->v_type = VAR_STRING;
6765 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
6766}
6767
6768/*
6769 * "json_decode()" function
6770 */
6771 static void
6772f_json_decode(typval_T *argvars, typval_T *rettv)
6773{
6774 js_read_T reader;
6775
6776 reader.js_buf = get_tv_string(&argvars[0]);
6777 reader.js_fill = NULL;
6778 reader.js_used = 0;
6779 if (json_decode_all(&reader, rettv, 0) != OK)
6780 EMSG(_(e_invarg));
6781}
6782
6783/*
6784 * "json_encode()" function
6785 */
6786 static void
6787f_json_encode(typval_T *argvars, typval_T *rettv)
6788{
6789 rettv->v_type = VAR_STRING;
6790 rettv->vval.v_string = json_encode(&argvars[0], 0);
6791}
6792
6793/*
6794 * "keys()" function
6795 */
6796 static void
6797f_keys(typval_T *argvars, typval_T *rettv)
6798{
6799 dict_list(argvars, rettv, 0);
6800}
6801
6802/*
6803 * "last_buffer_nr()" function.
6804 */
6805 static void
6806f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6807{
6808 int n = 0;
6809 buf_T *buf;
6810
Bram Moolenaar29323592016-07-24 22:04:11 +02006811 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812 if (n < buf->b_fnum)
6813 n = buf->b_fnum;
6814
6815 rettv->vval.v_number = n;
6816}
6817
6818/*
6819 * "len()" function
6820 */
6821 static void
6822f_len(typval_T *argvars, typval_T *rettv)
6823{
6824 switch (argvars[0].v_type)
6825 {
6826 case VAR_STRING:
6827 case VAR_NUMBER:
6828 rettv->vval.v_number = (varnumber_T)STRLEN(
6829 get_tv_string(&argvars[0]));
6830 break;
6831 case VAR_LIST:
6832 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6833 break;
6834 case VAR_DICT:
6835 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6836 break;
6837 case VAR_UNKNOWN:
6838 case VAR_SPECIAL:
6839 case VAR_FLOAT:
6840 case VAR_FUNC:
6841 case VAR_PARTIAL:
6842 case VAR_JOB:
6843 case VAR_CHANNEL:
6844 EMSG(_("E701: Invalid type for len()"));
6845 break;
6846 }
6847}
6848
6849static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
6850
6851 static void
6852libcall_common(typval_T *argvars, typval_T *rettv, int type)
6853{
6854#ifdef FEAT_LIBCALL
6855 char_u *string_in;
6856 char_u **string_result;
6857 int nr_result;
6858#endif
6859
6860 rettv->v_type = type;
6861 if (type != VAR_NUMBER)
6862 rettv->vval.v_string = NULL;
6863
6864 if (check_restricted() || check_secure())
6865 return;
6866
6867#ifdef FEAT_LIBCALL
6868 /* The first two args must be strings, otherwise its meaningless */
6869 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6870 {
6871 string_in = NULL;
6872 if (argvars[2].v_type == VAR_STRING)
6873 string_in = argvars[2].vval.v_string;
6874 if (type == VAR_NUMBER)
6875 string_result = NULL;
6876 else
6877 string_result = &rettv->vval.v_string;
6878 if (mch_libcall(argvars[0].vval.v_string,
6879 argvars[1].vval.v_string,
6880 string_in,
6881 argvars[2].vval.v_number,
6882 string_result,
6883 &nr_result) == OK
6884 && type == VAR_NUMBER)
6885 rettv->vval.v_number = nr_result;
6886 }
6887#endif
6888}
6889
6890/*
6891 * "libcall()" function
6892 */
6893 static void
6894f_libcall(typval_T *argvars, typval_T *rettv)
6895{
6896 libcall_common(argvars, rettv, VAR_STRING);
6897}
6898
6899/*
6900 * "libcallnr()" function
6901 */
6902 static void
6903f_libcallnr(typval_T *argvars, typval_T *rettv)
6904{
6905 libcall_common(argvars, rettv, VAR_NUMBER);
6906}
6907
6908/*
6909 * "line(string)" function
6910 */
6911 static void
6912f_line(typval_T *argvars, typval_T *rettv)
6913{
6914 linenr_T lnum = 0;
6915 pos_T *fp;
6916 int fnum;
6917
6918 fp = var2fpos(&argvars[0], TRUE, &fnum);
6919 if (fp != NULL)
6920 lnum = fp->lnum;
6921 rettv->vval.v_number = lnum;
6922}
6923
6924/*
6925 * "line2byte(lnum)" function
6926 */
6927 static void
6928f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6929{
6930#ifndef FEAT_BYTEOFF
6931 rettv->vval.v_number = -1;
6932#else
6933 linenr_T lnum;
6934
6935 lnum = get_tv_lnum(argvars);
6936 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6937 rettv->vval.v_number = -1;
6938 else
6939 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6940 if (rettv->vval.v_number >= 0)
6941 ++rettv->vval.v_number;
6942#endif
6943}
6944
6945/*
6946 * "lispindent(lnum)" function
6947 */
6948 static void
6949f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
6950{
6951#ifdef FEAT_LISP
6952 pos_T pos;
6953 linenr_T lnum;
6954
6955 pos = curwin->w_cursor;
6956 lnum = get_tv_lnum(argvars);
6957 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6958 {
6959 curwin->w_cursor.lnum = lnum;
6960 rettv->vval.v_number = get_lisp_indent();
6961 curwin->w_cursor = pos;
6962 }
6963 else
6964#endif
6965 rettv->vval.v_number = -1;
6966}
6967
6968/*
6969 * "localtime()" function
6970 */
6971 static void
6972f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
6973{
6974 rettv->vval.v_number = (varnumber_T)time(NULL);
6975}
6976
6977static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
6978
6979 static void
6980get_maparg(typval_T *argvars, typval_T *rettv, int exact)
6981{
6982 char_u *keys;
6983 char_u *which;
6984 char_u buf[NUMBUFLEN];
6985 char_u *keys_buf = NULL;
6986 char_u *rhs;
6987 int mode;
6988 int abbr = FALSE;
6989 int get_dict = FALSE;
6990 mapblock_T *mp;
6991 int buffer_local;
6992
6993 /* return empty string for failure */
6994 rettv->v_type = VAR_STRING;
6995 rettv->vval.v_string = NULL;
6996
6997 keys = get_tv_string(&argvars[0]);
6998 if (*keys == NUL)
6999 return;
7000
7001 if (argvars[1].v_type != VAR_UNKNOWN)
7002 {
7003 which = get_tv_string_buf_chk(&argvars[1], buf);
7004 if (argvars[2].v_type != VAR_UNKNOWN)
7005 {
7006 abbr = (int)get_tv_number(&argvars[2]);
7007 if (argvars[3].v_type != VAR_UNKNOWN)
7008 get_dict = (int)get_tv_number(&argvars[3]);
7009 }
7010 }
7011 else
7012 which = (char_u *)"";
7013 if (which == NULL)
7014 return;
7015
7016 mode = get_map_mode(&which, 0);
7017
7018 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7019 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7020 vim_free(keys_buf);
7021
7022 if (!get_dict)
7023 {
7024 /* Return a string. */
7025 if (rhs != NULL)
7026 rettv->vval.v_string = str2special_save(rhs, FALSE);
7027
7028 }
7029 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7030 {
7031 /* Return a dictionary. */
7032 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7033 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7034 dict_T *dict = rettv->vval.v_dict;
7035
7036 dict_add_nr_str(dict, "lhs", 0L, lhs);
7037 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7038 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7039 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7040 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7041 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7042 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7043 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7044 dict_add_nr_str(dict, "mode", 0L, mapmode);
7045
7046 vim_free(lhs);
7047 vim_free(mapmode);
7048 }
7049}
7050
7051#ifdef FEAT_FLOAT
7052/*
7053 * "log()" function
7054 */
7055 static void
7056f_log(typval_T *argvars, typval_T *rettv)
7057{
7058 float_T f = 0.0;
7059
7060 rettv->v_type = VAR_FLOAT;
7061 if (get_float_arg(argvars, &f) == OK)
7062 rettv->vval.v_float = log(f);
7063 else
7064 rettv->vval.v_float = 0.0;
7065}
7066
7067/*
7068 * "log10()" function
7069 */
7070 static void
7071f_log10(typval_T *argvars, typval_T *rettv)
7072{
7073 float_T f = 0.0;
7074
7075 rettv->v_type = VAR_FLOAT;
7076 if (get_float_arg(argvars, &f) == OK)
7077 rettv->vval.v_float = log10(f);
7078 else
7079 rettv->vval.v_float = 0.0;
7080}
7081#endif
7082
7083#ifdef FEAT_LUA
7084/*
7085 * "luaeval()" function
7086 */
7087 static void
7088f_luaeval(typval_T *argvars, typval_T *rettv)
7089{
7090 char_u *str;
7091 char_u buf[NUMBUFLEN];
7092
7093 str = get_tv_string_buf(&argvars[0], buf);
7094 do_luaeval(str, argvars + 1, rettv);
7095}
7096#endif
7097
7098/*
7099 * "map()" function
7100 */
7101 static void
7102f_map(typval_T *argvars, typval_T *rettv)
7103{
7104 filter_map(argvars, rettv, TRUE);
7105}
7106
7107/*
7108 * "maparg()" function
7109 */
7110 static void
7111f_maparg(typval_T *argvars, typval_T *rettv)
7112{
7113 get_maparg(argvars, rettv, TRUE);
7114}
7115
7116/*
7117 * "mapcheck()" function
7118 */
7119 static void
7120f_mapcheck(typval_T *argvars, typval_T *rettv)
7121{
7122 get_maparg(argvars, rettv, FALSE);
7123}
7124
7125static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
7126
7127 static void
7128find_some_match(typval_T *argvars, typval_T *rettv, int type)
7129{
7130 char_u *str = NULL;
7131 long len = 0;
7132 char_u *expr = NULL;
7133 char_u *pat;
7134 regmatch_T regmatch;
7135 char_u patbuf[NUMBUFLEN];
7136 char_u strbuf[NUMBUFLEN];
7137 char_u *save_cpo;
7138 long start = 0;
7139 long nth = 1;
7140 colnr_T startcol = 0;
7141 int match = 0;
7142 list_T *l = NULL;
7143 listitem_T *li = NULL;
7144 long idx = 0;
7145 char_u *tofree = NULL;
7146
7147 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7148 save_cpo = p_cpo;
7149 p_cpo = (char_u *)"";
7150
7151 rettv->vval.v_number = -1;
7152 if (type == 3 || type == 4)
7153 {
7154 /* type 3: return empty list when there are no matches.
7155 * type 4: return ["", -1, -1, -1] */
7156 if (rettv_list_alloc(rettv) == FAIL)
7157 goto theend;
7158 if (type == 4
7159 && (list_append_string(rettv->vval.v_list,
7160 (char_u *)"", 0) == FAIL
7161 || list_append_number(rettv->vval.v_list,
7162 (varnumber_T)-1) == FAIL
7163 || list_append_number(rettv->vval.v_list,
7164 (varnumber_T)-1) == FAIL
7165 || list_append_number(rettv->vval.v_list,
7166 (varnumber_T)-1) == FAIL))
7167 {
7168 list_free(rettv->vval.v_list);
7169 rettv->vval.v_list = NULL;
7170 goto theend;
7171 }
7172 }
7173 else if (type == 2)
7174 {
7175 rettv->v_type = VAR_STRING;
7176 rettv->vval.v_string = NULL;
7177 }
7178
7179 if (argvars[0].v_type == VAR_LIST)
7180 {
7181 if ((l = argvars[0].vval.v_list) == NULL)
7182 goto theend;
7183 li = l->lv_first;
7184 }
7185 else
7186 {
7187 expr = str = get_tv_string(&argvars[0]);
7188 len = (long)STRLEN(str);
7189 }
7190
7191 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7192 if (pat == NULL)
7193 goto theend;
7194
7195 if (argvars[2].v_type != VAR_UNKNOWN)
7196 {
7197 int error = FALSE;
7198
7199 start = (long)get_tv_number_chk(&argvars[2], &error);
7200 if (error)
7201 goto theend;
7202 if (l != NULL)
7203 {
7204 li = list_find(l, start);
7205 if (li == NULL)
7206 goto theend;
7207 idx = l->lv_idx; /* use the cached index */
7208 }
7209 else
7210 {
7211 if (start < 0)
7212 start = 0;
7213 if (start > len)
7214 goto theend;
7215 /* When "count" argument is there ignore matches before "start",
7216 * otherwise skip part of the string. Differs when pattern is "^"
7217 * or "\<". */
7218 if (argvars[3].v_type != VAR_UNKNOWN)
7219 startcol = start;
7220 else
7221 {
7222 str += start;
7223 len -= start;
7224 }
7225 }
7226
7227 if (argvars[3].v_type != VAR_UNKNOWN)
7228 nth = (long)get_tv_number_chk(&argvars[3], &error);
7229 if (error)
7230 goto theend;
7231 }
7232
7233 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7234 if (regmatch.regprog != NULL)
7235 {
7236 regmatch.rm_ic = p_ic;
7237
7238 for (;;)
7239 {
7240 if (l != NULL)
7241 {
7242 if (li == NULL)
7243 {
7244 match = FALSE;
7245 break;
7246 }
7247 vim_free(tofree);
7248 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7249 if (str == NULL)
7250 break;
7251 }
7252
7253 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7254
7255 if (match && --nth <= 0)
7256 break;
7257 if (l == NULL && !match)
7258 break;
7259
7260 /* Advance to just after the match. */
7261 if (l != NULL)
7262 {
7263 li = li->li_next;
7264 ++idx;
7265 }
7266 else
7267 {
7268#ifdef FEAT_MBYTE
7269 startcol = (colnr_T)(regmatch.startp[0]
7270 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7271#else
7272 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7273#endif
7274 if (startcol > (colnr_T)len
7275 || str + startcol <= regmatch.startp[0])
7276 {
7277 match = FALSE;
7278 break;
7279 }
7280 }
7281 }
7282
7283 if (match)
7284 {
7285 if (type == 4)
7286 {
7287 listitem_T *li1 = rettv->vval.v_list->lv_first;
7288 listitem_T *li2 = li1->li_next;
7289 listitem_T *li3 = li2->li_next;
7290 listitem_T *li4 = li3->li_next;
7291
7292 vim_free(li1->li_tv.vval.v_string);
7293 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7294 (int)(regmatch.endp[0] - regmatch.startp[0]));
7295 li3->li_tv.vval.v_number =
7296 (varnumber_T)(regmatch.startp[0] - expr);
7297 li4->li_tv.vval.v_number =
7298 (varnumber_T)(regmatch.endp[0] - expr);
7299 if (l != NULL)
7300 li2->li_tv.vval.v_number = (varnumber_T)idx;
7301 }
7302 else if (type == 3)
7303 {
7304 int i;
7305
7306 /* return list with matched string and submatches */
7307 for (i = 0; i < NSUBEXP; ++i)
7308 {
7309 if (regmatch.endp[i] == NULL)
7310 {
7311 if (list_append_string(rettv->vval.v_list,
7312 (char_u *)"", 0) == FAIL)
7313 break;
7314 }
7315 else if (list_append_string(rettv->vval.v_list,
7316 regmatch.startp[i],
7317 (int)(regmatch.endp[i] - regmatch.startp[i]))
7318 == FAIL)
7319 break;
7320 }
7321 }
7322 else if (type == 2)
7323 {
7324 /* return matched string */
7325 if (l != NULL)
7326 copy_tv(&li->li_tv, rettv);
7327 else
7328 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7329 (int)(regmatch.endp[0] - regmatch.startp[0]));
7330 }
7331 else if (l != NULL)
7332 rettv->vval.v_number = idx;
7333 else
7334 {
7335 if (type != 0)
7336 rettv->vval.v_number =
7337 (varnumber_T)(regmatch.startp[0] - str);
7338 else
7339 rettv->vval.v_number =
7340 (varnumber_T)(regmatch.endp[0] - str);
7341 rettv->vval.v_number += (varnumber_T)(str - expr);
7342 }
7343 }
7344 vim_regfree(regmatch.regprog);
7345 }
7346
7347 if (type == 4 && l == NULL)
7348 /* matchstrpos() without a list: drop the second item. */
7349 listitem_remove(rettv->vval.v_list,
7350 rettv->vval.v_list->lv_first->li_next);
7351
7352theend:
7353 vim_free(tofree);
7354 p_cpo = save_cpo;
7355}
7356
7357/*
7358 * "match()" function
7359 */
7360 static void
7361f_match(typval_T *argvars, typval_T *rettv)
7362{
7363 find_some_match(argvars, rettv, 1);
7364}
7365
7366/*
7367 * "matchadd()" function
7368 */
7369 static void
7370f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7371{
7372#ifdef FEAT_SEARCH_EXTRA
7373 char_u buf[NUMBUFLEN];
7374 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7375 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7376 int prio = 10; /* default priority */
7377 int id = -1;
7378 int error = FALSE;
7379 char_u *conceal_char = NULL;
7380
7381 rettv->vval.v_number = -1;
7382
7383 if (grp == NULL || pat == NULL)
7384 return;
7385 if (argvars[2].v_type != VAR_UNKNOWN)
7386 {
7387 prio = (int)get_tv_number_chk(&argvars[2], &error);
7388 if (argvars[3].v_type != VAR_UNKNOWN)
7389 {
7390 id = (int)get_tv_number_chk(&argvars[3], &error);
7391 if (argvars[4].v_type != VAR_UNKNOWN)
7392 {
7393 if (argvars[4].v_type != VAR_DICT)
7394 {
7395 EMSG(_(e_dictreq));
7396 return;
7397 }
7398 if (dict_find(argvars[4].vval.v_dict,
7399 (char_u *)"conceal", -1) != NULL)
7400 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7401 (char_u *)"conceal", FALSE);
7402 }
7403 }
7404 }
7405 if (error == TRUE)
7406 return;
7407 if (id >= 1 && id <= 3)
7408 {
7409 EMSGN("E798: ID is reserved for \":match\": %ld", id);
7410 return;
7411 }
7412
7413 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7414 conceal_char);
7415#endif
7416}
7417
7418/*
7419 * "matchaddpos()" function
7420 */
7421 static void
7422f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7423{
7424#ifdef FEAT_SEARCH_EXTRA
7425 char_u buf[NUMBUFLEN];
7426 char_u *group;
7427 int prio = 10;
7428 int id = -1;
7429 int error = FALSE;
7430 list_T *l;
7431 char_u *conceal_char = NULL;
7432
7433 rettv->vval.v_number = -1;
7434
7435 group = get_tv_string_buf_chk(&argvars[0], buf);
7436 if (group == NULL)
7437 return;
7438
7439 if (argvars[1].v_type != VAR_LIST)
7440 {
7441 EMSG2(_(e_listarg), "matchaddpos()");
7442 return;
7443 }
7444 l = argvars[1].vval.v_list;
7445 if (l == NULL)
7446 return;
7447
7448 if (argvars[2].v_type != VAR_UNKNOWN)
7449 {
7450 prio = (int)get_tv_number_chk(&argvars[2], &error);
7451 if (argvars[3].v_type != VAR_UNKNOWN)
7452 {
7453 id = (int)get_tv_number_chk(&argvars[3], &error);
7454 if (argvars[4].v_type != VAR_UNKNOWN)
7455 {
7456 if (argvars[4].v_type != VAR_DICT)
7457 {
7458 EMSG(_(e_dictreq));
7459 return;
7460 }
7461 if (dict_find(argvars[4].vval.v_dict,
7462 (char_u *)"conceal", -1) != NULL)
7463 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7464 (char_u *)"conceal", FALSE);
7465 }
7466 }
7467 }
7468 if (error == TRUE)
7469 return;
7470
7471 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7472 if (id == 1 || id == 2)
7473 {
7474 EMSGN("E798: ID is reserved for \":match\": %ld", id);
7475 return;
7476 }
7477
7478 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7479 conceal_char);
7480#endif
7481}
7482
7483/*
7484 * "matcharg()" function
7485 */
7486 static void
7487f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7488{
7489 if (rettv_list_alloc(rettv) == OK)
7490 {
7491#ifdef FEAT_SEARCH_EXTRA
7492 int id = (int)get_tv_number(&argvars[0]);
7493 matchitem_T *m;
7494
7495 if (id >= 1 && id <= 3)
7496 {
7497 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7498 {
7499 list_append_string(rettv->vval.v_list,
7500 syn_id2name(m->hlg_id), -1);
7501 list_append_string(rettv->vval.v_list, m->pattern, -1);
7502 }
7503 else
7504 {
7505 list_append_string(rettv->vval.v_list, NULL, -1);
7506 list_append_string(rettv->vval.v_list, NULL, -1);
7507 }
7508 }
7509#endif
7510 }
7511}
7512
7513/*
7514 * "matchdelete()" function
7515 */
7516 static void
7517f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7518{
7519#ifdef FEAT_SEARCH_EXTRA
7520 rettv->vval.v_number = match_delete(curwin,
7521 (int)get_tv_number(&argvars[0]), TRUE);
7522#endif
7523}
7524
7525/*
7526 * "matchend()" function
7527 */
7528 static void
7529f_matchend(typval_T *argvars, typval_T *rettv)
7530{
7531 find_some_match(argvars, rettv, 0);
7532}
7533
7534/*
7535 * "matchlist()" function
7536 */
7537 static void
7538f_matchlist(typval_T *argvars, typval_T *rettv)
7539{
7540 find_some_match(argvars, rettv, 3);
7541}
7542
7543/*
7544 * "matchstr()" function
7545 */
7546 static void
7547f_matchstr(typval_T *argvars, typval_T *rettv)
7548{
7549 find_some_match(argvars, rettv, 2);
7550}
7551
7552/*
7553 * "matchstrpos()" function
7554 */
7555 static void
7556f_matchstrpos(typval_T *argvars, typval_T *rettv)
7557{
7558 find_some_match(argvars, rettv, 4);
7559}
7560
7561static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7562
7563 static void
7564max_min(typval_T *argvars, typval_T *rettv, int domax)
7565{
7566 varnumber_T n = 0;
7567 varnumber_T i;
7568 int error = FALSE;
7569
7570 if (argvars[0].v_type == VAR_LIST)
7571 {
7572 list_T *l;
7573 listitem_T *li;
7574
7575 l = argvars[0].vval.v_list;
7576 if (l != NULL)
7577 {
7578 li = l->lv_first;
7579 if (li != NULL)
7580 {
7581 n = get_tv_number_chk(&li->li_tv, &error);
7582 for (;;)
7583 {
7584 li = li->li_next;
7585 if (li == NULL)
7586 break;
7587 i = get_tv_number_chk(&li->li_tv, &error);
7588 if (domax ? i > n : i < n)
7589 n = i;
7590 }
7591 }
7592 }
7593 }
7594 else if (argvars[0].v_type == VAR_DICT)
7595 {
7596 dict_T *d;
7597 int first = TRUE;
7598 hashitem_T *hi;
7599 int todo;
7600
7601 d = argvars[0].vval.v_dict;
7602 if (d != NULL)
7603 {
7604 todo = (int)d->dv_hashtab.ht_used;
7605 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7606 {
7607 if (!HASHITEM_EMPTY(hi))
7608 {
7609 --todo;
7610 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7611 if (first)
7612 {
7613 n = i;
7614 first = FALSE;
7615 }
7616 else if (domax ? i > n : i < n)
7617 n = i;
7618 }
7619 }
7620 }
7621 }
7622 else
7623 EMSG(_(e_listdictarg));
7624 rettv->vval.v_number = error ? 0 : n;
7625}
7626
7627/*
7628 * "max()" function
7629 */
7630 static void
7631f_max(typval_T *argvars, typval_T *rettv)
7632{
7633 max_min(argvars, rettv, TRUE);
7634}
7635
7636/*
7637 * "min()" function
7638 */
7639 static void
7640f_min(typval_T *argvars, typval_T *rettv)
7641{
7642 max_min(argvars, rettv, FALSE);
7643}
7644
7645static int mkdir_recurse(char_u *dir, int prot);
7646
7647/*
7648 * Create the directory in which "dir" is located, and higher levels when
7649 * needed.
7650 */
7651 static int
7652mkdir_recurse(char_u *dir, int prot)
7653{
7654 char_u *p;
7655 char_u *updir;
7656 int r = FAIL;
7657
7658 /* Get end of directory name in "dir".
7659 * We're done when it's "/" or "c:/". */
7660 p = gettail_sep(dir);
7661 if (p <= get_past_head(dir))
7662 return OK;
7663
7664 /* If the directory exists we're done. Otherwise: create it.*/
7665 updir = vim_strnsave(dir, (int)(p - dir));
7666 if (updir == NULL)
7667 return FAIL;
7668 if (mch_isdir(updir))
7669 r = OK;
7670 else if (mkdir_recurse(updir, prot) == OK)
7671 r = vim_mkdir_emsg(updir, prot);
7672 vim_free(updir);
7673 return r;
7674}
7675
7676#ifdef vim_mkdir
7677/*
7678 * "mkdir()" function
7679 */
7680 static void
7681f_mkdir(typval_T *argvars, typval_T *rettv)
7682{
7683 char_u *dir;
7684 char_u buf[NUMBUFLEN];
7685 int prot = 0755;
7686
7687 rettv->vval.v_number = FAIL;
7688 if (check_restricted() || check_secure())
7689 return;
7690
7691 dir = get_tv_string_buf(&argvars[0], buf);
7692 if (*dir == NUL)
7693 rettv->vval.v_number = FAIL;
7694 else
7695 {
7696 if (*gettail(dir) == NUL)
7697 /* remove trailing slashes */
7698 *gettail_sep(dir) = NUL;
7699
7700 if (argvars[1].v_type != VAR_UNKNOWN)
7701 {
7702 if (argvars[2].v_type != VAR_UNKNOWN)
7703 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7704 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7705 mkdir_recurse(dir, prot);
7706 }
7707 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7708 }
7709}
7710#endif
7711
7712/*
7713 * "mode()" function
7714 */
7715 static void
7716f_mode(typval_T *argvars, typval_T *rettv)
7717{
7718 char_u buf[3];
7719
7720 buf[1] = NUL;
7721 buf[2] = NUL;
7722
7723 if (time_for_testing == 93784)
7724 {
7725 /* Testing the two-character code. */
7726 buf[0] = 'x';
7727 buf[1] = '!';
7728 }
7729 else if (VIsual_active)
7730 {
7731 if (VIsual_select)
7732 buf[0] = VIsual_mode + 's' - 'v';
7733 else
7734 buf[0] = VIsual_mode;
7735 }
7736 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7737 || State == CONFIRM)
7738 {
7739 buf[0] = 'r';
7740 if (State == ASKMORE)
7741 buf[1] = 'm';
7742 else if (State == CONFIRM)
7743 buf[1] = '?';
7744 }
7745 else if (State == EXTERNCMD)
7746 buf[0] = '!';
7747 else if (State & INSERT)
7748 {
7749#ifdef FEAT_VREPLACE
7750 if (State & VREPLACE_FLAG)
7751 {
7752 buf[0] = 'R';
7753 buf[1] = 'v';
7754 }
7755 else
7756#endif
7757 if (State & REPLACE_FLAG)
7758 buf[0] = 'R';
7759 else
7760 buf[0] = 'i';
7761 }
7762 else if (State & CMDLINE)
7763 {
7764 buf[0] = 'c';
7765 if (exmode_active)
7766 buf[1] = 'v';
7767 }
7768 else if (exmode_active)
7769 {
7770 buf[0] = 'c';
7771 buf[1] = 'e';
7772 }
7773 else
7774 {
7775 buf[0] = 'n';
7776 if (finish_op)
7777 buf[1] = 'o';
7778 }
7779
7780 /* Clear out the minor mode when the argument is not a non-zero number or
7781 * non-empty string. */
7782 if (!non_zero_arg(&argvars[0]))
7783 buf[1] = NUL;
7784
7785 rettv->vval.v_string = vim_strsave(buf);
7786 rettv->v_type = VAR_STRING;
7787}
7788
7789#if defined(FEAT_MZSCHEME) || defined(PROTO)
7790/*
7791 * "mzeval()" function
7792 */
7793 static void
7794f_mzeval(typval_T *argvars, typval_T *rettv)
7795{
7796 char_u *str;
7797 char_u buf[NUMBUFLEN];
7798
7799 str = get_tv_string_buf(&argvars[0], buf);
7800 do_mzeval(str, rettv);
7801}
7802
7803 void
7804mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
7805{
7806 typval_T argvars[3];
7807
7808 argvars[0].v_type = VAR_STRING;
7809 argvars[0].vval.v_string = name;
7810 copy_tv(args, &argvars[1]);
7811 argvars[2].v_type = VAR_UNKNOWN;
7812 f_call(argvars, rettv);
7813 clear_tv(&argvars[1]);
7814}
7815#endif
7816
7817/*
7818 * "nextnonblank()" function
7819 */
7820 static void
7821f_nextnonblank(typval_T *argvars, typval_T *rettv)
7822{
7823 linenr_T lnum;
7824
7825 for (lnum = get_tv_lnum(argvars); ; ++lnum)
7826 {
7827 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
7828 {
7829 lnum = 0;
7830 break;
7831 }
7832 if (*skipwhite(ml_get(lnum)) != NUL)
7833 break;
7834 }
7835 rettv->vval.v_number = lnum;
7836}
7837
7838/*
7839 * "nr2char()" function
7840 */
7841 static void
7842f_nr2char(typval_T *argvars, typval_T *rettv)
7843{
7844 char_u buf[NUMBUFLEN];
7845
7846#ifdef FEAT_MBYTE
7847 if (has_mbyte)
7848 {
7849 int utf8 = 0;
7850
7851 if (argvars[1].v_type != VAR_UNKNOWN)
7852 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
7853 if (utf8)
7854 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7855 else
7856 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7857 }
7858 else
7859#endif
7860 {
7861 buf[0] = (char_u)get_tv_number(&argvars[0]);
7862 buf[1] = NUL;
7863 }
7864 rettv->v_type = VAR_STRING;
7865 rettv->vval.v_string = vim_strsave(buf);
7866}
7867
7868/*
7869 * "or(expr, expr)" function
7870 */
7871 static void
7872f_or(typval_T *argvars, typval_T *rettv)
7873{
7874 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
7875 | get_tv_number_chk(&argvars[1], NULL);
7876}
7877
7878/*
7879 * "pathshorten()" function
7880 */
7881 static void
7882f_pathshorten(typval_T *argvars, typval_T *rettv)
7883{
7884 char_u *p;
7885
7886 rettv->v_type = VAR_STRING;
7887 p = get_tv_string_chk(&argvars[0]);
7888 if (p == NULL)
7889 rettv->vval.v_string = NULL;
7890 else
7891 {
7892 p = vim_strsave(p);
7893 rettv->vval.v_string = p;
7894 if (p != NULL)
7895 shorten_dir(p);
7896 }
7897}
7898
7899#ifdef FEAT_PERL
7900/*
7901 * "perleval()" function
7902 */
7903 static void
7904f_perleval(typval_T *argvars, typval_T *rettv)
7905{
7906 char_u *str;
7907 char_u buf[NUMBUFLEN];
7908
7909 str = get_tv_string_buf(&argvars[0], buf);
7910 do_perleval(str, rettv);
7911}
7912#endif
7913
7914#ifdef FEAT_FLOAT
7915/*
7916 * "pow()" function
7917 */
7918 static void
7919f_pow(typval_T *argvars, typval_T *rettv)
7920{
7921 float_T fx = 0.0, fy = 0.0;
7922
7923 rettv->v_type = VAR_FLOAT;
7924 if (get_float_arg(argvars, &fx) == OK
7925 && get_float_arg(&argvars[1], &fy) == OK)
7926 rettv->vval.v_float = pow(fx, fy);
7927 else
7928 rettv->vval.v_float = 0.0;
7929}
7930#endif
7931
7932/*
7933 * "prevnonblank()" function
7934 */
7935 static void
7936f_prevnonblank(typval_T *argvars, typval_T *rettv)
7937{
7938 linenr_T lnum;
7939
7940 lnum = get_tv_lnum(argvars);
7941 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7942 lnum = 0;
7943 else
7944 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7945 --lnum;
7946 rettv->vval.v_number = lnum;
7947}
7948
7949/* This dummy va_list is here because:
7950 * - passing a NULL pointer doesn't work when va_list isn't a pointer
7951 * - locally in the function results in a "used before set" warning
7952 * - using va_start() to initialize it gives "function with fixed args" error */
7953static va_list ap;
7954
7955/*
7956 * "printf()" function
7957 */
7958 static void
7959f_printf(typval_T *argvars, typval_T *rettv)
7960{
7961 char_u buf[NUMBUFLEN];
7962 int len;
7963 char_u *s;
7964 int saved_did_emsg = did_emsg;
7965 char *fmt;
7966
7967 rettv->v_type = VAR_STRING;
7968 rettv->vval.v_string = NULL;
7969
7970 /* Get the required length, allocate the buffer and do it for real. */
7971 did_emsg = FALSE;
7972 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
7973 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
7974 if (!did_emsg)
7975 {
7976 s = alloc(len + 1);
7977 if (s != NULL)
7978 {
7979 rettv->vval.v_string = s;
7980 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
7981 }
7982 }
7983 did_emsg |= saved_did_emsg;
7984}
7985
7986/*
7987 * "pumvisible()" function
7988 */
7989 static void
7990f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7991{
7992#ifdef FEAT_INS_EXPAND
7993 if (pum_visible())
7994 rettv->vval.v_number = 1;
7995#endif
7996}
7997
7998#ifdef FEAT_PYTHON3
7999/*
8000 * "py3eval()" function
8001 */
8002 static void
8003f_py3eval(typval_T *argvars, typval_T *rettv)
8004{
8005 char_u *str;
8006 char_u buf[NUMBUFLEN];
8007
8008 str = get_tv_string_buf(&argvars[0], buf);
8009 do_py3eval(str, rettv);
8010}
8011#endif
8012
8013#ifdef FEAT_PYTHON
8014/*
8015 * "pyeval()" function
8016 */
8017 static void
8018f_pyeval(typval_T *argvars, typval_T *rettv)
8019{
8020 char_u *str;
8021 char_u buf[NUMBUFLEN];
8022
8023 str = get_tv_string_buf(&argvars[0], buf);
8024 do_pyeval(str, rettv);
8025}
8026#endif
8027
8028/*
8029 * "range()" function
8030 */
8031 static void
8032f_range(typval_T *argvars, typval_T *rettv)
8033{
8034 varnumber_T start;
8035 varnumber_T end;
8036 varnumber_T stride = 1;
8037 varnumber_T i;
8038 int error = FALSE;
8039
8040 start = get_tv_number_chk(&argvars[0], &error);
8041 if (argvars[1].v_type == VAR_UNKNOWN)
8042 {
8043 end = start - 1;
8044 start = 0;
8045 }
8046 else
8047 {
8048 end = get_tv_number_chk(&argvars[1], &error);
8049 if (argvars[2].v_type != VAR_UNKNOWN)
8050 stride = get_tv_number_chk(&argvars[2], &error);
8051 }
8052
8053 if (error)
8054 return; /* type error; errmsg already given */
8055 if (stride == 0)
8056 EMSG(_("E726: Stride is zero"));
8057 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8058 EMSG(_("E727: Start past end"));
8059 else
8060 {
8061 if (rettv_list_alloc(rettv) == OK)
8062 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8063 if (list_append_number(rettv->vval.v_list,
8064 (varnumber_T)i) == FAIL)
8065 break;
8066 }
8067}
8068
8069/*
8070 * "readfile()" function
8071 */
8072 static void
8073f_readfile(typval_T *argvars, typval_T *rettv)
8074{
8075 int binary = FALSE;
8076 int failed = FALSE;
8077 char_u *fname;
8078 FILE *fd;
8079 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8080 int io_size = sizeof(buf);
8081 int readlen; /* size of last fread() */
8082 char_u *prev = NULL; /* previously read bytes, if any */
8083 long prevlen = 0; /* length of data in prev */
8084 long prevsize = 0; /* size of prev buffer */
8085 long maxline = MAXLNUM;
8086 long cnt = 0;
8087 char_u *p; /* position in buf */
8088 char_u *start; /* start of current line */
8089
8090 if (argvars[1].v_type != VAR_UNKNOWN)
8091 {
8092 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8093 binary = TRUE;
8094 if (argvars[2].v_type != VAR_UNKNOWN)
8095 maxline = (long)get_tv_number(&argvars[2]);
8096 }
8097
8098 if (rettv_list_alloc(rettv) == FAIL)
8099 return;
8100
8101 /* Always open the file in binary mode, library functions have a mind of
8102 * their own about CR-LF conversion. */
8103 fname = get_tv_string(&argvars[0]);
8104 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8105 {
8106 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8107 return;
8108 }
8109
8110 while (cnt < maxline || maxline < 0)
8111 {
8112 readlen = (int)fread(buf, 1, io_size, fd);
8113
8114 /* This for loop processes what was read, but is also entered at end
8115 * of file so that either:
8116 * - an incomplete line gets written
8117 * - a "binary" file gets an empty line at the end if it ends in a
8118 * newline. */
8119 for (p = buf, start = buf;
8120 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8121 ++p)
8122 {
8123 if (*p == '\n' || readlen <= 0)
8124 {
8125 listitem_T *li;
8126 char_u *s = NULL;
8127 long_u len = p - start;
8128
8129 /* Finished a line. Remove CRs before NL. */
8130 if (readlen > 0 && !binary)
8131 {
8132 while (len > 0 && start[len - 1] == '\r')
8133 --len;
8134 /* removal may cross back to the "prev" string */
8135 if (len == 0)
8136 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8137 --prevlen;
8138 }
8139 if (prevlen == 0)
8140 s = vim_strnsave(start, (int)len);
8141 else
8142 {
8143 /* Change "prev" buffer to be the right size. This way
8144 * the bytes are only copied once, and very long lines are
8145 * allocated only once. */
8146 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8147 {
8148 mch_memmove(s + prevlen, start, len);
8149 s[prevlen + len] = NUL;
8150 prev = NULL; /* the list will own the string */
8151 prevlen = prevsize = 0;
8152 }
8153 }
8154 if (s == NULL)
8155 {
8156 do_outofmem_msg((long_u) prevlen + len + 1);
8157 failed = TRUE;
8158 break;
8159 }
8160
8161 if ((li = listitem_alloc()) == NULL)
8162 {
8163 vim_free(s);
8164 failed = TRUE;
8165 break;
8166 }
8167 li->li_tv.v_type = VAR_STRING;
8168 li->li_tv.v_lock = 0;
8169 li->li_tv.vval.v_string = s;
8170 list_append(rettv->vval.v_list, li);
8171
8172 start = p + 1; /* step over newline */
8173 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8174 break;
8175 }
8176 else if (*p == NUL)
8177 *p = '\n';
8178#ifdef FEAT_MBYTE
8179 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8180 * when finding the BF and check the previous two bytes. */
8181 else if (*p == 0xbf && enc_utf8 && !binary)
8182 {
8183 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8184 * + 1, these may be in the "prev" string. */
8185 char_u back1 = p >= buf + 1 ? p[-1]
8186 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8187 char_u back2 = p >= buf + 2 ? p[-2]
8188 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8189 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8190
8191 if (back2 == 0xef && back1 == 0xbb)
8192 {
8193 char_u *dest = p - 2;
8194
8195 /* Usually a BOM is at the beginning of a file, and so at
8196 * the beginning of a line; then we can just step over it.
8197 */
8198 if (start == dest)
8199 start = p + 1;
8200 else
8201 {
8202 /* have to shuffle buf to close gap */
8203 int adjust_prevlen = 0;
8204
8205 if (dest < buf)
8206 {
8207 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8208 dest = buf;
8209 }
8210 if (readlen > p - buf + 1)
8211 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8212 readlen -= 3 - adjust_prevlen;
8213 prevlen -= adjust_prevlen;
8214 p = dest - 1;
8215 }
8216 }
8217 }
8218#endif
8219 } /* for */
8220
8221 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8222 break;
8223 if (start < p)
8224 {
8225 /* There's part of a line in buf, store it in "prev". */
8226 if (p - start + prevlen >= prevsize)
8227 {
8228 /* need bigger "prev" buffer */
8229 char_u *newprev;
8230
8231 /* A common use case is ordinary text files and "prev" gets a
8232 * fragment of a line, so the first allocation is made
8233 * small, to avoid repeatedly 'allocing' large and
8234 * 'reallocing' small. */
8235 if (prevsize == 0)
8236 prevsize = (long)(p - start);
8237 else
8238 {
8239 long grow50pc = (prevsize * 3) / 2;
8240 long growmin = (long)((p - start) * 2 + prevlen);
8241 prevsize = grow50pc > growmin ? grow50pc : growmin;
8242 }
8243 newprev = prev == NULL ? alloc(prevsize)
8244 : vim_realloc(prev, prevsize);
8245 if (newprev == NULL)
8246 {
8247 do_outofmem_msg((long_u)prevsize);
8248 failed = TRUE;
8249 break;
8250 }
8251 prev = newprev;
8252 }
8253 /* Add the line part to end of "prev". */
8254 mch_memmove(prev + prevlen, start, p - start);
8255 prevlen += (long)(p - start);
8256 }
8257 } /* while */
8258
8259 /*
8260 * For a negative line count use only the lines at the end of the file,
8261 * free the rest.
8262 */
8263 if (!failed && maxline < 0)
8264 while (cnt > -maxline)
8265 {
8266 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8267 --cnt;
8268 }
8269
8270 if (failed)
8271 {
8272 list_free(rettv->vval.v_list);
8273 /* readfile doc says an empty list is returned on error */
8274 rettv->vval.v_list = list_alloc();
8275 }
8276
8277 vim_free(prev);
8278 fclose(fd);
8279}
8280
8281#if defined(FEAT_RELTIME)
8282static int list2proftime(typval_T *arg, proftime_T *tm);
8283
8284/*
8285 * Convert a List to proftime_T.
8286 * Return FAIL when there is something wrong.
8287 */
8288 static int
8289list2proftime(typval_T *arg, proftime_T *tm)
8290{
8291 long n1, n2;
8292 int error = FALSE;
8293
8294 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8295 || arg->vval.v_list->lv_len != 2)
8296 return FAIL;
8297 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8298 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8299# ifdef WIN3264
8300 tm->HighPart = n1;
8301 tm->LowPart = n2;
8302# else
8303 tm->tv_sec = n1;
8304 tm->tv_usec = n2;
8305# endif
8306 return error ? FAIL : OK;
8307}
8308#endif /* FEAT_RELTIME */
8309
8310/*
8311 * "reltime()" function
8312 */
8313 static void
8314f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8315{
8316#ifdef FEAT_RELTIME
8317 proftime_T res;
8318 proftime_T start;
8319
8320 if (argvars[0].v_type == VAR_UNKNOWN)
8321 {
8322 /* No arguments: get current time. */
8323 profile_start(&res);
8324 }
8325 else if (argvars[1].v_type == VAR_UNKNOWN)
8326 {
8327 if (list2proftime(&argvars[0], &res) == FAIL)
8328 return;
8329 profile_end(&res);
8330 }
8331 else
8332 {
8333 /* Two arguments: compute the difference. */
8334 if (list2proftime(&argvars[0], &start) == FAIL
8335 || list2proftime(&argvars[1], &res) == FAIL)
8336 return;
8337 profile_sub(&res, &start);
8338 }
8339
8340 if (rettv_list_alloc(rettv) == OK)
8341 {
8342 long n1, n2;
8343
8344# ifdef WIN3264
8345 n1 = res.HighPart;
8346 n2 = res.LowPart;
8347# else
8348 n1 = res.tv_sec;
8349 n2 = res.tv_usec;
8350# endif
8351 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8352 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8353 }
8354#endif
8355}
8356
8357#ifdef FEAT_FLOAT
8358/*
8359 * "reltimefloat()" function
8360 */
8361 static void
8362f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8363{
8364# ifdef FEAT_RELTIME
8365 proftime_T tm;
8366# endif
8367
8368 rettv->v_type = VAR_FLOAT;
8369 rettv->vval.v_float = 0;
8370# ifdef FEAT_RELTIME
8371 if (list2proftime(&argvars[0], &tm) == OK)
8372 rettv->vval.v_float = profile_float(&tm);
8373# endif
8374}
8375#endif
8376
8377/*
8378 * "reltimestr()" function
8379 */
8380 static void
8381f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8382{
8383#ifdef FEAT_RELTIME
8384 proftime_T tm;
8385#endif
8386
8387 rettv->v_type = VAR_STRING;
8388 rettv->vval.v_string = NULL;
8389#ifdef FEAT_RELTIME
8390 if (list2proftime(&argvars[0], &tm) == OK)
8391 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8392#endif
8393}
8394
8395#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8396static void make_connection(void);
8397static int check_connection(void);
8398
8399 static void
8400make_connection(void)
8401{
8402 if (X_DISPLAY == NULL
8403# ifdef FEAT_GUI
8404 && !gui.in_use
8405# endif
8406 )
8407 {
8408 x_force_connect = TRUE;
8409 setup_term_clip();
8410 x_force_connect = FALSE;
8411 }
8412}
8413
8414 static int
8415check_connection(void)
8416{
8417 make_connection();
8418 if (X_DISPLAY == NULL)
8419 {
8420 EMSG(_("E240: No connection to Vim server"));
8421 return FAIL;
8422 }
8423 return OK;
8424}
8425#endif
8426
8427#ifdef FEAT_CLIENTSERVER
8428 static void
8429remote_common(typval_T *argvars, typval_T *rettv, int expr)
8430{
8431 char_u *server_name;
8432 char_u *keys;
8433 char_u *r = NULL;
8434 char_u buf[NUMBUFLEN];
8435# ifdef WIN32
8436 HWND w;
8437# else
8438 Window w;
8439# endif
8440
8441 if (check_restricted() || check_secure())
8442 return;
8443
8444# ifdef FEAT_X11
8445 if (check_connection() == FAIL)
8446 return;
8447# endif
8448
8449 server_name = get_tv_string_chk(&argvars[0]);
8450 if (server_name == NULL)
8451 return; /* type error; errmsg already given */
8452 keys = get_tv_string_buf(&argvars[1], buf);
8453# ifdef WIN32
8454 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
8455# else
8456 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
8457 < 0)
8458# endif
8459 {
8460 if (r != NULL)
8461 EMSG(r); /* sending worked but evaluation failed */
8462 else
8463 EMSG2(_("E241: Unable to send to %s"), server_name);
8464 return;
8465 }
8466
8467 rettv->vval.v_string = r;
8468
8469 if (argvars[2].v_type != VAR_UNKNOWN)
8470 {
8471 dictitem_T v;
8472 char_u str[30];
8473 char_u *idvar;
8474
8475 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8476 v.di_tv.v_type = VAR_STRING;
8477 v.di_tv.vval.v_string = vim_strsave(str);
8478 idvar = get_tv_string_chk(&argvars[2]);
8479 if (idvar != NULL)
8480 set_var(idvar, &v.di_tv, FALSE);
8481 vim_free(v.di_tv.vval.v_string);
8482 }
8483}
8484#endif
8485
8486/*
8487 * "remote_expr()" function
8488 */
8489 static void
8490f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8491{
8492 rettv->v_type = VAR_STRING;
8493 rettv->vval.v_string = NULL;
8494#ifdef FEAT_CLIENTSERVER
8495 remote_common(argvars, rettv, TRUE);
8496#endif
8497}
8498
8499/*
8500 * "remote_foreground()" function
8501 */
8502 static void
8503f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8504{
8505#ifdef FEAT_CLIENTSERVER
8506# ifdef WIN32
8507 /* On Win32 it's done in this application. */
8508 {
8509 char_u *server_name = get_tv_string_chk(&argvars[0]);
8510
8511 if (server_name != NULL)
8512 serverForeground(server_name);
8513 }
8514# else
8515 /* Send a foreground() expression to the server. */
8516 argvars[1].v_type = VAR_STRING;
8517 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8518 argvars[2].v_type = VAR_UNKNOWN;
8519 remote_common(argvars, rettv, TRUE);
8520 vim_free(argvars[1].vval.v_string);
8521# endif
8522#endif
8523}
8524
8525 static void
8526f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8527{
8528#ifdef FEAT_CLIENTSERVER
8529 dictitem_T v;
8530 char_u *s = NULL;
8531# ifdef WIN32
8532 long_u n = 0;
8533# endif
8534 char_u *serverid;
8535
8536 if (check_restricted() || check_secure())
8537 {
8538 rettv->vval.v_number = -1;
8539 return;
8540 }
8541 serverid = get_tv_string_chk(&argvars[0]);
8542 if (serverid == NULL)
8543 {
8544 rettv->vval.v_number = -1;
8545 return; /* type error; errmsg already given */
8546 }
8547# ifdef WIN32
8548 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8549 if (n == 0)
8550 rettv->vval.v_number = -1;
8551 else
8552 {
8553 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
8554 rettv->vval.v_number = (s != NULL);
8555 }
8556# else
8557 if (check_connection() == FAIL)
8558 return;
8559
8560 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8561 serverStrToWin(serverid), &s);
8562# endif
8563
8564 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8565 {
8566 char_u *retvar;
8567
8568 v.di_tv.v_type = VAR_STRING;
8569 v.di_tv.vval.v_string = vim_strsave(s);
8570 retvar = get_tv_string_chk(&argvars[1]);
8571 if (retvar != NULL)
8572 set_var(retvar, &v.di_tv, FALSE);
8573 vim_free(v.di_tv.vval.v_string);
8574 }
8575#else
8576 rettv->vval.v_number = -1;
8577#endif
8578}
8579
8580 static void
8581f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8582{
8583 char_u *r = NULL;
8584
8585#ifdef FEAT_CLIENTSERVER
8586 char_u *serverid = get_tv_string_chk(&argvars[0]);
8587
8588 if (serverid != NULL && !check_restricted() && !check_secure())
8589 {
8590# ifdef WIN32
8591 /* The server's HWND is encoded in the 'id' parameter */
8592 long_u n = 0;
8593
8594 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8595 if (n != 0)
8596 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
8597 if (r == NULL)
8598# else
8599 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
8600 serverStrToWin(serverid), &r, FALSE) < 0)
8601# endif
8602 EMSG(_("E277: Unable to read a server reply"));
8603 }
8604#endif
8605 rettv->v_type = VAR_STRING;
8606 rettv->vval.v_string = r;
8607}
8608
8609/*
8610 * "remote_send()" function
8611 */
8612 static void
8613f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8614{
8615 rettv->v_type = VAR_STRING;
8616 rettv->vval.v_string = NULL;
8617#ifdef FEAT_CLIENTSERVER
8618 remote_common(argvars, rettv, FALSE);
8619#endif
8620}
8621
8622/*
8623 * "remove()" function
8624 */
8625 static void
8626f_remove(typval_T *argvars, typval_T *rettv)
8627{
8628 list_T *l;
8629 listitem_T *item, *item2;
8630 listitem_T *li;
8631 long idx;
8632 long end;
8633 char_u *key;
8634 dict_T *d;
8635 dictitem_T *di;
8636 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8637
8638 if (argvars[0].v_type == VAR_DICT)
8639 {
8640 if (argvars[2].v_type != VAR_UNKNOWN)
8641 EMSG2(_(e_toomanyarg), "remove()");
8642 else if ((d = argvars[0].vval.v_dict) != NULL
8643 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8644 {
8645 key = get_tv_string_chk(&argvars[1]);
8646 if (key != NULL)
8647 {
8648 di = dict_find(d, key, -1);
8649 if (di == NULL)
8650 EMSG2(_(e_dictkey), key);
8651 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8652 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8653 {
8654 *rettv = di->di_tv;
8655 init_tv(&di->di_tv);
8656 dictitem_remove(d, di);
8657 }
8658 }
8659 }
8660 }
8661 else if (argvars[0].v_type != VAR_LIST)
8662 EMSG2(_(e_listdictarg), "remove()");
8663 else if ((l = argvars[0].vval.v_list) != NULL
8664 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
8665 {
8666 int error = FALSE;
8667
8668 idx = (long)get_tv_number_chk(&argvars[1], &error);
8669 if (error)
8670 ; /* type error: do nothing, errmsg already given */
8671 else if ((item = list_find(l, idx)) == NULL)
8672 EMSGN(_(e_listidx), idx);
8673 else
8674 {
8675 if (argvars[2].v_type == VAR_UNKNOWN)
8676 {
8677 /* Remove one item, return its value. */
8678 vimlist_remove(l, item, item);
8679 *rettv = item->li_tv;
8680 vim_free(item);
8681 }
8682 else
8683 {
8684 /* Remove range of items, return list with values. */
8685 end = (long)get_tv_number_chk(&argvars[2], &error);
8686 if (error)
8687 ; /* type error: do nothing */
8688 else if ((item2 = list_find(l, end)) == NULL)
8689 EMSGN(_(e_listidx), end);
8690 else
8691 {
8692 int cnt = 0;
8693
8694 for (li = item; li != NULL; li = li->li_next)
8695 {
8696 ++cnt;
8697 if (li == item2)
8698 break;
8699 }
8700 if (li == NULL) /* didn't find "item2" after "item" */
8701 EMSG(_(e_invrange));
8702 else
8703 {
8704 vimlist_remove(l, item, item2);
8705 if (rettv_list_alloc(rettv) == OK)
8706 {
8707 l = rettv->vval.v_list;
8708 l->lv_first = item;
8709 l->lv_last = item2;
8710 item->li_prev = NULL;
8711 item2->li_next = NULL;
8712 l->lv_len = cnt;
8713 }
8714 }
8715 }
8716 }
8717 }
8718 }
8719}
8720
8721/*
8722 * "rename({from}, {to})" function
8723 */
8724 static void
8725f_rename(typval_T *argvars, typval_T *rettv)
8726{
8727 char_u buf[NUMBUFLEN];
8728
8729 if (check_restricted() || check_secure())
8730 rettv->vval.v_number = -1;
8731 else
8732 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
8733 get_tv_string_buf(&argvars[1], buf));
8734}
8735
8736/*
8737 * "repeat()" function
8738 */
8739 static void
8740f_repeat(typval_T *argvars, typval_T *rettv)
8741{
8742 char_u *p;
8743 int n;
8744 int slen;
8745 int len;
8746 char_u *r;
8747 int i;
8748
8749 n = (int)get_tv_number(&argvars[1]);
8750 if (argvars[0].v_type == VAR_LIST)
8751 {
8752 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
8753 while (n-- > 0)
8754 if (list_extend(rettv->vval.v_list,
8755 argvars[0].vval.v_list, NULL) == FAIL)
8756 break;
8757 }
8758 else
8759 {
8760 p = get_tv_string(&argvars[0]);
8761 rettv->v_type = VAR_STRING;
8762 rettv->vval.v_string = NULL;
8763
8764 slen = (int)STRLEN(p);
8765 len = slen * n;
8766 if (len <= 0)
8767 return;
8768
8769 r = alloc(len + 1);
8770 if (r != NULL)
8771 {
8772 for (i = 0; i < n; i++)
8773 mch_memmove(r + i * slen, p, (size_t)slen);
8774 r[len] = NUL;
8775 }
8776
8777 rettv->vval.v_string = r;
8778 }
8779}
8780
8781/*
8782 * "resolve()" function
8783 */
8784 static void
8785f_resolve(typval_T *argvars, typval_T *rettv)
8786{
8787 char_u *p;
8788#ifdef HAVE_READLINK
8789 char_u *buf = NULL;
8790#endif
8791
8792 p = get_tv_string(&argvars[0]);
8793#ifdef FEAT_SHORTCUT
8794 {
8795 char_u *v = NULL;
8796
8797 v = mch_resolve_shortcut(p);
8798 if (v != NULL)
8799 rettv->vval.v_string = v;
8800 else
8801 rettv->vval.v_string = vim_strsave(p);
8802 }
8803#else
8804# ifdef HAVE_READLINK
8805 {
8806 char_u *cpy;
8807 int len;
8808 char_u *remain = NULL;
8809 char_u *q;
8810 int is_relative_to_current = FALSE;
8811 int has_trailing_pathsep = FALSE;
8812 int limit = 100;
8813
8814 p = vim_strsave(p);
8815
8816 if (p[0] == '.' && (vim_ispathsep(p[1])
8817 || (p[1] == '.' && (vim_ispathsep(p[2])))))
8818 is_relative_to_current = TRUE;
8819
8820 len = STRLEN(p);
8821 if (len > 0 && after_pathsep(p, p + len))
8822 {
8823 has_trailing_pathsep = TRUE;
8824 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
8825 }
8826
8827 q = getnextcomp(p);
8828 if (*q != NUL)
8829 {
8830 /* Separate the first path component in "p", and keep the
8831 * remainder (beginning with the path separator). */
8832 remain = vim_strsave(q - 1);
8833 q[-1] = NUL;
8834 }
8835
8836 buf = alloc(MAXPATHL + 1);
8837 if (buf == NULL)
8838 goto fail;
8839
8840 for (;;)
8841 {
8842 for (;;)
8843 {
8844 len = readlink((char *)p, (char *)buf, MAXPATHL);
8845 if (len <= 0)
8846 break;
8847 buf[len] = NUL;
8848
8849 if (limit-- == 0)
8850 {
8851 vim_free(p);
8852 vim_free(remain);
8853 EMSG(_("E655: Too many symbolic links (cycle?)"));
8854 rettv->vval.v_string = NULL;
8855 goto fail;
8856 }
8857
8858 /* Ensure that the result will have a trailing path separator
8859 * if the argument has one. */
8860 if (remain == NULL && has_trailing_pathsep)
8861 add_pathsep(buf);
8862
8863 /* Separate the first path component in the link value and
8864 * concatenate the remainders. */
8865 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
8866 if (*q != NUL)
8867 {
8868 if (remain == NULL)
8869 remain = vim_strsave(q - 1);
8870 else
8871 {
8872 cpy = concat_str(q - 1, remain);
8873 if (cpy != NULL)
8874 {
8875 vim_free(remain);
8876 remain = cpy;
8877 }
8878 }
8879 q[-1] = NUL;
8880 }
8881
8882 q = gettail(p);
8883 if (q > p && *q == NUL)
8884 {
8885 /* Ignore trailing path separator. */
8886 q[-1] = NUL;
8887 q = gettail(p);
8888 }
8889 if (q > p && !mch_isFullName(buf))
8890 {
8891 /* symlink is relative to directory of argument */
8892 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
8893 if (cpy != NULL)
8894 {
8895 STRCPY(cpy, p);
8896 STRCPY(gettail(cpy), buf);
8897 vim_free(p);
8898 p = cpy;
8899 }
8900 }
8901 else
8902 {
8903 vim_free(p);
8904 p = vim_strsave(buf);
8905 }
8906 }
8907
8908 if (remain == NULL)
8909 break;
8910
8911 /* Append the first path component of "remain" to "p". */
8912 q = getnextcomp(remain + 1);
8913 len = q - remain - (*q != NUL);
8914 cpy = vim_strnsave(p, STRLEN(p) + len);
8915 if (cpy != NULL)
8916 {
8917 STRNCAT(cpy, remain, len);
8918 vim_free(p);
8919 p = cpy;
8920 }
8921 /* Shorten "remain". */
8922 if (*q != NUL)
8923 STRMOVE(remain, q - 1);
8924 else
8925 {
8926 vim_free(remain);
8927 remain = NULL;
8928 }
8929 }
8930
8931 /* If the result is a relative path name, make it explicitly relative to
8932 * the current directory if and only if the argument had this form. */
8933 if (!vim_ispathsep(*p))
8934 {
8935 if (is_relative_to_current
8936 && *p != NUL
8937 && !(p[0] == '.'
8938 && (p[1] == NUL
8939 || vim_ispathsep(p[1])
8940 || (p[1] == '.'
8941 && (p[2] == NUL
8942 || vim_ispathsep(p[2]))))))
8943 {
8944 /* Prepend "./". */
8945 cpy = concat_str((char_u *)"./", p);
8946 if (cpy != NULL)
8947 {
8948 vim_free(p);
8949 p = cpy;
8950 }
8951 }
8952 else if (!is_relative_to_current)
8953 {
8954 /* Strip leading "./". */
8955 q = p;
8956 while (q[0] == '.' && vim_ispathsep(q[1]))
8957 q += 2;
8958 if (q > p)
8959 STRMOVE(p, p + 2);
8960 }
8961 }
8962
8963 /* Ensure that the result will have no trailing path separator
8964 * if the argument had none. But keep "/" or "//". */
8965 if (!has_trailing_pathsep)
8966 {
8967 q = p + STRLEN(p);
8968 if (after_pathsep(p, q))
8969 *gettail_sep(p) = NUL;
8970 }
8971
8972 rettv->vval.v_string = p;
8973 }
8974# else
8975 rettv->vval.v_string = vim_strsave(p);
8976# endif
8977#endif
8978
8979 simplify_filename(rettv->vval.v_string);
8980
8981#ifdef HAVE_READLINK
8982fail:
8983 vim_free(buf);
8984#endif
8985 rettv->v_type = VAR_STRING;
8986}
8987
8988/*
8989 * "reverse({list})" function
8990 */
8991 static void
8992f_reverse(typval_T *argvars, typval_T *rettv)
8993{
8994 list_T *l;
8995 listitem_T *li, *ni;
8996
8997 if (argvars[0].v_type != VAR_LIST)
8998 EMSG2(_(e_listarg), "reverse()");
8999 else if ((l = argvars[0].vval.v_list) != NULL
9000 && !tv_check_lock(l->lv_lock,
9001 (char_u *)N_("reverse() argument"), TRUE))
9002 {
9003 li = l->lv_last;
9004 l->lv_first = l->lv_last = NULL;
9005 l->lv_len = 0;
9006 while (li != NULL)
9007 {
9008 ni = li->li_prev;
9009 list_append(l, li);
9010 li = ni;
9011 }
9012 rettv->vval.v_list = l;
9013 rettv->v_type = VAR_LIST;
9014 ++l->lv_refcount;
9015 l->lv_idx = l->lv_len - l->lv_idx - 1;
9016 }
9017}
9018
9019#define SP_NOMOVE 0x01 /* don't move cursor */
9020#define SP_REPEAT 0x02 /* repeat to find outer pair */
9021#define SP_RETCOUNT 0x04 /* return matchcount */
9022#define SP_SETPCMARK 0x08 /* set previous context mark */
9023#define SP_START 0x10 /* accept match at start position */
9024#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9025#define SP_END 0x40 /* leave cursor at end of match */
9026#define SP_COLUMN 0x80 /* start at cursor column */
9027
9028static int get_search_arg(typval_T *varp, int *flagsp);
9029
9030/*
9031 * Get flags for a search function.
9032 * Possibly sets "p_ws".
9033 * Returns BACKWARD, FORWARD or zero (for an error).
9034 */
9035 static int
9036get_search_arg(typval_T *varp, int *flagsp)
9037{
9038 int dir = FORWARD;
9039 char_u *flags;
9040 char_u nbuf[NUMBUFLEN];
9041 int mask;
9042
9043 if (varp->v_type != VAR_UNKNOWN)
9044 {
9045 flags = get_tv_string_buf_chk(varp, nbuf);
9046 if (flags == NULL)
9047 return 0; /* type error; errmsg already given */
9048 while (*flags != NUL)
9049 {
9050 switch (*flags)
9051 {
9052 case 'b': dir = BACKWARD; break;
9053 case 'w': p_ws = TRUE; break;
9054 case 'W': p_ws = FALSE; break;
9055 default: mask = 0;
9056 if (flagsp != NULL)
9057 switch (*flags)
9058 {
9059 case 'c': mask = SP_START; break;
9060 case 'e': mask = SP_END; break;
9061 case 'm': mask = SP_RETCOUNT; break;
9062 case 'n': mask = SP_NOMOVE; break;
9063 case 'p': mask = SP_SUBPAT; break;
9064 case 'r': mask = SP_REPEAT; break;
9065 case 's': mask = SP_SETPCMARK; break;
9066 case 'z': mask = SP_COLUMN; break;
9067 }
9068 if (mask == 0)
9069 {
9070 EMSG2(_(e_invarg2), flags);
9071 dir = 0;
9072 }
9073 else
9074 *flagsp |= mask;
9075 }
9076 if (dir == 0)
9077 break;
9078 ++flags;
9079 }
9080 }
9081 return dir;
9082}
9083
9084/*
9085 * Shared by search() and searchpos() functions.
9086 */
9087 static int
9088search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9089{
9090 int flags;
9091 char_u *pat;
9092 pos_T pos;
9093 pos_T save_cursor;
9094 int save_p_ws = p_ws;
9095 int dir;
9096 int retval = 0; /* default: FAIL */
9097 long lnum_stop = 0;
9098 proftime_T tm;
9099#ifdef FEAT_RELTIME
9100 long time_limit = 0;
9101#endif
9102 int options = SEARCH_KEEP;
9103 int subpatnum;
9104
9105 pat = get_tv_string(&argvars[0]);
9106 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9107 if (dir == 0)
9108 goto theend;
9109 flags = *flagsp;
9110 if (flags & SP_START)
9111 options |= SEARCH_START;
9112 if (flags & SP_END)
9113 options |= SEARCH_END;
9114 if (flags & SP_COLUMN)
9115 options |= SEARCH_COL;
9116
9117 /* Optional arguments: line number to stop searching and timeout. */
9118 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9119 {
9120 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9121 if (lnum_stop < 0)
9122 goto theend;
9123#ifdef FEAT_RELTIME
9124 if (argvars[3].v_type != VAR_UNKNOWN)
9125 {
9126 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9127 if (time_limit < 0)
9128 goto theend;
9129 }
9130#endif
9131 }
9132
9133#ifdef FEAT_RELTIME
9134 /* Set the time limit, if there is one. */
9135 profile_setlimit(time_limit, &tm);
9136#endif
9137
9138 /*
9139 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9140 * Check to make sure only those flags are set.
9141 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9142 * flags cannot be set. Check for that condition also.
9143 */
9144 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9145 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9146 {
9147 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9148 goto theend;
9149 }
9150
9151 pos = save_cursor = curwin->w_cursor;
9152 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9153 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
9154 if (subpatnum != FAIL)
9155 {
9156 if (flags & SP_SUBPAT)
9157 retval = subpatnum;
9158 else
9159 retval = pos.lnum;
9160 if (flags & SP_SETPCMARK)
9161 setpcmark();
9162 curwin->w_cursor = pos;
9163 if (match_pos != NULL)
9164 {
9165 /* Store the match cursor position */
9166 match_pos->lnum = pos.lnum;
9167 match_pos->col = pos.col + 1;
9168 }
9169 /* "/$" will put the cursor after the end of the line, may need to
9170 * correct that here */
9171 check_cursor();
9172 }
9173
9174 /* If 'n' flag is used: restore cursor position. */
9175 if (flags & SP_NOMOVE)
9176 curwin->w_cursor = save_cursor;
9177 else
9178 curwin->w_set_curswant = TRUE;
9179theend:
9180 p_ws = save_p_ws;
9181
9182 return retval;
9183}
9184
9185#ifdef FEAT_FLOAT
9186
9187/*
9188 * round() is not in C90, use ceil() or floor() instead.
9189 */
9190 float_T
9191vim_round(float_T f)
9192{
9193 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9194}
9195
9196/*
9197 * "round({float})" function
9198 */
9199 static void
9200f_round(typval_T *argvars, typval_T *rettv)
9201{
9202 float_T f = 0.0;
9203
9204 rettv->v_type = VAR_FLOAT;
9205 if (get_float_arg(argvars, &f) == OK)
9206 rettv->vval.v_float = vim_round(f);
9207 else
9208 rettv->vval.v_float = 0.0;
9209}
9210#endif
9211
9212/*
9213 * "screenattr()" function
9214 */
9215 static void
9216f_screenattr(typval_T *argvars, typval_T *rettv)
9217{
9218 int row;
9219 int col;
9220 int c;
9221
9222 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9223 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9224 if (row < 0 || row >= screen_Rows
9225 || col < 0 || col >= screen_Columns)
9226 c = -1;
9227 else
9228 c = ScreenAttrs[LineOffset[row] + col];
9229 rettv->vval.v_number = c;
9230}
9231
9232/*
9233 * "screenchar()" function
9234 */
9235 static void
9236f_screenchar(typval_T *argvars, typval_T *rettv)
9237{
9238 int row;
9239 int col;
9240 int off;
9241 int c;
9242
9243 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9244 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9245 if (row < 0 || row >= screen_Rows
9246 || col < 0 || col >= screen_Columns)
9247 c = -1;
9248 else
9249 {
9250 off = LineOffset[row] + col;
9251#ifdef FEAT_MBYTE
9252 if (enc_utf8 && ScreenLinesUC[off] != 0)
9253 c = ScreenLinesUC[off];
9254 else
9255#endif
9256 c = ScreenLines[off];
9257 }
9258 rettv->vval.v_number = c;
9259}
9260
9261/*
9262 * "screencol()" function
9263 *
9264 * First column is 1 to be consistent with virtcol().
9265 */
9266 static void
9267f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9268{
9269 rettv->vval.v_number = screen_screencol() + 1;
9270}
9271
9272/*
9273 * "screenrow()" function
9274 */
9275 static void
9276f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9277{
9278 rettv->vval.v_number = screen_screenrow() + 1;
9279}
9280
9281/*
9282 * "search()" function
9283 */
9284 static void
9285f_search(typval_T *argvars, typval_T *rettv)
9286{
9287 int flags = 0;
9288
9289 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9290}
9291
9292/*
9293 * "searchdecl()" function
9294 */
9295 static void
9296f_searchdecl(typval_T *argvars, typval_T *rettv)
9297{
9298 int locally = 1;
9299 int thisblock = 0;
9300 int error = FALSE;
9301 char_u *name;
9302
9303 rettv->vval.v_number = 1; /* default: FAIL */
9304
9305 name = get_tv_string_chk(&argvars[0]);
9306 if (argvars[1].v_type != VAR_UNKNOWN)
9307 {
9308 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9309 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9310 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9311 }
9312 if (!error && name != NULL)
9313 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9314 locally, thisblock, SEARCH_KEEP) == FAIL;
9315}
9316
9317/*
9318 * Used by searchpair() and searchpairpos()
9319 */
9320 static int
9321searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9322{
9323 char_u *spat, *mpat, *epat;
9324 char_u *skip;
9325 int save_p_ws = p_ws;
9326 int dir;
9327 int flags = 0;
9328 char_u nbuf1[NUMBUFLEN];
9329 char_u nbuf2[NUMBUFLEN];
9330 char_u nbuf3[NUMBUFLEN];
9331 int retval = 0; /* default: FAIL */
9332 long lnum_stop = 0;
9333 long time_limit = 0;
9334
9335 /* Get the three pattern arguments: start, middle, end. */
9336 spat = get_tv_string_chk(&argvars[0]);
9337 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9338 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9339 if (spat == NULL || mpat == NULL || epat == NULL)
9340 goto theend; /* type error */
9341
9342 /* Handle the optional fourth argument: flags */
9343 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9344 if (dir == 0)
9345 goto theend;
9346
9347 /* Don't accept SP_END or SP_SUBPAT.
9348 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9349 */
9350 if ((flags & (SP_END | SP_SUBPAT)) != 0
9351 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9352 {
9353 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9354 goto theend;
9355 }
9356
9357 /* Using 'r' implies 'W', otherwise it doesn't work. */
9358 if (flags & SP_REPEAT)
9359 p_ws = FALSE;
9360
9361 /* Optional fifth argument: skip expression */
9362 if (argvars[3].v_type == VAR_UNKNOWN
9363 || argvars[4].v_type == VAR_UNKNOWN)
9364 skip = (char_u *)"";
9365 else
9366 {
9367 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
9368 if (argvars[5].v_type != VAR_UNKNOWN)
9369 {
9370 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9371 if (lnum_stop < 0)
9372 goto theend;
9373#ifdef FEAT_RELTIME
9374 if (argvars[6].v_type != VAR_UNKNOWN)
9375 {
9376 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9377 if (time_limit < 0)
9378 goto theend;
9379 }
9380#endif
9381 }
9382 }
9383 if (skip == NULL)
9384 goto theend; /* type error */
9385
9386 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9387 match_pos, lnum_stop, time_limit);
9388
9389theend:
9390 p_ws = save_p_ws;
9391
9392 return retval;
9393}
9394
9395/*
9396 * "searchpair()" function
9397 */
9398 static void
9399f_searchpair(typval_T *argvars, typval_T *rettv)
9400{
9401 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9402}
9403
9404/*
9405 * "searchpairpos()" function
9406 */
9407 static void
9408f_searchpairpos(typval_T *argvars, typval_T *rettv)
9409{
9410 pos_T match_pos;
9411 int lnum = 0;
9412 int col = 0;
9413
9414 if (rettv_list_alloc(rettv) == FAIL)
9415 return;
9416
9417 if (searchpair_cmn(argvars, &match_pos) > 0)
9418 {
9419 lnum = match_pos.lnum;
9420 col = match_pos.col;
9421 }
9422
9423 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9424 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9425}
9426
9427/*
9428 * Search for a start/middle/end thing.
9429 * Used by searchpair(), see its documentation for the details.
9430 * Returns 0 or -1 for no match,
9431 */
9432 long
9433do_searchpair(
9434 char_u *spat, /* start pattern */
9435 char_u *mpat, /* middle pattern */
9436 char_u *epat, /* end pattern */
9437 int dir, /* BACKWARD or FORWARD */
9438 char_u *skip, /* skip expression */
9439 int flags, /* SP_SETPCMARK and other SP_ values */
9440 pos_T *match_pos,
9441 linenr_T lnum_stop, /* stop at this line if not zero */
9442 long time_limit UNUSED) /* stop after this many msec */
9443{
9444 char_u *save_cpo;
9445 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9446 long retval = 0;
9447 pos_T pos;
9448 pos_T firstpos;
9449 pos_T foundpos;
9450 pos_T save_cursor;
9451 pos_T save_pos;
9452 int n;
9453 int r;
9454 int nest = 1;
9455 int err;
9456 int options = SEARCH_KEEP;
9457 proftime_T tm;
9458
9459 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9460 save_cpo = p_cpo;
9461 p_cpo = empty_option;
9462
9463#ifdef FEAT_RELTIME
9464 /* Set the time limit, if there is one. */
9465 profile_setlimit(time_limit, &tm);
9466#endif
9467
9468 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9469 * start/middle/end (pat3, for the top pair). */
9470 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
9471 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
9472 if (pat2 == NULL || pat3 == NULL)
9473 goto theend;
9474 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
9475 if (*mpat == NUL)
9476 STRCPY(pat3, pat2);
9477 else
9478 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
9479 spat, epat, mpat);
9480 if (flags & SP_START)
9481 options |= SEARCH_START;
9482
9483 save_cursor = curwin->w_cursor;
9484 pos = curwin->w_cursor;
9485 clearpos(&firstpos);
9486 clearpos(&foundpos);
9487 pat = pat3;
9488 for (;;)
9489 {
9490 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9491 options, RE_SEARCH, lnum_stop, &tm);
9492 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
9493 /* didn't find it or found the first match again: FAIL */
9494 break;
9495
9496 if (firstpos.lnum == 0)
9497 firstpos = pos;
9498 if (equalpos(pos, foundpos))
9499 {
9500 /* Found the same position again. Can happen with a pattern that
9501 * has "\zs" at the end and searching backwards. Advance one
9502 * character and try again. */
9503 if (dir == BACKWARD)
9504 decl(&pos);
9505 else
9506 incl(&pos);
9507 }
9508 foundpos = pos;
9509
9510 /* clear the start flag to avoid getting stuck here */
9511 options &= ~SEARCH_START;
9512
9513 /* If the skip pattern matches, ignore this match. */
9514 if (*skip != NUL)
9515 {
9516 save_pos = curwin->w_cursor;
9517 curwin->w_cursor = pos;
9518 r = eval_to_bool(skip, &err, NULL, FALSE);
9519 curwin->w_cursor = save_pos;
9520 if (err)
9521 {
9522 /* Evaluating {skip} caused an error, break here. */
9523 curwin->w_cursor = save_cursor;
9524 retval = -1;
9525 break;
9526 }
9527 if (r)
9528 continue;
9529 }
9530
9531 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9532 {
9533 /* Found end when searching backwards or start when searching
9534 * forward: nested pair. */
9535 ++nest;
9536 pat = pat2; /* nested, don't search for middle */
9537 }
9538 else
9539 {
9540 /* Found end when searching forward or start when searching
9541 * backward: end of (nested) pair; or found middle in outer pair. */
9542 if (--nest == 1)
9543 pat = pat3; /* outer level, search for middle */
9544 }
9545
9546 if (nest == 0)
9547 {
9548 /* Found the match: return matchcount or line number. */
9549 if (flags & SP_RETCOUNT)
9550 ++retval;
9551 else
9552 retval = pos.lnum;
9553 if (flags & SP_SETPCMARK)
9554 setpcmark();
9555 curwin->w_cursor = pos;
9556 if (!(flags & SP_REPEAT))
9557 break;
9558 nest = 1; /* search for next unmatched */
9559 }
9560 }
9561
9562 if (match_pos != NULL)
9563 {
9564 /* Store the match cursor position */
9565 match_pos->lnum = curwin->w_cursor.lnum;
9566 match_pos->col = curwin->w_cursor.col + 1;
9567 }
9568
9569 /* If 'n' flag is used or search failed: restore cursor position. */
9570 if ((flags & SP_NOMOVE) || retval == 0)
9571 curwin->w_cursor = save_cursor;
9572
9573theend:
9574 vim_free(pat2);
9575 vim_free(pat3);
9576 if (p_cpo == empty_option)
9577 p_cpo = save_cpo;
9578 else
9579 /* Darn, evaluating the {skip} expression changed the value. */
9580 free_string_option(save_cpo);
9581
9582 return retval;
9583}
9584
9585/*
9586 * "searchpos()" function
9587 */
9588 static void
9589f_searchpos(typval_T *argvars, typval_T *rettv)
9590{
9591 pos_T match_pos;
9592 int lnum = 0;
9593 int col = 0;
9594 int n;
9595 int flags = 0;
9596
9597 if (rettv_list_alloc(rettv) == FAIL)
9598 return;
9599
9600 n = search_cmn(argvars, &match_pos, &flags);
9601 if (n > 0)
9602 {
9603 lnum = match_pos.lnum;
9604 col = match_pos.col;
9605 }
9606
9607 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9608 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9609 if (flags & SP_SUBPAT)
9610 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9611}
9612
9613 static void
9614f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9615{
9616#ifdef FEAT_CLIENTSERVER
9617 char_u buf[NUMBUFLEN];
9618 char_u *server = get_tv_string_chk(&argvars[0]);
9619 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9620
9621 rettv->vval.v_number = -1;
9622 if (server == NULL || reply == NULL)
9623 return;
9624 if (check_restricted() || check_secure())
9625 return;
9626# ifdef FEAT_X11
9627 if (check_connection() == FAIL)
9628 return;
9629# endif
9630
9631 if (serverSendReply(server, reply) < 0)
9632 {
9633 EMSG(_("E258: Unable to send to client"));
9634 return;
9635 }
9636 rettv->vval.v_number = 0;
9637#else
9638 rettv->vval.v_number = -1;
9639#endif
9640}
9641
9642 static void
9643f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9644{
9645 char_u *r = NULL;
9646
9647#ifdef FEAT_CLIENTSERVER
9648# ifdef WIN32
9649 r = serverGetVimNames();
9650# else
9651 make_connection();
9652 if (X_DISPLAY != NULL)
9653 r = serverGetVimNames(X_DISPLAY);
9654# endif
9655#endif
9656 rettv->v_type = VAR_STRING;
9657 rettv->vval.v_string = r;
9658}
9659
9660/*
9661 * "setbufvar()" function
9662 */
9663 static void
9664f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
9665{
9666 buf_T *buf;
9667 char_u *varname, *bufvarname;
9668 typval_T *varp;
9669 char_u nbuf[NUMBUFLEN];
9670
9671 if (check_restricted() || check_secure())
9672 return;
9673 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9674 varname = get_tv_string_chk(&argvars[1]);
9675 buf = get_buf_tv(&argvars[0], FALSE);
9676 varp = &argvars[2];
9677
9678 if (buf != NULL && varname != NULL && varp != NULL)
9679 {
9680 if (*varname == '&')
9681 {
9682 long numval;
9683 char_u *strval;
9684 int error = FALSE;
9685 aco_save_T aco;
9686
9687 /* set curbuf to be our buf, temporarily */
9688 aucmd_prepbuf(&aco, buf);
9689
9690 ++varname;
9691 numval = (long)get_tv_number_chk(varp, &error);
9692 strval = get_tv_string_buf_chk(varp, nbuf);
9693 if (!error && strval != NULL)
9694 set_option_value(varname, numval, strval, OPT_LOCAL);
9695
9696 /* reset notion of buffer */
9697 aucmd_restbuf(&aco);
9698 }
9699 else
9700 {
9701 buf_T *save_curbuf = curbuf;
9702
9703 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
9704 if (bufvarname != NULL)
9705 {
9706 curbuf = buf;
9707 STRCPY(bufvarname, "b:");
9708 STRCPY(bufvarname + 2, varname);
9709 set_var(bufvarname, varp, TRUE);
9710 vim_free(bufvarname);
9711 curbuf = save_curbuf;
9712 }
9713 }
9714 }
9715}
9716
9717 static void
9718f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
9719{
9720 dict_T *d;
9721 dictitem_T *di;
9722 char_u *csearch;
9723
9724 if (argvars[0].v_type != VAR_DICT)
9725 {
9726 EMSG(_(e_dictreq));
9727 return;
9728 }
9729
9730 if ((d = argvars[0].vval.v_dict) != NULL)
9731 {
9732 csearch = get_dict_string(d, (char_u *)"char", FALSE);
9733 if (csearch != NULL)
9734 {
9735#ifdef FEAT_MBYTE
9736 if (enc_utf8)
9737 {
9738 int pcc[MAX_MCO];
9739 int c = utfc_ptr2char(csearch, pcc);
9740
9741 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
9742 }
9743 else
9744#endif
9745 set_last_csearch(PTR2CHAR(csearch),
9746 csearch, MB_PTR2LEN(csearch));
9747 }
9748
9749 di = dict_find(d, (char_u *)"forward", -1);
9750 if (di != NULL)
9751 set_csearch_direction((int)get_tv_number(&di->di_tv)
9752 ? FORWARD : BACKWARD);
9753
9754 di = dict_find(d, (char_u *)"until", -1);
9755 if (di != NULL)
9756 set_csearch_until(!!get_tv_number(&di->di_tv));
9757 }
9758}
9759
9760/*
9761 * "setcmdpos()" function
9762 */
9763 static void
9764f_setcmdpos(typval_T *argvars, typval_T *rettv)
9765{
9766 int pos = (int)get_tv_number(&argvars[0]) - 1;
9767
9768 if (pos >= 0)
9769 rettv->vval.v_number = set_cmdline_pos(pos);
9770}
9771
9772/*
9773 * "setfperm({fname}, {mode})" function
9774 */
9775 static void
9776f_setfperm(typval_T *argvars, typval_T *rettv)
9777{
9778 char_u *fname;
9779 char_u modebuf[NUMBUFLEN];
9780 char_u *mode_str;
9781 int i;
9782 int mask;
9783 int mode = 0;
9784
9785 rettv->vval.v_number = 0;
9786 fname = get_tv_string_chk(&argvars[0]);
9787 if (fname == NULL)
9788 return;
9789 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
9790 if (mode_str == NULL)
9791 return;
9792 if (STRLEN(mode_str) != 9)
9793 {
9794 EMSG2(_(e_invarg2), mode_str);
9795 return;
9796 }
9797
9798 mask = 1;
9799 for (i = 8; i >= 0; --i)
9800 {
9801 if (mode_str[i] != '-')
9802 mode |= mask;
9803 mask = mask << 1;
9804 }
9805 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
9806}
9807
9808/*
9809 * "setline()" function
9810 */
9811 static void
9812f_setline(typval_T *argvars, typval_T *rettv)
9813{
9814 linenr_T lnum;
9815 char_u *line = NULL;
9816 list_T *l = NULL;
9817 listitem_T *li = NULL;
9818 long added = 0;
9819 linenr_T lcount = curbuf->b_ml.ml_line_count;
9820
9821 lnum = get_tv_lnum(&argvars[0]);
9822 if (argvars[1].v_type == VAR_LIST)
9823 {
9824 l = argvars[1].vval.v_list;
9825 li = l->lv_first;
9826 }
9827 else
9828 line = get_tv_string_chk(&argvars[1]);
9829
9830 /* default result is zero == OK */
9831 for (;;)
9832 {
9833 if (l != NULL)
9834 {
9835 /* list argument, get next string */
9836 if (li == NULL)
9837 break;
9838 line = get_tv_string_chk(&li->li_tv);
9839 li = li->li_next;
9840 }
9841
9842 rettv->vval.v_number = 1; /* FAIL */
9843 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
9844 break;
9845
9846 /* When coming here from Insert mode, sync undo, so that this can be
9847 * undone separately from what was previously inserted. */
9848 if (u_sync_once == 2)
9849 {
9850 u_sync_once = 1; /* notify that u_sync() was called */
9851 u_sync(TRUE);
9852 }
9853
9854 if (lnum <= curbuf->b_ml.ml_line_count)
9855 {
9856 /* existing line, replace it */
9857 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
9858 {
9859 changed_bytes(lnum, 0);
9860 if (lnum == curwin->w_cursor.lnum)
9861 check_cursor_col();
9862 rettv->vval.v_number = 0; /* OK */
9863 }
9864 }
9865 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
9866 {
9867 /* lnum is one past the last line, append the line */
9868 ++added;
9869 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
9870 rettv->vval.v_number = 0; /* OK */
9871 }
9872
9873 if (l == NULL) /* only one string argument */
9874 break;
9875 ++lnum;
9876 }
9877
9878 if (added > 0)
9879 appended_lines_mark(lcount, added);
9880}
9881
Bram Moolenaard823fa92016-08-12 16:29:27 +02009882static 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 +02009883
9884/*
9885 * Used by "setqflist()" and "setloclist()" functions
9886 */
9887 static void
9888set_qf_ll_list(
9889 win_T *wp UNUSED,
9890 typval_T *list_arg UNUSED,
9891 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +02009892 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009893 typval_T *rettv)
9894{
9895#ifdef FEAT_QUICKFIX
9896 static char *e_invact = N_("E927: Invalid action: '%s'");
9897 char_u *act;
9898 int action = 0;
9899#endif
9900
9901 rettv->vval.v_number = -1;
9902
9903#ifdef FEAT_QUICKFIX
9904 if (list_arg->v_type != VAR_LIST)
9905 EMSG(_(e_listreq));
9906 else
9907 {
9908 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +02009909 dict_T *d = NULL;
9910 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009911
9912 if (action_arg->v_type == VAR_STRING)
9913 {
9914 act = get_tv_string_chk(action_arg);
9915 if (act == NULL)
9916 return; /* type error; errmsg already given */
9917 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
9918 action = *act;
9919 else
9920 EMSG2(_(e_invact), act);
9921 }
9922 else if (action_arg->v_type == VAR_UNKNOWN)
9923 action = ' ';
9924 else
9925 EMSG(_(e_stringreq));
9926
Bram Moolenaard823fa92016-08-12 16:29:27 +02009927 if (action_arg->v_type != VAR_UNKNOWN
9928 && what_arg->v_type != VAR_UNKNOWN)
9929 {
9930 if (what_arg->v_type == VAR_DICT)
9931 d = what_arg->vval.v_dict;
9932 else
9933 {
9934 EMSG(_(e_dictreq));
9935 valid_dict = FALSE;
9936 }
9937 }
9938
9939 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
9940 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009941 rettv->vval.v_number = 0;
9942 }
9943#endif
9944}
9945
9946/*
9947 * "setloclist()" function
9948 */
9949 static void
9950f_setloclist(typval_T *argvars, typval_T *rettv)
9951{
9952 win_T *win;
9953
9954 rettv->vval.v_number = -1;
9955
9956 win = find_win_by_nr(&argvars[0], NULL);
9957 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02009958 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009959}
9960
9961/*
9962 * "setmatches()" function
9963 */
9964 static void
9965f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9966{
9967#ifdef FEAT_SEARCH_EXTRA
9968 list_T *l;
9969 listitem_T *li;
9970 dict_T *d;
9971 list_T *s = NULL;
9972
9973 rettv->vval.v_number = -1;
9974 if (argvars[0].v_type != VAR_LIST)
9975 {
9976 EMSG(_(e_listreq));
9977 return;
9978 }
9979 if ((l = argvars[0].vval.v_list) != NULL)
9980 {
9981
9982 /* To some extent make sure that we are dealing with a list from
9983 * "getmatches()". */
9984 li = l->lv_first;
9985 while (li != NULL)
9986 {
9987 if (li->li_tv.v_type != VAR_DICT
9988 || (d = li->li_tv.vval.v_dict) == NULL)
9989 {
9990 EMSG(_(e_invarg));
9991 return;
9992 }
9993 if (!(dict_find(d, (char_u *)"group", -1) != NULL
9994 && (dict_find(d, (char_u *)"pattern", -1) != NULL
9995 || dict_find(d, (char_u *)"pos1", -1) != NULL)
9996 && dict_find(d, (char_u *)"priority", -1) != NULL
9997 && dict_find(d, (char_u *)"id", -1) != NULL))
9998 {
9999 EMSG(_(e_invarg));
10000 return;
10001 }
10002 li = li->li_next;
10003 }
10004
10005 clear_matches(curwin);
10006 li = l->lv_first;
10007 while (li != NULL)
10008 {
10009 int i = 0;
10010 char_u buf[5];
10011 dictitem_T *di;
10012 char_u *group;
10013 int priority;
10014 int id;
10015 char_u *conceal;
10016
10017 d = li->li_tv.vval.v_dict;
10018 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10019 {
10020 if (s == NULL)
10021 {
10022 s = list_alloc();
10023 if (s == NULL)
10024 return;
10025 }
10026
10027 /* match from matchaddpos() */
10028 for (i = 1; i < 9; i++)
10029 {
10030 sprintf((char *)buf, (char *)"pos%d", i);
10031 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10032 {
10033 if (di->di_tv.v_type != VAR_LIST)
10034 return;
10035
10036 list_append_tv(s, &di->di_tv);
10037 s->lv_refcount++;
10038 }
10039 else
10040 break;
10041 }
10042 }
10043
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010044 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010045 priority = (int)get_dict_number(d, (char_u *)"priority");
10046 id = (int)get_dict_number(d, (char_u *)"id");
10047 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010048 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010049 : NULL;
10050 if (i == 0)
10051 {
10052 match_add(curwin, group,
10053 get_dict_string(d, (char_u *)"pattern", FALSE),
10054 priority, id, NULL, conceal);
10055 }
10056 else
10057 {
10058 match_add(curwin, group, NULL, priority, id, s, conceal);
10059 list_unref(s);
10060 s = NULL;
10061 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010062 vim_free(group);
10063 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010064
10065 li = li->li_next;
10066 }
10067 rettv->vval.v_number = 0;
10068 }
10069#endif
10070}
10071
10072/*
10073 * "setpos()" function
10074 */
10075 static void
10076f_setpos(typval_T *argvars, typval_T *rettv)
10077{
10078 pos_T pos;
10079 int fnum;
10080 char_u *name;
10081 colnr_T curswant = -1;
10082
10083 rettv->vval.v_number = -1;
10084 name = get_tv_string_chk(argvars);
10085 if (name != NULL)
10086 {
10087 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10088 {
10089 if (--pos.col < 0)
10090 pos.col = 0;
10091 if (name[0] == '.' && name[1] == NUL)
10092 {
10093 /* set cursor */
10094 if (fnum == curbuf->b_fnum)
10095 {
10096 curwin->w_cursor = pos;
10097 if (curswant >= 0)
10098 {
10099 curwin->w_curswant = curswant - 1;
10100 curwin->w_set_curswant = FALSE;
10101 }
10102 check_cursor();
10103 rettv->vval.v_number = 0;
10104 }
10105 else
10106 EMSG(_(e_invarg));
10107 }
10108 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10109 {
10110 /* set mark */
10111 if (setmark_pos(name[1], &pos, fnum) == OK)
10112 rettv->vval.v_number = 0;
10113 }
10114 else
10115 EMSG(_(e_invarg));
10116 }
10117 }
10118}
10119
10120/*
10121 * "setqflist()" function
10122 */
10123 static void
10124f_setqflist(typval_T *argvars, typval_T *rettv)
10125{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010126 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010127}
10128
10129/*
10130 * "setreg()" function
10131 */
10132 static void
10133f_setreg(typval_T *argvars, typval_T *rettv)
10134{
10135 int regname;
10136 char_u *strregname;
10137 char_u *stropt;
10138 char_u *strval;
10139 int append;
10140 char_u yank_type;
10141 long block_len;
10142
10143 block_len = -1;
10144 yank_type = MAUTO;
10145 append = FALSE;
10146
10147 strregname = get_tv_string_chk(argvars);
10148 rettv->vval.v_number = 1; /* FAIL is default */
10149
10150 if (strregname == NULL)
10151 return; /* type error; errmsg already given */
10152 regname = *strregname;
10153 if (regname == 0 || regname == '@')
10154 regname = '"';
10155
10156 if (argvars[2].v_type != VAR_UNKNOWN)
10157 {
10158 stropt = get_tv_string_chk(&argvars[2]);
10159 if (stropt == NULL)
10160 return; /* type error */
10161 for (; *stropt != NUL; ++stropt)
10162 switch (*stropt)
10163 {
10164 case 'a': case 'A': /* append */
10165 append = TRUE;
10166 break;
10167 case 'v': case 'c': /* character-wise selection */
10168 yank_type = MCHAR;
10169 break;
10170 case 'V': case 'l': /* line-wise selection */
10171 yank_type = MLINE;
10172 break;
10173 case 'b': case Ctrl_V: /* block-wise selection */
10174 yank_type = MBLOCK;
10175 if (VIM_ISDIGIT(stropt[1]))
10176 {
10177 ++stropt;
10178 block_len = getdigits(&stropt) - 1;
10179 --stropt;
10180 }
10181 break;
10182 }
10183 }
10184
10185 if (argvars[1].v_type == VAR_LIST)
10186 {
10187 char_u **lstval;
10188 char_u **allocval;
10189 char_u buf[NUMBUFLEN];
10190 char_u **curval;
10191 char_u **curallocval;
10192 list_T *ll = argvars[1].vval.v_list;
10193 listitem_T *li;
10194 int len;
10195
10196 /* If the list is NULL handle like an empty list. */
10197 len = ll == NULL ? 0 : ll->lv_len;
10198
10199 /* First half: use for pointers to result lines; second half: use for
10200 * pointers to allocated copies. */
10201 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10202 if (lstval == NULL)
10203 return;
10204 curval = lstval;
10205 allocval = lstval + len + 2;
10206 curallocval = allocval;
10207
10208 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10209 li = li->li_next)
10210 {
10211 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10212 if (strval == NULL)
10213 goto free_lstval;
10214 if (strval == buf)
10215 {
10216 /* Need to make a copy, next get_tv_string_buf_chk() will
10217 * overwrite the string. */
10218 strval = vim_strsave(buf);
10219 if (strval == NULL)
10220 goto free_lstval;
10221 *curallocval++ = strval;
10222 }
10223 *curval++ = strval;
10224 }
10225 *curval++ = NULL;
10226
10227 write_reg_contents_lst(regname, lstval, -1,
10228 append, yank_type, block_len);
10229free_lstval:
10230 while (curallocval > allocval)
10231 vim_free(*--curallocval);
10232 vim_free(lstval);
10233 }
10234 else
10235 {
10236 strval = get_tv_string_chk(&argvars[1]);
10237 if (strval == NULL)
10238 return;
10239 write_reg_contents_ex(regname, strval, -1,
10240 append, yank_type, block_len);
10241 }
10242 rettv->vval.v_number = 0;
10243}
10244
10245/*
10246 * "settabvar()" function
10247 */
10248 static void
10249f_settabvar(typval_T *argvars, typval_T *rettv)
10250{
10251#ifdef FEAT_WINDOWS
10252 tabpage_T *save_curtab;
10253 tabpage_T *tp;
10254#endif
10255 char_u *varname, *tabvarname;
10256 typval_T *varp;
10257
10258 rettv->vval.v_number = 0;
10259
10260 if (check_restricted() || check_secure())
10261 return;
10262
10263#ifdef FEAT_WINDOWS
10264 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10265#endif
10266 varname = get_tv_string_chk(&argvars[1]);
10267 varp = &argvars[2];
10268
10269 if (varname != NULL && varp != NULL
10270#ifdef FEAT_WINDOWS
10271 && tp != NULL
10272#endif
10273 )
10274 {
10275#ifdef FEAT_WINDOWS
10276 save_curtab = curtab;
10277 goto_tabpage_tp(tp, FALSE, FALSE);
10278#endif
10279
10280 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10281 if (tabvarname != NULL)
10282 {
10283 STRCPY(tabvarname, "t:");
10284 STRCPY(tabvarname + 2, varname);
10285 set_var(tabvarname, varp, TRUE);
10286 vim_free(tabvarname);
10287 }
10288
10289#ifdef FEAT_WINDOWS
10290 /* Restore current tabpage */
10291 if (valid_tabpage(save_curtab))
10292 goto_tabpage_tp(save_curtab, FALSE, FALSE);
10293#endif
10294 }
10295}
10296
10297/*
10298 * "settabwinvar()" function
10299 */
10300 static void
10301f_settabwinvar(typval_T *argvars, typval_T *rettv)
10302{
10303 setwinvar(argvars, rettv, 1);
10304}
10305
10306/*
10307 * "setwinvar()" function
10308 */
10309 static void
10310f_setwinvar(typval_T *argvars, typval_T *rettv)
10311{
10312 setwinvar(argvars, rettv, 0);
10313}
10314
10315#ifdef FEAT_CRYPT
10316/*
10317 * "sha256({string})" function
10318 */
10319 static void
10320f_sha256(typval_T *argvars, typval_T *rettv)
10321{
10322 char_u *p;
10323
10324 p = get_tv_string(&argvars[0]);
10325 rettv->vval.v_string = vim_strsave(
10326 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10327 rettv->v_type = VAR_STRING;
10328}
10329#endif /* FEAT_CRYPT */
10330
10331/*
10332 * "shellescape({string})" function
10333 */
10334 static void
10335f_shellescape(typval_T *argvars, typval_T *rettv)
10336{
10337 rettv->vval.v_string = vim_strsave_shellescape(
10338 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
10339 rettv->v_type = VAR_STRING;
10340}
10341
10342/*
10343 * shiftwidth() function
10344 */
10345 static void
10346f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10347{
10348 rettv->vval.v_number = get_sw_value(curbuf);
10349}
10350
10351/*
10352 * "simplify()" function
10353 */
10354 static void
10355f_simplify(typval_T *argvars, typval_T *rettv)
10356{
10357 char_u *p;
10358
10359 p = get_tv_string(&argvars[0]);
10360 rettv->vval.v_string = vim_strsave(p);
10361 simplify_filename(rettv->vval.v_string); /* simplify in place */
10362 rettv->v_type = VAR_STRING;
10363}
10364
10365#ifdef FEAT_FLOAT
10366/*
10367 * "sin()" function
10368 */
10369 static void
10370f_sin(typval_T *argvars, typval_T *rettv)
10371{
10372 float_T f = 0.0;
10373
10374 rettv->v_type = VAR_FLOAT;
10375 if (get_float_arg(argvars, &f) == OK)
10376 rettv->vval.v_float = sin(f);
10377 else
10378 rettv->vval.v_float = 0.0;
10379}
10380
10381/*
10382 * "sinh()" function
10383 */
10384 static void
10385f_sinh(typval_T *argvars, typval_T *rettv)
10386{
10387 float_T f = 0.0;
10388
10389 rettv->v_type = VAR_FLOAT;
10390 if (get_float_arg(argvars, &f) == OK)
10391 rettv->vval.v_float = sinh(f);
10392 else
10393 rettv->vval.v_float = 0.0;
10394}
10395#endif
10396
10397static int
10398#ifdef __BORLANDC__
10399 _RTLENTRYF
10400#endif
10401 item_compare(const void *s1, const void *s2);
10402static int
10403#ifdef __BORLANDC__
10404 _RTLENTRYF
10405#endif
10406 item_compare2(const void *s1, const void *s2);
10407
10408/* struct used in the array that's given to qsort() */
10409typedef struct
10410{
10411 listitem_T *item;
10412 int idx;
10413} sortItem_T;
10414
10415/* struct storing information about current sort */
10416typedef struct
10417{
10418 int item_compare_ic;
10419 int item_compare_numeric;
10420 int item_compare_numbers;
10421#ifdef FEAT_FLOAT
10422 int item_compare_float;
10423#endif
10424 char_u *item_compare_func;
10425 partial_T *item_compare_partial;
10426 dict_T *item_compare_selfdict;
10427 int item_compare_func_err;
10428 int item_compare_keep_zero;
10429} sortinfo_T;
10430static sortinfo_T *sortinfo = NULL;
10431static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10432#define ITEM_COMPARE_FAIL 999
10433
10434/*
10435 * Compare functions for f_sort() and f_uniq() below.
10436 */
10437 static int
10438#ifdef __BORLANDC__
10439_RTLENTRYF
10440#endif
10441item_compare(const void *s1, const void *s2)
10442{
10443 sortItem_T *si1, *si2;
10444 typval_T *tv1, *tv2;
10445 char_u *p1, *p2;
10446 char_u *tofree1 = NULL, *tofree2 = NULL;
10447 int res;
10448 char_u numbuf1[NUMBUFLEN];
10449 char_u numbuf2[NUMBUFLEN];
10450
10451 si1 = (sortItem_T *)s1;
10452 si2 = (sortItem_T *)s2;
10453 tv1 = &si1->item->li_tv;
10454 tv2 = &si2->item->li_tv;
10455
10456 if (sortinfo->item_compare_numbers)
10457 {
10458 varnumber_T v1 = get_tv_number(tv1);
10459 varnumber_T v2 = get_tv_number(tv2);
10460
10461 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10462 }
10463
10464#ifdef FEAT_FLOAT
10465 if (sortinfo->item_compare_float)
10466 {
10467 float_T v1 = get_tv_float(tv1);
10468 float_T v2 = get_tv_float(tv2);
10469
10470 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10471 }
10472#endif
10473
10474 /* tv2string() puts quotes around a string and allocates memory. Don't do
10475 * that for string variables. Use a single quote when comparing with a
10476 * non-string to do what the docs promise. */
10477 if (tv1->v_type == VAR_STRING)
10478 {
10479 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10480 p1 = (char_u *)"'";
10481 else
10482 p1 = tv1->vval.v_string;
10483 }
10484 else
10485 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10486 if (tv2->v_type == VAR_STRING)
10487 {
10488 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10489 p2 = (char_u *)"'";
10490 else
10491 p2 = tv2->vval.v_string;
10492 }
10493 else
10494 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10495 if (p1 == NULL)
10496 p1 = (char_u *)"";
10497 if (p2 == NULL)
10498 p2 = (char_u *)"";
10499 if (!sortinfo->item_compare_numeric)
10500 {
10501 if (sortinfo->item_compare_ic)
10502 res = STRICMP(p1, p2);
10503 else
10504 res = STRCMP(p1, p2);
10505 }
10506 else
10507 {
10508 double n1, n2;
10509 n1 = strtod((char *)p1, (char **)&p1);
10510 n2 = strtod((char *)p2, (char **)&p2);
10511 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10512 }
10513
10514 /* When the result would be zero, compare the item indexes. Makes the
10515 * sort stable. */
10516 if (res == 0 && !sortinfo->item_compare_keep_zero)
10517 res = si1->idx > si2->idx ? 1 : -1;
10518
10519 vim_free(tofree1);
10520 vim_free(tofree2);
10521 return res;
10522}
10523
10524 static int
10525#ifdef __BORLANDC__
10526_RTLENTRYF
10527#endif
10528item_compare2(const void *s1, const void *s2)
10529{
10530 sortItem_T *si1, *si2;
10531 int res;
10532 typval_T rettv;
10533 typval_T argv[3];
10534 int dummy;
10535 char_u *func_name;
10536 partial_T *partial = sortinfo->item_compare_partial;
10537
10538 /* shortcut after failure in previous call; compare all items equal */
10539 if (sortinfo->item_compare_func_err)
10540 return 0;
10541
10542 si1 = (sortItem_T *)s1;
10543 si2 = (sortItem_T *)s2;
10544
10545 if (partial == NULL)
10546 func_name = sortinfo->item_compare_func;
10547 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010548 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010549
10550 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10551 * in the copy without changing the original list items. */
10552 copy_tv(&si1->item->li_tv, &argv[0]);
10553 copy_tv(&si2->item->li_tv, &argv[1]);
10554
10555 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10556 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010557 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010558 partial, sortinfo->item_compare_selfdict);
10559 clear_tv(&argv[0]);
10560 clear_tv(&argv[1]);
10561
10562 if (res == FAIL)
10563 res = ITEM_COMPARE_FAIL;
10564 else
10565 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10566 if (sortinfo->item_compare_func_err)
10567 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10568 clear_tv(&rettv);
10569
10570 /* When the result would be zero, compare the pointers themselves. Makes
10571 * the sort stable. */
10572 if (res == 0 && !sortinfo->item_compare_keep_zero)
10573 res = si1->idx > si2->idx ? 1 : -1;
10574
10575 return res;
10576}
10577
10578/*
10579 * "sort({list})" function
10580 */
10581 static void
10582do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10583{
10584 list_T *l;
10585 listitem_T *li;
10586 sortItem_T *ptrs;
10587 sortinfo_T *old_sortinfo;
10588 sortinfo_T info;
10589 long len;
10590 long i;
10591
10592 /* Pointer to current info struct used in compare function. Save and
10593 * restore the current one for nested calls. */
10594 old_sortinfo = sortinfo;
10595 sortinfo = &info;
10596
10597 if (argvars[0].v_type != VAR_LIST)
10598 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
10599 else
10600 {
10601 l = argvars[0].vval.v_list;
10602 if (l == NULL || tv_check_lock(l->lv_lock,
10603 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
10604 TRUE))
10605 goto theend;
10606 rettv->vval.v_list = l;
10607 rettv->v_type = VAR_LIST;
10608 ++l->lv_refcount;
10609
10610 len = list_len(l);
10611 if (len <= 1)
10612 goto theend; /* short list sorts pretty quickly */
10613
10614 info.item_compare_ic = FALSE;
10615 info.item_compare_numeric = FALSE;
10616 info.item_compare_numbers = FALSE;
10617#ifdef FEAT_FLOAT
10618 info.item_compare_float = FALSE;
10619#endif
10620 info.item_compare_func = NULL;
10621 info.item_compare_partial = NULL;
10622 info.item_compare_selfdict = NULL;
10623 if (argvars[1].v_type != VAR_UNKNOWN)
10624 {
10625 /* optional second argument: {func} */
10626 if (argvars[1].v_type == VAR_FUNC)
10627 info.item_compare_func = argvars[1].vval.v_string;
10628 else if (argvars[1].v_type == VAR_PARTIAL)
10629 info.item_compare_partial = argvars[1].vval.v_partial;
10630 else
10631 {
10632 int error = FALSE;
10633
10634 i = (long)get_tv_number_chk(&argvars[1], &error);
10635 if (error)
10636 goto theend; /* type error; errmsg already given */
10637 if (i == 1)
10638 info.item_compare_ic = TRUE;
10639 else if (argvars[1].v_type != VAR_NUMBER)
10640 info.item_compare_func = get_tv_string(&argvars[1]);
10641 else if (i != 0)
10642 {
10643 EMSG(_(e_invarg));
10644 goto theend;
10645 }
10646 if (info.item_compare_func != NULL)
10647 {
10648 if (*info.item_compare_func == NUL)
10649 {
10650 /* empty string means default sort */
10651 info.item_compare_func = NULL;
10652 }
10653 else if (STRCMP(info.item_compare_func, "n") == 0)
10654 {
10655 info.item_compare_func = NULL;
10656 info.item_compare_numeric = TRUE;
10657 }
10658 else if (STRCMP(info.item_compare_func, "N") == 0)
10659 {
10660 info.item_compare_func = NULL;
10661 info.item_compare_numbers = TRUE;
10662 }
10663#ifdef FEAT_FLOAT
10664 else if (STRCMP(info.item_compare_func, "f") == 0)
10665 {
10666 info.item_compare_func = NULL;
10667 info.item_compare_float = TRUE;
10668 }
10669#endif
10670 else if (STRCMP(info.item_compare_func, "i") == 0)
10671 {
10672 info.item_compare_func = NULL;
10673 info.item_compare_ic = TRUE;
10674 }
10675 }
10676 }
10677
10678 if (argvars[2].v_type != VAR_UNKNOWN)
10679 {
10680 /* optional third argument: {dict} */
10681 if (argvars[2].v_type != VAR_DICT)
10682 {
10683 EMSG(_(e_dictreq));
10684 goto theend;
10685 }
10686 info.item_compare_selfdict = argvars[2].vval.v_dict;
10687 }
10688 }
10689
10690 /* Make an array with each entry pointing to an item in the List. */
10691 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
10692 if (ptrs == NULL)
10693 goto theend;
10694
10695 i = 0;
10696 if (sort)
10697 {
10698 /* sort(): ptrs will be the list to sort */
10699 for (li = l->lv_first; li != NULL; li = li->li_next)
10700 {
10701 ptrs[i].item = li;
10702 ptrs[i].idx = i;
10703 ++i;
10704 }
10705
10706 info.item_compare_func_err = FALSE;
10707 info.item_compare_keep_zero = FALSE;
10708 /* test the compare function */
10709 if ((info.item_compare_func != NULL
10710 || info.item_compare_partial != NULL)
10711 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
10712 == ITEM_COMPARE_FAIL)
10713 EMSG(_("E702: Sort compare function failed"));
10714 else
10715 {
10716 /* Sort the array with item pointers. */
10717 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
10718 info.item_compare_func == NULL
10719 && info.item_compare_partial == NULL
10720 ? item_compare : item_compare2);
10721
10722 if (!info.item_compare_func_err)
10723 {
10724 /* Clear the List and append the items in sorted order. */
10725 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
10726 l->lv_len = 0;
10727 for (i = 0; i < len; ++i)
10728 list_append(l, ptrs[i].item);
10729 }
10730 }
10731 }
10732 else
10733 {
10734 int (*item_compare_func_ptr)(const void *, const void *);
10735
10736 /* f_uniq(): ptrs will be a stack of items to remove */
10737 info.item_compare_func_err = FALSE;
10738 info.item_compare_keep_zero = TRUE;
10739 item_compare_func_ptr = info.item_compare_func != NULL
10740 || info.item_compare_partial != NULL
10741 ? item_compare2 : item_compare;
10742
10743 for (li = l->lv_first; li != NULL && li->li_next != NULL;
10744 li = li->li_next)
10745 {
10746 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
10747 == 0)
10748 ptrs[i++].item = li;
10749 if (info.item_compare_func_err)
10750 {
10751 EMSG(_("E882: Uniq compare function failed"));
10752 break;
10753 }
10754 }
10755
10756 if (!info.item_compare_func_err)
10757 {
10758 while (--i >= 0)
10759 {
10760 li = ptrs[i].item->li_next;
10761 ptrs[i].item->li_next = li->li_next;
10762 if (li->li_next != NULL)
10763 li->li_next->li_prev = ptrs[i].item;
10764 else
10765 l->lv_last = ptrs[i].item;
10766 list_fix_watch(l, li);
10767 listitem_free(li);
10768 l->lv_len--;
10769 }
10770 }
10771 }
10772
10773 vim_free(ptrs);
10774 }
10775theend:
10776 sortinfo = old_sortinfo;
10777}
10778
10779/*
10780 * "sort({list})" function
10781 */
10782 static void
10783f_sort(typval_T *argvars, typval_T *rettv)
10784{
10785 do_sort_uniq(argvars, rettv, TRUE);
10786}
10787
10788/*
10789 * "uniq({list})" function
10790 */
10791 static void
10792f_uniq(typval_T *argvars, typval_T *rettv)
10793{
10794 do_sort_uniq(argvars, rettv, FALSE);
10795}
10796
10797/*
10798 * "soundfold({word})" function
10799 */
10800 static void
10801f_soundfold(typval_T *argvars, typval_T *rettv)
10802{
10803 char_u *s;
10804
10805 rettv->v_type = VAR_STRING;
10806 s = get_tv_string(&argvars[0]);
10807#ifdef FEAT_SPELL
10808 rettv->vval.v_string = eval_soundfold(s);
10809#else
10810 rettv->vval.v_string = vim_strsave(s);
10811#endif
10812}
10813
10814/*
10815 * "spellbadword()" function
10816 */
10817 static void
10818f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10819{
10820 char_u *word = (char_u *)"";
10821 hlf_T attr = HLF_COUNT;
10822 int len = 0;
10823
10824 if (rettv_list_alloc(rettv) == FAIL)
10825 return;
10826
10827#ifdef FEAT_SPELL
10828 if (argvars[0].v_type == VAR_UNKNOWN)
10829 {
10830 /* Find the start and length of the badly spelled word. */
10831 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10832 if (len != 0)
10833 word = ml_get_cursor();
10834 }
10835 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10836 {
10837 char_u *str = get_tv_string_chk(&argvars[0]);
10838 int capcol = -1;
10839
10840 if (str != NULL)
10841 {
10842 /* Check the argument for spelling. */
10843 while (*str != NUL)
10844 {
10845 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10846 if (attr != HLF_COUNT)
10847 {
10848 word = str;
10849 break;
10850 }
10851 str += len;
10852 }
10853 }
10854 }
10855#endif
10856
10857 list_append_string(rettv->vval.v_list, word, len);
10858 list_append_string(rettv->vval.v_list, (char_u *)(
10859 attr == HLF_SPB ? "bad" :
10860 attr == HLF_SPR ? "rare" :
10861 attr == HLF_SPL ? "local" :
10862 attr == HLF_SPC ? "caps" :
10863 ""), -1);
10864}
10865
10866/*
10867 * "spellsuggest()" function
10868 */
10869 static void
10870f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
10871{
10872#ifdef FEAT_SPELL
10873 char_u *str;
10874 int typeerr = FALSE;
10875 int maxcount;
10876 garray_T ga;
10877 int i;
10878 listitem_T *li;
10879 int need_capital = FALSE;
10880#endif
10881
10882 if (rettv_list_alloc(rettv) == FAIL)
10883 return;
10884
10885#ifdef FEAT_SPELL
10886 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
10887 {
10888 str = get_tv_string(&argvars[0]);
10889 if (argvars[1].v_type != VAR_UNKNOWN)
10890 {
10891 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
10892 if (maxcount <= 0)
10893 return;
10894 if (argvars[2].v_type != VAR_UNKNOWN)
10895 {
10896 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
10897 if (typeerr)
10898 return;
10899 }
10900 }
10901 else
10902 maxcount = 25;
10903
10904 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
10905
10906 for (i = 0; i < ga.ga_len; ++i)
10907 {
10908 str = ((char_u **)ga.ga_data)[i];
10909
10910 li = listitem_alloc();
10911 if (li == NULL)
10912 vim_free(str);
10913 else
10914 {
10915 li->li_tv.v_type = VAR_STRING;
10916 li->li_tv.v_lock = 0;
10917 li->li_tv.vval.v_string = str;
10918 list_append(rettv->vval.v_list, li);
10919 }
10920 }
10921 ga_clear(&ga);
10922 }
10923#endif
10924}
10925
10926 static void
10927f_split(typval_T *argvars, typval_T *rettv)
10928{
10929 char_u *str;
10930 char_u *end;
10931 char_u *pat = NULL;
10932 regmatch_T regmatch;
10933 char_u patbuf[NUMBUFLEN];
10934 char_u *save_cpo;
10935 int match;
10936 colnr_T col = 0;
10937 int keepempty = FALSE;
10938 int typeerr = FALSE;
10939
10940 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10941 save_cpo = p_cpo;
10942 p_cpo = (char_u *)"";
10943
10944 str = get_tv_string(&argvars[0]);
10945 if (argvars[1].v_type != VAR_UNKNOWN)
10946 {
10947 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
10948 if (pat == NULL)
10949 typeerr = TRUE;
10950 if (argvars[2].v_type != VAR_UNKNOWN)
10951 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
10952 }
10953 if (pat == NULL || *pat == NUL)
10954 pat = (char_u *)"[\\x01- ]\\+";
10955
10956 if (rettv_list_alloc(rettv) == FAIL)
10957 return;
10958 if (typeerr)
10959 return;
10960
10961 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10962 if (regmatch.regprog != NULL)
10963 {
10964 regmatch.rm_ic = FALSE;
10965 while (*str != NUL || keepempty)
10966 {
10967 if (*str == NUL)
10968 match = FALSE; /* empty item at the end */
10969 else
10970 match = vim_regexec_nl(&regmatch, str, col);
10971 if (match)
10972 end = regmatch.startp[0];
10973 else
10974 end = str + STRLEN(str);
10975 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
10976 && *str != NUL && match && end < regmatch.endp[0]))
10977 {
10978 if (list_append_string(rettv->vval.v_list, str,
10979 (int)(end - str)) == FAIL)
10980 break;
10981 }
10982 if (!match)
10983 break;
10984 /* Advance to just after the match. */
10985 if (regmatch.endp[0] > str)
10986 col = 0;
10987 else
10988 {
10989 /* Don't get stuck at the same match. */
10990#ifdef FEAT_MBYTE
10991 col = (*mb_ptr2len)(regmatch.endp[0]);
10992#else
10993 col = 1;
10994#endif
10995 }
10996 str = regmatch.endp[0];
10997 }
10998
10999 vim_regfree(regmatch.regprog);
11000 }
11001
11002 p_cpo = save_cpo;
11003}
11004
11005#ifdef FEAT_FLOAT
11006/*
11007 * "sqrt()" function
11008 */
11009 static void
11010f_sqrt(typval_T *argvars, typval_T *rettv)
11011{
11012 float_T f = 0.0;
11013
11014 rettv->v_type = VAR_FLOAT;
11015 if (get_float_arg(argvars, &f) == OK)
11016 rettv->vval.v_float = sqrt(f);
11017 else
11018 rettv->vval.v_float = 0.0;
11019}
11020
11021/*
11022 * "str2float()" function
11023 */
11024 static void
11025f_str2float(typval_T *argvars, typval_T *rettv)
11026{
11027 char_u *p = skipwhite(get_tv_string(&argvars[0]));
11028
11029 if (*p == '+')
11030 p = skipwhite(p + 1);
11031 (void)string2float(p, &rettv->vval.v_float);
11032 rettv->v_type = VAR_FLOAT;
11033}
11034#endif
11035
11036/*
11037 * "str2nr()" function
11038 */
11039 static void
11040f_str2nr(typval_T *argvars, typval_T *rettv)
11041{
11042 int base = 10;
11043 char_u *p;
11044 varnumber_T n;
11045 int what;
11046
11047 if (argvars[1].v_type != VAR_UNKNOWN)
11048 {
11049 base = (int)get_tv_number(&argvars[1]);
11050 if (base != 2 && base != 8 && base != 10 && base != 16)
11051 {
11052 EMSG(_(e_invarg));
11053 return;
11054 }
11055 }
11056
11057 p = skipwhite(get_tv_string(&argvars[0]));
11058 if (*p == '+')
11059 p = skipwhite(p + 1);
11060 switch (base)
11061 {
11062 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11063 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11064 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11065 default: what = 0;
11066 }
11067 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
11068 rettv->vval.v_number = n;
11069}
11070
11071#ifdef HAVE_STRFTIME
11072/*
11073 * "strftime({format}[, {time}])" function
11074 */
11075 static void
11076f_strftime(typval_T *argvars, typval_T *rettv)
11077{
11078 char_u result_buf[256];
11079 struct tm *curtime;
11080 time_t seconds;
11081 char_u *p;
11082
11083 rettv->v_type = VAR_STRING;
11084
11085 p = get_tv_string(&argvars[0]);
11086 if (argvars[1].v_type == VAR_UNKNOWN)
11087 seconds = time(NULL);
11088 else
11089 seconds = (time_t)get_tv_number(&argvars[1]);
11090 curtime = localtime(&seconds);
11091 /* MSVC returns NULL for an invalid value of seconds. */
11092 if (curtime == NULL)
11093 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11094 else
11095 {
11096# ifdef FEAT_MBYTE
11097 vimconv_T conv;
11098 char_u *enc;
11099
11100 conv.vc_type = CONV_NONE;
11101 enc = enc_locale();
11102 convert_setup(&conv, p_enc, enc);
11103 if (conv.vc_type != CONV_NONE)
11104 p = string_convert(&conv, p, NULL);
11105# endif
11106 if (p != NULL)
11107 (void)strftime((char *)result_buf, sizeof(result_buf),
11108 (char *)p, curtime);
11109 else
11110 result_buf[0] = NUL;
11111
11112# ifdef FEAT_MBYTE
11113 if (conv.vc_type != CONV_NONE)
11114 vim_free(p);
11115 convert_setup(&conv, enc, p_enc);
11116 if (conv.vc_type != CONV_NONE)
11117 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11118 else
11119# endif
11120 rettv->vval.v_string = vim_strsave(result_buf);
11121
11122# ifdef FEAT_MBYTE
11123 /* Release conversion descriptors */
11124 convert_setup(&conv, NULL, NULL);
11125 vim_free(enc);
11126# endif
11127 }
11128}
11129#endif
11130
11131/*
11132 * "strgetchar()" function
11133 */
11134 static void
11135f_strgetchar(typval_T *argvars, typval_T *rettv)
11136{
11137 char_u *str;
11138 int len;
11139 int error = FALSE;
11140 int charidx;
11141
11142 rettv->vval.v_number = -1;
11143 str = get_tv_string_chk(&argvars[0]);
11144 if (str == NULL)
11145 return;
11146 len = (int)STRLEN(str);
11147 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11148 if (error)
11149 return;
11150#ifdef FEAT_MBYTE
11151 {
11152 int byteidx = 0;
11153
11154 while (charidx >= 0 && byteidx < len)
11155 {
11156 if (charidx == 0)
11157 {
11158 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11159 break;
11160 }
11161 --charidx;
11162 byteidx += mb_cptr2len(str + byteidx);
11163 }
11164 }
11165#else
11166 if (charidx < len)
11167 rettv->vval.v_number = str[charidx];
11168#endif
11169}
11170
11171/*
11172 * "stridx()" function
11173 */
11174 static void
11175f_stridx(typval_T *argvars, typval_T *rettv)
11176{
11177 char_u buf[NUMBUFLEN];
11178 char_u *needle;
11179 char_u *haystack;
11180 char_u *save_haystack;
11181 char_u *pos;
11182 int start_idx;
11183
11184 needle = get_tv_string_chk(&argvars[1]);
11185 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11186 rettv->vval.v_number = -1;
11187 if (needle == NULL || haystack == NULL)
11188 return; /* type error; errmsg already given */
11189
11190 if (argvars[2].v_type != VAR_UNKNOWN)
11191 {
11192 int error = FALSE;
11193
11194 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11195 if (error || start_idx >= (int)STRLEN(haystack))
11196 return;
11197 if (start_idx >= 0)
11198 haystack += start_idx;
11199 }
11200
11201 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11202 if (pos != NULL)
11203 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11204}
11205
11206/*
11207 * "string()" function
11208 */
11209 static void
11210f_string(typval_T *argvars, typval_T *rettv)
11211{
11212 char_u *tofree;
11213 char_u numbuf[NUMBUFLEN];
11214
11215 rettv->v_type = VAR_STRING;
11216 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11217 get_copyID());
11218 /* Make a copy if we have a value but it's not in allocated memory. */
11219 if (rettv->vval.v_string != NULL && tofree == NULL)
11220 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11221}
11222
11223/*
11224 * "strlen()" function
11225 */
11226 static void
11227f_strlen(typval_T *argvars, typval_T *rettv)
11228{
11229 rettv->vval.v_number = (varnumber_T)(STRLEN(
11230 get_tv_string(&argvars[0])));
11231}
11232
11233/*
11234 * "strchars()" function
11235 */
11236 static void
11237f_strchars(typval_T *argvars, typval_T *rettv)
11238{
11239 char_u *s = get_tv_string(&argvars[0]);
11240 int skipcc = 0;
11241#ifdef FEAT_MBYTE
11242 varnumber_T len = 0;
11243 int (*func_mb_ptr2char_adv)(char_u **pp);
11244#endif
11245
11246 if (argvars[1].v_type != VAR_UNKNOWN)
11247 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11248 if (skipcc < 0 || skipcc > 1)
11249 EMSG(_(e_invarg));
11250 else
11251 {
11252#ifdef FEAT_MBYTE
11253 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11254 while (*s != NUL)
11255 {
11256 func_mb_ptr2char_adv(&s);
11257 ++len;
11258 }
11259 rettv->vval.v_number = len;
11260#else
11261 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11262#endif
11263 }
11264}
11265
11266/*
11267 * "strdisplaywidth()" function
11268 */
11269 static void
11270f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11271{
11272 char_u *s = get_tv_string(&argvars[0]);
11273 int col = 0;
11274
11275 if (argvars[1].v_type != VAR_UNKNOWN)
11276 col = (int)get_tv_number(&argvars[1]);
11277
11278 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11279}
11280
11281/*
11282 * "strwidth()" function
11283 */
11284 static void
11285f_strwidth(typval_T *argvars, typval_T *rettv)
11286{
11287 char_u *s = get_tv_string(&argvars[0]);
11288
11289 rettv->vval.v_number = (varnumber_T)(
11290#ifdef FEAT_MBYTE
11291 mb_string2cells(s, -1)
11292#else
11293 STRLEN(s)
11294#endif
11295 );
11296}
11297
11298/*
11299 * "strcharpart()" function
11300 */
11301 static void
11302f_strcharpart(typval_T *argvars, typval_T *rettv)
11303{
11304#ifdef FEAT_MBYTE
11305 char_u *p;
11306 int nchar;
11307 int nbyte = 0;
11308 int charlen;
11309 int len = 0;
11310 int slen;
11311 int error = FALSE;
11312
11313 p = get_tv_string(&argvars[0]);
11314 slen = (int)STRLEN(p);
11315
11316 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11317 if (!error)
11318 {
11319 if (nchar > 0)
11320 while (nchar > 0 && nbyte < slen)
11321 {
11322 nbyte += mb_cptr2len(p + nbyte);
11323 --nchar;
11324 }
11325 else
11326 nbyte = nchar;
11327 if (argvars[2].v_type != VAR_UNKNOWN)
11328 {
11329 charlen = (int)get_tv_number(&argvars[2]);
11330 while (charlen > 0 && nbyte + len < slen)
11331 {
11332 int off = nbyte + len;
11333
11334 if (off < 0)
11335 len += 1;
11336 else
11337 len += mb_cptr2len(p + off);
11338 --charlen;
11339 }
11340 }
11341 else
11342 len = slen - nbyte; /* default: all bytes that are available. */
11343 }
11344
11345 /*
11346 * Only return the overlap between the specified part and the actual
11347 * string.
11348 */
11349 if (nbyte < 0)
11350 {
11351 len += nbyte;
11352 nbyte = 0;
11353 }
11354 else if (nbyte > slen)
11355 nbyte = slen;
11356 if (len < 0)
11357 len = 0;
11358 else if (nbyte + len > slen)
11359 len = slen - nbyte;
11360
11361 rettv->v_type = VAR_STRING;
11362 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11363#else
11364 f_strpart(argvars, rettv);
11365#endif
11366}
11367
11368/*
11369 * "strpart()" function
11370 */
11371 static void
11372f_strpart(typval_T *argvars, typval_T *rettv)
11373{
11374 char_u *p;
11375 int n;
11376 int len;
11377 int slen;
11378 int error = FALSE;
11379
11380 p = get_tv_string(&argvars[0]);
11381 slen = (int)STRLEN(p);
11382
11383 n = (int)get_tv_number_chk(&argvars[1], &error);
11384 if (error)
11385 len = 0;
11386 else if (argvars[2].v_type != VAR_UNKNOWN)
11387 len = (int)get_tv_number(&argvars[2]);
11388 else
11389 len = slen - n; /* default len: all bytes that are available. */
11390
11391 /*
11392 * Only return the overlap between the specified part and the actual
11393 * string.
11394 */
11395 if (n < 0)
11396 {
11397 len += n;
11398 n = 0;
11399 }
11400 else if (n > slen)
11401 n = slen;
11402 if (len < 0)
11403 len = 0;
11404 else if (n + len > slen)
11405 len = slen - n;
11406
11407 rettv->v_type = VAR_STRING;
11408 rettv->vval.v_string = vim_strnsave(p + n, len);
11409}
11410
11411/*
11412 * "strridx()" function
11413 */
11414 static void
11415f_strridx(typval_T *argvars, typval_T *rettv)
11416{
11417 char_u buf[NUMBUFLEN];
11418 char_u *needle;
11419 char_u *haystack;
11420 char_u *rest;
11421 char_u *lastmatch = NULL;
11422 int haystack_len, end_idx;
11423
11424 needle = get_tv_string_chk(&argvars[1]);
11425 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11426
11427 rettv->vval.v_number = -1;
11428 if (needle == NULL || haystack == NULL)
11429 return; /* type error; errmsg already given */
11430
11431 haystack_len = (int)STRLEN(haystack);
11432 if (argvars[2].v_type != VAR_UNKNOWN)
11433 {
11434 /* Third argument: upper limit for index */
11435 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11436 if (end_idx < 0)
11437 return; /* can never find a match */
11438 }
11439 else
11440 end_idx = haystack_len;
11441
11442 if (*needle == NUL)
11443 {
11444 /* Empty string matches past the end. */
11445 lastmatch = haystack + end_idx;
11446 }
11447 else
11448 {
11449 for (rest = haystack; *rest != '\0'; ++rest)
11450 {
11451 rest = (char_u *)strstr((char *)rest, (char *)needle);
11452 if (rest == NULL || rest > haystack + end_idx)
11453 break;
11454 lastmatch = rest;
11455 }
11456 }
11457
11458 if (lastmatch == NULL)
11459 rettv->vval.v_number = -1;
11460 else
11461 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11462}
11463
11464/*
11465 * "strtrans()" function
11466 */
11467 static void
11468f_strtrans(typval_T *argvars, typval_T *rettv)
11469{
11470 rettv->v_type = VAR_STRING;
11471 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11472}
11473
11474/*
11475 * "submatch()" function
11476 */
11477 static void
11478f_submatch(typval_T *argvars, typval_T *rettv)
11479{
11480 int error = FALSE;
11481 int no;
11482 int retList = 0;
11483
11484 no = (int)get_tv_number_chk(&argvars[0], &error);
11485 if (error)
11486 return;
11487 error = FALSE;
11488 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 */