blob: 89b523495d1c379b1fc388d3b366d604e3b2300f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* 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 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010030# ifdef FEAT_EVAL
31 scid_T uc_scriptID; /* SID where the command was defined */
32# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# endif
36} ucmd_T;
37
38#define UC_BUFFER 1 /* -buffer: local to current buffer */
39
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000040static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
43#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
44
45static void do_ucmd __ARGS((exarg_T *eap));
46static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static void ex_delcommand __ARGS((exarg_T *eap));
48# ifdef FEAT_CMDL_COMPL
49static char_u *get_user_command_name __ARGS((int idx));
50# endif
51
52#else
53# define ex_command ex_ni
54# define ex_comclear ex_ni
55# define ex_delcommand ex_ni
56#endif
57
58#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000061static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static int if_level = 0; /* depth in :if */
63#endif
Bram Moolenaara6f4d612011-09-21 19:10:46 +020064static void append_command __ARGS((char_u *cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static char_u *find_command __ARGS((exarg_T *eap, int *full));
66
67static void ex_abbreviate __ARGS((exarg_T *eap));
68static void ex_map __ARGS((exarg_T *eap));
69static void ex_unmap __ARGS((exarg_T *eap));
70static void ex_mapclear __ARGS((exarg_T *eap));
71static void ex_abclear __ARGS((exarg_T *eap));
72#ifndef FEAT_MENU
73# define ex_emenu ex_ni
74# define ex_menu ex_ni
75# define ex_menutranslate ex_ni
76#endif
77#ifdef FEAT_AUTOCMD
78static void ex_autocmd __ARGS((exarg_T *eap));
79static void ex_doautocmd __ARGS((exarg_T *eap));
80#else
81# define ex_autocmd ex_ni
82# define ex_doautocmd ex_ni
83# define ex_doautoall ex_ni
84#endif
85#ifdef FEAT_LISTCMDS
86static void ex_bunload __ARGS((exarg_T *eap));
87static void ex_buffer __ARGS((exarg_T *eap));
88static void ex_bmodified __ARGS((exarg_T *eap));
89static void ex_bnext __ARGS((exarg_T *eap));
90static void ex_bprevious __ARGS((exarg_T *eap));
91static void ex_brewind __ARGS((exarg_T *eap));
92static void ex_blast __ARGS((exarg_T *eap));
93#else
94# define ex_bunload ex_ni
95# define ex_buffer ex_ni
96# define ex_bmodified ex_ni
97# define ex_bnext ex_ni
98# define ex_bprevious ex_ni
99# define ex_brewind ex_ni
100# define ex_blast ex_ni
101# define buflist_list ex_ni
102# define ex_checktime ex_ni
103#endif
104#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
105# define ex_buffer_all ex_ni
106#endif
107static char_u *getargcmd __ARGS((char_u **));
108static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
109static int getargopt __ARGS((exarg_T *eap));
110#ifndef FEAT_QUICKFIX
111# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000112# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113# define ex_cc ex_ni
114# define ex_cnext ex_ni
115# define ex_cfile ex_ni
116# define qf_list ex_ni
117# define qf_age ex_ni
118# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
122# define ex_cclose ex_ni
123# define ex_copen ex_ni
124# define ex_cwindow ex_ni
125#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000126#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
127# define ex_cexpr ex_ni
128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
130static int check_more __ARGS((int, int));
131static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000132static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200133#if !defined(FEAT_PERL) \
134 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
135 || !defined(FEAT_TCL) \
136 || !defined(FEAT_RUBY) \
137 || !defined(FEAT_LUA) \
138 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000139# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void ex_script_ni __ARGS((exarg_T *eap));
141#endif
142static char_u *invalid_range __ARGS((exarg_T *eap));
143static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000144#ifdef FEAT_QUICKFIX
145static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
148static void ex_highlight __ARGS((exarg_T *eap));
149static void ex_colorscheme __ARGS((exarg_T *eap));
150static void ex_quit __ARGS((exarg_T *eap));
151static void ex_cquit __ARGS((exarg_T *eap));
152static void ex_quit_all __ARGS((exarg_T *eap));
153#ifdef FEAT_WINDOWS
154static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000155static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157static void ex_resize __ARGS((exarg_T *eap));
158static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000160static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000162static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#else
165# define ex_close ex_ni
166# define ex_only ex_ni
167# define ex_all ex_ni
168# define ex_resize ex_ni
169# define ex_splitview ex_ni
170# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000172# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000173# define ex_tabs ex_ni
174# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000175# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
178static void ex_pclose __ARGS((exarg_T *eap));
179static void ex_ptag __ARGS((exarg_T *eap));
180static void ex_pedit __ARGS((exarg_T *eap));
181#else
182# define ex_pclose ex_ni
183# define ex_ptag ex_ni
184# define ex_pedit ex_ni
185#endif
186static void ex_hide __ARGS((exarg_T *eap));
187static void ex_stop __ARGS((exarg_T *eap));
188static void ex_exit __ARGS((exarg_T *eap));
189static void ex_print __ARGS((exarg_T *eap));
190#ifdef FEAT_BYTEOFF
191static void ex_goto __ARGS((exarg_T *eap));
192#else
193# define ex_goto ex_ni
194#endif
195static void ex_shell __ARGS((exarg_T *eap));
196static void ex_preserve __ARGS((exarg_T *eap));
197static void ex_recover __ARGS((exarg_T *eap));
198#ifndef FEAT_LISTCMDS
199# define ex_argedit ex_ni
200# define ex_argadd ex_ni
201# define ex_argdelete ex_ni
202# define ex_listdo ex_ni
203#endif
204static void ex_mode __ARGS((exarg_T *eap));
205static void ex_wrongmodifier __ARGS((exarg_T *eap));
206static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000207static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static void ex_edit __ARGS((exarg_T *eap));
209#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
210# define ex_drop ex_ni
211#endif
212#ifndef FEAT_GUI
213# define ex_gui ex_nogui
214static void ex_nogui __ARGS((exarg_T *eap));
215#endif
216#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
217static void ex_tearoff __ARGS((exarg_T *eap));
218#else
219# define ex_tearoff ex_ni
220#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000221#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222static void ex_popup __ARGS((exarg_T *eap));
223#else
224# define ex_popup ex_ni
225#endif
226#ifndef FEAT_GUI_MSWIN
227# define ex_simalt ex_ni
228#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000229#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define gui_mch_find_dialog ex_ni
231# define gui_mch_replace_dialog ex_ni
232#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000233#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# define ex_helpfind ex_ni
235#endif
236#ifndef FEAT_CSCOPE
237# define do_cscope ex_ni
238# define do_scscope ex_ni
239# define do_cstag ex_ni
240#endif
241#ifndef FEAT_SYN_HL
242# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200243# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000244#endif
245#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000246# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000247# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000248# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000249# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000250# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000251#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200252#ifndef FEAT_PERSISTENT_UNDO
253# define ex_rundo ex_ni
254# define ex_wundo ex_ni
255#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200256#ifndef FEAT_LUA
257# define ex_lua ex_script_ni
258# define ex_luado ex_ni
259# define ex_luafile ex_ni
260#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000261#ifndef FEAT_MZSCHEME
262# define ex_mzscheme ex_script_ni
263# define ex_mzfile ex_ni
264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_PERL
266# define ex_perl ex_script_ni
267# define ex_perldo ex_ni
268#endif
269#ifndef FEAT_PYTHON
270# define ex_python ex_script_ni
271# define ex_pyfile ex_ni
272#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200273#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200274# define ex_py3 ex_script_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200275# define ex_py3file ex_ni
276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_TCL
278# define ex_tcl ex_script_ni
279# define ex_tcldo ex_ni
280# define ex_tclfile ex_ni
281#endif
282#ifndef FEAT_RUBY
283# define ex_ruby ex_script_ni
284# define ex_rubydo ex_ni
285# define ex_rubyfile ex_ni
286#endif
287#ifndef FEAT_SNIFF
288# define ex_sniff ex_ni
289#endif
290#ifndef FEAT_KEYMAP
291# define ex_loadkeymap ex_ni
292#endif
293static void ex_swapname __ARGS((exarg_T *eap));
294static void ex_syncbind __ARGS((exarg_T *eap));
295static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static void ex_pwd __ARGS((exarg_T *eap));
297static void ex_equal __ARGS((exarg_T *eap));
298static void ex_sleep __ARGS((exarg_T *eap));
299static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
300static void ex_winsize __ARGS((exarg_T *eap));
301#ifdef FEAT_WINDOWS
302static void ex_wincmd __ARGS((exarg_T *eap));
303#else
304# define ex_wincmd ex_ni
305#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000306#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307static void ex_winpos __ARGS((exarg_T *eap));
308#else
309# define ex_winpos ex_ni
310#endif
311static void ex_operators __ARGS((exarg_T *eap));
312static void ex_put __ARGS((exarg_T *eap));
313static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000314static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315static void ex_submagic __ARGS((exarg_T *eap));
316static void ex_join __ARGS((exarg_T *eap));
317static void ex_at __ARGS((exarg_T *eap));
318static void ex_bang __ARGS((exarg_T *eap));
319static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200320#ifdef FEAT_PERSISTENT_UNDO
321static void ex_wundo __ARGS((exarg_T *eap));
322static void ex_rundo __ARGS((exarg_T *eap));
323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000325static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static void ex_redir __ARGS((exarg_T *eap));
327static void ex_redraw __ARGS((exarg_T *eap));
328static void ex_redrawstatus __ARGS((exarg_T *eap));
329static void close_redir __ARGS((void));
330static void ex_mkrc __ARGS((exarg_T *eap));
331static void ex_mark __ARGS((exarg_T *eap));
332#ifdef FEAT_USR_CMDS
333static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000334static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#endif
336#ifdef FEAT_EX_EXTRA
337static void ex_normal __ARGS((exarg_T *eap));
338static void ex_startinsert __ARGS((exarg_T *eap));
339static void ex_stopinsert __ARGS((exarg_T *eap));
340#else
341# define ex_normal ex_ni
342# define ex_align ex_ni
343# define ex_retab ex_ni
344# define ex_startinsert ex_ni
345# define ex_stopinsert ex_ni
346# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000347# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#endif
349#ifdef FEAT_FIND_ID
350static void ex_checkpath __ARGS((exarg_T *eap));
351static void ex_findpat __ARGS((exarg_T *eap));
352#else
353# define ex_findpat ex_ni
354# define ex_checkpath ex_ni
355#endif
356#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
357static void ex_psearch __ARGS((exarg_T *eap));
358#else
359# define ex_psearch ex_ni
360#endif
361static void ex_tag __ARGS((exarg_T *eap));
362static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
363#ifndef FEAT_EVAL
364# define ex_scriptnames ex_ni
365# define ex_finish ex_ni
366# define ex_echo ex_ni
367# define ex_echohl ex_ni
368# define ex_execute ex_ni
369# define ex_call ex_ni
370# define ex_if ex_ni
371# define ex_endif ex_ni
372# define ex_else ex_ni
373# define ex_while ex_ni
374# define ex_continue ex_ni
375# define ex_break ex_ni
376# define ex_endwhile ex_ni
377# define ex_throw ex_ni
378# define ex_try ex_ni
379# define ex_catch ex_ni
380# define ex_finally ex_ni
381# define ex_endtry ex_ni
382# define ex_endfunction ex_ni
383# define ex_let ex_ni
384# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000385# define ex_lockvar ex_ni
386# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387# define ex_function ex_ni
388# define ex_delfunction ex_ni
389# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000390# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
392static char_u *arg_all __ARGS((void));
393#ifdef FEAT_SESSION
394static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000395static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396static void ex_loadview __ARGS((exarg_T *eap));
397static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000398static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#else
400# define ex_loadview ex_ni
401#endif
402#ifndef FEAT_EVAL
403# define ex_compiler ex_ni
404#endif
405#ifdef FEAT_VIMINFO
406static void ex_viminfo __ARGS((exarg_T *eap));
407#else
408# define ex_viminfo ex_ni
409#endif
410static void ex_behave __ARGS((exarg_T *eap));
411#ifdef FEAT_AUTOCMD
412static void ex_filetype __ARGS((exarg_T *eap));
413static void ex_setfiletype __ARGS((exarg_T *eap));
414#else
415# define ex_filetype ex_ni
416# define ex_setfiletype ex_ni
417#endif
418#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000419# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420# define ex_diffpatch ex_ni
421# define ex_diffgetput ex_ni
422# define ex_diffsplit ex_ni
423# define ex_diffthis ex_ni
424# define ex_diffupdate ex_ni
425#endif
426static void ex_digraphs __ARGS((exarg_T *eap));
427static void ex_set __ARGS((exarg_T *eap));
428#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
429# define ex_options ex_ni
430#endif
431#ifdef FEAT_SEARCH_EXTRA
432static void ex_nohlsearch __ARGS((exarg_T *eap));
433static void ex_match __ARGS((exarg_T *eap));
434#else
435# define ex_nohlsearch ex_ni
436# define ex_match ex_ni
437#endif
438#ifdef FEAT_CRYPT
439static void ex_X __ARGS((exarg_T *eap));
440#else
441# define ex_X ex_ni
442#endif
443#ifdef FEAT_FOLDING
444static void ex_fold __ARGS((exarg_T *eap));
445static void ex_foldopen __ARGS((exarg_T *eap));
446static void ex_folddo __ARGS((exarg_T *eap));
447#else
448# define ex_fold ex_ni
449# define ex_foldopen ex_ni
450# define ex_folddo ex_ni
451#endif
452#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
453 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
454# define ex_language ex_ni
455#endif
456#ifndef FEAT_SIGNS
457# define ex_sign ex_ni
458#endif
459#ifndef FEAT_SUN_WORKSHOP
460# define ex_wsverb ex_ni
461#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000462#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200463# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000464# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200465# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467
468#ifndef FEAT_EVAL
469# define ex_debug ex_ni
470# define ex_breakadd ex_ni
471# define ex_debuggreedy ex_ni
472# define ex_breakdel ex_ni
473# define ex_breaklist ex_ni
474#endif
475
476#ifndef FEAT_CMDHIST
477# define ex_history ex_ni
478#endif
479#ifndef FEAT_JUMPLIST
480# define ex_jumps ex_ni
481# define ex_changes ex_ni
482#endif
483
Bram Moolenaar05159a02005-02-26 23:04:13 +0000484#ifndef FEAT_PROFILE
485# define ex_profile ex_ni
486#endif
487
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488/*
489 * Declare cmdnames[].
490 */
491#define DO_DECLARE_EXCMD
492#include "ex_cmds.h"
493
494/*
495 * Table used to quickly search for a command, based on its first character.
496 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000497static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499 CMD_append,
500 CMD_buffer,
501 CMD_change,
502 CMD_delete,
503 CMD_edit,
504 CMD_file,
505 CMD_global,
506 CMD_help,
507 CMD_insert,
508 CMD_join,
509 CMD_k,
510 CMD_list,
511 CMD_move,
512 CMD_next,
513 CMD_open,
514 CMD_print,
515 CMD_quit,
516 CMD_read,
517 CMD_substitute,
518 CMD_t,
519 CMD_undo,
520 CMD_vglobal,
521 CMD_write,
522 CMD_xit,
523 CMD_yank,
524 CMD_z,
525 CMD_bang
526};
527
528static char_u dollar_command[2] = {'$', 0};
529
530
531#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000532/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533typedef struct
534{
535 char_u *line; /* command line */
536 linenr_T lnum; /* sourcing_lnum of the line */
537} wcmd_T;
538
539/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000540 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 garray_T *lines_gap; /* growarray with line info */
547 int current_line; /* last read line from growarray */
548 int repeating; /* TRUE when looping a second time */
549 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
550 char_u *(*getline) __ARGS((int, void *, int));
551 void *cookie;
552};
553
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000554static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
555static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000557
558/* Struct to save a few things while debugging. Used in do_cmdline() only. */
559struct dbg_stuff
560{
561 int trylevel;
562 int force_abort;
563 except_T *caught_stack;
564 char_u *vv_exception;
565 char_u *vv_throwpoint;
566 int did_emsg;
567 int got_int;
568 int did_throw;
569 int need_rethrow;
570 int check_cstack;
571 except_T *current_exception;
572};
573
574static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
575static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
576
577 static void
578save_dbg_stuff(dsp)
579 struct dbg_stuff *dsp;
580{
581 dsp->trylevel = trylevel; trylevel = 0;
582 dsp->force_abort = force_abort; force_abort = FALSE;
583 dsp->caught_stack = caught_stack; caught_stack = NULL;
584 dsp->vv_exception = v_exception(NULL);
585 dsp->vv_throwpoint = v_throwpoint(NULL);
586
587 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
588 dsp->did_emsg = did_emsg; did_emsg = FALSE;
589 dsp->got_int = got_int; got_int = FALSE;
590 dsp->did_throw = did_throw; did_throw = FALSE;
591 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
592 dsp->check_cstack = check_cstack; check_cstack = FALSE;
593 dsp->current_exception = current_exception; current_exception = NULL;
594}
595
596 static void
597restore_dbg_stuff(dsp)
598 struct dbg_stuff *dsp;
599{
600 suppress_errthrow = FALSE;
601 trylevel = dsp->trylevel;
602 force_abort = dsp->force_abort;
603 caught_stack = dsp->caught_stack;
604 (void)v_exception(dsp->vv_exception);
605 (void)v_throwpoint(dsp->vv_throwpoint);
606 did_emsg = dsp->did_emsg;
607 got_int = dsp->got_int;
608 did_throw = dsp->did_throw;
609 need_rethrow = dsp->need_rethrow;
610 check_cstack = dsp->check_cstack;
611 current_exception = dsp->current_exception;
612}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#endif
614
615
616/*
617 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
618 * command is given.
619 */
620 void
621do_exmode(improved)
622 int improved; /* TRUE for "improved Ex" mode */
623{
624 int save_msg_scroll;
625 int prev_msg_row;
626 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000627 int changedtick;
628
629 if (improved)
630 exmode_active = EXMODE_VIM;
631 else
632 exmode_active = EXMODE_NORMAL;
633 State = NORMAL;
634
635 /* When using ":global /pat/ visual" and then "Q" we return to continue
636 * the :global command. */
637 if (global_busy)
638 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640 save_msg_scroll = msg_scroll;
641 ++RedrawingDisabled; /* don't redisplay the window */
642 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#ifdef FEAT_GUI
644 /* Ignore scrollbar and mouse events in Ex mode */
645 ++hold_gui_events;
646#endif
647#ifdef FEAT_SNIFF
648 want_sniff_request = 0; /* No K_SNIFF wanted */
649#endif
650
651 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
652 while (exmode_active)
653 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000654#ifdef FEAT_EX_EXTRA
655 /* Check for a ":normal" command and no more characters left. */
656 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
657 {
658 exmode_active = FALSE;
659 break;
660 }
661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 msg_scroll = TRUE;
663 need_wait_return = FALSE;
664 ex_pressedreturn = FALSE;
665 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000666 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 prev_msg_row = msg_row;
668 prev_line = curwin->w_cursor.lnum;
669#ifdef FEAT_SNIFF
670 ProcessSniffRequests();
671#endif
672 if (improved)
673 {
674 cmdline_row = msg_row;
675 do_cmdline(NULL, getexline, NULL, 0);
676 }
677 else
678 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
679 lines_left = Rows - 1;
680
Bram Moolenaardf177f62005-02-22 08:39:57 +0000681 if ((prev_line != curwin->w_cursor.lnum
682 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000684 if (curbuf->b_ml.ml_flags & ML_EMPTY)
685 EMSG(_(e_emptybuf));
686 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000688 if (ex_pressedreturn)
689 {
690 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000691 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000692 msg_row = prev_msg_row;
693 if (prev_msg_row == Rows - 1)
694 msg_row--;
695 }
696 msg_col = 0;
697 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
698 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000701 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
702 {
703 if (curbuf->b_ml.ml_flags & ML_EMPTY)
704 EMSG(_(e_emptybuf));
705 else
706 EMSG(_("E501: At end-of-file"));
707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 }
709
710#ifdef FEAT_GUI
711 --hold_gui_events;
712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 --RedrawingDisabled;
714 --no_wait_return;
715 update_screen(CLEAR);
716 need_wait_return = FALSE;
717 msg_scroll = save_msg_scroll;
718}
719
720/*
721 * Execute a simple command line. Used for translated commands like "*".
722 */
723 int
724do_cmdline_cmd(cmd)
725 char_u *cmd;
726{
727 return do_cmdline(cmd, NULL, NULL,
728 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
729}
730
731/*
732 * do_cmdline(): execute one Ex command line
733 *
734 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100735 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 * 2. Split up in parts separated with '|'.
737 *
738 * This function can be called recursively!
739 *
740 * flags:
741 * DOCMD_VERBOSE - The command will be included in the error message.
742 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100743 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * DOCMD_KEYTYPED - Don't reset KeyTyped.
745 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
746 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
747 *
748 * return FAIL if cmdline could not be executed, OK otherwise
749 */
750 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100751do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753 char_u *(*fgetline) __ARGS((int, void *, int));
754 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 int flags;
756{
757 char_u *next_cmdline; /* next cmd to execute */
758 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100759 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 static int recursive = 0; /* recursive depth */
761 int msg_didout_before_start = 0;
762 int count = 0; /* line number count */
763 int did_inc = FALSE; /* incremented RedrawingDisabled */
764 int retval = OK;
765#ifdef FEAT_EVAL
766 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000767 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 int current_line = 0; /* active line in lines_ga */
769 char_u *fname = NULL; /* function or script name */
770 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
771 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000772 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 int initial_trylevel;
774 struct msglist **saved_msg_list = NULL;
775 struct msglist *private_msg_list;
776
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100777 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 char_u *(*cmd_getline) __ARGS((int, void *, int));
779 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000780 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000782 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100784# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785# define cmd_cookie cookie
786#endif
787 static int call_depth = 0; /* recursiveness */
788
789#ifdef FEAT_EVAL
790 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
791 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000792 * This ensures that the do_errthrow() call in do_one_cmd() does not
793 * combine the messages stored by an earlier invocation of do_one_cmd()
794 * with the command name of the later one. This would happen when
795 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 saved_msg_list = msg_list;
797 msg_list = &private_msg_list;
798 private_msg_list = NULL;
799#endif
800
801 /* It's possible to create an endless loop with ":execute", catch that
802 * here. The value of 200 allows nested function calls, ":source", etc. */
803 if (call_depth == 200)
804 {
805 EMSG(_("E169: Command too recursive"));
806#ifdef FEAT_EVAL
807 /* When converting to an exception, we do not include the command name
808 * since this is not an error of the specific command. */
809 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
810 msg_list = saved_msg_list;
811#endif
812 return FAIL;
813 }
814 ++call_depth;
815
816#ifdef FEAT_EVAL
817 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000818 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 cstack.cs_trylevel = 0;
820 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000821 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
823
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 ++ex_nesting_level;
830
831 /* Get the function or script name and the address where the next breakpoint
832 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
835 fname = func_name(real_cookie);
836 breakpoint = func_breakpoint(real_cookie);
837 dbg_tick = func_dbg_tick(real_cookie);
838 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100839 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
841 fname = sourcing_name;
842 breakpoint = source_breakpoint(real_cookie);
843 dbg_tick = source_dbg_tick(real_cookie);
844 }
845
846 /*
847 * Initialize "force_abort" and "suppress_errthrow" at the top level.
848 */
849 if (!recursive)
850 {
851 force_abort = FALSE;
852 suppress_errthrow = FALSE;
853 }
854
855 /*
856 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000857 * exception handling (used when debugging). Otherwise clear it to avoid
858 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000860 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000861 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000862 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200863 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865 initial_trylevel = trylevel;
866
867 /*
868 * "did_throw" will be set to TRUE when an exception is being thrown.
869 */
870 did_throw = FALSE;
871#endif
872 /*
873 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000874 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * If force_abort is set, we cancel everything.
876 */
877 did_emsg = FALSE;
878
879 /*
880 * KeyTyped is only set when calling vgetc(). Reset it here when not
881 * calling vgetc() (sourced command lines).
882 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (!(flags & DOCMD_KEYTYPED)
884 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 KeyTyped = FALSE;
886
887 /*
888 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000889 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * - for multiple commands on one line, separated with '|'
891 * - when repeating until there are no more lines (for ":source")
892 */
893 next_cmdline = cmdline;
894 do
895 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100897 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898#endif
899
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000900 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (next_cmdline == NULL
902#ifdef FEAT_EVAL
903 && !force_abort
904 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#endif
907 )
908 did_emsg = FALSE;
909
910 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000911 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100912 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 * 3. If a line is given: Make a copy, so we can mess with it.
914 */
915
916#ifdef FEAT_EVAL
917 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000918 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 /* Each '|' separated command is stored separately in lines_ga, to
921 * be able to jump to it. Don't use next_cmdline now. */
922 vim_free(cmdline_copy);
923 cmdline_copy = NULL;
924
925 /* Check if a function has returned or, unless it has an unclosed
926 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000930 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931 func_line_end(real_cookie);
932# endif
933 if (func_has_ended(real_cookie))
934 {
935 retval = FAIL;
936 break;
937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000939#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000940 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100941 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000942 script_line_end();
943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100946 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {
948 retval = FAIL;
949 break;
950 }
951
952 /* If breakpoints have been added/deleted need to check for it. */
953 if (breakpoint != NULL && dbg_tick != NULL
954 && *dbg_tick != debug_tick)
955 {
956 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100957 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 fname, sourcing_lnum);
959 *dbg_tick = debug_tick;
960 }
961
962 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
963 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
964
965 /* Did we encounter a breakpoint? */
966 if (breakpoint != NULL && *breakpoint != 0
967 && *breakpoint <= sourcing_lnum)
968 {
969 dbg_breakpoint(fname, sourcing_lnum);
970 /* Find next breakpoint. */
971 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100972 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 fname, sourcing_lnum);
974 *dbg_tick = debug_tick;
975 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000976# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000977 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000978 {
979 if (getline_is_func)
980 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100981 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000982 script_line_start();
983 }
984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
986
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100990 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 * below, so that it stores lines in or reads them from
992 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 * while/for loop. */
994 cmd_getline = get_loop_line;
995 cmd_cookie = (void *)&cmd_loop_cookie;
996 cmd_loop_cookie.lines_gap = &lines_ga;
997 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100998 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000999 cmd_loop_cookie.cookie = cookie;
1000 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002 else
1003 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001004 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 cmd_cookie = cookie;
1006 }
1007#endif
1008
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001009 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (next_cmdline == NULL)
1011 {
1012 /*
1013 * Need to set msg_didout for the first line after an ":if",
1014 * otherwise the ":if" will be overwritten.
1015 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001016 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001018 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#ifdef FEAT_EVAL
1020 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1021#else
1022 0
1023#endif
1024 )) == NULL)
1025 {
1026 /* Don't call wait_return for aborted command line. The NULL
1027 * returned for the end of a sourced file or executed function
1028 * doesn't do this. */
1029 if (KeyTyped && !(flags & DOCMD_REPEAT))
1030 need_wait_return = FALSE;
1031 retval = FAIL;
1032 break;
1033 }
1034 used_getline = TRUE;
1035
1036 /*
1037 * Keep the first typed line. Clear it when more lines are typed.
1038 */
1039 if (flags & DOCMD_KEEPLINE)
1040 {
1041 vim_free(repeat_cmdline);
1042 if (count == 0)
1043 repeat_cmdline = vim_strsave(next_cmdline);
1044 else
1045 repeat_cmdline = NULL;
1046 }
1047 }
1048
1049 /* 3. Make a copy of the command so we can mess with it. */
1050 else if (cmdline_copy == NULL)
1051 {
1052 next_cmdline = vim_strsave(next_cmdline);
1053 if (next_cmdline == NULL)
1054 {
1055 EMSG(_(e_outofmem));
1056 retval = FAIL;
1057 break;
1058 }
1059 }
1060 cmdline_copy = next_cmdline;
1061
1062#ifdef FEAT_EVAL
1063 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 * Save the current line when inside a ":while" or ":for", and when
1065 * the command looks like a ":while" or ":for", because we may need it
1066 * later. When there is a '|' and another command, it is stored
1067 * separately, because we need to be able to jump back to it from an
1068 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 if (current_line == lines_ga.ga_len
1071 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001073 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
1075 retval = FAIL;
1076 break;
1077 }
1078 }
1079 did_endif = FALSE;
1080#endif
1081
1082 if (count++ == 0)
1083 {
1084 /*
1085 * All output from the commands is put below each other, without
1086 * waiting for a return. Don't do this when executing commands
1087 * from a script or when being called recursive (e.g. for ":e
1088 * +command file").
1089 */
1090 if (!(flags & DOCMD_NOWAIT) && !recursive)
1091 {
1092 msg_didout_before_start = msg_didout;
1093 msg_didany = FALSE; /* no output yet */
1094 msg_start();
1095 msg_scroll = TRUE; /* put messages below each other */
1096 ++no_wait_return; /* dont wait for return until finished */
1097 ++RedrawingDisabled;
1098 did_inc = TRUE;
1099 }
1100 }
1101
1102 if (p_verbose >= 15 && sourcing_name != NULL)
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001105 verbose_enter_scroll();
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 smsg((char_u *)_("line %ld: %s"),
1108 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001109 if (msg_silent == 0)
1110 msg_puts((char_u *)"\n"); /* don't overwrite this */
1111
1112 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 --no_wait_return;
1114 }
1115
1116 /*
1117 * 2. Execute one '|' separated command.
1118 * do_one_cmd() will return NULL if there is no trailing '|'.
1119 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1120 */
1121 ++recursive;
1122 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1123#ifdef FEAT_EVAL
1124 &cstack,
1125#endif
1126 cmd_getline, cmd_cookie);
1127 --recursive;
1128
1129#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 if (cmd_cookie == (void *)&cmd_loop_cookie)
1131 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#endif
1135
1136 if (next_cmdline == NULL)
1137 {
1138 vim_free(cmdline_copy);
1139 cmdline_copy = NULL;
1140#ifdef FEAT_CMDHIST
1141 /*
1142 * If the command was typed, remember it for the ':' register.
1143 * Do this AFTER executing the command to make :@: work.
1144 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001145 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 && new_last_cmdline != NULL)
1147 {
1148 vim_free(last_cmdline);
1149 last_cmdline = new_last_cmdline;
1150 new_last_cmdline = NULL;
1151 }
1152#endif
1153 }
1154 else
1155 {
1156 /* need to copy the command after the '|' to cmdline_copy, for the
1157 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001158 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 next_cmdline = cmdline_copy;
1160 }
1161
1162
1163#ifdef FEAT_EVAL
1164 /* reset did_emsg for a function that is not aborted by an error */
1165 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001166 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 && !func_has_abort(real_cookie))
1168 did_emsg = FALSE;
1169
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172 ++current_line;
1173
1174 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 * An ":endwhile", ":endfor" and ":continue" is handled here.
1176 * If we were executing commands, jump back to the ":while" or
1177 * ":for".
1178 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 /* Jump back to the matching ":while" or ":for". Be careful
1185 * not to use a cs_line[] from an entry that isn't a ":while"
1186 * or ":for": It would make "current_line" invalid and can
1187 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (!did_emsg && !got_int && !did_throw
1189 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 && (cstack.cs_flags[cstack.cs_idx]
1191 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 && cstack.cs_line[cstack.cs_idx] >= 0
1193 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1194 {
1195 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 /* remember we jumped there */
1197 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 line_breakcheck(); /* check if CTRL-C typed */
1199
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 /* Check for the next breakpoint at or after the ":while"
1201 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (breakpoint != NULL)
1203 {
1204 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001205 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 fname,
1207 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1208 *dbg_tick = debug_tick;
1209 }
1210 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001215 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1216 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 }
1219
1220 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1227 }
1228 }
1229
1230 /*
1231 * When not inside any ":while" loop, clear remembered lines.
1232 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {
1235 if (lines_ga.ga_len > 0)
1236 {
1237 sourcing_lnum =
1238 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1239 free_cmdlines(&lines_ga);
1240 }
1241 current_line = 0;
1242 }
1243
1244 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001245 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1246 * being restored at the ":endtry". Reset them here and set the
1247 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1248 * This includes the case where a missing ":endif", ":endwhile" or
1249 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001251 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 cstack.cs_lflags &= ~CSL_HAD_FINA;
1254 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1255 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 did_throw ? (void *)current_exception : NULL);
1257 did_emsg = got_int = did_throw = FALSE;
1258 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1259 }
1260
1261 /* Update global "trylevel" for recursive calls to do_cmdline() from
1262 * within this loop. */
1263 trylevel = initial_trylevel + cstack.cs_trylevel;
1264
1265 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001266 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 * files) is aborted because of an error, an interrupt, or an uncaught
1268 * exception, cancel everything. If it is left normally, reset
1269 * force_abort to get the non-EH compatible abortion behavior for
1270 * the rest of the script.
1271 */
1272 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1273 force_abort = FALSE;
1274
1275 /* Convert an interrupt to an exception if appropriate. */
1276 (void)do_intthrow(&cstack);
1277#endif /* FEAT_EVAL */
1278
1279 }
1280 /*
1281 * Continue executing command lines when:
1282 * - no CTRL-C typed, no aborting error, no exception thrown or try
1283 * conditionals need to be checked for executing finally clauses or
1284 * catching an interrupt exception
1285 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001286 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 * looping for ":source" command or function call.
1288 */
1289 while (!((got_int
1290#ifdef FEAT_EVAL
1291 || (did_emsg && force_abort) || did_throw
1292#endif
1293 )
1294#ifdef FEAT_EVAL
1295 && cstack.cs_trylevel == 0
1296#endif
1297 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001298 && !(did_emsg
1299#ifdef FEAT_EVAL
1300 /* Keep going when inside try/catch, so that the error can be
1301 * dealth with, except when it is a syntax error, it may cause
1302 * the :endtry to be missed. */
1303 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1304#endif
1305 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001306 && (getline_equal(fgetline, cookie, getexmodeline)
1307 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 && (next_cmdline != NULL
1309#ifdef FEAT_EVAL
1310 || cstack.cs_idx >= 0
1311#endif
1312 || (flags & DOCMD_REPEAT)));
1313
1314 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001315 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316#ifdef FEAT_EVAL
1317 free_cmdlines(&lines_ga);
1318 ga_clear(&lines_ga);
1319
1320 if (cstack.cs_idx >= 0)
1321 {
1322 /*
1323 * If a sourced file or executed function ran to its end, report the
1324 * unclosed conditional.
1325 */
1326 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001327 && ((getline_equal(fgetline, cookie, getsourceline)
1328 && !source_finished(fgetline, cookie))
1329 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 && !func_has_ended(real_cookie))))
1331 {
1332 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1333 EMSG(_(e_endtry));
1334 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1335 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001336 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1337 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 else
1339 EMSG(_(e_endif));
1340 }
1341
1342 /*
1343 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1344 * ":endtry" in a sourced file or executed function. If the try
1345 * conditional is in its finally clause, ignore anything pending.
1346 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001347 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 */
1349 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001350 {
1351 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1352
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001353 if (idx >= 0)
1354 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001355 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1356 &cstack.cs_looplevel);
1357 }
1358 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 trylevel = initial_trylevel;
1360 }
1361
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001362 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1363 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001365 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 ? (char_u *)"endfunction" : (char_u *)NULL);
1367
1368 if (trylevel == 0)
1369 {
1370 /*
1371 * When an exception is being thrown out of the outermost try
1372 * conditional, discard the uncaught exception, disable the conversion
1373 * of interrupts or errors to exceptions, and ensure that no more
1374 * commands are executed.
1375 */
1376 if (did_throw)
1377 {
1378 void *p = NULL;
1379 char_u *saved_sourcing_name;
1380 int saved_sourcing_lnum;
1381 struct msglist *messages = NULL, *next;
1382
1383 /*
1384 * If the uncaught exception is a user exception, report it as an
1385 * error. If it is an error exception, display the saved error
1386 * message now. For an interrupt exception, do nothing; the
1387 * interrupt message is given elsewhere.
1388 */
1389 switch (current_exception->type)
1390 {
1391 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001392 vim_snprintf((char *)IObuff, IOSIZE,
1393 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 current_exception->value);
1395 p = vim_strsave(IObuff);
1396 break;
1397 case ET_ERROR:
1398 messages = current_exception->messages;
1399 current_exception->messages = NULL;
1400 break;
1401 case ET_INTERRUPT:
1402 break;
1403 default:
1404 p = vim_strsave((char_u *)_(e_internal));
1405 }
1406
1407 saved_sourcing_name = sourcing_name;
1408 saved_sourcing_lnum = sourcing_lnum;
1409 sourcing_name = current_exception->throw_name;
1410 sourcing_lnum = current_exception->throw_lnum;
1411 current_exception->throw_name = NULL;
1412
1413 discard_current_exception(); /* uses IObuff if 'verbose' */
1414 suppress_errthrow = TRUE;
1415 force_abort = TRUE;
1416
1417 if (messages != NULL)
1418 {
1419 do
1420 {
1421 next = messages->next;
1422 emsg(messages->msg);
1423 vim_free(messages->msg);
1424 vim_free(messages);
1425 messages = next;
1426 }
1427 while (messages != NULL);
1428 }
1429 else if (p != NULL)
1430 {
1431 emsg(p);
1432 vim_free(p);
1433 }
1434 vim_free(sourcing_name);
1435 sourcing_name = saved_sourcing_name;
1436 sourcing_lnum = saved_sourcing_lnum;
1437 }
1438
1439 /*
1440 * On an interrupt or an aborting error not converted to an exception,
1441 * disable the conversion of errors to exceptions. (Interrupts are not
1442 * converted any more, here.) This enables also the interrupt message
1443 * when force_abort is set and did_emsg unset in case of an interrupt
1444 * from a finally clause after an error.
1445 */
1446 else if (got_int || (did_emsg && force_abort))
1447 suppress_errthrow = TRUE;
1448 }
1449
1450 /*
1451 * The current cstack will be freed when do_cmdline() returns. An uncaught
1452 * exception will have to be rethrown in the previous cstack. If a function
1453 * has just returned or a script file was just finished and the previous
1454 * cstack belongs to the same function or, respectively, script file, it
1455 * will have to be checked for finally clauses to be executed due to the
1456 * ":return" or ":finish". This is done in do_one_cmd().
1457 */
1458 if (did_throw)
1459 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001460 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001462 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 && ex_nesting_level > func_level(real_cookie) + 1))
1464 {
1465 if (!did_throw)
1466 check_cstack = TRUE;
1467 }
1468 else
1469 {
1470 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001471 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 --ex_nesting_level;
1473 /*
1474 * Go to debug mode when returning from a function in which we are
1475 * single-stepping.
1476 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001477 if ((getline_equal(fgetline, cookie, getsourceline)
1478 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001480 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 ? (char_u *)_("End of sourced file")
1482 : (char_u *)_("End of function"));
1483 }
1484
1485 /*
1486 * Restore the exception environment (done after returning from the
1487 * debugger).
1488 */
1489 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001490 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 msg_list = saved_msg_list;
1493#endif /* FEAT_EVAL */
1494
1495 /*
1496 * If there was too much output to fit on the command line, ask the user to
1497 * hit return before redrawing the screen. With the ":global" command we do
1498 * this only once after the command is finished.
1499 */
1500 if (did_inc)
1501 {
1502 --RedrawingDisabled;
1503 --no_wait_return;
1504 msg_scroll = FALSE;
1505
1506 /*
1507 * When just finished an ":if"-":else" which was typed, no need to
1508 * wait for hit-return. Also for an error situation.
1509 */
1510 if (retval == FAIL
1511#ifdef FEAT_EVAL
1512 || (did_endif && KeyTyped && !did_emsg)
1513#endif
1514 )
1515 {
1516 need_wait_return = FALSE;
1517 msg_didany = FALSE; /* don't wait when restarting edit */
1518 }
1519 else if (need_wait_return)
1520 {
1521 /*
1522 * The msg_start() above clears msg_didout. The wait_return we do
1523 * here should not overwrite the command that may be shown before
1524 * doing that.
1525 */
1526 msg_didout |= msg_didout_before_start;
1527 wait_return(FALSE);
1528 }
1529 }
1530
Bram Moolenaar2a942252012-11-28 23:03:07 +01001531#ifdef FEAT_EVAL
1532 did_endif = FALSE; /* in case do_cmdline used recursively */
1533#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 /*
1535 * Reset if_level, in case a sourced script file contains more ":if" than
1536 * ":endif" (could be ":if x | foo | endif").
1537 */
1538 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001539#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001540
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 --call_depth;
1542 return retval;
1543}
1544
1545#ifdef FEAT_EVAL
1546/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 */
1549 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001550get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 int c;
1552 void *cookie;
1553 int indent;
1554{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 wcmd_T *wp;
1557 char_u *line;
1558
1559 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1560 {
1561 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001562 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001564 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (cp->getline == NULL)
1566 line = getcmdline(c, 0L, indent);
1567 else
1568 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001569 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 ++cp->current_line;
1571
1572 return line;
1573 }
1574
1575 KeyTyped = FALSE;
1576 ++cp->current_line;
1577 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1578 sourcing_lnum = wp->lnum;
1579 return vim_strsave(wp->line);
1580}
1581
1582/*
1583 * Store a line in "gap" so that a ":while" loop can execute it again.
1584 */
1585 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001586store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 garray_T *gap;
1588 char_u *line;
1589{
1590 if (ga_grow(gap, 1) == FAIL)
1591 return FAIL;
1592 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1593 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1594 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 return OK;
1596}
1597
1598/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001599 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 */
1601 static void
1602free_cmdlines(gap)
1603 garray_T *gap;
1604{
1605 while (gap->ga_len > 0)
1606 {
1607 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1608 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610}
1611#endif
1612
1613/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001614 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1615 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001618getline_equal(fgetline, cookie, func)
1619 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001620 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 char_u *(*func) __ARGS((int, void *, int));
1622{
1623#ifdef FEAT_EVAL
1624 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001625 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
Bram Moolenaar89d40322006-08-29 15:30:07 +00001627 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001628 * function that's originally used to obtain the lines. This may be
1629 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001630 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001631 cp = (struct loop_cookie *)cookie;
1632 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 {
1634 gp = cp->getline;
1635 cp = cp->cookie;
1636 }
1637 return gp == func;
1638#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640#endif
1641}
1642
1643#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1644/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001645 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 * getline function. Otherwise return "cookie".
1647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001649getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001650 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001651 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
1653# ifdef FEAT_EVAL
1654 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001655 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaar89d40322006-08-29 15:30:07 +00001657 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001658 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001660 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001661 cp = (struct loop_cookie *)cookie;
1662 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 gp = cp->getline;
1665 cp = cp->cookie;
1666 }
1667 return cp;
1668# else
1669 return cookie;
1670# endif
1671}
1672#endif
1673
1674/*
1675 * Execute one Ex command.
1676 *
1677 * If 'sourcing' is TRUE, the command will be included in the error message.
1678 *
1679 * 1. skip comment lines and leading space
1680 * 2. handle command modifiers
1681 * 3. parse range
1682 * 4. parse command
1683 * 5. parse arguments
1684 * 6. switch on command name
1685 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001686 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 *
1688 * This function may be called recursively!
1689 */
1690#if (_MSC_VER == 1200)
1691/*
Bram Moolenaared203462004-06-16 11:19:22 +00001692 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001694 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695#endif
1696 static char_u *
1697do_one_cmd(cmdlinep, sourcing,
1698#ifdef FEAT_EVAL
1699 cstack,
1700#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001701 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 char_u **cmdlinep;
1703 int sourcing;
1704#ifdef FEAT_EVAL
1705 struct condstack *cstack;
1706#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001707 char_u *(*fgetline) __ARGS((int, void *, int));
1708 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710 char_u *p;
1711 linenr_T lnum;
1712 long n;
1713 char_u *errormsg = NULL; /* error message */
1714 exarg_T ea; /* Ex command arguments */
1715 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001716 int save_msg_scroll = msg_scroll;
1717 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001719#ifdef HAVE_SANDBOX
1720 int did_sandbox = FALSE;
1721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 cmdmod_T save_cmdmod;
1723 int ni; /* set when Not Implemented */
1724
1725 vim_memset(&ea, 0, sizeof(ea));
1726 ea.line1 = 1;
1727 ea.line2 = 1;
1728#ifdef FEAT_EVAL
1729 ++ex_nesting_level;
1730#endif
1731
Bram Moolenaar21691f82012-12-05 19:13:18 +01001732 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if (quitmore
1734#ifdef FEAT_EVAL
1735 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001736 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001737#endif
1738#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001739 /* avoid that an autocommand, e.g. QuitPre, does this */
1740 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741#endif
1742 )
1743 --quitmore;
1744
1745 /*
1746 * Reset browse, confirm, etc.. They are restored when returning, for
1747 * recursive calls.
1748 */
1749 save_cmdmod = cmdmod;
1750 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1751
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001752 /* "#!anything" is handled like a comment. */
1753 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1754 goto doend;
1755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 /*
1757 * Repeat until no more command modifiers are found.
1758 */
1759 ea.cmd = *cmdlinep;
1760 for (;;)
1761 {
1762/*
1763 * 1. skip comment lines and leading white space and colons
1764 */
1765 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1766 ++ea.cmd;
1767
1768 /* in ex mode, an empty line works like :+ */
1769 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001770 && (getline_equal(fgetline, cookie, getexmodeline)
1771 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001772 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
1774 ea.cmd = (char_u *)"+";
1775 ex_pressedreturn = TRUE;
1776 }
1777
1778 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001779 if (*ea.cmd == '"')
1780 goto doend;
1781 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001782 {
1783 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
1787/*
1788 * 2. handle command modifiers.
1789 */
1790 p = ea.cmd;
1791 if (VIM_ISDIGIT(*ea.cmd))
1792 p = skipwhite(skipdigits(ea.cmd));
1793 switch (*p)
1794 {
1795 /* When adding an entry, also modify cmd_exists(). */
1796 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1797 break;
1798#ifdef FEAT_WINDOWS
1799 cmdmod.split |= WSP_ABOVE;
1800#endif
1801 continue;
1802
1803 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1804 {
1805#ifdef FEAT_WINDOWS
1806 cmdmod.split |= WSP_BELOW;
1807#endif
1808 continue;
1809 }
1810 if (checkforcmd(&ea.cmd, "browse", 3))
1811 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001812#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 cmdmod.browse = TRUE;
1814#endif
1815 continue;
1816 }
1817 if (!checkforcmd(&ea.cmd, "botright", 2))
1818 break;
1819#ifdef FEAT_WINDOWS
1820 cmdmod.split |= WSP_BOT;
1821#endif
1822 continue;
1823
1824 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1825 break;
1826#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1827 cmdmod.confirm = TRUE;
1828#endif
1829 continue;
1830
1831 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1832 {
1833 cmdmod.keepmarks = TRUE;
1834 continue;
1835 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001836 if (checkforcmd(&ea.cmd, "keepalt", 5))
1837 {
1838 cmdmod.keepalt = TRUE;
1839 continue;
1840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1842 break;
1843 cmdmod.keepjumps = TRUE;
1844 continue;
1845
1846 /* ":hide" and ":hide | cmd" are not modifiers */
1847 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1848 || *p == NUL || ends_excmd(*p))
1849 break;
1850 ea.cmd = p;
1851 cmdmod.hide = TRUE;
1852 continue;
1853
1854 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1855 {
1856 cmdmod.lockmarks = TRUE;
1857 continue;
1858 }
1859
1860 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1861 break;
1862#ifdef FEAT_WINDOWS
1863 cmdmod.split |= WSP_ABOVE;
1864#endif
1865 continue;
1866
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001867 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1868 break;
1869#ifdef FEAT_AUTOCMD
1870 if (cmdmod.save_ei == NULL)
1871 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001872 /* Set 'eventignore' to "all". Restore the
1873 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001874 cmdmod.save_ei = vim_strsave(p_ei);
1875 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001876 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001877 }
1878#endif
1879 continue;
1880
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1882 break;
1883#ifdef FEAT_WINDOWS
1884 cmdmod.split |= WSP_BELOW;
1885#endif
1886 continue;
1887
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001888 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1889 {
1890#ifdef HAVE_SANDBOX
1891 if (!did_sandbox)
1892 ++sandbox;
1893 did_sandbox = TRUE;
1894#endif
1895 continue;
1896 }
1897 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001899 if (save_msg_silent == -1)
1900 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1903 {
1904 /* ":silent!", but not "silent !cmd" */
1905 ea.cmd = skipwhite(ea.cmd + 1);
1906 ++emsg_silent;
1907 ++did_esilent;
1908 }
1909 continue;
1910
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001911 case 't': if (checkforcmd(&p, "tab", 3))
1912 {
1913#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001914 if (vim_isdigit(*ea.cmd))
1915 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1916 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001917 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001918 ea.cmd = p;
1919#endif
1920 continue;
1921 }
1922 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 break;
1924#ifdef FEAT_WINDOWS
1925 cmdmod.split |= WSP_TOP;
1926#endif
1927 continue;
1928
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001929 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1930 break;
1931 if (save_msg_silent == -1)
1932 save_msg_silent = msg_silent;
1933 msg_silent = 0;
1934 continue;
1935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1937 {
1938#ifdef FEAT_VERTSPLIT
1939 cmdmod.split |= WSP_VERT;
1940#endif
1941 continue;
1942 }
1943 if (!checkforcmd(&p, "verbose", 4))
1944 break;
1945 if (verbose_save < 0)
1946 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001947 if (vim_isdigit(*ea.cmd))
1948 p_verbose = atoi((char *)ea.cmd);
1949 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 p_verbose = 1;
1951 ea.cmd = p;
1952 continue;
1953 }
1954 break;
1955 }
1956
1957#ifdef FEAT_EVAL
1958 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1959 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1960#else
1961 ea.skip = (if_level > 0);
1962#endif
1963
1964#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001965# ifdef FEAT_PROFILE
1966 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001967 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001968 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001969 if (getline_equal(fgetline, cookie, get_func_line))
1970 func_line_exec(getline_cookie(fgetline, cookie));
1971 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001972 script_line_exec();
1973 }
1974#endif
1975
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 /* May go to debug mode. If this happens and the ">quit" debug command is
1977 * used, throw an interrupt exception and skip the next command. */
1978 dbg_check_breakpoint(&ea);
1979 if (!ea.skip && got_int)
1980 {
1981 ea.skip = TRUE;
1982 (void)do_intthrow(cstack);
1983 }
1984#endif
1985
1986/*
1987 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1988 *
1989 * where 'addr' is:
1990 *
1991 * % (entire file)
1992 * $ [+-NUM]
1993 * 'x [+-NUM] (where x denotes a currently defined mark)
1994 * . [+-NUM]
1995 * [+-NUM]..
1996 * NUM
1997 *
1998 * The ea.cmd pointer is updated to point to the first character following the
1999 * range spec. If an initial address is found, but no second, the upper bound
2000 * is equal to the lower.
2001 */
2002
2003 /* repeat for all ',' or ';' separated addresses */
2004 for (;;)
2005 {
2006 ea.line1 = ea.line2;
2007 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
2008 ea.cmd = skipwhite(ea.cmd);
2009 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
2010 if (ea.cmd == NULL) /* error detected */
2011 goto doend;
2012 if (lnum == MAXLNUM)
2013 {
2014 if (*ea.cmd == '%') /* '%' - all lines */
2015 {
2016 ++ea.cmd;
2017 ea.line1 = 1;
2018 ea.line2 = curbuf->b_ml.ml_line_count;
2019 ++ea.addr_count;
2020 }
2021 /* '*' - visual area */
2022 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2023 {
2024 pos_T *fp;
2025
2026 ++ea.cmd;
2027 if (!ea.skip)
2028 {
2029 fp = getmark('<', FALSE);
2030 if (check_mark(fp) == FAIL)
2031 goto doend;
2032 ea.line1 = fp->lnum;
2033 fp = getmark('>', FALSE);
2034 if (check_mark(fp) == FAIL)
2035 goto doend;
2036 ea.line2 = fp->lnum;
2037 ++ea.addr_count;
2038 }
2039 }
2040 }
2041 else
2042 ea.line2 = lnum;
2043 ea.addr_count++;
2044
2045 if (*ea.cmd == ';')
2046 {
2047 if (!ea.skip)
2048 curwin->w_cursor.lnum = ea.line2;
2049 }
2050 else if (*ea.cmd != ',')
2051 break;
2052 ++ea.cmd;
2053 }
2054
2055 /* One address given: set start and end lines */
2056 if (ea.addr_count == 1)
2057 {
2058 ea.line1 = ea.line2;
2059 /* ... but only implicit: really no address given */
2060 if (lnum == MAXLNUM)
2061 ea.addr_count = 0;
2062 }
2063
2064 /* Don't leave the cursor on an illegal line (caused by ';') */
2065 check_cursor_lnum();
2066
2067/*
2068 * 4. parse command
2069 */
2070
2071 /*
2072 * Skip ':' and any white space
2073 */
2074 ea.cmd = skipwhite(ea.cmd);
2075 while (*ea.cmd == ':')
2076 ea.cmd = skipwhite(ea.cmd + 1);
2077
2078 /*
2079 * If we got a line, but no command, then go to the line.
2080 * If we find a '|' or '\n' we set ea.nextcmd.
2081 */
2082 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2083 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2084 {
2085 /*
2086 * strange vi behaviour:
2087 * ":3" jumps to line 3
2088 * ":3|..." prints line 3
2089 * ":|" prints current line
2090 */
2091 if (ea.skip) /* skip this if inside :if */
2092 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002093 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
2095 ea.cmdidx = CMD_print;
2096 ea.argt = RANGE+COUNT+TRLBAR;
2097 if ((errormsg = invalid_range(&ea)) == NULL)
2098 {
2099 correct_range(&ea);
2100 ex_print(&ea);
2101 }
2102 }
2103 else if (ea.addr_count != 0)
2104 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002105 if (ea.line2 > curbuf->b_ml.ml_line_count)
2106 {
2107 /* With '-' in 'cpoptions' a line number past the file is an
2108 * error, otherwise put it at the end of the file. */
2109 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2110 ea.line2 = -1;
2111 else
2112 ea.line2 = curbuf->b_ml.ml_line_count;
2113 }
2114
2115 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002116 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 else
2118 {
2119 if (ea.line2 == 0)
2120 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 else
2122 curwin->w_cursor.lnum = ea.line2;
2123 beginline(BL_SOL | BL_FIX);
2124 }
2125 }
2126 goto doend;
2127 }
2128
2129 /* Find the command and let "p" point to after it. */
2130 p = find_command(&ea, NULL);
2131
2132#ifdef FEAT_USR_CMDS
2133 if (p == NULL)
2134 {
2135 if (!ea.skip)
2136 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2137 goto doend;
2138 }
2139 /* Check for wrong commands. */
2140 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2141 {
2142 errormsg = uc_fun_cmd();
2143 goto doend;
2144 }
2145#endif
2146 if (ea.cmdidx == CMD_SIZE)
2147 {
2148 if (!ea.skip)
2149 {
2150 STRCPY(IObuff, _("E492: Not an editor command"));
2151 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002152 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002154 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 goto doend;
2157 }
2158
2159 ni = (
2160#ifdef FEAT_USR_CMDS
2161 !USER_CMDIDX(ea.cmdidx) &&
2162#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002163 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002164#ifdef HAVE_EX_SCRIPT_NI
2165 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2166#endif
2167 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
2169#ifndef FEAT_EVAL
2170 /*
2171 * When the expression evaluation is disabled, recognize the ":if" and
2172 * ":endif" commands and ignore everything in between it.
2173 */
2174 if (ea.cmdidx == CMD_if)
2175 ++if_level;
2176 if (if_level)
2177 {
2178 if (ea.cmdidx == CMD_endif)
2179 --if_level;
2180 goto doend;
2181 }
2182
2183#endif
2184
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002185 /* forced commands */
2186 if (*p == '!' && ea.cmdidx != CMD_substitute
2187 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
2189 ++p;
2190 ea.forceit = TRUE;
2191 }
2192 else
2193 ea.forceit = FALSE;
2194
2195/*
2196 * 5. parse arguments
2197 */
2198#ifdef FEAT_USR_CMDS
2199 if (!USER_CMDIDX(ea.cmdidx))
2200#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002201 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
2203 if (!ea.skip)
2204 {
2205#ifdef HAVE_SANDBOX
2206 if (sandbox != 0 && !(ea.argt & SBOXOK))
2207 {
2208 /* Command not allowed in sandbox. */
2209 errormsg = (char_u *)_(e_sandbox);
2210 goto doend;
2211 }
2212#endif
2213 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2214 {
2215 /* Command not allowed in non-'modifiable' buffer */
2216 errormsg = (char_u *)_(e_modifiable);
2217 goto doend;
2218 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002219
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002220 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221# ifdef FEAT_USR_CMDS
2222 && !USER_CMDIDX(ea.cmdidx)
2223# endif
2224 )
2225 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002226 /* Command not allowed when editing the command line. */
2227#ifdef FEAT_CMDWIN
2228 if (cmdwin_type != 0)
2229 errormsg = (char_u *)_(e_cmdwin);
2230 else
2231#endif
2232 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 goto doend;
2234 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002235#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002236 /* Disallow editing another buffer when "curbuf_lock" is set.
2237 * Do allow ":edit" (check for argument later).
2238 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002239 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002240 && ea.cmdidx != CMD_edit
2241 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002242# ifdef FEAT_USR_CMDS
2243 && !USER_CMDIDX(ea.cmdidx)
2244# endif
2245 && curbuf_locked())
2246 goto doend;
2247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
2249 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2250 {
2251 /* no range allowed */
2252 errormsg = (char_u *)_(e_norange);
2253 goto doend;
2254 }
2255 }
2256
2257 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2258 {
2259 errormsg = (char_u *)_(e_nobang);
2260 goto doend;
2261 }
2262
2263 /*
2264 * Don't complain about the range if it is not used
2265 * (could happen if line_count is accidentally set to 0).
2266 */
2267 if (!ea.skip && !ni)
2268 {
2269 /*
2270 * If the range is backwards, ask for confirmation and, if given, swap
2271 * ea.line1 & ea.line2 so it's forwards again.
2272 * When global command is busy, don't ask, will fail below.
2273 */
2274 if (!global_busy && ea.line1 > ea.line2)
2275 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002276 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002278 if (sourcing || exmode_active)
2279 {
2280 errormsg = (char_u *)_("E493: Backwards range given");
2281 goto doend;
2282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (ask_yesno((char_u *)
2284 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002285 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287 lnum = ea.line1;
2288 ea.line1 = ea.line2;
2289 ea.line2 = lnum;
2290 }
2291 if ((errormsg = invalid_range(&ea)) != NULL)
2292 goto doend;
2293 }
2294
2295 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2296 ea.line2 = 1;
2297
2298 correct_range(&ea);
2299
2300#ifdef FEAT_FOLDING
2301 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2302 {
2303 /* Put the first line at the start of a closed fold, put the last line
2304 * at the end of a closed fold. */
2305 (void)hasFolding(ea.line1, &ea.line1, NULL);
2306 (void)hasFolding(ea.line2, NULL, &ea.line2);
2307 }
2308#endif
2309
2310#ifdef FEAT_QUICKFIX
2311 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002312 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 * option here, so things like % get expanded.
2314 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002315 p = replace_makeprg(&ea, p, cmdlinep);
2316 if (p == NULL)
2317 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318#endif
2319
2320 /*
2321 * Skip to start of argument.
2322 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2323 */
2324 if (ea.cmdidx == CMD_bang)
2325 ea.arg = p;
2326 else
2327 ea.arg = skipwhite(p);
2328
2329 /*
2330 * Check for "++opt=val" argument.
2331 * Must be first, allow ":w ++enc=utf8 !cmd"
2332 */
2333 if (ea.argt & ARGOPT)
2334 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2335 if (getargopt(&ea) == FAIL && !ni)
2336 {
2337 errormsg = (char_u *)_(e_invarg);
2338 goto doend;
2339 }
2340
2341 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2342 {
2343 if (*ea.arg == '>') /* append */
2344 {
2345 if (*++ea.arg != '>') /* typed wrong */
2346 {
2347 errormsg = (char_u *)_("E494: Use w or w>>");
2348 goto doend;
2349 }
2350 ea.arg = skipwhite(ea.arg + 1);
2351 ea.append = TRUE;
2352 }
2353 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2354 {
2355 ++ea.arg;
2356 ea.usefilter = TRUE;
2357 }
2358 }
2359
2360 if (ea.cmdidx == CMD_read)
2361 {
2362 if (ea.forceit)
2363 {
2364 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2365 ea.forceit = FALSE;
2366 }
2367 else if (*ea.arg == '!') /* :r !filter */
2368 {
2369 ++ea.arg;
2370 ea.usefilter = TRUE;
2371 }
2372 }
2373
2374 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2375 {
2376 ea.amount = 1;
2377 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2378 {
2379 ++ea.arg;
2380 ++ea.amount;
2381 }
2382 ea.arg = skipwhite(ea.arg);
2383 }
2384
2385 /*
2386 * Check for "+command" argument, before checking for next command.
2387 * Don't do this for ":read !cmd" and ":write !cmd".
2388 */
2389 if ((ea.argt & EDITCMD) && !ea.usefilter)
2390 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2391
2392 /*
2393 * Check for '|' to separate commands and '"' to start comments.
2394 * Don't do this for ":read !cmd" and ":write !cmd".
2395 */
2396 if ((ea.argt & TRLBAR) && !ea.usefilter)
2397 separate_nextcmd(&ea);
2398
2399 /*
2400 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002401 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2402 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002404 else if (ea.cmdidx == CMD_bang
2405 || ea.cmdidx == CMD_global
2406 || ea.cmdidx == CMD_vglobal
2407 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
2409 for (p = ea.arg; *p; ++p)
2410 {
2411 /* Remove one backslash before a newline, so that it's possible to
2412 * pass a newline to the shell and also a newline that is preceded
2413 * with a backslash. This makes it impossible to end a shell
2414 * command in a backslash, but that doesn't appear useful.
2415 * Halving the number of backslashes is incompatible with previous
2416 * versions. */
2417 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002418 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 else if (*p == '\n')
2420 {
2421 ea.nextcmd = p + 1;
2422 *p = NUL;
2423 break;
2424 }
2425 }
2426 }
2427
2428 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2429 {
2430 ea.line1 = 1;
2431 ea.line2 = curbuf->b_ml.ml_line_count;
2432 }
2433
2434 /* accept numbered register only when no count allowed (:put) */
2435 if ( (ea.argt & REGSTR)
2436 && *ea.arg != NUL
2437#ifdef FEAT_USR_CMDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 /* Do not allow register = for user commands */
2439 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440#endif
2441 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2442 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002443#ifndef FEAT_CLIPBOARD
2444 /* check these explicitly for a more specific error message */
2445 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002447 errormsg = (char_u *)_(e_invalidreg);
2448 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450#endif
Bram Moolenaar85de2062011-05-05 14:26:41 +02002451 if (
2452#ifdef FEAT_USR_CMDS
2453 valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2454 && USER_CMDIDX(ea.cmdidx)))
2455#else
2456 valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2457#endif
2458 )
2459 {
2460 ea.regname = *ea.arg++;
2461#ifdef FEAT_EVAL
2462 /* for '=' register: accept the rest of the line as an expression */
2463 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2464 {
2465 set_expr_line(vim_strsave(ea.arg));
2466 ea.arg += STRLEN(ea.arg);
2467 }
2468#endif
2469 ea.arg = skipwhite(ea.arg);
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
2472
2473 /*
2474 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2475 * count, it's a buffer name.
2476 */
2477 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2478 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2479 || vim_iswhite(*p)))
2480 {
2481 n = getdigits(&ea.arg);
2482 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002483 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
2485 errormsg = (char_u *)_(e_zerocount);
2486 goto doend;
2487 }
2488 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2489 {
2490 ea.line2 = n;
2491 if (ea.addr_count == 0)
2492 ea.addr_count = 1;
2493 }
2494 else
2495 {
2496 ea.line1 = ea.line2;
2497 ea.line2 += n - 1;
2498 ++ea.addr_count;
2499 /*
2500 * Be vi compatible: no error message for out of range.
2501 */
2502 if (ea.line2 > curbuf->b_ml.ml_line_count)
2503 ea.line2 = curbuf->b_ml.ml_line_count;
2504 }
2505 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002506
2507 /*
2508 * Check for flags: 'l', 'p' and '#'.
2509 */
2510 if (ea.argt & EXFLAGS)
2511 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002513 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002514 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
2516 errormsg = (char_u *)_(e_trailing);
2517 goto doend;
2518 }
2519
2520 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2521 {
2522 errormsg = (char_u *)_(e_argreq);
2523 goto doend;
2524 }
2525
2526#ifdef FEAT_EVAL
2527 /*
2528 * Skip the command when it's not going to be executed.
2529 * The commands like :if, :endif, etc. always need to be executed.
2530 * Also make an exception for commands that handle a trailing command
2531 * themselves.
2532 */
2533 if (ea.skip)
2534 {
2535 switch (ea.cmdidx)
2536 {
2537 /* commands that need evaluation */
2538 case CMD_while:
2539 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002540 case CMD_for:
2541 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 case CMD_if:
2543 case CMD_elseif:
2544 case CMD_else:
2545 case CMD_endif:
2546 case CMD_try:
2547 case CMD_catch:
2548 case CMD_finally:
2549 case CMD_endtry:
2550 case CMD_function:
2551 break;
2552
2553 /* Commands that handle '|' themselves. Check: A command should
2554 * either have the TRLBAR flag, appear in this list or appear in
2555 * the list at ":help :bar". */
2556 case CMD_aboveleft:
2557 case CMD_and:
2558 case CMD_belowright:
2559 case CMD_botright:
2560 case CMD_browse:
2561 case CMD_call:
2562 case CMD_confirm:
2563 case CMD_delfunction:
2564 case CMD_djump:
2565 case CMD_dlist:
2566 case CMD_dsearch:
2567 case CMD_dsplit:
2568 case CMD_echo:
2569 case CMD_echoerr:
2570 case CMD_echomsg:
2571 case CMD_echon:
2572 case CMD_execute:
2573 case CMD_help:
2574 case CMD_hide:
2575 case CMD_ijump:
2576 case CMD_ilist:
2577 case CMD_isearch:
2578 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002579 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 case CMD_keepjumps:
2581 case CMD_keepmarks:
2582 case CMD_leftabove:
2583 case CMD_let:
2584 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002585 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002587 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 case CMD_perl:
2589 case CMD_psearch:
2590 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002591 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002592 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 case CMD_return:
2594 case CMD_rightbelow:
2595 case CMD_ruby:
2596 case CMD_silent:
2597 case CMD_smagic:
2598 case CMD_snomagic:
2599 case CMD_substitute:
2600 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002601 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 case CMD_tcl:
2603 case CMD_throw:
2604 case CMD_tilde:
2605 case CMD_topleft:
2606 case CMD_unlet:
2607 case CMD_verbose:
2608 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002609 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 break;
2611
2612 default: goto doend;
2613 }
2614 }
2615#endif
2616
2617 if (ea.argt & XFILE)
2618 {
2619 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2620 goto doend;
2621 }
2622
2623#ifdef FEAT_LISTCMDS
2624 /*
2625 * Accept buffer name. Cannot be used at the same time with a buffer
2626 * number. Don't do this for a user command.
2627 */
2628 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2629# ifdef FEAT_USR_CMDS
2630 && !USER_CMDIDX(ea.cmdidx)
2631# endif
2632 )
2633 {
2634 /*
2635 * :bdelete, :bwipeout and :bunload take several arguments, separated
2636 * by spaces: find next space (skipping over escaped characters).
2637 * The others take one argument: ignore trailing spaces.
2638 */
2639 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2640 || ea.cmdidx == CMD_bunload)
2641 p = skiptowhite_esc(ea.arg);
2642 else
2643 {
2644 p = ea.arg + STRLEN(ea.arg);
2645 while (p > ea.arg && vim_iswhite(p[-1]))
2646 --p;
2647 }
2648 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2649 if (ea.line2 < 0) /* failed */
2650 goto doend;
2651 ea.addr_count = 1;
2652 ea.arg = skipwhite(p);
2653 }
2654#endif
2655
2656/*
2657 * 6. switch on command name
2658 *
2659 * The "ea" structure holds the arguments that can be used.
2660 */
2661 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002662 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 ea.cookie = cookie;
2664#ifdef FEAT_EVAL
2665 ea.cstack = cstack;
2666#endif
2667
2668#ifdef FEAT_USR_CMDS
2669 if (USER_CMDIDX(ea.cmdidx))
2670 {
2671 /*
2672 * Execute a user-defined command.
2673 */
2674 do_ucmd(&ea);
2675 }
2676 else
2677#endif
2678 {
2679 /*
2680 * Call the function to execute the command.
2681 */
2682 ea.errmsg = NULL;
2683 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2684 if (ea.errmsg != NULL)
2685 errormsg = (char_u *)_(ea.errmsg);
2686 }
2687
2688#ifdef FEAT_EVAL
2689 /*
2690 * If the command just executed called do_cmdline(), any throw or ":return"
2691 * or ":finish" encountered there must also check the cstack of the still
2692 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2693 * exception, or reanimate a returned function or finished script file and
2694 * return or finish it again.
2695 */
2696 if (need_rethrow)
2697 do_throw(cstack);
2698 else if (check_cstack)
2699 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002700 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002702 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 && current_func_returned())
2704 do_return(&ea, TRUE, FALSE, NULL);
2705 }
2706 need_rethrow = check_cstack = FALSE;
2707#endif
2708
2709doend:
2710 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2711 curwin->w_cursor.lnum = 1;
2712
2713 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2714 {
2715 if (sourcing)
2716 {
2717 if (errormsg != IObuff)
2718 {
2719 STRCPY(IObuff, errormsg);
2720 errormsg = IObuff;
2721 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002722 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
2724 emsg(errormsg);
2725 }
2726#ifdef FEAT_EVAL
2727 do_errthrow(cstack,
2728 (ea.cmdidx != CMD_SIZE
2729# ifdef FEAT_USR_CMDS
2730 && !USER_CMDIDX(ea.cmdidx)
2731# endif
2732 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2733#endif
2734
2735 if (verbose_save >= 0)
2736 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002737#ifdef FEAT_AUTOCMD
2738 if (cmdmod.save_ei != NULL)
2739 {
2740 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002741 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2742 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002743 free_string_option(cmdmod.save_ei);
2744 }
2745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747 cmdmod = save_cmdmod;
2748
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002749 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
2751 /* messages could be enabled for a serious error, need to check if the
2752 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002753 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002754 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 emsg_silent -= did_esilent;
2756 if (emsg_silent < 0)
2757 emsg_silent = 0;
2758 /* Restore msg_scroll, it's set by file I/O commands, even when no
2759 * message is actually displayed. */
2760 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002761
2762 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2763 * somewhere in the line. Put it back in the first column. */
2764 if (redirecting())
2765 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002768#ifdef HAVE_SANDBOX
2769 if (did_sandbox)
2770 --sandbox;
2771#endif
2772
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2774 ea.nextcmd = NULL;
2775
2776#ifdef FEAT_EVAL
2777 --ex_nesting_level;
2778#endif
2779
2780 return ea.nextcmd;
2781}
2782#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002783 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784#endif
2785
2786/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002787 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 * If there is a match advance "pp" to the argument and return TRUE.
2789 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002790 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002792 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 char *cmd; /* name of command */
2794 int len; /* required length */
2795{
2796 int i;
2797
2798 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002799 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 break;
2801 if (i >= len && !isalpha((*pp)[i]))
2802 {
2803 *pp = skipwhite(*pp + i);
2804 return TRUE;
2805 }
2806 return FALSE;
2807}
2808
2809/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002810 * Append "cmd" to the error message in IObuff.
2811 * Takes care of limiting the length and handling 0xa0, which would be
2812 * invisible otherwise.
2813 */
2814 static void
2815append_command(cmd)
2816 char_u *cmd;
2817{
2818 char_u *s = cmd;
2819 char_u *d;
2820
2821 STRCAT(IObuff, ": ");
2822 d = IObuff + STRLEN(IObuff);
2823 while (*s != NUL && d - IObuff < IOSIZE - 7)
2824 {
2825 if (
2826#ifdef FEAT_MBYTE
2827 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2828#endif
2829 *s == 0xa0)
2830 {
2831 s +=
2832#ifdef FEAT_MBYTE
2833 enc_utf8 ? 2 :
2834#endif
2835 1;
2836 STRCPY(d, "<a0>");
2837 d += 4;
2838 }
2839 else
2840 MB_COPY_CHAR(s, d);
2841 }
2842 *d = NUL;
2843}
2844
2845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002847 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002849 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 * Returns NULL for an ambiguous user command.
2851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 static char_u *
2853find_command(eap, full)
2854 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002855 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
2857 int len;
2858 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002859 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
2861 /*
2862 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002863 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 * - the 'k' command can directly be followed by any character.
2865 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2866 * but :sre[wind] is another command, as are :scrip[tnames],
2867 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002868 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 */
2870 p = eap->cmd;
2871 if (*p == 'k')
2872 {
2873 eap->cmdidx = CMD_k;
2874 ++p;
2875 }
2876 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002877 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2878 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 || p[1] == 'g'
2880 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2881 || p[1] == 'I'
2882 || (p[1] == 'r' && p[2] != 'e')))
2883 {
2884 eap->cmdidx = CMD_substitute;
2885 ++p;
2886 }
2887 else
2888 {
2889 while (ASCII_ISALPHA(*p))
2890 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002891 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002892 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002893 while (ASCII_ISALNUM(*p))
2894 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002895
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 /* check for non-alpha command */
2897 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2898 ++p;
2899 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002900 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2901 {
2902 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2903 * :delete with the 'l' flag. Same for 'p'. */
2904 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002905 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002906 break;
2907 if (i == len - 1)
2908 {
2909 --len;
2910 if (p[-1] == 'l')
2911 eap->flags |= EXFLAG_LIST;
2912 else
2913 eap->flags |= EXFLAG_PRINT;
2914 }
2915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916
2917 if (ASCII_ISLOWER(*eap->cmd))
2918 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2919 else
2920 eap->cmdidx = cmdidxs[26];
2921
2922 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2923 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2924 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2925 (size_t)len) == 0)
2926 {
2927#ifdef FEAT_EVAL
2928 if (full != NULL
2929 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2930 *full = TRUE;
2931#endif
2932 break;
2933 }
2934
2935#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002936 /* Look for a user defined command as a last resort. Let ":Print" be
2937 * overruled by a user defined command. */
2938 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2939 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002941 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 while (ASCII_ISALNUM(*p))
2943 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002944 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 }
2946#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002947 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 eap->cmdidx = CMD_SIZE;
2949 }
2950
2951 return p;
2952}
2953
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002954#ifdef FEAT_USR_CMDS
2955/*
2956 * Search for a user command that matches "eap->cmd".
2957 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2958 * Return a pointer to just after the command.
2959 * Return NULL if there is no matching command.
2960 */
2961 static char_u *
2962find_ucmd(eap, p, full, xp, compl)
2963 exarg_T *eap;
2964 char_u *p; /* end of the command (possibly including count) */
2965 int *full; /* set to TRUE for a full match */
2966 expand_T *xp; /* used for completion, NULL otherwise */
2967 int *compl; /* completion flags or NULL */
2968{
2969 int len = (int)(p - eap->cmd);
2970 int j, k, matchlen = 0;
2971 ucmd_T *uc;
2972 int found = FALSE;
2973 int possible = FALSE;
2974 char_u *cp, *np; /* Point into typed cmd and test name */
2975 garray_T *gap;
2976 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2977 only full match global is accepted. */
2978
2979 /*
2980 * Look for buffer-local user commands first, then global ones.
2981 */
2982 gap = &curbuf->b_ucmds;
2983 for (;;)
2984 {
2985 for (j = 0; j < gap->ga_len; ++j)
2986 {
2987 uc = USER_CMD_GA(gap, j);
2988 cp = eap->cmd;
2989 np = uc->uc_name;
2990 k = 0;
2991 while (k < len && *np != NUL && *cp++ == *np++)
2992 k++;
2993 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2994 {
2995 /* If finding a second match, the command is ambiguous. But
2996 * not if a buffer-local command wasn't a full match and a
2997 * global command is a full match. */
2998 if (k == len && found && *np != NUL)
2999 {
3000 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003001 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003002 amb_local = TRUE;
3003 }
3004
3005 if (!found || (k == len && *np == NUL))
3006 {
3007 /* If we matched up to a digit, then there could
3008 * be another command including the digit that we
3009 * should use instead.
3010 */
3011 if (k == len)
3012 found = TRUE;
3013 else
3014 possible = TRUE;
3015
3016 if (gap == &ucmds)
3017 eap->cmdidx = CMD_USER;
3018 else
3019 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003020 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003021 eap->useridx = j;
3022
3023# ifdef FEAT_CMDL_COMPL
3024 if (compl != NULL)
3025 *compl = uc->uc_compl;
3026# ifdef FEAT_EVAL
3027 if (xp != NULL)
3028 {
3029 xp->xp_arg = uc->uc_compl_arg;
3030 xp->xp_scriptID = uc->uc_scriptID;
3031 }
3032# endif
3033# endif
3034 /* Do not search for further abbreviations
3035 * if this is an exact match. */
3036 matchlen = k;
3037 if (k == len && *np == NUL)
3038 {
3039 if (full != NULL)
3040 *full = TRUE;
3041 amb_local = FALSE;
3042 break;
3043 }
3044 }
3045 }
3046 }
3047
3048 /* Stop if we found a full match or searched all. */
3049 if (j < gap->ga_len || gap == &ucmds)
3050 break;
3051 gap = &ucmds;
3052 }
3053
3054 /* Only found ambiguous matches. */
3055 if (amb_local)
3056 {
3057 if (xp != NULL)
3058 xp->xp_context = EXPAND_UNSUCCESSFUL;
3059 return NULL;
3060 }
3061
3062 /* The match we found may be followed immediately by a number. Move "p"
3063 * back to point to it. */
3064 if (found || possible)
3065 return p + (matchlen - len);
3066 return p;
3067}
3068#endif
3069
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003071static struct cmdmod
3072{
3073 char *name;
3074 int minlen;
3075 int has_count; /* :123verbose :3tab */
3076} cmdmods[] = {
3077 {"aboveleft", 3, FALSE},
3078 {"belowright", 3, FALSE},
3079 {"botright", 2, FALSE},
3080 {"browse", 3, FALSE},
3081 {"confirm", 4, FALSE},
3082 {"hide", 3, FALSE},
3083 {"keepalt", 5, FALSE},
3084 {"keepjumps", 5, FALSE},
3085 {"keepmarks", 3, FALSE},
3086 {"leftabove", 5, FALSE},
3087 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003088 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003089 {"rightbelow", 6, FALSE},
3090 {"sandbox", 3, FALSE},
3091 {"silent", 3, FALSE},
3092 {"tab", 3, TRUE},
3093 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003094 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003095 {"verbose", 4, TRUE},
3096 {"vertical", 4, FALSE},
3097};
3098
3099/*
3100 * Return length of a command modifier (including optional count).
3101 * Return zero when it's not a modifier.
3102 */
3103 int
3104modifier_len(cmd)
3105 char_u *cmd;
3106{
3107 int i, j;
3108 char_u *p = cmd;
3109
3110 if (VIM_ISDIGIT(*cmd))
3111 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003112 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003113 {
3114 for (j = 0; p[j] != NUL; ++j)
3115 if (p[j] != cmdmods[i].name[j])
3116 break;
3117 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3118 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003119 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003120 }
3121 return 0;
3122}
3123
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124/*
3125 * Return > 0 if an Ex command "name" exists.
3126 * Return 2 if there is an exact match.
3127 * Return 3 if there is an ambiguous match.
3128 */
3129 int
3130cmd_exists(name)
3131 char_u *name;
3132{
3133 exarg_T ea;
3134 int full = FALSE;
3135 int i;
3136 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003137 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
3139 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003140 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 {
3142 for (j = 0; name[j] != NUL; ++j)
3143 if (name[j] != cmdmods[i].name[j])
3144 break;
3145 if (name[j] == NUL && j >= cmdmods[i].minlen)
3146 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3147 }
3148
Bram Moolenaara9587612006-05-04 21:47:50 +00003149 /* Check built-in commands and user defined commands.
3150 * For ":2match" and ":3match" we need to skip the number. */
3151 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003153 p = find_command(&ea, &full);
3154 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003156 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3157 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003158 if (*skipwhite(p) != NUL)
3159 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3161}
3162#endif
3163
3164/*
3165 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3166 * we don't need/want deleted. Maybe this could be done better if we didn't
3167 * repeat all this stuff. The only problem is that they may not stay
3168 * perfectly compatible with each other, but then the command line syntax
3169 * probably won't change that much -- webb.
3170 */
3171 char_u *
3172set_one_cmd_context(xp, buff)
3173 expand_T *xp;
3174 char_u *buff; /* buffer for command string */
3175{
3176 char_u *p;
3177 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003178 int len = 0;
3179 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3181 int compl = EXPAND_NOTHING;
3182#endif
3183#ifdef FEAT_CMDL_COMPL
3184 int delim;
3185#endif
3186 int forceit = FALSE;
3187 int usefilter = FALSE; /* filter instead of file name */
3188
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003189 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 xp->xp_pattern = buff;
3191 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003192 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
3194/*
3195 * 2. skip comment lines and leading space, colons or bars
3196 */
3197 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3198 ;
3199 xp->xp_pattern = cmd;
3200
3201 if (*cmd == NUL)
3202 return NULL;
3203 if (*cmd == '"') /* ignore comment lines */
3204 {
3205 xp->xp_context = EXPAND_NOTHING;
3206 return NULL;
3207 }
3208
3209/*
3210 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3211 */
3212 cmd = skip_range(cmd, &xp->xp_context);
3213
3214/*
3215 * 4. parse command
3216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 xp->xp_pattern = cmd;
3218 if (*cmd == NUL)
3219 return NULL;
3220 if (*cmd == '"')
3221 {
3222 xp->xp_context = EXPAND_NOTHING;
3223 return NULL;
3224 }
3225
3226 if (*cmd == '|' || *cmd == '\n')
3227 return cmd + 1; /* There's another command */
3228
3229 /*
3230 * Isolate the command and search for it in the command table.
3231 * Exceptions:
3232 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003233 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3235 */
3236 if (*cmd == 'k' && cmd[1] != 'e')
3237 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003238 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 p = cmd + 1;
3240 }
3241 else
3242 {
3243 p = cmd;
3244 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3245 ++p;
3246 /* check for non-alpha command */
3247 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3248 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003249 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003251 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 {
3253 xp->xp_context = EXPAND_UNSUCCESSFUL;
3254 return NULL;
3255 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003256 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003257 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3258 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3259 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 break;
3261
3262#ifdef FEAT_USR_CMDS
3263 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3265 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266#endif
3267 }
3268
3269 /*
3270 * If the cursor is touching the command, and it ends in an alpha-numeric
3271 * character, complete the command name.
3272 */
3273 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3274 return NULL;
3275
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003276 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
3278 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3279 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003280 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 p = cmd + 1;
3282 }
3283#ifdef FEAT_USR_CMDS
3284 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3285 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003286 ea.cmd = cmd;
3287 p = find_ucmd(&ea, p, NULL, xp,
3288# if defined(FEAT_CMDL_COMPL)
3289 &compl
3290# else
3291 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003293 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003294 if (p == NULL)
3295 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
3297#endif
3298 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003299 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 {
3301 /* Not still touching the command and it was an illegal one */
3302 xp->xp_context = EXPAND_UNSUCCESSFUL;
3303 return NULL;
3304 }
3305
3306 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3307
3308 if (*p == '!') /* forced commands */
3309 {
3310 forceit = TRUE;
3311 ++p;
3312 }
3313
3314/*
3315 * 5. parse arguments
3316 */
3317#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003318 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003320 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
3322 arg = skipwhite(p);
3323
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003324 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 {
3326 if (*arg == '>') /* append */
3327 {
3328 if (*++arg == '>')
3329 ++arg;
3330 arg = skipwhite(arg);
3331 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003332 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
3334 ++arg;
3335 usefilter = TRUE;
3336 }
3337 }
3338
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003339 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 {
3341 usefilter = forceit; /* :r! filter if forced */
3342 if (*arg == '!') /* :r !filter */
3343 {
3344 ++arg;
3345 usefilter = TRUE;
3346 }
3347 }
3348
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003349 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 {
3351 while (*arg == *cmd) /* allow any number of '>' or '<' */
3352 ++arg;
3353 arg = skipwhite(arg);
3354 }
3355
3356 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003357 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 {
3359 /* Check if we're in the +command */
3360 p = arg + 1;
3361 arg = skip_cmd_arg(arg, FALSE);
3362
3363 /* Still touching the command after '+'? */
3364 if (*arg == NUL)
3365 return p;
3366
3367 /* Skip space(s) after +command to get to the real argument */
3368 arg = skipwhite(arg);
3369 }
3370
3371 /*
3372 * Check for '|' to separate commands and '"' to start comments.
3373 * Don't do this for ":read !cmd" and ":write !cmd".
3374 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003375 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 {
3377 p = arg;
3378 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003379 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 p += 2;
3381 while (*p)
3382 {
3383 if (*p == Ctrl_V)
3384 {
3385 if (p[1] != NUL)
3386 ++p;
3387 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003388 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 || *p == '|' || *p == '\n')
3390 {
3391 if (*(p - 1) != '\\')
3392 {
3393 if (*p == '|' || *p == '\n')
3394 return p + 1;
3395 return NULL; /* It's a comment */
3396 }
3397 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003398 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
3400 }
3401
3402 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003403 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 vim_strchr((char_u *)"|\"", *arg) == NULL)
3405 return NULL;
3406
3407 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003408 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003410 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003411 while (*p && p < buff + len)
3412 {
3413 if (*p == ' ' || *p == TAB)
3414 {
3415 /* argument starts after a space */
3416 xp->xp_pattern = ++p;
3417 }
3418 else
3419 {
3420 if (*p == '\\' && *(p + 1) != NUL)
3421 ++p; /* skip over escaped character */
3422 mb_ptr_adv(p);
3423 }
3424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003426 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003428 int c;
3429 int in_quote = FALSE;
3430 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
3432 /*
3433 * Allow spaces within back-quotes to count as part of the argument
3434 * being expanded.
3435 */
3436 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003437 p = xp->xp_pattern;
3438 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003440#ifdef FEAT_MBYTE
3441 if (has_mbyte)
3442 c = mb_ptr2char(p);
3443 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003445 c = *p;
3446 if (c == '\\' && p[1] != NUL)
3447 ++p;
3448 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
3450 if (!in_quote)
3451 {
3452 xp->xp_pattern = p;
3453 bow = p + 1;
3454 }
3455 in_quote = !in_quote;
3456 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003457 /* An argument can contain just about everything, except
3458 * characters that end the command and white space. */
3459 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003460#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003461 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003462#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003463 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003464 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003465 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003466 while (*p != NUL)
3467 {
3468#ifdef FEAT_MBYTE
3469 if (has_mbyte)
3470 c = mb_ptr2char(p);
3471 else
3472#endif
3473 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003474 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003475 break;
3476#ifdef FEAT_MBYTE
3477 if (has_mbyte)
3478 len = (*mb_ptr2len)(p);
3479 else
3480#endif
3481 len = 1;
3482 mb_ptr_adv(p);
3483 }
3484 if (in_quote)
3485 bow = p;
3486 else
3487 xp->xp_pattern = p;
3488 p -= len;
3489 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003490 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
3492
3493 /*
3494 * If we are still inside the quotes, and we passed a space, just
3495 * expand from there.
3496 */
3497 if (bow != NULL && in_quote)
3498 xp->xp_pattern = bow;
3499 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003500
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003501 /* For a shell command more chars need to be escaped. */
3502 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003503 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003504#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003505 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003506#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003507 /* When still after the command name expand executables. */
3508 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003509 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
3512 /* Check for environment variable */
3513 if (*xp->xp_pattern == '$'
3514#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3515 || *xp->xp_pattern == '%'
3516#endif
3517 )
3518 {
3519 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3520 if (!vim_isIDc(*p))
3521 break;
3522 if (*p == NUL)
3523 {
3524 xp->xp_context = EXPAND_ENV_VARS;
3525 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003526#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3527 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003528 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003529 compl = EXPAND_ENV_VARS;
3530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003533#if defined(FEAT_CMDL_COMPL)
3534 /* Check for user names */
3535 if (*xp->xp_pattern == '~')
3536 {
3537 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3538 ;
3539 /* Complete ~user only if it partially matches a user name.
3540 * A full match ~user<Tab> will be replaced by user's home
3541 * directory i.e. something like ~user<Tab> -> /home/user/ */
3542 if (*p == NUL && p > xp->xp_pattern + 1
3543 && match_user(xp->xp_pattern + 1) == 1)
3544 {
3545 xp->xp_context = EXPAND_USER;
3546 ++xp->xp_pattern;
3547 }
3548 }
3549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
3551
3552/*
3553 * 6. switch on command name
3554 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003555 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003557 case CMD_find:
3558 case CMD_sfind:
3559 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003560 if (xp->xp_context == EXPAND_FILES)
3561 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003562 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 case CMD_cd:
3564 case CMD_chdir:
3565 case CMD_lcd:
3566 case CMD_lchdir:
3567 if (xp->xp_context == EXPAND_FILES)
3568 xp->xp_context = EXPAND_DIRECTORIES;
3569 break;
3570 case CMD_help:
3571 xp->xp_context = EXPAND_HELP;
3572 xp->xp_pattern = arg;
3573 break;
3574
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003575 /* Command modifiers: return the argument.
3576 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003578 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 case CMD_belowright:
3580 case CMD_botright:
3581 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003582 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003584 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 case CMD_folddoclosed:
3586 case CMD_folddoopen:
3587 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003588 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 case CMD_keepjumps:
3590 case CMD_keepmarks:
3591 case CMD_leftabove:
3592 case CMD_lockmarks:
3593 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003594 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003596 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 case CMD_topleft:
3598 case CMD_verbose:
3599 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003600 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 return arg;
3602
Bram Moolenaar4f688582007-07-24 12:34:30 +00003603#ifdef FEAT_CMDL_COMPL
3604# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 case CMD_match:
3606 if (*arg == NUL || !ends_excmd(*arg))
3607 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003608 /* also complete "None" */
3609 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 arg = skipwhite(skiptowhite(arg));
3611 if (*arg != NUL)
3612 {
3613 xp->xp_context = EXPAND_NOTHING;
3614 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3615 }
3616 }
3617 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620/*
3621 * All completion for the +cmdline_compl feature goes here.
3622 */
3623
3624# ifdef FEAT_USR_CMDS
3625 case CMD_command:
3626 /* Check for attributes */
3627 while (*arg == '-')
3628 {
3629 arg++; /* Skip "-" */
3630 p = skiptowhite(arg);
3631 if (*p == NUL)
3632 {
3633 /* Cursor is still in the attribute */
3634 p = vim_strchr(arg, '=');
3635 if (p == NULL)
3636 {
3637 /* No "=", so complete attribute names */
3638 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3639 xp->xp_pattern = arg;
3640 return NULL;
3641 }
3642
3643 /* For the -complete and -nargs attributes, we complete
3644 * their arguments as well.
3645 */
3646 if (STRNICMP(arg, "complete", p - arg) == 0)
3647 {
3648 xp->xp_context = EXPAND_USER_COMPLETE;
3649 xp->xp_pattern = p + 1;
3650 return NULL;
3651 }
3652 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3653 {
3654 xp->xp_context = EXPAND_USER_NARGS;
3655 xp->xp_pattern = p + 1;
3656 return NULL;
3657 }
3658 return NULL;
3659 }
3660 arg = skipwhite(p);
3661 }
3662
3663 /* After the attributes comes the new command name */
3664 p = skiptowhite(arg);
3665 if (*p == NUL)
3666 {
3667 xp->xp_context = EXPAND_USER_COMMANDS;
3668 xp->xp_pattern = arg;
3669 break;
3670 }
3671
3672 /* And finally comes a normal command */
3673 return skipwhite(p);
3674
3675 case CMD_delcommand:
3676 xp->xp_context = EXPAND_USER_COMMANDS;
3677 xp->xp_pattern = arg;
3678 break;
3679# endif
3680
3681 case CMD_global:
3682 case CMD_vglobal:
3683 delim = *arg; /* get the delimiter */
3684 if (delim)
3685 ++arg; /* skip delimiter if there is one */
3686
3687 while (arg[0] != NUL && arg[0] != delim)
3688 {
3689 if (arg[0] == '\\' && arg[1] != NUL)
3690 ++arg;
3691 ++arg;
3692 }
3693 if (arg[0] != NUL)
3694 return arg + 1;
3695 break;
3696 case CMD_and:
3697 case CMD_substitute:
3698 delim = *arg;
3699 if (delim)
3700 {
3701 /* skip "from" part */
3702 ++arg;
3703 arg = skip_regexp(arg, delim, p_magic, NULL);
3704 }
3705 /* skip "to" part */
3706 while (arg[0] != NUL && arg[0] != delim)
3707 {
3708 if (arg[0] == '\\' && arg[1] != NUL)
3709 ++arg;
3710 ++arg;
3711 }
3712 if (arg[0] != NUL) /* skip delimiter */
3713 ++arg;
3714 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3715 ++arg;
3716 if (arg[0] != NUL)
3717 return arg;
3718 break;
3719 case CMD_isearch:
3720 case CMD_dsearch:
3721 case CMD_ilist:
3722 case CMD_dlist:
3723 case CMD_ijump:
3724 case CMD_psearch:
3725 case CMD_djump:
3726 case CMD_isplit:
3727 case CMD_dsplit:
3728 arg = skipwhite(skipdigits(arg)); /* skip count */
3729 if (*arg == '/') /* Match regexp, not just whole words */
3730 {
3731 for (++arg; *arg && *arg != '/'; arg++)
3732 if (*arg == '\\' && arg[1] != NUL)
3733 arg++;
3734 if (*arg)
3735 {
3736 arg = skipwhite(arg + 1);
3737
3738 /* Check for trailing illegal characters */
3739 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3740 xp->xp_context = EXPAND_NOTHING;
3741 else
3742 return arg;
3743 }
3744 }
3745 break;
3746#ifdef FEAT_AUTOCMD
3747 case CMD_autocmd:
3748 return set_context_in_autocmd(xp, arg, FALSE);
3749
3750 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003751 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 return set_context_in_autocmd(xp, arg, TRUE);
3753#endif
3754 case CMD_set:
3755 set_context_in_set_cmd(xp, arg, 0);
3756 break;
3757 case CMD_setglobal:
3758 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3759 break;
3760 case CMD_setlocal:
3761 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3762 break;
3763 case CMD_tag:
3764 case CMD_stag:
3765 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003766 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 case CMD_tselect:
3768 case CMD_stselect:
3769 case CMD_ptselect:
3770 case CMD_tjump:
3771 case CMD_stjump:
3772 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003773 if (*p_wop != NUL)
3774 xp->xp_context = EXPAND_TAGS_LISTFILES;
3775 else
3776 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 xp->xp_pattern = arg;
3778 break;
3779 case CMD_augroup:
3780 xp->xp_context = EXPAND_AUGROUP;
3781 xp->xp_pattern = arg;
3782 break;
3783#ifdef FEAT_SYN_HL
3784 case CMD_syntax:
3785 set_context_in_syntax_cmd(xp, arg);
3786 break;
3787#endif
3788#ifdef FEAT_EVAL
3789 case CMD_let:
3790 case CMD_if:
3791 case CMD_elseif:
3792 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003793 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 case CMD_echo:
3795 case CMD_echon:
3796 case CMD_execute:
3797 case CMD_echomsg:
3798 case CMD_echoerr:
3799 case CMD_call:
3800 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003801 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 break;
3803
3804 case CMD_unlet:
3805 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3806 arg = xp->xp_pattern + 1;
3807 xp->xp_context = EXPAND_USER_VARS;
3808 xp->xp_pattern = arg;
3809 break;
3810
3811 case CMD_function:
3812 case CMD_delfunction:
3813 xp->xp_context = EXPAND_USER_FUNC;
3814 xp->xp_pattern = arg;
3815 break;
3816
3817 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003818 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 break;
3820#endif
3821 case CMD_highlight:
3822 set_context_in_highlight_cmd(xp, arg);
3823 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003824#ifdef FEAT_CSCOPE
3825 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003826 case CMD_lcscope:
3827 case CMD_scscope:
3828 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003829 break;
3830#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003831#ifdef FEAT_SIGNS
3832 case CMD_sign:
3833 set_context_in_sign_cmd(xp, arg);
3834 break;
3835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836#ifdef FEAT_LISTCMDS
3837 case CMD_bdelete:
3838 case CMD_bwipeout:
3839 case CMD_bunload:
3840 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3841 arg = xp->xp_pattern + 1;
3842 /*FALLTHROUGH*/
3843 case CMD_buffer:
3844 case CMD_sbuffer:
3845 case CMD_checktime:
3846 xp->xp_context = EXPAND_BUFFERS;
3847 xp->xp_pattern = arg;
3848 break;
3849#endif
3850#ifdef FEAT_USR_CMDS
3851 case CMD_USER:
3852 case CMD_USER_BUF:
3853 if (compl != EXPAND_NOTHING)
3854 {
3855 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003856 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 {
3858# ifdef FEAT_MENU
3859 if (compl == EXPAND_MENUS)
3860 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3861# endif
3862 if (compl == EXPAND_COMMANDS)
3863 return arg;
3864 if (compl == EXPAND_MAPPINGS)
3865 return set_context_in_map_cmd(xp, (char_u *)"map",
3866 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003867 /* Find start of last argument. */
3868 p = arg;
3869 while (*p)
3870 {
3871 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02003872 /* argument starts after a space */
3873 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003874 else if (*p == '\\' && *(p + 1) != NUL)
3875 ++p; /* skip over escaped character */
3876 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 xp->xp_pattern = arg;
3879 }
3880 xp->xp_context = compl;
3881 }
3882 break;
3883#endif
3884 case CMD_map: case CMD_noremap:
3885 case CMD_nmap: case CMD_nnoremap:
3886 case CMD_vmap: case CMD_vnoremap:
3887 case CMD_omap: case CMD_onoremap:
3888 case CMD_imap: case CMD_inoremap:
3889 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003890 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003892 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 case CMD_unmap:
3894 case CMD_nunmap:
3895 case CMD_vunmap:
3896 case CMD_ounmap:
3897 case CMD_iunmap:
3898 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003899 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003901 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 case CMD_abbreviate: case CMD_noreabbrev:
3903 case CMD_cabbrev: case CMD_cnoreabbrev:
3904 case CMD_iabbrev: case CMD_inoreabbrev:
3905 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003906 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 case CMD_unabbreviate:
3908 case CMD_cunabbrev:
3909 case CMD_iunabbrev:
3910 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003911 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912#ifdef FEAT_MENU
3913 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3914 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3915 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3916 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3917 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3918 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3919 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3920 case CMD_tmenu: case CMD_tunmenu:
3921 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3922 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3923#endif
3924
3925 case CMD_colorscheme:
3926 xp->xp_context = EXPAND_COLORS;
3927 xp->xp_pattern = arg;
3928 break;
3929
3930 case CMD_compiler:
3931 xp->xp_context = EXPAND_COMPILER;
3932 xp->xp_pattern = arg;
3933 break;
3934
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003935 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003936 xp->xp_context = EXPAND_OWNSYNTAX;
3937 xp->xp_pattern = arg;
3938 break;
3939
3940 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003941 xp->xp_context = EXPAND_FILETYPE;
3942 xp->xp_pattern = arg;
3943 break;
3944
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3946 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3947 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02003948 p = skiptowhite(arg);
3949 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
3951 xp->xp_context = EXPAND_LANGUAGE;
3952 xp->xp_pattern = arg;
3953 }
3954 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02003955 {
3956 if ( STRNCMP(arg, "messages", p - arg) == 0
3957 || STRNCMP(arg, "ctype", p - arg) == 0
3958 || STRNCMP(arg, "time", p - arg) == 0)
3959 {
3960 xp->xp_context = EXPAND_LOCALES;
3961 xp->xp_pattern = skipwhite(p);
3962 }
3963 else
3964 xp->xp_context = EXPAND_NOTHING;
3965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 break;
3967#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003968#if defined(FEAT_PROFILE)
3969 case CMD_profile:
3970 set_context_in_profile_cmd(xp, arg);
3971 break;
3972#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003973 case CMD_behave:
3974 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003975 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003976 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02003978#if defined(FEAT_CMDHIST)
3979 case CMD_history:
3980 xp->xp_context = EXPAND_HISTORY;
3981 xp->xp_pattern = arg;
3982 break;
3983#endif
3984
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985#endif /* FEAT_CMDL_COMPL */
3986
3987 default:
3988 break;
3989 }
3990 return NULL;
3991}
3992
3993/*
3994 * skip a range specifier of the form: addr [,addr] [;addr] ..
3995 *
3996 * Backslashed delimiters after / or ? will be skipped, and commands will
3997 * not be expanded between /'s and ?'s or after "'".
3998 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003999 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 * Returns the "cmd" pointer advanced to beyond the range.
4001 */
4002 char_u *
4003skip_range(cmd, ctx)
4004 char_u *cmd;
4005 int *ctx; /* pointer to xp_context or NULL */
4006{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004007 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaardf177f62005-02-22 08:39:57 +00004009 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 {
4011 if (*cmd == '\'')
4012 {
4013 if (*++cmd == NUL && ctx != NULL)
4014 *ctx = EXPAND_NOTHING;
4015 }
4016 else if (*cmd == '/' || *cmd == '?')
4017 {
4018 delim = *cmd++;
4019 while (*cmd != NUL && *cmd != delim)
4020 if (*cmd++ == '\\' && *cmd != NUL)
4021 ++cmd;
4022 if (*cmd == NUL && ctx != NULL)
4023 *ctx = EXPAND_NOTHING;
4024 }
4025 if (*cmd != NUL)
4026 ++cmd;
4027 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004028
4029 /* Skip ":" and white space. */
4030 while (*cmd == ':')
4031 cmd = skipwhite(cmd + 1);
4032
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 return cmd;
4034}
4035
4036/*
4037 * get a single EX address
4038 *
4039 * Set ptr to the next character after the part that was interpreted.
4040 * Set ptr to NULL when an error is encountered.
4041 *
4042 * Return MAXLNUM when no Ex address was found.
4043 */
4044 static linenr_T
4045get_address(ptr, skip, to_other_file)
4046 char_u **ptr;
4047 int skip; /* only skip the address, don't use it */
4048 int to_other_file; /* flag: may jump to other file */
4049{
4050 int c;
4051 int i;
4052 long n;
4053 char_u *cmd;
4054 pos_T pos;
4055 pos_T *fp;
4056 linenr_T lnum;
4057
4058 cmd = skipwhite(*ptr);
4059 lnum = MAXLNUM;
4060 do
4061 {
4062 switch (*cmd)
4063 {
4064 case '.': /* '.' - Cursor position */
4065 ++cmd;
4066 lnum = curwin->w_cursor.lnum;
4067 break;
4068
4069 case '$': /* '$' - last line */
4070 ++cmd;
4071 lnum = curbuf->b_ml.ml_line_count;
4072 break;
4073
4074 case '\'': /* ''' - mark */
4075 if (*++cmd == NUL)
4076 {
4077 cmd = NULL;
4078 goto error;
4079 }
4080 if (skip)
4081 ++cmd;
4082 else
4083 {
4084 /* Only accept a mark in another file when it is
4085 * used by itself: ":'M". */
4086 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4087 ++cmd;
4088 if (fp == (pos_T *)-1)
4089 /* Jumped to another file. */
4090 lnum = curwin->w_cursor.lnum;
4091 else
4092 {
4093 if (check_mark(fp) == FAIL)
4094 {
4095 cmd = NULL;
4096 goto error;
4097 }
4098 lnum = fp->lnum;
4099 }
4100 }
4101 break;
4102
4103 case '/':
4104 case '?': /* '/' or '?' - search */
4105 c = *cmd++;
4106 if (skip) /* skip "/pat/" */
4107 {
4108 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4109 if (*cmd == c)
4110 ++cmd;
4111 }
4112 else
4113 {
4114 pos = curwin->w_cursor; /* save curwin->w_cursor */
4115 /*
4116 * When '/' or '?' follows another address, start
4117 * from there.
4118 */
4119 if (lnum != MAXLNUM)
4120 curwin->w_cursor.lnum = lnum;
4121 /*
4122 * Start a forward search at the end of the line.
4123 * Start a backward search at the start of the line.
4124 * This makes sure we never match in the current
4125 * line, and can match anywhere in the
4126 * next/previous line.
4127 */
4128 if (c == '/')
4129 curwin->w_cursor.col = MAXCOL;
4130 else
4131 curwin->w_cursor.col = 0;
4132 searchcmdlen = 0;
4133 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004134 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 {
4136 curwin->w_cursor = pos;
4137 cmd = NULL;
4138 goto error;
4139 }
4140 lnum = curwin->w_cursor.lnum;
4141 curwin->w_cursor = pos;
4142 /* adjust command string pointer */
4143 cmd += searchcmdlen;
4144 }
4145 break;
4146
4147 case '\\': /* "\?", "\/" or "\&", repeat search */
4148 ++cmd;
4149 if (*cmd == '&')
4150 i = RE_SUBST;
4151 else if (*cmd == '?' || *cmd == '/')
4152 i = RE_SEARCH;
4153 else
4154 {
4155 EMSG(_(e_backslash));
4156 cmd = NULL;
4157 goto error;
4158 }
4159
4160 if (!skip)
4161 {
4162 /*
4163 * When search follows another address, start from
4164 * there.
4165 */
4166 if (lnum != MAXLNUM)
4167 pos.lnum = lnum;
4168 else
4169 pos.lnum = curwin->w_cursor.lnum;
4170
4171 /*
4172 * Start the search just like for the above
4173 * do_search().
4174 */
4175 if (*cmd != '?')
4176 pos.col = MAXCOL;
4177 else
4178 pos.col = 0;
4179 if (searchit(curwin, curbuf, &pos,
4180 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004181 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004182 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 lnum = pos.lnum;
4184 else
4185 {
4186 cmd = NULL;
4187 goto error;
4188 }
4189 }
4190 ++cmd;
4191 break;
4192
4193 default:
4194 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4195 lnum = getdigits(&cmd);
4196 }
4197
4198 for (;;)
4199 {
4200 cmd = skipwhite(cmd);
4201 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4202 break;
4203
4204 if (lnum == MAXLNUM)
4205 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4206 if (VIM_ISDIGIT(*cmd))
4207 i = '+'; /* "number" is same as "+number" */
4208 else
4209 i = *cmd++;
4210 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4211 n = 1;
4212 else
4213 n = getdigits(&cmd);
4214 if (i == '-')
4215 lnum -= n;
4216 else
4217 lnum += n;
4218 }
4219 } while (*cmd == '/' || *cmd == '?');
4220
4221error:
4222 *ptr = cmd;
4223 return lnum;
4224}
4225
4226/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004227 * Get flags from an Ex command argument.
4228 */
4229 static void
4230get_flags(eap)
4231 exarg_T *eap;
4232{
4233 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4234 {
4235 if (*eap->arg == 'l')
4236 eap->flags |= EXFLAG_LIST;
4237 else if (*eap->arg == 'p')
4238 eap->flags |= EXFLAG_PRINT;
4239 else
4240 eap->flags |= EXFLAG_NR;
4241 eap->arg = skipwhite(eap->arg + 1);
4242 }
4243}
4244
4245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 * Function called for command which is Not Implemented. NI!
4247 */
4248 void
4249ex_ni(eap)
4250 exarg_T *eap;
4251{
4252 if (!eap->skip)
4253 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4254}
4255
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004256#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257/*
4258 * Function called for script command which is Not Implemented. NI!
4259 * Skips over ":perl <<EOF" constructs.
4260 */
4261 static void
4262ex_script_ni(eap)
4263 exarg_T *eap;
4264{
4265 if (!eap->skip)
4266 ex_ni(eap);
4267 else
4268 vim_free(script_get(eap, eap->arg));
4269}
4270#endif
4271
4272/*
4273 * Check range in Ex command for validity.
4274 * Return NULL when valid, error message when invalid.
4275 */
4276 static char_u *
4277invalid_range(eap)
4278 exarg_T *eap;
4279{
4280 if ( eap->line1 < 0
4281 || eap->line2 < 0
4282 || eap->line1 > eap->line2
4283 || ((eap->argt & RANGE)
4284 && !(eap->argt & NOTADR)
4285 && eap->line2 > curbuf->b_ml.ml_line_count
4286#ifdef FEAT_DIFF
4287 + (eap->cmdidx == CMD_diffget)
4288#endif
4289 ))
4290 return (char_u *)_(e_invrange);
4291 return NULL;
4292}
4293
4294/*
4295 * Correct the range for zero line number, if required.
4296 */
4297 static void
4298correct_range(eap)
4299 exarg_T *eap;
4300{
4301 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4302 {
4303 if (eap->line1 == 0)
4304 eap->line1 = 1;
4305 if (eap->line2 == 0)
4306 eap->line2 = 1;
4307 }
4308}
4309
Bram Moolenaar748bf032005-02-02 23:04:36 +00004310#ifdef FEAT_QUICKFIX
4311static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4312
4313/*
4314 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4315 * pattern. Otherwise return eap->arg.
4316 */
4317 static char_u *
4318skip_grep_pat(eap)
4319 exarg_T *eap;
4320{
4321 char_u *p = eap->arg;
4322
Bram Moolenaara37420f2006-02-04 22:37:47 +00004323 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4324 || eap->cmdidx == CMD_vimgrepadd
4325 || eap->cmdidx == CMD_lvimgrepadd
4326 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004327 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004328 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004329 if (p == NULL)
4330 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004331 }
4332 return p;
4333}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004334
4335/*
4336 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4337 * in the command line, so that things like % get expanded.
4338 */
4339 static char_u *
4340replace_makeprg(eap, p, cmdlinep)
4341 exarg_T *eap;
4342 char_u *p;
4343 char_u **cmdlinep;
4344{
4345 char_u *new_cmdline;
4346 char_u *program;
4347 char_u *pos;
4348 char_u *ptr;
4349 int len;
4350 int i;
4351
4352 /*
4353 * Don't do it when ":vimgrep" is used for ":grep".
4354 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004355 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4356 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4357 || eap->cmdidx == CMD_grepadd
4358 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004359 && !grep_internal(eap->cmdidx))
4360 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004361 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4362 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004363 {
4364 if (*curbuf->b_p_gp == NUL)
4365 program = p_gp;
4366 else
4367 program = curbuf->b_p_gp;
4368 }
4369 else
4370 {
4371 if (*curbuf->b_p_mp == NUL)
4372 program = p_mp;
4373 else
4374 program = curbuf->b_p_mp;
4375 }
4376
4377 p = skipwhite(p);
4378
4379 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4380 {
4381 /* replace $* by given arguments */
4382 i = 1;
4383 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4384 ++i;
4385 len = (int)STRLEN(p);
4386 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4387 if (new_cmdline == NULL)
4388 return NULL; /* out of memory */
4389 ptr = new_cmdline;
4390 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4391 {
4392 i = (int)(pos - program);
4393 STRNCPY(ptr, program, i);
4394 STRCPY(ptr += i, p);
4395 ptr += len;
4396 program = pos + 2;
4397 }
4398 STRCPY(ptr, program);
4399 }
4400 else
4401 {
4402 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4403 if (new_cmdline == NULL)
4404 return NULL; /* out of memory */
4405 STRCPY(new_cmdline, program);
4406 STRCAT(new_cmdline, " ");
4407 STRCAT(new_cmdline, p);
4408 }
4409 msg_make(p);
4410
4411 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4412 vim_free(*cmdlinep);
4413 *cmdlinep = new_cmdline;
4414 p = new_cmdline;
4415 }
4416 return p;
4417}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004418#endif
4419
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420/*
4421 * Expand file name in Ex command argument.
4422 * Return FAIL for failure, OK otherwise.
4423 */
4424 int
4425expand_filename(eap, cmdlinep, errormsgp)
4426 exarg_T *eap;
4427 char_u **cmdlinep;
4428 char_u **errormsgp;
4429{
4430 int has_wildcards; /* need to expand wildcards */
4431 char_u *repl;
4432 int srclen;
4433 char_u *p;
4434 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004435 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
Bram Moolenaar748bf032005-02-02 23:04:36 +00004437#ifdef FEAT_QUICKFIX
4438 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4439 p = skip_grep_pat(eap);
4440#else
4441 p = eap->arg;
4442#endif
4443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 /*
4445 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4446 * the file name contains a wildcard it should not cause expanding.
4447 * (it will be expanded anyway if there is a wildcard before replacing).
4448 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004449 has_wildcards = mch_has_wildcard(p);
4450 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004452#ifdef FEAT_EVAL
4453 /* Skip over `=expr`, wildcards in it are not expanded. */
4454 if (p[0] == '`' && p[1] == '=')
4455 {
4456 p += 2;
4457 (void)skip_expr(&p);
4458 if (*p == '`')
4459 ++p;
4460 continue;
4461 }
4462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 /*
4464 * Quick check if this cannot be the start of a special string.
4465 * Also removes backslash before '%', '#' and '<'.
4466 */
4467 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4468 {
4469 ++p;
4470 continue;
4471 }
4472
4473 /*
4474 * Try to find a match at this position.
4475 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004476 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4477 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 if (*errormsgp != NULL) /* error detected */
4479 return FAIL;
4480 if (repl == NULL) /* no match found */
4481 {
4482 p += srclen;
4483 continue;
4484 }
4485
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004486 /* Wildcards won't be expanded below, the replacement is taken
4487 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4488 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4489 {
4490 char_u *l = repl;
4491
4492 repl = expand_env_save(repl);
4493 vim_free(l);
4494 }
4495
Bram Moolenaar63b92542007-03-27 14:57:09 +00004496 /* Need to escape white space et al. with a backslash.
4497 * Don't do this for:
4498 * - replacement that already has been escaped: "##"
4499 * - shell commands (may have to use quotes instead).
4500 * - non-unix systems when there is a single argument (spaces don't
4501 * separate arguments then).
4502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004504 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 && eap->cmdidx != CMD_bang
4506 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004507 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004509 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004511 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512#ifndef UNIX
4513 && !(eap->argt & NOSPC)
4514#endif
4515 )
4516 {
4517 char_u *l;
4518#ifdef BACKSLASH_IN_FILENAME
4519 /* Don't escape a backslash here, because rem_backslash() doesn't
4520 * remove it later. */
4521 static char_u *nobslash = (char_u *)" \t\"|";
4522# define ESCAPE_CHARS nobslash
4523#else
4524# define ESCAPE_CHARS escape_chars
4525#endif
4526
4527 for (l = repl; *l; ++l)
4528 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4529 {
4530 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4531 if (l != NULL)
4532 {
4533 vim_free(repl);
4534 repl = l;
4535 }
4536 break;
4537 }
4538 }
4539
4540 /* For a shell command a '!' must be escaped. */
4541 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004542 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 {
4544 char_u *l;
4545
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004546 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (l != NULL)
4548 {
4549 vim_free(repl);
4550 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004551 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 if (strstr((char *)p_sh, "sh") != NULL)
4553 {
4554 l = vim_strsave_escaped(repl, (char_u *)"!");
4555 if (l != NULL)
4556 {
4557 vim_free(repl);
4558 repl = l;
4559 }
4560 }
4561 }
4562 }
4563
4564 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4565 vim_free(repl);
4566 if (p == NULL)
4567 return FAIL;
4568 }
4569
4570 /*
4571 * One file argument: Expand wildcards.
4572 * Don't do this with ":r !command" or ":w !command".
4573 */
4574 if ((eap->argt & NOSPC) && !eap->usefilter)
4575 {
4576 /*
4577 * May do this twice:
4578 * 1. Replace environment variables.
4579 * 2. Replace any other wildcards, remove backslashes.
4580 */
4581 for (n = 1; n <= 2; ++n)
4582 {
4583 if (n == 2)
4584 {
4585#ifdef UNIX
4586 /*
4587 * Only for Unix we check for more than one file name.
4588 * For other systems spaces are considered to be part
4589 * of the file name.
4590 * Only check here if there is no wildcard, otherwise
4591 * ExpandOne() will check for errors. This allows
4592 * ":e `ls ve*.c`" on Unix.
4593 */
4594 if (!has_wildcards)
4595 for (p = eap->arg; *p; ++p)
4596 {
4597 /* skip escaped characters */
4598 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4599 ++p;
4600 else if (vim_iswhite(*p))
4601 {
4602 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4603 return FAIL;
4604 }
4605 }
4606#endif
4607
4608 /*
4609 * Halve the number of backslashes (this is Vi compatible).
4610 * For Unix and OS/2, when wildcards are expanded, this is
4611 * done by ExpandOne() below.
4612 */
4613#if defined(UNIX) || defined(OS2)
4614 if (!has_wildcards)
4615#endif
4616 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618
4619 if (has_wildcards)
4620 {
4621 if (n == 1)
4622 {
4623 /*
4624 * First loop: May expand environment variables. This
4625 * can be done much faster with expand_env() than with
4626 * something else (e.g., calling a shell).
4627 * After expanding environment variables, check again
4628 * if there are still wildcards present.
4629 */
4630 if (vim_strchr(eap->arg, '$') != NULL
4631 || vim_strchr(eap->arg, '~') != NULL)
4632 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004633 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004634 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 has_wildcards = mch_has_wildcard(NameBuff);
4636 p = NameBuff;
4637 }
4638 else
4639 p = NULL;
4640 }
4641 else /* n == 2 */
4642 {
4643 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004644 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
4646 ExpandInit(&xpc);
4647 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004648 if (p_wic)
4649 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004651 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 if (p == NULL)
4653 return FAIL;
4654 }
4655 if (p != NULL)
4656 {
4657 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4658 p, cmdlinep);
4659 if (n == 2) /* p came from ExpandOne() */
4660 vim_free(p);
4661 }
4662 }
4663 }
4664 }
4665 return OK;
4666}
4667
4668/*
4669 * Replace part of the command line, keeping eap->cmd, eap->arg and
4670 * eap->nextcmd correct.
4671 * "src" points to the part that is to be replaced, of length "srclen".
4672 * "repl" is the replacement string.
4673 * Returns a pointer to the character after the replaced string.
4674 * Returns NULL for failure.
4675 */
4676 static char_u *
4677repl_cmdline(eap, src, srclen, repl, cmdlinep)
4678 exarg_T *eap;
4679 char_u *src;
4680 int srclen;
4681 char_u *repl;
4682 char_u **cmdlinep;
4683{
4684 int len;
4685 int i;
4686 char_u *new_cmdline;
4687
4688 /*
4689 * The new command line is build in new_cmdline[].
4690 * First allocate it.
4691 * Careful: a "+cmd" argument may have been NUL terminated.
4692 */
4693 len = (int)STRLEN(repl);
4694 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004695 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4697 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4698 return NULL; /* out of memory! */
4699
4700 /*
4701 * Copy the stuff before the expanded part.
4702 * Copy the expanded stuff.
4703 * Copy what came after the expanded part.
4704 * Copy the next commands, if there are any.
4705 */
4706 i = (int)(src - *cmdlinep); /* length of part before match */
4707 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004708
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 mch_memmove(new_cmdline + i, repl, (size_t)len);
4710 i += len; /* remember the end of the string */
4711 STRCPY(new_cmdline + i, src + srclen);
4712 src = new_cmdline + i; /* remember where to continue */
4713
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004714 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
4716 i = (int)STRLEN(new_cmdline) + 1;
4717 STRCPY(new_cmdline + i, eap->nextcmd);
4718 eap->nextcmd = new_cmdline + i;
4719 }
4720 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4721 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4722 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4723 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4724 vim_free(*cmdlinep);
4725 *cmdlinep = new_cmdline;
4726
4727 return src;
4728}
4729
4730/*
4731 * Check for '|' to separate commands and '"' to start comments.
4732 */
4733 void
4734separate_nextcmd(eap)
4735 exarg_T *eap;
4736{
4737 char_u *p;
4738
Bram Moolenaar86b68352004-12-27 21:59:20 +00004739#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004740 p = skip_grep_pat(eap);
4741#else
4742 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004743#endif
4744
4745 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 {
4747 if (*p == Ctrl_V)
4748 {
4749 if (eap->argt & (USECTRLV | XFILE))
4750 ++p; /* skip CTRL-V and next char */
4751 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004752 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004753 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 if (*p == NUL) /* stop at NUL after CTRL-V */
4755 break;
4756 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004757
4758#ifdef FEAT_EVAL
4759 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004760 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004761 {
4762 p += 2;
4763 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004764 }
4765#endif
4766
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 /* Check for '"': start of comment or '|': next command */
4768 /* :@" and :*" do not start a comment!
4769 * :redir @" doesn't either. */
4770 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4771 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4772 || p != eap->arg)
4773 && (eap->cmdidx != CMD_redir
4774 || p != eap->arg + 1 || p[-1] != '@'))
4775 || *p == '|' || *p == '\n')
4776 {
4777 /*
4778 * We remove the '\' before the '|', unless USECTRLV is used
4779 * AND 'b' is present in 'cpoptions'.
4780 */
4781 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4782 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4783 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004784 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 --p;
4786 }
4787 else
4788 {
4789 eap->nextcmd = check_nextcmd(p);
4790 *p = NUL;
4791 break;
4792 }
4793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004795
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4797 del_trailing_spaces(eap->arg);
4798}
4799
4800/*
4801 * get + command from ex argument
4802 */
4803 static char_u *
4804getargcmd(argp)
4805 char_u **argp;
4806{
4807 char_u *arg = *argp;
4808 char_u *command = NULL;
4809
4810 if (*arg == '+') /* +[command] */
4811 {
4812 ++arg;
4813 if (vim_isspace(*arg))
4814 command = dollar_command;
4815 else
4816 {
4817 command = arg;
4818 arg = skip_cmd_arg(command, TRUE);
4819 if (*arg != NUL)
4820 *arg++ = NUL; /* terminate command with NUL */
4821 }
4822
4823 arg = skipwhite(arg); /* skip over spaces */
4824 *argp = arg;
4825 }
4826 return command;
4827}
4828
4829/*
4830 * Find end of "+command" argument. Skip over "\ " and "\\".
4831 */
4832 static char_u *
4833skip_cmd_arg(p, rembs)
4834 char_u *p;
4835 int rembs; /* TRUE to halve the number of backslashes */
4836{
4837 while (*p && !vim_isspace(*p))
4838 {
4839 if (*p == '\\' && p[1] != NUL)
4840 {
4841 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004842 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 else
4844 ++p;
4845 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004846 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
4848 return p;
4849}
4850
4851/*
4852 * Get "++opt=arg" argument.
4853 * Return FAIL or OK.
4854 */
4855 static int
4856getargopt(eap)
4857 exarg_T *eap;
4858{
4859 char_u *arg = eap->arg + 2;
4860 int *pp = NULL;
4861#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004862 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 char_u *p;
4864#endif
4865
4866 /* ":edit ++[no]bin[ary] file" */
4867 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4868 {
4869 if (*arg == 'n')
4870 {
4871 arg += 2;
4872 eap->force_bin = FORCE_NOBIN;
4873 }
4874 else
4875 eap->force_bin = FORCE_BIN;
4876 if (!checkforcmd(&arg, "binary", 3))
4877 return FAIL;
4878 eap->arg = skipwhite(arg);
4879 return OK;
4880 }
4881
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004882 /* ":read ++edit file" */
4883 if (STRNCMP(arg, "edit", 4) == 0)
4884 {
4885 eap->read_edit = TRUE;
4886 eap->arg = skipwhite(arg + 4);
4887 return OK;
4888 }
4889
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 if (STRNCMP(arg, "ff", 2) == 0)
4891 {
4892 arg += 2;
4893 pp = &eap->force_ff;
4894 }
4895 else if (STRNCMP(arg, "fileformat", 10) == 0)
4896 {
4897 arg += 10;
4898 pp = &eap->force_ff;
4899 }
4900#ifdef FEAT_MBYTE
4901 else if (STRNCMP(arg, "enc", 3) == 0)
4902 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004903 if (STRNCMP(arg, "encoding", 8) == 0)
4904 arg += 8;
4905 else
4906 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 pp = &eap->force_enc;
4908 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004909 else if (STRNCMP(arg, "bad", 3) == 0)
4910 {
4911 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004912 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914#endif
4915
4916 if (pp == NULL || *arg != '=')
4917 return FAIL;
4918
4919 ++arg;
4920 *pp = (int)(arg - eap->cmd);
4921 arg = skip_cmd_arg(arg, FALSE);
4922 eap->arg = skipwhite(arg);
4923 *arg = NUL;
4924
4925#ifdef FEAT_MBYTE
4926 if (pp == &eap->force_ff)
4927 {
4928#endif
4929 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4930 return FAIL;
4931#ifdef FEAT_MBYTE
4932 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004933 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
4935 /* Make 'fileencoding' lower case. */
4936 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4937 *p = TOLOWER_ASC(*p);
4938 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004939 else
4940 {
4941 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4942 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004943 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004944 if (STRICMP(p, "keep") == 0)
4945 eap->bad_char = BAD_KEEP;
4946 else if (STRICMP(p, "drop") == 0)
4947 eap->bad_char = BAD_DROP;
4948 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4949 eap->bad_char = *p;
4950 else
4951 return FAIL;
4952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953#endif
4954
4955 return OK;
4956}
4957
4958/*
4959 * ":abbreviate" and friends.
4960 */
4961 static void
4962ex_abbreviate(eap)
4963 exarg_T *eap;
4964{
4965 do_exmap(eap, TRUE); /* almost the same as mapping */
4966}
4967
4968/*
4969 * ":map" and friends.
4970 */
4971 static void
4972ex_map(eap)
4973 exarg_T *eap;
4974{
4975 /*
4976 * If we are sourcing .exrc or .vimrc in current directory we
4977 * print the mappings for security reasons.
4978 */
4979 if (secure)
4980 {
4981 secure = 2;
4982 msg_outtrans(eap->cmd);
4983 msg_putchar('\n');
4984 }
4985 do_exmap(eap, FALSE);
4986}
4987
4988/*
4989 * ":unmap" and friends.
4990 */
4991 static void
4992ex_unmap(eap)
4993 exarg_T *eap;
4994{
4995 do_exmap(eap, FALSE);
4996}
4997
4998/*
4999 * ":mapclear" and friends.
5000 */
5001 static void
5002ex_mapclear(eap)
5003 exarg_T *eap;
5004{
5005 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5006}
5007
5008/*
5009 * ":abclear" and friends.
5010 */
5011 static void
5012ex_abclear(eap)
5013 exarg_T *eap;
5014{
5015 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5016}
5017
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005018#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 static void
5020ex_autocmd(eap)
5021 exarg_T *eap;
5022{
5023 /*
5024 * Disallow auto commands from .exrc and .vimrc in current
5025 * directory for security reasons.
5026 */
5027 if (secure)
5028 {
5029 secure = 2;
5030 eap->errmsg = e_curdir;
5031 }
5032 else if (eap->cmdidx == CMD_autocmd)
5033 do_autocmd(eap->arg, eap->forceit);
5034 else
5035 do_augroup(eap->arg, eap->forceit);
5036}
5037
5038/*
5039 * ":doautocmd": Apply the automatic commands to the current buffer.
5040 */
5041 static void
5042ex_doautocmd(eap)
5043 exarg_T *eap;
5044{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005045 char_u *arg = eap->arg;
5046 int call_do_modelines = check_nomodeline(&arg);
5047
5048 (void)do_doautocmd(arg, TRUE);
5049 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5050 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051}
5052#endif
5053
5054#ifdef FEAT_LISTCMDS
5055/*
5056 * :[N]bunload[!] [N] [bufname] unload buffer
5057 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5058 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5059 */
5060 static void
5061ex_bunload(eap)
5062 exarg_T *eap;
5063{
5064 eap->errmsg = do_bufdel(
5065 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5066 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5067 : DOBUF_UNLOAD, eap->arg,
5068 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5069}
5070
5071/*
5072 * :[N]buffer [N] to buffer N
5073 * :[N]sbuffer [N] to buffer N
5074 */
5075 static void
5076ex_buffer(eap)
5077 exarg_T *eap;
5078{
5079 if (*eap->arg)
5080 eap->errmsg = e_trailing;
5081 else
5082 {
5083 if (eap->addr_count == 0) /* default is current buffer */
5084 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5085 else
5086 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
5087 }
5088}
5089
5090/*
5091 * :[N]bmodified [N] to next mod. buffer
5092 * :[N]sbmodified [N] to next mod. buffer
5093 */
5094 static void
5095ex_bmodified(eap)
5096 exarg_T *eap;
5097{
5098 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
5099}
5100
5101/*
5102 * :[N]bnext [N] to next buffer
5103 * :[N]sbnext [N] split and to next buffer
5104 */
5105 static void
5106ex_bnext(eap)
5107 exarg_T *eap;
5108{
5109 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
5110}
5111
5112/*
5113 * :[N]bNext [N] to previous buffer
5114 * :[N]bprevious [N] to previous buffer
5115 * :[N]sbNext [N] split and to previous buffer
5116 * :[N]sbprevious [N] split and to previous buffer
5117 */
5118 static void
5119ex_bprevious(eap)
5120 exarg_T *eap;
5121{
5122 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5123}
5124
5125/*
5126 * :brewind to first buffer
5127 * :bfirst to first buffer
5128 * :sbrewind split and to first buffer
5129 * :sbfirst split and to first buffer
5130 */
5131 static void
5132ex_brewind(eap)
5133 exarg_T *eap;
5134{
5135 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5136}
5137
5138/*
5139 * :blast to last buffer
5140 * :sblast split and to last buffer
5141 */
5142 static void
5143ex_blast(eap)
5144 exarg_T *eap;
5145{
5146 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5147}
5148#endif
5149
5150 int
5151ends_excmd(c)
5152 int c;
5153{
5154 return (c == NUL || c == '|' || c == '"' || c == '\n');
5155}
5156
5157#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5158 || defined(PROTO)
5159/*
5160 * Return the next command, after the first '|' or '\n'.
5161 * Return NULL if not found.
5162 */
5163 char_u *
5164find_nextcmd(p)
5165 char_u *p;
5166{
5167 while (*p != '|' && *p != '\n')
5168 {
5169 if (*p == NUL)
5170 return NULL;
5171 ++p;
5172 }
5173 return (p + 1);
5174}
5175#endif
5176
5177/*
5178 * Check if *p is a separator between Ex commands.
5179 * Return NULL if it isn't, (p + 1) if it is.
5180 */
5181 char_u *
5182check_nextcmd(p)
5183 char_u *p;
5184{
5185 p = skipwhite(p);
5186 if (*p == '|' || *p == '\n')
5187 return (p + 1);
5188 else
5189 return NULL;
5190}
5191
5192/*
5193 * - if there are more files to edit
5194 * - and this is the last window
5195 * - and forceit not used
5196 * - and not repeated twice on a row
5197 * return FAIL and give error message if 'message' TRUE
5198 * return OK otherwise
5199 */
5200 static int
5201check_more(message, forceit)
5202 int message; /* when FALSE check only, no messages */
5203 int forceit;
5204{
5205 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5206
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005207 if (!forceit && only_one_window()
5208 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 {
5210 if (message)
5211 {
5212#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5213 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5214 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005215 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216
5217 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005218 vim_strncpy(buff,
5219 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005220 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005222 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 _("%d more files to edit. Quit anyway?"), n);
5224 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5225 return OK;
5226 return FAIL;
5227 }
5228#endif
5229 if (n == 1)
5230 EMSG(_("E173: 1 more file to edit"));
5231 else
5232 EMSGN(_("E173: %ld more files to edit"), n);
5233 quitmore = 2; /* next try to quit is allowed */
5234 }
5235 return FAIL;
5236 }
5237 return OK;
5238}
5239
5240#ifdef FEAT_CMDL_COMPL
5241/*
5242 * Function given to ExpandGeneric() to obtain the list of command names.
5243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 char_u *
5245get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005246 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 int idx;
5248{
5249 if (idx >= (int)CMD_SIZE)
5250# ifdef FEAT_USR_CMDS
5251 return get_user_command_name(idx);
5252# else
5253 return NULL;
5254# endif
5255 return cmdnames[idx].cmd_name;
5256}
5257#endif
5258
5259#if defined(FEAT_USR_CMDS) || defined(PROTO)
5260static int uc_add_command __ARGS((char_u *name, size_t name_len, char_u *rep, long argt, long def, int flags, int compl, char_u *compl_arg, int force));
5261static void uc_list __ARGS((char_u *name, size_t name_len));
5262static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5263static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5264static size_t uc_check_code __ARGS((char_u *code, size_t len, char_u *buf, ucmd_T *cmd, exarg_T *eap, char_u **split_buf, size_t *split_len));
5265
5266 static int
5267uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5268 char_u *name;
5269 size_t name_len;
5270 char_u *rep;
5271 long argt;
5272 long def;
5273 int flags;
5274 int compl;
5275 char_u *compl_arg;
5276 int force;
5277{
5278 ucmd_T *cmd = NULL;
5279 char_u *p;
5280 int i;
5281 int cmp = 1;
5282 char_u *rep_buf = NULL;
5283 garray_T *gap;
5284
Bram Moolenaar9c102382006-05-03 21:26:49 +00005285 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 if (rep_buf == NULL)
5287 {
5288 /* Can't replace termcodes - try using the string as is */
5289 rep_buf = vim_strsave(rep);
5290
5291 /* Give up if out of memory */
5292 if (rep_buf == NULL)
5293 return FAIL;
5294 }
5295
5296 /* get address of growarray: global or in curbuf */
5297 if (flags & UC_BUFFER)
5298 {
5299 gap = &curbuf->b_ucmds;
5300 if (gap->ga_itemsize == 0)
5301 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5302 }
5303 else
5304 gap = &ucmds;
5305
5306 /* Search for the command in the already defined commands. */
5307 for (i = 0; i < gap->ga_len; ++i)
5308 {
5309 size_t len;
5310
5311 cmd = USER_CMD_GA(gap, i);
5312 len = STRLEN(cmd->uc_name);
5313 cmp = STRNCMP(name, cmd->uc_name, name_len);
5314 if (cmp == 0)
5315 {
5316 if (name_len < len)
5317 cmp = -1;
5318 else if (name_len > len)
5319 cmp = 1;
5320 }
5321
5322 if (cmp == 0)
5323 {
5324 if (!force)
5325 {
5326 EMSG(_("E174: Command already exists: add ! to replace it"));
5327 goto fail;
5328 }
5329
5330 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005331 cmd->uc_rep = NULL;
5332#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5333 vim_free(cmd->uc_compl_arg);
5334 cmd->uc_compl_arg = NULL;
5335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 break;
5337 }
5338
5339 /* Stop as soon as we pass the name to add */
5340 if (cmp < 0)
5341 break;
5342 }
5343
5344 /* Extend the array unless we're replacing an existing command */
5345 if (cmp != 0)
5346 {
5347 if (ga_grow(gap, 1) != OK)
5348 goto fail;
5349 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5350 goto fail;
5351
5352 cmd = USER_CMD_GA(gap, i);
5353 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5354
5355 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
5357 cmd->uc_name = p;
5358 }
5359
5360 cmd->uc_rep = rep_buf;
5361 cmd->uc_argt = argt;
5362 cmd->uc_def = def;
5363 cmd->uc_compl = compl;
5364#ifdef FEAT_EVAL
5365 cmd->uc_scriptID = current_SID;
5366# ifdef FEAT_CMDL_COMPL
5367 cmd->uc_compl_arg = compl_arg;
5368# endif
5369#endif
5370
5371 return OK;
5372
5373fail:
5374 vim_free(rep_buf);
5375#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5376 vim_free(compl_arg);
5377#endif
5378 return FAIL;
5379}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005382#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383/*
5384 * List of names for completion for ":command" with the EXPAND_ flag.
5385 * Must be alphabetical for completion.
5386 */
5387static struct
5388{
5389 int expand;
5390 char *name;
5391} command_complete[] =
5392{
5393 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005394 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005396 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005398 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005399#if defined(FEAT_CSCOPE)
5400 {EXPAND_CSCOPE, "cscope"},
5401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5403 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005404 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405#endif
5406 {EXPAND_DIRECTORIES, "dir"},
5407 {EXPAND_ENV_VARS, "environment"},
5408 {EXPAND_EVENTS, "event"},
5409 {EXPAND_EXPRESSION, "expression"},
5410 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005411 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005412 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 {EXPAND_FUNCTIONS, "function"},
5414 {EXPAND_HELP, "help"},
5415 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005416#if defined(FEAT_CMDHIST)
5417 {EXPAND_HISTORY, "history"},
5418#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005419#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005420 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005421 {EXPAND_LOCALES, "locale"},
5422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 {EXPAND_MAPPINGS, "mapping"},
5424 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005425 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005427 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005428#if defined(FEAT_SIGNS)
5429 {EXPAND_SIGN, "sign"},
5430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {EXPAND_TAGS, "tag"},
5432 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005433 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 {EXPAND_USER_VARS, "var"},
5435 {0, NULL}
5436};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005439#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 static void
5441uc_list(name, name_len)
5442 char_u *name;
5443 size_t name_len;
5444{
5445 int i, j;
5446 int found = FALSE;
5447 ucmd_T *cmd;
5448 int len;
5449 long a;
5450 garray_T *gap;
5451
5452 gap = &curbuf->b_ucmds;
5453 for (;;)
5454 {
5455 for (i = 0; i < gap->ga_len; ++i)
5456 {
5457 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005458 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459
5460 /* Skip commands which don't match the requested prefix */
5461 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5462 continue;
5463
5464 /* Put out the title first time */
5465 if (!found)
5466 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5467 found = TRUE;
5468 msg_putchar('\n');
5469 if (got_int)
5470 break;
5471
5472 /* Special cases */
5473 msg_putchar(a & BANG ? '!' : ' ');
5474 msg_putchar(a & REGSTR ? '"' : ' ');
5475 msg_putchar(gap != &ucmds ? 'b' : ' ');
5476 msg_putchar(' ');
5477
5478 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5479 len = (int)STRLEN(cmd->uc_name) + 4;
5480
5481 do {
5482 msg_putchar(' ');
5483 ++len;
5484 } while (len < 16);
5485
5486 len = 0;
5487
5488 /* Arguments */
5489 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5490 {
5491 case 0: IObuff[len++] = '0'; break;
5492 case (EXTRA): IObuff[len++] = '*'; break;
5493 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5494 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5495 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5496 }
5497
5498 do {
5499 IObuff[len++] = ' ';
5500 } while (len < 5);
5501
5502 /* Range */
5503 if (a & (RANGE|COUNT))
5504 {
5505 if (a & COUNT)
5506 {
5507 /* -count=N */
5508 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5509 len += (int)STRLEN(IObuff + len);
5510 }
5511 else if (a & DFLALL)
5512 IObuff[len++] = '%';
5513 else if (cmd->uc_def >= 0)
5514 {
5515 /* -range=N */
5516 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5517 len += (int)STRLEN(IObuff + len);
5518 }
5519 else
5520 IObuff[len++] = '.';
5521 }
5522
5523 do {
5524 IObuff[len++] = ' ';
5525 } while (len < 11);
5526
5527 /* Completion */
5528 for (j = 0; command_complete[j].expand != 0; ++j)
5529 if (command_complete[j].expand == cmd->uc_compl)
5530 {
5531 STRCPY(IObuff + len, command_complete[j].name);
5532 len += (int)STRLEN(IObuff + len);
5533 break;
5534 }
5535
5536 do {
5537 IObuff[len++] = ' ';
5538 } while (len < 21);
5539
5540 IObuff[len] = '\0';
5541 msg_outtrans(IObuff);
5542
5543 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005544#ifdef FEAT_EVAL
5545 if (p_verbose > 0)
5546 last_set_msg(cmd->uc_scriptID);
5547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 out_flush();
5549 ui_breakcheck();
5550 if (got_int)
5551 break;
5552 }
5553 if (gap == &ucmds || i < gap->ga_len)
5554 break;
5555 gap = &ucmds;
5556 }
5557
5558 if (!found)
5559 MSG(_("No user-defined commands found"));
5560}
5561
5562 static char_u *
5563uc_fun_cmd()
5564{
5565 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5566 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5567 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5568 0xb9, 0x7f, 0};
5569 int i;
5570
5571 for (i = 0; fcmd[i]; ++i)
5572 IObuff[i] = fcmd[i] - 0x40;
5573 IObuff[i] = 0;
5574 return IObuff;
5575}
5576
5577 static int
5578uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5579 char_u *attr;
5580 size_t len;
5581 long *argt;
5582 long *def;
5583 int *flags;
5584 int *compl;
5585 char_u **compl_arg;
5586{
5587 char_u *p;
5588
5589 if (len == 0)
5590 {
5591 EMSG(_("E175: No attribute specified"));
5592 return FAIL;
5593 }
5594
5595 /* First, try the simple attributes (no arguments) */
5596 if (STRNICMP(attr, "bang", len) == 0)
5597 *argt |= BANG;
5598 else if (STRNICMP(attr, "buffer", len) == 0)
5599 *flags |= UC_BUFFER;
5600 else if (STRNICMP(attr, "register", len) == 0)
5601 *argt |= REGSTR;
5602 else if (STRNICMP(attr, "bar", len) == 0)
5603 *argt |= TRLBAR;
5604 else
5605 {
5606 int i;
5607 char_u *val = NULL;
5608 size_t vallen = 0;
5609 size_t attrlen = len;
5610
5611 /* Look for the attribute name - which is the part before any '=' */
5612 for (i = 0; i < (int)len; ++i)
5613 {
5614 if (attr[i] == '=')
5615 {
5616 val = &attr[i + 1];
5617 vallen = len - i - 1;
5618 attrlen = i;
5619 break;
5620 }
5621 }
5622
5623 if (STRNICMP(attr, "nargs", attrlen) == 0)
5624 {
5625 if (vallen == 1)
5626 {
5627 if (*val == '0')
5628 /* Do nothing - this is the default */;
5629 else if (*val == '1')
5630 *argt |= (EXTRA | NOSPC | NEEDARG);
5631 else if (*val == '*')
5632 *argt |= EXTRA;
5633 else if (*val == '?')
5634 *argt |= (EXTRA | NOSPC);
5635 else if (*val == '+')
5636 *argt |= (EXTRA | NEEDARG);
5637 else
5638 goto wrong_nargs;
5639 }
5640 else
5641 {
5642wrong_nargs:
5643 EMSG(_("E176: Invalid number of arguments"));
5644 return FAIL;
5645 }
5646 }
5647 else if (STRNICMP(attr, "range", attrlen) == 0)
5648 {
5649 *argt |= RANGE;
5650 if (vallen == 1 && *val == '%')
5651 *argt |= DFLALL;
5652 else if (val != NULL)
5653 {
5654 p = val;
5655 if (*def >= 0)
5656 {
5657two_count:
5658 EMSG(_("E177: Count cannot be specified twice"));
5659 return FAIL;
5660 }
5661
5662 *def = getdigits(&p);
5663 *argt |= (ZEROR | NOTADR);
5664
5665 if (p != val + vallen || vallen == 0)
5666 {
5667invalid_count:
5668 EMSG(_("E178: Invalid default value for count"));
5669 return FAIL;
5670 }
5671 }
5672 }
5673 else if (STRNICMP(attr, "count", attrlen) == 0)
5674 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005675 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676
5677 if (val != NULL)
5678 {
5679 p = val;
5680 if (*def >= 0)
5681 goto two_count;
5682
5683 *def = getdigits(&p);
5684
5685 if (p != val + vallen)
5686 goto invalid_count;
5687 }
5688
5689 if (*def < 0)
5690 *def = 0;
5691 }
5692 else if (STRNICMP(attr, "complete", attrlen) == 0)
5693 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 if (val == NULL)
5695 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005696 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 return FAIL;
5698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005700 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5701 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 }
5704 else
5705 {
5706 char_u ch = attr[len];
5707 attr[len] = '\0';
5708 EMSG2(_("E181: Invalid attribute: %s"), attr);
5709 attr[len] = ch;
5710 return FAIL;
5711 }
5712 }
5713
5714 return OK;
5715}
5716
Bram Moolenaara850a712009-01-28 14:42:59 +00005717/*
5718 * ":command ..."
5719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 static void
5721ex_command(eap)
5722 exarg_T *eap;
5723{
5724 char_u *name;
5725 char_u *end;
5726 char_u *p;
5727 long argt = 0;
5728 long def = -1;
5729 int flags = 0;
5730 int compl = EXPAND_NOTHING;
5731 char_u *compl_arg = NULL;
5732 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005733 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
5735 p = eap->arg;
5736
5737 /* Check for attributes */
5738 while (*p == '-')
5739 {
5740 ++p;
5741 end = skiptowhite(p);
5742 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5743 == FAIL)
5744 return;
5745 p = skipwhite(end);
5746 }
5747
5748 /* Get the name (if any) and skip to the following argument */
5749 name = p;
5750 if (ASCII_ISALPHA(*p))
5751 while (ASCII_ISALNUM(*p))
5752 ++p;
5753 if (!ends_excmd(*p) && !vim_iswhite(*p))
5754 {
5755 EMSG(_("E182: Invalid command name"));
5756 return;
5757 }
5758 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005759 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
5761 /* If there is nothing after the name, and no attributes were specified,
5762 * we are listing commands
5763 */
5764 p = skipwhite(end);
5765 if (!has_attr && ends_excmd(*p))
5766 {
5767 uc_list(name, end - name);
5768 }
5769 else if (!ASCII_ISUPPER(*name))
5770 {
5771 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5772 return;
5773 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005774 else if ((name_len == 1 && *name == 'X')
5775 || (name_len <= 4
5776 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5777 {
5778 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5779 return;
5780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 else
5782 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5783 eap->forceit);
5784}
5785
5786/*
5787 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 * Clear all user commands, global and for current buffer.
5789 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005790 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005792 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793{
5794 uc_clear(&ucmds);
5795 uc_clear(&curbuf->b_ucmds);
5796}
5797
5798/*
5799 * Clear all user commands for "gap".
5800 */
5801 void
5802uc_clear(gap)
5803 garray_T *gap;
5804{
5805 int i;
5806 ucmd_T *cmd;
5807
5808 for (i = 0; i < gap->ga_len; ++i)
5809 {
5810 cmd = USER_CMD_GA(gap, i);
5811 vim_free(cmd->uc_name);
5812 vim_free(cmd->uc_rep);
5813# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5814 vim_free(cmd->uc_compl_arg);
5815# endif
5816 }
5817 ga_clear(gap);
5818}
5819
5820 static void
5821ex_delcommand(eap)
5822 exarg_T *eap;
5823{
5824 int i = 0;
5825 ucmd_T *cmd = NULL;
5826 int cmp = -1;
5827 garray_T *gap;
5828
5829 gap = &curbuf->b_ucmds;
5830 for (;;)
5831 {
5832 for (i = 0; i < gap->ga_len; ++i)
5833 {
5834 cmd = USER_CMD_GA(gap, i);
5835 cmp = STRCMP(eap->arg, cmd->uc_name);
5836 if (cmp <= 0)
5837 break;
5838 }
5839 if (gap == &ucmds || cmp == 0)
5840 break;
5841 gap = &ucmds;
5842 }
5843
5844 if (cmp != 0)
5845 {
5846 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5847 return;
5848 }
5849
5850 vim_free(cmd->uc_name);
5851 vim_free(cmd->uc_rep);
5852# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5853 vim_free(cmd->uc_compl_arg);
5854# endif
5855
5856 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
5858 if (i < gap->ga_len)
5859 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5860}
5861
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005862/*
5863 * split and quote args for <f-args>
5864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 static char_u *
5866uc_split_args(arg, lenp)
5867 char_u *arg;
5868 size_t *lenp;
5869{
5870 char_u *buf;
5871 char_u *p;
5872 char_u *q;
5873 int len;
5874
5875 /* Precalculate length */
5876 p = arg;
5877 len = 2; /* Initial and final quotes */
5878
5879 while (*p)
5880 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005881 if (p[0] == '\\' && p[1] == '\\')
5882 {
5883 len += 2;
5884 p += 2;
5885 }
5886 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 {
5888 len += 1;
5889 p += 2;
5890 }
5891 else if (*p == '\\' || *p == '"')
5892 {
5893 len += 2;
5894 p += 1;
5895 }
5896 else if (vim_iswhite(*p))
5897 {
5898 p = skipwhite(p);
5899 if (*p == NUL)
5900 break;
5901 len += 3; /* "," */
5902 }
5903 else
5904 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005905#ifdef FEAT_MBYTE
5906 int charlen = (*mb_ptr2len)(p);
5907 len += charlen;
5908 p += charlen;
5909#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 ++len;
5911 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02005912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 }
5914 }
5915
5916 buf = alloc(len + 1);
5917 if (buf == NULL)
5918 {
5919 *lenp = 0;
5920 return buf;
5921 }
5922
5923 p = arg;
5924 q = buf;
5925 *q++ = '"';
5926 while (*p)
5927 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005928 if (p[0] == '\\' && p[1] == '\\')
5929 {
5930 *q++ = '\\';
5931 *q++ = '\\';
5932 p += 2;
5933 }
5934 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 {
5936 *q++ = p[1];
5937 p += 2;
5938 }
5939 else if (*p == '\\' || *p == '"')
5940 {
5941 *q++ = '\\';
5942 *q++ = *p++;
5943 }
5944 else if (vim_iswhite(*p))
5945 {
5946 p = skipwhite(p);
5947 if (*p == NUL)
5948 break;
5949 *q++ = '"';
5950 *q++ = ',';
5951 *q++ = '"';
5952 }
5953 else
5954 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02005955 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 }
5957 }
5958 *q++ = '"';
5959 *q = 0;
5960
5961 *lenp = len;
5962 return buf;
5963}
5964
5965/*
5966 * Check for a <> code in a user command.
5967 * "code" points to the '<'. "len" the length of the <> (inclusive).
5968 * "buf" is where the result is to be added.
5969 * "split_buf" points to a buffer used for splitting, caller should free it.
5970 * "split_len" is the length of what "split_buf" contains.
5971 * Returns the length of the replacement, which has been added to "buf".
5972 * Returns -1 if there was no match, and only the "<" has been copied.
5973 */
5974 static size_t
5975uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5976 char_u *code;
5977 size_t len;
5978 char_u *buf;
5979 ucmd_T *cmd; /* the user command we're expanding */
5980 exarg_T *eap; /* ex arguments */
5981 char_u **split_buf;
5982 size_t *split_len;
5983{
5984 size_t result = 0;
5985 char_u *p = code + 1;
5986 size_t l = len - 2;
5987 int quote = 0;
5988 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5989 ct_LT, ct_NONE } type = ct_NONE;
5990
5991 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5992 {
5993 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5994 p += 2;
5995 l -= 2;
5996 }
5997
Bram Moolenaar371d5402006-03-20 21:47:49 +00005998 ++l;
5999 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006001 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006003 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006005 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006007 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006009 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006011 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006013 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 type = ct_REGISTER;
6015
6016 switch (type)
6017 {
6018 case ct_ARGS:
6019 /* Simple case first */
6020 if (*eap->arg == NUL)
6021 {
6022 if (quote == 1)
6023 {
6024 result = 2;
6025 if (buf != NULL)
6026 STRCPY(buf, "''");
6027 }
6028 else
6029 result = 0;
6030 break;
6031 }
6032
6033 /* When specified there is a single argument don't split it.
6034 * Works for ":Cmd %" when % is "a b c". */
6035 if ((eap->argt & NOSPC) && quote == 2)
6036 quote = 1;
6037
6038 switch (quote)
6039 {
6040 case 0: /* No quoting, no splitting */
6041 result = STRLEN(eap->arg);
6042 if (buf != NULL)
6043 STRCPY(buf, eap->arg);
6044 break;
6045 case 1: /* Quote, but don't split */
6046 result = STRLEN(eap->arg) + 2;
6047 for (p = eap->arg; *p; ++p)
6048 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006049#ifdef FEAT_MBYTE
6050 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6051 /* DBCS can contain \ in a trail byte, skip the
6052 * double-byte character. */
6053 ++p;
6054 else
6055#endif
6056 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 ++result;
6058 }
6059
6060 if (buf != NULL)
6061 {
6062 *buf++ = '"';
6063 for (p = eap->arg; *p; ++p)
6064 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006065#ifdef FEAT_MBYTE
6066 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6067 /* DBCS can contain \ in a trail byte, copy the
6068 * double-byte character to avoid escaping. */
6069 *buf++ = *p++;
6070 else
6071#endif
6072 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 *buf++ = '\\';
6074 *buf++ = *p;
6075 }
6076 *buf = '"';
6077 }
6078
6079 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006080 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 /* This is hard, so only do it once, and cache the result */
6082 if (*split_buf == NULL)
6083 *split_buf = uc_split_args(eap->arg, split_len);
6084
6085 result = *split_len;
6086 if (buf != NULL && result != 0)
6087 STRCPY(buf, *split_buf);
6088
6089 break;
6090 }
6091 break;
6092
6093 case ct_BANG:
6094 result = eap->forceit ? 1 : 0;
6095 if (quote)
6096 result += 2;
6097 if (buf != NULL)
6098 {
6099 if (quote)
6100 *buf++ = '"';
6101 if (eap->forceit)
6102 *buf++ = '!';
6103 if (quote)
6104 *buf = '"';
6105 }
6106 break;
6107
6108 case ct_LINE1:
6109 case ct_LINE2:
6110 case ct_COUNT:
6111 {
6112 char num_buf[20];
6113 long num = (type == ct_LINE1) ? eap->line1 :
6114 (type == ct_LINE2) ? eap->line2 :
6115 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6116 size_t num_len;
6117
6118 sprintf(num_buf, "%ld", num);
6119 num_len = STRLEN(num_buf);
6120 result = num_len;
6121
6122 if (quote)
6123 result += 2;
6124
6125 if (buf != NULL)
6126 {
6127 if (quote)
6128 *buf++ = '"';
6129 STRCPY(buf, num_buf);
6130 buf += num_len;
6131 if (quote)
6132 *buf = '"';
6133 }
6134
6135 break;
6136 }
6137
6138 case ct_REGISTER:
6139 result = eap->regname ? 1 : 0;
6140 if (quote)
6141 result += 2;
6142 if (buf != NULL)
6143 {
6144 if (quote)
6145 *buf++ = '\'';
6146 if (eap->regname)
6147 *buf++ = eap->regname;
6148 if (quote)
6149 *buf = '\'';
6150 }
6151 break;
6152
6153 case ct_LT:
6154 result = 1;
6155 if (buf != NULL)
6156 *buf = '<';
6157 break;
6158
6159 default:
6160 /* Not recognized: just copy the '<' and return -1. */
6161 result = (size_t)-1;
6162 if (buf != NULL)
6163 *buf = '<';
6164 break;
6165 }
6166
6167 return result;
6168}
6169
6170 static void
6171do_ucmd(eap)
6172 exarg_T *eap;
6173{
6174 char_u *buf;
6175 char_u *p;
6176 char_u *q;
6177
6178 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006179 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006180 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 size_t len, totlen;
6182
6183 size_t split_len = 0;
6184 char_u *split_buf = NULL;
6185 ucmd_T *cmd;
6186#ifdef FEAT_EVAL
6187 scid_T save_current_SID = current_SID;
6188#endif
6189
6190 if (eap->cmdidx == CMD_USER)
6191 cmd = USER_CMD(eap->useridx);
6192 else
6193 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6194
6195 /*
6196 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006197 * First round: "buf" is NULL, compute length, allocate "buf".
6198 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 */
6200 buf = NULL;
6201 for (;;)
6202 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006203 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006204 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006206
6207 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006209 start = vim_strchr(p, '<');
6210 if (start != NULL)
6211 end = vim_strchr(start + 1, '>');
6212 if (buf != NULL)
6213 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006214 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6215 ;
6216 if (*ksp == K_SPECIAL
6217 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006218 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6219# ifdef FEAT_GUI
6220 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6221# endif
6222 ))
6223 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006224 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006225 * KS_SPECIAL KE_FILLER, like for mappings, but
6226 * do_cmdline() doesn't handle that, so convert it back.
6227 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6228 len = ksp - p;
6229 if (len > 0)
6230 {
6231 mch_memmove(q, p, len);
6232 q += len;
6233 }
6234 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6235 p = ksp + 3;
6236 continue;
6237 }
6238 }
6239
6240 /* break if there no <item> is found */
6241 if (start == NULL || end == NULL)
6242 break;
6243
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 /* Include the '>' */
6245 ++end;
6246
6247 /* Take everything up to the '<' */
6248 len = start - p;
6249 if (buf == NULL)
6250 totlen += len;
6251 else
6252 {
6253 mch_memmove(q, p, len);
6254 q += len;
6255 }
6256
6257 len = uc_check_code(start, end - start, q, cmd, eap,
6258 &split_buf, &split_len);
6259 if (len == (size_t)-1)
6260 {
6261 /* no match, continue after '<' */
6262 p = start + 1;
6263 len = 1;
6264 }
6265 else
6266 p = end;
6267 if (buf == NULL)
6268 totlen += len;
6269 else
6270 q += len;
6271 }
6272 if (buf != NULL) /* second time here, finished */
6273 {
6274 STRCPY(q, p);
6275 break;
6276 }
6277
6278 totlen += STRLEN(p); /* Add on the trailing characters */
6279 buf = alloc((unsigned)(totlen + 1));
6280 if (buf == NULL)
6281 {
6282 vim_free(split_buf);
6283 return;
6284 }
6285 }
6286
6287#ifdef FEAT_EVAL
6288 current_SID = cmd->uc_scriptID;
6289#endif
6290 (void)do_cmdline(buf, eap->getline, eap->cookie,
6291 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6292#ifdef FEAT_EVAL
6293 current_SID = save_current_SID;
6294#endif
6295 vim_free(buf);
6296 vim_free(split_buf);
6297}
6298
6299# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6300 static char_u *
6301get_user_command_name(idx)
6302 int idx;
6303{
6304 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6305}
6306
6307/*
6308 * Function given to ExpandGeneric() to obtain the list of user command names.
6309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 char_u *
6311get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006312 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 int idx;
6314{
6315 if (idx < curbuf->b_ucmds.ga_len)
6316 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6317 idx -= curbuf->b_ucmds.ga_len;
6318 if (idx < ucmds.ga_len)
6319 return USER_CMD(idx)->uc_name;
6320 return NULL;
6321}
6322
6323/*
6324 * Function given to ExpandGeneric() to obtain the list of user command
6325 * attributes.
6326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 char_u *
6328get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006329 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 int idx;
6331{
6332 static char *user_cmd_flags[] =
6333 {"bang", "bar", "buffer", "complete", "count",
6334 "nargs", "range", "register"};
6335
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006336 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 return NULL;
6338 return (char_u *)user_cmd_flags[idx];
6339}
6340
6341/*
6342 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 char_u *
6345get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006346 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 int idx;
6348{
6349 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6350
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006351 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 return NULL;
6353 return (char_u *)user_cmd_nargs[idx];
6354}
6355
6356/*
6357 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 char_u *
6360get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006361 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 int idx;
6363{
6364 return (char_u *)command_complete[idx].name;
6365}
6366# endif /* FEAT_CMDL_COMPL */
6367
6368#endif /* FEAT_USR_CMDS */
6369
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006370#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6371/*
6372 * Parse a completion argument "value[vallen]".
6373 * The detected completion goes in "*complp", argument type in "*argt".
6374 * When there is an argument, for function and user defined completion, it's
6375 * copied to allocated memory and stored in "*compl_arg".
6376 * Returns FAIL if something is wrong.
6377 */
6378 int
6379parse_compl_arg(value, vallen, complp, argt, compl_arg)
6380 char_u *value;
6381 int vallen;
6382 int *complp;
6383 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006384 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006385{
6386 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006387# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006388 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006389# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006390 int i;
6391 int valend = vallen;
6392
6393 /* Look for any argument part - which is the part after any ',' */
6394 for (i = 0; i < vallen; ++i)
6395 {
6396 if (value[i] == ',')
6397 {
6398 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006399# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006400 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006401# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006402 valend = i;
6403 break;
6404 }
6405 }
6406
6407 for (i = 0; command_complete[i].expand != 0; ++i)
6408 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006409 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006410 && STRNCMP(value, command_complete[i].name, valend) == 0)
6411 {
6412 *complp = command_complete[i].expand;
6413 if (command_complete[i].expand == EXPAND_BUFFERS)
6414 *argt |= BUFNAME;
6415 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6416 || command_complete[i].expand == EXPAND_FILES)
6417 *argt |= XFILE;
6418 break;
6419 }
6420 }
6421
6422 if (command_complete[i].expand == 0)
6423 {
6424 EMSG2(_("E180: Invalid complete value: %s"), value);
6425 return FAIL;
6426 }
6427
6428# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6429 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6430 && arg != NULL)
6431# else
6432 if (arg != NULL)
6433# endif
6434 {
6435 EMSG(_("E468: Completion argument only allowed for custom completion"));
6436 return FAIL;
6437 }
6438
6439# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6440 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6441 && arg == NULL)
6442 {
6443 EMSG(_("E467: Custom completion requires a function argument"));
6444 return FAIL;
6445 }
6446
6447 if (arg != NULL)
6448 *compl_arg = vim_strnsave(arg, (int)arglen);
6449# endif
6450 return OK;
6451}
6452#endif
6453
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 static void
6455ex_colorscheme(eap)
6456 exarg_T *eap;
6457{
Bram Moolenaare6850792010-05-14 15:28:44 +02006458 if (*eap->arg == NUL)
6459 {
6460#ifdef FEAT_EVAL
6461 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6462 char_u *p = NULL;
6463
6464 if (expr != NULL)
6465 {
6466 ++emsg_off;
6467 p = eval_to_string(expr, NULL, FALSE);
6468 --emsg_off;
6469 vim_free(expr);
6470 }
6471 if (p != NULL)
6472 {
6473 MSG(p);
6474 vim_free(p);
6475 }
6476 else
6477 MSG("default");
6478#else
6479 MSG(_("unknown"));
6480#endif
6481 }
6482 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006483 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484}
6485
6486 static void
6487ex_highlight(eap)
6488 exarg_T *eap;
6489{
6490 if (*eap->arg == NUL && eap->cmd[2] == '!')
6491 MSG(_("Greetings, Vim user!"));
6492 do_highlight(eap->arg, eap->forceit, FALSE);
6493}
6494
6495
6496/*
6497 * Call this function if we thought we were going to exit, but we won't
6498 * (because of an error). May need to restore the terminal mode.
6499 */
6500 void
6501not_exiting()
6502{
6503 exiting = FALSE;
6504 settmode(TMODE_RAW);
6505}
6506
6507/*
6508 * ":quit": quit current window, quit Vim if closed the last window.
6509 */
6510 static void
6511ex_quit(eap)
6512 exarg_T *eap;
6513{
6514#ifdef FEAT_CMDWIN
6515 if (cmdwin_type != 0)
6516 {
6517 cmdwin_result = Ctrl_C;
6518 return;
6519 }
6520#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006521 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006522 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006523 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006524 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006525 return;
6526 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006527#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006528 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02006529 /* Refuse to quick when locked or when the buffer in the last window is
6530 * being closed (can only happen in autocommands). */
6531 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006532 return;
6533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534
6535#ifdef FEAT_NETBEANS_INTG
6536 netbeansForcedQuit = eap->forceit;
6537#endif
6538
6539 /*
6540 * If there are more files or windows we won't exit.
6541 */
6542 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6543 exiting = TRUE;
6544 if ((!P_HID(curbuf)
6545 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6546 || check_more(TRUE, eap->forceit) == FAIL
6547 || (only_one_window() && check_changed_any(eap->forceit)))
6548 {
6549 not_exiting();
6550 }
6551 else
6552 {
6553#ifdef FEAT_WINDOWS
6554 if (only_one_window()) /* quit last window */
6555#endif
6556 getout(0);
6557#ifdef FEAT_WINDOWS
6558# ifdef FEAT_GUI
6559 need_mouse_correct = TRUE;
6560# endif
6561 /* close window; may free buffer */
6562 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6563#endif
6564 }
6565}
6566
6567/*
6568 * ":cquit".
6569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 static void
6571ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006572 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573{
6574 getout(1); /* this does not always pass on the exit code to the Manx
6575 compiler. why? */
6576}
6577
6578/*
6579 * ":qall": try to quit all windows
6580 */
6581 static void
6582ex_quit_all(eap)
6583 exarg_T *eap;
6584{
6585# ifdef FEAT_CMDWIN
6586 if (cmdwin_type != 0)
6587 {
6588 if (eap->forceit)
6589 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6590 else
6591 cmdwin_result = K_XF2;
6592 return;
6593 }
6594# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006595
6596 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006597 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006598 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006599 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006600 return;
6601 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006602#ifdef FEAT_AUTOCMD
6603 if (curbuf_locked())
6604 return;
6605#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006606
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 exiting = TRUE;
6608 if (eap->forceit || !check_changed_any(FALSE))
6609 getout(0);
6610 not_exiting();
6611}
6612
Bram Moolenaard9967712006-03-11 21:18:15 +00006613#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614/*
6615 * ":close": close current window, unless it is the last one
6616 */
6617 static void
6618ex_close(eap)
6619 exarg_T *eap;
6620{
6621# ifdef FEAT_CMDWIN
6622 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006623 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 else
6625# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006626 if (!text_locked()
6627#ifdef FEAT_AUTOCMD
6628 && !curbuf_locked()
6629#endif
6630 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006631 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632}
6633
Bram Moolenaard9967712006-03-11 21:18:15 +00006634# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006635/*
6636 * ":pclose": Close any preview window.
6637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006639ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006641{
6642 win_T *win;
6643
6644 for (win = firstwin; win != NULL; win = win->w_next)
6645 if (win->w_p_pvw)
6646 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006647 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006648 break;
6649 }
6650}
Bram Moolenaard9967712006-03-11 21:18:15 +00006651# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006652
Bram Moolenaarf740b292006-02-16 22:11:02 +00006653/*
6654 * Close window "win" and take care of handling closing the last window for a
6655 * modified buffer.
6656 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006657 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006658ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006659 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006661 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662{
6663 int need_hide;
6664 buf_T *buf = win->w_buffer;
6665
6666 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006667 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006669# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 if ((p_confirm || cmdmod.confirm) && p_write)
6671 {
6672 dialog_changed(buf, FALSE);
6673 if (buf_valid(buf) && bufIsChanged(buf))
6674 return;
6675 need_hide = FALSE;
6676 }
6677 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {
6680 EMSG(_(e_nowrtmsg));
6681 return;
6682 }
6683 }
6684
Bram Moolenaard9967712006-03-11 21:18:15 +00006685# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006687# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006688
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006690 if (tp == NULL)
6691 win_close(win, !need_hide && !P_HID(buf));
6692 else
6693 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694}
6695
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006697 * ":tabclose": close current tab page, unless it is the last one.
6698 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 */
6700 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006701ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 exarg_T *eap;
6703{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006704 tabpage_T *tp;
6705
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006706# ifdef FEAT_CMDWIN
6707 if (cmdwin_type != 0)
6708 cmdwin_result = K_IGNORE;
6709 else
6710# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006711 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006712 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006713 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006715 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006716 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006717 tp = find_tabpage((int)eap->line2);
6718 if (tp == NULL)
6719 {
6720 beep_flush();
6721 return;
6722 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006723 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006724 {
6725 tabpage_close_other(tp, eap->forceit);
6726 return;
6727 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006728 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006729 if (!text_locked()
6730#ifdef FEAT_AUTOCMD
6731 && !curbuf_locked()
6732#endif
6733 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006734 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006736}
6737
6738/*
6739 * ":tabonly": close all tab pages except the current one
6740 */
6741 static void
6742ex_tabonly(eap)
6743 exarg_T *eap;
6744{
6745 tabpage_T *tp;
6746 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006747
6748# ifdef FEAT_CMDWIN
6749 if (cmdwin_type != 0)
6750 cmdwin_result = K_IGNORE;
6751 else
6752# endif
6753 if (first_tabpage->tp_next == NULL)
6754 MSG(_("Already only one tab page"));
6755 else
6756 {
6757 /* Repeat this up to a 1000 times, because autocommands may mess
6758 * up the lists. */
6759 for (done = 0; done < 1000; ++done)
6760 {
6761 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6762 if (tp->tp_topframe != topframe)
6763 {
6764 tabpage_close_other(tp, eap->forceit);
6765 /* if we failed to close it quit */
6766 if (valid_tabpage(tp))
6767 done = 1000;
6768 /* start over, "tp" is now invalid */
6769 break;
6770 }
6771 if (first_tabpage->tp_next == NULL)
6772 break;
6773 }
6774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776
6777/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006778 * Close the current tab page.
6779 */
6780 void
6781tabpage_close(forceit)
6782 int forceit;
6783{
6784 /* First close all the windows but the current one. If that worked then
6785 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006786 if (lastwin != firstwin)
6787 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006788 if (lastwin == firstwin)
6789 ex_win_close(forceit, curwin, NULL);
6790# ifdef FEAT_GUI
6791 need_mouse_correct = TRUE;
6792# endif
6793}
6794
6795/*
6796 * Close tab page "tp", which is not the current tab page.
6797 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006798 * Also takes care of the tab pages line disappearing when closing the
6799 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006800 */
6801 void
6802tabpage_close_other(tp, forceit)
6803 tabpage_T *tp;
6804 int forceit;
6805{
6806 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006807 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006808 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006809
6810 /* Limit to 1000 windows, autocommands may add a window while we close
6811 * one. OK, so I'm paranoid... */
6812 while (++done < 1000)
6813 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006814 wp = tp->tp_firstwin;
6815 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006816
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006817 /* Autocommands may delete the tab page under our fingers and we may
6818 * fail to close a window with a modified buffer. */
6819 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006820 break;
6821 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006822
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006823 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006824 if (h != tabline_height())
6825 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006826}
6827
6828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 * ":only".
6830 */
6831 static void
6832ex_only(eap)
6833 exarg_T *eap;
6834{
6835# ifdef FEAT_GUI
6836 need_mouse_correct = TRUE;
6837# endif
6838 close_others(TRUE, eap->forceit);
6839}
6840
6841/*
6842 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006843 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006845 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846ex_all(eap)
6847 exarg_T *eap;
6848{
6849 if (eap->addr_count == 0)
6850 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006851 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852}
6853#endif /* FEAT_WINDOWS */
6854
6855 static void
6856ex_hide(eap)
6857 exarg_T *eap;
6858{
6859 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6860 eap->errmsg = e_invarg;
6861 else
6862 {
6863 /* ":hide" or ":hide | cmd": hide current window */
6864 eap->nextcmd = check_nextcmd(eap->arg);
6865#ifdef FEAT_WINDOWS
6866 if (!eap->skip)
6867 {
6868# ifdef FEAT_GUI
6869 need_mouse_correct = TRUE;
6870# endif
6871 win_close(curwin, FALSE); /* don't free buffer */
6872 }
6873#endif
6874 }
6875}
6876
6877/*
6878 * ":stop" and ":suspend": Suspend Vim.
6879 */
6880 static void
6881ex_stop(eap)
6882 exarg_T *eap;
6883{
6884 /*
6885 * Disallow suspending for "rvim".
6886 */
6887 if (!check_restricted()
6888#ifdef WIN3264
6889 /*
6890 * Check if external commands are allowed now.
6891 */
6892 && can_end_termcap_mode(TRUE)
6893#endif
6894 )
6895 {
6896 if (!eap->forceit)
6897 autowrite_all();
6898 windgoto((int)Rows - 1, 0);
6899 out_char('\n');
6900 out_flush();
6901 stoptermcap();
6902 out_flush(); /* needed for SUN to restore xterm buffer */
6903#ifdef FEAT_TITLE
6904 mch_restore_title(3); /* restore window titles */
6905#endif
6906 ui_suspend(); /* call machine specific function */
6907#ifdef FEAT_TITLE
6908 maketitle();
6909 resettitle(); /* force updating the title */
6910#endif
6911 starttermcap();
6912 scroll_start(); /* scroll screen before redrawing */
6913 redraw_later_clear();
6914 shell_resized(); /* may have resized window */
6915 }
6916}
6917
6918/*
6919 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6920 */
6921 static void
6922ex_exit(eap)
6923 exarg_T *eap;
6924{
6925#ifdef FEAT_CMDWIN
6926 if (cmdwin_type != 0)
6927 {
6928 cmdwin_result = Ctrl_C;
6929 return;
6930 }
6931#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006932 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006933 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006934 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006935 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006936 return;
6937 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006938#ifdef FEAT_AUTOCMD
6939 if (curbuf_locked())
6940 return;
6941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942
6943 /*
6944 * if more files or windows we won't exit
6945 */
6946 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6947 exiting = TRUE;
6948 if ( ((eap->cmdidx == CMD_wq
6949 || curbufIsChanged())
6950 && do_write(eap) == FAIL)
6951 || check_more(TRUE, eap->forceit) == FAIL
6952 || (only_one_window() && check_changed_any(eap->forceit)))
6953 {
6954 not_exiting();
6955 }
6956 else
6957 {
6958#ifdef FEAT_WINDOWS
6959 if (only_one_window()) /* quit last window, exit Vim */
6960#endif
6961 getout(0);
6962#ifdef FEAT_WINDOWS
6963# ifdef FEAT_GUI
6964 need_mouse_correct = TRUE;
6965# endif
6966 /* quit current window, may free buffer */
6967 win_close(curwin, !P_HID(curwin->w_buffer));
6968#endif
6969 }
6970}
6971
6972/*
6973 * ":print", ":list", ":number".
6974 */
6975 static void
6976ex_print(eap)
6977 exarg_T *eap;
6978{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006979 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6980 EMSG(_(e_emptybuf));
6981 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006983 for ( ;!got_int; ui_breakcheck())
6984 {
6985 print_line(eap->line1,
6986 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6987 || (eap->flags & EXFLAG_NR)),
6988 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6989 if (++eap->line1 > eap->line2)
6990 break;
6991 out_flush(); /* show one line at a time */
6992 }
6993 setpcmark();
6994 /* put cursor at last line */
6995 curwin->w_cursor.lnum = eap->line2;
6996 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 }
6998
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000}
7001
7002#ifdef FEAT_BYTEOFF
7003 static void
7004ex_goto(eap)
7005 exarg_T *eap;
7006{
7007 goto_byte(eap->line2);
7008}
7009#endif
7010
7011/*
7012 * ":shell".
7013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 static void
7015ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007016 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017{
7018 do_shell(NULL, 0);
7019}
7020
7021#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7022 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007023 || defined(FEAT_GUI_MSWIN) \
7024 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 || defined(PROTO)
7026
7027/*
7028 * Handle a file drop. The code is here because a drop is *nearly* like an
7029 * :args command, but not quite (we have a list of exact filenames, so we
7030 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7031 * code is very similar to :args and hence needs access to a lot of the static
7032 * functions in this file.
7033 *
7034 * The list should be allocated using alloc(), as should each item in the
7035 * list. This function takes over responsibility for freeing the list.
7036 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007037 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7039 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7040 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7041 * file functionality is (currently) not in EMX this is not presently a
7042 * problem.
7043 */
7044 void
7045handle_drop(filec, filev, split)
7046 int filec; /* the number of files dropped */
7047 char_u **filev; /* the list of files dropped */
7048 int split; /* force splitting the window */
7049{
7050 exarg_T ea;
7051 int save_msg_scroll = msg_scroll;
7052
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007053 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007054 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007056#ifdef FEAT_AUTOCMD
7057 if (curbuf_locked())
7058 return;
7059#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007060 /* When the screen is being updated we should not change buffers and
7061 * windows structures, it may cause freed memory to be used. */
7062 if (updating_screen)
7063 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064
7065 /* Check whether the current buffer is changed. If so, we will need
7066 * to split the current window or data could be lost.
7067 * We don't need to check if the 'hidden' option is set, as in this
7068 * case the buffer won't be lost.
7069 */
7070 if (!P_HID(curbuf) && !split)
7071 {
7072 ++emsg_off;
7073 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
7074 --emsg_off;
7075 }
7076 if (split)
7077 {
7078# ifdef FEAT_WINDOWS
7079 if (win_split(0, 0) == FAIL)
7080 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007081 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082
7083 /* When splitting the window, create a new alist. Otherwise the
7084 * existing one is overwritten. */
7085 alist_unlink(curwin->w_alist);
7086 alist_new();
7087# else
7088 return; /* can't split, always fail */
7089# endif
7090 }
7091
7092 /*
7093 * Set up the new argument list.
7094 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007095 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
7097 /*
7098 * Move to the first file.
7099 */
7100 /* Fake up a minimal "next" command for do_argfile() */
7101 vim_memset(&ea, 0, sizeof(ea));
7102 ea.cmd = (char_u *)"next";
7103 do_argfile(&ea, 0);
7104
7105 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7106 * mode that is not needed here. */
7107 need_start_insertmode = FALSE;
7108
7109 /* Restore msg_scroll, otherwise a following command may cause scrolling
7110 * unexpectedly. The screen will be redrawn by the caller, thus
7111 * msg_scroll being set by displaying a message is irrelevant. */
7112 msg_scroll = save_msg_scroll;
7113}
7114#endif
7115
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116/*
7117 * Clear an argument list: free all file names and reset it to zero entries.
7118 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007119 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120alist_clear(al)
7121 alist_T *al;
7122{
7123 while (--al->al_ga.ga_len >= 0)
7124 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7125 ga_clear(&al->al_ga);
7126}
7127
7128/*
7129 * Init an argument list.
7130 */
7131 void
7132alist_init(al)
7133 alist_T *al;
7134{
7135 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7136}
7137
7138#if defined(FEAT_WINDOWS) || defined(PROTO)
7139
7140/*
7141 * Remove a reference from an argument list.
7142 * Ignored when the argument list is the global one.
7143 * If the argument list is no longer used by any window, free it.
7144 */
7145 void
7146alist_unlink(al)
7147 alist_T *al;
7148{
7149 if (al != &global_alist && --al->al_refcount <= 0)
7150 {
7151 alist_clear(al);
7152 vim_free(al);
7153 }
7154}
7155
7156# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7157/*
7158 * Create a new argument list and use it for the current window.
7159 */
7160 void
7161alist_new()
7162{
7163 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7164 if (curwin->w_alist == NULL)
7165 {
7166 curwin->w_alist = &global_alist;
7167 ++global_alist.al_refcount;
7168 }
7169 else
7170 {
7171 curwin->w_alist->al_refcount = 1;
7172 alist_init(curwin->w_alist);
7173 }
7174}
7175# endif
7176#endif
7177
7178#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7179/*
7180 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007181 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7182 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 */
7184 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007185alist_expand(fnum_list, fnum_len)
7186 int *fnum_list;
7187 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188{
7189 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007190 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 char_u **new_arg_files;
7192 int new_arg_file_count;
7193 char_u *save_p_su = p_su;
7194 int i;
7195
7196 /* Don't use 'suffixes' here. This should work like the shell did the
7197 * expansion. Also, the vimrc file isn't read yet, thus the user
7198 * can't set the options. */
7199 p_su = empty_option;
7200 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7201 if (old_arg_files != NULL)
7202 {
7203 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007204 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7205 old_arg_count = GARGCOUNT;
7206 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007208 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 && new_arg_file_count > 0)
7210 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007211 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7212 TRUE, fnum_list, fnum_len);
7213 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 }
7216 p_su = save_p_su;
7217}
7218#endif
7219
7220/*
7221 * Set the argument list for the current window.
7222 * Takes over the allocated files[] and the allocated fnames in it.
7223 */
7224 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007225alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 alist_T *al;
7227 int count;
7228 char_u **files;
7229 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007230 int *fnum_list;
7231 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232{
7233 int i;
7234
7235 alist_clear(al);
7236 if (ga_grow(&al->al_ga, count) == OK)
7237 {
7238 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007239 {
7240 if (got_int)
7241 {
7242 /* When adding many buffers this can take a long time. Allow
7243 * interrupting here. */
7244 while (i < count)
7245 vim_free(files[i++]);
7246 break;
7247 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007248
7249 /* May set buffer name of a buffer previously used for the
7250 * argument list, so that it's re-used by alist_add. */
7251 if (fnum_list != NULL && i < fnum_len)
7252 buf_set_name(fnum_list[i], files[i]);
7253
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007255 ui_breakcheck();
7256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 vim_free(files);
7258 }
7259 else
7260 FreeWild(count, files);
7261#ifdef FEAT_WINDOWS
7262 if (al == &global_alist)
7263#endif
7264 arg_had_last = FALSE;
7265}
7266
7267/*
7268 * Add file "fname" to argument list "al".
7269 * "fname" must have been allocated and "al" must have been checked for room.
7270 */
7271 void
7272alist_add(al, fname, set_fnum)
7273 alist_T *al;
7274 char_u *fname;
7275 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7276{
7277 if (fname == NULL) /* don't add NULL file names */
7278 return;
7279#ifdef BACKSLASH_IN_FILENAME
7280 slash_adjust(fname);
7281#endif
7282 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7283 if (set_fnum > 0)
7284 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7285 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7286 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287}
7288
7289#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7290/*
7291 * Adjust slashes in file names. Called after 'shellslash' was set.
7292 */
7293 void
7294alist_slash_adjust()
7295{
7296 int i;
7297# ifdef FEAT_WINDOWS
7298 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007299 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300# endif
7301
7302 for (i = 0; i < GARGCOUNT; ++i)
7303 if (GARGLIST[i].ae_fname != NULL)
7304 slash_adjust(GARGLIST[i].ae_fname);
7305# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007306 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 if (wp->w_alist != &global_alist)
7308 for (i = 0; i < WARGCOUNT(wp); ++i)
7309 if (WARGLIST(wp)[i].ae_fname != NULL)
7310 slash_adjust(WARGLIST(wp)[i].ae_fname);
7311# endif
7312}
7313#endif
7314
7315/*
7316 * ":preserve".
7317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 static void
7319ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007320 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007322 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 ml_preserve(curbuf, TRUE);
7324}
7325
7326/*
7327 * ":recover".
7328 */
7329 static void
7330ex_recover(eap)
7331 exarg_T *eap;
7332{
7333 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7334 recoverymode = TRUE;
7335 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7336 && (*eap->arg == NUL
7337 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7338 ml_recover();
7339 recoverymode = FALSE;
7340}
7341
7342/*
7343 * Command modifier used in a wrong way.
7344 */
7345 static void
7346ex_wrongmodifier(eap)
7347 exarg_T *eap;
7348{
7349 eap->errmsg = e_invcmd;
7350}
7351
7352#ifdef FEAT_WINDOWS
7353/*
7354 * :sview [+command] file split window with new file, read-only
7355 * :split [[+command] file] split window with current or new file
7356 * :vsplit [[+command] file] split window vertically with current or new file
7357 * :new [[+command] file] split window with no or new file
7358 * :vnew [[+command] file] split vertically window with no or new file
7359 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007360 *
7361 * :tabedit open new Tab page with empty window
7362 * :tabedit [+command] file open new Tab page and edit "file"
7363 * :tabnew [[+command] file] just like :tabedit
7364 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 */
7366 void
7367ex_splitview(eap)
7368 exarg_T *eap;
7369{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007370 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007371# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007373# endif
7374# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007378# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7380 {
7381 ex_ni(eap);
7382 return;
7383 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007386# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007390# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007392 * quickfix windows. But it's OK when doing ":tab split". */
7393 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 {
7395 if (eap->cmdidx == CMD_split)
7396 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007397# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 if (eap->cmdidx == CMD_vsplit)
7399 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007404# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007405 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 {
7407 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7408 FNAME_MESS, TRUE, curbuf->b_ffname);
7409 if (fname == NULL)
7410 goto theend;
7411 eap->arg = fname;
7412 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007413# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007417# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007419# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 && eap->cmdidx != CMD_new)
7423 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007424# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007425 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007426# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007427 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007428# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007429 au_has_group((char_u *)"FileExplorer"))
7430 {
7431 /* No browsing supported but we do have the file explorer:
7432 * Edit the directory. */
7433 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7434 eap->arg = (char_u *)".";
7435 }
7436 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007437# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007438 {
7439 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007441 if (fname == NULL)
7442 goto theend;
7443 eap->arg = fname;
7444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 }
7446 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007449 /*
7450 * Either open new tab page or split the window.
7451 */
7452 if (eap->cmdidx == CMD_tabedit
7453 || eap->cmdidx == CMD_tabfind
7454 || eap->cmdidx == CMD_tabnew)
7455 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007456 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7457 : eap->addr_count == 0 ? 0
7458 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007459 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007460 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007461
7462 /* set the alternate buffer for the window we came from */
7463 if (curwin != old_curwin
7464 && win_valid(old_curwin)
7465 && old_curwin->w_buffer != curbuf
7466 && !cmdmod.keepalt)
7467 old_curwin->w_alt_fnum = curbuf->b_fnum;
7468 }
7469 }
7470 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7472 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007473# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 /* Reset 'scrollbind' when editing another file, but keep it when
7475 * doing ":split" without arguments. */
7476 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007477# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007481 {
7482 RESET_BINDING(curwin);
7483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 else
7485 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007486# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 do_exedit(eap, old_curwin);
7488 }
7489
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007490# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007492# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007494# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495theend:
7496 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007497# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007499
7500/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007501 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007502 */
7503 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007504tabpage_new()
7505{
7506 exarg_T ea;
7507
7508 vim_memset(&ea, 0, sizeof(ea));
7509 ea.cmdidx = CMD_tabnew;
7510 ea.cmd = (char_u *)"tabn";
7511 ea.arg = (char_u *)"";
7512 ex_splitview(&ea);
7513}
7514
7515/*
7516 * :tabnext command
7517 */
7518 static void
7519ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007520 exarg_T *eap;
7521{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007522 switch (eap->cmdidx)
7523 {
7524 case CMD_tabfirst:
7525 case CMD_tabrewind:
7526 goto_tabpage(1);
7527 break;
7528 case CMD_tablast:
7529 goto_tabpage(9999);
7530 break;
7531 case CMD_tabprevious:
7532 case CMD_tabNext:
7533 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7534 break;
7535 default: /* CMD_tabnext */
7536 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7537 break;
7538 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007539}
7540
7541/*
7542 * :tabmove command
7543 */
7544 static void
7545ex_tabmove(eap)
7546 exarg_T *eap;
7547{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007548 int tab_number = 9999;
7549
7550 if (eap->arg && *eap->arg != NUL)
7551 {
7552 char_u *p = eap->arg;
7553 int relative = 0; /* argument +N/-N means: move N places to the
7554 * right/left relative to the current position. */
7555
7556 if (*eap->arg == '-')
7557 {
7558 relative = -1;
7559 p = eap->arg + 1;
7560 }
7561 else if (*eap->arg == '+')
7562 {
7563 relative = 1;
7564 p = eap->arg + 1;
7565 }
7566 else
7567 p = eap->arg;
7568
7569 if (p == skipdigits(p))
7570 {
7571 /* No numbers as argument. */
7572 eap->errmsg = e_invarg;
7573 return;
7574 }
7575
7576 tab_number = getdigits(&p);
7577 if (relative != 0)
7578 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7579 }
7580 else if (eap->addr_count != 0)
7581 tab_number = eap->line2;
7582
7583 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007584}
7585
7586/*
7587 * :tabs command: List tabs and their contents.
7588 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007589 static void
7590ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007591 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007592{
7593 tabpage_T *tp;
7594 win_T *wp;
7595 int tabcount = 1;
7596
7597 msg_start();
7598 msg_scroll = TRUE;
7599 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7600 {
7601 msg_putchar('\n');
7602 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7603 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7604 out_flush(); /* output one line at a time */
7605 ui_breakcheck();
7606
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007607 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007608 wp = firstwin;
7609 else
7610 wp = tp->tp_firstwin;
7611 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7612 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007613 msg_putchar('\n');
7614 msg_putchar(wp == curwin ? '>' : ' ');
7615 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007616 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7617 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007618 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007619 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007620 else
7621 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7622 IObuff, IOSIZE, TRUE);
7623 msg_outtrans(IObuff);
7624 out_flush(); /* output one line at a time */
7625 ui_breakcheck();
7626 }
7627 }
7628}
7629
7630#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631
7632/*
7633 * ":mode": Set screen mode.
7634 * If no argument given, just get the screen size and redraw.
7635 */
7636 static void
7637ex_mode(eap)
7638 exarg_T *eap;
7639{
7640 if (*eap->arg == NUL)
7641 shell_resized();
7642 else
7643 mch_screenmode(eap->arg);
7644}
7645
7646#ifdef FEAT_WINDOWS
7647/*
7648 * ":resize".
7649 * set, increment or decrement current window height
7650 */
7651 static void
7652ex_resize(eap)
7653 exarg_T *eap;
7654{
7655 int n;
7656 win_T *wp = curwin;
7657
7658 if (eap->addr_count > 0)
7659 {
7660 n = eap->line2;
7661 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7662 ;
7663 }
7664
7665#ifdef FEAT_GUI
7666 need_mouse_correct = TRUE;
7667#endif
7668 n = atol((char *)eap->arg);
7669#ifdef FEAT_VERTSPLIT
7670 if (cmdmod.split & WSP_VERT)
7671 {
7672 if (*eap->arg == '-' || *eap->arg == '+')
7673 n += W_WIDTH(curwin);
7674 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7675 n = 9999;
7676 win_setwidth_win((int)n, wp);
7677 }
7678 else
7679#endif
7680 {
7681 if (*eap->arg == '-' || *eap->arg == '+')
7682 n += curwin->w_height;
7683 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7684 n = 9999;
7685 win_setheight_win((int)n, wp);
7686 }
7687}
7688#endif
7689
7690/*
7691 * ":find [+command] <file>" command.
7692 */
7693 static void
7694ex_find(eap)
7695 exarg_T *eap;
7696{
7697#ifdef FEAT_SEARCHPATH
7698 char_u *fname;
7699 int count;
7700
7701 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7702 TRUE, curbuf->b_ffname);
7703 if (eap->addr_count > 0)
7704 {
7705 /* Repeat finding the file "count" times. This matters when it
7706 * appears several times in the path. */
7707 count = eap->line2;
7708 while (fname != NULL && --count > 0)
7709 {
7710 vim_free(fname);
7711 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7712 FALSE, curbuf->b_ffname);
7713 }
7714 }
7715
7716 if (fname != NULL)
7717 {
7718 eap->arg = fname;
7719#endif
7720 do_exedit(eap, NULL);
7721#ifdef FEAT_SEARCHPATH
7722 vim_free(fname);
7723 }
7724#endif
7725}
7726
7727/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007728 * ":open" simulation: for now just work like ":visual".
7729 */
7730 static void
7731ex_open(eap)
7732 exarg_T *eap;
7733{
7734 regmatch_T regmatch;
7735 char_u *p;
7736
7737 curwin->w_cursor.lnum = eap->line2;
7738 beginline(BL_SOL | BL_FIX);
7739 if (*eap->arg == '/')
7740 {
7741 /* ":open /pattern/": put cursor in column found with pattern */
7742 ++eap->arg;
7743 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7744 *p = NUL;
7745 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7746 if (regmatch.regprog != NULL)
7747 {
7748 regmatch.rm_ic = p_ic;
7749 p = ml_get_curline();
7750 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007751 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007752 else
7753 EMSG(_(e_nomatch));
7754 vim_free(regmatch.regprog);
7755 }
7756 /* Move to the NUL, ignore any other arguments. */
7757 eap->arg += STRLEN(eap->arg);
7758 }
7759 check_cursor();
7760
7761 eap->cmdidx = CMD_visual;
7762 do_exedit(eap, NULL);
7763}
7764
7765/*
7766 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 */
7768 static void
7769ex_edit(eap)
7770 exarg_T *eap;
7771{
7772 do_exedit(eap, NULL);
7773}
7774
7775/*
7776 * ":edit <file>" command and alikes.
7777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 void
7779do_exedit(eap, old_curwin)
7780 exarg_T *eap;
7781 win_T *old_curwin; /* curwin before doing a split or NULL */
7782{
7783 int n;
7784#ifdef FEAT_WINDOWS
7785 int need_hide;
7786#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007787 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788
7789 /*
7790 * ":vi" command ends Ex mode.
7791 */
7792 if (exmode_active && (eap->cmdidx == CMD_visual
7793 || eap->cmdidx == CMD_view))
7794 {
7795 exmode_active = FALSE;
7796 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007797 {
7798 /* Special case: ":global/pat/visual\NLvi-commands" */
7799 if (global_busy)
7800 {
7801 int rd = RedrawingDisabled;
7802 int nwr = no_wait_return;
7803 int ms = msg_scroll;
7804#ifdef FEAT_GUI
7805 int he = hold_gui_events;
7806#endif
7807
7808 if (eap->nextcmd != NULL)
7809 {
7810 stuffReadbuff(eap->nextcmd);
7811 eap->nextcmd = NULL;
7812 }
7813
7814 if (exmode_was != EXMODE_VIM)
7815 settmode(TMODE_RAW);
7816 RedrawingDisabled = 0;
7817 no_wait_return = 0;
7818 need_wait_return = FALSE;
7819 msg_scroll = 0;
7820#ifdef FEAT_GUI
7821 hold_gui_events = 0;
7822#endif
7823 must_redraw = CLEAR;
7824
7825 main_loop(FALSE, TRUE);
7826
7827 RedrawingDisabled = rd;
7828 no_wait_return = nwr;
7829 msg_scroll = ms;
7830#ifdef FEAT_GUI
7831 hold_gui_events = he;
7832#endif
7833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 }
7837
7838 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007839 || eap->cmdidx == CMD_tabnew
7840 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841#ifdef FEAT_VERTSPLIT
7842 || eap->cmdidx == CMD_vnew
7843#endif
7844 ) && *eap->arg == NUL)
7845 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007846 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 setpcmark();
7848 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007849 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7850 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 }
7852 else if ((eap->cmdidx != CMD_split
7853#ifdef FEAT_VERTSPLIT
7854 && eap->cmdidx != CMD_vsplit
7855#endif
7856 )
7857 || *eap->arg != NUL
7858#ifdef FEAT_BROWSE
7859 || cmdmod.browse
7860#endif
7861 )
7862 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007863#ifdef FEAT_AUTOCMD
7864 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7865 * can bring us here, others are stopped earlier. */
7866 if (*eap->arg != NUL && curbuf_locked())
7867 return;
7868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 n = readonlymode;
7870 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7871 readonlymode = TRUE;
7872 else if (eap->cmdidx == CMD_enew)
7873 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7874 empty buffer */
7875 setpcmark();
7876 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7877 NULL, eap,
7878 /* ":edit" goes to first line if Vi compatible */
7879 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7880 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7881 ? ECMD_ONE : eap->do_ecmd_lnum,
7882 (P_HID(curbuf) ? ECMD_HIDE : 0)
7883 + (eap->forceit ? ECMD_FORCEIT : 0)
7884#ifdef FEAT_LISTCMDS
7885 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7886#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007887 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {
7889 /* Editing the file failed. If the window was split, close it. */
7890#ifdef FEAT_WINDOWS
7891 if (old_curwin != NULL)
7892 {
7893 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7894 if (!need_hide || P_HID(curbuf))
7895 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007896# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7897 cleanup_T cs;
7898
7899 /* Reset the error/interrupt/exception state here so that
7900 * aborting() returns FALSE when closing a window. */
7901 enter_cleanup(&cs);
7902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903# ifdef FEAT_GUI
7904 need_mouse_correct = TRUE;
7905# endif
7906 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007907
7908# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7909 /* Restore the error/interrupt/exception state if not
7910 * discarded by a new aborting error, interrupt, or
7911 * uncaught exception. */
7912 leave_cleanup(&cs);
7913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 }
7915 }
7916#endif
7917 }
7918 else if (readonlymode && curbuf->b_nwindows == 1)
7919 {
7920 /* When editing an already visited buffer, 'readonly' won't be set
7921 * but the previous value is kept. With ":view" and ":sview" we
7922 * want the file to be readonly, except when another window is
7923 * editing the same buffer. */
7924 curbuf->b_p_ro = TRUE;
7925 }
7926 readonlymode = n;
7927 }
7928 else
7929 {
7930 if (eap->do_ecmd_cmd != NULL)
7931 do_cmdline_cmd(eap->do_ecmd_cmd);
7932#ifdef FEAT_TITLE
7933 n = curwin->w_arg_idx_invalid;
7934#endif
7935 check_arg_idx(curwin);
7936#ifdef FEAT_TITLE
7937 if (n != curwin->w_arg_idx_invalid)
7938 maketitle();
7939#endif
7940 }
7941
7942#ifdef FEAT_WINDOWS
7943 /*
7944 * if ":split file" worked, set alternate file name in old window to new
7945 * file
7946 */
7947 if (old_curwin != NULL
7948 && *eap->arg != NUL
7949 && curwin != old_curwin
7950 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007951 && old_curwin->w_buffer != curbuf
7952 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 old_curwin->w_alt_fnum = curbuf->b_fnum;
7954#endif
7955
7956 ex_no_reprint = TRUE;
7957}
7958
7959#ifndef FEAT_GUI
7960/*
7961 * ":gui" and ":gvim" when there is no GUI.
7962 */
7963 static void
7964ex_nogui(eap)
7965 exarg_T *eap;
7966{
7967 eap->errmsg = e_nogvim;
7968}
7969#endif
7970
7971#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7972 static void
7973ex_tearoff(eap)
7974 exarg_T *eap;
7975{
7976 gui_make_tearoff(eap->arg);
7977}
7978#endif
7979
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007980#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 static void
7982ex_popup(eap)
7983 exarg_T *eap;
7984{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007985 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986}
7987#endif
7988
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 static void
7990ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007991 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992{
7993 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7994 MSG(_("No swap file"));
7995 else
7996 msg(curbuf->b_ml.ml_mfp->mf_fname);
7997}
7998
7999/*
8000 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8001 * offset.
8002 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 static void
8005ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008006 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007{
8008#ifdef FEAT_SCROLLBIND
8009 win_T *wp;
8010 long topline;
8011 long y;
8012 linenr_T old_linenr = curwin->w_cursor.lnum;
8013
8014 setpcmark();
8015
8016 /*
8017 * determine max topline
8018 */
8019 if (curwin->w_p_scb)
8020 {
8021 topline = curwin->w_topline;
8022 for (wp = firstwin; wp; wp = wp->w_next)
8023 {
8024 if (wp->w_p_scb && wp->w_buffer)
8025 {
8026 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8027 if (topline > y)
8028 topline = y;
8029 }
8030 }
8031 if (topline < 1)
8032 topline = 1;
8033 }
8034 else
8035 {
8036 topline = 1;
8037 }
8038
8039
8040 /*
8041 * set all scrollbind windows to the same topline
8042 */
8043 wp = curwin;
8044 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8045 {
8046 if (curwin->w_p_scb)
8047 {
8048 y = topline - curwin->w_topline;
8049 if (y > 0)
8050 scrollup(y, TRUE);
8051 else
8052 scrolldown(-y, TRUE);
8053 curwin->w_scbind_pos = topline;
8054 redraw_later(VALID);
8055 cursor_correct();
8056#ifdef FEAT_WINDOWS
8057 curwin->w_redr_status = TRUE;
8058#endif
8059 }
8060 }
8061 curwin = wp;
8062 if (curwin->w_p_scb)
8063 {
8064 did_syncbind = TRUE;
8065 checkpcmark();
8066 if (old_linenr != curwin->w_cursor.lnum)
8067 {
8068 char_u ctrl_o[2];
8069
8070 ctrl_o[0] = Ctrl_O;
8071 ctrl_o[1] = 0;
8072 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8073 }
8074 }
8075#endif
8076}
8077
8078
8079 static void
8080ex_read(eap)
8081 exarg_T *eap;
8082{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008083 int i;
8084 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8085 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086
8087 if (eap->usefilter) /* :r!cmd */
8088 do_bang(1, eap, FALSE, FALSE, TRUE);
8089 else
8090 {
8091 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8092 return;
8093
8094#ifdef FEAT_BROWSE
8095 if (cmdmod.browse)
8096 {
8097 char_u *browseFile;
8098
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008099 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 NULL, NULL, NULL, curbuf);
8101 if (browseFile != NULL)
8102 {
8103 i = readfile(browseFile, NULL,
8104 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8105 vim_free(browseFile);
8106 }
8107 else
8108 i = OK;
8109 }
8110 else
8111#endif
8112 if (*eap->arg == NUL)
8113 {
8114 if (check_fname() == FAIL) /* check for no file name */
8115 return;
8116 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8117 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8118 }
8119 else
8120 {
8121 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8122 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8123 i = readfile(eap->arg, NULL,
8124 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8125
8126 }
8127 if (i == FAIL)
8128 {
8129#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8130 if (!aborting())
8131#endif
8132 EMSG2(_(e_notopen), eap->arg);
8133 }
8134 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008135 {
8136 if (empty && exmode_active)
8137 {
8138 /* Delete the empty line that remains. Historically ex does
8139 * this but vi doesn't. */
8140 if (eap->line2 == 0)
8141 lnum = curbuf->b_ml.ml_line_count;
8142 else
8143 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008144 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008145 {
8146 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008147 if (curwin->w_cursor.lnum > 1
8148 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008149 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008150 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008151 }
8152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 }
8156}
8157
Bram Moolenaarea408852005-06-25 22:49:46 +00008158static char_u *prev_dir = NULL;
8159
8160#if defined(EXITFREE) || defined(PROTO)
8161 void
8162free_cd_dir()
8163{
8164 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008165 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008166
8167 vim_free(globaldir);
8168 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008169}
8170#endif
8171
8172
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173/*
8174 * ":cd", ":lcd", ":chdir" and ":lchdir".
8175 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008176 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177ex_cd(eap)
8178 exarg_T *eap;
8179{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 char_u *new_dir;
8181 char_u *tofree;
8182
8183 new_dir = eap->arg;
8184#if !defined(UNIX) && !defined(VMS)
8185 /* for non-UNIX ":cd" means: print current directory */
8186 if (*new_dir == NUL)
8187 ex_pwd(NULL);
8188 else
8189#endif
8190 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008191#ifdef FEAT_AUTOCMD
8192 if (allbuf_locked())
8193 return;
8194#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008195 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8196 && !eap->forceit)
8197 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008198 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008199 return;
8200 }
8201
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 /* ":cd -": Change to previous directory */
8203 if (STRCMP(new_dir, "-") == 0)
8204 {
8205 if (prev_dir == NULL)
8206 {
8207 EMSG(_("E186: No previous directory"));
8208 return;
8209 }
8210 new_dir = prev_dir;
8211 }
8212
8213 /* Save current directory for next ":cd -" */
8214 tofree = prev_dir;
8215 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8216 prev_dir = vim_strsave(NameBuff);
8217 else
8218 prev_dir = NULL;
8219
8220#if defined(UNIX) || defined(VMS)
8221 /* for UNIX ":cd" means: go to home directory */
8222 if (*new_dir == NUL)
8223 {
8224 /* use NameBuff for home directory name */
8225# ifdef VMS
8226 char_u *p;
8227
8228 p = mch_getenv((char_u *)"SYS$LOGIN");
8229 if (p == NULL || *p == NUL) /* empty is the same as not set */
8230 NameBuff[0] = NUL;
8231 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008232 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233# else
8234 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8235# endif
8236 new_dir = NameBuff;
8237 }
8238#endif
8239 if (new_dir == NULL || vim_chdir(new_dir))
8240 EMSG(_(e_failed));
8241 else
8242 {
8243 vim_free(curwin->w_localdir);
8244 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8245 {
8246 /* If still in global directory, need to remember current
8247 * directory as global directory. */
8248 if (globaldir == NULL && prev_dir != NULL)
8249 globaldir = vim_strsave(prev_dir);
8250 /* Remember this local directory for the window. */
8251 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8252 curwin->w_localdir = vim_strsave(NameBuff);
8253 }
8254 else
8255 {
8256 /* We are now in the global directory, no need to remember its
8257 * name. */
8258 vim_free(globaldir);
8259 globaldir = NULL;
8260 curwin->w_localdir = NULL;
8261 }
8262
8263 shorten_fnames(TRUE);
8264
8265 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008266 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 ex_pwd(eap);
8268 }
8269 vim_free(tofree);
8270 }
8271}
8272
8273/*
8274 * ":pwd".
8275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 static void
8277ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008278 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279{
8280 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8281 {
8282#ifdef BACKSLASH_IN_FILENAME
8283 slash_adjust(NameBuff);
8284#endif
8285 msg(NameBuff);
8286 }
8287 else
8288 EMSG(_("E187: Unknown"));
8289}
8290
8291/*
8292 * ":=".
8293 */
8294 static void
8295ex_equal(eap)
8296 exarg_T *eap;
8297{
8298 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008299 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300}
8301
8302 static void
8303ex_sleep(eap)
8304 exarg_T *eap;
8305{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008306 int n;
8307 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308
8309 if (cursor_valid())
8310 {
8311 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8312 if (n >= 0)
8313 windgoto((int)n, curwin->w_wcol);
8314 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008315
8316 len = eap->line2;
8317 switch (*eap->arg)
8318 {
8319 case 'm': break;
8320 case NUL: len *= 1000L; break;
8321 default: EMSG2(_(e_invarg2), eap->arg); return;
8322 }
8323 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324}
8325
8326/*
8327 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8328 */
8329 void
8330do_sleep(msec)
8331 long msec;
8332{
8333 long done;
8334
8335 cursor_on();
8336 out_flush();
8337 for (done = 0; !got_int && done < msec; done += 1000L)
8338 {
8339 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8340 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008341#ifdef FEAT_NETBEANS_INTG
8342 /* Process the netbeans messages that may have been received in the
8343 * call to ui_breakcheck() when the GUI is in use. This may occur when
8344 * running a test case. */
8345 netbeans_parse_messages();
8346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 }
8348}
8349
8350 static void
8351do_exmap(eap, isabbrev)
8352 exarg_T *eap;
8353 int isabbrev;
8354{
8355 int mode;
8356 char_u *cmdp;
8357
8358 cmdp = eap->cmd;
8359 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8360
8361 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8362 eap->arg, mode, isabbrev))
8363 {
8364 case 1: EMSG(_(e_invarg));
8365 break;
8366 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8367 break;
8368 }
8369}
8370
8371/*
8372 * ":winsize" command (obsolete).
8373 */
8374 static void
8375ex_winsize(eap)
8376 exarg_T *eap;
8377{
8378 int w, h;
8379 char_u *arg = eap->arg;
8380 char_u *p;
8381
8382 w = getdigits(&arg);
8383 arg = skipwhite(arg);
8384 p = arg;
8385 h = getdigits(&arg);
8386 if (*p != NUL && *arg == NUL)
8387 set_shellsize(w, h, TRUE);
8388 else
8389 EMSG(_("E465: :winsize requires two number arguments"));
8390}
8391
8392#ifdef FEAT_WINDOWS
8393 static void
8394ex_wincmd(eap)
8395 exarg_T *eap;
8396{
8397 int xchar = NUL;
8398 char_u *p;
8399
8400 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8401 {
8402 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8403 if (eap->arg[1] == NUL)
8404 {
8405 EMSG(_(e_invarg));
8406 return;
8407 }
8408 xchar = eap->arg[1];
8409 p = eap->arg + 2;
8410 }
8411 else
8412 p = eap->arg + 1;
8413
8414 eap->nextcmd = check_nextcmd(p);
8415 p = skipwhite(p);
8416 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8417 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008418 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 {
8420 /* Pass flags on for ":vertical wincmd ]". */
8421 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008422 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8424 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008425 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 }
8427}
8428#endif
8429
Bram Moolenaar843ee412004-06-30 16:16:41 +00008430#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431/*
8432 * ":winpos".
8433 */
8434 static void
8435ex_winpos(eap)
8436 exarg_T *eap;
8437{
8438 int x, y;
8439 char_u *arg = eap->arg;
8440 char_u *p;
8441
8442 if (*arg == NUL)
8443 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008444# if defined(FEAT_GUI) || defined(MSWIN)
8445# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008447# else
8448 if (mch_get_winpos(&x, &y) != FAIL)
8449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 {
8451 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8452 msg(IObuff);
8453 }
8454 else
8455# endif
8456 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8457 }
8458 else
8459 {
8460 x = getdigits(&arg);
8461 arg = skipwhite(arg);
8462 p = arg;
8463 y = getdigits(&arg);
8464 if (*p == NUL || *arg != NUL)
8465 {
8466 EMSG(_("E466: :winpos requires two number arguments"));
8467 return;
8468 }
8469# ifdef FEAT_GUI
8470 if (gui.in_use)
8471 gui_mch_set_winpos(x, y);
8472 else if (gui.starting)
8473 {
8474 /* Remember the coordinates for when the window is opened. */
8475 gui_win_x = x;
8476 gui_win_y = y;
8477 }
8478# ifdef HAVE_TGETENT
8479 else
8480# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008481# else
8482# ifdef MSWIN
8483 mch_set_winpos(x, y);
8484# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485# endif
8486# ifdef HAVE_TGETENT
8487 if (*T_CWP)
8488 term_set_winpos(x, y);
8489# endif
8490 }
8491}
8492#endif
8493
8494/*
8495 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8496 */
8497 static void
8498ex_operators(eap)
8499 exarg_T *eap;
8500{
8501 oparg_T oa;
8502
8503 clear_oparg(&oa);
8504 oa.regname = eap->regname;
8505 oa.start.lnum = eap->line1;
8506 oa.end.lnum = eap->line2;
8507 oa.line_count = eap->line2 - eap->line1 + 1;
8508 oa.motion_type = MLINE;
8509#ifdef FEAT_VIRTUALEDIT
8510 virtual_op = FALSE;
8511#endif
8512 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8513 {
8514 setpcmark();
8515 curwin->w_cursor.lnum = eap->line1;
8516 beginline(BL_SOL | BL_FIX);
8517 }
8518
8519 switch (eap->cmdidx)
8520 {
8521 case CMD_delete:
8522 oa.op_type = OP_DELETE;
8523 op_delete(&oa);
8524 break;
8525
8526 case CMD_yank:
8527 oa.op_type = OP_YANK;
8528 (void)op_yank(&oa, FALSE, TRUE);
8529 break;
8530
8531 default: /* CMD_rshift or CMD_lshift */
8532 if ((eap->cmdidx == CMD_rshift)
8533#ifdef FEAT_RIGHTLEFT
8534 ^ curwin->w_p_rl
8535#endif
8536 )
8537 oa.op_type = OP_RSHIFT;
8538 else
8539 oa.op_type = OP_LSHIFT;
8540 op_shift(&oa, FALSE, eap->amount);
8541 break;
8542 }
8543#ifdef FEAT_VIRTUALEDIT
8544 virtual_op = MAYBE;
8545#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008546 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547}
8548
8549/*
8550 * ":put".
8551 */
8552 static void
8553ex_put(eap)
8554 exarg_T *eap;
8555{
8556 /* ":0put" works like ":1put!". */
8557 if (eap->line2 == 0)
8558 {
8559 eap->line2 = 1;
8560 eap->forceit = TRUE;
8561 }
8562 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008563 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8564 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565}
8566
8567/*
8568 * Handle ":copy" and ":move".
8569 */
8570 static void
8571ex_copymove(eap)
8572 exarg_T *eap;
8573{
8574 long n;
8575
8576 n = get_address(&eap->arg, FALSE, FALSE);
8577 if (eap->arg == NULL) /* error detected */
8578 {
8579 eap->nextcmd = NULL;
8580 return;
8581 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008582 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583
8584 /*
8585 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8586 */
8587 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8588 {
8589 EMSG(_(e_invaddr));
8590 return;
8591 }
8592
8593 if (eap->cmdidx == CMD_move)
8594 {
8595 if (do_move(eap->line1, eap->line2, n) == FAIL)
8596 return;
8597 }
8598 else
8599 ex_copy(eap->line1, eap->line2, n);
8600 u_clearline();
8601 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008602 ex_may_print(eap);
8603}
8604
8605/*
8606 * Print the current line if flags were given to the Ex command.
8607 */
8608 static void
8609ex_may_print(eap)
8610 exarg_T *eap;
8611{
8612 if (eap->flags != 0)
8613 {
8614 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8615 (eap->flags & EXFLAG_LIST));
8616 ex_no_reprint = TRUE;
8617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618}
8619
8620/*
8621 * ":smagic" and ":snomagic".
8622 */
8623 static void
8624ex_submagic(eap)
8625 exarg_T *eap;
8626{
8627 int magic_save = p_magic;
8628
8629 p_magic = (eap->cmdidx == CMD_smagic);
8630 do_sub(eap);
8631 p_magic = magic_save;
8632}
8633
8634/*
8635 * ":join".
8636 */
8637 static void
8638ex_join(eap)
8639 exarg_T *eap;
8640{
8641 curwin->w_cursor.lnum = eap->line1;
8642 if (eap->line1 == eap->line2)
8643 {
8644 if (eap->addr_count >= 2) /* :2,2join does nothing */
8645 return;
8646 if (eap->line2 == curbuf->b_ml.ml_line_count)
8647 {
8648 beep_flush();
8649 return;
8650 }
8651 ++eap->line2;
8652 }
Bram Moolenaar81340392012-06-06 16:12:59 +02008653 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008655 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656}
8657
8658/*
8659 * ":[addr]@r" or ":[addr]*r": execute register
8660 */
8661 static void
8662ex_at(eap)
8663 exarg_T *eap;
8664{
8665 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008666 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667
8668 curwin->w_cursor.lnum = eap->line2;
8669
8670#ifdef USE_ON_FLY_SCROLL
8671 dont_scroll = TRUE; /* disallow scrolling here */
8672#endif
8673
8674 /* get the register name. No name means to use the previous one */
8675 c = *eap->arg;
8676 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8677 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008678 /* Put the register in the typeahead buffer with the "silent" flag. */
8679 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8680 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008681 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 else
8685 {
8686 int save_efr = exec_from_reg;
8687
8688 exec_from_reg = TRUE;
8689
8690 /*
8691 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008692 * Continue until the stuff buffer is empty and all added characters
8693 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008695 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8697
8698 exec_from_reg = save_efr;
8699 }
8700}
8701
8702/*
8703 * ":!".
8704 */
8705 static void
8706ex_bang(eap)
8707 exarg_T *eap;
8708{
8709 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8710}
8711
8712/*
8713 * ":undo".
8714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 static void
8716ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008717 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008719 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008720 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008721 else
8722 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723}
8724
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008725#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008726 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008727ex_wundo(eap)
8728 exarg_T *eap;
8729{
8730 char_u hash[UNDO_HASH_SIZE];
8731
8732 u_compute_hash(hash);
8733 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8734}
8735
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008736 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008737ex_rundo(eap)
8738 exarg_T *eap;
8739{
8740 char_u hash[UNDO_HASH_SIZE];
8741
8742 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008743 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008744}
8745#endif
8746
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747/*
8748 * ":redo".
8749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 static void
8751ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008752 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753{
8754 u_redo(1);
8755}
8756
8757/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008758 * ":earlier" and ":later".
8759 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008760 static void
8761ex_later(eap)
8762 exarg_T *eap;
8763{
8764 long count = 0;
8765 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008766 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008767 char_u *p = eap->arg;
8768
8769 if (*p == NUL)
8770 count = 1;
8771 else if (isdigit(*p))
8772 {
8773 count = getdigits(&p);
8774 switch (*p)
8775 {
8776 case 's': ++p; sec = TRUE; break;
8777 case 'm': ++p; sec = TRUE; count *= 60; break;
8778 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008779 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8780 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008781 }
8782 }
8783
8784 if (*p != NUL)
8785 EMSG2(_(e_invarg2), eap->arg);
8786 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008787 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8788 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008789}
8790
8791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 * ":redir": start/stop redirection.
8793 */
8794 static void
8795ex_redir(eap)
8796 exarg_T *eap;
8797{
8798 char *mode;
8799 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008800 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801
8802 if (STRICMP(eap->arg, "END") == 0)
8803 close_redir();
8804 else
8805 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008806 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008808 ++arg;
8809 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008811 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 mode = "a";
8813 }
8814 else
8815 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008816 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817
8818 close_redir();
8819
8820 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008821 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 if (fname == NULL)
8823 return;
8824#ifdef FEAT_BROWSE
8825 if (cmdmod.browse)
8826 {
8827 char_u *browseFile;
8828
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008829 browseFile = do_browse(BROWSE_SAVE,
8830 (char_u *)_("Save Redirection"),
8831 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 if (browseFile == NULL)
8833 return; /* operation cancelled */
8834 vim_free(fname);
8835 fname = browseFile;
8836 eap->forceit = TRUE; /* since dialog already asked */
8837 }
8838#endif
8839
8840 redir_fd = open_exfile(fname, eap->forceit, mode);
8841 vim_free(fname);
8842 }
8843#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008844 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 {
8846 /* redirect to a register a-z (resp. A-Z for appending) */
8847 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008848 ++arg;
8849 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008851 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008852 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008854 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008856 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008857 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008858 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008859 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008861 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008862 if (*arg == '>')
8863 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008864 /* Make register empty when not using @A-@Z and the
8865 * command is valid. */
8866 if (*arg == NUL && !isupper(redir_reg))
8867 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008869 }
8870 if (*arg != NUL)
8871 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008872 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008873 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008874 }
8875 }
8876 else if (*arg == '=' && arg[1] == '>')
8877 {
8878 int append;
8879
8880 /* redirect to a variable */
8881 close_redir();
8882 arg += 2;
8883
8884 if (*arg == '>')
8885 {
8886 ++arg;
8887 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 }
8889 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008890 append = FALSE;
8891
8892 if (var_redir_start(skipwhite(arg), append) == OK)
8893 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 }
8895#endif
8896
8897 /* TODO: redirect to a buffer */
8898
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008900 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008902
8903 /* Make sure redirection is not off. Can happen for cmdline completion
8904 * that indirectly invokes a command to catch its output. */
8905 if (redir_fd != NULL
8906#ifdef FEAT_EVAL
8907 || redir_reg || redir_vname
8908#endif
8909 )
8910 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911}
8912
8913/*
8914 * ":redraw": force redraw
8915 */
8916 static void
8917ex_redraw(eap)
8918 exarg_T *eap;
8919{
8920 int r = RedrawingDisabled;
8921 int p = p_lz;
8922
8923 RedrawingDisabled = 0;
8924 p_lz = FALSE;
8925 update_topline();
8926 update_screen(eap->forceit ? CLEAR :
8927#ifdef FEAT_VISUAL
8928 VIsual_active ? INVERTED :
8929#endif
8930 0);
8931#ifdef FEAT_TITLE
8932 if (need_maketitle)
8933 maketitle();
8934#endif
8935 RedrawingDisabled = r;
8936 p_lz = p;
8937
8938 /* Reset msg_didout, so that a message that's there is overwritten. */
8939 msg_didout = FALSE;
8940 msg_col = 0;
8941
8942 out_flush();
8943}
8944
8945/*
8946 * ":redrawstatus": force redraw of status line(s)
8947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 static void
8949ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008950 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951{
8952#if defined(FEAT_WINDOWS)
8953 int r = RedrawingDisabled;
8954 int p = p_lz;
8955
8956 RedrawingDisabled = 0;
8957 p_lz = FALSE;
8958 if (eap->forceit)
8959 status_redraw_all();
8960 else
8961 status_redraw_curbuf();
8962 update_screen(
8963# ifdef FEAT_VISUAL
8964 VIsual_active ? INVERTED :
8965# endif
8966 0);
8967 RedrawingDisabled = r;
8968 p_lz = p;
8969 out_flush();
8970#endif
8971}
8972
8973 static void
8974close_redir()
8975{
8976 if (redir_fd != NULL)
8977 {
8978 fclose(redir_fd);
8979 redir_fd = NULL;
8980 }
8981#ifdef FEAT_EVAL
8982 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008983 if (redir_vname)
8984 {
8985 var_redir_stop();
8986 redir_vname = 0;
8987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988#endif
8989}
8990
8991#if defined(FEAT_SESSION) && defined(USE_CRNL)
8992# define MKSESSION_NL
8993static int mksession_nl = FALSE; /* use NL only in put_eol() */
8994#endif
8995
8996/*
8997 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8998 */
8999 static void
9000ex_mkrc(eap)
9001 exarg_T *eap;
9002{
9003 FILE *fd;
9004 int failed = FALSE;
9005 char_u *fname;
9006#ifdef FEAT_BROWSE
9007 char_u *browseFile = NULL;
9008#endif
9009#ifdef FEAT_SESSION
9010 int view_session = FALSE;
9011 int using_vdir = FALSE; /* using 'viewdir'? */
9012 char_u *viewFile = NULL;
9013 unsigned *flagp;
9014#endif
9015
9016 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9017 {
9018#ifdef FEAT_SESSION
9019 view_session = TRUE;
9020#else
9021 ex_ni(eap);
9022 return;
9023#endif
9024 }
9025
9026#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009027 /* Use the short file name until ":lcd" is used. We also don't use the
9028 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009029 did_lcd = FALSE;
9030
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9032 if (eap->cmdidx == CMD_mkview
9033 && (*eap->arg == NUL
9034 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9035 {
9036 eap->forceit = TRUE;
9037 fname = get_view_file(*eap->arg);
9038 if (fname == NULL)
9039 return;
9040 viewFile = fname;
9041 using_vdir = TRUE;
9042 }
9043 else
9044#endif
9045 if (*eap->arg != NUL)
9046 fname = eap->arg;
9047 else if (eap->cmdidx == CMD_mkvimrc)
9048 fname = (char_u *)VIMRC_FILE;
9049#ifdef FEAT_SESSION
9050 else if (eap->cmdidx == CMD_mksession)
9051 fname = (char_u *)SESSION_FILE;
9052#endif
9053 else
9054 fname = (char_u *)EXRC_FILE;
9055
9056#ifdef FEAT_BROWSE
9057 if (cmdmod.browse)
9058 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009059 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060# ifdef FEAT_SESSION
9061 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9062 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9063# endif
9064 (char_u *)_("Save Setup"),
9065 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9066 if (browseFile == NULL)
9067 goto theend;
9068 fname = browseFile;
9069 eap->forceit = TRUE; /* since dialog already asked */
9070 }
9071#endif
9072
9073#if defined(FEAT_SESSION) && defined(vim_mkdir)
9074 /* When using 'viewdir' may have to create the directory. */
9075 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009076 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077#endif
9078
9079 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9080 if (fd != NULL)
9081 {
9082#ifdef FEAT_SESSION
9083 if (eap->cmdidx == CMD_mkview)
9084 flagp = &vop_flags;
9085 else
9086 flagp = &ssop_flags;
9087#endif
9088
9089#ifdef MKSESSION_NL
9090 /* "unix" in 'sessionoptions': use NL line separator */
9091 if (view_session && (*flagp & SSOP_UNIX))
9092 mksession_nl = TRUE;
9093#endif
9094
9095 /* Write the version command for :mkvimrc */
9096 if (eap->cmdidx == CMD_mkvimrc)
9097 (void)put_line(fd, "version 6.0");
9098
9099#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009100 if (eap->cmdidx == CMD_mksession)
9101 {
9102 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9103 failed = TRUE;
9104 }
9105
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 if (eap->cmdidx != CMD_mkview)
9107#endif
9108 {
9109 /* Write setting 'compatible' first, because it has side effects.
9110 * For that same reason only do it when needed. */
9111 if (p_cp)
9112 (void)put_line(fd, "if !&cp | set cp | endif");
9113 else
9114 (void)put_line(fd, "if &cp | set nocp | endif");
9115 }
9116
9117#ifdef FEAT_SESSION
9118 if (!view_session
9119 || (eap->cmdidx == CMD_mksession
9120 && (*flagp & SSOP_OPTIONS)))
9121#endif
9122 failed |= (makemap(fd, NULL) == FAIL
9123 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9124
9125#ifdef FEAT_SESSION
9126 if (!failed && view_session)
9127 {
9128 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9129 failed = TRUE;
9130 if (eap->cmdidx == CMD_mksession)
9131 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009132 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133
Bram Moolenaard9462e32011-04-11 21:35:11 +02009134 dirnow = alloc(MAXPATHL);
9135 if (dirnow == NULL)
9136 failed = TRUE;
9137 else
9138 {
9139 /*
9140 * Change to session file's dir.
9141 */
9142 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009144 *dirnow = NUL;
9145 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9146 {
9147 if (vim_chdirfile(fname) == OK)
9148 shorten_fnames(TRUE);
9149 }
9150 else if (*dirnow != NUL
9151 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9152 {
9153 if (mch_chdir((char *)globaldir) == 0)
9154 shorten_fnames(TRUE);
9155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156
Bram Moolenaard9462e32011-04-11 21:35:11 +02009157 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158
Bram Moolenaard9462e32011-04-11 21:35:11 +02009159 /* restore original dir */
9160 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009162 {
9163 if (mch_chdir((char *)dirnow) != 0)
9164 EMSG(_(e_prev_dir));
9165 shorten_fnames(TRUE);
9166 }
9167 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 }
9169 }
9170 else
9171 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009172 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9173 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 }
9175 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9176 == FAIL)
9177 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009178 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9179 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009180 if (eap->cmdidx == CMD_mksession)
9181 {
9182 if (put_line(fd, "unlet SessionLoad") == FAIL)
9183 failed = TRUE;
9184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 }
9186#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009187 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9188 failed = TRUE;
9189
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 failed |= fclose(fd);
9191
9192 if (failed)
9193 EMSG(_(e_write));
9194#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9195 else if (eap->cmdidx == CMD_mksession)
9196 {
9197 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009198 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199
Bram Moolenaard9462e32011-04-11 21:35:11 +02009200 tbuf = alloc(MAXPATHL);
9201 if (tbuf != NULL)
9202 {
9203 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9204 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9205 vim_free(tbuf);
9206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 }
9208#endif
9209#ifdef MKSESSION_NL
9210 mksession_nl = FALSE;
9211#endif
9212 }
9213
9214#ifdef FEAT_BROWSE
9215theend:
9216 vim_free(browseFile);
9217#endif
9218#ifdef FEAT_SESSION
9219 vim_free(viewFile);
9220#endif
9221}
9222
Bram Moolenaardf177f62005-02-22 08:39:57 +00009223#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9224 || defined(PROTO)
9225 int
9226vim_mkdir_emsg(name, prot)
9227 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009228 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009229{
9230 if (vim_mkdir(name, prot) != 0)
9231 {
9232 EMSG2(_("E739: Cannot create directory: %s"), name);
9233 return FAIL;
9234 }
9235 return OK;
9236}
9237#endif
9238
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239/*
9240 * Open a file for writing for an Ex command, with some checks.
9241 * Return file descriptor, or NULL on failure.
9242 */
9243 FILE *
9244open_exfile(fname, forceit, mode)
9245 char_u *fname;
9246 int forceit;
9247 char *mode; /* "w" for create new file or "a" for append */
9248{
9249 FILE *fd;
9250
9251#ifdef UNIX
9252 /* with Unix it is possible to open a directory */
9253 if (mch_isdir(fname))
9254 {
9255 EMSG2(_(e_isadir2), fname);
9256 return NULL;
9257 }
9258#endif
9259 if (!forceit && *mode != 'a' && vim_fexists(fname))
9260 {
9261 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9262 return NULL;
9263 }
9264
9265 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9266 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9267
9268 return fd;
9269}
9270
9271/*
9272 * ":mark" and ":k".
9273 */
9274 static void
9275ex_mark(eap)
9276 exarg_T *eap;
9277{
9278 pos_T pos;
9279
9280 if (*eap->arg == NUL) /* No argument? */
9281 EMSG(_(e_argreq));
9282 else if (eap->arg[1] != NUL) /* more than one character? */
9283 EMSG(_(e_trailing));
9284 else
9285 {
9286 pos = curwin->w_cursor; /* save curwin->w_cursor */
9287 curwin->w_cursor.lnum = eap->line2;
9288 beginline(BL_WHITE | BL_FIX);
9289 if (setmark(*eap->arg) == FAIL) /* set mark */
9290 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9291 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9292 }
9293}
9294
9295/*
9296 * Update w_topline, w_leftcol and the cursor position.
9297 */
9298 void
9299update_topline_cursor()
9300{
9301 check_cursor(); /* put cursor on valid line */
9302 update_topline();
9303 if (!curwin->w_p_wrap)
9304 validate_cursor();
9305 update_curswant();
9306}
9307
9308#ifdef FEAT_EX_EXTRA
9309/*
9310 * ":normal[!] {commands}": Execute normal mode commands.
9311 */
9312 static void
9313ex_normal(eap)
9314 exarg_T *eap;
9315{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 int save_msg_scroll = msg_scroll;
9317 int save_restart_edit = restart_edit;
9318 int save_msg_didout = msg_didout;
9319 int save_State = State;
9320 tasave_T tabuf;
9321 int save_insertmode = p_im;
9322 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009323 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324#ifdef FEAT_MBYTE
9325 char_u *arg = NULL;
9326 int l;
9327 char_u *p;
9328#endif
9329
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009330 if (ex_normal_lock > 0)
9331 {
9332 EMSG(_(e_secure));
9333 return;
9334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 if (ex_normal_busy >= p_mmd)
9336 {
9337 EMSG(_("E192: Recursive use of :normal too deep"));
9338 return;
9339 }
9340 ++ex_normal_busy;
9341
9342 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9343 restart_edit = 0; /* don't go to Insert mode */
9344 p_im = FALSE; /* don't use 'insertmode' */
9345
9346#ifdef FEAT_MBYTE
9347 /*
9348 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9349 * this for the K_SPECIAL leading byte, otherwise special keys will not
9350 * work.
9351 */
9352 if (has_mbyte)
9353 {
9354 int len = 0;
9355
9356 /* Count the number of characters to be escaped. */
9357 for (p = eap->arg; *p != NUL; ++p)
9358 {
9359# ifdef FEAT_GUI
9360 if (*p == CSI) /* leadbyte CSI */
9361 len += 2;
9362# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009363 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9365# ifdef FEAT_GUI
9366 || *p == CSI
9367# endif
9368 )
9369 len += 2;
9370 }
9371 if (len > 0)
9372 {
9373 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9374 if (arg != NULL)
9375 {
9376 len = 0;
9377 for (p = eap->arg; *p != NUL; ++p)
9378 {
9379 arg[len++] = *p;
9380# ifdef FEAT_GUI
9381 if (*p == CSI)
9382 {
9383 arg[len++] = KS_EXTRA;
9384 arg[len++] = (int)KE_CSI;
9385 }
9386# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009387 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 {
9389 arg[len++] = *++p;
9390 if (*p == K_SPECIAL)
9391 {
9392 arg[len++] = KS_SPECIAL;
9393 arg[len++] = KE_FILLER;
9394 }
9395# ifdef FEAT_GUI
9396 else if (*p == CSI)
9397 {
9398 arg[len++] = KS_EXTRA;
9399 arg[len++] = (int)KE_CSI;
9400 }
9401# endif
9402 }
9403 arg[len] = NUL;
9404 }
9405 }
9406 }
9407 }
9408#endif
9409
9410 /*
9411 * Save the current typeahead. This is required to allow using ":normal"
9412 * from an event handler and makes sure we don't hang when the argument
9413 * ends with half a command.
9414 */
9415 save_typeahead(&tabuf);
9416 if (tabuf.typebuf_valid)
9417 {
9418 /*
9419 * Repeat the :normal command for each line in the range. When no
9420 * range given, execute it just once, without positioning the cursor
9421 * first.
9422 */
9423 do
9424 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 if (eap->addr_count != 0)
9426 {
9427 curwin->w_cursor.lnum = eap->line1++;
9428 curwin->w_cursor.col = 0;
9429 }
9430
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009431 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432#ifdef FEAT_MBYTE
9433 arg != NULL ? arg :
9434#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009435 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 }
9437 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9438 }
9439
9440 /* Might not return to the main loop when in an event handler. */
9441 update_topline_cursor();
9442
9443 /* Restore the previous typeahead. */
9444 restore_typeahead(&tabuf);
9445
9446 --ex_normal_busy;
9447 msg_scroll = save_msg_scroll;
9448 restart_edit = save_restart_edit;
9449 p_im = save_insertmode;
9450 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009451 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9453
9454 /* Restore the state (needed when called from a function executed for
9455 * 'indentexpr'). */
9456 State = save_State;
9457#ifdef FEAT_MBYTE
9458 vim_free(arg);
9459#endif
9460}
9461
9462/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009463 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 */
9465 static void
9466ex_startinsert(eap)
9467 exarg_T *eap;
9468{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009469 if (eap->forceit)
9470 {
9471 coladvance((colnr_T)MAXCOL);
9472 curwin->w_curswant = MAXCOL;
9473 curwin->w_set_curswant = FALSE;
9474 }
9475
Bram Moolenaara40c5002005-01-09 21:16:21 +00009476 /* Ignore the command when already in Insert mode. Inserting an
9477 * expression register that invokes a function can do this. */
9478 if (State & INSERT)
9479 return;
9480
Bram Moolenaar64969662005-12-14 21:59:55 +00009481 if (eap->cmdidx == CMD_startinsert)
9482 restart_edit = 'a';
9483 else if (eap->cmdidx == CMD_startreplace)
9484 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009486 restart_edit = 'V';
9487
9488 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009490 if (eap->cmdidx == CMD_startinsert)
9491 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 curwin->w_curswant = 0; /* avoid MAXCOL */
9493 }
9494}
9495
9496/*
9497 * ":stopinsert"
9498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 static void
9500ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009501 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502{
9503 restart_edit = 0;
9504 stop_insert_mode = TRUE;
9505}
9506#endif
9507
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009508#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9509/*
9510 * Execute normal mode command "cmd".
9511 * "remap" can be REMAP_NONE or REMAP_YES.
9512 */
9513 void
9514exec_normal_cmd(cmd, remap, silent)
9515 char_u *cmd;
9516 int remap;
9517 int silent;
9518{
9519 oparg_T oa;
9520
9521 /*
9522 * Stuff the argument into the typeahead buffer.
9523 * Execute normal_cmd() until there is no typeahead left.
9524 */
9525 clear_oparg(&oa);
9526 finish_op = FALSE;
9527 ins_typebuf(cmd, remap, 0, TRUE, silent);
9528 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9529 && !got_int)
9530 {
9531 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009532 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009533 }
9534}
9535#endif
9536
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537#ifdef FEAT_FIND_ID
9538 static void
9539ex_checkpath(eap)
9540 exarg_T *eap;
9541{
9542 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9543 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9544 (linenr_T)1, (linenr_T)MAXLNUM);
9545}
9546
9547#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9548/*
9549 * ":psearch"
9550 */
9551 static void
9552ex_psearch(eap)
9553 exarg_T *eap;
9554{
9555 g_do_tagpreview = p_pvh;
9556 ex_findpat(eap);
9557 g_do_tagpreview = 0;
9558}
9559#endif
9560
9561 static void
9562ex_findpat(eap)
9563 exarg_T *eap;
9564{
9565 int whole = TRUE;
9566 long n;
9567 char_u *p;
9568 int action;
9569
9570 switch (cmdnames[eap->cmdidx].cmd_name[2])
9571 {
9572 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9573 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9574 action = ACTION_GOTO;
9575 else
9576 action = ACTION_SHOW;
9577 break;
9578 case 'i': /* ":ilist" and ":dlist" */
9579 action = ACTION_SHOW_ALL;
9580 break;
9581 case 'u': /* ":ijump" and ":djump" */
9582 action = ACTION_GOTO;
9583 break;
9584 default: /* ":isplit" and ":dsplit" */
9585 action = ACTION_SPLIT;
9586 break;
9587 }
9588
9589 n = 1;
9590 if (vim_isdigit(*eap->arg)) /* get count */
9591 {
9592 n = getdigits(&eap->arg);
9593 eap->arg = skipwhite(eap->arg);
9594 }
9595 if (*eap->arg == '/') /* Match regexp, not just whole words */
9596 {
9597 whole = FALSE;
9598 ++eap->arg;
9599 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9600 if (*p)
9601 {
9602 *p++ = NUL;
9603 p = skipwhite(p);
9604
9605 /* Check for trailing illegal characters */
9606 if (!ends_excmd(*p))
9607 eap->errmsg = e_trailing;
9608 else
9609 eap->nextcmd = check_nextcmd(p);
9610 }
9611 }
9612 if (!eap->skip)
9613 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9614 whole, !eap->forceit,
9615 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9616 n, action, eap->line1, eap->line2);
9617}
9618#endif
9619
9620#ifdef FEAT_WINDOWS
9621
9622# ifdef FEAT_QUICKFIX
9623/*
9624 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9625 */
9626 static void
9627ex_ptag(eap)
9628 exarg_T *eap;
9629{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009630 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9632}
9633
9634/*
9635 * ":pedit"
9636 */
9637 static void
9638ex_pedit(eap)
9639 exarg_T *eap;
9640{
9641 win_T *curwin_save = curwin;
9642
9643 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009644 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 keep_help_flag = curwin_save->w_buffer->b_help;
9646 do_exedit(eap, NULL);
9647 keep_help_flag = FALSE;
9648 if (curwin != curwin_save && win_valid(curwin_save))
9649 {
9650 /* Return cursor to where we were */
9651 validate_cursor();
9652 redraw_later(VALID);
9653 win_enter(curwin_save, TRUE);
9654 }
9655 g_do_tagpreview = 0;
9656}
9657# endif
9658
9659/*
9660 * ":stag", ":stselect" and ":stjump".
9661 */
9662 static void
9663ex_stag(eap)
9664 exarg_T *eap;
9665{
9666 postponed_split = -1;
9667 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009668 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9670 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009671 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672}
9673#endif
9674
9675/*
9676 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9677 */
9678 static void
9679ex_tag(eap)
9680 exarg_T *eap;
9681{
9682 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9683}
9684
9685 static void
9686ex_tag_cmd(eap, name)
9687 exarg_T *eap;
9688 char_u *name;
9689{
9690 int cmd;
9691
9692 switch (name[1])
9693 {
9694 case 'j': cmd = DT_JUMP; /* ":tjump" */
9695 break;
9696 case 's': cmd = DT_SELECT; /* ":tselect" */
9697 break;
9698 case 'p': cmd = DT_PREV; /* ":tprevious" */
9699 break;
9700 case 'N': cmd = DT_PREV; /* ":tNext" */
9701 break;
9702 case 'n': cmd = DT_NEXT; /* ":tnext" */
9703 break;
9704 case 'o': cmd = DT_POP; /* ":pop" */
9705 break;
9706 case 'f': /* ":tfirst" */
9707 case 'r': cmd = DT_FIRST; /* ":trewind" */
9708 break;
9709 case 'l': cmd = DT_LAST; /* ":tlast" */
9710 break;
9711 default: /* ":tag" */
9712#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009713 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 {
9715 do_cstag(eap);
9716 return;
9717 }
9718#endif
9719 cmd = DT_TAG;
9720 break;
9721 }
9722
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009723 if (name[0] == 'l')
9724 {
9725#ifndef FEAT_QUICKFIX
9726 ex_ni(eap);
9727 return;
9728#else
9729 cmd = DT_LTAG;
9730#endif
9731 }
9732
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9734 eap->forceit, TRUE);
9735}
9736
9737/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009738 * Check "str" for starting with a special cmdline variable.
9739 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9740 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9741 */
9742 int
9743find_cmdline_var(src, usedlen)
9744 char_u *src;
9745 int *usedlen;
9746{
9747 int len;
9748 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009749 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009750 "%",
9751#define SPEC_PERC 0
9752 "#",
9753#define SPEC_HASH 1
9754 "<cword>", /* cursor word */
9755#define SPEC_CWORD 2
9756 "<cWORD>", /* cursor WORD */
9757#define SPEC_CCWORD 3
9758 "<cfile>", /* cursor path name */
9759#define SPEC_CFILE 4
9760 "<sfile>", /* ":so" file name */
9761#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009762 "<slnum>", /* ":so" file line number */
9763#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009764#ifdef FEAT_AUTOCMD
9765 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009766# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009767 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009768# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009769 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009770# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009771#endif
9772#ifdef FEAT_CLIENTSERVER
9773 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009774# ifdef FEAT_AUTOCMD
9775# define SPEC_CLIENT 10
9776# else
9777# define SPEC_CLIENT 7
9778# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009779#endif
9780 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009781
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009782 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009783 {
9784 len = (int)STRLEN(spec_str[i]);
9785 if (STRNCMP(src, spec_str[i], len) == 0)
9786 {
9787 *usedlen = len;
9788 return i;
9789 }
9790 }
9791 return -1;
9792}
9793
9794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 * Evaluate cmdline variables.
9796 *
9797 * change '%' to curbuf->b_ffname
9798 * '#' to curwin->w_altfile
9799 * '<cword>' to word under the cursor
9800 * '<cWORD>' to WORD under the cursor
9801 * '<cfile>' to path name under the cursor
9802 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009803 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 * '<afile>' to file name for autocommand
9805 * '<abuf>' to buffer number for autocommand
9806 * '<amatch>' to matching name for autocommand
9807 *
9808 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9809 * "" for error without a message) and NULL is returned.
9810 * Returns an allocated string if a valid match was found.
9811 * Returns NULL if no match was found. "usedlen" then still contains the
9812 * number of characters to skip.
9813 */
9814 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009815eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009817 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 int *usedlen; /* characters after src that are used */
9819 linenr_T *lnump; /* line number for :e command, or NULL */
9820 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009821 int *escaped; /* return value has escaped white space (can
9822 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823{
9824 int i;
9825 char_u *s;
9826 char_u *result;
9827 char_u *resultbuf = NULL;
9828 int resultlen;
9829 buf_T *buf;
9830 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9831 int spec_idx;
9832#ifdef FEAT_MODIFY_FNAME
9833 int skip_mod = FALSE;
9834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836
9837 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009838 if (escaped != NULL)
9839 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840
9841 /*
9842 * Check if there is something to do.
9843 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009844 spec_idx = find_cmdline_var(src, usedlen);
9845 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 {
9847 *usedlen = 1;
9848 return NULL;
9849 }
9850
9851 /*
9852 * Skip when preceded with a backslash "\%" and "\#".
9853 * Note: In "\\%" the % is also not recognized!
9854 */
9855 if (src > srcstart && src[-1] == '\\')
9856 {
9857 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009858 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 return NULL;
9860 }
9861
9862 /*
9863 * word or WORD under cursor
9864 */
9865 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9866 {
9867 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9868 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9869 if (resultlen == 0)
9870 {
9871 *errormsg = (char_u *)"";
9872 return NULL;
9873 }
9874 }
9875
9876 /*
9877 * '#': Alternate file name
9878 * '%': Current file name
9879 * File name under the cursor
9880 * File name for autocommand
9881 * and following modifiers
9882 */
9883 else
9884 {
9885 switch (spec_idx)
9886 {
9887 case SPEC_PERC: /* '%': current file */
9888 if (curbuf->b_fname == NULL)
9889 {
9890 result = (char_u *)"";
9891 valid = 0; /* Must have ":p:h" to be valid */
9892 }
9893 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 break;
9896
9897 case SPEC_HASH: /* '#' or "#99": alternate file */
9898 if (src[1] == '#') /* "##": the argument list */
9899 {
9900 result = arg_all();
9901 resultbuf = result;
9902 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009903 if (escaped != NULL)
9904 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905#ifdef FEAT_MODIFY_FNAME
9906 skip_mod = TRUE;
9907#endif
9908 break;
9909 }
9910 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009911 if (*s == '<') /* "#<99" uses v:oldfiles */
9912 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 i = (int)getdigits(&s);
9914 *usedlen = (int)(s - src); /* length of what we expand */
9915
Bram Moolenaard812df62008-11-09 12:46:09 +00009916 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009918 if (*usedlen < 2)
9919 {
9920 /* Should we give an error message for #<text? */
9921 *usedlen = 1;
9922 return NULL;
9923 }
9924#ifdef FEAT_EVAL
9925 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9926 (long)i);
9927 if (result == NULL)
9928 {
9929 *errormsg = (char_u *)"";
9930 return NULL;
9931 }
9932#else
9933 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 }
9937 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009938 {
9939 buf = buflist_findnr(i);
9940 if (buf == NULL)
9941 {
9942 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9943 return NULL;
9944 }
9945 if (lnump != NULL)
9946 *lnump = ECMD_LAST;
9947 if (buf->b_fname == NULL)
9948 {
9949 result = (char_u *)"";
9950 valid = 0; /* Must have ":p:h" to be valid */
9951 }
9952 else
9953 result = buf->b_fname;
9954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 break;
9956
9957#ifdef FEAT_SEARCHPATH
9958 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009959 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 if (result == NULL)
9961 {
9962 *errormsg = (char_u *)"";
9963 return NULL;
9964 }
9965 resultbuf = result; /* remember allocated string */
9966 break;
9967#endif
9968
9969#ifdef FEAT_AUTOCMD
9970 case SPEC_AFILE: /* file name for autocommand */
9971 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009972 if (result != NULL && !autocmd_fname_full)
9973 {
9974 /* Still need to turn the fname into a full path. It is
9975 * postponed to avoid a delay when <afile> is not used. */
9976 autocmd_fname_full = TRUE;
9977 result = FullName_save(autocmd_fname, FALSE);
9978 vim_free(autocmd_fname);
9979 autocmd_fname = result;
9980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 if (result == NULL)
9982 {
9983 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9984 return NULL;
9985 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009986 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 break;
9988
9989 case SPEC_ABUF: /* buffer number for autocommand */
9990 if (autocmd_bufnr <= 0)
9991 {
9992 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9993 return NULL;
9994 }
9995 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9996 result = strbuf;
9997 break;
9998
9999 case SPEC_AMATCH: /* match name for autocommand */
10000 result = autocmd_match;
10001 if (result == NULL)
10002 {
10003 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10004 return NULL;
10005 }
10006 break;
10007
10008#endif
10009 case SPEC_SFILE: /* file name for ":so" command */
10010 result = sourcing_name;
10011 if (result == NULL)
10012 {
10013 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10014 return NULL;
10015 }
10016 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010017 case SPEC_SLNUM: /* line in file for ":so" command */
10018 if (sourcing_name == NULL || sourcing_lnum == 0)
10019 {
10020 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10021 return NULL;
10022 }
10023 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10024 result = strbuf;
10025 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026#if defined(FEAT_CLIENTSERVER)
10027 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010028 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10029 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 result = strbuf;
10031 break;
10032#endif
10033 }
10034
10035 resultlen = (int)STRLEN(result); /* length of new string */
10036 if (src[*usedlen] == '<') /* remove the file name extension */
10037 {
10038 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 resultlen = (int)(s - result);
10041 }
10042#ifdef FEAT_MODIFY_FNAME
10043 else if (!skip_mod)
10044 {
10045 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10046 &resultlen);
10047 if (result == NULL)
10048 {
10049 *errormsg = (char_u *)"";
10050 return NULL;
10051 }
10052 }
10053#endif
10054 }
10055
10056 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10057 {
10058 if (valid != VALID_HEAD + VALID_PATH)
10059 /* xgettext:no-c-format */
10060 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10061 else
10062 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10063 result = NULL;
10064 }
10065 else
10066 result = vim_strnsave(result, resultlen);
10067 vim_free(resultbuf);
10068 return result;
10069}
10070
10071/*
10072 * Concatenate all files in the argument list, separated by spaces, and return
10073 * it in one allocated string.
10074 * Spaces and backslashes in the file names are escaped with a backslash.
10075 * Returns NULL when out of memory.
10076 */
10077 static char_u *
10078arg_all()
10079{
10080 int len;
10081 int idx;
10082 char_u *retval = NULL;
10083 char_u *p;
10084
10085 /*
10086 * Do this loop two times:
10087 * first time: compute the total length
10088 * second time: concatenate the names
10089 */
10090 for (;;)
10091 {
10092 len = 0;
10093 for (idx = 0; idx < ARGCOUNT; ++idx)
10094 {
10095 p = alist_name(&ARGLIST[idx]);
10096 if (p != NULL)
10097 {
10098 if (len > 0)
10099 {
10100 /* insert a space in between names */
10101 if (retval != NULL)
10102 retval[len] = ' ';
10103 ++len;
10104 }
10105 for ( ; *p != NUL; ++p)
10106 {
10107 if (*p == ' ' || *p == '\\')
10108 {
10109 /* insert a backslash */
10110 if (retval != NULL)
10111 retval[len] = '\\';
10112 ++len;
10113 }
10114 if (retval != NULL)
10115 retval[len] = *p;
10116 ++len;
10117 }
10118 }
10119 }
10120
10121 /* second time: break here */
10122 if (retval != NULL)
10123 {
10124 retval[len] = NUL;
10125 break;
10126 }
10127
10128 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010129 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 if (retval == NULL)
10131 break;
10132 }
10133
10134 return retval;
10135}
10136
10137#if defined(FEAT_AUTOCMD) || defined(PROTO)
10138/*
10139 * Expand the <sfile> string in "arg".
10140 *
10141 * Returns an allocated string, or NULL for any error.
10142 */
10143 char_u *
10144expand_sfile(arg)
10145 char_u *arg;
10146{
10147 char_u *errormsg;
10148 int len;
10149 char_u *result;
10150 char_u *newres;
10151 char_u *repl;
10152 int srclen;
10153 char_u *p;
10154
10155 result = vim_strsave(arg);
10156 if (result == NULL)
10157 return NULL;
10158
10159 for (p = result; *p; )
10160 {
10161 if (STRNCMP(p, "<sfile>", 7) != 0)
10162 ++p;
10163 else
10164 {
10165 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010166 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 if (errormsg != NULL)
10168 {
10169 if (*errormsg)
10170 emsg(errormsg);
10171 vim_free(result);
10172 return NULL;
10173 }
10174 if (repl == NULL) /* no match (cannot happen) */
10175 {
10176 p += srclen;
10177 continue;
10178 }
10179 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10180 newres = alloc(len);
10181 if (newres == NULL)
10182 {
10183 vim_free(repl);
10184 vim_free(result);
10185 return NULL;
10186 }
10187 mch_memmove(newres, result, (size_t)(p - result));
10188 STRCPY(newres + (p - result), repl);
10189 len = (int)STRLEN(newres);
10190 STRCAT(newres, p + srclen);
10191 vim_free(repl);
10192 vim_free(result);
10193 result = newres;
10194 p = newres + len; /* continue after the match */
10195 }
10196 }
10197
10198 return result;
10199}
10200#endif
10201
10202#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010203static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10204 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10206static frame_T *ses_skipframe __ARGS((frame_T *fr));
10207static int ses_do_frame __ARGS((frame_T *fr));
10208static int ses_do_win __ARGS((win_T *wp));
10209static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10210static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10211static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10212
10213/*
10214 * Write openfile commands for the current buffers to an .exrc file.
10215 * Return FAIL on error, OK otherwise.
10216 */
10217 static int
10218makeopens(fd, dirnow)
10219 FILE *fd;
10220 char_u *dirnow; /* Current directory name */
10221{
10222 buf_T *buf;
10223 int only_save_windows = TRUE;
10224 int nr;
10225 int cnr = 1;
10226 int restore_size = TRUE;
10227 win_T *wp;
10228 char_u *sname;
10229 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010230 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010231 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010232 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010233 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010234 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235
10236 if (ssop_flags & SSOP_BUFFERS)
10237 only_save_windows = FALSE; /* Save ALL buffers */
10238
10239 /*
10240 * Begin by setting the this_session variable, and then other
10241 * sessionable variables.
10242 */
10243#ifdef FEAT_EVAL
10244 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10245 return FAIL;
10246 if (ssop_flags & SSOP_GLOBALS)
10247 if (store_session_globals(fd) == FAIL)
10248 return FAIL;
10249#endif
10250
10251 /*
10252 * Close all windows but one.
10253 */
10254 if (put_line(fd, "silent only") == FAIL)
10255 return FAIL;
10256
10257 /*
10258 * Now a :cd command to the session directory or the current directory
10259 */
10260 if (ssop_flags & SSOP_SESDIR)
10261 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010262 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10263 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 return FAIL;
10265 }
10266 else if (ssop_flags & SSOP_CURDIR)
10267 {
10268 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10269 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010270 || fputs("cd ", fd) < 0
10271 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10272 || put_eol(fd) == FAIL)
10273 {
10274 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 vim_free(sname);
10278 }
10279
10280 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010281 * If there is an empty, unnamed buffer we will wipe it out later.
10282 * Remember the buffer number.
10283 */
10284 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10285 return FAIL;
10286 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10287 return FAIL;
10288 if (put_line(fd, "endif") == FAIL)
10289 return FAIL;
10290
10291 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 * Now save the current files, current buffer first.
10293 */
10294 if (put_line(fd, "set shortmess=aoO") == FAIL)
10295 return FAIL;
10296
10297 /* Now put the other buffers into the buffer list */
10298 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10299 {
10300 if (!(only_save_windows && buf->b_nwindows == 0)
10301 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10302 && buf->b_fname != NULL
10303 && buf->b_p_bl)
10304 {
10305 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10306 : buf->b_wininfo->wi_fpos.lnum) < 0
10307 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10308 return FAIL;
10309 }
10310 }
10311
10312 /* the global argument list */
10313 if (ses_arglist(fd, "args", &global_alist.al_ga,
10314 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10315 return FAIL;
10316
10317 if (ssop_flags & SSOP_RESIZE)
10318 {
10319 /* Note: after the restore we still check it worked!*/
10320 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10321 || put_eol(fd) == FAIL)
10322 return FAIL;
10323 }
10324
10325#ifdef FEAT_GUI
10326 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10327 {
10328 int x, y;
10329
10330 if (gui_mch_get_winpos(&x, &y) == OK)
10331 {
10332 /* Note: after the restore we still check it worked!*/
10333 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10334 return FAIL;
10335 }
10336 }
10337#endif
10338
10339 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010340 * May repeat putting Windows for each tab, when "tabpages" is in
10341 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010342 * Don't use goto_tabpage(), it may change directory and trigger
10343 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010345 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010346 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010347 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010349 int need_tabnew = FALSE;
10350
Bram Moolenaar18144c82006-04-12 21:52:12 +000010351 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010353 tabpage_T *tp = find_tabpage(tabnr);
10354
10355 if (tp == NULL)
10356 break; /* done all tab pages */
10357 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010358 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010359 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010360 tab_topframe = topframe;
10361 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010362 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010363 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010364 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010365 tab_topframe = tp->tp_topframe;
10366 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010367 if (tabnr > 1)
10368 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370
Bram Moolenaar18144c82006-04-12 21:52:12 +000010371 /*
10372 * Before creating the window layout, try loading one file. If this
10373 * is aborted we don't end up with a number of useless windows.
10374 * This may have side effects! (e.g., compressed or network file).
10375 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010376 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010377 {
10378 if (ses_do_win(wp)
10379 && wp->w_buffer->b_ffname != NULL
10380 && !wp->w_buffer->b_help
10381#ifdef FEAT_QUICKFIX
10382 && !bt_nofile(wp->w_buffer)
10383#endif
10384 )
10385 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010386 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010387 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10388 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010389 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010390 if (!wp->w_arg_idx_invalid)
10391 edited_win = wp;
10392 break;
10393 }
10394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010396 /* If no file got edited create an empty tab page. */
10397 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10398 return FAIL;
10399
Bram Moolenaar18144c82006-04-12 21:52:12 +000010400 /*
10401 * Save current window layout.
10402 */
10403 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010405 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010407 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10408 return FAIL;
10409 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10410 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411
Bram Moolenaar18144c82006-04-12 21:52:12 +000010412 /*
10413 * Check if window sizes can be restored (no windows omitted).
10414 * Remember the window number of the current window after restoring.
10415 */
10416 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010417 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010418 {
10419 if (ses_do_win(wp))
10420 ++nr;
10421 else
10422 restore_size = FALSE;
10423 if (curwin == wp)
10424 cnr = nr;
10425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426
Bram Moolenaar18144c82006-04-12 21:52:12 +000010427 /* Go to the first window. */
10428 if (put_line(fd, "wincmd t") == FAIL)
10429 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010430
Bram Moolenaar18144c82006-04-12 21:52:12 +000010431 /*
10432 * If more than one window, see if sizes can be restored.
10433 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10434 * resized when moving between windows.
10435 * Do this before restoring the view, so that the topline and the
10436 * cursor can be set. This is done again below.
10437 */
10438 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10439 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010440 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010441 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442
Bram Moolenaar18144c82006-04-12 21:52:12 +000010443 /*
10444 * Restore the view of the window (options, file, cursor, etc.).
10445 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010446 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010447 {
10448 if (!ses_do_win(wp))
10449 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010450 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10451 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010452 return FAIL;
10453 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10454 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010455 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010456 }
10457
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010458 /* The argument index in the first tab page is zero, need to set it in
10459 * each window. For further tab pages it's the window where we do
10460 * "tabedit". */
10461 cur_arg_idx = next_arg_idx;
10462
Bram Moolenaar18144c82006-04-12 21:52:12 +000010463 /*
10464 * Restore cursor to the current window if it's not the first one.
10465 */
10466 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10467 || put_eol(fd) == FAIL))
10468 return FAIL;
10469
10470 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010471 * Restore window sizes again after jumping around in windows, because
10472 * the current window has a minimum size while others may not.
10473 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010474 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010475 return FAIL;
10476
Bram Moolenaar18144c82006-04-12 21:52:12 +000010477 /* Don't continue in another tab page when doing only the current one
10478 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010479 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010480 break;
10481 }
10482
10483 if (ssop_flags & SSOP_TABPAGES)
10484 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010485 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10486 || put_eol(fd) == FAIL)
10487 return FAIL;
10488 }
10489
Bram Moolenaar9c102382006-05-03 21:26:49 +000010490 /*
10491 * Wipe out an empty unnamed buffer we started in.
10492 */
10493 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10494 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010495 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010496 return FAIL;
10497 if (put_line(fd, "endif") == FAIL)
10498 return FAIL;
10499 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10500 return FAIL;
10501
10502 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10503 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10504 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10505 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506
10507 /*
10508 * Lastly, execute the x.vim file if it exists.
10509 */
10510 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10511 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010512 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 || put_line(fd, "endif") == FAIL)
10514 return FAIL;
10515
10516 return OK;
10517}
10518
10519 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010520ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 FILE *fd;
10522 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010523 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524{
10525 int n = 0;
10526 win_T *wp;
10527
10528 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10529 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010530 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 {
10532 if (!ses_do_win(wp))
10533 continue;
10534 ++n;
10535
10536 /* restore height when not full height */
10537 if (wp->w_height + wp->w_status_height < topframe->fr_height
10538 && (fprintf(fd,
10539 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10540 n, (long)wp->w_height, Rows / 2, Rows) < 0
10541 || put_eol(fd) == FAIL))
10542 return FAIL;
10543
10544 /* restore width when not full width */
10545 if (wp->w_width < Columns && (fprintf(fd,
10546 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10547 n, (long)wp->w_width, Columns / 2, Columns) < 0
10548 || put_eol(fd) == FAIL))
10549 return FAIL;
10550 }
10551 }
10552 else
10553 {
10554 /* Just equalise window sizes */
10555 if (put_line(fd, "wincmd =") == FAIL)
10556 return FAIL;
10557 }
10558 return OK;
10559}
10560
10561/*
10562 * Write commands to "fd" to recursively create windows for frame "fr",
10563 * horizontally and vertically split.
10564 * After the commands the last window in the frame is the current window.
10565 * Returns FAIL when writing the commands to "fd" fails.
10566 */
10567 static int
10568ses_win_rec(fd, fr)
10569 FILE *fd;
10570 frame_T *fr;
10571{
10572 frame_T *frc;
10573 int count = 0;
10574
10575 if (fr->fr_layout != FR_LEAF)
10576 {
10577 /* Find first frame that's not skipped and then create a window for
10578 * each following one (first frame is already there). */
10579 frc = ses_skipframe(fr->fr_child);
10580 if (frc != NULL)
10581 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10582 {
10583 /* Make window as big as possible so that we have lots of room
10584 * to split. */
10585 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10586 || put_line(fd, fr->fr_layout == FR_COL
10587 ? "split" : "vsplit") == FAIL)
10588 return FAIL;
10589 ++count;
10590 }
10591
10592 /* Go back to the first window. */
10593 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10594 ? "%dwincmd k" : "%dwincmd h", count) < 0
10595 || put_eol(fd) == FAIL))
10596 return FAIL;
10597
10598 /* Recursively create frames/windows in each window of this column or
10599 * row. */
10600 frc = ses_skipframe(fr->fr_child);
10601 while (frc != NULL)
10602 {
10603 ses_win_rec(fd, frc);
10604 frc = ses_skipframe(frc->fr_next);
10605 /* Go to next window. */
10606 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10607 return FAIL;
10608 }
10609 }
10610 return OK;
10611}
10612
10613/*
10614 * Skip frames that don't contain windows we want to save in the Session.
10615 * Returns NULL when there none.
10616 */
10617 static frame_T *
10618ses_skipframe(fr)
10619 frame_T *fr;
10620{
10621 frame_T *frc;
10622
10623 for (frc = fr; frc != NULL; frc = frc->fr_next)
10624 if (ses_do_frame(frc))
10625 break;
10626 return frc;
10627}
10628
10629/*
10630 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10631 * the Session.
10632 */
10633 static int
10634ses_do_frame(fr)
10635 frame_T *fr;
10636{
10637 frame_T *frc;
10638
10639 if (fr->fr_layout == FR_LEAF)
10640 return ses_do_win(fr->fr_win);
10641 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10642 if (ses_do_frame(frc))
10643 return TRUE;
10644 return FALSE;
10645}
10646
10647/*
10648 * Return non-zero if window "wp" is to be stored in the Session.
10649 */
10650 static int
10651ses_do_win(wp)
10652 win_T *wp;
10653{
10654 if (wp->w_buffer->b_fname == NULL
10655#ifdef FEAT_QUICKFIX
10656 /* When 'buftype' is "nofile" can't restore the window contents. */
10657 || bt_nofile(wp->w_buffer)
10658#endif
10659 )
10660 return (ssop_flags & SSOP_BLANK);
10661 if (wp->w_buffer->b_help)
10662 return (ssop_flags & SSOP_HELP);
10663 return TRUE;
10664}
10665
10666/*
10667 * Write commands to "fd" to restore the view of a window.
10668 * Caller must make sure 'scrolloff' is zero.
10669 */
10670 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010671put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 FILE *fd;
10673 win_T *wp;
10674 int add_edit; /* add ":edit" command to view */
10675 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010676 int current_arg_idx; /* current argument index of the window, use
10677 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678{
10679 win_T *save_curwin;
10680 int f;
10681 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010682 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683
10684 /* Always restore cursor position for ":mksession". For ":mkview" only
10685 * when 'viewoptions' contains "cursor". */
10686 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10687
10688 /*
10689 * Local argument list.
10690 */
10691 if (wp->w_alist == &global_alist)
10692 {
10693 if (put_line(fd, "argglobal") == FAIL)
10694 return FAIL;
10695 }
10696 else
10697 {
10698 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10699 flagp == &vop_flags
10700 || !(*flagp & SSOP_CURDIR)
10701 || wp->w_localdir != NULL, flagp) == FAIL)
10702 return FAIL;
10703 }
10704
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010705 /* Only when part of a session: restore the argument index. Some
10706 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010707 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010708 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010710 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 || put_eol(fd) == FAIL)
10712 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010713 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 }
10715
10716 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010717 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 {
10719 /*
10720 * Load the file.
10721 */
10722 if (wp->w_buffer->b_ffname != NULL
10723#ifdef FEAT_QUICKFIX
10724 && !bt_nofile(wp->w_buffer)
10725#endif
10726 )
10727 {
10728 /*
10729 * Editing a file in this buffer: use ":edit file".
10730 * This may have side effects! (e.g., compressed or network file).
10731 */
10732 if (fputs("edit ", fd) < 0
10733 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10734 return FAIL;
10735 }
10736 else
10737 {
10738 /* No file in this buffer, just make it empty. */
10739 if (put_line(fd, "enew") == FAIL)
10740 return FAIL;
10741#ifdef FEAT_QUICKFIX
10742 if (wp->w_buffer->b_ffname != NULL)
10743 {
10744 /* The buffer does have a name, but it's not a file name. */
10745 if (fputs("file ", fd) < 0
10746 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10747 return FAIL;
10748 }
10749#endif
10750 do_cursor = FALSE;
10751 }
10752 }
10753
10754 /*
10755 * Local mappings and abbreviations.
10756 */
10757 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10758 && makemap(fd, wp->w_buffer) == FAIL)
10759 return FAIL;
10760
10761 /*
10762 * Local options. Need to go to the window temporarily.
10763 * Store only local values when using ":mkview" and when ":mksession" is
10764 * used and 'sessionoptions' doesn't include "options".
10765 * Some folding options are always stored when "folds" is included,
10766 * otherwise the folds would not be restored correctly.
10767 */
10768 save_curwin = curwin;
10769 curwin = wp;
10770 curbuf = curwin->w_buffer;
10771 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10772 f = makeset(fd, OPT_LOCAL,
10773 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10774#ifdef FEAT_FOLDING
10775 else if (*flagp & SSOP_FOLDS)
10776 f = makefoldset(fd);
10777#endif
10778 else
10779 f = OK;
10780 curwin = save_curwin;
10781 curbuf = curwin->w_buffer;
10782 if (f == FAIL)
10783 return FAIL;
10784
10785#ifdef FEAT_FOLDING
10786 /*
10787 * Save Folds when 'buftype' is empty and for help files.
10788 */
10789 if ((*flagp & SSOP_FOLDS)
10790 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010791# ifdef FEAT_QUICKFIX
10792 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10793# endif
10794 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 {
10796 if (put_folds(fd, wp) == FAIL)
10797 return FAIL;
10798 }
10799#endif
10800
10801 /*
10802 * Set the cursor after creating folds, since that moves the cursor.
10803 */
10804 if (do_cursor)
10805 {
10806
10807 /* Restore the cursor line in the file and relatively in the
10808 * window. Don't use "G", it changes the jumplist. */
10809 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10810 (long)wp->w_cursor.lnum,
10811 (long)(wp->w_cursor.lnum - wp->w_topline),
10812 (long)wp->w_height / 2, (long)wp->w_height) < 0
10813 || put_eol(fd) == FAIL
10814 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10815 || put_line(fd, "exe s:l") == FAIL
10816 || put_line(fd, "normal! zt") == FAIL
10817 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10818 || put_eol(fd) == FAIL)
10819 return FAIL;
10820 /* Restore the cursor column and left offset when not wrapping. */
10821 if (wp->w_cursor.col == 0)
10822 {
10823 if (put_line(fd, "normal! 0") == FAIL)
10824 return FAIL;
10825 }
10826 else
10827 {
10828 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10829 {
10830 if (fprintf(fd,
10831 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010832 (long)wp->w_virtcol + 1,
10833 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 (long)wp->w_width / 2, (long)wp->w_width) < 0
10835 || put_eol(fd) == FAIL
10836 || put_line(fd, "if s:c > 0") == FAIL
10837 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010838 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10839 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 || put_eol(fd) == FAIL
10841 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010010842 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 || put_eol(fd) == FAIL
10844 || put_line(fd, "endif") == FAIL)
10845 return FAIL;
10846 }
10847 else
10848 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010849 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 || put_eol(fd) == FAIL)
10851 return FAIL;
10852 }
10853 }
10854 }
10855
10856 /*
10857 * Local directory.
10858 */
10859 if (wp->w_localdir != NULL)
10860 {
10861 if (fputs("lcd ", fd) < 0
10862 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10863 || put_eol(fd) == FAIL)
10864 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010865 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 }
10867
10868 return OK;
10869}
10870
10871/*
10872 * Write an argument list to the session file.
10873 * Returns FAIL if writing fails.
10874 */
10875 static int
10876ses_arglist(fd, cmd, gap, fullname, flagp)
10877 FILE *fd;
10878 char *cmd;
10879 garray_T *gap;
10880 int fullname; /* TRUE: use full path name */
10881 unsigned *flagp;
10882{
10883 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010884 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 char_u *s;
10886
10887 if (gap->ga_len == 0)
10888 return put_line(fd, "silent! argdel *");
10889 if (fputs(cmd, fd) < 0)
10890 return FAIL;
10891 for (i = 0; i < gap->ga_len; ++i)
10892 {
10893 /* NULL file names are skipped (only happens when out of memory). */
10894 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10895 if (s != NULL)
10896 {
10897 if (fullname)
10898 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010899 buf = alloc(MAXPATHL);
10900 if (buf != NULL)
10901 {
10902 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10903 s = buf;
10904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 }
10906 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010907 {
10908 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010910 }
10911 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 }
10913 }
10914 return put_eol(fd);
10915}
10916
10917/*
10918 * Write a buffer name to the session file.
10919 * Also ends the line.
10920 * Returns FAIL if writing fails.
10921 */
10922 static int
10923ses_fname(fd, buf, flagp)
10924 FILE *fd;
10925 buf_T *buf;
10926 unsigned *flagp;
10927{
10928 char_u *name;
10929
10930 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010931 * the session file will be sourced.
10932 * Don't do this for ":mkview", we don't know the current directory.
10933 * Don't do this after ":lcd", we don't keep track of what the current
10934 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 if (buf->b_sfname != NULL
10936 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010937 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010938#ifdef FEAT_AUTOCHDIR
10939 && !p_acd
10940#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010941 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 name = buf->b_sfname;
10943 else
10944 name = buf->b_ffname;
10945 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10946 return FAIL;
10947 return OK;
10948}
10949
10950/*
10951 * Write a file name to the session file.
10952 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10953 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010954 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 */
10956 static int
10957ses_put_fname(fd, name, flagp)
10958 FILE *fd;
10959 char_u *name;
10960 unsigned *flagp;
10961{
10962 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010963 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965
10966 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010967 if (sname == NULL)
10968 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010970 if (*flagp & SSOP_SLASH)
10971 {
10972 /* change all backslashes to forward slashes */
10973 for (p = sname; *p != NUL; mb_ptr_adv(p))
10974 if (*p == '\\')
10975 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010977
10978 /* escapse special characters */
10979 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010981 if (p == NULL)
10982 return FAIL;
10983
10984 /* write the result */
10985 if (fputs((char *)p, fd) < 0)
10986 retval = FAIL;
10987
10988 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 return retval;
10990}
10991
10992/*
10993 * ":loadview [nr]"
10994 */
10995 static void
10996ex_loadview(eap)
10997 exarg_T *eap;
10998{
10999 char_u *fname;
11000
11001 fname = get_view_file(*eap->arg);
11002 if (fname != NULL)
11003 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011004 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 vim_free(fname);
11006 }
11007}
11008
11009/*
11010 * Get the name of the view file for the current buffer.
11011 */
11012 static char_u *
11013get_view_file(c)
11014 int c;
11015{
11016 int len = 0;
11017 char_u *p, *s;
11018 char_u *retval;
11019 char_u *sname;
11020
11021 if (curbuf->b_ffname == NULL)
11022 {
11023 EMSG(_(e_noname));
11024 return NULL;
11025 }
11026 sname = home_replace_save(NULL, curbuf->b_ffname);
11027 if (sname == NULL)
11028 return NULL;
11029
11030 /*
11031 * We want a file name without separators, because we're not going to make
11032 * a directory.
11033 * "normal" path separator -> "=+"
11034 * "=" -> "=="
11035 * ":" path separator -> "=-"
11036 */
11037 for (p = sname; *p; ++p)
11038 if (*p == '=' || vim_ispathsep(*p))
11039 ++len;
11040 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11041 if (retval != NULL)
11042 {
11043 STRCPY(retval, p_vdir);
11044 add_pathsep(retval);
11045 s = retval + STRLEN(retval);
11046 for (p = sname; *p; ++p)
11047 {
11048 if (*p == '=')
11049 {
11050 *s++ = '=';
11051 *s++ = '=';
11052 }
11053 else if (vim_ispathsep(*p))
11054 {
11055 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011056#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 if (*p == ':')
11058 *s++ = '-';
11059 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011061 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 }
11063 else
11064 *s++ = *p;
11065 }
11066 *s++ = '=';
11067 *s++ = c;
11068 STRCPY(s, ".vim");
11069 }
11070
11071 vim_free(sname);
11072 return retval;
11073}
11074
11075#endif /* FEAT_SESSION */
11076
11077/*
11078 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11079 * Return FAIL for a write error.
11080 */
11081 int
11082put_eol(fd)
11083 FILE *fd;
11084{
11085 if (
11086#ifdef USE_CRNL
11087 (
11088# ifdef MKSESSION_NL
11089 !mksession_nl &&
11090# endif
11091 (putc('\r', fd) < 0)) ||
11092#endif
11093 (putc('\n', fd) < 0))
11094 return FAIL;
11095 return OK;
11096}
11097
11098/*
11099 * Write a line to "fd".
11100 * Return FAIL for a write error.
11101 */
11102 int
11103put_line(fd, s)
11104 FILE *fd;
11105 char *s;
11106{
11107 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11108 return FAIL;
11109 return OK;
11110}
11111
11112#ifdef FEAT_VIMINFO
11113/*
11114 * ":rviminfo" and ":wviminfo".
11115 */
11116 static void
11117ex_viminfo(eap)
11118 exarg_T *eap;
11119{
11120 char_u *save_viminfo;
11121
11122 save_viminfo = p_viminfo;
11123 if (*p_viminfo == NUL)
11124 p_viminfo = (char_u *)"'100";
11125 if (eap->cmdidx == CMD_rviminfo)
11126 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011127 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11128 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 EMSG(_("E195: Cannot open viminfo file for reading"));
11130 }
11131 else
11132 write_viminfo(eap->arg, eap->forceit);
11133 p_viminfo = save_viminfo;
11134}
11135#endif
11136
11137#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011138/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011139 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011140 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 void
11143dialog_msg(buff, format, fname)
11144 char_u *buff;
11145 char *format;
11146 char_u *fname;
11147{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 if (fname == NULL)
11149 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011150 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151}
11152#endif
11153
11154/*
11155 * ":behave {mswin,xterm}"
11156 */
11157 static void
11158ex_behave(eap)
11159 exarg_T *eap;
11160{
11161 if (STRCMP(eap->arg, "mswin") == 0)
11162 {
11163 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11164 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11165 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11166 set_option_value((char_u *)"keymodel", 0L,
11167 (char_u *)"startsel,stopsel", 0);
11168 }
11169 else if (STRCMP(eap->arg, "xterm") == 0)
11170 {
11171 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11172 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11173 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11174 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11175 }
11176 else
11177 EMSG2(_(e_invarg2), eap->arg);
11178}
11179
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011180#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11181/*
11182 * Function given to ExpandGeneric() to obtain the possible arguments of the
11183 * ":behave {mswin,xterm}" command.
11184 */
11185 char_u *
11186get_behave_arg(xp, idx)
11187 expand_T *xp UNUSED;
11188 int idx;
11189{
11190 if (idx == 0)
11191 return (char_u *)"mswin";
11192 if (idx == 1)
11193 return (char_u *)"xterm";
11194 return NULL;
11195}
11196#endif
11197
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198#ifdef FEAT_AUTOCMD
11199static int filetype_detect = FALSE;
11200static int filetype_plugin = FALSE;
11201static int filetype_indent = FALSE;
11202
11203/*
11204 * ":filetype [plugin] [indent] {on,off,detect}"
11205 * on: Load the filetype.vim file to install autocommands for file types.
11206 * off: Load the ftoff.vim file to remove all autocommands for file types.
11207 * plugin on: load filetype.vim and ftplugin.vim
11208 * plugin off: load ftplugof.vim
11209 * indent on: load filetype.vim and indent.vim
11210 * indent off: load indoff.vim
11211 */
11212 static void
11213ex_filetype(eap)
11214 exarg_T *eap;
11215{
11216 char_u *arg = eap->arg;
11217 int plugin = FALSE;
11218 int indent = FALSE;
11219
11220 if (*eap->arg == NUL)
11221 {
11222 /* Print current status. */
11223 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11224 filetype_detect ? "ON" : "OFF",
11225 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11226 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11227 return;
11228 }
11229
11230 /* Accept "plugin" and "indent" in any order. */
11231 for (;;)
11232 {
11233 if (STRNCMP(arg, "plugin", 6) == 0)
11234 {
11235 plugin = TRUE;
11236 arg = skipwhite(arg + 6);
11237 continue;
11238 }
11239 if (STRNCMP(arg, "indent", 6) == 0)
11240 {
11241 indent = TRUE;
11242 arg = skipwhite(arg + 6);
11243 continue;
11244 }
11245 break;
11246 }
11247 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11248 {
11249 if (*arg == 'o' || !filetype_detect)
11250 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011251 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 filetype_detect = TRUE;
11253 if (plugin)
11254 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011255 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 filetype_plugin = TRUE;
11257 }
11258 if (indent)
11259 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011260 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 filetype_indent = TRUE;
11262 }
11263 }
11264 if (*arg == 'd')
11265 {
11266 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011267 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 }
11269 }
11270 else if (STRCMP(arg, "off") == 0)
11271 {
11272 if (plugin || indent)
11273 {
11274 if (plugin)
11275 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011276 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 filetype_plugin = FALSE;
11278 }
11279 if (indent)
11280 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011281 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 filetype_indent = FALSE;
11283 }
11284 }
11285 else
11286 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011287 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288 filetype_detect = FALSE;
11289 }
11290 }
11291 else
11292 EMSG2(_(e_invarg2), arg);
11293}
11294
11295/*
11296 * ":setfiletype {name}"
11297 */
11298 static void
11299ex_setfiletype(eap)
11300 exarg_T *eap;
11301{
11302 if (!did_filetype)
11303 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11304}
11305#endif
11306
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 static void
11308ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011309 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310{
11311#ifdef FEAT_DIGRAPHS
11312 if (*eap->arg != NUL)
11313 putdigraph(eap->arg);
11314 else
11315 listdigraphs();
11316#else
11317 EMSG(_("E196: No digraphs in this version"));
11318#endif
11319}
11320
11321 static void
11322ex_set(eap)
11323 exarg_T *eap;
11324{
11325 int flags = 0;
11326
11327 if (eap->cmdidx == CMD_setlocal)
11328 flags = OPT_LOCAL;
11329 else if (eap->cmdidx == CMD_setglobal)
11330 flags = OPT_GLOBAL;
11331#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11332 if (cmdmod.browse && flags == 0)
11333 ex_options(eap);
11334 else
11335#endif
11336 (void)do_set(eap->arg, flags);
11337}
11338
11339#ifdef FEAT_SEARCH_EXTRA
11340/*
11341 * ":nohlsearch"
11342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 static void
11344ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011345 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346{
11347 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011348 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349}
11350
11351/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011352 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 * Sets nextcmd to the start of the next command, if any. Also called when
11354 * skipping commands to find the next command.
11355 */
11356 static void
11357ex_match(eap)
11358 exarg_T *eap;
11359{
11360 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011361 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 char_u *end;
11363 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011364 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011365
11366 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011367 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011368 else
11369 {
11370 EMSG(e_invcmd);
11371 return;
11372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373
11374 /* First clear any old pattern. */
11375 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011376 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377
11378 if (ends_excmd(*eap->arg))
11379 end = eap->arg;
11380 else if ((STRNICMP(eap->arg, "none", 4) == 0
11381 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11382 end = eap->arg + 4;
11383 else
11384 {
11385 p = skiptowhite(eap->arg);
11386 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011387 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 p = skipwhite(p);
11389 if (*p == NUL)
11390 {
11391 /* There must be two arguments. */
11392 EMSG2(_(e_invarg2), eap->arg);
11393 return;
11394 }
11395 end = skip_regexp(p + 1, *p, TRUE, NULL);
11396 if (!eap->skip)
11397 {
11398 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11399 {
11400 eap->errmsg = e_trailing;
11401 return;
11402 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011403 if (*end != *p)
11404 {
11405 EMSG2(_(e_invarg2), p);
11406 return;
11407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408
11409 c = *end;
11410 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011411 match_add(curwin, g, p + 1, 10, id);
11412 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011413 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 }
11415 }
11416 eap->nextcmd = find_nextcmd(end);
11417}
11418#endif
11419
11420#ifdef FEAT_CRYPT
11421/*
11422 * ":X": Get crypt key
11423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 static void
11425ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011426 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011428 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011429 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430}
11431#endif
11432
11433#ifdef FEAT_FOLDING
11434 static void
11435ex_fold(eap)
11436 exarg_T *eap;
11437{
11438 if (foldManualAllowed(TRUE))
11439 foldCreate(eap->line1, eap->line2);
11440}
11441
11442 static void
11443ex_foldopen(eap)
11444 exarg_T *eap;
11445{
11446 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11447 eap->forceit, FALSE);
11448}
11449
11450 static void
11451ex_folddo(eap)
11452 exarg_T *eap;
11453{
11454 linenr_T lnum;
11455
11456 /* First set the marks for all lines closed/open. */
11457 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11458 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11459 ml_setmarked(lnum);
11460
11461 /* Execute the command on the marked lines. */
11462 global_exe(eap->arg);
11463 ml_clearmarked(); /* clear rest of the marks */
11464}
11465#endif