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