blob: 379bc634fc84a7a696b77c3da92fe67a5ba2bfe9 [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 Moolenaard823fa92016-08-12 16:29:27 +02004702static void get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv);
4703
4704 static void
4705get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4706{
4707#ifdef FEAT_QUICKFIX
4708 if (what_arg->v_type == VAR_UNKNOWN)
4709 {
4710 if (rettv_list_alloc(rettv) == OK)
4711 if (is_qf || wp != NULL)
4712 (void)get_errorlist(wp, -1, rettv->vval.v_list);
4713 }
4714 else
4715 {
4716 if (rettv_dict_alloc(rettv) == OK)
4717 if (is_qf || (wp != NULL))
4718 {
4719 if (what_arg->v_type == VAR_DICT)
4720 {
4721 dict_T *d = what_arg->vval.v_dict;
4722
4723 if (d != NULL)
4724 get_errorlist_properties(wp, d, rettv->vval.v_dict);
4725 }
4726 else
4727 EMSG(_(e_dictreq));
4728 }
4729 }
4730#endif
4731}
4732
4733/*
4734 * "getloclist()" function
4735 */
4736 static void
4737f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4738{
4739#ifdef FEAT_QUICKFIX
4740 win_T *wp;
4741
4742 wp = find_win_by_nr(&argvars[0], NULL);
4743 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4744#endif
4745}
4746
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004747/*
4748 * "getmatches()" function
4749 */
4750 static void
4751f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4752{
4753#ifdef FEAT_SEARCH_EXTRA
4754 dict_T *dict;
4755 matchitem_T *cur = curwin->w_match_head;
4756 int i;
4757
4758 if (rettv_list_alloc(rettv) == OK)
4759 {
4760 while (cur != NULL)
4761 {
4762 dict = dict_alloc();
4763 if (dict == NULL)
4764 return;
4765 if (cur->match.regprog == NULL)
4766 {
4767 /* match added with matchaddpos() */
4768 for (i = 0; i < MAXPOSMATCH; ++i)
4769 {
4770 llpos_T *llpos;
4771 char buf[6];
4772 list_T *l;
4773
4774 llpos = &cur->pos.pos[i];
4775 if (llpos->lnum == 0)
4776 break;
4777 l = list_alloc();
4778 if (l == NULL)
4779 break;
4780 list_append_number(l, (varnumber_T)llpos->lnum);
4781 if (llpos->col > 0)
4782 {
4783 list_append_number(l, (varnumber_T)llpos->col);
4784 list_append_number(l, (varnumber_T)llpos->len);
4785 }
4786 sprintf(buf, "pos%d", i + 1);
4787 dict_add_list(dict, buf, l);
4788 }
4789 }
4790 else
4791 {
4792 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4793 }
4794 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4795 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4796 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4797# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4798 if (cur->conceal_char)
4799 {
4800 char_u buf[MB_MAXBYTES + 1];
4801
4802 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
4803 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
4804 }
4805# endif
4806 list_append_dict(rettv->vval.v_list, dict);
4807 cur = cur->next;
4808 }
4809 }
4810#endif
4811}
4812
4813/*
4814 * "getpid()" function
4815 */
4816 static void
4817f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4818{
4819 rettv->vval.v_number = mch_get_pid();
4820}
4821
4822 static void
4823getpos_both(
4824 typval_T *argvars,
4825 typval_T *rettv,
4826 int getcurpos)
4827{
4828 pos_T *fp;
4829 list_T *l;
4830 int fnum = -1;
4831
4832 if (rettv_list_alloc(rettv) == OK)
4833 {
4834 l = rettv->vval.v_list;
4835 if (getcurpos)
4836 fp = &curwin->w_cursor;
4837 else
4838 fp = var2fpos(&argvars[0], TRUE, &fnum);
4839 if (fnum != -1)
4840 list_append_number(l, (varnumber_T)fnum);
4841 else
4842 list_append_number(l, (varnumber_T)0);
4843 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4844 : (varnumber_T)0);
4845 list_append_number(l, (fp != NULL)
4846 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4847 : (varnumber_T)0);
4848 list_append_number(l,
4849#ifdef FEAT_VIRTUALEDIT
4850 (fp != NULL) ? (varnumber_T)fp->coladd :
4851#endif
4852 (varnumber_T)0);
4853 if (getcurpos)
4854 {
4855 update_curswant();
4856 list_append_number(l, curwin->w_curswant == MAXCOL ?
4857 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
4858 }
4859 }
4860 else
4861 rettv->vval.v_number = FALSE;
4862}
4863
4864
4865/*
4866 * "getcurpos()" function
4867 */
4868 static void
4869f_getcurpos(typval_T *argvars, typval_T *rettv)
4870{
4871 getpos_both(argvars, rettv, TRUE);
4872}
4873
4874/*
4875 * "getpos(string)" function
4876 */
4877 static void
4878f_getpos(typval_T *argvars, typval_T *rettv)
4879{
4880 getpos_both(argvars, rettv, FALSE);
4881}
4882
4883/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004884 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004885 */
4886 static void
4887f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4888{
4889#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004890 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004891#endif
4892}
4893
4894/*
4895 * "getreg()" function
4896 */
4897 static void
4898f_getreg(typval_T *argvars, typval_T *rettv)
4899{
4900 char_u *strregname;
4901 int regname;
4902 int arg2 = FALSE;
4903 int return_list = FALSE;
4904 int error = FALSE;
4905
4906 if (argvars[0].v_type != VAR_UNKNOWN)
4907 {
4908 strregname = get_tv_string_chk(&argvars[0]);
4909 error = strregname == NULL;
4910 if (argvars[1].v_type != VAR_UNKNOWN)
4911 {
4912 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
4913 if (!error && argvars[2].v_type != VAR_UNKNOWN)
4914 return_list = (int)get_tv_number_chk(&argvars[2], &error);
4915 }
4916 }
4917 else
4918 strregname = get_vim_var_str(VV_REG);
4919
4920 if (error)
4921 return;
4922
4923 regname = (strregname == NULL ? '"' : *strregname);
4924 if (regname == 0)
4925 regname = '"';
4926
4927 if (return_list)
4928 {
4929 rettv->v_type = VAR_LIST;
4930 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4931 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4932 if (rettv->vval.v_list == NULL)
4933 (void)rettv_list_alloc(rettv);
4934 else
4935 ++rettv->vval.v_list->lv_refcount;
4936 }
4937 else
4938 {
4939 rettv->v_type = VAR_STRING;
4940 rettv->vval.v_string = get_reg_contents(regname,
4941 arg2 ? GREG_EXPR_SRC : 0);
4942 }
4943}
4944
4945/*
4946 * "getregtype()" function
4947 */
4948 static void
4949f_getregtype(typval_T *argvars, typval_T *rettv)
4950{
4951 char_u *strregname;
4952 int regname;
4953 char_u buf[NUMBUFLEN + 2];
4954 long reglen = 0;
4955
4956 if (argvars[0].v_type != VAR_UNKNOWN)
4957 {
4958 strregname = get_tv_string_chk(&argvars[0]);
4959 if (strregname == NULL) /* type error; errmsg already given */
4960 {
4961 rettv->v_type = VAR_STRING;
4962 rettv->vval.v_string = NULL;
4963 return;
4964 }
4965 }
4966 else
4967 /* Default to v:register */
4968 strregname = get_vim_var_str(VV_REG);
4969
4970 regname = (strregname == NULL ? '"' : *strregname);
4971 if (regname == 0)
4972 regname = '"';
4973
4974 buf[0] = NUL;
4975 buf[1] = NUL;
4976 switch (get_reg_type(regname, &reglen))
4977 {
4978 case MLINE: buf[0] = 'V'; break;
4979 case MCHAR: buf[0] = 'v'; break;
4980 case MBLOCK:
4981 buf[0] = Ctrl_V;
4982 sprintf((char *)buf + 1, "%ld", reglen + 1);
4983 break;
4984 }
4985 rettv->v_type = VAR_STRING;
4986 rettv->vval.v_string = vim_strsave(buf);
4987}
4988
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004989#ifdef FEAT_WINDOWS
4990/*
4991 * Returns information (variables, options, etc.) about a tab page
4992 * as a dictionary.
4993 */
4994 static dict_T *
4995get_tabpage_info(tabpage_T *tp, int tp_idx)
4996{
4997 win_T *wp;
4998 dict_T *dict;
4999 dict_T *vars;
5000 list_T *l;
5001
5002 dict = dict_alloc();
5003 if (dict == NULL)
5004 return NULL;
5005
5006 dict_add_nr_str(dict, "nr", tp_idx, NULL);
5007
5008 l = list_alloc();
5009 if (l != NULL)
5010 {
5011 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5012 wp; wp = wp->w_next)
5013 list_append_number(l, (varnumber_T)wp->w_id);
5014 dict_add_list(dict, "windows", l);
5015 }
5016
5017 /* Copy tabpage variables */
5018 vars = dict_copy(tp->tp_vars, TRUE, 0);
5019 if (vars != NULL)
5020 dict_add_dict(dict, "variables", vars);
5021
5022 return dict;
5023}
5024#endif
5025
5026/*
5027 * "gettabinfo()" function
5028 */
5029 static void
5030f_gettabinfo(typval_T *argvars, typval_T *rettv)
5031{
5032#ifdef FEAT_WINDOWS
5033 tabpage_T *tp, *tparg = NULL;
5034 dict_T *d;
5035 int tpnr = 1;
5036
5037 if (rettv_list_alloc(rettv) != OK)
5038 return;
5039
5040 if (argvars[0].v_type != VAR_UNKNOWN)
5041 {
5042 /* Information about one tab page */
5043 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5044 if (tparg == NULL)
5045 return;
5046 }
5047
5048 /* Get information about a specific tab page or all tab pages */
5049 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, tpnr++)
5050 {
5051 if (tparg != NULL && tp != tparg)
5052 continue;
5053 d = get_tabpage_info(tp, tpnr);
5054 if (d != NULL)
5055 list_append_dict(rettv->vval.v_list, d);
5056 if (tparg != NULL)
5057 return;
5058 }
5059#endif
5060}
5061
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005062/*
5063 * "gettabvar()" function
5064 */
5065 static void
5066f_gettabvar(typval_T *argvars, typval_T *rettv)
5067{
5068 win_T *oldcurwin;
5069 tabpage_T *tp, *oldtabpage;
5070 dictitem_T *v;
5071 char_u *varname;
5072 int done = FALSE;
5073
5074 rettv->v_type = VAR_STRING;
5075 rettv->vval.v_string = NULL;
5076
5077 varname = get_tv_string_chk(&argvars[1]);
5078 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5079 if (tp != NULL && varname != NULL)
5080 {
5081 /* Set tp to be our tabpage, temporarily. Also set the window to the
5082 * first window in the tabpage, otherwise the window is not valid. */
5083 if (switch_win(&oldcurwin, &oldtabpage,
5084 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
5085 == OK)
5086 {
5087 /* look up the variable */
5088 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5089 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5090 if (v != NULL)
5091 {
5092 copy_tv(&v->di_tv, rettv);
5093 done = TRUE;
5094 }
5095 }
5096
5097 /* restore previous notion of curwin */
5098 restore_win(oldcurwin, oldtabpage, TRUE);
5099 }
5100
5101 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5102 copy_tv(&argvars[2], rettv);
5103}
5104
5105/*
5106 * "gettabwinvar()" function
5107 */
5108 static void
5109f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5110{
5111 getwinvar(argvars, rettv, 1);
5112}
5113
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005114#ifdef FEAT_WINDOWS
5115/*
5116 * Returns information about a window as a dictionary.
5117 */
5118 static dict_T *
5119get_win_info(win_T *wp, short tpnr, short winnr)
5120{
5121 dict_T *dict;
5122 dict_T *vars;
5123 dict_T *opts;
5124
5125 dict = dict_alloc();
5126 if (dict == NULL)
5127 return NULL;
5128
5129 dict_add_nr_str(dict, "tpnr", tpnr, NULL);
5130 dict_add_nr_str(dict, "nr", winnr, NULL);
5131 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5132 dict_add_nr_str(dict, "height", wp->w_height, NULL);
5133 dict_add_nr_str(dict, "width", wp->w_width, NULL);
5134 dict_add_nr_str(dict, "bufnum", wp->w_buffer->b_fnum, NULL);
5135
5136 /* Copy window variables */
5137 vars = dict_copy(wp->w_vars, TRUE, 0);
5138 if (vars != NULL)
5139 dict_add_dict(dict, "variables", vars);
5140
5141 /* Copy window options */
5142 opts = get_winbuf_options(FALSE);
5143 if (opts != NULL)
5144 dict_add_dict(dict, "options", opts);
5145
5146 return dict;
5147}
5148#endif
5149
5150/*
5151 * "getwininfo()" function
5152 */
5153 static void
5154f_getwininfo(typval_T *argvars, typval_T *rettv)
5155{
5156#ifdef FEAT_WINDOWS
5157 tabpage_T *tp;
5158 win_T *wp = NULL, *wparg = NULL;
5159 dict_T *d;
5160 short tabnr, winnr;
5161#endif
5162
5163 if (rettv_list_alloc(rettv) != OK)
5164 return;
5165
5166#ifdef FEAT_WINDOWS
5167 if (argvars[0].v_type != VAR_UNKNOWN)
5168 {
5169 wparg = win_id2wp(argvars);
5170 if (wparg == NULL)
5171 return;
5172 }
5173
5174 /* Collect information about either all the windows across all the tab
5175 * pages or one particular window.
5176 */
5177 tabnr = 1;
5178 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, tabnr++)
5179 {
5180 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5181 winnr = 1;
5182 for (; wp; wp = wp->w_next, winnr++)
5183 {
5184 if (wparg != NULL && wp != wparg)
5185 continue;
5186 d = get_win_info(wp, tabnr, winnr);
5187 if (d != NULL)
5188 list_append_dict(rettv->vval.v_list, d);
5189 if (wparg != NULL)
5190 /* found information about a specific window */
5191 return;
5192 }
5193 }
5194#endif
5195}
5196
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005197/*
5198 * "getwinposx()" function
5199 */
5200 static void
5201f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5202{
5203 rettv->vval.v_number = -1;
5204#ifdef FEAT_GUI
5205 if (gui.in_use)
5206 {
5207 int x, y;
5208
5209 if (gui_mch_get_winpos(&x, &y) == OK)
5210 rettv->vval.v_number = x;
5211 }
5212#endif
5213}
5214
5215/*
5216 * "win_findbuf()" function
5217 */
5218 static void
5219f_win_findbuf(typval_T *argvars, typval_T *rettv)
5220{
5221 if (rettv_list_alloc(rettv) != FAIL)
5222 win_findbuf(argvars, rettv->vval.v_list);
5223}
5224
5225/*
5226 * "win_getid()" function
5227 */
5228 static void
5229f_win_getid(typval_T *argvars, typval_T *rettv)
5230{
5231 rettv->vval.v_number = win_getid(argvars);
5232}
5233
5234/*
5235 * "win_gotoid()" function
5236 */
5237 static void
5238f_win_gotoid(typval_T *argvars, typval_T *rettv)
5239{
5240 rettv->vval.v_number = win_gotoid(argvars);
5241}
5242
5243/*
5244 * "win_id2tabwin()" function
5245 */
5246 static void
5247f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5248{
5249 if (rettv_list_alloc(rettv) != FAIL)
5250 win_id2tabwin(argvars, rettv->vval.v_list);
5251}
5252
5253/*
5254 * "win_id2win()" function
5255 */
5256 static void
5257f_win_id2win(typval_T *argvars, typval_T *rettv)
5258{
5259 rettv->vval.v_number = win_id2win(argvars);
5260}
5261
5262/*
5263 * "getwinposy()" function
5264 */
5265 static void
5266f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5267{
5268 rettv->vval.v_number = -1;
5269#ifdef FEAT_GUI
5270 if (gui.in_use)
5271 {
5272 int x, y;
5273
5274 if (gui_mch_get_winpos(&x, &y) == OK)
5275 rettv->vval.v_number = y;
5276 }
5277#endif
5278}
5279
5280/*
5281 * "getwinvar()" function
5282 */
5283 static void
5284f_getwinvar(typval_T *argvars, typval_T *rettv)
5285{
5286 getwinvar(argvars, rettv, 0);
5287}
5288
5289/*
5290 * "glob()" function
5291 */
5292 static void
5293f_glob(typval_T *argvars, typval_T *rettv)
5294{
5295 int options = WILD_SILENT|WILD_USE_NL;
5296 expand_T xpc;
5297 int error = FALSE;
5298
5299 /* When the optional second argument is non-zero, don't remove matches
5300 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5301 rettv->v_type = VAR_STRING;
5302 if (argvars[1].v_type != VAR_UNKNOWN)
5303 {
5304 if (get_tv_number_chk(&argvars[1], &error))
5305 options |= WILD_KEEP_ALL;
5306 if (argvars[2].v_type != VAR_UNKNOWN)
5307 {
5308 if (get_tv_number_chk(&argvars[2], &error))
5309 {
5310 rettv->v_type = VAR_LIST;
5311 rettv->vval.v_list = NULL;
5312 }
5313 if (argvars[3].v_type != VAR_UNKNOWN
5314 && get_tv_number_chk(&argvars[3], &error))
5315 options |= WILD_ALLLINKS;
5316 }
5317 }
5318 if (!error)
5319 {
5320 ExpandInit(&xpc);
5321 xpc.xp_context = EXPAND_FILES;
5322 if (p_wic)
5323 options += WILD_ICASE;
5324 if (rettv->v_type == VAR_STRING)
5325 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5326 NULL, options, WILD_ALL);
5327 else if (rettv_list_alloc(rettv) != FAIL)
5328 {
5329 int i;
5330
5331 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5332 NULL, options, WILD_ALL_KEEP);
5333 for (i = 0; i < xpc.xp_numfiles; i++)
5334 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5335
5336 ExpandCleanup(&xpc);
5337 }
5338 }
5339 else
5340 rettv->vval.v_string = NULL;
5341}
5342
5343/*
5344 * "globpath()" function
5345 */
5346 static void
5347f_globpath(typval_T *argvars, typval_T *rettv)
5348{
5349 int flags = 0;
5350 char_u buf1[NUMBUFLEN];
5351 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5352 int error = FALSE;
5353 garray_T ga;
5354 int i;
5355
5356 /* When the optional second argument is non-zero, don't remove matches
5357 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5358 rettv->v_type = VAR_STRING;
5359 if (argvars[2].v_type != VAR_UNKNOWN)
5360 {
5361 if (get_tv_number_chk(&argvars[2], &error))
5362 flags |= WILD_KEEP_ALL;
5363 if (argvars[3].v_type != VAR_UNKNOWN)
5364 {
5365 if (get_tv_number_chk(&argvars[3], &error))
5366 {
5367 rettv->v_type = VAR_LIST;
5368 rettv->vval.v_list = NULL;
5369 }
5370 if (argvars[4].v_type != VAR_UNKNOWN
5371 && get_tv_number_chk(&argvars[4], &error))
5372 flags |= WILD_ALLLINKS;
5373 }
5374 }
5375 if (file != NULL && !error)
5376 {
5377 ga_init2(&ga, (int)sizeof(char_u *), 10);
5378 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5379 if (rettv->v_type == VAR_STRING)
5380 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5381 else if (rettv_list_alloc(rettv) != FAIL)
5382 for (i = 0; i < ga.ga_len; ++i)
5383 list_append_string(rettv->vval.v_list,
5384 ((char_u **)(ga.ga_data))[i], -1);
5385 ga_clear_strings(&ga);
5386 }
5387 else
5388 rettv->vval.v_string = NULL;
5389}
5390
5391/*
5392 * "glob2regpat()" function
5393 */
5394 static void
5395f_glob2regpat(typval_T *argvars, typval_T *rettv)
5396{
5397 char_u *pat = get_tv_string_chk(&argvars[0]);
5398
5399 rettv->v_type = VAR_STRING;
5400 rettv->vval.v_string = (pat == NULL)
5401 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5402}
5403
5404/* for VIM_VERSION_ defines */
5405#include "version.h"
5406
5407/*
5408 * "has()" function
5409 */
5410 static void
5411f_has(typval_T *argvars, typval_T *rettv)
5412{
5413 int i;
5414 char_u *name;
5415 int n = FALSE;
5416 static char *(has_list[]) =
5417 {
5418#ifdef AMIGA
5419 "amiga",
5420# ifdef FEAT_ARP
5421 "arp",
5422# endif
5423#endif
5424#ifdef __BEOS__
5425 "beos",
5426#endif
5427#ifdef MACOS
5428 "mac",
5429#endif
5430#if defined(MACOS_X_UNIX)
5431 "macunix", /* built with 'darwin' enabled */
5432#endif
5433#if defined(__APPLE__) && __APPLE__ == 1
5434 "osx", /* built with or without 'darwin' enabled */
5435#endif
5436#ifdef __QNX__
5437 "qnx",
5438#endif
5439#ifdef UNIX
5440 "unix",
5441#endif
5442#ifdef VMS
5443 "vms",
5444#endif
5445#ifdef WIN32
5446 "win32",
5447#endif
5448#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5449 "win32unix",
5450#endif
5451#if defined(WIN64) || defined(_WIN64)
5452 "win64",
5453#endif
5454#ifdef EBCDIC
5455 "ebcdic",
5456#endif
5457#ifndef CASE_INSENSITIVE_FILENAME
5458 "fname_case",
5459#endif
5460#ifdef HAVE_ACL
5461 "acl",
5462#endif
5463#ifdef FEAT_ARABIC
5464 "arabic",
5465#endif
5466#ifdef FEAT_AUTOCMD
5467 "autocmd",
5468#endif
5469#ifdef FEAT_BEVAL
5470 "balloon_eval",
5471# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5472 "balloon_multiline",
5473# endif
5474#endif
5475#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5476 "builtin_terms",
5477# ifdef ALL_BUILTIN_TCAPS
5478 "all_builtin_terms",
5479# endif
5480#endif
5481#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5482 || defined(FEAT_GUI_W32) \
5483 || defined(FEAT_GUI_MOTIF))
5484 "browsefilter",
5485#endif
5486#ifdef FEAT_BYTEOFF
5487 "byte_offset",
5488#endif
5489#ifdef FEAT_JOB_CHANNEL
5490 "channel",
5491#endif
5492#ifdef FEAT_CINDENT
5493 "cindent",
5494#endif
5495#ifdef FEAT_CLIENTSERVER
5496 "clientserver",
5497#endif
5498#ifdef FEAT_CLIPBOARD
5499 "clipboard",
5500#endif
5501#ifdef FEAT_CMDL_COMPL
5502 "cmdline_compl",
5503#endif
5504#ifdef FEAT_CMDHIST
5505 "cmdline_hist",
5506#endif
5507#ifdef FEAT_COMMENTS
5508 "comments",
5509#endif
5510#ifdef FEAT_CONCEAL
5511 "conceal",
5512#endif
5513#ifdef FEAT_CRYPT
5514 "cryptv",
5515 "crypt-blowfish",
5516 "crypt-blowfish2",
5517#endif
5518#ifdef FEAT_CSCOPE
5519 "cscope",
5520#endif
5521#ifdef FEAT_CURSORBIND
5522 "cursorbind",
5523#endif
5524#ifdef CURSOR_SHAPE
5525 "cursorshape",
5526#endif
5527#ifdef DEBUG
5528 "debug",
5529#endif
5530#ifdef FEAT_CON_DIALOG
5531 "dialog_con",
5532#endif
5533#ifdef FEAT_GUI_DIALOG
5534 "dialog_gui",
5535#endif
5536#ifdef FEAT_DIFF
5537 "diff",
5538#endif
5539#ifdef FEAT_DIGRAPHS
5540 "digraphs",
5541#endif
5542#ifdef FEAT_DIRECTX
5543 "directx",
5544#endif
5545#ifdef FEAT_DND
5546 "dnd",
5547#endif
5548#ifdef FEAT_EMACS_TAGS
5549 "emacs_tags",
5550#endif
5551 "eval", /* always present, of course! */
5552 "ex_extra", /* graduated feature */
5553#ifdef FEAT_SEARCH_EXTRA
5554 "extra_search",
5555#endif
5556#ifdef FEAT_FKMAP
5557 "farsi",
5558#endif
5559#ifdef FEAT_SEARCHPATH
5560 "file_in_path",
5561#endif
5562#ifdef FEAT_FILTERPIPE
5563 "filterpipe",
5564#endif
5565#ifdef FEAT_FIND_ID
5566 "find_in_path",
5567#endif
5568#ifdef FEAT_FLOAT
5569 "float",
5570#endif
5571#ifdef FEAT_FOLDING
5572 "folding",
5573#endif
5574#ifdef FEAT_FOOTER
5575 "footer",
5576#endif
5577#if !defined(USE_SYSTEM) && defined(UNIX)
5578 "fork",
5579#endif
5580#ifdef FEAT_GETTEXT
5581 "gettext",
5582#endif
5583#ifdef FEAT_GUI
5584 "gui",
5585#endif
5586#ifdef FEAT_GUI_ATHENA
5587# ifdef FEAT_GUI_NEXTAW
5588 "gui_neXtaw",
5589# else
5590 "gui_athena",
5591# endif
5592#endif
5593#ifdef FEAT_GUI_GTK
5594 "gui_gtk",
5595# ifdef USE_GTK3
5596 "gui_gtk3",
5597# else
5598 "gui_gtk2",
5599# endif
5600#endif
5601#ifdef FEAT_GUI_GNOME
5602 "gui_gnome",
5603#endif
5604#ifdef FEAT_GUI_MAC
5605 "gui_mac",
5606#endif
5607#ifdef FEAT_GUI_MOTIF
5608 "gui_motif",
5609#endif
5610#ifdef FEAT_GUI_PHOTON
5611 "gui_photon",
5612#endif
5613#ifdef FEAT_GUI_W32
5614 "gui_win32",
5615#endif
5616#ifdef FEAT_HANGULIN
5617 "hangul_input",
5618#endif
5619#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5620 "iconv",
5621#endif
5622#ifdef FEAT_INS_EXPAND
5623 "insert_expand",
5624#endif
5625#ifdef FEAT_JOB_CHANNEL
5626 "job",
5627#endif
5628#ifdef FEAT_JUMPLIST
5629 "jumplist",
5630#endif
5631#ifdef FEAT_KEYMAP
5632 "keymap",
5633#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005634 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005635#ifdef FEAT_LANGMAP
5636 "langmap",
5637#endif
5638#ifdef FEAT_LIBCALL
5639 "libcall",
5640#endif
5641#ifdef FEAT_LINEBREAK
5642 "linebreak",
5643#endif
5644#ifdef FEAT_LISP
5645 "lispindent",
5646#endif
5647#ifdef FEAT_LISTCMDS
5648 "listcmds",
5649#endif
5650#ifdef FEAT_LOCALMAP
5651 "localmap",
5652#endif
5653#ifdef FEAT_LUA
5654# ifndef DYNAMIC_LUA
5655 "lua",
5656# endif
5657#endif
5658#ifdef FEAT_MENU
5659 "menu",
5660#endif
5661#ifdef FEAT_SESSION
5662 "mksession",
5663#endif
5664#ifdef FEAT_MODIFY_FNAME
5665 "modify_fname",
5666#endif
5667#ifdef FEAT_MOUSE
5668 "mouse",
5669#endif
5670#ifdef FEAT_MOUSESHAPE
5671 "mouseshape",
5672#endif
5673#if defined(UNIX) || defined(VMS)
5674# ifdef FEAT_MOUSE_DEC
5675 "mouse_dec",
5676# endif
5677# ifdef FEAT_MOUSE_GPM
5678 "mouse_gpm",
5679# endif
5680# ifdef FEAT_MOUSE_JSB
5681 "mouse_jsbterm",
5682# endif
5683# ifdef FEAT_MOUSE_NET
5684 "mouse_netterm",
5685# endif
5686# ifdef FEAT_MOUSE_PTERM
5687 "mouse_pterm",
5688# endif
5689# ifdef FEAT_MOUSE_SGR
5690 "mouse_sgr",
5691# endif
5692# ifdef FEAT_SYSMOUSE
5693 "mouse_sysmouse",
5694# endif
5695# ifdef FEAT_MOUSE_URXVT
5696 "mouse_urxvt",
5697# endif
5698# ifdef FEAT_MOUSE_XTERM
5699 "mouse_xterm",
5700# endif
5701#endif
5702#ifdef FEAT_MBYTE
5703 "multi_byte",
5704#endif
5705#ifdef FEAT_MBYTE_IME
5706 "multi_byte_ime",
5707#endif
5708#ifdef FEAT_MULTI_LANG
5709 "multi_lang",
5710#endif
5711#ifdef FEAT_MZSCHEME
5712#ifndef DYNAMIC_MZSCHEME
5713 "mzscheme",
5714#endif
5715#endif
5716#ifdef FEAT_NUM64
5717 "num64",
5718#endif
5719#ifdef FEAT_OLE
5720 "ole",
5721#endif
5722 "packages",
5723#ifdef FEAT_PATH_EXTRA
5724 "path_extra",
5725#endif
5726#ifdef FEAT_PERL
5727#ifndef DYNAMIC_PERL
5728 "perl",
5729#endif
5730#endif
5731#ifdef FEAT_PERSISTENT_UNDO
5732 "persistent_undo",
5733#endif
5734#ifdef FEAT_PYTHON
5735#ifndef DYNAMIC_PYTHON
5736 "python",
5737#endif
5738#endif
5739#ifdef FEAT_PYTHON3
5740#ifndef DYNAMIC_PYTHON3
5741 "python3",
5742#endif
5743#endif
5744#ifdef FEAT_POSTSCRIPT
5745 "postscript",
5746#endif
5747#ifdef FEAT_PRINTER
5748 "printer",
5749#endif
5750#ifdef FEAT_PROFILE
5751 "profile",
5752#endif
5753#ifdef FEAT_RELTIME
5754 "reltime",
5755#endif
5756#ifdef FEAT_QUICKFIX
5757 "quickfix",
5758#endif
5759#ifdef FEAT_RIGHTLEFT
5760 "rightleft",
5761#endif
5762#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5763 "ruby",
5764#endif
5765#ifdef FEAT_SCROLLBIND
5766 "scrollbind",
5767#endif
5768#ifdef FEAT_CMDL_INFO
5769 "showcmd",
5770 "cmdline_info",
5771#endif
5772#ifdef FEAT_SIGNS
5773 "signs",
5774#endif
5775#ifdef FEAT_SMARTINDENT
5776 "smartindent",
5777#endif
5778#ifdef STARTUPTIME
5779 "startuptime",
5780#endif
5781#ifdef FEAT_STL_OPT
5782 "statusline",
5783#endif
5784#ifdef FEAT_SUN_WORKSHOP
5785 "sun_workshop",
5786#endif
5787#ifdef FEAT_NETBEANS_INTG
5788 "netbeans_intg",
5789#endif
5790#ifdef FEAT_SPELL
5791 "spell",
5792#endif
5793#ifdef FEAT_SYN_HL
5794 "syntax",
5795#endif
5796#if defined(USE_SYSTEM) || !defined(UNIX)
5797 "system",
5798#endif
5799#ifdef FEAT_TAG_BINS
5800 "tag_binary",
5801#endif
5802#ifdef FEAT_TAG_OLDSTATIC
5803 "tag_old_static",
5804#endif
5805#ifdef FEAT_TAG_ANYWHITE
5806 "tag_any_white",
5807#endif
5808#ifdef FEAT_TCL
5809# ifndef DYNAMIC_TCL
5810 "tcl",
5811# endif
5812#endif
5813#ifdef FEAT_TERMGUICOLORS
5814 "termguicolors",
5815#endif
5816#ifdef TERMINFO
5817 "terminfo",
5818#endif
5819#ifdef FEAT_TERMRESPONSE
5820 "termresponse",
5821#endif
5822#ifdef FEAT_TEXTOBJ
5823 "textobjects",
5824#endif
5825#ifdef HAVE_TGETENT
5826 "tgetent",
5827#endif
5828#ifdef FEAT_TIMERS
5829 "timers",
5830#endif
5831#ifdef FEAT_TITLE
5832 "title",
5833#endif
5834#ifdef FEAT_TOOLBAR
5835 "toolbar",
5836#endif
5837#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5838 "unnamedplus",
5839#endif
5840#ifdef FEAT_USR_CMDS
5841 "user-commands", /* was accidentally included in 5.4 */
5842 "user_commands",
5843#endif
5844#ifdef FEAT_VIMINFO
5845 "viminfo",
5846#endif
5847#ifdef FEAT_WINDOWS
5848 "vertsplit",
5849#endif
5850#ifdef FEAT_VIRTUALEDIT
5851 "virtualedit",
5852#endif
5853 "visual",
5854#ifdef FEAT_VISUALEXTRA
5855 "visualextra",
5856#endif
5857#ifdef FEAT_VREPLACE
5858 "vreplace",
5859#endif
5860#ifdef FEAT_WILDIGN
5861 "wildignore",
5862#endif
5863#ifdef FEAT_WILDMENU
5864 "wildmenu",
5865#endif
5866#ifdef FEAT_WINDOWS
5867 "windows",
5868#endif
5869#ifdef FEAT_WAK
5870 "winaltkeys",
5871#endif
5872#ifdef FEAT_WRITEBACKUP
5873 "writebackup",
5874#endif
5875#ifdef FEAT_XIM
5876 "xim",
5877#endif
5878#ifdef FEAT_XFONTSET
5879 "xfontset",
5880#endif
5881#ifdef FEAT_XPM_W32
5882 "xpm",
5883 "xpm_w32", /* for backward compatibility */
5884#else
5885# if defined(HAVE_XPM)
5886 "xpm",
5887# endif
5888#endif
5889#ifdef USE_XSMP
5890 "xsmp",
5891#endif
5892#ifdef USE_XSMP_INTERACT
5893 "xsmp_interact",
5894#endif
5895#ifdef FEAT_XCLIPBOARD
5896 "xterm_clipboard",
5897#endif
5898#ifdef FEAT_XTERM_SAVE
5899 "xterm_save",
5900#endif
5901#if defined(UNIX) && defined(FEAT_X11)
5902 "X11",
5903#endif
5904 NULL
5905 };
5906
5907 name = get_tv_string(&argvars[0]);
5908 for (i = 0; has_list[i] != NULL; ++i)
5909 if (STRICMP(name, has_list[i]) == 0)
5910 {
5911 n = TRUE;
5912 break;
5913 }
5914
5915 if (n == FALSE)
5916 {
5917 if (STRNICMP(name, "patch", 5) == 0)
5918 {
5919 if (name[5] == '-'
5920 && STRLEN(name) >= 11
5921 && vim_isdigit(name[6])
5922 && vim_isdigit(name[8])
5923 && vim_isdigit(name[10]))
5924 {
5925 int major = atoi((char *)name + 6);
5926 int minor = atoi((char *)name + 8);
5927
5928 /* Expect "patch-9.9.01234". */
5929 n = (major < VIM_VERSION_MAJOR
5930 || (major == VIM_VERSION_MAJOR
5931 && (minor < VIM_VERSION_MINOR
5932 || (minor == VIM_VERSION_MINOR
5933 && has_patch(atoi((char *)name + 10))))));
5934 }
5935 else
5936 n = has_patch(atoi((char *)name + 5));
5937 }
5938 else if (STRICMP(name, "vim_starting") == 0)
5939 n = (starting != 0);
5940#ifdef FEAT_MBYTE
5941 else if (STRICMP(name, "multi_byte_encoding") == 0)
5942 n = has_mbyte;
5943#endif
5944#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
5945 else if (STRICMP(name, "balloon_multiline") == 0)
5946 n = multiline_balloon_available();
5947#endif
5948#ifdef DYNAMIC_TCL
5949 else if (STRICMP(name, "tcl") == 0)
5950 n = tcl_enabled(FALSE);
5951#endif
5952#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5953 else if (STRICMP(name, "iconv") == 0)
5954 n = iconv_enabled(FALSE);
5955#endif
5956#ifdef DYNAMIC_LUA
5957 else if (STRICMP(name, "lua") == 0)
5958 n = lua_enabled(FALSE);
5959#endif
5960#ifdef DYNAMIC_MZSCHEME
5961 else if (STRICMP(name, "mzscheme") == 0)
5962 n = mzscheme_enabled(FALSE);
5963#endif
5964#ifdef DYNAMIC_RUBY
5965 else if (STRICMP(name, "ruby") == 0)
5966 n = ruby_enabled(FALSE);
5967#endif
5968#ifdef FEAT_PYTHON
5969#ifdef DYNAMIC_PYTHON
5970 else if (STRICMP(name, "python") == 0)
5971 n = python_enabled(FALSE);
5972#endif
5973#endif
5974#ifdef FEAT_PYTHON3
5975#ifdef DYNAMIC_PYTHON3
5976 else if (STRICMP(name, "python3") == 0)
5977 n = python3_enabled(FALSE);
5978#endif
5979#endif
5980#ifdef DYNAMIC_PERL
5981 else if (STRICMP(name, "perl") == 0)
5982 n = perl_enabled(FALSE);
5983#endif
5984#ifdef FEAT_GUI
5985 else if (STRICMP(name, "gui_running") == 0)
5986 n = (gui.in_use || gui.starting);
5987# ifdef FEAT_GUI_W32
5988 else if (STRICMP(name, "gui_win32s") == 0)
5989 n = gui_is_win32s();
5990# endif
5991# ifdef FEAT_BROWSE
5992 else if (STRICMP(name, "browse") == 0)
5993 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
5994# endif
5995#endif
5996#ifdef FEAT_SYN_HL
5997 else if (STRICMP(name, "syntax_items") == 0)
5998 n = syntax_present(curwin);
5999#endif
6000#if defined(WIN3264)
6001 else if (STRICMP(name, "win95") == 0)
6002 n = mch_windows95();
6003#endif
6004#ifdef FEAT_NETBEANS_INTG
6005 else if (STRICMP(name, "netbeans_enabled") == 0)
6006 n = netbeans_active();
6007#endif
6008 }
6009
6010 rettv->vval.v_number = n;
6011}
6012
6013/*
6014 * "has_key()" function
6015 */
6016 static void
6017f_has_key(typval_T *argvars, typval_T *rettv)
6018{
6019 if (argvars[0].v_type != VAR_DICT)
6020 {
6021 EMSG(_(e_dictreq));
6022 return;
6023 }
6024 if (argvars[0].vval.v_dict == NULL)
6025 return;
6026
6027 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6028 get_tv_string(&argvars[1]), -1) != NULL;
6029}
6030
6031/*
6032 * "haslocaldir()" function
6033 */
6034 static void
6035f_haslocaldir(typval_T *argvars, typval_T *rettv)
6036{
6037 win_T *wp = NULL;
6038
6039 wp = find_tabwin(&argvars[0], &argvars[1]);
6040 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6041}
6042
6043/*
6044 * "hasmapto()" function
6045 */
6046 static void
6047f_hasmapto(typval_T *argvars, typval_T *rettv)
6048{
6049 char_u *name;
6050 char_u *mode;
6051 char_u buf[NUMBUFLEN];
6052 int abbr = FALSE;
6053
6054 name = get_tv_string(&argvars[0]);
6055 if (argvars[1].v_type == VAR_UNKNOWN)
6056 mode = (char_u *)"nvo";
6057 else
6058 {
6059 mode = get_tv_string_buf(&argvars[1], buf);
6060 if (argvars[2].v_type != VAR_UNKNOWN)
6061 abbr = (int)get_tv_number(&argvars[2]);
6062 }
6063
6064 if (map_to_exists(name, mode, abbr))
6065 rettv->vval.v_number = TRUE;
6066 else
6067 rettv->vval.v_number = FALSE;
6068}
6069
6070/*
6071 * "histadd()" function
6072 */
6073 static void
6074f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6075{
6076#ifdef FEAT_CMDHIST
6077 int histype;
6078 char_u *str;
6079 char_u buf[NUMBUFLEN];
6080#endif
6081
6082 rettv->vval.v_number = FALSE;
6083 if (check_restricted() || check_secure())
6084 return;
6085#ifdef FEAT_CMDHIST
6086 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6087 histype = str != NULL ? get_histtype(str) : -1;
6088 if (histype >= 0)
6089 {
6090 str = get_tv_string_buf(&argvars[1], buf);
6091 if (*str != NUL)
6092 {
6093 init_history();
6094 add_to_history(histype, str, FALSE, NUL);
6095 rettv->vval.v_number = TRUE;
6096 return;
6097 }
6098 }
6099#endif
6100}
6101
6102/*
6103 * "histdel()" function
6104 */
6105 static void
6106f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6107{
6108#ifdef FEAT_CMDHIST
6109 int n;
6110 char_u buf[NUMBUFLEN];
6111 char_u *str;
6112
6113 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6114 if (str == NULL)
6115 n = 0;
6116 else if (argvars[1].v_type == VAR_UNKNOWN)
6117 /* only one argument: clear entire history */
6118 n = clr_history(get_histtype(str));
6119 else if (argvars[1].v_type == VAR_NUMBER)
6120 /* index given: remove that entry */
6121 n = del_history_idx(get_histtype(str),
6122 (int)get_tv_number(&argvars[1]));
6123 else
6124 /* string given: remove all matching entries */
6125 n = del_history_entry(get_histtype(str),
6126 get_tv_string_buf(&argvars[1], buf));
6127 rettv->vval.v_number = n;
6128#endif
6129}
6130
6131/*
6132 * "histget()" function
6133 */
6134 static void
6135f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6136{
6137#ifdef FEAT_CMDHIST
6138 int type;
6139 int idx;
6140 char_u *str;
6141
6142 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6143 if (str == NULL)
6144 rettv->vval.v_string = NULL;
6145 else
6146 {
6147 type = get_histtype(str);
6148 if (argvars[1].v_type == VAR_UNKNOWN)
6149 idx = get_history_idx(type);
6150 else
6151 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6152 /* -1 on type error */
6153 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6154 }
6155#else
6156 rettv->vval.v_string = NULL;
6157#endif
6158 rettv->v_type = VAR_STRING;
6159}
6160
6161/*
6162 * "histnr()" function
6163 */
6164 static void
6165f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6166{
6167 int i;
6168
6169#ifdef FEAT_CMDHIST
6170 char_u *history = get_tv_string_chk(&argvars[0]);
6171
6172 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6173 if (i >= HIST_CMD && i < HIST_COUNT)
6174 i = get_history_idx(i);
6175 else
6176#endif
6177 i = -1;
6178 rettv->vval.v_number = i;
6179}
6180
6181/*
6182 * "highlightID(name)" function
6183 */
6184 static void
6185f_hlID(typval_T *argvars, typval_T *rettv)
6186{
6187 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6188}
6189
6190/*
6191 * "highlight_exists()" function
6192 */
6193 static void
6194f_hlexists(typval_T *argvars, typval_T *rettv)
6195{
6196 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6197}
6198
6199/*
6200 * "hostname()" function
6201 */
6202 static void
6203f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6204{
6205 char_u hostname[256];
6206
6207 mch_get_host_name(hostname, 256);
6208 rettv->v_type = VAR_STRING;
6209 rettv->vval.v_string = vim_strsave(hostname);
6210}
6211
6212/*
6213 * iconv() function
6214 */
6215 static void
6216f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6217{
6218#ifdef FEAT_MBYTE
6219 char_u buf1[NUMBUFLEN];
6220 char_u buf2[NUMBUFLEN];
6221 char_u *from, *to, *str;
6222 vimconv_T vimconv;
6223#endif
6224
6225 rettv->v_type = VAR_STRING;
6226 rettv->vval.v_string = NULL;
6227
6228#ifdef FEAT_MBYTE
6229 str = get_tv_string(&argvars[0]);
6230 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6231 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6232 vimconv.vc_type = CONV_NONE;
6233 convert_setup(&vimconv, from, to);
6234
6235 /* If the encodings are equal, no conversion needed. */
6236 if (vimconv.vc_type == CONV_NONE)
6237 rettv->vval.v_string = vim_strsave(str);
6238 else
6239 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6240
6241 convert_setup(&vimconv, NULL, NULL);
6242 vim_free(from);
6243 vim_free(to);
6244#endif
6245}
6246
6247/*
6248 * "indent()" function
6249 */
6250 static void
6251f_indent(typval_T *argvars, typval_T *rettv)
6252{
6253 linenr_T lnum;
6254
6255 lnum = get_tv_lnum(argvars);
6256 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6257 rettv->vval.v_number = get_indent_lnum(lnum);
6258 else
6259 rettv->vval.v_number = -1;
6260}
6261
6262/*
6263 * "index()" function
6264 */
6265 static void
6266f_index(typval_T *argvars, typval_T *rettv)
6267{
6268 list_T *l;
6269 listitem_T *item;
6270 long idx = 0;
6271 int ic = FALSE;
6272
6273 rettv->vval.v_number = -1;
6274 if (argvars[0].v_type != VAR_LIST)
6275 {
6276 EMSG(_(e_listreq));
6277 return;
6278 }
6279 l = argvars[0].vval.v_list;
6280 if (l != NULL)
6281 {
6282 item = l->lv_first;
6283 if (argvars[2].v_type != VAR_UNKNOWN)
6284 {
6285 int error = FALSE;
6286
6287 /* Start at specified item. Use the cached index that list_find()
6288 * sets, so that a negative number also works. */
6289 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6290 idx = l->lv_idx;
6291 if (argvars[3].v_type != VAR_UNKNOWN)
6292 ic = (int)get_tv_number_chk(&argvars[3], &error);
6293 if (error)
6294 item = NULL;
6295 }
6296
6297 for ( ; item != NULL; item = item->li_next, ++idx)
6298 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6299 {
6300 rettv->vval.v_number = idx;
6301 break;
6302 }
6303 }
6304}
6305
6306static int inputsecret_flag = 0;
6307
6308/*
6309 * "input()" function
6310 * Also handles inputsecret() when inputsecret is set.
6311 */
6312 static void
6313f_input(typval_T *argvars, typval_T *rettv)
6314{
6315 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6316}
6317
6318/*
6319 * "inputdialog()" function
6320 */
6321 static void
6322f_inputdialog(typval_T *argvars, typval_T *rettv)
6323{
6324#if defined(FEAT_GUI_TEXTDIALOG)
6325 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6326 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6327 {
6328 char_u *message;
6329 char_u buf[NUMBUFLEN];
6330 char_u *defstr = (char_u *)"";
6331
6332 message = get_tv_string_chk(&argvars[0]);
6333 if (argvars[1].v_type != VAR_UNKNOWN
6334 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6335 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6336 else
6337 IObuff[0] = NUL;
6338 if (message != NULL && defstr != NULL
6339 && do_dialog(VIM_QUESTION, NULL, message,
6340 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6341 rettv->vval.v_string = vim_strsave(IObuff);
6342 else
6343 {
6344 if (message != NULL && defstr != NULL
6345 && argvars[1].v_type != VAR_UNKNOWN
6346 && argvars[2].v_type != VAR_UNKNOWN)
6347 rettv->vval.v_string = vim_strsave(
6348 get_tv_string_buf(&argvars[2], buf));
6349 else
6350 rettv->vval.v_string = NULL;
6351 }
6352 rettv->v_type = VAR_STRING;
6353 }
6354 else
6355#endif
6356 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6357}
6358
6359/*
6360 * "inputlist()" function
6361 */
6362 static void
6363f_inputlist(typval_T *argvars, typval_T *rettv)
6364{
6365 listitem_T *li;
6366 int selected;
6367 int mouse_used;
6368
6369#ifdef NO_CONSOLE_INPUT
6370 /* While starting up, there is no place to enter text. */
6371 if (no_console_input())
6372 return;
6373#endif
6374 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6375 {
6376 EMSG2(_(e_listarg), "inputlist()");
6377 return;
6378 }
6379
6380 msg_start();
6381 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6382 lines_left = Rows; /* avoid more prompt */
6383 msg_scroll = TRUE;
6384 msg_clr_eos();
6385
6386 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6387 {
6388 msg_puts(get_tv_string(&li->li_tv));
6389 msg_putchar('\n');
6390 }
6391
6392 /* Ask for choice. */
6393 selected = prompt_for_number(&mouse_used);
6394 if (mouse_used)
6395 selected -= lines_left;
6396
6397 rettv->vval.v_number = selected;
6398}
6399
6400
6401static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6402
6403/*
6404 * "inputrestore()" function
6405 */
6406 static void
6407f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6408{
6409 if (ga_userinput.ga_len > 0)
6410 {
6411 --ga_userinput.ga_len;
6412 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6413 + ga_userinput.ga_len);
6414 /* default return is zero == OK */
6415 }
6416 else if (p_verbose > 1)
6417 {
6418 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6419 rettv->vval.v_number = 1; /* Failed */
6420 }
6421}
6422
6423/*
6424 * "inputsave()" function
6425 */
6426 static void
6427f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6428{
6429 /* Add an entry to the stack of typeahead storage. */
6430 if (ga_grow(&ga_userinput, 1) == OK)
6431 {
6432 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6433 + ga_userinput.ga_len);
6434 ++ga_userinput.ga_len;
6435 /* default return is zero == OK */
6436 }
6437 else
6438 rettv->vval.v_number = 1; /* Failed */
6439}
6440
6441/*
6442 * "inputsecret()" function
6443 */
6444 static void
6445f_inputsecret(typval_T *argvars, typval_T *rettv)
6446{
6447 ++cmdline_star;
6448 ++inputsecret_flag;
6449 f_input(argvars, rettv);
6450 --cmdline_star;
6451 --inputsecret_flag;
6452}
6453
6454/*
6455 * "insert()" function
6456 */
6457 static void
6458f_insert(typval_T *argvars, typval_T *rettv)
6459{
6460 long before = 0;
6461 listitem_T *item;
6462 list_T *l;
6463 int error = FALSE;
6464
6465 if (argvars[0].v_type != VAR_LIST)
6466 EMSG2(_(e_listarg), "insert()");
6467 else if ((l = argvars[0].vval.v_list) != NULL
6468 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6469 {
6470 if (argvars[2].v_type != VAR_UNKNOWN)
6471 before = (long)get_tv_number_chk(&argvars[2], &error);
6472 if (error)
6473 return; /* type error; errmsg already given */
6474
6475 if (before == l->lv_len)
6476 item = NULL;
6477 else
6478 {
6479 item = list_find(l, before);
6480 if (item == NULL)
6481 {
6482 EMSGN(_(e_listidx), before);
6483 l = NULL;
6484 }
6485 }
6486 if (l != NULL)
6487 {
6488 list_insert_tv(l, &argvars[1], item);
6489 copy_tv(&argvars[0], rettv);
6490 }
6491 }
6492}
6493
6494/*
6495 * "invert(expr)" function
6496 */
6497 static void
6498f_invert(typval_T *argvars, typval_T *rettv)
6499{
6500 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6501}
6502
6503/*
6504 * "isdirectory()" function
6505 */
6506 static void
6507f_isdirectory(typval_T *argvars, typval_T *rettv)
6508{
6509 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6510}
6511
6512/*
6513 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6514 * or it refers to a List or Dictionary that is locked.
6515 */
6516 static int
6517tv_islocked(typval_T *tv)
6518{
6519 return (tv->v_lock & VAR_LOCKED)
6520 || (tv->v_type == VAR_LIST
6521 && tv->vval.v_list != NULL
6522 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6523 || (tv->v_type == VAR_DICT
6524 && tv->vval.v_dict != NULL
6525 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6526}
6527
6528/*
6529 * "islocked()" function
6530 */
6531 static void
6532f_islocked(typval_T *argvars, typval_T *rettv)
6533{
6534 lval_T lv;
6535 char_u *end;
6536 dictitem_T *di;
6537
6538 rettv->vval.v_number = -1;
6539 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
6540 GLV_NO_AUTOLOAD, FNE_CHECK_START);
6541 if (end != NULL && lv.ll_name != NULL)
6542 {
6543 if (*end != NUL)
6544 EMSG(_(e_trailing));
6545 else
6546 {
6547 if (lv.ll_tv == NULL)
6548 {
6549 if (check_changedtick(lv.ll_name))
6550 rettv->vval.v_number = 1; /* always locked */
6551 else
6552 {
6553 di = find_var(lv.ll_name, NULL, TRUE);
6554 if (di != NULL)
6555 {
6556 /* Consider a variable locked when:
6557 * 1. the variable itself is locked
6558 * 2. the value of the variable is locked.
6559 * 3. the List or Dict value is locked.
6560 */
6561 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6562 || tv_islocked(&di->di_tv));
6563 }
6564 }
6565 }
6566 else if (lv.ll_range)
6567 EMSG(_("E786: Range not allowed"));
6568 else if (lv.ll_newkey != NULL)
6569 EMSG2(_(e_dictkey), lv.ll_newkey);
6570 else if (lv.ll_list != NULL)
6571 /* List item. */
6572 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6573 else
6574 /* Dictionary item. */
6575 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6576 }
6577 }
6578
6579 clear_lval(&lv);
6580}
6581
6582#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6583/*
6584 * "isnan()" function
6585 */
6586 static void
6587f_isnan(typval_T *argvars, typval_T *rettv)
6588{
6589 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6590 && isnan(argvars[0].vval.v_float);
6591}
6592#endif
6593
6594/*
6595 * "items(dict)" function
6596 */
6597 static void
6598f_items(typval_T *argvars, typval_T *rettv)
6599{
6600 dict_list(argvars, rettv, 2);
6601}
6602
6603#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6604/*
6605 * Get the job from the argument.
6606 * Returns NULL if the job is invalid.
6607 */
6608 static job_T *
6609get_job_arg(typval_T *tv)
6610{
6611 job_T *job;
6612
6613 if (tv->v_type != VAR_JOB)
6614 {
6615 EMSG2(_(e_invarg2), get_tv_string(tv));
6616 return NULL;
6617 }
6618 job = tv->vval.v_job;
6619
6620 if (job == NULL)
6621 EMSG(_("E916: not a valid job"));
6622 return job;
6623}
6624
6625/*
6626 * "job_getchannel()" function
6627 */
6628 static void
6629f_job_getchannel(typval_T *argvars, typval_T *rettv)
6630{
6631 job_T *job = get_job_arg(&argvars[0]);
6632
6633 if (job != NULL)
6634 {
6635 rettv->v_type = VAR_CHANNEL;
6636 rettv->vval.v_channel = job->jv_channel;
6637 if (job->jv_channel != NULL)
6638 ++job->jv_channel->ch_refcount;
6639 }
6640}
6641
6642/*
6643 * "job_info()" function
6644 */
6645 static void
6646f_job_info(typval_T *argvars, typval_T *rettv)
6647{
6648 job_T *job = get_job_arg(&argvars[0]);
6649
6650 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6651 job_info(job, rettv->vval.v_dict);
6652}
6653
6654/*
6655 * "job_setoptions()" function
6656 */
6657 static void
6658f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6659{
6660 job_T *job = get_job_arg(&argvars[0]);
6661 jobopt_T opt;
6662
6663 if (job == NULL)
6664 return;
6665 clear_job_options(&opt);
6666 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
6667 job_set_options(job, &opt);
6668 free_job_options(&opt);
6669}
6670
6671/*
6672 * "job_start()" function
6673 */
6674 static void
6675f_job_start(typval_T *argvars, typval_T *rettv)
6676{
6677 rettv->v_type = VAR_JOB;
6678 if (check_restricted() || check_secure())
6679 return;
6680 rettv->vval.v_job = job_start(argvars);
6681}
6682
6683/*
6684 * "job_status()" function
6685 */
6686 static void
6687f_job_status(typval_T *argvars, typval_T *rettv)
6688{
6689 job_T *job = get_job_arg(&argvars[0]);
6690
6691 if (job != NULL)
6692 {
6693 rettv->v_type = VAR_STRING;
6694 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6695 }
6696}
6697
6698/*
6699 * "job_stop()" function
6700 */
6701 static void
6702f_job_stop(typval_T *argvars, typval_T *rettv)
6703{
6704 job_T *job = get_job_arg(&argvars[0]);
6705
6706 if (job != NULL)
6707 rettv->vval.v_number = job_stop(job, argvars);
6708}
6709#endif
6710
6711/*
6712 * "join()" function
6713 */
6714 static void
6715f_join(typval_T *argvars, typval_T *rettv)
6716{
6717 garray_T ga;
6718 char_u *sep;
6719
6720 if (argvars[0].v_type != VAR_LIST)
6721 {
6722 EMSG(_(e_listreq));
6723 return;
6724 }
6725 if (argvars[0].vval.v_list == NULL)
6726 return;
6727 if (argvars[1].v_type == VAR_UNKNOWN)
6728 sep = (char_u *)" ";
6729 else
6730 sep = get_tv_string_chk(&argvars[1]);
6731
6732 rettv->v_type = VAR_STRING;
6733
6734 if (sep != NULL)
6735 {
6736 ga_init2(&ga, (int)sizeof(char), 80);
6737 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6738 ga_append(&ga, NUL);
6739 rettv->vval.v_string = (char_u *)ga.ga_data;
6740 }
6741 else
6742 rettv->vval.v_string = NULL;
6743}
6744
6745/*
6746 * "js_decode()" function
6747 */
6748 static void
6749f_js_decode(typval_T *argvars, typval_T *rettv)
6750{
6751 js_read_T reader;
6752
6753 reader.js_buf = get_tv_string(&argvars[0]);
6754 reader.js_fill = NULL;
6755 reader.js_used = 0;
6756 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
6757 EMSG(_(e_invarg));
6758}
6759
6760/*
6761 * "js_encode()" function
6762 */
6763 static void
6764f_js_encode(typval_T *argvars, typval_T *rettv)
6765{
6766 rettv->v_type = VAR_STRING;
6767 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
6768}
6769
6770/*
6771 * "json_decode()" function
6772 */
6773 static void
6774f_json_decode(typval_T *argvars, typval_T *rettv)
6775{
6776 js_read_T reader;
6777
6778 reader.js_buf = get_tv_string(&argvars[0]);
6779 reader.js_fill = NULL;
6780 reader.js_used = 0;
6781 if (json_decode_all(&reader, rettv, 0) != OK)
6782 EMSG(_(e_invarg));
6783}
6784
6785/*
6786 * "json_encode()" function
6787 */
6788 static void
6789f_json_encode(typval_T *argvars, typval_T *rettv)
6790{
6791 rettv->v_type = VAR_STRING;
6792 rettv->vval.v_string = json_encode(&argvars[0], 0);
6793}
6794
6795/*
6796 * "keys()" function
6797 */
6798 static void
6799f_keys(typval_T *argvars, typval_T *rettv)
6800{
6801 dict_list(argvars, rettv, 0);
6802}
6803
6804/*
6805 * "last_buffer_nr()" function.
6806 */
6807 static void
6808f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6809{
6810 int n = 0;
6811 buf_T *buf;
6812
Bram Moolenaar29323592016-07-24 22:04:11 +02006813 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006814 if (n < buf->b_fnum)
6815 n = buf->b_fnum;
6816
6817 rettv->vval.v_number = n;
6818}
6819
6820/*
6821 * "len()" function
6822 */
6823 static void
6824f_len(typval_T *argvars, typval_T *rettv)
6825{
6826 switch (argvars[0].v_type)
6827 {
6828 case VAR_STRING:
6829 case VAR_NUMBER:
6830 rettv->vval.v_number = (varnumber_T)STRLEN(
6831 get_tv_string(&argvars[0]));
6832 break;
6833 case VAR_LIST:
6834 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6835 break;
6836 case VAR_DICT:
6837 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6838 break;
6839 case VAR_UNKNOWN:
6840 case VAR_SPECIAL:
6841 case VAR_FLOAT:
6842 case VAR_FUNC:
6843 case VAR_PARTIAL:
6844 case VAR_JOB:
6845 case VAR_CHANNEL:
6846 EMSG(_("E701: Invalid type for len()"));
6847 break;
6848 }
6849}
6850
6851static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
6852
6853 static void
6854libcall_common(typval_T *argvars, typval_T *rettv, int type)
6855{
6856#ifdef FEAT_LIBCALL
6857 char_u *string_in;
6858 char_u **string_result;
6859 int nr_result;
6860#endif
6861
6862 rettv->v_type = type;
6863 if (type != VAR_NUMBER)
6864 rettv->vval.v_string = NULL;
6865
6866 if (check_restricted() || check_secure())
6867 return;
6868
6869#ifdef FEAT_LIBCALL
6870 /* The first two args must be strings, otherwise its meaningless */
6871 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6872 {
6873 string_in = NULL;
6874 if (argvars[2].v_type == VAR_STRING)
6875 string_in = argvars[2].vval.v_string;
6876 if (type == VAR_NUMBER)
6877 string_result = NULL;
6878 else
6879 string_result = &rettv->vval.v_string;
6880 if (mch_libcall(argvars[0].vval.v_string,
6881 argvars[1].vval.v_string,
6882 string_in,
6883 argvars[2].vval.v_number,
6884 string_result,
6885 &nr_result) == OK
6886 && type == VAR_NUMBER)
6887 rettv->vval.v_number = nr_result;
6888 }
6889#endif
6890}
6891
6892/*
6893 * "libcall()" function
6894 */
6895 static void
6896f_libcall(typval_T *argvars, typval_T *rettv)
6897{
6898 libcall_common(argvars, rettv, VAR_STRING);
6899}
6900
6901/*
6902 * "libcallnr()" function
6903 */
6904 static void
6905f_libcallnr(typval_T *argvars, typval_T *rettv)
6906{
6907 libcall_common(argvars, rettv, VAR_NUMBER);
6908}
6909
6910/*
6911 * "line(string)" function
6912 */
6913 static void
6914f_line(typval_T *argvars, typval_T *rettv)
6915{
6916 linenr_T lnum = 0;
6917 pos_T *fp;
6918 int fnum;
6919
6920 fp = var2fpos(&argvars[0], TRUE, &fnum);
6921 if (fp != NULL)
6922 lnum = fp->lnum;
6923 rettv->vval.v_number = lnum;
6924}
6925
6926/*
6927 * "line2byte(lnum)" function
6928 */
6929 static void
6930f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6931{
6932#ifndef FEAT_BYTEOFF
6933 rettv->vval.v_number = -1;
6934#else
6935 linenr_T lnum;
6936
6937 lnum = get_tv_lnum(argvars);
6938 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6939 rettv->vval.v_number = -1;
6940 else
6941 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6942 if (rettv->vval.v_number >= 0)
6943 ++rettv->vval.v_number;
6944#endif
6945}
6946
6947/*
6948 * "lispindent(lnum)" function
6949 */
6950 static void
6951f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
6952{
6953#ifdef FEAT_LISP
6954 pos_T pos;
6955 linenr_T lnum;
6956
6957 pos = curwin->w_cursor;
6958 lnum = get_tv_lnum(argvars);
6959 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6960 {
6961 curwin->w_cursor.lnum = lnum;
6962 rettv->vval.v_number = get_lisp_indent();
6963 curwin->w_cursor = pos;
6964 }
6965 else
6966#endif
6967 rettv->vval.v_number = -1;
6968}
6969
6970/*
6971 * "localtime()" function
6972 */
6973 static void
6974f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
6975{
6976 rettv->vval.v_number = (varnumber_T)time(NULL);
6977}
6978
6979static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
6980
6981 static void
6982get_maparg(typval_T *argvars, typval_T *rettv, int exact)
6983{
6984 char_u *keys;
6985 char_u *which;
6986 char_u buf[NUMBUFLEN];
6987 char_u *keys_buf = NULL;
6988 char_u *rhs;
6989 int mode;
6990 int abbr = FALSE;
6991 int get_dict = FALSE;
6992 mapblock_T *mp;
6993 int buffer_local;
6994
6995 /* return empty string for failure */
6996 rettv->v_type = VAR_STRING;
6997 rettv->vval.v_string = NULL;
6998
6999 keys = get_tv_string(&argvars[0]);
7000 if (*keys == NUL)
7001 return;
7002
7003 if (argvars[1].v_type != VAR_UNKNOWN)
7004 {
7005 which = get_tv_string_buf_chk(&argvars[1], buf);
7006 if (argvars[2].v_type != VAR_UNKNOWN)
7007 {
7008 abbr = (int)get_tv_number(&argvars[2]);
7009 if (argvars[3].v_type != VAR_UNKNOWN)
7010 get_dict = (int)get_tv_number(&argvars[3]);
7011 }
7012 }
7013 else
7014 which = (char_u *)"";
7015 if (which == NULL)
7016 return;
7017
7018 mode = get_map_mode(&which, 0);
7019
7020 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7021 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7022 vim_free(keys_buf);
7023
7024 if (!get_dict)
7025 {
7026 /* Return a string. */
7027 if (rhs != NULL)
7028 rettv->vval.v_string = str2special_save(rhs, FALSE);
7029
7030 }
7031 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7032 {
7033 /* Return a dictionary. */
7034 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7035 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7036 dict_T *dict = rettv->vval.v_dict;
7037
7038 dict_add_nr_str(dict, "lhs", 0L, lhs);
7039 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7040 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7041 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7042 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7043 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7044 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7045 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7046 dict_add_nr_str(dict, "mode", 0L, mapmode);
7047
7048 vim_free(lhs);
7049 vim_free(mapmode);
7050 }
7051}
7052
7053#ifdef FEAT_FLOAT
7054/*
7055 * "log()" function
7056 */
7057 static void
7058f_log(typval_T *argvars, typval_T *rettv)
7059{
7060 float_T f = 0.0;
7061
7062 rettv->v_type = VAR_FLOAT;
7063 if (get_float_arg(argvars, &f) == OK)
7064 rettv->vval.v_float = log(f);
7065 else
7066 rettv->vval.v_float = 0.0;
7067}
7068
7069/*
7070 * "log10()" function
7071 */
7072 static void
7073f_log10(typval_T *argvars, typval_T *rettv)
7074{
7075 float_T f = 0.0;
7076
7077 rettv->v_type = VAR_FLOAT;
7078 if (get_float_arg(argvars, &f) == OK)
7079 rettv->vval.v_float = log10(f);
7080 else
7081 rettv->vval.v_float = 0.0;
7082}
7083#endif
7084
7085#ifdef FEAT_LUA
7086/*
7087 * "luaeval()" function
7088 */
7089 static void
7090f_luaeval(typval_T *argvars, typval_T *rettv)
7091{
7092 char_u *str;
7093 char_u buf[NUMBUFLEN];
7094
7095 str = get_tv_string_buf(&argvars[0], buf);
7096 do_luaeval(str, argvars + 1, rettv);
7097}
7098#endif
7099
7100/*
7101 * "map()" function
7102 */
7103 static void
7104f_map(typval_T *argvars, typval_T *rettv)
7105{
7106 filter_map(argvars, rettv, TRUE);
7107}
7108
7109/*
7110 * "maparg()" function
7111 */
7112 static void
7113f_maparg(typval_T *argvars, typval_T *rettv)
7114{
7115 get_maparg(argvars, rettv, TRUE);
7116}
7117
7118/*
7119 * "mapcheck()" function
7120 */
7121 static void
7122f_mapcheck(typval_T *argvars, typval_T *rettv)
7123{
7124 get_maparg(argvars, rettv, FALSE);
7125}
7126
7127static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
7128
7129 static void
7130find_some_match(typval_T *argvars, typval_T *rettv, int type)
7131{
7132 char_u *str = NULL;
7133 long len = 0;
7134 char_u *expr = NULL;
7135 char_u *pat;
7136 regmatch_T regmatch;
7137 char_u patbuf[NUMBUFLEN];
7138 char_u strbuf[NUMBUFLEN];
7139 char_u *save_cpo;
7140 long start = 0;
7141 long nth = 1;
7142 colnr_T startcol = 0;
7143 int match = 0;
7144 list_T *l = NULL;
7145 listitem_T *li = NULL;
7146 long idx = 0;
7147 char_u *tofree = NULL;
7148
7149 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7150 save_cpo = p_cpo;
7151 p_cpo = (char_u *)"";
7152
7153 rettv->vval.v_number = -1;
7154 if (type == 3 || type == 4)
7155 {
7156 /* type 3: return empty list when there are no matches.
7157 * type 4: return ["", -1, -1, -1] */
7158 if (rettv_list_alloc(rettv) == FAIL)
7159 goto theend;
7160 if (type == 4
7161 && (list_append_string(rettv->vval.v_list,
7162 (char_u *)"", 0) == 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 || list_append_number(rettv->vval.v_list,
7168 (varnumber_T)-1) == FAIL))
7169 {
7170 list_free(rettv->vval.v_list);
7171 rettv->vval.v_list = NULL;
7172 goto theend;
7173 }
7174 }
7175 else if (type == 2)
7176 {
7177 rettv->v_type = VAR_STRING;
7178 rettv->vval.v_string = NULL;
7179 }
7180
7181 if (argvars[0].v_type == VAR_LIST)
7182 {
7183 if ((l = argvars[0].vval.v_list) == NULL)
7184 goto theend;
7185 li = l->lv_first;
7186 }
7187 else
7188 {
7189 expr = str = get_tv_string(&argvars[0]);
7190 len = (long)STRLEN(str);
7191 }
7192
7193 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7194 if (pat == NULL)
7195 goto theend;
7196
7197 if (argvars[2].v_type != VAR_UNKNOWN)
7198 {
7199 int error = FALSE;
7200
7201 start = (long)get_tv_number_chk(&argvars[2], &error);
7202 if (error)
7203 goto theend;
7204 if (l != NULL)
7205 {
7206 li = list_find(l, start);
7207 if (li == NULL)
7208 goto theend;
7209 idx = l->lv_idx; /* use the cached index */
7210 }
7211 else
7212 {
7213 if (start < 0)
7214 start = 0;
7215 if (start > len)
7216 goto theend;
7217 /* When "count" argument is there ignore matches before "start",
7218 * otherwise skip part of the string. Differs when pattern is "^"
7219 * or "\<". */
7220 if (argvars[3].v_type != VAR_UNKNOWN)
7221 startcol = start;
7222 else
7223 {
7224 str += start;
7225 len -= start;
7226 }
7227 }
7228
7229 if (argvars[3].v_type != VAR_UNKNOWN)
7230 nth = (long)get_tv_number_chk(&argvars[3], &error);
7231 if (error)
7232 goto theend;
7233 }
7234
7235 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7236 if (regmatch.regprog != NULL)
7237 {
7238 regmatch.rm_ic = p_ic;
7239
7240 for (;;)
7241 {
7242 if (l != NULL)
7243 {
7244 if (li == NULL)
7245 {
7246 match = FALSE;
7247 break;
7248 }
7249 vim_free(tofree);
7250 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7251 if (str == NULL)
7252 break;
7253 }
7254
7255 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7256
7257 if (match && --nth <= 0)
7258 break;
7259 if (l == NULL && !match)
7260 break;
7261
7262 /* Advance to just after the match. */
7263 if (l != NULL)
7264 {
7265 li = li->li_next;
7266 ++idx;
7267 }
7268 else
7269 {
7270#ifdef FEAT_MBYTE
7271 startcol = (colnr_T)(regmatch.startp[0]
7272 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7273#else
7274 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7275#endif
7276 if (startcol > (colnr_T)len
7277 || str + startcol <= regmatch.startp[0])
7278 {
7279 match = FALSE;
7280 break;
7281 }
7282 }
7283 }
7284
7285 if (match)
7286 {
7287 if (type == 4)
7288 {
7289 listitem_T *li1 = rettv->vval.v_list->lv_first;
7290 listitem_T *li2 = li1->li_next;
7291 listitem_T *li3 = li2->li_next;
7292 listitem_T *li4 = li3->li_next;
7293
7294 vim_free(li1->li_tv.vval.v_string);
7295 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7296 (int)(regmatch.endp[0] - regmatch.startp[0]));
7297 li3->li_tv.vval.v_number =
7298 (varnumber_T)(regmatch.startp[0] - expr);
7299 li4->li_tv.vval.v_number =
7300 (varnumber_T)(regmatch.endp[0] - expr);
7301 if (l != NULL)
7302 li2->li_tv.vval.v_number = (varnumber_T)idx;
7303 }
7304 else if (type == 3)
7305 {
7306 int i;
7307
7308 /* return list with matched string and submatches */
7309 for (i = 0; i < NSUBEXP; ++i)
7310 {
7311 if (regmatch.endp[i] == NULL)
7312 {
7313 if (list_append_string(rettv->vval.v_list,
7314 (char_u *)"", 0) == FAIL)
7315 break;
7316 }
7317 else if (list_append_string(rettv->vval.v_list,
7318 regmatch.startp[i],
7319 (int)(regmatch.endp[i] - regmatch.startp[i]))
7320 == FAIL)
7321 break;
7322 }
7323 }
7324 else if (type == 2)
7325 {
7326 /* return matched string */
7327 if (l != NULL)
7328 copy_tv(&li->li_tv, rettv);
7329 else
7330 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7331 (int)(regmatch.endp[0] - regmatch.startp[0]));
7332 }
7333 else if (l != NULL)
7334 rettv->vval.v_number = idx;
7335 else
7336 {
7337 if (type != 0)
7338 rettv->vval.v_number =
7339 (varnumber_T)(regmatch.startp[0] - str);
7340 else
7341 rettv->vval.v_number =
7342 (varnumber_T)(regmatch.endp[0] - str);
7343 rettv->vval.v_number += (varnumber_T)(str - expr);
7344 }
7345 }
7346 vim_regfree(regmatch.regprog);
7347 }
7348
7349 if (type == 4 && l == NULL)
7350 /* matchstrpos() without a list: drop the second item. */
7351 listitem_remove(rettv->vval.v_list,
7352 rettv->vval.v_list->lv_first->li_next);
7353
7354theend:
7355 vim_free(tofree);
7356 p_cpo = save_cpo;
7357}
7358
7359/*
7360 * "match()" function
7361 */
7362 static void
7363f_match(typval_T *argvars, typval_T *rettv)
7364{
7365 find_some_match(argvars, rettv, 1);
7366}
7367
7368/*
7369 * "matchadd()" function
7370 */
7371 static void
7372f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7373{
7374#ifdef FEAT_SEARCH_EXTRA
7375 char_u buf[NUMBUFLEN];
7376 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7377 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7378 int prio = 10; /* default priority */
7379 int id = -1;
7380 int error = FALSE;
7381 char_u *conceal_char = NULL;
7382
7383 rettv->vval.v_number = -1;
7384
7385 if (grp == NULL || pat == NULL)
7386 return;
7387 if (argvars[2].v_type != VAR_UNKNOWN)
7388 {
7389 prio = (int)get_tv_number_chk(&argvars[2], &error);
7390 if (argvars[3].v_type != VAR_UNKNOWN)
7391 {
7392 id = (int)get_tv_number_chk(&argvars[3], &error);
7393 if (argvars[4].v_type != VAR_UNKNOWN)
7394 {
7395 if (argvars[4].v_type != VAR_DICT)
7396 {
7397 EMSG(_(e_dictreq));
7398 return;
7399 }
7400 if (dict_find(argvars[4].vval.v_dict,
7401 (char_u *)"conceal", -1) != NULL)
7402 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7403 (char_u *)"conceal", FALSE);
7404 }
7405 }
7406 }
7407 if (error == TRUE)
7408 return;
7409 if (id >= 1 && id <= 3)
7410 {
7411 EMSGN("E798: ID is reserved for \":match\": %ld", id);
7412 return;
7413 }
7414
7415 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7416 conceal_char);
7417#endif
7418}
7419
7420/*
7421 * "matchaddpos()" function
7422 */
7423 static void
7424f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7425{
7426#ifdef FEAT_SEARCH_EXTRA
7427 char_u buf[NUMBUFLEN];
7428 char_u *group;
7429 int prio = 10;
7430 int id = -1;
7431 int error = FALSE;
7432 list_T *l;
7433 char_u *conceal_char = NULL;
7434
7435 rettv->vval.v_number = -1;
7436
7437 group = get_tv_string_buf_chk(&argvars[0], buf);
7438 if (group == NULL)
7439 return;
7440
7441 if (argvars[1].v_type != VAR_LIST)
7442 {
7443 EMSG2(_(e_listarg), "matchaddpos()");
7444 return;
7445 }
7446 l = argvars[1].vval.v_list;
7447 if (l == NULL)
7448 return;
7449
7450 if (argvars[2].v_type != VAR_UNKNOWN)
7451 {
7452 prio = (int)get_tv_number_chk(&argvars[2], &error);
7453 if (argvars[3].v_type != VAR_UNKNOWN)
7454 {
7455 id = (int)get_tv_number_chk(&argvars[3], &error);
7456 if (argvars[4].v_type != VAR_UNKNOWN)
7457 {
7458 if (argvars[4].v_type != VAR_DICT)
7459 {
7460 EMSG(_(e_dictreq));
7461 return;
7462 }
7463 if (dict_find(argvars[4].vval.v_dict,
7464 (char_u *)"conceal", -1) != NULL)
7465 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7466 (char_u *)"conceal", FALSE);
7467 }
7468 }
7469 }
7470 if (error == TRUE)
7471 return;
7472
7473 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7474 if (id == 1 || id == 2)
7475 {
7476 EMSGN("E798: ID is reserved for \":match\": %ld", id);
7477 return;
7478 }
7479
7480 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7481 conceal_char);
7482#endif
7483}
7484
7485/*
7486 * "matcharg()" function
7487 */
7488 static void
7489f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7490{
7491 if (rettv_list_alloc(rettv) == OK)
7492 {
7493#ifdef FEAT_SEARCH_EXTRA
7494 int id = (int)get_tv_number(&argvars[0]);
7495 matchitem_T *m;
7496
7497 if (id >= 1 && id <= 3)
7498 {
7499 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7500 {
7501 list_append_string(rettv->vval.v_list,
7502 syn_id2name(m->hlg_id), -1);
7503 list_append_string(rettv->vval.v_list, m->pattern, -1);
7504 }
7505 else
7506 {
7507 list_append_string(rettv->vval.v_list, NULL, -1);
7508 list_append_string(rettv->vval.v_list, NULL, -1);
7509 }
7510 }
7511#endif
7512 }
7513}
7514
7515/*
7516 * "matchdelete()" function
7517 */
7518 static void
7519f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7520{
7521#ifdef FEAT_SEARCH_EXTRA
7522 rettv->vval.v_number = match_delete(curwin,
7523 (int)get_tv_number(&argvars[0]), TRUE);
7524#endif
7525}
7526
7527/*
7528 * "matchend()" function
7529 */
7530 static void
7531f_matchend(typval_T *argvars, typval_T *rettv)
7532{
7533 find_some_match(argvars, rettv, 0);
7534}
7535
7536/*
7537 * "matchlist()" function
7538 */
7539 static void
7540f_matchlist(typval_T *argvars, typval_T *rettv)
7541{
7542 find_some_match(argvars, rettv, 3);
7543}
7544
7545/*
7546 * "matchstr()" function
7547 */
7548 static void
7549f_matchstr(typval_T *argvars, typval_T *rettv)
7550{
7551 find_some_match(argvars, rettv, 2);
7552}
7553
7554/*
7555 * "matchstrpos()" function
7556 */
7557 static void
7558f_matchstrpos(typval_T *argvars, typval_T *rettv)
7559{
7560 find_some_match(argvars, rettv, 4);
7561}
7562
7563static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7564
7565 static void
7566max_min(typval_T *argvars, typval_T *rettv, int domax)
7567{
7568 varnumber_T n = 0;
7569 varnumber_T i;
7570 int error = FALSE;
7571
7572 if (argvars[0].v_type == VAR_LIST)
7573 {
7574 list_T *l;
7575 listitem_T *li;
7576
7577 l = argvars[0].vval.v_list;
7578 if (l != NULL)
7579 {
7580 li = l->lv_first;
7581 if (li != NULL)
7582 {
7583 n = get_tv_number_chk(&li->li_tv, &error);
7584 for (;;)
7585 {
7586 li = li->li_next;
7587 if (li == NULL)
7588 break;
7589 i = get_tv_number_chk(&li->li_tv, &error);
7590 if (domax ? i > n : i < n)
7591 n = i;
7592 }
7593 }
7594 }
7595 }
7596 else if (argvars[0].v_type == VAR_DICT)
7597 {
7598 dict_T *d;
7599 int first = TRUE;
7600 hashitem_T *hi;
7601 int todo;
7602
7603 d = argvars[0].vval.v_dict;
7604 if (d != NULL)
7605 {
7606 todo = (int)d->dv_hashtab.ht_used;
7607 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7608 {
7609 if (!HASHITEM_EMPTY(hi))
7610 {
7611 --todo;
7612 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7613 if (first)
7614 {
7615 n = i;
7616 first = FALSE;
7617 }
7618 else if (domax ? i > n : i < n)
7619 n = i;
7620 }
7621 }
7622 }
7623 }
7624 else
7625 EMSG(_(e_listdictarg));
7626 rettv->vval.v_number = error ? 0 : n;
7627}
7628
7629/*
7630 * "max()" function
7631 */
7632 static void
7633f_max(typval_T *argvars, typval_T *rettv)
7634{
7635 max_min(argvars, rettv, TRUE);
7636}
7637
7638/*
7639 * "min()" function
7640 */
7641 static void
7642f_min(typval_T *argvars, typval_T *rettv)
7643{
7644 max_min(argvars, rettv, FALSE);
7645}
7646
7647static int mkdir_recurse(char_u *dir, int prot);
7648
7649/*
7650 * Create the directory in which "dir" is located, and higher levels when
7651 * needed.
7652 */
7653 static int
7654mkdir_recurse(char_u *dir, int prot)
7655{
7656 char_u *p;
7657 char_u *updir;
7658 int r = FAIL;
7659
7660 /* Get end of directory name in "dir".
7661 * We're done when it's "/" or "c:/". */
7662 p = gettail_sep(dir);
7663 if (p <= get_past_head(dir))
7664 return OK;
7665
7666 /* If the directory exists we're done. Otherwise: create it.*/
7667 updir = vim_strnsave(dir, (int)(p - dir));
7668 if (updir == NULL)
7669 return FAIL;
7670 if (mch_isdir(updir))
7671 r = OK;
7672 else if (mkdir_recurse(updir, prot) == OK)
7673 r = vim_mkdir_emsg(updir, prot);
7674 vim_free(updir);
7675 return r;
7676}
7677
7678#ifdef vim_mkdir
7679/*
7680 * "mkdir()" function
7681 */
7682 static void
7683f_mkdir(typval_T *argvars, typval_T *rettv)
7684{
7685 char_u *dir;
7686 char_u buf[NUMBUFLEN];
7687 int prot = 0755;
7688
7689 rettv->vval.v_number = FAIL;
7690 if (check_restricted() || check_secure())
7691 return;
7692
7693 dir = get_tv_string_buf(&argvars[0], buf);
7694 if (*dir == NUL)
7695 rettv->vval.v_number = FAIL;
7696 else
7697 {
7698 if (*gettail(dir) == NUL)
7699 /* remove trailing slashes */
7700 *gettail_sep(dir) = NUL;
7701
7702 if (argvars[1].v_type != VAR_UNKNOWN)
7703 {
7704 if (argvars[2].v_type != VAR_UNKNOWN)
7705 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7706 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7707 mkdir_recurse(dir, prot);
7708 }
7709 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7710 }
7711}
7712#endif
7713
7714/*
7715 * "mode()" function
7716 */
7717 static void
7718f_mode(typval_T *argvars, typval_T *rettv)
7719{
7720 char_u buf[3];
7721
7722 buf[1] = NUL;
7723 buf[2] = NUL;
7724
7725 if (time_for_testing == 93784)
7726 {
7727 /* Testing the two-character code. */
7728 buf[0] = 'x';
7729 buf[1] = '!';
7730 }
7731 else if (VIsual_active)
7732 {
7733 if (VIsual_select)
7734 buf[0] = VIsual_mode + 's' - 'v';
7735 else
7736 buf[0] = VIsual_mode;
7737 }
7738 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7739 || State == CONFIRM)
7740 {
7741 buf[0] = 'r';
7742 if (State == ASKMORE)
7743 buf[1] = 'm';
7744 else if (State == CONFIRM)
7745 buf[1] = '?';
7746 }
7747 else if (State == EXTERNCMD)
7748 buf[0] = '!';
7749 else if (State & INSERT)
7750 {
7751#ifdef FEAT_VREPLACE
7752 if (State & VREPLACE_FLAG)
7753 {
7754 buf[0] = 'R';
7755 buf[1] = 'v';
7756 }
7757 else
7758#endif
7759 if (State & REPLACE_FLAG)
7760 buf[0] = 'R';
7761 else
7762 buf[0] = 'i';
7763 }
7764 else if (State & CMDLINE)
7765 {
7766 buf[0] = 'c';
7767 if (exmode_active)
7768 buf[1] = 'v';
7769 }
7770 else if (exmode_active)
7771 {
7772 buf[0] = 'c';
7773 buf[1] = 'e';
7774 }
7775 else
7776 {
7777 buf[0] = 'n';
7778 if (finish_op)
7779 buf[1] = 'o';
7780 }
7781
7782 /* Clear out the minor mode when the argument is not a non-zero number or
7783 * non-empty string. */
7784 if (!non_zero_arg(&argvars[0]))
7785 buf[1] = NUL;
7786
7787 rettv->vval.v_string = vim_strsave(buf);
7788 rettv->v_type = VAR_STRING;
7789}
7790
7791#if defined(FEAT_MZSCHEME) || defined(PROTO)
7792/*
7793 * "mzeval()" function
7794 */
7795 static void
7796f_mzeval(typval_T *argvars, typval_T *rettv)
7797{
7798 char_u *str;
7799 char_u buf[NUMBUFLEN];
7800
7801 str = get_tv_string_buf(&argvars[0], buf);
7802 do_mzeval(str, rettv);
7803}
7804
7805 void
7806mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
7807{
7808 typval_T argvars[3];
7809
7810 argvars[0].v_type = VAR_STRING;
7811 argvars[0].vval.v_string = name;
7812 copy_tv(args, &argvars[1]);
7813 argvars[2].v_type = VAR_UNKNOWN;
7814 f_call(argvars, rettv);
7815 clear_tv(&argvars[1]);
7816}
7817#endif
7818
7819/*
7820 * "nextnonblank()" function
7821 */
7822 static void
7823f_nextnonblank(typval_T *argvars, typval_T *rettv)
7824{
7825 linenr_T lnum;
7826
7827 for (lnum = get_tv_lnum(argvars); ; ++lnum)
7828 {
7829 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
7830 {
7831 lnum = 0;
7832 break;
7833 }
7834 if (*skipwhite(ml_get(lnum)) != NUL)
7835 break;
7836 }
7837 rettv->vval.v_number = lnum;
7838}
7839
7840/*
7841 * "nr2char()" function
7842 */
7843 static void
7844f_nr2char(typval_T *argvars, typval_T *rettv)
7845{
7846 char_u buf[NUMBUFLEN];
7847
7848#ifdef FEAT_MBYTE
7849 if (has_mbyte)
7850 {
7851 int utf8 = 0;
7852
7853 if (argvars[1].v_type != VAR_UNKNOWN)
7854 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
7855 if (utf8)
7856 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7857 else
7858 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7859 }
7860 else
7861#endif
7862 {
7863 buf[0] = (char_u)get_tv_number(&argvars[0]);
7864 buf[1] = NUL;
7865 }
7866 rettv->v_type = VAR_STRING;
7867 rettv->vval.v_string = vim_strsave(buf);
7868}
7869
7870/*
7871 * "or(expr, expr)" function
7872 */
7873 static void
7874f_or(typval_T *argvars, typval_T *rettv)
7875{
7876 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
7877 | get_tv_number_chk(&argvars[1], NULL);
7878}
7879
7880/*
7881 * "pathshorten()" function
7882 */
7883 static void
7884f_pathshorten(typval_T *argvars, typval_T *rettv)
7885{
7886 char_u *p;
7887
7888 rettv->v_type = VAR_STRING;
7889 p = get_tv_string_chk(&argvars[0]);
7890 if (p == NULL)
7891 rettv->vval.v_string = NULL;
7892 else
7893 {
7894 p = vim_strsave(p);
7895 rettv->vval.v_string = p;
7896 if (p != NULL)
7897 shorten_dir(p);
7898 }
7899}
7900
7901#ifdef FEAT_PERL
7902/*
7903 * "perleval()" function
7904 */
7905 static void
7906f_perleval(typval_T *argvars, typval_T *rettv)
7907{
7908 char_u *str;
7909 char_u buf[NUMBUFLEN];
7910
7911 str = get_tv_string_buf(&argvars[0], buf);
7912 do_perleval(str, rettv);
7913}
7914#endif
7915
7916#ifdef FEAT_FLOAT
7917/*
7918 * "pow()" function
7919 */
7920 static void
7921f_pow(typval_T *argvars, typval_T *rettv)
7922{
7923 float_T fx = 0.0, fy = 0.0;
7924
7925 rettv->v_type = VAR_FLOAT;
7926 if (get_float_arg(argvars, &fx) == OK
7927 && get_float_arg(&argvars[1], &fy) == OK)
7928 rettv->vval.v_float = pow(fx, fy);
7929 else
7930 rettv->vval.v_float = 0.0;
7931}
7932#endif
7933
7934/*
7935 * "prevnonblank()" function
7936 */
7937 static void
7938f_prevnonblank(typval_T *argvars, typval_T *rettv)
7939{
7940 linenr_T lnum;
7941
7942 lnum = get_tv_lnum(argvars);
7943 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7944 lnum = 0;
7945 else
7946 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7947 --lnum;
7948 rettv->vval.v_number = lnum;
7949}
7950
7951/* This dummy va_list is here because:
7952 * - passing a NULL pointer doesn't work when va_list isn't a pointer
7953 * - locally in the function results in a "used before set" warning
7954 * - using va_start() to initialize it gives "function with fixed args" error */
7955static va_list ap;
7956
7957/*
7958 * "printf()" function
7959 */
7960 static void
7961f_printf(typval_T *argvars, typval_T *rettv)
7962{
7963 char_u buf[NUMBUFLEN];
7964 int len;
7965 char_u *s;
7966 int saved_did_emsg = did_emsg;
7967 char *fmt;
7968
7969 rettv->v_type = VAR_STRING;
7970 rettv->vval.v_string = NULL;
7971
7972 /* Get the required length, allocate the buffer and do it for real. */
7973 did_emsg = FALSE;
7974 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
7975 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
7976 if (!did_emsg)
7977 {
7978 s = alloc(len + 1);
7979 if (s != NULL)
7980 {
7981 rettv->vval.v_string = s;
7982 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
7983 }
7984 }
7985 did_emsg |= saved_did_emsg;
7986}
7987
7988/*
7989 * "pumvisible()" function
7990 */
7991 static void
7992f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7993{
7994#ifdef FEAT_INS_EXPAND
7995 if (pum_visible())
7996 rettv->vval.v_number = 1;
7997#endif
7998}
7999
8000#ifdef FEAT_PYTHON3
8001/*
8002 * "py3eval()" function
8003 */
8004 static void
8005f_py3eval(typval_T *argvars, typval_T *rettv)
8006{
8007 char_u *str;
8008 char_u buf[NUMBUFLEN];
8009
8010 str = get_tv_string_buf(&argvars[0], buf);
8011 do_py3eval(str, rettv);
8012}
8013#endif
8014
8015#ifdef FEAT_PYTHON
8016/*
8017 * "pyeval()" function
8018 */
8019 static void
8020f_pyeval(typval_T *argvars, typval_T *rettv)
8021{
8022 char_u *str;
8023 char_u buf[NUMBUFLEN];
8024
8025 str = get_tv_string_buf(&argvars[0], buf);
8026 do_pyeval(str, rettv);
8027}
8028#endif
8029
8030/*
8031 * "range()" function
8032 */
8033 static void
8034f_range(typval_T *argvars, typval_T *rettv)
8035{
8036 varnumber_T start;
8037 varnumber_T end;
8038 varnumber_T stride = 1;
8039 varnumber_T i;
8040 int error = FALSE;
8041
8042 start = get_tv_number_chk(&argvars[0], &error);
8043 if (argvars[1].v_type == VAR_UNKNOWN)
8044 {
8045 end = start - 1;
8046 start = 0;
8047 }
8048 else
8049 {
8050 end = get_tv_number_chk(&argvars[1], &error);
8051 if (argvars[2].v_type != VAR_UNKNOWN)
8052 stride = get_tv_number_chk(&argvars[2], &error);
8053 }
8054
8055 if (error)
8056 return; /* type error; errmsg already given */
8057 if (stride == 0)
8058 EMSG(_("E726: Stride is zero"));
8059 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8060 EMSG(_("E727: Start past end"));
8061 else
8062 {
8063 if (rettv_list_alloc(rettv) == OK)
8064 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8065 if (list_append_number(rettv->vval.v_list,
8066 (varnumber_T)i) == FAIL)
8067 break;
8068 }
8069}
8070
8071/*
8072 * "readfile()" function
8073 */
8074 static void
8075f_readfile(typval_T *argvars, typval_T *rettv)
8076{
8077 int binary = FALSE;
8078 int failed = FALSE;
8079 char_u *fname;
8080 FILE *fd;
8081 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8082 int io_size = sizeof(buf);
8083 int readlen; /* size of last fread() */
8084 char_u *prev = NULL; /* previously read bytes, if any */
8085 long prevlen = 0; /* length of data in prev */
8086 long prevsize = 0; /* size of prev buffer */
8087 long maxline = MAXLNUM;
8088 long cnt = 0;
8089 char_u *p; /* position in buf */
8090 char_u *start; /* start of current line */
8091
8092 if (argvars[1].v_type != VAR_UNKNOWN)
8093 {
8094 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8095 binary = TRUE;
8096 if (argvars[2].v_type != VAR_UNKNOWN)
8097 maxline = (long)get_tv_number(&argvars[2]);
8098 }
8099
8100 if (rettv_list_alloc(rettv) == FAIL)
8101 return;
8102
8103 /* Always open the file in binary mode, library functions have a mind of
8104 * their own about CR-LF conversion. */
8105 fname = get_tv_string(&argvars[0]);
8106 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8107 {
8108 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8109 return;
8110 }
8111
8112 while (cnt < maxline || maxline < 0)
8113 {
8114 readlen = (int)fread(buf, 1, io_size, fd);
8115
8116 /* This for loop processes what was read, but is also entered at end
8117 * of file so that either:
8118 * - an incomplete line gets written
8119 * - a "binary" file gets an empty line at the end if it ends in a
8120 * newline. */
8121 for (p = buf, start = buf;
8122 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8123 ++p)
8124 {
8125 if (*p == '\n' || readlen <= 0)
8126 {
8127 listitem_T *li;
8128 char_u *s = NULL;
8129 long_u len = p - start;
8130
8131 /* Finished a line. Remove CRs before NL. */
8132 if (readlen > 0 && !binary)
8133 {
8134 while (len > 0 && start[len - 1] == '\r')
8135 --len;
8136 /* removal may cross back to the "prev" string */
8137 if (len == 0)
8138 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8139 --prevlen;
8140 }
8141 if (prevlen == 0)
8142 s = vim_strnsave(start, (int)len);
8143 else
8144 {
8145 /* Change "prev" buffer to be the right size. This way
8146 * the bytes are only copied once, and very long lines are
8147 * allocated only once. */
8148 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8149 {
8150 mch_memmove(s + prevlen, start, len);
8151 s[prevlen + len] = NUL;
8152 prev = NULL; /* the list will own the string */
8153 prevlen = prevsize = 0;
8154 }
8155 }
8156 if (s == NULL)
8157 {
8158 do_outofmem_msg((long_u) prevlen + len + 1);
8159 failed = TRUE;
8160 break;
8161 }
8162
8163 if ((li = listitem_alloc()) == NULL)
8164 {
8165 vim_free(s);
8166 failed = TRUE;
8167 break;
8168 }
8169 li->li_tv.v_type = VAR_STRING;
8170 li->li_tv.v_lock = 0;
8171 li->li_tv.vval.v_string = s;
8172 list_append(rettv->vval.v_list, li);
8173
8174 start = p + 1; /* step over newline */
8175 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8176 break;
8177 }
8178 else if (*p == NUL)
8179 *p = '\n';
8180#ifdef FEAT_MBYTE
8181 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8182 * when finding the BF and check the previous two bytes. */
8183 else if (*p == 0xbf && enc_utf8 && !binary)
8184 {
8185 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8186 * + 1, these may be in the "prev" string. */
8187 char_u back1 = p >= buf + 1 ? p[-1]
8188 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8189 char_u back2 = p >= buf + 2 ? p[-2]
8190 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8191 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8192
8193 if (back2 == 0xef && back1 == 0xbb)
8194 {
8195 char_u *dest = p - 2;
8196
8197 /* Usually a BOM is at the beginning of a file, and so at
8198 * the beginning of a line; then we can just step over it.
8199 */
8200 if (start == dest)
8201 start = p + 1;
8202 else
8203 {
8204 /* have to shuffle buf to close gap */
8205 int adjust_prevlen = 0;
8206
8207 if (dest < buf)
8208 {
8209 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8210 dest = buf;
8211 }
8212 if (readlen > p - buf + 1)
8213 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8214 readlen -= 3 - adjust_prevlen;
8215 prevlen -= adjust_prevlen;
8216 p = dest - 1;
8217 }
8218 }
8219 }
8220#endif
8221 } /* for */
8222
8223 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8224 break;
8225 if (start < p)
8226 {
8227 /* There's part of a line in buf, store it in "prev". */
8228 if (p - start + prevlen >= prevsize)
8229 {
8230 /* need bigger "prev" buffer */
8231 char_u *newprev;
8232
8233 /* A common use case is ordinary text files and "prev" gets a
8234 * fragment of a line, so the first allocation is made
8235 * small, to avoid repeatedly 'allocing' large and
8236 * 'reallocing' small. */
8237 if (prevsize == 0)
8238 prevsize = (long)(p - start);
8239 else
8240 {
8241 long grow50pc = (prevsize * 3) / 2;
8242 long growmin = (long)((p - start) * 2 + prevlen);
8243 prevsize = grow50pc > growmin ? grow50pc : growmin;
8244 }
8245 newprev = prev == NULL ? alloc(prevsize)
8246 : vim_realloc(prev, prevsize);
8247 if (newprev == NULL)
8248 {
8249 do_outofmem_msg((long_u)prevsize);
8250 failed = TRUE;
8251 break;
8252 }
8253 prev = newprev;
8254 }
8255 /* Add the line part to end of "prev". */
8256 mch_memmove(prev + prevlen, start, p - start);
8257 prevlen += (long)(p - start);
8258 }
8259 } /* while */
8260
8261 /*
8262 * For a negative line count use only the lines at the end of the file,
8263 * free the rest.
8264 */
8265 if (!failed && maxline < 0)
8266 while (cnt > -maxline)
8267 {
8268 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8269 --cnt;
8270 }
8271
8272 if (failed)
8273 {
8274 list_free(rettv->vval.v_list);
8275 /* readfile doc says an empty list is returned on error */
8276 rettv->vval.v_list = list_alloc();
8277 }
8278
8279 vim_free(prev);
8280 fclose(fd);
8281}
8282
8283#if defined(FEAT_RELTIME)
8284static int list2proftime(typval_T *arg, proftime_T *tm);
8285
8286/*
8287 * Convert a List to proftime_T.
8288 * Return FAIL when there is something wrong.
8289 */
8290 static int
8291list2proftime(typval_T *arg, proftime_T *tm)
8292{
8293 long n1, n2;
8294 int error = FALSE;
8295
8296 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8297 || arg->vval.v_list->lv_len != 2)
8298 return FAIL;
8299 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8300 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8301# ifdef WIN3264
8302 tm->HighPart = n1;
8303 tm->LowPart = n2;
8304# else
8305 tm->tv_sec = n1;
8306 tm->tv_usec = n2;
8307# endif
8308 return error ? FAIL : OK;
8309}
8310#endif /* FEAT_RELTIME */
8311
8312/*
8313 * "reltime()" function
8314 */
8315 static void
8316f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8317{
8318#ifdef FEAT_RELTIME
8319 proftime_T res;
8320 proftime_T start;
8321
8322 if (argvars[0].v_type == VAR_UNKNOWN)
8323 {
8324 /* No arguments: get current time. */
8325 profile_start(&res);
8326 }
8327 else if (argvars[1].v_type == VAR_UNKNOWN)
8328 {
8329 if (list2proftime(&argvars[0], &res) == FAIL)
8330 return;
8331 profile_end(&res);
8332 }
8333 else
8334 {
8335 /* Two arguments: compute the difference. */
8336 if (list2proftime(&argvars[0], &start) == FAIL
8337 || list2proftime(&argvars[1], &res) == FAIL)
8338 return;
8339 profile_sub(&res, &start);
8340 }
8341
8342 if (rettv_list_alloc(rettv) == OK)
8343 {
8344 long n1, n2;
8345
8346# ifdef WIN3264
8347 n1 = res.HighPart;
8348 n2 = res.LowPart;
8349# else
8350 n1 = res.tv_sec;
8351 n2 = res.tv_usec;
8352# endif
8353 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8354 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8355 }
8356#endif
8357}
8358
8359#ifdef FEAT_FLOAT
8360/*
8361 * "reltimefloat()" function
8362 */
8363 static void
8364f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8365{
8366# ifdef FEAT_RELTIME
8367 proftime_T tm;
8368# endif
8369
8370 rettv->v_type = VAR_FLOAT;
8371 rettv->vval.v_float = 0;
8372# ifdef FEAT_RELTIME
8373 if (list2proftime(&argvars[0], &tm) == OK)
8374 rettv->vval.v_float = profile_float(&tm);
8375# endif
8376}
8377#endif
8378
8379/*
8380 * "reltimestr()" function
8381 */
8382 static void
8383f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8384{
8385#ifdef FEAT_RELTIME
8386 proftime_T tm;
8387#endif
8388
8389 rettv->v_type = VAR_STRING;
8390 rettv->vval.v_string = NULL;
8391#ifdef FEAT_RELTIME
8392 if (list2proftime(&argvars[0], &tm) == OK)
8393 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8394#endif
8395}
8396
8397#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8398static void make_connection(void);
8399static int check_connection(void);
8400
8401 static void
8402make_connection(void)
8403{
8404 if (X_DISPLAY == NULL
8405# ifdef FEAT_GUI
8406 && !gui.in_use
8407# endif
8408 )
8409 {
8410 x_force_connect = TRUE;
8411 setup_term_clip();
8412 x_force_connect = FALSE;
8413 }
8414}
8415
8416 static int
8417check_connection(void)
8418{
8419 make_connection();
8420 if (X_DISPLAY == NULL)
8421 {
8422 EMSG(_("E240: No connection to Vim server"));
8423 return FAIL;
8424 }
8425 return OK;
8426}
8427#endif
8428
8429#ifdef FEAT_CLIENTSERVER
8430 static void
8431remote_common(typval_T *argvars, typval_T *rettv, int expr)
8432{
8433 char_u *server_name;
8434 char_u *keys;
8435 char_u *r = NULL;
8436 char_u buf[NUMBUFLEN];
8437# ifdef WIN32
8438 HWND w;
8439# else
8440 Window w;
8441# endif
8442
8443 if (check_restricted() || check_secure())
8444 return;
8445
8446# ifdef FEAT_X11
8447 if (check_connection() == FAIL)
8448 return;
8449# endif
8450
8451 server_name = get_tv_string_chk(&argvars[0]);
8452 if (server_name == NULL)
8453 return; /* type error; errmsg already given */
8454 keys = get_tv_string_buf(&argvars[1], buf);
8455# ifdef WIN32
8456 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
8457# else
8458 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
8459 < 0)
8460# endif
8461 {
8462 if (r != NULL)
8463 EMSG(r); /* sending worked but evaluation failed */
8464 else
8465 EMSG2(_("E241: Unable to send to %s"), server_name);
8466 return;
8467 }
8468
8469 rettv->vval.v_string = r;
8470
8471 if (argvars[2].v_type != VAR_UNKNOWN)
8472 {
8473 dictitem_T v;
8474 char_u str[30];
8475 char_u *idvar;
8476
8477 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8478 v.di_tv.v_type = VAR_STRING;
8479 v.di_tv.vval.v_string = vim_strsave(str);
8480 idvar = get_tv_string_chk(&argvars[2]);
8481 if (idvar != NULL)
8482 set_var(idvar, &v.di_tv, FALSE);
8483 vim_free(v.di_tv.vval.v_string);
8484 }
8485}
8486#endif
8487
8488/*
8489 * "remote_expr()" function
8490 */
8491 static void
8492f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8493{
8494 rettv->v_type = VAR_STRING;
8495 rettv->vval.v_string = NULL;
8496#ifdef FEAT_CLIENTSERVER
8497 remote_common(argvars, rettv, TRUE);
8498#endif
8499}
8500
8501/*
8502 * "remote_foreground()" function
8503 */
8504 static void
8505f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8506{
8507#ifdef FEAT_CLIENTSERVER
8508# ifdef WIN32
8509 /* On Win32 it's done in this application. */
8510 {
8511 char_u *server_name = get_tv_string_chk(&argvars[0]);
8512
8513 if (server_name != NULL)
8514 serverForeground(server_name);
8515 }
8516# else
8517 /* Send a foreground() expression to the server. */
8518 argvars[1].v_type = VAR_STRING;
8519 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8520 argvars[2].v_type = VAR_UNKNOWN;
8521 remote_common(argvars, rettv, TRUE);
8522 vim_free(argvars[1].vval.v_string);
8523# endif
8524#endif
8525}
8526
8527 static void
8528f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8529{
8530#ifdef FEAT_CLIENTSERVER
8531 dictitem_T v;
8532 char_u *s = NULL;
8533# ifdef WIN32
8534 long_u n = 0;
8535# endif
8536 char_u *serverid;
8537
8538 if (check_restricted() || check_secure())
8539 {
8540 rettv->vval.v_number = -1;
8541 return;
8542 }
8543 serverid = get_tv_string_chk(&argvars[0]);
8544 if (serverid == NULL)
8545 {
8546 rettv->vval.v_number = -1;
8547 return; /* type error; errmsg already given */
8548 }
8549# ifdef WIN32
8550 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8551 if (n == 0)
8552 rettv->vval.v_number = -1;
8553 else
8554 {
8555 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
8556 rettv->vval.v_number = (s != NULL);
8557 }
8558# else
8559 if (check_connection() == FAIL)
8560 return;
8561
8562 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8563 serverStrToWin(serverid), &s);
8564# endif
8565
8566 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8567 {
8568 char_u *retvar;
8569
8570 v.di_tv.v_type = VAR_STRING;
8571 v.di_tv.vval.v_string = vim_strsave(s);
8572 retvar = get_tv_string_chk(&argvars[1]);
8573 if (retvar != NULL)
8574 set_var(retvar, &v.di_tv, FALSE);
8575 vim_free(v.di_tv.vval.v_string);
8576 }
8577#else
8578 rettv->vval.v_number = -1;
8579#endif
8580}
8581
8582 static void
8583f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8584{
8585 char_u *r = NULL;
8586
8587#ifdef FEAT_CLIENTSERVER
8588 char_u *serverid = get_tv_string_chk(&argvars[0]);
8589
8590 if (serverid != NULL && !check_restricted() && !check_secure())
8591 {
8592# ifdef WIN32
8593 /* The server's HWND is encoded in the 'id' parameter */
8594 long_u n = 0;
8595
8596 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8597 if (n != 0)
8598 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
8599 if (r == NULL)
8600# else
8601 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
8602 serverStrToWin(serverid), &r, FALSE) < 0)
8603# endif
8604 EMSG(_("E277: Unable to read a server reply"));
8605 }
8606#endif
8607 rettv->v_type = VAR_STRING;
8608 rettv->vval.v_string = r;
8609}
8610
8611/*
8612 * "remote_send()" function
8613 */
8614 static void
8615f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8616{
8617 rettv->v_type = VAR_STRING;
8618 rettv->vval.v_string = NULL;
8619#ifdef FEAT_CLIENTSERVER
8620 remote_common(argvars, rettv, FALSE);
8621#endif
8622}
8623
8624/*
8625 * "remove()" function
8626 */
8627 static void
8628f_remove(typval_T *argvars, typval_T *rettv)
8629{
8630 list_T *l;
8631 listitem_T *item, *item2;
8632 listitem_T *li;
8633 long idx;
8634 long end;
8635 char_u *key;
8636 dict_T *d;
8637 dictitem_T *di;
8638 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8639
8640 if (argvars[0].v_type == VAR_DICT)
8641 {
8642 if (argvars[2].v_type != VAR_UNKNOWN)
8643 EMSG2(_(e_toomanyarg), "remove()");
8644 else if ((d = argvars[0].vval.v_dict) != NULL
8645 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8646 {
8647 key = get_tv_string_chk(&argvars[1]);
8648 if (key != NULL)
8649 {
8650 di = dict_find(d, key, -1);
8651 if (di == NULL)
8652 EMSG2(_(e_dictkey), key);
8653 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8654 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8655 {
8656 *rettv = di->di_tv;
8657 init_tv(&di->di_tv);
8658 dictitem_remove(d, di);
8659 }
8660 }
8661 }
8662 }
8663 else if (argvars[0].v_type != VAR_LIST)
8664 EMSG2(_(e_listdictarg), "remove()");
8665 else if ((l = argvars[0].vval.v_list) != NULL
8666 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
8667 {
8668 int error = FALSE;
8669
8670 idx = (long)get_tv_number_chk(&argvars[1], &error);
8671 if (error)
8672 ; /* type error: do nothing, errmsg already given */
8673 else if ((item = list_find(l, idx)) == NULL)
8674 EMSGN(_(e_listidx), idx);
8675 else
8676 {
8677 if (argvars[2].v_type == VAR_UNKNOWN)
8678 {
8679 /* Remove one item, return its value. */
8680 vimlist_remove(l, item, item);
8681 *rettv = item->li_tv;
8682 vim_free(item);
8683 }
8684 else
8685 {
8686 /* Remove range of items, return list with values. */
8687 end = (long)get_tv_number_chk(&argvars[2], &error);
8688 if (error)
8689 ; /* type error: do nothing */
8690 else if ((item2 = list_find(l, end)) == NULL)
8691 EMSGN(_(e_listidx), end);
8692 else
8693 {
8694 int cnt = 0;
8695
8696 for (li = item; li != NULL; li = li->li_next)
8697 {
8698 ++cnt;
8699 if (li == item2)
8700 break;
8701 }
8702 if (li == NULL) /* didn't find "item2" after "item" */
8703 EMSG(_(e_invrange));
8704 else
8705 {
8706 vimlist_remove(l, item, item2);
8707 if (rettv_list_alloc(rettv) == OK)
8708 {
8709 l = rettv->vval.v_list;
8710 l->lv_first = item;
8711 l->lv_last = item2;
8712 item->li_prev = NULL;
8713 item2->li_next = NULL;
8714 l->lv_len = cnt;
8715 }
8716 }
8717 }
8718 }
8719 }
8720 }
8721}
8722
8723/*
8724 * "rename({from}, {to})" function
8725 */
8726 static void
8727f_rename(typval_T *argvars, typval_T *rettv)
8728{
8729 char_u buf[NUMBUFLEN];
8730
8731 if (check_restricted() || check_secure())
8732 rettv->vval.v_number = -1;
8733 else
8734 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
8735 get_tv_string_buf(&argvars[1], buf));
8736}
8737
8738/*
8739 * "repeat()" function
8740 */
8741 static void
8742f_repeat(typval_T *argvars, typval_T *rettv)
8743{
8744 char_u *p;
8745 int n;
8746 int slen;
8747 int len;
8748 char_u *r;
8749 int i;
8750
8751 n = (int)get_tv_number(&argvars[1]);
8752 if (argvars[0].v_type == VAR_LIST)
8753 {
8754 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
8755 while (n-- > 0)
8756 if (list_extend(rettv->vval.v_list,
8757 argvars[0].vval.v_list, NULL) == FAIL)
8758 break;
8759 }
8760 else
8761 {
8762 p = get_tv_string(&argvars[0]);
8763 rettv->v_type = VAR_STRING;
8764 rettv->vval.v_string = NULL;
8765
8766 slen = (int)STRLEN(p);
8767 len = slen * n;
8768 if (len <= 0)
8769 return;
8770
8771 r = alloc(len + 1);
8772 if (r != NULL)
8773 {
8774 for (i = 0; i < n; i++)
8775 mch_memmove(r + i * slen, p, (size_t)slen);
8776 r[len] = NUL;
8777 }
8778
8779 rettv->vval.v_string = r;
8780 }
8781}
8782
8783/*
8784 * "resolve()" function
8785 */
8786 static void
8787f_resolve(typval_T *argvars, typval_T *rettv)
8788{
8789 char_u *p;
8790#ifdef HAVE_READLINK
8791 char_u *buf = NULL;
8792#endif
8793
8794 p = get_tv_string(&argvars[0]);
8795#ifdef FEAT_SHORTCUT
8796 {
8797 char_u *v = NULL;
8798
8799 v = mch_resolve_shortcut(p);
8800 if (v != NULL)
8801 rettv->vval.v_string = v;
8802 else
8803 rettv->vval.v_string = vim_strsave(p);
8804 }
8805#else
8806# ifdef HAVE_READLINK
8807 {
8808 char_u *cpy;
8809 int len;
8810 char_u *remain = NULL;
8811 char_u *q;
8812 int is_relative_to_current = FALSE;
8813 int has_trailing_pathsep = FALSE;
8814 int limit = 100;
8815
8816 p = vim_strsave(p);
8817
8818 if (p[0] == '.' && (vim_ispathsep(p[1])
8819 || (p[1] == '.' && (vim_ispathsep(p[2])))))
8820 is_relative_to_current = TRUE;
8821
8822 len = STRLEN(p);
8823 if (len > 0 && after_pathsep(p, p + len))
8824 {
8825 has_trailing_pathsep = TRUE;
8826 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
8827 }
8828
8829 q = getnextcomp(p);
8830 if (*q != NUL)
8831 {
8832 /* Separate the first path component in "p", and keep the
8833 * remainder (beginning with the path separator). */
8834 remain = vim_strsave(q - 1);
8835 q[-1] = NUL;
8836 }
8837
8838 buf = alloc(MAXPATHL + 1);
8839 if (buf == NULL)
8840 goto fail;
8841
8842 for (;;)
8843 {
8844 for (;;)
8845 {
8846 len = readlink((char *)p, (char *)buf, MAXPATHL);
8847 if (len <= 0)
8848 break;
8849 buf[len] = NUL;
8850
8851 if (limit-- == 0)
8852 {
8853 vim_free(p);
8854 vim_free(remain);
8855 EMSG(_("E655: Too many symbolic links (cycle?)"));
8856 rettv->vval.v_string = NULL;
8857 goto fail;
8858 }
8859
8860 /* Ensure that the result will have a trailing path separator
8861 * if the argument has one. */
8862 if (remain == NULL && has_trailing_pathsep)
8863 add_pathsep(buf);
8864
8865 /* Separate the first path component in the link value and
8866 * concatenate the remainders. */
8867 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
8868 if (*q != NUL)
8869 {
8870 if (remain == NULL)
8871 remain = vim_strsave(q - 1);
8872 else
8873 {
8874 cpy = concat_str(q - 1, remain);
8875 if (cpy != NULL)
8876 {
8877 vim_free(remain);
8878 remain = cpy;
8879 }
8880 }
8881 q[-1] = NUL;
8882 }
8883
8884 q = gettail(p);
8885 if (q > p && *q == NUL)
8886 {
8887 /* Ignore trailing path separator. */
8888 q[-1] = NUL;
8889 q = gettail(p);
8890 }
8891 if (q > p && !mch_isFullName(buf))
8892 {
8893 /* symlink is relative to directory of argument */
8894 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
8895 if (cpy != NULL)
8896 {
8897 STRCPY(cpy, p);
8898 STRCPY(gettail(cpy), buf);
8899 vim_free(p);
8900 p = cpy;
8901 }
8902 }
8903 else
8904 {
8905 vim_free(p);
8906 p = vim_strsave(buf);
8907 }
8908 }
8909
8910 if (remain == NULL)
8911 break;
8912
8913 /* Append the first path component of "remain" to "p". */
8914 q = getnextcomp(remain + 1);
8915 len = q - remain - (*q != NUL);
8916 cpy = vim_strnsave(p, STRLEN(p) + len);
8917 if (cpy != NULL)
8918 {
8919 STRNCAT(cpy, remain, len);
8920 vim_free(p);
8921 p = cpy;
8922 }
8923 /* Shorten "remain". */
8924 if (*q != NUL)
8925 STRMOVE(remain, q - 1);
8926 else
8927 {
8928 vim_free(remain);
8929 remain = NULL;
8930 }
8931 }
8932
8933 /* If the result is a relative path name, make it explicitly relative to
8934 * the current directory if and only if the argument had this form. */
8935 if (!vim_ispathsep(*p))
8936 {
8937 if (is_relative_to_current
8938 && *p != NUL
8939 && !(p[0] == '.'
8940 && (p[1] == NUL
8941 || vim_ispathsep(p[1])
8942 || (p[1] == '.'
8943 && (p[2] == NUL
8944 || vim_ispathsep(p[2]))))))
8945 {
8946 /* Prepend "./". */
8947 cpy = concat_str((char_u *)"./", p);
8948 if (cpy != NULL)
8949 {
8950 vim_free(p);
8951 p = cpy;
8952 }
8953 }
8954 else if (!is_relative_to_current)
8955 {
8956 /* Strip leading "./". */
8957 q = p;
8958 while (q[0] == '.' && vim_ispathsep(q[1]))
8959 q += 2;
8960 if (q > p)
8961 STRMOVE(p, p + 2);
8962 }
8963 }
8964
8965 /* Ensure that the result will have no trailing path separator
8966 * if the argument had none. But keep "/" or "//". */
8967 if (!has_trailing_pathsep)
8968 {
8969 q = p + STRLEN(p);
8970 if (after_pathsep(p, q))
8971 *gettail_sep(p) = NUL;
8972 }
8973
8974 rettv->vval.v_string = p;
8975 }
8976# else
8977 rettv->vval.v_string = vim_strsave(p);
8978# endif
8979#endif
8980
8981 simplify_filename(rettv->vval.v_string);
8982
8983#ifdef HAVE_READLINK
8984fail:
8985 vim_free(buf);
8986#endif
8987 rettv->v_type = VAR_STRING;
8988}
8989
8990/*
8991 * "reverse({list})" function
8992 */
8993 static void
8994f_reverse(typval_T *argvars, typval_T *rettv)
8995{
8996 list_T *l;
8997 listitem_T *li, *ni;
8998
8999 if (argvars[0].v_type != VAR_LIST)
9000 EMSG2(_(e_listarg), "reverse()");
9001 else if ((l = argvars[0].vval.v_list) != NULL
9002 && !tv_check_lock(l->lv_lock,
9003 (char_u *)N_("reverse() argument"), TRUE))
9004 {
9005 li = l->lv_last;
9006 l->lv_first = l->lv_last = NULL;
9007 l->lv_len = 0;
9008 while (li != NULL)
9009 {
9010 ni = li->li_prev;
9011 list_append(l, li);
9012 li = ni;
9013 }
9014 rettv->vval.v_list = l;
9015 rettv->v_type = VAR_LIST;
9016 ++l->lv_refcount;
9017 l->lv_idx = l->lv_len - l->lv_idx - 1;
9018 }
9019}
9020
9021#define SP_NOMOVE 0x01 /* don't move cursor */
9022#define SP_REPEAT 0x02 /* repeat to find outer pair */
9023#define SP_RETCOUNT 0x04 /* return matchcount */
9024#define SP_SETPCMARK 0x08 /* set previous context mark */
9025#define SP_START 0x10 /* accept match at start position */
9026#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9027#define SP_END 0x40 /* leave cursor at end of match */
9028#define SP_COLUMN 0x80 /* start at cursor column */
9029
9030static int get_search_arg(typval_T *varp, int *flagsp);
9031
9032/*
9033 * Get flags for a search function.
9034 * Possibly sets "p_ws".
9035 * Returns BACKWARD, FORWARD or zero (for an error).
9036 */
9037 static int
9038get_search_arg(typval_T *varp, int *flagsp)
9039{
9040 int dir = FORWARD;
9041 char_u *flags;
9042 char_u nbuf[NUMBUFLEN];
9043 int mask;
9044
9045 if (varp->v_type != VAR_UNKNOWN)
9046 {
9047 flags = get_tv_string_buf_chk(varp, nbuf);
9048 if (flags == NULL)
9049 return 0; /* type error; errmsg already given */
9050 while (*flags != NUL)
9051 {
9052 switch (*flags)
9053 {
9054 case 'b': dir = BACKWARD; break;
9055 case 'w': p_ws = TRUE; break;
9056 case 'W': p_ws = FALSE; break;
9057 default: mask = 0;
9058 if (flagsp != NULL)
9059 switch (*flags)
9060 {
9061 case 'c': mask = SP_START; break;
9062 case 'e': mask = SP_END; break;
9063 case 'm': mask = SP_RETCOUNT; break;
9064 case 'n': mask = SP_NOMOVE; break;
9065 case 'p': mask = SP_SUBPAT; break;
9066 case 'r': mask = SP_REPEAT; break;
9067 case 's': mask = SP_SETPCMARK; break;
9068 case 'z': mask = SP_COLUMN; break;
9069 }
9070 if (mask == 0)
9071 {
9072 EMSG2(_(e_invarg2), flags);
9073 dir = 0;
9074 }
9075 else
9076 *flagsp |= mask;
9077 }
9078 if (dir == 0)
9079 break;
9080 ++flags;
9081 }
9082 }
9083 return dir;
9084}
9085
9086/*
9087 * Shared by search() and searchpos() functions.
9088 */
9089 static int
9090search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9091{
9092 int flags;
9093 char_u *pat;
9094 pos_T pos;
9095 pos_T save_cursor;
9096 int save_p_ws = p_ws;
9097 int dir;
9098 int retval = 0; /* default: FAIL */
9099 long lnum_stop = 0;
9100 proftime_T tm;
9101#ifdef FEAT_RELTIME
9102 long time_limit = 0;
9103#endif
9104 int options = SEARCH_KEEP;
9105 int subpatnum;
9106
9107 pat = get_tv_string(&argvars[0]);
9108 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9109 if (dir == 0)
9110 goto theend;
9111 flags = *flagsp;
9112 if (flags & SP_START)
9113 options |= SEARCH_START;
9114 if (flags & SP_END)
9115 options |= SEARCH_END;
9116 if (flags & SP_COLUMN)
9117 options |= SEARCH_COL;
9118
9119 /* Optional arguments: line number to stop searching and timeout. */
9120 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9121 {
9122 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9123 if (lnum_stop < 0)
9124 goto theend;
9125#ifdef FEAT_RELTIME
9126 if (argvars[3].v_type != VAR_UNKNOWN)
9127 {
9128 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9129 if (time_limit < 0)
9130 goto theend;
9131 }
9132#endif
9133 }
9134
9135#ifdef FEAT_RELTIME
9136 /* Set the time limit, if there is one. */
9137 profile_setlimit(time_limit, &tm);
9138#endif
9139
9140 /*
9141 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9142 * Check to make sure only those flags are set.
9143 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9144 * flags cannot be set. Check for that condition also.
9145 */
9146 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9147 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9148 {
9149 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9150 goto theend;
9151 }
9152
9153 pos = save_cursor = curwin->w_cursor;
9154 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9155 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
9156 if (subpatnum != FAIL)
9157 {
9158 if (flags & SP_SUBPAT)
9159 retval = subpatnum;
9160 else
9161 retval = pos.lnum;
9162 if (flags & SP_SETPCMARK)
9163 setpcmark();
9164 curwin->w_cursor = pos;
9165 if (match_pos != NULL)
9166 {
9167 /* Store the match cursor position */
9168 match_pos->lnum = pos.lnum;
9169 match_pos->col = pos.col + 1;
9170 }
9171 /* "/$" will put the cursor after the end of the line, may need to
9172 * correct that here */
9173 check_cursor();
9174 }
9175
9176 /* If 'n' flag is used: restore cursor position. */
9177 if (flags & SP_NOMOVE)
9178 curwin->w_cursor = save_cursor;
9179 else
9180 curwin->w_set_curswant = TRUE;
9181theend:
9182 p_ws = save_p_ws;
9183
9184 return retval;
9185}
9186
9187#ifdef FEAT_FLOAT
9188
9189/*
9190 * round() is not in C90, use ceil() or floor() instead.
9191 */
9192 float_T
9193vim_round(float_T f)
9194{
9195 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9196}
9197
9198/*
9199 * "round({float})" function
9200 */
9201 static void
9202f_round(typval_T *argvars, typval_T *rettv)
9203{
9204 float_T f = 0.0;
9205
9206 rettv->v_type = VAR_FLOAT;
9207 if (get_float_arg(argvars, &f) == OK)
9208 rettv->vval.v_float = vim_round(f);
9209 else
9210 rettv->vval.v_float = 0.0;
9211}
9212#endif
9213
9214/*
9215 * "screenattr()" function
9216 */
9217 static void
9218f_screenattr(typval_T *argvars, typval_T *rettv)
9219{
9220 int row;
9221 int col;
9222 int c;
9223
9224 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9225 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9226 if (row < 0 || row >= screen_Rows
9227 || col < 0 || col >= screen_Columns)
9228 c = -1;
9229 else
9230 c = ScreenAttrs[LineOffset[row] + col];
9231 rettv->vval.v_number = c;
9232}
9233
9234/*
9235 * "screenchar()" function
9236 */
9237 static void
9238f_screenchar(typval_T *argvars, typval_T *rettv)
9239{
9240 int row;
9241 int col;
9242 int off;
9243 int c;
9244
9245 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9246 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9247 if (row < 0 || row >= screen_Rows
9248 || col < 0 || col >= screen_Columns)
9249 c = -1;
9250 else
9251 {
9252 off = LineOffset[row] + col;
9253#ifdef FEAT_MBYTE
9254 if (enc_utf8 && ScreenLinesUC[off] != 0)
9255 c = ScreenLinesUC[off];
9256 else
9257#endif
9258 c = ScreenLines[off];
9259 }
9260 rettv->vval.v_number = c;
9261}
9262
9263/*
9264 * "screencol()" function
9265 *
9266 * First column is 1 to be consistent with virtcol().
9267 */
9268 static void
9269f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9270{
9271 rettv->vval.v_number = screen_screencol() + 1;
9272}
9273
9274/*
9275 * "screenrow()" function
9276 */
9277 static void
9278f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9279{
9280 rettv->vval.v_number = screen_screenrow() + 1;
9281}
9282
9283/*
9284 * "search()" function
9285 */
9286 static void
9287f_search(typval_T *argvars, typval_T *rettv)
9288{
9289 int flags = 0;
9290
9291 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9292}
9293
9294/*
9295 * "searchdecl()" function
9296 */
9297 static void
9298f_searchdecl(typval_T *argvars, typval_T *rettv)
9299{
9300 int locally = 1;
9301 int thisblock = 0;
9302 int error = FALSE;
9303 char_u *name;
9304
9305 rettv->vval.v_number = 1; /* default: FAIL */
9306
9307 name = get_tv_string_chk(&argvars[0]);
9308 if (argvars[1].v_type != VAR_UNKNOWN)
9309 {
9310 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9311 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9312 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9313 }
9314 if (!error && name != NULL)
9315 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9316 locally, thisblock, SEARCH_KEEP) == FAIL;
9317}
9318
9319/*
9320 * Used by searchpair() and searchpairpos()
9321 */
9322 static int
9323searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9324{
9325 char_u *spat, *mpat, *epat;
9326 char_u *skip;
9327 int save_p_ws = p_ws;
9328 int dir;
9329 int flags = 0;
9330 char_u nbuf1[NUMBUFLEN];
9331 char_u nbuf2[NUMBUFLEN];
9332 char_u nbuf3[NUMBUFLEN];
9333 int retval = 0; /* default: FAIL */
9334 long lnum_stop = 0;
9335 long time_limit = 0;
9336
9337 /* Get the three pattern arguments: start, middle, end. */
9338 spat = get_tv_string_chk(&argvars[0]);
9339 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9340 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9341 if (spat == NULL || mpat == NULL || epat == NULL)
9342 goto theend; /* type error */
9343
9344 /* Handle the optional fourth argument: flags */
9345 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9346 if (dir == 0)
9347 goto theend;
9348
9349 /* Don't accept SP_END or SP_SUBPAT.
9350 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9351 */
9352 if ((flags & (SP_END | SP_SUBPAT)) != 0
9353 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9354 {
9355 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9356 goto theend;
9357 }
9358
9359 /* Using 'r' implies 'W', otherwise it doesn't work. */
9360 if (flags & SP_REPEAT)
9361 p_ws = FALSE;
9362
9363 /* Optional fifth argument: skip expression */
9364 if (argvars[3].v_type == VAR_UNKNOWN
9365 || argvars[4].v_type == VAR_UNKNOWN)
9366 skip = (char_u *)"";
9367 else
9368 {
9369 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
9370 if (argvars[5].v_type != VAR_UNKNOWN)
9371 {
9372 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9373 if (lnum_stop < 0)
9374 goto theend;
9375#ifdef FEAT_RELTIME
9376 if (argvars[6].v_type != VAR_UNKNOWN)
9377 {
9378 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9379 if (time_limit < 0)
9380 goto theend;
9381 }
9382#endif
9383 }
9384 }
9385 if (skip == NULL)
9386 goto theend; /* type error */
9387
9388 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9389 match_pos, lnum_stop, time_limit);
9390
9391theend:
9392 p_ws = save_p_ws;
9393
9394 return retval;
9395}
9396
9397/*
9398 * "searchpair()" function
9399 */
9400 static void
9401f_searchpair(typval_T *argvars, typval_T *rettv)
9402{
9403 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9404}
9405
9406/*
9407 * "searchpairpos()" function
9408 */
9409 static void
9410f_searchpairpos(typval_T *argvars, typval_T *rettv)
9411{
9412 pos_T match_pos;
9413 int lnum = 0;
9414 int col = 0;
9415
9416 if (rettv_list_alloc(rettv) == FAIL)
9417 return;
9418
9419 if (searchpair_cmn(argvars, &match_pos) > 0)
9420 {
9421 lnum = match_pos.lnum;
9422 col = match_pos.col;
9423 }
9424
9425 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9426 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9427}
9428
9429/*
9430 * Search for a start/middle/end thing.
9431 * Used by searchpair(), see its documentation for the details.
9432 * Returns 0 or -1 for no match,
9433 */
9434 long
9435do_searchpair(
9436 char_u *spat, /* start pattern */
9437 char_u *mpat, /* middle pattern */
9438 char_u *epat, /* end pattern */
9439 int dir, /* BACKWARD or FORWARD */
9440 char_u *skip, /* skip expression */
9441 int flags, /* SP_SETPCMARK and other SP_ values */
9442 pos_T *match_pos,
9443 linenr_T lnum_stop, /* stop at this line if not zero */
9444 long time_limit UNUSED) /* stop after this many msec */
9445{
9446 char_u *save_cpo;
9447 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9448 long retval = 0;
9449 pos_T pos;
9450 pos_T firstpos;
9451 pos_T foundpos;
9452 pos_T save_cursor;
9453 pos_T save_pos;
9454 int n;
9455 int r;
9456 int nest = 1;
9457 int err;
9458 int options = SEARCH_KEEP;
9459 proftime_T tm;
9460
9461 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9462 save_cpo = p_cpo;
9463 p_cpo = empty_option;
9464
9465#ifdef FEAT_RELTIME
9466 /* Set the time limit, if there is one. */
9467 profile_setlimit(time_limit, &tm);
9468#endif
9469
9470 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9471 * start/middle/end (pat3, for the top pair). */
9472 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
9473 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
9474 if (pat2 == NULL || pat3 == NULL)
9475 goto theend;
9476 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
9477 if (*mpat == NUL)
9478 STRCPY(pat3, pat2);
9479 else
9480 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
9481 spat, epat, mpat);
9482 if (flags & SP_START)
9483 options |= SEARCH_START;
9484
9485 save_cursor = curwin->w_cursor;
9486 pos = curwin->w_cursor;
9487 clearpos(&firstpos);
9488 clearpos(&foundpos);
9489 pat = pat3;
9490 for (;;)
9491 {
9492 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9493 options, RE_SEARCH, lnum_stop, &tm);
9494 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
9495 /* didn't find it or found the first match again: FAIL */
9496 break;
9497
9498 if (firstpos.lnum == 0)
9499 firstpos = pos;
9500 if (equalpos(pos, foundpos))
9501 {
9502 /* Found the same position again. Can happen with a pattern that
9503 * has "\zs" at the end and searching backwards. Advance one
9504 * character and try again. */
9505 if (dir == BACKWARD)
9506 decl(&pos);
9507 else
9508 incl(&pos);
9509 }
9510 foundpos = pos;
9511
9512 /* clear the start flag to avoid getting stuck here */
9513 options &= ~SEARCH_START;
9514
9515 /* If the skip pattern matches, ignore this match. */
9516 if (*skip != NUL)
9517 {
9518 save_pos = curwin->w_cursor;
9519 curwin->w_cursor = pos;
9520 r = eval_to_bool(skip, &err, NULL, FALSE);
9521 curwin->w_cursor = save_pos;
9522 if (err)
9523 {
9524 /* Evaluating {skip} caused an error, break here. */
9525 curwin->w_cursor = save_cursor;
9526 retval = -1;
9527 break;
9528 }
9529 if (r)
9530 continue;
9531 }
9532
9533 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9534 {
9535 /* Found end when searching backwards or start when searching
9536 * forward: nested pair. */
9537 ++nest;
9538 pat = pat2; /* nested, don't search for middle */
9539 }
9540 else
9541 {
9542 /* Found end when searching forward or start when searching
9543 * backward: end of (nested) pair; or found middle in outer pair. */
9544 if (--nest == 1)
9545 pat = pat3; /* outer level, search for middle */
9546 }
9547
9548 if (nest == 0)
9549 {
9550 /* Found the match: return matchcount or line number. */
9551 if (flags & SP_RETCOUNT)
9552 ++retval;
9553 else
9554 retval = pos.lnum;
9555 if (flags & SP_SETPCMARK)
9556 setpcmark();
9557 curwin->w_cursor = pos;
9558 if (!(flags & SP_REPEAT))
9559 break;
9560 nest = 1; /* search for next unmatched */
9561 }
9562 }
9563
9564 if (match_pos != NULL)
9565 {
9566 /* Store the match cursor position */
9567 match_pos->lnum = curwin->w_cursor.lnum;
9568 match_pos->col = curwin->w_cursor.col + 1;
9569 }
9570
9571 /* If 'n' flag is used or search failed: restore cursor position. */
9572 if ((flags & SP_NOMOVE) || retval == 0)
9573 curwin->w_cursor = save_cursor;
9574
9575theend:
9576 vim_free(pat2);
9577 vim_free(pat3);
9578 if (p_cpo == empty_option)
9579 p_cpo = save_cpo;
9580 else
9581 /* Darn, evaluating the {skip} expression changed the value. */
9582 free_string_option(save_cpo);
9583
9584 return retval;
9585}
9586
9587/*
9588 * "searchpos()" function
9589 */
9590 static void
9591f_searchpos(typval_T *argvars, typval_T *rettv)
9592{
9593 pos_T match_pos;
9594 int lnum = 0;
9595 int col = 0;
9596 int n;
9597 int flags = 0;
9598
9599 if (rettv_list_alloc(rettv) == FAIL)
9600 return;
9601
9602 n = search_cmn(argvars, &match_pos, &flags);
9603 if (n > 0)
9604 {
9605 lnum = match_pos.lnum;
9606 col = match_pos.col;
9607 }
9608
9609 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9610 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9611 if (flags & SP_SUBPAT)
9612 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9613}
9614
9615 static void
9616f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9617{
9618#ifdef FEAT_CLIENTSERVER
9619 char_u buf[NUMBUFLEN];
9620 char_u *server = get_tv_string_chk(&argvars[0]);
9621 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9622
9623 rettv->vval.v_number = -1;
9624 if (server == NULL || reply == NULL)
9625 return;
9626 if (check_restricted() || check_secure())
9627 return;
9628# ifdef FEAT_X11
9629 if (check_connection() == FAIL)
9630 return;
9631# endif
9632
9633 if (serverSendReply(server, reply) < 0)
9634 {
9635 EMSG(_("E258: Unable to send to client"));
9636 return;
9637 }
9638 rettv->vval.v_number = 0;
9639#else
9640 rettv->vval.v_number = -1;
9641#endif
9642}
9643
9644 static void
9645f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9646{
9647 char_u *r = NULL;
9648
9649#ifdef FEAT_CLIENTSERVER
9650# ifdef WIN32
9651 r = serverGetVimNames();
9652# else
9653 make_connection();
9654 if (X_DISPLAY != NULL)
9655 r = serverGetVimNames(X_DISPLAY);
9656# endif
9657#endif
9658 rettv->v_type = VAR_STRING;
9659 rettv->vval.v_string = r;
9660}
9661
9662/*
9663 * "setbufvar()" function
9664 */
9665 static void
9666f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
9667{
9668 buf_T *buf;
9669 char_u *varname, *bufvarname;
9670 typval_T *varp;
9671 char_u nbuf[NUMBUFLEN];
9672
9673 if (check_restricted() || check_secure())
9674 return;
9675 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9676 varname = get_tv_string_chk(&argvars[1]);
9677 buf = get_buf_tv(&argvars[0], FALSE);
9678 varp = &argvars[2];
9679
9680 if (buf != NULL && varname != NULL && varp != NULL)
9681 {
9682 if (*varname == '&')
9683 {
9684 long numval;
9685 char_u *strval;
9686 int error = FALSE;
9687 aco_save_T aco;
9688
9689 /* set curbuf to be our buf, temporarily */
9690 aucmd_prepbuf(&aco, buf);
9691
9692 ++varname;
9693 numval = (long)get_tv_number_chk(varp, &error);
9694 strval = get_tv_string_buf_chk(varp, nbuf);
9695 if (!error && strval != NULL)
9696 set_option_value(varname, numval, strval, OPT_LOCAL);
9697
9698 /* reset notion of buffer */
9699 aucmd_restbuf(&aco);
9700 }
9701 else
9702 {
9703 buf_T *save_curbuf = curbuf;
9704
9705 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
9706 if (bufvarname != NULL)
9707 {
9708 curbuf = buf;
9709 STRCPY(bufvarname, "b:");
9710 STRCPY(bufvarname + 2, varname);
9711 set_var(bufvarname, varp, TRUE);
9712 vim_free(bufvarname);
9713 curbuf = save_curbuf;
9714 }
9715 }
9716 }
9717}
9718
9719 static void
9720f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
9721{
9722 dict_T *d;
9723 dictitem_T *di;
9724 char_u *csearch;
9725
9726 if (argvars[0].v_type != VAR_DICT)
9727 {
9728 EMSG(_(e_dictreq));
9729 return;
9730 }
9731
9732 if ((d = argvars[0].vval.v_dict) != NULL)
9733 {
9734 csearch = get_dict_string(d, (char_u *)"char", FALSE);
9735 if (csearch != NULL)
9736 {
9737#ifdef FEAT_MBYTE
9738 if (enc_utf8)
9739 {
9740 int pcc[MAX_MCO];
9741 int c = utfc_ptr2char(csearch, pcc);
9742
9743 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
9744 }
9745 else
9746#endif
9747 set_last_csearch(PTR2CHAR(csearch),
9748 csearch, MB_PTR2LEN(csearch));
9749 }
9750
9751 di = dict_find(d, (char_u *)"forward", -1);
9752 if (di != NULL)
9753 set_csearch_direction((int)get_tv_number(&di->di_tv)
9754 ? FORWARD : BACKWARD);
9755
9756 di = dict_find(d, (char_u *)"until", -1);
9757 if (di != NULL)
9758 set_csearch_until(!!get_tv_number(&di->di_tv));
9759 }
9760}
9761
9762/*
9763 * "setcmdpos()" function
9764 */
9765 static void
9766f_setcmdpos(typval_T *argvars, typval_T *rettv)
9767{
9768 int pos = (int)get_tv_number(&argvars[0]) - 1;
9769
9770 if (pos >= 0)
9771 rettv->vval.v_number = set_cmdline_pos(pos);
9772}
9773
9774/*
9775 * "setfperm({fname}, {mode})" function
9776 */
9777 static void
9778f_setfperm(typval_T *argvars, typval_T *rettv)
9779{
9780 char_u *fname;
9781 char_u modebuf[NUMBUFLEN];
9782 char_u *mode_str;
9783 int i;
9784 int mask;
9785 int mode = 0;
9786
9787 rettv->vval.v_number = 0;
9788 fname = get_tv_string_chk(&argvars[0]);
9789 if (fname == NULL)
9790 return;
9791 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
9792 if (mode_str == NULL)
9793 return;
9794 if (STRLEN(mode_str) != 9)
9795 {
9796 EMSG2(_(e_invarg2), mode_str);
9797 return;
9798 }
9799
9800 mask = 1;
9801 for (i = 8; i >= 0; --i)
9802 {
9803 if (mode_str[i] != '-')
9804 mode |= mask;
9805 mask = mask << 1;
9806 }
9807 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
9808}
9809
9810/*
9811 * "setline()" function
9812 */
9813 static void
9814f_setline(typval_T *argvars, typval_T *rettv)
9815{
9816 linenr_T lnum;
9817 char_u *line = NULL;
9818 list_T *l = NULL;
9819 listitem_T *li = NULL;
9820 long added = 0;
9821 linenr_T lcount = curbuf->b_ml.ml_line_count;
9822
9823 lnum = get_tv_lnum(&argvars[0]);
9824 if (argvars[1].v_type == VAR_LIST)
9825 {
9826 l = argvars[1].vval.v_list;
9827 li = l->lv_first;
9828 }
9829 else
9830 line = get_tv_string_chk(&argvars[1]);
9831
9832 /* default result is zero == OK */
9833 for (;;)
9834 {
9835 if (l != NULL)
9836 {
9837 /* list argument, get next string */
9838 if (li == NULL)
9839 break;
9840 line = get_tv_string_chk(&li->li_tv);
9841 li = li->li_next;
9842 }
9843
9844 rettv->vval.v_number = 1; /* FAIL */
9845 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
9846 break;
9847
9848 /* When coming here from Insert mode, sync undo, so that this can be
9849 * undone separately from what was previously inserted. */
9850 if (u_sync_once == 2)
9851 {
9852 u_sync_once = 1; /* notify that u_sync() was called */
9853 u_sync(TRUE);
9854 }
9855
9856 if (lnum <= curbuf->b_ml.ml_line_count)
9857 {
9858 /* existing line, replace it */
9859 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
9860 {
9861 changed_bytes(lnum, 0);
9862 if (lnum == curwin->w_cursor.lnum)
9863 check_cursor_col();
9864 rettv->vval.v_number = 0; /* OK */
9865 }
9866 }
9867 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
9868 {
9869 /* lnum is one past the last line, append the line */
9870 ++added;
9871 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
9872 rettv->vval.v_number = 0; /* OK */
9873 }
9874
9875 if (l == NULL) /* only one string argument */
9876 break;
9877 ++lnum;
9878 }
9879
9880 if (added > 0)
9881 appended_lines_mark(lcount, added);
9882}
9883
Bram Moolenaard823fa92016-08-12 16:29:27 +02009884static 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 +02009885
9886/*
9887 * Used by "setqflist()" and "setloclist()" functions
9888 */
9889 static void
9890set_qf_ll_list(
9891 win_T *wp UNUSED,
9892 typval_T *list_arg UNUSED,
9893 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +02009894 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009895 typval_T *rettv)
9896{
9897#ifdef FEAT_QUICKFIX
9898 static char *e_invact = N_("E927: Invalid action: '%s'");
9899 char_u *act;
9900 int action = 0;
9901#endif
9902
9903 rettv->vval.v_number = -1;
9904
9905#ifdef FEAT_QUICKFIX
9906 if (list_arg->v_type != VAR_LIST)
9907 EMSG(_(e_listreq));
9908 else
9909 {
9910 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +02009911 dict_T *d = NULL;
9912 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009913
9914 if (action_arg->v_type == VAR_STRING)
9915 {
9916 act = get_tv_string_chk(action_arg);
9917 if (act == NULL)
9918 return; /* type error; errmsg already given */
9919 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
9920 action = *act;
9921 else
9922 EMSG2(_(e_invact), act);
9923 }
9924 else if (action_arg->v_type == VAR_UNKNOWN)
9925 action = ' ';
9926 else
9927 EMSG(_(e_stringreq));
9928
Bram Moolenaard823fa92016-08-12 16:29:27 +02009929 if (action_arg->v_type != VAR_UNKNOWN
9930 && what_arg->v_type != VAR_UNKNOWN)
9931 {
9932 if (what_arg->v_type == VAR_DICT)
9933 d = what_arg->vval.v_dict;
9934 else
9935 {
9936 EMSG(_(e_dictreq));
9937 valid_dict = FALSE;
9938 }
9939 }
9940
9941 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
9942 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943 rettv->vval.v_number = 0;
9944 }
9945#endif
9946}
9947
9948/*
9949 * "setloclist()" function
9950 */
9951 static void
9952f_setloclist(typval_T *argvars, typval_T *rettv)
9953{
9954 win_T *win;
9955
9956 rettv->vval.v_number = -1;
9957
9958 win = find_win_by_nr(&argvars[0], NULL);
9959 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02009960 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009961}
9962
9963/*
9964 * "setmatches()" function
9965 */
9966 static void
9967f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9968{
9969#ifdef FEAT_SEARCH_EXTRA
9970 list_T *l;
9971 listitem_T *li;
9972 dict_T *d;
9973 list_T *s = NULL;
9974
9975 rettv->vval.v_number = -1;
9976 if (argvars[0].v_type != VAR_LIST)
9977 {
9978 EMSG(_(e_listreq));
9979 return;
9980 }
9981 if ((l = argvars[0].vval.v_list) != NULL)
9982 {
9983
9984 /* To some extent make sure that we are dealing with a list from
9985 * "getmatches()". */
9986 li = l->lv_first;
9987 while (li != NULL)
9988 {
9989 if (li->li_tv.v_type != VAR_DICT
9990 || (d = li->li_tv.vval.v_dict) == NULL)
9991 {
9992 EMSG(_(e_invarg));
9993 return;
9994 }
9995 if (!(dict_find(d, (char_u *)"group", -1) != NULL
9996 && (dict_find(d, (char_u *)"pattern", -1) != NULL
9997 || dict_find(d, (char_u *)"pos1", -1) != NULL)
9998 && dict_find(d, (char_u *)"priority", -1) != NULL
9999 && dict_find(d, (char_u *)"id", -1) != NULL))
10000 {
10001 EMSG(_(e_invarg));
10002 return;
10003 }
10004 li = li->li_next;
10005 }
10006
10007 clear_matches(curwin);
10008 li = l->lv_first;
10009 while (li != NULL)
10010 {
10011 int i = 0;
10012 char_u buf[5];
10013 dictitem_T *di;
10014 char_u *group;
10015 int priority;
10016 int id;
10017 char_u *conceal;
10018
10019 d = li->li_tv.vval.v_dict;
10020 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10021 {
10022 if (s == NULL)
10023 {
10024 s = list_alloc();
10025 if (s == NULL)
10026 return;
10027 }
10028
10029 /* match from matchaddpos() */
10030 for (i = 1; i < 9; i++)
10031 {
10032 sprintf((char *)buf, (char *)"pos%d", i);
10033 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10034 {
10035 if (di->di_tv.v_type != VAR_LIST)
10036 return;
10037
10038 list_append_tv(s, &di->di_tv);
10039 s->lv_refcount++;
10040 }
10041 else
10042 break;
10043 }
10044 }
10045
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010046 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010047 priority = (int)get_dict_number(d, (char_u *)"priority");
10048 id = (int)get_dict_number(d, (char_u *)"id");
10049 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010050 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010051 : NULL;
10052 if (i == 0)
10053 {
10054 match_add(curwin, group,
10055 get_dict_string(d, (char_u *)"pattern", FALSE),
10056 priority, id, NULL, conceal);
10057 }
10058 else
10059 {
10060 match_add(curwin, group, NULL, priority, id, s, conceal);
10061 list_unref(s);
10062 s = NULL;
10063 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010064 vim_free(group);
10065 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010066
10067 li = li->li_next;
10068 }
10069 rettv->vval.v_number = 0;
10070 }
10071#endif
10072}
10073
10074/*
10075 * "setpos()" function
10076 */
10077 static void
10078f_setpos(typval_T *argvars, typval_T *rettv)
10079{
10080 pos_T pos;
10081 int fnum;
10082 char_u *name;
10083 colnr_T curswant = -1;
10084
10085 rettv->vval.v_number = -1;
10086 name = get_tv_string_chk(argvars);
10087 if (name != NULL)
10088 {
10089 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10090 {
10091 if (--pos.col < 0)
10092 pos.col = 0;
10093 if (name[0] == '.' && name[1] == NUL)
10094 {
10095 /* set cursor */
10096 if (fnum == curbuf->b_fnum)
10097 {
10098 curwin->w_cursor = pos;
10099 if (curswant >= 0)
10100 {
10101 curwin->w_curswant = curswant - 1;
10102 curwin->w_set_curswant = FALSE;
10103 }
10104 check_cursor();
10105 rettv->vval.v_number = 0;
10106 }
10107 else
10108 EMSG(_(e_invarg));
10109 }
10110 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10111 {
10112 /* set mark */
10113 if (setmark_pos(name[1], &pos, fnum) == OK)
10114 rettv->vval.v_number = 0;
10115 }
10116 else
10117 EMSG(_(e_invarg));
10118 }
10119 }
10120}
10121
10122/*
10123 * "setqflist()" function
10124 */
10125 static void
10126f_setqflist(typval_T *argvars, typval_T *rettv)
10127{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010128 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010129}
10130
10131/*
10132 * "setreg()" function
10133 */
10134 static void
10135f_setreg(typval_T *argvars, typval_T *rettv)
10136{
10137 int regname;
10138 char_u *strregname;
10139 char_u *stropt;
10140 char_u *strval;
10141 int append;
10142 char_u yank_type;
10143 long block_len;
10144
10145 block_len = -1;
10146 yank_type = MAUTO;
10147 append = FALSE;
10148
10149 strregname = get_tv_string_chk(argvars);
10150 rettv->vval.v_number = 1; /* FAIL is default */
10151
10152 if (strregname == NULL)
10153 return; /* type error; errmsg already given */
10154 regname = *strregname;
10155 if (regname == 0 || regname == '@')
10156 regname = '"';
10157
10158 if (argvars[2].v_type != VAR_UNKNOWN)
10159 {
10160 stropt = get_tv_string_chk(&argvars[2]);
10161 if (stropt == NULL)
10162 return; /* type error */
10163 for (; *stropt != NUL; ++stropt)
10164 switch (*stropt)
10165 {
10166 case 'a': case 'A': /* append */
10167 append = TRUE;
10168 break;
10169 case 'v': case 'c': /* character-wise selection */
10170 yank_type = MCHAR;
10171 break;
10172 case 'V': case 'l': /* line-wise selection */
10173 yank_type = MLINE;
10174 break;
10175 case 'b': case Ctrl_V: /* block-wise selection */
10176 yank_type = MBLOCK;
10177 if (VIM_ISDIGIT(stropt[1]))
10178 {
10179 ++stropt;
10180 block_len = getdigits(&stropt) - 1;
10181 --stropt;
10182 }
10183 break;
10184 }
10185 }
10186
10187 if (argvars[1].v_type == VAR_LIST)
10188 {
10189 char_u **lstval;
10190 char_u **allocval;
10191 char_u buf[NUMBUFLEN];
10192 char_u **curval;
10193 char_u **curallocval;
10194 list_T *ll = argvars[1].vval.v_list;
10195 listitem_T *li;
10196 int len;
10197
10198 /* If the list is NULL handle like an empty list. */
10199 len = ll == NULL ? 0 : ll->lv_len;
10200
10201 /* First half: use for pointers to result lines; second half: use for
10202 * pointers to allocated copies. */
10203 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10204 if (lstval == NULL)
10205 return;
10206 curval = lstval;
10207 allocval = lstval + len + 2;
10208 curallocval = allocval;
10209
10210 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10211 li = li->li_next)
10212 {
10213 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10214 if (strval == NULL)
10215 goto free_lstval;
10216 if (strval == buf)
10217 {
10218 /* Need to make a copy, next get_tv_string_buf_chk() will
10219 * overwrite the string. */
10220 strval = vim_strsave(buf);
10221 if (strval == NULL)
10222 goto free_lstval;
10223 *curallocval++ = strval;
10224 }
10225 *curval++ = strval;
10226 }
10227 *curval++ = NULL;
10228
10229 write_reg_contents_lst(regname, lstval, -1,
10230 append, yank_type, block_len);
10231free_lstval:
10232 while (curallocval > allocval)
10233 vim_free(*--curallocval);
10234 vim_free(lstval);
10235 }
10236 else
10237 {
10238 strval = get_tv_string_chk(&argvars[1]);
10239 if (strval == NULL)
10240 return;
10241 write_reg_contents_ex(regname, strval, -1,
10242 append, yank_type, block_len);
10243 }
10244 rettv->vval.v_number = 0;
10245}
10246
10247/*
10248 * "settabvar()" function
10249 */
10250 static void
10251f_settabvar(typval_T *argvars, typval_T *rettv)
10252{
10253#ifdef FEAT_WINDOWS
10254 tabpage_T *save_curtab;
10255 tabpage_T *tp;
10256#endif
10257 char_u *varname, *tabvarname;
10258 typval_T *varp;
10259
10260 rettv->vval.v_number = 0;
10261
10262 if (check_restricted() || check_secure())
10263 return;
10264
10265#ifdef FEAT_WINDOWS
10266 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10267#endif
10268 varname = get_tv_string_chk(&argvars[1]);
10269 varp = &argvars[2];
10270
10271 if (varname != NULL && varp != NULL
10272#ifdef FEAT_WINDOWS
10273 && tp != NULL
10274#endif
10275 )
10276 {
10277#ifdef FEAT_WINDOWS
10278 save_curtab = curtab;
10279 goto_tabpage_tp(tp, FALSE, FALSE);
10280#endif
10281
10282 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10283 if (tabvarname != NULL)
10284 {
10285 STRCPY(tabvarname, "t:");
10286 STRCPY(tabvarname + 2, varname);
10287 set_var(tabvarname, varp, TRUE);
10288 vim_free(tabvarname);
10289 }
10290
10291#ifdef FEAT_WINDOWS
10292 /* Restore current tabpage */
10293 if (valid_tabpage(save_curtab))
10294 goto_tabpage_tp(save_curtab, FALSE, FALSE);
10295#endif
10296 }
10297}
10298
10299/*
10300 * "settabwinvar()" function
10301 */
10302 static void
10303f_settabwinvar(typval_T *argvars, typval_T *rettv)
10304{
10305 setwinvar(argvars, rettv, 1);
10306}
10307
10308/*
10309 * "setwinvar()" function
10310 */
10311 static void
10312f_setwinvar(typval_T *argvars, typval_T *rettv)
10313{
10314 setwinvar(argvars, rettv, 0);
10315}
10316
10317#ifdef FEAT_CRYPT
10318/*
10319 * "sha256({string})" function
10320 */
10321 static void
10322f_sha256(typval_T *argvars, typval_T *rettv)
10323{
10324 char_u *p;
10325
10326 p = get_tv_string(&argvars[0]);
10327 rettv->vval.v_string = vim_strsave(
10328 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10329 rettv->v_type = VAR_STRING;
10330}
10331#endif /* FEAT_CRYPT */
10332
10333/*
10334 * "shellescape({string})" function
10335 */
10336 static void
10337f_shellescape(typval_T *argvars, typval_T *rettv)
10338{
10339 rettv->vval.v_string = vim_strsave_shellescape(
10340 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
10341 rettv->v_type = VAR_STRING;
10342}
10343
10344/*
10345 * shiftwidth() function
10346 */
10347 static void
10348f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10349{
10350 rettv->vval.v_number = get_sw_value(curbuf);
10351}
10352
10353/*
10354 * "simplify()" function
10355 */
10356 static void
10357f_simplify(typval_T *argvars, typval_T *rettv)
10358{
10359 char_u *p;
10360
10361 p = get_tv_string(&argvars[0]);
10362 rettv->vval.v_string = vim_strsave(p);
10363 simplify_filename(rettv->vval.v_string); /* simplify in place */
10364 rettv->v_type = VAR_STRING;
10365}
10366
10367#ifdef FEAT_FLOAT
10368/*
10369 * "sin()" function
10370 */
10371 static void
10372f_sin(typval_T *argvars, typval_T *rettv)
10373{
10374 float_T f = 0.0;
10375
10376 rettv->v_type = VAR_FLOAT;
10377 if (get_float_arg(argvars, &f) == OK)
10378 rettv->vval.v_float = sin(f);
10379 else
10380 rettv->vval.v_float = 0.0;
10381}
10382
10383/*
10384 * "sinh()" function
10385 */
10386 static void
10387f_sinh(typval_T *argvars, typval_T *rettv)
10388{
10389 float_T f = 0.0;
10390
10391 rettv->v_type = VAR_FLOAT;
10392 if (get_float_arg(argvars, &f) == OK)
10393 rettv->vval.v_float = sinh(f);
10394 else
10395 rettv->vval.v_float = 0.0;
10396}
10397#endif
10398
10399static int
10400#ifdef __BORLANDC__
10401 _RTLENTRYF
10402#endif
10403 item_compare(const void *s1, const void *s2);
10404static int
10405#ifdef __BORLANDC__
10406 _RTLENTRYF
10407#endif
10408 item_compare2(const void *s1, const void *s2);
10409
10410/* struct used in the array that's given to qsort() */
10411typedef struct
10412{
10413 listitem_T *item;
10414 int idx;
10415} sortItem_T;
10416
10417/* struct storing information about current sort */
10418typedef struct
10419{
10420 int item_compare_ic;
10421 int item_compare_numeric;
10422 int item_compare_numbers;
10423#ifdef FEAT_FLOAT
10424 int item_compare_float;
10425#endif
10426 char_u *item_compare_func;
10427 partial_T *item_compare_partial;
10428 dict_T *item_compare_selfdict;
10429 int item_compare_func_err;
10430 int item_compare_keep_zero;
10431} sortinfo_T;
10432static sortinfo_T *sortinfo = NULL;
10433static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10434#define ITEM_COMPARE_FAIL 999
10435
10436/*
10437 * Compare functions for f_sort() and f_uniq() below.
10438 */
10439 static int
10440#ifdef __BORLANDC__
10441_RTLENTRYF
10442#endif
10443item_compare(const void *s1, const void *s2)
10444{
10445 sortItem_T *si1, *si2;
10446 typval_T *tv1, *tv2;
10447 char_u *p1, *p2;
10448 char_u *tofree1 = NULL, *tofree2 = NULL;
10449 int res;
10450 char_u numbuf1[NUMBUFLEN];
10451 char_u numbuf2[NUMBUFLEN];
10452
10453 si1 = (sortItem_T *)s1;
10454 si2 = (sortItem_T *)s2;
10455 tv1 = &si1->item->li_tv;
10456 tv2 = &si2->item->li_tv;
10457
10458 if (sortinfo->item_compare_numbers)
10459 {
10460 varnumber_T v1 = get_tv_number(tv1);
10461 varnumber_T v2 = get_tv_number(tv2);
10462
10463 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10464 }
10465
10466#ifdef FEAT_FLOAT
10467 if (sortinfo->item_compare_float)
10468 {
10469 float_T v1 = get_tv_float(tv1);
10470 float_T v2 = get_tv_float(tv2);
10471
10472 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10473 }
10474#endif
10475
10476 /* tv2string() puts quotes around a string and allocates memory. Don't do
10477 * that for string variables. Use a single quote when comparing with a
10478 * non-string to do what the docs promise. */
10479 if (tv1->v_type == VAR_STRING)
10480 {
10481 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10482 p1 = (char_u *)"'";
10483 else
10484 p1 = tv1->vval.v_string;
10485 }
10486 else
10487 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10488 if (tv2->v_type == VAR_STRING)
10489 {
10490 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10491 p2 = (char_u *)"'";
10492 else
10493 p2 = tv2->vval.v_string;
10494 }
10495 else
10496 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10497 if (p1 == NULL)
10498 p1 = (char_u *)"";
10499 if (p2 == NULL)
10500 p2 = (char_u *)"";
10501 if (!sortinfo->item_compare_numeric)
10502 {
10503 if (sortinfo->item_compare_ic)
10504 res = STRICMP(p1, p2);
10505 else
10506 res = STRCMP(p1, p2);
10507 }
10508 else
10509 {
10510 double n1, n2;
10511 n1 = strtod((char *)p1, (char **)&p1);
10512 n2 = strtod((char *)p2, (char **)&p2);
10513 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10514 }
10515
10516 /* When the result would be zero, compare the item indexes. Makes the
10517 * sort stable. */
10518 if (res == 0 && !sortinfo->item_compare_keep_zero)
10519 res = si1->idx > si2->idx ? 1 : -1;
10520
10521 vim_free(tofree1);
10522 vim_free(tofree2);
10523 return res;
10524}
10525
10526 static int
10527#ifdef __BORLANDC__
10528_RTLENTRYF
10529#endif
10530item_compare2(const void *s1, const void *s2)
10531{
10532 sortItem_T *si1, *si2;
10533 int res;
10534 typval_T rettv;
10535 typval_T argv[3];
10536 int dummy;
10537 char_u *func_name;
10538 partial_T *partial = sortinfo->item_compare_partial;
10539
10540 /* shortcut after failure in previous call; compare all items equal */
10541 if (sortinfo->item_compare_func_err)
10542 return 0;
10543
10544 si1 = (sortItem_T *)s1;
10545 si2 = (sortItem_T *)s2;
10546
10547 if (partial == NULL)
10548 func_name = sortinfo->item_compare_func;
10549 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010550 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010551
10552 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10553 * in the copy without changing the original list items. */
10554 copy_tv(&si1->item->li_tv, &argv[0]);
10555 copy_tv(&si2->item->li_tv, &argv[1]);
10556
10557 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10558 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010559 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010560 partial, sortinfo->item_compare_selfdict);
10561 clear_tv(&argv[0]);
10562 clear_tv(&argv[1]);
10563
10564 if (res == FAIL)
10565 res = ITEM_COMPARE_FAIL;
10566 else
10567 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10568 if (sortinfo->item_compare_func_err)
10569 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10570 clear_tv(&rettv);
10571
10572 /* When the result would be zero, compare the pointers themselves. Makes
10573 * the sort stable. */
10574 if (res == 0 && !sortinfo->item_compare_keep_zero)
10575 res = si1->idx > si2->idx ? 1 : -1;
10576
10577 return res;
10578}
10579
10580/*
10581 * "sort({list})" function
10582 */
10583 static void
10584do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10585{
10586 list_T *l;
10587 listitem_T *li;
10588 sortItem_T *ptrs;
10589 sortinfo_T *old_sortinfo;
10590 sortinfo_T info;
10591 long len;
10592 long i;
10593
10594 /* Pointer to current info struct used in compare function. Save and
10595 * restore the current one for nested calls. */
10596 old_sortinfo = sortinfo;
10597 sortinfo = &info;
10598
10599 if (argvars[0].v_type != VAR_LIST)
10600 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
10601 else
10602 {
10603 l = argvars[0].vval.v_list;
10604 if (l == NULL || tv_check_lock(l->lv_lock,
10605 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
10606 TRUE))
10607 goto theend;
10608 rettv->vval.v_list = l;
10609 rettv->v_type = VAR_LIST;
10610 ++l->lv_refcount;
10611
10612 len = list_len(l);
10613 if (len <= 1)
10614 goto theend; /* short list sorts pretty quickly */
10615
10616 info.item_compare_ic = FALSE;
10617 info.item_compare_numeric = FALSE;
10618 info.item_compare_numbers = FALSE;
10619#ifdef FEAT_FLOAT
10620 info.item_compare_float = FALSE;
10621#endif
10622 info.item_compare_func = NULL;
10623 info.item_compare_partial = NULL;
10624 info.item_compare_selfdict = NULL;
10625 if (argvars[1].v_type != VAR_UNKNOWN)
10626 {
10627 /* optional second argument: {func} */
10628 if (argvars[1].v_type == VAR_FUNC)
10629 info.item_compare_func = argvars[1].vval.v_string;
10630 else if (argvars[1].v_type == VAR_PARTIAL)
10631 info.item_compare_partial = argvars[1].vval.v_partial;
10632 else
10633 {
10634 int error = FALSE;
10635
10636 i = (long)get_tv_number_chk(&argvars[1], &error);
10637 if (error)
10638 goto theend; /* type error; errmsg already given */
10639 if (i == 1)
10640 info.item_compare_ic = TRUE;
10641 else if (argvars[1].v_type != VAR_NUMBER)
10642 info.item_compare_func = get_tv_string(&argvars[1]);
10643 else if (i != 0)
10644 {
10645 EMSG(_(e_invarg));
10646 goto theend;
10647 }
10648 if (info.item_compare_func != NULL)
10649 {
10650 if (*info.item_compare_func == NUL)
10651 {
10652 /* empty string means default sort */
10653 info.item_compare_func = NULL;
10654 }
10655 else if (STRCMP(info.item_compare_func, "n") == 0)
10656 {
10657 info.item_compare_func = NULL;
10658 info.item_compare_numeric = TRUE;
10659 }
10660 else if (STRCMP(info.item_compare_func, "N") == 0)
10661 {
10662 info.item_compare_func = NULL;
10663 info.item_compare_numbers = TRUE;
10664 }
10665#ifdef FEAT_FLOAT
10666 else if (STRCMP(info.item_compare_func, "f") == 0)
10667 {
10668 info.item_compare_func = NULL;
10669 info.item_compare_float = TRUE;
10670 }
10671#endif
10672 else if (STRCMP(info.item_compare_func, "i") == 0)
10673 {
10674 info.item_compare_func = NULL;
10675 info.item_compare_ic = TRUE;
10676 }
10677 }
10678 }
10679
10680 if (argvars[2].v_type != VAR_UNKNOWN)
10681 {
10682 /* optional third argument: {dict} */
10683 if (argvars[2].v_type != VAR_DICT)
10684 {
10685 EMSG(_(e_dictreq));
10686 goto theend;
10687 }
10688 info.item_compare_selfdict = argvars[2].vval.v_dict;
10689 }
10690 }
10691
10692 /* Make an array with each entry pointing to an item in the List. */
10693 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
10694 if (ptrs == NULL)
10695 goto theend;
10696
10697 i = 0;
10698 if (sort)
10699 {
10700 /* sort(): ptrs will be the list to sort */
10701 for (li = l->lv_first; li != NULL; li = li->li_next)
10702 {
10703 ptrs[i].item = li;
10704 ptrs[i].idx = i;
10705 ++i;
10706 }
10707
10708 info.item_compare_func_err = FALSE;
10709 info.item_compare_keep_zero = FALSE;
10710 /* test the compare function */
10711 if ((info.item_compare_func != NULL
10712 || info.item_compare_partial != NULL)
10713 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
10714 == ITEM_COMPARE_FAIL)
10715 EMSG(_("E702: Sort compare function failed"));
10716 else
10717 {
10718 /* Sort the array with item pointers. */
10719 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
10720 info.item_compare_func == NULL
10721 && info.item_compare_partial == NULL
10722 ? item_compare : item_compare2);
10723
10724 if (!info.item_compare_func_err)
10725 {
10726 /* Clear the List and append the items in sorted order. */
10727 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
10728 l->lv_len = 0;
10729 for (i = 0; i < len; ++i)
10730 list_append(l, ptrs[i].item);
10731 }
10732 }
10733 }
10734 else
10735 {
10736 int (*item_compare_func_ptr)(const void *, const void *);
10737
10738 /* f_uniq(): ptrs will be a stack of items to remove */
10739 info.item_compare_func_err = FALSE;
10740 info.item_compare_keep_zero = TRUE;
10741 item_compare_func_ptr = info.item_compare_func != NULL
10742 || info.item_compare_partial != NULL
10743 ? item_compare2 : item_compare;
10744
10745 for (li = l->lv_first; li != NULL && li->li_next != NULL;
10746 li = li->li_next)
10747 {
10748 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
10749 == 0)
10750 ptrs[i++].item = li;
10751 if (info.item_compare_func_err)
10752 {
10753 EMSG(_("E882: Uniq compare function failed"));
10754 break;
10755 }
10756 }
10757
10758 if (!info.item_compare_func_err)
10759 {
10760 while (--i >= 0)
10761 {
10762 li = ptrs[i].item->li_next;
10763 ptrs[i].item->li_next = li->li_next;
10764 if (li->li_next != NULL)
10765 li->li_next->li_prev = ptrs[i].item;
10766 else
10767 l->lv_last = ptrs[i].item;
10768 list_fix_watch(l, li);
10769 listitem_free(li);
10770 l->lv_len--;
10771 }
10772 }
10773 }
10774
10775 vim_free(ptrs);
10776 }
10777theend:
10778 sortinfo = old_sortinfo;
10779}
10780
10781/*
10782 * "sort({list})" function
10783 */
10784 static void
10785f_sort(typval_T *argvars, typval_T *rettv)
10786{
10787 do_sort_uniq(argvars, rettv, TRUE);
10788}
10789
10790/*
10791 * "uniq({list})" function
10792 */
10793 static void
10794f_uniq(typval_T *argvars, typval_T *rettv)
10795{
10796 do_sort_uniq(argvars, rettv, FALSE);
10797}
10798
10799/*
10800 * "soundfold({word})" function
10801 */
10802 static void
10803f_soundfold(typval_T *argvars, typval_T *rettv)
10804{
10805 char_u *s;
10806
10807 rettv->v_type = VAR_STRING;
10808 s = get_tv_string(&argvars[0]);
10809#ifdef FEAT_SPELL
10810 rettv->vval.v_string = eval_soundfold(s);
10811#else
10812 rettv->vval.v_string = vim_strsave(s);
10813#endif
10814}
10815
10816/*
10817 * "spellbadword()" function
10818 */
10819 static void
10820f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10821{
10822 char_u *word = (char_u *)"";
10823 hlf_T attr = HLF_COUNT;
10824 int len = 0;
10825
10826 if (rettv_list_alloc(rettv) == FAIL)
10827 return;
10828
10829#ifdef FEAT_SPELL
10830 if (argvars[0].v_type == VAR_UNKNOWN)
10831 {
10832 /* Find the start and length of the badly spelled word. */
10833 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10834 if (len != 0)
10835 word = ml_get_cursor();
10836 }
10837 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10838 {
10839 char_u *str = get_tv_string_chk(&argvars[0]);
10840 int capcol = -1;
10841
10842 if (str != NULL)
10843 {
10844 /* Check the argument for spelling. */
10845 while (*str != NUL)
10846 {
10847 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10848 if (attr != HLF_COUNT)
10849 {
10850 word = str;
10851 break;
10852 }
10853 str += len;
10854 }
10855 }
10856 }
10857#endif
10858
10859 list_append_string(rettv->vval.v_list, word, len);
10860 list_append_string(rettv->vval.v_list, (char_u *)(
10861 attr == HLF_SPB ? "bad" :
10862 attr == HLF_SPR ? "rare" :
10863 attr == HLF_SPL ? "local" :
10864 attr == HLF_SPC ? "caps" :
10865 ""), -1);
10866}
10867
10868/*
10869 * "spellsuggest()" function
10870 */
10871 static void
10872f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
10873{
10874#ifdef FEAT_SPELL
10875 char_u *str;
10876 int typeerr = FALSE;
10877 int maxcount;
10878 garray_T ga;
10879 int i;
10880 listitem_T *li;
10881 int need_capital = FALSE;
10882#endif
10883
10884 if (rettv_list_alloc(rettv) == FAIL)
10885 return;
10886
10887#ifdef FEAT_SPELL
10888 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
10889 {
10890 str = get_tv_string(&argvars[0]);
10891 if (argvars[1].v_type != VAR_UNKNOWN)
10892 {
10893 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
10894 if (maxcount <= 0)
10895 return;
10896 if (argvars[2].v_type != VAR_UNKNOWN)
10897 {
10898 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
10899 if (typeerr)
10900 return;
10901 }
10902 }
10903 else
10904 maxcount = 25;
10905
10906 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
10907
10908 for (i = 0; i < ga.ga_len; ++i)
10909 {
10910 str = ((char_u **)ga.ga_data)[i];
10911
10912 li = listitem_alloc();
10913 if (li == NULL)
10914 vim_free(str);
10915 else
10916 {
10917 li->li_tv.v_type = VAR_STRING;
10918 li->li_tv.v_lock = 0;
10919 li->li_tv.vval.v_string = str;
10920 list_append(rettv->vval.v_list, li);
10921 }
10922 }
10923 ga_clear(&ga);
10924 }
10925#endif
10926}
10927
10928 static void
10929f_split(typval_T *argvars, typval_T *rettv)
10930{
10931 char_u *str;
10932 char_u *end;
10933 char_u *pat = NULL;
10934 regmatch_T regmatch;
10935 char_u patbuf[NUMBUFLEN];
10936 char_u *save_cpo;
10937 int match;
10938 colnr_T col = 0;
10939 int keepempty = FALSE;
10940 int typeerr = FALSE;
10941
10942 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10943 save_cpo = p_cpo;
10944 p_cpo = (char_u *)"";
10945
10946 str = get_tv_string(&argvars[0]);
10947 if (argvars[1].v_type != VAR_UNKNOWN)
10948 {
10949 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
10950 if (pat == NULL)
10951 typeerr = TRUE;
10952 if (argvars[2].v_type != VAR_UNKNOWN)
10953 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
10954 }
10955 if (pat == NULL || *pat == NUL)
10956 pat = (char_u *)"[\\x01- ]\\+";
10957
10958 if (rettv_list_alloc(rettv) == FAIL)
10959 return;
10960 if (typeerr)
10961 return;
10962
10963 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10964 if (regmatch.regprog != NULL)
10965 {
10966 regmatch.rm_ic = FALSE;
10967 while (*str != NUL || keepempty)
10968 {
10969 if (*str == NUL)
10970 match = FALSE; /* empty item at the end */
10971 else
10972 match = vim_regexec_nl(&regmatch, str, col);
10973 if (match)
10974 end = regmatch.startp[0];
10975 else
10976 end = str + STRLEN(str);
10977 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
10978 && *str != NUL && match && end < regmatch.endp[0]))
10979 {
10980 if (list_append_string(rettv->vval.v_list, str,
10981 (int)(end - str)) == FAIL)
10982 break;
10983 }
10984 if (!match)
10985 break;
10986 /* Advance to just after the match. */
10987 if (regmatch.endp[0] > str)
10988 col = 0;
10989 else
10990 {
10991 /* Don't get stuck at the same match. */
10992#ifdef FEAT_MBYTE
10993 col = (*mb_ptr2len)(regmatch.endp[0]);
10994#else
10995 col = 1;
10996#endif
10997 }
10998 str = regmatch.endp[0];
10999 }
11000
11001 vim_regfree(regmatch.regprog);
11002 }
11003
11004 p_cpo = save_cpo;
11005}
11006
11007#ifdef FEAT_FLOAT
11008/*
11009 * "sqrt()" function
11010 */
11011 static void
11012f_sqrt(typval_T *argvars, typval_T *rettv)
11013{
11014 float_T f = 0.0;
11015
11016 rettv->v_type = VAR_FLOAT;
11017 if (get_float_arg(argvars, &f) == OK)
11018 rettv->vval.v_float = sqrt(f);
11019 else
11020 rettv->vval.v_float = 0.0;
11021}
11022
11023/*
11024 * "str2float()" function
11025 */
11026 static void
11027f_str2float(typval_T *argvars, typval_T *rettv)
11028{
11029 char_u *p = skipwhite(get_tv_string(&argvars[0]));
11030
11031 if (*p == '+')
11032 p = skipwhite(p + 1);
11033 (void)string2float(p, &rettv->vval.v_float);
11034 rettv->v_type = VAR_FLOAT;
11035}
11036#endif
11037
11038/*
11039 * "str2nr()" function
11040 */
11041 static void
11042f_str2nr(typval_T *argvars, typval_T *rettv)
11043{
11044 int base = 10;
11045 char_u *p;
11046 varnumber_T n;
11047 int what;
11048
11049 if (argvars[1].v_type != VAR_UNKNOWN)
11050 {
11051 base = (int)get_tv_number(&argvars[1]);
11052 if (base != 2 && base != 8 && base != 10 && base != 16)
11053 {
11054 EMSG(_(e_invarg));
11055 return;
11056 }
11057 }
11058
11059 p = skipwhite(get_tv_string(&argvars[0]));
11060 if (*p == '+')
11061 p = skipwhite(p + 1);
11062 switch (base)
11063 {
11064 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11065 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11066 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11067 default: what = 0;
11068 }
11069 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
11070 rettv->vval.v_number = n;
11071}
11072
11073#ifdef HAVE_STRFTIME
11074/*
11075 * "strftime({format}[, {time}])" function
11076 */
11077 static void
11078f_strftime(typval_T *argvars, typval_T *rettv)
11079{
11080 char_u result_buf[256];
11081 struct tm *curtime;
11082 time_t seconds;
11083 char_u *p;
11084
11085 rettv->v_type = VAR_STRING;
11086
11087 p = get_tv_string(&argvars[0]);
11088 if (argvars[1].v_type == VAR_UNKNOWN)
11089 seconds = time(NULL);
11090 else
11091 seconds = (time_t)get_tv_number(&argvars[1]);
11092 curtime = localtime(&seconds);
11093 /* MSVC returns NULL for an invalid value of seconds. */
11094 if (curtime == NULL)
11095 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11096 else
11097 {
11098# ifdef FEAT_MBYTE
11099 vimconv_T conv;
11100 char_u *enc;
11101
11102 conv.vc_type = CONV_NONE;
11103 enc = enc_locale();
11104 convert_setup(&conv, p_enc, enc);
11105 if (conv.vc_type != CONV_NONE)
11106 p = string_convert(&conv, p, NULL);
11107# endif
11108 if (p != NULL)
11109 (void)strftime((char *)result_buf, sizeof(result_buf),
11110 (char *)p, curtime);
11111 else
11112 result_buf[0] = NUL;
11113
11114# ifdef FEAT_MBYTE
11115 if (conv.vc_type != CONV_NONE)
11116 vim_free(p);
11117 convert_setup(&conv, enc, p_enc);
11118 if (conv.vc_type != CONV_NONE)
11119 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11120 else
11121# endif
11122 rettv->vval.v_string = vim_strsave(result_buf);
11123
11124# ifdef FEAT_MBYTE
11125 /* Release conversion descriptors */
11126 convert_setup(&conv, NULL, NULL);
11127 vim_free(enc);
11128# endif
11129 }
11130}
11131#endif
11132
11133/*
11134 * "strgetchar()" function
11135 */
11136 static void
11137f_strgetchar(typval_T *argvars, typval_T *rettv)
11138{
11139 char_u *str;
11140 int len;
11141 int error = FALSE;
11142 int charidx;
11143
11144 rettv->vval.v_number = -1;
11145 str = get_tv_string_chk(&argvars[0]);
11146 if (str == NULL)
11147 return;
11148 len = (int)STRLEN(str);
11149 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11150 if (error)
11151 return;
11152#ifdef FEAT_MBYTE
11153 {
11154 int byteidx = 0;
11155
11156 while (charidx >= 0 && byteidx < len)
11157 {
11158 if (charidx == 0)
11159 {
11160 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11161 break;
11162 }
11163 --charidx;
11164 byteidx += mb_cptr2len(str + byteidx);
11165 }
11166 }
11167#else
11168 if (charidx < len)
11169 rettv->vval.v_number = str[charidx];
11170#endif
11171}
11172
11173/*
11174 * "stridx()" function
11175 */
11176 static void
11177f_stridx(typval_T *argvars, typval_T *rettv)
11178{
11179 char_u buf[NUMBUFLEN];
11180 char_u *needle;
11181 char_u *haystack;
11182 char_u *save_haystack;
11183 char_u *pos;
11184 int start_idx;
11185
11186 needle = get_tv_string_chk(&argvars[1]);
11187 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11188 rettv->vval.v_number = -1;
11189 if (needle == NULL || haystack == NULL)
11190 return; /* type error; errmsg already given */
11191
11192 if (argvars[2].v_type != VAR_UNKNOWN)
11193 {
11194 int error = FALSE;
11195
11196 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11197 if (error || start_idx >= (int)STRLEN(haystack))
11198 return;
11199 if (start_idx >= 0)
11200 haystack += start_idx;
11201 }
11202
11203 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11204 if (pos != NULL)
11205 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11206}
11207
11208/*
11209 * "string()" function
11210 */
11211 static void
11212f_string(typval_T *argvars, typval_T *rettv)
11213{
11214 char_u *tofree;
11215 char_u numbuf[NUMBUFLEN];
11216
11217 rettv->v_type = VAR_STRING;
11218 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11219 get_copyID());
11220 /* Make a copy if we have a value but it's not in allocated memory. */
11221 if (rettv->vval.v_string != NULL && tofree == NULL)
11222 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11223}
11224
11225/*
11226 * "strlen()" function
11227 */
11228 static void
11229f_strlen(typval_T *argvars, typval_T *rettv)
11230{
11231 rettv->vval.v_number = (varnumber_T)(STRLEN(
11232 get_tv_string(&argvars[0])));
11233}
11234
11235/*
11236 * "strchars()" function
11237 */
11238 static void
11239f_strchars(typval_T *argvars, typval_T *rettv)
11240{
11241 char_u *s = get_tv_string(&argvars[0]);
11242 int skipcc = 0;
11243#ifdef FEAT_MBYTE
11244 varnumber_T len = 0;
11245 int (*func_mb_ptr2char_adv)(char_u **pp);
11246#endif
11247
11248 if (argvars[1].v_type != VAR_UNKNOWN)
11249 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11250 if (skipcc < 0 || skipcc > 1)
11251 EMSG(_(e_invarg));
11252 else
11253 {
11254#ifdef FEAT_MBYTE
11255 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11256 while (*s != NUL)
11257 {
11258 func_mb_ptr2char_adv(&s);
11259 ++len;
11260 }
11261 rettv->vval.v_number = len;
11262#else
11263 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11264#endif
11265 }
11266}
11267
11268/*
11269 * "strdisplaywidth()" function
11270 */
11271 static void
11272f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11273{
11274 char_u *s = get_tv_string(&argvars[0]);
11275 int col = 0;
11276
11277 if (argvars[1].v_type != VAR_UNKNOWN)
11278 col = (int)get_tv_number(&argvars[1]);
11279
11280 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11281}
11282
11283/*
11284 * "strwidth()" function
11285 */
11286 static void
11287f_strwidth(typval_T *argvars, typval_T *rettv)
11288{
11289 char_u *s = get_tv_string(&argvars[0]);
11290
11291 rettv->vval.v_number = (varnumber_T)(
11292#ifdef FEAT_MBYTE
11293 mb_string2cells(s, -1)
11294#else
11295 STRLEN(s)
11296#endif
11297 );
11298}
11299
11300/*
11301 * "strcharpart()" function
11302 */
11303 static void
11304f_strcharpart(typval_T *argvars, typval_T *rettv)
11305{
11306#ifdef FEAT_MBYTE
11307 char_u *p;
11308 int nchar;
11309 int nbyte = 0;
11310 int charlen;
11311 int len = 0;
11312 int slen;
11313 int error = FALSE;
11314
11315 p = get_tv_string(&argvars[0]);
11316 slen = (int)STRLEN(p);
11317
11318 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11319 if (!error)
11320 {
11321 if (nchar > 0)
11322 while (nchar > 0 && nbyte < slen)
11323 {
11324 nbyte += mb_cptr2len(p + nbyte);
11325 --nchar;
11326 }
11327 else
11328 nbyte = nchar;
11329 if (argvars[2].v_type != VAR_UNKNOWN)
11330 {
11331 charlen = (int)get_tv_number(&argvars[2]);
11332 while (charlen > 0 && nbyte + len < slen)
11333 {
11334 int off = nbyte + len;
11335
11336 if (off < 0)
11337 len += 1;
11338 else
11339 len += mb_cptr2len(p + off);
11340 --charlen;
11341 }
11342 }
11343 else
11344 len = slen - nbyte; /* default: all bytes that are available. */
11345 }
11346
11347 /*
11348 * Only return the overlap between the specified part and the actual
11349 * string.
11350 */
11351 if (nbyte < 0)
11352 {
11353 len += nbyte;
11354 nbyte = 0;
11355 }
11356 else if (nbyte > slen)
11357 nbyte = slen;
11358 if (len < 0)
11359 len = 0;
11360 else if (nbyte + len > slen)
11361 len = slen - nbyte;
11362
11363 rettv->v_type = VAR_STRING;
11364 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11365#else
11366 f_strpart(argvars, rettv);
11367#endif
11368}
11369
11370/*
11371 * "strpart()" function
11372 */
11373 static void
11374f_strpart(typval_T *argvars, typval_T *rettv)
11375{
11376 char_u *p;
11377 int n;
11378 int len;
11379 int slen;
11380 int error = FALSE;
11381
11382 p = get_tv_string(&argvars[0]);
11383 slen = (int)STRLEN(p);
11384
11385 n = (int)get_tv_number_chk(&argvars[1], &error);
11386 if (error)
11387 len = 0;
11388 else if (argvars[2].v_type != VAR_UNKNOWN)
11389 len = (int)get_tv_number(&argvars[2]);
11390 else
11391 len = slen - n; /* default len: all bytes that are available. */
11392
11393 /*
11394 * Only return the overlap between the specified part and the actual
11395 * string.
11396 */
11397 if (n < 0)
11398 {
11399 len += n;
11400 n = 0;
11401 }
11402 else if (n > slen)
11403 n = slen;
11404 if (len < 0)
11405 len = 0;
11406 else if (n + len > slen)
11407 len = slen - n;
11408
11409 rettv->v_type = VAR_STRING;
11410 rettv->vval.v_string = vim_strnsave(p + n, len);
11411}
11412
11413/*
11414 * "strridx()" function
11415 */
11416 static void
11417f_strridx(typval_T *argvars, typval_T *rettv)
11418{
11419 char_u buf[NUMBUFLEN];
11420 char_u *needle;
11421 char_u *haystack;
11422 char_u *rest;
11423 char_u *lastmatch = NULL;
11424 int haystack_len, end_idx;
11425
11426 needle = get_tv_string_chk(&argvars[1]);
11427 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11428
11429 rettv->vval.v_number = -1;
11430 if (needle == NULL || haystack == NULL)
11431 return; /* type error; errmsg already given */
11432
11433 haystack_len = (int)STRLEN(haystack);
11434 if (argvars[2].v_type != VAR_UNKNOWN)
11435 {
11436 /* Third argument: upper limit for index */
11437 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11438 if (end_idx < 0)
11439 return; /* can never find a match */
11440 }
11441 else
11442 end_idx = haystack_len;
11443
11444 if (*needle == NUL)
11445 {
11446 /* Empty string matches past the end. */
11447 lastmatch = haystack + end_idx;
11448 }
11449 else
11450 {
11451 for (rest = haystack; *rest != '\0'; ++rest)
11452 {
11453 rest = (char_u *)strstr((char *)rest, (char *)needle);
11454 if (rest == NULL || rest > haystack + end_idx)
11455 break;
11456 lastmatch = rest;
11457 }
11458 }
11459
11460 if (lastmatch == NULL)
11461 rettv->vval.v_number = -1;
11462 else
11463 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11464}
11465
11466/*
11467 * "strtrans()" function
11468 */
11469 static void
11470f_strtrans(typval_T *argvars, typval_T *rettv)
11471{
11472 rettv->v_type = VAR_STRING;
11473 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11474}
11475
11476/*
11477 * "submatch()" function
11478 */
11479 static void
11480f_submatch(typval_T *argvars, typval_T *rettv)
11481{
11482 int error = FALSE;
11483 int no;
11484 int retList = 0;
11485
11486 no = (int)get_tv_number_chk(&argvars[0], &error);
11487 if (error)
11488 return;
11489 error = FALSE;
11490 if (argvars[1].v_type != VAR_UNKNOWN)
11491 retList = (int)get_tv_number_chk(&argvars[1], &error);
11492 if (error)
11493 return;
11494
11495 if (retList == 0)
11496 {
11497 rettv->v_type = VAR_STRING;
11498 rettv->vval.v_string = reg_submatch(no);
11499 }
11500 else
11501 {
11502 rettv->v_type = VAR_LIST;
11503 rettv->vval.v_list = reg_submatch_list(no);
11504 }
11505}
11506
11507/*
11508 * "substitute()" function
11509 */
11510 static void
11511f_substitute(typval_T *argvars, typval_T *rettv)
11512{
11513 char_u patbuf[NUMBUFLEN];
11514 char_u subbuf[NUMBUFLEN];
11515 char_u flagsbuf[NUMBUFLEN];
11516
11517 char_u *str = get_tv_string_chk(&argvars[0]);
11518 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011519 char_u *sub = NULL;
11520 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011521 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11522
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011523 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11524 expr = &argvars[2];
11525 else
11526 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11527
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011528 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011529 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11530 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011531 rettv->vval.v_string = NULL;
11532 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011533 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011534}
11535
11536/*
11537 * "synID(lnum, col, trans)" function
11538 */
11539 static void
11540f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11541{
11542 int id = 0;
11543#ifdef FEAT_SYN_HL
11544 linenr_T lnum;
11545 colnr_T col;
11546 int trans;
11547 int transerr = FALSE;
11548
11549 lnum = get_tv_lnum(argvars); /* -1 on type error */
11550 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11551 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11552
11553 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11554 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11555 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11556#endif
11557
11558 rettv->vval.v_number = id;
11559}
11560
11561/*
11562 * "synIDattr(id, what [, mode])" function
11563 */
11564 static void
11565f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11566{
11567 char_u *p = NULL;
11568#ifdef FEAT_SYN_HL
11569 int id;
11570 char_u *what;
11571 char_u *mode;
11572 char_u modebuf[NUMBUFLEN];
11573 int modec;
11574
11575 id = (int)get_tv_number(&argvars[0]);
11576 what = get_tv_string(&argvars[1]);
11577 if (argvars[2].v_type != VAR_UNKNOWN)
11578 {
11579 mode = get_tv_string_buf(&argvars[2], modebuf);
11580 modec = TOLOWER_ASC(mode[0]);
11581 if (modec != 't' && modec != 'c' && modec != 'g')
11582 modec = 0; /* replace invalid with current */
11583 }
11584 else
11585 {
11586#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11587 if (USE_24BIT)
11588 modec = 'g';
11589 else
11590#endif
11591 if (t_colors > 1)
11592 modec = 'c';
11593 else
11594 modec = 't';
11595 }
11596
11597
11598 switch (TOLOWER_ASC(what[0]))
11599 {
11600 case 'b':
11601 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11602 p = highlight_color(id, what, modec);
11603 else /* bold */
11604 p = highlight_has_attr(id, HL_BOLD, modec);
11605 break;
11606
11607 case 'f': /* fg[#] or font */
11608 p = highlight_color(id, what, modec);
11609 break;
11610
11611 case 'i':
11612 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11613 p = highlight_has_attr(id, HL_INVERSE, modec);
11614 else /* italic */
11615 p = highlight_has_attr(id, HL_ITALIC, modec);
11616 break;
11617
11618 case 'n': /* name */
11619 p = get_highlight_name(NULL, id - 1);
11620 break;
11621
11622 case 'r': /* reverse */
11623 p = highlight_has_attr(id, HL_INVERSE, modec);
11624 break;
11625
11626 case 's':
11627 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11628 p = highlight_color(id, what, modec);
11629 else /* standout */
11630 p = highlight_has_attr(id, HL_STANDOUT, modec);
11631 break;
11632
11633 case 'u':
11634 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11635 /* underline */
11636 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11637 else
11638 /* undercurl */
11639 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11640 break;
11641 }
11642
11643 if (p != NULL)
11644 p = vim_strsave(p);
11645#endif
11646 rettv->v_type = VAR_STRING;
11647 rettv->vval.v_string = p;
11648}
11649
11650/*
11651 * "synIDtrans(id)" function
11652 */
11653 static void
11654f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11655{
11656 int id;
11657
11658#ifdef FEAT_SYN_HL
11659 id = (int)get_tv_number(&argvars[0]);
11660
11661 if (id > 0)
11662 id = syn_get_final_id(id);
11663 else
11664#endif
11665 id = 0;
11666
11667 rettv->vval.v_number = id;
11668}
11669
11670/*
11671 * "synconcealed(lnum, col)" function
11672 */
11673 static void
11674f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11675{
11676#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11677 linenr_T lnum;
11678 colnr_T col;
11679 int syntax_flags = 0;
11680 int cchar;
11681 int matchid = 0;
11682 char_u str[NUMBUFLEN];
11683#endif
11684
11685 rettv->v_type = VAR_LIST;
11686 rettv->vval.v_list = NULL;
11687
11688#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11689 lnum = get_tv_lnum(argvars); /* -1 on type error */
11690 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11691
11692 vim_memset(str, NUL, sizeof(str));
11693
11694 if (rettv_list_alloc(rettv) != FAIL)
11695 {
11696 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11697 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11698 && curwin->w_p_cole > 0)
11699 {
11700 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11701 syntax_flags = get_syntax_info(&matchid);
11702
11703 /* get the conceal character */
11704 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11705 {
11706 cchar = syn_get_sub_char();
11707 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
11708 cchar = lcs_conceal;
11709 if (cchar != NUL)
11710 {
11711# ifdef FEAT_MBYTE
11712 if (has_mbyte)
11713 (*mb_char2bytes)(cchar, str);
11714 else
11715# endif
11716 str[0] = cchar;
11717 }
11718 }
11719 }
11720
11721 list_append_number(rettv->vval.v_list,
11722 (syntax_flags & HL_CONCEAL) != 0);
11723 /* -1 to auto-determine strlen */
11724 list_append_string(rettv->vval.v_list, str, -1);
11725 list_append_number(rettv->vval.v_list, matchid);
11726 }
11727#endif
11728}
11729
11730/*
11731 * "synstack(lnum, col)" function
11732 */
11733 static void
11734f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11735{
11736#ifdef FEAT_SYN_HL
11737 linenr_T lnum;
11738 colnr_T col;
11739 int i;
11740 int id;
11741#endif
11742
11743 rettv->v_type = VAR_LIST;
11744 rettv->vval.v_list = NULL;
11745
11746#ifdef FEAT_SYN_HL
11747 lnum = get_tv_lnum(argvars); /* -1 on type error */
11748 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11749
11750 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11751 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11752 && rettv_list_alloc(rettv) != FAIL)
11753 {
11754 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11755 for (i = 0; ; ++i)
11756 {
11757 id = syn_get_stack_item(i);
11758 if (id < 0)
11759 break;
11760 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11761 break;
11762 }
11763 }
11764#endif
11765}
11766
11767 static void
11768get_cmd_output_as_rettv(
11769 typval_T *argvars,
11770 typval_T *rettv,
11771 int retlist)
11772{
11773 char_u *res = NULL;
11774 char_u *p;
11775 char_u *infile = NULL;
11776 char_u buf[NUMBUFLEN];
11777 int err = FALSE;
11778 FILE *fd;
11779 list_T *list = NULL;
11780 int flags = SHELL_SILENT;
11781
11782 rettv->v_type = VAR_STRING;
11783 rettv->vval.v_string = NULL;
11784 if (check_restricted() || check_secure())
11785 goto errret;
11786
11787 if (argvars[1].v_type != VAR_UNKNOWN)
11788 {
11789 /*
11790 * Write the string to a temp file, to be used for input of the shell
11791 * command.
11792 */
11793 if ((infile = vim_tempname('i', TRUE)) == NULL)
11794 {
11795 EMSG(_(e_notmp));
11796 goto errret;
11797 }
11798
11799 fd = mch_fopen((char *)infile, WRITEBIN);
11800 if (fd == NULL)
11801 {
11802 EMSG2(_(e_notopen), infile);
11803 goto errret;
11804 }
11805 if (argvars[1].v_type == VAR_LIST)
11806 {
11807 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11808 err = TRUE;
11809 }
11810 else
11811 {
11812 size_t len;
11813
11814 p = get_tv_string_buf_chk(&argvars[1], buf);
11815 if (p == NULL)
11816 {
11817 fclose(fd);
11818 goto errret; /* type error; errmsg already given */
11819 }
11820 len = STRLEN(p);
11821 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11822 err = TRUE;
11823 }
11824 if (fclose(fd) != 0)
11825 err = TRUE;
11826 if (err)
11827 {
11828 EMSG(_("E677: Error writing temp file"));
11829 goto errret;
11830 }
11831 }
11832
11833 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
11834 * echoes typeahead, that messes up the display. */
11835 if (!msg_silent)
11836 flags += SHELL_COOKED;
11837
11838 if (retlist)
11839 {
11840 int len;
11841 listitem_T *li;
11842 char_u *s = NULL;
11843 char_u *start;
11844 char_u *end;
11845 int i;
11846
11847 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
11848 if (res == NULL)
11849 goto errret;
11850
11851 list = list_alloc();
11852 if (list == NULL)
11853 goto errret;
11854
11855 for (i = 0; i < len; ++i)
11856 {
11857 start = res + i;
11858 while (i < len && res[i] != NL)
11859 ++i;
11860 end = res + i;
11861
11862 s = alloc((unsigned)(end - start + 1));
11863 if (s == NULL)
11864 goto errret;
11865
11866 for (p = s; start < end; ++p, ++start)
11867 *p = *start == NUL ? NL : *start;
11868 *p = NUL;
11869
11870 li = listitem_alloc();
11871 if (li == NULL)
11872 {
11873 vim_free(s);
11874 goto errret;
11875 }
11876 li->li_tv.v_type = VAR_STRING;
11877 li->li_tv.v_lock = 0;
11878 li->li_tv.vval.v_string = s;
11879 list_append(list, li);
11880 }
11881
11882 ++list->lv_refcount;
11883 rettv->v_type = VAR_LIST;
11884 rettv->vval.v_list = list;
11885 list = NULL;
11886 }
11887 else
11888 {
11889 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
11890#ifdef USE_CR
11891 /* translate <CR> into <NL> */
11892 if (res != NULL)
11893 {
11894 char_u *s;
11895
11896 for (s = res; *s; ++s)
11897 {
11898 if (*s == CAR)
11899 *s = NL;
11900 }
11901 }
11902#else
11903# ifdef USE_CRNL
11904 /* translate <CR><NL> into <NL> */
11905 if (res != NULL)
11906 {
11907 char_u *s, *d;
11908
11909 d = res;
11910 for (s = res; *s; ++s)
11911 {
11912 if (s[0] == CAR && s[1] == NL)
11913 ++s;
11914 *d++ = *s;
11915 }
11916 *d = NUL;
11917 }
11918# endif
11919#endif
11920 rettv->vval.v_string = res;
11921 res = NULL;
11922 }
11923
11924errret:
11925 if (infile != NULL)
11926 {
11927 mch_remove(infile);
11928 vim_free(infile);
11929 }
11930 if (res != NULL)
11931 vim_free(res);
11932 if (list != NULL)
11933 list_free(list);
11934}
11935
11936/*
11937 * "system()" function
11938 */
11939 static void
11940f_system(typval_T *argvars, typval_T *rettv)
11941{
11942 get_cmd_output_as_rettv(argvars, rettv, FALSE);
11943}
11944
11945/*
11946 * "systemlist()" function
11947 */
11948 static void
11949f_systemlist(typval_T *argvars, typval_T *rettv)
11950{
11951 get_cmd_output_as_rettv(argvars, rettv, TRUE);
11952}
11953
11954/*
11955 * "tabpagebuflist()" function
11956 */
11957 static void
11958f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11959{
11960#ifdef FEAT_WINDOWS
11961 tabpage_T *tp;
11962 win_T *wp = NULL;
11963
11964 if (argvars[0].v_type == VAR_UNKNOWN)
11965 wp = firstwin;
11966 else
11967 {
11968 tp = find_tabpage((int)get_tv_number(&argvars[0]));
11969 if (tp != NULL)
11970 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
11971 }
11972 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
11973 {
11974 for (; wp != NULL; wp = wp->w_next)
11975 if (list_append_number(rettv->vval.v_list,
11976 wp->w_buffer->b_fnum) == FAIL)
11977 break;
11978 }
11979#endif
11980}
11981
11982
11983/*
11984 * "tabpagenr()" function
11985 */
11986 static void
11987f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
11988{
11989 int nr = 1;
11990#ifdef FEAT_WINDOWS
11991 char_u *arg;
11992
11993 if (argvars[0].v_type != VAR_UNKNOWN)
11994 {
11995 arg = get_tv_string_chk(&argvars[0]);
11996 nr = 0;
11997 if (arg != NULL)
11998 {
11999 if (STRCMP(arg, "$") == 0)
12000 nr = tabpage_index(NULL) - 1;
12001 else
12002 EMSG2(_(e_invexpr2), arg);
12003 }
12004 }
12005 else
12006 nr = tabpage_index(curtab);
12007#endif
12008 rettv->vval.v_number = nr;
12009}
12010
12011
12012#ifdef FEAT_WINDOWS
12013static int get_winnr(tabpage_T *tp, typval_T *argvar);
12014
12015/*
12016 * Common code for tabpagewinnr() and winnr().
12017 */
12018 static int
12019get_winnr(tabpage_T *tp, typval_T *argvar)
12020{
12021 win_T *twin;
12022 int nr = 1;
12023 win_T *wp;
12024 char_u *arg;
12025
12026 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12027 if (argvar->v_type != VAR_UNKNOWN)
12028 {
12029 arg = get_tv_string_chk(argvar);
12030 if (arg == NULL)
12031 nr = 0; /* type error; errmsg already given */
12032 else if (STRCMP(arg, "$") == 0)
12033 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12034 else if (STRCMP(arg, "#") == 0)
12035 {
12036 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12037 if (twin == NULL)
12038 nr = 0;
12039 }
12040 else
12041 {
12042 EMSG2(_(e_invexpr2), arg);
12043 nr = 0;
12044 }
12045 }
12046
12047 if (nr > 0)
12048 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12049 wp != twin; wp = wp->w_next)
12050 {
12051 if (wp == NULL)
12052 {
12053 /* didn't find it in this tabpage */
12054 nr = 0;
12055 break;
12056 }
12057 ++nr;
12058 }
12059 return nr;
12060}
12061#endif
12062
12063/*
12064 * "tabpagewinnr()" function
12065 */
12066 static void
12067f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12068{
12069 int nr = 1;
12070#ifdef FEAT_WINDOWS
12071 tabpage_T *tp;
12072
12073 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12074 if (tp == NULL)
12075 nr = 0;
12076 else
12077 nr = get_winnr(tp, &argvars[1]);
12078#endif
12079 rettv->vval.v_number = nr;
12080}
12081
12082
12083/*
12084 * "tagfiles()" function
12085 */
12086 static void
12087f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12088{
12089 char_u *fname;
12090 tagname_T tn;
12091 int first;
12092
12093 if (rettv_list_alloc(rettv) == FAIL)
12094 return;
12095 fname = alloc(MAXPATHL);
12096 if (fname == NULL)
12097 return;
12098
12099 for (first = TRUE; ; first = FALSE)
12100 if (get_tagfname(&tn, first, fname) == FAIL
12101 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12102 break;
12103 tagname_free(&tn);
12104 vim_free(fname);
12105}
12106
12107/*
12108 * "taglist()" function
12109 */
12110 static void
12111f_taglist(typval_T *argvars, typval_T *rettv)
12112{
12113 char_u *tag_pattern;
12114
12115 tag_pattern = get_tv_string(&argvars[0]);
12116
12117 rettv->vval.v_number = FALSE;
12118 if (*tag_pattern == NUL)
12119 return;
12120
12121 if (rettv_list_alloc(rettv) == OK)
12122 (void)get_tags(rettv->vval.v_list, tag_pattern);
12123}
12124
12125/*
12126 * "tempname()" function
12127 */
12128 static void
12129f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12130{
12131 static int x = 'A';
12132
12133 rettv->v_type = VAR_STRING;
12134 rettv->vval.v_string = vim_tempname(x, FALSE);
12135
12136 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12137 * names. Skip 'I' and 'O', they are used for shell redirection. */
12138 do
12139 {
12140 if (x == 'Z')
12141 x = '0';
12142 else if (x == '9')
12143 x = 'A';
12144 else
12145 {
12146#ifdef EBCDIC
12147 if (x == 'I')
12148 x = 'J';
12149 else if (x == 'R')
12150 x = 'S';
12151 else
12152#endif
12153 ++x;
12154 }
12155 } while (x == 'I' || x == 'O');
12156}
12157
12158#ifdef FEAT_FLOAT
12159/*
12160 * "tan()" function
12161 */
12162 static void
12163f_tan(typval_T *argvars, typval_T *rettv)
12164{
12165 float_T f = 0.0;
12166
12167 rettv->v_type = VAR_FLOAT;
12168 if (get_float_arg(argvars, &f) == OK)
12169 rettv->vval.v_float = tan(f);
12170 else
12171 rettv->vval.v_float = 0.0;
12172}
12173
12174/*
12175 * "tanh()" function
12176 */
12177 static void
12178f_tanh(typval_T *argvars, typval_T *rettv)
12179{
12180 float_T f = 0.0;
12181
12182 rettv->v_type = VAR_FLOAT;
12183 if (get_float_arg(argvars, &f) == OK)
12184 rettv->vval.v_float = tanh(f);
12185 else
12186 rettv->vval.v_float = 0.0;
12187}
12188#endif
12189
12190/*
12191 * "test_alloc_fail(id, countdown, repeat)" function
12192 */
12193 static void
12194f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12195{
12196 if (argvars[0].v_type != VAR_NUMBER
12197 || argvars[0].vval.v_number <= 0
12198 || argvars[1].v_type != VAR_NUMBER
12199 || argvars[1].vval.v_number < 0
12200 || argvars[2].v_type != VAR_NUMBER)
12201 EMSG(_(e_invarg));
12202 else
12203 {
12204 alloc_fail_id = argvars[0].vval.v_number;
12205 if (alloc_fail_id >= aid_last)
12206 EMSG(_(e_invarg));
12207 alloc_fail_countdown = argvars[1].vval.v_number;
12208 alloc_fail_repeat = argvars[2].vval.v_number;
12209 did_outofmem_msg = FALSE;
12210 }
12211}
12212
12213/*
12214 * "test_autochdir()"
12215 */
12216 static void
12217f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12218{
12219#if defined(FEAT_AUTOCHDIR)
12220 test_autochdir = TRUE;
12221#endif
12222}
12223
12224/*
12225 * "test_disable_char_avail({expr})" function
12226 */
12227 static void
12228f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
12229{
12230 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
12231}
12232
12233/*
12234 * "test_garbagecollect_now()" function
12235 */
12236 static void
12237f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12238{
12239 /* This is dangerous, any Lists and Dicts used internally may be freed
12240 * while still in use. */
12241 garbage_collect(TRUE);
12242}
12243
12244#ifdef FEAT_JOB_CHANNEL
12245 static void
12246f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12247{
12248 rettv->v_type = VAR_CHANNEL;
12249 rettv->vval.v_channel = NULL;
12250}
12251#endif
12252
12253 static void
12254f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12255{
12256 rettv->v_type = VAR_DICT;
12257 rettv->vval.v_dict = NULL;
12258}
12259
12260#ifdef FEAT_JOB_CHANNEL
12261 static void
12262f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12263{
12264 rettv->v_type = VAR_JOB;
12265 rettv->vval.v_job = NULL;
12266}
12267#endif
12268
12269 static void
12270f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12271{
12272 rettv->v_type = VAR_LIST;
12273 rettv->vval.v_list = NULL;
12274}
12275
12276 static void
12277f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12278{
12279 rettv->v_type = VAR_PARTIAL;
12280 rettv->vval.v_partial = NULL;
12281}
12282
12283 static void
12284f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12285{
12286 rettv->v_type = VAR_STRING;
12287 rettv->vval.v_string = NULL;
12288}
12289
12290 static void
12291f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12292{
12293 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12294}
12295
12296#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12297/*
12298 * Get a callback from "arg". It can be a Funcref or a function name.
12299 * When "arg" is zero return an empty string.
12300 * Return NULL for an invalid argument.
12301 */
12302 char_u *
12303get_callback(typval_T *arg, partial_T **pp)
12304{
12305 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12306 {
12307 *pp = arg->vval.v_partial;
12308 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012309 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012310 }
12311 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012312 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012313 {
12314 func_ref(arg->vval.v_string);
12315 return arg->vval.v_string;
12316 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012317 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12318 return (char_u *)"";
12319 EMSG(_("E921: Invalid callback argument"));
12320 return NULL;
12321}
12322
12323/*
12324 * Unref/free "callback" and "partial" retured by get_callback().
12325 */
12326 void
12327free_callback(char_u *callback, partial_T *partial)
12328{
12329 if (partial != NULL)
12330 partial_unref(partial);
12331 else if (callback != NULL)
12332 {
12333 func_unref(callback);
12334 vim_free(callback);
12335 }
12336}
12337#endif
12338
12339#ifdef FEAT_TIMERS
12340/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012341 * "timer_info([timer])" function
12342 */
12343 static void
12344f_timer_info(typval_T *argvars, typval_T *rettv)
12345{
12346 timer_T *timer = NULL;
12347
12348 if (rettv_list_alloc(rettv) != OK)
12349 return;
12350 if (argvars[0].v_type != VAR_UNKNOWN)
12351 {
12352 if (argvars[0].v_type != VAR_NUMBER)
12353 EMSG(_(e_number_exp));
12354 else
12355 {
12356 timer = find_timer((int)get_tv_number(&argvars[0]));
12357 if (timer != NULL)
12358 add_timer_info(rettv, timer);
12359 }
12360 }
12361 else
12362 add_timer_info_all(rettv);
12363}
12364
12365/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012366 * "timer_pause(timer, paused)" function
12367 */
12368 static void
12369f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12370{
12371 timer_T *timer = NULL;
12372 int paused = (int)get_tv_number(&argvars[1]);
12373
12374 if (argvars[0].v_type != VAR_NUMBER)
12375 EMSG(_(e_number_exp));
12376 else
12377 {
12378 timer = find_timer((int)get_tv_number(&argvars[0]));
12379 if (timer != NULL)
12380 timer->tr_paused = paused;
12381 }
12382}
12383
12384/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012385 * "timer_start(time, callback [, options])" function
12386 */
12387 static void
12388f_timer_start(typval_T *argvars, typval_T *rettv)
12389{
12390 long msec = (long)get_tv_number(&argvars[0]);
12391 timer_T *timer;
12392 int repeat = 0;
12393 char_u *callback;
12394 dict_T *dict;
12395
12396 if (check_secure())
12397 return;
12398 if (argvars[2].v_type != VAR_UNKNOWN)
12399 {
12400 if (argvars[2].v_type != VAR_DICT
12401 || (dict = argvars[2].vval.v_dict) == NULL)
12402 {
12403 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12404 return;
12405 }
12406 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12407 repeat = get_dict_number(dict, (char_u *)"repeat");
12408 }
12409
12410 timer = create_timer(msec, repeat);
12411 callback = get_callback(&argvars[1], &timer->tr_partial);
12412 if (callback == NULL)
12413 {
12414 stop_timer(timer);
12415 rettv->vval.v_number = -1;
12416 }
12417 else
12418 {
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012419 if (timer->tr_partial == NULL)
12420 timer->tr_callback = vim_strsave(callback);
12421 else
12422 /* pointer into the partial */
12423 timer->tr_callback = callback;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012424 rettv->vval.v_number = timer->tr_id;
12425 }
12426}
12427
12428/*
12429 * "timer_stop(timer)" function
12430 */
12431 static void
12432f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12433{
12434 timer_T *timer;
12435
12436 if (argvars[0].v_type != VAR_NUMBER)
12437 {
12438 EMSG(_(e_number_exp));
12439 return;
12440 }
12441 timer = find_timer((int)get_tv_number(&argvars[0]));
12442 if (timer != NULL)
12443 stop_timer(timer);
12444}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012445
12446/*
12447 * "timer_stopall()" function
12448 */
12449 static void
12450f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12451{
12452 stop_all_timers();
12453}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012454#endif
12455
12456/*
12457 * "tolower(string)" function
12458 */
12459 static void
12460f_tolower(typval_T *argvars, typval_T *rettv)
12461{
12462 char_u *p;
12463
12464 p = vim_strsave(get_tv_string(&argvars[0]));
12465 rettv->v_type = VAR_STRING;
12466 rettv->vval.v_string = p;
12467
12468 if (p != NULL)
12469 while (*p != NUL)
12470 {
12471#ifdef FEAT_MBYTE
12472 int l;
12473
12474 if (enc_utf8)
12475 {
12476 int c, lc;
12477
12478 c = utf_ptr2char(p);
12479 lc = utf_tolower(c);
12480 l = utf_ptr2len(p);
12481 /* TODO: reallocate string when byte count changes. */
12482 if (utf_char2len(lc) == l)
12483 utf_char2bytes(lc, p);
12484 p += l;
12485 }
12486 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
12487 p += l; /* skip multi-byte character */
12488 else
12489#endif
12490 {
12491 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
12492 ++p;
12493 }
12494 }
12495}
12496
12497/*
12498 * "toupper(string)" function
12499 */
12500 static void
12501f_toupper(typval_T *argvars, typval_T *rettv)
12502{
12503 rettv->v_type = VAR_STRING;
12504 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12505}
12506
12507/*
12508 * "tr(string, fromstr, tostr)" function
12509 */
12510 static void
12511f_tr(typval_T *argvars, typval_T *rettv)
12512{
12513 char_u *in_str;
12514 char_u *fromstr;
12515 char_u *tostr;
12516 char_u *p;
12517#ifdef FEAT_MBYTE
12518 int inlen;
12519 int fromlen;
12520 int tolen;
12521 int idx;
12522 char_u *cpstr;
12523 int cplen;
12524 int first = TRUE;
12525#endif
12526 char_u buf[NUMBUFLEN];
12527 char_u buf2[NUMBUFLEN];
12528 garray_T ga;
12529
12530 in_str = get_tv_string(&argvars[0]);
12531 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
12532 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
12533
12534 /* Default return value: empty string. */
12535 rettv->v_type = VAR_STRING;
12536 rettv->vval.v_string = NULL;
12537 if (fromstr == NULL || tostr == NULL)
12538 return; /* type error; errmsg already given */
12539 ga_init2(&ga, (int)sizeof(char), 80);
12540
12541#ifdef FEAT_MBYTE
12542 if (!has_mbyte)
12543#endif
12544 /* not multi-byte: fromstr and tostr must be the same length */
12545 if (STRLEN(fromstr) != STRLEN(tostr))
12546 {
12547#ifdef FEAT_MBYTE
12548error:
12549#endif
12550 EMSG2(_(e_invarg2), fromstr);
12551 ga_clear(&ga);
12552 return;
12553 }
12554
12555 /* fromstr and tostr have to contain the same number of chars */
12556 while (*in_str != NUL)
12557 {
12558#ifdef FEAT_MBYTE
12559 if (has_mbyte)
12560 {
12561 inlen = (*mb_ptr2len)(in_str);
12562 cpstr = in_str;
12563 cplen = inlen;
12564 idx = 0;
12565 for (p = fromstr; *p != NUL; p += fromlen)
12566 {
12567 fromlen = (*mb_ptr2len)(p);
12568 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12569 {
12570 for (p = tostr; *p != NUL; p += tolen)
12571 {
12572 tolen = (*mb_ptr2len)(p);
12573 if (idx-- == 0)
12574 {
12575 cplen = tolen;
12576 cpstr = p;
12577 break;
12578 }
12579 }
12580 if (*p == NUL) /* tostr is shorter than fromstr */
12581 goto error;
12582 break;
12583 }
12584 ++idx;
12585 }
12586
12587 if (first && cpstr == in_str)
12588 {
12589 /* Check that fromstr and tostr have the same number of
12590 * (multi-byte) characters. Done only once when a character
12591 * of in_str doesn't appear in fromstr. */
12592 first = FALSE;
12593 for (p = tostr; *p != NUL; p += tolen)
12594 {
12595 tolen = (*mb_ptr2len)(p);
12596 --idx;
12597 }
12598 if (idx != 0)
12599 goto error;
12600 }
12601
12602 (void)ga_grow(&ga, cplen);
12603 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12604 ga.ga_len += cplen;
12605
12606 in_str += inlen;
12607 }
12608 else
12609#endif
12610 {
12611 /* When not using multi-byte chars we can do it faster. */
12612 p = vim_strchr(fromstr, *in_str);
12613 if (p != NULL)
12614 ga_append(&ga, tostr[p - fromstr]);
12615 else
12616 ga_append(&ga, *in_str);
12617 ++in_str;
12618 }
12619 }
12620
12621 /* add a terminating NUL */
12622 (void)ga_grow(&ga, 1);
12623 ga_append(&ga, NUL);
12624
12625 rettv->vval.v_string = ga.ga_data;
12626}
12627
12628#ifdef FEAT_FLOAT
12629/*
12630 * "trunc({float})" function
12631 */
12632 static void
12633f_trunc(typval_T *argvars, typval_T *rettv)
12634{
12635 float_T f = 0.0;
12636
12637 rettv->v_type = VAR_FLOAT;
12638 if (get_float_arg(argvars, &f) == OK)
12639 /* trunc() is not in C90, use floor() or ceil() instead. */
12640 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12641 else
12642 rettv->vval.v_float = 0.0;
12643}
12644#endif
12645
12646/*
12647 * "type(expr)" function
12648 */
12649 static void
12650f_type(typval_T *argvars, typval_T *rettv)
12651{
12652 int n = -1;
12653
12654 switch (argvars[0].v_type)
12655 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012656 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12657 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012658 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012659 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12660 case VAR_LIST: n = VAR_TYPE_LIST; break;
12661 case VAR_DICT: n = VAR_TYPE_DICT; break;
12662 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012663 case VAR_SPECIAL:
12664 if (argvars[0].vval.v_number == VVAL_FALSE
12665 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012666 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012667 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012668 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012669 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012670 case VAR_JOB: n = VAR_TYPE_JOB; break;
12671 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012672 case VAR_UNKNOWN:
12673 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
12674 n = -1;
12675 break;
12676 }
12677 rettv->vval.v_number = n;
12678}
12679
12680/*
12681 * "undofile(name)" function
12682 */
12683 static void
12684f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12685{
12686 rettv->v_type = VAR_STRING;
12687#ifdef FEAT_PERSISTENT_UNDO
12688 {
12689 char_u *fname = get_tv_string(&argvars[0]);
12690
12691 if (*fname == NUL)
12692 {
12693 /* If there is no file name there will be no undo file. */
12694 rettv->vval.v_string = NULL;
12695 }
12696 else
12697 {
12698 char_u *ffname = FullName_save(fname, FALSE);
12699
12700 if (ffname != NULL)
12701 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12702 vim_free(ffname);
12703 }
12704 }
12705#else
12706 rettv->vval.v_string = NULL;
12707#endif
12708}
12709
12710/*
12711 * "undotree()" function
12712 */
12713 static void
12714f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12715{
12716 if (rettv_dict_alloc(rettv) == OK)
12717 {
12718 dict_T *dict = rettv->vval.v_dict;
12719 list_T *list;
12720
12721 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
12722 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
12723 dict_add_nr_str(dict, "save_last",
12724 (long)curbuf->b_u_save_nr_last, NULL);
12725 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
12726 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
12727 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
12728
12729 list = list_alloc();
12730 if (list != NULL)
12731 {
12732 u_eval_tree(curbuf->b_u_oldhead, list);
12733 dict_add_list(dict, "entries", list);
12734 }
12735 }
12736}
12737
12738/*
12739 * "values(dict)" function
12740 */
12741 static void
12742f_values(typval_T *argvars, typval_T *rettv)
12743{
12744 dict_list(argvars, rettv, 1);
12745}
12746
12747/*
12748 * "virtcol(string)" function
12749 */
12750 static void
12751f_virtcol(typval_T *argvars, typval_T *rettv)
12752{
12753 colnr_T vcol = 0;
12754 pos_T *fp;
12755 int fnum = curbuf->b_fnum;
12756
12757 fp = var2fpos(&argvars[0], FALSE, &fnum);
12758 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12759 && fnum == curbuf->b_fnum)
12760 {
12761 getvvcol(curwin, fp, NULL, NULL, &vcol);
12762 ++vcol;
12763 }
12764
12765 rettv->vval.v_number = vcol;
12766}
12767
12768/*
12769 * "visualmode()" function
12770 */
12771 static void
12772f_visualmode(typval_T *argvars, typval_T *rettv)
12773{
12774 char_u str[2];
12775
12776 rettv->v_type = VAR_STRING;
12777 str[0] = curbuf->b_visual_mode_eval;
12778 str[1] = NUL;
12779 rettv->vval.v_string = vim_strsave(str);
12780
12781 /* A non-zero number or non-empty string argument: reset mode. */
12782 if (non_zero_arg(&argvars[0]))
12783 curbuf->b_visual_mode_eval = NUL;
12784}
12785
12786/*
12787 * "wildmenumode()" function
12788 */
12789 static void
12790f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12791{
12792#ifdef FEAT_WILDMENU
12793 if (wild_menu_showing)
12794 rettv->vval.v_number = 1;
12795#endif
12796}
12797
12798/*
12799 * "winbufnr(nr)" function
12800 */
12801 static void
12802f_winbufnr(typval_T *argvars, typval_T *rettv)
12803{
12804 win_T *wp;
12805
12806 wp = find_win_by_nr(&argvars[0], NULL);
12807 if (wp == NULL)
12808 rettv->vval.v_number = -1;
12809 else
12810 rettv->vval.v_number = wp->w_buffer->b_fnum;
12811}
12812
12813/*
12814 * "wincol()" function
12815 */
12816 static void
12817f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12818{
12819 validate_cursor();
12820 rettv->vval.v_number = curwin->w_wcol + 1;
12821}
12822
12823/*
12824 * "winheight(nr)" function
12825 */
12826 static void
12827f_winheight(typval_T *argvars, typval_T *rettv)
12828{
12829 win_T *wp;
12830
12831 wp = find_win_by_nr(&argvars[0], NULL);
12832 if (wp == NULL)
12833 rettv->vval.v_number = -1;
12834 else
12835 rettv->vval.v_number = wp->w_height;
12836}
12837
12838/*
12839 * "winline()" function
12840 */
12841 static void
12842f_winline(typval_T *argvars UNUSED, typval_T *rettv)
12843{
12844 validate_cursor();
12845 rettv->vval.v_number = curwin->w_wrow + 1;
12846}
12847
12848/*
12849 * "winnr()" function
12850 */
12851 static void
12852f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
12853{
12854 int nr = 1;
12855
12856#ifdef FEAT_WINDOWS
12857 nr = get_winnr(curtab, &argvars[0]);
12858#endif
12859 rettv->vval.v_number = nr;
12860}
12861
12862/*
12863 * "winrestcmd()" function
12864 */
12865 static void
12866f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
12867{
12868#ifdef FEAT_WINDOWS
12869 win_T *wp;
12870 int winnr = 1;
12871 garray_T ga;
12872 char_u buf[50];
12873
12874 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020012875 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012876 {
12877 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12878 ga_concat(&ga, buf);
12879 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12880 ga_concat(&ga, buf);
12881 ++winnr;
12882 }
12883 ga_append(&ga, NUL);
12884
12885 rettv->vval.v_string = ga.ga_data;
12886#else
12887 rettv->vval.v_string = NULL;
12888#endif
12889 rettv->v_type = VAR_STRING;
12890}
12891
12892/*
12893 * "winrestview()" function
12894 */
12895 static void
12896f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
12897{
12898 dict_T *dict;
12899
12900 if (argvars[0].v_type != VAR_DICT
12901 || (dict = argvars[0].vval.v_dict) == NULL)
12902 EMSG(_(e_invarg));
12903 else
12904 {
12905 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
12906 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
12907 if (dict_find(dict, (char_u *)"col", -1) != NULL)
12908 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
12909#ifdef FEAT_VIRTUALEDIT
12910 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
12911 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
12912#endif
12913 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
12914 {
12915 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
12916 curwin->w_set_curswant = FALSE;
12917 }
12918
12919 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
12920 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
12921#ifdef FEAT_DIFF
12922 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
12923 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
12924#endif
12925 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
12926 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
12927 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
12928 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
12929
12930 check_cursor();
12931 win_new_height(curwin, curwin->w_height);
12932# ifdef FEAT_WINDOWS
12933 win_new_width(curwin, W_WIDTH(curwin));
12934# endif
12935 changed_window_setting();
12936
12937 if (curwin->w_topline <= 0)
12938 curwin->w_topline = 1;
12939 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
12940 curwin->w_topline = curbuf->b_ml.ml_line_count;
12941#ifdef FEAT_DIFF
12942 check_topfill(curwin, TRUE);
12943#endif
12944 }
12945}
12946
12947/*
12948 * "winsaveview()" function
12949 */
12950 static void
12951f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
12952{
12953 dict_T *dict;
12954
12955 if (rettv_dict_alloc(rettv) == FAIL)
12956 return;
12957 dict = rettv->vval.v_dict;
12958
12959 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
12960 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
12961#ifdef FEAT_VIRTUALEDIT
12962 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
12963#endif
12964 update_curswant();
12965 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
12966
12967 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
12968#ifdef FEAT_DIFF
12969 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
12970#endif
12971 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
12972 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
12973}
12974
12975/*
12976 * "winwidth(nr)" function
12977 */
12978 static void
12979f_winwidth(typval_T *argvars, typval_T *rettv)
12980{
12981 win_T *wp;
12982
12983 wp = find_win_by_nr(&argvars[0], NULL);
12984 if (wp == NULL)
12985 rettv->vval.v_number = -1;
12986 else
12987#ifdef FEAT_WINDOWS
12988 rettv->vval.v_number = wp->w_width;
12989#else
12990 rettv->vval.v_number = Columns;
12991#endif
12992}
12993
12994/*
12995 * "wordcount()" function
12996 */
12997 static void
12998f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
12999{
13000 if (rettv_dict_alloc(rettv) == FAIL)
13001 return;
13002 cursor_pos_info(rettv->vval.v_dict);
13003}
13004
13005/*
13006 * "writefile()" function
13007 */
13008 static void
13009f_writefile(typval_T *argvars, typval_T *rettv)
13010{
13011 int binary = FALSE;
13012 int append = FALSE;
13013 char_u *fname;
13014 FILE *fd;
13015 int ret = 0;
13016
13017 if (check_restricted() || check_secure())
13018 return;
13019
13020 if (argvars[0].v_type != VAR_LIST)
13021 {
13022 EMSG2(_(e_listarg), "writefile()");
13023 return;
13024 }
13025 if (argvars[0].vval.v_list == NULL)
13026 return;
13027
13028 if (argvars[2].v_type != VAR_UNKNOWN)
13029 {
13030 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
13031 binary = TRUE;
13032 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
13033 append = TRUE;
13034 }
13035
13036 /* Always open the file in binary mode, library functions have a mind of
13037 * their own about CR-LF conversion. */
13038 fname = get_tv_string(&argvars[1]);
13039 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13040 append ? APPENDBIN : WRITEBIN)) == NULL)
13041 {
13042 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13043 ret = -1;
13044 }
13045 else
13046 {
13047 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
13048 ret = -1;
13049 fclose(fd);
13050 }
13051
13052 rettv->vval.v_number = ret;
13053}
13054
13055/*
13056 * "xor(expr, expr)" function
13057 */
13058 static void
13059f_xor(typval_T *argvars, typval_T *rettv)
13060{
13061 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13062 ^ get_tv_number_chk(&argvars[1], NULL);
13063}
13064
13065
13066#endif /* FEAT_EVAL */