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